c1c36812c3d29938805569ae9aacfeb8b202dccd
[muen/linux.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) 2017-2018 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/idr.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/kthread.h>
31 #include <linux/pci.h>
32 #include <linux/spinlock.h>
33 #include <linux/ctype.h>
34 #include <linux/aer.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <linux/miscdevice.h>
38 #include <linux/percpu.h>
39 #include <linux/msi.h>
40 #include <linux/bitops.h>
41
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_host.h>
45 #include <scsi/scsi_transport_fc.h>
46 #include <scsi/scsi_tcq.h>
47 #include <scsi/fc/fc_fs.h>
48
49 #include <linux/nvme-fc-driver.h>
50
51 #include "lpfc_hw4.h"
52 #include "lpfc_hw.h"
53 #include "lpfc_sli.h"
54 #include "lpfc_sli4.h"
55 #include "lpfc_nl.h"
56 #include "lpfc_disc.h"
57 #include "lpfc.h"
58 #include "lpfc_scsi.h"
59 #include "lpfc_nvme.h"
60 #include "lpfc_nvmet.h"
61 #include "lpfc_logmsg.h"
62 #include "lpfc_crtn.h"
63 #include "lpfc_vport.h"
64 #include "lpfc_version.h"
65 #include "lpfc_ids.h"
66
67 char *_dump_buf_data;
68 unsigned long _dump_buf_data_order;
69 char *_dump_buf_dif;
70 unsigned long _dump_buf_dif_order;
71 spinlock_t _dump_buf_lock;
72
73 /* Used when mapping IRQ vectors in a driver centric manner */
74 uint16_t *lpfc_used_cpu;
75 uint32_t lpfc_present_cpu;
76
77 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
78 static int lpfc_post_rcv_buf(struct lpfc_hba *);
79 static int lpfc_sli4_queue_verify(struct lpfc_hba *);
80 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
81 static int lpfc_setup_endian_order(struct lpfc_hba *);
82 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
83 static void lpfc_free_els_sgl_list(struct lpfc_hba *);
84 static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
85 static void lpfc_init_sgl_list(struct lpfc_hba *);
86 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
87 static void lpfc_free_active_sgl(struct lpfc_hba *);
88 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
89 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
90 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
91 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
92 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
93 static void lpfc_sli4_disable_intr(struct lpfc_hba *);
94 static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
95 static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
96
97 static struct scsi_transport_template *lpfc_transport_template = NULL;
98 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
99 static DEFINE_IDR(lpfc_hba_index);
100 #define LPFC_NVMET_BUF_POST 254
101
102 /**
103  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
104  * @phba: pointer to lpfc hba data structure.
105  *
106  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
107  * mailbox command. It retrieves the revision information from the HBA and
108  * collects the Vital Product Data (VPD) about the HBA for preparing the
109  * configuration of the HBA.
110  *
111  * Return codes:
112  *   0 - success.
113  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
114  *   Any other value - indicates an error.
115  **/
116 int
117 lpfc_config_port_prep(struct lpfc_hba *phba)
118 {
119         lpfc_vpd_t *vp = &phba->vpd;
120         int i = 0, rc;
121         LPFC_MBOXQ_t *pmb;
122         MAILBOX_t *mb;
123         char *lpfc_vpd_data = NULL;
124         uint16_t offset = 0;
125         static char licensed[56] =
126                     "key unlock for use with gnu public licensed code only\0";
127         static int init_key = 1;
128
129         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
130         if (!pmb) {
131                 phba->link_state = LPFC_HBA_ERROR;
132                 return -ENOMEM;
133         }
134
135         mb = &pmb->u.mb;
136         phba->link_state = LPFC_INIT_MBX_CMDS;
137
138         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
139                 if (init_key) {
140                         uint32_t *ptext = (uint32_t *) licensed;
141
142                         for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
143                                 *ptext = cpu_to_be32(*ptext);
144                         init_key = 0;
145                 }
146
147                 lpfc_read_nv(phba, pmb);
148                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
149                         sizeof (mb->un.varRDnvp.rsvd3));
150                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
151                          sizeof (licensed));
152
153                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154
155                 if (rc != MBX_SUCCESS) {
156                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
157                                         "0324 Config Port initialization "
158                                         "error, mbxCmd x%x READ_NVPARM, "
159                                         "mbxStatus x%x\n",
160                                         mb->mbxCommand, mb->mbxStatus);
161                         mempool_free(pmb, phba->mbox_mem_pool);
162                         return -ERESTART;
163                 }
164                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
165                        sizeof(phba->wwnn));
166                 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
167                        sizeof(phba->wwpn));
168         }
169
170         /*
171          * Clear all option bits except LPFC_SLI3_BG_ENABLED,
172          * which was already set in lpfc_get_cfgparam()
173          */
174         phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
175
176         /* Setup and issue mailbox READ REV command */
177         lpfc_read_rev(phba, pmb);
178         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
179         if (rc != MBX_SUCCESS) {
180                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
181                                 "0439 Adapter failed to init, mbxCmd x%x "
182                                 "READ_REV, mbxStatus x%x\n",
183                                 mb->mbxCommand, mb->mbxStatus);
184                 mempool_free( pmb, phba->mbox_mem_pool);
185                 return -ERESTART;
186         }
187
188
189         /*
190          * The value of rr must be 1 since the driver set the cv field to 1.
191          * This setting requires the FW to set all revision fields.
192          */
193         if (mb->un.varRdRev.rr == 0) {
194                 vp->rev.rBit = 0;
195                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
196                                 "0440 Adapter failed to init, READ_REV has "
197                                 "missing revision information.\n");
198                 mempool_free(pmb, phba->mbox_mem_pool);
199                 return -ERESTART;
200         }
201
202         if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
203                 mempool_free(pmb, phba->mbox_mem_pool);
204                 return -EINVAL;
205         }
206
207         /* Save information as VPD data */
208         vp->rev.rBit = 1;
209         memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
210         vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
211         memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
212         vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
213         memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
214         vp->rev.biuRev = mb->un.varRdRev.biuRev;
215         vp->rev.smRev = mb->un.varRdRev.smRev;
216         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
217         vp->rev.endecRev = mb->un.varRdRev.endecRev;
218         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
219         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
220         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
221         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
222         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
223         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
224
225         /* If the sli feature level is less then 9, we must
226          * tear down all RPIs and VPIs on link down if NPIV
227          * is enabled.
228          */
229         if (vp->rev.feaLevelHigh < 9)
230                 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
231
232         if (lpfc_is_LC_HBA(phba->pcidev->device))
233                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
234                                                 sizeof (phba->RandomData));
235
236         /* Get adapter VPD information */
237         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
238         if (!lpfc_vpd_data)
239                 goto out_free_mbox;
240         do {
241                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
242                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
243
244                 if (rc != MBX_SUCCESS) {
245                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
246                                         "0441 VPD not present on adapter, "
247                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
248                                         mb->mbxCommand, mb->mbxStatus);
249                         mb->un.varDmp.word_cnt = 0;
250                 }
251                 /* dump mem may return a zero when finished or we got a
252                  * mailbox error, either way we are done.
253                  */
254                 if (mb->un.varDmp.word_cnt == 0)
255                         break;
256                 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
257                         mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
258                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
259                                       lpfc_vpd_data + offset,
260                                       mb->un.varDmp.word_cnt);
261                 offset += mb->un.varDmp.word_cnt;
262         } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
263         lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
264
265         kfree(lpfc_vpd_data);
266 out_free_mbox:
267         mempool_free(pmb, phba->mbox_mem_pool);
268         return 0;
269 }
270
271 /**
272  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
273  * @phba: pointer to lpfc hba data structure.
274  * @pmboxq: pointer to the driver internal queue element for mailbox command.
275  *
276  * This is the completion handler for driver's configuring asynchronous event
277  * mailbox command to the device. If the mailbox command returns successfully,
278  * it will set internal async event support flag to 1; otherwise, it will
279  * set internal async event support flag to 0.
280  **/
281 static void
282 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
283 {
284         if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
285                 phba->temp_sensor_support = 1;
286         else
287                 phba->temp_sensor_support = 0;
288         mempool_free(pmboxq, phba->mbox_mem_pool);
289         return;
290 }
291
292 /**
293  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
294  * @phba: pointer to lpfc hba data structure.
295  * @pmboxq: pointer to the driver internal queue element for mailbox command.
296  *
297  * This is the completion handler for dump mailbox command for getting
298  * wake up parameters. When this command complete, the response contain
299  * Option rom version of the HBA. This function translate the version number
300  * into a human readable string and store it in OptionROMVersion.
301  **/
302 static void
303 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
304 {
305         struct prog_id *prg;
306         uint32_t prog_id_word;
307         char dist = ' ';
308         /* character array used for decoding dist type. */
309         char dist_char[] = "nabx";
310
311         if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
312                 mempool_free(pmboxq, phba->mbox_mem_pool);
313                 return;
314         }
315
316         prg = (struct prog_id *) &prog_id_word;
317
318         /* word 7 contain option rom version */
319         prog_id_word = pmboxq->u.mb.un.varWords[7];
320
321         /* Decode the Option rom version word to a readable string */
322         if (prg->dist < 4)
323                 dist = dist_char[prg->dist];
324
325         if ((prg->dist == 3) && (prg->num == 0))
326                 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
327                         prg->ver, prg->rev, prg->lev);
328         else
329                 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
330                         prg->ver, prg->rev, prg->lev,
331                         dist, prg->num);
332         mempool_free(pmboxq, phba->mbox_mem_pool);
333         return;
334 }
335
336 /**
337  * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
338  *      cfg_soft_wwnn, cfg_soft_wwpn
339  * @vport: pointer to lpfc vport data structure.
340  *
341  *
342  * Return codes
343  *   None.
344  **/
345 void
346 lpfc_update_vport_wwn(struct lpfc_vport *vport)
347 {
348         uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
349         u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
350
351         /* If the soft name exists then update it using the service params */
352         if (vport->phba->cfg_soft_wwnn)
353                 u64_to_wwn(vport->phba->cfg_soft_wwnn,
354                            vport->fc_sparam.nodeName.u.wwn);
355         if (vport->phba->cfg_soft_wwpn)
356                 u64_to_wwn(vport->phba->cfg_soft_wwpn,
357                            vport->fc_sparam.portName.u.wwn);
358
359         /*
360          * If the name is empty or there exists a soft name
361          * then copy the service params name, otherwise use the fc name
362          */
363         if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
364                 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
365                         sizeof(struct lpfc_name));
366         else
367                 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
368                         sizeof(struct lpfc_name));
369
370         /*
371          * If the port name has changed, then set the Param changes flag
372          * to unreg the login
373          */
374         if (vport->fc_portname.u.wwn[0] != 0 &&
375                 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
376                         sizeof(struct lpfc_name)))
377                 vport->vport_flag |= FAWWPN_PARAM_CHG;
378
379         if (vport->fc_portname.u.wwn[0] == 0 ||
380             vport->phba->cfg_soft_wwpn ||
381             (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
382             vport->vport_flag & FAWWPN_SET) {
383                 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
384                         sizeof(struct lpfc_name));
385                 vport->vport_flag &= ~FAWWPN_SET;
386                 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
387                         vport->vport_flag |= FAWWPN_SET;
388         }
389         else
390                 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
391                         sizeof(struct lpfc_name));
392 }
393
394 /**
395  * lpfc_config_port_post - Perform lpfc initialization after config port
396  * @phba: pointer to lpfc hba data structure.
397  *
398  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
399  * command call. It performs all internal resource and state setups on the
400  * port: post IOCB buffers, enable appropriate host interrupt attentions,
401  * ELS ring timers, etc.
402  *
403  * Return codes
404  *   0 - success.
405  *   Any other value - error.
406  **/
407 int
408 lpfc_config_port_post(struct lpfc_hba *phba)
409 {
410         struct lpfc_vport *vport = phba->pport;
411         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
412         LPFC_MBOXQ_t *pmb;
413         MAILBOX_t *mb;
414         struct lpfc_dmabuf *mp;
415         struct lpfc_sli *psli = &phba->sli;
416         uint32_t status, timeout;
417         int i, j;
418         int rc;
419
420         spin_lock_irq(&phba->hbalock);
421         /*
422          * If the Config port completed correctly the HBA is not
423          * over heated any more.
424          */
425         if (phba->over_temp_state == HBA_OVER_TEMP)
426                 phba->over_temp_state = HBA_NORMAL_TEMP;
427         spin_unlock_irq(&phba->hbalock);
428
429         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
430         if (!pmb) {
431                 phba->link_state = LPFC_HBA_ERROR;
432                 return -ENOMEM;
433         }
434         mb = &pmb->u.mb;
435
436         /* Get login parameters for NID.  */
437         rc = lpfc_read_sparam(phba, pmb, 0);
438         if (rc) {
439                 mempool_free(pmb, phba->mbox_mem_pool);
440                 return -ENOMEM;
441         }
442
443         pmb->vport = vport;
444         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
445                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
446                                 "0448 Adapter failed init, mbxCmd x%x "
447                                 "READ_SPARM mbxStatus x%x\n",
448                                 mb->mbxCommand, mb->mbxStatus);
449                 phba->link_state = LPFC_HBA_ERROR;
450                 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
451                 mempool_free(pmb, phba->mbox_mem_pool);
452                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
453                 kfree(mp);
454                 return -EIO;
455         }
456
457         mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
458
459         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
460         lpfc_mbuf_free(phba, mp->virt, mp->phys);
461         kfree(mp);
462         pmb->ctx_buf = NULL;
463         lpfc_update_vport_wwn(vport);
464
465         /* Update the fc_host data structures with new wwn. */
466         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
467         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
468         fc_host_max_npiv_vports(shost) = phba->max_vpi;
469
470         /* If no serial number in VPD data, use low 6 bytes of WWNN */
471         /* This should be consolidated into parse_vpd ? - mr */
472         if (phba->SerialNumber[0] == 0) {
473                 uint8_t *outptr;
474
475                 outptr = &vport->fc_nodename.u.s.IEEE[0];
476                 for (i = 0; i < 12; i++) {
477                         status = *outptr++;
478                         j = ((status & 0xf0) >> 4);
479                         if (j <= 9)
480                                 phba->SerialNumber[i] =
481                                     (char)((uint8_t) 0x30 + (uint8_t) j);
482                         else
483                                 phba->SerialNumber[i] =
484                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
485                         i++;
486                         j = (status & 0xf);
487                         if (j <= 9)
488                                 phba->SerialNumber[i] =
489                                     (char)((uint8_t) 0x30 + (uint8_t) j);
490                         else
491                                 phba->SerialNumber[i] =
492                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
493                 }
494         }
495
496         lpfc_read_config(phba, pmb);
497         pmb->vport = vport;
498         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
499                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
500                                 "0453 Adapter failed to init, mbxCmd x%x "
501                                 "READ_CONFIG, mbxStatus x%x\n",
502                                 mb->mbxCommand, mb->mbxStatus);
503                 phba->link_state = LPFC_HBA_ERROR;
504                 mempool_free( pmb, phba->mbox_mem_pool);
505                 return -EIO;
506         }
507
508         /* Check if the port is disabled */
509         lpfc_sli_read_link_ste(phba);
510
511         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
512         i = (mb->un.varRdConfig.max_xri + 1);
513         if (phba->cfg_hba_queue_depth > i) {
514                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
515                                 "3359 HBA queue depth changed from %d to %d\n",
516                                 phba->cfg_hba_queue_depth, i);
517                 phba->cfg_hba_queue_depth = i;
518         }
519
520         /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
521         i = (mb->un.varRdConfig.max_xri >> 3);
522         if (phba->pport->cfg_lun_queue_depth > i) {
523                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
524                                 "3360 LUN queue depth changed from %d to %d\n",
525                                 phba->pport->cfg_lun_queue_depth, i);
526                 phba->pport->cfg_lun_queue_depth = i;
527         }
528
529         phba->lmt = mb->un.varRdConfig.lmt;
530
531         /* Get the default values for Model Name and Description */
532         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
533
534         phba->link_state = LPFC_LINK_DOWN;
535
536         /* Only process IOCBs on ELS ring till hba_state is READY */
537         if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
538                 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
539         if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
540                 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
541
542         /* Post receive buffers for desired rings */
543         if (phba->sli_rev != 3)
544                 lpfc_post_rcv_buf(phba);
545
546         /*
547          * Configure HBA MSI-X attention conditions to messages if MSI-X mode
548          */
549         if (phba->intr_type == MSIX) {
550                 rc = lpfc_config_msi(phba, pmb);
551                 if (rc) {
552                         mempool_free(pmb, phba->mbox_mem_pool);
553                         return -EIO;
554                 }
555                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
556                 if (rc != MBX_SUCCESS) {
557                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
558                                         "0352 Config MSI mailbox command "
559                                         "failed, mbxCmd x%x, mbxStatus x%x\n",
560                                         pmb->u.mb.mbxCommand,
561                                         pmb->u.mb.mbxStatus);
562                         mempool_free(pmb, phba->mbox_mem_pool);
563                         return -EIO;
564                 }
565         }
566
567         spin_lock_irq(&phba->hbalock);
568         /* Initialize ERATT handling flag */
569         phba->hba_flag &= ~HBA_ERATT_HANDLED;
570
571         /* Enable appropriate host interrupts */
572         if (lpfc_readl(phba->HCregaddr, &status)) {
573                 spin_unlock_irq(&phba->hbalock);
574                 return -EIO;
575         }
576         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
577         if (psli->num_rings > 0)
578                 status |= HC_R0INT_ENA;
579         if (psli->num_rings > 1)
580                 status |= HC_R1INT_ENA;
581         if (psli->num_rings > 2)
582                 status |= HC_R2INT_ENA;
583         if (psli->num_rings > 3)
584                 status |= HC_R3INT_ENA;
585
586         if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
587             (phba->cfg_poll & DISABLE_FCP_RING_INT))
588                 status &= ~(HC_R0INT_ENA);
589
590         writel(status, phba->HCregaddr);
591         readl(phba->HCregaddr); /* flush */
592         spin_unlock_irq(&phba->hbalock);
593
594         /* Set up ring-0 (ELS) timer */
595         timeout = phba->fc_ratov * 2;
596         mod_timer(&vport->els_tmofunc,
597                   jiffies + msecs_to_jiffies(1000 * timeout));
598         /* Set up heart beat (HB) timer */
599         mod_timer(&phba->hb_tmofunc,
600                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
601         phba->hb_outstanding = 0;
602         phba->last_completion_time = jiffies;
603         /* Set up error attention (ERATT) polling timer */
604         mod_timer(&phba->eratt_poll,
605                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
606
607         if (phba->hba_flag & LINK_DISABLED) {
608                 lpfc_printf_log(phba,
609                         KERN_ERR, LOG_INIT,
610                         "2598 Adapter Link is disabled.\n");
611                 lpfc_down_link(phba, pmb);
612                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
613                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
614                 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
615                         lpfc_printf_log(phba,
616                         KERN_ERR, LOG_INIT,
617                         "2599 Adapter failed to issue DOWN_LINK"
618                         " mbox command rc 0x%x\n", rc);
619
620                         mempool_free(pmb, phba->mbox_mem_pool);
621                         return -EIO;
622                 }
623         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
624                 mempool_free(pmb, phba->mbox_mem_pool);
625                 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
626                 if (rc)
627                         return rc;
628         }
629         /* MBOX buffer will be freed in mbox compl */
630         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
631         if (!pmb) {
632                 phba->link_state = LPFC_HBA_ERROR;
633                 return -ENOMEM;
634         }
635
636         lpfc_config_async(phba, pmb, LPFC_ELS_RING);
637         pmb->mbox_cmpl = lpfc_config_async_cmpl;
638         pmb->vport = phba->pport;
639         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
640
641         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
642                 lpfc_printf_log(phba,
643                                 KERN_ERR,
644                                 LOG_INIT,
645                                 "0456 Adapter failed to issue "
646                                 "ASYNCEVT_ENABLE mbox status x%x\n",
647                                 rc);
648                 mempool_free(pmb, phba->mbox_mem_pool);
649         }
650
651         /* Get Option rom version */
652         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
653         if (!pmb) {
654                 phba->link_state = LPFC_HBA_ERROR;
655                 return -ENOMEM;
656         }
657
658         lpfc_dump_wakeup_param(phba, pmb);
659         pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
660         pmb->vport = phba->pport;
661         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
662
663         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
664                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
665                                 "to get Option ROM version status x%x\n", rc);
666                 mempool_free(pmb, phba->mbox_mem_pool);
667         }
668
669         return 0;
670 }
671
672 /**
673  * lpfc_hba_init_link - Initialize the FC link
674  * @phba: pointer to lpfc hba data structure.
675  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
676  *
677  * This routine will issue the INIT_LINK mailbox command call.
678  * It is available to other drivers through the lpfc_hba data
679  * structure for use as a delayed link up mechanism with the
680  * module parameter lpfc_suppress_link_up.
681  *
682  * Return code
683  *              0 - success
684  *              Any other value - error
685  **/
686 static int
687 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
688 {
689         return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
690 }
691
692 /**
693  * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
694  * @phba: pointer to lpfc hba data structure.
695  * @fc_topology: desired fc topology.
696  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
697  *
698  * This routine will issue the INIT_LINK mailbox command call.
699  * It is available to other drivers through the lpfc_hba data
700  * structure for use as a delayed link up mechanism with the
701  * module parameter lpfc_suppress_link_up.
702  *
703  * Return code
704  *              0 - success
705  *              Any other value - error
706  **/
707 int
708 lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
709                                uint32_t flag)
710 {
711         struct lpfc_vport *vport = phba->pport;
712         LPFC_MBOXQ_t *pmb;
713         MAILBOX_t *mb;
714         int rc;
715
716         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
717         if (!pmb) {
718                 phba->link_state = LPFC_HBA_ERROR;
719                 return -ENOMEM;
720         }
721         mb = &pmb->u.mb;
722         pmb->vport = vport;
723
724         if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
725             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
726              !(phba->lmt & LMT_1Gb)) ||
727             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
728              !(phba->lmt & LMT_2Gb)) ||
729             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
730              !(phba->lmt & LMT_4Gb)) ||
731             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
732              !(phba->lmt & LMT_8Gb)) ||
733             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
734              !(phba->lmt & LMT_10Gb)) ||
735             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
736              !(phba->lmt & LMT_16Gb)) ||
737             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
738              !(phba->lmt & LMT_32Gb)) ||
739             ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
740              !(phba->lmt & LMT_64Gb))) {
741                 /* Reset link speed to auto */
742                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
743                         "1302 Invalid speed for this board:%d "
744                         "Reset link speed to auto.\n",
745                         phba->cfg_link_speed);
746                         phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
747         }
748         lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
749         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
750         if (phba->sli_rev < LPFC_SLI_REV4)
751                 lpfc_set_loopback_flag(phba);
752         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
753         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
754                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
755                         "0498 Adapter failed to init, mbxCmd x%x "
756                         "INIT_LINK, mbxStatus x%x\n",
757                         mb->mbxCommand, mb->mbxStatus);
758                 if (phba->sli_rev <= LPFC_SLI_REV3) {
759                         /* Clear all interrupt enable conditions */
760                         writel(0, phba->HCregaddr);
761                         readl(phba->HCregaddr); /* flush */
762                         /* Clear all pending interrupts */
763                         writel(0xffffffff, phba->HAregaddr);
764                         readl(phba->HAregaddr); /* flush */
765                 }
766                 phba->link_state = LPFC_HBA_ERROR;
767                 if (rc != MBX_BUSY || flag == MBX_POLL)
768                         mempool_free(pmb, phba->mbox_mem_pool);
769                 return -EIO;
770         }
771         phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
772         if (flag == MBX_POLL)
773                 mempool_free(pmb, phba->mbox_mem_pool);
774
775         return 0;
776 }
777
778 /**
779  * lpfc_hba_down_link - this routine downs the FC link
780  * @phba: pointer to lpfc hba data structure.
781  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
782  *
783  * This routine will issue the DOWN_LINK mailbox command call.
784  * It is available to other drivers through the lpfc_hba data
785  * structure for use to stop the link.
786  *
787  * Return code
788  *              0 - success
789  *              Any other value - error
790  **/
791 static int
792 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
793 {
794         LPFC_MBOXQ_t *pmb;
795         int rc;
796
797         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
798         if (!pmb) {
799                 phba->link_state = LPFC_HBA_ERROR;
800                 return -ENOMEM;
801         }
802
803         lpfc_printf_log(phba,
804                 KERN_ERR, LOG_INIT,
805                 "0491 Adapter Link is disabled.\n");
806         lpfc_down_link(phba, pmb);
807         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
808         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
809         if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
810                 lpfc_printf_log(phba,
811                 KERN_ERR, LOG_INIT,
812                 "2522 Adapter failed to issue DOWN_LINK"
813                 " mbox command rc 0x%x\n", rc);
814
815                 mempool_free(pmb, phba->mbox_mem_pool);
816                 return -EIO;
817         }
818         if (flag == MBX_POLL)
819                 mempool_free(pmb, phba->mbox_mem_pool);
820
821         return 0;
822 }
823
824 /**
825  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
826  * @phba: pointer to lpfc HBA data structure.
827  *
828  * This routine will do LPFC uninitialization before the HBA is reset when
829  * bringing down the SLI Layer.
830  *
831  * Return codes
832  *   0 - success.
833  *   Any other value - error.
834  **/
835 int
836 lpfc_hba_down_prep(struct lpfc_hba *phba)
837 {
838         struct lpfc_vport **vports;
839         int i;
840
841         if (phba->sli_rev <= LPFC_SLI_REV3) {
842                 /* Disable interrupts */
843                 writel(0, phba->HCregaddr);
844                 readl(phba->HCregaddr); /* flush */
845         }
846
847         if (phba->pport->load_flag & FC_UNLOADING)
848                 lpfc_cleanup_discovery_resources(phba->pport);
849         else {
850                 vports = lpfc_create_vport_work_array(phba);
851                 if (vports != NULL)
852                         for (i = 0; i <= phba->max_vports &&
853                                 vports[i] != NULL; i++)
854                                 lpfc_cleanup_discovery_resources(vports[i]);
855                 lpfc_destroy_vport_work_array(phba, vports);
856         }
857         return 0;
858 }
859
860 /**
861  * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
862  * rspiocb which got deferred
863  *
864  * @phba: pointer to lpfc HBA data structure.
865  *
866  * This routine will cleanup completed slow path events after HBA is reset
867  * when bringing down the SLI Layer.
868  *
869  *
870  * Return codes
871  *   void.
872  **/
873 static void
874 lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
875 {
876         struct lpfc_iocbq *rspiocbq;
877         struct hbq_dmabuf *dmabuf;
878         struct lpfc_cq_event *cq_event;
879
880         spin_lock_irq(&phba->hbalock);
881         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
882         spin_unlock_irq(&phba->hbalock);
883
884         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
885                 /* Get the response iocb from the head of work queue */
886                 spin_lock_irq(&phba->hbalock);
887                 list_remove_head(&phba->sli4_hba.sp_queue_event,
888                                  cq_event, struct lpfc_cq_event, list);
889                 spin_unlock_irq(&phba->hbalock);
890
891                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
892                 case CQE_CODE_COMPL_WQE:
893                         rspiocbq = container_of(cq_event, struct lpfc_iocbq,
894                                                  cq_event);
895                         lpfc_sli_release_iocbq(phba, rspiocbq);
896                         break;
897                 case CQE_CODE_RECEIVE:
898                 case CQE_CODE_RECEIVE_V1:
899                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
900                                               cq_event);
901                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
902                 }
903         }
904 }
905
906 /**
907  * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
908  * @phba: pointer to lpfc HBA data structure.
909  *
910  * This routine will cleanup posted ELS buffers after the HBA is reset
911  * when bringing down the SLI Layer.
912  *
913  *
914  * Return codes
915  *   void.
916  **/
917 static void
918 lpfc_hba_free_post_buf(struct lpfc_hba *phba)
919 {
920         struct lpfc_sli *psli = &phba->sli;
921         struct lpfc_sli_ring *pring;
922         struct lpfc_dmabuf *mp, *next_mp;
923         LIST_HEAD(buflist);
924         int count;
925
926         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
927                 lpfc_sli_hbqbuf_free_all(phba);
928         else {
929                 /* Cleanup preposted buffers on the ELS ring */
930                 pring = &psli->sli3_ring[LPFC_ELS_RING];
931                 spin_lock_irq(&phba->hbalock);
932                 list_splice_init(&pring->postbufq, &buflist);
933                 spin_unlock_irq(&phba->hbalock);
934
935                 count = 0;
936                 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
937                         list_del(&mp->list);
938                         count++;
939                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
940                         kfree(mp);
941                 }
942
943                 spin_lock_irq(&phba->hbalock);
944                 pring->postbufq_cnt -= count;
945                 spin_unlock_irq(&phba->hbalock);
946         }
947 }
948
949 /**
950  * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
951  * @phba: pointer to lpfc HBA data structure.
952  *
953  * This routine will cleanup the txcmplq after the HBA is reset when bringing
954  * down the SLI Layer.
955  *
956  * Return codes
957  *   void
958  **/
959 static void
960 lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
961 {
962         struct lpfc_sli *psli = &phba->sli;
963         struct lpfc_queue *qp = NULL;
964         struct lpfc_sli_ring *pring;
965         LIST_HEAD(completions);
966         int i;
967         struct lpfc_iocbq *piocb, *next_iocb;
968
969         if (phba->sli_rev != LPFC_SLI_REV4) {
970                 for (i = 0; i < psli->num_rings; i++) {
971                         pring = &psli->sli3_ring[i];
972                         spin_lock_irq(&phba->hbalock);
973                         /* At this point in time the HBA is either reset or DOA
974                          * Nothing should be on txcmplq as it will
975                          * NEVER complete.
976                          */
977                         list_splice_init(&pring->txcmplq, &completions);
978                         pring->txcmplq_cnt = 0;
979                         spin_unlock_irq(&phba->hbalock);
980
981                         lpfc_sli_abort_iocb_ring(phba, pring);
982                 }
983                 /* Cancel all the IOCBs from the completions list */
984                 lpfc_sli_cancel_iocbs(phba, &completions,
985                                       IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
986                 return;
987         }
988         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
989                 pring = qp->pring;
990                 if (!pring)
991                         continue;
992                 spin_lock_irq(&pring->ring_lock);
993                 list_for_each_entry_safe(piocb, next_iocb,
994                                          &pring->txcmplq, list)
995                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
996                 list_splice_init(&pring->txcmplq, &completions);
997                 pring->txcmplq_cnt = 0;
998                 spin_unlock_irq(&pring->ring_lock);
999                 lpfc_sli_abort_iocb_ring(phba, pring);
1000         }
1001         /* Cancel all the IOCBs from the completions list */
1002         lpfc_sli_cancel_iocbs(phba, &completions,
1003                               IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
1004 }
1005
1006 /**
1007  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
1008         int i;
1009  * @phba: pointer to lpfc HBA data structure.
1010  *
1011  * This routine will do uninitialization after the HBA is reset when bring
1012  * down the SLI Layer.
1013  *
1014  * Return codes
1015  *   0 - success.
1016  *   Any other value - error.
1017  **/
1018 static int
1019 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
1020 {
1021         lpfc_hba_free_post_buf(phba);
1022         lpfc_hba_clean_txcmplq(phba);
1023         return 0;
1024 }
1025
1026 /**
1027  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1028  * @phba: pointer to lpfc HBA data structure.
1029  *
1030  * This routine will do uninitialization after the HBA is reset when bring
1031  * down the SLI Layer.
1032  *
1033  * Return codes
1034  *   0 - success.
1035  *   Any other value - error.
1036  **/
1037 static int
1038 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1039 {
1040         struct lpfc_scsi_buf *psb, *psb_next;
1041         struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
1042         LIST_HEAD(aborts);
1043         LIST_HEAD(nvme_aborts);
1044         LIST_HEAD(nvmet_aborts);
1045         unsigned long iflag = 0;
1046         struct lpfc_sglq *sglq_entry = NULL;
1047         int cnt;
1048
1049
1050         lpfc_sli_hbqbuf_free_all(phba);
1051         lpfc_hba_clean_txcmplq(phba);
1052
1053         /* At this point in time the HBA is either reset or DOA. Either
1054          * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
1055          * on the lpfc_els_sgl_list so that it can either be freed if the
1056          * driver is unloading or reposted if the driver is restarting
1057          * the port.
1058          */
1059         spin_lock_irq(&phba->hbalock);  /* required for lpfc_els_sgl_list and */
1060                                         /* scsl_buf_list */
1061         /* sgl_list_lock required because worker thread uses this
1062          * list.
1063          */
1064         spin_lock(&phba->sli4_hba.sgl_list_lock);
1065         list_for_each_entry(sglq_entry,
1066                 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1067                 sglq_entry->state = SGL_FREED;
1068
1069         list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1070                         &phba->sli4_hba.lpfc_els_sgl_list);
1071
1072
1073         spin_unlock(&phba->sli4_hba.sgl_list_lock);
1074         /* abts_scsi_buf_list_lock required because worker thread uses this
1075          * list.
1076          */
1077         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1078                 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1079                 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1080                                  &aborts);
1081                 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1082         }
1083
1084         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1085                 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1086                 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1087                                  &nvme_aborts);
1088                 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1089                                  &nvmet_aborts);
1090                 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1091         }
1092
1093         spin_unlock_irq(&phba->hbalock);
1094
1095         list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1096                 psb->pCmd = NULL;
1097                 psb->status = IOSTAT_SUCCESS;
1098         }
1099         spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1100         list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1101         spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1102
1103         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1104                 cnt = 0;
1105                 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1106                         psb->pCmd = NULL;
1107                         psb->status = IOSTAT_SUCCESS;
1108                         cnt++;
1109                 }
1110                 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1111                 phba->put_nvme_bufs += cnt;
1112                 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1113                 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1114
1115                 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1116                         ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
1117                         lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
1118                 }
1119         }
1120
1121         lpfc_sli4_free_sp_events(phba);
1122         return 0;
1123 }
1124
1125 /**
1126  * lpfc_hba_down_post - Wrapper func for hba down post routine
1127  * @phba: pointer to lpfc HBA data structure.
1128  *
1129  * This routine wraps the actual SLI3 or SLI4 routine for performing
1130  * uninitialization after the HBA is reset when bring down the SLI Layer.
1131  *
1132  * Return codes
1133  *   0 - success.
1134  *   Any other value - error.
1135  **/
1136 int
1137 lpfc_hba_down_post(struct lpfc_hba *phba)
1138 {
1139         return (*phba->lpfc_hba_down_post)(phba);
1140 }
1141
1142 /**
1143  * lpfc_hb_timeout - The HBA-timer timeout handler
1144  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1145  *
1146  * This is the HBA-timer timeout handler registered to the lpfc driver. When
1147  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1148  * work-port-events bitmap and the worker thread is notified. This timeout
1149  * event will be used by the worker thread to invoke the actual timeout
1150  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1151  * be performed in the timeout handler and the HBA timeout event bit shall
1152  * be cleared by the worker thread after it has taken the event bitmap out.
1153  **/
1154 static void
1155 lpfc_hb_timeout(struct timer_list *t)
1156 {
1157         struct lpfc_hba *phba;
1158         uint32_t tmo_posted;
1159         unsigned long iflag;
1160
1161         phba = from_timer(phba, t, hb_tmofunc);
1162
1163         /* Check for heart beat timeout conditions */
1164         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1165         tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1166         if (!tmo_posted)
1167                 phba->pport->work_port_events |= WORKER_HB_TMO;
1168         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1169
1170         /* Tell the worker thread there is work to do */
1171         if (!tmo_posted)
1172                 lpfc_worker_wake_up(phba);
1173         return;
1174 }
1175
1176 /**
1177  * lpfc_rrq_timeout - The RRQ-timer timeout handler
1178  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1179  *
1180  * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1181  * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1182  * work-port-events bitmap and the worker thread is notified. This timeout
1183  * event will be used by the worker thread to invoke the actual timeout
1184  * handler routine, lpfc_rrq_handler. Any periodical operations will
1185  * be performed in the timeout handler and the RRQ timeout event bit shall
1186  * be cleared by the worker thread after it has taken the event bitmap out.
1187  **/
1188 static void
1189 lpfc_rrq_timeout(struct timer_list *t)
1190 {
1191         struct lpfc_hba *phba;
1192         unsigned long iflag;
1193
1194         phba = from_timer(phba, t, rrq_tmr);
1195         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1196         if (!(phba->pport->load_flag & FC_UNLOADING))
1197                 phba->hba_flag |= HBA_RRQ_ACTIVE;
1198         else
1199                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1200         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1201
1202         if (!(phba->pport->load_flag & FC_UNLOADING))
1203                 lpfc_worker_wake_up(phba);
1204 }
1205
1206 /**
1207  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
1208  * @phba: pointer to lpfc hba data structure.
1209  * @pmboxq: pointer to the driver internal queue element for mailbox command.
1210  *
1211  * This is the callback function to the lpfc heart-beat mailbox command.
1212  * If configured, the lpfc driver issues the heart-beat mailbox command to
1213  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1214  * heart-beat mailbox command is issued, the driver shall set up heart-beat
1215  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1216  * heart-beat outstanding state. Once the mailbox command comes back and
1217  * no error conditions detected, the heart-beat mailbox command timer is
1218  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1219  * state is cleared for the next heart-beat. If the timer expired with the
1220  * heart-beat outstanding state set, the driver will put the HBA offline.
1221  **/
1222 static void
1223 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1224 {
1225         unsigned long drvr_flag;
1226
1227         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1228         phba->hb_outstanding = 0;
1229         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1230
1231         /* Check and reset heart-beat timer is necessary */
1232         mempool_free(pmboxq, phba->mbox_mem_pool);
1233         if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1234                 !(phba->link_state == LPFC_HBA_ERROR) &&
1235                 !(phba->pport->load_flag & FC_UNLOADING))
1236                 mod_timer(&phba->hb_tmofunc,
1237                           jiffies +
1238                           msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1239         return;
1240 }
1241
1242 /**
1243  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1244  * @phba: pointer to lpfc hba data structure.
1245  *
1246  * This is the actual HBA-timer timeout handler to be invoked by the worker
1247  * thread whenever the HBA timer fired and HBA-timeout event posted. This
1248  * handler performs any periodic operations needed for the device. If such
1249  * periodic event has already been attended to either in the interrupt handler
1250  * or by processing slow-ring or fast-ring events within the HBA-timer
1251  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1252  * the timer for the next timeout period. If lpfc heart-beat mailbox command
1253  * is configured and there is no heart-beat mailbox command outstanding, a
1254  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1255  * has been a heart-beat mailbox command outstanding, the HBA shall be put
1256  * to offline.
1257  **/
1258 void
1259 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1260 {
1261         struct lpfc_vport **vports;
1262         LPFC_MBOXQ_t *pmboxq;
1263         struct lpfc_dmabuf *buf_ptr;
1264         int retval, i;
1265         struct lpfc_sli *psli = &phba->sli;
1266         LIST_HEAD(completions);
1267         struct lpfc_queue *qp;
1268         unsigned long time_elapsed;
1269         uint32_t tick_cqe, max_cqe, val;
1270         uint64_t tot, data1, data2, data3;
1271         struct lpfc_nvmet_tgtport *tgtp;
1272         struct lpfc_register reg_data;
1273         struct nvme_fc_local_port *localport;
1274         struct lpfc_nvme_lport *lport;
1275         struct lpfc_nvme_ctrl_stat *cstat;
1276         void __iomem *eqdreg = phba->sli4_hba.u.if_type2.EQDregaddr;
1277
1278         vports = lpfc_create_vport_work_array(phba);
1279         if (vports != NULL)
1280                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1281                         lpfc_rcv_seq_check_edtov(vports[i]);
1282                         lpfc_fdmi_num_disc_check(vports[i]);
1283                 }
1284         lpfc_destroy_vport_work_array(phba, vports);
1285
1286         if ((phba->link_state == LPFC_HBA_ERROR) ||
1287                 (phba->pport->load_flag & FC_UNLOADING) ||
1288                 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1289                 return;
1290
1291         if (phba->cfg_auto_imax) {
1292                 if (!phba->last_eqdelay_time) {
1293                         phba->last_eqdelay_time = jiffies;
1294                         goto skip_eqdelay;
1295                 }
1296                 time_elapsed = jiffies - phba->last_eqdelay_time;
1297                 phba->last_eqdelay_time = jiffies;
1298
1299                 tot = 0xffff;
1300                 /* Check outstanding IO count */
1301                 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1302                         if (phba->nvmet_support) {
1303                                 tgtp = phba->targetport->private;
1304                                 /* Calculate outstanding IOs */
1305                                 tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1306                                 tot += atomic_read(&tgtp->xmt_fcp_release);
1307                                 tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1308                         } else {
1309                                 localport = phba->pport->localport;
1310                                 if (!localport || !localport->private)
1311                                         goto skip_eqdelay;
1312                                 lport = (struct lpfc_nvme_lport *)
1313                                         localport->private;
1314                                 tot = 0;
1315                                 for (i = 0;
1316                                         i < phba->cfg_nvme_io_channel; i++) {
1317                                         cstat = &lport->cstat[i];
1318                                         data1 = atomic_read(
1319                                                 &cstat->fc4NvmeInputRequests);
1320                                         data2 = atomic_read(
1321                                                 &cstat->fc4NvmeOutputRequests);
1322                                         data3 = atomic_read(
1323                                                 &cstat->fc4NvmeControlRequests);
1324                                         tot += (data1 + data2 + data3);
1325                                         tot -= atomic_read(
1326                                                 &cstat->fc4NvmeIoCmpls);
1327                                 }
1328                         }
1329                 }
1330
1331                 /* Interrupts per sec per EQ */
1332                 val = phba->cfg_fcp_imax / phba->io_channel_irqs;
1333                 tick_cqe = val / CONFIG_HZ; /* Per tick per EQ */
1334
1335                 /* Assume 1 CQE/ISR, calc max CQEs allowed for time duration */
1336                 max_cqe = time_elapsed * tick_cqe;
1337
1338                 for (i = 0; i < phba->io_channel_irqs; i++) {
1339                         /* Fast-path EQ */
1340                         qp = phba->sli4_hba.hba_eq[i];
1341                         if (!qp)
1342                                 continue;
1343
1344                         /* Use no EQ delay if we don't have many outstanding
1345                          * IOs, or if we are only processing 1 CQE/ISR or less.
1346                          * Otherwise, assume we can process up to lpfc_fcp_imax
1347                          * interrupts per HBA.
1348                          */
1349                         if (tot < LPFC_NODELAY_MAX_IO ||
1350                             qp->EQ_cqe_cnt <= max_cqe)
1351                                 val = 0;
1352                         else
1353                                 val = phba->cfg_fcp_imax;
1354
1355                         if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
1356                                 /* Use EQ Delay Register method */
1357
1358                                 /* Convert for EQ Delay register */
1359                                 if (val) {
1360                                         /* First, interrupts per sec per EQ */
1361                                         val = phba->cfg_fcp_imax /
1362                                                 phba->io_channel_irqs;
1363
1364                                         /* us delay between each interrupt */
1365                                         val = LPFC_SEC_TO_USEC / val;
1366                                 }
1367                                 if (val != qp->q_mode) {
1368                                         reg_data.word0 = 0;
1369                                         bf_set(lpfc_sliport_eqdelay_id,
1370                                                &reg_data, qp->queue_id);
1371                                         bf_set(lpfc_sliport_eqdelay_delay,
1372                                                &reg_data, val);
1373                                         writel(reg_data.word0, eqdreg);
1374                                 }
1375                         } else {
1376                                 /* Use mbox command method */
1377                                 if (val != qp->q_mode)
1378                                         lpfc_modify_hba_eq_delay(phba, i,
1379                                                                  1, val);
1380                         }
1381
1382                         /*
1383                          * val is cfg_fcp_imax or 0 for mbox delay or us delay
1384                          * between interrupts for EQDR.
1385                          */
1386                         qp->q_mode = val;
1387                         qp->EQ_cqe_cnt = 0;
1388                 }
1389         }
1390
1391 skip_eqdelay:
1392         spin_lock_irq(&phba->pport->work_port_lock);
1393
1394         if (time_after(phba->last_completion_time +
1395                         msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1396                         jiffies)) {
1397                 spin_unlock_irq(&phba->pport->work_port_lock);
1398                 if (!phba->hb_outstanding)
1399                         mod_timer(&phba->hb_tmofunc,
1400                                 jiffies +
1401                                 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1402                 else
1403                         mod_timer(&phba->hb_tmofunc,
1404                                 jiffies +
1405                                 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1406                 return;
1407         }
1408         spin_unlock_irq(&phba->pport->work_port_lock);
1409
1410         if (phba->elsbuf_cnt &&
1411                 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1412                 spin_lock_irq(&phba->hbalock);
1413                 list_splice_init(&phba->elsbuf, &completions);
1414                 phba->elsbuf_cnt = 0;
1415                 phba->elsbuf_prev_cnt = 0;
1416                 spin_unlock_irq(&phba->hbalock);
1417
1418                 while (!list_empty(&completions)) {
1419                         list_remove_head(&completions, buf_ptr,
1420                                 struct lpfc_dmabuf, list);
1421                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1422                         kfree(buf_ptr);
1423                 }
1424         }
1425         phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1426
1427         /* If there is no heart beat outstanding, issue a heartbeat command */
1428         if (phba->cfg_enable_hba_heartbeat) {
1429                 if (!phba->hb_outstanding) {
1430                         if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1431                                 (list_empty(&psli->mboxq))) {
1432                                 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1433                                                         GFP_KERNEL);
1434                                 if (!pmboxq) {
1435                                         mod_timer(&phba->hb_tmofunc,
1436                                                  jiffies +
1437                                                  msecs_to_jiffies(1000 *
1438                                                  LPFC_HB_MBOX_INTERVAL));
1439                                         return;
1440                                 }
1441
1442                                 lpfc_heart_beat(phba, pmboxq);
1443                                 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1444                                 pmboxq->vport = phba->pport;
1445                                 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1446                                                 MBX_NOWAIT);
1447
1448                                 if (retval != MBX_BUSY &&
1449                                         retval != MBX_SUCCESS) {
1450                                         mempool_free(pmboxq,
1451                                                         phba->mbox_mem_pool);
1452                                         mod_timer(&phba->hb_tmofunc,
1453                                                 jiffies +
1454                                                 msecs_to_jiffies(1000 *
1455                                                 LPFC_HB_MBOX_INTERVAL));
1456                                         return;
1457                                 }
1458                                 phba->skipped_hb = 0;
1459                                 phba->hb_outstanding = 1;
1460                         } else if (time_before_eq(phba->last_completion_time,
1461                                         phba->skipped_hb)) {
1462                                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1463                                         "2857 Last completion time not "
1464                                         " updated in %d ms\n",
1465                                         jiffies_to_msecs(jiffies
1466                                                  - phba->last_completion_time));
1467                         } else
1468                                 phba->skipped_hb = jiffies;
1469
1470                         mod_timer(&phba->hb_tmofunc,
1471                                  jiffies +
1472                                  msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1473                         return;
1474                 } else {
1475                         /*
1476                         * If heart beat timeout called with hb_outstanding set
1477                         * we need to give the hb mailbox cmd a chance to
1478                         * complete or TMO.
1479                         */
1480                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1481                                         "0459 Adapter heartbeat still out"
1482                                         "standing:last compl time was %d ms.\n",
1483                                         jiffies_to_msecs(jiffies
1484                                                  - phba->last_completion_time));
1485                         mod_timer(&phba->hb_tmofunc,
1486                                 jiffies +
1487                                 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1488                 }
1489         } else {
1490                         mod_timer(&phba->hb_tmofunc,
1491                                 jiffies +
1492                                 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1493         }
1494 }
1495
1496 /**
1497  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1498  * @phba: pointer to lpfc hba data structure.
1499  *
1500  * This routine is called to bring the HBA offline when HBA hardware error
1501  * other than Port Error 6 has been detected.
1502  **/
1503 static void
1504 lpfc_offline_eratt(struct lpfc_hba *phba)
1505 {
1506         struct lpfc_sli   *psli = &phba->sli;
1507
1508         spin_lock_irq(&phba->hbalock);
1509         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1510         spin_unlock_irq(&phba->hbalock);
1511         lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1512
1513         lpfc_offline(phba);
1514         lpfc_reset_barrier(phba);
1515         spin_lock_irq(&phba->hbalock);
1516         lpfc_sli_brdreset(phba);
1517         spin_unlock_irq(&phba->hbalock);
1518         lpfc_hba_down_post(phba);
1519         lpfc_sli_brdready(phba, HS_MBRDY);
1520         lpfc_unblock_mgmt_io(phba);
1521         phba->link_state = LPFC_HBA_ERROR;
1522         return;
1523 }
1524
1525 /**
1526  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1527  * @phba: pointer to lpfc hba data structure.
1528  *
1529  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1530  * other than Port Error 6 has been detected.
1531  **/
1532 void
1533 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1534 {
1535         spin_lock_irq(&phba->hbalock);
1536         phba->link_state = LPFC_HBA_ERROR;
1537         spin_unlock_irq(&phba->hbalock);
1538
1539         lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1540         lpfc_offline(phba);
1541         lpfc_hba_down_post(phba);
1542         lpfc_unblock_mgmt_io(phba);
1543 }
1544
1545 /**
1546  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1547  * @phba: pointer to lpfc hba data structure.
1548  *
1549  * This routine is invoked to handle the deferred HBA hardware error
1550  * conditions. This type of error is indicated by HBA by setting ER1
1551  * and another ER bit in the host status register. The driver will
1552  * wait until the ER1 bit clears before handling the error condition.
1553  **/
1554 static void
1555 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1556 {
1557         uint32_t old_host_status = phba->work_hs;
1558         struct lpfc_sli *psli = &phba->sli;
1559
1560         /* If the pci channel is offline, ignore possible errors,
1561          * since we cannot communicate with the pci card anyway.
1562          */
1563         if (pci_channel_offline(phba->pcidev)) {
1564                 spin_lock_irq(&phba->hbalock);
1565                 phba->hba_flag &= ~DEFER_ERATT;
1566                 spin_unlock_irq(&phba->hbalock);
1567                 return;
1568         }
1569
1570         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1571                 "0479 Deferred Adapter Hardware Error "
1572                 "Data: x%x x%x x%x\n",
1573                 phba->work_hs,
1574                 phba->work_status[0], phba->work_status[1]);
1575
1576         spin_lock_irq(&phba->hbalock);
1577         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1578         spin_unlock_irq(&phba->hbalock);
1579
1580
1581         /*
1582          * Firmware stops when it triggred erratt. That could cause the I/Os
1583          * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1584          * SCSI layer retry it after re-establishing link.
1585          */
1586         lpfc_sli_abort_fcp_rings(phba);
1587
1588         /*
1589          * There was a firmware error. Take the hba offline and then
1590          * attempt to restart it.
1591          */
1592         lpfc_offline_prep(phba, LPFC_MBX_WAIT);
1593         lpfc_offline(phba);
1594
1595         /* Wait for the ER1 bit to clear.*/
1596         while (phba->work_hs & HS_FFER1) {
1597                 msleep(100);
1598                 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1599                         phba->work_hs = UNPLUG_ERR ;
1600                         break;
1601                 }
1602                 /* If driver is unloading let the worker thread continue */
1603                 if (phba->pport->load_flag & FC_UNLOADING) {
1604                         phba->work_hs = 0;
1605                         break;
1606                 }
1607         }
1608
1609         /*
1610          * This is to ptrotect against a race condition in which
1611          * first write to the host attention register clear the
1612          * host status register.
1613          */
1614         if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1615                 phba->work_hs = old_host_status & ~HS_FFER1;
1616
1617         spin_lock_irq(&phba->hbalock);
1618         phba->hba_flag &= ~DEFER_ERATT;
1619         spin_unlock_irq(&phba->hbalock);
1620         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1621         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1622 }
1623
1624 static void
1625 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1626 {
1627         struct lpfc_board_event_header board_event;
1628         struct Scsi_Host *shost;
1629
1630         board_event.event_type = FC_REG_BOARD_EVENT;
1631         board_event.subcategory = LPFC_EVENT_PORTINTERR;
1632         shost = lpfc_shost_from_vport(phba->pport);
1633         fc_host_post_vendor_event(shost, fc_get_event_number(),
1634                                   sizeof(board_event),
1635                                   (char *) &board_event,
1636                                   LPFC_NL_VENDOR_ID);
1637 }
1638
1639 /**
1640  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1641  * @phba: pointer to lpfc hba data structure.
1642  *
1643  * This routine is invoked to handle the following HBA hardware error
1644  * conditions:
1645  * 1 - HBA error attention interrupt
1646  * 2 - DMA ring index out of range
1647  * 3 - Mailbox command came back as unknown
1648  **/
1649 static void
1650 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1651 {
1652         struct lpfc_vport *vport = phba->pport;
1653         struct lpfc_sli   *psli = &phba->sli;
1654         uint32_t event_data;
1655         unsigned long temperature;
1656         struct temp_event temp_event_data;
1657         struct Scsi_Host  *shost;
1658
1659         /* If the pci channel is offline, ignore possible errors,
1660          * since we cannot communicate with the pci card anyway.
1661          */
1662         if (pci_channel_offline(phba->pcidev)) {
1663                 spin_lock_irq(&phba->hbalock);
1664                 phba->hba_flag &= ~DEFER_ERATT;
1665                 spin_unlock_irq(&phba->hbalock);
1666                 return;
1667         }
1668
1669         /* If resets are disabled then leave the HBA alone and return */
1670         if (!phba->cfg_enable_hba_reset)
1671                 return;
1672
1673         /* Send an internal error event to mgmt application */
1674         lpfc_board_errevt_to_mgmt(phba);
1675
1676         if (phba->hba_flag & DEFER_ERATT)
1677                 lpfc_handle_deferred_eratt(phba);
1678
1679         if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1680                 if (phba->work_hs & HS_FFER6)
1681                         /* Re-establishing Link */
1682                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1683                                         "1301 Re-establishing Link "
1684                                         "Data: x%x x%x x%x\n",
1685                                         phba->work_hs, phba->work_status[0],
1686                                         phba->work_status[1]);
1687                 if (phba->work_hs & HS_FFER8)
1688                         /* Device Zeroization */
1689                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1690                                         "2861 Host Authentication device "
1691                                         "zeroization Data:x%x x%x x%x\n",
1692                                         phba->work_hs, phba->work_status[0],
1693                                         phba->work_status[1]);
1694
1695                 spin_lock_irq(&phba->hbalock);
1696                 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1697                 spin_unlock_irq(&phba->hbalock);
1698
1699                 /*
1700                 * Firmware stops when it triggled erratt with HS_FFER6.
1701                 * That could cause the I/Os dropped by the firmware.
1702                 * Error iocb (I/O) on txcmplq and let the SCSI layer
1703                 * retry it after re-establishing link.
1704                 */
1705                 lpfc_sli_abort_fcp_rings(phba);
1706
1707                 /*
1708                  * There was a firmware error.  Take the hba offline and then
1709                  * attempt to restart it.
1710                  */
1711                 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1712                 lpfc_offline(phba);
1713                 lpfc_sli_brdrestart(phba);
1714                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
1715                         lpfc_unblock_mgmt_io(phba);
1716                         return;
1717                 }
1718                 lpfc_unblock_mgmt_io(phba);
1719         } else if (phba->work_hs & HS_CRIT_TEMP) {
1720                 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1721                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1722                 temp_event_data.event_code = LPFC_CRIT_TEMP;
1723                 temp_event_data.data = (uint32_t)temperature;
1724
1725                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1726                                 "0406 Adapter maximum temperature exceeded "
1727                                 "(%ld), taking this port offline "
1728                                 "Data: x%x x%x x%x\n",
1729                                 temperature, phba->work_hs,
1730                                 phba->work_status[0], phba->work_status[1]);
1731
1732                 shost = lpfc_shost_from_vport(phba->pport);
1733                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1734                                           sizeof(temp_event_data),
1735                                           (char *) &temp_event_data,
1736                                           SCSI_NL_VID_TYPE_PCI
1737                                           | PCI_VENDOR_ID_EMULEX);
1738
1739                 spin_lock_irq(&phba->hbalock);
1740                 phba->over_temp_state = HBA_OVER_TEMP;
1741                 spin_unlock_irq(&phba->hbalock);
1742                 lpfc_offline_eratt(phba);
1743
1744         } else {
1745                 /* The if clause above forces this code path when the status
1746                  * failure is a value other than FFER6. Do not call the offline
1747                  * twice. This is the adapter hardware error path.
1748                  */
1749                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1750                                 "0457 Adapter Hardware Error "
1751                                 "Data: x%x x%x x%x\n",
1752                                 phba->work_hs,
1753                                 phba->work_status[0], phba->work_status[1]);
1754
1755                 event_data = FC_REG_DUMP_EVENT;
1756                 shost = lpfc_shost_from_vport(vport);
1757                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1758                                 sizeof(event_data), (char *) &event_data,
1759                                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1760
1761                 lpfc_offline_eratt(phba);
1762         }
1763         return;
1764 }
1765
1766 /**
1767  * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1768  * @phba: pointer to lpfc hba data structure.
1769  * @mbx_action: flag for mailbox shutdown action.
1770  *
1771  * This routine is invoked to perform an SLI4 port PCI function reset in
1772  * response to port status register polling attention. It waits for port
1773  * status register (ERR, RDY, RN) bits before proceeding with function reset.
1774  * During this process, interrupt vectors are freed and later requested
1775  * for handling possible port resource change.
1776  **/
1777 static int
1778 lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1779                             bool en_rn_msg)
1780 {
1781         int rc;
1782         uint32_t intr_mode;
1783
1784         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
1785             LPFC_SLI_INTF_IF_TYPE_2) {
1786                 /*
1787                  * On error status condition, driver need to wait for port
1788                  * ready before performing reset.
1789                  */
1790                 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1791                 if (rc)
1792                         return rc;
1793         }
1794
1795         /* need reset: attempt for port recovery */
1796         if (en_rn_msg)
1797                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1798                                 "2887 Reset Needed: Attempting Port "
1799                                 "Recovery...\n");
1800         lpfc_offline_prep(phba, mbx_action);
1801         lpfc_offline(phba);
1802         /* release interrupt for possible resource change */
1803         lpfc_sli4_disable_intr(phba);
1804         rc = lpfc_sli_brdrestart(phba);
1805         if (rc) {
1806                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1807                                 "6309 Failed to restart board\n");
1808                 return rc;
1809         }
1810         /* request and enable interrupt */
1811         intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1812         if (intr_mode == LPFC_INTR_ERROR) {
1813                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1814                                 "3175 Failed to enable interrupt\n");
1815                 return -EIO;
1816         }
1817         phba->intr_mode = intr_mode;
1818         rc = lpfc_online(phba);
1819         if (rc == 0)
1820                 lpfc_unblock_mgmt_io(phba);
1821
1822         return rc;
1823 }
1824
1825 /**
1826  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1827  * @phba: pointer to lpfc hba data structure.
1828  *
1829  * This routine is invoked to handle the SLI4 HBA hardware error attention
1830  * conditions.
1831  **/
1832 static void
1833 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1834 {
1835         struct lpfc_vport *vport = phba->pport;
1836         uint32_t event_data;
1837         struct Scsi_Host *shost;
1838         uint32_t if_type;
1839         struct lpfc_register portstat_reg = {0};
1840         uint32_t reg_err1, reg_err2;
1841         uint32_t uerrlo_reg, uemasklo_reg;
1842         uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
1843         bool en_rn_msg = true;
1844         struct temp_event temp_event_data;
1845         struct lpfc_register portsmphr_reg;
1846         int rc, i;
1847
1848         /* If the pci channel is offline, ignore possible errors, since
1849          * we cannot communicate with the pci card anyway.
1850          */
1851         if (pci_channel_offline(phba->pcidev))
1852                 return;
1853
1854         memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
1855         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1856         switch (if_type) {
1857         case LPFC_SLI_INTF_IF_TYPE_0:
1858                 pci_rd_rc1 = lpfc_readl(
1859                                 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1860                                 &uerrlo_reg);
1861                 pci_rd_rc2 = lpfc_readl(
1862                                 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1863                                 &uemasklo_reg);
1864                 /* consider PCI bus read error as pci_channel_offline */
1865                 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1866                         return;
1867                 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1868                         lpfc_sli4_offline_eratt(phba);
1869                         return;
1870                 }
1871                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1872                                 "7623 Checking UE recoverable");
1873
1874                 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1875                         if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1876                                        &portsmphr_reg.word0))
1877                                 continue;
1878
1879                         smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1880                                                    &portsmphr_reg);
1881                         if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1882                             LPFC_PORT_SEM_UE_RECOVERABLE)
1883                                 break;
1884                         /*Sleep for 1Sec, before checking SEMAPHORE */
1885                         msleep(1000);
1886                 }
1887
1888                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1889                                 "4827 smphr_port_status x%x : Waited %dSec",
1890                                 smphr_port_status, i);
1891
1892                 /* Recoverable UE, reset the HBA device */
1893                 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1894                     LPFC_PORT_SEM_UE_RECOVERABLE) {
1895                         for (i = 0; i < 20; i++) {
1896                                 msleep(1000);
1897                                 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1898                                     &portsmphr_reg.word0) &&
1899                                     (LPFC_POST_STAGE_PORT_READY ==
1900                                      bf_get(lpfc_port_smphr_port_status,
1901                                      &portsmphr_reg))) {
1902                                         rc = lpfc_sli4_port_sta_fn_reset(phba,
1903                                                 LPFC_MBX_NO_WAIT, en_rn_msg);
1904                                         if (rc == 0)
1905                                                 return;
1906                                         lpfc_printf_log(phba,
1907                                                 KERN_ERR, LOG_INIT,
1908                                                 "4215 Failed to recover UE");
1909                                         break;
1910                                 }
1911                         }
1912                 }
1913                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1914                                 "7624 Firmware not ready: Failing UE recovery,"
1915                                 " waited %dSec", i);
1916                 lpfc_sli4_offline_eratt(phba);
1917                 break;
1918
1919         case LPFC_SLI_INTF_IF_TYPE_2:
1920         case LPFC_SLI_INTF_IF_TYPE_6:
1921                 pci_rd_rc1 = lpfc_readl(
1922                                 phba->sli4_hba.u.if_type2.STATUSregaddr,
1923                                 &portstat_reg.word0);
1924                 /* consider PCI bus read error as pci_channel_offline */
1925                 if (pci_rd_rc1 == -EIO) {
1926                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1927                                 "3151 PCI bus read access failure: x%x\n",
1928                                 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
1929                         return;
1930                 }
1931                 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1932                 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
1933                 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1934                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1935                                 "2889 Port Overtemperature event, "
1936                                 "taking port offline Data: x%x x%x\n",
1937                                 reg_err1, reg_err2);
1938
1939                         phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
1940                         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1941                         temp_event_data.event_code = LPFC_CRIT_TEMP;
1942                         temp_event_data.data = 0xFFFFFFFF;
1943
1944                         shost = lpfc_shost_from_vport(phba->pport);
1945                         fc_host_post_vendor_event(shost, fc_get_event_number(),
1946                                                   sizeof(temp_event_data),
1947                                                   (char *)&temp_event_data,
1948                                                   SCSI_NL_VID_TYPE_PCI
1949                                                   | PCI_VENDOR_ID_EMULEX);
1950
1951                         spin_lock_irq(&phba->hbalock);
1952                         phba->over_temp_state = HBA_OVER_TEMP;
1953                         spin_unlock_irq(&phba->hbalock);
1954                         lpfc_sli4_offline_eratt(phba);
1955                         return;
1956                 }
1957                 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1958                     reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
1959                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1960                                         "3143 Port Down: Firmware Update "
1961                                         "Detected\n");
1962                         en_rn_msg = false;
1963                 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1964                          reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1965                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1966                                         "3144 Port Down: Debug Dump\n");
1967                 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1968                          reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1969                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1970                                         "3145 Port Down: Provisioning\n");
1971
1972                 /* If resets are disabled then leave the HBA alone and return */
1973                 if (!phba->cfg_enable_hba_reset)
1974                         return;
1975
1976                 /* Check port status register for function reset */
1977                 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1978                                 en_rn_msg);
1979                 if (rc == 0) {
1980                         /* don't report event on forced debug dump */
1981                         if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1982                             reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1983                                 return;
1984                         else
1985                                 break;
1986                 }
1987                 /* fall through for not able to recover */
1988                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1989                                 "3152 Unrecoverable error, bring the port "
1990                                 "offline\n");
1991                 lpfc_sli4_offline_eratt(phba);
1992                 break;
1993         case LPFC_SLI_INTF_IF_TYPE_1:
1994         default:
1995                 break;
1996         }
1997         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1998                         "3123 Report dump event to upper layer\n");
1999         /* Send an internal error event to mgmt application */
2000         lpfc_board_errevt_to_mgmt(phba);
2001
2002         event_data = FC_REG_DUMP_EVENT;
2003         shost = lpfc_shost_from_vport(vport);
2004         fc_host_post_vendor_event(shost, fc_get_event_number(),
2005                                   sizeof(event_data), (char *) &event_data,
2006                                   SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
2007 }
2008
2009 /**
2010  * lpfc_handle_eratt - Wrapper func for handling hba error attention
2011  * @phba: pointer to lpfc HBA data structure.
2012  *
2013  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2014  * routine from the API jump table function pointer from the lpfc_hba struct.
2015  *
2016  * Return codes
2017  *   0 - success.
2018  *   Any other value - error.
2019  **/
2020 void
2021 lpfc_handle_eratt(struct lpfc_hba *phba)
2022 {
2023         (*phba->lpfc_handle_eratt)(phba);
2024 }
2025
2026 /**
2027  * lpfc_handle_latt - The HBA link event handler
2028  * @phba: pointer to lpfc hba data structure.
2029  *
2030  * This routine is invoked from the worker thread to handle a HBA host
2031  * attention link event. SLI3 only.
2032  **/
2033 void
2034 lpfc_handle_latt(struct lpfc_hba *phba)
2035 {
2036         struct lpfc_vport *vport = phba->pport;
2037         struct lpfc_sli   *psli = &phba->sli;
2038         LPFC_MBOXQ_t *pmb;
2039         volatile uint32_t control;
2040         struct lpfc_dmabuf *mp;
2041         int rc = 0;
2042
2043         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2044         if (!pmb) {
2045                 rc = 1;
2046                 goto lpfc_handle_latt_err_exit;
2047         }
2048
2049         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2050         if (!mp) {
2051                 rc = 2;
2052                 goto lpfc_handle_latt_free_pmb;
2053         }
2054
2055         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2056         if (!mp->virt) {
2057                 rc = 3;
2058                 goto lpfc_handle_latt_free_mp;
2059         }
2060
2061         /* Cleanup any outstanding ELS commands */
2062         lpfc_els_flush_all_cmd(phba);
2063
2064         psli->slistat.link_event++;
2065         lpfc_read_topology(phba, pmb, mp);
2066         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
2067         pmb->vport = vport;
2068         /* Block ELS IOCBs until we have processed this mbox command */
2069         phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2070         rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
2071         if (rc == MBX_NOT_FINISHED) {
2072                 rc = 4;
2073                 goto lpfc_handle_latt_free_mbuf;
2074         }
2075
2076         /* Clear Link Attention in HA REG */
2077         spin_lock_irq(&phba->hbalock);
2078         writel(HA_LATT, phba->HAregaddr);
2079         readl(phba->HAregaddr); /* flush */
2080         spin_unlock_irq(&phba->hbalock);
2081
2082         return;
2083
2084 lpfc_handle_latt_free_mbuf:
2085         phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
2086         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2087 lpfc_handle_latt_free_mp:
2088         kfree(mp);
2089 lpfc_handle_latt_free_pmb:
2090         mempool_free(pmb, phba->mbox_mem_pool);
2091 lpfc_handle_latt_err_exit:
2092         /* Enable Link attention interrupts */
2093         spin_lock_irq(&phba->hbalock);
2094         psli->sli_flag |= LPFC_PROCESS_LA;
2095         control = readl(phba->HCregaddr);
2096         control |= HC_LAINT_ENA;
2097         writel(control, phba->HCregaddr);
2098         readl(phba->HCregaddr); /* flush */
2099
2100         /* Clear Link Attention in HA REG */
2101         writel(HA_LATT, phba->HAregaddr);
2102         readl(phba->HAregaddr); /* flush */
2103         spin_unlock_irq(&phba->hbalock);
2104         lpfc_linkdown(phba);
2105         phba->link_state = LPFC_HBA_ERROR;
2106
2107         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2108                      "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
2109
2110         return;
2111 }
2112
2113 /**
2114  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
2115  * @phba: pointer to lpfc hba data structure.
2116  * @vpd: pointer to the vital product data.
2117  * @len: length of the vital product data in bytes.
2118  *
2119  * This routine parses the Vital Product Data (VPD). The VPD is treated as
2120  * an array of characters. In this routine, the ModelName, ProgramType, and
2121  * ModelDesc, etc. fields of the phba data structure will be populated.
2122  *
2123  * Return codes
2124  *   0 - pointer to the VPD passed in is NULL
2125  *   1 - success
2126  **/
2127 int
2128 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
2129 {
2130         uint8_t lenlo, lenhi;
2131         int Length;
2132         int i, j;
2133         int finished = 0;
2134         int index = 0;
2135
2136         if (!vpd)
2137                 return 0;
2138
2139         /* Vital Product */
2140         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2141                         "0455 Vital Product Data: x%x x%x x%x x%x\n",
2142                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2143                         (uint32_t) vpd[3]);
2144         while (!finished && (index < (len - 4))) {
2145                 switch (vpd[index]) {
2146                 case 0x82:
2147                 case 0x91:
2148                         index += 1;
2149                         lenlo = vpd[index];
2150                         index += 1;
2151                         lenhi = vpd[index];
2152                         index += 1;
2153                         i = ((((unsigned short)lenhi) << 8) + lenlo);
2154                         index += i;
2155                         break;
2156                 case 0x90:
2157                         index += 1;
2158                         lenlo = vpd[index];
2159                         index += 1;
2160                         lenhi = vpd[index];
2161                         index += 1;
2162                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
2163                         if (Length > len - index)
2164                                 Length = len - index;
2165                         while (Length > 0) {
2166                         /* Look for Serial Number */
2167                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2168                                 index += 2;
2169                                 i = vpd[index];
2170                                 index += 1;
2171                                 j = 0;
2172                                 Length -= (3+i);
2173                                 while(i--) {
2174                                         phba->SerialNumber[j++] = vpd[index++];
2175                                         if (j == 31)
2176                                                 break;
2177                                 }
2178                                 phba->SerialNumber[j] = 0;
2179                                 continue;
2180                         }
2181                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2182                                 phba->vpd_flag |= VPD_MODEL_DESC;
2183                                 index += 2;
2184                                 i = vpd[index];
2185                                 index += 1;
2186                                 j = 0;
2187                                 Length -= (3+i);
2188                                 while(i--) {
2189                                         phba->ModelDesc[j++] = vpd[index++];
2190                                         if (j == 255)
2191                                                 break;
2192                                 }
2193                                 phba->ModelDesc[j] = 0;
2194                                 continue;
2195                         }
2196                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2197                                 phba->vpd_flag |= VPD_MODEL_NAME;
2198                                 index += 2;
2199                                 i = vpd[index];
2200                                 index += 1;
2201                                 j = 0;
2202                                 Length -= (3+i);
2203                                 while(i--) {
2204                                         phba->ModelName[j++] = vpd[index++];
2205                                         if (j == 79)
2206                                                 break;
2207                                 }
2208                                 phba->ModelName[j] = 0;
2209                                 continue;
2210                         }
2211                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2212                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2213                                 index += 2;
2214                                 i = vpd[index];
2215                                 index += 1;
2216                                 j = 0;
2217                                 Length -= (3+i);
2218                                 while(i--) {
2219                                         phba->ProgramType[j++] = vpd[index++];
2220                                         if (j == 255)
2221                                                 break;
2222                                 }
2223                                 phba->ProgramType[j] = 0;
2224                                 continue;
2225                         }
2226                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2227                                 phba->vpd_flag |= VPD_PORT;
2228                                 index += 2;
2229                                 i = vpd[index];
2230                                 index += 1;
2231                                 j = 0;
2232                                 Length -= (3+i);
2233                                 while(i--) {
2234                                         if ((phba->sli_rev == LPFC_SLI_REV4) &&
2235                                             (phba->sli4_hba.pport_name_sta ==
2236                                              LPFC_SLI4_PPNAME_GET)) {
2237                                                 j++;
2238                                                 index++;
2239                                         } else
2240                                                 phba->Port[j++] = vpd[index++];
2241                                         if (j == 19)
2242                                                 break;
2243                                 }
2244                                 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2245                                     (phba->sli4_hba.pport_name_sta ==
2246                                      LPFC_SLI4_PPNAME_NON))
2247                                         phba->Port[j] = 0;
2248                                 continue;
2249                         }
2250                         else {
2251                                 index += 2;
2252                                 i = vpd[index];
2253                                 index += 1;
2254                                 index += i;
2255                                 Length -= (3 + i);
2256                         }
2257                 }
2258                 finished = 0;
2259                 break;
2260                 case 0x78:
2261                         finished = 1;
2262                         break;
2263                 default:
2264                         index ++;
2265                         break;
2266                 }
2267         }
2268
2269         return(1);
2270 }
2271
2272 /**
2273  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
2274  * @phba: pointer to lpfc hba data structure.
2275  * @mdp: pointer to the data structure to hold the derived model name.
2276  * @descp: pointer to the data structure to hold the derived description.
2277  *
2278  * This routine retrieves HBA's description based on its registered PCI device
2279  * ID. The @descp passed into this function points to an array of 256 chars. It
2280  * shall be returned with the model name, maximum speed, and the host bus type.
2281  * The @mdp passed into this function points to an array of 80 chars. When the
2282  * function returns, the @mdp will be filled with the model name.
2283  **/
2284 static void
2285 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
2286 {
2287         lpfc_vpd_t *vp;
2288         uint16_t dev_id = phba->pcidev->device;
2289         int max_speed;
2290         int GE = 0;
2291         int oneConnect = 0; /* default is not a oneConnect */
2292         struct {
2293                 char *name;
2294                 char *bus;
2295                 char *function;
2296         } m = {"<Unknown>", "", ""};
2297
2298         if (mdp && mdp[0] != '\0'
2299                 && descp && descp[0] != '\0')
2300                 return;
2301
2302         if (phba->lmt & LMT_64Gb)
2303                 max_speed = 64;
2304         else if (phba->lmt & LMT_32Gb)
2305                 max_speed = 32;
2306         else if (phba->lmt & LMT_16Gb)
2307                 max_speed = 16;
2308         else if (phba->lmt & LMT_10Gb)
2309                 max_speed = 10;
2310         else if (phba->lmt & LMT_8Gb)
2311                 max_speed = 8;
2312         else if (phba->lmt & LMT_4Gb)
2313                 max_speed = 4;
2314         else if (phba->lmt & LMT_2Gb)
2315                 max_speed = 2;
2316         else if (phba->lmt & LMT_1Gb)
2317                 max_speed = 1;
2318         else
2319                 max_speed = 0;
2320
2321         vp = &phba->vpd;
2322
2323         switch (dev_id) {
2324         case PCI_DEVICE_ID_FIREFLY:
2325                 m = (typeof(m)){"LP6000", "PCI",
2326                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2327                 break;
2328         case PCI_DEVICE_ID_SUPERFLY:
2329                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
2330                         m = (typeof(m)){"LP7000", "PCI", ""};
2331                 else
2332                         m = (typeof(m)){"LP7000E", "PCI", ""};
2333                 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2334                 break;
2335         case PCI_DEVICE_ID_DRAGONFLY:
2336                 m = (typeof(m)){"LP8000", "PCI",
2337                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2338                 break;
2339         case PCI_DEVICE_ID_CENTAUR:
2340                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
2341                         m = (typeof(m)){"LP9002", "PCI", ""};
2342                 else
2343                         m = (typeof(m)){"LP9000", "PCI", ""};
2344                 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2345                 break;
2346         case PCI_DEVICE_ID_RFLY:
2347                 m = (typeof(m)){"LP952", "PCI",
2348                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2349                 break;
2350         case PCI_DEVICE_ID_PEGASUS:
2351                 m = (typeof(m)){"LP9802", "PCI-X",
2352                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2353                 break;
2354         case PCI_DEVICE_ID_THOR:
2355                 m = (typeof(m)){"LP10000", "PCI-X",
2356                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2357                 break;
2358         case PCI_DEVICE_ID_VIPER:
2359                 m = (typeof(m)){"LPX1000",  "PCI-X",
2360                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2361                 break;
2362         case PCI_DEVICE_ID_PFLY:
2363                 m = (typeof(m)){"LP982", "PCI-X",
2364                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2365                 break;
2366         case PCI_DEVICE_ID_TFLY:
2367                 m = (typeof(m)){"LP1050", "PCI-X",
2368                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2369                 break;
2370         case PCI_DEVICE_ID_HELIOS:
2371                 m = (typeof(m)){"LP11000", "PCI-X2",
2372                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2373                 break;
2374         case PCI_DEVICE_ID_HELIOS_SCSP:
2375                 m = (typeof(m)){"LP11000-SP", "PCI-X2",
2376                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2377                 break;
2378         case PCI_DEVICE_ID_HELIOS_DCSP:
2379                 m = (typeof(m)){"LP11002-SP",  "PCI-X2",
2380                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2381                 break;
2382         case PCI_DEVICE_ID_NEPTUNE:
2383                 m = (typeof(m)){"LPe1000", "PCIe",
2384                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2385                 break;
2386         case PCI_DEVICE_ID_NEPTUNE_SCSP:
2387                 m = (typeof(m)){"LPe1000-SP", "PCIe",
2388                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2389                 break;
2390         case PCI_DEVICE_ID_NEPTUNE_DCSP:
2391                 m = (typeof(m)){"LPe1002-SP", "PCIe",
2392                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2393                 break;
2394         case PCI_DEVICE_ID_BMID:
2395                 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
2396                 break;
2397         case PCI_DEVICE_ID_BSMB:
2398                 m = (typeof(m)){"LP111", "PCI-X2",
2399                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2400                 break;
2401         case PCI_DEVICE_ID_ZEPHYR:
2402                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2403                 break;
2404         case PCI_DEVICE_ID_ZEPHYR_SCSP:
2405                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2406                 break;
2407         case PCI_DEVICE_ID_ZEPHYR_DCSP:
2408                 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
2409                 GE = 1;
2410                 break;
2411         case PCI_DEVICE_ID_ZMID:
2412                 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
2413                 break;
2414         case PCI_DEVICE_ID_ZSMB:
2415                 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
2416                 break;
2417         case PCI_DEVICE_ID_LP101:
2418                 m = (typeof(m)){"LP101", "PCI-X",
2419                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2420                 break;
2421         case PCI_DEVICE_ID_LP10000S:
2422                 m = (typeof(m)){"LP10000-S", "PCI",
2423                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2424                 break;
2425         case PCI_DEVICE_ID_LP11000S:
2426                 m = (typeof(m)){"LP11000-S", "PCI-X2",
2427                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2428                 break;
2429         case PCI_DEVICE_ID_LPE11000S:
2430                 m = (typeof(m)){"LPe11000-S", "PCIe",
2431                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2432                 break;
2433         case PCI_DEVICE_ID_SAT:
2434                 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
2435                 break;
2436         case PCI_DEVICE_ID_SAT_MID:
2437                 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
2438                 break;
2439         case PCI_DEVICE_ID_SAT_SMB:
2440                 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
2441                 break;
2442         case PCI_DEVICE_ID_SAT_DCSP:
2443                 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
2444                 break;
2445         case PCI_DEVICE_ID_SAT_SCSP:
2446                 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
2447                 break;
2448         case PCI_DEVICE_ID_SAT_S:
2449                 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
2450                 break;
2451         case PCI_DEVICE_ID_HORNET:
2452                 m = (typeof(m)){"LP21000", "PCIe",
2453                                 "Obsolete, Unsupported FCoE Adapter"};
2454                 GE = 1;
2455                 break;
2456         case PCI_DEVICE_ID_PROTEUS_VF:
2457                 m = (typeof(m)){"LPev12000", "PCIe IOV",
2458                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2459                 break;
2460         case PCI_DEVICE_ID_PROTEUS_PF:
2461                 m = (typeof(m)){"LPev12000", "PCIe IOV",
2462                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2463                 break;
2464         case PCI_DEVICE_ID_PROTEUS_S:
2465                 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2466                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2467                 break;
2468         case PCI_DEVICE_ID_TIGERSHARK:
2469                 oneConnect = 1;
2470                 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
2471                 break;
2472         case PCI_DEVICE_ID_TOMCAT:
2473                 oneConnect = 1;
2474                 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2475                 break;
2476         case PCI_DEVICE_ID_FALCON:
2477                 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2478                                 "EmulexSecure Fibre"};
2479                 break;
2480         case PCI_DEVICE_ID_BALIUS:
2481                 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2482                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2483                 break;
2484         case PCI_DEVICE_ID_LANCER_FC:
2485                 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
2486                 break;
2487         case PCI_DEVICE_ID_LANCER_FC_VF:
2488                 m = (typeof(m)){"LPe16000", "PCIe",
2489                                 "Obsolete, Unsupported Fibre Channel Adapter"};
2490                 break;
2491         case PCI_DEVICE_ID_LANCER_FCOE:
2492                 oneConnect = 1;
2493                 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
2494                 break;
2495         case PCI_DEVICE_ID_LANCER_FCOE_VF:
2496                 oneConnect = 1;
2497                 m = (typeof(m)){"OCe15100", "PCIe",
2498                                 "Obsolete, Unsupported FCoE"};
2499                 break;
2500         case PCI_DEVICE_ID_LANCER_G6_FC:
2501                 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2502                 break;
2503         case PCI_DEVICE_ID_LANCER_G7_FC:
2504                 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2505                 break;
2506         case PCI_DEVICE_ID_SKYHAWK:
2507         case PCI_DEVICE_ID_SKYHAWK_VF:
2508                 oneConnect = 1;
2509                 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2510                 break;
2511         default:
2512                 m = (typeof(m)){"Unknown", "", ""};
2513                 break;
2514         }
2515
2516         if (mdp && mdp[0] == '\0')
2517                 snprintf(mdp, 79,"%s", m.name);
2518         /*
2519          * oneConnect hba requires special processing, they are all initiators
2520          * and we put the port number on the end
2521          */
2522         if (descp && descp[0] == '\0') {
2523                 if (oneConnect)
2524                         snprintf(descp, 255,
2525                                 "Emulex OneConnect %s, %s Initiator %s",
2526                                 m.name, m.function,
2527                                 phba->Port);
2528                 else if (max_speed == 0)
2529                         snprintf(descp, 255,
2530                                 "Emulex %s %s %s",
2531                                 m.name, m.bus, m.function);
2532                 else
2533                         snprintf(descp, 255,
2534                                 "Emulex %s %d%s %s %s",
2535                                 m.name, max_speed, (GE) ? "GE" : "Gb",
2536                                 m.bus, m.function);
2537         }
2538 }
2539
2540 /**
2541  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
2542  * @phba: pointer to lpfc hba data structure.
2543  * @pring: pointer to a IOCB ring.
2544  * @cnt: the number of IOCBs to be posted to the IOCB ring.
2545  *
2546  * This routine posts a given number of IOCBs with the associated DMA buffer
2547  * descriptors specified by the cnt argument to the given IOCB ring.
2548  *
2549  * Return codes
2550  *   The number of IOCBs NOT able to be posted to the IOCB ring.
2551  **/
2552 int
2553 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2554 {
2555         IOCB_t *icmd;
2556         struct lpfc_iocbq *iocb;
2557         struct lpfc_dmabuf *mp1, *mp2;
2558
2559         cnt += pring->missbufcnt;
2560
2561         /* While there are buffers to post */
2562         while (cnt > 0) {
2563                 /* Allocate buffer for  command iocb */
2564                 iocb = lpfc_sli_get_iocbq(phba);
2565                 if (iocb == NULL) {
2566                         pring->missbufcnt = cnt;
2567                         return cnt;
2568                 }
2569                 icmd = &iocb->iocb;
2570
2571                 /* 2 buffers can be posted per command */
2572                 /* Allocate buffer to post */
2573                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2574                 if (mp1)
2575                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2576                 if (!mp1 || !mp1->virt) {
2577                         kfree(mp1);
2578                         lpfc_sli_release_iocbq(phba, iocb);
2579                         pring->missbufcnt = cnt;
2580                         return cnt;
2581                 }
2582
2583                 INIT_LIST_HEAD(&mp1->list);
2584                 /* Allocate buffer to post */
2585                 if (cnt > 1) {
2586                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2587                         if (mp2)
2588                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2589                                                             &mp2->phys);
2590                         if (!mp2 || !mp2->virt) {
2591                                 kfree(mp2);
2592                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2593                                 kfree(mp1);
2594                                 lpfc_sli_release_iocbq(phba, iocb);
2595                                 pring->missbufcnt = cnt;
2596                                 return cnt;
2597                         }
2598
2599                         INIT_LIST_HEAD(&mp2->list);
2600                 } else {
2601                         mp2 = NULL;
2602                 }
2603
2604                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2605                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2606                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2607                 icmd->ulpBdeCount = 1;
2608                 cnt--;
2609                 if (mp2) {
2610                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2611                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2612                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2613                         cnt--;
2614                         icmd->ulpBdeCount = 2;
2615                 }
2616
2617                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2618                 icmd->ulpLe = 1;
2619
2620                 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2621                     IOCB_ERROR) {
2622                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2623                         kfree(mp1);
2624                         cnt++;
2625                         if (mp2) {
2626                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2627                                 kfree(mp2);
2628                                 cnt++;
2629                         }
2630                         lpfc_sli_release_iocbq(phba, iocb);
2631                         pring->missbufcnt = cnt;
2632                         return cnt;
2633                 }
2634                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2635                 if (mp2)
2636                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2637         }
2638         pring->missbufcnt = 0;
2639         return 0;
2640 }
2641
2642 /**
2643  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2644  * @phba: pointer to lpfc hba data structure.
2645  *
2646  * This routine posts initial receive IOCB buffers to the ELS ring. The
2647  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2648  * set to 64 IOCBs. SLI3 only.
2649  *
2650  * Return codes
2651  *   0 - success (currently always success)
2652  **/
2653 static int
2654 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2655 {
2656         struct lpfc_sli *psli = &phba->sli;
2657
2658         /* Ring 0, ELS / CT buffers */
2659         lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2660         /* Ring 2 - FCP no buffers needed */
2661
2662         return 0;
2663 }
2664
2665 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2666
2667 /**
2668  * lpfc_sha_init - Set up initial array of hash table entries
2669  * @HashResultPointer: pointer to an array as hash table.
2670  *
2671  * This routine sets up the initial values to the array of hash table entries
2672  * for the LC HBAs.
2673  **/
2674 static void
2675 lpfc_sha_init(uint32_t * HashResultPointer)
2676 {
2677         HashResultPointer[0] = 0x67452301;
2678         HashResultPointer[1] = 0xEFCDAB89;
2679         HashResultPointer[2] = 0x98BADCFE;
2680         HashResultPointer[3] = 0x10325476;
2681         HashResultPointer[4] = 0xC3D2E1F0;
2682 }
2683
2684 /**
2685  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2686  * @HashResultPointer: pointer to an initial/result hash table.
2687  * @HashWorkingPointer: pointer to an working hash table.
2688  *
2689  * This routine iterates an initial hash table pointed by @HashResultPointer
2690  * with the values from the working hash table pointeed by @HashWorkingPointer.
2691  * The results are putting back to the initial hash table, returned through
2692  * the @HashResultPointer as the result hash table.
2693  **/
2694 static void
2695 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2696 {
2697         int t;
2698         uint32_t TEMP;
2699         uint32_t A, B, C, D, E;
2700         t = 16;
2701         do {
2702                 HashWorkingPointer[t] =
2703                     S(1,
2704                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2705                                                                      8] ^
2706                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2707         } while (++t <= 79);
2708         t = 0;
2709         A = HashResultPointer[0];
2710         B = HashResultPointer[1];
2711         C = HashResultPointer[2];
2712         D = HashResultPointer[3];
2713         E = HashResultPointer[4];
2714
2715         do {
2716                 if (t < 20) {
2717                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2718                 } else if (t < 40) {
2719                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2720                 } else if (t < 60) {
2721                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2722                 } else {
2723                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2724                 }
2725                 TEMP += S(5, A) + E + HashWorkingPointer[t];
2726                 E = D;
2727                 D = C;
2728                 C = S(30, B);
2729                 B = A;
2730                 A = TEMP;
2731         } while (++t <= 79);
2732
2733         HashResultPointer[0] += A;
2734         HashResultPointer[1] += B;
2735         HashResultPointer[2] += C;
2736         HashResultPointer[3] += D;
2737         HashResultPointer[4] += E;
2738
2739 }
2740
2741 /**
2742  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2743  * @RandomChallenge: pointer to the entry of host challenge random number array.
2744  * @HashWorking: pointer to the entry of the working hash array.
2745  *
2746  * This routine calculates the working hash array referred by @HashWorking
2747  * from the challenge random numbers associated with the host, referred by
2748  * @RandomChallenge. The result is put into the entry of the working hash
2749  * array and returned by reference through @HashWorking.
2750  **/
2751 static void
2752 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2753 {
2754         *HashWorking = (*RandomChallenge ^ *HashWorking);
2755 }
2756
2757 /**
2758  * lpfc_hba_init - Perform special handling for LC HBA initialization
2759  * @phba: pointer to lpfc hba data structure.
2760  * @hbainit: pointer to an array of unsigned 32-bit integers.
2761  *
2762  * This routine performs the special handling for LC HBA initialization.
2763  **/
2764 void
2765 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2766 {
2767         int t;
2768         uint32_t *HashWorking;
2769         uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2770
2771         HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2772         if (!HashWorking)
2773                 return;
2774
2775         HashWorking[0] = HashWorking[78] = *pwwnn++;
2776         HashWorking[1] = HashWorking[79] = *pwwnn;
2777
2778         for (t = 0; t < 7; t++)
2779                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2780
2781         lpfc_sha_init(hbainit);
2782         lpfc_sha_iterate(hbainit, HashWorking);
2783         kfree(HashWorking);
2784 }
2785
2786 /**
2787  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2788  * @vport: pointer to a virtual N_Port data structure.
2789  *
2790  * This routine performs the necessary cleanups before deleting the @vport.
2791  * It invokes the discovery state machine to perform necessary state
2792  * transitions and to release the ndlps associated with the @vport. Note,
2793  * the physical port is treated as @vport 0.
2794  **/
2795 void
2796 lpfc_cleanup(struct lpfc_vport *vport)
2797 {
2798         struct lpfc_hba   *phba = vport->phba;
2799         struct lpfc_nodelist *ndlp, *next_ndlp;
2800         int i = 0;
2801
2802         if (phba->link_state > LPFC_LINK_DOWN)
2803                 lpfc_port_link_failure(vport);
2804
2805         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2806                 if (!NLP_CHK_NODE_ACT(ndlp)) {
2807                         ndlp = lpfc_enable_node(vport, ndlp,
2808                                                 NLP_STE_UNUSED_NODE);
2809                         if (!ndlp)
2810                                 continue;
2811                         spin_lock_irq(&phba->ndlp_lock);
2812                         NLP_SET_FREE_REQ(ndlp);
2813                         spin_unlock_irq(&phba->ndlp_lock);
2814                         /* Trigger the release of the ndlp memory */
2815                         lpfc_nlp_put(ndlp);
2816                         continue;
2817                 }
2818                 spin_lock_irq(&phba->ndlp_lock);
2819                 if (NLP_CHK_FREE_REQ(ndlp)) {
2820                         /* The ndlp should not be in memory free mode already */
2821                         spin_unlock_irq(&phba->ndlp_lock);
2822                         continue;
2823                 } else
2824                         /* Indicate request for freeing ndlp memory */
2825                         NLP_SET_FREE_REQ(ndlp);
2826                 spin_unlock_irq(&phba->ndlp_lock);
2827
2828                 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2829                     ndlp->nlp_DID == Fabric_DID) {
2830                         /* Just free up ndlp with Fabric_DID for vports */
2831                         lpfc_nlp_put(ndlp);
2832                         continue;
2833                 }
2834
2835                 /* take care of nodes in unused state before the state
2836                  * machine taking action.
2837                  */
2838                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2839                         lpfc_nlp_put(ndlp);
2840                         continue;
2841                 }
2842
2843                 if (ndlp->nlp_type & NLP_FABRIC)
2844                         lpfc_disc_state_machine(vport, ndlp, NULL,
2845                                         NLP_EVT_DEVICE_RECOVERY);
2846
2847                 lpfc_disc_state_machine(vport, ndlp, NULL,
2848                                              NLP_EVT_DEVICE_RM);
2849         }
2850
2851         /* At this point, ALL ndlp's should be gone
2852          * because of the previous NLP_EVT_DEVICE_RM.
2853          * Lets wait for this to happen, if needed.
2854          */
2855         while (!list_empty(&vport->fc_nodes)) {
2856                 if (i++ > 3000) {
2857                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2858                                 "0233 Nodelist not empty\n");
2859                         list_for_each_entry_safe(ndlp, next_ndlp,
2860                                                 &vport->fc_nodes, nlp_listp) {
2861                                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2862                                                 LOG_NODE,
2863                                                 "0282 did:x%x ndlp:x%p "
2864                                                 "usgmap:x%x refcnt:%d\n",
2865                                                 ndlp->nlp_DID, (void *)ndlp,
2866                                                 ndlp->nlp_usg_map,
2867                                                 kref_read(&ndlp->kref));
2868                         }
2869                         break;
2870                 }
2871
2872                 /* Wait for any activity on ndlps to settle */
2873                 msleep(10);
2874         }
2875         lpfc_cleanup_vports_rrqs(vport, NULL);
2876 }
2877
2878 /**
2879  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2880  * @vport: pointer to a virtual N_Port data structure.
2881  *
2882  * This routine stops all the timers associated with a @vport. This function
2883  * is invoked before disabling or deleting a @vport. Note that the physical
2884  * port is treated as @vport 0.
2885  **/
2886 void
2887 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2888 {
2889         del_timer_sync(&vport->els_tmofunc);
2890         del_timer_sync(&vport->delayed_disc_tmo);
2891         lpfc_can_disctmo(vport);
2892         return;
2893 }
2894
2895 /**
2896  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2897  * @phba: pointer to lpfc hba data structure.
2898  *
2899  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2900  * caller of this routine should already hold the host lock.
2901  **/
2902 void
2903 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2904 {
2905         /* Clear pending FCF rediscovery wait flag */
2906         phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2907
2908         /* Now, try to stop the timer */
2909         del_timer(&phba->fcf.redisc_wait);
2910 }
2911
2912 /**
2913  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2914  * @phba: pointer to lpfc hba data structure.
2915  *
2916  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2917  * checks whether the FCF rediscovery wait timer is pending with the host
2918  * lock held before proceeding with disabling the timer and clearing the
2919  * wait timer pendig flag.
2920  **/
2921 void
2922 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2923 {
2924         spin_lock_irq(&phba->hbalock);
2925         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2926                 /* FCF rediscovery timer already fired or stopped */
2927                 spin_unlock_irq(&phba->hbalock);
2928                 return;
2929         }
2930         __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2931         /* Clear failover in progress flags */
2932         phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2933         spin_unlock_irq(&phba->hbalock);
2934 }
2935
2936 /**
2937  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2938  * @phba: pointer to lpfc hba data structure.
2939  *
2940  * This routine stops all the timers associated with a HBA. This function is
2941  * invoked before either putting a HBA offline or unloading the driver.
2942  **/
2943 void
2944 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2945 {
2946         lpfc_stop_vport_timers(phba->pport);
2947         del_timer_sync(&phba->sli.mbox_tmo);
2948         del_timer_sync(&phba->fabric_block_timer);
2949         del_timer_sync(&phba->eratt_poll);
2950         del_timer_sync(&phba->hb_tmofunc);
2951         if (phba->sli_rev == LPFC_SLI_REV4) {
2952                 del_timer_sync(&phba->rrq_tmr);
2953                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2954         }
2955         phba->hb_outstanding = 0;
2956
2957         switch (phba->pci_dev_grp) {
2958         case LPFC_PCI_DEV_LP:
2959                 /* Stop any LightPulse device specific driver timers */
2960                 del_timer_sync(&phba->fcp_poll_timer);
2961                 break;
2962         case LPFC_PCI_DEV_OC:
2963                 /* Stop any OneConnect device sepcific driver timers */
2964                 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2965                 break;
2966         default:
2967                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2968                                 "0297 Invalid device group (x%x)\n",
2969                                 phba->pci_dev_grp);
2970                 break;
2971         }
2972         return;
2973 }
2974
2975 /**
2976  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2977  * @phba: pointer to lpfc hba data structure.
2978  *
2979  * This routine marks a HBA's management interface as blocked. Once the HBA's
2980  * management interface is marked as blocked, all the user space access to
2981  * the HBA, whether they are from sysfs interface or libdfc interface will
2982  * all be blocked. The HBA is set to block the management interface when the
2983  * driver prepares the HBA interface for online or offline.
2984  **/
2985 static void
2986 lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
2987 {
2988         unsigned long iflag;
2989         uint8_t actcmd = MBX_HEARTBEAT;
2990         unsigned long timeout;
2991
2992         spin_lock_irqsave(&phba->hbalock, iflag);
2993         phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2994         spin_unlock_irqrestore(&phba->hbalock, iflag);
2995         if (mbx_action == LPFC_MBX_NO_WAIT)
2996                 return;
2997         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2998         spin_lock_irqsave(&phba->hbalock, iflag);
2999         if (phba->sli.mbox_active) {
3000                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
3001                 /* Determine how long we might wait for the active mailbox
3002                  * command to be gracefully completed by firmware.
3003                  */
3004                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3005                                 phba->sli.mbox_active) * 1000) + jiffies;
3006         }
3007         spin_unlock_irqrestore(&phba->hbalock, iflag);
3008
3009         /* Wait for the outstnading mailbox command to complete */
3010         while (phba->sli.mbox_active) {
3011                 /* Check active mailbox complete status every 2ms */
3012                 msleep(2);
3013                 if (time_after(jiffies, timeout)) {
3014                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3015                                 "2813 Mgmt IO is Blocked %x "
3016                                 "- mbox cmd %x still active\n",
3017                                 phba->sli.sli_flag, actcmd);
3018                         break;
3019                 }
3020         }
3021 }
3022
3023 /**
3024  * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3025  * @phba: pointer to lpfc hba data structure.
3026  *
3027  * Allocate RPIs for all active remote nodes. This is needed whenever
3028  * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3029  * is to fixup the temporary rpi assignments.
3030  **/
3031 void
3032 lpfc_sli4_node_prep(struct lpfc_hba *phba)
3033 {
3034         struct lpfc_nodelist  *ndlp, *next_ndlp;
3035         struct lpfc_vport **vports;
3036         int i, rpi;
3037         unsigned long flags;
3038
3039         if (phba->sli_rev != LPFC_SLI_REV4)
3040                 return;
3041
3042         vports = lpfc_create_vport_work_array(phba);
3043         if (vports == NULL)
3044                 return;
3045
3046         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3047                 if (vports[i]->load_flag & FC_UNLOADING)
3048                         continue;
3049
3050                 list_for_each_entry_safe(ndlp, next_ndlp,
3051                                          &vports[i]->fc_nodes,
3052                                          nlp_listp) {
3053                         if (!NLP_CHK_NODE_ACT(ndlp))
3054                                 continue;
3055                         rpi = lpfc_sli4_alloc_rpi(phba);
3056                         if (rpi == LPFC_RPI_ALLOC_ERROR) {
3057                                 spin_lock_irqsave(&phba->ndlp_lock, flags);
3058                                 NLP_CLR_NODE_ACT(ndlp);
3059                                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3060                                 continue;
3061                         }
3062                         ndlp->nlp_rpi = rpi;
3063                         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3064                                          "0009 rpi:%x DID:%x "
3065                                          "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3066                                          ndlp->nlp_DID, ndlp->nlp_flag,
3067                                          ndlp->nlp_usg_map, ndlp);
3068                 }
3069         }
3070         lpfc_destroy_vport_work_array(phba, vports);
3071 }
3072
3073 /**
3074  * lpfc_online - Initialize and bring a HBA online
3075  * @phba: pointer to lpfc hba data structure.
3076  *
3077  * This routine initializes the HBA and brings a HBA online. During this
3078  * process, the management interface is blocked to prevent user space access
3079  * to the HBA interfering with the driver initialization.
3080  *
3081  * Return codes
3082  *   0 - successful
3083  *   1 - failed
3084  **/
3085 int
3086 lpfc_online(struct lpfc_hba *phba)
3087 {
3088         struct lpfc_vport *vport;
3089         struct lpfc_vport **vports;
3090         int i, error = 0;
3091         bool vpis_cleared = false;
3092
3093         if (!phba)
3094                 return 0;
3095         vport = phba->pport;
3096
3097         if (!(vport->fc_flag & FC_OFFLINE_MODE))
3098                 return 0;
3099
3100         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3101                         "0458 Bring Adapter online\n");
3102
3103         lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
3104
3105         if (phba->sli_rev == LPFC_SLI_REV4) {
3106                 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3107                         lpfc_unblock_mgmt_io(phba);
3108                         return 1;
3109                 }
3110                 spin_lock_irq(&phba->hbalock);
3111                 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3112                         vpis_cleared = true;
3113                 spin_unlock_irq(&phba->hbalock);
3114
3115                 /* Reestablish the local initiator port.
3116                  * The offline process destroyed the previous lport.
3117                  */
3118                 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3119                                 !phba->nvmet_support) {
3120                         error = lpfc_nvme_create_localport(phba->pport);
3121                         if (error)
3122                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3123                                         "6132 NVME restore reg failed "
3124                                         "on nvmei error x%x\n", error);
3125                 }
3126         } else {
3127                 lpfc_sli_queue_init(phba);
3128                 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3129                         lpfc_unblock_mgmt_io(phba);
3130                         return 1;
3131                 }
3132         }
3133
3134         vports = lpfc_create_vport_work_array(phba);
3135         if (vports != NULL) {
3136                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3137                         struct Scsi_Host *shost;
3138                         shost = lpfc_shost_from_vport(vports[i]);
3139                         spin_lock_irq(shost->host_lock);
3140                         vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3141                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3142                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3143                         if (phba->sli_rev == LPFC_SLI_REV4) {
3144                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
3145                                 if ((vpis_cleared) &&
3146                                     (vports[i]->port_type !=
3147                                         LPFC_PHYSICAL_PORT))
3148                                         vports[i]->vpi = 0;
3149                         }
3150                         spin_unlock_irq(shost->host_lock);
3151                 }
3152         }
3153         lpfc_destroy_vport_work_array(phba, vports);
3154
3155         lpfc_unblock_mgmt_io(phba);
3156         return 0;
3157 }
3158
3159 /**
3160  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
3161  * @phba: pointer to lpfc hba data structure.
3162  *
3163  * This routine marks a HBA's management interface as not blocked. Once the
3164  * HBA's management interface is marked as not blocked, all the user space
3165  * access to the HBA, whether they are from sysfs interface or libdfc
3166  * interface will be allowed. The HBA is set to block the management interface
3167  * when the driver prepares the HBA interface for online or offline and then
3168  * set to unblock the management interface afterwards.
3169  **/
3170 void
3171 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3172 {
3173         unsigned long iflag;
3174
3175         spin_lock_irqsave(&phba->hbalock, iflag);
3176         phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3177         spin_unlock_irqrestore(&phba->hbalock, iflag);
3178 }
3179
3180 /**
3181  * lpfc_offline_prep - Prepare a HBA to be brought offline
3182  * @phba: pointer to lpfc hba data structure.
3183  *
3184  * This routine is invoked to prepare a HBA to be brought offline. It performs
3185  * unregistration login to all the nodes on all vports and flushes the mailbox
3186  * queue to make it ready to be brought offline.
3187  **/
3188 void
3189 lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
3190 {
3191         struct lpfc_vport *vport = phba->pport;
3192         struct lpfc_nodelist  *ndlp, *next_ndlp;
3193         struct lpfc_vport **vports;
3194         struct Scsi_Host *shost;
3195         int i;
3196
3197         if (vport->fc_flag & FC_OFFLINE_MODE)
3198                 return;
3199
3200         lpfc_block_mgmt_io(phba, mbx_action);
3201
3202         lpfc_linkdown(phba);
3203
3204         /* Issue an unreg_login to all nodes on all vports */
3205         vports = lpfc_create_vport_work_array(phba);
3206         if (vports != NULL) {
3207                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3208                         if (vports[i]->load_flag & FC_UNLOADING)
3209                                 continue;
3210                         shost = lpfc_shost_from_vport(vports[i]);
3211                         spin_lock_irq(shost->host_lock);
3212                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
3213                         vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3214                         vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
3215                         spin_unlock_irq(shost->host_lock);
3216
3217                         shost = lpfc_shost_from_vport(vports[i]);
3218                         list_for_each_entry_safe(ndlp, next_ndlp,
3219                                                  &vports[i]->fc_nodes,
3220                                                  nlp_listp) {
3221                                 if (!NLP_CHK_NODE_ACT(ndlp))
3222                                         continue;
3223                                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3224                                         continue;
3225                                 if (ndlp->nlp_type & NLP_FABRIC) {
3226                                         lpfc_disc_state_machine(vports[i], ndlp,
3227                                                 NULL, NLP_EVT_DEVICE_RECOVERY);
3228                                         lpfc_disc_state_machine(vports[i], ndlp,
3229                                                 NULL, NLP_EVT_DEVICE_RM);
3230                                 }
3231                                 spin_lock_irq(shost->host_lock);
3232                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3233                                 spin_unlock_irq(shost->host_lock);
3234                                 /*
3235                                  * Whenever an SLI4 port goes offline, free the
3236                                  * RPI. Get a new RPI when the adapter port
3237                                  * comes back online.
3238                                  */
3239                                 if (phba->sli_rev == LPFC_SLI_REV4) {
3240                                         lpfc_printf_vlog(ndlp->vport,
3241                                                          KERN_INFO, LOG_NODE,
3242                                                          "0011 lpfc_offline: "
3243                                                          "ndlp:x%p did %x "
3244                                                          "usgmap:x%x rpi:%x\n",
3245                                                          ndlp, ndlp->nlp_DID,
3246                                                          ndlp->nlp_usg_map,
3247                                                          ndlp->nlp_rpi);
3248
3249                                         lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
3250                                 }
3251                                 lpfc_unreg_rpi(vports[i], ndlp);
3252                         }
3253                 }
3254         }
3255         lpfc_destroy_vport_work_array(phba, vports);
3256
3257         lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
3258
3259         if (phba->wq)
3260                 flush_workqueue(phba->wq);
3261 }
3262
3263 /**
3264  * lpfc_offline - Bring a HBA offline
3265  * @phba: pointer to lpfc hba data structure.
3266  *
3267  * This routine actually brings a HBA offline. It stops all the timers
3268  * associated with the HBA, brings down the SLI layer, and eventually
3269  * marks the HBA as in offline state for the upper layer protocol.
3270  **/
3271 void
3272 lpfc_offline(struct lpfc_hba *phba)
3273 {
3274         struct Scsi_Host  *shost;
3275         struct lpfc_vport **vports;
3276         int i;
3277
3278         if (phba->pport->fc_flag & FC_OFFLINE_MODE)
3279                 return;
3280
3281         /* stop port and all timers associated with this hba */
3282         lpfc_stop_port(phba);
3283
3284         /* Tear down the local and target port registrations.  The
3285          * nvme transports need to cleanup.
3286          */
3287         lpfc_nvmet_destroy_targetport(phba);
3288         lpfc_nvme_destroy_localport(phba->pport);
3289
3290         vports = lpfc_create_vport_work_array(phba);
3291         if (vports != NULL)
3292                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3293                         lpfc_stop_vport_timers(vports[i]);
3294         lpfc_destroy_vport_work_array(phba, vports);
3295         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3296                         "0460 Bring Adapter offline\n");
3297         /* Bring down the SLI Layer and cleanup.  The HBA is offline
3298            now.  */
3299         lpfc_sli_hba_down(phba);
3300         spin_lock_irq(&phba->hbalock);
3301         phba->work_ha = 0;
3302         spin_unlock_irq(&phba->hbalock);
3303         vports = lpfc_create_vport_work_array(phba);
3304         if (vports != NULL)
3305                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3306                         shost = lpfc_shost_from_vport(vports[i]);
3307                         spin_lock_irq(shost->host_lock);
3308                         vports[i]->work_port_events = 0;
3309                         vports[i]->fc_flag |= FC_OFFLINE_MODE;
3310                         spin_unlock_irq(shost->host_lock);
3311                 }
3312         lpfc_destroy_vport_work_array(phba, vports);
3313 }
3314
3315 /**
3316  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
3317  * @phba: pointer to lpfc hba data structure.
3318  *
3319  * This routine is to free all the SCSI buffers and IOCBs from the driver
3320  * list back to kernel. It is called from lpfc_pci_remove_one to free
3321  * the internal resources before the device is removed from the system.
3322  **/
3323 static void
3324 lpfc_scsi_free(struct lpfc_hba *phba)
3325 {
3326         struct lpfc_scsi_buf *sb, *sb_next;
3327
3328         if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3329                 return;
3330
3331         spin_lock_irq(&phba->hbalock);
3332
3333         /* Release all the lpfc_scsi_bufs maintained by this host. */
3334
3335         spin_lock(&phba->scsi_buf_list_put_lock);
3336         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3337                                  list) {
3338                 list_del(&sb->list);
3339                 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3340                               sb->dma_handle);
3341                 kfree(sb);
3342                 phba->total_scsi_bufs--;
3343         }
3344         spin_unlock(&phba->scsi_buf_list_put_lock);
3345
3346         spin_lock(&phba->scsi_buf_list_get_lock);
3347         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3348                                  list) {
3349                 list_del(&sb->list);
3350                 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3351                               sb->dma_handle);
3352                 kfree(sb);
3353                 phba->total_scsi_bufs--;
3354         }
3355         spin_unlock(&phba->scsi_buf_list_get_lock);
3356         spin_unlock_irq(&phba->hbalock);
3357 }
3358 /**
3359  * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3360  * @phba: pointer to lpfc hba data structure.
3361  *
3362  * This routine is to free all the NVME buffers and IOCBs from the driver
3363  * list back to kernel. It is called from lpfc_pci_remove_one to free
3364  * the internal resources before the device is removed from the system.
3365  **/
3366 static void
3367 lpfc_nvme_free(struct lpfc_hba *phba)
3368 {
3369         struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
3370
3371         if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3372                 return;
3373
3374         spin_lock_irq(&phba->hbalock);
3375
3376         /* Release all the lpfc_nvme_bufs maintained by this host. */
3377         spin_lock(&phba->nvme_buf_list_put_lock);
3378         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3379                                  &phba->lpfc_nvme_buf_list_put, list) {
3380                 list_del(&lpfc_ncmd->list);
3381                 phba->put_nvme_bufs--;
3382                 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3383                               lpfc_ncmd->dma_handle);
3384                 kfree(lpfc_ncmd);
3385                 phba->total_nvme_bufs--;
3386         }
3387         spin_unlock(&phba->nvme_buf_list_put_lock);
3388
3389         spin_lock(&phba->nvme_buf_list_get_lock);
3390         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3391                                  &phba->lpfc_nvme_buf_list_get, list) {
3392                 list_del(&lpfc_ncmd->list);
3393                 phba->get_nvme_bufs--;
3394                 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3395                               lpfc_ncmd->dma_handle);
3396                 kfree(lpfc_ncmd);
3397                 phba->total_nvme_bufs--;
3398         }
3399         spin_unlock(&phba->nvme_buf_list_get_lock);
3400         spin_unlock_irq(&phba->hbalock);
3401 }
3402 /**
3403  * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
3404  * @phba: pointer to lpfc hba data structure.
3405  *
3406  * This routine first calculates the sizes of the current els and allocated
3407  * scsi sgl lists, and then goes through all sgls to updates the physical
3408  * XRIs assigned due to port function reset. During port initialization, the
3409  * current els and allocated scsi sgl lists are 0s.
3410  *
3411  * Return codes
3412  *   0 - successful (for now, it always returns 0)
3413  **/
3414 int
3415 lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
3416 {
3417         struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3418         uint16_t i, lxri, xri_cnt, els_xri_cnt;
3419         LIST_HEAD(els_sgl_list);
3420         int rc;
3421
3422         /*
3423          * update on pci function's els xri-sgl list
3424          */
3425         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3426
3427         if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3428                 /* els xri-sgl expanded */
3429                 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3430                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3431                                 "3157 ELS xri-sgl count increased from "
3432                                 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3433                                 els_xri_cnt);
3434                 /* allocate the additional els sgls */
3435                 for (i = 0; i < xri_cnt; i++) {
3436                         sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3437                                              GFP_KERNEL);
3438                         if (sglq_entry == NULL) {
3439                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3440                                                 "2562 Failure to allocate an "
3441                                                 "ELS sgl entry:%d\n", i);
3442                                 rc = -ENOMEM;
3443                                 goto out_free_mem;
3444                         }
3445                         sglq_entry->buff_type = GEN_BUFF_TYPE;
3446                         sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3447                                                            &sglq_entry->phys);
3448                         if (sglq_entry->virt == NULL) {
3449                                 kfree(sglq_entry);
3450                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3451                                                 "2563 Failure to allocate an "
3452                                                 "ELS mbuf:%d\n", i);
3453                                 rc = -ENOMEM;
3454                                 goto out_free_mem;
3455                         }
3456                         sglq_entry->sgl = sglq_entry->virt;
3457                         memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3458                         sglq_entry->state = SGL_FREED;
3459                         list_add_tail(&sglq_entry->list, &els_sgl_list);
3460                 }
3461                 spin_lock_irq(&phba->hbalock);
3462                 spin_lock(&phba->sli4_hba.sgl_list_lock);
3463                 list_splice_init(&els_sgl_list,
3464                                  &phba->sli4_hba.lpfc_els_sgl_list);
3465                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3466                 spin_unlock_irq(&phba->hbalock);
3467         } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3468                 /* els xri-sgl shrinked */
3469                 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3470                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3471                                 "3158 ELS xri-sgl count decreased from "
3472                                 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3473                                 els_xri_cnt);
3474                 spin_lock_irq(&phba->hbalock);
3475                 spin_lock(&phba->sli4_hba.sgl_list_lock);
3476                 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3477                                  &els_sgl_list);
3478                 /* release extra els sgls from list */
3479                 for (i = 0; i < xri_cnt; i++) {
3480                         list_remove_head(&els_sgl_list,
3481                                          sglq_entry, struct lpfc_sglq, list);
3482                         if (sglq_entry) {
3483                                 __lpfc_mbuf_free(phba, sglq_entry->virt,
3484                                                  sglq_entry->phys);
3485                                 kfree(sglq_entry);
3486                         }
3487                 }
3488                 list_splice_init(&els_sgl_list,
3489                                  &phba->sli4_hba.lpfc_els_sgl_list);
3490                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3491                 spin_unlock_irq(&phba->hbalock);
3492         } else
3493                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3494                                 "3163 ELS xri-sgl count unchanged: %d\n",
3495                                 els_xri_cnt);
3496         phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3497
3498         /* update xris to els sgls on the list */
3499         sglq_entry = NULL;
3500         sglq_entry_next = NULL;
3501         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3502                                  &phba->sli4_hba.lpfc_els_sgl_list, list) {
3503                 lxri = lpfc_sli4_next_xritag(phba);
3504                 if (lxri == NO_XRI) {
3505                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3506                                         "2400 Failed to allocate xri for "
3507                                         "ELS sgl\n");
3508                         rc = -ENOMEM;
3509                         goto out_free_mem;
3510                 }
3511                 sglq_entry->sli4_lxritag = lxri;
3512                 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3513         }
3514         return 0;
3515
3516 out_free_mem:
3517         lpfc_free_els_sgl_list(phba);
3518         return rc;
3519 }
3520
3521 /**
3522  * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3523  * @phba: pointer to lpfc hba data structure.
3524  *
3525  * This routine first calculates the sizes of the current els and allocated
3526  * scsi sgl lists, and then goes through all sgls to updates the physical
3527  * XRIs assigned due to port function reset. During port initialization, the
3528  * current els and allocated scsi sgl lists are 0s.
3529  *
3530  * Return codes
3531  *   0 - successful (for now, it always returns 0)
3532  **/
3533 int
3534 lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3535 {
3536         struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3537         uint16_t i, lxri, xri_cnt, els_xri_cnt;
3538         uint16_t nvmet_xri_cnt;
3539         LIST_HEAD(nvmet_sgl_list);
3540         int rc;
3541
3542         /*
3543          * update on pci function's nvmet xri-sgl list
3544          */
3545         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3546
3547         /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3548         nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3549         if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3550                 /* els xri-sgl expanded */
3551                 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3552                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3553                                 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3554                                 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3555                 /* allocate the additional nvmet sgls */
3556                 for (i = 0; i < xri_cnt; i++) {
3557                         sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3558                                              GFP_KERNEL);
3559                         if (sglq_entry == NULL) {
3560                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3561                                                 "6303 Failure to allocate an "
3562                                                 "NVMET sgl entry:%d\n", i);
3563                                 rc = -ENOMEM;
3564                                 goto out_free_mem;