scsi: lpfc: Add shutdown method for kexec
[pandora-kernel.git] / drivers / scsi / lpfc / lpfc_init.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2011 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/kthread.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
31 #include <linux/ctype.h>
32 #include <linux/aer.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_device.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_transport_fc.h>
40
41 #include "lpfc_hw4.h"
42 #include "lpfc_hw.h"
43 #include "lpfc_sli.h"
44 #include "lpfc_sli4.h"
45 #include "lpfc_nl.h"
46 #include "lpfc_disc.h"
47 #include "lpfc_scsi.h"
48 #include "lpfc.h"
49 #include "lpfc_logmsg.h"
50 #include "lpfc_crtn.h"
51 #include "lpfc_vport.h"
52 #include "lpfc_version.h"
53
54 char *_dump_buf_data;
55 unsigned long _dump_buf_data_order;
56 char *_dump_buf_dif;
57 unsigned long _dump_buf_dif_order;
58 spinlock_t _dump_buf_lock;
59
60 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
61 static int lpfc_post_rcv_buf(struct lpfc_hba *);
62 static int lpfc_sli4_queue_verify(struct lpfc_hba *);
63 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
64 static int lpfc_setup_endian_order(struct lpfc_hba *);
65 static int lpfc_sli4_read_config(struct lpfc_hba *);
66 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
67 static void lpfc_free_sgl_list(struct lpfc_hba *);
68 static int lpfc_init_sgl_list(struct lpfc_hba *);
69 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70 static void lpfc_free_active_sgl(struct lpfc_hba *);
71 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
76
77 static struct scsi_transport_template *lpfc_transport_template = NULL;
78 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
79 static DEFINE_IDR(lpfc_hba_index);
80
81 /**
82  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
83  * @phba: pointer to lpfc hba data structure.
84  *
85  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
86  * mailbox command. It retrieves the revision information from the HBA and
87  * collects the Vital Product Data (VPD) about the HBA for preparing the
88  * configuration of the HBA.
89  *
90  * Return codes:
91  *   0 - success.
92  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
93  *   Any other value - indicates an error.
94  **/
95 int
96 lpfc_config_port_prep(struct lpfc_hba *phba)
97 {
98         lpfc_vpd_t *vp = &phba->vpd;
99         int i = 0, rc;
100         LPFC_MBOXQ_t *pmb;
101         MAILBOX_t *mb;
102         char *lpfc_vpd_data = NULL;
103         uint16_t offset = 0;
104         static char licensed[56] =
105                     "key unlock for use with gnu public licensed code only\0";
106         static int init_key = 1;
107
108         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
109         if (!pmb) {
110                 phba->link_state = LPFC_HBA_ERROR;
111                 return -ENOMEM;
112         }
113
114         mb = &pmb->u.mb;
115         phba->link_state = LPFC_INIT_MBX_CMDS;
116
117         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
118                 if (init_key) {
119                         uint32_t *ptext = (uint32_t *) licensed;
120
121                         for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
122                                 *ptext = cpu_to_be32(*ptext);
123                         init_key = 0;
124                 }
125
126                 lpfc_read_nv(phba, pmb);
127                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
128                         sizeof (mb->un.varRDnvp.rsvd3));
129                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
130                          sizeof (licensed));
131
132                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
133
134                 if (rc != MBX_SUCCESS) {
135                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
136                                         "0324 Config Port initialization "
137                                         "error, mbxCmd x%x READ_NVPARM, "
138                                         "mbxStatus x%x\n",
139                                         mb->mbxCommand, mb->mbxStatus);
140                         mempool_free(pmb, phba->mbox_mem_pool);
141                         return -ERESTART;
142                 }
143                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
144                        sizeof(phba->wwnn));
145                 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
146                        sizeof(phba->wwpn));
147         }
148
149         phba->sli3_options = 0x0;
150
151         /* Setup and issue mailbox READ REV command */
152         lpfc_read_rev(phba, pmb);
153         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154         if (rc != MBX_SUCCESS) {
155                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
156                                 "0439 Adapter failed to init, mbxCmd x%x "
157                                 "READ_REV, mbxStatus x%x\n",
158                                 mb->mbxCommand, mb->mbxStatus);
159                 mempool_free( pmb, phba->mbox_mem_pool);
160                 return -ERESTART;
161         }
162
163
164         /*
165          * The value of rr must be 1 since the driver set the cv field to 1.
166          * This setting requires the FW to set all revision fields.
167          */
168         if (mb->un.varRdRev.rr == 0) {
169                 vp->rev.rBit = 0;
170                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
171                                 "0440 Adapter failed to init, READ_REV has "
172                                 "missing revision information.\n");
173                 mempool_free(pmb, phba->mbox_mem_pool);
174                 return -ERESTART;
175         }
176
177         if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
178                 mempool_free(pmb, phba->mbox_mem_pool);
179                 return -EINVAL;
180         }
181
182         /* Save information as VPD data */
183         vp->rev.rBit = 1;
184         memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
185         vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
186         memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
187         vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
188         memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
189         vp->rev.biuRev = mb->un.varRdRev.biuRev;
190         vp->rev.smRev = mb->un.varRdRev.smRev;
191         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
192         vp->rev.endecRev = mb->un.varRdRev.endecRev;
193         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
194         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
195         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
196         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
197         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
198         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
199
200         /* If the sli feature level is less then 9, we must
201          * tear down all RPIs and VPIs on link down if NPIV
202          * is enabled.
203          */
204         if (vp->rev.feaLevelHigh < 9)
205                 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
206
207         if (lpfc_is_LC_HBA(phba->pcidev->device))
208                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
209                                                 sizeof (phba->RandomData));
210
211         /* Get adapter VPD information */
212         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
213         if (!lpfc_vpd_data)
214                 goto out_free_mbox;
215         do {
216                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
217                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219                 if (rc != MBX_SUCCESS) {
220                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
221                                         "0441 VPD not present on adapter, "
222                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
223                                         mb->mbxCommand, mb->mbxStatus);
224                         mb->un.varDmp.word_cnt = 0;
225                 }
226                 /* dump mem may return a zero when finished or we got a
227                  * mailbox error, either way we are done.
228                  */
229                 if (mb->un.varDmp.word_cnt == 0)
230                         break;
231                 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232                         mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
233                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234                                       lpfc_vpd_data + offset,
235                                       mb->un.varDmp.word_cnt);
236                 offset += mb->un.varDmp.word_cnt;
237         } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238         lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
239
240         kfree(lpfc_vpd_data);
241 out_free_mbox:
242         mempool_free(pmb, phba->mbox_mem_pool);
243         return 0;
244 }
245
246 /**
247  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
248  * @phba: pointer to lpfc hba data structure.
249  * @pmboxq: pointer to the driver internal queue element for mailbox command.
250  *
251  * This is the completion handler for driver's configuring asynchronous event
252  * mailbox command to the device. If the mailbox command returns successfully,
253  * it will set internal async event support flag to 1; otherwise, it will
254  * set internal async event support flag to 0.
255  **/
256 static void
257 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258 {
259         if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
260                 phba->temp_sensor_support = 1;
261         else
262                 phba->temp_sensor_support = 0;
263         mempool_free(pmboxq, phba->mbox_mem_pool);
264         return;
265 }
266
267 /**
268  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
269  * @phba: pointer to lpfc hba data structure.
270  * @pmboxq: pointer to the driver internal queue element for mailbox command.
271  *
272  * This is the completion handler for dump mailbox command for getting
273  * wake up parameters. When this command complete, the response contain
274  * Option rom version of the HBA. This function translate the version number
275  * into a human readable string and store it in OptionROMVersion.
276  **/
277 static void
278 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279 {
280         struct prog_id *prg;
281         uint32_t prog_id_word;
282         char dist = ' ';
283         /* character array used for decoding dist type. */
284         char dist_char[] = "nabx";
285
286         if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
287                 mempool_free(pmboxq, phba->mbox_mem_pool);
288                 return;
289         }
290
291         prg = (struct prog_id *) &prog_id_word;
292
293         /* word 7 contain option rom version */
294         prog_id_word = pmboxq->u.mb.un.varWords[7];
295
296         /* Decode the Option rom version word to a readable string */
297         if (prg->dist < 4)
298                 dist = dist_char[prg->dist];
299
300         if ((prg->dist == 3) && (prg->num == 0))
301                 sprintf(phba->OptionROMVersion, "%d.%d%d",
302                         prg->ver, prg->rev, prg->lev);
303         else
304                 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305                         prg->ver, prg->rev, prg->lev,
306                         dist, prg->num);
307         mempool_free(pmboxq, phba->mbox_mem_pool);
308         return;
309 }
310
311 /**
312  * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
313  *      cfg_soft_wwnn, cfg_soft_wwpn
314  * @vport: pointer to lpfc vport data structure.
315  *
316  *
317  * Return codes
318  *   None.
319  **/
320 void
321 lpfc_update_vport_wwn(struct lpfc_vport *vport)
322 {
323         /* If the soft name exists then update it using the service params */
324         if (vport->phba->cfg_soft_wwnn)
325                 u64_to_wwn(vport->phba->cfg_soft_wwnn,
326                            vport->fc_sparam.nodeName.u.wwn);
327         if (vport->phba->cfg_soft_wwpn)
328                 u64_to_wwn(vport->phba->cfg_soft_wwpn,
329                            vport->fc_sparam.portName.u.wwn);
330
331         /*
332          * If the name is empty or there exists a soft name
333          * then copy the service params name, otherwise use the fc name
334          */
335         if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
336                 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
337                         sizeof(struct lpfc_name));
338         else
339                 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
340                         sizeof(struct lpfc_name));
341
342         if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
343                 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
344                         sizeof(struct lpfc_name));
345         else
346                 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
347                         sizeof(struct lpfc_name));
348 }
349
350 /**
351  * lpfc_config_port_post - Perform lpfc initialization after config port
352  * @phba: pointer to lpfc hba data structure.
353  *
354  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
355  * command call. It performs all internal resource and state setups on the
356  * port: post IOCB buffers, enable appropriate host interrupt attentions,
357  * ELS ring timers, etc.
358  *
359  * Return codes
360  *   0 - success.
361  *   Any other value - error.
362  **/
363 int
364 lpfc_config_port_post(struct lpfc_hba *phba)
365 {
366         struct lpfc_vport *vport = phba->pport;
367         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
368         LPFC_MBOXQ_t *pmb;
369         MAILBOX_t *mb;
370         struct lpfc_dmabuf *mp;
371         struct lpfc_sli *psli = &phba->sli;
372         uint32_t status, timeout;
373         int i, j;
374         int rc;
375
376         spin_lock_irq(&phba->hbalock);
377         /*
378          * If the Config port completed correctly the HBA is not
379          * over heated any more.
380          */
381         if (phba->over_temp_state == HBA_OVER_TEMP)
382                 phba->over_temp_state = HBA_NORMAL_TEMP;
383         spin_unlock_irq(&phba->hbalock);
384
385         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
386         if (!pmb) {
387                 phba->link_state = LPFC_HBA_ERROR;
388                 return -ENOMEM;
389         }
390         mb = &pmb->u.mb;
391
392         /* Get login parameters for NID.  */
393         rc = lpfc_read_sparam(phba, pmb, 0);
394         if (rc) {
395                 mempool_free(pmb, phba->mbox_mem_pool);
396                 return -ENOMEM;
397         }
398
399         pmb->vport = vport;
400         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
401                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
402                                 "0448 Adapter failed init, mbxCmd x%x "
403                                 "READ_SPARM mbxStatus x%x\n",
404                                 mb->mbxCommand, mb->mbxStatus);
405                 phba->link_state = LPFC_HBA_ERROR;
406                 mp = (struct lpfc_dmabuf *) pmb->context1;
407                 mempool_free(pmb, phba->mbox_mem_pool);
408                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
409                 kfree(mp);
410                 return -EIO;
411         }
412
413         mp = (struct lpfc_dmabuf *) pmb->context1;
414
415         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
416         lpfc_mbuf_free(phba, mp->virt, mp->phys);
417         kfree(mp);
418         pmb->context1 = NULL;
419         lpfc_update_vport_wwn(vport);
420
421         /* Update the fc_host data structures with new wwn. */
422         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
423         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
424         fc_host_max_npiv_vports(shost) = phba->max_vpi;
425
426         /* If no serial number in VPD data, use low 6 bytes of WWNN */
427         /* This should be consolidated into parse_vpd ? - mr */
428         if (phba->SerialNumber[0] == 0) {
429                 uint8_t *outptr;
430
431                 outptr = &vport->fc_nodename.u.s.IEEE[0];
432                 for (i = 0; i < 12; i++) {
433                         status = *outptr++;
434                         j = ((status & 0xf0) >> 4);
435                         if (j <= 9)
436                                 phba->SerialNumber[i] =
437                                     (char)((uint8_t) 0x30 + (uint8_t) j);
438                         else
439                                 phba->SerialNumber[i] =
440                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
441                         i++;
442                         j = (status & 0xf);
443                         if (j <= 9)
444                                 phba->SerialNumber[i] =
445                                     (char)((uint8_t) 0x30 + (uint8_t) j);
446                         else
447                                 phba->SerialNumber[i] =
448                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449                 }
450         }
451
452         lpfc_read_config(phba, pmb);
453         pmb->vport = vport;
454         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
455                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
456                                 "0453 Adapter failed to init, mbxCmd x%x "
457                                 "READ_CONFIG, mbxStatus x%x\n",
458                                 mb->mbxCommand, mb->mbxStatus);
459                 phba->link_state = LPFC_HBA_ERROR;
460                 mempool_free( pmb, phba->mbox_mem_pool);
461                 return -EIO;
462         }
463
464         /* Check if the port is disabled */
465         lpfc_sli_read_link_ste(phba);
466
467         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
468         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
469                 phba->cfg_hba_queue_depth =
470                         (mb->un.varRdConfig.max_xri + 1) -
471                                         lpfc_sli4_get_els_iocb_cnt(phba);
472
473         phba->lmt = mb->un.varRdConfig.lmt;
474
475         /* Get the default values for Model Name and Description */
476         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
477
478         if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_16G)
479             || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G)
480                 && !(phba->lmt & LMT_1Gb))
481             || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G)
482                 && !(phba->lmt & LMT_2Gb))
483             || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G)
484                 && !(phba->lmt & LMT_4Gb))
485             || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G)
486                 && !(phba->lmt & LMT_8Gb))
487             || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G)
488                 && !(phba->lmt & LMT_10Gb))
489             || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G)
490                 && !(phba->lmt & LMT_16Gb))) {
491                 /* Reset link speed to auto */
492                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
493                         "1302 Invalid speed for this board: "
494                         "Reset link speed to auto: x%x\n",
495                         phba->cfg_link_speed);
496                         phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
497         }
498
499         phba->link_state = LPFC_LINK_DOWN;
500
501         /* Only process IOCBs on ELS ring till hba_state is READY */
502         if (psli->ring[psli->extra_ring].cmdringaddr)
503                 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
504         if (psli->ring[psli->fcp_ring].cmdringaddr)
505                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
506         if (psli->ring[psli->next_ring].cmdringaddr)
507                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
508
509         /* Post receive buffers for desired rings */
510         if (phba->sli_rev != 3)
511                 lpfc_post_rcv_buf(phba);
512
513         /*
514          * Configure HBA MSI-X attention conditions to messages if MSI-X mode
515          */
516         if (phba->intr_type == MSIX) {
517                 rc = lpfc_config_msi(phba, pmb);
518                 if (rc) {
519                         mempool_free(pmb, phba->mbox_mem_pool);
520                         return -EIO;
521                 }
522                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
523                 if (rc != MBX_SUCCESS) {
524                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
525                                         "0352 Config MSI mailbox command "
526                                         "failed, mbxCmd x%x, mbxStatus x%x\n",
527                                         pmb->u.mb.mbxCommand,
528                                         pmb->u.mb.mbxStatus);
529                         mempool_free(pmb, phba->mbox_mem_pool);
530                         return -EIO;
531                 }
532         }
533
534         spin_lock_irq(&phba->hbalock);
535         /* Initialize ERATT handling flag */
536         phba->hba_flag &= ~HBA_ERATT_HANDLED;
537
538         /* Enable appropriate host interrupts */
539         if (lpfc_readl(phba->HCregaddr, &status)) {
540                 spin_unlock_irq(&phba->hbalock);
541                 return -EIO;
542         }
543         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
544         if (psli->num_rings > 0)
545                 status |= HC_R0INT_ENA;
546         if (psli->num_rings > 1)
547                 status |= HC_R1INT_ENA;
548         if (psli->num_rings > 2)
549                 status |= HC_R2INT_ENA;
550         if (psli->num_rings > 3)
551                 status |= HC_R3INT_ENA;
552
553         if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
554             (phba->cfg_poll & DISABLE_FCP_RING_INT))
555                 status &= ~(HC_R0INT_ENA);
556
557         writel(status, phba->HCregaddr);
558         readl(phba->HCregaddr); /* flush */
559         spin_unlock_irq(&phba->hbalock);
560
561         /* Set up ring-0 (ELS) timer */
562         timeout = phba->fc_ratov * 2;
563         mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
564         /* Set up heart beat (HB) timer */
565         mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
566         phba->hb_outstanding = 0;
567         phba->last_completion_time = jiffies;
568         /* Set up error attention (ERATT) polling timer */
569         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
570
571         if (phba->hba_flag & LINK_DISABLED) {
572                 lpfc_printf_log(phba,
573                         KERN_ERR, LOG_INIT,
574                         "2598 Adapter Link is disabled.\n");
575                 lpfc_down_link(phba, pmb);
576                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
577                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
578                 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
579                         lpfc_printf_log(phba,
580                         KERN_ERR, LOG_INIT,
581                         "2599 Adapter failed to issue DOWN_LINK"
582                         " mbox command rc 0x%x\n", rc);
583
584                         mempool_free(pmb, phba->mbox_mem_pool);
585                         return -EIO;
586                 }
587         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
588                 lpfc_init_link(phba, pmb, phba->cfg_topology,
589                         phba->cfg_link_speed);
590                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
591                 lpfc_set_loopback_flag(phba);
592                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
593                 if (rc != MBX_SUCCESS) {
594                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
595                                 "0454 Adapter failed to init, mbxCmd x%x "
596                                 "INIT_LINK, mbxStatus x%x\n",
597                                 mb->mbxCommand, mb->mbxStatus);
598
599                         /* Clear all interrupt enable conditions */
600                         writel(0, phba->HCregaddr);
601                         readl(phba->HCregaddr); /* flush */
602                         /* Clear all pending interrupts */
603                         writel(0xffffffff, phba->HAregaddr);
604                         readl(phba->HAregaddr); /* flush */
605                         phba->link_state = LPFC_HBA_ERROR;
606                         if (rc != MBX_BUSY)
607                                 mempool_free(pmb, phba->mbox_mem_pool);
608                         return -EIO;
609                 }
610         }
611         /* MBOX buffer will be freed in mbox compl */
612         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
613         if (!pmb) {
614                 phba->link_state = LPFC_HBA_ERROR;
615                 return -ENOMEM;
616         }
617
618         lpfc_config_async(phba, pmb, LPFC_ELS_RING);
619         pmb->mbox_cmpl = lpfc_config_async_cmpl;
620         pmb->vport = phba->pport;
621         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
622
623         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
624                 lpfc_printf_log(phba,
625                                 KERN_ERR,
626                                 LOG_INIT,
627                                 "0456 Adapter failed to issue "
628                                 "ASYNCEVT_ENABLE mbox status x%x\n",
629                                 rc);
630                 mempool_free(pmb, phba->mbox_mem_pool);
631         }
632
633         /* Get Option rom version */
634         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
635         if (!pmb) {
636                 phba->link_state = LPFC_HBA_ERROR;
637                 return -ENOMEM;
638         }
639
640         lpfc_dump_wakeup_param(phba, pmb);
641         pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
642         pmb->vport = phba->pport;
643         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
644
645         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
646                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
647                                 "to get Option ROM version status x%x\n", rc);
648                 mempool_free(pmb, phba->mbox_mem_pool);
649         }
650
651         return 0;
652 }
653
654 /**
655  * lpfc_hba_init_link - Initialize the FC link
656  * @phba: pointer to lpfc hba data structure.
657  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
658  *
659  * This routine will issue the INIT_LINK mailbox command call.
660  * It is available to other drivers through the lpfc_hba data
661  * structure for use as a delayed link up mechanism with the
662  * module parameter lpfc_suppress_link_up.
663  *
664  * Return code
665  *              0 - success
666  *              Any other value - error
667  **/
668 int
669 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
670 {
671         struct lpfc_vport *vport = phba->pport;
672         LPFC_MBOXQ_t *pmb;
673         MAILBOX_t *mb;
674         int rc;
675
676         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
677         if (!pmb) {
678                 phba->link_state = LPFC_HBA_ERROR;
679                 return -ENOMEM;
680         }
681         mb = &pmb->u.mb;
682         pmb->vport = vport;
683
684         lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
685         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
686         lpfc_set_loopback_flag(phba);
687         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
688         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
689                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
690                         "0498 Adapter failed to init, mbxCmd x%x "
691                         "INIT_LINK, mbxStatus x%x\n",
692                         mb->mbxCommand, mb->mbxStatus);
693                 if (phba->sli_rev <= LPFC_SLI_REV3) {
694                         /* Clear all interrupt enable conditions */
695                         writel(0, phba->HCregaddr);
696                         readl(phba->HCregaddr); /* flush */
697                         /* Clear all pending interrupts */
698                         writel(0xffffffff, phba->HAregaddr);
699                         readl(phba->HAregaddr); /* flush */
700                 }
701                 phba->link_state = LPFC_HBA_ERROR;
702                 if (rc != MBX_BUSY || flag == MBX_POLL)
703                         mempool_free(pmb, phba->mbox_mem_pool);
704                 return -EIO;
705         }
706         phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
707         if (flag == MBX_POLL)
708                 mempool_free(pmb, phba->mbox_mem_pool);
709
710         return 0;
711 }
712
713 /**
714  * lpfc_hba_down_link - this routine downs the FC link
715  * @phba: pointer to lpfc hba data structure.
716  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
717  *
718  * This routine will issue the DOWN_LINK mailbox command call.
719  * It is available to other drivers through the lpfc_hba data
720  * structure for use to stop the link.
721  *
722  * Return code
723  *              0 - success
724  *              Any other value - error
725  **/
726 int
727 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
728 {
729         LPFC_MBOXQ_t *pmb;
730         int rc;
731
732         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
733         if (!pmb) {
734                 phba->link_state = LPFC_HBA_ERROR;
735                 return -ENOMEM;
736         }
737
738         lpfc_printf_log(phba,
739                 KERN_ERR, LOG_INIT,
740                 "0491 Adapter Link is disabled.\n");
741         lpfc_down_link(phba, pmb);
742         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
743         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
744         if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
745                 lpfc_printf_log(phba,
746                 KERN_ERR, LOG_INIT,
747                 "2522 Adapter failed to issue DOWN_LINK"
748                 " mbox command rc 0x%x\n", rc);
749
750                 mempool_free(pmb, phba->mbox_mem_pool);
751                 return -EIO;
752         }
753         if (flag == MBX_POLL)
754                 mempool_free(pmb, phba->mbox_mem_pool);
755
756         return 0;
757 }
758
759 /**
760  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
761  * @phba: pointer to lpfc HBA data structure.
762  *
763  * This routine will do LPFC uninitialization before the HBA is reset when
764  * bringing down the SLI Layer.
765  *
766  * Return codes
767  *   0 - success.
768  *   Any other value - error.
769  **/
770 int
771 lpfc_hba_down_prep(struct lpfc_hba *phba)
772 {
773         struct lpfc_vport **vports;
774         int i;
775
776         if (phba->sli_rev <= LPFC_SLI_REV3) {
777                 /* Disable interrupts */
778                 writel(0, phba->HCregaddr);
779                 readl(phba->HCregaddr); /* flush */
780         }
781
782         if (phba->pport->load_flag & FC_UNLOADING)
783                 lpfc_cleanup_discovery_resources(phba->pport);
784         else {
785                 vports = lpfc_create_vport_work_array(phba);
786                 if (vports != NULL)
787                         for (i = 0; i <= phba->max_vports &&
788                                 vports[i] != NULL; i++)
789                                 lpfc_cleanup_discovery_resources(vports[i]);
790                 lpfc_destroy_vport_work_array(phba, vports);
791         }
792         return 0;
793 }
794
795 /**
796  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
797  * @phba: pointer to lpfc HBA data structure.
798  *
799  * This routine will do uninitialization after the HBA is reset when bring
800  * down the SLI Layer.
801  *
802  * Return codes
803  *   0 - success.
804  *   Any other value - error.
805  **/
806 static int
807 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
808 {
809         struct lpfc_sli *psli = &phba->sli;
810         struct lpfc_sli_ring *pring;
811         struct lpfc_dmabuf *mp, *next_mp;
812         LIST_HEAD(completions);
813         int i;
814
815         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
816                 lpfc_sli_hbqbuf_free_all(phba);
817         else {
818                 /* Cleanup preposted buffers on the ELS ring */
819                 pring = &psli->ring[LPFC_ELS_RING];
820                 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
821                         list_del(&mp->list);
822                         pring->postbufq_cnt--;
823                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
824                         kfree(mp);
825                 }
826         }
827
828         spin_lock_irq(&phba->hbalock);
829         for (i = 0; i < psli->num_rings; i++) {
830                 pring = &psli->ring[i];
831
832                 /* At this point in time the HBA is either reset or DOA. Either
833                  * way, nothing should be on txcmplq as it will NEVER complete.
834                  */
835                 list_splice_init(&pring->txcmplq, &completions);
836                 pring->txcmplq_cnt = 0;
837                 spin_unlock_irq(&phba->hbalock);
838
839                 /* Cancel all the IOCBs from the completions list */
840                 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
841                                       IOERR_SLI_ABORTED);
842
843                 lpfc_sli_abort_iocb_ring(phba, pring);
844                 spin_lock_irq(&phba->hbalock);
845         }
846         spin_unlock_irq(&phba->hbalock);
847
848         return 0;
849 }
850
851 /**
852  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
853  * @phba: pointer to lpfc HBA data structure.
854  *
855  * This routine will do uninitialization after the HBA is reset when bring
856  * down the SLI Layer.
857  *
858  * Return codes
859  *   0 - success.
860  *   Any other value - error.
861  **/
862 static int
863 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
864 {
865         struct lpfc_scsi_buf *psb, *psb_next;
866         LIST_HEAD(aborts);
867         int ret;
868         unsigned long iflag = 0;
869         struct lpfc_sglq *sglq_entry = NULL;
870
871         ret = lpfc_hba_down_post_s3(phba);
872         if (ret)
873                 return ret;
874         /* At this point in time the HBA is either reset or DOA. Either
875          * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
876          * on the lpfc_sgl_list so that it can either be freed if the
877          * driver is unloading or reposted if the driver is restarting
878          * the port.
879          */
880         spin_lock_irq(&phba->hbalock);  /* required for lpfc_sgl_list and */
881                                         /* scsl_buf_list */
882         /* abts_sgl_list_lock required because worker thread uses this
883          * list.
884          */
885         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
886         list_for_each_entry(sglq_entry,
887                 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
888                 sglq_entry->state = SGL_FREED;
889
890         list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
891                         &phba->sli4_hba.lpfc_sgl_list);
892         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
893         /* abts_scsi_buf_list_lock required because worker thread uses this
894          * list.
895          */
896         spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
897         list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
898                         &aborts);
899         spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
900         spin_unlock_irq(&phba->hbalock);
901
902         list_for_each_entry_safe(psb, psb_next, &aborts, list) {
903                 psb->pCmd = NULL;
904                 psb->status = IOSTAT_SUCCESS;
905         }
906         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
907         list_splice(&aborts, &phba->lpfc_scsi_buf_list);
908         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
909         return 0;
910 }
911
912 /**
913  * lpfc_hba_down_post - Wrapper func for hba down post routine
914  * @phba: pointer to lpfc HBA data structure.
915  *
916  * This routine wraps the actual SLI3 or SLI4 routine for performing
917  * uninitialization after the HBA is reset when bring down the SLI Layer.
918  *
919  * Return codes
920  *   0 - success.
921  *   Any other value - error.
922  **/
923 int
924 lpfc_hba_down_post(struct lpfc_hba *phba)
925 {
926         return (*phba->lpfc_hba_down_post)(phba);
927 }
928
929 /**
930  * lpfc_hb_timeout - The HBA-timer timeout handler
931  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
932  *
933  * This is the HBA-timer timeout handler registered to the lpfc driver. When
934  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
935  * work-port-events bitmap and the worker thread is notified. This timeout
936  * event will be used by the worker thread to invoke the actual timeout
937  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
938  * be performed in the timeout handler and the HBA timeout event bit shall
939  * be cleared by the worker thread after it has taken the event bitmap out.
940  **/
941 static void
942 lpfc_hb_timeout(unsigned long ptr)
943 {
944         struct lpfc_hba *phba;
945         uint32_t tmo_posted;
946         unsigned long iflag;
947
948         phba = (struct lpfc_hba *)ptr;
949
950         /* Check for heart beat timeout conditions */
951         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
952         tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
953         if (!tmo_posted)
954                 phba->pport->work_port_events |= WORKER_HB_TMO;
955         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
956
957         /* Tell the worker thread there is work to do */
958         if (!tmo_posted)
959                 lpfc_worker_wake_up(phba);
960         return;
961 }
962
963 /**
964  * lpfc_rrq_timeout - The RRQ-timer timeout handler
965  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
966  *
967  * This is the RRQ-timer timeout handler registered to the lpfc driver. When
968  * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
969  * work-port-events bitmap and the worker thread is notified. This timeout
970  * event will be used by the worker thread to invoke the actual timeout
971  * handler routine, lpfc_rrq_handler. Any periodical operations will
972  * be performed in the timeout handler and the RRQ timeout event bit shall
973  * be cleared by the worker thread after it has taken the event bitmap out.
974  **/
975 static void
976 lpfc_rrq_timeout(unsigned long ptr)
977 {
978         struct lpfc_hba *phba;
979         unsigned long iflag;
980
981         phba = (struct lpfc_hba *)ptr;
982         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
983         phba->hba_flag |= HBA_RRQ_ACTIVE;
984         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
985         lpfc_worker_wake_up(phba);
986 }
987
988 /**
989  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
990  * @phba: pointer to lpfc hba data structure.
991  * @pmboxq: pointer to the driver internal queue element for mailbox command.
992  *
993  * This is the callback function to the lpfc heart-beat mailbox command.
994  * If configured, the lpfc driver issues the heart-beat mailbox command to
995  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
996  * heart-beat mailbox command is issued, the driver shall set up heart-beat
997  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
998  * heart-beat outstanding state. Once the mailbox command comes back and
999  * no error conditions detected, the heart-beat mailbox command timer is
1000  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1001  * state is cleared for the next heart-beat. If the timer expired with the
1002  * heart-beat outstanding state set, the driver will put the HBA offline.
1003  **/
1004 static void
1005 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1006 {
1007         unsigned long drvr_flag;
1008
1009         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1010         phba->hb_outstanding = 0;
1011         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1012
1013         /* Check and reset heart-beat timer is necessary */
1014         mempool_free(pmboxq, phba->mbox_mem_pool);
1015         if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1016                 !(phba->link_state == LPFC_HBA_ERROR) &&
1017                 !(phba->pport->load_flag & FC_UNLOADING))
1018                 mod_timer(&phba->hb_tmofunc,
1019                         jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1020         return;
1021 }
1022
1023 /**
1024  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1025  * @phba: pointer to lpfc hba data structure.
1026  *
1027  * This is the actual HBA-timer timeout handler to be invoked by the worker
1028  * thread whenever the HBA timer fired and HBA-timeout event posted. This
1029  * handler performs any periodic operations needed for the device. If such
1030  * periodic event has already been attended to either in the interrupt handler
1031  * or by processing slow-ring or fast-ring events within the HBA-timer
1032  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1033  * the timer for the next timeout period. If lpfc heart-beat mailbox command
1034  * is configured and there is no heart-beat mailbox command outstanding, a
1035  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1036  * has been a heart-beat mailbox command outstanding, the HBA shall be put
1037  * to offline.
1038  **/
1039 void
1040 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1041 {
1042         struct lpfc_vport **vports;
1043         LPFC_MBOXQ_t *pmboxq;
1044         struct lpfc_dmabuf *buf_ptr;
1045         int retval, i;
1046         struct lpfc_sli *psli = &phba->sli;
1047         LIST_HEAD(completions);
1048
1049         vports = lpfc_create_vport_work_array(phba);
1050         if (vports != NULL)
1051                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1052                         lpfc_rcv_seq_check_edtov(vports[i]);
1053         lpfc_destroy_vport_work_array(phba, vports);
1054
1055         if ((phba->link_state == LPFC_HBA_ERROR) ||
1056                 (phba->pport->load_flag & FC_UNLOADING) ||
1057                 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1058                 return;
1059
1060         spin_lock_irq(&phba->pport->work_port_lock);
1061
1062         if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1063                 jiffies)) {
1064                 spin_unlock_irq(&phba->pport->work_port_lock);
1065                 if (!phba->hb_outstanding)
1066                         mod_timer(&phba->hb_tmofunc,
1067                                 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1068                 else
1069                         mod_timer(&phba->hb_tmofunc,
1070                                 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1071                 return;
1072         }
1073         spin_unlock_irq(&phba->pport->work_port_lock);
1074
1075         if (phba->elsbuf_cnt &&
1076                 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1077                 spin_lock_irq(&phba->hbalock);
1078                 list_splice_init(&phba->elsbuf, &completions);
1079                 phba->elsbuf_cnt = 0;
1080                 phba->elsbuf_prev_cnt = 0;
1081                 spin_unlock_irq(&phba->hbalock);
1082
1083                 while (!list_empty(&completions)) {
1084                         list_remove_head(&completions, buf_ptr,
1085                                 struct lpfc_dmabuf, list);
1086                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1087                         kfree(buf_ptr);
1088                 }
1089         }
1090         phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1091
1092         /* If there is no heart beat outstanding, issue a heartbeat command */
1093         if (phba->cfg_enable_hba_heartbeat) {
1094                 if (!phba->hb_outstanding) {
1095                         if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1096                                 (list_empty(&psli->mboxq))) {
1097                                 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1098                                                         GFP_KERNEL);
1099                                 if (!pmboxq) {
1100                                         mod_timer(&phba->hb_tmofunc,
1101                                                  jiffies +
1102                                                  HZ * LPFC_HB_MBOX_INTERVAL);
1103                                         return;
1104                                 }
1105
1106                                 lpfc_heart_beat(phba, pmboxq);
1107                                 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1108                                 pmboxq->vport = phba->pport;
1109                                 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1110                                                 MBX_NOWAIT);
1111
1112                                 if (retval != MBX_BUSY &&
1113                                         retval != MBX_SUCCESS) {
1114                                         mempool_free(pmboxq,
1115                                                         phba->mbox_mem_pool);
1116                                         mod_timer(&phba->hb_tmofunc,
1117                                                 jiffies +
1118                                                 HZ * LPFC_HB_MBOX_INTERVAL);
1119                                         return;
1120                                 }
1121                                 phba->skipped_hb = 0;
1122                                 phba->hb_outstanding = 1;
1123                         } else if (time_before_eq(phba->last_completion_time,
1124                                         phba->skipped_hb)) {
1125                                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1126                                         "2857 Last completion time not "
1127                                         " updated in %d ms\n",
1128                                         jiffies_to_msecs(jiffies
1129                                                  - phba->last_completion_time));
1130                         } else
1131                                 phba->skipped_hb = jiffies;
1132
1133                         mod_timer(&phba->hb_tmofunc,
1134                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1135                         return;
1136                 } else {
1137                         /*
1138                         * If heart beat timeout called with hb_outstanding set
1139                         * we need to give the hb mailbox cmd a chance to
1140                         * complete or TMO.
1141                         */
1142                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1143                                         "0459 Adapter heartbeat still out"
1144                                         "standing:last compl time was %d ms.\n",
1145                                         jiffies_to_msecs(jiffies
1146                                                  - phba->last_completion_time));
1147                         mod_timer(&phba->hb_tmofunc,
1148                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1149                 }
1150         }
1151 }
1152
1153 /**
1154  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1155  * @phba: pointer to lpfc hba data structure.
1156  *
1157  * This routine is called to bring the HBA offline when HBA hardware error
1158  * other than Port Error 6 has been detected.
1159  **/
1160 static void
1161 lpfc_offline_eratt(struct lpfc_hba *phba)
1162 {
1163         struct lpfc_sli   *psli = &phba->sli;
1164
1165         spin_lock_irq(&phba->hbalock);
1166         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1167         spin_unlock_irq(&phba->hbalock);
1168         lpfc_offline_prep(phba);
1169
1170         lpfc_offline(phba);
1171         lpfc_reset_barrier(phba);
1172         spin_lock_irq(&phba->hbalock);
1173         lpfc_sli_brdreset(phba);
1174         spin_unlock_irq(&phba->hbalock);
1175         lpfc_hba_down_post(phba);
1176         lpfc_sli_brdready(phba, HS_MBRDY);
1177         lpfc_unblock_mgmt_io(phba);
1178         phba->link_state = LPFC_HBA_ERROR;
1179         return;
1180 }
1181
1182 /**
1183  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1184  * @phba: pointer to lpfc hba data structure.
1185  *
1186  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1187  * other than Port Error 6 has been detected.
1188  **/
1189 static void
1190 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1191 {
1192         lpfc_offline_prep(phba);
1193         lpfc_offline(phba);
1194         lpfc_sli4_brdreset(phba);
1195         lpfc_hba_down_post(phba);
1196         lpfc_sli4_post_status_check(phba);
1197         lpfc_unblock_mgmt_io(phba);
1198         phba->link_state = LPFC_HBA_ERROR;
1199 }
1200
1201 /**
1202  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1203  * @phba: pointer to lpfc hba data structure.
1204  *
1205  * This routine is invoked to handle the deferred HBA hardware error
1206  * conditions. This type of error is indicated by HBA by setting ER1
1207  * and another ER bit in the host status register. The driver will
1208  * wait until the ER1 bit clears before handling the error condition.
1209  **/
1210 static void
1211 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1212 {
1213         uint32_t old_host_status = phba->work_hs;
1214         struct lpfc_sli_ring  *pring;
1215         struct lpfc_sli *psli = &phba->sli;
1216
1217         /* If the pci channel is offline, ignore possible errors,
1218          * since we cannot communicate with the pci card anyway.
1219          */
1220         if (pci_channel_offline(phba->pcidev)) {
1221                 spin_lock_irq(&phba->hbalock);
1222                 phba->hba_flag &= ~DEFER_ERATT;
1223                 spin_unlock_irq(&phba->hbalock);
1224                 return;
1225         }
1226
1227         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1228                 "0479 Deferred Adapter Hardware Error "
1229                 "Data: x%x x%x x%x\n",
1230                 phba->work_hs,
1231                 phba->work_status[0], phba->work_status[1]);
1232
1233         spin_lock_irq(&phba->hbalock);
1234         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1235         spin_unlock_irq(&phba->hbalock);
1236
1237
1238         /*
1239          * Firmware stops when it triggred erratt. That could cause the I/Os
1240          * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1241          * SCSI layer retry it after re-establishing link.
1242          */
1243         pring = &psli->ring[psli->fcp_ring];
1244         lpfc_sli_abort_iocb_ring(phba, pring);
1245
1246         /*
1247          * There was a firmware error. Take the hba offline and then
1248          * attempt to restart it.
1249          */
1250         lpfc_offline_prep(phba);
1251         lpfc_offline(phba);
1252
1253         /* Wait for the ER1 bit to clear.*/
1254         while (phba->work_hs & HS_FFER1) {
1255                 msleep(100);
1256                 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1257                         phba->work_hs = UNPLUG_ERR ;
1258                         break;
1259                 }
1260                 /* If driver is unloading let the worker thread continue */
1261                 if (phba->pport->load_flag & FC_UNLOADING) {
1262                         phba->work_hs = 0;
1263                         break;
1264                 }
1265         }
1266
1267         /*
1268          * This is to ptrotect against a race condition in which
1269          * first write to the host attention register clear the
1270          * host status register.
1271          */
1272         if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1273                 phba->work_hs = old_host_status & ~HS_FFER1;
1274
1275         spin_lock_irq(&phba->hbalock);
1276         phba->hba_flag &= ~DEFER_ERATT;
1277         spin_unlock_irq(&phba->hbalock);
1278         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1279         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1280 }
1281
1282 static void
1283 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1284 {
1285         struct lpfc_board_event_header board_event;
1286         struct Scsi_Host *shost;
1287
1288         board_event.event_type = FC_REG_BOARD_EVENT;
1289         board_event.subcategory = LPFC_EVENT_PORTINTERR;
1290         shost = lpfc_shost_from_vport(phba->pport);
1291         fc_host_post_vendor_event(shost, fc_get_event_number(),
1292                                   sizeof(board_event),
1293                                   (char *) &board_event,
1294                                   LPFC_NL_VENDOR_ID);
1295 }
1296
1297 /**
1298  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1299  * @phba: pointer to lpfc hba data structure.
1300  *
1301  * This routine is invoked to handle the following HBA hardware error
1302  * conditions:
1303  * 1 - HBA error attention interrupt
1304  * 2 - DMA ring index out of range
1305  * 3 - Mailbox command came back as unknown
1306  **/
1307 static void
1308 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1309 {
1310         struct lpfc_vport *vport = phba->pport;
1311         struct lpfc_sli   *psli = &phba->sli;
1312         struct lpfc_sli_ring  *pring;
1313         uint32_t event_data;
1314         unsigned long temperature;
1315         struct temp_event temp_event_data;
1316         struct Scsi_Host  *shost;
1317
1318         /* If the pci channel is offline, ignore possible errors,
1319          * since we cannot communicate with the pci card anyway.
1320          */
1321         if (pci_channel_offline(phba->pcidev)) {
1322                 spin_lock_irq(&phba->hbalock);
1323                 phba->hba_flag &= ~DEFER_ERATT;
1324                 spin_unlock_irq(&phba->hbalock);
1325                 return;
1326         }
1327
1328         /* If resets are disabled then leave the HBA alone and return */
1329         if (!phba->cfg_enable_hba_reset)
1330                 return;
1331
1332         /* Send an internal error event to mgmt application */
1333         lpfc_board_errevt_to_mgmt(phba);
1334
1335         if (phba->hba_flag & DEFER_ERATT)
1336                 lpfc_handle_deferred_eratt(phba);
1337
1338         if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1339                 if (phba->work_hs & HS_FFER6)
1340                         /* Re-establishing Link */
1341                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1342                                         "1301 Re-establishing Link "
1343                                         "Data: x%x x%x x%x\n",
1344                                         phba->work_hs, phba->work_status[0],
1345                                         phba->work_status[1]);
1346                 if (phba->work_hs & HS_FFER8)
1347                         /* Device Zeroization */
1348                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1349                                         "2861 Host Authentication device "
1350                                         "zeroization Data:x%x x%x x%x\n",
1351                                         phba->work_hs, phba->work_status[0],
1352                                         phba->work_status[1]);
1353
1354                 spin_lock_irq(&phba->hbalock);
1355                 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1356                 spin_unlock_irq(&phba->hbalock);
1357
1358                 /*
1359                 * Firmware stops when it triggled erratt with HS_FFER6.
1360                 * That could cause the I/Os dropped by the firmware.
1361                 * Error iocb (I/O) on txcmplq and let the SCSI layer
1362                 * retry it after re-establishing link.
1363                 */
1364                 pring = &psli->ring[psli->fcp_ring];
1365                 lpfc_sli_abort_iocb_ring(phba, pring);
1366
1367                 /*
1368                  * There was a firmware error.  Take the hba offline and then
1369                  * attempt to restart it.
1370                  */
1371                 lpfc_offline_prep(phba);
1372                 lpfc_offline(phba);
1373                 lpfc_sli_brdrestart(phba);
1374                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
1375                         lpfc_unblock_mgmt_io(phba);
1376                         return;
1377                 }
1378                 lpfc_unblock_mgmt_io(phba);
1379         } else if (phba->work_hs & HS_CRIT_TEMP) {
1380                 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1381                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1382                 temp_event_data.event_code = LPFC_CRIT_TEMP;
1383                 temp_event_data.data = (uint32_t)temperature;
1384
1385                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1386                                 "0406 Adapter maximum temperature exceeded "
1387                                 "(%ld), taking this port offline "
1388                                 "Data: x%x x%x x%x\n",
1389                                 temperature, phba->work_hs,
1390                                 phba->work_status[0], phba->work_status[1]);
1391
1392                 shost = lpfc_shost_from_vport(phba->pport);
1393                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1394                                           sizeof(temp_event_data),
1395                                           (char *) &temp_event_data,
1396                                           SCSI_NL_VID_TYPE_PCI
1397                                           | PCI_VENDOR_ID_EMULEX);
1398
1399                 spin_lock_irq(&phba->hbalock);
1400                 phba->over_temp_state = HBA_OVER_TEMP;
1401                 spin_unlock_irq(&phba->hbalock);
1402                 lpfc_offline_eratt(phba);
1403
1404         } else {
1405                 /* The if clause above forces this code path when the status
1406                  * failure is a value other than FFER6. Do not call the offline
1407                  * twice. This is the adapter hardware error path.
1408                  */
1409                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1410                                 "0457 Adapter Hardware Error "
1411                                 "Data: x%x x%x x%x\n",
1412                                 phba->work_hs,
1413                                 phba->work_status[0], phba->work_status[1]);
1414
1415                 event_data = FC_REG_DUMP_EVENT;
1416                 shost = lpfc_shost_from_vport(vport);
1417                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1418                                 sizeof(event_data), (char *) &event_data,
1419                                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1420
1421                 lpfc_offline_eratt(phba);
1422         }
1423         return;
1424 }
1425
1426 /**
1427  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1428  * @phba: pointer to lpfc hba data structure.
1429  *
1430  * This routine is invoked to handle the SLI4 HBA hardware error attention
1431  * conditions.
1432  **/
1433 static void
1434 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1435 {
1436         struct lpfc_vport *vport = phba->pport;
1437         uint32_t event_data;
1438         struct Scsi_Host *shost;
1439         uint32_t if_type;
1440         struct lpfc_register portstat_reg;
1441         int rc;
1442
1443         /* If the pci channel is offline, ignore possible errors, since
1444          * we cannot communicate with the pci card anyway.
1445          */
1446         if (pci_channel_offline(phba->pcidev))
1447                 return;
1448         /* If resets are disabled then leave the HBA alone and return */
1449         if (!phba->cfg_enable_hba_reset)
1450                 return;
1451
1452         /* Send an internal error event to mgmt application */
1453         lpfc_board_errevt_to_mgmt(phba);
1454
1455         /* For now, the actual action for SLI4 device handling is not
1456          * specified yet, just treated it as adaptor hardware failure
1457          */
1458         event_data = FC_REG_DUMP_EVENT;
1459         shost = lpfc_shost_from_vport(vport);
1460         fc_host_post_vendor_event(shost, fc_get_event_number(),
1461                                   sizeof(event_data), (char *) &event_data,
1462                                   SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1463
1464         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1465         switch (if_type) {
1466         case LPFC_SLI_INTF_IF_TYPE_0:
1467                 lpfc_sli4_offline_eratt(phba);
1468                 break;
1469         case LPFC_SLI_INTF_IF_TYPE_2:
1470                 portstat_reg.word0 =
1471                         readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
1472
1473                 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1474                         /* TODO: Register for Overtemp async events. */
1475                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1476                                 "2889 Port Overtemperature event, "
1477                                 "taking port\n");
1478                         spin_lock_irq(&phba->hbalock);
1479                         phba->over_temp_state = HBA_OVER_TEMP;
1480                         spin_unlock_irq(&phba->hbalock);
1481                         lpfc_sli4_offline_eratt(phba);
1482                         return;
1483                 }
1484                 /*
1485                  * On error status condition, driver need to wait for port
1486                  * ready before performing reset.
1487                  */
1488                 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1489                 if (!rc) {
1490                         /* need reset: attempt for port recovery */
1491                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1492                                         "2887 Port Error: Attempting "
1493                                         "Port Recovery\n");
1494                         lpfc_offline_prep(phba);
1495                         lpfc_offline(phba);
1496                         lpfc_sli_brdrestart(phba);
1497                         if (lpfc_online(phba) == 0) {
1498                                 lpfc_unblock_mgmt_io(phba);
1499                                 return;
1500                         }
1501                         /* fall through for not able to recover */
1502                 }
1503                 lpfc_sli4_offline_eratt(phba);
1504                 break;
1505         case LPFC_SLI_INTF_IF_TYPE_1:
1506         default:
1507                 break;
1508         }
1509 }
1510
1511 /**
1512  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1513  * @phba: pointer to lpfc HBA data structure.
1514  *
1515  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1516  * routine from the API jump table function pointer from the lpfc_hba struct.
1517  *
1518  * Return codes
1519  *   0 - success.
1520  *   Any other value - error.
1521  **/
1522 void
1523 lpfc_handle_eratt(struct lpfc_hba *phba)
1524 {
1525         (*phba->lpfc_handle_eratt)(phba);
1526 }
1527
1528 /**
1529  * lpfc_handle_latt - The HBA link event handler
1530  * @phba: pointer to lpfc hba data structure.
1531  *
1532  * This routine is invoked from the worker thread to handle a HBA host
1533  * attention link event.
1534  **/
1535 void
1536 lpfc_handle_latt(struct lpfc_hba *phba)
1537 {
1538         struct lpfc_vport *vport = phba->pport;
1539         struct lpfc_sli   *psli = &phba->sli;
1540         LPFC_MBOXQ_t *pmb;
1541         volatile uint32_t control;
1542         struct lpfc_dmabuf *mp;
1543         int rc = 0;
1544
1545         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1546         if (!pmb) {
1547                 rc = 1;
1548                 goto lpfc_handle_latt_err_exit;
1549         }
1550
1551         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1552         if (!mp) {
1553                 rc = 2;
1554                 goto lpfc_handle_latt_free_pmb;
1555         }
1556
1557         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1558         if (!mp->virt) {
1559                 rc = 3;
1560                 goto lpfc_handle_latt_free_mp;
1561         }
1562
1563         /* Cleanup any outstanding ELS commands */
1564         lpfc_els_flush_all_cmd(phba);
1565
1566         psli->slistat.link_event++;
1567         lpfc_read_topology(phba, pmb, mp);
1568         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
1569         pmb->vport = vport;
1570         /* Block ELS IOCBs until we have processed this mbox command */
1571         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1572         rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1573         if (rc == MBX_NOT_FINISHED) {
1574                 rc = 4;
1575                 goto lpfc_handle_latt_free_mbuf;
1576         }
1577
1578         /* Clear Link Attention in HA REG */
1579         spin_lock_irq(&phba->hbalock);
1580         writel(HA_LATT, phba->HAregaddr);
1581         readl(phba->HAregaddr); /* flush */
1582         spin_unlock_irq(&phba->hbalock);
1583
1584         return;
1585
1586 lpfc_handle_latt_free_mbuf:
1587         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1588         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1589 lpfc_handle_latt_free_mp:
1590         kfree(mp);
1591 lpfc_handle_latt_free_pmb:
1592         mempool_free(pmb, phba->mbox_mem_pool);
1593 lpfc_handle_latt_err_exit:
1594         /* Enable Link attention interrupts */
1595         spin_lock_irq(&phba->hbalock);
1596         psli->sli_flag |= LPFC_PROCESS_LA;
1597         control = readl(phba->HCregaddr);
1598         control |= HC_LAINT_ENA;
1599         writel(control, phba->HCregaddr);
1600         readl(phba->HCregaddr); /* flush */
1601
1602         /* Clear Link Attention in HA REG */
1603         writel(HA_LATT, phba->HAregaddr);
1604         readl(phba->HAregaddr); /* flush */
1605         spin_unlock_irq(&phba->hbalock);
1606         lpfc_linkdown(phba);
1607         phba->link_state = LPFC_HBA_ERROR;
1608
1609         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1610                      "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1611
1612         return;
1613 }
1614
1615 /**
1616  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1617  * @phba: pointer to lpfc hba data structure.
1618  * @vpd: pointer to the vital product data.
1619  * @len: length of the vital product data in bytes.
1620  *
1621  * This routine parses the Vital Product Data (VPD). The VPD is treated as
1622  * an array of characters. In this routine, the ModelName, ProgramType, and
1623  * ModelDesc, etc. fields of the phba data structure will be populated.
1624  *
1625  * Return codes
1626  *   0 - pointer to the VPD passed in is NULL
1627  *   1 - success
1628  **/
1629 int
1630 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1631 {
1632         uint8_t lenlo, lenhi;
1633         int Length;
1634         int i, j;
1635         int finished = 0;
1636         int index = 0;
1637
1638         if (!vpd)
1639                 return 0;
1640
1641         /* Vital Product */
1642         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1643                         "0455 Vital Product Data: x%x x%x x%x x%x\n",
1644                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1645                         (uint32_t) vpd[3]);
1646         while (!finished && (index < (len - 4))) {
1647                 switch (vpd[index]) {
1648                 case 0x82:
1649                 case 0x91:
1650                         index += 1;
1651                         lenlo = vpd[index];
1652                         index += 1;
1653                         lenhi = vpd[index];
1654                         index += 1;
1655                         i = ((((unsigned short)lenhi) << 8) + lenlo);
1656                         index += i;
1657                         break;
1658                 case 0x90:
1659                         index += 1;
1660                         lenlo = vpd[index];
1661                         index += 1;
1662                         lenhi = vpd[index];
1663                         index += 1;
1664                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
1665                         if (Length > len - index)
1666                                 Length = len - index;
1667                         while (Length > 0) {
1668                         /* Look for Serial Number */
1669                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1670                                 index += 2;
1671                                 i = vpd[index];
1672                                 index += 1;
1673                                 j = 0;
1674                                 Length -= (3+i);
1675                                 while(i--) {
1676                                         phba->SerialNumber[j++] = vpd[index++];
1677                                         if (j == 31)
1678                                                 break;
1679                                 }
1680                                 phba->SerialNumber[j] = 0;
1681                                 continue;
1682                         }
1683                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1684                                 phba->vpd_flag |= VPD_MODEL_DESC;
1685                                 index += 2;
1686                                 i = vpd[index];
1687                                 index += 1;
1688                                 j = 0;
1689                                 Length -= (3+i);
1690                                 while(i--) {
1691                                         phba->ModelDesc[j++] = vpd[index++];
1692                                         if (j == 255)
1693                                                 break;
1694                                 }
1695                                 phba->ModelDesc[j] = 0;
1696                                 continue;
1697                         }
1698                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1699                                 phba->vpd_flag |= VPD_MODEL_NAME;
1700                                 index += 2;
1701                                 i = vpd[index];
1702                                 index += 1;
1703                                 j = 0;
1704                                 Length -= (3+i);
1705                                 while(i--) {
1706                                         phba->ModelName[j++] = vpd[index++];
1707                                         if (j == 79)
1708                                                 break;
1709                                 }
1710                                 phba->ModelName[j] = 0;
1711                                 continue;
1712                         }
1713                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1714                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1715                                 index += 2;
1716                                 i = vpd[index];
1717                                 index += 1;
1718                                 j = 0;
1719                                 Length -= (3+i);
1720                                 while(i--) {
1721                                         phba->ProgramType[j++] = vpd[index++];
1722                                         if (j == 255)
1723                                                 break;
1724                                 }
1725                                 phba->ProgramType[j] = 0;
1726                                 continue;
1727                         }
1728                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1729                                 phba->vpd_flag |= VPD_PORT;
1730                                 index += 2;
1731                                 i = vpd[index];
1732                                 index += 1;
1733                                 j = 0;
1734                                 Length -= (3+i);
1735                                 while(i--) {
1736                                         if ((phba->sli_rev == LPFC_SLI_REV4) &&
1737                                             (phba->sli4_hba.pport_name_sta ==
1738                                              LPFC_SLI4_PPNAME_GET)) {
1739                                                 j++;
1740                                                 index++;
1741                                         } else
1742                                                 phba->Port[j++] = vpd[index++];
1743                                         if (j == 19)
1744                                                 break;
1745                                 }
1746                                 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1747                                     (phba->sli4_hba.pport_name_sta ==
1748                                      LPFC_SLI4_PPNAME_NON))
1749                                         phba->Port[j] = 0;
1750                                 continue;
1751                         }
1752                         else {
1753                                 index += 2;
1754                                 i = vpd[index];
1755                                 index += 1;
1756                                 index += i;
1757                                 Length -= (3 + i);
1758                         }
1759                 }
1760                 finished = 0;
1761                 break;
1762                 case 0x78:
1763                         finished = 1;
1764                         break;
1765                 default:
1766                         index ++;
1767                         break;
1768                 }
1769         }
1770
1771         return(1);
1772 }
1773
1774 /**
1775  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1776  * @phba: pointer to lpfc hba data structure.
1777  * @mdp: pointer to the data structure to hold the derived model name.
1778  * @descp: pointer to the data structure to hold the derived description.
1779  *
1780  * This routine retrieves HBA's description based on its registered PCI device
1781  * ID. The @descp passed into this function points to an array of 256 chars. It
1782  * shall be returned with the model name, maximum speed, and the host bus type.
1783  * The @mdp passed into this function points to an array of 80 chars. When the
1784  * function returns, the @mdp will be filled with the model name.
1785  **/
1786 static void
1787 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1788 {
1789         lpfc_vpd_t *vp;
1790         uint16_t dev_id = phba->pcidev->device;
1791         int max_speed;
1792         int GE = 0;
1793         int oneConnect = 0; /* default is not a oneConnect */
1794         struct {
1795                 char *name;
1796                 char *bus;
1797                 char *function;
1798         } m = {"<Unknown>", "", ""};
1799
1800         if (mdp && mdp[0] != '\0'
1801                 && descp && descp[0] != '\0')
1802                 return;
1803
1804         if (phba->lmt & LMT_16Gb)
1805                 max_speed = 16;
1806         else if (phba->lmt & LMT_10Gb)
1807                 max_speed = 10;
1808         else if (phba->lmt & LMT_8Gb)
1809                 max_speed = 8;
1810         else if (phba->lmt & LMT_4Gb)
1811                 max_speed = 4;
1812         else if (phba->lmt & LMT_2Gb)
1813                 max_speed = 2;
1814         else
1815                 max_speed = 1;
1816
1817         vp = &phba->vpd;
1818
1819         switch (dev_id) {
1820         case PCI_DEVICE_ID_FIREFLY:
1821                 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1822                 break;
1823         case PCI_DEVICE_ID_SUPERFLY:
1824                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1825                         m = (typeof(m)){"LP7000", "PCI",
1826                                         "Fibre Channel Adapter"};
1827                 else
1828                         m = (typeof(m)){"LP7000E", "PCI",
1829                                         "Fibre Channel Adapter"};
1830                 break;
1831         case PCI_DEVICE_ID_DRAGONFLY:
1832                 m = (typeof(m)){"LP8000", "PCI",
1833                                 "Fibre Channel Adapter"};
1834                 break;
1835         case PCI_DEVICE_ID_CENTAUR:
1836                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1837                         m = (typeof(m)){"LP9002", "PCI",
1838                                         "Fibre Channel Adapter"};
1839                 else
1840                         m = (typeof(m)){"LP9000", "PCI",
1841                                         "Fibre Channel Adapter"};
1842                 break;
1843         case PCI_DEVICE_ID_RFLY:
1844                 m = (typeof(m)){"LP952", "PCI",
1845                                 "Fibre Channel Adapter"};
1846                 break;
1847         case PCI_DEVICE_ID_PEGASUS:
1848                 m = (typeof(m)){"LP9802", "PCI-X",
1849                                 "Fibre Channel Adapter"};
1850                 break;
1851         case PCI_DEVICE_ID_THOR:
1852                 m = (typeof(m)){"LP10000", "PCI-X",
1853                                 "Fibre Channel Adapter"};
1854                 break;
1855         case PCI_DEVICE_ID_VIPER:
1856                 m = (typeof(m)){"LPX1000",  "PCI-X",
1857                                 "Fibre Channel Adapter"};
1858                 break;
1859         case PCI_DEVICE_ID_PFLY:
1860                 m = (typeof(m)){"LP982", "PCI-X",
1861                                 "Fibre Channel Adapter"};
1862                 break;
1863         case PCI_DEVICE_ID_TFLY:
1864                 m = (typeof(m)){"LP1050", "PCI-X",
1865                                 "Fibre Channel Adapter"};
1866                 break;
1867         case PCI_DEVICE_ID_HELIOS:
1868                 m = (typeof(m)){"LP11000", "PCI-X2",
1869                                 "Fibre Channel Adapter"};
1870                 break;
1871         case PCI_DEVICE_ID_HELIOS_SCSP:
1872                 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1873                                 "Fibre Channel Adapter"};
1874                 break;
1875         case PCI_DEVICE_ID_HELIOS_DCSP:
1876                 m = (typeof(m)){"LP11002-SP",  "PCI-X2",
1877                                 "Fibre Channel Adapter"};
1878                 break;
1879         case PCI_DEVICE_ID_NEPTUNE:
1880                 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1881                 break;
1882         case PCI_DEVICE_ID_NEPTUNE_SCSP:
1883                 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1884                 break;
1885         case PCI_DEVICE_ID_NEPTUNE_DCSP:
1886                 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1887                 break;
1888         case PCI_DEVICE_ID_BMID:
1889                 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1890                 break;
1891         case PCI_DEVICE_ID_BSMB:
1892                 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1893                 break;
1894         case PCI_DEVICE_ID_ZEPHYR:
1895                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1896                 break;
1897         case PCI_DEVICE_ID_ZEPHYR_SCSP:
1898                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1899                 break;
1900         case PCI_DEVICE_ID_ZEPHYR_DCSP:
1901                 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1902                 GE = 1;
1903                 break;
1904         case PCI_DEVICE_ID_ZMID:
1905                 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1906                 break;
1907         case PCI_DEVICE_ID_ZSMB:
1908                 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1909                 break;
1910         case PCI_DEVICE_ID_LP101:
1911                 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1912                 break;
1913         case PCI_DEVICE_ID_LP10000S:
1914                 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1915                 break;
1916         case PCI_DEVICE_ID_LP11000S:
1917                 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1918                 break;
1919         case PCI_DEVICE_ID_LPE11000S:
1920                 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
1921                 break;
1922         case PCI_DEVICE_ID_SAT:
1923                 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
1924                 break;
1925         case PCI_DEVICE_ID_SAT_MID:
1926                 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
1927                 break;
1928         case PCI_DEVICE_ID_SAT_SMB:
1929                 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
1930                 break;
1931         case PCI_DEVICE_ID_SAT_DCSP:
1932                 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
1933                 break;
1934         case PCI_DEVICE_ID_SAT_SCSP:
1935                 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
1936                 break;
1937         case PCI_DEVICE_ID_SAT_S:
1938                 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
1939                 break;
1940         case PCI_DEVICE_ID_HORNET:
1941                 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
1942                 GE = 1;
1943                 break;
1944         case PCI_DEVICE_ID_PROTEUS_VF:
1945                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1946                                 "Fibre Channel Adapter"};
1947                 break;
1948         case PCI_DEVICE_ID_PROTEUS_PF:
1949                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1950                                 "Fibre Channel Adapter"};
1951                 break;
1952         case PCI_DEVICE_ID_PROTEUS_S:
1953                 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1954                                 "Fibre Channel Adapter"};
1955                 break;
1956         case PCI_DEVICE_ID_TIGERSHARK:
1957                 oneConnect = 1;
1958                 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
1959                 break;
1960         case PCI_DEVICE_ID_TOMCAT:
1961                 oneConnect = 1;
1962                 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1963                 break;
1964         case PCI_DEVICE_ID_FALCON:
1965                 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1966                                 "EmulexSecure Fibre"};
1967                 break;
1968         case PCI_DEVICE_ID_BALIUS:
1969                 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1970                                 "Fibre Channel Adapter"};
1971                 break;
1972         case PCI_DEVICE_ID_LANCER_FC:
1973         case PCI_DEVICE_ID_LANCER_FC_VF:
1974                 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
1975                 break;
1976         case PCI_DEVICE_ID_LANCER_FCOE:
1977         case PCI_DEVICE_ID_LANCER_FCOE_VF:
1978                 oneConnect = 1;
1979                 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
1980                 break;
1981         default:
1982                 m = (typeof(m)){"Unknown", "", ""};
1983                 break;
1984         }
1985
1986         if (mdp && mdp[0] == '\0')
1987                 snprintf(mdp, 79,"%s", m.name);
1988         /*
1989          * oneConnect hba requires special processing, they are all initiators
1990          * and we put the port number on the end
1991          */
1992         if (descp && descp[0] == '\0') {
1993                 if (oneConnect)
1994                         snprintf(descp, 255,
1995                                 "Emulex OneConnect %s, %s Initiator, Port %s",
1996                                 m.name, m.function,
1997                                 phba->Port);
1998                 else
1999                         snprintf(descp, 255,
2000                                 "Emulex %s %d%s %s %s",
2001                                 m.name, max_speed, (GE) ? "GE" : "Gb",
2002                                 m.bus, m.function);
2003         }
2004 }
2005
2006 /**
2007  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
2008  * @phba: pointer to lpfc hba data structure.
2009  * @pring: pointer to a IOCB ring.
2010  * @cnt: the number of IOCBs to be posted to the IOCB ring.
2011  *
2012  * This routine posts a given number of IOCBs with the associated DMA buffer
2013  * descriptors specified by the cnt argument to the given IOCB ring.
2014  *
2015  * Return codes
2016  *   The number of IOCBs NOT able to be posted to the IOCB ring.
2017  **/
2018 int
2019 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2020 {
2021         IOCB_t *icmd;
2022         struct lpfc_iocbq *iocb;
2023         struct lpfc_dmabuf *mp1, *mp2;
2024
2025         cnt += pring->missbufcnt;
2026
2027         /* While there are buffers to post */
2028         while (cnt > 0) {
2029                 /* Allocate buffer for  command iocb */
2030                 iocb = lpfc_sli_get_iocbq(phba);
2031                 if (iocb == NULL) {
2032                         pring->missbufcnt = cnt;
2033                         return cnt;
2034                 }
2035                 icmd = &iocb->iocb;
2036
2037                 /* 2 buffers can be posted per command */
2038                 /* Allocate buffer to post */
2039                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2040                 if (mp1)
2041                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2042                 if (!mp1 || !mp1->virt) {
2043                         kfree(mp1);
2044                         lpfc_sli_release_iocbq(phba, iocb);
2045                         pring->missbufcnt = cnt;
2046                         return cnt;
2047                 }
2048
2049                 INIT_LIST_HEAD(&mp1->list);
2050                 /* Allocate buffer to post */
2051                 if (cnt > 1) {
2052                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2053                         if (mp2)
2054                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2055                                                             &mp2->phys);
2056                         if (!mp2 || !mp2->virt) {
2057                                 kfree(mp2);
2058                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2059                                 kfree(mp1);
2060                                 lpfc_sli_release_iocbq(phba, iocb);
2061                                 pring->missbufcnt = cnt;
2062                                 return cnt;
2063                         }
2064
2065                         INIT_LIST_HEAD(&mp2->list);
2066                 } else {
2067                         mp2 = NULL;
2068                 }
2069
2070                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2071                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2072                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2073                 icmd->ulpBdeCount = 1;
2074                 cnt--;
2075                 if (mp2) {
2076                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2077                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2078                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2079                         cnt--;
2080                         icmd->ulpBdeCount = 2;
2081                 }
2082
2083                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2084                 icmd->ulpLe = 1;
2085
2086                 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2087                     IOCB_ERROR) {
2088                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2089                         kfree(mp1);
2090                         cnt++;
2091                         if (mp2) {
2092                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2093                                 kfree(mp2);
2094                                 cnt++;
2095                         }
2096                         lpfc_sli_release_iocbq(phba, iocb);
2097                         pring->missbufcnt = cnt;
2098                         return cnt;
2099                 }
2100                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2101                 if (mp2)
2102                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2103         }
2104         pring->missbufcnt = 0;
2105         return 0;
2106 }
2107
2108 /**
2109  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2110  * @phba: pointer to lpfc hba data structure.
2111  *
2112  * This routine posts initial receive IOCB buffers to the ELS ring. The
2113  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2114  * set to 64 IOCBs.
2115  *
2116  * Return codes
2117  *   0 - success (currently always success)
2118  **/
2119 static int
2120 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2121 {
2122         struct lpfc_sli *psli = &phba->sli;
2123
2124         /* Ring 0, ELS / CT buffers */
2125         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2126         /* Ring 2 - FCP no buffers needed */
2127
2128         return 0;
2129 }
2130
2131 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2132
2133 /**
2134  * lpfc_sha_init - Set up initial array of hash table entries
2135  * @HashResultPointer: pointer to an array as hash table.
2136  *
2137  * This routine sets up the initial values to the array of hash table entries
2138  * for the LC HBAs.
2139  **/
2140 static void
2141 lpfc_sha_init(uint32_t * HashResultPointer)
2142 {
2143         HashResultPointer[0] = 0x67452301;
2144         HashResultPointer[1] = 0xEFCDAB89;
2145         HashResultPointer[2] = 0x98BADCFE;
2146         HashResultPointer[3] = 0x10325476;
2147         HashResultPointer[4] = 0xC3D2E1F0;
2148 }
2149
2150 /**
2151  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2152  * @HashResultPointer: pointer to an initial/result hash table.
2153  * @HashWorkingPointer: pointer to an working hash table.
2154  *
2155  * This routine iterates an initial hash table pointed by @HashResultPointer
2156  * with the values from the working hash table pointeed by @HashWorkingPointer.
2157  * The results are putting back to the initial hash table, returned through
2158  * the @HashResultPointer as the result hash table.
2159  **/
2160 static void
2161 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2162 {
2163         int t;
2164         uint32_t TEMP;
2165         uint32_t A, B, C, D, E;
2166         t = 16;
2167         do {
2168                 HashWorkingPointer[t] =
2169                     S(1,
2170                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2171                                                                      8] ^
2172                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2173         } while (++t <= 79);
2174         t = 0;
2175         A = HashResultPointer[0];
2176         B = HashResultPointer[1];
2177         C = HashResultPointer[2];
2178         D = HashResultPointer[3];
2179         E = HashResultPointer[4];
2180
2181         do {
2182                 if (t < 20) {
2183                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2184                 } else if (t < 40) {
2185                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2186                 } else if (t < 60) {
2187                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2188                 } else {
2189                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2190                 }
2191                 TEMP += S(5, A) + E + HashWorkingPointer[t];
2192                 E = D;
2193                 D = C;
2194                 C = S(30, B);
2195                 B = A;
2196                 A = TEMP;
2197         } while (++t <= 79);
2198
2199         HashResultPointer[0] += A;
2200         HashResultPointer[1] += B;
2201         HashResultPointer[2] += C;
2202         HashResultPointer[3] += D;
2203         HashResultPointer[4] += E;
2204
2205 }
2206
2207 /**
2208  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2209  * @RandomChallenge: pointer to the entry of host challenge random number array.
2210  * @HashWorking: pointer to the entry of the working hash array.
2211  *
2212  * This routine calculates the working hash array referred by @HashWorking
2213  * from the challenge random numbers associated with the host, referred by
2214  * @RandomChallenge. The result is put into the entry of the working hash
2215  * array and returned by reference through @HashWorking.
2216  **/
2217 static void
2218 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2219 {
2220         *HashWorking = (*RandomChallenge ^ *HashWorking);
2221 }
2222
2223 /**
2224  * lpfc_hba_init - Perform special handling for LC HBA initialization
2225  * @phba: pointer to lpfc hba data structure.
2226  * @hbainit: pointer to an array of unsigned 32-bit integers.
2227  *
2228  * This routine performs the special handling for LC HBA initialization.
2229  **/
2230 void
2231 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2232 {
2233         int t;
2234         uint32_t *HashWorking;
2235         uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2236
2237         HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2238         if (!HashWorking)
2239                 return;
2240
2241         HashWorking[0] = HashWorking[78] = *pwwnn++;
2242         HashWorking[1] = HashWorking[79] = *pwwnn;
2243
2244         for (t = 0; t < 7; t++)
2245                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2246
2247         lpfc_sha_init(hbainit);
2248         lpfc_sha_iterate(hbainit, HashWorking);
2249         kfree(HashWorking);
2250 }
2251
2252 /**
2253  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2254  * @vport: pointer to a virtual N_Port data structure.
2255  *
2256  * This routine performs the necessary cleanups before deleting the @vport.
2257  * It invokes the discovery state machine to perform necessary state
2258  * transitions and to release the ndlps associated with the @vport. Note,
2259  * the physical port is treated as @vport 0.
2260  **/
2261 void
2262 lpfc_cleanup(struct lpfc_vport *vport)
2263 {
2264         struct lpfc_hba   *phba = vport->phba;
2265         struct lpfc_nodelist *ndlp, *next_ndlp;
2266         int i = 0;
2267
2268         if (phba->link_state > LPFC_LINK_DOWN)
2269                 lpfc_port_link_failure(vport);
2270
2271         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2272                 if (!NLP_CHK_NODE_ACT(ndlp)) {
2273                         ndlp = lpfc_enable_node(vport, ndlp,
2274                                                 NLP_STE_UNUSED_NODE);
2275                         if (!ndlp)
2276                                 continue;
2277                         spin_lock_irq(&phba->ndlp_lock);
2278                         NLP_SET_FREE_REQ(ndlp);
2279                         spin_unlock_irq(&phba->ndlp_lock);
2280                         /* Trigger the release of the ndlp memory */
2281                         lpfc_nlp_put(ndlp);
2282                         continue;
2283                 }
2284                 spin_lock_irq(&phba->ndlp_lock);
2285                 if (NLP_CHK_FREE_REQ(ndlp)) {
2286                         /* The ndlp should not be in memory free mode already */
2287                         spin_unlock_irq(&phba->ndlp_lock);
2288                         continue;
2289                 } else
2290                         /* Indicate request for freeing ndlp memory */
2291                         NLP_SET_FREE_REQ(ndlp);
2292                 spin_unlock_irq(&phba->ndlp_lock);
2293
2294                 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2295                     ndlp->nlp_DID == Fabric_DID) {
2296                         /* Just free up ndlp with Fabric_DID for vports */
2297                         lpfc_nlp_put(ndlp);
2298                         continue;
2299                 }
2300
2301                 if (ndlp->nlp_type & NLP_FABRIC)
2302                         lpfc_disc_state_machine(vport, ndlp, NULL,
2303                                         NLP_EVT_DEVICE_RECOVERY);
2304
2305                 lpfc_disc_state_machine(vport, ndlp, NULL,
2306                                              NLP_EVT_DEVICE_RM);
2307
2308         }
2309
2310         /* At this point, ALL ndlp's should be gone
2311          * because of the previous NLP_EVT_DEVICE_RM.
2312          * Lets wait for this to happen, if needed.
2313          */
2314         while (!list_empty(&vport->fc_nodes)) {
2315                 if (i++ > 3000) {
2316                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2317                                 "0233 Nodelist not empty\n");
2318                         list_for_each_entry_safe(ndlp, next_ndlp,
2319                                                 &vport->fc_nodes, nlp_listp) {
2320                                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2321                                                 LOG_NODE,
2322                                                 "0282 did:x%x ndlp:x%p "
2323                                                 "usgmap:x%x refcnt:%d\n",
2324                                                 ndlp->nlp_DID, (void *)ndlp,
2325                                                 ndlp->nlp_usg_map,
2326                                                 atomic_read(
2327                                                         &ndlp->kref.refcount));
2328                         }
2329                         break;
2330                 }
2331
2332                 /* Wait for any activity on ndlps to settle */
2333                 msleep(10);
2334         }
2335         lpfc_cleanup_vports_rrqs(vport, NULL);
2336 }
2337
2338 /**
2339  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2340  * @vport: pointer to a virtual N_Port data structure.
2341  *
2342  * This routine stops all the timers associated with a @vport. This function
2343  * is invoked before disabling or deleting a @vport. Note that the physical
2344  * port is treated as @vport 0.
2345  **/
2346 void
2347 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2348 {
2349         del_timer_sync(&vport->els_tmofunc);
2350         del_timer_sync(&vport->fc_fdmitmo);
2351         del_timer_sync(&vport->delayed_disc_tmo);
2352         lpfc_can_disctmo(vport);
2353         return;
2354 }
2355
2356 /**
2357  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2358  * @phba: pointer to lpfc hba data structure.
2359  *
2360  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2361  * caller of this routine should already hold the host lock.
2362  **/
2363 void
2364 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2365 {
2366         /* Clear pending FCF rediscovery wait flag */
2367         phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2368
2369         /* Now, try to stop the timer */
2370         del_timer(&phba->fcf.redisc_wait);
2371 }
2372
2373 /**
2374  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2375  * @phba: pointer to lpfc hba data structure.
2376  *
2377  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2378  * checks whether the FCF rediscovery wait timer is pending with the host
2379  * lock held before proceeding with disabling the timer and clearing the
2380  * wait timer pendig flag.
2381  **/
2382 void
2383 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2384 {
2385         spin_lock_irq(&phba->hbalock);
2386         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2387                 /* FCF rediscovery timer already fired or stopped */
2388                 spin_unlock_irq(&phba->hbalock);
2389                 return;
2390         }
2391         __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2392         /* Clear failover in progress flags */
2393         phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2394         spin_unlock_irq(&phba->hbalock);
2395 }
2396
2397 /**
2398  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2399  * @phba: pointer to lpfc hba data structure.
2400  *
2401  * This routine stops all the timers associated with a HBA. This function is
2402  * invoked before either putting a HBA offline or unloading the driver.
2403  **/
2404 void
2405 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2406 {
2407         lpfc_stop_vport_timers(phba->pport);
2408         del_timer_sync(&phba->sli.mbox_tmo);
2409         del_timer_sync(&phba->fabric_block_timer);
2410         del_timer_sync(&phba->eratt_poll);
2411         del_timer_sync(&phba->hb_tmofunc);
2412         if (phba->sli_rev == LPFC_SLI_REV4) {
2413                 del_timer_sync(&phba->rrq_tmr);
2414                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2415         }
2416         phba->hb_outstanding = 0;
2417
2418         switch (phba->pci_dev_grp) {
2419         case LPFC_PCI_DEV_LP:
2420                 /* Stop any LightPulse device specific driver timers */
2421                 del_timer_sync(&phba->fcp_poll_timer);
2422                 break;
2423         case LPFC_PCI_DEV_OC:
2424                 /* Stop any OneConnect device sepcific driver timers */
2425                 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2426                 break;
2427         default:
2428                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2429                                 "0297 Invalid device group (x%x)\n",
2430                                 phba->pci_dev_grp);
2431                 break;
2432         }
2433         return;
2434 }
2435
2436 /**
2437  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2438  * @phba: pointer to lpfc hba data structure.
2439  *
2440  * This routine marks a HBA's management interface as blocked. Once the HBA's
2441  * management interface is marked as blocked, all the user space access to
2442  * the HBA, whether they are from sysfs interface or libdfc interface will
2443  * all be blocked. The HBA is set to block the management interface when the
2444  * driver prepares the HBA interface for online or offline.
2445  **/
2446 static void
2447 lpfc_block_mgmt_io(struct lpfc_hba * phba)
2448 {
2449         unsigned long iflag;
2450         uint8_t actcmd = MBX_HEARTBEAT;
2451         unsigned long timeout;
2452
2453         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2454         spin_lock_irqsave(&phba->hbalock, iflag);
2455         phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2456         if (phba->sli.mbox_active) {
2457                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2458                 /* Determine how long we might wait for the active mailbox
2459                  * command to be gracefully completed by firmware.
2460                  */
2461                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2462                                 phba->sli.mbox_active) * 1000) + jiffies;
2463         }
2464         spin_unlock_irqrestore(&phba->hbalock, iflag);
2465
2466         /* Wait for the outstnading mailbox command to complete */
2467         while (phba->sli.mbox_active) {
2468                 /* Check active mailbox complete status every 2ms */
2469                 msleep(2);
2470                 if (time_after(jiffies, timeout)) {
2471                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2472                                 "2813 Mgmt IO is Blocked %x "
2473                                 "- mbox cmd %x still active\n",
2474                                 phba->sli.sli_flag, actcmd);
2475                         break;
2476                 }
2477         }
2478 }
2479
2480 /**
2481  * lpfc_online - Initialize and bring a HBA online
2482  * @phba: pointer to lpfc hba data structure.
2483  *
2484  * This routine initializes the HBA and brings a HBA online. During this
2485  * process, the management interface is blocked to prevent user space access
2486  * to the HBA interfering with the driver initialization.
2487  *
2488  * Return codes
2489  *   0 - successful
2490  *   1 - failed
2491  **/
2492 int
2493 lpfc_online(struct lpfc_hba *phba)
2494 {
2495         struct lpfc_vport *vport;
2496         struct lpfc_vport **vports;
2497         int i;
2498
2499         if (!phba)
2500                 return 0;
2501         vport = phba->pport;
2502
2503         if (!(vport->fc_flag & FC_OFFLINE_MODE))
2504                 return 0;
2505
2506         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2507                         "0458 Bring Adapter online\n");
2508
2509         lpfc_block_mgmt_io(phba);
2510
2511         if (!lpfc_sli_queue_setup(phba)) {
2512                 lpfc_unblock_mgmt_io(phba);
2513                 return 1;
2514         }
2515
2516         if (phba->sli_rev == LPFC_SLI_REV4) {
2517                 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2518                         lpfc_unblock_mgmt_io(phba);
2519                         return 1;
2520                 }
2521         } else {
2522                 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2523                         lpfc_unblock_mgmt_io(phba);
2524                         return 1;
2525                 }
2526         }
2527
2528         vports = lpfc_create_vport_work_array(phba);
2529         if (vports != NULL) {
2530                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2531                         struct Scsi_Host *shost;
2532                         shost = lpfc_shost_from_vport(vports[i]);
2533                         spin_lock_irq(shost->host_lock);
2534                         vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2535                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2536                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2537                         if (phba->sli_rev == LPFC_SLI_REV4)
2538                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2539                         spin_unlock_irq(shost->host_lock);
2540                 }
2541         }
2542         lpfc_destroy_vport_work_array(phba, vports);
2543
2544         lpfc_unblock_mgmt_io(phba);
2545         return 0;
2546 }
2547
2548 /**
2549  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2550  * @phba: pointer to lpfc hba data structure.
2551  *
2552  * This routine marks a HBA's management interface as not blocked. Once the
2553  * HBA's management interface is marked as not blocked, all the user space
2554  * access to the HBA, whether they are from sysfs interface or libdfc
2555  * interface will be allowed. The HBA is set to block the management interface
2556  * when the driver prepares the HBA interface for online or offline and then
2557  * set to unblock the management interface afterwards.
2558  **/
2559 void
2560 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2561 {
2562         unsigned long iflag;
2563
2564         spin_lock_irqsave(&phba->hbalock, iflag);
2565         phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2566         spin_unlock_irqrestore(&phba->hbalock, iflag);
2567 }
2568
2569 /**
2570  * lpfc_offline_prep - Prepare a HBA to be brought offline
2571  * @phba: pointer to lpfc hba data structure.
2572  *
2573  * This routine is invoked to prepare a HBA to be brought offline. It performs
2574  * unregistration login to all the nodes on all vports and flushes the mailbox
2575  * queue to make it ready to be brought offline.
2576  **/
2577 void
2578 lpfc_offline_prep(struct lpfc_hba * phba)
2579 {
2580         struct lpfc_vport *vport = phba->pport;
2581         struct lpfc_nodelist  *ndlp, *next_ndlp;
2582         struct lpfc_vport **vports;
2583         struct Scsi_Host *shost;
2584         int i;
2585
2586         if (vport->fc_flag & FC_OFFLINE_MODE)
2587                 return;
2588
2589         lpfc_block_mgmt_io(phba);
2590
2591         lpfc_linkdown(phba);
2592
2593         /* Issue an unreg_login to all nodes on all vports */
2594         vports = lpfc_create_vport_work_array(phba);
2595         if (vports != NULL) {
2596                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2597                         if (vports[i]->load_flag & FC_UNLOADING)
2598                                 continue;
2599                         shost = lpfc_shost_from_vport(vports[i]);
2600                         spin_lock_irq(shost->host_lock);
2601                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2602                         vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2603                         vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2604                         spin_unlock_irq(shost->host_lock);
2605
2606                         shost = lpfc_shost_from_vport(vports[i]);
2607                         list_for_each_entry_safe(ndlp, next_ndlp,
2608                                                  &vports[i]->fc_nodes,
2609                                                  nlp_listp) {
2610                                 if (!NLP_CHK_NODE_ACT(ndlp))
2611                                         continue;
2612                                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2613                                         continue;
2614                                 if (ndlp->nlp_type & NLP_FABRIC) {
2615                                         lpfc_disc_state_machine(vports[i], ndlp,
2616                                                 NULL, NLP_EVT_DEVICE_RECOVERY);
2617                                         lpfc_disc_state_machine(vports[i], ndlp,
2618                                                 NULL, NLP_EVT_DEVICE_RM);
2619                                 }
2620                                 spin_lock_irq(shost->host_lock);
2621                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2622                                 spin_unlock_irq(shost->host_lock);
2623                                 lpfc_unreg_rpi(vports[i], ndlp);
2624                         }
2625                 }
2626         }
2627         lpfc_destroy_vport_work_array(phba, vports);
2628
2629         lpfc_sli_mbox_sys_shutdown(phba);
2630 }
2631
2632 /**
2633  * lpfc_offline - Bring a HBA offline
2634  * @phba: pointer to lpfc hba data structure.
2635  *
2636  * This routine actually brings a HBA offline. It stops all the timers
2637  * associated with the HBA, brings down the SLI layer, and eventually
2638  * marks the HBA as in offline state for the upper layer protocol.
2639  **/
2640 void
2641 lpfc_offline(struct lpfc_hba *phba)
2642 {
2643         struct Scsi_Host  *shost;
2644         struct lpfc_vport **vports;
2645         int i;
2646
2647         if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2648                 return;
2649
2650         /* stop port and all timers associated with this hba */
2651         lpfc_stop_port(phba);
2652         vports = lpfc_create_vport_work_array(phba);
2653         if (vports != NULL)
2654                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2655                         lpfc_stop_vport_timers(vports[i]);
2656         lpfc_destroy_vport_work_array(phba, vports);
2657         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2658                         "0460 Bring Adapter offline\n");
2659         /* Bring down the SLI Layer and cleanup.  The HBA is offline
2660            now.  */
2661         lpfc_sli_hba_down(phba);
2662         spin_lock_irq(&phba->hbalock);
2663         phba->work_ha = 0;
2664         spin_unlock_irq(&phba->hbalock);
2665         vports = lpfc_create_vport_work_array(phba);
2666         if (vports != NULL)
2667                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2668                         shost = lpfc_shost_from_vport(vports[i]);
2669                         spin_lock_irq(shost->host_lock);
2670                         vports[i]->work_port_events = 0;
2671                         vports[i]->fc_flag |= FC_OFFLINE_MODE;
2672                         spin_unlock_irq(shost->host_lock);
2673                 }
2674         lpfc_destroy_vport_work_array(phba, vports);
2675 }
2676
2677 /**
2678  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2679  * @phba: pointer to lpfc hba data structure.
2680  *
2681  * This routine is to free all the SCSI buffers and IOCBs from the driver
2682  * list back to kernel. It is called from lpfc_pci_remove_one to free
2683  * the internal resources before the device is removed from the system.
2684  *
2685  * Return codes
2686  *   0 - successful (for now, it always returns 0)
2687  **/
2688 static int
2689 lpfc_scsi_free(struct lpfc_hba *phba)
2690 {
2691         struct lpfc_scsi_buf *sb, *sb_next;
2692         struct lpfc_iocbq *io, *io_next;
2693
2694         spin_lock_irq(&phba->hbalock);
2695         /* Release all the lpfc_scsi_bufs maintained by this host. */
2696         spin_lock(&phba->scsi_buf_list_lock);
2697         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2698                 list_del(&sb->list);
2699                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2700                               sb->dma_handle);
2701                 kfree(sb);
2702                 phba->total_scsi_bufs--;
2703         }
2704         spin_unlock(&phba->scsi_buf_list_lock);
2705
2706         /* Release all the lpfc_iocbq entries maintained by this host. */
2707         list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2708                 list_del(&io->list);
2709                 kfree(io);
2710                 phba->total_iocbq_bufs--;
2711         }
2712
2713         spin_unlock_irq(&phba->hbalock);
2714         return 0;
2715 }
2716
2717 /**
2718  * lpfc_create_port - Create an FC port
2719  * @phba: pointer to lpfc hba data structure.
2720  * @instance: a unique integer ID to this FC port.
2721  * @dev: pointer to the device data structure.
2722  *
2723  * This routine creates a FC port for the upper layer protocol. The FC port
2724  * can be created on top of either a physical port or a virtual port provided
2725  * by the HBA. This routine also allocates a SCSI host data structure (shost)
2726  * and associates the FC port created before adding the shost into the SCSI
2727  * layer.
2728  *
2729  * Return codes
2730  *   @vport - pointer to the virtual N_Port data structure.
2731  *   NULL - port create failed.
2732  **/
2733 struct lpfc_vport *
2734 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
2735 {
2736         struct lpfc_vport *vport;
2737         struct Scsi_Host  *shost;
2738         int error = 0;
2739
2740         if (dev != &phba->pcidev->dev)
2741                 shost = scsi_host_alloc(&lpfc_vport_template,
2742                                         sizeof(struct lpfc_vport));
2743         else
2744                 shost = scsi_host_alloc(&lpfc_template,
2745                                         sizeof(struct lpfc_vport));
2746         if (!shost)
2747                 goto out;
2748
2749         vport = (struct lpfc_vport *) shost->hostdata;
2750         vport->phba = phba;
2751         vport->load_flag |= FC_LOADING;
2752         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2753         vport->fc_rscn_flush = 0;
2754
2755         lpfc_get_vport_cfgparam(vport);
2756         shost->unique_id = instance;
2757         shost->max_id = LPFC_MAX_TARGET;
2758         shost->max_lun = vport->cfg_max_luns;
2759         shost->this_id = -1;
2760         shost->max_cmd_len = 16;
2761         if (phba->sli_rev == LPFC_SLI_REV4) {
2762                 shost->dma_boundary =
2763                         phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
2764                 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2765         }
2766
2767         /*
2768          * Set initial can_queue value since 0 is no longer supported and
2769          * scsi_add_host will fail. This will be adjusted later based on the
2770          * max xri value determined in hba setup.
2771          */
2772         shost->can_queue = phba->cfg_hba_queue_depth - 10;
2773         if (dev != &phba->pcidev->dev) {
2774                 shost->transportt = lpfc_vport_transport_template;
2775                 vport->port_type = LPFC_NPIV_PORT;
2776         } else {
2777                 shost->transportt = lpfc_transport_template;
2778                 vport->port_type = LPFC_PHYSICAL_PORT;
2779         }
2780
2781         /* Initialize all internally managed lists. */
2782         INIT_LIST_HEAD(&vport->fc_nodes);
2783         INIT_LIST_HEAD(&vport->rcv_buffer_list);
2784         spin_lock_init(&vport->work_port_lock);
2785
2786         init_timer(&vport->fc_disctmo);
2787         vport->fc_disctmo.function = lpfc_disc_timeout;
2788         vport->fc_disctmo.data = (unsigned long)vport;
2789
2790         init_timer(&vport->fc_fdmitmo);
2791         vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2792         vport->fc_fdmitmo.data = (unsigned long)vport;
2793
2794         init_timer(&vport->els_tmofunc);
2795         vport->els_tmofunc.function = lpfc_els_timeout;
2796         vport->els_tmofunc.data = (unsigned long)vport;
2797
2798         init_timer(&vport->delayed_disc_tmo);
2799         vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
2800         vport->delayed_disc_tmo.data = (unsigned long)vport;
2801
2802         error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2803         if (error)
2804                 goto out_put_shost;
2805
2806         spin_lock_irq(&phba->hbalock);
2807         list_add_tail(&vport->listentry, &phba->port_list);
2808         spin_unlock_irq(&phba->hbalock);
2809         return vport;
2810
2811 out_put_shost:
2812         scsi_host_put(shost);
2813 out:
2814         return NULL;
2815 }
2816
2817 /**
2818  * destroy_port -  destroy an FC port
2819  * @vport: pointer to an lpfc virtual N_Port data structure.
2820  *
2821  * This routine destroys a FC port from the upper layer protocol. All the
2822  * resources associated with the port are released.
2823  **/
2824 void
2825 destroy_port(struct lpfc_vport *vport)
2826 {
2827         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2828         struct lpfc_hba  *phba = vport->phba;
2829
2830         lpfc_debugfs_terminate(vport);
2831         fc_remove_host(shost);
2832         scsi_remove_host(shost);
2833
2834         spin_lock_irq(&phba->hbalock);
2835         list_del_init(&vport->listentry);
2836         spin_unlock_irq(&phba->hbalock);
2837
2838         lpfc_cleanup(vport);
2839         return;
2840 }
2841
2842 /**
2843  * lpfc_get_instance - Get a unique integer ID
2844  *
2845  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2846  * uses the kernel idr facility to perform the task.
2847  *
2848  * Return codes:
2849  *   instance - a unique integer ID allocated as the new instance.
2850  *   -1 - lpfc get instance failed.
2851  **/
2852 int
2853 lpfc_get_instance(void)
2854 {
2855         int instance = 0;
2856
2857         /* Assign an unused number */
2858         if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2859                 return -1;
2860         if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2861                 return -1;
2862         return instance;
2863 }
2864
2865 /**
2866  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
2867  * @shost: pointer to SCSI host data structure.
2868  * @time: elapsed time of the scan in jiffies.
2869  *
2870  * This routine is called by the SCSI layer with a SCSI host to determine
2871  * whether the scan host is finished.
2872  *
2873  * Note: there is no scan_start function as adapter initialization will have
2874  * asynchronously kicked off the link initialization.
2875  *
2876  * Return codes
2877  *   0 - SCSI host scan is not over yet.
2878  *   1 - SCSI host scan is over.
2879  **/
2880 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2881 {
2882         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2883         struct lpfc_hba   *phba = vport->phba;
2884         int stat = 0;
2885
2886         spin_lock_irq(shost->host_lock);
2887
2888         if (vport->load_flag & FC_UNLOADING) {
2889                 stat = 1;
2890                 goto finished;
2891         }
2892         if (time >= 30 * HZ) {
2893                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2894                                 "0461 Scanning longer than 30 "
2895                                 "seconds.  Continuing initialization\n");
2896                 stat = 1;
2897                 goto finished;
2898         }
2899         if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2900                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2901                                 "0465 Link down longer than 15 "
2902                                 "seconds.  Continuing initialization\n");
2903                 stat = 1;
2904                 goto finished;
2905         }
2906
2907         if (vport->port_state != LPFC_VPORT_READY)
2908                 goto finished;
2909         if (vport->num_disc_nodes || vport->fc_prli_sent)
2910                 goto finished;
2911         if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2912                 goto finished;
2913         if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2914                 goto finished;
2915
2916         stat = 1;
2917
2918 finished:
2919         spin_unlock_irq(shost->host_lock);
2920         return stat;
2921 }
2922
2923 /**
2924  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
2925  * @shost: pointer to SCSI host data structure.
2926  *
2927  * This routine initializes a given SCSI host attributes on a FC port. The
2928  * SCSI host can be either on top of a physical port or a virtual port.
2929  **/
2930 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2931 {
2932         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2933         struct lpfc_hba   *phba = vport->phba;
2934         /*
2935          * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
2936          */
2937
2938         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2939         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2940         fc_host_supported_classes(shost) = FC_COS_CLASS3;
2941
2942         memset(fc_host_supported_fc4s(shost), 0,
2943                sizeof(fc_host_supported_fc4s(shost)));
2944         fc_host_supported_fc4s(shost)[2] = 1;
2945         fc_host_supported_fc4s(shost)[7] = 1;
2946
2947         lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2948                                  sizeof fc_host_symbolic_name(shost));
2949
2950         fc_host_supported_speeds(shost) = 0;
2951         if (phba->lmt & LMT_16Gb)
2952                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
2953         if (phba->lmt & LMT_10Gb)
2954                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2955         if (phba->lmt & LMT_8Gb)
2956                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2957         if (phba->lmt & LMT_4Gb)
2958                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2959         if (phba->lmt & LMT_2Gb)
2960                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2961         if (phba->lmt & LMT_1Gb)
2962                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2963
2964         fc_host_maxframe_size(shost) =
2965                 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2966                 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2967
2968         fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
2969
2970         /* This value is also unchanging */
2971         memset(fc_host_active_fc4s(shost), 0,
2972                sizeof(fc_host_active_fc4s(shost)));
2973         fc_host_active_fc4s(shost)[2] = 1;
2974         fc_host_active_fc4s(shost)[7] = 1;
2975
2976         fc_host_max_npiv_vports(shost) = phba->max_vpi;
2977         spin_lock_irq(shost->host_lock);
2978         vport->load_flag &= ~FC_LOADING;
2979         spin_unlock_irq(shost->host_lock);
2980 }
2981
2982 /**
2983  * lpfc_stop_port_s3 - Stop SLI3 device port
2984  * @phba: pointer to lpfc hba data structure.
2985  *
2986  * This routine is invoked to stop an SLI3 device port, it&nbs