]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/message/fusion/mptbase.c
d890b2b8a93e94973f345068f95b75e9121b679d
[mv-sheeva.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 Logic PCI chip/adapter(s)
6  *      running LSI Logic Fusion MPT (Message Passing Technology) firmware.
7  *
8  *  Copyright (c) 1999-2005 LSI Logic Corporation
9  *  (mailto:mpt_linux_developer@lsil.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/config.h>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/errno.h>
53 #include <linux/init.h>
54 #include <linux/slab.h>
55 #include <linux/types.h>
56 #include <linux/pci.h>
57 #include <linux/kdev_t.h>
58 #include <linux/blkdev.h>
59 #include <linux/delay.h>
60 #include <linux/interrupt.h>            /* needed for in_interrupt() proto */
61 #include <linux/dma-mapping.h>
62 #include <asm/io.h>
63 #ifdef CONFIG_MTRR
64 #include <asm/mtrr.h>
65 #endif
66 #ifdef __sparc__
67 #include <asm/irq.h>                    /* needed for __irq_itoa() proto */
68 #endif
69
70 #include "mptbase.h"
71
72 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
73 #define my_NAME         "Fusion MPT base driver"
74 #define my_VERSION      MPT_LINUX_VERSION_COMMON
75 #define MYNAM           "mptbase"
76
77 MODULE_AUTHOR(MODULEAUTHOR);
78 MODULE_DESCRIPTION(my_NAME);
79 MODULE_LICENSE("GPL");
80
81 /*
82  *  cmd line parameters
83  */
84 #ifdef MFCNT
85 static int mfcounter = 0;
86 #define PRINT_MF_COUNT 20000
87 #endif
88
89 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
90 /*
91  *  Public data...
92  */
93 int mpt_lan_index = -1;
94 int mpt_stm_index = -1;
95
96 struct proc_dir_entry *mpt_proc_root_dir;
97
98 #define WHOINIT_UNKNOWN         0xAA
99
100 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
101 /*
102  *  Private data...
103  */
104                                         /* Adapter link list */
105 LIST_HEAD(ioc_list);
106                                         /* Callback lookup table */
107 static MPT_CALLBACK              MptCallbacks[MPT_MAX_PROTOCOL_DRIVERS];
108                                         /* Protocol driver class lookup table */
109 static int                       MptDriverClass[MPT_MAX_PROTOCOL_DRIVERS];
110                                         /* Event handler lookup table */
111 static MPT_EVHANDLER             MptEvHandlers[MPT_MAX_PROTOCOL_DRIVERS];
112                                         /* Reset handler lookup table */
113 static MPT_RESETHANDLER          MptResetHandlers[MPT_MAX_PROTOCOL_DRIVERS];
114 static struct mpt_pci_driver    *MptDeviceDriverHandlers[MPT_MAX_PROTOCOL_DRIVERS];
115
116 static int      mpt_base_index = -1;
117 static int      last_drv_idx = -1;
118
119 static DECLARE_WAIT_QUEUE_HEAD(mpt_waitq);
120
121 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
122 /*
123  *  Forward protos...
124  */
125 static irqreturn_t mpt_interrupt(int irq, void *bus_id, struct pt_regs *r);
126 static int      mpt_base_reply(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req, MPT_FRAME_HDR *reply);
127 static int      mpt_handshake_req_reply_wait(MPT_ADAPTER *ioc, int reqBytes,
128                         u32 *req, int replyBytes, u16 *u16reply, int maxwait,
129                         int sleepFlag);
130 static int      mpt_do_ioc_recovery(MPT_ADAPTER *ioc, u32 reason, int sleepFlag);
131 static void     mpt_detect_bound_ports(MPT_ADAPTER *ioc, struct pci_dev *pdev);
132 static void     mpt_adapter_disable(MPT_ADAPTER *ioc);
133 static void     mpt_adapter_dispose(MPT_ADAPTER *ioc);
134
135 static void     MptDisplayIocCapabilities(MPT_ADAPTER *ioc);
136 static int      MakeIocReady(MPT_ADAPTER *ioc, int force, int sleepFlag);
137 static int      GetIocFacts(MPT_ADAPTER *ioc, int sleepFlag, int reason);
138 static int      GetPortFacts(MPT_ADAPTER *ioc, int portnum, int sleepFlag);
139 static int      SendIocInit(MPT_ADAPTER *ioc, int sleepFlag);
140 static int      SendPortEnable(MPT_ADAPTER *ioc, int portnum, int sleepFlag);
141 static int      mpt_do_upload(MPT_ADAPTER *ioc, int sleepFlag);
142 static int      mpt_downloadboot(MPT_ADAPTER *ioc, MpiFwHeader_t *pFwHeader, int sleepFlag);
143 static int      mpt_diag_reset(MPT_ADAPTER *ioc, int ignore, int sleepFlag);
144 static int      KickStart(MPT_ADAPTER *ioc, int ignore, int sleepFlag);
145 static int      SendIocReset(MPT_ADAPTER *ioc, u8 reset_type, int sleepFlag);
146 static int      PrimeIocFifos(MPT_ADAPTER *ioc);
147 static int      WaitForDoorbellAck(MPT_ADAPTER *ioc, int howlong, int sleepFlag);
148 static int      WaitForDoorbellInt(MPT_ADAPTER *ioc, int howlong, int sleepFlag);
149 static int      WaitForDoorbellReply(MPT_ADAPTER *ioc, int howlong, int sleepFlag);
150 static int      GetLanConfigPages(MPT_ADAPTER *ioc);
151 static int      GetIoUnitPage2(MPT_ADAPTER *ioc);
152 int             mptbase_sas_persist_operation(MPT_ADAPTER *ioc, u8 persist_opcode);
153 static int      mpt_GetScsiPortSettings(MPT_ADAPTER *ioc, int portnum);
154 static int      mpt_readScsiDevicePageHeaders(MPT_ADAPTER *ioc, int portnum);
155 static void     mpt_read_ioc_pg_1(MPT_ADAPTER *ioc);
156 static void     mpt_read_ioc_pg_4(MPT_ADAPTER *ioc);
157 static void     mpt_timer_expired(unsigned long data);
158 static int      SendEventNotification(MPT_ADAPTER *ioc, u8 EvSwitch);
159 static int      SendEventAck(MPT_ADAPTER *ioc, EventNotificationReply_t *evnp);
160 static int      mpt_host_page_access_control(MPT_ADAPTER *ioc, u8 access_control_value, int sleepFlag);
161 static int      mpt_host_page_alloc(MPT_ADAPTER *ioc, pIOCInit_t ioc_init);
162
163 #ifdef CONFIG_PROC_FS
164 static int      procmpt_summary_read(char *buf, char **start, off_t offset,
165                                 int request, int *eof, void *data);
166 static int      procmpt_version_read(char *buf, char **start, off_t offset,
167                                 int request, int *eof, void *data);
168 static int      procmpt_iocinfo_read(char *buf, char **start, off_t offset,
169                                 int request, int *eof, void *data);
170 #endif
171 static void     mpt_get_fw_exp_ver(char *buf, MPT_ADAPTER *ioc);
172
173 //int           mpt_HardResetHandler(MPT_ADAPTER *ioc, int sleepFlag);
174 static int      ProcessEventNotification(MPT_ADAPTER *ioc, EventNotificationReply_t *evReply, int *evHandlers);
175 static void     mpt_sp_ioc_info(MPT_ADAPTER *ioc, u32 ioc_status, MPT_FRAME_HDR *mf);
176 static void     mpt_fc_log_info(MPT_ADAPTER *ioc, u32 log_info);
177 static void     mpt_sp_log_info(MPT_ADAPTER *ioc, u32 log_info);
178 static void     mpt_sas_log_info(MPT_ADAPTER *ioc, u32 log_info);
179
180 /* module entry point */
181 static int  __init    fusion_init  (void);
182 static void __exit    fusion_exit  (void);
183
184 #define CHIPREG_READ32(addr)            readl_relaxed(addr)
185 #define CHIPREG_READ32_dmasync(addr)    readl(addr)
186 #define CHIPREG_WRITE32(addr,val)       writel(val, addr)
187 #define CHIPREG_PIO_WRITE32(addr,val)   outl(val, (unsigned long)addr)
188 #define CHIPREG_PIO_READ32(addr)        inl((unsigned long)addr)
189
190 static void
191 pci_disable_io_access(struct pci_dev *pdev)
192 {
193         u16 command_reg;
194
195         pci_read_config_word(pdev, PCI_COMMAND, &command_reg);
196         command_reg &= ~1;
197         pci_write_config_word(pdev, PCI_COMMAND, command_reg);
198 }
199
200 static void
201 pci_enable_io_access(struct pci_dev *pdev)
202 {
203         u16 command_reg;
204
205         pci_read_config_word(pdev, PCI_COMMAND, &command_reg);
206         command_reg |= 1;
207         pci_write_config_word(pdev, PCI_COMMAND, command_reg);
208 }
209
210 /*
211  *  Process turbo (context) reply...
212  */
213 static void
214 mpt_turbo_reply(MPT_ADAPTER *ioc, u32 pa)
215 {
216         MPT_FRAME_HDR *mf = NULL;
217         MPT_FRAME_HDR *mr = NULL;
218         int req_idx = 0;
219         int cb_idx;
220
221         dmfprintk((MYIOC_s_INFO_FMT "Got TURBO reply req_idx=%08x\n",
222                                 ioc->name, pa));
223
224         switch (pa >> MPI_CONTEXT_REPLY_TYPE_SHIFT) {
225         case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
226                 req_idx = pa & 0x0000FFFF;
227                 cb_idx = (pa & 0x00FF0000) >> 16;
228                 mf = MPT_INDEX_2_MFPTR(ioc, req_idx);
229                 break;
230         case MPI_CONTEXT_REPLY_TYPE_LAN:
231                 cb_idx = mpt_lan_index;
232                 /*
233                  *  Blind set of mf to NULL here was fatal
234                  *  after lan_reply says "freeme"
235                  *  Fix sort of combined with an optimization here;
236                  *  added explicit check for case where lan_reply
237                  *  was just returning 1 and doing nothing else.
238                  *  For this case skip the callback, but set up
239                  *  proper mf value first here:-)
240                  */
241                 if ((pa & 0x58000000) == 0x58000000) {
242                         req_idx = pa & 0x0000FFFF;
243                         mf = MPT_INDEX_2_MFPTR(ioc, req_idx);
244                         mpt_free_msg_frame(ioc, mf);
245                         mb();
246                         return;
247                         break;
248                 }
249                 mr = (MPT_FRAME_HDR *) CAST_U32_TO_PTR(pa);
250                 break;
251         case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
252                 cb_idx = mpt_stm_index;
253                 mr = (MPT_FRAME_HDR *) CAST_U32_TO_PTR(pa);
254                 break;
255         default:
256                 cb_idx = 0;
257                 BUG();
258         }
259
260         /*  Check for (valid) IO callback!  */
261         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS ||
262                         MptCallbacks[cb_idx] == NULL) {
263                 printk(MYIOC_s_WARN_FMT "%s: Invalid cb_idx (%d)!\n",
264                                 __FUNCTION__, ioc->name, cb_idx);
265                 goto out;
266         }
267
268         if (MptCallbacks[cb_idx](ioc, mf, mr))
269                 mpt_free_msg_frame(ioc, mf);
270  out:
271         mb();
272 }
273
274 static void
275 mpt_reply(MPT_ADAPTER *ioc, u32 pa)
276 {
277         MPT_FRAME_HDR   *mf;
278         MPT_FRAME_HDR   *mr;
279         int              req_idx;
280         int              cb_idx;
281         int              freeme;
282
283         u32 reply_dma_low;
284         u16 ioc_stat;
285
286         /* non-TURBO reply!  Hmmm, something may be up...
287          *  Newest turbo reply mechanism; get address
288          *  via left shift 1 (get rid of MPI_ADDRESS_REPLY_A_BIT)!
289          */
290
291         /* Map DMA address of reply header to cpu address.
292          * pa is 32 bits - but the dma address may be 32 or 64 bits
293          * get offset based only only the low addresses
294          */
295
296         reply_dma_low = (pa <<= 1);
297         mr = (MPT_FRAME_HDR *)((u8 *)ioc->reply_frames +
298                          (reply_dma_low - ioc->reply_frames_low_dma));
299
300         req_idx = le16_to_cpu(mr->u.frame.hwhdr.msgctxu.fld.req_idx);
301         cb_idx = mr->u.frame.hwhdr.msgctxu.fld.cb_idx;
302         mf = MPT_INDEX_2_MFPTR(ioc, req_idx);
303
304         dmfprintk((MYIOC_s_INFO_FMT "Got non-TURBO reply=%p req_idx=%x cb_idx=%x Function=%x\n",
305                         ioc->name, mr, req_idx, cb_idx, mr->u.hdr.Function));
306         DBG_DUMP_REPLY_FRAME(mr)
307
308          /*  Check/log IOC log info
309          */
310         ioc_stat = le16_to_cpu(mr->u.reply.IOCStatus);
311         if (ioc_stat & MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
312                 u32      log_info = le32_to_cpu(mr->u.reply.IOCLogInfo);
313                 if (ioc->bus_type == FC)
314                         mpt_fc_log_info(ioc, log_info);
315                 else if (ioc->bus_type == SPI)
316                         mpt_sp_log_info(ioc, log_info);
317                 else if (ioc->bus_type == SAS)
318                         mpt_sas_log_info(ioc, log_info);
319         }
320         if (ioc_stat & MPI_IOCSTATUS_MASK) {
321                 if (ioc->bus_type == SPI &&
322                     cb_idx != mpt_stm_index &&
323                     cb_idx != mpt_lan_index)
324                         mpt_sp_ioc_info(ioc, (u32)ioc_stat, mf);
325         }
326
327
328         /*  Check for (valid) IO callback!  */
329         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS ||
330                         MptCallbacks[cb_idx] == NULL) {
331                 printk(MYIOC_s_WARN_FMT "%s: Invalid cb_idx (%d)!\n",
332                                 __FUNCTION__, ioc->name, cb_idx);
333                 freeme = 0;
334                 goto out;
335         }
336
337         freeme = MptCallbacks[cb_idx](ioc, mf, mr);
338
339  out:
340         /*  Flush (non-TURBO) reply with a WRITE!  */
341         CHIPREG_WRITE32(&ioc->chip->ReplyFifo, pa);
342
343         if (freeme)
344                 mpt_free_msg_frame(ioc, mf);
345         mb();
346 }
347
348 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
349 /*
350  *      mpt_interrupt - MPT adapter (IOC) specific interrupt handler.
351  *      @irq: irq number (not used)
352  *      @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
353  *      @r: pt_regs pointer (not used)
354  *
355  *      This routine is registered via the request_irq() kernel API call,
356  *      and handles all interrupts generated from a specific MPT adapter
357  *      (also referred to as a IO Controller or IOC).
358  *      This routine must clear the interrupt from the adapter and does
359  *      so by reading the reply FIFO.  Multiple replies may be processed
360  *      per single call to this routine.
361  *
362  *      This routine handles register-level access of the adapter but
363  *      dispatches (calls) a protocol-specific callback routine to handle
364  *      the protocol-specific details of the MPT request completion.
365  */
366 static irqreturn_t
367 mpt_interrupt(int irq, void *bus_id, struct pt_regs *r)
368 {
369         MPT_ADAPTER *ioc = bus_id;
370         u32 pa;
371
372         /*
373          *  Drain the reply FIFO!
374          */
375         while (1) {
376                 pa = CHIPREG_READ32_dmasync(&ioc->chip->ReplyFifo);
377                 if (pa == 0xFFFFFFFF)
378                         return IRQ_HANDLED;
379                 else if (pa & MPI_ADDRESS_REPLY_A_BIT)
380                         mpt_reply(ioc, pa);
381                 else
382                         mpt_turbo_reply(ioc, pa);
383         }
384
385         return IRQ_HANDLED;
386 }
387
388 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
389 /*
390  *      mpt_base_reply - MPT base driver's callback routine; all base driver
391  *      "internal" request/reply processing is routed here.
392  *      Currently used for EventNotification and EventAck handling.
393  *      @ioc: Pointer to MPT_ADAPTER structure
394  *      @mf: Pointer to original MPT request frame
395  *      @reply: Pointer to MPT reply frame (NULL if TurboReply)
396  *
397  *      Returns 1 indicating original alloc'd request frame ptr
398  *      should be freed, or 0 if it shouldn't.
399  */
400 static int
401 mpt_base_reply(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *reply)
402 {
403         int freereq = 1;
404         u8 func;
405
406         dmfprintk((MYIOC_s_INFO_FMT "mpt_base_reply() called\n", ioc->name));
407
408 #if defined(MPT_DEBUG_MSG_FRAME)
409         if (!(reply->u.hdr.MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY)) {
410                 dmfprintk((KERN_INFO MYNAM ": Original request frame (@%p) header\n", mf));
411                 DBG_DUMP_REQUEST_FRAME_HDR(mf)
412         }
413 #endif
414
415         func = reply->u.hdr.Function;
416         dmfprintk((MYIOC_s_INFO_FMT "mpt_base_reply, Function=%02Xh\n",
417                         ioc->name, func));
418
419         if (func == MPI_FUNCTION_EVENT_NOTIFICATION) {
420                 EventNotificationReply_t *pEvReply = (EventNotificationReply_t *) reply;
421                 int evHandlers = 0;
422                 int results;
423
424                 results = ProcessEventNotification(ioc, pEvReply, &evHandlers);
425                 if (results != evHandlers) {
426                         /* CHECKME! Any special handling needed here? */
427                         devtprintk((MYIOC_s_WARN_FMT "Called %d event handlers, sum results = %d\n",
428                                         ioc->name, evHandlers, results));
429                 }
430
431                 /*
432                  *      Hmmm...  It seems that EventNotificationReply is an exception
433                  *      to the rule of one reply per request.
434                  */
435                 if (pEvReply->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) {
436                         freereq = 0;
437                         devtprintk((MYIOC_s_WARN_FMT "EVENT_NOTIFICATION reply %p does not return Request frame\n",
438                                 ioc->name, pEvReply));
439                 } else {
440                         devtprintk((MYIOC_s_WARN_FMT "EVENT_NOTIFICATION reply %p returns Request frame\n",
441                                 ioc->name, pEvReply));
442                 }
443
444 #ifdef CONFIG_PROC_FS
445 //              LogEvent(ioc, pEvReply);
446 #endif
447
448         } else if (func == MPI_FUNCTION_EVENT_ACK) {
449                 dprintk((MYIOC_s_INFO_FMT "mpt_base_reply, EventAck reply received\n",
450                                 ioc->name));
451         } else if (func == MPI_FUNCTION_CONFIG ||
452                    func == MPI_FUNCTION_TOOLBOX) {
453                 CONFIGPARMS *pCfg;
454                 unsigned long flags;
455
456                 dcprintk((MYIOC_s_INFO_FMT "config_complete (mf=%p,mr=%p)\n",
457                                 ioc->name, mf, reply));
458
459                 pCfg = * ((CONFIGPARMS **)((u8 *) mf + ioc->req_sz - sizeof(void *)));
460
461                 if (pCfg) {
462                         /* disable timer and remove from linked list */
463                         del_timer(&pCfg->timer);
464
465                         spin_lock_irqsave(&ioc->FreeQlock, flags);
466                         list_del(&pCfg->linkage);
467                         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
468
469                         /*
470                          *      If IOC Status is SUCCESS, save the header
471                          *      and set the status code to GOOD.
472                          */
473                         pCfg->status = MPT_CONFIG_ERROR;
474                         if (reply) {
475                                 ConfigReply_t   *pReply = (ConfigReply_t *)reply;
476                                 u16              status;
477
478                                 status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
479                                 dcprintk((KERN_NOTICE "  IOCStatus=%04xh, IOCLogInfo=%08xh\n",
480                                      status, le32_to_cpu(pReply->IOCLogInfo)));
481
482                                 pCfg->status = status;
483                                 if (status == MPI_IOCSTATUS_SUCCESS) {
484                                         if ((pReply->Header.PageType &
485                                             MPI_CONFIG_PAGETYPE_MASK) ==
486                                             MPI_CONFIG_PAGETYPE_EXTENDED) {
487                                                 pCfg->cfghdr.ehdr->ExtPageLength =
488                                                     le16_to_cpu(pReply->ExtPageLength);
489                                                 pCfg->cfghdr.ehdr->ExtPageType =
490                                                     pReply->ExtPageType;
491                                         }
492                                         pCfg->cfghdr.hdr->PageVersion = pReply->Header.PageVersion;
493
494                                         /* If this is a regular header, save PageLength. */
495                                         /* LMP Do this better so not using a reserved field! */
496                                         pCfg->cfghdr.hdr->PageLength = pReply->Header.PageLength;
497                                         pCfg->cfghdr.hdr->PageNumber = pReply->Header.PageNumber;
498                                         pCfg->cfghdr.hdr->PageType = pReply->Header.PageType;
499                                 }
500                         }
501
502                         /*
503                          *      Wake up the original calling thread
504                          */
505                         pCfg->wait_done = 1;
506                         wake_up(&mpt_waitq);
507                 }
508         } else if (func == MPI_FUNCTION_SAS_IO_UNIT_CONTROL) {
509                 /* we should be always getting a reply frame */
510                 memcpy(ioc->persist_reply_frame, reply,
511                     min(MPT_DEFAULT_FRAME_SIZE,
512                     4*reply->u.reply.MsgLength));
513                 del_timer(&ioc->persist_timer);
514                 ioc->persist_wait_done = 1;
515                 wake_up(&mpt_waitq);
516         } else {
517                 printk(MYIOC_s_ERR_FMT "Unexpected msg function (=%02Xh) reply received!\n",
518                                 ioc->name, func);
519         }
520
521         /*
522          *      Conditionally tell caller to free the original
523          *      EventNotification/EventAck/unexpected request frame!
524          */
525         return freereq;
526 }
527
528 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
529 /**
530  *      mpt_register - Register protocol-specific main callback handler.
531  *      @cbfunc: callback function pointer
532  *      @dclass: Protocol driver's class (%MPT_DRIVER_CLASS enum value)
533  *
534  *      This routine is called by a protocol-specific driver (SCSI host,
535  *      LAN, SCSI target) to register it's reply callback routine.  Each
536  *      protocol-specific driver must do this before it will be able to
537  *      use any IOC resources, such as obtaining request frames.
538  *
539  *      NOTES: The SCSI protocol driver currently calls this routine thrice
540  *      in order to register separate callbacks; one for "normal" SCSI IO;
541  *      one for MptScsiTaskMgmt requests; one for Scan/DV requests.
542  *
543  *      Returns a positive integer valued "handle" in the
544  *      range (and S.O.D. order) {N,...,7,6,5,...,1} if successful.
545  *      Any non-positive return value (including zero!) should be considered
546  *      an error by the caller.
547  */
548 int
549 mpt_register(MPT_CALLBACK cbfunc, MPT_DRIVER_CLASS dclass)
550 {
551         int i;
552
553         last_drv_idx = -1;
554
555         /*
556          *  Search for empty callback slot in this order: {N,...,7,6,5,...,1}
557          *  (slot/handle 0 is reserved!)
558          */
559         for (i = MPT_MAX_PROTOCOL_DRIVERS-1; i; i--) {
560                 if (MptCallbacks[i] == NULL) {
561                         MptCallbacks[i] = cbfunc;
562                         MptDriverClass[i] = dclass;
563                         MptEvHandlers[i] = NULL;
564                         last_drv_idx = i;
565                         break;
566                 }
567         }
568
569         return last_drv_idx;
570 }
571
572 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
573 /**
574  *      mpt_deregister - Deregister a protocol drivers resources.
575  *      @cb_idx: previously registered callback handle
576  *
577  *      Each protocol-specific driver should call this routine when it's
578  *      module is unloaded.
579  */
580 void
581 mpt_deregister(int cb_idx)
582 {
583         if ((cb_idx >= 0) && (cb_idx < MPT_MAX_PROTOCOL_DRIVERS)) {
584                 MptCallbacks[cb_idx] = NULL;
585                 MptDriverClass[cb_idx] = MPTUNKNOWN_DRIVER;
586                 MptEvHandlers[cb_idx] = NULL;
587
588                 last_drv_idx++;
589         }
590 }
591
592 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
593 /**
594  *      mpt_event_register - Register protocol-specific event callback
595  *      handler.
596  *      @cb_idx: previously registered (via mpt_register) callback handle
597  *      @ev_cbfunc: callback function
598  *
599  *      This routine can be called by one or more protocol-specific drivers
600  *      if/when they choose to be notified of MPT events.
601  *
602  *      Returns 0 for success.
603  */
604 int
605 mpt_event_register(int cb_idx, MPT_EVHANDLER ev_cbfunc)
606 {
607         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
608                 return -1;
609
610         MptEvHandlers[cb_idx] = ev_cbfunc;
611         return 0;
612 }
613
614 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
615 /**
616  *      mpt_event_deregister - Deregister protocol-specific event callback
617  *      handler.
618  *      @cb_idx: previously registered callback handle
619  *
620  *      Each protocol-specific driver should call this routine
621  *      when it does not (or can no longer) handle events,
622  *      or when it's module is unloaded.
623  */
624 void
625 mpt_event_deregister(int cb_idx)
626 {
627         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
628                 return;
629
630         MptEvHandlers[cb_idx] = NULL;
631 }
632
633 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
634 /**
635  *      mpt_reset_register - Register protocol-specific IOC reset handler.
636  *      @cb_idx: previously registered (via mpt_register) callback handle
637  *      @reset_func: reset function
638  *
639  *      This routine can be called by one or more protocol-specific drivers
640  *      if/when they choose to be notified of IOC resets.
641  *
642  *      Returns 0 for success.
643  */
644 int
645 mpt_reset_register(int cb_idx, MPT_RESETHANDLER reset_func)
646 {
647         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
648                 return -1;
649
650         MptResetHandlers[cb_idx] = reset_func;
651         return 0;
652 }
653
654 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
655 /**
656  *      mpt_reset_deregister - Deregister protocol-specific IOC reset handler.
657  *      @cb_idx: previously registered callback handle
658  *
659  *      Each protocol-specific driver should call this routine
660  *      when it does not (or can no longer) handle IOC reset handling,
661  *      or when it's module is unloaded.
662  */
663 void
664 mpt_reset_deregister(int cb_idx)
665 {
666         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
667                 return;
668
669         MptResetHandlers[cb_idx] = NULL;
670 }
671
672 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
673 /**
674  *      mpt_device_driver_register - Register device driver hooks
675  */
676 int
677 mpt_device_driver_register(struct mpt_pci_driver * dd_cbfunc, int cb_idx)
678 {
679         MPT_ADAPTER     *ioc;
680
681         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS) {
682                 return -EINVAL;
683         }
684
685         MptDeviceDriverHandlers[cb_idx] = dd_cbfunc;
686
687         /* call per pci device probe entry point */
688         list_for_each_entry(ioc, &ioc_list, list) {
689                 if(dd_cbfunc->probe) {
690                         dd_cbfunc->probe(ioc->pcidev,
691                           ioc->pcidev->driver->id_table);
692                 }
693          }
694
695         return 0;
696 }
697
698 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
699 /**
700  *      mpt_device_driver_deregister - DeRegister device driver hooks
701  */
702 void
703 mpt_device_driver_deregister(int cb_idx)
704 {
705         struct mpt_pci_driver *dd_cbfunc;
706         MPT_ADAPTER     *ioc;
707
708         if (cb_idx < 1 || cb_idx >= MPT_MAX_PROTOCOL_DRIVERS)
709                 return;
710
711         dd_cbfunc = MptDeviceDriverHandlers[cb_idx];
712
713         list_for_each_entry(ioc, &ioc_list, list) {
714                 if (dd_cbfunc->remove)
715                         dd_cbfunc->remove(ioc->pcidev);
716         }
717
718         MptDeviceDriverHandlers[cb_idx] = NULL;
719 }
720
721
722 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
723 /**
724  *      mpt_get_msg_frame - Obtain a MPT request frame from the pool (of 1024)
725  *      allocated per MPT adapter.
726  *      @handle: Handle of registered MPT protocol driver
727  *      @ioc: Pointer to MPT adapter structure
728  *
729  *      Returns pointer to a MPT request frame or %NULL if none are available
730  *      or IOC is not active.
731  */
732 MPT_FRAME_HDR*
733 mpt_get_msg_frame(int handle, MPT_ADAPTER *ioc)
734 {
735         MPT_FRAME_HDR *mf;
736         unsigned long flags;
737         u16      req_idx;       /* Request index */
738
739         /* validate handle and ioc identifier */
740
741 #ifdef MFCNT
742         if (!ioc->active)
743                 printk(KERN_WARNING "IOC Not Active! mpt_get_msg_frame returning NULL!\n");
744 #endif
745
746         /* If interrupts are not attached, do not return a request frame */
747         if (!ioc->active)
748                 return NULL;
749
750         spin_lock_irqsave(&ioc->FreeQlock, flags);
751         if (!list_empty(&ioc->FreeQ)) {
752                 int req_offset;
753
754                 mf = list_entry(ioc->FreeQ.next, MPT_FRAME_HDR,
755                                 u.frame.linkage.list);
756                 list_del(&mf->u.frame.linkage.list);
757                 mf->u.frame.linkage.arg1 = 0;
758                 mf->u.frame.hwhdr.msgctxu.fld.cb_idx = handle;  /* byte */
759                 req_offset = (u8 *)mf - (u8 *)ioc->req_frames;
760                                                                 /* u16! */
761                 req_idx = req_offset / ioc->req_sz;
762                 mf->u.frame.hwhdr.msgctxu.fld.req_idx = cpu_to_le16(req_idx);
763                 mf->u.frame.hwhdr.msgctxu.fld.rsvd = 0;
764                 ioc->RequestNB[req_idx] = ioc->NB_for_64_byte_frame; /* Default, will be changed if necessary in SG generation */
765 #ifdef MFCNT
766                 ioc->mfcnt++;
767 #endif
768         }
769         else
770                 mf = NULL;
771         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
772
773 #ifdef MFCNT
774         if (mf == NULL)
775                 printk(KERN_WARNING "IOC Active. No free Msg Frames! Count 0x%x Max 0x%x\n", ioc->mfcnt, ioc->req_depth);
776         mfcounter++;
777         if (mfcounter == PRINT_MF_COUNT)
778                 printk(KERN_INFO "MF Count 0x%x Max 0x%x \n", ioc->mfcnt, ioc->req_depth);
779 #endif
780
781         dmfprintk((KERN_INFO MYNAM ": %s: mpt_get_msg_frame(%d,%d), got mf=%p\n",
782                         ioc->name, handle, ioc->id, mf));
783         return mf;
784 }
785
786 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
787 /**
788  *      mpt_put_msg_frame - Send a protocol specific MPT request frame
789  *      to a IOC.
790  *      @handle: Handle of registered MPT protocol driver
791  *      @ioc: Pointer to MPT adapter structure
792  *      @mf: Pointer to MPT request frame
793  *
794  *      This routine posts a MPT request frame to the request post FIFO of a
795  *      specific MPT adapter.
796  */
797 void
798 mpt_put_msg_frame(int handle, MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf)
799 {
800         u32 mf_dma_addr;
801         int req_offset;
802         u16      req_idx;       /* Request index */
803
804         /* ensure values are reset properly! */
805         mf->u.frame.hwhdr.msgctxu.fld.cb_idx = handle;          /* byte */
806         req_offset = (u8 *)mf - (u8 *)ioc->req_frames;
807                                                                 /* u16! */
808         req_idx = req_offset / ioc->req_sz;
809         mf->u.frame.hwhdr.msgctxu.fld.req_idx = cpu_to_le16(req_idx);
810         mf->u.frame.hwhdr.msgctxu.fld.rsvd = 0;
811
812 #ifdef MPT_DEBUG_MSG_FRAME
813         {
814                 u32     *m = mf->u.frame.hwhdr.__hdr;
815                 int      ii, n;
816
817                 printk(KERN_INFO MYNAM ": %s: About to Put msg frame @ %p:\n" KERN_INFO " ",
818                                 ioc->name, m);
819                 n = ioc->req_sz/4 - 1;
820                 while (m[n] == 0)
821                         n--;
822                 for (ii=0; ii<=n; ii++) {
823                         if (ii && ((ii%8)==0))
824                                 printk("\n" KERN_INFO " ");
825                         printk(" %08x", le32_to_cpu(m[ii]));
826                 }
827                 printk("\n");
828         }
829 #endif
830
831         mf_dma_addr = (ioc->req_frames_low_dma + req_offset) | ioc->RequestNB[req_idx];
832         dsgprintk((MYIOC_s_INFO_FMT "mf_dma_addr=%x req_idx=%d RequestNB=%x\n", ioc->name, mf_dma_addr, req_idx, ioc->RequestNB[req_idx]));
833         CHIPREG_WRITE32(&ioc->chip->RequestFifo, mf_dma_addr);
834 }
835
836 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
837 /**
838  *      mpt_free_msg_frame - Place MPT request frame back on FreeQ.
839  *      @handle: Handle of registered MPT protocol driver
840  *      @ioc: Pointer to MPT adapter structure
841  *      @mf: Pointer to MPT request frame
842  *
843  *      This routine places a MPT request frame back on the MPT adapter's
844  *      FreeQ.
845  */
846 void
847 mpt_free_msg_frame(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf)
848 {
849         unsigned long flags;
850
851         /*  Put Request back on FreeQ!  */
852         spin_lock_irqsave(&ioc->FreeQlock, flags);
853         mf->u.frame.linkage.arg1 = 0xdeadbeaf; /* signature to know if this mf is freed */
854         list_add_tail(&mf->u.frame.linkage.list, &ioc->FreeQ);
855 #ifdef MFCNT
856         ioc->mfcnt--;
857 #endif
858         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
859 }
860
861 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
862 /**
863  *      mpt_add_sge - Place a simple SGE at address pAddr.
864  *      @pAddr: virtual address for SGE
865  *      @flagslength: SGE flags and data transfer length
866  *      @dma_addr: Physical address
867  *
868  *      This routine places a MPT request frame back on the MPT adapter's
869  *      FreeQ.
870  */
871 void
872 mpt_add_sge(char *pAddr, u32 flagslength, dma_addr_t dma_addr)
873 {
874         if (sizeof(dma_addr_t) == sizeof(u64)) {
875                 SGESimple64_t *pSge = (SGESimple64_t *) pAddr;
876                 u32 tmp = dma_addr & 0xFFFFFFFF;
877
878                 pSge->FlagsLength = cpu_to_le32(flagslength);
879                 pSge->Address.Low = cpu_to_le32(tmp);
880                 tmp = (u32) ((u64)dma_addr >> 32);
881                 pSge->Address.High = cpu_to_le32(tmp);
882
883         } else {
884                 SGESimple32_t *pSge = (SGESimple32_t *) pAddr;
885                 pSge->FlagsLength = cpu_to_le32(flagslength);
886                 pSge->Address = cpu_to_le32(dma_addr);
887         }
888 }
889
890 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
891 /**
892  *      mpt_send_handshake_request - Send MPT request via doorbell
893  *      handshake method.
894  *      @handle: Handle of registered MPT protocol driver
895  *      @ioc: Pointer to MPT adapter structure
896  *      @reqBytes: Size of the request in bytes
897  *      @req: Pointer to MPT request frame
898  *      @sleepFlag: Use schedule if CAN_SLEEP else use udelay.
899  *
900  *      This routine is used exclusively to send MptScsiTaskMgmt
901  *      requests since they are required to be sent via doorbell handshake.
902  *
903  *      NOTE: It is the callers responsibility to byte-swap fields in the
904  *      request which are greater than 1 byte in size.
905  *
906  *      Returns 0 for success, non-zero for failure.
907  */
908 int
909 mpt_send_handshake_request(int handle, MPT_ADAPTER *ioc, int reqBytes, u32 *req, int sleepFlag)
910 {
911         int              r = 0;
912         u8      *req_as_bytes;
913         int      ii;
914
915         /* State is known to be good upon entering
916          * this function so issue the bus reset
917          * request.
918          */
919
920         /*
921          * Emulate what mpt_put_msg_frame() does /wrt to sanity
922          * setting cb_idx/req_idx.  But ONLY if this request
923          * is in proper (pre-alloc'd) request buffer range...
924          */
925         ii = MFPTR_2_MPT_INDEX(ioc,(MPT_FRAME_HDR*)req);
926         if (reqBytes >= 12 && ii >= 0 && ii < ioc->req_depth) {
927                 MPT_FRAME_HDR *mf = (MPT_FRAME_HDR*)req;
928                 mf->u.frame.hwhdr.msgctxu.fld.req_idx = cpu_to_le16(ii);
929                 mf->u.frame.hwhdr.msgctxu.fld.cb_idx = handle;
930         }
931
932         /* Make sure there are no doorbells */
933         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
934
935         CHIPREG_WRITE32(&ioc->chip->Doorbell,
936                         ((MPI_FUNCTION_HANDSHAKE<<MPI_DOORBELL_FUNCTION_SHIFT) |
937                          ((reqBytes/4)<<MPI_DOORBELL_ADD_DWORDS_SHIFT)));
938
939         /* Wait for IOC doorbell int */
940         if ((ii = WaitForDoorbellInt(ioc, 5, sleepFlag)) < 0) {
941                 return ii;
942         }
943
944         /* Read doorbell and check for active bit */
945         if (!(CHIPREG_READ32(&ioc->chip->Doorbell) & MPI_DOORBELL_ACTIVE))
946                 return -5;
947
948         dhsprintk((KERN_INFO MYNAM ": %s: mpt_send_handshake_request start, WaitCnt=%d\n",
949                 ioc->name, ii));
950
951         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
952
953         if ((r = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0) {
954                 return -2;
955         }
956
957         /* Send request via doorbell handshake */
958         req_as_bytes = (u8 *) req;
959         for (ii = 0; ii < reqBytes/4; ii++) {
960                 u32 word;
961
962                 word = ((req_as_bytes[(ii*4) + 0] <<  0) |
963                         (req_as_bytes[(ii*4) + 1] <<  8) |
964                         (req_as_bytes[(ii*4) + 2] << 16) |
965                         (req_as_bytes[(ii*4) + 3] << 24));
966                 CHIPREG_WRITE32(&ioc->chip->Doorbell, word);
967                 if ((r = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0) {
968                         r = -3;
969                         break;
970                 }
971         }
972
973         if (r >= 0 && WaitForDoorbellInt(ioc, 10, sleepFlag) >= 0)
974                 r = 0;
975         else
976                 r = -4;
977
978         /* Make sure there are no doorbells */
979         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
980
981         return r;
982 }
983
984 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
985 /**
986  * mpt_host_page_access_control - provides mechanism for the host
987  * driver to control the IOC's Host Page Buffer access.
988  * @ioc: Pointer to MPT adapter structure
989  * @access_control_value: define bits below
990  *
991  * Access Control Value - bits[15:12]
992  * 0h Reserved
993  * 1h Enable Access { MPI_DB_HPBAC_ENABLE_ACCESS }
994  * 2h Disable Access { MPI_DB_HPBAC_DISABLE_ACCESS }
995  * 3h Free Buffer { MPI_DB_HPBAC_FREE_BUFFER }
996  *
997  * Returns 0 for success, non-zero for failure.
998  */
999
1000 static int
1001 mpt_host_page_access_control(MPT_ADAPTER *ioc, u8 access_control_value, int sleepFlag)
1002 {
1003         int      r = 0;
1004
1005         /* return if in use */
1006         if (CHIPREG_READ32(&ioc->chip->Doorbell)
1007             & MPI_DOORBELL_ACTIVE)
1008             return -1;
1009
1010         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1011
1012         CHIPREG_WRITE32(&ioc->chip->Doorbell,
1013                 ((MPI_FUNCTION_HOST_PAGEBUF_ACCESS_CONTROL
1014                  <<MPI_DOORBELL_FUNCTION_SHIFT) |
1015                  (access_control_value<<12)));
1016
1017         /* Wait for IOC to clear Doorbell Status bit */
1018         if ((r = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0) {
1019                 return -2;
1020         }else
1021                 return 0;
1022 }
1023
1024 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1025 /**
1026  *      mpt_host_page_alloc - allocate system memory for the fw
1027  *      If we already allocated memory in past, then resend the same pointer.
1028  *      ioc@: Pointer to pointer to IOC adapter
1029  *      ioc_init@: Pointer to ioc init config page
1030  *
1031  *      Returns 0 for success, non-zero for failure.
1032  */
1033 static int
1034 mpt_host_page_alloc(MPT_ADAPTER *ioc, pIOCInit_t ioc_init)
1035 {
1036         char    *psge;
1037         int     flags_length;
1038         u32     host_page_buffer_sz=0;
1039
1040         if(!ioc->HostPageBuffer) {
1041
1042                 host_page_buffer_sz =
1043                     le32_to_cpu(ioc->facts.HostPageBufferSGE.FlagsLength) & 0xFFFFFF;
1044
1045                 if(!host_page_buffer_sz)
1046                         return 0; /* fw doesn't need any host buffers */
1047
1048                 /* spin till we get enough memory */
1049                 while(host_page_buffer_sz > 0) {
1050
1051                         if((ioc->HostPageBuffer = pci_alloc_consistent(
1052                             ioc->pcidev,
1053                             host_page_buffer_sz,
1054                             &ioc->HostPageBuffer_dma)) != NULL) {
1055
1056                                 dinitprintk((MYIOC_s_INFO_FMT
1057                                     "host_page_buffer @ %p, dma @ %x, sz=%d bytes\n",
1058                                     ioc->name,
1059                                     ioc->HostPageBuffer,
1060                                     ioc->HostPageBuffer_dma,
1061                                     host_page_buffer_sz));
1062                                 ioc->alloc_total += host_page_buffer_sz;
1063                                 ioc->HostPageBuffer_sz = host_page_buffer_sz;
1064                                 break;
1065                         }
1066
1067                         host_page_buffer_sz -= (4*1024);
1068                 }
1069         }
1070
1071         if(!ioc->HostPageBuffer) {
1072                 printk(MYIOC_s_ERR_FMT
1073                     "Failed to alloc memory for host_page_buffer!\n",
1074                     ioc->name);
1075                 return -999;
1076         }
1077
1078         psge = (char *)&ioc_init->HostPageBufferSGE;
1079         flags_length = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1080             MPI_SGE_FLAGS_SYSTEM_ADDRESS |
1081             MPI_SGE_FLAGS_32_BIT_ADDRESSING |
1082             MPI_SGE_FLAGS_HOST_TO_IOC |
1083             MPI_SGE_FLAGS_END_OF_BUFFER;
1084         if (sizeof(dma_addr_t) == sizeof(u64)) {
1085             flags_length |= MPI_SGE_FLAGS_64_BIT_ADDRESSING;
1086         }
1087         flags_length = flags_length << MPI_SGE_FLAGS_SHIFT;
1088         flags_length |= ioc->HostPageBuffer_sz;
1089         mpt_add_sge(psge, flags_length, ioc->HostPageBuffer_dma);
1090         ioc->facts.HostPageBufferSGE = ioc_init->HostPageBufferSGE;
1091
1092 return 0;
1093 }
1094
1095 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1096 /**
1097  *      mpt_verify_adapter - Given a unique IOC identifier, set pointer to
1098  *      the associated MPT adapter structure.
1099  *      @iocid: IOC unique identifier (integer)
1100  *      @iocpp: Pointer to pointer to IOC adapter
1101  *
1102  *      Returns iocid and sets iocpp.
1103  */
1104 int
1105 mpt_verify_adapter(int iocid, MPT_ADAPTER **iocpp)
1106 {
1107         MPT_ADAPTER *ioc;
1108
1109         list_for_each_entry(ioc,&ioc_list,list) {
1110                 if (ioc->id == iocid) {
1111                         *iocpp =ioc;
1112                         return iocid;
1113                 }
1114         }
1115
1116         *iocpp = NULL;
1117         return -1;
1118 }
1119
1120 int
1121 mpt_alt_ioc_wait(MPT_ADAPTER *ioc)
1122 {
1123         int loop_count = 30 * 4;  /* Wait 30 seconds */
1124         int status = -1; /* -1 means failed to get board READY */
1125
1126         do {
1127                 spin_lock(&ioc->initializing_hba_lock);
1128                 if (ioc->initializing_hba_lock_flag == 0) {
1129                         ioc->initializing_hba_lock_flag=1;
1130                         spin_unlock(&ioc->initializing_hba_lock);
1131                         status = 0;
1132                         break;
1133                 }
1134                 spin_unlock(&ioc->initializing_hba_lock);
1135                 set_current_state(TASK_INTERRUPTIBLE);
1136                 schedule_timeout(HZ/4);
1137         } while (--loop_count);
1138
1139         return status;
1140 }
1141
1142 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1143 /*
1144  *      mpt_bringup_adapter - This is a wrapper function for mpt_do_ioc_recovery
1145  *      @ioc: Pointer to MPT adapter structure
1146  *      @sleepFlag: Use schedule if CAN_SLEEP else use udelay.
1147  *
1148  *      This routine performs all the steps necessary to bring the IOC
1149  *      to a OPERATIONAL state.
1150  *
1151  *      Special Note: This function was added with spin lock's so as to allow
1152  *      the dv(domain validation) work thread to succeed on the other channel
1153  *      that maybe occuring at the same time when this function is called.
1154  *      Without this lock, the dv would fail when message frames were
1155  *      requested during hba bringup on the alternate ioc.
1156  */
1157 static int
1158 mpt_bringup_adapter(MPT_ADAPTER *ioc, int sleepFlag)
1159 {
1160         int r;
1161
1162         if(ioc->alt_ioc) {
1163                 if((r=mpt_alt_ioc_wait(ioc->alt_ioc)!=0))
1164                         return r;
1165         }
1166
1167         r = mpt_do_ioc_recovery(ioc, MPT_HOSTEVENT_IOC_BRINGUP,
1168             CAN_SLEEP);
1169
1170         if(ioc->alt_ioc) {
1171                 spin_lock(&ioc->alt_ioc->initializing_hba_lock);
1172                 ioc->alt_ioc->initializing_hba_lock_flag=0;
1173                 spin_unlock(&ioc->alt_ioc->initializing_hba_lock);
1174         }
1175
1176 return r;
1177 }
1178
1179 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1180 /*
1181  *      mpt_attach - Install a PCI intelligent MPT adapter.
1182  *      @pdev: Pointer to pci_dev structure
1183  *
1184  *      This routine performs all the steps necessary to bring the IOC of
1185  *      a MPT adapter to a OPERATIONAL state.  This includes registering
1186  *      memory regions, registering the interrupt, and allocating request
1187  *      and reply memory pools.
1188  *
1189  *      This routine also pre-fetches the LAN MAC address of a Fibre Channel
1190  *      MPT adapter.
1191  *
1192  *      Returns 0 for success, non-zero for failure.
1193  *
1194  *      TODO: Add support for polled controllers
1195  */
1196 int
1197 mpt_attach(struct pci_dev *pdev, const struct pci_device_id *id)
1198 {
1199         MPT_ADAPTER     *ioc;
1200         u8              __iomem *mem;
1201         unsigned long    mem_phys;
1202         unsigned long    port;
1203         u32              msize;
1204         u32              psize;
1205         int              ii;
1206         int              r = -ENODEV;
1207         u8               revision;
1208         u8               pcixcmd;
1209         static int       mpt_ids = 0;
1210 #ifdef CONFIG_PROC_FS
1211         struct proc_dir_entry *dent, *ent;
1212 #endif
1213
1214         if (pci_enable_device(pdev))
1215                 return r;
1216
1217         dinitprintk((KERN_WARNING MYNAM ": mpt_adapter_install\n"));
1218
1219         if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
1220                 dprintk((KERN_INFO MYNAM
1221                         ": 64 BIT PCI BUS DMA ADDRESSING SUPPORTED\n"));
1222         } else if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1223                 printk(KERN_WARNING MYNAM ": 32 BIT PCI BUS DMA ADDRESSING NOT SUPPORTED\n");
1224                 return r;
1225         }
1226
1227         if (!pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
1228                 dprintk((KERN_INFO MYNAM
1229                         ": Using 64 bit consistent mask\n"));
1230         else
1231                 dprintk((KERN_INFO MYNAM
1232                         ": Not using 64 bit consistent mask\n"));
1233
1234         ioc = kzalloc(sizeof(MPT_ADAPTER), GFP_ATOMIC);
1235         if (ioc == NULL) {
1236                 printk(KERN_ERR MYNAM ": ERROR - Insufficient memory to add adapter!\n");
1237                 return -ENOMEM;
1238         }
1239         ioc->alloc_total = sizeof(MPT_ADAPTER);
1240         ioc->req_sz = MPT_DEFAULT_FRAME_SIZE;           /* avoid div by zero! */
1241         ioc->reply_sz = MPT_REPLY_FRAME_SIZE;
1242
1243         ioc->pcidev = pdev;
1244         ioc->diagPending = 0;
1245         spin_lock_init(&ioc->diagLock);
1246         spin_lock_init(&ioc->fc_rescan_work_lock);
1247         spin_lock_init(&ioc->fc_rport_lock);
1248         spin_lock_init(&ioc->initializing_hba_lock);
1249
1250         /* Initialize the event logging.
1251          */
1252         ioc->eventTypes = 0;    /* None */
1253         ioc->eventContext = 0;
1254         ioc->eventLogSize = 0;
1255         ioc->events = NULL;
1256
1257 #ifdef MFCNT
1258         ioc->mfcnt = 0;
1259 #endif
1260
1261         ioc->cached_fw = NULL;
1262
1263         /* Initilize SCSI Config Data structure
1264          */
1265         memset(&ioc->spi_data, 0, sizeof(SpiCfgData));
1266
1267         /* Initialize the running configQ head.
1268          */
1269         INIT_LIST_HEAD(&ioc->configQ);
1270
1271         /* Initialize the fc rport list head.
1272          */
1273         INIT_LIST_HEAD(&ioc->fc_rports);
1274
1275         /* Find lookup slot. */
1276         INIT_LIST_HEAD(&ioc->list);
1277         ioc->id = mpt_ids++;
1278
1279         mem_phys = msize = 0;
1280         port = psize = 0;
1281         for (ii=0; ii < DEVICE_COUNT_RESOURCE; ii++) {
1282                 if (pci_resource_flags(pdev, ii) & PCI_BASE_ADDRESS_SPACE_IO) {
1283                         /* Get I/O space! */
1284                         port = pci_resource_start(pdev, ii);
1285                         psize = pci_resource_len(pdev,ii);
1286                 } else {
1287                         /* Get memmap */
1288                         mem_phys = pci_resource_start(pdev, ii);
1289                         msize = pci_resource_len(pdev,ii);
1290                         break;
1291                 }
1292         }
1293         ioc->mem_size = msize;
1294
1295         if (ii == DEVICE_COUNT_RESOURCE) {
1296                 printk(KERN_ERR MYNAM ": ERROR - MPT adapter has no memory regions defined!\n");
1297                 kfree(ioc);
1298                 return -EINVAL;
1299         }
1300
1301         dinitprintk((KERN_INFO MYNAM ": MPT adapter @ %lx, msize=%dd bytes\n", mem_phys, msize));
1302         dinitprintk((KERN_INFO MYNAM ": (port i/o @ %lx, psize=%dd bytes)\n", port, psize));
1303
1304         mem = NULL;
1305         /* Get logical ptr for PciMem0 space */
1306         /*mem = ioremap(mem_phys, msize);*/
1307         mem = ioremap(mem_phys, 0x100);
1308         if (mem == NULL) {
1309                 printk(KERN_ERR MYNAM ": ERROR - Unable to map adapter memory!\n");
1310                 kfree(ioc);
1311                 return -EINVAL;
1312         }
1313         ioc->memmap = mem;
1314         dinitprintk((KERN_INFO MYNAM ": mem = %p, mem_phys = %lx\n", mem, mem_phys));
1315
1316         dinitprintk((KERN_INFO MYNAM ": facts @ %p, pfacts[0] @ %p\n",
1317                         &ioc->facts, &ioc->pfacts[0]));
1318
1319         ioc->mem_phys = mem_phys;
1320         ioc->chip = (SYSIF_REGS __iomem *)mem;
1321
1322         /* Save Port IO values in case we need to do downloadboot */
1323         {
1324                 u8 *pmem = (u8*)port;
1325                 ioc->pio_mem_phys = port;
1326                 ioc->pio_chip = (SYSIF_REGS __iomem *)pmem;
1327         }
1328
1329         if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC909) {
1330                 ioc->prod_name = "LSIFC909";
1331                 ioc->bus_type = FC;
1332         }
1333         else if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC929) {
1334                 ioc->prod_name = "LSIFC929";
1335                 ioc->bus_type = FC;
1336         }
1337         else if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC919) {
1338                 ioc->prod_name = "LSIFC919";
1339                 ioc->bus_type = FC;
1340         }
1341         else if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC929X) {
1342                 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1343                 ioc->bus_type = FC;
1344                 if (revision < XL_929) {
1345                         ioc->prod_name = "LSIFC929X";
1346                         /* 929X Chip Fix. Set Split transactions level
1347                         * for PCIX. Set MOST bits to zero.
1348                         */
1349                         pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1350                         pcixcmd &= 0x8F;
1351                         pci_write_config_byte(pdev, 0x6a, pcixcmd);
1352                 } else {
1353                         ioc->prod_name = "LSIFC929XL";
1354                         /* 929XL Chip Fix. Set MMRBC to 0x08.
1355                         */
1356                         pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1357                         pcixcmd |= 0x08;
1358                         pci_write_config_byte(pdev, 0x6a, pcixcmd);
1359                 }
1360         }
1361         else if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC919X) {
1362                 ioc->prod_name = "LSIFC919X";
1363                 ioc->bus_type = FC;
1364                 /* 919X Chip Fix. Set Split transactions level
1365                  * for PCIX. Set MOST bits to zero.
1366                  */
1367                 pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1368                 pcixcmd &= 0x8F;
1369                 pci_write_config_byte(pdev, 0x6a, pcixcmd);
1370         }
1371         else if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC939X) {
1372                 ioc->prod_name = "LSIFC939X";
1373                 ioc->bus_type = FC;
1374                 ioc->errata_flag_1064 = 1;
1375         }
1376         else if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC949X) {
1377                 ioc->prod_name = "LSIFC949X";
1378                 ioc->bus_type = FC;
1379                 ioc->errata_flag_1064 = 1;
1380         }
1381         else if (pdev->device == MPI_MANUFACTPAGE_DEVICEID_FC949E) {
1382                 ioc->prod_name = "LSIFC949E";
1383                 ioc->bus_type = FC;
1384         }
1385         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_53C1030) {
1386                 ioc->prod_name = "LSI53C1030";
1387                 ioc->bus_type = SPI;
1388                 /* 1030 Chip Fix. Disable Split transactions
1389                  * for PCIX. Set MOST bits to zero if Rev < C0( = 8).
1390                  */
1391                 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1392                 if (revision < C0_1030) {
1393                         pci_read_config_byte(pdev, 0x6a, &pcixcmd);
1394                         pcixcmd &= 0x8F;
1395                         pci_write_config_byte(pdev, 0x6a, pcixcmd);
1396                 }
1397         }
1398         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_1030_53C1035) {
1399                 ioc->prod_name = "LSI53C1035";
1400                 ioc->bus_type = SPI;
1401         }
1402         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_SAS1064) {
1403                 ioc->prod_name = "LSISAS1064";
1404                 ioc->bus_type = SAS;
1405                 ioc->errata_flag_1064 = 1;
1406         }
1407         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_SAS1066) {
1408                 ioc->prod_name = "LSISAS1066";
1409                 ioc->bus_type = SAS;
1410                 ioc->errata_flag_1064 = 1;
1411         }
1412         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_SAS1068) {
1413                 ioc->prod_name = "LSISAS1068";
1414                 ioc->bus_type = SAS;
1415                 ioc->errata_flag_1064 = 1;
1416         }
1417         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_SAS1064E) {
1418                 ioc->prod_name = "LSISAS1064E";
1419                 ioc->bus_type = SAS;
1420         }
1421         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_SAS1066E) {
1422                 ioc->prod_name = "LSISAS1066E";
1423                 ioc->bus_type = SAS;
1424         }
1425         else if (pdev->device == MPI_MANUFACTPAGE_DEVID_SAS1068E) {
1426                 ioc->prod_name = "LSISAS1068E";
1427                 ioc->bus_type = SAS;
1428         }
1429
1430         if (ioc->errata_flag_1064)
1431                 pci_disable_io_access(pdev);
1432
1433         sprintf(ioc->name, "ioc%d", ioc->id);
1434
1435         spin_lock_init(&ioc->FreeQlock);
1436
1437         /* Disable all! */
1438         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1439         ioc->active = 0;
1440         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1441
1442         /* Set lookup ptr. */
1443         list_add_tail(&ioc->list, &ioc_list);
1444
1445         ioc->pci_irq = -1;
1446         if (pdev->irq) {
1447                 r = request_irq(pdev->irq, mpt_interrupt, SA_SHIRQ, ioc->name, ioc);
1448
1449                 if (r < 0) {
1450 #ifndef __sparc__
1451                         printk(MYIOC_s_ERR_FMT "Unable to allocate interrupt %d!\n",
1452                                         ioc->name, pdev->irq);
1453 #else
1454                         printk(MYIOC_s_ERR_FMT "Unable to allocate interrupt %s!\n",
1455                                         ioc->name, __irq_itoa(pdev->irq));
1456 #endif
1457                         list_del(&ioc->list);
1458                         iounmap(mem);
1459                         kfree(ioc);
1460                         return -EBUSY;
1461                 }
1462
1463                 ioc->pci_irq = pdev->irq;
1464
1465                 pci_set_master(pdev);                   /* ?? */
1466                 pci_set_drvdata(pdev, ioc);
1467
1468 #ifndef __sparc__
1469                 dprintk((KERN_INFO MYNAM ": %s installed at interrupt %d\n", ioc->name, pdev->irq));
1470 #else
1471                 dprintk((KERN_INFO MYNAM ": %s installed at interrupt %s\n", ioc->name, __irq_itoa(pdev->irq)));
1472 #endif
1473         }
1474
1475         /* Check for "bound ports" (929, 929X, 1030, 1035) to reduce redundant resets.
1476          */
1477         mpt_detect_bound_ports(ioc, pdev);
1478
1479         if ((r = mpt_bringup_adapter(ioc, CAN_SLEEP)) != 0){
1480                 printk(KERN_WARNING MYNAM
1481                   ": WARNING - %s did not initialize properly! (%d)\n",
1482                   ioc->name, r);
1483
1484                 list_del(&ioc->list);
1485                 free_irq(ioc->pci_irq, ioc);
1486                 iounmap(mem);
1487                 kfree(ioc);
1488                 pci_set_drvdata(pdev, NULL);
1489                 return r;
1490         }
1491
1492         /* call per device driver probe entry point */
1493         for(ii=0; ii<MPT_MAX_PROTOCOL_DRIVERS; ii++) {
1494                 if(MptDeviceDriverHandlers[ii] &&
1495                   MptDeviceDriverHandlers[ii]->probe) {
1496                         MptDeviceDriverHandlers[ii]->probe(pdev,id);
1497                 }
1498         }
1499
1500 #ifdef CONFIG_PROC_FS
1501         /*
1502          *  Create "/proc/mpt/iocN" subdirectory entry for each MPT adapter.
1503          */
1504         dent = proc_mkdir(ioc->name, mpt_proc_root_dir);
1505         if (dent) {
1506                 ent = create_proc_entry("info", S_IFREG|S_IRUGO, dent);
1507                 if (ent) {
1508                         ent->read_proc = procmpt_iocinfo_read;
1509                         ent->data = ioc;
1510                 }
1511                 ent = create_proc_entry("summary", S_IFREG|S_IRUGO, dent);
1512                 if (ent) {
1513                         ent->read_proc = procmpt_summary_read;
1514                         ent->data = ioc;
1515                 }
1516         }
1517 #endif
1518
1519         return 0;
1520 }
1521
1522 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1523 /*
1524  *      mpt_detach - Remove a PCI intelligent MPT adapter.
1525  *      @pdev: Pointer to pci_dev structure
1526  *
1527  */
1528
1529 void
1530 mpt_detach(struct pci_dev *pdev)
1531 {
1532         MPT_ADAPTER     *ioc = pci_get_drvdata(pdev);
1533         char pname[32];
1534         int ii;
1535
1536         sprintf(pname, MPT_PROCFS_MPTBASEDIR "/%s/summary", ioc->name);
1537         remove_proc_entry(pname, NULL);
1538         sprintf(pname, MPT_PROCFS_MPTBASEDIR "/%s/info", ioc->name);
1539         remove_proc_entry(pname, NULL);
1540         sprintf(pname, MPT_PROCFS_MPTBASEDIR "/%s", ioc->name);
1541         remove_proc_entry(pname, NULL);
1542
1543         /* call per device driver remove entry point */
1544         for(ii=0; ii<MPT_MAX_PROTOCOL_DRIVERS; ii++) {
1545                 if(MptDeviceDriverHandlers[ii] &&
1546                   MptDeviceDriverHandlers[ii]->remove) {
1547                         MptDeviceDriverHandlers[ii]->remove(pdev);
1548                 }
1549         }
1550
1551         /* Disable interrupts! */
1552         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1553
1554         ioc->active = 0;
1555         synchronize_irq(pdev->irq);
1556
1557         /* Clear any lingering interrupt */
1558         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1559
1560         CHIPREG_READ32(&ioc->chip->IntStatus);
1561
1562         mpt_adapter_dispose(ioc);
1563
1564         pci_set_drvdata(pdev, NULL);
1565 }
1566
1567 /**************************************************************************
1568  * Power Management
1569  */
1570 #ifdef CONFIG_PM
1571 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1572 /*
1573  *      mpt_suspend - Fusion MPT base driver suspend routine.
1574  *
1575  *
1576  */
1577 int
1578 mpt_suspend(struct pci_dev *pdev, pm_message_t state)
1579 {
1580         u32 device_state;
1581         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1582
1583         device_state=pci_choose_state(pdev, state);
1584
1585         printk(MYIOC_s_INFO_FMT
1586         "pci-suspend: pdev=0x%p, slot=%s, Entering operating state [D%d]\n",
1587                 ioc->name, pdev, pci_name(pdev), device_state);
1588
1589         pci_save_state(pdev);
1590
1591         /* put ioc into READY_STATE */
1592         if(SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, CAN_SLEEP)) {
1593                 printk(MYIOC_s_ERR_FMT
1594                 "pci-suspend:  IOC msg unit reset failed!\n", ioc->name);
1595         }
1596
1597         /* disable interrupts */
1598         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1599         ioc->active = 0;
1600
1601         /* Clear any lingering interrupt */
1602         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
1603
1604         pci_disable_device(pdev);
1605         pci_set_power_state(pdev, device_state);
1606
1607         return 0;
1608 }
1609
1610 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1611 /*
1612  *      mpt_resume - Fusion MPT base driver resume routine.
1613  *
1614  *
1615  */
1616 int
1617 mpt_resume(struct pci_dev *pdev)
1618 {
1619         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1620         u32 device_state = pdev->current_state;
1621         int recovery_state;
1622         int ii;
1623
1624         printk(MYIOC_s_INFO_FMT
1625         "pci-resume: pdev=0x%p, slot=%s, Previous operating state [D%d]\n",
1626                 ioc->name, pdev, pci_name(pdev), device_state);
1627
1628         pci_set_power_state(pdev, 0);
1629         pci_restore_state(pdev);
1630         pci_enable_device(pdev);
1631
1632         /* enable interrupts */
1633         CHIPREG_WRITE32(&ioc->chip->IntMask, MPI_HIM_DIM);
1634         ioc->active = 1;
1635
1636         /* F/W not running */
1637         if(!CHIPREG_READ32(&ioc->chip->Doorbell)) {
1638                 /* enable domain validation flags */
1639                 for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
1640                         ioc->spi_data.dvStatus[ii] |= MPT_SCSICFG_NEED_DV;
1641                 }
1642         }
1643
1644         printk(MYIOC_s_INFO_FMT
1645                 "pci-resume: ioc-state=0x%x,doorbell=0x%x\n",
1646                 ioc->name,
1647                 (mpt_GetIocState(ioc, 1) >> MPI_IOC_STATE_SHIFT),
1648                 CHIPREG_READ32(&ioc->chip->Doorbell));
1649
1650         /* bring ioc to operational state */
1651         if ((recovery_state = mpt_do_ioc_recovery(ioc,
1652             MPT_HOSTEVENT_IOC_RECOVER, CAN_SLEEP)) != 0) {
1653                 printk(MYIOC_s_INFO_FMT
1654                         "pci-resume: Cannot recover, error:[%x]\n",
1655                         ioc->name, recovery_state);
1656         } else {
1657                 printk(MYIOC_s_INFO_FMT
1658                         "pci-resume: success\n", ioc->name);
1659         }
1660
1661         return 0;
1662 }
1663 #endif
1664
1665 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1666 /*
1667  *      mpt_do_ioc_recovery - Initialize or recover MPT adapter.
1668  *      @ioc: Pointer to MPT adapter structure
1669  *      @reason: Event word / reason
1670  *      @sleepFlag: Use schedule if CAN_SLEEP else use udelay.
1671  *
1672  *      This routine performs all the steps necessary to bring the IOC
1673  *      to a OPERATIONAL state.
1674  *
1675  *      This routine also pre-fetches the LAN MAC address of a Fibre Channel
1676  *      MPT adapter.
1677  *
1678  *      Returns:
1679  *               0 for success
1680  *              -1 if failed to get board READY
1681  *              -2 if READY but IOCFacts Failed
1682  *              -3 if READY but PrimeIOCFifos Failed
1683  *              -4 if READY but IOCInit Failed
1684  */
1685 static int
1686 mpt_do_ioc_recovery(MPT_ADAPTER *ioc, u32 reason, int sleepFlag)
1687 {
1688         int      hard_reset_done = 0;
1689         int      alt_ioc_ready = 0;
1690         int      hard;
1691         int      rc=0;
1692         int      ii;
1693         int      handlers;
1694         int      ret = 0;
1695         int      reset_alt_ioc_active = 0;
1696
1697         printk(KERN_INFO MYNAM ": Initiating %s %s\n",
1698                         ioc->name, reason==MPT_HOSTEVENT_IOC_BRINGUP ? "bringup" : "recovery");
1699
1700         /* Disable reply interrupts (also blocks FreeQ) */
1701         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
1702         ioc->active = 0;
1703
1704         if (ioc->alt_ioc) {
1705                 if (ioc->alt_ioc->active)
1706                         reset_alt_ioc_active = 1;
1707
1708                 /* Disable alt-IOC's reply interrupts (and FreeQ) for a bit ... */
1709                 CHIPREG_WRITE32(&ioc->alt_ioc->chip->IntMask, 0xFFFFFFFF);
1710                 ioc->alt_ioc->active = 0;
1711         }
1712
1713         hard = 1;
1714         if (reason == MPT_HOSTEVENT_IOC_BRINGUP)
1715                 hard = 0;
1716
1717         if ((hard_reset_done = MakeIocReady(ioc, hard, sleepFlag)) < 0) {
1718                 if (hard_reset_done == -4) {
1719                         printk(KERN_WARNING MYNAM ": %s Owned by PEER..skipping!\n",
1720                                         ioc->name);
1721
1722                         if (reset_alt_ioc_active && ioc->alt_ioc) {
1723                                 /* (re)Enable alt-IOC! (reply interrupt, FreeQ) */
1724                                 dprintk((KERN_INFO MYNAM ": alt-%s reply irq re-enabled\n",
1725                                                 ioc->alt_ioc->name));
1726                                 CHIPREG_WRITE32(&ioc->alt_ioc->chip->IntMask, MPI_HIM_DIM);
1727                                 ioc->alt_ioc->active = 1;
1728                         }
1729
1730                 } else {
1731                         printk(KERN_WARNING MYNAM ": %s NOT READY WARNING!\n",
1732                                         ioc->name);
1733                 }
1734                 return -1;
1735         }
1736
1737         /* hard_reset_done = 0 if a soft reset was performed
1738          * and 1 if a hard reset was performed.
1739          */
1740         if (hard_reset_done && reset_alt_ioc_active && ioc->alt_ioc) {
1741                 if ((rc = MakeIocReady(ioc->alt_ioc, 0, sleepFlag)) == 0)
1742                         alt_ioc_ready = 1;
1743                 else
1744                         printk(KERN_WARNING MYNAM
1745                                         ": alt-%s: Not ready WARNING!\n",
1746                                         ioc->alt_ioc->name);
1747         }
1748
1749         for (ii=0; ii<5; ii++) {
1750                 /* Get IOC facts! Allow 5 retries */
1751                 if ((rc = GetIocFacts(ioc, sleepFlag, reason)) == 0)
1752                         break;
1753         }
1754
1755
1756         if (ii == 5) {
1757                 dinitprintk((MYIOC_s_INFO_FMT "Retry IocFacts failed rc=%x\n", ioc->name, rc));
1758                 ret = -2;
1759         } else if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
1760                 MptDisplayIocCapabilities(ioc);
1761         }
1762
1763         if (alt_ioc_ready) {
1764                 if ((rc = GetIocFacts(ioc->alt_ioc, sleepFlag, reason)) != 0) {
1765                         dinitprintk((MYIOC_s_INFO_FMT "Initial Alt IocFacts failed rc=%x\n", ioc->name, rc));
1766                         /* Retry - alt IOC was initialized once
1767                          */
1768                         rc = GetIocFacts(ioc->alt_ioc, sleepFlag, reason);
1769                 }
1770                 if (rc) {
1771                         dinitprintk((MYIOC_s_INFO_FMT "Retry Alt IocFacts failed rc=%x\n", ioc->name, rc));
1772                         alt_ioc_ready = 0;
1773                         reset_alt_ioc_active = 0;
1774                 } else if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
1775                         MptDisplayIocCapabilities(ioc->alt_ioc);
1776                 }
1777         }
1778
1779         /* Prime reply & request queues!
1780          * (mucho alloc's) Must be done prior to
1781          * init as upper addresses are needed for init.
1782          * If fails, continue with alt-ioc processing
1783          */
1784         if ((ret == 0) && ((rc = PrimeIocFifos(ioc)) != 0))
1785                 ret = -3;
1786
1787         /* May need to check/upload firmware & data here!
1788          * If fails, continue with alt-ioc processing
1789          */
1790         if ((ret == 0) && ((rc = SendIocInit(ioc, sleepFlag)) != 0))
1791                 ret = -4;
1792 // NEW!
1793         if (alt_ioc_ready && ((rc = PrimeIocFifos(ioc->alt_ioc)) != 0)) {
1794                 printk(KERN_WARNING MYNAM ": alt-%s: (%d) FIFO mgmt alloc WARNING!\n",
1795                                 ioc->alt_ioc->name, rc);
1796                 alt_ioc_ready = 0;
1797                 reset_alt_ioc_active = 0;
1798         }
1799
1800         if (alt_ioc_ready) {
1801                 if ((rc = SendIocInit(ioc->alt_ioc, sleepFlag)) != 0) {
1802                         alt_ioc_ready = 0;
1803                         reset_alt_ioc_active = 0;
1804                         printk(KERN_WARNING MYNAM
1805                                 ": alt-%s: (%d) init failure WARNING!\n",
1806                                         ioc->alt_ioc->name, rc);
1807                 }
1808         }
1809
1810         if (reason == MPT_HOSTEVENT_IOC_BRINGUP){
1811                 if (ioc->upload_fw) {
1812                         ddlprintk((MYIOC_s_INFO_FMT
1813                                 "firmware upload required!\n", ioc->name));
1814
1815                         /* Controller is not operational, cannot do upload
1816                          */
1817                         if (ret == 0) {
1818                                 rc = mpt_do_upload(ioc, sleepFlag);
1819                                 if (rc == 0) {
1820                                         if (ioc->alt_ioc && ioc->alt_ioc->cached_fw) {
1821                                                 /*
1822                                                  * Maintain only one pointer to FW memory
1823                                                  * so there will not be two attempt to
1824                                                  * downloadboot onboard dual function
1825                                                  * chips (mpt_adapter_disable,
1826                                                  * mpt_diag_reset)
1827                                                  */
1828                                                 ioc->cached_fw = NULL;
1829                                                 ddlprintk((MYIOC_s_INFO_FMT ": mpt_upload:  alt_%s has cached_fw=%p \n",
1830                                                         ioc->name, ioc->alt_ioc->name, ioc->alt_ioc->cached_fw));
1831                                         }
1832                                 } else {
1833                                         printk(KERN_WARNING MYNAM ": firmware upload failure!\n");
1834                                         ret = -5;
1835                                 }
1836                         }
1837                 }
1838         }
1839
1840         if (ret == 0) {
1841                 /* Enable! (reply interrupt) */
1842                 CHIPREG_WRITE32(&ioc->chip->IntMask, MPI_HIM_DIM);
1843                 ioc->active = 1;
1844         }
1845
1846         if (reset_alt_ioc_active && ioc->alt_ioc) {
1847                 /* (re)Enable alt-IOC! (reply interrupt) */
1848                 dinitprintk((KERN_INFO MYNAM ": alt-%s reply irq re-enabled\n",
1849                                 ioc->alt_ioc->name));
1850                 CHIPREG_WRITE32(&ioc->alt_ioc->chip->IntMask, MPI_HIM_DIM);
1851                 ioc->alt_ioc->active = 1;
1852         }
1853
1854         /*  Enable MPT base driver management of EventNotification
1855          *  and EventAck handling.
1856          */
1857         if ((ret == 0) && (!ioc->facts.EventState))
1858                 (void) SendEventNotification(ioc, 1);   /* 1=Enable EventNotification */
1859
1860         if (ioc->alt_ioc && alt_ioc_ready && !ioc->alt_ioc->facts.EventState)
1861                 (void) SendEventNotification(ioc->alt_ioc, 1);  /* 1=Enable EventNotification */
1862
1863         /*      Add additional "reason" check before call to GetLanConfigPages
1864          *      (combined with GetIoUnitPage2 call).  This prevents a somewhat
1865          *      recursive scenario; GetLanConfigPages times out, timer expired
1866          *      routine calls HardResetHandler, which calls into here again,
1867          *      and we try GetLanConfigPages again...
1868          */
1869         if ((ret == 0) && (reason == MPT_HOSTEVENT_IOC_BRINGUP)) {
1870                 if (ioc->bus_type == SAS) {
1871
1872                         /* clear persistency table */
1873                         if(ioc->facts.IOCExceptions &
1874                             MPI_IOCFACTS_EXCEPT_PERSISTENT_TABLE_FULL) {
1875                                 ret = mptbase_sas_persist_operation(ioc,
1876                                     MPI_SAS_OP_CLEAR_NOT_PRESENT);
1877                                 if(ret != 0)
1878                                         return -1;
1879                         }
1880
1881                         /* Find IM volumes
1882                          */
1883                         mpt_findImVolumes(ioc);
1884
1885                 } else if (ioc->bus_type == FC) {
1886                         /*
1887                          *  Pre-fetch FC port WWN and stuff...
1888                          *  (FCPortPage0_t stuff)
1889                          */
1890                         for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
1891                                 (void) mptbase_GetFcPortPage0(ioc, ii);
1892                         }
1893
1894                         if ((ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_LAN) &&
1895                             (ioc->lan_cnfg_page0.Header.PageLength == 0)) {
1896                                 /*
1897                                  *  Pre-fetch the ports LAN MAC address!
1898                                  *  (LANPage1_t stuff)
1899                                  */
1900                                 (void) GetLanConfigPages(ioc);
1901 #ifdef MPT_DEBUG
1902                                 {
1903                                         u8 *a = (u8*)&ioc->lan_cnfg_page1.HardwareAddressLow;
1904                                         dprintk((MYIOC_s_INFO_FMT "LanAddr = %02X:%02X:%02X:%02X:%02X:%02X\n",
1905                                                         ioc->name, a[5], a[4], a[3], a[2], a[1], a[0] ));
1906                                 }
1907 #endif
1908                         }
1909                 } else {
1910                         /* Get NVRAM and adapter maximums from SPP 0 and 2
1911                          */
1912                         mpt_GetScsiPortSettings(ioc, 0);
1913
1914                         /* Get version and length of SDP 1
1915                          */
1916                         mpt_readScsiDevicePageHeaders(ioc, 0);
1917
1918                         /* Find IM volumes
1919                          */
1920                         if (ioc->facts.MsgVersion >= MPI_VERSION_01_02)
1921                                 mpt_findImVolumes(ioc);
1922
1923                         /* Check, and possibly reset, the coalescing value
1924                          */
1925                         mpt_read_ioc_pg_1(ioc);
1926
1927                         mpt_read_ioc_pg_4(ioc);
1928                 }
1929
1930                 GetIoUnitPage2(ioc);
1931         }
1932
1933         /*
1934          * Call each currently registered protocol IOC reset handler
1935          * with post-reset indication.
1936          * NOTE: If we're doing _IOC_BRINGUP, there can be no
1937          * MptResetHandlers[] registered yet.
1938          */
1939         if (hard_reset_done) {
1940                 rc = handlers = 0;
1941                 for (ii=MPT_MAX_PROTOCOL_DRIVERS-1; ii; ii--) {
1942                         if ((ret == 0) && MptResetHandlers[ii]) {
1943                                 dprintk((MYIOC_s_INFO_FMT "Calling IOC post_reset handler #%d\n",
1944                                                 ioc->name, ii));
1945                                 rc += (*(MptResetHandlers[ii]))(ioc, MPT_IOC_POST_RESET);
1946                                 handlers++;
1947                         }
1948
1949                         if (alt_ioc_ready && MptResetHandlers[ii]) {
1950                                 drsprintk((MYIOC_s_INFO_FMT "Calling alt-%s post_reset handler #%d\n",
1951                                                 ioc->name, ioc->alt_ioc->name, ii));
1952                                 rc += (*(MptResetHandlers[ii]))(ioc->alt_ioc, MPT_IOC_POST_RESET);
1953                                 handlers++;
1954                         }
1955                 }
1956                 /* FIXME?  Examine results here? */
1957         }
1958
1959         return ret;
1960 }
1961
1962 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1963 /*
1964  *      mpt_detect_bound_ports - Search for PCI bus/dev_function
1965  *      which matches PCI bus/dev_function (+/-1) for newly discovered 929,
1966  *      929X, 1030 or 1035.
1967  *      @ioc: Pointer to MPT adapter structure
1968  *      @pdev: Pointer to (struct pci_dev) structure
1969  *
1970  *      If match on PCI dev_function +/-1 is found, bind the two MPT adapters
1971  *      using alt_ioc pointer fields in their %MPT_ADAPTER structures.
1972  */
1973 static void
1974 mpt_detect_bound_ports(MPT_ADAPTER *ioc, struct pci_dev *pdev)
1975 {
1976         struct pci_dev *peer=NULL;
1977         unsigned int slot = PCI_SLOT(pdev->devfn);
1978         unsigned int func = PCI_FUNC(pdev->devfn);
1979         MPT_ADAPTER *ioc_srch;
1980
1981         dprintk((MYIOC_s_INFO_FMT "PCI device %s devfn=%x/%x,"
1982             " searching for devfn match on %x or %x\n",
1983                 ioc->name, pci_name(pdev), pdev->bus->number,
1984                 pdev->devfn, func-1, func+1));
1985
1986         peer = pci_get_slot(pdev->bus, PCI_DEVFN(slot,func-1));
1987         if (!peer) {
1988                 peer = pci_get_slot(pdev->bus, PCI_DEVFN(slot,func+1));
1989                 if (!peer)
1990                         return;
1991         }
1992
1993         list_for_each_entry(ioc_srch, &ioc_list, list) {
1994                 struct pci_dev *_pcidev = ioc_srch->pcidev;
1995                 if (_pcidev == peer) {
1996                         /* Paranoia checks */
1997                         if (ioc->alt_ioc != NULL) {
1998                                 printk(KERN_WARNING MYNAM ": Oops, already bound (%s <==> %s)!\n",
1999                                         ioc->name, ioc->alt_ioc->name);
2000                                 break;
2001                         } else if (ioc_srch->alt_ioc != NULL) {
2002                                 printk(KERN_WARNING MYNAM ": Oops, already bound (%s <==> %s)!\n",
2003                                         ioc_srch->name, ioc_srch->alt_ioc->name);
2004                                 break;
2005                         }
2006                         dprintk((KERN_INFO MYNAM ": FOUND! binding %s <==> %s\n",
2007                                 ioc->name, ioc_srch->name));
2008                         ioc_srch->alt_ioc = ioc;
2009                         ioc->alt_ioc = ioc_srch;
2010                 }
2011         }
2012         pci_dev_put(peer);
2013 }
2014
2015 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2016 /*
2017  *      mpt_adapter_disable - Disable misbehaving MPT adapter.
2018  *      @this: Pointer to MPT adapter structure
2019  */
2020 static void
2021 mpt_adapter_disable(MPT_ADAPTER *ioc)
2022 {
2023         int sz;
2024         int ret;
2025
2026         if (ioc->cached_fw != NULL) {
2027                 ddlprintk((KERN_INFO MYNAM ": mpt_adapter_disable: Pushing FW onto adapter\n"));
2028                 if ((ret = mpt_downloadboot(ioc, (MpiFwHeader_t *)ioc->cached_fw, NO_SLEEP)) < 0) {
2029                         printk(KERN_WARNING MYNAM
2030                                 ": firmware downloadboot failure (%d)!\n", ret);
2031                 }
2032         }
2033
2034         /* Disable adapter interrupts! */
2035         CHIPREG_WRITE32(&ioc->chip->IntMask, 0xFFFFFFFF);
2036         ioc->active = 0;
2037         /* Clear any lingering interrupt */
2038         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
2039
2040         if (ioc->alloc != NULL) {
2041                 sz = ioc->alloc_sz;
2042                 dexitprintk((KERN_INFO MYNAM ": %s.free  @ %p, sz=%d bytes\n",
2043                         ioc->name, ioc->alloc, ioc->alloc_sz));
2044                 pci_free_consistent(ioc->pcidev, sz,
2045                                 ioc->alloc, ioc->alloc_dma);
2046                 ioc->reply_frames = NULL;
2047                 ioc->req_frames = NULL;
2048                 ioc->alloc = NULL;
2049                 ioc->alloc_total -= sz;
2050         }
2051
2052         if (ioc->sense_buf_pool != NULL) {
2053                 sz = (ioc->req_depth * MPT_SENSE_BUFFER_ALLOC);
2054                 pci_free_consistent(ioc->pcidev, sz,
2055                                 ioc->sense_buf_pool, ioc->sense_buf_pool_dma);
2056                 ioc->sense_buf_pool = NULL;
2057                 ioc->alloc_total -= sz;
2058         }
2059
2060         if (ioc->events != NULL){
2061                 sz = MPTCTL_EVENT_LOG_SIZE * sizeof(MPT_IOCTL_EVENTS);
2062                 kfree(ioc->events);
2063                 ioc->events = NULL;
2064                 ioc->alloc_total -= sz;
2065         }
2066
2067         if (ioc->cached_fw != NULL) {
2068                 sz = ioc->facts.FWImageSize;
2069                 pci_free_consistent(ioc->pcidev, sz,
2070                         ioc->cached_fw, ioc->cached_fw_dma);
2071                 ioc->cached_fw = NULL;
2072                 ioc->alloc_total -= sz;
2073         }
2074
2075         kfree(ioc->spi_data.nvram);
2076         kfree(ioc->raid_data.pIocPg3);
2077         ioc->spi_data.nvram = NULL;
2078         ioc->raid_data.pIocPg3 = NULL;
2079
2080         if (ioc->spi_data.pIocPg4 != NULL) {
2081                 sz = ioc->spi_data.IocPg4Sz;
2082                 pci_free_consistent(ioc->pcidev, sz, 
2083                         ioc->spi_data.pIocPg4,
2084                         ioc->spi_data.IocPg4_dma);
2085                 ioc->spi_data.pIocPg4 = NULL;
2086                 ioc->alloc_total -= sz;
2087         }
2088
2089         if (ioc->ReqToChain != NULL) {
2090                 kfree(ioc->ReqToChain);
2091                 kfree(ioc->RequestNB);
2092                 ioc->ReqToChain = NULL;
2093         }
2094
2095         kfree(ioc->ChainToChain);
2096         ioc->ChainToChain = NULL;
2097
2098         if (ioc->HostPageBuffer != NULL) {
2099                 if((ret = mpt_host_page_access_control(ioc,
2100                     MPI_DB_HPBAC_FREE_BUFFER, NO_SLEEP)) != 0) {
2101                         printk(KERN_ERR MYNAM
2102                            ": %s: host page buffers free failed (%d)!\n",
2103                             __FUNCTION__, ret);
2104                 }
2105                 dexitprintk((KERN_INFO MYNAM ": %s HostPageBuffer free  @ %p, sz=%d bytes\n",
2106                         ioc->name, ioc->HostPageBuffer, ioc->HostPageBuffer_sz));
2107                 pci_free_consistent(ioc->pcidev, ioc->HostPageBuffer_sz,
2108                                 ioc->HostPageBuffer,
2109                                 ioc->HostPageBuffer_dma);
2110                 ioc->HostPageBuffer = NULL;
2111                 ioc->HostPageBuffer_sz = 0;
2112                 ioc->alloc_total -= ioc->HostPageBuffer_sz;
2113         }
2114 }
2115
2116 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2117 /*
2118  *      mpt_adapter_dispose - Free all resources associated with a MPT
2119  *      adapter.
2120  *      @ioc: Pointer to MPT adapter structure
2121  *
2122  *      This routine unregisters h/w resources and frees all alloc'd memory
2123  *      associated with a MPT adapter structure.
2124  */
2125 static void
2126 mpt_adapter_dispose(MPT_ADAPTER *ioc)
2127 {
2128         int sz_first, sz_last;
2129
2130         if (ioc == NULL)
2131                 return;
2132
2133         sz_first = ioc->alloc_total;
2134
2135         mpt_adapter_disable(ioc);
2136
2137         if (ioc->pci_irq != -1) {
2138                 free_irq(ioc->pci_irq, ioc);
2139                 ioc->pci_irq = -1;
2140         }
2141
2142         if (ioc->memmap != NULL) {
2143                 iounmap(ioc->memmap);
2144                 ioc->memmap = NULL;
2145         }
2146
2147 #if defined(CONFIG_MTRR) && 0
2148         if (ioc->mtrr_reg > 0) {
2149                 mtrr_del(ioc->mtrr_reg, 0, 0);
2150                 dprintk((KERN_INFO MYNAM ": %s: MTRR region de-registered\n", ioc->name));
2151         }
2152 #endif
2153
2154         /*  Zap the adapter lookup ptr!  */
2155         list_del(&ioc->list);
2156
2157         sz_last = ioc->alloc_total;
2158         dprintk((KERN_INFO MYNAM ": %s: free'd %d of %d bytes\n",
2159                         ioc->name, sz_first-sz_last+(int)sizeof(*ioc), sz_first));
2160         kfree(ioc);
2161 }
2162
2163 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2164 /*
2165  *      MptDisplayIocCapabilities - Disply IOC's capacilities.
2166  *      @ioc: Pointer to MPT adapter structure
2167  */
2168 static void
2169 MptDisplayIocCapabilities(MPT_ADAPTER *ioc)
2170 {
2171         int i = 0;
2172
2173         printk(KERN_INFO "%s: ", ioc->name);
2174         if (ioc->prod_name && strlen(ioc->prod_name) > 3)
2175                 printk("%s: ", ioc->prod_name+3);
2176         printk("Capabilities={");
2177
2178         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
2179                 printk("Initiator");
2180                 i++;
2181         }
2182
2183         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
2184                 printk("%sTarget", i ? "," : "");
2185                 i++;
2186         }
2187
2188         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_LAN) {
2189                 printk("%sLAN", i ? "," : "");
2190                 i++;
2191         }
2192
2193 #if 0
2194         /*
2195          *  This would probably evoke more questions than it's worth
2196          */
2197         if (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
2198                 printk("%sLogBusAddr", i ? "," : "");
2199                 i++;
2200         }
2201 #endif
2202
2203         printk("}\n");
2204 }
2205
2206 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2207 /*
2208  *      MakeIocReady - Get IOC to a READY state, using KickStart if needed.
2209  *      @ioc: Pointer to MPT_ADAPTER structure
2210  *      @force: Force hard KickStart of IOC
2211  *      @sleepFlag: Specifies whether the process can sleep
2212  *
2213  *      Returns:
2214  *               1 - DIAG reset and READY
2215  *               0 - READY initially OR soft reset and READY
2216  *              -1 - Any failure on KickStart
2217  *              -2 - Msg Unit Reset Failed
2218  *              -3 - IO Unit Reset Failed
2219  *              -4 - IOC owned by a PEER
2220  */
2221 static int
2222 MakeIocReady(MPT_ADAPTER *ioc, int force, int sleepFlag)
2223 {
2224         u32      ioc_state;
2225         int      statefault = 0;
2226         int      cntdn;
2227         int      hard_reset_done = 0;
2228         int      r;
2229         int      ii;
2230         int      whoinit;
2231
2232         /* Get current [raw] IOC state  */
2233         ioc_state = mpt_GetIocState(ioc, 0);
2234         dhsprintk((KERN_INFO MYNAM "::MakeIocReady, %s [raw] state=%08x\n", ioc->name, ioc_state));
2235
2236         /*
2237          *      Check to see if IOC got left/stuck in doorbell handshake
2238          *      grip of death.  If so, hard reset the IOC.
2239          */
2240         if (ioc_state & MPI_DOORBELL_ACTIVE) {
2241                 statefault = 1;
2242                 printk(MYIOC_s_WARN_FMT "Unexpected doorbell active!\n",
2243                                 ioc->name);
2244         }
2245
2246         /* Is it already READY? */
2247         if (!statefault && (ioc_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_READY)
2248                 return 0;
2249
2250         /*
2251          *      Check to see if IOC is in FAULT state.
2252          */
2253         if ((ioc_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_FAULT) {
2254                 statefault = 2;
2255                 printk(MYIOC_s_WARN_FMT "IOC is in FAULT state!!!\n",
2256                                 ioc->name);
2257                 printk(KERN_WARNING "           FAULT code = %04xh\n",
2258                                 ioc_state & MPI_DOORBELL_DATA_MASK);
2259         }
2260
2261         /*
2262          *      Hmmm...  Did it get left operational?
2263          */
2264         if ((ioc_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_OPERATIONAL) {
2265                 dinitprintk((MYIOC_s_INFO_FMT "IOC operational unexpected\n",
2266                                 ioc->name));
2267
2268                 /* Check WhoInit.
2269                  * If PCI Peer, exit.
2270                  * Else, if no fault conditions are present, issue a MessageUnitReset
2271                  * Else, fall through to KickStart case
2272                  */
2273                 whoinit = (ioc_state & MPI_DOORBELL_WHO_INIT_MASK) >> MPI_DOORBELL_WHO_INIT_SHIFT;
2274                 dinitprintk((KERN_INFO MYNAM
2275                         ": whoinit 0x%x statefault %d force %d\n",
2276                         whoinit, statefault, force));
2277                 if (whoinit == MPI_WHOINIT_PCI_PEER)
2278                         return -4;
2279                 else {
2280                         if ((statefault == 0 ) && (force == 0)) {
2281                                 if ((r = SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, sleepFlag)) == 0)
2282                                         return 0;
2283                         }
2284                         statefault = 3;
2285                 }
2286         }
2287
2288         hard_reset_done = KickStart(ioc, statefault||force, sleepFlag);
2289         if (hard_reset_done < 0)
2290                 return -1;
2291
2292         /*
2293          *  Loop here waiting for IOC to come READY.
2294          */
2295         ii = 0;
2296         cntdn = ((sleepFlag == CAN_SLEEP) ? HZ : 1000) * 5;     /* 5 seconds */
2297
2298         while ((ioc_state = mpt_GetIocState(ioc, 1)) != MPI_IOC_STATE_READY) {
2299                 if (ioc_state == MPI_IOC_STATE_OPERATIONAL) {
2300                         /*
2301                          *  BIOS or previous driver load left IOC in OP state.
2302                          *  Reset messaging FIFOs.
2303                          */
2304                         if ((r = SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, sleepFlag)) != 0) {
2305                                 printk(MYIOC_s_ERR_FMT "IOC msg unit reset failed!\n", ioc->name);
2306                                 return -2;
2307                         }
2308                 } else if (ioc_state == MPI_IOC_STATE_RESET) {
2309                         /*
2310                          *  Something is wrong.  Try to get IOC back
2311                          *  to a known state.
2312                          */
2313                         if ((r = SendIocReset(ioc, MPI_FUNCTION_IO_UNIT_RESET, sleepFlag)) != 0) {
2314                                 printk(MYIOC_s_ERR_FMT "IO unit reset failed!\n", ioc->name);
2315                                 return -3;
2316                         }
2317                 }
2318
2319                 ii++; cntdn--;
2320                 if (!cntdn) {
2321                         printk(MYIOC_s_ERR_FMT "Wait IOC_READY state timeout(%d)!\n",
2322                                         ioc->name, (int)((ii+5)/HZ));
2323                         return -ETIME;
2324                 }
2325
2326                 if (sleepFlag == CAN_SLEEP) {
2327                         msleep_interruptible(1);
2328                 } else {
2329                         mdelay (1);     /* 1 msec delay */
2330                 }
2331
2332         }
2333
2334         if (statefault < 3) {
2335                 printk(MYIOC_s_INFO_FMT "Recovered from %s\n",
2336                                 ioc->name,
2337                                 statefault==1 ? "stuck handshake" : "IOC FAULT");
2338         }
2339
2340         return hard_reset_done;
2341 }
2342
2343 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2344 /*
2345  *      mpt_GetIocState - Get the current state of a MPT adapter.
2346  *      @ioc: Pointer to MPT_ADAPTER structure
2347  *      @cooked: Request raw or cooked IOC state
2348  *
2349  *      Returns all IOC Doorbell register bits if cooked==0, else just the
2350  *      Doorbell bits in MPI_IOC_STATE_MASK.
2351  */
2352 u32
2353 mpt_GetIocState(MPT_ADAPTER *ioc, int cooked)
2354 {
2355         u32 s, sc;
2356
2357         /*  Get!  */
2358         s = CHIPREG_READ32(&ioc->chip->Doorbell);
2359 //      dprintk((MYIOC_s_INFO_FMT "raw state = %08x\n", ioc->name, s));
2360         sc = s & MPI_IOC_STATE_MASK;
2361
2362         /*  Save!  */
2363         ioc->last_state = sc;
2364
2365         return cooked ? sc : s;
2366 }
2367
2368 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2369 /*
2370  *      GetIocFacts - Send IOCFacts request to MPT adapter.
2371  *      @ioc: Pointer to MPT_ADAPTER structure
2372  *      @sleepFlag: Specifies whether the process can sleep
2373  *      @reason: If recovery, only update facts.
2374  *
2375  *      Returns 0 for success, non-zero for failure.
2376  */
2377 static int
2378 GetIocFacts(MPT_ADAPTER *ioc, int sleepFlag, int reason)
2379 {
2380         IOCFacts_t               get_facts;
2381         IOCFactsReply_t         *facts;
2382         int                      r;
2383         int                      req_sz;
2384         int                      reply_sz;
2385         int                      sz;
2386         u32                      status, vv;
2387         u8                       shiftFactor=1;
2388
2389         /* IOC *must* NOT be in RESET state! */
2390         if (ioc->last_state == MPI_IOC_STATE_RESET) {
2391                 printk(KERN_ERR MYNAM ": ERROR - Can't get IOCFacts, %s NOT READY! (%08x)\n",
2392                                 ioc->name,
2393                                 ioc->last_state );
2394                 return -44;
2395         }
2396
2397         facts = &ioc->facts;
2398
2399         /* Destination (reply area)... */
2400         reply_sz = sizeof(*facts);
2401         memset(facts, 0, reply_sz);
2402
2403         /* Request area (get_facts on the stack right now!) */
2404         req_sz = sizeof(get_facts);
2405         memset(&get_facts, 0, req_sz);
2406
2407         get_facts.Function = MPI_FUNCTION_IOC_FACTS;
2408         /* Assert: All other get_facts fields are zero! */
2409
2410         dinitprintk((MYIOC_s_INFO_FMT
2411             "Sending get IocFacts request req_sz=%d reply_sz=%d\n",
2412             ioc->name, req_sz, reply_sz));
2413
2414         /* No non-zero fields in the get_facts request are greater than
2415          * 1 byte in size, so we can just fire it off as is.
2416          */
2417         r = mpt_handshake_req_reply_wait(ioc, req_sz, (u32*)&get_facts,
2418                         reply_sz, (u16*)facts, 5 /*seconds*/, sleepFlag);
2419         if (r != 0)
2420                 return r;
2421
2422         /*
2423          * Now byte swap (GRRR) the necessary fields before any further
2424          * inspection of reply contents.
2425          *
2426          * But need to do some sanity checks on MsgLength (byte) field
2427          * to make sure we don't zero IOC's req_sz!
2428          */
2429         /* Did we get a valid reply? */
2430         if (facts->MsgLength > offsetof(IOCFactsReply_t, RequestFrameSize)/sizeof(u32)) {
2431                 if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
2432                         /*
2433                          * If not been here, done that, save off first WhoInit value
2434                          */
2435                         if (ioc->FirstWhoInit == WHOINIT_UNKNOWN)
2436                                 ioc->FirstWhoInit = facts->WhoInit;
2437                 }
2438
2439                 facts->MsgVersion = le16_to_cpu(facts->MsgVersion);
2440                 facts->MsgContext = le32_to_cpu(facts->MsgContext);
2441                 facts->IOCExceptions = le16_to_cpu(facts->IOCExceptions);
2442                 facts->IOCStatus = le16_to_cpu(facts->IOCStatus);
2443                 facts->IOCLogInfo = le32_to_cpu(facts->IOCLogInfo);
2444                 status = le16_to_cpu(facts->IOCStatus) & MPI_IOCSTATUS_MASK;
2445                 /* CHECKME! IOCStatus, IOCLogInfo */
2446
2447                 facts->ReplyQueueDepth = le16_to_cpu(facts->ReplyQueueDepth);
2448                 facts->RequestFrameSize = le16_to_cpu(facts->RequestFrameSize);
2449
2450                 /*
2451                  * FC f/w version changed between 1.1 and 1.2
2452                  *      Old: u16{Major(4),Minor(4),SubMinor(8)}
2453                  *      New: u32{Major(8),Minor(8),Unit(8),Dev(8)}
2454                  */
2455                 if (facts->MsgVersion < 0x0102) {
2456                         /*
2457                          *      Handle old FC f/w style, convert to new...
2458                          */
2459                         u16      oldv = le16_to_cpu(facts->Reserved_0101_FWVersion);
2460                         facts->FWVersion.Word =
2461                                         ((oldv<<12) & 0xFF000000) |
2462                                         ((oldv<<8)  & 0x000FFF00);
2463                 } else
2464                         facts->FWVersion.Word = le32_to_cpu(facts->FWVersion.Word);
2465
2466                 facts->ProductID = le16_to_cpu(facts->ProductID);
2467                 facts->CurrentHostMfaHighAddr =
2468                                 le32_to_cpu(facts->CurrentHostMfaHighAddr);
2469                 facts->GlobalCredits = le16_to_cpu(facts->GlobalCredits);
2470                 facts->CurrentSenseBufferHighAddr =
2471                                 le32_to_cpu(facts->CurrentSenseBufferHighAddr);
2472                 facts->CurReplyFrameSize =
2473                                 le16_to_cpu(facts->CurReplyFrameSize);
2474                 facts->IOCCapabilities = le32_to_cpu(facts->IOCCapabilities);
2475
2476                 /*
2477                  * Handle NEW (!) IOCFactsReply fields in MPI-1.01.xx
2478                  * Older MPI-1.00.xx struct had 13 dwords, and enlarged
2479                  * to 14 in MPI-1.01.0x.
2480                  */
2481                 if (facts->MsgLength >= (offsetof(IOCFactsReply_t,FWImageSize) + 7)/4 &&
2482                     facts->MsgVersion > 0x0100) {
2483                         facts->FWImageSize = le32_to_cpu(facts->FWImageSize);
2484                 }
2485
2486                 sz = facts->FWImageSize;
2487                 if ( sz & 0x01 )
2488                         sz += 1;
2489                 if ( sz & 0x02 )
2490                         sz += 2;
2491                 facts->FWImageSize = sz;
2492
2493                 if (!facts->RequestFrameSize) {
2494                         /*  Something is wrong!  */
2495                         printk(MYIOC_s_ERR_FMT "IOC reported invalid 0 request size!\n",
2496                                         ioc->name);
2497                         return -55;
2498                 }
2499
2500                 r = sz = facts->BlockSize;
2501                 vv = ((63 / (sz * 4)) + 1) & 0x03;
2502                 ioc->NB_for_64_byte_frame = vv;
2503                 while ( sz )
2504                 {
2505                         shiftFactor++;
2506                         sz = sz >> 1;
2507                 }
2508                 ioc->NBShiftFactor  = shiftFactor;
2509                 dinitprintk((MYIOC_s_INFO_FMT "NB_for_64_byte_frame=%x NBShiftFactor=%x BlockSize=%x\n",
2510                                         ioc->name, vv, shiftFactor, r));
2511
2512                 if (reason == MPT_HOSTEVENT_IOC_BRINGUP) {
2513                         /*
2514                          * Set values for this IOC's request & reply frame sizes,
2515                          * and request & reply queue depths...
2516                          */
2517                         ioc->req_sz = min(MPT_DEFAULT_FRAME_SIZE, facts->RequestFrameSize * 4);
2518                         ioc->req_depth = min_t(int, MPT_MAX_REQ_DEPTH, facts->GlobalCredits);
2519                         ioc->reply_sz = MPT_REPLY_FRAME_SIZE;
2520                         ioc->reply_depth = min_t(int, MPT_DEFAULT_REPLY_DEPTH, facts->ReplyQueueDepth);
2521
2522                         dinitprintk((MYIOC_s_INFO_FMT "reply_sz=%3d, reply_depth=%4d\n",
2523                                 ioc->name, ioc->reply_sz, ioc->reply_depth));
2524                         dinitprintk((MYIOC_s_INFO_FMT "req_sz  =%3d, req_depth  =%4d\n",
2525                                 ioc->name, ioc->req_sz, ioc->req_depth));
2526
2527                         /* Get port facts! */
2528                         if ( (r = GetPortFacts(ioc, 0, sleepFlag)) != 0 )
2529                                 return r;
2530                 }
2531         } else {
2532                 printk(MYIOC_s_ERR_FMT
2533                      "Invalid IOC facts reply, msgLength=%d offsetof=%zd!\n",
2534                      ioc->name, facts->MsgLength, (offsetof(IOCFactsReply_t,
2535                      RequestFrameSize)/sizeof(u32)));
2536                 return -66;
2537         }
2538
2539         return 0;
2540 }
2541
2542 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2543 /*
2544  *      GetPortFacts - Send PortFacts request to MPT adapter.
2545  *      @ioc: Pointer to MPT_ADAPTER structure
2546  *      @portnum: Port number
2547  *      @sleepFlag: Specifies whether the process can sleep
2548  *
2549  *      Returns 0 for success, non-zero for failure.
2550  */
2551 static int
2552 GetPortFacts(MPT_ADAPTER *ioc, int portnum, int sleepFlag)
2553 {
2554         PortFacts_t              get_pfacts;
2555         PortFactsReply_t        *pfacts;
2556         int                      ii;
2557         int                      req_sz;
2558         int                      reply_sz;
2559
2560         /* IOC *must* NOT be in RESET state! */
2561         if (ioc->last_state == MPI_IOC_STATE_RESET) {
2562                 printk(KERN_ERR MYNAM ": ERROR - Can't get PortFacts, %s NOT READY! (%08x)\n",
2563                                 ioc->name,
2564                                 ioc->last_state );
2565                 return -4;
2566         }
2567
2568         pfacts = &ioc->pfacts[portnum];
2569
2570         /* Destination (reply area)...  */
2571         reply_sz = sizeof(*pfacts);
2572         memset(pfacts, 0, reply_sz);
2573
2574         /* Request area (get_pfacts on the stack right now!) */
2575         req_sz = sizeof(get_pfacts);
2576         memset(&get_pfacts, 0, req_sz);
2577
2578         get_pfacts.Function = MPI_FUNCTION_PORT_FACTS;
2579         get_pfacts.PortNumber = portnum;
2580         /* Assert: All other get_pfacts fields are zero! */
2581
2582         dinitprintk((MYIOC_s_INFO_FMT "Sending get PortFacts(%d) request\n",
2583                         ioc->name, portnum));
2584
2585         /* No non-zero fields in the get_pfacts request are greater than
2586          * 1 byte in size, so we can just fire it off as is.
2587          */
2588         ii = mpt_handshake_req_reply_wait(ioc, req_sz, (u32*)&get_pfacts,
2589                                 reply_sz, (u16*)pfacts, 5 /*seconds*/, sleepFlag);
2590         if (ii != 0)
2591                 return ii;
2592
2593         /* Did we get a valid reply? */
2594
2595         /* Now byte swap the necessary fields in the response. */
2596         pfacts->MsgContext = le32_to_cpu(pfacts->MsgContext);
2597         pfacts->IOCStatus = le16_to_cpu(pfacts->IOCStatus);
2598         pfacts->IOCLogInfo = le32_to_cpu(pfacts->IOCLogInfo);
2599         pfacts->MaxDevices = le16_to_cpu(pfacts->MaxDevices);
2600         pfacts->PortSCSIID = le16_to_cpu(pfacts->PortSCSIID);
2601         pfacts->ProtocolFlags = le16_to_cpu(pfacts->ProtocolFlags);
2602         pfacts->MaxPostedCmdBuffers = le16_to_cpu(pfacts->MaxPostedCmdBuffers);
2603         pfacts->MaxPersistentIDs = le16_to_cpu(pfacts->MaxPersistentIDs);
2604         pfacts->MaxLanBuckets = le16_to_cpu(pfacts->MaxLanBuckets);
2605
2606         return 0;
2607 }
2608
2609 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2610 /*
2611  *      SendIocInit - Send IOCInit request to MPT adapter.
2612  *      @ioc: Pointer to MPT_ADAPTER structure
2613  *      @sleepFlag: Specifies whether the process can sleep
2614  *
2615  *      Send IOCInit followed by PortEnable to bring IOC to OPERATIONAL state.
2616  *
2617  *      Returns 0 for success, non-zero for failure.
2618  */
2619 static int
2620 SendIocInit(MPT_ADAPTER *ioc, int sleepFlag)
2621 {
2622         IOCInit_t                ioc_init;
2623         MPIDefaultReply_t        init_reply;
2624         u32                      state;
2625         int                      r;
2626         int                      count;
2627         int                      cntdn;
2628
2629         memset(&ioc_init, 0, sizeof(ioc_init));
2630         memset(&init_reply, 0, sizeof(init_reply));
2631
2632         ioc_init.WhoInit = MPI_WHOINIT_HOST_DRIVER;
2633         ioc_init.Function = MPI_FUNCTION_IOC_INIT;
2634
2635         /* If we are in a recovery mode and we uploaded the FW image,
2636          * then this pointer is not NULL. Skip the upload a second time.
2637          * Set this flag if cached_fw set for either IOC.
2638          */
2639         if (ioc->facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT)
2640                 ioc->upload_fw = 1;
2641         else
2642                 ioc->upload_fw = 0;
2643         ddlprintk((MYIOC_s_INFO_FMT "upload_fw %d facts.Flags=%x\n",
2644                    ioc->name, ioc->upload_fw, ioc->facts.Flags));
2645
2646         if(ioc->bus_type == SAS)
2647                 ioc_init.MaxDevices = ioc->facts.MaxDevices;
2648         else if(ioc->bus_type == FC)
2649                 ioc_init.MaxDevices = MPT_MAX_FC_DEVICES;
2650         else
2651                 ioc_init.MaxDevices = MPT_MAX_SCSI_DEVICES;
2652         ioc_init.MaxBuses = MPT_MAX_BUS;
2653         dinitprintk((MYIOC_s_INFO_FMT "facts.MsgVersion=%x\n",
2654                    ioc->name, ioc->facts.MsgVersion));
2655         if (ioc->facts.MsgVersion >= MPI_VERSION_01_05) {
2656                 // set MsgVersion and HeaderVersion host driver was built with
2657                 ioc_init.MsgVersion = cpu_to_le16(MPI_VERSION);
2658                 ioc_init.HeaderVersion = cpu_to_le16(MPI_HEADER_VERSION);
2659
2660                 if (ioc->facts.Flags & MPI_IOCFACTS_FLAGS_HOST_PAGE_BUFFER_PERSISTENT) {
2661                         ioc_init.HostPageBufferSGE = ioc->facts.HostPageBufferSGE;
2662                 } else if(mpt_host_page_alloc(ioc, &ioc_init))
2663                         return -99;
2664         }
2665         ioc_init.ReplyFrameSize = cpu_to_le16(ioc->reply_sz);   /* in BYTES */
2666
2667         if (sizeof(dma_addr_t) == sizeof(u64)) {
2668                 /* Save the upper 32-bits of the request
2669                  * (reply) and sense buffers.
2670                  */
2671                 ioc_init.HostMfaHighAddr = cpu_to_le32((u32)((u64)ioc->alloc_dma >> 32));
2672                 ioc_init.SenseBufferHighAddr = cpu_to_le32((u32)((u64)ioc->sense_buf_pool_dma >> 32));
2673         } else {
2674                 /* Force 32-bit addressing */
2675                 ioc_init.HostMfaHighAddr = cpu_to_le32(0);
2676                 ioc_init.SenseBufferHighAddr = cpu_to_le32(0);
2677         }
2678
2679         ioc->facts.CurrentHostMfaHighAddr = ioc_init.HostMfaHighAddr;
2680         ioc->facts.CurrentSenseBufferHighAddr = ioc_init.SenseBufferHighAddr;
2681         ioc->facts.MaxDevices = ioc_init.MaxDevices;
2682         ioc->facts.MaxBuses = ioc_init.MaxBuses;
2683
2684         dhsprintk((MYIOC_s_INFO_FMT "Sending IOCInit (req @ %p)\n",
2685                         ioc->name, &ioc_init));
2686
2687         r = mpt_handshake_req_reply_wait(ioc, sizeof(IOCInit_t), (u32*)&ioc_init,
2688                                 sizeof(MPIDefaultReply_t), (u16*)&init_reply, 10 /*seconds*/, sleepFlag);
2689         if (r != 0) {
2690                 printk(MYIOC_s_ERR_FMT "Sending IOCInit failed(%d)!\n",ioc->name, r);
2691                 return r;
2692         }
2693
2694         /* No need to byte swap the multibyte fields in the reply
2695          * since we don't even look at it's contents.
2696          */
2697
2698         dhsprintk((MYIOC_s_INFO_FMT "Sending PortEnable (req @ %p)\n",
2699                         ioc->name, &ioc_init));
2700
2701         if ((r = SendPortEnable(ioc, 0, sleepFlag)) != 0) {
2702                 printk(MYIOC_s_ERR_FMT "Sending PortEnable failed(%d)!\n",ioc->name, r);
2703                 return r;
2704         }
2705
2706         /* YIKES!  SUPER IMPORTANT!!!
2707          *  Poll IocState until _OPERATIONAL while IOC is doing
2708          *  LoopInit and TargetDiscovery!
2709          */
2710         count = 0;
2711         cntdn = ((sleepFlag == CAN_SLEEP) ? HZ : 1000) * 60;    /* 60 seconds */
2712         state = mpt_GetIocState(ioc, 1);
2713         while (state != MPI_IOC_STATE_OPERATIONAL && --cntdn) {
2714                 if (sleepFlag == CAN_SLEEP) {
2715                         msleep_interruptible(1);
2716                 } else {
2717                         mdelay(1);
2718                 }
2719
2720                 if (!cntdn) {
2721                         printk(MYIOC_s_ERR_FMT "Wait IOC_OP state timeout(%d)!\n",
2722                                         ioc->name, (int)((count+5)/HZ));
2723                         return -9;
2724                 }
2725
2726                 state = mpt_GetIocState(ioc, 1);
2727                 count++;
2728         }
2729         dinitprintk((MYIOC_s_INFO_FMT "INFO - Wait IOC_OPERATIONAL state (cnt=%d)\n",
2730                         ioc->name, count));
2731
2732         return r;
2733 }
2734
2735 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2736 /*
2737  *      SendPortEnable - Send PortEnable request to MPT adapter port.
2738  *      @ioc: Pointer to MPT_ADAPTER structure
2739  *      @portnum: Port number to enable
2740  *      @sleepFlag: Specifies whether the process can sleep
2741  *
2742  *      Send PortEnable to bring IOC to OPERATIONAL state.
2743  *
2744  *      Returns 0 for success, non-zero for failure.
2745  */
2746 static int
2747 SendPortEnable(MPT_ADAPTER *ioc, int portnum, int sleepFlag)
2748 {
2749         PortEnable_t             port_enable;
2750         MPIDefaultReply_t        reply_buf;
2751         int      rc;
2752         int      req_sz;
2753         int      reply_sz;
2754
2755         /*  Destination...  */
2756         reply_sz = sizeof(MPIDefaultReply_t);
2757         memset(&reply_buf, 0, reply_sz);
2758
2759         req_sz = sizeof(PortEnable_t);
2760         memset(&port_enable, 0, req_sz);
2761
2762         port_enable.Function = MPI_FUNCTION_PORT_ENABLE;
2763         port_enable.PortNumber = portnum;
2764 /*      port_enable.ChainOffset = 0;            */
2765 /*      port_enable.MsgFlags = 0;               */
2766 /*      port_enable.MsgContext = 0;             */
2767
2768         dinitprintk((MYIOC_s_INFO_FMT "Sending Port(%d)Enable (req @ %p)\n",
2769                         ioc->name, portnum, &port_enable));
2770
2771         /* RAID FW may take a long time to enable
2772          */
2773         if ( (ioc->facts.ProductID & MPI_FW_HEADER_PID_PROD_MASK)
2774                         > MPI_FW_HEADER_PID_PROD_TARGET_SCSI ) {
2775                 rc = mpt_handshake_req_reply_wait(ioc, req_sz, (u32*)&port_enable,
2776                                 reply_sz, (u16*)&reply_buf, 300 /*seconds*/, sleepFlag);
2777         } else {
2778                 rc = mpt_handshake_req_reply_wait(ioc, req_sz, (u32*)&port_enable,
2779                                 reply_sz, (u16*)&reply_buf, 30 /*seconds*/, sleepFlag);
2780         }
2781         return rc;
2782 }
2783
2784 /*
2785  *      ioc: Pointer to MPT_ADAPTER structure
2786  *      size - total FW bytes
2787  */
2788 void
2789 mpt_alloc_fw_memory(MPT_ADAPTER *ioc, int size)
2790 {
2791         if (ioc->cached_fw)
2792                 return;  /* use already allocated memory */
2793         if (ioc->alt_ioc && ioc->alt_ioc->cached_fw) {
2794                 ioc->cached_fw = ioc->alt_ioc->cached_fw;  /* use alt_ioc's memory */
2795                 ioc->cached_fw_dma = ioc->alt_ioc->cached_fw_dma;
2796         } else {
2797                 if ( (ioc->cached_fw = pci_alloc_consistent(ioc->pcidev, size, &ioc->cached_fw_dma) ) )
2798                         ioc->alloc_total += size;
2799         }
2800 }
2801 /*
2802  * If alt_img is NULL, delete from ioc structure.
2803  * Else, delete a secondary image in same format.
2804  */
2805 void
2806 mpt_free_fw_memory(MPT_ADAPTER *ioc)
2807 {
2808         int sz;
2809
2810         sz = ioc->facts.FWImageSize;
2811         dinitprintk((KERN_INFO MYNAM "free_fw_memory: FW Image  @ %p[%p], sz=%d[%x] bytes\n",
2812                  ioc->cached_fw, (void *)(ulong)ioc->cached_fw_dma, sz, sz));
2813         pci_free_consistent(ioc->pcidev, sz,
2814                         ioc->cached_fw, ioc->cached_fw_dma);
2815         ioc->cached_fw = NULL;
2816
2817         return;
2818 }
2819
2820
2821 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2822 /*
2823  *      mpt_do_upload - Construct and Send FWUpload request to MPT adapter port.
2824  *      @ioc: Pointer to MPT_ADAPTER structure
2825  *      @sleepFlag: Specifies whether the process can sleep
2826  *
2827  *      Returns 0 for success, >0 for handshake failure
2828  *              <0 for fw upload failure.
2829  *
2830  *      Remark: If bound IOC and a successful FWUpload was performed
2831  *      on the bound IOC, the second image is discarded
2832  *      and memory is free'd. Both channels must upload to prevent
2833  *      IOC from running in degraded mode.
2834  */
2835 static int
2836 mpt_do_upload(MPT_ADAPTER *ioc, int sleepFlag)
2837 {
2838         u8                       request[ioc->req_sz];
2839         u8                       reply[sizeof(FWUploadReply_t)];
2840         FWUpload_t              *prequest;
2841         FWUploadReply_t         *preply;
2842         FWUploadTCSGE_t         *ptcsge;
2843         int                      sgeoffset;
2844         u32                      flagsLength;
2845         int                      ii, sz, reply_sz;
2846         int                      cmdStatus;
2847
2848         /* If the image size is 0, we are done.
2849          */
2850         if ((sz = ioc->facts.FWImageSize) == 0)
2851                 return 0;
2852
2853         mpt_alloc_fw_memory(ioc, sz);
2854
2855         dinitprintk((KERN_INFO MYNAM ": FW Image  @ %p[%p], sz=%d[%x] bytes\n",
2856                  ioc->cached_fw, (void *)(ulong)ioc->cached_fw_dma, sz, sz));
2857
2858         if (ioc->cached_fw == NULL) {
2859                 /* Major Failure.
2860                  */
2861                 return -ENOMEM;
2862         }
2863
2864         prequest = (FWUpload_t *)&request;
2865         preply = (FWUploadReply_t *)&reply;
2866
2867         /*  Destination...  */
2868         memset(prequest, 0, ioc->req_sz);
2869
2870         reply_sz = sizeof(reply);
2871         memset(preply, 0, reply_sz);
2872
2873         prequest->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2874         prequest->Function = MPI_FUNCTION_FW_UPLOAD;
2875
2876         ptcsge = (FWUploadTCSGE_t *) &prequest->SGL;
2877         ptcsge->DetailsLength = 12;
2878         ptcsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2879         ptcsge->ImageSize = cpu_to_le32(sz);
2880
2881         sgeoffset = sizeof(FWUpload_t) - sizeof(SGE_MPI_UNION) + sizeof(FWUploadTCSGE_t);
2882
2883         flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ | sz;
2884         mpt_add_sge(&request[sgeoffset], flagsLength, ioc->cached_fw_dma);
2885
2886         sgeoffset += sizeof(u32) + sizeof(dma_addr_t);
2887         dinitprintk((KERN_INFO MYNAM ": Sending FW Upload (req @ %p) sgeoffset=%d \n",
2888                         prequest, sgeoffset));
2889         DBG_DUMP_FW_REQUEST_FRAME(prequest)
2890
2891         ii = mpt_handshake_req_reply_wait(ioc, sgeoffset, (u32*)prequest,
2892                                 reply_sz, (u16*)preply, 65 /*seconds*/, sleepFlag);
2893
2894         dinitprintk((KERN_INFO MYNAM ": FW Upload completed rc=%x \n", ii));
2895
2896         cmdStatus = -EFAULT;
2897         if (ii == 0) {
2898                 /* Handshake transfer was complete and successful.
2899                  * Check the Reply Frame.
2900                  */
2901                 int status, transfer_sz;
2902                 status = le16_to_cpu(preply->IOCStatus);
2903                 if (status == MPI_IOCSTATUS_SUCCESS) {
2904                         transfer_sz = le32_to_cpu(preply->ActualImageSize);
2905                         if (transfer_sz == sz)
2906                                 cmdStatus = 0;
2907                 }
2908         }
2909         dinitprintk((MYIOC_s_INFO_FMT ": do_upload cmdStatus=%d \n",
2910                         ioc->name, cmdStatus));
2911
2912
2913         if (cmdStatus) {
2914
2915                 ddlprintk((MYIOC_s_INFO_FMT ": fw upload failed, freeing image \n",
2916                         ioc->name));
2917                 mpt_free_fw_memory(ioc);
2918         }
2919
2920         return cmdStatus;
2921 }
2922
2923 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2924 /*
2925  *      mpt_downloadboot - DownloadBoot code
2926  *      @ioc: Pointer to MPT_ADAPTER structure
2927  *      @flag: Specify which part of IOC memory is to be uploaded.
2928  *      @sleepFlag: Specifies whether the process can sleep
2929  *
2930  *      FwDownloadBoot requires Programmed IO access.
2931  *
2932  *      Returns 0 for success
2933  *              -1 FW Image size is 0
2934  *              -2 No valid cached_fw Pointer
2935  *              <0 for fw upload failure.
2936  */
2937 static int
2938 mpt_downloadboot(MPT_ADAPTER *ioc, MpiFwHeader_t *pFwHeader, int sleepFlag)
2939 {
2940         MpiExtImageHeader_t     *pExtImage;
2941         u32                      fwSize;
2942         u32                      diag0val;
2943         int                      count;
2944         u32                     *ptrFw;
2945         u32                      diagRwData;
2946         u32                      nextImage;
2947         u32                      load_addr;
2948         u32                      ioc_state=0;
2949
2950         ddlprintk((MYIOC_s_INFO_FMT "downloadboot: fw size 0x%x (%d), FW Ptr %p\n",
2951                                 ioc->name, pFwHeader->ImageSize, pFwHeader->ImageSize, pFwHeader));
2952
2953         CHIPREG_WRITE32(&ioc->chip->WriteSequence, 0xFF);
2954         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_1ST_KEY_VALUE);
2955         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_2ND_KEY_VALUE);
2956         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_3RD_KEY_VALUE);
2957         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_4TH_KEY_VALUE);
2958         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_5TH_KEY_VALUE);
2959
2960         CHIPREG_WRITE32(&ioc->chip->Diagnostic, (MPI_DIAG_PREVENT_IOC_BOOT | MPI_DIAG_DISABLE_ARM));
2961
2962         /* wait 1 msec */
2963         if (sleepFlag == CAN_SLEEP) {
2964                 msleep_interruptible(1);
2965         } else {
2966                 mdelay (1);
2967         }
2968
2969         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
2970         CHIPREG_WRITE32(&ioc->chip->Diagnostic, diag0val | MPI_DIAG_RESET_ADAPTER);
2971
2972         for (count = 0; count < 30; count ++) {
2973                 diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
2974                 if (!(diag0val & MPI_DIAG_RESET_ADAPTER)) {
2975                         ddlprintk((MYIOC_s_INFO_FMT "RESET_ADAPTER cleared, count=%d\n",
2976                                 ioc->name, count));
2977                         break;
2978                 }
2979                 /* wait .1 sec */
2980                 if (sleepFlag == CAN_SLEEP) {
2981                         msleep_interruptible (100);
2982                 } else {
2983                         mdelay (100);
2984                 }
2985         }
2986
2987         if ( count == 30 ) {
2988                 ddlprintk((MYIOC_s_INFO_FMT "downloadboot failed! "
2989                 "Unable to get MPI_DIAG_DRWE mode, diag0val=%x\n",
2990                 ioc->name, diag0val));
2991                 return -3;
2992         }
2993
2994         CHIPREG_WRITE32(&ioc->chip->WriteSequence, 0xFF);
2995         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_1ST_KEY_VALUE);
2996         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_2ND_KEY_VALUE);
2997         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_3RD_KEY_VALUE);
2998         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_4TH_KEY_VALUE);
2999         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_5TH_KEY_VALUE);
3000
3001         /* Set the DiagRwEn and Disable ARM bits */
3002         CHIPREG_WRITE32(&ioc->chip->Diagnostic, (MPI_DIAG_RW_ENABLE | MPI_DIAG_DISABLE_ARM));
3003
3004         fwSize = (pFwHeader->ImageSize + 3)/4;
3005         ptrFw = (u32 *) pFwHeader;
3006
3007         /* Write the LoadStartAddress to the DiagRw Address Register
3008          * using Programmed IO
3009          */
3010         if (ioc->errata_flag_1064)
3011                 pci_enable_io_access(ioc->pcidev);
3012
3013         CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwAddress, pFwHeader->LoadStartAddress);
3014         ddlprintk((MYIOC_s_INFO_FMT "LoadStart addr written 0x%x \n",
3015                 ioc->name, pFwHeader->LoadStartAddress));
3016
3017         ddlprintk((MYIOC_s_INFO_FMT "Write FW Image: 0x%x bytes @ %p\n",
3018                                 ioc->name, fwSize*4, ptrFw));
3019         while (fwSize--) {
3020                 CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwData, *ptrFw++);
3021         }
3022
3023         nextImage = pFwHeader->NextImageHeaderOffset;
3024         while (nextImage) {
3025                 pExtImage = (MpiExtImageHeader_t *) ((char *)pFwHeader + nextImage);
3026
3027                 load_addr = pExtImage->LoadStartAddress;
3028
3029                 fwSize = (pExtImage->ImageSize + 3) >> 2;
3030                 ptrFw = (u32 *)pExtImage;
3031
3032                 ddlprintk((MYIOC_s_INFO_FMT "Write Ext Image: 0x%x (%d) bytes @ %p load_addr=%x\n",
3033                                                 ioc->name, fwSize*4, fwSize*4, ptrFw, load_addr));
3034                 CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwAddress, load_addr);
3035
3036                 while (fwSize--) {
3037                         CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwData, *ptrFw++);
3038                 }
3039                 nextImage = pExtImage->NextImageHeaderOffset;
3040         }
3041
3042         /* Write the IopResetVectorRegAddr */
3043         ddlprintk((MYIOC_s_INFO_FMT "Write IopResetVector Addr=%x! \n", ioc->name,      pFwHeader->IopResetRegAddr));
3044         CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwAddress, pFwHeader->IopResetRegAddr);
3045
3046         /* Write the IopResetVectorValue */
3047         ddlprintk((MYIOC_s_INFO_FMT "Write IopResetVector Value=%x! \n", ioc->name, pFwHeader->IopResetVectorValue));
3048         CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwData, pFwHeader->IopResetVectorValue);
3049
3050         /* Clear the internal flash bad bit - autoincrementing register,
3051          * so must do two writes.
3052          */
3053         if (ioc->bus_type == SPI) {
3054                 /*
3055                  * 1030 and 1035 H/W errata, workaround to access
3056                  * the ClearFlashBadSignatureBit
3057                  */
3058                 CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwAddress, 0x3F000000);
3059                 diagRwData = CHIPREG_PIO_READ32(&ioc->pio_chip->DiagRwData);
3060                 diagRwData |= 0x40000000;
3061                 CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwAddress, 0x3F000000);
3062                 CHIPREG_PIO_WRITE32(&ioc->pio_chip->DiagRwData, diagRwData);
3063
3064         } else /* if((ioc->bus_type == SAS) || (ioc->bus_type == FC)) */ {
3065                 diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3066                 CHIPREG_WRITE32(&ioc->chip->Diagnostic, diag0val |
3067                     MPI_DIAG_CLEAR_FLASH_BAD_SIG);
3068
3069                 /* wait 1 msec */
3070                 if (sleepFlag == CAN_SLEEP) {
3071                         msleep_interruptible (1);
3072                 } else {
3073                         mdelay (1);
3074                 }
3075         }
3076
3077         if (ioc->errata_flag_1064)
3078                 pci_disable_io_access(ioc->pcidev);
3079
3080         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3081         ddlprintk((MYIOC_s_INFO_FMT "downloadboot diag0val=%x, "
3082                 "turning off PREVENT_IOC_BOOT, DISABLE_ARM, RW_ENABLE\n",
3083                 ioc->name, diag0val));
3084         diag0val &= ~(MPI_DIAG_PREVENT_IOC_BOOT | MPI_DIAG_DISABLE_ARM | MPI_DIAG_RW_ENABLE);
3085         ddlprintk((MYIOC_s_INFO_FMT "downloadboot now diag0val=%x\n",
3086                 ioc->name, diag0val));
3087         CHIPREG_WRITE32(&ioc->chip->Diagnostic, diag0val);
3088
3089         /* Write 0xFF to reset the sequencer */
3090         CHIPREG_WRITE32(&ioc->chip->WriteSequence, 0xFF);
3091
3092         if (ioc->bus_type == SAS) {
3093                 ioc_state = mpt_GetIocState(ioc, 0);
3094                 if ( (GetIocFacts(ioc, sleepFlag,
3095                                 MPT_HOSTEVENT_IOC_BRINGUP)) != 0 ) {
3096                         ddlprintk((MYIOC_s_INFO_FMT "GetIocFacts failed: IocState=%x\n",
3097                                         ioc->name, ioc_state));
3098                         return -EFAULT;
3099                 }
3100         }
3101
3102         for (count=0; count<HZ*20; count++) {
3103                 if ((ioc_state = mpt_GetIocState(ioc, 0)) & MPI_IOC_STATE_READY) {
3104                         ddlprintk((MYIOC_s_INFO_FMT "downloadboot successful! (count=%d) IocState=%x\n",
3105                                         ioc->name, count, ioc_state));
3106                         if (ioc->bus_type == SAS) {
3107                                 return 0;
3108                         }
3109                         if ((SendIocInit(ioc, sleepFlag)) != 0) {
3110                                 ddlprintk((MYIOC_s_INFO_FMT "downloadboot: SendIocInit failed\n",
3111                                         ioc->name));
3112                                 return -EFAULT;
3113                         }
3114                         ddlprintk((MYIOC_s_INFO_FMT "downloadboot: SendIocInit successful\n",
3115                                         ioc->name));
3116                         return 0;
3117                 }
3118                 if (sleepFlag == CAN_SLEEP) {
3119                         msleep_interruptible (10);
3120                 } else {
3121                         mdelay (10);
3122                 }
3123         }
3124         ddlprintk((MYIOC_s_INFO_FMT "downloadboot failed! IocState=%x\n",
3125                 ioc->name, ioc_state));
3126         return -EFAULT;
3127 }
3128
3129 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3130 /*
3131  *      KickStart - Perform hard reset of MPT adapter.
3132  *      @ioc: Pointer to MPT_ADAPTER structure
3133  *      @force: Force hard reset
3134  *      @sleepFlag: Specifies whether the process can sleep
3135  *
3136  *      This routine places MPT adapter in diagnostic mode via the
3137  *      WriteSequence register, and then performs a hard reset of adapter
3138  *      via the Diagnostic register.
3139  *
3140  *      Inputs:   sleepflag - CAN_SLEEP (non-interrupt thread)
3141  *                      or NO_SLEEP (interrupt thread, use mdelay)
3142  *                force - 1 if doorbell active, board fault state
3143  *                              board operational, IOC_RECOVERY or
3144  *                              IOC_BRINGUP and there is an alt_ioc.
3145  *                        0 else
3146  *
3147  *      Returns:
3148  *               1 - hard reset, READY
3149  *               0 - no reset due to History bit, READY
3150  *              -1 - no reset due to History bit but not READY
3151  *                   OR reset but failed to come READY
3152  *              -2 - no reset, could not enter DIAG mode
3153  *              -3 - reset but bad FW bit
3154  */
3155 static int
3156 KickStart(MPT_ADAPTER *ioc, int force, int sleepFlag)
3157 {
3158         int hard_reset_done = 0;
3159         u32 ioc_state=0;
3160         int cnt,cntdn;
3161
3162         dinitprintk((KERN_WARNING MYNAM ": KickStarting %s!\n", ioc->name));
3163         if (ioc->bus_type == SPI) {
3164                 /* Always issue a Msg Unit Reset first. This will clear some
3165                  * SCSI bus hang conditions.
3166                  */
3167                 SendIocReset(ioc, MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET, sleepFlag);
3168
3169                 if (sleepFlag == CAN_SLEEP) {
3170                         msleep_interruptible (1000);
3171                 } else {
3172                         mdelay (1000);
3173                 }
3174         }
3175
3176         hard_reset_done = mpt_diag_reset(ioc, force, sleepFlag);
3177         if (hard_reset_done < 0)
3178                 return hard_reset_done;
3179
3180         dinitprintk((MYIOC_s_INFO_FMT "Diagnostic reset successful!\n",
3181                         ioc->name));
3182
3183         cntdn = ((sleepFlag == CAN_SLEEP) ? HZ : 1000) * 2;     /* 2 seconds */
3184         for (cnt=0; cnt<cntdn; cnt++) {
3185                 ioc_state = mpt_GetIocState(ioc, 1);
3186                 if ((ioc_state == MPI_IOC_STATE_READY) || (ioc_state == MPI_IOC_STATE_OPERATIONAL)) {
3187                         dinitprintk((MYIOC_s_INFO_FMT "KickStart successful! (cnt=%d)\n",
3188                                         ioc->name, cnt));
3189                         return hard_reset_done;
3190                 }
3191                 if (sleepFlag == CAN_SLEEP) {
3192                         msleep_interruptible (10);
3193                 } else {
3194                         mdelay (10);
3195                 }
3196         }
3197
3198         printk(MYIOC_s_ERR_FMT "Failed to come READY after reset! IocState=%x\n",
3199                         ioc->name, ioc_state);
3200         return -1;
3201 }
3202
3203 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3204 /*
3205  *      mpt_diag_reset - Perform hard reset of the adapter.
3206  *      @ioc: Pointer to MPT_ADAPTER structure
3207  *      @ignore: Set if to honor and clear to ignore
3208  *              the reset history bit
3209  *      @sleepflag: CAN_SLEEP if called in a non-interrupt thread,
3210  *              else set to NO_SLEEP (use mdelay instead)
3211  *
3212  *      This routine places the adapter in diagnostic mode via the
3213  *      WriteSequence register and then performs a hard reset of adapter
3214  *      via the Diagnostic register. Adapter should be in ready state
3215  *      upon successful completion.
3216  *
3217  *      Returns:  1  hard reset successful
3218  *                0  no reset performed because reset history bit set
3219  *               -2  enabling diagnostic mode failed
3220  *               -3  diagnostic reset failed
3221  */
3222 static int
3223 mpt_diag_reset(MPT_ADAPTER *ioc, int ignore, int sleepFlag)
3224 {
3225         u32 diag0val;
3226         u32 doorbell;
3227         int hard_reset_done = 0;
3228         int count = 0;
3229 #ifdef MPT_DEBUG
3230         u32 diag1val = 0;
3231 #endif
3232
3233         /* Clear any existing interrupts */
3234         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
3235
3236         /* Use "Diagnostic reset" method! (only thing available!) */
3237         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3238
3239 #ifdef MPT_DEBUG
3240         if (ioc->alt_ioc)
3241                 diag1val = CHIPREG_READ32(&ioc->alt_ioc->chip->Diagnostic);
3242         dprintk((MYIOC_s_INFO_FMT "DbG1: diag0=%08x, diag1=%08x\n",
3243                         ioc->name, diag0val, diag1val));
3244 #endif
3245
3246         /* Do the reset if we are told to ignore the reset history
3247          * or if the reset history is 0
3248          */
3249         if (ignore || !(diag0val & MPI_DIAG_RESET_HISTORY)) {
3250                 while ((diag0val & MPI_DIAG_DRWE) == 0) {
3251                         /* Write magic sequence to WriteSequence register
3252                          * Loop until in diagnostic mode
3253                          */
3254                         CHIPREG_WRITE32(&ioc->chip->WriteSequence, 0xFF);
3255                         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_1ST_KEY_VALUE);
3256                         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_2ND_KEY_VALUE);
3257                         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_3RD_KEY_VALUE);
3258                         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_4TH_KEY_VALUE);
3259                         CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_5TH_KEY_VALUE);
3260
3261                         /* wait 100 msec */
3262                         if (sleepFlag == CAN_SLEEP) {
3263                                 msleep_interruptible (100);
3264                         } else {
3265                                 mdelay (100);
3266                         }
3267
3268                         count++;
3269                         if (count > 20) {
3270                                 printk(MYIOC_s_ERR_FMT "Enable Diagnostic mode FAILED! (%02xh)\n",
3271                                                 ioc->name, diag0val);
3272                                 return -2;
3273
3274                         }
3275
3276                         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3277
3278                         dprintk((MYIOC_s_INFO_FMT "Wrote magic DiagWriteEn sequence (%x)\n",
3279                                         ioc->name, diag0val));
3280                 }
3281
3282 #ifdef MPT_DEBUG
3283                 if (ioc->alt_ioc)
3284                         diag1val = CHIPREG_READ32(&ioc->alt_ioc->chip->Diagnostic);
3285                 dprintk((MYIOC_s_INFO_FMT "DbG2: diag0=%08x, diag1=%08x\n",
3286                                 ioc->name, diag0val, diag1val));
3287 #endif
3288                 /*
3289                  * Disable the ARM (Bug fix)
3290                  *
3291                  */
3292                 CHIPREG_WRITE32(&ioc->chip->Diagnostic, diag0val | MPI_DIAG_DISABLE_ARM);
3293                 mdelay(1);
3294
3295                 /*
3296                  * Now hit the reset bit in the Diagnostic register
3297                  * (THE BIG HAMMER!) (Clears DRWE bit).
3298                  */
3299                 CHIPREG_WRITE32(&ioc->chip->Diagnostic, diag0val | MPI_DIAG_RESET_ADAPTER);
3300                 hard_reset_done = 1;
3301                 dprintk((MYIOC_s_INFO_FMT "Diagnostic reset performed\n",
3302                                 ioc->name));
3303
3304                 /*
3305                  * Call each currently registered protocol IOC reset handler
3306                  * with pre-reset indication.
3307                  * NOTE: If we're doing _IOC_BRINGUP, there can be no
3308                  * MptResetHandlers[] registered yet.
3309                  */
3310                 {
3311                         int      ii;
3312                         int      r = 0;
3313
3314                         for (ii=MPT_MAX_PROTOCOL_DRIVERS-1; ii; ii--) {
3315                                 if (MptResetHandlers[ii]) {
3316                                         dprintk((MYIOC_s_INFO_FMT "Calling IOC pre_reset handler #%d\n",
3317                                                         ioc->name, ii));
3318                                         r += (*(MptResetHandlers[ii]))(ioc, MPT_IOC_PRE_RESET);
3319                                         if (ioc->alt_ioc) {
3320                                                 dprintk((MYIOC_s_INFO_FMT "Calling alt-%s pre_reset handler #%d\n",
3321                                                                 ioc->name, ioc->alt_ioc->name, ii));
3322                                                 r += (*(MptResetHandlers[ii]))(ioc->alt_ioc, MPT_IOC_PRE_RESET);
3323                                         }
3324                                 }
3325                         }
3326                         /* FIXME?  Examine results here? */
3327                 }
3328
3329                 if (ioc->cached_fw) {
3330                         /* If the DownloadBoot operation fails, the
3331                          * IOC will be left unusable. This is a fatal error
3332                          * case.  _diag_reset will return < 0
3333                          */
3334                         for (count = 0; count < 30; count ++) {
3335                                 diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3336                                 if (!(diag0val & MPI_DIAG_RESET_ADAPTER)) {
3337                                         break;
3338                                 }
3339
3340                                 /* wait 1 sec */
3341                                 if (sleepFlag == CAN_SLEEP) {
3342                                         msleep_interruptible (1000);
3343                                 } else {
3344                                         mdelay (1000);
3345                                 }
3346                         }
3347                         if ((count = mpt_downloadboot(ioc,
3348                                 (MpiFwHeader_t *)ioc->cached_fw, sleepFlag)) < 0) {
3349                                 printk(KERN_WARNING MYNAM
3350                                         ": firmware downloadboot failure (%d)!\n", count);
3351                         }
3352
3353                 } else {
3354                         /* Wait for FW to reload and for board
3355                          * to go to the READY state.
3356                          * Maximum wait is 60 seconds.
3357                          * If fail, no error will check again
3358                          * with calling program.
3359                          */
3360                         for (count = 0; count < 60; count ++) {
3361                                 doorbell = CHIPREG_READ32(&ioc->chip->Doorbell);
3362                                 doorbell &= MPI_IOC_STATE_MASK;
3363
3364                                 if (doorbell == MPI_IOC_STATE_READY) {
3365                                         break;
3366                                 }
3367
3368                                 /* wait 1 sec */
3369                                 if (sleepFlag == CAN_SLEEP) {
3370                                         msleep_interruptible (1000);
3371                                 } else {
3372                                         mdelay (1000);
3373                                 }
3374                         }
3375                 }
3376         }
3377
3378         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3379 #ifdef MPT_DEBUG
3380         if (ioc->alt_ioc)
3381                 diag1val = CHIPREG_READ32(&ioc->alt_ioc->chip->Diagnostic);
3382         dprintk((MYIOC_s_INFO_FMT "DbG3: diag0=%08x, diag1=%08x\n",
3383                 ioc->name, diag0val, diag1val));
3384 #endif
3385
3386         /* Clear RESET_HISTORY bit!  Place board in the
3387          * diagnostic mode to update the diag register.
3388          */
3389         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3390         count = 0;
3391         while ((diag0val & MPI_DIAG_DRWE) == 0) {
3392                 /* Write magic sequence to WriteSequence register
3393                  * Loop until in diagnostic mode
3394                  */
3395                 CHIPREG_WRITE32(&ioc->chip->WriteSequence, 0xFF);
3396                 CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_1ST_KEY_VALUE);
3397                 CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_2ND_KEY_VALUE);
3398                 CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_3RD_KEY_VALUE);
3399                 CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_4TH_KEY_VALUE);
3400                 CHIPREG_WRITE32(&ioc->chip->WriteSequence, MPI_WRSEQ_5TH_KEY_VALUE);
3401
3402                 /* wait 100 msec */
3403                 if (sleepFlag == CAN_SLEEP) {
3404                         msleep_interruptible (100);
3405                 } else {
3406                         mdelay (100);
3407                 }
3408
3409                 count++;
3410                 if (count > 20) {
3411                         printk(MYIOC_s_ERR_FMT "Enable Diagnostic mode FAILED! (%02xh)\n",
3412                                         ioc->name, diag0val);
3413                         break;
3414                 }
3415                 diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3416         }
3417         diag0val &= ~MPI_DIAG_RESET_HISTORY;
3418         CHIPREG_WRITE32(&ioc->chip->Diagnostic, diag0val);
3419         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3420         if (diag0val & MPI_DIAG_RESET_HISTORY) {
3421                 printk(MYIOC_s_WARN_FMT "ResetHistory bit failed to clear!\n",
3422                                 ioc->name);
3423         }
3424
3425         /* Disable Diagnostic Mode
3426          */
3427         CHIPREG_WRITE32(&ioc->chip->WriteSequence, 0xFFFFFFFF);
3428
3429         /* Check FW reload status flags.
3430          */
3431         diag0val = CHIPREG_READ32(&ioc->chip->Diagnostic);
3432         if (diag0val & (MPI_DIAG_FLASH_BAD_SIG | MPI_DIAG_RESET_ADAPTER | MPI_DIAG_DISABLE_ARM)) {
3433                 printk(MYIOC_s_ERR_FMT "Diagnostic reset FAILED! (%02xh)\n",
3434                                 ioc->name, diag0val);
3435                 return -3;
3436         }
3437
3438 #ifdef MPT_DEBUG
3439         if (ioc->alt_ioc)
3440                 diag1val = CHIPREG_READ32(&ioc->alt_ioc->chip->Diagnostic);
3441         dprintk((MYIOC_s_INFO_FMT "DbG4: diag0=%08x, diag1=%08x\n",
3442                         ioc->name, diag0val, diag1val));
3443 #endif
3444
3445         /*
3446          * Reset flag that says we've enabled event notification
3447          */
3448         ioc->facts.EventState = 0;
3449
3450         if (ioc->alt_ioc)
3451                 ioc->alt_ioc->facts.EventState = 0;
3452
3453         return hard_reset_done;
3454 }
3455
3456 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3457 /*
3458  *      SendIocReset - Send IOCReset request to MPT adapter.
3459  *      @ioc: Pointer to MPT_ADAPTER structure
3460  *      @reset_type: reset type, expected values are
3461  *      %MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET or %MPI_FUNCTION_IO_UNIT_RESET
3462  *
3463  *      Send IOCReset request to the MPT adapter.
3464  *
3465  *      Returns 0 for success, non-zero for failure.
3466  */
3467 static int
3468 SendIocReset(MPT_ADAPTER *ioc, u8 reset_type, int sleepFlag)
3469 {
3470         int r;
3471         u32 state;
3472         int cntdn, count;
3473
3474         drsprintk((KERN_INFO MYNAM ": %s: Sending IOC reset(0x%02x)!\n",
3475                         ioc->name, reset_type));
3476         CHIPREG_WRITE32(&ioc->chip->Doorbell, reset_type<<MPI_DOORBELL_FUNCTION_SHIFT);
3477         if ((r = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0)
3478                 return r;
3479
3480         /* FW ACK'd request, wait for READY state
3481          */
3482         count = 0;
3483         cntdn = ((sleepFlag == CAN_SLEEP) ? HZ : 1000) * 15;    /* 15 seconds */
3484
3485         while ((state = mpt_GetIocState(ioc, 1)) != MPI_IOC_STATE_READY) {
3486                 cntdn--;
3487                 count++;
3488                 if (!cntdn) {
3489                         if (sleepFlag != CAN_SLEEP)
3490                                 count *= 10;
3491
3492                         printk(KERN_ERR MYNAM ": %s: ERROR - Wait IOC_READY state timeout(%d)!\n",
3493                                         ioc->name, (int)((count+5)/HZ));
3494                         return -ETIME;
3495                 }
3496
3497                 if (sleepFlag == CAN_SLEEP) {
3498                         msleep_interruptible(1);
3499                 } else {
3500                         mdelay (1);     /* 1 msec delay */
3501                 }
3502         }
3503
3504         /* TODO!
3505          *  Cleanup all event stuff for this IOC; re-issue EventNotification
3506          *  request if needed.
3507          */
3508         if (ioc->facts.Function)
3509                 ioc->facts.EventState = 0;
3510
3511         return 0;
3512 }
3513
3514 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3515 /*
3516  *      initChainBuffers - Allocate memory for and initialize
3517  *      chain buffers, chain buffer control arrays and spinlock.
3518  *      @hd: Pointer to MPT_SCSI_HOST structure
3519  *      @init: If set, initialize the spin lock.
3520  */
3521 static int
3522 initChainBuffers(MPT_ADAPTER *ioc)
3523 {
3524         u8              *mem;
3525         int             sz, ii, num_chain;
3526         int             scale, num_sge, numSGE;
3527
3528         /* ReqToChain size must equal the req_depth
3529          * index = req_idx
3530          */
3531         if (ioc->ReqToChain == NULL) {
3532                 sz = ioc->req_depth * sizeof(int);
3533                 mem = kmalloc(sz, GFP_ATOMIC);
3534                 if (mem == NULL)
3535                         return -1;
3536
3537                 ioc->ReqToChain = (int *) mem;
3538                 dinitprintk((KERN_INFO MYNAM ": %s ReqToChain alloc  @ %p, sz=%d bytes\n",
3539                                 ioc->name, mem, sz));
3540                 mem = kmalloc(sz, GFP_ATOMIC);
3541                 if (mem == NULL)
3542                         return -1;
3543
3544                 ioc->RequestNB = (int *) mem;
3545                 dinitprintk((KERN_INFO MYNAM ": %s RequestNB alloc  @ %p, sz=%d bytes\n",
3546                                 ioc->name, mem, sz));
3547         }
3548         for (ii = 0; ii < ioc->req_depth; ii++) {
3549                 ioc->ReqToChain[ii] = MPT_HOST_NO_CHAIN;
3550         }
3551
3552         /* ChainToChain size must equal the total number
3553          * of chain buffers to be allocated.
3554          * index = chain_idx
3555          *
3556          * Calculate the number of chain buffers needed(plus 1) per I/O
3557          * then multiply the the maximum number of simultaneous cmds
3558          *
3559          * num_sge = num sge in request frame + last chain buffer
3560          * scale = num sge per chain buffer if no chain element
3561          */
3562         scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
3563         if (sizeof(dma_addr_t) == sizeof(u64))
3564                 num_sge =  scale + (ioc->req_sz - 60) / (sizeof(dma_addr_t) + sizeof(u32));
3565         else
3566                 num_sge =  1+ scale + (ioc->req_sz - 64) / (sizeof(dma_addr_t) + sizeof(u32));
3567
3568         if (sizeof(dma_addr_t) == sizeof(u64)) {
3569                 numSGE = (scale - 1) * (ioc->facts.MaxChainDepth-1) + scale +
3570                         (ioc->req_sz - 60) / (sizeof(dma_addr_t) + sizeof(u32));
3571         } else {
3572                 numSGE = 1 + (scale - 1) * (ioc->facts.MaxChainDepth-1) + scale +
3573                         (ioc->req_sz - 64) / (sizeof(dma_addr_t) + sizeof(u32));
3574         }
3575         dinitprintk((KERN_INFO MYNAM ": %s num_sge=%d numSGE=%d\n",
3576                 ioc->name, num_sge, numSGE));
3577
3578         if ( numSGE > MPT_SCSI_SG_DEPTH )
3579                 numSGE = MPT_SCSI_SG_DEPTH;
3580
3581         num_chain = 1;
3582         while (numSGE - num_sge > 0) {
3583                 num_chain++;
3584                 num_sge += (scale - 1);
3585         }
3586         num_chain++;
3587
3588         dinitprintk((KERN_INFO MYNAM ": %s Now numSGE=%d num_sge=%d num_chain=%d\n",
3589                 ioc->name, numSGE, num_sge, num_chain));
3590
3591         if (ioc->bus_type == SPI)
3592                 num_chain *= MPT_SCSI_CAN_QUEUE;
3593         else
3594                 num_chain *= MPT_FC_CAN_QUEUE;
3595
3596         ioc->num_chain = num_chain;
3597
3598         sz = num_chain * sizeof(int);
3599         if (ioc->ChainToChain == NULL) {
3600                 mem = kmalloc(sz, GFP_ATOMIC);
3601                 if (mem == NULL)
3602                         return -1;
3603
3604                 ioc->ChainToChain = (int *) mem;
3605                 dinitprintk((KERN_INFO MYNAM ": %s ChainToChain alloc @ %p, sz=%d bytes\n",
3606                                 ioc->name, mem, sz));
3607         } else {
3608                 mem = (u8 *) ioc->ChainToChain;
3609         }
3610         memset(mem, 0xFF, sz);
3611         return num_chain;
3612 }
3613
3614 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3615 /*
3616  *      PrimeIocFifos - Initialize IOC request and reply FIFOs.
3617  *      @ioc: Pointer to MPT_ADAPTER structure
3618  *
3619  *      This routine allocates memory for the MPT reply and request frame
3620  *      pools (if necessary), and primes the IOC reply FIFO with
3621  *      reply frames.
3622  *
3623  *      Returns 0 for success, non-zero for failure.
3624  */
3625 static int
3626 PrimeIocFifos(MPT_ADAPTER *ioc)
3627 {
3628         MPT_FRAME_HDR *mf;
3629         unsigned long flags;
3630         dma_addr_t alloc_dma;
3631         u8 *mem;
3632         int i, reply_sz, sz, total_size, num_chain;
3633
3634         /*  Prime reply FIFO...  */
3635
3636         if (ioc->reply_frames == NULL) {
3637                 if ( (num_chain = initChainBuffers(ioc)) < 0)
3638                         return -1;
3639
3640                 total_size = reply_sz = (ioc->reply_sz * ioc->reply_depth);
3641                 dinitprintk((KERN_INFO MYNAM ": %s.ReplyBuffer sz=%d bytes, ReplyDepth=%d\n",
3642                                 ioc->name, ioc->reply_sz, ioc->reply_depth));
3643                 dinitprintk((KERN_INFO MYNAM ": %s.ReplyBuffer sz=%d[%x] bytes\n",
3644                                 ioc->name, reply_sz, reply_sz));
3645
3646                 sz = (ioc->req_sz * ioc->req_depth);
3647                 dinitprintk((KERN_INFO MYNAM ": %s.RequestBuffer sz=%d bytes, RequestDepth=%d\n",
3648                                 ioc->name, ioc->req_sz, ioc->req_depth));
3649                 dinitprintk((KERN_INFO MYNAM ": %s.RequestBuffer sz=%d[%x] bytes\n",
3650                                 ioc->name, sz, sz));
3651                 total_size += sz;
3652
3653                 sz = num_chain * ioc->req_sz; /* chain buffer pool size */
3654                 dinitprintk((KERN_INFO MYNAM ": %s.ChainBuffer sz=%d bytes, ChainDepth=%d\n",
3655                                 ioc->name, ioc->req_sz, num_chain));
3656                 dinitprintk((KERN_INFO MYNAM ": %s.ChainBuffer sz=%d[%x] bytes num_chain=%d\n",
3657                                 ioc->name, sz, sz, num_chain));
3658
3659                 total_size += sz;
3660                 mem = pci_alloc_consistent(ioc->pcidev, total_size, &alloc_dma);
3661                 if (mem == NULL) {
3662                         printk(MYIOC_s_ERR_FMT "Unable to allocate Reply, Request, Chain Buffers!\n",
3663                                 ioc->name);
3664                         goto out_fail;
3665                 }
3666
3667                 dinitprintk((KERN_INFO MYNAM ": %s.Total alloc @ %p[%p], sz=%d[%x] bytes\n",
3668                                 ioc->name, mem, (void *)(ulong)alloc_dma, total_size, total_size));
3669
3670                 memset(mem, 0, total_size);
3671                 ioc->alloc_total += total_size;
3672                 ioc->alloc = mem;
3673                 ioc->alloc_dma = alloc_dma;
3674                 ioc->alloc_sz = total_size;
3675                 ioc->reply_frames = (MPT_FRAME_HDR *) mem;
3676                 ioc->reply_frames_low_dma = (u32) (alloc_dma & 0xFFFFFFFF);
3677
3678                 dinitprintk((KERN_INFO MYNAM ": %s ReplyBuffers @ %p[%p]\n",
3679                         ioc->name, ioc->reply_frames, (void *)(ulong)alloc_dma));
3680
3681                 alloc_dma += reply_sz;
3682                 mem += reply_sz;
3683
3684                 /*  Request FIFO - WE manage this!  */
3685
3686                 ioc->req_frames = (MPT_FRAME_HDR *) mem;
3687                 ioc->req_frames_dma = alloc_dma;
3688
3689                 dinitprintk((KERN_INFO MYNAM ": %s RequestBuffers @ %p[%p]\n",
3690                                 ioc->name, mem, (void *)(ulong)alloc_dma));
3691
3692                 ioc->req_frames_low_dma = (u32) (alloc_dma & 0xFFFFFFFF);
3693
3694 #if defined(CONFIG_MTRR) && 0
3695                 /*
3696                  *  Enable Write Combining MTRR for IOC's memory region.
3697                  *  (at least as much as we can; "size and base must be
3698                  *  multiples of 4 kiB"
3699                  */
3700                 ioc->mtrr_reg = mtrr_add(ioc->req_frames_dma,
3701                                          sz,
3702                                          MTRR_TYPE_WRCOMB, 1);
3703                 dprintk((MYIOC_s_INFO_FMT "MTRR region registered (base:size=%08x:%x)\n",
3704                                 ioc->name, ioc->req_frames_dma, sz));
3705 #endif
3706
3707                 for (i = 0; i < ioc->req_depth; i++) {
3708                         alloc_dma += ioc->req_sz;
3709                         mem += ioc->req_sz;
3710                 }
3711
3712                 ioc->ChainBuffer = mem;
3713                 ioc->ChainBufferDMA = alloc_dma;
3714
3715                 dinitprintk((KERN_INFO MYNAM " :%s ChainBuffers @ %p(%p)\n",
3716                         ioc->name, ioc->ChainBuffer, (void *)(ulong)ioc->ChainBufferDMA));
3717
3718                 /* Initialize the free chain Q.
3719                 */
3720
3721                 INIT_LIST_HEAD(&ioc->FreeChainQ);
3722
3723                 /* Post the chain buffers to the FreeChainQ.
3724                 */
3725                 mem = (u8 *)ioc->ChainBuffer;
3726                 for (i=0; i < num_chain; i++) {
3727                         mf = (MPT_FRAME_HDR *) mem;
3728                         list_add_tail(&mf->u.frame.linkage.list, &ioc->FreeChainQ);
3729                         mem += ioc->req_sz;
3730                 }
3731
3732                 /* Initialize Request frames linked list
3733                  */
3734                 alloc_dma = ioc->req_frames_dma;
3735                 mem = (u8 *) ioc->req_frames;
3736
3737                 spin_lock_irqsave(&ioc->FreeQlock, flags);
3738                 INIT_LIST_HEAD(&ioc->FreeQ);
3739                 for (i = 0; i < ioc->req_depth; i++) {
3740                         mf = (MPT_FRAME_HDR *) mem;
3741
3742                         /*  Queue REQUESTs *internally*!  */
3743                         list_add_tail(&mf->u.frame.linkage.list, &ioc->FreeQ);
3744
3745                         mem += ioc->req_sz;
3746                 }
3747                 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
3748
3749                 sz = (ioc->req_depth * MPT_SENSE_BUFFER_ALLOC);
3750                 ioc->sense_buf_pool =
3751                         pci_alloc_consistent(ioc->pcidev, sz, &ioc->sense_buf_pool_dma);
3752                 if (ioc->sense_buf_pool == NULL) {
3753                         printk(MYIOC_s_ERR_FMT "Unable to allocate Sense Buffers!\n",
3754                                 ioc->name);
3755                         goto out_fail;
3756                 }
3757
3758                 ioc->sense_buf_low_dma = (u32) (ioc->sense_buf_pool_dma & 0xFFFFFFFF);
3759                 ioc->alloc_total += sz;
3760                 dinitprintk((KERN_INFO MYNAM ": %s.SenseBuffers @ %p[%p]\n",
3761                         ioc->name, ioc->sense_buf_pool, (void *)(ulong)ioc->sense_buf_pool_dma));
3762
3763         }
3764
3765         /* Post Reply frames to FIFO
3766          */
3767         alloc_dma = ioc->alloc_dma;
3768         dinitprintk((KERN_INFO MYNAM ": %s.ReplyBuffers @ %p[%p]\n",
3769                 ioc->name, ioc->reply_frames, (void *)(ulong)alloc_dma));
3770
3771         for (i = 0; i < ioc->reply_depth; i++) {
3772                 /*  Write each address to the IOC!  */
3773                 CHIPREG_WRITE32(&ioc->chip->ReplyFifo, alloc_dma);
3774                 alloc_dma += ioc->reply_sz;
3775         }
3776
3777         return 0;
3778
3779 out_fail:
3780         if (ioc->alloc != NULL) {
3781                 sz = ioc->alloc_sz;
3782                 pci_free_consistent(ioc->pcidev,
3783                                 sz,
3784                                 ioc->alloc, ioc->alloc_dma);
3785                 ioc->reply_frames = NULL;
3786                 ioc->req_frames = NULL;
3787                 ioc->alloc_total -= sz;
3788         }
3789         if (ioc->sense_buf_pool != NULL) {
3790                 sz = (ioc->req_depth * MPT_SENSE_BUFFER_ALLOC);
3791                 pci_free_consistent(ioc->pcidev,
3792                                 sz,
3793                                 ioc->sense_buf_pool, ioc->sense_buf_pool_dma);
3794                 ioc->sense_buf_pool = NULL;
3795         }
3796         return -1;
3797 }
3798
3799 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3800 /**
3801  *      mpt_handshake_req_reply_wait - Send MPT request to and receive reply
3802  *      from IOC via doorbell handshake method.
3803  *      @ioc: Pointer to MPT_ADAPTER structure
3804  *      @reqBytes: Size of the request in bytes
3805  *      @req: Pointer to MPT request frame
3806  *      @replyBytes: Expected size of the reply in bytes
3807  *      @u16reply: Pointer to area where reply should be written
3808  *      @maxwait: Max wait time for a reply (in seconds)
3809  *      @sleepFlag: Specifies whether the process can sleep
3810  *
3811  *      NOTES: It is the callers responsibility to byte-swap fields in the
3812  *      request which are greater than 1 byte in size.  It is also the
3813  *      callers responsibility to byte-swap response fields which are
3814  *      greater than 1 byte in size.
3815  *
3816  *      Returns 0 for success, non-zero for failure.
3817  */
3818 static int
3819 mpt_handshake_req_reply_wait(MPT_ADAPTER *ioc, int reqBytes, u32 *req,
3820                 int replyBytes, u16 *u16reply, int maxwait, int sleepFlag)
3821 {
3822         MPIDefaultReply_t *mptReply;
3823         int failcnt = 0;
3824         int t;
3825
3826         /*
3827          * Get ready to cache a handshake reply
3828          */
3829         ioc->hs_reply_idx = 0;
3830         mptReply = (MPIDefaultReply_t *) ioc->hs_reply;
3831         mptReply->MsgLength = 0;
3832
3833         /*
3834          * Make sure there are no doorbells (WRITE 0 to IntStatus reg),
3835          * then tell IOC that we want to handshake a request of N words.
3836          * (WRITE u32val to Doorbell reg).
3837          */
3838         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
3839         CHIPREG_WRITE32(&ioc->chip->Doorbell,
3840                         ((MPI_FUNCTION_HANDSHAKE<<MPI_DOORBELL_FUNCTION_SHIFT) |
3841                          ((reqBytes/4)<<MPI_DOORBELL_ADD_DWORDS_SHIFT)));
3842
3843         /*
3844          * Wait for IOC's doorbell handshake int
3845          */
3846         if ((t = WaitForDoorbellInt(ioc, 5, sleepFlag)) < 0)
3847                 failcnt++;
3848
3849         dhsprintk((MYIOC_s_INFO_FMT "HandShake request start reqBytes=%d, WaitCnt=%d%s\n",
3850                         ioc->name, reqBytes, t, failcnt ? " - MISSING DOORBELL HANDSHAKE!" : ""));
3851
3852         /* Read doorbell and check for active bit */
3853         if (!(CHIPREG_READ32(&ioc->chip->Doorbell) & MPI_DOORBELL_ACTIVE))
3854                         return -1;
3855
3856         /*
3857          * Clear doorbell int (WRITE 0 to IntStatus reg),
3858          * then wait for IOC to ACKnowledge that it's ready for
3859          * our handshake request.
3860          */
3861         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
3862         if (!failcnt && (t = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0)
3863                 failcnt++;
3864
3865         if (!failcnt) {
3866                 int      ii;
3867                 u8      *req_as_bytes = (u8 *) req;
3868
3869                 /*
3870                  * Stuff request words via doorbell handshake,
3871                  * with ACK from IOC for each.
3872                  */
3873                 for (ii = 0; !failcnt && ii < reqBytes/4; ii++) {
3874                         u32 word = ((req_as_bytes[(ii*4) + 0] <<  0) |
3875                                     (req_as_bytes[(ii*4) + 1] <<  8) |
3876                                     (req_as_bytes[(ii*4) + 2] << 16) |
3877                                     (req_as_bytes[(ii*4) + 3] << 24));
3878
3879                         CHIPREG_WRITE32(&ioc->chip->Doorbell, word);
3880                         if ((t = WaitForDoorbellAck(ioc, 5, sleepFlag)) < 0)
3881                                 failcnt++;
3882                 }
3883
3884                 dhsprintk((KERN_INFO MYNAM ": Handshake request frame (@%p) header\n", req));
3885                 DBG_DUMP_REQUEST_FRAME_HDR(req)
3886
3887                 dhsprintk((MYIOC_s_INFO_FMT "HandShake request post done, WaitCnt=%d%s\n",
3888                                 ioc->name, t, failcnt ? " - MISSING DOORBELL ACK!" : ""));
3889
3890                 /*
3891                  * Wait for completion of doorbell handshake reply from the IOC
3892                  */
3893                 if (!failcnt && (t = WaitForDoorbellReply(ioc, maxwait, sleepFlag)) < 0)
3894                         failcnt++;
3895
3896                 dhsprintk((MYIOC_s_INFO_FMT "HandShake reply count=%d%s\n",
3897                                 ioc->name, t, failcnt ? " - MISSING DOORBELL REPLY!" : ""));
3898
3899                 /*
3900                  * Copy out the cached reply...
3901                  */
3902                 for (ii=0; ii < min(replyBytes/2,mptReply->MsgLength*2); ii++)
3903                         u16reply[ii] = ioc->hs_reply[ii];
3904         } else {
3905                 return -99;
3906         }
3907
3908         return -failcnt;
3909 }
3910
3911 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3912 /*
3913  *      WaitForDoorbellAck - Wait for IOC to clear the IOP_DOORBELL_STATUS bit
3914  *      in it's IntStatus register.
3915  *      @ioc: Pointer to MPT_ADAPTER structure
3916  *      @howlong: How long to wait (in seconds)
3917  *      @sleepFlag: Specifies whether the process can sleep
3918  *
3919  *      This routine waits (up to ~2 seconds max) for IOC doorbell
3920  *      handshake ACKnowledge.
3921  *
3922  *      Returns a negative value on failure, else wait loop count.
3923  */
3924 static int
3925 WaitForDoorbellAck(MPT_ADAPTER *ioc, int howlong, int sleepFlag)
3926 {
3927         int cntdn;
3928         int count = 0;
3929         u32 intstat=0;
3930
3931         cntdn = 1000 * howlong;
3932
3933         if (sleepFlag == CAN_SLEEP) {
3934                 while (--cntdn) {
3935                         intstat = CHIPREG_READ32(&ioc->chip->IntStatus);
3936                         if (! (intstat & MPI_HIS_IOP_DOORBELL_STATUS))
3937                                 break;
3938                         msleep_interruptible (1);
3939                         count++;
3940                 }
3941         } else {
3942                 while (--cntdn) {
3943                         intstat = CHIPREG_READ32(&ioc->chip->IntStatus);
3944                         if (! (intstat & MPI_HIS_IOP_DOORBELL_STATUS))
3945                                 break;
3946                         mdelay (1);
3947                         count++;
3948                 }
3949         }
3950
3951         if (cntdn) {
3952                 dprintk((MYIOC_s_INFO_FMT "WaitForDoorbell ACK (count=%d)\n",
3953                                 ioc->name, count));
3954                 return count;
3955         }
3956
3957         printk(MYIOC_s_ERR_FMT "Doorbell ACK timeout (count=%d), IntStatus=%x!\n",
3958                         ioc->name, count, intstat);
3959         return -1;
3960 }
3961
3962 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3963 /*
3964  *      WaitForDoorbellInt - Wait for IOC to set the HIS_DOORBELL_INTERRUPT bit
3965  *      in it's IntStatus register.
3966  *      @ioc: Pointer to MPT_ADAPTER structure
3967  *      @howlong: How long to wait (in seconds)
3968  *      @sleepFlag: Specifies whether the process can sleep
3969  *
3970  *      This routine waits (up to ~2 seconds max) for IOC doorbell interrupt.
3971  *
3972  *      Returns a negative value on failure, else wait loop count.
3973  */
3974 static int
3975 WaitForDoorbellInt(MPT_ADAPTER *ioc, int howlong, int sleepFlag)
3976 {
3977         int cntdn;
3978         int count = 0;
3979         u32 intstat=0;
3980
3981         cntdn = 1000 * howlong;
3982         if (sleepFlag == CAN_SLEEP) {
3983                 while (--cntdn) {
3984                         intstat = CHIPREG_READ32(&ioc->chip->IntStatus);
3985                         if (intstat & MPI_HIS_DOORBELL_INTERRUPT)
3986                                 break;
3987                         msleep_interruptible(1);
3988                         count++;
3989                 }
3990         } else {
3991                 while (--cntdn) {
3992                         intstat = CHIPREG_READ32(&ioc->chip->IntStatus);
3993                         if (intstat & MPI_HIS_DOORBELL_INTERRUPT)
3994                                 break;
3995                         mdelay(1);
3996                         count++;
3997                 }
3998         }
3999
4000         if (cntdn) {
4001                 dprintk((MYIOC_s_INFO_FMT "WaitForDoorbell INT (cnt=%d) howlong=%d\n",
4002                                 ioc->name, count, howlong));
4003                 return count;
4004         }
4005
4006         printk(MYIOC_s_ERR_FMT "Doorbell INT timeout (count=%d), IntStatus=%x!\n",
4007                         ioc->name, count, intstat);
4008         return -1;
4009 }
4010
4011 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4012 /*
4013  *      WaitForDoorbellReply - Wait for and capture a IOC handshake reply.
4014  *      @ioc: Pointer to MPT_ADAPTER structure
4015  *      @howlong: How long to wait (in seconds)
4016  *      @sleepFlag: Specifies whether the process can sleep
4017  *
4018  *      This routine polls the IOC for a handshake reply, 16 bits at a time.
4019  *      Reply is cached to IOC private area large enough to hold a maximum
4020  *      of 128 bytes of reply data.
4021  *
4022  *      Returns a negative value on failure, else size of reply in WORDS.
4023  */
4024 static int
4025 WaitForDoorbellReply(MPT_ADAPTER *ioc, int howlong, int sleepFlag)
4026 {
4027         int u16cnt = 0;
4028         int failcnt = 0;
4029         int t;
4030         u16 *hs_reply = ioc->hs_reply;
4031         volatile MPIDefaultReply_t *mptReply = (MPIDefaultReply_t *) ioc->hs_reply;
4032         u16 hword;
4033
4034         hs_reply[0] = hs_reply[1] = hs_reply[7] = 0;
4035
4036         /*
4037          * Get first two u16's so we can look at IOC's intended reply MsgLength
4038          */
4039         u16cnt=0;
4040         if ((t = WaitForDoorbellInt(ioc, howlong, sleepFlag)) < 0) {
4041                 failcnt++;
4042         } else {
4043                 hs_reply[u16cnt++] = le16_to_cpu(CHIPREG_READ32(&ioc->chip->Doorbell) & 0x0000FFFF);
4044                 CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
4045                 if ((t = WaitForDoorbellInt(ioc, 5, sleepFlag)) < 0)
4046                         failcnt++;
4047                 else {
4048                         hs_reply[u16cnt++] = le16_to_cpu(CHIPREG_READ32(&ioc->chip->Doorbell) & 0x0000FFFF);
4049                         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
4050                 }
4051         }
4052
4053         dhsprintk((MYIOC_s_INFO_FMT "WaitCnt=%d First handshake reply word=%08x%s\n",
4054                         ioc->name, t, le32_to_cpu(*(u32 *)hs_reply),
4055                         failcnt ? " - MISSING DOORBELL HANDSHAKE!" : ""));
4056
4057         /*
4058          * If no error (and IOC said MsgLength is > 0), piece together
4059          * reply 16 bits at a time.
4060          */
4061         for (u16cnt=2; !failcnt && u16cnt < (2 * mptReply->MsgLength); u16cnt++) {
4062                 if ((t = WaitForDoorbellInt(ioc, 5, sleepFlag)) < 0)
4063                         failcnt++;
4064                 hword = le16_to_cpu(CHIPREG_READ32(&ioc->chip->Doorbell) & 0x0000FFFF);
4065                 /* don't overflow our IOC hs_reply[] buffer! */
4066                 if (u16cnt < sizeof(ioc->hs_reply) / sizeof(ioc->hs_reply[0]))
4067                         hs_reply[u16cnt] = hword;
4068                 CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
4069         }
4070
4071         if (!failcnt && (t = WaitForDoorbellInt(ioc, 5, sleepFlag)) < 0)
4072                 failcnt++;
4073         CHIPREG_WRITE32(&ioc->chip->IntStatus, 0);
4074
4075         if (failcnt) {
4076                 printk(MYIOC_s_ERR_FMT "Handshake reply failure!\n",
4077                                 ioc->name);
4078                 return -failcnt;
4079         }
4080 #if 0
4081         else if (u16cnt != (2 * mptReply->MsgLength)) {
4082                 return -101;
4083         }
4084         else if ((mptReply->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
4085                 return -102;
4086         }
4087 #endif
4088
4089         dhsprintk((MYIOC_s_INFO_FMT "Got Handshake reply:\n", ioc->name));
4090         DBG_DUMP_REPLY_FRAME(mptReply)
4091
4092         dhsprintk((MYIOC_s_INFO_FMT "WaitForDoorbell REPLY WaitCnt=%d (sz=%d)\n",
4093                         ioc->name, t, u16cnt/2));
4094         return u16cnt/2;
4095 }
4096
4097 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4098 /*
4099  *      GetLanConfigPages - Fetch LANConfig pages.
4100  *      @ioc: Pointer to MPT_ADAPTER structure
4101  *
4102  *      Return: 0 for success
4103  *      -ENOMEM if no memory available
4104  *              -EPERM if not allowed due to ISR context
4105  *              -EAGAIN if no msg frames currently available
4106  *              -EFAULT for non-successful reply or no reply (timeout)
4107  */
4108 static int
4109 GetLanConfigPages(MPT_ADAPTER *ioc)
4110 {
4111         ConfigPageHeader_t       hdr;
4112         CONFIGPARMS              cfg;
4113         LANPage0_t              *ppage0_alloc;
4114         dma_addr_t               page0_dma;
4115         LANPage1_t              *ppage1_alloc;
4116         dma_addr_t               page1_dma;
4117         int                      rc = 0;
4118         int                      data_sz;
4119         int                      copy_sz;
4120
4121         /* Get LAN Page 0 header */
4122         hdr.PageVersion = 0;
4123         hdr.PageLength = 0;
4124         hdr.PageNumber = 0;
4125         hdr.PageType = MPI_CONFIG_PAGETYPE_LAN;
4126         cfg.cfghdr.hdr = &hdr;
4127         cfg.physAddr = -1;
4128         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4129         cfg.dir = 0;
4130         cfg.pageAddr = 0;
4131         cfg.timeout = 0;
4132
4133         if ((rc = mpt_config(ioc, &cfg)) != 0)
4134                 return rc;
4135
4136         if (hdr.PageLength > 0) {
4137                 data_sz = hdr.PageLength * 4;
4138                 ppage0_alloc = (LANPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
4139                 rc = -ENOMEM;
4140                 if (ppage0_alloc) {
4141                         memset((u8 *)ppage0_alloc, 0, data_sz);
4142                         cfg.physAddr = page0_dma;
4143                         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4144
4145                         if ((rc = mpt_config(ioc, &cfg)) == 0) {
4146                                 /* save the data */
4147                                 copy_sz = min_t(int, sizeof(LANPage0_t), data_sz);
4148                                 memcpy(&ioc->lan_cnfg_page0, ppage0_alloc, copy_sz);
4149
4150                         }
4151
4152                         pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
4153
4154                         /* FIXME!
4155                          *      Normalize endianness of structure data,
4156                          *      by byte-swapping all > 1 byte fields!
4157                          */
4158
4159                 }
4160
4161                 if (rc)
4162                         return rc;
4163         }
4164
4165         /* Get LAN Page 1 header */
4166         hdr.PageVersion = 0;
4167         hdr.PageLength = 0;
4168         hdr.PageNumber = 1;
4169         hdr.PageType = MPI_CONFIG_PAGETYPE_LAN;
4170         cfg.cfghdr.hdr = &hdr;
4171         cfg.physAddr = -1;
4172         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4173         cfg.dir = 0;
4174         cfg.pageAddr = 0;
4175
4176         if ((rc = mpt_config(ioc, &cfg)) != 0)
4177                 return rc;
4178
4179         if (hdr.PageLength == 0)
4180                 return 0;
4181
4182         data_sz = hdr.PageLength * 4;
4183         rc = -ENOMEM;
4184         ppage1_alloc = (LANPage1_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page1_dma);
4185         if (ppage1_alloc) {
4186                 memset((u8 *)ppage1_alloc, 0, data_sz);
4187                 cfg.physAddr = page1_dma;
4188                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4189
4190                 if ((rc = mpt_config(ioc, &cfg)) == 0) {
4191                         /* save the data */
4192                         copy_sz = min_t(int, sizeof(LANPage1_t), data_sz);
4193                         memcpy(&ioc->lan_cnfg_page1, ppage1_alloc, copy_sz);
4194                 }
4195
4196                 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage1_alloc, page1_dma);
4197
4198                 /* FIXME!
4199                  *      Normalize endianness of structure data,
4200                  *      by byte-swapping all > 1 byte fields!
4201                  */
4202
4203         }
4204
4205         return rc;
4206 }
4207
4208 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4209 /*
4210  *      mptbase_GetFcPortPage0 - Fetch FCPort config Page0.
4211  *      @ioc: Pointer to MPT_ADAPTER structure
4212  *      @portnum: IOC Port number
4213  *
4214  *      Return: 0 for success
4215  *      -ENOMEM if no memory available
4216  *              -EPERM if not allowed due to ISR context
4217  *              -EAGAIN if no msg frames currently available
4218  *              -EFAULT for non-successful reply or no reply (timeout)
4219  */
4220 int
4221 mptbase_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
4222 {
4223         ConfigPageHeader_t       hdr;
4224         CONFIGPARMS              cfg;
4225         FCPortPage0_t           *ppage0_alloc;
4226         FCPortPage0_t           *pp0dest;
4227         dma_addr_t               page0_dma;
4228         int                      data_sz;
4229         int                      copy_sz;
4230         int                      rc;
4231         int                      count = 400;
4232
4233
4234         /* Get FCPort Page 0 header */
4235         hdr.PageVersion = 0;
4236         hdr.PageLength = 0;
4237         hdr.PageNumber = 0;
4238         hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
4239         cfg.cfghdr.hdr = &hdr;
4240         cfg.physAddr = -1;
4241         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4242         cfg.dir = 0;
4243         cfg.pageAddr = portnum;
4244         cfg.timeout = 0;
4245
4246         if ((rc = mpt_config(ioc, &cfg)) != 0)
4247                 return rc;
4248
4249         if (hdr.PageLength == 0)
4250                 return 0;
4251
4252         data_sz = hdr.PageLength * 4;
4253         rc = -ENOMEM;
4254         ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
4255         if (ppage0_alloc) {
4256
4257  try_again:
4258                 memset((u8 *)ppage0_alloc, 0, data_sz);
4259                 cfg.physAddr = page0_dma;
4260                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4261
4262                 if ((rc = mpt_config(ioc, &cfg)) == 0) {
4263                         /* save the data */
4264                         pp0dest = &ioc->fc_port_page0[portnum];
4265                         copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
4266                         memcpy(pp0dest, ppage0_alloc, copy_sz);
4267
4268                         /*
4269                          *      Normalize endianness of structure data,
4270                          *      by byte-swapping all > 1 byte fields!
4271                          */
4272                         pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
4273                         pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
4274                         pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
4275                         pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
4276                         pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
4277                         pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
4278                         pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
4279                         pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
4280                         pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
4281                         pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
4282                         pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
4283                         pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
4284                         pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
4285                         pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
4286                         pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
4287                         pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
4288
4289                         /*
4290                          * if still doing discovery,
4291                          * hang loose a while until finished
4292                          */
4293                         if (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) {
4294                                 if (count-- > 0) {
4295                                         msleep_interruptible(100);
4296                                         goto try_again;
4297                                 }
4298                                 printk(MYIOC_s_INFO_FMT "Firmware discovery not"
4299                                                         " complete.\n",
4300                                                 ioc->name);
4301                         }
4302                 }
4303
4304                 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
4305         }
4306
4307         return rc;
4308 }
4309
4310 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4311 /*
4312  *      mptbase_sas_persist_operation - Perform operation on SAS Persitent Table
4313  *      @ioc: Pointer to MPT_ADAPTER structure
4314  *      @sas_address: 64bit SAS Address for operation.
4315  *      @target_id: specified target for operation
4316  *      @bus: specified bus for operation
4317  *      @persist_opcode: see below
4318  *
4319  *      MPI_SAS_OP_CLEAR_NOT_PRESENT - Free all persist TargetID mappings for
4320  *              devices not currently present.
4321  *      MPI_SAS_OP_CLEAR_ALL_PERSISTENT - Clear al persist TargetID mappings
4322  *
4323  *      NOTE: Don't use not this function during interrupt time.
4324  *
4325  *      Returns: 0 for success, non-zero error
4326  */
4327
4328 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4329 int
4330 mptbase_sas_persist_operation(MPT_ADAPTER *ioc, u8 persist_opcode)
4331 {
4332         SasIoUnitControlRequest_t       *sasIoUnitCntrReq;
4333         SasIoUnitControlReply_t         *sasIoUnitCntrReply;
4334         MPT_FRAME_HDR                   *mf = NULL;
4335         MPIHeader_t                     *mpi_hdr;
4336
4337
4338         /* insure garbage is not sent to fw */
4339         switch(persist_opcode) {
4340
4341         case MPI_SAS_OP_CLEAR_NOT_PRESENT:
4342         case MPI_SAS_OP_CLEAR_ALL_PERSISTENT:
4343                 break;
4344
4345         default:
4346                 return -1;
4347                 break;
4348         }
4349
4350         printk("%s: persist_opcode=%x\n",__FUNCTION__, persist_opcode);
4351
4352         /* Get a MF for this command.
4353          */
4354         if ((mf = mpt_get_msg_frame(mpt_base_index, ioc)) == NULL) {
4355                 printk("%s: no msg frames!\n",__FUNCTION__);
4356                 return -1;
4357         }
4358
4359         mpi_hdr = (MPIHeader_t *) mf;
4360         sasIoUnitCntrReq = (SasIoUnitControlRequest_t *)mf;
4361         memset(sasIoUnitCntrReq,0,sizeof(SasIoUnitControlRequest_t));
4362         sasIoUnitCntrReq->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
4363         sasIoUnitCntrReq->MsgContext = mpi_hdr->MsgContext;
4364         sasIoUnitCntrReq->Operation = persist_opcode;
4365
4366         init_timer(&ioc->persist_timer);
4367         ioc->persist_timer.data = (unsigned long) ioc;
4368         ioc->persist_timer.function = mpt_timer_expired;
4369         ioc->persist_timer.expires = jiffies + HZ*10 /* 10 sec */;
4370         ioc->persist_wait_done=0;
4371         add_timer(&ioc->persist_timer);
4372         mpt_put_msg_frame(mpt_base_index, ioc, mf);
4373         wait_event(mpt_waitq, ioc->persist_wait_done);
4374
4375         sasIoUnitCntrReply =
4376             (SasIoUnitControlReply_t *)ioc->persist_reply_frame;
4377         if (le16_to_cpu(sasIoUnitCntrReply->IOCStatus) != MPI_IOCSTATUS_SUCCESS) {
4378                 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
4379                     __FUNCTION__,
4380                     sasIoUnitCntrReply->IOCStatus,
4381                     sasIoUnitCntrReply->IOCLogInfo);
4382                 return -1;
4383         }
4384
4385         printk("%s: success\n",__FUNCTION__);
4386         return 0;
4387 }
4388
4389 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4390 /*
4391  *      GetIoUnitPage2 - Retrieve BIOS version and boot order information.
4392  *      @ioc: Pointer to MPT_ADAPTER structure
4393  *
4394  *      Returns: 0 for success
4395  *      -ENOMEM if no memory available
4396  *              -EPERM if not allowed due to ISR context
4397  *              -EAGAIN if no msg frames currently available
4398  *              -EFAULT for non-successful reply or no reply (timeout)
4399  */
4400 static int
4401 GetIoUnitPage2(MPT_ADAPTER *ioc)
4402 {
4403         ConfigPageHeader_t       hdr;
4404         CONFIGPARMS              cfg;
4405         IOUnitPage2_t           *ppage_alloc;
4406         dma_addr_t               page_dma;
4407         int                      data_sz;
4408         int                      rc;
4409
4410         /* Get the page header */
4411         hdr.PageVersion = 0;
4412         hdr.PageLength = 0;
4413         hdr.PageNumber = 2;
4414         hdr.PageType = MPI_CONFIG_PAGETYPE_IO_UNIT;
4415         cfg.cfghdr.hdr = &hdr;
4416         cfg.physAddr = -1;
4417         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4418         cfg.dir = 0;
4419         cfg.pageAddr = 0;
4420         cfg.timeout = 0;
4421
4422         if ((rc = mpt_config(ioc, &cfg)) != 0)
4423                 return rc;
4424
4425         if (hdr.PageLength == 0)
4426                 return 0;
4427
4428         /* Read the config page */
4429         data_sz = hdr.PageLength * 4;
4430         rc = -ENOMEM;
4431         ppage_alloc = (IOUnitPage2_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page_dma);
4432         if (ppage_alloc) {
4433                 memset((u8 *)ppage_alloc, 0, data_sz);
4434                 cfg.physAddr = page_dma;
4435                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4436
4437                 /* If Good, save data */
4438                 if ((rc = mpt_config(ioc, &cfg)) == 0)
4439                         ioc->biosVersion = le32_to_cpu(ppage_alloc->BiosVersion);
4440
4441                 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage_alloc, page_dma);
4442         }
4443
4444         return rc;
4445 }
4446
4447 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4448 /*      mpt_GetScsiPortSettings - read SCSI Port Page 0 and 2
4449  *      @ioc: Pointer to a Adapter Strucutre
4450  *      @portnum: IOC port number
4451  *
4452  *      Return: -EFAULT if read of config page header fails
4453  *                      or if no nvram
4454  *      If read of SCSI Port Page 0 fails,
4455  *              NVRAM = MPT_HOST_NVRAM_INVALID  (0xFFFFFFFF)
4456  *              Adapter settings: async, narrow
4457  *              Return 1
4458  *      If read of SCSI Port Page 2 fails,
4459  *              Adapter settings valid
4460  *              NVRAM = MPT_HOST_NVRAM_INVALID  (0xFFFFFFFF)
4461  *              Return 1
4462  *      Else
4463  *              Both valid
4464  *              Return 0
4465  *      CHECK - what type of locking mechanisms should be used????
4466  */
4467 static int
4468 mpt_GetScsiPortSettings(MPT_ADAPTER *ioc, int portnum)
4469 {
4470         u8                      *pbuf;
4471         dma_addr_t               buf_dma;
4472         CONFIGPARMS              cfg;
4473         ConfigPageHeader_t       header;
4474         int                      ii;
4475         int                      data, rc = 0;
4476
4477         /* Allocate memory
4478          */
4479         if (!ioc->spi_data.nvram) {
4480                 int      sz;
4481                 u8      *mem;
4482                 sz = MPT_MAX_SCSI_DEVICES * sizeof(int);
4483                 mem = kmalloc(sz, GFP_ATOMIC);
4484                 if (mem == NULL)
4485                         return -EFAULT;
4486
4487                 ioc->spi_data.nvram = (int *) mem;
4488
4489                 dprintk((MYIOC_s_INFO_FMT "SCSI device NVRAM settings @ %p, sz=%d\n",
4490                         ioc->name, ioc->spi_data.nvram, sz));
4491         }
4492
4493         /* Invalidate NVRAM information
4494          */
4495         for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
4496                 ioc->spi_data.nvram[ii] = MPT_HOST_NVRAM_INVALID;
4497         }
4498
4499         /* Read SPP0 header, allocate memory, then read page.
4500          */
4501         header.PageVersion = 0;
4502         header.PageLength = 0;
4503         header.PageNumber = 0;
4504         header.PageType = MPI_CONFIG_PAGETYPE_SCSI_PORT;
4505         cfg.cfghdr.hdr = &header;
4506         cfg.physAddr = -1;
4507         cfg.pageAddr = portnum;
4508         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4509         cfg.dir = 0;
4510         cfg.timeout = 0;        /* use default */
4511         if (mpt_config(ioc, &cfg) != 0)
4512                  return -EFAULT;
4513
4514         if (header.PageLength > 0) {
4515                 pbuf = pci_alloc_consistent(ioc->pcidev, header.PageLength * 4, &buf_dma);
4516                 if (pbuf) {
4517                         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4518                         cfg.physAddr = buf_dma;
4519                         if (mpt_config(ioc, &cfg) != 0) {
4520                                 ioc->spi_data.maxBusWidth = MPT_NARROW;
4521                                 ioc->spi_data.maxSyncOffset = 0;
4522                                 ioc->spi_data.minSyncFactor = MPT_ASYNC;
4523                                 ioc->spi_data.busType = MPT_HOST_BUS_UNKNOWN;
4524                                 rc = 1;
4525                                 ddvprintk((MYIOC_s_INFO_FMT "Unable to read PortPage0 minSyncFactor=%x\n",
4526                                         ioc->name, ioc->spi_data.minSyncFactor));
4527                         } else {
4528                                 /* Save the Port Page 0 data
4529                                  */
4530                                 SCSIPortPage0_t  *pPP0 = (SCSIPortPage0_t  *) pbuf;
4531                                 pPP0->Capabilities = le32_to_cpu(pPP0->Capabilities);
4532                                 pPP0->PhysicalInterface = le32_to_cpu(pPP0->PhysicalInterface);
4533
4534                                 if ( (pPP0->Capabilities & MPI_SCSIPORTPAGE0_CAP_QAS) == 0 ) {
4535                                         ioc->spi_data.noQas |= MPT_TARGET_NO_NEGO_QAS;
4536                                         ddvprintk((KERN_INFO MYNAM " :%s noQas due to Capabilities=%x\n",
4537                                                 ioc->name, pPP0->Capabilities));
4538                                 }
4539                                 ioc->spi_data.maxBusWidth = pPP0->Capabilities & MPI_SCSIPORTPAGE0_CAP_WIDE ? 1 : 0;
4540                                 data = pPP0->Capabilities & MPI_SCSIPORTPAGE0_CAP_MAX_SYNC_OFFSET_MASK;
4541                                 if (data) {
4542                                         ioc->spi_data.maxSyncOffset = (u8) (data >> 16);
4543                                         data = pPP0->Capabilities & MPI_SCSIPORTPAGE0_CAP_MIN_SYNC_PERIOD_MASK;
4544                                         ioc->spi_data.minSyncFactor = (u8) (data >> 8);
4545                                         ddvprintk((MYIOC_s_INFO_FMT "PortPage0 minSyncFactor=%x\n",
4546                                                 ioc->name, ioc->spi_data.minSyncFactor));
4547                                 } else {
4548                                         ioc->spi_data.maxSyncOffset = 0;
4549                                         ioc->spi_data.minSyncFactor = MPT_ASYNC;
4550                                 }
4551
4552                                 ioc->spi_data.busType = pPP0->PhysicalInterface & MPI_SCSIPORTPAGE0_PHY_SIGNAL_TYPE_MASK;
4553
4554                                 /* Update the minSyncFactor based on bus type.
4555                                  */
4556                                 if ((ioc->spi_data.busType == MPI_SCSIPORTPAGE0_PHY_SIGNAL_HVD) ||
4557                                         (ioc->spi_data.busType == MPI_SCSIPORTPAGE0_PHY_SIGNAL_SE))  {
4558
4559                                         if (ioc->spi_data.minSyncFactor < MPT_ULTRA) {
4560                                                 ioc->spi_data.minSyncFactor = MPT_ULTRA;
4561                                                 ddvprintk((MYIOC_s_INFO_FMT "HVD or SE detected, minSyncFactor=%x\n",
4562                                                         ioc->name, ioc->spi_data.minSyncFactor));
4563                                         }
4564                                 }
4565                         }
4566                         if (pbuf) {
4567                                 pci_free_consistent(ioc->pcidev, header.PageLength * 4, pbuf, buf_dma);
4568                         }
4569                 }
4570         }
4571
4572         /* SCSI Port Page 2 - Read the header then the page.
4573          */
4574         header.PageVersion = 0;
4575         header.PageLength = 0;
4576         header.PageNumber = 2;
4577         header.PageType = MPI_CONFIG_PAGETYPE_SCSI_PORT;
4578         cfg.cfghdr.hdr = &header;
4579         cfg.physAddr = -1;
4580         cfg.pageAddr = portnum;
4581         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4582         cfg.dir = 0;
4583         if (mpt_config(ioc, &cfg) != 0)
4584                 return -EFAULT;
4585
4586         if (header.PageLength > 0) {
4587                 /* Allocate memory and read SCSI Port Page 2
4588                  */
4589                 pbuf = pci_alloc_consistent(ioc->pcidev, header.PageLength * 4, &buf_dma);
4590                 if (pbuf) {
4591                         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_NVRAM;
4592                         cfg.physAddr = buf_dma;
4593                         if (mpt_config(ioc, &cfg) != 0) {
4594                                 /* Nvram data is left with INVALID mark
4595                                  */
4596                                 rc = 1;
4597                         } else {
4598                                 SCSIPortPage2_t *pPP2 = (SCSIPortPage2_t  *) pbuf;
4599                                 MpiDeviceInfo_t *pdevice = NULL;
4600
4601                                 /* Save the Port Page 2 data
4602                                  * (reformat into a 32bit quantity)
4603                                  */
4604                                 data = le32_to_cpu(pPP2->PortFlags) & MPI_SCSIPORTPAGE2_PORT_FLAGS_DV_MASK;
4605                                 ioc->spi_data.PortFlags = data;
4606                                 for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
4607                                         pdevice = &pPP2->DeviceSettings[ii];
4608                                         data = (le16_to_cpu(pdevice->DeviceFlags) << 16) |
4609                                                 (pdevice->SyncFactor << 8) | pdevice->Timeout;
4610                                         ioc->spi_data.nvram[ii] = data;
4611                                 }
4612                         }
4613
4614                         pci_free_consistent(ioc->pcidev, header.PageLength * 4, pbuf, buf_dma);
4615                 }
4616         }
4617
4618         /* Update Adapter limits with those from NVRAM
4619          * Comment: Don't need to do this. Target performance
4620          * parameters will never exceed the adapters limits.
4621          */
4622
4623         return rc;
4624 }
4625
4626 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4627 /*      mpt_readScsiDevicePageHeaders - save version and length of SDP1
4628  *      @ioc: Pointer to a Adapter Strucutre
4629  *      @portnum: IOC port number
4630  *
4631  *      Return: -EFAULT if read of config page header fails
4632  *              or 0 if success.
4633  */
4634 static int
4635 mpt_readScsiDevicePageHeaders(MPT_ADAPTER *ioc, int portnum)
4636 {
4637         CONFIGPARMS              cfg;
4638         ConfigPageHeader_t       header;
4639
4640         /* Read the SCSI Device Page 1 header
4641          */
4642         header.PageVersion = 0;
4643         header.PageLength = 0;
4644         header.PageNumber = 1;
4645         header.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
4646         cfg.cfghdr.hdr = &header;
4647         cfg.physAddr = -1;
4648         cfg.pageAddr = portnum;
4649         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4650         cfg.dir = 0;
4651         cfg.timeout = 0;
4652         if (mpt_config(ioc, &cfg) != 0)
4653                  return -EFAULT;
4654
4655         ioc->spi_data.sdp1version = cfg.cfghdr.hdr->PageVersion;
4656         ioc->spi_data.sdp1length = cfg.cfghdr.hdr->PageLength;
4657
4658         header.PageVersion = 0;
4659         header.PageLength = 0;
4660         header.PageNumber = 0;
4661         header.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
4662         if (mpt_config(ioc, &cfg) != 0)
4663                  return -EFAULT;
4664
4665         ioc->spi_data.sdp0version = cfg.cfghdr.hdr->PageVersion;
4666         ioc->spi_data.sdp0length = cfg.cfghdr.hdr->PageLength;
4667
4668         dcprintk((MYIOC_s_INFO_FMT "Headers: 0: version %d length %d\n",
4669                         ioc->name, ioc->spi_data.sdp0version, ioc->spi_data.sdp0length));
4670
4671         dcprintk((MYIOC_s_INFO_FMT "Headers: 1: version %d length %d\n",
4672                         ioc->name, ioc->spi_data.sdp1version, ioc->spi_data.sdp1length));
4673         return 0;
4674 }
4675
4676 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4677 /**
4678  *      mpt_findImVolumes - Identify IDs of hidden disks and RAID Volumes
4679  *      @ioc: Pointer to a Adapter Strucutre
4680  *      @portnum: IOC port number
4681  *
4682  *      Return:
4683  *      0 on success
4684  *      -EFAULT if read of config page header fails or data pointer not NULL
4685  *      -ENOMEM if pci_alloc failed
4686  */
4687 int
4688 mpt_findImVolumes(MPT_ADAPTER *ioc)
4689 {
4690         IOCPage2_t              *pIoc2;
4691         u8                      *mem;
4692         ConfigPageIoc2RaidVol_t *pIocRv;
4693         dma_addr_t               ioc2_dma;
4694         CONFIGPARMS              cfg;
4695         ConfigPageHeader_t       header;
4696         int                      jj;
4697         int                      rc = 0;
4698         int                      iocpage2sz;
4699         u8                       nVols, nPhys;
4700         u8                       vid, vbus, vioc;
4701
4702         /* Read IOCP2 header then the page.
4703          */
4704         header.PageVersion = 0;
4705         header.PageLength = 0;
4706         header.PageNumber = 2;
4707         header.PageType = MPI_CONFIG_PAGETYPE_IOC;
4708         cfg.cfghdr.hdr = &header;
4709         cfg.physAddr = -1;
4710         cfg.pageAddr = 0;
4711         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4712         cfg.dir = 0;
4713         cfg.timeout = 0;
4714         if (mpt_config(ioc, &cfg) != 0)
4715                  return -EFAULT;
4716
4717         if (header.PageLength == 0)
4718                 return -EFAULT;
4719
4720         iocpage2sz = header.PageLength * 4;
4721         pIoc2 = pci_alloc_consistent(ioc->pcidev, iocpage2sz, &ioc2_dma);
4722         if (!pIoc2)
4723                 return -ENOMEM;
4724
4725         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4726         cfg.physAddr = ioc2_dma;
4727         if (mpt_config(ioc, &cfg) != 0)
4728                 goto done_and_free;
4729
4730         if ( (mem = (u8 *)ioc->raid_data.pIocPg2) == NULL ) {
4731                 mem = kmalloc(iocpage2sz, GFP_ATOMIC);
4732                 if (mem) {
4733                         ioc->raid_data.pIocPg2 = (IOCPage2_t *) mem;
4734                 } else {
4735                         goto done_and_free;
4736                 }
4737         }
4738         memcpy(mem, (u8 *)pIoc2, iocpage2sz);
4739
4740         /* Identify RAID Volume Id's */
4741         nVols = pIoc2->NumActiveVolumes;
4742         if ( nVols == 0) {
4743                 /* No RAID Volume.
4744                  */
4745                 goto done_and_free;
4746         } else {
4747                 /* At least 1 RAID Volume
4748                  */
4749                 pIocRv = pIoc2->RaidVolume;
4750                 ioc->raid_data.isRaid = 0;
4751                 for (jj = 0; jj < nVols; jj++, pIocRv++) {
4752                         vid = pIocRv->VolumeID;
4753                         vbus = pIocRv->VolumeBus;
4754                         vioc = pIocRv->VolumeIOC;
4755
4756                         /* find the match
4757                          */
4758                         if (vbus == 0) {
4759                                 ioc->raid_data.isRaid |= (1 << vid);
4760                         } else {
4761                                 /* Error! Always bus 0
4762                                  */
4763                         }
4764                 }
4765         }
4766
4767         /* Identify Hidden Physical Disk Id's */
4768         nPhys = pIoc2->NumActivePhysDisks;
4769         if (nPhys == 0) {
4770                 /* No physical disks.
4771                  */
4772         } else {
4773                 mpt_read_ioc_pg_3(ioc);
4774         }
4775
4776 done_and_free:
4777         pci_free_consistent(ioc->pcidev, iocpage2sz, pIoc2, ioc2_dma);
4778
4779         return rc;
4780 }
4781
4782 int
4783 mpt_read_ioc_pg_3(MPT_ADAPTER *ioc)
4784 {
4785         IOCPage3_t              *pIoc3;
4786         u8                      *mem;
4787         CONFIGPARMS              cfg;
4788         ConfigPageHeader_t       header;
4789         dma_addr_t               ioc3_dma;
4790         int                      iocpage3sz = 0;
4791
4792         /* Free the old page
4793          */
4794         kfree(ioc->raid_data.pIocPg3);
4795         ioc->raid_data.pIocPg3 = NULL;
4796
4797         /* There is at least one physical disk.
4798          * Read and save IOC Page 3
4799          */
4800         header.PageVersion = 0;
4801         header.PageLength = 0;
4802         header.PageNumber = 3;
4803         header.PageType = MPI_CONFIG_PAGETYPE_IOC;
4804         cfg.cfghdr.hdr = &header;
4805         cfg.physAddr = -1;
4806         cfg.pageAddr = 0;
4807         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4808         cfg.dir = 0;
4809         cfg.timeout = 0;
4810         if (mpt_config(ioc, &cfg) != 0)
4811                 return 0;
4812
4813         if (header.PageLength == 0)
4814                 return 0;
4815
4816         /* Read Header good, alloc memory
4817          */
4818         iocpage3sz = header.PageLength * 4;
4819         pIoc3 = pci_alloc_consistent(ioc->pcidev, iocpage3sz, &ioc3_dma);
4820         if (!pIoc3)
4821                 return 0;
4822
4823         /* Read the Page and save the data
4824          * into malloc'd memory.
4825          */
4826         cfg.physAddr = ioc3_dma;
4827         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4828         if (mpt_config(ioc, &cfg) == 0) {
4829                 mem = kmalloc(iocpage3sz, GFP_ATOMIC);
4830                 if (mem) {
4831                         memcpy(mem, (u8 *)pIoc3, iocpage3sz);
4832                         ioc->raid_data.pIocPg3 = (IOCPage3_t *) mem;
4833                 }
4834         }
4835
4836         pci_free_consistent(ioc->pcidev, iocpage3sz, pIoc3, ioc3_dma);
4837
4838         return 0;
4839 }
4840
4841 static void
4842 mpt_read_ioc_pg_4(MPT_ADAPTER *ioc)
4843 {
4844         IOCPage4_t              *pIoc4;
4845         CONFIGPARMS              cfg;
4846         ConfigPageHeader_t       header;
4847         dma_addr_t               ioc4_dma;
4848         int                      iocpage4sz;
4849
4850         /* Read and save IOC Page 4
4851          */
4852         header.PageVersion = 0;
4853         header.PageLength = 0;
4854         header.PageNumber = 4;
4855         header.PageType = MPI_CONFIG_PAGETYPE_IOC;
4856         cfg.cfghdr.hdr = &header;
4857         cfg.physAddr = -1;
4858         cfg.pageAddr = 0;
4859         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4860         cfg.dir = 0;
4861         cfg.timeout = 0;
4862         if (mpt_config(ioc, &cfg) != 0)
4863                 return;
4864
4865         if (header.PageLength == 0)
4866                 return;
4867
4868         if ( (pIoc4 = ioc->spi_data.pIocPg4) == NULL ) {
4869                 iocpage4sz = (header.PageLength + 4) * 4; /* Allow 4 additional SEP's */
4870                 pIoc4 = pci_alloc_consistent(ioc->pcidev, iocpage4sz, &ioc4_dma);
4871                 if (!pIoc4)
4872                         return;
4873         } else {
4874                 ioc4_dma = ioc->spi_data.IocPg4_dma;
4875                 iocpage4sz = ioc->spi_data.IocPg4Sz;
4876         }
4877
4878         /* Read the Page into dma memory.
4879          */
4880         cfg.physAddr = ioc4_dma;
4881         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4882         if (mpt_config(ioc, &cfg) == 0) {
4883                 ioc->spi_data.pIocPg4 = (IOCPage4_t *) pIoc4;
4884                 ioc->spi_data.IocPg4_dma = ioc4_dma;
4885                 ioc->spi_data.IocPg4Sz = iocpage4sz;
4886         } else {
4887                 pci_free_consistent(ioc->pcidev, iocpage4sz, pIoc4, ioc4_dma);
4888                 ioc->spi_data.pIocPg4 = NULL;
4889         }
4890 }
4891
4892 static void
4893 mpt_read_ioc_pg_1(MPT_ADAPTER *ioc)
4894 {
4895         IOCPage1_t              *pIoc1;
4896         CONFIGPARMS              cfg;
4897         ConfigPageHeader_t       header;
4898         dma_addr_t               ioc1_dma;
4899         int                      iocpage1sz = 0;
4900         u32                      tmp;
4901
4902         /* Check the Coalescing Timeout in IOC Page 1
4903          */
4904         header.PageVersion = 0;
4905         header.PageLength = 0;
4906         header.PageNumber = 1;
4907         header.PageType = MPI_CONFIG_PAGETYPE_IOC;
4908         cfg.cfghdr.hdr = &header;
4909         cfg.physAddr = -1;
4910         cfg.pageAddr = 0;
4911         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
4912         cfg.dir = 0;
4913         cfg.timeout = 0;
4914         if (mpt_config(ioc, &cfg) != 0)
4915                 return;
4916
4917         if (header.PageLength == 0)
4918                 return;
4919
4920         /* Read Header good, alloc memory
4921          */
4922         iocpage1sz = header.PageLength * 4;
4923         pIoc1 = pci_alloc_consistent(ioc->pcidev, iocpage1sz, &ioc1_dma);
4924         if (!pIoc1)
4925                 return;
4926
4927         /* Read the Page and check coalescing timeout
4928          */
4929         cfg.physAddr = ioc1_dma;
4930         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
4931         if (mpt_config(ioc, &cfg) == 0) {
4932                 
4933                 tmp = le32_to_cpu(pIoc1->Flags) & MPI_IOCPAGE1_REPLY_COALESCING;
4934                 if (tmp == MPI_IOCPAGE1_REPLY_COALESCING) {
4935                         tmp = le32_to_cpu(pIoc1->CoalescingTimeout);
4936
4937                         dprintk((MYIOC_s_INFO_FMT "Coalescing Enabled Timeout = %d\n",
4938                                         ioc->name, tmp));
4939
4940                         if (tmp > MPT_COALESCING_TIMEOUT) {
4941                                 pIoc1->CoalescingTimeout = cpu_to_le32(MPT_COALESCING_TIMEOUT);
4942
4943                                 /* Write NVRAM and current
4944                                  */
4945                                 cfg.dir = 1;
4946                                 cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
4947                                 if (mpt_config(ioc, &cfg) == 0) {
4948                                         dprintk((MYIOC_s_INFO_FMT "Reset Current Coalescing Timeout to = %d\n",
4949                                                         ioc->name, MPT_COALESCING_TIMEOUT));
4950
4951                                         cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM;
4952                                         if (mpt_config(ioc, &cfg) == 0) {
4953                                                 dprintk((MYIOC_s_INFO_FMT "Reset NVRAM Coalescing Timeout to = %d\n",
4954                                                                 ioc->name, MPT_COALESCING_TIMEOUT));
4955                                         } else {
4956                                                 dprintk((MYIOC_s_INFO_FMT "Reset NVRAM Coalescing Timeout Failed\n",
4957                                                                         ioc->name));
4958                                         }
4959
4960                                 } else {
4961                                         dprintk((MYIOC_s_WARN_FMT "Reset of Current Coalescing Timeout Failed!\n",
4962                                                                 ioc->name));
4963                                 }
4964                         }
4965
4966                 } else {
4967                         dprintk((MYIOC_s_WARN_FMT "Coalescing Disabled\n", ioc->name));
4968                 }
4969         }
4970
4971         pci_free_consistent(ioc->pcidev, iocpage1sz, pIoc1, ioc1_dma);
4972
4973         return;
4974 }
4975
4976 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4977 /*
4978  *      SendEventNotification - Send EventNotification (on or off) request
4979  *      to MPT adapter.
4980  *      @ioc: Pointer to MPT_ADAPTER structure
4981  *      @EvSwitch: Event switch flags
4982  */
4983 static int
4984 SendEventNotification(MPT_ADAPTER *ioc, u8 EvSwitch)
4985 {
4986         EventNotification_t     *evnp;
4987
4988         evnp = (EventNotification_t *) mpt_get_msg_frame(mpt_base_index, ioc);
4989         if (evnp == NULL) {
4990                 devtprintk((MYIOC_s_WARN_FMT "Unable to allocate event request frame!\n",
4991                                 ioc->name));
4992                 return 0;
4993         }
4994         memset(evnp, 0, sizeof(*evnp));
4995
4996         devtprintk((MYIOC_s_INFO_FMT "Sending EventNotification (%d) request %p\n", ioc->name, EvSwitch, evnp));
4997
4998         evnp->Function = MPI_FUNCTION_EVENT_NOTIFICATION;
4999         evnp->ChainOffset = 0;
5000         evnp->MsgFlags = 0;
5001         evnp->Switch = EvSwitch;
5002
5003         mpt_put_msg_frame(mpt_base_index, ioc, (MPT_FRAME_HDR *)evnp);
5004
5005         return 0;
5006 }
5007
5008 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5009 /**
5010  *      SendEventAck - Send EventAck request to MPT adapter.
5011  *      @ioc: Pointer to MPT_ADAPTER structure
5012  *      @evnp: Pointer to original EventNotification request
5013  */
5014 static int
5015 SendEventAck(MPT_ADAPTER *ioc, EventNotificationReply_t *evnp)
5016 {
5017         EventAck_t      *pAck;
5018
5019         if ((pAck = (EventAck_t *) mpt_get_msg_frame(mpt_base_index, ioc)) == NULL) {
5020                 printk(MYIOC_s_WARN_FMT "Unable to allocate event ACK "
5021                         "request frame for Event=%x EventContext=%x EventData=%x!\n",
5022                         ioc->name, evnp->Event, le32_to_cpu(evnp->EventContext),
5023                         le32_to_cpu(evnp->Data[0]));
5024                 return -1;
5025         }
5026         memset(pAck, 0, sizeof(*pAck));
5027
5028         dprintk((MYIOC_s_INFO_FMT "Sending EventAck\n", ioc->name));
5029
5030         pAck->Function     = MPI_FUNCTION_EVENT_ACK;
5031         pAck->ChainOffset  = 0;
5032         pAck->MsgFlags     = 0;
5033         pAck->Event        = evnp->Event;
5034         pAck->EventContext = evnp->EventContext;
5035
5036         mpt_put_msg_frame(mpt_base_index, ioc, (MPT_FRAME_HDR *)pAck);
5037
5038         return 0;
5039 }
5040
5041 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5042 /**
5043  *      mpt_config - Generic function to issue config message
5044  *      @ioc - Pointer to an adapter structure
5045  *      @cfg - Pointer to a configuration structure. Struct contains
5046  *              action, page address, direction, physical address
5047  *              and pointer to a configuration page header
5048  *              Page header is updated.
5049  *
5050  *      Returns 0 for success
5051  *      -EPERM if not allowed due to ISR context
5052  *      -EAGAIN if no msg frames currently available
5053  *      -EFAULT for non-successful reply or no reply (timeout)
5054  */
5055 int
5056 mpt_config(MPT_ADAPTER *ioc, CONFIGPARMS *pCfg)
5057 {
5058         Config_t        *pReq;
5059         ConfigExtendedPageHeader_t  *pExtHdr = NULL;
5060         MPT_FRAME_HDR   *mf;
5061         unsigned long    flags;
5062         int              ii, rc;
5063         int              flagsLength;
5064         int              in_isr;
5065
5066         /*      Prevent calling wait_event() (below), if caller happens
5067          *      to be in ISR context, because that is fatal!
5068          */
5069         in_isr = in_interrupt();
5070         if (in_isr) {
5071                 dcprintk((MYIOC_s_WARN_FMT "Config request not allowed in ISR context!\n",
5072                                 ioc->name));
5073                 return -EPERM;
5074         }
5075
5076         /* Get and Populate a free Frame
5077          */
5078         if ((mf = mpt_get_msg_frame(mpt_base_index, ioc)) == NULL) {
5079                 dcprintk((MYIOC_s_WARN_FMT "mpt_config: no msg frames!\n",
5080                                 ioc->name));
5081                 return -EAGAIN;
5082         }
5083         pReq = (Config_t *)mf;
5084         pReq->Action = pCfg->action;
5085         pReq->Reserved = 0;
5086         pReq->ChainOffset = 0;
5087         pReq->Function = MPI_FUNCTION_CONFIG;
5088
5089         /* Assume page type is not extended and clear "reserved" fields. */
5090         pReq->ExtPageLength = 0;
5091         pReq->ExtPageType = 0;
5092         pReq->MsgFlags = 0;
5093
5094         for (ii=0; ii < 8; ii++)
5095                 pReq->Reserved2[ii] = 0;
5096
5097         pReq->Header.PageVersion = pCfg->cfghdr.hdr->PageVersion;
5098         pReq->Header.PageLength = pCfg->cfghdr.hdr->PageLength;
5099         pReq->Header.PageNumber = pCfg->cfghdr.hdr->PageNumber;
5100         pReq->Header.PageType = (pCfg->cfghdr.hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
5101
5102         if ((pCfg->cfghdr.hdr->PageType & MPI_CONFIG_PAGETYPE_MASK) == MPI_CONFIG_PAGETYPE_EXTENDED) {
5103                 pExtHdr = (ConfigExtendedPageHeader_t *)pCfg->cfghdr.ehdr;
5104                 pReq->ExtPageLength = cpu_to_le16(pExtHdr->ExtPageLength);
5105                 pReq->ExtPageType = pExtHdr->ExtPageType;
5106                 pReq->Header.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
5107
5108                 /* Page Length must be treated as a reserved field for the extended header. */
5109                 pReq->Header.PageLength = 0;
5110         }
5111
5112         pReq->PageAddress = cpu_to_le32(pCfg->pageAddr);
5113
5114         /* Add a SGE to the config request.
5115          */
5116         if (pCfg->dir)
5117                 flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE;
5118         else
5119                 flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
5120
5121         if ((pCfg->cfghdr.hdr->PageType & MPI_CONFIG_PAGETYPE_MASK) == MPI_CONFIG_PAGETYPE_EXTENDED) {
5122                 flagsLength |= pExtHdr->ExtPageLength * 4;
5123
5124                 dcprintk((MYIOC_s_INFO_FMT "Sending Config request type %d, page %d and action %d\n",
5125                         ioc->name, pReq->ExtPageType, pReq->Header.PageNumber, pReq->Action));
5126         }
5127         else {
5128                 flagsLength |= pCfg->cfghdr.hdr->PageLength * 4;
5129
5130                 dcprintk((MYIOC_s_INFO_FMT "Sending Config request type %d, page %d and action %d\n",
5131                         ioc->name, pReq->Header.PageType, pReq->Header.PageNumber, pReq->Action));
5132         }
5133
5134         mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, pCfg->physAddr);
5135
5136         /* Append pCfg pointer to end of mf
5137          */
5138         *((void **) (((u8 *) mf) + (ioc->req_sz - sizeof(void *)))) =  (void *) pCfg;
5139
5140         /* Initalize the timer
5141          */
5142         init_timer(&pCfg->timer);
5143         pCfg->timer.data = (unsigned long) ioc;
5144         pCfg->timer.function = mpt_timer_expired;
5145         pCfg->wait_done = 0;
5146
5147         /* Set the timer; ensure 10 second minimum */
5148         if (pCfg->timeout < 10)
5149                 pCfg->timer.expires = jiffies + HZ*10;
5150         else
5151                 pCfg->timer.expires = jiffies + HZ*pCfg->timeout;
5152
5153         /* Add to end of Q, set timer and then issue this command */
5154         spin_lock_irqsave(&ioc->FreeQlock, flags);
5155         list_add_tail(&pCfg->linkage, &ioc->configQ);
5156         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
5157
5158         add_timer(&pCfg->timer);
5159         mpt_put_msg_frame(mpt_base_index, ioc, mf);
5160         wait_event(mpt_waitq, pCfg->wait_done);
5161
5162         /* mf has been freed - do not access */
5163
5164         rc = pCfg->status;
5165
5166         return rc;
5167 }
5168
5169 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5170 /**
5171  *      mpt_toolbox - Generic function to issue toolbox message
5172  *      @ioc - Pointer to an adapter structure
5173  *      @cfg - Pointer to a toolbox structure. Struct contains
5174  *              action, page address, direction, physical address
5175  *              and pointer to a configuration page header
5176  *              Page header is updated.
5177  *
5178  *      Returns 0 for success
5179  *      -EPERM if not allowed due to ISR context
5180  *      -EAGAIN if no msg frames currently available
5181  *      -EFAULT for non-successful reply or no reply (timeout)
5182  */
5183 int
5184 mpt_toolbox(MPT_ADAPTER *ioc, CONFIGPARMS *pCfg)
5185 {
5186         ToolboxIstwiReadWriteRequest_t  *pReq;
5187         MPT_FRAME_HDR   *mf;
5188         struct pci_dev  *pdev;
5189         unsigned long    flags;
5190         int              rc;
5191         u32              flagsLength;
5192         int              in_isr;
5193
5194         /*      Prevent calling wait_event() (below), if caller happens
5195          *      to be in ISR context, because that is fatal!
5196          */
5197         in_isr = in_interrupt();
5198         if (in_isr) {
5199                 dcprintk((MYIOC_s_WARN_FMT "toobox request not allowed in ISR context!\n",
5200                                 ioc->name));
5201                 return -EPERM;
5202         }
5203
5204         /* Get and Populate a free Frame
5205          */
5206         if ((mf = mpt_get_msg_frame(mpt_base_index, ioc)) == NULL) {
5207                 dcprintk((MYIOC_s_WARN_FMT "mpt_toolbox: no msg frames!\n",
5208                                 ioc->name));
5209                 return -EAGAIN;
5210         }
5211         pReq = (ToolboxIstwiReadWriteRequest_t  *)mf;
5212         pReq->Tool = pCfg->action;
5213         pReq->Reserved = 0;
5214         pReq->ChainOffset = 0;
5215         pReq->Function = MPI_FUNCTION_TOOLBOX;
5216         pReq->Reserved1 = 0;
5217         pReq->Reserved2 = 0;
5218         pReq->MsgFlags = 0;
5219         pReq->Flags = pCfg->dir;
5220         pReq->BusNum = 0;
5221         pReq->Reserved3 = 0;
5222         pReq->NumAddressBytes = 0x01;
5223         pReq->Reserved4 = 0;
5224         pReq->DataLength = cpu_to_le16(0x04);
5225         pdev = ioc->pcidev;
5226         if (pdev->devfn & 1)
5227                 pReq->DeviceAddr = 0xB2;
5228         else
5229                 pReq->DeviceAddr = 0xB0;
5230         pReq->Addr1 = 0;
5231         pReq->Addr2 = 0;
5232         pReq->Addr3 = 0;
5233         pReq->Reserved5 = 0;
5234
5235         /* Add a SGE to the config request.
5236          */
5237
5238         flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ | 4;
5239
5240         mpt_add_sge((char *)&pReq->SGL, flagsLength, pCfg->physAddr);
5241
5242         dcprintk((MYIOC_s_INFO_FMT "Sending Toolbox request, Tool=%x\n",
5243                 ioc->name, pReq->Tool));
5244
5245         /* Append pCfg pointer to end of mf
5246          */
5247         *((void **) (((u8 *) mf) + (ioc->req_sz - sizeof(void *)))) =  (void *) pCfg;
5248
5249         /* Initalize the timer
5250          */
5251         init_timer(&pCfg->timer);
5252         pCfg->timer.data = (unsigned long) ioc;
5253         pCfg->timer.function = mpt_timer_expired;
5254         pCfg->wait_done = 0;
5255
5256         /* Set the timer; ensure 10 second minimum */
5257         if (pCfg->timeout < 10)
5258                 pCfg->timer.expires = jiffies + HZ*10;
5259         else
5260                 pCfg->timer.expires = jiffies + HZ*pCfg->timeout;
5261
5262         /* Add to end of Q, set timer and then issue this command */
5263         spin_lock_irqsave(&ioc->FreeQlock, flags);
5264         list_add_tail(&pCfg->linkage, &ioc->configQ);
5265         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
5266
5267         add_timer(&pCfg->timer);
5268         mpt_put_msg_frame(mpt_base_index, ioc, mf);
5269         wait_event(mpt_waitq, pCfg->wait_done);
5270
5271         /* mf has been freed - do not access */
5272
5273         rc = pCfg->status;
5274
5275         return rc;
5276 }
5277
5278 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5279 /*
5280  *      mpt_timer_expired - Call back for timer process.
5281  *      Used only internal config functionality.
5282  *      @data: Pointer to MPT_SCSI_HOST recast as an unsigned long
5283  */
5284 static void
5285 mpt_timer_expired(unsigned long data)
5286 {
5287         MPT_ADAPTER *ioc = (MPT_ADAPTER *) data;
5288
5289         dcprintk((MYIOC_s_WARN_FMT "mpt_timer_expired! \n", ioc->name));
5290
5291         /* Perform a FW reload */
5292         if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0)
5293                 printk(MYIOC_s_WARN_FMT "Firmware Reload FAILED!\n", ioc->name);
5294
5295         /* No more processing.
5296          * Hard reset clean-up will wake up
5297          * process and free all resources.
5298          */
5299         dcprintk((MYIOC_s_WARN_FMT "mpt_timer_expired complete!\n", ioc->name));
5300
5301         return;
5302 }
5303
5304 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5305 /*
5306  *      mpt_ioc_reset - Base cleanup for hard reset
5307  *      @ioc: Pointer to the adapter structure
5308  *      @reset_phase: Indicates pre- or post-reset functionality
5309  *
5310  *      Remark: Free's resources with internally generated commands.
5311  */
5312 static int
5313 mpt_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
5314 {
5315         CONFIGPARMS *pCfg;
5316         unsigned long flags;
5317
5318         dprintk((KERN_WARNING MYNAM
5319                         ": IOC %s_reset routed to MPT base driver!\n",
5320                         reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
5321                         reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
5322
5323         if (reset_phase == MPT_IOC_SETUP_RESET) {
5324                 ;
5325         } else if (reset_phase == MPT_IOC_PRE_RESET) {
5326                 /* If the internal config Q is not empty -
5327                  * delete timer. MF resources will be freed when
5328                  * the FIFO's are primed.
5329                  */
5330                 spin_lock_irqsave(&ioc->FreeQlock, flags);
5331                 list_for_each_entry(pCfg, &ioc->configQ, linkage)
5332                         del_timer(&pCfg->timer);
5333                 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
5334
5335         } else {
5336                 CONFIGPARMS *pNext;
5337
5338                 /* Search the configQ for internal commands.
5339                  * Flush the Q, and wake up all suspended threads.
5340                  */
5341                 spin_lock_irqsave(&ioc->FreeQlock, flags);
5342                 list_for_each_entry_safe(pCfg, pNext, &ioc->configQ, linkage) {
5343                         list_del(&pCfg->linkage);
5344
5345                         pCfg->status = MPT_CONFIG_ERROR;
5346                         pCfg->wait_done = 1;
5347                         wake_up(&mpt_waitq);
5348                 }
5349                 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
5350         }
5351
5352         return 1;               /* currently means nothing really */
5353 }
5354
5355
5356 #ifdef CONFIG_PROC_FS           /* { */
5357 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5358 /*
5359  *      procfs (%MPT_PROCFS_MPTBASEDIR/...) support stuff...
5360  */
5361 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5362 /*
5363  *      procmpt_create - Create %MPT_PROCFS_MPTBASEDIR entries.
5364  *
5365  *      Returns 0 for success, non-zero for failure.
5366  */
5367 static int
5368 procmpt_create(void)
5369 {
5370         struct proc_dir_entry   *ent;
5371
5372         mpt_proc_root_dir = proc_mkdir(MPT_PROCFS_MPTBASEDIR, NULL);
5373         if (mpt_proc_root_dir == NULL)
5374                 return -ENOTDIR;
5375
5376         ent = create_proc_entry("summary", S_IFREG|S_IRUGO, mpt_proc_root_dir);
5377         if (ent)
5378                 ent->read_proc = procmpt_summary_read;
5379
5380         ent = create_proc_entry("version", S_IFREG|S_IRUGO, mpt_proc_root_dir);
5381         if (ent)
5382                 ent->read_proc = procmpt_version_read;
5383
5384         return 0;
5385 }
5386
5387 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5388 /*
5389  *      procmpt_destroy - Tear down %MPT_PROCFS_MPTBASEDIR entries.
5390  *
5391  *      Returns 0 for success, non-zero for failure.
5392  */
5393 static void
5394 procmpt_destroy(void)
5395 {
5396         remove_proc_entry("version", mpt_proc_root_dir);
5397         remove_proc_entry("summary", mpt_proc_root_dir);
5398         remove_proc_entry(MPT_PROCFS_MPTBASEDIR, NULL);
5399 }
5400
5401 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5402 /*
5403  *      procmpt_summary_read - Handle read request from /proc/mpt/summary
5404  *      or from /proc/mpt/iocN/summary.
5405  *      @buf: Pointer to area to write information
5406  *      @start: Pointer to start pointer
5407  *      @offset: Offset to start writing
5408  *      @request:
5409  *      @eof: Pointer to EOF integer
5410  *      @data: Pointer
5411  *
5412  *      Returns number of characters written to process performing the read.
5413  */
5414 static int
5415 procmpt_summary_read(char *buf, char **start, off_t offset, int request, int *eof, void *data)
5416 {
5417         MPT_ADAPTER *ioc;
5418         char *out = buf;
5419         int len;
5420
5421         if (data) {
5422                 int more = 0;
5423
5424                 ioc = data;
5425                 mpt_print_ioc_summary(ioc, out, &more, 0, 1);
5426
5427                 out += more;
5428         } else {
5429                 list_for_each_entry(ioc, &ioc_list, list) {
5430                         int     more = 0;
5431
5432                         mpt_print_ioc_summary(ioc, out, &more, 0, 1);
5433
5434                         out += more;
5435                         if ((out-buf) >= request)
5436                                 break;
5437                 }
5438         }
5439
5440         len = out - buf;
5441
5442         MPT_PROC_READ_RETURN(buf,start,offset,request,eof,len);
5443 }
5444
5445 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5446 /*
5447  *      procmpt_version_read - Handle read request from /proc/mpt/version.
5448  *      @buf: Pointer to area to write information
5449  *      @start: Pointer to start pointer
5450  *      @offset: Offset to start writing
5451  *      @request:
5452  *      @eof: Pointer to EOF integer
5453  *      @data: Pointer
5454  *
5455  *      Returns number of characters written to process performing the read.
5456  */
5457 static int
5458 procmpt_version_read(char *buf, char **start, off_t offset, int request, int *eof, void *data)
5459 {
5460         int      ii;
5461         int      scsi, fc, sas, lan, ctl, targ, dmp;
5462         char    *drvname;
5463         int      len;
5464
5465         len = sprintf(buf, "%s-%s\n", "mptlinux", MPT_LINUX_VERSION_COMMON);
5466         len += sprintf(buf+len, "  Fusion MPT base driver\n");
5467
5468         scsi = fc = sas = lan = ctl = targ = dmp = 0;
5469         for (ii=MPT_MAX_PROTOCOL_DRIVERS-1; ii; ii--) {
5470                 drvname = NULL;
5471                 if (MptCallbacks[ii]) {
5472                         switch (MptDriverClass[ii]) {
5473                         case MPTSPI_DRIVER:
5474                                 if (!scsi++) drvname = "SPI host";
5475                                 break;
5476                         case MPTFC_DRIVER:
5477                                 if (!fc++) drvname = "FC host";
5478                                 break;
5479                         case MPTSAS_DRIVER:
5480                                 if (!sas++) drvname = "SAS host";
5481                                 break;
5482                         case MPTLAN_DRIVER:
5483                                 if (!lan++) drvname = "LAN";
5484                                 break;
5485                         case MPTSTM_DRIVER:
5486                                 if (!targ++) drvname = "SCSI target";
5487                                 break;
5488                         case MPTCTL_DRIVER:
5489                                 if (!ctl++) drvname = "ioctl";
5490                                 break;
5491                         }
5492
5493                         if (drvname)
5494                                 len += sprintf(buf+len, "  Fusion MPT %s driver\n", drvname);
5495                 }
5496         }
5497
5498         MPT_PROC_READ_RETURN(buf,start,offset,request,eof,len);
5499 }
5500
5501 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5502 /*
5503  *      procmpt_iocinfo_read - Handle read request from /proc/mpt/iocN/info.
5504  *      @buf: Pointer to area to write information
5505  *      @start: Pointer to start pointer
5506  *      @offset: Offset to start writing
5507  *      @request:
5508  *      @eof: Pointer to EOF integer
5509  *      @data: Pointer
5510  *
5511  *      Returns number of characters written to process performing the read.
5512  */
5513 static int
5514 procmpt_iocinfo_read(char *buf, char **start, off_t offset, int request, int *eof, void *data)
5515 {
5516         MPT_ADAPTER     *ioc = data;
5517         int              len;
5518         char             expVer[32];
5519         int              sz;
5520         int              p;
5521
5522         mpt_get_fw_exp_ver(expVer, ioc);
5523
5524         len = sprintf(buf, "%s:", ioc->name);
5525         if (ioc->facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT)
5526                 len += sprintf(buf+len, "  (f/w download boot flag set)");
5527 //      if (ioc->facts.IOCExceptions & MPI_IOCFACTS_EXCEPT_CONFIG_CHECKSUM_FAIL)
5528 //              len += sprintf(buf+len, "  CONFIG_CHECKSUM_FAIL!");
5529
5530         len += sprintf(buf+len, "\n  ProductID = 0x%04x (%s)\n",
5531                         ioc->facts.ProductID,
5532                         ioc->prod_name);
5533         len += sprintf(buf+len, "  FWVersion = 0x%08x%s", ioc->facts.FWVersion.Word, expVer);
5534         if (ioc->facts.FWImageSize)
5535                 len += sprintf(buf+len, " (fw_size=%d)", ioc->facts.FWImageSize);
5536         len += sprintf(buf+len, "\n  MsgVersion = 0x%04x\n", ioc->facts.MsgVersion);
5537         len += sprintf(buf+len, "  FirstWhoInit = 0x%02x\n", ioc->FirstWhoInit);
5538         len += sprintf(buf+len, "  EventState = 0x%02x\n", ioc->facts.EventState);
5539
5540         len += sprintf(buf+len, "  CurrentHostMfaHighAddr = 0x%08x\n",
5541                         ioc->facts.CurrentHostMfaHighAddr);
5542         len += sprintf(buf+len, "  CurrentSenseBufferHighAddr = 0x%08x\n",
5543                         ioc->facts.CurrentSenseBufferHighAddr);
5544
5545         len += sprintf(buf+len, "  MaxChainDepth = 0x%02x frames\n", ioc->facts.MaxChainDepth);
5546         len += sprintf(buf+len, "  MinBlockSize = 0x%02x bytes\n", 4*ioc->facts.BlockSize);
5547
5548         len += sprintf(buf+len, "  RequestFrames @ 0x%p (Dma @ 0x%p)\n",
5549                                         (void *)ioc->req_frames, (void *)(ulong)ioc->req_frames_dma);
5550         /*
5551          *  Rounding UP to nearest 4-kB boundary here...
5552          */
5553         sz = (ioc->req_sz * ioc->req_depth) + 128;
5554         sz = ((sz + 0x1000UL - 1UL) / 0x1000) * 0x1000;
5555         len += sprintf(buf+len, "    {CurReqSz=%d} x {CurReqDepth=%d} = %d bytes ^= 0x%x\n",
5556                                         ioc->req_sz, ioc->req_depth, ioc->req_sz*ioc->req_depth, sz);
5557         len += sprintf(buf+len, "    {MaxReqSz=%d}   {MaxReqDepth=%d}\n",
5558                                         4*ioc->facts.RequestFrameSize,
5559                                         ioc->facts.GlobalCredits);
5560
5561         len += sprintf(buf+len, "  Frames   @ 0x%p (Dma @ 0x%p)\n",
5562                                         (void *)ioc->alloc, (void *)(ulong)ioc->alloc_dma);
5563         sz = (ioc->reply_sz * ioc->reply_depth) + 128;
5564         len += sprintf(buf+len, "    {CurRepSz=%d} x {CurRepDepth=%d} = %d bytes ^= 0x%x\n",
5565                                         ioc->reply_sz, ioc->reply_depth, ioc->reply_sz*ioc->reply_depth, sz);
5566         len += sprintf(buf+len, "    {MaxRepSz=%d}   {MaxRepDepth=%d}\n",
5567                                         ioc->facts.CurReplyFrameSize,
5568                                         ioc->facts.ReplyQueueDepth);
5569
5570         len += sprintf(buf+len, "  MaxDevices = %d\n",
5571                         (ioc->facts.MaxDevices==0) ? 255 : ioc->facts.MaxDevices);
5572         len += sprintf(buf+len, "  MaxBuses = %d\n", ioc->facts.MaxBuses);
5573
5574         /* per-port info */
5575         for (p=0; p < ioc->facts.NumberOfPorts; p++) {
5576                 len += sprintf(buf+len, "  PortNumber = %d (of %d)\n",
5577                                 p+1,
5578                                 ioc->facts.NumberOfPorts);
5579                 if (ioc->bus_type == FC) {
5580                         if (ioc->pfacts[p].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_LAN) {
5581                                 u8 *a = (u8*)&ioc->lan_cnfg_page1.HardwareAddressLow;
5582                                 len += sprintf(buf+len, "    LanAddr = %02X:%02X:%02X:%02X:%02X:%02X\n",
5583                                                 a[5], a[4], a[3], a[2], a[1], a[0]);
5584                         }
5585                         len += sprintf(buf+len, "    WWN = %08X%08X:%08X%08X\n",
5586                                         ioc->fc_port_page0[p].WWNN.High,
5587                                         ioc->fc_port_page0[p].WWNN.Low,
5588                                         ioc->fc_port_page0[p].WWPN.High,
5589                                         ioc->fc_port_page0[p].WWPN.Low);
5590                 }
5591         }
5592
5593         MPT_PROC_READ_RETURN(buf,start,offset,request,eof,len);
5594 }
5595
5596 #endif          /* CONFIG_PROC_FS } */
5597
5598 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5599 static void
5600 mpt_get_fw_exp_ver(char *buf, MPT_ADAPTER *ioc)
5601 {
5602         buf[0] ='\0';
5603         if ((ioc->facts.FWVersion.Word >> 24) == 0x0E) {
5604                 sprintf(buf, " (Exp %02d%02d)",
5605                         (ioc->facts.FWVersion.Word >> 16) & 0x00FF,     /* Month */
5606                         (ioc->facts.FWVersion.Word >> 8) & 0x1F);       /* Day */
5607
5608                 /* insider hack! */
5609                 if ((ioc->facts.FWVersion.Word >> 8) & 0x80)
5610                         strcat(buf, " [MDBG]");
5611         }
5612 }
5613
5614 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5615 /**
5616  *      mpt_print_ioc_summary - Write ASCII summary of IOC to a buffer.
5617  *      @ioc: Pointer to MPT_ADAPTER structure
5618  *      @buffer: Pointer to buffer where IOC summary info should be written
5619  *      @size: Pointer to number of bytes we wrote (set by this routine)
5620  *      @len: Offset at which to start writing in buffer
5621  *      @showlan: Display LAN stuff?
5622  *
5623  *      This routine writes (english readable) ASCII text, which represents
5624  *      a summary of IOC information, to a buffer.
5625  */
5626 void
5627 mpt_print_ioc_summary(MPT_ADAPTER *ioc, char *buffer, int *size, int len, int showlan)
5628 {
5629         char expVer[32];
5630         int y;
5631
5632         mpt_get_fw_exp_ver(expVer, ioc);
5633
5634         /*
5635          *  Shorter summary of attached ioc's...
5636          */
5637         y = sprintf(buffer+len, "%s: %s, %s%08xh%s, Ports=%d, MaxQ=%d",
5638                         ioc->name,
5639                         ioc->prod_name,
5640                         MPT_FW_REV_MAGIC_ID_STRING,     /* "FwRev=" or somesuch */
5641                         ioc->facts.FWVersion.Word,
5642                         expVer,
5643                         ioc->facts.NumberOfPorts,
5644                         ioc->req_depth);
5645
5646         if (showlan && (ioc->pfacts[0].ProtocolFlags & MPI_PORTFACTS_PROTOCOL_LAN)) {
5647                 u8 *a = (u8*)&ioc->lan_cnfg_page1.HardwareAddressLow;
5648                 y += sprintf(buffer+len+y, ", LanAddr=%02X:%02X:%02X:%02X:%02X:%02X",
5649                         a[5], a[4], a[3], a[2], a[1], a[0]);
5650         }
5651
5652 #ifndef __sparc__
5653         y += sprintf(buffer+len+y, ", IRQ=%d", ioc->pci_irq);
5654 #else
5655         y += sprintf(buffer+len+y, ", IRQ=%s", __irq_itoa(ioc->pci_irq));
5656 #endif
5657
5658         if (!ioc->active)
5659                 y += sprintf(buffer+len+y, " (disabled)");
5660
5661         y += sprintf(buffer+len+y, "\n");
5662
5663         *size = y;
5664 }
5665
5666 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5667 /*
5668  *      Reset Handling
5669  */
5670 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5671 /**
5672  *      mpt_HardResetHandler - Generic reset handler, issue SCSI Task
5673  *      Management call based on input arg values.  If TaskMgmt fails,
5674  *      return associated SCSI request.
5675  *      @ioc: Pointer to MPT_ADAPTER structure
5676  *      @sleepFlag: Indicates if sleep or schedule must be called.
5677  *
5678  *      Remark: _HardResetHandler can be invoked from an interrupt thread (timer)
5679  *      or a non-interrupt thread.  In the former, must not call schedule().
5680  *
5681  *      Remark: A return of -1 is a FATAL error case, as it means a
5682  *      FW reload/initialization failed.
5683  *
5684  *      Returns 0 for SUCCESS or -1 if FAILED.
5685  */
5686 int
5687 mpt_HardResetHandler(MPT_ADAPTER *ioc, int sleepFlag)
5688 {
5689         int              rc;
5690         unsigned long    flags;
5691
5692         dtmprintk((MYIOC_s_INFO_FMT "HardResetHandler Entered!\n", ioc->name));
5693 #ifdef MFCNT
5694         printk(MYIOC_s_INFO_FMT "HardResetHandler Entered!\n", ioc->name);
5695         printk("MF count 0x%x !\n", ioc->mfcnt);
5696 #endif
5697
5698         /* Reset the adapter. Prevent more than 1 call to
5699          * mpt_do_ioc_recovery at any instant in time.
5700          */
5701         spin_lock_irqsave(&ioc->diagLock, flags);
5702         if ((ioc->diagPending) || (ioc->alt_ioc && ioc->alt_ioc->diagPending)){
5703                 spin_unlock_irqrestore(&ioc->diagLock, flags);
5704                 return 0;
5705         } else {
5706                 ioc->diagPending = 1;
5707         }
5708         spin_unlock_irqrestore(&ioc->diagLock, flags);
5709
5710         /* FIXME: If do_ioc_recovery fails, repeat....
5711          */
5712
5713         /* The SCSI driver needs to adjust timeouts on all current
5714          * commands prior to the diagnostic reset being issued.
5715          * Prevents timeouts occuring during a diagnostic reset...very bad.
5716          * For all other protocol drivers, this is a no-op.
5717          */
5718         {
5719                 int      ii;
5720                 int      r = 0;
5721
5722                 for (ii=MPT_MAX_PROTOCOL_DRIVERS-1; ii; ii--) {
5723                         if (MptResetHandlers[ii]) {
5724                                 dtmprintk((MYIOC_s_INFO_FMT "Calling IOC reset_setup handler #%d\n",
5725                                                 ioc->name, ii));
5726                                 r += (*(MptResetHandlers[ii]))(ioc, MPT_IOC_SETUP_RESET);
5727                                 if (ioc->alt_ioc) {
5728                                         dtmprintk((MYIOC_s_INFO_FMT "Calling alt-%s setup reset handler #%d\n",
5729                                                         ioc->name, ioc->alt_ioc->name, ii));
5730                                         r += (*(MptResetHandlers[ii]))(ioc->alt_ioc, MPT_IOC_SETUP_RESET);
5731                                 }
5732                         }
5733                 }
5734         }
5735
5736         if ((rc = mpt_do_ioc_recovery(ioc, MPT_HOSTEVENT_IOC_RECOVER, sleepFlag)) != 0) {
5737                 printk(KERN_WARNING MYNAM ": WARNING - (%d) Cannot recover %s\n",
5738                         rc, ioc->name);
5739         }
5740         ioc->reload_fw = 0;
5741         if (ioc->alt_ioc)
5742                 ioc->alt_ioc->reload_fw = 0;
5743
5744         spin_lock_irqsave(&ioc->diagLock, flags);
5745         ioc->diagPending = 0;
5746         if (ioc->alt_ioc)
5747                 ioc->alt_ioc->diagPending = 0;
5748         spin_unlock_irqrestore(&ioc->diagLock, flags);
5749
5750         dtmprintk((MYIOC_s_INFO_FMT "HardResetHandler rc = %d!\n", ioc->name, rc));
5751
5752         return rc;
5753 }
5754
5755 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5756 static void
5757 EventDescriptionStr(u8 event, u32 evData0, char *evStr)
5758 {
5759         char *ds;
5760
5761         switch(event) {
5762         case MPI_EVENT_NONE:
5763                 ds = "None";
5764                 break;
5765         case MPI_EVENT_LOG_DATA:
5766                 ds = "Log Data";
5767                 break;
5768         case MPI_EVENT_STATE_CHANGE:
5769                 ds = "State Change";
5770                 break;
5771         case MPI_EVENT_UNIT_ATTENTION:
5772                 ds = "Unit Attention";
5773                 break;
5774         case MPI_EVENT_IOC_BUS_RESET:
5775                 ds = "IOC Bus Reset";
5776                 break;
5777         case MPI_EVENT_EXT_BUS_RESET:
5778                 ds = "External Bus Reset";
5779                 break;
5780         case MPI_EVENT_RESCAN:
5781                 ds = "Bus Rescan Event";
5782                 /* Ok, do we need to do anything here? As far as
5783                    I can tell, this is when a new device gets added
5784                    to the loop. */
5785                 break;
5786         case MPI_EVENT_LINK_STATUS_CHANGE:
5787                 if (evData0 == MPI_EVENT_LINK_STATUS_FAILURE)
5788                         ds = "Link Status(FAILURE) Change";
5789                 else
5790                         ds = "Link Status(ACTIVE) Change";
5791                 break;
5792         case MPI_EVENT_LOOP_STATE_CHANGE:
5793                 if (evData0 == MPI_EVENT_LOOP_STATE_CHANGE_LIP)
5794                         ds = "Loop State(LIP) Change";
5795                 else if (evData0 == MPI_EVENT_LOOP_STATE_CHANGE_LPE)
5796                         ds = "Loop State(LPE) Change";                  /* ??? */
5797                 else
5798                         ds = "Loop State(LPB) Change";                  /* ??? */
5799                 break;
5800         case MPI_EVENT_LOGOUT:
5801                 ds = "Logout";
5802                 break;
5803         case MPI_EVENT_EVENT_CHANGE:
5804                 if (evData0)
5805                         ds = "Events(ON) Change";
5806                 else
5807                         ds = "Events(OFF) Change";
5808                 break;
5809         case MPI_EVENT_INTEGRATED_RAID:
5810         {
5811                 u8 ReasonCode = (u8)(evData0 >> 16);
5812                 switch (ReasonCode) {
5813                 case MPI_EVENT_RAID_RC_VOLUME_CREATED :
5814                         ds = "Integrated Raid: Volume Created";
5815                         break;
5816                 case MPI_EVENT_RAID_RC_VOLUME_DELETED :
5817                         ds = "Integrated Raid: Volume Deleted";
5818                         break;
5819                 case MPI_EVENT_RAID_RC_VOLUME_SETTINGS_CHANGED :
5820                         ds = "Integrated Raid: Volume Settings Changed";
5821                         break;
5822                 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED :
5823                         ds = "Integrated Raid: Volume Status Changed";
5824                         break;
5825                 case MPI_EVENT_RAID_RC_VOLUME_PHYSDISK_CHANGED :
5826                         ds = "Integrated Raid: Volume Physdisk Changed";
5827                         break;
5828                 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED :
5829                         ds = "Integrated Raid: Physdisk Created";
5830                         break;
5831                 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED :
5832                         ds = "Integrated Raid: Physdisk Deleted";
5833                         break;
5834                 case MPI_EVENT_RAID_RC_PHYSDISK_SETTINGS_CHANGED :
5835                         ds = "Integrated Raid: Physdisk Settings Changed";
5836                         break;
5837                 case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED :
5838                         ds = "Integrated Raid: Physdisk Status Changed";
5839                         break;
5840                 case MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED :
5841                         ds = "Integrated Raid: Domain Validation Needed";
5842                         break;
5843                 case MPI_EVENT_RAID_RC_SMART_DATA :
5844                         ds = "Integrated Raid; Smart Data";
5845                         break;
5846                 case MPI_EVENT_RAID_RC_REPLACE_ACTION_STARTED :
5847                         ds = "Integrated Raid: Replace Action Started";
5848                         break;
5849                 default:
5850                         ds = "Integrated Raid";
5851                 break;
5852                 }
5853                 break;
5854         }
5855         case MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE:
5856                 ds = "SCSI Device Status Change";
5857                 break;
5858         case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
5859         {
5860                 u8 ReasonCode = (u8)(evData0 >> 16);
5861                 switch (ReasonCode) {
5862                 case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
5863                         ds = "SAS Device Status Change: Added";
5864                         break;
5865                 case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
5866                         ds = "SAS Device Status Change: Deleted";
5867                         break;
5868                 case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5869                         ds = "SAS Device Status Change: SMART Data";
5870                         break;
5871                 case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
5872                         ds = "SAS Device Status Change: No Persistancy Added";
5873                         break;
5874                 default:
5875                         ds = "SAS Device Status Change: Unknown";
5876                 break;
5877                 }
5878                 break;
5879         }
5880         case MPI_EVENT_ON_BUS_TIMER_EXPIRED:
5881                 ds = "Bus Timer Expired";
5882                 break;
5883         case MPI_EVENT_QUEUE_FULL:
5884                 ds = "Queue Full";
5885                 break;
5886         case MPI_EVENT_SAS_SES:
5887                 ds = "SAS SES Event";
5888                 break;
5889         case MPI_EVENT_PERSISTENT_TABLE_FULL:
5890                 ds = "Persistent Table Full";
5891                 break;
5892         case MPI_EVENT_SAS_PHY_LINK_STATUS:
5893                 ds = "SAS PHY Link Status";
5894                 break;
5895         case MPI_EVENT_SAS_DISCOVERY_ERROR:
5896                 ds = "SAS Discovery Error";
5897                 break;
5898
5899         /*
5900          *  MPT base "custom" events may be added here...
5901          */
5902         default:
5903                 ds = "Unknown";
5904                 break;
5905         }
5906         strcpy(evStr,ds);
5907 }
5908
5909 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5910 /*
5911  *      ProcessEventNotification - Route a received EventNotificationReply to
5912  *      all currently regeistered event handlers.
5913  *      @ioc: Pointer to MPT_ADAPTER structure
5914  *      @pEventReply: Pointer to EventNotification reply frame
5915  *      @evHandlers: Pointer to integer, number of event handlers
5916  *
5917  *      Returns sum of event handlers return values.
5918  */
5919 static int
5920 ProcessEventNotification(MPT_ADAPTER *ioc, EventNotificationReply_t *pEventReply, int *evHandlers)
5921 {
5922         u16 evDataLen;
5923         u32 evData0 = 0;
5924 //      u32 evCtx;
5925         int ii;
5926         int r = 0;
5927         int handlers = 0;
5928         char evStr[100];
5929         u8 event;
5930
5931         /*
5932          *  Do platform normalization of values
5933          */
5934         event = le32_to_cpu(pEventReply->Event) & 0xFF;
5935 //      evCtx = le32_to_cpu(pEventReply->EventContext);
5936         evDataLen = le16_to_cpu(pEventReply->EventDataLength);
5937         if (evDataLen) {
5938                 evData0 = le32_to_cpu(pEventReply->Data[0]);
5939         }
5940
5941         EventDescriptionStr(event, evData0, evStr);
5942         devtprintk((MYIOC_s_INFO_FMT "MPT event (%s=%02Xh) detected!\n",
5943                         ioc->name,
5944                         evStr,
5945                         event));
5946
5947 #if defined(MPT_DEBUG) || defined(MPT_DEBUG_EVENTS)
5948         printk(KERN_INFO MYNAM ": Event data:\n" KERN_INFO);
5949         for (ii = 0; ii < evDataLen; ii++)
5950                 printk(" %08x", le32_to_cpu(pEventReply->Data[ii]));
5951         printk("\n");
5952 #endif
5953
5954         /*
5955          *  Do general / base driver event processing
5956          */
5957         switch(event) {
5958         case MPI_EVENT_EVENT_CHANGE:            /* 0A */
5959                 if (evDataLen) {
5960                         u8 evState = evData0 & 0xFF;
5961
5962                         /* CHECKME! What if evState unexpectedly says OFF (0)? */
5963
5964                         /* Update EventState field in cached IocFacts */
5965                         if (ioc->facts.Function) {
5966                                 ioc->facts.EventState = evState;
5967                         }
5968                 }
5969                 break;
5970         default:
5971                 break;
5972         }
5973
5974         /*
5975          * Should this event be logged? Events are written sequentially.
5976          * When buffer is full, start again at the top.
5977          */
5978         if (ioc->events && (ioc->eventTypes & ( 1 << event))) {
5979                 int idx;
5980
5981                 idx = ioc->eventContext % ioc->eventLogSize;
5982
5983                 ioc->events[idx].event = event;
5984                 ioc->events[idx].eventContext = ioc->eventContext;
5985
5986                 for (ii = 0; ii < 2; ii++) {
5987                         if (ii < evDataLen)
5988                                 ioc->events[idx].data[ii] = le32_to_cpu(pEventReply->Data[ii]);
5989                         else
5990                                 ioc->events[idx].data[ii] =  0;
5991                 }
5992
5993                 ioc->eventContext++;
5994         }
5995
5996
5997         /*
5998          *  Call each currently registered protocol event handler.
5999          */
6000         for (ii=MPT_MAX_PROTOCOL_DRIVERS-1; ii; ii--) {
6001                 if (MptEvHandlers[ii]) {
6002                         devtprintk((MYIOC_s_INFO_FMT "Routing Event to event handler #%d\n",
6003                                         ioc->name, ii));
6004                         r += (*(MptEvHandlers[ii]))(ioc, pEventReply);
6005                         handlers++;
6006                 }
6007         }
6008         /* FIXME?  Examine results here? */
6009
6010         /*
6011          *  If needed, send (a single) EventAck.
6012          */
6013         if (pEventReply->AckRequired == MPI_EVENT_NOTIFICATION_ACK_REQUIRED) {
6014                 devtprintk((MYIOC_s_WARN_FMT
6015                         "EventAck required\n",ioc->name));
6016                 if ((ii = SendEventAck(ioc, pEventReply)) != 0) {
6017                         devtprintk((MYIOC_s_WARN_FMT "SendEventAck returned %d\n",
6018                                         ioc->name, ii));
6019                 }
6020         }
6021
6022         *evHandlers = handlers;
6023         return r;
6024 }
6025
6026 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
6027 /*
6028  *      mpt_fc_log_info - Log information returned from Fibre Channel IOC.
6029  *      @ioc: Pointer to MPT_ADAPTER structure
6030  *      @log_info: U32 LogInfo reply word from the IOC
6031  *
6032  *      Refer to lsi/fc_log.h.
6033  */
6034 static void
6035 mpt_fc_log_info(MPT_ADAPTER *ioc, u32 log_info)
6036 {
6037         static char *subcl_str[8] = {
6038                 "FCP Initiator", "FCP Target", "LAN", "MPI Message Layer",
6039                 "FC Link", "Context Manager", "Invalid Field Offset", "State Change Info"
6040         };
6041         u8 subcl = (log_info >> 24) & 0x7;
6042
6043         printk(MYIOC_s_INFO_FMT "LogInfo(0x%08x): SubCl={%s}\n",
6044                         ioc->name, log_info, subcl_str[subcl]);
6045 }
6046
6047 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
6048 /*
6049  *      mpt_sp_log_info - Log information returned from SCSI Parallel IOC.
6050  *      @ioc: Pointer to MPT_ADAPTER structure
6051  *      @mr: Pointer to MPT reply frame
6052  *      @log_info: U32 LogInfo word from the IOC
6053  *
6054  *      Refer to lsi/sp_log.h.
6055  */
6056 static void
6057 mpt_sp_log_info(MPT_ADAPTER *ioc, u32 log_info)
6058 {
6059         u32 info = log_info & 0x00FF0000;
6060         char *desc = "unknown";
6061
6062         switch (info) {
6063         case 0x00010000:
6064                 desc = "bug! MID not found";
6065                 if (ioc->reload_fw == 0)
6066                         ioc->reload_fw++;
6067                 break;
6068
6069         case 0x00020000:
6070                 desc = "Parity Error";
6071                 break;
6072
6073         case 0x00030000:
6074                 desc = "ASYNC Outbound Overrun";
6075                 break;
6076
6077         case 0x00040000:
6078                 desc = "SYNC Offset Error";
6079                 break;
6080
6081         case 0x00050000:
6082                 desc = "BM Change";
6083                 break;
6084
6085         case 0x00060000:
6086                 desc = "Msg In Overflow";
6087                 break;
6088
6089         case 0x00070000:
6090                 desc = "DMA Error";
6091                 break;
6092
6093         case 0x00080000:
6094                 desc = "Outbound DMA Overrun";
6095                 break;
6096
6097         case 0x00090000:
6098                 desc = "Task Management";
6099                 break;
6100
6101         case 0x000A0000:
6102                 desc = "Device Problem";
6103                 break;
6104
6105         case 0x000B0000:
6106                 desc = "Invalid Phase Change";
6107                 break;
6108
6109         case 0x000C0000:
6110                 desc = "Untagged Table Size";
6111                 break;
6112
6113         }
6114
6115         printk(MYIOC_s_INFO_FMT "LogInfo(0x%08x): F/W: %s\n", ioc->name, log_info, desc);
6116 }
6117
6118 /* strings for sas loginfo */
6119         static char *originator_str[] = {
6120                 "IOP",                                          /* 00h */
6121                 "PL",                                           /* 01h */
6122                 "IR"                                            /* 02h */
6123         };
6124         static char *iop_code_str[] = {
6125                 NULL,                                           /* 00h */
6126                 "Invalid SAS Address",                          /* 01h */
6127                 NULL,                                           /* 02h */
6128                 "Invalid Page",                                 /* 03h */
6129                 NULL,                                           /* 04h */
6130                 "Task Terminated"                               /* 05h */
6131         };
6132         static char *pl_code_str[] = {
6133                 NULL,                                           /* 00h */
6134                 "Open Failure",                                 /* 01h */
6135                 "Invalid Scatter Gather List",                  /* 02h */
6136                 "Wrong Relative Offset or Frame Length",        /* 03h */
6137                 "Frame Transfer Error",                         /* 04h */
6138                 "Transmit Frame Connected Low",                 /* 05h */
6139                 "SATA Non-NCQ RW Error Bit Set",                /* 06h */
6140                 "SATA Read Log Receive Data Error",             /* 07h */
6141                 "SATA NCQ Fail All Commands After Error",       /* 08h */
6142                 "SATA Error in Receive Set Device Bit FIS",     /* 09h */
6143                 "Receive Frame Invalid Message",                /* 0Ah */
6144                 "Receive Context Message Valid Error",          /* 0Bh */
6145                 "Receive Frame Current Frame Error",            /* 0Ch */
6146                 "SATA Link Down",                               /* 0Dh */
6147                 "Discovery SATA Init W IOS",                    /* 0Eh */
6148                 "Config Invalid Page",                          /* 0Fh */
6149                 "Discovery SATA Init Timeout",                  /* 10h */
6150                 "Reset",                                        /* 11h */
6151                 "Abort",                                        /* 12h */
6152                 "IO Not Yet Executed",                          /* 13h */
6153                 "IO Executed",                                  /* 14h */
6154                 NULL,                                           /* 15h */
6155                 NULL,                                           /* 16h */
6156                 NULL,                                           /* 17h */
6157                 NULL,                                           /* 18h */
6158                 NULL,                                           /* 19h */
6159                 NULL,                                           /* 1Ah */
6160                 NULL,                                           /* 1Bh */
6161                 NULL,                                           /* 1Ch */
6162                 NULL,                                           /* 1Dh */
6163                 NULL,                                           /* 1Eh */
6164                 NULL,                                           /* 1Fh */
6165                 "Enclosure Management"                          /* 20h */
6166         };
6167
6168 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
6169 /*
6170  *      mpt_sas_log_info - Log information returned from SAS IOC.
6171  *      @ioc: Pointer to MPT_ADAPTER structure
6172  *      @log_info: U32 LogInfo reply word from the IOC
6173  *
6174  *      Refer to lsi/mpi_log_sas.h.
6175  */
6176 static void
6177 mpt_sas_log_info(MPT_ADAPTER *ioc, u32 log_info)
6178 {
6179 union loginfo_type {
6180         u32     loginfo;
6181         struct {
6182                 u32     subcode:16;
6183                 u32     code:8;
6184                 u32     originator:4;
6185                 u32     bus_type:4;
6186         }dw;
6187 };
6188         union loginfo_type sas_loginfo;
6189         char *code_desc = NULL;
6190
6191         sas_loginfo.loginfo = log_info;
6192         if ((sas_loginfo.dw.bus_type != 3 /*SAS*/) &&
6193             (sas_loginfo.dw.originator < sizeof(originator_str)/sizeof(char*)))
6194                 return;
6195         if ((sas_loginfo.dw.originator == 0 /*IOP*/) &&
6196             (sas_loginfo.dw.code < sizeof(iop_code_str)/sizeof(char*))) {
6197                 code_desc = iop_code_str[sas_loginfo.dw.code];
6198         }else if ((sas_loginfo.dw.originator == 1 /*PL*/) &&
6199             (sas_loginfo.dw.code < sizeof(pl_code_str)/sizeof(char*) )) {
6200                 code_desc = pl_code_str[sas_loginfo.dw.code];
6201         }
6202
6203         if (code_desc != NULL)
6204                 printk(MYIOC_s_INFO_FMT
6205                         "LogInfo(0x%08x): Originator={%s}, Code={%s},"
6206                         " SubCode(0x%04x)\n",
6207                         ioc->name,
6208                         log_info,
6209                         originator_str[sas_loginfo.dw.originator],
6210                         code_desc,
6211                         sas_loginfo.dw.subcode);
6212         else
6213                 printk(MYIOC_s_INFO_FMT
6214                         "LogInfo(0x%08x): Originator={%s}, Code=(0x%02x),"
6215                         " SubCode(0x%04x)\n",
6216                         ioc->name,
6217                         log_info,
6218                         originator_str[sas_loginfo.dw.originator],
6219                         sas_loginfo.dw.code,
6220                         sas_loginfo.dw.subcode);
6221 }
6222
6223 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
6224 /*
6225  *      mpt_sp_ioc_info - IOC information returned from SCSI Parallel IOC.
6226  *      @ioc: Pointer to MPT_ADAPTER structure
6227  *      @ioc_status: U32 IOCStatus word from IOC
6228  *      @mf: Pointer to MPT request frame
6229  *
6230  *      Refer to lsi/mpi.h.
6231  */
6232 static void
6233 mpt_sp_ioc_info(MPT_ADAPTER *ioc, u32 ioc_status, MPT_FRAME_HDR *mf)
6234 {
6235         u32 status = ioc_status & MPI_IOCSTATUS_MASK;
6236         char *desc = "";
6237
6238         switch (status) {
6239         case MPI_IOCSTATUS_INVALID_FUNCTION: /* 0x0001 */
6240                 desc = "Invalid Function";
6241                 break;
6242
6243         case MPI_IOCSTATUS_BUSY: /* 0x0002 */
6244                 desc = "Busy";
6245                 break;
6246
6247         case MPI_IOCSTATUS_INVALID_SGL: /* 0x0003 */
6248                 desc = "Invalid SGL";
6249                 break;
6250
6251         case MPI_IOCSTATUS_INTERNAL_ERROR: /* 0x0004 */
6252                 desc = "Internal Error";
6253                 break;
6254
6255         case MPI_IOCSTATUS_RESERVED: /* 0x0005 */
6256                 desc = "Reserved";
6257                 break;
6258
6259         case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES: /* 0x0006 */
6260                 desc = "Insufficient Resources";
6261                 break;
6262
6263         case MPI_IOCSTATUS_INVALID_FIELD: /* 0x0007 */
6264                 desc = "Invalid Field";
6265                 break;
6266
6267         case MPI_IOCSTATUS_INVALID_STATE: /* 0x0008 */
6268                 desc = "Invalid State";
6269                 break;
6270
6271         case MPI_IOCSTATUS_CONFIG_INVALID_ACTION: /* 0x0020 */
6272         case MPI_IOCSTATUS_CONFIG_INVALID_TYPE:   /* 0x0021 */
6273         case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:   /* 0x0022 */
6274         case MPI_IOCSTATUS_CONFIG_INVALID_DATA:   /* 0x0023 */
6275         case MPI_IOCSTATUS_CONFIG_NO_DEFAULTS:    /* 0x0024 */
6276         case MPI_IOCSTATUS_CONFIG_CANT_COMMIT:    /* 0x0025 */
6277                 /* No message for Config IOCStatus values */
6278                 break;
6279
6280         case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR: /* 0x0040 */
6281                 /* No message for recovered error
6282                 desc = "SCSI Recovered Error";
6283                 */
6284                 break;
6285
6286         case MPI_IOCSTATUS_SCSI_INVALID_BUS: /* 0x0041 */
6287                 desc = "SCSI Invalid Bus";
6288                 break;
6289
6290         case MPI_IOCSTATUS_SCSI_INVALID_TARGETID: /* 0x0042 */
6291                 desc = "SCSI Invalid TargetID";
6292                 break;
6293
6294         case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE: /* 0x0043 */
6295           {
6296                 SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
6297                 U8 cdb = pScsiReq->CDB[0];
6298                 if (cdb != 0x12) { /* Inquiry is issued for device scanning */
6299                         desc = "SCSI Device Not There";
6300                 }
6301                 break;
6302           }
6303
6304         case MPI_IOCSTATUS_SCSI_DATA_OVERRUN: /* 0x0044 */
6305                 desc = "SCSI Data Overrun";
6306                 break;
6307
6308         case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN: /* 0x0045 */
6309                 /* This error is checked in scsi_io_done(). Skip.
6310                 desc = "SCSI Data Underrun";
6311                 */
6312                 break;
6313
6314         case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR: /* 0x0046 */
6315                 desc = "SCSI I/O Data Error";
6316                 break;
6317
6318         case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR: /* 0x0047 */
6319                 desc = "SCSI Protocol Error";
6320                 break;
6321
6322         case MPI_IOCSTATUS_SCSI_TASK_TERMINATED: /* 0x0048 */
6323                 desc = "SCSI Task Terminated";
6324                 break;
6325
6326         case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH: /* 0x0049 */
6327                 desc = "SCSI Residual Mismatch";
6328                 break;
6329
6330         case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED: /* 0x004A */
6331                 desc = "SCSI Task Management Failed";
6332                 break;
6333
6334         case MPI_IOCSTATUS_SCSI_IOC_TERMINATED: /* 0x004B */
6335                 desc = "SCSI IOC Terminated";
6336                 break;
6337
6338         case MPI_IOCSTATUS_SCSI_EXT_TERMINATED: /* 0x004C */
6339                 desc = "SCSI Ext Terminated";
6340                 break;
6341
6342         default:
6343                 desc = "Others";
6344                 break;
6345         }
6346         if (desc != "")
6347                 printk(MYIOC_s_INFO_FMT "IOCStatus(0x%04x): %s\n", ioc->name, status, desc);
6348 }
6349
6350 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
6351 EXPORT_SYMBOL(mpt_attach);
6352 EXPORT_SYMBOL(mpt_detach);
6353 #ifdef CONFIG_PM
6354 EXPORT_SYMBOL(mpt_resume);
6355 EXPORT_SYMBOL(mpt_suspend);
6356 #endif
6357 EXPORT_SYMBOL(ioc_list);
6358 EXPORT_SYMBOL(mpt_proc_root_dir);
6359 EXPORT_SYMBOL(mpt_register);
6360 EXPORT_SYMBOL(mpt_deregister);
6361 EXPORT_SYMBOL(mpt_event_register);
6362 EXPORT_SYMBOL(mpt_event_deregister);
6363 EXPORT_SYMBOL(mpt_reset_register);
6364 EXPORT_SYMBOL(mpt_reset_deregister);
6365 EXPORT_SYMBOL(mpt_device_driver_register);
6366 EXPORT_SYMBOL(mpt_device_driver_deregister);
6367 EXPORT_SYMBOL(mpt_get_msg_frame);
6368 EXPORT_SYMBOL(mpt_put_msg_frame);
6369 EXPORT_SYMBOL(mpt_free_msg_frame);
6370 EXPORT_SYMBOL(mpt_add_sge);
6371 EXPORT_SYMBOL(mpt_send_handshake_request);
6372 EXPORT_SYMBOL(mpt_verify_adapter);
6373 EXPORT_SYMBOL(mpt_GetIocState);
6374 EXPORT_SYMBOL(mpt_print_ioc_summary);
6375 EXPORT_SYMBOL(mpt_lan_index);
6376 EXPORT_SYMBOL(mpt_stm_index);
6377 EXPORT_SYMBOL(mpt_HardResetHandler);
6378 EXPORT_SYMBOL(mpt_config);
6379 EXPORT_SYMBOL(mpt_toolbox);
6380 EXPORT_SYMBOL(mpt_findImVolumes);
6381 EXPORT_SYMBOL(mpt_read_ioc_pg_3);
6382 EXPORT_SYMBOL(mpt_alloc_fw_memory);
6383 EXPORT_SYMBOL(mpt_free_fw_memory);
6384 EXPORT_SYMBOL(mptbase_sas_persist_operation);
6385 EXPORT_SYMBOL(mpt_alt_ioc_wait);
6386 EXPORT_SYMBOL(mptbase_GetFcPortPage0);
6387
6388
6389 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
6390 /*
6391  *      fusion_init - Fusion MPT base driver initialization routine.
6392  *
6393  *      Returns 0 for success, non-zero for failure.
6394  */
6395 static int __init
6396 fusion_init(void)
6397 {
6398         int i;
6399
6400         show_mptmod_ver(my_NAME, my_VERSION);
6401         printk(KERN_INFO COPYRIGHT "\n");
6402
6403         for (i = 0; i < MPT_MAX_PROTOCOL_DRIVERS; i++) {
6404                 MptCallbacks[i] = NULL;
6405                 MptDriverClass[i] = MPTUNKNOWN_DRIVER;
6406                 MptEvHandlers[i] = NULL;
6407                 MptResetHandlers[i] = NULL;
6408         }
6409
6410         /*  Register ourselves (mptbase) in order to facilitate
6411          *  EventNotification handling.
6412          */
6413         mpt_base_index = mpt_register(mpt_base_reply, MPTBASE_DRIVER);
6414
6415         /* Register for hard reset handling callbacks.
6416          */
6417         if (mpt_reset_register(mpt_base_index, mpt_ioc_reset) == 0) {
6418                 dprintk((KERN_INFO MYNAM ": Register for IOC reset notification\n"));
6419         } else {
6420                 /* FIXME! */
6421         }
6422
6423 #ifdef CONFIG_PROC_FS
6424         (void) procmpt_create();
6425 #endif
6426         return 0;
6427 }
6428
6429 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
6430 /*
6431  *      fusion_exit - Perform driver unload cleanup.
6432  *
6433  *      This routine frees all resources associated with each MPT adapter
6434  *      and removes all %MPT_PROCFS_MPTBASEDIR entries.
6435  */
6436 static void __exit
6437 fusion_exit(void)
6438 {
6439
6440         dexitprintk((KERN_INFO MYNAM ": fusion_exit() called!\n"));
6441
6442         mpt_reset_deregister(mpt_base_index);
6443
6444 #ifdef CONFIG_PROC_FS
6445         procmpt_destroy();
6446 #endif
6447 }
6448
6449 module_init(fusion_init);
6450 module_exit(fusion_exit);