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