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