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