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