• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
lpfc_config_port_prep(struct lpfc_hba * phba)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
lpfc_config_async_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)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
lpfc_dump_wakeup_param_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)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
lpfc_update_vport_wwn(struct lpfc_vport * vport)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
lpfc_config_port_post(struct lpfc_hba * phba)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->context1;
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->context1;
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->context1 = 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
lpfc_hba_init_link(struct lpfc_hba * phba,uint32_t flag)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
lpfc_hba_init_link_fc_topology(struct lpfc_hba * phba,uint32_t fc_topology,uint32_t flag)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
lpfc_hba_down_link(struct lpfc_hba * phba,uint32_t flag)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
lpfc_hba_down_prep(struct lpfc_hba * phba)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
lpfc_sli4_free_sp_events(struct lpfc_hba * phba)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
lpfc_hba_free_post_buf(struct lpfc_hba * phba)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
lpfc_hba_clean_txcmplq(struct lpfc_hba * phba)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
lpfc_hba_down_post_s3(struct lpfc_hba * phba)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
lpfc_hba_down_post_s4(struct lpfc_hba * phba)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
lpfc_hba_down_post(struct lpfc_hba * phba)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
lpfc_hb_timeout(struct timer_list * t)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
lpfc_rrq_timeout(struct timer_list * t)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
lpfc_hb_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)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
lpfc_hb_timeout_handler(struct lpfc_hba * phba)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
lpfc_offline_eratt(struct lpfc_hba * phba)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
lpfc_sli4_offline_eratt(struct lpfc_hba * phba)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
lpfc_handle_deferred_eratt(struct lpfc_hba * phba)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
lpfc_board_errevt_to_mgmt(struct lpfc_hba * phba)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
lpfc_handle_eratt_s3(struct lpfc_hba * phba)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
lpfc_sli4_port_sta_fn_reset(struct lpfc_hba * phba,int mbx_action,bool en_rn_msg)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
lpfc_handle_eratt_s4(struct lpfc_hba * phba)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
lpfc_handle_eratt(struct lpfc_hba * phba)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
lpfc_handle_latt(struct lpfc_hba * phba)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
lpfc_parse_vpd(struct lpfc_hba * phba,uint8_t * vpd,int len)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
lpfc_get_hba_model_desc(struct lpfc_hba * phba,uint8_t * mdp,uint8_t * descp)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
lpfc_post_buffer(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,int cnt)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
lpfc_post_rcv_buf(struct lpfc_hba * phba)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
lpfc_sha_init(uint32_t * HashResultPointer)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
lpfc_sha_iterate(uint32_t * HashResultPointer,uint32_t * HashWorkingPointer)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
lpfc_challenge_key(uint32_t * RandomChallenge,uint32_t * HashWorking)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
lpfc_hba_init(struct lpfc_hba * phba,uint32_t * hbainit)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
lpfc_cleanup(struct lpfc_vport * vport)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
lpfc_stop_vport_timers(struct lpfc_vport * vport)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
__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba * phba)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
lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba * phba)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
lpfc_stop_hba_timers(struct lpfc_hba * phba)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
lpfc_block_mgmt_io(struct lpfc_hba * phba,int mbx_action)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
lpfc_sli4_node_prep(struct lpfc_hba * phba)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
lpfc_online(struct lpfc_hba * phba)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
lpfc_unblock_mgmt_io(struct lpfc_hba * phba)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
lpfc_offline_prep(struct lpfc_hba * phba,int mbx_action)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
lpfc_offline(struct lpfc_hba * phba)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
lpfc_scsi_free(struct lpfc_hba * phba)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
lpfc_nvme_free(struct lpfc_hba * phba)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
lpfc_sli4_els_sgl_update(struct lpfc_hba * phba)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
lpfc_sli4_nvmet_sgl_update(struct lpfc_hba * phba)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;
3565 			}
3566 			sglq_entry->buff_type = NVMET_BUFF_TYPE;
3567 			sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3568 							   &sglq_entry->phys);
3569 			if (sglq_entry->virt == NULL) {
3570 				kfree(sglq_entry);
3571 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3572 						"6304 Failure to allocate an "
3573 						"NVMET buf:%d\n", i);
3574 				rc = -ENOMEM;
3575 				goto out_free_mem;
3576 			}
3577 			sglq_entry->sgl = sglq_entry->virt;
3578 			memset(sglq_entry->sgl, 0,
3579 			       phba->cfg_sg_dma_buf_size);
3580 			sglq_entry->state = SGL_FREED;
3581 			list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3582 		}
3583 		spin_lock_irq(&phba->hbalock);
3584 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3585 		list_splice_init(&nvmet_sgl_list,
3586 				 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3587 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3588 		spin_unlock_irq(&phba->hbalock);
3589 	} else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3590 		/* nvmet xri-sgl shrunk */
3591 		xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3592 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3593 				"6305 NVMET xri-sgl count decreased from "
3594 				"%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3595 				nvmet_xri_cnt);
3596 		spin_lock_irq(&phba->hbalock);
3597 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3598 		list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3599 				 &nvmet_sgl_list);
3600 		/* release extra nvmet sgls from list */
3601 		for (i = 0; i < xri_cnt; i++) {
3602 			list_remove_head(&nvmet_sgl_list,
3603 					 sglq_entry, struct lpfc_sglq, list);
3604 			if (sglq_entry) {
3605 				lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3606 						    sglq_entry->phys);
3607 				kfree(sglq_entry);
3608 			}
3609 		}
3610 		list_splice_init(&nvmet_sgl_list,
3611 				 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3612 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3613 		spin_unlock_irq(&phba->hbalock);
3614 	} else
3615 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3616 				"6306 NVMET xri-sgl count unchanged: %d\n",
3617 				nvmet_xri_cnt);
3618 	phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3619 
3620 	/* update xris to nvmet sgls on the list */
3621 	sglq_entry = NULL;
3622 	sglq_entry_next = NULL;
3623 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3624 				 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3625 		lxri = lpfc_sli4_next_xritag(phba);
3626 		if (lxri == NO_XRI) {
3627 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3628 					"6307 Failed to allocate xri for "
3629 					"NVMET sgl\n");
3630 			rc = -ENOMEM;
3631 			goto out_free_mem;
3632 		}
3633 		sglq_entry->sli4_lxritag = lxri;
3634 		sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3635 	}
3636 	return 0;
3637 
3638 out_free_mem:
3639 	lpfc_free_nvmet_sgl_list(phba);
3640 	return rc;
3641 }
3642 
3643 /**
3644  * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3645  * @phba: pointer to lpfc hba data structure.
3646  *
3647  * This routine first calculates the sizes of the current els and allocated
3648  * scsi sgl lists, and then goes through all sgls to updates the physical
3649  * XRIs assigned due to port function reset. During port initialization, the
3650  * current els and allocated scsi sgl lists are 0s.
3651  *
3652  * Return codes
3653  *   0 - successful (for now, it always returns 0)
3654  **/
3655 int
lpfc_sli4_scsi_sgl_update(struct lpfc_hba * phba)3656 lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3657 {
3658 	struct lpfc_scsi_buf *psb, *psb_next;
3659 	uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3660 	LIST_HEAD(scsi_sgl_list);
3661 	int rc;
3662 
3663 	/*
3664 	 * update on pci function's els xri-sgl list
3665 	 */
3666 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3667 	phba->total_scsi_bufs = 0;
3668 
3669 	/*
3670 	 * update on pci function's allocated scsi xri-sgl list
3671 	 */
3672 	/* maximum number of xris available for scsi buffers */
3673 	phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3674 				      els_xri_cnt;
3675 
3676 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3677 		return 0;
3678 
3679 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3680 		phba->sli4_hba.scsi_xri_max =  /* Split them up */
3681 			(phba->sli4_hba.scsi_xri_max *
3682 			 phba->cfg_xri_split) / 100;
3683 
3684 	spin_lock_irq(&phba->scsi_buf_list_get_lock);
3685 	spin_lock(&phba->scsi_buf_list_put_lock);
3686 	list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3687 	list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
3688 	spin_unlock(&phba->scsi_buf_list_put_lock);
3689 	spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3690 
3691 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3692 			"6060 Current allocated SCSI xri-sgl count:%d, "
3693 			"maximum  SCSI xri count:%d (split:%d)\n",
3694 			phba->sli4_hba.scsi_xri_cnt,
3695 			phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3696 
3697 	if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3698 		/* max scsi xri shrinked below the allocated scsi buffers */
3699 		scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3700 					phba->sli4_hba.scsi_xri_max;
3701 		/* release the extra allocated scsi buffers */
3702 		for (i = 0; i < scsi_xri_cnt; i++) {
3703 			list_remove_head(&scsi_sgl_list, psb,
3704 					 struct lpfc_scsi_buf, list);
3705 			if (psb) {
3706 				dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3707 					      psb->data, psb->dma_handle);
3708 				kfree(psb);
3709 			}
3710 		}
3711 		spin_lock_irq(&phba->scsi_buf_list_get_lock);
3712 		phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
3713 		spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3714 	}
3715 
3716 	/* update xris associated to remaining allocated scsi buffers */
3717 	psb = NULL;
3718 	psb_next = NULL;
3719 	list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3720 		lxri = lpfc_sli4_next_xritag(phba);
3721 		if (lxri == NO_XRI) {
3722 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3723 					"2560 Failed to allocate xri for "
3724 					"scsi buffer\n");
3725 			rc = -ENOMEM;
3726 			goto out_free_mem;
3727 		}
3728 		psb->cur_iocbq.sli4_lxritag = lxri;
3729 		psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3730 	}
3731 	spin_lock_irq(&phba->scsi_buf_list_get_lock);
3732 	spin_lock(&phba->scsi_buf_list_put_lock);
3733 	list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3734 	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
3735 	spin_unlock(&phba->scsi_buf_list_put_lock);
3736 	spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3737 	return 0;
3738 
3739 out_free_mem:
3740 	lpfc_scsi_free(phba);
3741 	return rc;
3742 }
3743 
3744 static uint64_t
lpfc_get_wwpn(struct lpfc_hba * phba)3745 lpfc_get_wwpn(struct lpfc_hba *phba)
3746 {
3747 	uint64_t wwn;
3748 	int rc;
3749 	LPFC_MBOXQ_t *mboxq;
3750 	MAILBOX_t *mb;
3751 
3752 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3753 						GFP_KERNEL);
3754 	if (!mboxq)
3755 		return (uint64_t)-1;
3756 
3757 	/* First get WWN of HBA instance */
3758 	lpfc_read_nv(phba, mboxq);
3759 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3760 	if (rc != MBX_SUCCESS) {
3761 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3762 				"6019 Mailbox failed , mbxCmd x%x "
3763 				"READ_NV, mbxStatus x%x\n",
3764 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3765 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3766 		mempool_free(mboxq, phba->mbox_mem_pool);
3767 		return (uint64_t) -1;
3768 	}
3769 	mb = &mboxq->u.mb;
3770 	memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3771 	/* wwn is WWPN of HBA instance */
3772 	mempool_free(mboxq, phba->mbox_mem_pool);
3773 	if (phba->sli_rev == LPFC_SLI_REV4)
3774 		return be64_to_cpu(wwn);
3775 	else
3776 		return rol64(wwn, 32);
3777 }
3778 
3779 /**
3780  * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3781  * @phba: pointer to lpfc hba data structure.
3782  *
3783  * This routine first calculates the sizes of the current els and allocated
3784  * scsi sgl lists, and then goes through all sgls to updates the physical
3785  * XRIs assigned due to port function reset. During port initialization, the
3786  * current els and allocated scsi sgl lists are 0s.
3787  *
3788  * Return codes
3789  *   0 - successful (for now, it always returns 0)
3790  **/
3791 int
lpfc_sli4_nvme_sgl_update(struct lpfc_hba * phba)3792 lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3793 {
3794 	struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3795 	uint16_t i, lxri, els_xri_cnt;
3796 	uint16_t nvme_xri_cnt, nvme_xri_max;
3797 	LIST_HEAD(nvme_sgl_list);
3798 	int rc, cnt;
3799 
3800 	phba->total_nvme_bufs = 0;
3801 	phba->get_nvme_bufs = 0;
3802 	phba->put_nvme_bufs = 0;
3803 
3804 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3805 		return 0;
3806 	/*
3807 	 * update on pci function's allocated nvme xri-sgl list
3808 	 */
3809 
3810 	/* maximum number of xris available for nvme buffers */
3811 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3812 	nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3813 	phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3814 	phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3815 
3816 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3817 			"6074 Current allocated NVME xri-sgl count:%d, "
3818 			"maximum  NVME xri count:%d\n",
3819 			phba->sli4_hba.nvme_xri_cnt,
3820 			phba->sli4_hba.nvme_xri_max);
3821 
3822 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
3823 	spin_lock(&phba->nvme_buf_list_put_lock);
3824 	list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3825 	list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3826 	cnt = phba->get_nvme_bufs + phba->put_nvme_bufs;
3827 	phba->get_nvme_bufs = 0;
3828 	phba->put_nvme_bufs = 0;
3829 	spin_unlock(&phba->nvme_buf_list_put_lock);
3830 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3831 
3832 	if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3833 		/* max nvme xri shrunk below the allocated nvme buffers */
3834 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
3835 		nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3836 					phba->sli4_hba.nvme_xri_max;
3837 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3838 		/* release the extra allocated nvme buffers */
3839 		for (i = 0; i < nvme_xri_cnt; i++) {
3840 			list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3841 					 struct lpfc_nvme_buf, list);
3842 			if (lpfc_ncmd) {
3843 				dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3844 					      lpfc_ncmd->data,
3845 					      lpfc_ncmd->dma_handle);
3846 				kfree(lpfc_ncmd);
3847 			}
3848 		}
3849 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
3850 		phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3851 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3852 	}
3853 
3854 	/* update xris associated to remaining allocated nvme buffers */
3855 	lpfc_ncmd = NULL;
3856 	lpfc_ncmd_next = NULL;
3857 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3858 				 &nvme_sgl_list, list) {
3859 		lxri = lpfc_sli4_next_xritag(phba);
3860 		if (lxri == NO_XRI) {
3861 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3862 					"6075 Failed to allocate xri for "
3863 					"nvme buffer\n");
3864 			rc = -ENOMEM;
3865 			goto out_free_mem;
3866 		}
3867 		lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3868 		lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3869 	}
3870 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
3871 	spin_lock(&phba->nvme_buf_list_put_lock);
3872 	list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3873 	phba->get_nvme_bufs = cnt;
3874 	INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3875 	spin_unlock(&phba->nvme_buf_list_put_lock);
3876 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3877 	return 0;
3878 
3879 out_free_mem:
3880 	lpfc_nvme_free(phba);
3881 	return rc;
3882 }
3883 
3884 /**
3885  * lpfc_create_port - Create an FC port
3886  * @phba: pointer to lpfc hba data structure.
3887  * @instance: a unique integer ID to this FC port.
3888  * @dev: pointer to the device data structure.
3889  *
3890  * This routine creates a FC port for the upper layer protocol. The FC port
3891  * can be created on top of either a physical port or a virtual port provided
3892  * by the HBA. This routine also allocates a SCSI host data structure (shost)
3893  * and associates the FC port created before adding the shost into the SCSI
3894  * layer.
3895  *
3896  * Return codes
3897  *   @vport - pointer to the virtual N_Port data structure.
3898  *   NULL - port create failed.
3899  **/
3900 struct lpfc_vport *
lpfc_create_port(struct lpfc_hba * phba,int instance,struct device * dev)3901 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
3902 {
3903 	struct lpfc_vport *vport;
3904 	struct Scsi_Host  *shost = NULL;
3905 	int error = 0;
3906 	int i;
3907 	uint64_t wwn;
3908 	bool use_no_reset_hba = false;
3909 	int rc;
3910 
3911 	if (lpfc_no_hba_reset_cnt) {
3912 		if (phba->sli_rev < LPFC_SLI_REV4 &&
3913 		    dev == &phba->pcidev->dev) {
3914 			/* Reset the port first */
3915 			lpfc_sli_brdrestart(phba);
3916 			rc = lpfc_sli_chipset_init(phba);
3917 			if (rc)
3918 				return NULL;
3919 		}
3920 		wwn = lpfc_get_wwpn(phba);
3921 	}
3922 
3923 	for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3924 		if (wwn == lpfc_no_hba_reset[i]) {
3925 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3926 					"6020 Setting use_no_reset port=%llx\n",
3927 					wwn);
3928 			use_no_reset_hba = true;
3929 			break;
3930 		}
3931 	}
3932 
3933 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3934 		if (dev != &phba->pcidev->dev) {
3935 			shost = scsi_host_alloc(&lpfc_vport_template,
3936 						sizeof(struct lpfc_vport));
3937 		} else {
3938 			if (!use_no_reset_hba)
3939 				shost = scsi_host_alloc(&lpfc_template,
3940 						sizeof(struct lpfc_vport));
3941 			else
3942 				shost = scsi_host_alloc(&lpfc_template_no_hr,
3943 						sizeof(struct lpfc_vport));
3944 		}
3945 	} else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3946 		shost = scsi_host_alloc(&lpfc_template_nvme,
3947 					sizeof(struct lpfc_vport));
3948 	}
3949 	if (!shost)
3950 		goto out;
3951 
3952 	vport = (struct lpfc_vport *) shost->hostdata;
3953 	vport->phba = phba;
3954 	vport->load_flag |= FC_LOADING;
3955 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3956 	vport->fc_rscn_flush = 0;
3957 	lpfc_get_vport_cfgparam(vport);
3958 
3959 	shost->unique_id = instance;
3960 	shost->max_id = LPFC_MAX_TARGET;
3961 	shost->max_lun = vport->cfg_max_luns;
3962 	shost->this_id = -1;
3963 	shost->max_cmd_len = 16;
3964 	shost->nr_hw_queues = phba->cfg_fcp_io_channel;
3965 	if (phba->sli_rev == LPFC_SLI_REV4) {
3966 		shost->dma_boundary =
3967 			phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
3968 		shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3969 	}
3970 
3971 	/*
3972 	 * Set initial can_queue value since 0 is no longer supported and
3973 	 * scsi_add_host will fail. This will be adjusted later based on the
3974 	 * max xri value determined in hba setup.
3975 	 */
3976 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
3977 	if (dev != &phba->pcidev->dev) {
3978 		shost->transportt = lpfc_vport_transport_template;
3979 		vport->port_type = LPFC_NPIV_PORT;
3980 	} else {
3981 		shost->transportt = lpfc_transport_template;
3982 		vport->port_type = LPFC_PHYSICAL_PORT;
3983 	}
3984 
3985 	/* Initialize all internally managed lists. */
3986 	INIT_LIST_HEAD(&vport->fc_nodes);
3987 	INIT_LIST_HEAD(&vport->rcv_buffer_list);
3988 	spin_lock_init(&vport->work_port_lock);
3989 
3990 	timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
3991 
3992 	timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
3993 
3994 	timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
3995 
3996 	error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
3997 	if (error)
3998 		goto out_put_shost;
3999 
4000 	spin_lock_irq(&phba->hbalock);
4001 	list_add_tail(&vport->listentry, &phba->port_list);
4002 	spin_unlock_irq(&phba->hbalock);
4003 	return vport;
4004 
4005 out_put_shost:
4006 	scsi_host_put(shost);
4007 out:
4008 	return NULL;
4009 }
4010 
4011 /**
4012  * destroy_port -  destroy an FC port
4013  * @vport: pointer to an lpfc virtual N_Port data structure.
4014  *
4015  * This routine destroys a FC port from the upper layer protocol. All the
4016  * resources associated with the port are released.
4017  **/
4018 void
destroy_port(struct lpfc_vport * vport)4019 destroy_port(struct lpfc_vport *vport)
4020 {
4021 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4022 	struct lpfc_hba  *phba = vport->phba;
4023 
4024 	lpfc_debugfs_terminate(vport);
4025 	fc_remove_host(shost);
4026 	scsi_remove_host(shost);
4027 
4028 	spin_lock_irq(&phba->hbalock);
4029 	list_del_init(&vport->listentry);
4030 	spin_unlock_irq(&phba->hbalock);
4031 
4032 	lpfc_cleanup(vport);
4033 	return;
4034 }
4035 
4036 /**
4037  * lpfc_get_instance - Get a unique integer ID
4038  *
4039  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4040  * uses the kernel idr facility to perform the task.
4041  *
4042  * Return codes:
4043  *   instance - a unique integer ID allocated as the new instance.
4044  *   -1 - lpfc get instance failed.
4045  **/
4046 int
lpfc_get_instance(void)4047 lpfc_get_instance(void)
4048 {
4049 	int ret;
4050 
4051 	ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4052 	return ret < 0 ? -1 : ret;
4053 }
4054 
4055 /**
4056  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
4057  * @shost: pointer to SCSI host data structure.
4058  * @time: elapsed time of the scan in jiffies.
4059  *
4060  * This routine is called by the SCSI layer with a SCSI host to determine
4061  * whether the scan host is finished.
4062  *
4063  * Note: there is no scan_start function as adapter initialization will have
4064  * asynchronously kicked off the link initialization.
4065  *
4066  * Return codes
4067  *   0 - SCSI host scan is not over yet.
4068  *   1 - SCSI host scan is over.
4069  **/
lpfc_scan_finished(struct Scsi_Host * shost,unsigned long time)4070 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4071 {
4072 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4073 	struct lpfc_hba   *phba = vport->phba;
4074 	int stat = 0;
4075 
4076 	spin_lock_irq(shost->host_lock);
4077 
4078 	if (vport->load_flag & FC_UNLOADING) {
4079 		stat = 1;
4080 		goto finished;
4081 	}
4082 	if (time >= msecs_to_jiffies(30 * 1000)) {
4083 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4084 				"0461 Scanning longer than 30 "
4085 				"seconds.  Continuing initialization\n");
4086 		stat = 1;
4087 		goto finished;
4088 	}
4089 	if (time >= msecs_to_jiffies(15 * 1000) &&
4090 	    phba->link_state <= LPFC_LINK_DOWN) {
4091 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4092 				"0465 Link down longer than 15 "
4093 				"seconds.  Continuing initialization\n");
4094 		stat = 1;
4095 		goto finished;
4096 	}
4097 
4098 	if (vport->port_state != LPFC_VPORT_READY)
4099 		goto finished;
4100 	if (vport->num_disc_nodes || vport->fc_prli_sent)
4101 		goto finished;
4102 	if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
4103 		goto finished;
4104 	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
4105 		goto finished;
4106 
4107 	stat = 1;
4108 
4109 finished:
4110 	spin_unlock_irq(shost->host_lock);
4111 	return stat;
4112 }
4113 
4114 /**
4115  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
4116  * @shost: pointer to SCSI host data structure.
4117  *
4118  * This routine initializes a given SCSI host attributes on a FC port. The
4119  * SCSI host can be either on top of a physical port or a virtual port.
4120  **/
lpfc_host_attrib_init(struct Scsi_Host * shost)4121 void lpfc_host_attrib_init(struct Scsi_Host *shost)
4122 {
4123 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4124 	struct lpfc_hba   *phba = vport->phba;
4125 	/*
4126 	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
4127 	 */
4128 
4129 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4130 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4131 	fc_host_supported_classes(shost) = FC_COS_CLASS3;
4132 
4133 	memset(fc_host_supported_fc4s(shost), 0,
4134 	       sizeof(fc_host_supported_fc4s(shost)));
4135 	fc_host_supported_fc4s(shost)[2] = 1;
4136 	fc_host_supported_fc4s(shost)[7] = 1;
4137 
4138 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4139 				 sizeof fc_host_symbolic_name(shost));
4140 
4141 	fc_host_supported_speeds(shost) = 0;
4142 	if (phba->lmt & LMT_64Gb)
4143 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4144 	if (phba->lmt & LMT_32Gb)
4145 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4146 	if (phba->lmt & LMT_16Gb)
4147 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4148 	if (phba->lmt & LMT_10Gb)
4149 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4150 	if (phba->lmt & LMT_8Gb)
4151 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4152 	if (phba->lmt & LMT_4Gb)
4153 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4154 	if (phba->lmt & LMT_2Gb)
4155 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4156 	if (phba->lmt & LMT_1Gb)
4157 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4158 
4159 	fc_host_maxframe_size(shost) =
4160 		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4161 		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
4162 
4163 	fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4164 
4165 	/* This value is also unchanging */
4166 	memset(fc_host_active_fc4s(shost), 0,
4167 	       sizeof(fc_host_active_fc4s(shost)));
4168 	fc_host_active_fc4s(shost)[2] = 1;
4169 	fc_host_active_fc4s(shost)[7] = 1;
4170 
4171 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
4172 	spin_lock_irq(shost->host_lock);
4173 	vport->load_flag &= ~FC_LOADING;
4174 	spin_unlock_irq(shost->host_lock);
4175 }
4176 
4177 /**
4178  * lpfc_stop_port_s3 - Stop SLI3 device port
4179  * @phba: pointer to lpfc hba data structure.
4180  *
4181  * This routine is invoked to stop an SLI3 device port, it stops the device
4182  * from generating interrupts and stops the device driver's timers for the
4183  * device.
4184  **/
4185 static void
lpfc_stop_port_s3(struct lpfc_hba * phba)4186 lpfc_stop_port_s3(struct lpfc_hba *phba)
4187 {
4188 	/* Clear all interrupt enable conditions */
4189 	writel(0, phba->HCregaddr);
4190 	readl(phba->HCregaddr); /* flush */
4191 	/* Clear all pending interrupts */
4192 	writel(0xffffffff, phba->HAregaddr);
4193 	readl(phba->HAregaddr); /* flush */
4194 
4195 	/* Reset some HBA SLI setup states */
4196 	lpfc_stop_hba_timers(phba);
4197 	phba->pport->work_port_events = 0;
4198 }
4199 
4200 /**
4201  * lpfc_stop_port_s4 - Stop SLI4 device port
4202  * @phba: pointer to lpfc hba data structure.
4203  *
4204  * This routine is invoked to stop an SLI4 device port, it stops the device
4205  * from generating interrupts and stops the device driver's timers for the
4206  * device.
4207  **/
4208 static void
lpfc_stop_port_s4(struct lpfc_hba * phba)4209 lpfc_stop_port_s4(struct lpfc_hba *phba)
4210 {
4211 	/* Reset some HBA SLI4 setup states */
4212 	lpfc_stop_hba_timers(phba);
4213 	phba->pport->work_port_events = 0;
4214 	phba->sli4_hba.intr_enable = 0;
4215 }
4216 
4217 /**
4218  * lpfc_stop_port - Wrapper function for stopping hba port
4219  * @phba: Pointer to HBA context object.
4220  *
4221  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4222  * the API jump table function pointer from the lpfc_hba struct.
4223  **/
4224 void
lpfc_stop_port(struct lpfc_hba * phba)4225 lpfc_stop_port(struct lpfc_hba *phba)
4226 {
4227 	phba->lpfc_stop_port(phba);
4228 
4229 	if (phba->wq)
4230 		flush_workqueue(phba->wq);
4231 }
4232 
4233 /**
4234  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4235  * @phba: Pointer to hba for which this call is being executed.
4236  *
4237  * This routine starts the timer waiting for the FCF rediscovery to complete.
4238  **/
4239 void
lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba * phba)4240 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4241 {
4242 	unsigned long fcf_redisc_wait_tmo =
4243 		(jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4244 	/* Start fcf rediscovery wait period timer */
4245 	mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4246 	spin_lock_irq(&phba->hbalock);
4247 	/* Allow action to new fcf asynchronous event */
4248 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4249 	/* Mark the FCF rediscovery pending state */
4250 	phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4251 	spin_unlock_irq(&phba->hbalock);
4252 }
4253 
4254 /**
4255  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4256  * @ptr: Map to lpfc_hba data structure pointer.
4257  *
4258  * This routine is invoked when waiting for FCF table rediscover has been
4259  * timed out. If new FCF record(s) has (have) been discovered during the
4260  * wait period, a new FCF event shall be added to the FCOE async event
4261  * list, and then worker thread shall be waked up for processing from the
4262  * worker thread context.
4263  **/
4264 static void
lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list * t)4265 lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
4266 {
4267 	struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
4268 
4269 	/* Don't send FCF rediscovery event if timer cancelled */
4270 	spin_lock_irq(&phba->hbalock);
4271 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4272 		spin_unlock_irq(&phba->hbalock);
4273 		return;
4274 	}
4275 	/* Clear FCF rediscovery timer pending flag */
4276 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4277 	/* FCF rediscovery event to worker thread */
4278 	phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4279 	spin_unlock_irq(&phba->hbalock);
4280 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
4281 			"2776 FCF rediscover quiescent timer expired\n");
4282 	/* wake up worker thread */
4283 	lpfc_worker_wake_up(phba);
4284 }
4285 
4286 /**
4287  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4288  * @phba: pointer to lpfc hba data structure.
4289  * @acqe_link: pointer to the async link completion queue entry.
4290  *
4291  * This routine is to parse the SLI4 link-attention link fault code.
4292  **/
4293 static void
lpfc_sli4_parse_latt_fault(struct lpfc_hba * phba,struct lpfc_acqe_link * acqe_link)4294 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4295 			   struct lpfc_acqe_link *acqe_link)
4296 {
4297 	switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4298 	case LPFC_ASYNC_LINK_FAULT_NONE:
4299 	case LPFC_ASYNC_LINK_FAULT_LOCAL:
4300 	case LPFC_ASYNC_LINK_FAULT_REMOTE:
4301 	case LPFC_ASYNC_LINK_FAULT_LR_LRR:
4302 		break;
4303 	default:
4304 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4305 				"0398 Unknown link fault code: x%x\n",
4306 				bf_get(lpfc_acqe_link_fault, acqe_link));
4307 		break;
4308 	}
4309 }
4310 
4311 /**
4312  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4313  * @phba: pointer to lpfc hba data structure.
4314  * @acqe_link: pointer to the async link completion queue entry.
4315  *
4316  * This routine is to parse the SLI4 link attention type and translate it
4317  * into the base driver's link attention type coding.
4318  *
4319  * Return: Link attention type in terms of base driver's coding.
4320  **/
4321 static uint8_t
lpfc_sli4_parse_latt_type(struct lpfc_hba * phba,struct lpfc_acqe_link * acqe_link)4322 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4323 			  struct lpfc_acqe_link *acqe_link)
4324 {
4325 	uint8_t att_type;
4326 
4327 	switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4328 	case LPFC_ASYNC_LINK_STATUS_DOWN:
4329 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
4330 		att_type = LPFC_ATT_LINK_DOWN;
4331 		break;
4332 	case LPFC_ASYNC_LINK_STATUS_UP:
4333 		/* Ignore physical link up events - wait for logical link up */
4334 		att_type = LPFC_ATT_RESERVED;
4335 		break;
4336 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
4337 		att_type = LPFC_ATT_LINK_UP;
4338 		break;
4339 	default:
4340 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4341 				"0399 Invalid link attention type: x%x\n",
4342 				bf_get(lpfc_acqe_link_status, acqe_link));
4343 		att_type = LPFC_ATT_RESERVED;
4344 		break;
4345 	}
4346 	return att_type;
4347 }
4348 
4349 /**
4350  * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4351  * @phba: pointer to lpfc hba data structure.
4352  *
4353  * This routine is to get an SLI3 FC port's link speed in Mbps.
4354  *
4355  * Return: link speed in terms of Mbps.
4356  **/
4357 uint32_t
lpfc_sli_port_speed_get(struct lpfc_hba * phba)4358 lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4359 {
4360 	uint32_t link_speed;
4361 
4362 	if (!lpfc_is_link_up(phba))
4363 		return 0;
4364 
4365 	if (phba->sli_rev <= LPFC_SLI_REV3) {
4366 		switch (phba->fc_linkspeed) {
4367 		case LPFC_LINK_SPEED_1GHZ:
4368 			link_speed = 1000;
4369 			break;
4370 		case LPFC_LINK_SPEED_2GHZ:
4371 			link_speed = 2000;
4372 			break;
4373 		case LPFC_LINK_SPEED_4GHZ:
4374 			link_speed = 4000;
4375 			break;
4376 		case LPFC_LINK_SPEED_8GHZ:
4377 			link_speed = 8000;
4378 			break;
4379 		case LPFC_LINK_SPEED_10GHZ:
4380 			link_speed = 10000;
4381 			break;
4382 		case LPFC_LINK_SPEED_16GHZ:
4383 			link_speed = 16000;
4384 			break;
4385 		default:
4386 			link_speed = 0;
4387 		}
4388 	} else {
4389 		if (phba->sli4_hba.link_state.logical_speed)
4390 			link_speed =
4391 			      phba->sli4_hba.link_state.logical_speed;
4392 		else
4393 			link_speed = phba->sli4_hba.link_state.speed;
4394 	}
4395 	return link_speed;
4396 }
4397 
4398 /**
4399  * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4400  * @phba: pointer to lpfc hba data structure.
4401  * @evt_code: asynchronous event code.
4402  * @speed_code: asynchronous event link speed code.
4403  *
4404  * This routine is to parse the giving SLI4 async event link speed code into
4405  * value of Mbps for the link speed.
4406  *
4407  * Return: link speed in terms of Mbps.
4408  **/
4409 static uint32_t
lpfc_sli4_port_speed_parse(struct lpfc_hba * phba,uint32_t evt_code,uint8_t speed_code)4410 lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4411 			   uint8_t speed_code)
4412 {
4413 	uint32_t port_speed;
4414 
4415 	switch (evt_code) {
4416 	case LPFC_TRAILER_CODE_LINK:
4417 		switch (speed_code) {
4418 		case LPFC_ASYNC_LINK_SPEED_ZERO:
4419 			port_speed = 0;
4420 			break;
4421 		case LPFC_ASYNC_LINK_SPEED_10MBPS:
4422 			port_speed = 10;
4423 			break;
4424 		case LPFC_ASYNC_LINK_SPEED_100MBPS:
4425 			port_speed = 100;
4426 			break;
4427 		case LPFC_ASYNC_LINK_SPEED_1GBPS:
4428 			port_speed = 1000;
4429 			break;
4430 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
4431 			port_speed = 10000;
4432 			break;
4433 		case LPFC_ASYNC_LINK_SPEED_20GBPS:
4434 			port_speed = 20000;
4435 			break;
4436 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
4437 			port_speed = 25000;
4438 			break;
4439 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
4440 			port_speed = 40000;
4441 			break;
4442 		default:
4443 			port_speed = 0;
4444 		}
4445 		break;
4446 	case LPFC_TRAILER_CODE_FC:
4447 		switch (speed_code) {
4448 		case LPFC_FC_LA_SPEED_UNKNOWN:
4449 			port_speed = 0;
4450 			break;
4451 		case LPFC_FC_LA_SPEED_1G:
4452 			port_speed = 1000;
4453 			break;
4454 		case LPFC_FC_LA_SPEED_2G:
4455 			port_speed = 2000;
4456 			break;
4457 		case LPFC_FC_LA_SPEED_4G:
4458 			port_speed = 4000;
4459 			break;
4460 		case LPFC_FC_LA_SPEED_8G:
4461 			port_speed = 8000;
4462 			break;
4463 		case LPFC_FC_LA_SPEED_10G:
4464 			port_speed = 10000;
4465 			break;
4466 		case LPFC_FC_LA_SPEED_16G:
4467 			port_speed = 16000;
4468 			break;
4469 		case LPFC_FC_LA_SPEED_32G:
4470 			port_speed = 32000;
4471 			break;
4472 		case LPFC_FC_LA_SPEED_64G:
4473 			port_speed = 64000;
4474 			break;
4475 		default:
4476 			port_speed = 0;
4477 		}
4478 		break;
4479 	default:
4480 		port_speed = 0;
4481 	}
4482 	return port_speed;
4483 }
4484 
4485 /**
4486  * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
4487  * @phba: pointer to lpfc hba data structure.
4488  * @acqe_link: pointer to the async link completion queue entry.
4489  *
4490  * This routine is to handle the SLI4 asynchronous FCoE link event.
4491  **/
4492 static void
lpfc_sli4_async_link_evt(struct lpfc_hba * phba,struct lpfc_acqe_link * acqe_link)4493 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4494 			 struct lpfc_acqe_link *acqe_link)
4495 {
4496 	struct lpfc_dmabuf *mp;
4497 	LPFC_MBOXQ_t *pmb;
4498 	MAILBOX_t *mb;
4499 	struct lpfc_mbx_read_top *la;
4500 	uint8_t att_type;
4501 	int rc;
4502 
4503 	att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
4504 	if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
4505 		return;
4506 	phba->fcoe_eventtag = acqe_link->event_tag;
4507 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4508 	if (!pmb) {
4509 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4510 				"0395 The mboxq allocation failed\n");
4511 		return;
4512 	}
4513 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4514 	if (!mp) {
4515 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4516 				"0396 The lpfc_dmabuf allocation failed\n");
4517 		goto out_free_pmb;
4518 	}
4519 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4520 	if (!mp->virt) {
4521 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4522 				"0397 The mbuf allocation failed\n");
4523 		goto out_free_dmabuf;
4524 	}
4525 
4526 	/* Cleanup any outstanding ELS commands */
4527 	lpfc_els_flush_all_cmd(phba);
4528 
4529 	/* Block ELS IOCBs until we have done process link event */
4530 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4531 
4532 	/* Update link event statistics */
4533 	phba->sli.slistat.link_event++;
4534 
4535 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4536 	lpfc_read_topology(phba, pmb, mp);
4537 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4538 	pmb->vport = phba->pport;
4539 
4540 	/* Keep the link status for extra SLI4 state machine reference */
4541 	phba->sli4_hba.link_state.speed =
4542 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4543 				bf_get(lpfc_acqe_link_speed, acqe_link));
4544 	phba->sli4_hba.link_state.duplex =
4545 				bf_get(lpfc_acqe_link_duplex, acqe_link);
4546 	phba->sli4_hba.link_state.status =
4547 				bf_get(lpfc_acqe_link_status, acqe_link);
4548 	phba->sli4_hba.link_state.type =
4549 				bf_get(lpfc_acqe_link_type, acqe_link);
4550 	phba->sli4_hba.link_state.number =
4551 				bf_get(lpfc_acqe_link_number, acqe_link);
4552 	phba->sli4_hba.link_state.fault =
4553 				bf_get(lpfc_acqe_link_fault, acqe_link);
4554 	phba->sli4_hba.link_state.logical_speed =
4555 			bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4556 
4557 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4558 			"2900 Async FC/FCoE Link event - Speed:%dGBit "
4559 			"duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4560 			"Logical speed:%dMbps Fault:%d\n",
4561 			phba->sli4_hba.link_state.speed,
4562 			phba->sli4_hba.link_state.topology,
4563 			phba->sli4_hba.link_state.status,
4564 			phba->sli4_hba.link_state.type,
4565 			phba->sli4_hba.link_state.number,
4566 			phba->sli4_hba.link_state.logical_speed,
4567 			phba->sli4_hba.link_state.fault);
4568 	/*
4569 	 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4570 	 * topology info. Note: Optional for non FC-AL ports.
4571 	 */
4572 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4573 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4574 		if (rc == MBX_NOT_FINISHED)
4575 			goto out_free_dmabuf;
4576 		return;
4577 	}
4578 	/*
4579 	 * For FCoE Mode: fill in all the topology information we need and call
4580 	 * the READ_TOPOLOGY completion routine to continue without actually
4581 	 * sending the READ_TOPOLOGY mailbox command to the port.
4582 	 */
4583 	/* Initialize completion status */
4584 	mb = &pmb->u.mb;
4585 	mb->mbxStatus = MBX_SUCCESS;
4586 
4587 	/* Parse port fault information field */
4588 	lpfc_sli4_parse_latt_fault(phba, acqe_link);
4589 
4590 	/* Parse and translate link attention fields */
4591 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4592 	la->eventTag = acqe_link->event_tag;
4593 	bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4594 	bf_set(lpfc_mbx_read_top_link_spd, la,
4595 	       (bf_get(lpfc_acqe_link_speed, acqe_link)));
4596 
4597 	/* Fake the the following irrelvant fields */
4598 	bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4599 	bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4600 	bf_set(lpfc_mbx_read_top_il, la, 0);
4601 	bf_set(lpfc_mbx_read_top_pb, la, 0);
4602 	bf_set(lpfc_mbx_read_top_fa, la, 0);
4603 	bf_set(lpfc_mbx_read_top_mm, la, 0);
4604 
4605 	/* Invoke the lpfc_handle_latt mailbox command callback function */
4606 	lpfc_mbx_cmpl_read_topology(phba, pmb);
4607 
4608 	return;
4609 
4610 out_free_dmabuf:
4611 	kfree(mp);
4612 out_free_pmb:
4613 	mempool_free(pmb, phba->mbox_mem_pool);
4614 }
4615 
4616 /**
4617  * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4618  * @phba: pointer to lpfc hba data structure.
4619  * @acqe_fc: pointer to the async fc completion queue entry.
4620  *
4621  * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4622  * that the event was received and then issue a read_topology mailbox command so
4623  * that the rest of the driver will treat it the same as SLI3.
4624  **/
4625 static void
lpfc_sli4_async_fc_evt(struct lpfc_hba * phba,struct lpfc_acqe_fc_la * acqe_fc)4626 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4627 {
4628 	struct lpfc_dmabuf *mp;
4629 	LPFC_MBOXQ_t *pmb;
4630 	MAILBOX_t *mb;
4631 	struct lpfc_mbx_read_top *la;
4632 	int rc;
4633 
4634 	if (bf_get(lpfc_trailer_type, acqe_fc) !=
4635 	    LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4636 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4637 				"2895 Non FC link Event detected.(%d)\n",
4638 				bf_get(lpfc_trailer_type, acqe_fc));
4639 		return;
4640 	}
4641 	/* Keep the link status for extra SLI4 state machine reference */
4642 	phba->sli4_hba.link_state.speed =
4643 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4644 				bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
4645 	phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4646 	phba->sli4_hba.link_state.topology =
4647 				bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4648 	phba->sli4_hba.link_state.status =
4649 				bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4650 	phba->sli4_hba.link_state.type =
4651 				bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4652 	phba->sli4_hba.link_state.number =
4653 				bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4654 	phba->sli4_hba.link_state.fault =
4655 				bf_get(lpfc_acqe_link_fault, acqe_fc);
4656 	phba->sli4_hba.link_state.logical_speed =
4657 				bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
4658 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4659 			"2896 Async FC event - Speed:%dGBaud Topology:x%x "
4660 			"LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4661 			"%dMbps Fault:%d\n",
4662 			phba->sli4_hba.link_state.speed,
4663 			phba->sli4_hba.link_state.topology,
4664 			phba->sli4_hba.link_state.status,
4665 			phba->sli4_hba.link_state.type,
4666 			phba->sli4_hba.link_state.number,
4667 			phba->sli4_hba.link_state.logical_speed,
4668 			phba->sli4_hba.link_state.fault);
4669 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4670 	if (!pmb) {
4671 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4672 				"2897 The mboxq allocation failed\n");
4673 		return;
4674 	}
4675 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4676 	if (!mp) {
4677 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4678 				"2898 The lpfc_dmabuf allocation failed\n");
4679 		goto out_free_pmb;
4680 	}
4681 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4682 	if (!mp->virt) {
4683 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4684 				"2899 The mbuf allocation failed\n");
4685 		goto out_free_dmabuf;
4686 	}
4687 
4688 	/* Cleanup any outstanding ELS commands */
4689 	lpfc_els_flush_all_cmd(phba);
4690 
4691 	/* Block ELS IOCBs until we have done process link event */
4692 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4693 
4694 	/* Update link event statistics */
4695 	phba->sli.slistat.link_event++;
4696 
4697 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4698 	lpfc_read_topology(phba, pmb, mp);
4699 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4700 	pmb->vport = phba->pport;
4701 
4702 	if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4703 		phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
4704 
4705 		switch (phba->sli4_hba.link_state.status) {
4706 		case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
4707 			phba->link_flag |= LS_MDS_LINK_DOWN;
4708 			break;
4709 		case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
4710 			phba->link_flag |= LS_MDS_LOOPBACK;
4711 			break;
4712 		default:
4713 			break;
4714 		}
4715 
4716 		/* Initialize completion status */
4717 		mb = &pmb->u.mb;
4718 		mb->mbxStatus = MBX_SUCCESS;
4719 
4720 		/* Parse port fault information field */
4721 		lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
4722 
4723 		/* Parse and translate link attention fields */
4724 		la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4725 		la->eventTag = acqe_fc->event_tag;
4726 
4727 		if (phba->sli4_hba.link_state.status ==
4728 		    LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4729 			bf_set(lpfc_mbx_read_top_att_type, la,
4730 			       LPFC_FC_LA_TYPE_UNEXP_WWPN);
4731 		} else {
4732 			bf_set(lpfc_mbx_read_top_att_type, la,
4733 			       LPFC_FC_LA_TYPE_LINK_DOWN);
4734 		}
4735 		/* Invoke the mailbox command callback function */
4736 		lpfc_mbx_cmpl_read_topology(phba, pmb);
4737 
4738 		return;
4739 	}
4740 
4741 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4742 	if (rc == MBX_NOT_FINISHED)
4743 		goto out_free_dmabuf;
4744 	return;
4745 
4746 out_free_dmabuf:
4747 	kfree(mp);
4748 out_free_pmb:
4749 	mempool_free(pmb, phba->mbox_mem_pool);
4750 }
4751 
4752 /**
4753  * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4754  * @phba: pointer to lpfc hba data structure.
4755  * @acqe_fc: pointer to the async SLI completion queue entry.
4756  *
4757  * This routine is to handle the SLI4 asynchronous SLI events.
4758  **/
4759 static void
lpfc_sli4_async_sli_evt(struct lpfc_hba * phba,struct lpfc_acqe_sli * acqe_sli)4760 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4761 {
4762 	char port_name;
4763 	char message[128];
4764 	uint8_t status;
4765 	uint8_t evt_type;
4766 	uint8_t operational = 0;
4767 	struct temp_event temp_event_data;
4768 	struct lpfc_acqe_misconfigured_event *misconfigured;
4769 	struct Scsi_Host  *shost;
4770 
4771 	evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4772 
4773 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4774 			"2901 Async SLI event - Event Data1:x%08x Event Data2:"
4775 			"x%08x SLI Event Type:%d\n",
4776 			acqe_sli->event_data1, acqe_sli->event_data2,
4777 			evt_type);
4778 
4779 	port_name = phba->Port[0];
4780 	if (port_name == 0x00)
4781 		port_name = '?'; /* get port name is empty */
4782 
4783 	switch (evt_type) {
4784 	case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4785 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4786 		temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4787 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4788 
4789 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4790 				"3190 Over Temperature:%d Celsius- Port Name %c\n",
4791 				acqe_sli->event_data1, port_name);
4792 
4793 		phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
4794 		shost = lpfc_shost_from_vport(phba->pport);
4795 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4796 					  sizeof(temp_event_data),
4797 					  (char *)&temp_event_data,
4798 					  SCSI_NL_VID_TYPE_PCI
4799 					  | PCI_VENDOR_ID_EMULEX);
4800 		break;
4801 	case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4802 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4803 		temp_event_data.event_code = LPFC_NORMAL_TEMP;
4804 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4805 
4806 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4807 				"3191 Normal Temperature:%d Celsius - Port Name %c\n",
4808 				acqe_sli->event_data1, port_name);
4809 
4810 		shost = lpfc_shost_from_vport(phba->pport);
4811 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4812 					  sizeof(temp_event_data),
4813 					  (char *)&temp_event_data,
4814 					  SCSI_NL_VID_TYPE_PCI
4815 					  | PCI_VENDOR_ID_EMULEX);
4816 		break;
4817 	case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4818 		misconfigured = (struct lpfc_acqe_misconfigured_event *)
4819 					&acqe_sli->event_data1;
4820 
4821 		/* fetch the status for this port */
4822 		switch (phba->sli4_hba.lnk_info.lnk_no) {
4823 		case LPFC_LINK_NUMBER_0:
4824 			status = bf_get(lpfc_sli_misconfigured_port0_state,
4825 					&misconfigured->theEvent);
4826 			operational = bf_get(lpfc_sli_misconfigured_port0_op,
4827 					&misconfigured->theEvent);
4828 			break;
4829 		case LPFC_LINK_NUMBER_1:
4830 			status = bf_get(lpfc_sli_misconfigured_port1_state,
4831 					&misconfigured->theEvent);
4832 			operational = bf_get(lpfc_sli_misconfigured_port1_op,
4833 					&misconfigured->theEvent);
4834 			break;
4835 		case LPFC_LINK_NUMBER_2:
4836 			status = bf_get(lpfc_sli_misconfigured_port2_state,
4837 					&misconfigured->theEvent);
4838 			operational = bf_get(lpfc_sli_misconfigured_port2_op,
4839 					&misconfigured->theEvent);
4840 			break;
4841 		case LPFC_LINK_NUMBER_3:
4842 			status = bf_get(lpfc_sli_misconfigured_port3_state,
4843 					&misconfigured->theEvent);
4844 			operational = bf_get(lpfc_sli_misconfigured_port3_op,
4845 					&misconfigured->theEvent);
4846 			break;
4847 		default:
4848 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4849 					"3296 "
4850 					"LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4851 					"event: Invalid link %d",
4852 					phba->sli4_hba.lnk_info.lnk_no);
4853 			return;
4854 		}
4855 
4856 		/* Skip if optic state unchanged */
4857 		if (phba->sli4_hba.lnk_info.optic_state == status)
4858 			return;
4859 
4860 		switch (status) {
4861 		case LPFC_SLI_EVENT_STATUS_VALID:
4862 			sprintf(message, "Physical Link is functional");
4863 			break;
4864 		case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4865 			sprintf(message, "Optics faulted/incorrectly "
4866 				"installed/not installed - Reseat optics, "
4867 				"if issue not resolved, replace.");
4868 			break;
4869 		case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4870 			sprintf(message,
4871 				"Optics of two types installed - Remove one "
4872 				"optic or install matching pair of optics.");
4873 			break;
4874 		case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4875 			sprintf(message, "Incompatible optics - Replace with "
4876 				"compatible optics for card to function.");
4877 			break;
4878 		case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4879 			sprintf(message, "Unqualified optics - Replace with "
4880 				"Avago optics for Warranty and Technical "
4881 				"Support - Link is%s operational",
4882 				(operational) ? " not" : "");
4883 			break;
4884 		case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4885 			sprintf(message, "Uncertified optics - Replace with "
4886 				"Avago-certified optics to enable link "
4887 				"operation - Link is%s operational",
4888 				(operational) ? " not" : "");
4889 			break;
4890 		default:
4891 			/* firmware is reporting a status we don't know about */
4892 			sprintf(message, "Unknown event status x%02x", status);
4893 			break;
4894 		}
4895 		phba->sli4_hba.lnk_info.optic_state = status;
4896 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4897 				"3176 Port Name %c %s\n", port_name, message);
4898 		break;
4899 	case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4900 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4901 				"3192 Remote DPort Test Initiated - "
4902 				"Event Data1:x%08x Event Data2: x%08x\n",
4903 				acqe_sli->event_data1, acqe_sli->event_data2);
4904 		break;
4905 	default:
4906 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4907 				"3193 Async SLI event - Event Data1:x%08x Event Data2:"
4908 				"x%08x SLI Event Type:%d\n",
4909 				acqe_sli->event_data1, acqe_sli->event_data2,
4910 				evt_type);
4911 		break;
4912 	}
4913 }
4914 
4915 /**
4916  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4917  * @vport: pointer to vport data structure.
4918  *
4919  * This routine is to perform Clear Virtual Link (CVL) on a vport in
4920  * response to a CVL event.
4921  *
4922  * Return the pointer to the ndlp with the vport if successful, otherwise
4923  * return NULL.
4924  **/
4925 static struct lpfc_nodelist *
lpfc_sli4_perform_vport_cvl(struct lpfc_vport * vport)4926 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4927 {
4928 	struct lpfc_nodelist *ndlp;
4929 	struct Scsi_Host *shost;
4930 	struct lpfc_hba *phba;
4931 
4932 	if (!vport)
4933 		return NULL;
4934 	phba = vport->phba;
4935 	if (!phba)
4936 		return NULL;
4937 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
4938 	if (!ndlp) {
4939 		/* Cannot find existing Fabric ndlp, so allocate a new one */
4940 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
4941 		if (!ndlp)
4942 			return 0;
4943 		/* Set the node type */
4944 		ndlp->nlp_type |= NLP_FABRIC;
4945 		/* Put ndlp onto node list */
4946 		lpfc_enqueue_node(vport, ndlp);
4947 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
4948 		/* re-setup ndlp without removing from node list */
4949 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4950 		if (!ndlp)
4951 			return 0;
4952 	}
4953 	if ((phba->pport->port_state < LPFC_FLOGI) &&
4954 		(phba->pport->port_state != LPFC_VPORT_FAILED))
4955 		return NULL;
4956 	/* If virtual link is not yet instantiated ignore CVL */
4957 	if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4958 		&& (vport->port_state != LPFC_VPORT_FAILED))
4959 		return NULL;
4960 	shost = lpfc_shost_from_vport(vport);
4961 	if (!shost)
4962 		return NULL;
4963 	lpfc_linkdown_port(vport);
4964 	lpfc_cleanup_pending_mbox(vport);
4965 	spin_lock_irq(shost->host_lock);
4966 	vport->fc_flag |= FC_VPORT_CVL_RCVD;
4967 	spin_unlock_irq(shost->host_lock);
4968 
4969 	return ndlp;
4970 }
4971 
4972 /**
4973  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4974  * @vport: pointer to lpfc hba data structure.
4975  *
4976  * This routine is to perform Clear Virtual Link (CVL) on all vports in
4977  * response to a FCF dead event.
4978  **/
4979 static void
lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba * phba)4980 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4981 {
4982 	struct lpfc_vport **vports;
4983 	int i;
4984 
4985 	vports = lpfc_create_vport_work_array(phba);
4986 	if (vports)
4987 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4988 			lpfc_sli4_perform_vport_cvl(vports[i]);
4989 	lpfc_destroy_vport_work_array(phba, vports);
4990 }
4991 
4992 /**
4993  * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
4994  * @phba: pointer to lpfc hba data structure.
4995  * @acqe_link: pointer to the async fcoe completion queue entry.
4996  *
4997  * This routine is to handle the SLI4 asynchronous fcoe event.
4998  **/
4999 static void
lpfc_sli4_async_fip_evt(struct lpfc_hba * phba,struct lpfc_acqe_fip * acqe_fip)5000 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
5001 			struct lpfc_acqe_fip *acqe_fip)
5002 {
5003 	uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
5004 	int rc;
5005 	struct lpfc_vport *vport;
5006 	struct lpfc_nodelist *ndlp;
5007 	struct Scsi_Host  *shost;
5008 	int active_vlink_present;
5009 	struct lpfc_vport **vports;
5010 	int i;
5011 
5012 	phba->fc_eventTag = acqe_fip->event_tag;
5013 	phba->fcoe_eventtag = acqe_fip->event_tag;
5014 	switch (event_type) {
5015 	case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5016 	case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5017 		if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
5018 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5019 					LOG_DISCOVERY,
5020 					"2546 New FCF event, evt_tag:x%x, "
5021 					"index:x%x\n",
5022 					acqe_fip->event_tag,
5023 					acqe_fip->index);
5024 		else
5025 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5026 					LOG_DISCOVERY,
5027 					"2788 FCF param modified event, "
5028 					"evt_tag:x%x, index:x%x\n",
5029 					acqe_fip->event_tag,
5030 					acqe_fip->index);
5031 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5032 			/*
5033 			 * During period of FCF discovery, read the FCF
5034 			 * table record indexed by the event to update
5035 			 * FCF roundrobin failover eligible FCF bmask.
5036 			 */
5037 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5038 					LOG_DISCOVERY,
5039 					"2779 Read FCF (x%x) for updating "
5040 					"roundrobin FCF failover bmask\n",
5041 					acqe_fip->index);
5042 			rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
5043 		}
5044 
5045 		/* If the FCF discovery is in progress, do nothing. */
5046 		spin_lock_irq(&phba->hbalock);
5047 		if (phba->hba_flag & FCF_TS_INPROG) {
5048 			spin_unlock_irq(&phba->hbalock);
5049 			break;
5050 		}
5051 		/* If fast FCF failover rescan event is pending, do nothing */
5052 		if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
5053 			spin_unlock_irq(&phba->hbalock);
5054 			break;
5055 		}
5056 
5057 		/* If the FCF has been in discovered state, do nothing. */
5058 		if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
5059 			spin_unlock_irq(&phba->hbalock);
5060 			break;
5061 		}
5062 		spin_unlock_irq(&phba->hbalock);
5063 
5064 		/* Otherwise, scan the entire FCF table and re-discover SAN */
5065 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5066 				"2770 Start FCF table scan per async FCF "
5067 				"event, evt_tag:x%x, index:x%x\n",
5068 				acqe_fip->event_tag, acqe_fip->index);
5069 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5070 						     LPFC_FCOE_FCF_GET_FIRST);
5071 		if (rc)
5072 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5073 					"2547 Issue FCF scan read FCF mailbox "
5074 					"command failed (x%x)\n", rc);
5075 		break;
5076 
5077 	case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
5078 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5079 			"2548 FCF Table full count 0x%x tag 0x%x\n",
5080 			bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5081 			acqe_fip->event_tag);
5082 		break;
5083 
5084 	case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
5085 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5086 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5087 			"2549 FCF (x%x) disconnected from network, "
5088 			"tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
5089 		/*
5090 		 * If we are in the middle of FCF failover process, clear
5091 		 * the corresponding FCF bit in the roundrobin bitmap.
5092 		 */
5093 		spin_lock_irq(&phba->hbalock);
5094 		if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5095 		    (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
5096 			spin_unlock_irq(&phba->hbalock);
5097 			/* Update FLOGI FCF failover eligible FCF bmask */
5098 			lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
5099 			break;
5100 		}
5101 		spin_unlock_irq(&phba->hbalock);
5102 
5103 		/* If the event is not for currently used fcf do nothing */
5104 		if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
5105 			break;
5106 
5107 		/*
5108 		 * Otherwise, request the port to rediscover the entire FCF
5109 		 * table for a fast recovery from case that the current FCF
5110 		 * is no longer valid as we are not in the middle of FCF
5111 		 * failover process already.
5112 		 */
5113 		spin_lock_irq(&phba->hbalock);
5114 		/* Mark the fast failover process in progress */
5115 		phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5116 		spin_unlock_irq(&phba->hbalock);
5117 
5118 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5119 				"2771 Start FCF fast failover process due to "
5120 				"FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5121 				"\n", acqe_fip->event_tag, acqe_fip->index);
5122 		rc = lpfc_sli4_redisc_fcf_table(phba);
5123 		if (rc) {
5124 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5125 					LOG_DISCOVERY,
5126 					"2772 Issue FCF rediscover mailbox "
5127 					"command failed, fail through to FCF "
5128 					"dead event\n");
5129 			spin_lock_irq(&phba->hbalock);
5130 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5131 			spin_unlock_irq(&phba->hbalock);
5132 			/*
5133 			 * Last resort will fail over by treating this
5134 			 * as a link down to FCF registration.
5135 			 */
5136 			lpfc_sli4_fcf_dead_failthrough(phba);
5137 		} else {
5138 			/* Reset FCF roundrobin bmask for new discovery */
5139 			lpfc_sli4_clear_fcf_rr_bmask(phba);
5140 			/*
5141 			 * Handling fast FCF failover to a DEAD FCF event is
5142 			 * considered equalivant to receiving CVL to all vports.
5143 			 */
5144 			lpfc_sli4_perform_all_vport_cvl(phba);
5145 		}
5146 		break;
5147 	case LPFC_FIP_EVENT_TYPE_CVL:
5148 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5149 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5150 			"2718 Clear Virtual Link Received for VPI 0x%x"
5151 			" tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
5152 
5153 		vport = lpfc_find_vport_by_vpid(phba,
5154 						acqe_fip->index);
5155 		ndlp = lpfc_sli4_perform_vport_cvl(vport);
5156 		if (!ndlp)
5157 			break;
5158 		active_vlink_present = 0;
5159 
5160 		vports = lpfc_create_vport_work_array(phba);
5161 		if (vports) {
5162 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5163 					i++) {
5164 				if ((!(vports[i]->fc_flag &
5165 					FC_VPORT_CVL_RCVD)) &&
5166 					(vports[i]->port_state > LPFC_FDISC)) {
5167 					active_vlink_present = 1;
5168 					break;
5169 				}
5170 			}
5171 			lpfc_destroy_vport_work_array(phba, vports);
5172 		}
5173 
5174 		/*
5175 		 * Don't re-instantiate if vport is marked for deletion.
5176 		 * If we are here first then vport_delete is going to wait
5177 		 * for discovery to complete.
5178 		 */
5179 		if (!(vport->load_flag & FC_UNLOADING) &&
5180 					active_vlink_present) {
5181 			/*
5182 			 * If there are other active VLinks present,
5183 			 * re-instantiate the Vlink using FDISC.
5184 			 */
5185 			mod_timer(&ndlp->nlp_delayfunc,
5186 				  jiffies + msecs_to_jiffies(1000));
5187 			shost = lpfc_shost_from_vport(vport);
5188 			spin_lock_irq(shost->host_lock);
5189 			ndlp->nlp_flag |= NLP_DELAY_TMO;
5190 			spin_unlock_irq(shost->host_lock);
5191 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5192 			vport->port_state = LPFC_FDISC;
5193 		} else {
5194 			/*
5195 			 * Otherwise, we request port to rediscover
5196 			 * the entire FCF table for a fast recovery
5197 			 * from possible case that the current FCF
5198 			 * is no longer valid if we are not already
5199 			 * in the FCF failover process.
5200 			 */
5201 			spin_lock_irq(&phba->hbalock);
5202 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5203 				spin_unlock_irq(&phba->hbalock);
5204 				break;
5205 			}
5206 			/* Mark the fast failover process in progress */
5207 			phba->fcf.fcf_flag |= FCF_ACVL_DISC;
5208 			spin_unlock_irq(&phba->hbalock);
5209 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5210 					LOG_DISCOVERY,
5211 					"2773 Start FCF failover per CVL, "
5212 					"evt_tag:x%x\n", acqe_fip->event_tag);
5213 			rc = lpfc_sli4_redisc_fcf_table(phba);
5214 			if (rc) {
5215 				lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5216 						LOG_DISCOVERY,
5217 						"2774 Issue FCF rediscover "
5218 						"mailbox command failed, "
5219 						"through to CVL event\n");
5220 				spin_lock_irq(&phba->hbalock);
5221 				phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
5222 				spin_unlock_irq(&phba->hbalock);
5223 				/*
5224 				 * Last resort will be re-try on the
5225 				 * the current registered FCF entry.
5226 				 */
5227 				lpfc_retry_pport_discovery(phba);
5228 			} else
5229 				/*
5230 				 * Reset FCF roundrobin bmask for new
5231 				 * discovery.
5232 				 */
5233 				lpfc_sli4_clear_fcf_rr_bmask(phba);
5234 		}
5235 		break;
5236 	default:
5237 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5238 			"0288 Unknown FCoE event type 0x%x event tag "
5239 			"0x%x\n", event_type, acqe_fip->event_tag);
5240 		break;
5241 	}
5242 }
5243 
5244 /**
5245  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5246  * @phba: pointer to lpfc hba data structure.
5247  * @acqe_link: pointer to the async dcbx completion queue entry.
5248  *
5249  * This routine is to handle the SLI4 asynchronous dcbx event.
5250  **/
5251 static void
lpfc_sli4_async_dcbx_evt(struct lpfc_hba * phba,struct lpfc_acqe_dcbx * acqe_dcbx)5252 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5253 			 struct lpfc_acqe_dcbx *acqe_dcbx)
5254 {
5255 	phba->fc_eventTag = acqe_dcbx->event_tag;
5256 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5257 			"0290 The SLI4 DCBX asynchronous event is not "
5258 			"handled yet\n");
5259 }
5260 
5261 /**
5262  * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5263  * @phba: pointer to lpfc hba data structure.
5264  * @acqe_link: pointer to the async grp5 completion queue entry.
5265  *
5266  * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5267  * is an asynchronous notified of a logical link speed change.  The Port
5268  * reports the logical link speed in units of 10Mbps.
5269  **/
5270 static void
lpfc_sli4_async_grp5_evt(struct lpfc_hba * phba,struct lpfc_acqe_grp5 * acqe_grp5)5271 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5272 			 struct lpfc_acqe_grp5 *acqe_grp5)
5273 {
5274 	uint16_t prev_ll_spd;
5275 
5276 	phba->fc_eventTag = acqe_grp5->event_tag;
5277 	phba->fcoe_eventtag = acqe_grp5->event_tag;
5278 	prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5279 	phba->sli4_hba.link_state.logical_speed =
5280 		(bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
5281 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5282 			"2789 GRP5 Async Event: Updating logical link speed "
5283 			"from %dMbps to %dMbps\n", prev_ll_spd,
5284 			phba->sli4_hba.link_state.logical_speed);
5285 }
5286 
5287 /**
5288  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5289  * @phba: pointer to lpfc hba data structure.
5290  *
5291  * This routine is invoked by the worker thread to process all the pending
5292  * SLI4 asynchronous events.
5293  **/
lpfc_sli4_async_event_proc(struct lpfc_hba * phba)5294 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5295 {
5296 	struct lpfc_cq_event *cq_event;
5297 
5298 	/* First, declare the async event has been handled */
5299 	spin_lock_irq(&phba->hbalock);
5300 	phba->hba_flag &= ~ASYNC_EVENT;
5301 	spin_unlock_irq(&phba->hbalock);
5302 	/* Now, handle all the async events */
5303 	while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5304 		/* Get the first event from the head of the event queue */
5305 		spin_lock_irq(&phba->hbalock);
5306 		list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5307 				 cq_event, struct lpfc_cq_event, list);
5308 		spin_unlock_irq(&phba->hbalock);
5309 		/* Process the asynchronous event */
5310 		switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5311 		case LPFC_TRAILER_CODE_LINK:
5312 			lpfc_sli4_async_link_evt(phba,
5313 						 &cq_event->cqe.acqe_link);
5314 			break;
5315 		case LPFC_TRAILER_CODE_FCOE:
5316 			lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
5317 			break;
5318 		case LPFC_TRAILER_CODE_DCBX:
5319 			lpfc_sli4_async_dcbx_evt(phba,
5320 						 &cq_event->cqe.acqe_dcbx);
5321 			break;
5322 		case LPFC_TRAILER_CODE_GRP5:
5323 			lpfc_sli4_async_grp5_evt(phba,
5324 						 &cq_event->cqe.acqe_grp5);
5325 			break;
5326 		case LPFC_TRAILER_CODE_FC:
5327 			lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5328 			break;
5329 		case LPFC_TRAILER_CODE_SLI:
5330 			lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5331 			break;
5332 		default:
5333 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5334 					"1804 Invalid asynchrous event code: "
5335 					"x%x\n", bf_get(lpfc_trailer_code,
5336 					&cq_event->cqe.mcqe_cmpl));
5337 			break;
5338 		}
5339 		/* Free the completion event processed to the free pool */
5340 		lpfc_sli4_cq_event_release(phba, cq_event);
5341 	}
5342 }
5343 
5344 /**
5345  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5346  * @phba: pointer to lpfc hba data structure.
5347  *
5348  * This routine is invoked by the worker thread to process FCF table
5349  * rediscovery pending completion event.
5350  **/
lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba * phba)5351 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5352 {
5353 	int rc;
5354 
5355 	spin_lock_irq(&phba->hbalock);
5356 	/* Clear FCF rediscovery timeout event */
5357 	phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5358 	/* Clear driver fast failover FCF record flag */
5359 	phba->fcf.failover_rec.flag = 0;
5360 	/* Set state for FCF fast failover */
5361 	phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5362 	spin_unlock_irq(&phba->hbalock);
5363 
5364 	/* Scan FCF table from the first entry to re-discover SAN */
5365 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5366 			"2777 Start post-quiescent FCF table scan\n");
5367 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
5368 	if (rc)
5369 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5370 				"2747 Issue FCF scan read FCF mailbox "
5371 				"command failed 0x%x\n", rc);
5372 }
5373 
5374 /**
5375  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5376  * @phba: pointer to lpfc hba data structure.
5377  * @dev_grp: The HBA PCI-Device group number.
5378  *
5379  * This routine is invoked to set up the per HBA PCI-Device group function
5380  * API jump table entries.
5381  *
5382  * Return: 0 if success, otherwise -ENODEV
5383  **/
5384 int
lpfc_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)5385 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5386 {
5387 	int rc;
5388 
5389 	/* Set up lpfc PCI-device group */
5390 	phba->pci_dev_grp = dev_grp;
5391 
5392 	/* The LPFC_PCI_DEV_OC uses SLI4 */
5393 	if (dev_grp == LPFC_PCI_DEV_OC)
5394 		phba->sli_rev = LPFC_SLI_REV4;
5395 
5396 	/* Set up device INIT API function jump table */
5397 	rc = lpfc_init_api_table_setup(phba, dev_grp);
5398 	if (rc)
5399 		return -ENODEV;
5400 	/* Set up SCSI API function jump table */
5401 	rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5402 	if (rc)
5403 		return -ENODEV;
5404 	/* Set up SLI API function jump table */
5405 	rc = lpfc_sli_api_table_setup(phba, dev_grp);
5406 	if (rc)
5407 		return -ENODEV;
5408 	/* Set up MBOX API function jump table */
5409 	rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5410 	if (rc)
5411 		return -ENODEV;
5412 
5413 	return 0;
5414 }
5415 
5416 /**
5417  * lpfc_log_intr_mode - Log the active interrupt mode
5418  * @phba: pointer to lpfc hba data structure.
5419  * @intr_mode: active interrupt mode adopted.
5420  *
5421  * This routine it invoked to log the currently used active interrupt mode
5422  * to the device.
5423  **/
lpfc_log_intr_mode(struct lpfc_hba * phba,uint32_t intr_mode)5424 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5425 {
5426 	switch (intr_mode) {
5427 	case 0:
5428 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5429 				"0470 Enable INTx interrupt mode.\n");
5430 		break;
5431 	case 1:
5432 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5433 				"0481 Enabled MSI interrupt mode.\n");
5434 		break;
5435 	case 2:
5436 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5437 				"0480 Enabled MSI-X interrupt mode.\n");
5438 		break;
5439 	default:
5440 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5441 				"0482 Illegal interrupt mode.\n");
5442 		break;
5443 	}
5444 	return;
5445 }
5446 
5447 /**
5448  * lpfc_enable_pci_dev - Enable a generic PCI device.
5449  * @phba: pointer to lpfc hba data structure.
5450  *
5451  * This routine is invoked to enable the PCI device that is common to all
5452  * PCI devices.
5453  *
5454  * Return codes
5455  * 	0 - successful
5456  * 	other values - error
5457  **/
5458 static int
lpfc_enable_pci_dev(struct lpfc_hba * phba)5459 lpfc_enable_pci_dev(struct lpfc_hba *phba)
5460 {
5461 	struct pci_dev *pdev;
5462 
5463 	/* Obtain PCI device reference */
5464 	if (!phba->pcidev)
5465 		goto out_error;
5466 	else
5467 		pdev = phba->pcidev;
5468 	/* Enable PCI device */
5469 	if (pci_enable_device_mem(pdev))
5470 		goto out_error;
5471 	/* Request PCI resource for the device */
5472 	if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
5473 		goto out_disable_device;
5474 	/* Set up device as PCI master and save state for EEH */
5475 	pci_set_master(pdev);
5476 	pci_try_set_mwi(pdev);
5477 	pci_save_state(pdev);
5478 
5479 	/* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5480 	if (pci_is_pcie(pdev))
5481 		pdev->needs_freset = 1;
5482 
5483 	return 0;
5484 
5485 out_disable_device:
5486 	pci_disable_device(pdev);
5487 out_error:
5488 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5489 			"1401 Failed to enable pci device\n");
5490 	return -ENODEV;
5491 }
5492 
5493 /**
5494  * lpfc_disable_pci_dev - Disable a generic PCI device.
5495  * @phba: pointer to lpfc hba data structure.
5496  *
5497  * This routine is invoked to disable the PCI device that is common to all
5498  * PCI devices.
5499  **/
5500 static void
lpfc_disable_pci_dev(struct lpfc_hba * phba)5501 lpfc_disable_pci_dev(struct lpfc_hba *phba)
5502 {
5503 	struct pci_dev *pdev;
5504 
5505 	/* Obtain PCI device reference */
5506 	if (!phba->pcidev)
5507 		return;
5508 	else
5509 		pdev = phba->pcidev;
5510 	/* Release PCI resource and disable PCI device */
5511 	pci_release_mem_regions(pdev);
5512 	pci_disable_device(pdev);
5513 
5514 	return;
5515 }
5516 
5517 /**
5518  * lpfc_reset_hba - Reset a hba
5519  * @phba: pointer to lpfc hba data structure.
5520  *
5521  * This routine is invoked to reset a hba device. It brings the HBA
5522  * offline, performs a board restart, and then brings the board back
5523  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5524  * on outstanding mailbox commands.
5525  **/
5526 void
lpfc_reset_hba(struct lpfc_hba * phba)5527 lpfc_reset_hba(struct lpfc_hba *phba)
5528 {
5529 	/* If resets are disabled then set error state and return. */
5530 	if (!phba->cfg_enable_hba_reset) {
5531 		phba->link_state = LPFC_HBA_ERROR;
5532 		return;
5533 	}
5534 	if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5535 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5536 	else
5537 		lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
5538 	lpfc_offline(phba);
5539 	lpfc_sli_brdrestart(phba);
5540 	lpfc_online(phba);
5541 	lpfc_unblock_mgmt_io(phba);
5542 }
5543 
5544 /**
5545  * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5546  * @phba: pointer to lpfc hba data structure.
5547  *
5548  * This function enables the PCI SR-IOV virtual functions to a physical
5549  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5550  * enable the number of virtual functions to the physical function. As
5551  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5552  * API call does not considered as an error condition for most of the device.
5553  **/
5554 uint16_t
lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba * phba)5555 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5556 {
5557 	struct pci_dev *pdev = phba->pcidev;
5558 	uint16_t nr_virtfn;
5559 	int pos;
5560 
5561 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5562 	if (pos == 0)
5563 		return 0;
5564 
5565 	pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5566 	return nr_virtfn;
5567 }
5568 
5569 /**
5570  * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5571  * @phba: pointer to lpfc hba data structure.
5572  * @nr_vfn: number of virtual functions to be enabled.
5573  *
5574  * This function enables the PCI SR-IOV virtual functions to a physical
5575  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5576  * enable the number of virtual functions to the physical function. As
5577  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5578  * API call does not considered as an error condition for most of the device.
5579  **/
5580 int
lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba * phba,int nr_vfn)5581 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5582 {
5583 	struct pci_dev *pdev = phba->pcidev;
5584 	uint16_t max_nr_vfn;
5585 	int rc;
5586 
5587 	max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5588 	if (nr_vfn > max_nr_vfn) {
5589 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5590 				"3057 Requested vfs (%d) greater than "
5591 				"supported vfs (%d)", nr_vfn, max_nr_vfn);
5592 		return -EINVAL;
5593 	}
5594 
5595 	rc = pci_enable_sriov(pdev, nr_vfn);
5596 	if (rc) {
5597 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5598 				"2806 Failed to enable sriov on this device "
5599 				"with vfn number nr_vf:%d, rc:%d\n",
5600 				nr_vfn, rc);
5601 	} else
5602 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5603 				"2807 Successful enable sriov on this device "
5604 				"with vfn number nr_vf:%d\n", nr_vfn);
5605 	return rc;
5606 }
5607 
5608 /**
5609  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5610  * @phba: pointer to lpfc hba data structure.
5611  *
5612  * This routine is invoked to set up the driver internal resources before the
5613  * device specific resource setup to support the HBA device it attached to.
5614  *
5615  * Return codes
5616  *	0 - successful
5617  *	other values - error
5618  **/
5619 static int
lpfc_setup_driver_resource_phase1(struct lpfc_hba * phba)5620 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5621 {
5622 	struct lpfc_sli *psli = &phba->sli;
5623 
5624 	/*
5625 	 * Driver resources common to all SLI revisions
5626 	 */
5627 	atomic_set(&phba->fast_event_count, 0);
5628 	spin_lock_init(&phba->hbalock);
5629 
5630 	/* Initialize ndlp management spinlock */
5631 	spin_lock_init(&phba->ndlp_lock);
5632 
5633 	INIT_LIST_HEAD(&phba->port_list);
5634 	INIT_LIST_HEAD(&phba->work_list);
5635 	init_waitqueue_head(&phba->wait_4_mlo_m_q);
5636 
5637 	/* Initialize the wait queue head for the kernel thread */
5638 	init_waitqueue_head(&phba->work_waitq);
5639 
5640 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5641 			"1403 Protocols supported %s %s %s\n",
5642 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5643 				"SCSI" : " "),
5644 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
5645 				"NVME" : " "),
5646 			(phba->nvmet_support ? "NVMET" : " "));
5647 
5648 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5649 		/* Initialize the scsi buffer list used by driver for scsi IO */
5650 		spin_lock_init(&phba->scsi_buf_list_get_lock);
5651 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5652 		spin_lock_init(&phba->scsi_buf_list_put_lock);
5653 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5654 	}
5655 
5656 	if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5657 		(phba->nvmet_support == 0)) {
5658 		/* Initialize the NVME buffer list used by driver for NVME IO */
5659 		spin_lock_init(&phba->nvme_buf_list_get_lock);
5660 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5661 		phba->get_nvme_bufs = 0;
5662 		spin_lock_init(&phba->nvme_buf_list_put_lock);
5663 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5664 		phba->put_nvme_bufs = 0;
5665 	}
5666 
5667 	/* Initialize the fabric iocb list */
5668 	INIT_LIST_HEAD(&phba->fabric_iocb_list);
5669 
5670 	/* Initialize list to save ELS buffers */
5671 	INIT_LIST_HEAD(&phba->elsbuf);
5672 
5673 	/* Initialize FCF connection rec list */
5674 	INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5675 
5676 	/* Initialize OAS configuration list */
5677 	spin_lock_init(&phba->devicelock);
5678 	INIT_LIST_HEAD(&phba->luns);
5679 
5680 	/* MBOX heartbeat timer */
5681 	timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
5682 	/* Fabric block timer */
5683 	timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
5684 	/* EA polling mode timer */
5685 	timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
5686 	/* Heartbeat timer */
5687 	timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
5688 
5689 	return 0;
5690 }
5691 
5692 /**
5693  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5694  * @phba: pointer to lpfc hba data structure.
5695  *
5696  * This routine is invoked to set up the driver internal resources specific to
5697  * support the SLI-3 HBA device it attached to.
5698  *
5699  * Return codes
5700  * 0 - successful
5701  * other values - error
5702  **/
5703 static int
lpfc_sli_driver_resource_setup(struct lpfc_hba * phba)5704 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5705 {
5706 	int rc;
5707 
5708 	/*
5709 	 * Initialize timers used by driver
5710 	 */
5711 
5712 	/* FCP polling mode timer */
5713 	timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
5714 
5715 	/* Host attention work mask setup */
5716 	phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5717 	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5718 
5719 	/* Get all the module params for configuring this host */
5720 	lpfc_get_cfgparam(phba);
5721 	/* Set up phase-1 common device driver resources */
5722 
5723 	rc = lpfc_setup_driver_resource_phase1(phba);
5724 	if (rc)
5725 		return -ENODEV;
5726 
5727 	if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5728 		phba->menlo_flag |= HBA_MENLO_SUPPORT;
5729 		/* check for menlo minimum sg count */
5730 		if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5731 			phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5732 	}
5733 
5734 	if (!phba->sli.sli3_ring)
5735 		phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
5736 					      sizeof(struct lpfc_sli_ring),
5737 					      GFP_KERNEL);
5738 	if (!phba->sli.sli3_ring)
5739 		return -ENOMEM;
5740 
5741 	/*
5742 	 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
5743 	 * used to create the sg_dma_buf_pool must be dynamically calculated.
5744 	 */
5745 
5746 	/* Initialize the host templates the configured values. */
5747 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5748 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5749 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5750 
5751 	/* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5752 	if (phba->cfg_enable_bg) {
5753 		/*
5754 		 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5755 		 * the FCP rsp, and a BDE for each. Sice we have no control
5756 		 * over how many protection data segments the SCSI Layer
5757 		 * will hand us (ie: there could be one for every block
5758 		 * in the IO), we just allocate enough BDEs to accomidate
5759 		 * our max amount and we need to limit lpfc_sg_seg_cnt to
5760 		 * minimize the risk of running out.
5761 		 */
5762 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5763 			sizeof(struct fcp_rsp) +
5764 			(LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5765 
5766 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5767 			phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5768 
5769 		/* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5770 		phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5771 	} else {
5772 		/*
5773 		 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5774 		 * the FCP rsp, a BDE for each, and a BDE for up to
5775 		 * cfg_sg_seg_cnt data segments.
5776 		 */
5777 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5778 			sizeof(struct fcp_rsp) +
5779 			((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5780 
5781 		/* Total BDEs in BPL for scsi_sg_list */
5782 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5783 	}
5784 
5785 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5786 			"9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5787 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5788 			phba->cfg_total_seg_cnt);
5789 
5790 	phba->max_vpi = LPFC_MAX_VPI;
5791 	/* This will be set to correct value after config_port mbox */
5792 	phba->max_vports = 0;
5793 
5794 	/*
5795 	 * Initialize the SLI Layer to run with lpfc HBAs.
5796 	 */
5797 	lpfc_sli_setup(phba);
5798 	lpfc_sli_queue_init(phba);
5799 
5800 	/* Allocate device driver memory */
5801 	if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5802 		return -ENOMEM;
5803 
5804 	/*
5805 	 * Enable sr-iov virtual functions if supported and configured
5806 	 * through the module parameter.
5807 	 */
5808 	if (phba->cfg_sriov_nr_virtfn > 0) {
5809 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5810 						 phba->cfg_sriov_nr_virtfn);
5811 		if (rc) {
5812 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5813 					"2808 Requested number of SR-IOV "
5814 					"virtual functions (%d) is not "
5815 					"supported\n",
5816 					phba->cfg_sriov_nr_virtfn);
5817 			phba->cfg_sriov_nr_virtfn = 0;
5818 		}
5819 	}
5820 
5821 	return 0;
5822 }
5823 
5824 /**
5825  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5826  * @phba: pointer to lpfc hba data structure.
5827  *
5828  * This routine is invoked to unset the driver internal resources set up
5829  * specific for supporting the SLI-3 HBA device it attached to.
5830  **/
5831 static void
lpfc_sli_driver_resource_unset(struct lpfc_hba * phba)5832 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5833 {
5834 	/* Free device driver memory allocated */
5835 	lpfc_mem_free_all(phba);
5836 
5837 	return;
5838 }
5839 
5840 /**
5841  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5842  * @phba: pointer to lpfc hba data structure.
5843  *
5844  * This routine is invoked to set up the driver internal resources specific to
5845  * support the SLI-4 HBA device it attached to.
5846  *
5847  * Return codes
5848  * 	0 - successful
5849  * 	other values - error
5850  **/
5851 static int
lpfc_sli4_driver_resource_setup(struct lpfc_hba * phba)5852 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5853 {
5854 	LPFC_MBOXQ_t *mboxq;
5855 	MAILBOX_t *mb;
5856 	int rc, i, max_buf_size;
5857 	uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5858 	struct lpfc_mqe *mqe;
5859 	int longs;
5860 	int fof_vectors = 0;
5861 	int extra;
5862 	uint64_t wwn;
5863 	u32 if_type;
5864 	u32 if_fam;
5865 
5866 	phba->sli4_hba.num_online_cpu = num_online_cpus();
5867 	phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5868 	phba->sli4_hba.curr_disp_cpu = 0;
5869 
5870 	/* Get all the module params for configuring this host */
5871 	lpfc_get_cfgparam(phba);
5872 
5873 	/* Set up phase-1 common device driver resources */
5874 	rc = lpfc_setup_driver_resource_phase1(phba);
5875 	if (rc)
5876 		return -ENODEV;
5877 
5878 	/* Before proceed, wait for POST done and device ready */
5879 	rc = lpfc_sli4_post_status_check(phba);
5880 	if (rc)
5881 		return -ENODEV;
5882 
5883 	/*
5884 	 * Initialize timers used by driver
5885 	 */
5886 
5887 	timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
5888 
5889 	/* FCF rediscover timer */
5890 	timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
5891 
5892 	/*
5893 	 * Control structure for handling external multi-buffer mailbox
5894 	 * command pass-through.
5895 	 */
5896 	memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5897 		sizeof(struct lpfc_mbox_ext_buf_ctx));
5898 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5899 
5900 	phba->max_vpi = LPFC_MAX_VPI;
5901 
5902 	/* This will be set to correct value after the read_config mbox */
5903 	phba->max_vports = 0;
5904 
5905 	/* Program the default value of vlan_id and fc_map */
5906 	phba->valid_vlan = 0;
5907 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5908 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5909 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5910 
5911 	/*
5912 	 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5913 	 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5914 	 * The WQ create will allocate the ring.
5915 	 */
5916 
5917 	/*
5918 	 * 1 for cmd, 1 for rsp, NVME adds an extra one
5919 	 * for boundary conditions in its max_sgl_segment template.
5920 	 */
5921 	extra = 2;
5922 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
5923 		extra++;
5924 
5925 	/*
5926 	 * It doesn't matter what family our adapter is in, we are
5927 	 * limited to 2 Pages, 512 SGEs, for our SGL.
5928 	 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5929 	 */
5930 	max_buf_size = (2 * SLI4_PAGE_SIZE);
5931 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - extra)
5932 		phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - extra;
5933 
5934 	/*
5935 	 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5936 	 * used to create the sg_dma_buf_pool must be calculated.
5937 	 */
5938 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5939 		/*
5940 		 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5941 		 * the FCP rsp, and a SGE. Sice we have no control
5942 		 * over how many protection segments the SCSI Layer
5943 		 * will hand us (ie: there could be one for every block
5944 		 * in the IO), just allocate enough SGEs to accomidate
5945 		 * our max amount and we need to limit lpfc_sg_seg_cnt
5946 		 * to minimize the risk of running out.
5947 		 */
5948 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5949 				sizeof(struct fcp_rsp) + max_buf_size;
5950 
5951 		/* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5952 		phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5953 
5954 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5955 			phba->cfg_sg_seg_cnt =
5956 				LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5957 	} else {
5958 		/*
5959 		 * The scsi_buf for a regular I/O holds the FCP cmnd,
5960 		 * the FCP rsp, a SGE for each, and a SGE for up to
5961 		 * cfg_sg_seg_cnt data segments.
5962 		 */
5963 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5964 				sizeof(struct fcp_rsp) +
5965 				((phba->cfg_sg_seg_cnt + extra) *
5966 				sizeof(struct sli4_sge));
5967 
5968 		/* Total SGEs for scsi_sg_list */
5969 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
5970 
5971 		/*
5972 		 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
5973 		 * need to post 1 page for the SGL.
5974 		 */
5975 	}
5976 
5977 	/* Initialize the host templates with the updated values. */
5978 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5979 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5980 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5981 
5982 	if (phba->cfg_sg_dma_buf_size  <= LPFC_MIN_SG_SLI4_BUF_SZ)
5983 		phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5984 	else
5985 		phba->cfg_sg_dma_buf_size =
5986 			SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5987 
5988 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5989 			"9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5990 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5991 			phba->cfg_total_seg_cnt);
5992 
5993 	/* Initialize buffer queue management fields */
5994 	INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
5995 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5996 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5997 
5998 	/*
5999 	 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6000 	 */
6001 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6002 		/* Initialize the Abort scsi buffer list used by driver */
6003 		spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
6004 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
6005 	}
6006 
6007 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6008 		/* Initialize the Abort nvme buffer list used by driver */
6009 		spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
6010 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
6011 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6012 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
6013 	}
6014 
6015 	/* This abort list used by worker thread */
6016 	spin_lock_init(&phba->sli4_hba.sgl_list_lock);
6017 	spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
6018 
6019 	/*
6020 	 * Initialize driver internal slow-path work queues
6021 	 */
6022 
6023 	/* Driver internel slow-path CQ Event pool */
6024 	INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6025 	/* Response IOCB work queue list */
6026 	INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
6027 	/* Asynchronous event CQ Event work queue list */
6028 	INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6029 	/* Fast-path XRI aborted CQ Event work queue list */
6030 	INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6031 	/* Slow-path XRI aborted CQ Event work queue list */
6032 	INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6033 	/* Receive queue CQ Event work queue list */
6034 	INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6035 
6036 	/* Initialize extent block lists. */
6037 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6038 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6039 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6040 	INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6041 
6042 	/* Initialize mboxq lists. If the early init routines fail
6043 	 * these lists need to be correctly initialized.
6044 	 */
6045 	INIT_LIST_HEAD(&phba->sli.mboxq);
6046 	INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6047 
6048 	/* initialize optic_state to 0xFF */
6049 	phba->sli4_hba.lnk_info.optic_state = 0xff;
6050 
6051 	/* Allocate device driver memory */
6052 	rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6053 	if (rc)
6054 		return -ENOMEM;
6055 
6056 	/* IF Type 2 ports get initialized now. */
6057 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
6058 	    LPFC_SLI_INTF_IF_TYPE_2) {
6059 		rc = lpfc_pci_function_reset(phba);
6060 		if (unlikely(rc)) {
6061 			rc = -ENODEV;
6062 			goto out_free_mem;
6063 		}
6064 		phba->temp_sensor_support = 1;
6065 	}
6066 
6067 	/* Create the bootstrap mailbox command */
6068 	rc = lpfc_create_bootstrap_mbox(phba);
6069 	if (unlikely(rc))
6070 		goto out_free_mem;
6071 
6072 	/* Set up the host's endian order with the device. */
6073 	rc = lpfc_setup_endian_order(phba);
6074 	if (unlikely(rc))
6075 		goto out_free_bsmbx;
6076 
6077 	/* Set up the hba's configuration parameters. */
6078 	rc = lpfc_sli4_read_config(phba);
6079 	if (unlikely(rc))
6080 		goto out_free_bsmbx;
6081 	rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6082 	if (unlikely(rc))
6083 		goto out_free_bsmbx;
6084 
6085 	/* IF Type 0 ports get initialized now. */
6086 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6087 	    LPFC_SLI_INTF_IF_TYPE_0) {
6088 		rc = lpfc_pci_function_reset(phba);
6089 		if (unlikely(rc))
6090 			goto out_free_bsmbx;
6091 	}
6092 
6093 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6094 						       GFP_KERNEL);
6095 	if (!mboxq) {
6096 		rc = -ENOMEM;
6097 		goto out_free_bsmbx;
6098 	}
6099 
6100 	/* Check for NVMET being configured */
6101 	phba->nvmet_support = 0;
6102 	if (lpfc_enable_nvmet_cnt) {
6103 
6104 		/* First get WWN of HBA instance */
6105 		lpfc_read_nv(phba, mboxq);
6106 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6107 		if (rc != MBX_SUCCESS) {
6108 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6109 					"6016 Mailbox failed , mbxCmd x%x "
6110 					"READ_NV, mbxStatus x%x\n",
6111 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6112 					bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6113 			mempool_free(mboxq, phba->mbox_mem_pool);
6114 			rc = -EIO;
6115 			goto out_free_bsmbx;
6116 		}
6117 		mb = &mboxq->u.mb;
6118 		memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6119 		       sizeof(uint64_t));
6120 		wwn = cpu_to_be64(wwn);
6121 		phba->sli4_hba.wwnn.u.name = wwn;
6122 		memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6123 		       sizeof(uint64_t));
6124 		/* wwn is WWPN of HBA instance */
6125 		wwn = cpu_to_be64(wwn);
6126 		phba->sli4_hba.wwpn.u.name = wwn;
6127 
6128 		/* Check to see if it matches any module parameter */
6129 		for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6130 			if (wwn == lpfc_enable_nvmet[i]) {
6131 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
6132 				if (lpfc_nvmet_mem_alloc(phba))
6133 					break;
6134 
6135 				phba->nvmet_support = 1; /* a match */
6136 
6137 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6138 						"6017 NVME Target %016llx\n",
6139 						wwn);
6140 #else
6141 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6142 						"6021 Can't enable NVME Target."
6143 						" NVME_TARGET_FC infrastructure"
6144 						" is not in kernel\n");
6145 #endif
6146 				break;
6147 			}
6148 		}
6149 	}
6150 
6151 	lpfc_nvme_mod_param_dep(phba);
6152 
6153 	/* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
6154 	lpfc_supported_pages(mboxq);
6155 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6156 	if (!rc) {
6157 		mqe = &mboxq->u.mqe;
6158 		memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6159 		       LPFC_MAX_SUPPORTED_PAGES);
6160 		for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6161 			switch (pn_page[i]) {
6162 			case LPFC_SLI4_PARAMETERS:
6163 				phba->sli4_hba.pc_sli4_params.supported = 1;
6164 				break;
6165 			default:
6166 				break;
6167 			}
6168 		}
6169 		/* Read the port's SLI4 Parameters capabilities if supported. */
6170 		if (phba->sli4_hba.pc_sli4_params.supported)
6171 			rc = lpfc_pc_sli4_params_get(phba, mboxq);
6172 		if (rc) {
6173 			mempool_free(mboxq, phba->mbox_mem_pool);
6174 			rc = -EIO;
6175 			goto out_free_bsmbx;
6176 		}
6177 	}
6178 
6179 	/*
6180 	 * Get sli4 parameters that override parameters from Port capabilities.
6181 	 * If this call fails, it isn't critical unless the SLI4 parameters come
6182 	 * back in conflict.
6183 	 */
6184 	rc = lpfc_get_sli4_parameters(phba, mboxq);
6185 	if (rc) {
6186 		if_type = bf_get(lpfc_sli_intf_if_type,
6187 				 &phba->sli4_hba.sli_intf);
6188 		if_fam = bf_get(lpfc_sli_intf_sli_family,
6189 				&phba->sli4_hba.sli_intf);
6190 		if (phba->sli4_hba.extents_in_use &&
6191 		    phba->sli4_hba.rpi_hdrs_in_use) {
6192 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6193 				"2999 Unsupported SLI4 Parameters "
6194 				"Extents and RPI headers enabled.\n");
6195 			if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6196 			    if_fam ==  LPFC_SLI_INTF_FAMILY_BE2) {
6197 				mempool_free(mboxq, phba->mbox_mem_pool);
6198 				rc = -EIO;
6199 				goto out_free_bsmbx;
6200 			}
6201 		}
6202 		if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6203 		      if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6204 			mempool_free(mboxq, phba->mbox_mem_pool);
6205 			rc = -EIO;
6206 			goto out_free_bsmbx;
6207 		}
6208 	}
6209 
6210 	mempool_free(mboxq, phba->mbox_mem_pool);
6211 
6212 	/* Verify OAS is supported */
6213 	lpfc_sli4_oas_verify(phba);
6214 	if (phba->cfg_fof)
6215 		fof_vectors = 1;
6216 
6217 	/* Verify all the SLI4 queues */
6218 	rc = lpfc_sli4_queue_verify(phba);
6219 	if (rc)
6220 		goto out_free_bsmbx;
6221 
6222 	/* Create driver internal CQE event pool */
6223 	rc = lpfc_sli4_cq_event_pool_create(phba);
6224 	if (rc)
6225 		goto out_free_bsmbx;
6226 
6227 	/* Initialize sgl lists per host */
6228 	lpfc_init_sgl_list(phba);
6229 
6230 	/* Allocate and initialize active sgl array */
6231 	rc = lpfc_init_active_sgl_array(phba);
6232 	if (rc) {
6233 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6234 				"1430 Failed to initialize sgl list.\n");
6235 		goto out_destroy_cq_event_pool;
6236 	}
6237 	rc = lpfc_sli4_init_rpi_hdrs(phba);
6238 	if (rc) {
6239 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6240 				"1432 Failed to initialize rpi headers.\n");
6241 		goto out_free_active_sgl;
6242 	}
6243 
6244 	/* Allocate eligible FCF bmask memory for FCF roundrobin failover */
6245 	longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6246 	phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
6247 					 GFP_KERNEL);
6248 	if (!phba->fcf.fcf_rr_bmask) {
6249 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6250 				"2759 Failed allocate memory for FCF round "
6251 				"robin failover bmask\n");
6252 		rc = -ENOMEM;
6253 		goto out_remove_rpi_hdrs;
6254 	}
6255 
6256 	phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6257 						sizeof(struct lpfc_hba_eq_hdl),
6258 						GFP_KERNEL);
6259 	if (!phba->sli4_hba.hba_eq_hdl) {
6260 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6261 				"2572 Failed allocate memory for "
6262 				"fast-path per-EQ handle array\n");
6263 		rc = -ENOMEM;
6264 		goto out_free_fcf_rr_bmask;
6265 	}
6266 
6267 	phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6268 					sizeof(struct lpfc_vector_map_info),
6269 					GFP_KERNEL);
6270 	if (!phba->sli4_hba.cpu_map) {
6271 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6272 				"3327 Failed allocate memory for msi-x "
6273 				"interrupt vector mapping\n");
6274 		rc = -ENOMEM;
6275 		goto out_free_hba_eq_hdl;
6276 	}
6277 	if (lpfc_used_cpu == NULL) {
6278 		lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6279 						GFP_KERNEL);
6280 		if (!lpfc_used_cpu) {
6281 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6282 					"3335 Failed allocate memory for msi-x "
6283 					"interrupt vector mapping\n");
6284 			kfree(phba->sli4_hba.cpu_map);
6285 			rc = -ENOMEM;
6286 			goto out_free_hba_eq_hdl;
6287 		}
6288 		for (i = 0; i < lpfc_present_cpu; i++)
6289 			lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6290 	}
6291 
6292 	/*
6293 	 * Enable sr-iov virtual functions if supported and configured
6294 	 * through the module parameter.
6295 	 */
6296 	if (phba->cfg_sriov_nr_virtfn > 0) {
6297 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6298 						 phba->cfg_sriov_nr_virtfn);
6299 		if (rc) {
6300 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6301 					"3020 Requested number of SR-IOV "
6302 					"virtual functions (%d) is not "
6303 					"supported\n",
6304 					phba->cfg_sriov_nr_virtfn);
6305 			phba->cfg_sriov_nr_virtfn = 0;
6306 		}
6307 	}
6308 
6309 	return 0;
6310 
6311 out_free_hba_eq_hdl:
6312 	kfree(phba->sli4_hba.hba_eq_hdl);
6313 out_free_fcf_rr_bmask:
6314 	kfree(phba->fcf.fcf_rr_bmask);
6315 out_remove_rpi_hdrs:
6316 	lpfc_sli4_remove_rpi_hdrs(phba);
6317 out_free_active_sgl:
6318 	lpfc_free_active_sgl(phba);
6319 out_destroy_cq_event_pool:
6320 	lpfc_sli4_cq_event_pool_destroy(phba);
6321 out_free_bsmbx:
6322 	lpfc_destroy_bootstrap_mbox(phba);
6323 out_free_mem:
6324 	lpfc_mem_free(phba);
6325 	return rc;
6326 }
6327 
6328 /**
6329  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6330  * @phba: pointer to lpfc hba data structure.
6331  *
6332  * This routine is invoked to unset the driver internal resources set up
6333  * specific for supporting the SLI-4 HBA device it attached to.
6334  **/
6335 static void
lpfc_sli4_driver_resource_unset(struct lpfc_hba * phba)6336 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6337 {
6338 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6339 
6340 	/* Free memory allocated for msi-x interrupt vector to CPU mapping */
6341 	kfree(phba->sli4_hba.cpu_map);
6342 	phba->sli4_hba.num_present_cpu = 0;
6343 	phba->sli4_hba.num_online_cpu = 0;
6344 	phba->sli4_hba.curr_disp_cpu = 0;
6345 
6346 	/* Free memory allocated for fast-path work queue handles */
6347 	kfree(phba->sli4_hba.hba_eq_hdl);
6348 
6349 	/* Free the allocated rpi headers. */
6350 	lpfc_sli4_remove_rpi_hdrs(phba);
6351 	lpfc_sli4_remove_rpis(phba);
6352 
6353 	/* Free eligible FCF index bmask */
6354 	kfree(phba->fcf.fcf_rr_bmask);
6355 
6356 	/* Free the ELS sgl list */
6357 	lpfc_free_active_sgl(phba);
6358 	lpfc_free_els_sgl_list(phba);
6359 	lpfc_free_nvmet_sgl_list(phba);
6360 
6361 	/* Free the completion queue EQ event pool */
6362 	lpfc_sli4_cq_event_release_all(phba);
6363 	lpfc_sli4_cq_event_pool_destroy(phba);
6364 
6365 	/* Release resource identifiers. */
6366 	lpfc_sli4_dealloc_resource_identifiers(phba);
6367 
6368 	/* Free the bsmbx region. */
6369 	lpfc_destroy_bootstrap_mbox(phba);
6370 
6371 	/* Free the SLI Layer memory with SLI4 HBAs */
6372 	lpfc_mem_free_all(phba);
6373 
6374 	/* Free the current connect table */
6375 	list_for_each_entry_safe(conn_entry, next_conn_entry,
6376 		&phba->fcf_conn_rec_list, list) {
6377 		list_del_init(&conn_entry->list);
6378 		kfree(conn_entry);
6379 	}
6380 
6381 	return;
6382 }
6383 
6384 /**
6385  * lpfc_init_api_table_setup - Set up init api function jump table
6386  * @phba: The hba struct for which this call is being executed.
6387  * @dev_grp: The HBA PCI-Device group number.
6388  *
6389  * This routine sets up the device INIT interface API function jump table
6390  * in @phba struct.
6391  *
6392  * Returns: 0 - success, -ENODEV - failure.
6393  **/
6394 int
lpfc_init_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)6395 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6396 {
6397 	phba->lpfc_hba_init_link = lpfc_hba_init_link;
6398 	phba->lpfc_hba_down_link = lpfc_hba_down_link;
6399 	phba->lpfc_selective_reset = lpfc_selective_reset;
6400 	switch (dev_grp) {
6401 	case LPFC_PCI_DEV_LP:
6402 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6403 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6404 		phba->lpfc_stop_port = lpfc_stop_port_s3;
6405 		break;
6406 	case LPFC_PCI_DEV_OC:
6407 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6408 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6409 		phba->lpfc_stop_port = lpfc_stop_port_s4;
6410 		break;
6411 	default:
6412 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6413 				"1431 Invalid HBA PCI-device group: 0x%x\n",
6414 				dev_grp);
6415 		return -ENODEV;
6416 		break;
6417 	}
6418 	return 0;
6419 }
6420 
6421 /**
6422  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6423  * @phba: pointer to lpfc hba data structure.
6424  *
6425  * This routine is invoked to set up the driver internal resources after the
6426  * device specific resource setup to support the HBA device it attached to.
6427  *
6428  * Return codes
6429  * 	0 - successful
6430  * 	other values - error
6431  **/
6432 static int
lpfc_setup_driver_resource_phase2(struct lpfc_hba * phba)6433 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6434 {
6435 	int error;
6436 
6437 	/* Startup the kernel thread for this host adapter. */
6438 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
6439 					  "lpfc_worker_%d", phba->brd_no);
6440 	if (IS_ERR(phba->worker_thread)) {
6441 		error = PTR_ERR(phba->worker_thread);
6442 		return error;
6443 	}
6444 
6445 	/* The lpfc_wq workqueue for deferred irq use, is only used for SLI4 */
6446 	if (phba->sli_rev == LPFC_SLI_REV4)
6447 		phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6448 	else
6449 		phba->wq = NULL;
6450 
6451 	return 0;
6452 }
6453 
6454 /**
6455  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6456  * @phba: pointer to lpfc hba data structure.
6457  *
6458  * This routine is invoked to unset the driver internal resources set up after
6459  * the device specific resource setup for supporting the HBA device it
6460  * attached to.
6461  **/
6462 static void
lpfc_unset_driver_resource_phase2(struct lpfc_hba * phba)6463 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6464 {
6465 	if (phba->wq) {
6466 		flush_workqueue(phba->wq);
6467 		destroy_workqueue(phba->wq);
6468 		phba->wq = NULL;
6469 	}
6470 
6471 	/* Stop kernel worker thread */
6472 	if (phba->worker_thread)
6473 		kthread_stop(phba->worker_thread);
6474 }
6475 
6476 /**
6477  * lpfc_free_iocb_list - Free iocb list.
6478  * @phba: pointer to lpfc hba data structure.
6479  *
6480  * This routine is invoked to free the driver's IOCB list and memory.
6481  **/
6482 void
lpfc_free_iocb_list(struct lpfc_hba * phba)6483 lpfc_free_iocb_list(struct lpfc_hba *phba)
6484 {
6485 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6486 
6487 	spin_lock_irq(&phba->hbalock);
6488 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
6489 				 &phba->lpfc_iocb_list, list) {
6490 		list_del(&iocbq_entry->list);
6491 		kfree(iocbq_entry);
6492 		phba->total_iocbq_bufs--;
6493 	}
6494 	spin_unlock_irq(&phba->hbalock);
6495 
6496 	return;
6497 }
6498 
6499 /**
6500  * lpfc_init_iocb_list - Allocate and initialize iocb list.
6501  * @phba: pointer to lpfc hba data structure.
6502  *
6503  * This routine is invoked to allocate and initizlize the driver's IOCB
6504  * list and set up the IOCB tag array accordingly.
6505  *
6506  * Return codes
6507  *	0 - successful
6508  *	other values - error
6509  **/
6510 int
lpfc_init_iocb_list(struct lpfc_hba * phba,int iocb_count)6511 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6512 {
6513 	struct lpfc_iocbq *iocbq_entry = NULL;
6514 	uint16_t iotag;
6515 	int i;
6516 
6517 	/* Initialize and populate the iocb list per host.  */
6518 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
6519 	for (i = 0; i < iocb_count; i++) {
6520 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
6521 		if (iocbq_entry == NULL) {
6522 			printk(KERN_ERR "%s: only allocated %d iocbs of "
6523 				"expected %d count. Unloading driver.\n",
6524 				__func__, i, LPFC_IOCB_LIST_CNT);
6525 			goto out_free_iocbq;
6526 		}
6527 
6528 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6529 		if (iotag == 0) {
6530 			kfree(iocbq_entry);
6531 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
6532 				"Unloading driver.\n", __func__);
6533 			goto out_free_iocbq;
6534 		}
6535 		iocbq_entry->sli4_lxritag = NO_XRI;
6536 		iocbq_entry->sli4_xritag = NO_XRI;
6537 
6538 		spin_lock_irq(&phba->hbalock);
6539 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6540 		phba->total_iocbq_bufs++;
6541 		spin_unlock_irq(&phba->hbalock);
6542 	}
6543 
6544 	return 0;
6545 
6546 out_free_iocbq:
6547 	lpfc_free_iocb_list(phba);
6548 
6549 	return -ENOMEM;
6550 }
6551 
6552 /**
6553  * lpfc_free_sgl_list - Free a given sgl list.
6554  * @phba: pointer to lpfc hba data structure.
6555  * @sglq_list: pointer to the head of sgl list.
6556  *
6557  * This routine is invoked to free a give sgl list and memory.
6558  **/
6559 void
lpfc_free_sgl_list(struct lpfc_hba * phba,struct list_head * sglq_list)6560 lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
6561 {
6562 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6563 
6564 	list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6565 		list_del(&sglq_entry->list);
6566 		lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6567 		kfree(sglq_entry);
6568 	}
6569 }
6570 
6571 /**
6572  * lpfc_free_els_sgl_list - Free els sgl list.
6573  * @phba: pointer to lpfc hba data structure.
6574  *
6575  * This routine is invoked to free the driver's els sgl list and memory.
6576  **/
6577 static void
lpfc_free_els_sgl_list(struct lpfc_hba * phba)6578 lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6579 {
6580 	LIST_HEAD(sglq_list);
6581 
6582 	/* Retrieve all els sgls from driver list */
6583 	spin_lock_irq(&phba->hbalock);
6584 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6585 	list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6586 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6587 	spin_unlock_irq(&phba->hbalock);
6588 
6589 	/* Now free the sgl list */
6590 	lpfc_free_sgl_list(phba, &sglq_list);
6591 }
6592 
6593 /**
6594  * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6595  * @phba: pointer to lpfc hba data structure.
6596  *
6597  * This routine is invoked to free the driver's nvmet sgl list and memory.
6598  **/
6599 static void
lpfc_free_nvmet_sgl_list(struct lpfc_hba * phba)6600 lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6601 {
6602 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6603 	LIST_HEAD(sglq_list);
6604 
6605 	/* Retrieve all nvmet sgls from driver list */
6606 	spin_lock_irq(&phba->hbalock);
6607 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6608 	list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6609 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6610 	spin_unlock_irq(&phba->hbalock);
6611 
6612 	/* Now free the sgl list */
6613 	list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6614 		list_del(&sglq_entry->list);
6615 		lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6616 		kfree(sglq_entry);
6617 	}
6618 
6619 	/* Update the nvmet_xri_cnt to reflect no current sgls.
6620 	 * The next initialization cycle sets the count and allocates
6621 	 * the sgls over again.
6622 	 */
6623 	phba->sli4_hba.nvmet_xri_cnt = 0;
6624 }
6625 
6626 /**
6627  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6628  * @phba: pointer to lpfc hba data structure.
6629  *
6630  * This routine is invoked to allocate the driver's active sgl memory.
6631  * This array will hold the sglq_entry's for active IOs.
6632  **/
6633 static int
lpfc_init_active_sgl_array(struct lpfc_hba * phba)6634 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6635 {
6636 	int size;
6637 	size = sizeof(struct lpfc_sglq *);
6638 	size *= phba->sli4_hba.max_cfg_param.max_xri;
6639 
6640 	phba->sli4_hba.lpfc_sglq_active_list =
6641 		kzalloc(size, GFP_KERNEL);
6642 	if (!phba->sli4_hba.lpfc_sglq_active_list)
6643 		return -ENOMEM;
6644 	return 0;
6645 }
6646 
6647 /**
6648  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6649  * @phba: pointer to lpfc hba data structure.
6650  *
6651  * This routine is invoked to walk through the array of active sglq entries
6652  * and free all of the resources.
6653  * This is just a place holder for now.
6654  **/
6655 static void
lpfc_free_active_sgl(struct lpfc_hba * phba)6656 lpfc_free_active_sgl(struct lpfc_hba *phba)
6657 {
6658 	kfree(phba->sli4_hba.lpfc_sglq_active_list);
6659 }
6660 
6661 /**
6662  * lpfc_init_sgl_list - Allocate and initialize sgl list.
6663  * @phba: pointer to lpfc hba data structure.
6664  *
6665  * This routine is invoked to allocate and initizlize the driver's sgl
6666  * list and set up the sgl xritag tag array accordingly.
6667  *
6668  **/
6669 static void
lpfc_init_sgl_list(struct lpfc_hba * phba)6670 lpfc_init_sgl_list(struct lpfc_hba *phba)
6671 {
6672 	/* Initialize and populate the sglq list per host/VF. */
6673 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
6674 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
6675 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6676 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6677 
6678 	/* els xri-sgl book keeping */
6679 	phba->sli4_hba.els_xri_cnt = 0;
6680 
6681 	/* scsi xri-buffer book keeping */
6682 	phba->sli4_hba.scsi_xri_cnt = 0;
6683 
6684 	/* nvme xri-buffer book keeping */
6685 	phba->sli4_hba.nvme_xri_cnt = 0;
6686 }
6687 
6688 /**
6689  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6690  * @phba: pointer to lpfc hba data structure.
6691  *
6692  * This routine is invoked to post rpi header templates to the
6693  * port for those SLI4 ports that do not support extents.  This routine
6694  * posts a PAGE_SIZE memory region to the port to hold up to
6695  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
6696  * and should be called only when interrupts are disabled.
6697  *
6698  * Return codes
6699  * 	0 - successful
6700  *	-ERROR - otherwise.
6701  **/
6702 int
lpfc_sli4_init_rpi_hdrs(struct lpfc_hba * phba)6703 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6704 {
6705 	int rc = 0;
6706 	struct lpfc_rpi_hdr *rpi_hdr;
6707 
6708 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
6709 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6710 		return rc;
6711 	if (phba->sli4_hba.extents_in_use)
6712 		return -EIO;
6713 
6714 	rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6715 	if (!rpi_hdr) {
6716 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6717 				"0391 Error during rpi post operation\n");
6718 		lpfc_sli4_remove_rpis(phba);
6719 		rc = -ENODEV;
6720 	}
6721 
6722 	return rc;
6723 }
6724 
6725 /**
6726  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6727  * @phba: pointer to lpfc hba data structure.
6728  *
6729  * This routine is invoked to allocate a single 4KB memory region to
6730  * support rpis and stores them in the phba.  This single region
6731  * provides support for up to 64 rpis.  The region is used globally
6732  * by the device.
6733  *
6734  * Returns:
6735  *   A valid rpi hdr on success.
6736  *   A NULL pointer on any failure.
6737  **/
6738 struct lpfc_rpi_hdr *
lpfc_sli4_create_rpi_hdr(struct lpfc_hba * phba)6739 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6740 {
6741 	uint16_t rpi_limit, curr_rpi_range;
6742 	struct lpfc_dmabuf *dmabuf;
6743 	struct lpfc_rpi_hdr *rpi_hdr;
6744 
6745 	/*
6746 	 * If the SLI4 port supports extents, posting the rpi header isn't
6747 	 * required.  Set the expected maximum count and let the actual value
6748 	 * get set when extents are fully allocated.
6749 	 */
6750 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6751 		return NULL;
6752 	if (phba->sli4_hba.extents_in_use)
6753 		return NULL;
6754 
6755 	/* The limit on the logical index is just the max_rpi count. */
6756 	rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
6757 
6758 	spin_lock_irq(&phba->hbalock);
6759 	/*
6760 	 * Establish the starting RPI in this header block.  The starting
6761 	 * rpi is normalized to a zero base because the physical rpi is
6762 	 * port based.
6763 	 */
6764 	curr_rpi_range = phba->sli4_hba.next_rpi;
6765 	spin_unlock_irq(&phba->hbalock);
6766 
6767 	/* Reached full RPI range */
6768 	if (curr_rpi_range == rpi_limit)
6769 		return NULL;
6770 
6771 	/*
6772 	 * First allocate the protocol header region for the port.  The
6773 	 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6774 	 */
6775 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6776 	if (!dmabuf)
6777 		return NULL;
6778 
6779 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6780 					   LPFC_HDR_TEMPLATE_SIZE,
6781 					   &dmabuf->phys, GFP_KERNEL);
6782 	if (!dmabuf->virt) {
6783 		rpi_hdr = NULL;
6784 		goto err_free_dmabuf;
6785 	}
6786 
6787 	if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6788 		rpi_hdr = NULL;
6789 		goto err_free_coherent;
6790 	}
6791 
6792 	/* Save the rpi header data for cleanup later. */
6793 	rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6794 	if (!rpi_hdr)
6795 		goto err_free_coherent;
6796 
6797 	rpi_hdr->dmabuf = dmabuf;
6798 	rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6799 	rpi_hdr->page_count = 1;
6800 	spin_lock_irq(&phba->hbalock);
6801 
6802 	/* The rpi_hdr stores the logical index only. */
6803 	rpi_hdr->start_rpi = curr_rpi_range;
6804 	rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
6805 	list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6806 
6807 	spin_unlock_irq(&phba->hbalock);
6808 	return rpi_hdr;
6809 
6810  err_free_coherent:
6811 	dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6812 			  dmabuf->virt, dmabuf->phys);
6813  err_free_dmabuf:
6814 	kfree(dmabuf);
6815 	return NULL;
6816 }
6817 
6818 /**
6819  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6820  * @phba: pointer to lpfc hba data structure.
6821  *
6822  * This routine is invoked to remove all memory resources allocated
6823  * to support rpis for SLI4 ports not supporting extents. This routine
6824  * presumes the caller has released all rpis consumed by fabric or port
6825  * logins and is prepared to have the header pages removed.
6826  **/
6827 void
lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba * phba)6828 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6829 {
6830 	struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6831 
6832 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6833 		goto exit;
6834 
6835 	list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6836 				 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6837 		list_del(&rpi_hdr->list);
6838 		dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6839 				  rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6840 		kfree(rpi_hdr->dmabuf);
6841 		kfree(rpi_hdr);
6842 	}
6843  exit:
6844 	/* There are no rpis available to the port now. */
6845 	phba->sli4_hba.next_rpi = 0;
6846 }
6847 
6848 /**
6849  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6850  * @pdev: pointer to pci device data structure.
6851  *
6852  * This routine is invoked to allocate the driver hba data structure for an
6853  * HBA device. If the allocation is successful, the phba reference to the
6854  * PCI device data structure is set.
6855  *
6856  * Return codes
6857  *      pointer to @phba - successful
6858  *      NULL - error
6859  **/
6860 static struct lpfc_hba *
lpfc_hba_alloc(struct pci_dev * pdev)6861 lpfc_hba_alloc(struct pci_dev *pdev)
6862 {
6863 	struct lpfc_hba *phba;
6864 
6865 	/* Allocate memory for HBA structure */
6866 	phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6867 	if (!phba) {
6868 		dev_err(&pdev->dev, "failed to allocate hba struct\n");
6869 		return NULL;
6870 	}
6871 
6872 	/* Set reference to PCI device in HBA structure */
6873 	phba->pcidev = pdev;
6874 
6875 	/* Assign an unused board number */
6876 	phba->brd_no = lpfc_get_instance();
6877 	if (phba->brd_no < 0) {
6878 		kfree(phba);
6879 		return NULL;
6880 	}
6881 	phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
6882 
6883 	spin_lock_init(&phba->ct_ev_lock);
6884 	INIT_LIST_HEAD(&phba->ct_ev_waiters);
6885 
6886 	return phba;
6887 }
6888 
6889 /**
6890  * lpfc_hba_free - Free driver hba data structure with a device.
6891  * @phba: pointer to lpfc hba data structure.
6892  *
6893  * This routine is invoked to free the driver hba data structure with an
6894  * HBA device.
6895  **/
6896 static void
lpfc_hba_free(struct lpfc_hba * phba)6897 lpfc_hba_free(struct lpfc_hba *phba)
6898 {
6899 	/* Release the driver assigned board number */
6900 	idr_remove(&lpfc_hba_index, phba->brd_no);
6901 
6902 	/* Free memory allocated with sli3 rings */
6903 	kfree(phba->sli.sli3_ring);
6904 	phba->sli.sli3_ring = NULL;
6905 
6906 	kfree(phba);
6907 	return;
6908 }
6909 
6910 /**
6911  * lpfc_create_shost - Create hba physical port with associated scsi host.
6912  * @phba: pointer to lpfc hba data structure.
6913  *
6914  * This routine is invoked to create HBA physical port and associate a SCSI
6915  * host with it.
6916  *
6917  * Return codes
6918  *      0 - successful
6919  *      other values - error
6920  **/
6921 static int
lpfc_create_shost(struct lpfc_hba * phba)6922 lpfc_create_shost(struct lpfc_hba *phba)
6923 {
6924 	struct lpfc_vport *vport;
6925 	struct Scsi_Host  *shost;
6926 
6927 	/* Initialize HBA FC structure */
6928 	phba->fc_edtov = FF_DEF_EDTOV;
6929 	phba->fc_ratov = FF_DEF_RATOV;
6930 	phba->fc_altov = FF_DEF_ALTOV;
6931 	phba->fc_arbtov = FF_DEF_ARBTOV;
6932 
6933 	atomic_set(&phba->sdev_cnt, 0);
6934 	atomic_set(&phba->fc4ScsiInputRequests, 0);
6935 	atomic_set(&phba->fc4ScsiOutputRequests, 0);
6936 	atomic_set(&phba->fc4ScsiControlRequests, 0);
6937 	atomic_set(&phba->fc4ScsiIoCmpls, 0);
6938 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
6939 	if (!vport)
6940 		return -ENODEV;
6941 
6942 	shost = lpfc_shost_from_vport(vport);
6943 	phba->pport = vport;
6944 
6945 	if (phba->nvmet_support) {
6946 		/* Only 1 vport (pport) will support NVME target */
6947 		if (phba->txrdy_payload_pool == NULL) {
6948 			phba->txrdy_payload_pool = dma_pool_create(
6949 				"txrdy_pool", &phba->pcidev->dev,
6950 				TXRDY_PAYLOAD_LEN, 16, 0);
6951 			if (phba->txrdy_payload_pool) {
6952 				phba->targetport = NULL;
6953 				phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6954 				lpfc_printf_log(phba, KERN_INFO,
6955 						LOG_INIT | LOG_NVME_DISC,
6956 						"6076 NVME Target Found\n");
6957 			}
6958 		}
6959 	}
6960 
6961 	lpfc_debugfs_initialize(vport);
6962 	/* Put reference to SCSI host to driver's device private data */
6963 	pci_set_drvdata(phba->pcidev, shost);
6964 
6965 	/*
6966 	 * At this point we are fully registered with PSA. In addition,
6967 	 * any initial discovery should be completed.
6968 	 */
6969 	vport->load_flag |= FC_ALLOW_FDMI;
6970 	if (phba->cfg_enable_SmartSAN ||
6971 	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
6972 
6973 		/* Setup appropriate attribute masks */
6974 		vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
6975 		if (phba->cfg_enable_SmartSAN)
6976 			vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6977 		else
6978 			vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6979 	}
6980 	return 0;
6981 }
6982 
6983 /**
6984  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6985  * @phba: pointer to lpfc hba data structure.
6986  *
6987  * This routine is invoked to destroy HBA physical port and the associated
6988  * SCSI host.
6989  **/
6990 static void
lpfc_destroy_shost(struct lpfc_hba * phba)6991 lpfc_destroy_shost(struct lpfc_hba *phba)
6992 {
6993 	struct lpfc_vport *vport = phba->pport;
6994 
6995 	/* Destroy physical port that associated with the SCSI host */
6996 	destroy_port(vport);
6997 
6998 	return;
6999 }
7000 
7001 /**
7002  * lpfc_setup_bg - Setup Block guard structures and debug areas.
7003  * @phba: pointer to lpfc hba data structure.
7004  * @shost: the shost to be used to detect Block guard settings.
7005  *
7006  * This routine sets up the local Block guard protocol settings for @shost.
7007  * This routine also allocates memory for debugging bg buffers.
7008  **/
7009 static void
lpfc_setup_bg(struct lpfc_hba * phba,struct Scsi_Host * shost)7010 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7011 {
7012 	uint32_t old_mask;
7013 	uint32_t old_guard;
7014 
7015 	int pagecnt = 10;
7016 	if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7017 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7018 				"1478 Registering BlockGuard with the "
7019 				"SCSI layer\n");
7020 
7021 		old_mask = phba->cfg_prot_mask;
7022 		old_guard = phba->cfg_prot_guard;
7023 
7024 		/* Only allow supported values */
7025 		phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
7026 			SHOST_DIX_TYPE0_PROTECTION |
7027 			SHOST_DIX_TYPE1_PROTECTION);
7028 		phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7029 					 SHOST_DIX_GUARD_CRC);
7030 
7031 		/* DIF Type 1 protection for profiles AST1/C1 is end to end */
7032 		if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7033 			phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
7034 
7035 		if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7036 			if ((old_mask != phba->cfg_prot_mask) ||
7037 				(old_guard != phba->cfg_prot_guard))
7038 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7039 					"1475 Registering BlockGuard with the "
7040 					"SCSI layer: mask %d  guard %d\n",
7041 					phba->cfg_prot_mask,
7042 					phba->cfg_prot_guard);
7043 
7044 			scsi_host_set_prot(shost, phba->cfg_prot_mask);
7045 			scsi_host_set_guard(shost, phba->cfg_prot_guard);
7046 		} else
7047 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7048 				"1479 Not Registering BlockGuard with the SCSI "
7049 				"layer, Bad protection parameters: %d %d\n",
7050 				old_mask, old_guard);
7051 	}
7052 
7053 	if (!_dump_buf_data) {
7054 		while (pagecnt) {
7055 			spin_lock_init(&_dump_buf_lock);
7056 			_dump_buf_data =
7057 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
7058 			if (_dump_buf_data) {
7059 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7060 					"9043 BLKGRD: allocated %d pages for "
7061 				       "_dump_buf_data at 0x%p\n",
7062 				       (1 << pagecnt), _dump_buf_data);
7063 				_dump_buf_data_order = pagecnt;
7064 				memset(_dump_buf_data, 0,
7065 				       ((1 << PAGE_SHIFT) << pagecnt));
7066 				break;
7067 			} else
7068 				--pagecnt;
7069 		}
7070 		if (!_dump_buf_data_order)
7071 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7072 				"9044 BLKGRD: ERROR unable to allocate "
7073 			       "memory for hexdump\n");
7074 	} else
7075 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7076 			"9045 BLKGRD: already allocated _dump_buf_data=0x%p"
7077 		       "\n", _dump_buf_data);
7078 	if (!_dump_buf_dif) {
7079 		while (pagecnt) {
7080 			_dump_buf_dif =
7081 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
7082 			if (_dump_buf_dif) {
7083 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7084 					"9046 BLKGRD: allocated %d pages for "
7085 				       "_dump_buf_dif at 0x%p\n",
7086 				       (1 << pagecnt), _dump_buf_dif);
7087 				_dump_buf_dif_order = pagecnt;
7088 				memset(_dump_buf_dif, 0,
7089 				       ((1 << PAGE_SHIFT) << pagecnt));
7090 				break;
7091 			} else
7092 				--pagecnt;
7093 		}
7094 		if (!_dump_buf_dif_order)
7095 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7096 			"9047 BLKGRD: ERROR unable to allocate "
7097 			       "memory for hexdump\n");
7098 	} else
7099 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7100 			"9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
7101 		       _dump_buf_dif);
7102 }
7103 
7104 /**
7105  * lpfc_post_init_setup - Perform necessary device post initialization setup.
7106  * @phba: pointer to lpfc hba data structure.
7107  *
7108  * This routine is invoked to perform all the necessary post initialization
7109  * setup for the device.
7110  **/
7111 static void
lpfc_post_init_setup(struct lpfc_hba * phba)7112 lpfc_post_init_setup(struct lpfc_hba *phba)
7113 {
7114 	struct Scsi_Host  *shost;
7115 	struct lpfc_adapter_event_header adapter_event;
7116 
7117 	/* Get the default values for Model Name and Description */
7118 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7119 
7120 	/*
7121 	 * hba setup may have changed the hba_queue_depth so we need to
7122 	 * adjust the value of can_queue.
7123 	 */
7124 	shost = pci_get_drvdata(phba->pcidev);
7125 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
7126 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7127 		lpfc_setup_bg(phba, shost);
7128 
7129 	lpfc_host_attrib_init(shost);
7130 
7131 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7132 		spin_lock_irq(shost->host_lock);
7133 		lpfc_poll_start_timer(phba);
7134 		spin_unlock_irq(shost->host_lock);
7135 	}
7136 
7137 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7138 			"0428 Perform SCSI scan\n");
7139 	/* Send board arrival event to upper layer */
7140 	adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7141 	adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7142 	fc_host_post_vendor_event(shost, fc_get_event_number(),
7143 				  sizeof(adapter_event),
7144 				  (char *) &adapter_event,
7145 				  LPFC_NL_VENDOR_ID);
7146 	return;
7147 }
7148 
7149 /**
7150  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7151  * @phba: pointer to lpfc hba data structure.
7152  *
7153  * This routine is invoked to set up the PCI device memory space for device
7154  * with SLI-3 interface spec.
7155  *
7156  * Return codes
7157  * 	0 - successful
7158  * 	other values - error
7159  **/
7160 static int
lpfc_sli_pci_mem_setup(struct lpfc_hba * phba)7161 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7162 {
7163 	struct pci_dev *pdev;
7164 	unsigned long bar0map_len, bar2map_len;
7165 	int i, hbq_count;
7166 	void *ptr;
7167 	int error = -ENODEV;
7168 
7169 	/* Obtain PCI device reference */
7170 	if (!phba->pcidev)
7171 		return error;
7172 	else
7173 		pdev = phba->pcidev;
7174 
7175 	/* Set the device DMA mask size */
7176 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7177 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7178 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7179 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7180 			return error;
7181 		}
7182 	}
7183 
7184 	/* Get the bus address of Bar0 and Bar2 and the number of bytes
7185 	 * required by each mapping.
7186 	 */
7187 	phba->pci_bar0_map = pci_resource_start(pdev, 0);
7188 	bar0map_len = pci_resource_len(pdev, 0);
7189 
7190 	phba->pci_bar2_map = pci_resource_start(pdev, 2);
7191 	bar2map_len = pci_resource_len(pdev, 2);
7192 
7193 	/* Map HBA SLIM to a kernel virtual address. */
7194 	phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7195 	if (!phba->slim_memmap_p) {
7196 		dev_printk(KERN_ERR, &pdev->dev,
7197 			   "ioremap failed for SLIM memory.\n");
7198 		goto out;
7199 	}
7200 
7201 	/* Map HBA Control Registers to a kernel virtual address. */
7202 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7203 	if (!phba->ctrl_regs_memmap_p) {
7204 		dev_printk(KERN_ERR, &pdev->dev,
7205 			   "ioremap failed for HBA control registers.\n");
7206 		goto out_iounmap_slim;
7207 	}
7208 
7209 	/* Allocate memory for SLI-2 structures */
7210 	phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7211 						&phba->slim2p.phys, GFP_KERNEL);
7212 	if (!phba->slim2p.virt)
7213 		goto out_iounmap;
7214 
7215 	phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7216 	phba->mbox_ext = (phba->slim2p.virt +
7217 		offsetof(struct lpfc_sli2_slim, mbx_ext_words));
7218 	phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7219 	phba->IOCBs = (phba->slim2p.virt +
7220 		       offsetof(struct lpfc_sli2_slim, IOCBs));
7221 
7222 	phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7223 						 lpfc_sli_hbq_size(),
7224 						 &phba->hbqslimp.phys,
7225 						 GFP_KERNEL);
7226 	if (!phba->hbqslimp.virt)
7227 		goto out_free_slim;
7228 
7229 	hbq_count = lpfc_sli_hbq_count();
7230 	ptr = phba->hbqslimp.virt;
7231 	for (i = 0; i < hbq_count; ++i) {
7232 		phba->hbqs[i].hbq_virt = ptr;
7233 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7234 		ptr += (lpfc_hbq_defs[i]->entry_count *
7235 			sizeof(struct lpfc_hbq_entry));
7236 	}
7237 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7238 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7239 
7240 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7241 
7242 	phba->MBslimaddr = phba->slim_memmap_p;
7243 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7244 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7245 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7246 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
7247 
7248 	return 0;
7249 
7250 out_free_slim:
7251 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7252 			  phba->slim2p.virt, phba->slim2p.phys);
7253 out_iounmap:
7254 	iounmap(phba->ctrl_regs_memmap_p);
7255 out_iounmap_slim:
7256 	iounmap(phba->slim_memmap_p);
7257 out:
7258 	return error;
7259 }
7260 
7261 /**
7262  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7263  * @phba: pointer to lpfc hba data structure.
7264  *
7265  * This routine is invoked to unset the PCI device memory space for device
7266  * with SLI-3 interface spec.
7267  **/
7268 static void
lpfc_sli_pci_mem_unset(struct lpfc_hba * phba)7269 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7270 {
7271 	struct pci_dev *pdev;
7272 
7273 	/* Obtain PCI device reference */
7274 	if (!phba->pcidev)
7275 		return;
7276 	else
7277 		pdev = phba->pcidev;
7278 
7279 	/* Free coherent DMA memory allocated */
7280 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7281 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
7282 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7283 			  phba->slim2p.virt, phba->slim2p.phys);
7284 
7285 	/* I/O memory unmap */
7286 	iounmap(phba->ctrl_regs_memmap_p);
7287 	iounmap(phba->slim_memmap_p);
7288 
7289 	return;
7290 }
7291 
7292 /**
7293  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7294  * @phba: pointer to lpfc hba data structure.
7295  *
7296  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7297  * done and check status.
7298  *
7299  * Return 0 if successful, otherwise -ENODEV.
7300  **/
7301 int
lpfc_sli4_post_status_check(struct lpfc_hba * phba)7302 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7303 {
7304 	struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7305 	struct lpfc_register reg_data;
7306 	int i, port_error = 0;
7307 	uint32_t if_type;
7308 
7309 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7310 	memset(&reg_data, 0, sizeof(reg_data));
7311 	if (!phba->sli4_hba.PSMPHRregaddr)
7312 		return -ENODEV;
7313 
7314 	/* Wait up to 30 seconds for the SLI Port POST done and ready */
7315 	for (i = 0; i < 3000; i++) {
7316 		if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7317 			&portsmphr_reg.word0) ||
7318 			(bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
7319 			/* Port has a fatal POST error, break out */
7320 			port_error = -ENODEV;
7321 			break;
7322 		}
7323 		if (LPFC_POST_STAGE_PORT_READY ==
7324 		    bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
7325 			break;
7326 		msleep(10);
7327 	}
7328 
7329 	/*
7330 	 * If there was a port error during POST, then don't proceed with
7331 	 * other register reads as the data may not be valid.  Just exit.
7332 	 */
7333 	if (port_error) {
7334 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7335 			"1408 Port Failed POST - portsmphr=0x%x, "
7336 			"perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7337 			"scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7338 			portsmphr_reg.word0,
7339 			bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7340 			bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7341 			bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7342 			bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7343 			bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7344 			bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7345 			bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7346 			bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7347 	} else {
7348 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7349 				"2534 Device Info: SLIFamily=0x%x, "
7350 				"SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7351 				"SLIHint_2=0x%x, FT=0x%x\n",
7352 				bf_get(lpfc_sli_intf_sli_family,
7353 				       &phba->sli4_hba.sli_intf),
7354 				bf_get(lpfc_sli_intf_slirev,
7355 				       &phba->sli4_hba.sli_intf),
7356 				bf_get(lpfc_sli_intf_if_type,
7357 				       &phba->sli4_hba.sli_intf),
7358 				bf_get(lpfc_sli_intf_sli_hint1,
7359 				       &phba->sli4_hba.sli_intf),
7360 				bf_get(lpfc_sli_intf_sli_hint2,
7361 				       &phba->sli4_hba.sli_intf),
7362 				bf_get(lpfc_sli_intf_func_type,
7363 				       &phba->sli4_hba.sli_intf));
7364 		/*
7365 		 * Check for other Port errors during the initialization
7366 		 * process.  Fail the load if the port did not come up
7367 		 * correctly.
7368 		 */
7369 		if_type = bf_get(lpfc_sli_intf_if_type,
7370 				 &phba->sli4_hba.sli_intf);
7371 		switch (if_type) {
7372 		case LPFC_SLI_INTF_IF_TYPE_0:
7373 			phba->sli4_hba.ue_mask_lo =
7374 			      readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7375 			phba->sli4_hba.ue_mask_hi =
7376 			      readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7377 			uerrlo_reg.word0 =
7378 			      readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7379 			uerrhi_reg.word0 =
7380 				readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7381 			if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7382 			    (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7383 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7384 						"1422 Unrecoverable Error "
7385 						"Detected during POST "
7386 						"uerr_lo_reg=0x%x, "
7387 						"uerr_hi_reg=0x%x, "
7388 						"ue_mask_lo_reg=0x%x, "
7389 						"ue_mask_hi_reg=0x%x\n",
7390 						uerrlo_reg.word0,
7391 						uerrhi_reg.word0,
7392 						phba->sli4_hba.ue_mask_lo,
7393 						phba->sli4_hba.ue_mask_hi);
7394 				port_error = -ENODEV;
7395 			}
7396 			break;
7397 		case LPFC_SLI_INTF_IF_TYPE_2:
7398 		case LPFC_SLI_INTF_IF_TYPE_6:
7399 			/* Final checks.  The port status should be clean. */
7400 			if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7401 				&reg_data.word0) ||
7402 				(bf_get(lpfc_sliport_status_err, &reg_data) &&
7403 				 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
7404 				phba->work_status[0] =
7405 					readl(phba->sli4_hba.u.if_type2.
7406 					      ERR1regaddr);
7407 				phba->work_status[1] =
7408 					readl(phba->sli4_hba.u.if_type2.
7409 					      ERR2regaddr);
7410 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7411 					"2888 Unrecoverable port error "
7412 					"following POST: port status reg "
7413 					"0x%x, port_smphr reg 0x%x, "
7414 					"error 1=0x%x, error 2=0x%x\n",
7415 					reg_data.word0,
7416 					portsmphr_reg.word0,
7417 					phba->work_status[0],
7418 					phba->work_status[1]);
7419 				port_error = -ENODEV;
7420 			}
7421 			break;
7422 		case LPFC_SLI_INTF_IF_TYPE_1:
7423 		default:
7424 			break;
7425 		}
7426 	}
7427 	return port_error;
7428 }
7429 
7430 /**
7431  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7432  * @phba: pointer to lpfc hba data structure.
7433  * @if_type:  The SLI4 interface type getting configured.
7434  *
7435  * This routine is invoked to set up SLI4 BAR0 PCI config space register
7436  * memory map.
7437  **/
7438 static void
lpfc_sli4_bar0_register_memmap(struct lpfc_hba * phba,uint32_t if_type)7439 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7440 {
7441 	switch (if_type) {
7442 	case LPFC_SLI_INTF_IF_TYPE_0:
7443 		phba->sli4_hba.u.if_type0.UERRLOregaddr =
7444 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7445 		phba->sli4_hba.u.if_type0.UERRHIregaddr =
7446 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7447 		phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7448 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7449 		phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7450 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7451 		phba->sli4_hba.SLIINTFregaddr =
7452 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7453 		break;
7454 	case LPFC_SLI_INTF_IF_TYPE_2:
7455 		phba->sli4_hba.u.if_type2.EQDregaddr =
7456 			phba->sli4_hba.conf_regs_memmap_p +
7457 						LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7458 		phba->sli4_hba.u.if_type2.ERR1regaddr =
7459 			phba->sli4_hba.conf_regs_memmap_p +
7460 						LPFC_CTL_PORT_ER1_OFFSET;
7461 		phba->sli4_hba.u.if_type2.ERR2regaddr =
7462 			phba->sli4_hba.conf_regs_memmap_p +
7463 						LPFC_CTL_PORT_ER2_OFFSET;
7464 		phba->sli4_hba.u.if_type2.CTRLregaddr =
7465 			phba->sli4_hba.conf_regs_memmap_p +
7466 						LPFC_CTL_PORT_CTL_OFFSET;
7467 		phba->sli4_hba.u.if_type2.STATUSregaddr =
7468 			phba->sli4_hba.conf_regs_memmap_p +
7469 						LPFC_CTL_PORT_STA_OFFSET;
7470 		phba->sli4_hba.SLIINTFregaddr =
7471 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7472 		phba->sli4_hba.PSMPHRregaddr =
7473 			phba->sli4_hba.conf_regs_memmap_p +
7474 						LPFC_CTL_PORT_SEM_OFFSET;
7475 		phba->sli4_hba.RQDBregaddr =
7476 			phba->sli4_hba.conf_regs_memmap_p +
7477 						LPFC_ULP0_RQ_DOORBELL;
7478 		phba->sli4_hba.WQDBregaddr =
7479 			phba->sli4_hba.conf_regs_memmap_p +
7480 						LPFC_ULP0_WQ_DOORBELL;
7481 		phba->sli4_hba.CQDBregaddr =
7482 			phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7483 		phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
7484 		phba->sli4_hba.MQDBregaddr =
7485 			phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7486 		phba->sli4_hba.BMBXregaddr =
7487 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7488 		break;
7489 	case LPFC_SLI_INTF_IF_TYPE_6:
7490 		phba->sli4_hba.u.if_type2.EQDregaddr =
7491 			phba->sli4_hba.conf_regs_memmap_p +
7492 						LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7493 		phba->sli4_hba.u.if_type2.ERR1regaddr =
7494 			phba->sli4_hba.conf_regs_memmap_p +
7495 						LPFC_CTL_PORT_ER1_OFFSET;
7496 		phba->sli4_hba.u.if_type2.ERR2regaddr =
7497 			phba->sli4_hba.conf_regs_memmap_p +
7498 						LPFC_CTL_PORT_ER2_OFFSET;
7499 		phba->sli4_hba.u.if_type2.CTRLregaddr =
7500 			phba->sli4_hba.conf_regs_memmap_p +
7501 						LPFC_CTL_PORT_CTL_OFFSET;
7502 		phba->sli4_hba.u.if_type2.STATUSregaddr =
7503 			phba->sli4_hba.conf_regs_memmap_p +
7504 						LPFC_CTL_PORT_STA_OFFSET;
7505 		phba->sli4_hba.PSMPHRregaddr =
7506 			phba->sli4_hba.conf_regs_memmap_p +
7507 						LPFC_CTL_PORT_SEM_OFFSET;
7508 		phba->sli4_hba.BMBXregaddr =
7509 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7510 		break;
7511 	case LPFC_SLI_INTF_IF_TYPE_1:
7512 	default:
7513 		dev_printk(KERN_ERR, &phba->pcidev->dev,
7514 			   "FATAL - unsupported SLI4 interface type - %d\n",
7515 			   if_type);
7516 		break;
7517 	}
7518 }
7519 
7520 /**
7521  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7522  * @phba: pointer to lpfc hba data structure.
7523  *
7524  * This routine is invoked to set up SLI4 BAR1 register memory map.
7525  **/
7526 static void
lpfc_sli4_bar1_register_memmap(struct lpfc_hba * phba,uint32_t if_type)7527 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7528 {
7529 	switch (if_type) {
7530 	case LPFC_SLI_INTF_IF_TYPE_0:
7531 		phba->sli4_hba.PSMPHRregaddr =
7532 			phba->sli4_hba.ctrl_regs_memmap_p +
7533 			LPFC_SLIPORT_IF0_SMPHR;
7534 		phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7535 			LPFC_HST_ISR0;
7536 		phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7537 			LPFC_HST_IMR0;
7538 		phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7539 			LPFC_HST_ISCR0;
7540 		break;
7541 	case LPFC_SLI_INTF_IF_TYPE_6:
7542 		phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7543 			LPFC_IF6_RQ_DOORBELL;
7544 		phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7545 			LPFC_IF6_WQ_DOORBELL;
7546 		phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7547 			LPFC_IF6_CQ_DOORBELL;
7548 		phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7549 			LPFC_IF6_EQ_DOORBELL;
7550 		phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7551 			LPFC_IF6_MQ_DOORBELL;
7552 		break;
7553 	case LPFC_SLI_INTF_IF_TYPE_2:
7554 	case LPFC_SLI_INTF_IF_TYPE_1:
7555 	default:
7556 		dev_err(&phba->pcidev->dev,
7557 			   "FATAL - unsupported SLI4 interface type - %d\n",
7558 			   if_type);
7559 		break;
7560 	}
7561 }
7562 
7563 /**
7564  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7565  * @phba: pointer to lpfc hba data structure.
7566  * @vf: virtual function number
7567  *
7568  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7569  * based on the given viftual function number, @vf.
7570  *
7571  * Return 0 if successful, otherwise -ENODEV.
7572  **/
7573 static int
lpfc_sli4_bar2_register_memmap(struct lpfc_hba * phba,uint32_t vf)7574 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7575 {
7576 	if (vf > LPFC_VIR_FUNC_MAX)
7577 		return -ENODEV;
7578 
7579 	phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7580 				vf * LPFC_VFR_PAGE_SIZE +
7581 					LPFC_ULP0_RQ_DOORBELL);
7582 	phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7583 				vf * LPFC_VFR_PAGE_SIZE +
7584 					LPFC_ULP0_WQ_DOORBELL);
7585 	phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7586 				vf * LPFC_VFR_PAGE_SIZE +
7587 					LPFC_EQCQ_DOORBELL);
7588 	phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
7589 	phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7590 				vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7591 	phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7592 				vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7593 	return 0;
7594 }
7595 
7596 /**
7597  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7598  * @phba: pointer to lpfc hba data structure.
7599  *
7600  * This routine is invoked to create the bootstrap mailbox
7601  * region consistent with the SLI-4 interface spec.  This
7602  * routine allocates all memory necessary to communicate
7603  * mailbox commands to the port and sets up all alignment
7604  * needs.  No locks are expected to be held when calling
7605  * this routine.
7606  *
7607  * Return codes
7608  * 	0 - successful
7609  * 	-ENOMEM - could not allocated memory.
7610  **/
7611 static int
lpfc_create_bootstrap_mbox(struct lpfc_hba * phba)7612 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7613 {
7614 	uint32_t bmbx_size;
7615 	struct lpfc_dmabuf *dmabuf;
7616 	struct dma_address *dma_address;
7617 	uint32_t pa_addr;
7618 	uint64_t phys_addr;
7619 
7620 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7621 	if (!dmabuf)
7622 		return -ENOMEM;
7623 
7624 	/*
7625 	 * The bootstrap mailbox region is comprised of 2 parts
7626 	 * plus an alignment restriction of 16 bytes.
7627 	 */
7628 	bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
7629 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7630 					   &dmabuf->phys, GFP_KERNEL);
7631 	if (!dmabuf->virt) {
7632 		kfree(dmabuf);
7633 		return -ENOMEM;
7634 	}
7635 
7636 	/*
7637 	 * Initialize the bootstrap mailbox pointers now so that the register
7638 	 * operations are simple later.  The mailbox dma address is required
7639 	 * to be 16-byte aligned.  Also align the virtual memory as each
7640 	 * maibox is copied into the bmbx mailbox region before issuing the
7641 	 * command to the port.
7642 	 */
7643 	phba->sli4_hba.bmbx.dmabuf = dmabuf;
7644 	phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7645 
7646 	phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7647 					      LPFC_ALIGN_16_BYTE);
7648 	phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7649 					      LPFC_ALIGN_16_BYTE);
7650 
7651 	/*
7652 	 * Set the high and low physical addresses now.  The SLI4 alignment
7653 	 * requirement is 16 bytes and the mailbox is posted to the port
7654 	 * as two 30-bit addresses.  The other data is a bit marking whether
7655 	 * the 30-bit address is the high or low address.
7656 	 * Upcast bmbx aphys to 64bits so shift instruction compiles
7657 	 * clean on 32 bit machines.
7658 	 */
7659 	dma_address = &phba->sli4_hba.bmbx.dma_address;
7660 	phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7661 	pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7662 	dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7663 					   LPFC_BMBX_BIT1_ADDR_HI);
7664 
7665 	pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7666 	dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7667 					   LPFC_BMBX_BIT1_ADDR_LO);
7668 	return 0;
7669 }
7670 
7671 /**
7672  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7673  * @phba: pointer to lpfc hba data structure.
7674  *
7675  * This routine is invoked to teardown the bootstrap mailbox
7676  * region and release all host resources. This routine requires
7677  * the caller to ensure all mailbox commands recovered, no
7678  * additional mailbox comands are sent, and interrupts are disabled
7679  * before calling this routine.
7680  *
7681  **/
7682 static void
lpfc_destroy_bootstrap_mbox(struct lpfc_hba * phba)7683 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7684 {
7685 	dma_free_coherent(&phba->pcidev->dev,
7686 			  phba->sli4_hba.bmbx.bmbx_size,
7687 			  phba->sli4_hba.bmbx.dmabuf->virt,
7688 			  phba->sli4_hba.bmbx.dmabuf->phys);
7689 
7690 	kfree(phba->sli4_hba.bmbx.dmabuf);
7691 	memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7692 }
7693 
7694 /**
7695  * lpfc_sli4_read_config - Get the config parameters.
7696  * @phba: pointer to lpfc hba data structure.
7697  *
7698  * This routine is invoked to read the configuration parameters from the HBA.
7699  * The configuration parameters are used to set the base and maximum values
7700  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7701  * allocation for the port.
7702  *
7703  * Return codes
7704  * 	0 - successful
7705  * 	-ENOMEM - No available memory
7706  *      -EIO - The mailbox failed to complete successfully.
7707  **/
7708 int
lpfc_sli4_read_config(struct lpfc_hba * phba)7709 lpfc_sli4_read_config(struct lpfc_hba *phba)
7710 {
7711 	LPFC_MBOXQ_t *pmb;
7712 	struct lpfc_mbx_read_config *rd_config;
7713 	union  lpfc_sli4_cfg_shdr *shdr;
7714 	uint32_t shdr_status, shdr_add_status;
7715 	struct lpfc_mbx_get_func_cfg *get_func_cfg;
7716 	struct lpfc_rsrc_desc_fcfcoe *desc;
7717 	char *pdesc_0;
7718 	uint16_t forced_link_speed;
7719 	uint32_t if_type;
7720 	int length, i, rc = 0, rc2;
7721 
7722 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7723 	if (!pmb) {
7724 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7725 				"2011 Unable to allocate memory for issuing "
7726 				"SLI_CONFIG_SPECIAL mailbox command\n");
7727 		return -ENOMEM;
7728 	}
7729 
7730 	lpfc_read_config(phba, pmb);
7731 
7732 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7733 	if (rc != MBX_SUCCESS) {
7734 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7735 			"2012 Mailbox failed , mbxCmd x%x "
7736 			"READ_CONFIG, mbxStatus x%x\n",
7737 			bf_get(lpfc_mqe_command, &pmb->u.mqe),
7738 			bf_get(lpfc_mqe_status, &pmb->u.mqe));
7739 		rc = -EIO;
7740 	} else {
7741 		rd_config = &pmb->u.mqe.un.rd_config;
7742 		if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7743 			phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7744 			phba->sli4_hba.lnk_info.lnk_tp =
7745 				bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7746 			phba->sli4_hba.lnk_info.lnk_no =
7747 				bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7748 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7749 					"3081 lnk_type:%d, lnk_numb:%d\n",
7750 					phba->sli4_hba.lnk_info.lnk_tp,
7751 					phba->sli4_hba.lnk_info.lnk_no);
7752 		} else
7753 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7754 					"3082 Mailbox (x%x) returned ldv:x0\n",
7755 					bf_get(lpfc_mqe_command, &pmb->u.mqe));
7756 		if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
7757 			phba->bbcredit_support = 1;
7758 			phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
7759 		}
7760 
7761 		phba->sli4_hba.extents_in_use =
7762 			bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
7763 		phba->sli4_hba.max_cfg_param.max_xri =
7764 			bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7765 		phba->sli4_hba.max_cfg_param.xri_base =
7766 			bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7767 		phba->sli4_hba.max_cfg_param.max_vpi =
7768 			bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7769 		/* Limit the max we support */
7770 		if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
7771 			phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
7772 		phba->sli4_hba.max_cfg_param.vpi_base =
7773 			bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7774 		phba->sli4_hba.max_cfg_param.max_rpi =
7775 			bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7776 		phba->sli4_hba.max_cfg_param.rpi_base =
7777 			bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7778 		phba->sli4_hba.max_cfg_param.max_vfi =
7779 			bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7780 		phba->sli4_hba.max_cfg_param.vfi_base =
7781 			bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7782 		phba->sli4_hba.max_cfg_param.max_fcfi =
7783 			bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
7784 		phba->sli4_hba.max_cfg_param.max_eq =
7785 			bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7786 		phba->sli4_hba.max_cfg_param.max_rq =
7787 			bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7788 		phba->sli4_hba.max_cfg_param.max_wq =
7789 			bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7790 		phba->sli4_hba.max_cfg_param.max_cq =
7791 			bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7792 		phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7793 		phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7794 		phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7795 		phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
7796 		phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7797 				(phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
7798 		phba->max_vports = phba->max_vpi;
7799 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7800 				"2003 cfg params Extents? %d "
7801 				"XRI(B:%d M:%d), "
7802 				"VPI(B:%d M:%d) "
7803 				"VFI(B:%d M:%d) "
7804 				"RPI(B:%d M:%d) "
7805 				"FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
7806 				phba->sli4_hba.extents_in_use,
7807 				phba->sli4_hba.max_cfg_param.xri_base,
7808 				phba->sli4_hba.max_cfg_param.max_xri,
7809 				phba->sli4_hba.max_cfg_param.vpi_base,
7810 				phba->sli4_hba.max_cfg_param.max_vpi,
7811 				phba->sli4_hba.max_cfg_param.vfi_base,
7812 				phba->sli4_hba.max_cfg_param.max_vfi,
7813 				phba->sli4_hba.max_cfg_param.rpi_base,
7814 				phba->sli4_hba.max_cfg_param.max_rpi,
7815 				phba->sli4_hba.max_cfg_param.max_fcfi,
7816 				phba->sli4_hba.max_cfg_param.max_eq,
7817 				phba->sli4_hba.max_cfg_param.max_cq,
7818 				phba->sli4_hba.max_cfg_param.max_wq,
7819 				phba->sli4_hba.max_cfg_param.max_rq);
7820 
7821 		/*
7822 		 * Calculate NVME queue resources based on how
7823 		 * many WQ/CQs are available.
7824 		 */
7825 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
7826 			length = phba->sli4_hba.max_cfg_param.max_wq;
7827 			if (phba->sli4_hba.max_cfg_param.max_cq <
7828 			    phba->sli4_hba.max_cfg_param.max_wq)
7829 				length = phba->sli4_hba.max_cfg_param.max_cq;
7830 
7831 			/*
7832 			 * Whats left after this can go toward NVME.
7833 			 * The minus 6 accounts for ELS, NVME LS, MBOX
7834 			 * fof plus a couple extra. When configured for
7835 			 * NVMET, FCP io channel WQs are not created.
7836 			 */
7837 			length -= 6;
7838 			if (!phba->nvmet_support)
7839 				length -= phba->cfg_fcp_io_channel;
7840 
7841 			if (phba->cfg_nvme_io_channel > length) {
7842 				lpfc_printf_log(
7843 					phba, KERN_ERR, LOG_SLI,
7844 					"2005 Reducing NVME IO channel to %d: "
7845 					"WQ %d CQ %d NVMEIO %d FCPIO %d\n",
7846 					length,
7847 					phba->sli4_hba.max_cfg_param.max_wq,
7848 					phba->sli4_hba.max_cfg_param.max_cq,
7849 					phba->cfg_nvme_io_channel,
7850 					phba->cfg_fcp_io_channel);
7851 
7852 				phba->cfg_nvme_io_channel = length;
7853 			}
7854 		}
7855 	}
7856 
7857 	if (rc)
7858 		goto read_cfg_out;
7859 
7860 	/* Update link speed if forced link speed is supported */
7861 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7862 	if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
7863 		forced_link_speed =
7864 			bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7865 		if (forced_link_speed) {
7866 			phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7867 
7868 			switch (forced_link_speed) {
7869 			case LINK_SPEED_1G:
7870 				phba->cfg_link_speed =
7871 					LPFC_USER_LINK_SPEED_1G;
7872 				break;
7873 			case LINK_SPEED_2G:
7874 				phba->cfg_link_speed =
7875 					LPFC_USER_LINK_SPEED_2G;
7876 				break;
7877 			case LINK_SPEED_4G:
7878 				phba->cfg_link_speed =
7879 					LPFC_USER_LINK_SPEED_4G;
7880 				break;
7881 			case LINK_SPEED_8G:
7882 				phba->cfg_link_speed =
7883 					LPFC_USER_LINK_SPEED_8G;
7884 				break;
7885 			case LINK_SPEED_10G:
7886 				phba->cfg_link_speed =
7887 					LPFC_USER_LINK_SPEED_10G;
7888 				break;
7889 			case LINK_SPEED_16G:
7890 				phba->cfg_link_speed =
7891 					LPFC_USER_LINK_SPEED_16G;
7892 				break;
7893 			case LINK_SPEED_32G:
7894 				phba->cfg_link_speed =
7895 					LPFC_USER_LINK_SPEED_32G;
7896 				break;
7897 			case LINK_SPEED_64G:
7898 				phba->cfg_link_speed =
7899 					LPFC_USER_LINK_SPEED_64G;
7900 				break;
7901 			case 0xffff:
7902 				phba->cfg_link_speed =
7903 					LPFC_USER_LINK_SPEED_AUTO;
7904 				break;
7905 			default:
7906 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7907 						"0047 Unrecognized link "
7908 						"speed : %d\n",
7909 						forced_link_speed);
7910 				phba->cfg_link_speed =
7911 					LPFC_USER_LINK_SPEED_AUTO;
7912 			}
7913 		}
7914 	}
7915 
7916 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
7917 	length = phba->sli4_hba.max_cfg_param.max_xri -
7918 			lpfc_sli4_get_els_iocb_cnt(phba);
7919 	if (phba->cfg_hba_queue_depth > length) {
7920 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7921 				"3361 HBA queue depth changed from %d to %d\n",
7922 				phba->cfg_hba_queue_depth, length);
7923 		phba->cfg_hba_queue_depth = length;
7924 	}
7925 
7926 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7927 	    LPFC_SLI_INTF_IF_TYPE_2)
7928 		goto read_cfg_out;
7929 
7930 	/* get the pf# and vf# for SLI4 if_type 2 port */
7931 	length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7932 		  sizeof(struct lpfc_sli4_cfg_mhdr));
7933 	lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7934 			 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7935 			 length, LPFC_SLI4_MBX_EMBED);
7936 
7937 	rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7938 	shdr = (union lpfc_sli4_cfg_shdr *)
7939 				&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7940 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7941 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7942 	if (rc2 || shdr_status || shdr_add_status) {
7943 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7944 				"3026 Mailbox failed , mbxCmd x%x "
7945 				"GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7946 				bf_get(lpfc_mqe_command, &pmb->u.mqe),
7947 				bf_get(lpfc_mqe_status, &pmb->u.mqe));
7948 		goto read_cfg_out;
7949 	}
7950 
7951 	/* search for fc_fcoe resrouce descriptor */
7952 	get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
7953 
7954 	pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7955 	desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7956 	length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7957 	if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7958 		length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7959 	else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7960 		goto read_cfg_out;
7961 
7962 	for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
7963 		desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
7964 		if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
7965 		    bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
7966 			phba->sli4_hba.iov.pf_number =
7967 				bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7968 			phba->sli4_hba.iov.vf_number =
7969 				bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7970 			break;
7971 		}
7972 	}
7973 
7974 	if (i < LPFC_RSRC_DESC_MAX_NUM)
7975 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7976 				"3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7977 				"vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7978 				phba->sli4_hba.iov.vf_number);
7979 	else
7980 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7981 				"3028 GET_FUNCTION_CONFIG: failed to find "
7982 				"Resrouce Descriptor:x%x\n",
7983 				LPFC_RSRC_DESC_TYPE_FCFCOE);
7984 
7985 read_cfg_out:
7986 	mempool_free(pmb, phba->mbox_mem_pool);
7987 	return rc;
7988 }
7989 
7990 /**
7991  * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
7992  * @phba: pointer to lpfc hba data structure.
7993  *
7994  * This routine is invoked to setup the port-side endian order when
7995  * the port if_type is 0.  This routine has no function for other
7996  * if_types.
7997  *
7998  * Return codes
7999  * 	0 - successful
8000  * 	-ENOMEM - No available memory
8001  *      -EIO - The mailbox failed to complete successfully.
8002  **/
8003 static int
lpfc_setup_endian_order(struct lpfc_hba * phba)8004 lpfc_setup_endian_order(struct lpfc_hba *phba)
8005 {
8006 	LPFC_MBOXQ_t *mboxq;
8007 	uint32_t if_type, rc = 0;
8008 	uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8009 				      HOST_ENDIAN_HIGH_WORD1};
8010 
8011 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8012 	switch (if_type) {
8013 	case LPFC_SLI_INTF_IF_TYPE_0:
8014 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8015 						       GFP_KERNEL);
8016 		if (!mboxq) {
8017 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8018 					"0492 Unable to allocate memory for "
8019 					"issuing SLI_CONFIG_SPECIAL mailbox "
8020 					"command\n");
8021 			return -ENOMEM;
8022 		}
8023 
8024 		/*
8025 		 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8026 		 * two words to contain special data values and no other data.
8027 		 */
8028 		memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8029 		memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8030 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8031 		if (rc != MBX_SUCCESS) {
8032 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8033 					"0493 SLI_CONFIG_SPECIAL mailbox "
8034 					"failed with status x%x\n",
8035 					rc);
8036 			rc = -EIO;
8037 		}
8038 		mempool_free(mboxq, phba->mbox_mem_pool);
8039 		break;
8040 	case LPFC_SLI_INTF_IF_TYPE_6:
8041 	case LPFC_SLI_INTF_IF_TYPE_2:
8042 	case LPFC_SLI_INTF_IF_TYPE_1:
8043 	default:
8044 		break;
8045 	}
8046 	return rc;
8047 }
8048 
8049 /**
8050  * lpfc_sli4_queue_verify - Verify and update EQ counts
8051  * @phba: pointer to lpfc hba data structure.
8052  *
8053  * This routine is invoked to check the user settable queue counts for EQs.
8054  * After this routine is called the counts will be set to valid values that
8055  * adhere to the constraints of the system's interrupt vectors and the port's
8056  * queue resources.
8057  *
8058  * Return codes
8059  *      0 - successful
8060  *      -ENOMEM - No available memory
8061  **/
8062 static int
lpfc_sli4_queue_verify(struct lpfc_hba * phba)8063 lpfc_sli4_queue_verify(struct lpfc_hba *phba)
8064 {
8065 	int io_channel;
8066 	int fof_vectors = phba->cfg_fof ? 1 : 0;
8067 
8068 	/*
8069 	 * Sanity check for configured queue parameters against the run-time
8070 	 * device parameters
8071 	 */
8072 
8073 	/* Sanity check on HBA EQ parameters */
8074 	io_channel = phba->io_channel_irqs;
8075 
8076 	if (phba->sli4_hba.num_online_cpu < io_channel) {
8077 		lpfc_printf_log(phba,
8078 				KERN_ERR, LOG_INIT,
8079 				"3188 Reducing IO channels to match number of "
8080 				"online CPUs: from %d to %d\n",
8081 				io_channel, phba->sli4_hba.num_online_cpu);
8082 		io_channel = phba->sli4_hba.num_online_cpu;
8083 	}
8084 
8085 	if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
8086 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8087 				"2575 Reducing IO channels to match number of "
8088 				"available EQs: from %d to %d\n",
8089 				io_channel,
8090 				phba->sli4_hba.max_cfg_param.max_eq);
8091 		io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
8092 	}
8093 
8094 	/* The actual number of FCP / NVME event queues adopted */
8095 	if (io_channel != phba->io_channel_irqs)
8096 		phba->io_channel_irqs = io_channel;
8097 	if (phba->cfg_fcp_io_channel > io_channel)
8098 		phba->cfg_fcp_io_channel = io_channel;
8099 	if (phba->cfg_nvme_io_channel > io_channel)
8100 		phba->cfg_nvme_io_channel = io_channel;
8101 	if (phba->nvmet_support) {
8102 		if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
8103 			phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
8104 	}
8105 	if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8106 		phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
8107 
8108 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8109 			"2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
8110 			phba->io_channel_irqs, phba->cfg_fcp_io_channel,
8111 			phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
8112 
8113 	/* Get EQ depth from module parameter, fake the default for now */
8114 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8115 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8116 
8117 	/* Get CQ depth from module parameter, fake the default for now */
8118 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8119 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8120 	return 0;
8121 }
8122 
8123 static int
lpfc_alloc_nvme_wq_cq(struct lpfc_hba * phba,int wqidx)8124 lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
8125 {
8126 	struct lpfc_queue *qdesc;
8127 
8128 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8129 				      phba->sli4_hba.cq_esize,
8130 				      LPFC_CQE_EXP_COUNT);
8131 	if (!qdesc) {
8132 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8133 				"0508 Failed allocate fast-path NVME CQ (%d)\n",
8134 				wqidx);
8135 		return 1;
8136 	}
8137 	qdesc->qe_valid = 1;
8138 	phba->sli4_hba.nvme_cq[wqidx] = qdesc;
8139 
8140 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8141 				      LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT);
8142 	if (!qdesc) {
8143 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8144 				"0509 Failed allocate fast-path NVME WQ (%d)\n",
8145 				wqidx);
8146 		return 1;
8147 	}
8148 	phba->sli4_hba.nvme_wq[wqidx] = qdesc;
8149 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8150 	return 0;
8151 }
8152 
8153 static int
lpfc_alloc_fcp_wq_cq(struct lpfc_hba * phba,int wqidx)8154 lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8155 {
8156 	struct lpfc_queue *qdesc;
8157 	uint32_t wqesize;
8158 
8159 	/* Create Fast Path FCP CQs */
8160 	if (phba->enab_exp_wqcq_pages)
8161 		/* Increase the CQ size when WQEs contain an embedded cdb */
8162 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8163 					      phba->sli4_hba.cq_esize,
8164 					      LPFC_CQE_EXP_COUNT);
8165 
8166 	else
8167 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8168 					      phba->sli4_hba.cq_esize,
8169 					      phba->sli4_hba.cq_ecount);
8170 	if (!qdesc) {
8171 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8172 			"0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8173 		return 1;
8174 	}
8175 	qdesc->qe_valid = 1;
8176 	phba->sli4_hba.fcp_cq[wqidx] = qdesc;
8177 
8178 	/* Create Fast Path FCP WQs */
8179 	if (phba->enab_exp_wqcq_pages) {
8180 		/* Increase the WQ size when WQEs contain an embedded cdb */
8181 		wqesize = (phba->fcp_embed_io) ?
8182 			LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
8183 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8184 					      wqesize,
8185 					      LPFC_WQE_EXP_COUNT);
8186 	} else
8187 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8188 					      phba->sli4_hba.wq_esize,
8189 					      phba->sli4_hba.wq_ecount);
8190 
8191 	if (!qdesc) {
8192 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8193 				"0503 Failed allocate fast-path FCP WQ (%d)\n",
8194 				wqidx);
8195 		return 1;
8196 	}
8197 	phba->sli4_hba.fcp_wq[wqidx] = qdesc;
8198 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8199 	return 0;
8200 }
8201 
8202 /**
8203  * lpfc_sli4_queue_create - Create all the SLI4 queues
8204  * @phba: pointer to lpfc hba data structure.
8205  *
8206  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8207  * operation. For each SLI4 queue type, the parameters such as queue entry
8208  * count (queue depth) shall be taken from the module parameter. For now,
8209  * we just use some constant number as place holder.
8210  *
8211  * Return codes
8212  *      0 - successful
8213  *      -ENOMEM - No availble memory
8214  *      -EIO - The mailbox failed to complete successfully.
8215  **/
8216 int
lpfc_sli4_queue_create(struct lpfc_hba * phba)8217 lpfc_sli4_queue_create(struct lpfc_hba *phba)
8218 {
8219 	struct lpfc_queue *qdesc;
8220 	int idx, io_channel;
8221 
8222 	/*
8223 	 * Create HBA Record arrays.
8224 	 * Both NVME and FCP will share that same vectors / EQs
8225 	 */
8226 	io_channel = phba->io_channel_irqs;
8227 	if (!io_channel)
8228 		return -ERANGE;
8229 
8230 	phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8231 	phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8232 	phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8233 	phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8234 	phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8235 	phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
8236 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8237 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8238 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8239 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8240 
8241 	phba->sli4_hba.hba_eq =  kcalloc(io_channel,
8242 					sizeof(struct lpfc_queue *),
8243 					GFP_KERNEL);
8244 	if (!phba->sli4_hba.hba_eq) {
8245 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8246 			"2576 Failed allocate memory for "
8247 			"fast-path EQ record array\n");
8248 		goto out_error;
8249 	}
8250 
8251 	if (phba->cfg_fcp_io_channel) {
8252 		phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8253 						sizeof(struct lpfc_queue *),
8254 						GFP_KERNEL);
8255 		if (!phba->sli4_hba.fcp_cq) {
8256 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8257 					"2577 Failed allocate memory for "
8258 					"fast-path CQ record array\n");
8259 			goto out_error;
8260 		}
8261 		phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8262 						sizeof(struct lpfc_queue *),
8263 						GFP_KERNEL);
8264 		if (!phba->sli4_hba.fcp_wq) {
8265 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8266 					"2578 Failed allocate memory for "
8267 					"fast-path FCP WQ record array\n");
8268 			goto out_error;
8269 		}
8270 		/*
8271 		 * Since the first EQ can have multiple CQs associated with it,
8272 		 * this array is used to quickly see if we have a FCP fast-path
8273 		 * CQ match.
8274 		 */
8275 		phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8276 							sizeof(uint16_t),
8277 							GFP_KERNEL);
8278 		if (!phba->sli4_hba.fcp_cq_map) {
8279 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8280 					"2545 Failed allocate memory for "
8281 					"fast-path CQ map\n");
8282 			goto out_error;
8283 		}
8284 	}
8285 
8286 	if (phba->cfg_nvme_io_channel) {
8287 		phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8288 						sizeof(struct lpfc_queue *),
8289 						GFP_KERNEL);
8290 		if (!phba->sli4_hba.nvme_cq) {
8291 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8292 					"6077 Failed allocate memory for "
8293 					"fast-path CQ record array\n");
8294 			goto out_error;
8295 		}
8296 
8297 		phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8298 						sizeof(struct lpfc_queue *),
8299 						GFP_KERNEL);
8300 		if (!phba->sli4_hba.nvme_wq) {
8301 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8302 					"2581 Failed allocate memory for "
8303 					"fast-path NVME WQ record array\n");
8304 			goto out_error;
8305 		}
8306 
8307 		/*
8308 		 * Since the first EQ can have multiple CQs associated with it,
8309 		 * this array is used to quickly see if we have a NVME fast-path
8310 		 * CQ match.
8311 		 */
8312 		phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8313 							sizeof(uint16_t),
8314 							GFP_KERNEL);
8315 		if (!phba->sli4_hba.nvme_cq_map) {
8316 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8317 					"6078 Failed allocate memory for "
8318 					"fast-path CQ map\n");
8319 			goto out_error;
8320 		}
8321 
8322 		if (phba->nvmet_support) {
8323 			phba->sli4_hba.nvmet_cqset = kcalloc(
8324 					phba->cfg_nvmet_mrq,
8325 					sizeof(struct lpfc_queue *),
8326 					GFP_KERNEL);
8327 			if (!phba->sli4_hba.nvmet_cqset) {
8328 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8329 					"3121 Fail allocate memory for "
8330 					"fast-path CQ set array\n");
8331 				goto out_error;
8332 			}
8333 			phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8334 					phba->cfg_nvmet_mrq,
8335 					sizeof(struct lpfc_queue *),
8336 					GFP_KERNEL);
8337 			if (!phba->sli4_hba.nvmet_mrq_hdr) {
8338 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8339 					"3122 Fail allocate memory for "
8340 					"fast-path RQ set hdr array\n");
8341 				goto out_error;
8342 			}
8343 			phba->sli4_hba.nvmet_mrq_data = kcalloc(
8344 					phba->cfg_nvmet_mrq,
8345 					sizeof(struct lpfc_queue *),
8346 					GFP_KERNEL);
8347 			if (!phba->sli4_hba.nvmet_mrq_data) {
8348 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8349 					"3124 Fail allocate memory for "
8350 					"fast-path RQ set data array\n");
8351 				goto out_error;
8352 			}
8353 		}
8354 	}
8355 
8356 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8357 
8358 	/* Create HBA Event Queues (EQs) */
8359 	for (idx = 0; idx < io_channel; idx++) {
8360 		/* Create EQs */
8361 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8362 					      phba->sli4_hba.eq_esize,
8363 					      phba->sli4_hba.eq_ecount);
8364 		if (!qdesc) {
8365 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8366 					"0497 Failed allocate EQ (%d)\n", idx);
8367 			goto out_error;
8368 		}
8369 		qdesc->qe_valid = 1;
8370 		phba->sli4_hba.hba_eq[idx] = qdesc;
8371 	}
8372 
8373 	/* FCP and NVME io channels are not required to be balanced */
8374 
8375 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8376 		if (lpfc_alloc_fcp_wq_cq(phba, idx))
8377 			goto out_error;
8378 
8379 	for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8380 		if (lpfc_alloc_nvme_wq_cq(phba, idx))
8381 			goto out_error;
8382 
8383 	if (phba->nvmet_support) {
8384 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8385 			qdesc = lpfc_sli4_queue_alloc(phba,
8386 						      LPFC_DEFAULT_PAGE_SIZE,
8387 						      phba->sli4_hba.cq_esize,
8388 						      phba->sli4_hba.cq_ecount);
8389 			if (!qdesc) {
8390 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8391 					"3142 Failed allocate NVME "
8392 					"CQ Set (%d)\n", idx);
8393 				goto out_error;
8394 			}
8395 			qdesc->qe_valid = 1;
8396 			phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8397 		}
8398 	}
8399 
8400 	/*
8401 	 * Create Slow Path Completion Queues (CQs)
8402 	 */
8403 
8404 	/* Create slow-path Mailbox Command Complete Queue */
8405 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8406 				      phba->sli4_hba.cq_esize,
8407 				      phba->sli4_hba.cq_ecount);
8408 	if (!qdesc) {
8409 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8410 				"0500 Failed allocate slow-path mailbox CQ\n");
8411 		goto out_error;
8412 	}
8413 	qdesc->qe_valid = 1;
8414 	phba->sli4_hba.mbx_cq = qdesc;
8415 
8416 	/* Create slow-path ELS Complete Queue */
8417 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8418 				      phba->sli4_hba.cq_esize,
8419 				      phba->sli4_hba.cq_ecount);
8420 	if (!qdesc) {
8421 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8422 				"0501 Failed allocate slow-path ELS CQ\n");
8423 		goto out_error;
8424 	}
8425 	qdesc->qe_valid = 1;
8426 	phba->sli4_hba.els_cq = qdesc;
8427 
8428 
8429 	/*
8430 	 * Create Slow Path Work Queues (WQs)
8431 	 */
8432 
8433 	/* Create Mailbox Command Queue */
8434 
8435 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8436 				      phba->sli4_hba.mq_esize,
8437 				      phba->sli4_hba.mq_ecount);
8438 	if (!qdesc) {
8439 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8440 				"0505 Failed allocate slow-path MQ\n");
8441 		goto out_error;
8442 	}
8443 	phba->sli4_hba.mbx_wq = qdesc;
8444 
8445 	/*
8446 	 * Create ELS Work Queues
8447 	 */
8448 
8449 	/* Create slow-path ELS Work Queue */
8450 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8451 				      phba->sli4_hba.wq_esize,
8452 				      phba->sli4_hba.wq_ecount);
8453 	if (!qdesc) {
8454 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8455 				"0504 Failed allocate slow-path ELS WQ\n");
8456 		goto out_error;
8457 	}
8458 	phba->sli4_hba.els_wq = qdesc;
8459 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8460 
8461 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8462 		/* Create NVME LS Complete Queue */
8463 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8464 					      phba->sli4_hba.cq_esize,
8465 					      phba->sli4_hba.cq_ecount);
8466 		if (!qdesc) {
8467 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8468 					"6079 Failed allocate NVME LS CQ\n");
8469 			goto out_error;
8470 		}
8471 		qdesc->qe_valid = 1;
8472 		phba->sli4_hba.nvmels_cq = qdesc;
8473 
8474 		/* Create NVME LS Work Queue */
8475 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8476 					      phba->sli4_hba.wq_esize,
8477 					      phba->sli4_hba.wq_ecount);
8478 		if (!qdesc) {
8479 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8480 					"6080 Failed allocate NVME LS WQ\n");
8481 			goto out_error;
8482 		}
8483 		phba->sli4_hba.nvmels_wq = qdesc;
8484 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8485 	}
8486 
8487 	/*
8488 	 * Create Receive Queue (RQ)
8489 	 */
8490 
8491 	/* Create Receive Queue for header */
8492 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8493 				      phba->sli4_hba.rq_esize,
8494 				      phba->sli4_hba.rq_ecount);
8495 	if (!qdesc) {
8496 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8497 				"0506 Failed allocate receive HRQ\n");
8498 		goto out_error;
8499 	}
8500 	phba->sli4_hba.hdr_rq = qdesc;
8501 
8502 	/* Create Receive Queue for data */
8503 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8504 				      phba->sli4_hba.rq_esize,
8505 				      phba->sli4_hba.rq_ecount);
8506 	if (!qdesc) {
8507 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8508 				"0507 Failed allocate receive DRQ\n");
8509 		goto out_error;
8510 	}
8511 	phba->sli4_hba.dat_rq = qdesc;
8512 
8513 	if (phba->nvmet_support) {
8514 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8515 			/* Create NVMET Receive Queue for header */
8516 			qdesc = lpfc_sli4_queue_alloc(phba,
8517 						      LPFC_DEFAULT_PAGE_SIZE,
8518 						      phba->sli4_hba.rq_esize,
8519 						      LPFC_NVMET_RQE_DEF_COUNT);
8520 			if (!qdesc) {
8521 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8522 						"3146 Failed allocate "
8523 						"receive HRQ\n");
8524 				goto out_error;
8525 			}
8526 			phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8527 
8528 			/* Only needed for header of RQ pair */
8529 			qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8530 					      GFP_KERNEL);
8531 			if (qdesc->rqbp == NULL) {
8532 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8533 						"6131 Failed allocate "
8534 						"Header RQBP\n");
8535 				goto out_error;
8536 			}
8537 
8538 			/* Put list in known state in case driver load fails. */
8539 			INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8540 
8541 			/* Create NVMET Receive Queue for data */
8542 			qdesc = lpfc_sli4_queue_alloc(phba,
8543 						      LPFC_DEFAULT_PAGE_SIZE,
8544 						      phba->sli4_hba.rq_esize,
8545 						      LPFC_NVMET_RQE_DEF_COUNT);
8546 			if (!qdesc) {
8547 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8548 						"3156 Failed allocate "
8549 						"receive DRQ\n");
8550 				goto out_error;
8551 			}
8552 			phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8553 		}
8554 	}
8555 
8556 	/* Create the Queues needed for Flash Optimized Fabric operations */
8557 	if (phba->cfg_fof)
8558 		lpfc_fof_queue_create(phba);
8559 	return 0;
8560 
8561 out_error:
8562 	lpfc_sli4_queue_destroy(phba);
8563 	return -ENOMEM;
8564 }
8565 
8566 static inline void
__lpfc_sli4_release_queue(struct lpfc_queue ** qp)8567 __lpfc_sli4_release_queue(struct lpfc_queue **qp)
8568 {
8569 	if (*qp != NULL) {
8570 		lpfc_sli4_queue_free(*qp);
8571 		*qp = NULL;
8572 	}
8573 }
8574 
8575 static inline void
lpfc_sli4_release_queues(struct lpfc_queue *** qs,int max)8576 lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8577 {
8578 	int idx;
8579 
8580 	if (*qs == NULL)
8581 		return;
8582 
8583 	for (idx = 0; idx < max; idx++)
8584 		__lpfc_sli4_release_queue(&(*qs)[idx]);
8585 
8586 	kfree(*qs);
8587 	*qs = NULL;
8588 }
8589 
8590 static inline void
lpfc_sli4_release_queue_map(uint16_t ** qmap)8591 lpfc_sli4_release_queue_map(uint16_t **qmap)
8592 {
8593 	if (*qmap != NULL) {
8594 		kfree(*qmap);
8595 		*qmap = NULL;
8596 	}
8597 }
8598 
8599 /**
8600  * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8601  * @phba: pointer to lpfc hba data structure.
8602  *
8603  * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8604  * operation.
8605  *
8606  * Return codes
8607  *      0 - successful
8608  *      -ENOMEM - No available memory
8609  *      -EIO - The mailbox failed to complete successfully.
8610  **/
8611 void
lpfc_sli4_queue_destroy(struct lpfc_hba * phba)8612 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8613 {
8614 	if (phba->cfg_fof)
8615 		lpfc_fof_queue_destroy(phba);
8616 
8617 	/* Release HBA eqs */
8618 	lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
8619 
8620 	/* Release FCP cqs */
8621 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8622 				 phba->cfg_fcp_io_channel);
8623 
8624 	/* Release FCP wqs */
8625 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8626 				 phba->cfg_fcp_io_channel);
8627 
8628 	/* Release FCP CQ mapping array */
8629 	lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8630 
8631 	/* Release NVME cqs */
8632 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8633 					phba->cfg_nvme_io_channel);
8634 
8635 	/* Release NVME wqs */
8636 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8637 					phba->cfg_nvme_io_channel);
8638 
8639 	/* Release NVME CQ mapping array */
8640 	lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
8641 
8642 	if (phba->nvmet_support) {
8643 		lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8644 					 phba->cfg_nvmet_mrq);
8645 
8646 		lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8647 					 phba->cfg_nvmet_mrq);
8648 		lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8649 					 phba->cfg_nvmet_mrq);
8650 	}
8651 
8652 	/* Release mailbox command work queue */
8653 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
8654 
8655 	/* Release ELS work queue */
8656 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8657 
8658 	/* Release ELS work queue */
8659 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
8660 
8661 	/* Release unsolicited receive queue */
8662 	__lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8663 	__lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
8664 
8665 	/* Release ELS complete queue */
8666 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8667 
8668 	/* Release NVME LS complete queue */
8669 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
8670 
8671 	/* Release mailbox command complete queue */
8672 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8673 
8674 	/* Everything on this list has been freed */
8675 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8676 }
8677 
8678 int
lpfc_free_rq_buffer(struct lpfc_hba * phba,struct lpfc_queue * rq)8679 lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8680 {
8681 	struct lpfc_rqb *rqbp;
8682 	struct lpfc_dmabuf *h_buf;
8683 	struct rqb_dmabuf *rqb_buffer;
8684 
8685 	rqbp = rq->rqbp;
8686 	while (!list_empty(&rqbp->rqb_buffer_list)) {
8687 		list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8688 				 struct lpfc_dmabuf, list);
8689 
8690 		rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8691 		(rqbp->rqb_free_buffer)(phba, rqb_buffer);
8692 		rqbp->buffer_count--;
8693 	}
8694 	return 1;
8695 }
8696 
8697 static int
lpfc_create_wq_cq(struct lpfc_hba * phba,struct lpfc_queue * eq,struct lpfc_queue * cq,struct lpfc_queue * wq,uint16_t * cq_map,int qidx,uint32_t qtype)8698 lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8699 	struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8700 	int qidx, uint32_t qtype)
8701 {
8702 	struct lpfc_sli_ring *pring;
8703 	int rc;
8704 
8705 	if (!eq || !cq || !wq) {
8706 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8707 			"6085 Fast-path %s (%d) not allocated\n",
8708 			((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8709 		return -ENOMEM;
8710 	}
8711 
8712 	/* create the Cq first */
8713 	rc = lpfc_cq_create(phba, cq, eq,
8714 			(qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8715 	if (rc) {
8716 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8717 			"6086 Failed setup of CQ (%d), rc = 0x%x\n",
8718 			qidx, (uint32_t)rc);
8719 		return rc;
8720 	}
8721 	cq->chann = qidx;
8722 
8723 	if (qtype != LPFC_MBOX) {
8724 		/* Setup nvme_cq_map for fast lookup */
8725 		if (cq_map)
8726 			*cq_map = cq->queue_id;
8727 
8728 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8729 			"6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8730 			qidx, cq->queue_id, qidx, eq->queue_id);
8731 
8732 		/* create the wq */
8733 		rc = lpfc_wq_create(phba, wq, cq, qtype);
8734 		if (rc) {
8735 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8736 				"6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8737 				qidx, (uint32_t)rc);
8738 			/* no need to tear down cq - caller will do so */
8739 			return rc;
8740 		}
8741 		wq->chann = qidx;
8742 
8743 		/* Bind this CQ/WQ to the NVME ring */
8744 		pring = wq->pring;
8745 		pring->sli.sli4.wqp = (void *)wq;
8746 		cq->pring = pring;
8747 
8748 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8749 			"2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8750 			qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8751 	} else {
8752 		rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8753 		if (rc) {
8754 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8755 				"0539 Failed setup of slow-path MQ: "
8756 				"rc = 0x%x\n", rc);
8757 			/* no need to tear down cq - caller will do so */
8758 			return rc;
8759 		}
8760 
8761 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8762 			"2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8763 			phba->sli4_hba.mbx_wq->queue_id,
8764 			phba->sli4_hba.mbx_cq->queue_id);
8765 	}
8766 
8767 	return 0;
8768 }
8769 
8770 /**
8771  * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8772  * @phba: pointer to lpfc hba data structure.
8773  *
8774  * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8775  * operation.
8776  *
8777  * Return codes
8778  *      0 - successful
8779  *      -ENOMEM - No available memory
8780  *      -EIO - The mailbox failed to complete successfully.
8781  **/
8782 int
lpfc_sli4_queue_setup(struct lpfc_hba * phba)8783 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8784 {
8785 	uint32_t shdr_status, shdr_add_status;
8786 	union lpfc_sli4_cfg_shdr *shdr;
8787 	LPFC_MBOXQ_t *mboxq;
8788 	int qidx;
8789 	uint32_t length, io_channel;
8790 	int rc = -ENOMEM;
8791 
8792 	/* Check for dual-ULP support */
8793 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8794 	if (!mboxq) {
8795 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8796 				"3249 Unable to allocate memory for "
8797 				"QUERY_FW_CFG mailbox command\n");
8798 		return -ENOMEM;
8799 	}
8800 	length = (sizeof(struct lpfc_mbx_query_fw_config) -
8801 		  sizeof(struct lpfc_sli4_cfg_mhdr));
8802 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8803 			 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8804 			 length, LPFC_SLI4_MBX_EMBED);
8805 
8806 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8807 
8808 	shdr = (union lpfc_sli4_cfg_shdr *)
8809 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8810 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8811 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8812 	if (shdr_status || shdr_add_status || rc) {
8813 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8814 				"3250 QUERY_FW_CFG mailbox failed with status "
8815 				"x%x add_status x%x, mbx status x%x\n",
8816 				shdr_status, shdr_add_status, rc);
8817 		if (rc != MBX_TIMEOUT)
8818 			mempool_free(mboxq, phba->mbox_mem_pool);
8819 		rc = -ENXIO;
8820 		goto out_error;
8821 	}
8822 
8823 	phba->sli4_hba.fw_func_mode =
8824 			mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8825 	phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8826 	phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
8827 	phba->sli4_hba.physical_port =
8828 			mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
8829 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8830 			"3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8831 			"ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8832 			phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8833 
8834 	if (rc != MBX_TIMEOUT)
8835 		mempool_free(mboxq, phba->mbox_mem_pool);
8836 
8837 	/*
8838 	 * Set up HBA Event Queues (EQs)
8839 	 */
8840 	io_channel = phba->io_channel_irqs;
8841 
8842 	/* Set up HBA event queue */
8843 	if (io_channel && !phba->sli4_hba.hba_eq) {
8844 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8845 				"3147 Fast-path EQs not allocated\n");
8846 		rc = -ENOMEM;
8847 		goto out_error;
8848 	}
8849 	for (qidx = 0; qidx < io_channel; qidx++) {
8850 		if (!phba->sli4_hba.hba_eq[qidx]) {
8851 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8852 					"0522 Fast-path EQ (%d) not "
8853 					"allocated\n", qidx);
8854 			rc = -ENOMEM;
8855 			goto out_destroy;
8856 		}
8857 		rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8858 						phba->cfg_fcp_imax);
8859 		if (rc) {
8860 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8861 					"0523 Failed setup of fast-path EQ "
8862 					"(%d), rc = 0x%x\n", qidx,
8863 					(uint32_t)rc);
8864 			goto out_destroy;
8865 		}
8866 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8867 				"2584 HBA EQ setup: queue[%d]-id=%d\n",
8868 				qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
8869 	}
8870 
8871 	if (phba->cfg_nvme_io_channel) {
8872 		if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
8873 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8874 				"6084 Fast-path NVME %s array not allocated\n",
8875 				(phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
8876 			rc = -ENOMEM;
8877 			goto out_destroy;
8878 		}
8879 
8880 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8881 			rc = lpfc_create_wq_cq(phba,
8882 					phba->sli4_hba.hba_eq[
8883 						qidx % io_channel],
8884 					phba->sli4_hba.nvme_cq[qidx],
8885 					phba->sli4_hba.nvme_wq[qidx],
8886 					&phba->sli4_hba.nvme_cq_map[qidx],
8887 					qidx, LPFC_NVME);
8888 			if (rc) {
8889 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8890 					"6123 Failed to setup fastpath "
8891 					"NVME WQ/CQ (%d), rc = 0x%x\n",
8892 					qidx, (uint32_t)rc);
8893 				goto out_destroy;
8894 			}
8895 		}
8896 	}
8897 
8898 	if (phba->cfg_fcp_io_channel) {
8899 		/* Set up fast-path FCP Response Complete Queue */
8900 		if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
8901 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8902 				"3148 Fast-path FCP %s array not allocated\n",
8903 				phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
8904 			rc = -ENOMEM;
8905 			goto out_destroy;
8906 		}
8907 
8908 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8909 			rc = lpfc_create_wq_cq(phba,
8910 					phba->sli4_hba.hba_eq[
8911 						qidx % io_channel],
8912 					phba->sli4_hba.fcp_cq[qidx],
8913 					phba->sli4_hba.fcp_wq[qidx],
8914 					&phba->sli4_hba.fcp_cq_map[qidx],
8915 					qidx, LPFC_FCP);
8916 			if (rc) {
8917 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8918 					"0535 Failed to setup fastpath "
8919 					"FCP WQ/CQ (%d), rc = 0x%x\n",
8920 					qidx, (uint32_t)rc);
8921 				goto out_destroy;
8922 			}
8923 		}
8924 	}
8925 
8926 	/*
8927 	 * Set up Slow Path Complete Queues (CQs)
8928 	 */
8929 
8930 	/* Set up slow-path MBOX CQ/MQ */
8931 
8932 	if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
8933 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8934 				"0528 %s not allocated\n",
8935 				phba->sli4_hba.mbx_cq ?
8936 				"Mailbox WQ" : "Mailbox CQ");
8937 		rc = -ENOMEM;
8938 		goto out_destroy;
8939 	}
8940 
8941 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8942 			       phba->sli4_hba.mbx_cq,
8943 			       phba->sli4_hba.mbx_wq,
8944 			       NULL, 0, LPFC_MBOX);
8945 	if (rc) {
8946 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8947 			"0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8948 			(uint32_t)rc);
8949 		goto out_destroy;
8950 	}
8951 	if (phba->nvmet_support) {
8952 		if (!phba->sli4_hba.nvmet_cqset) {
8953 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8954 					"3165 Fast-path NVME CQ Set "
8955 					"array not allocated\n");
8956 			rc = -ENOMEM;
8957 			goto out_destroy;
8958 		}
8959 		if (phba->cfg_nvmet_mrq > 1) {
8960 			rc = lpfc_cq_create_set(phba,
8961 					phba->sli4_hba.nvmet_cqset,
8962 					phba->sli4_hba.hba_eq,
8963 					LPFC_WCQ, LPFC_NVMET);
8964 			if (rc) {
8965 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8966 						"3164 Failed setup of NVME CQ "
8967 						"Set, rc = 0x%x\n",
8968 						(uint32_t)rc);
8969 				goto out_destroy;
8970 			}
8971 		} else {
8972 			/* Set up NVMET Receive Complete Queue */
8973 			rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8974 					    phba->sli4_hba.hba_eq[0],
8975 					    LPFC_WCQ, LPFC_NVMET);
8976 			if (rc) {
8977 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8978 						"6089 Failed setup NVMET CQ: "
8979 						"rc = 0x%x\n", (uint32_t)rc);
8980 				goto out_destroy;
8981 			}
8982 			phba->sli4_hba.nvmet_cqset[0]->chann = 0;
8983 
8984 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8985 					"6090 NVMET CQ setup: cq-id=%d, "
8986 					"parent eq-id=%d\n",
8987 					phba->sli4_hba.nvmet_cqset[0]->queue_id,
8988 					phba->sli4_hba.hba_eq[0]->queue_id);
8989 		}
8990 	}
8991 
8992 	/* Set up slow-path ELS WQ/CQ */
8993 	if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
8994 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8995 				"0530 ELS %s not allocated\n",
8996 				phba->sli4_hba.els_cq ? "WQ" : "CQ");
8997 		rc = -ENOMEM;
8998 		goto out_destroy;
8999 	}
9000 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
9001 					phba->sli4_hba.els_cq,
9002 					phba->sli4_hba.els_wq,
9003 					NULL, 0, LPFC_ELS);
9004 	if (rc) {
9005 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9006 			"0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9007 			(uint32_t)rc);
9008 		goto out_destroy;
9009 	}
9010 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9011 			"2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9012 			phba->sli4_hba.els_wq->queue_id,
9013 			phba->sli4_hba.els_cq->queue_id);
9014 
9015 	if (phba->cfg_nvme_io_channel) {
9016 		/* Set up NVME LS Complete Queue */
9017 		if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
9018 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9019 					"6091 LS %s not allocated\n",
9020 					phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9021 			rc = -ENOMEM;
9022 			goto out_destroy;
9023 		}
9024 		rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
9025 					phba->sli4_hba.nvmels_cq,
9026 					phba->sli4_hba.nvmels_wq,
9027 					NULL, 0, LPFC_NVME_LS);
9028 		if (rc) {
9029 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9030 				"0529 Failed setup of NVVME LS WQ/CQ: "
9031 				"rc = 0x%x\n", (uint32_t)rc);
9032 			goto out_destroy;
9033 		}
9034 
9035 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9036 				"6096 ELS WQ setup: wq-id=%d, "
9037 				"parent cq-id=%d\n",
9038 				phba->sli4_hba.nvmels_wq->queue_id,
9039 				phba->sli4_hba.nvmels_cq->queue_id);
9040 	}
9041 
9042 	/*
9043 	 * Create NVMET Receive Queue (RQ)
9044 	 */
9045 	if (phba->nvmet_support) {
9046 		if ((!phba->sli4_hba.nvmet_cqset) ||
9047 		    (!phba->sli4_hba.nvmet_mrq_hdr) ||
9048 		    (!phba->sli4_hba.nvmet_mrq_data)) {
9049 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9050 					"6130 MRQ CQ Queues not "
9051 					"allocated\n");
9052 			rc = -ENOMEM;
9053 			goto out_destroy;
9054 		}
9055 		if (phba->cfg_nvmet_mrq > 1) {
9056 			rc = lpfc_mrq_create(phba,
9057 					     phba->sli4_hba.nvmet_mrq_hdr,
9058 					     phba->sli4_hba.nvmet_mrq_data,
9059 					     phba->sli4_hba.nvmet_cqset,
9060 					     LPFC_NVMET);
9061 			if (rc) {
9062 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9063 						"6098 Failed setup of NVMET "
9064 						"MRQ: rc = 0x%x\n",
9065 						(uint32_t)rc);
9066 				goto out_destroy;
9067 			}
9068 
9069 		} else {
9070 			rc = lpfc_rq_create(phba,
9071 					    phba->sli4_hba.nvmet_mrq_hdr[0],
9072 					    phba->sli4_hba.nvmet_mrq_data[0],
9073 					    phba->sli4_hba.nvmet_cqset[0],
9074 					    LPFC_NVMET);
9075 			if (rc) {
9076 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9077 						"6057 Failed setup of NVMET "
9078 						"Receive Queue: rc = 0x%x\n",
9079 						(uint32_t)rc);
9080 				goto out_destroy;
9081 			}
9082 
9083 			lpfc_printf_log(
9084 				phba, KERN_INFO, LOG_INIT,
9085 				"6099 NVMET RQ setup: hdr-rq-id=%d, "
9086 				"dat-rq-id=%d parent cq-id=%d\n",
9087 				phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9088 				phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9089 				phba->sli4_hba.nvmet_cqset[0]->queue_id);
9090 
9091 		}
9092 	}
9093 
9094 	if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9095 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9096 				"0540 Receive Queue not allocated\n");
9097 		rc = -ENOMEM;
9098 		goto out_destroy;
9099 	}
9100 
9101 	rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
9102 			    phba->sli4_hba.els_cq, LPFC_USOL);
9103 	if (rc) {
9104 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9105 				"0541 Failed setup of Receive Queue: "
9106 				"rc = 0x%x\n", (uint32_t)rc);
9107 		goto out_destroy;
9108 	}
9109 
9110 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9111 			"2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9112 			"parent cq-id=%d\n",
9113 			phba->sli4_hba.hdr_rq->queue_id,
9114 			phba->sli4_hba.dat_rq->queue_id,
9115 			phba->sli4_hba.els_cq->queue_id);
9116 
9117 	if (phba->cfg_fof) {
9118 		rc = lpfc_fof_queue_setup(phba);
9119 		if (rc) {
9120 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9121 					"0549 Failed setup of FOF Queues: "
9122 					"rc = 0x%x\n", rc);
9123 			goto out_destroy;
9124 		}
9125 	}
9126 
9127 	for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
9128 		lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
9129 					 phba->cfg_fcp_imax);
9130 
9131 	return 0;
9132 
9133 out_destroy:
9134 	lpfc_sli4_queue_unset(phba);
9135 out_error:
9136 	return rc;
9137 }
9138 
9139 /**
9140  * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9141  * @phba: pointer to lpfc hba data structure.
9142  *
9143  * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9144  * operation.
9145  *
9146  * Return codes
9147  *      0 - successful
9148  *      -ENOMEM - No available memory
9149  *      -EIO - The mailbox failed to complete successfully.
9150  **/
9151 void
lpfc_sli4_queue_unset(struct lpfc_hba * phba)9152 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9153 {
9154 	int qidx;
9155 
9156 	/* Unset the queues created for Flash Optimized Fabric operations */
9157 	if (phba->cfg_fof)
9158 		lpfc_fof_queue_destroy(phba);
9159 
9160 	/* Unset mailbox command work queue */
9161 	if (phba->sli4_hba.mbx_wq)
9162 		lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9163 
9164 	/* Unset NVME LS work queue */
9165 	if (phba->sli4_hba.nvmels_wq)
9166 		lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9167 
9168 	/* Unset ELS work queue */
9169 	if (phba->sli4_hba.els_wq)
9170 		lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9171 
9172 	/* Unset unsolicited receive queue */
9173 	if (phba->sli4_hba.hdr_rq)
9174 		lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9175 				phba->sli4_hba.dat_rq);
9176 
9177 	/* Unset FCP work queue */
9178 	if (phba->sli4_hba.fcp_wq)
9179 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9180 			lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
9181 
9182 	/* Unset NVME work queue */
9183 	if (phba->sli4_hba.nvme_wq) {
9184 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9185 			lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
9186 	}
9187 
9188 	/* Unset mailbox command complete queue */
9189 	if (phba->sli4_hba.mbx_cq)
9190 		lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9191 
9192 	/* Unset ELS complete queue */
9193 	if (phba->sli4_hba.els_cq)
9194 		lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9195 
9196 	/* Unset NVME LS complete queue */
9197 	if (phba->sli4_hba.nvmels_cq)
9198 		lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9199 
9200 	/* Unset NVME response complete queue */
9201 	if (phba->sli4_hba.nvme_cq)
9202 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9203 			lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
9204 
9205 	if (phba->nvmet_support) {
9206 		/* Unset NVMET MRQ queue */
9207 		if (phba->sli4_hba.nvmet_mrq_hdr) {
9208 			for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9209 				lpfc_rq_destroy(
9210 					phba,
9211 					phba->sli4_hba.nvmet_mrq_hdr[qidx],
9212 					phba->sli4_hba.nvmet_mrq_data[qidx]);
9213 		}
9214 
9215 		/* Unset NVMET CQ Set complete queue */
9216 		if (phba->sli4_hba.nvmet_cqset) {
9217 			for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9218 				lpfc_cq_destroy(
9219 					phba, phba->sli4_hba.nvmet_cqset[qidx]);
9220 		}
9221 	}
9222 
9223 	/* Unset FCP response complete queue */
9224 	if (phba->sli4_hba.fcp_cq)
9225 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9226 			lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
9227 
9228 	/* Unset fast-path event queue */
9229 	if (phba->sli4_hba.hba_eq)
9230 		for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
9231 			lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
9232 }
9233 
9234 /**
9235  * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9236  * @phba: pointer to lpfc hba data structure.
9237  *
9238  * This routine is invoked to allocate and set up a pool of completion queue
9239  * events. The body of the completion queue event is a completion queue entry
9240  * CQE. For now, this pool is used for the interrupt service routine to queue
9241  * the following HBA completion queue events for the worker thread to process:
9242  *   - Mailbox asynchronous events
9243  *   - Receive queue completion unsolicited events
9244  * Later, this can be used for all the slow-path events.
9245  *
9246  * Return codes
9247  *      0 - successful
9248  *      -ENOMEM - No available memory
9249  **/
9250 static int
lpfc_sli4_cq_event_pool_create(struct lpfc_hba * phba)9251 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9252 {
9253 	struct lpfc_cq_event *cq_event;
9254 	int i;
9255 
9256 	for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9257 		cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9258 		if (!cq_event)
9259 			goto out_pool_create_fail;
9260 		list_add_tail(&cq_event->list,
9261 			      &phba->sli4_hba.sp_cqe_event_pool);
9262 	}
9263 	return 0;
9264 
9265 out_pool_create_fail:
9266 	lpfc_sli4_cq_event_pool_destroy(phba);
9267 	return -ENOMEM;
9268 }
9269 
9270 /**
9271  * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9272  * @phba: pointer to lpfc hba data structure.
9273  *
9274  * This routine is invoked to free the pool of completion queue events at
9275  * driver unload time. Note that, it is the responsibility of the driver
9276  * cleanup routine to free all the outstanding completion-queue events
9277  * allocated from this pool back into the pool before invoking this routine
9278  * to destroy the pool.
9279  **/
9280 static void
lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba * phba)9281 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9282 {
9283 	struct lpfc_cq_event *cq_event, *next_cq_event;
9284 
9285 	list_for_each_entry_safe(cq_event, next_cq_event,
9286 				 &phba->sli4_hba.sp_cqe_event_pool, list) {
9287 		list_del(&cq_event->list);
9288 		kfree(cq_event);
9289 	}
9290 }
9291 
9292 /**
9293  * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9294  * @phba: pointer to lpfc hba data structure.
9295  *
9296  * This routine is the lock free version of the API invoked to allocate a
9297  * completion-queue event from the free pool.
9298  *
9299  * Return: Pointer to the newly allocated completion-queue event if successful
9300  *         NULL otherwise.
9301  **/
9302 struct lpfc_cq_event *
__lpfc_sli4_cq_event_alloc(struct lpfc_hba * phba)9303 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9304 {
9305 	struct lpfc_cq_event *cq_event = NULL;
9306 
9307 	list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9308 			 struct lpfc_cq_event, list);
9309 	return cq_event;
9310 }
9311 
9312 /**
9313  * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9314  * @phba: pointer to lpfc hba data structure.
9315  *
9316  * This routine is the lock version of the API invoked to allocate a
9317  * completion-queue event from the free pool.
9318  *
9319  * Return: Pointer to the newly allocated completion-queue event if successful
9320  *         NULL otherwise.
9321  **/
9322 struct lpfc_cq_event *
lpfc_sli4_cq_event_alloc(struct lpfc_hba * phba)9323 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9324 {
9325 	struct lpfc_cq_event *cq_event;
9326 	unsigned long iflags;
9327 
9328 	spin_lock_irqsave(&phba->hbalock, iflags);
9329 	cq_event = __lpfc_sli4_cq_event_alloc(phba);
9330 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9331 	return cq_event;
9332 }
9333 
9334 /**
9335  * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9336  * @phba: pointer to lpfc hba data structure.
9337  * @cq_event: pointer to the completion queue event to be freed.
9338  *
9339  * This routine is the lock free version of the API invoked to release a
9340  * completion-queue event back into the free pool.
9341  **/
9342 void
__lpfc_sli4_cq_event_release(struct lpfc_hba * phba,struct lpfc_cq_event * cq_event)9343 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9344 			     struct lpfc_cq_event *cq_event)
9345 {
9346 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9347 }
9348 
9349 /**
9350  * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9351  * @phba: pointer to lpfc hba data structure.
9352  * @cq_event: pointer to the completion queue event to be freed.
9353  *
9354  * This routine is the lock version of the API invoked to release a
9355  * completion-queue event back into the free pool.
9356  **/
9357 void
lpfc_sli4_cq_event_release(struct lpfc_hba * phba,struct lpfc_cq_event * cq_event)9358 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9359 			   struct lpfc_cq_event *cq_event)
9360 {
9361 	unsigned long iflags;
9362 	spin_lock_irqsave(&phba->hbalock, iflags);
9363 	__lpfc_sli4_cq_event_release(phba, cq_event);
9364 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9365 }
9366 
9367 /**
9368  * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9369  * @phba: pointer to lpfc hba data structure.
9370  *
9371  * This routine is to free all the pending completion-queue events to the
9372  * back into the free pool for device reset.
9373  **/
9374 static void
lpfc_sli4_cq_event_release_all(struct lpfc_hba * phba)9375 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9376 {
9377 	LIST_HEAD(cqelist);
9378 	struct lpfc_cq_event *cqe;
9379 	unsigned long iflags;
9380 
9381 	/* Retrieve all the pending WCQEs from pending WCQE lists */
9382 	spin_lock_irqsave(&phba->hbalock, iflags);
9383 	/* Pending FCP XRI abort events */
9384 	list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9385 			 &cqelist);
9386 	/* Pending ELS XRI abort events */
9387 	list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9388 			 &cqelist);
9389 	/* Pending asynnc events */
9390 	list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9391 			 &cqelist);
9392 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9393 
9394 	while (!list_empty(&cqelist)) {
9395 		list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9396 		lpfc_sli4_cq_event_release(phba, cqe);
9397 	}
9398 }
9399 
9400 /**
9401  * lpfc_pci_function_reset - Reset pci function.
9402  * @phba: pointer to lpfc hba data structure.
9403  *
9404  * This routine is invoked to request a PCI function reset. It will destroys
9405  * all resources assigned to the PCI function which originates this request.
9406  *
9407  * Return codes
9408  *      0 - successful
9409  *      -ENOMEM - No available memory
9410  *      -EIO - The mailbox failed to complete successfully.
9411  **/
9412 int
lpfc_pci_function_reset(struct lpfc_hba * phba)9413 lpfc_pci_function_reset(struct lpfc_hba *phba)
9414 {
9415 	LPFC_MBOXQ_t *mboxq;
9416 	uint32_t rc = 0, if_type;
9417 	uint32_t shdr_status, shdr_add_status;
9418 	uint32_t rdy_chk;
9419 	uint32_t port_reset = 0;
9420 	union lpfc_sli4_cfg_shdr *shdr;
9421 	struct lpfc_register reg_data;
9422 	uint16_t devid;
9423 
9424 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9425 	switch (if_type) {
9426 	case LPFC_SLI_INTF_IF_TYPE_0:
9427 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9428 						       GFP_KERNEL);
9429 		if (!mboxq) {
9430 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9431 					"0494 Unable to allocate memory for "
9432 					"issuing SLI_FUNCTION_RESET mailbox "
9433 					"command\n");
9434 			return -ENOMEM;
9435 		}
9436 
9437 		/* Setup PCI function reset mailbox-ioctl command */
9438 		lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9439 				 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9440 				 LPFC_SLI4_MBX_EMBED);
9441 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9442 		shdr = (union lpfc_sli4_cfg_shdr *)
9443 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9444 		shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9445 		shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9446 					 &shdr->response);
9447 		if (rc != MBX_TIMEOUT)
9448 			mempool_free(mboxq, phba->mbox_mem_pool);
9449 		if (shdr_status || shdr_add_status || rc) {
9450 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9451 					"0495 SLI_FUNCTION_RESET mailbox "
9452 					"failed with status x%x add_status x%x,"
9453 					" mbx status x%x\n",
9454 					shdr_status, shdr_add_status, rc);
9455 			rc = -ENXIO;
9456 		}
9457 		break;
9458 	case LPFC_SLI_INTF_IF_TYPE_2:
9459 	case LPFC_SLI_INTF_IF_TYPE_6:
9460 wait:
9461 		/*
9462 		 * Poll the Port Status Register and wait for RDY for
9463 		 * up to 30 seconds. If the port doesn't respond, treat
9464 		 * it as an error.
9465 		 */
9466 		for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
9467 			if (lpfc_readl(phba->sli4_hba.u.if_type2.
9468 				STATUSregaddr, &reg_data.word0)) {
9469 				rc = -ENODEV;
9470 				goto out;
9471 			}
9472 			if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9473 				break;
9474 			msleep(20);
9475 		}
9476 
9477 		if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9478 			phba->work_status[0] = readl(
9479 				phba->sli4_hba.u.if_type2.ERR1regaddr);
9480 			phba->work_status[1] = readl(
9481 				phba->sli4_hba.u.if_type2.ERR2regaddr);
9482 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9483 					"2890 Port not ready, port status reg "
9484 					"0x%x error 1=0x%x, error 2=0x%x\n",
9485 					reg_data.word0,
9486 					phba->work_status[0],
9487 					phba->work_status[1]);
9488 			rc = -ENODEV;
9489 			goto out;
9490 		}
9491 
9492 		if (!port_reset) {
9493 			/*
9494 			 * Reset the port now
9495 			 */
9496 			reg_data.word0 = 0;
9497 			bf_set(lpfc_sliport_ctrl_end, &reg_data,
9498 			       LPFC_SLIPORT_LITTLE_ENDIAN);
9499 			bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9500 			       LPFC_SLIPORT_INIT_PORT);
9501 			writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9502 			       CTRLregaddr);
9503 			/* flush */
9504 			pci_read_config_word(phba->pcidev,
9505 					     PCI_DEVICE_ID, &devid);
9506 
9507 			port_reset = 1;
9508 			msleep(20);
9509 			goto wait;
9510 		} else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9511 			rc = -ENODEV;
9512 			goto out;
9513 		}
9514 		break;
9515 
9516 	case LPFC_SLI_INTF_IF_TYPE_1:
9517 	default:
9518 		break;
9519 	}
9520 
9521 out:
9522 	/* Catch the not-ready port failure after a port reset. */
9523 	if (rc) {
9524 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9525 				"3317 HBA not functional: IP Reset Failed "
9526 				"try: echo fw_reset > board_mode\n");
9527 		rc = -ENODEV;
9528 	}
9529 
9530 	return rc;
9531 }
9532 
9533 /**
9534  * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9535  * @phba: pointer to lpfc hba data structure.
9536  *
9537  * This routine is invoked to set up the PCI device memory space for device
9538  * with SLI-4 interface spec.
9539  *
9540  * Return codes
9541  * 	0 - successful
9542  * 	other values - error
9543  **/
9544 static int
lpfc_sli4_pci_mem_setup(struct lpfc_hba * phba)9545 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9546 {
9547 	struct pci_dev *pdev;
9548 	unsigned long bar0map_len, bar1map_len, bar2map_len;
9549 	int error = -ENODEV;
9550 	uint32_t if_type;
9551 
9552 	/* Obtain PCI device reference */
9553 	if (!phba->pcidev)
9554 		return error;
9555 	else
9556 		pdev = phba->pcidev;
9557 
9558 	/* Set the device DMA mask size */
9559 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9560 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9561 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9562 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
9563 			return error;
9564 		}
9565 	}
9566 
9567 	/*
9568 	 * The BARs and register set definitions and offset locations are
9569 	 * dependent on the if_type.
9570 	 */
9571 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9572 				  &phba->sli4_hba.sli_intf.word0)) {
9573 		return error;
9574 	}
9575 
9576 	/* There is no SLI3 failback for SLI4 devices. */
9577 	if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9578 	    LPFC_SLI_INTF_VALID) {
9579 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9580 				"2894 SLI_INTF reg contents invalid "
9581 				"sli_intf reg 0x%x\n",
9582 				phba->sli4_hba.sli_intf.word0);
9583 		return error;
9584 	}
9585 
9586 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9587 	/*
9588 	 * Get the bus address of SLI4 device Bar regions and the
9589 	 * number of bytes required by each mapping. The mapping of the
9590 	 * particular PCI BARs regions is dependent on the type of
9591 	 * SLI4 device.
9592 	 */
9593 	if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9594 		phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9595 		bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
9596 
9597 		/*
9598 		 * Map SLI4 PCI Config Space Register base to a kernel virtual
9599 		 * addr
9600 		 */
9601 		phba->sli4_hba.conf_regs_memmap_p =
9602 			ioremap(phba->pci_bar0_map, bar0map_len);
9603 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9604 			dev_printk(KERN_ERR, &pdev->dev,
9605 				   "ioremap failed for SLI4 PCI config "
9606 				   "registers.\n");
9607 			goto out;
9608 		}
9609 		phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
9610 		/* Set up BAR0 PCI config space register memory map */
9611 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9612 	} else {
9613 		phba->pci_bar0_map = pci_resource_start(pdev, 1);
9614 		bar0map_len = pci_resource_len(pdev, 1);
9615 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9616 			dev_printk(KERN_ERR, &pdev->dev,
9617 			   "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9618 			goto out;
9619 		}
9620 		phba->sli4_hba.conf_regs_memmap_p =
9621 				ioremap(phba->pci_bar0_map, bar0map_len);
9622 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9623 			dev_printk(KERN_ERR, &pdev->dev,
9624 				"ioremap failed for SLI4 PCI config "
9625 				"registers.\n");
9626 				goto out;
9627 		}
9628 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9629 	}
9630 
9631 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9632 		if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
9633 			/*
9634 			 * Map SLI4 if type 0 HBA Control Register base to a
9635 			 * kernel virtual address and setup the registers.
9636 			 */
9637 			phba->pci_bar1_map = pci_resource_start(pdev,
9638 								PCI_64BIT_BAR2);
9639 			bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9640 			phba->sli4_hba.ctrl_regs_memmap_p =
9641 					ioremap(phba->pci_bar1_map,
9642 						bar1map_len);
9643 			if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9644 				dev_err(&pdev->dev,
9645 					   "ioremap failed for SLI4 HBA "
9646 					    "control registers.\n");
9647 				error = -ENOMEM;
9648 				goto out_iounmap_conf;
9649 			}
9650 			phba->pci_bar2_memmap_p =
9651 					 phba->sli4_hba.ctrl_regs_memmap_p;
9652 			lpfc_sli4_bar1_register_memmap(phba, if_type);
9653 		} else {
9654 			error = -ENOMEM;
9655 			goto out_iounmap_conf;
9656 		}
9657 	}
9658 
9659 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
9660 	    (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
9661 		/*
9662 		 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
9663 		 * virtual address and setup the registers.
9664 		 */
9665 		phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9666 		bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9667 		phba->sli4_hba.drbl_regs_memmap_p =
9668 				ioremap(phba->pci_bar1_map, bar1map_len);
9669 		if (!phba->sli4_hba.drbl_regs_memmap_p) {
9670 			dev_err(&pdev->dev,
9671 			   "ioremap failed for SLI4 HBA doorbell registers.\n");
9672 			goto out_iounmap_conf;
9673 		}
9674 		phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
9675 		lpfc_sli4_bar1_register_memmap(phba, if_type);
9676 	}
9677 
9678 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9679 		if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9680 			/*
9681 			 * Map SLI4 if type 0 HBA Doorbell Register base to
9682 			 * a kernel virtual address and setup the registers.
9683 			 */
9684 			phba->pci_bar2_map = pci_resource_start(pdev,
9685 								PCI_64BIT_BAR4);
9686 			bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9687 			phba->sli4_hba.drbl_regs_memmap_p =
9688 					ioremap(phba->pci_bar2_map,
9689 						bar2map_len);
9690 			if (!phba->sli4_hba.drbl_regs_memmap_p) {
9691 				dev_err(&pdev->dev,
9692 					   "ioremap failed for SLI4 HBA"
9693 					   " doorbell registers.\n");
9694 				error = -ENOMEM;
9695 				goto out_iounmap_ctrl;
9696 			}
9697 			phba->pci_bar4_memmap_p =
9698 					phba->sli4_hba.drbl_regs_memmap_p;
9699 			error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9700 			if (error)
9701 				goto out_iounmap_all;
9702 		} else {
9703 			error = -ENOMEM;
9704 			goto out_iounmap_all;
9705 		}
9706 	}
9707 
9708 	if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
9709 	    pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9710 		/*
9711 		 * Map SLI4 if type 6 HBA DPP Register base to a kernel
9712 		 * virtual address and setup the registers.
9713 		 */
9714 		phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9715 		bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9716 		phba->sli4_hba.dpp_regs_memmap_p =
9717 				ioremap(phba->pci_bar2_map, bar2map_len);
9718 		if (!phba->sli4_hba.dpp_regs_memmap_p) {
9719 			dev_err(&pdev->dev,
9720 			   "ioremap failed for SLI4 HBA dpp registers.\n");
9721 			goto out_iounmap_ctrl;
9722 		}
9723 		phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
9724 	}
9725 
9726 	/* Set up the EQ/CQ register handeling functions now */
9727 	switch (if_type) {
9728 	case LPFC_SLI_INTF_IF_TYPE_0:
9729 	case LPFC_SLI_INTF_IF_TYPE_2:
9730 		phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
9731 		phba->sli4_hba.sli4_eq_release = lpfc_sli4_eq_release;
9732 		phba->sli4_hba.sli4_cq_release = lpfc_sli4_cq_release;
9733 		break;
9734 	case LPFC_SLI_INTF_IF_TYPE_6:
9735 		phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
9736 		phba->sli4_hba.sli4_eq_release = lpfc_sli4_if6_eq_release;
9737 		phba->sli4_hba.sli4_cq_release = lpfc_sli4_if6_cq_release;
9738 		break;
9739 	default:
9740 		break;
9741 	}
9742 
9743 	return 0;
9744 
9745 out_iounmap_all:
9746 	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9747 out_iounmap_ctrl:
9748 	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9749 out_iounmap_conf:
9750 	iounmap(phba->sli4_hba.conf_regs_memmap_p);
9751 out:
9752 	return error;
9753 }
9754 
9755 /**
9756  * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9757  * @phba: pointer to lpfc hba data structure.
9758  *
9759  * This routine is invoked to unset the PCI device memory space for device
9760  * with SLI-4 interface spec.
9761  **/
9762 static void
lpfc_sli4_pci_mem_unset(struct lpfc_hba * phba)9763 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9764 {
9765 	uint32_t if_type;
9766 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9767 
9768 	switch (if_type) {
9769 	case LPFC_SLI_INTF_IF_TYPE_0:
9770 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9771 		iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9772 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9773 		break;
9774 	case LPFC_SLI_INTF_IF_TYPE_2:
9775 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9776 		break;
9777 	case LPFC_SLI_INTF_IF_TYPE_6:
9778 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9779 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9780 		break;
9781 	case LPFC_SLI_INTF_IF_TYPE_1:
9782 	default:
9783 		dev_printk(KERN_ERR, &phba->pcidev->dev,
9784 			   "FATAL - unsupported SLI4 interface type - %d\n",
9785 			   if_type);
9786 		break;
9787 	}
9788 }
9789 
9790 /**
9791  * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9792  * @phba: pointer to lpfc hba data structure.
9793  *
9794  * This routine is invoked to enable the MSI-X interrupt vectors to device
9795  * with SLI-3 interface specs.
9796  *
9797  * Return codes
9798  *   0 - successful
9799  *   other values - error
9800  **/
9801 static int
lpfc_sli_enable_msix(struct lpfc_hba * phba)9802 lpfc_sli_enable_msix(struct lpfc_hba *phba)
9803 {
9804 	int rc;
9805 	LPFC_MBOXQ_t *pmb;
9806 
9807 	/* Set up MSI-X multi-message vectors */
9808 	rc = pci_alloc_irq_vectors(phba->pcidev,
9809 			LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9810 	if (rc < 0) {
9811 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9812 				"0420 PCI enable MSI-X failed (%d)\n", rc);
9813 		goto vec_fail_out;
9814 	}
9815 
9816 	/*
9817 	 * Assign MSI-X vectors to interrupt handlers
9818 	 */
9819 
9820 	/* vector-0 is associated to slow-path handler */
9821 	rc = request_irq(pci_irq_vector(phba->pcidev, 0),
9822 			 &lpfc_sli_sp_intr_handler, 0,
9823 			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9824 	if (rc) {
9825 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9826 				"0421 MSI-X slow-path request_irq failed "
9827 				"(%d)\n", rc);
9828 		goto msi_fail_out;
9829 	}
9830 
9831 	/* vector-1 is associated to fast-path handler */
9832 	rc = request_irq(pci_irq_vector(phba->pcidev, 1),
9833 			 &lpfc_sli_fp_intr_handler, 0,
9834 			 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9835 
9836 	if (rc) {
9837 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9838 				"0429 MSI-X fast-path request_irq failed "
9839 				"(%d)\n", rc);
9840 		goto irq_fail_out;
9841 	}
9842 
9843 	/*
9844 	 * Configure HBA MSI-X attention conditions to messages
9845 	 */
9846 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9847 
9848 	if (!pmb) {
9849 		rc = -ENOMEM;
9850 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9851 				"0474 Unable to allocate memory for issuing "
9852 				"MBOX_CONFIG_MSI command\n");
9853 		goto mem_fail_out;
9854 	}
9855 	rc = lpfc_config_msi(phba, pmb);
9856 	if (rc)
9857 		goto mbx_fail_out;
9858 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9859 	if (rc != MBX_SUCCESS) {
9860 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9861 				"0351 Config MSI mailbox command failed, "
9862 				"mbxCmd x%x, mbxStatus x%x\n",
9863 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9864 		goto mbx_fail_out;
9865 	}
9866 
9867 	/* Free memory allocated for mailbox command */
9868 	mempool_free(pmb, phba->mbox_mem_pool);
9869 	return rc;
9870 
9871 mbx_fail_out:
9872 	/* Free memory allocated for mailbox command */
9873 	mempool_free(pmb, phba->mbox_mem_pool);
9874 
9875 mem_fail_out:
9876 	/* free the irq already requested */
9877 	free_irq(pci_irq_vector(phba->pcidev, 1), phba);
9878 
9879 irq_fail_out:
9880 	/* free the irq already requested */
9881 	free_irq(pci_irq_vector(phba->pcidev, 0), phba);
9882 
9883 msi_fail_out:
9884 	/* Unconfigure MSI-X capability structure */
9885 	pci_free_irq_vectors(phba->pcidev);
9886 
9887 vec_fail_out:
9888 	return rc;
9889 }
9890 
9891 /**
9892  * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9893  * @phba: pointer to lpfc hba data structure.
9894  *
9895  * This routine is invoked to enable the MSI interrupt mode to device with
9896  * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9897  * enable the MSI vector. The device driver is responsible for calling the
9898  * request_irq() to register MSI vector with a interrupt the handler, which
9899  * is done in this function.
9900  *
9901  * Return codes
9902  * 	0 - successful
9903  * 	other values - error
9904  */
9905 static int
lpfc_sli_enable_msi(struct lpfc_hba * phba)9906 lpfc_sli_enable_msi(struct lpfc_hba *phba)
9907 {
9908 	int rc;
9909 
9910 	rc = pci_enable_msi(phba->pcidev);
9911 	if (!rc)
9912 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9913 				"0462 PCI enable MSI mode success.\n");
9914 	else {
9915 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9916 				"0471 PCI enable MSI mode failed (%d)\n", rc);
9917 		return rc;
9918 	}
9919 
9920 	rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9921 			 0, LPFC_DRIVER_NAME, phba);
9922 	if (rc) {
9923 		pci_disable_msi(phba->pcidev);
9924 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9925 				"0478 MSI request_irq failed (%d)\n", rc);
9926 	}
9927 	return rc;
9928 }
9929 
9930 /**
9931  * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9932  * @phba: pointer to lpfc hba data structure.
9933  *
9934  * This routine is invoked to enable device interrupt and associate driver's
9935  * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9936  * spec. Depends on the interrupt mode configured to the driver, the driver
9937  * will try to fallback from the configured interrupt mode to an interrupt
9938  * mode which is supported by the platform, kernel, and device in the order
9939  * of:
9940  * MSI-X -> MSI -> IRQ.
9941  *
9942  * Return codes
9943  *   0 - successful
9944  *   other values - error
9945  **/
9946 static uint32_t
lpfc_sli_enable_intr(struct lpfc_hba * phba,uint32_t cfg_mode)9947 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9948 {
9949 	uint32_t intr_mode = LPFC_INTR_ERROR;
9950 	int retval;
9951 
9952 	if (cfg_mode == 2) {
9953 		/* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9954 		retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9955 		if (!retval) {
9956 			/* Now, try to enable MSI-X interrupt mode */
9957 			retval = lpfc_sli_enable_msix(phba);
9958 			if (!retval) {
9959 				/* Indicate initialization to MSI-X mode */
9960 				phba->intr_type = MSIX;
9961 				intr_mode = 2;
9962 			}
9963 		}
9964 	}
9965 
9966 	/* Fallback to MSI if MSI-X initialization failed */
9967 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9968 		retval = lpfc_sli_enable_msi(phba);
9969 		if (!retval) {
9970 			/* Indicate initialization to MSI mode */
9971 			phba->intr_type = MSI;
9972 			intr_mode = 1;
9973 		}
9974 	}
9975 
9976 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9977 	if (phba->intr_type == NONE) {
9978 		retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9979 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9980 		if (!retval) {
9981 			/* Indicate initialization to INTx mode */
9982 			phba->intr_type = INTx;
9983 			intr_mode = 0;
9984 		}
9985 	}
9986 	return intr_mode;
9987 }
9988 
9989 /**
9990  * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9991  * @phba: pointer to lpfc hba data structure.
9992  *
9993  * This routine is invoked to disable device interrupt and disassociate the
9994  * driver's interrupt handler(s) from interrupt vector(s) to device with
9995  * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9996  * release the interrupt vector(s) for the message signaled interrupt.
9997  **/
9998 static void
lpfc_sli_disable_intr(struct lpfc_hba * phba)9999 lpfc_sli_disable_intr(struct lpfc_hba *phba)
10000 {
10001 	int nr_irqs, i;
10002 
10003 	if (phba->intr_type == MSIX)
10004 		nr_irqs = LPFC_MSIX_VECTORS;
10005 	else
10006 		nr_irqs = 1;
10007 
10008 	for (i = 0; i < nr_irqs; i++)
10009 		free_irq(pci_irq_vector(phba->pcidev, i), phba);
10010 	pci_free_irq_vectors(phba->pcidev);
10011 
10012 	/* Reset interrupt management states */
10013 	phba->intr_type = NONE;
10014 	phba->sli.slistat.sli_intr = 0;
10015 }
10016 
10017 /**
10018  * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
10019  * @phba: pointer to lpfc hba data structure.
10020  * @vectors: number of msix vectors allocated.
10021  *
10022  * The routine will figure out the CPU affinity assignment for every
10023  * MSI-X vector allocated for the HBA.  The hba_eq_hdl will be updated
10024  * with a pointer to the CPU mask that defines ALL the CPUs this vector
10025  * can be associated with. If the vector can be unquely associated with
10026  * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
10027  * In addition, the CPU to IO channel mapping will be calculated
10028  * and the phba->sli4_hba.cpu_map array will reflect this.
10029  */
10030 static void
lpfc_cpu_affinity_check(struct lpfc_hba * phba,int vectors)10031 lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
10032 {
10033 	struct lpfc_vector_map_info *cpup;
10034 	int index = 0;
10035 	int vec = 0;
10036 	int cpu;
10037 #ifdef CONFIG_X86
10038 	struct cpuinfo_x86 *cpuinfo;
10039 #endif
10040 
10041 	/* Init cpu_map array */
10042 	memset(phba->sli4_hba.cpu_map, 0xff,
10043 	       (sizeof(struct lpfc_vector_map_info) *
10044 	       phba->sli4_hba.num_present_cpu));
10045 
10046 	/* Update CPU map with physical id and core id of each CPU */
10047 	cpup = phba->sli4_hba.cpu_map;
10048 	for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
10049 #ifdef CONFIG_X86
10050 		cpuinfo = &cpu_data(cpu);
10051 		cpup->phys_id = cpuinfo->phys_proc_id;
10052 		cpup->core_id = cpuinfo->cpu_core_id;
10053 #else
10054 		/* No distinction between CPUs for other platforms */
10055 		cpup->phys_id = 0;
10056 		cpup->core_id = 0;
10057 #endif
10058 		cpup->channel_id = index;  /* For now round robin */
10059 		cpup->irq = pci_irq_vector(phba->pcidev, vec);
10060 		vec++;
10061 		if (vec >= vectors)
10062 			vec = 0;
10063 		index++;
10064 		if (index >= phba->cfg_fcp_io_channel)
10065 			index = 0;
10066 		cpup++;
10067 	}
10068 }
10069 
10070 
10071 /**
10072  * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
10073  * @phba: pointer to lpfc hba data structure.
10074  *
10075  * This routine is invoked to enable the MSI-X interrupt vectors to device
10076  * with SLI-4 interface spec.
10077  *
10078  * Return codes
10079  * 0 - successful
10080  * other values - error
10081  **/
10082 static int
lpfc_sli4_enable_msix(struct lpfc_hba * phba)10083 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
10084 {
10085 	int vectors, rc, index;
10086 	char *name;
10087 
10088 	/* Set up MSI-X multi-message vectors */
10089 	vectors = phba->io_channel_irqs;
10090 	if (phba->cfg_fof)
10091 		vectors++;
10092 
10093 	rc = pci_alloc_irq_vectors(phba->pcidev,
10094 				(phba->nvmet_support) ? 1 : 2,
10095 				vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
10096 	if (rc < 0) {
10097 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10098 				"0484 PCI enable MSI-X failed (%d)\n", rc);
10099 		goto vec_fail_out;
10100 	}
10101 	vectors = rc;
10102 
10103 	/* Assign MSI-X vectors to interrupt handlers */
10104 	for (index = 0; index < vectors; index++) {
10105 		name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
10106 		memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
10107 		snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
10108 			 LPFC_DRIVER_HANDLER_NAME"%d", index);
10109 
10110 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
10111 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10112 		atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
10113 		if (phba->cfg_fof && (index == (vectors - 1)))
10114 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
10115 				 &lpfc_sli4_fof_intr_handler, 0,
10116 				 name,
10117 				 &phba->sli4_hba.hba_eq_hdl[index]);
10118 		else
10119 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
10120 				 &lpfc_sli4_hba_intr_handler, 0,
10121 				 name,
10122 				 &phba->sli4_hba.hba_eq_hdl[index]);
10123 		if (rc) {
10124 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10125 					"0486 MSI-X fast-path (%d) "
10126 					"request_irq failed (%d)\n", index, rc);
10127 			goto cfg_fail_out;
10128 		}
10129 	}
10130 
10131 	if (phba->cfg_fof)
10132 		vectors--;
10133 
10134 	if (vectors != phba->io_channel_irqs) {
10135 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10136 				"3238 Reducing IO channels to match number of "
10137 				"MSI-X vectors, requested %d got %d\n",
10138 				phba->io_channel_irqs, vectors);
10139 		if (phba->cfg_fcp_io_channel > vectors)
10140 			phba->cfg_fcp_io_channel = vectors;
10141 		if (phba->cfg_nvme_io_channel > vectors)
10142 			phba->cfg_nvme_io_channel = vectors;
10143 		if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
10144 			phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10145 		else
10146 			phba->io_channel_irqs = phba->cfg_nvme_io_channel;
10147 	}
10148 	lpfc_cpu_affinity_check(phba, vectors);
10149 
10150 	return rc;
10151 
10152 cfg_fail_out:
10153 	/* free the irq already requested */
10154 	for (--index; index >= 0; index--)
10155 		free_irq(pci_irq_vector(phba->pcidev, index),
10156 				&phba->sli4_hba.hba_eq_hdl[index]);
10157 
10158 	/* Unconfigure MSI-X capability structure */
10159 	pci_free_irq_vectors(phba->pcidev);
10160 
10161 vec_fail_out:
10162 	return rc;
10163 }
10164 
10165 /**
10166  * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
10167  * @phba: pointer to lpfc hba data structure.
10168  *
10169  * This routine is invoked to enable the MSI interrupt mode to device with
10170  * SLI-4 interface spec. The kernel function pci_enable_msi() is called
10171  * to enable the MSI vector. The device driver is responsible for calling
10172  * the request_irq() to register MSI vector with a interrupt the handler,
10173  * which is done in this function.
10174  *
10175  * Return codes
10176  * 	0 - successful
10177  * 	other values - error
10178  **/
10179 static int
lpfc_sli4_enable_msi(struct lpfc_hba * phba)10180 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
10181 {
10182 	int rc, index;
10183 
10184 	rc = pci_enable_msi(phba->pcidev);
10185 	if (!rc)
10186 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10187 				"0487 PCI enable MSI mode success.\n");
10188 	else {
10189 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10190 				"0488 PCI enable MSI mode failed (%d)\n", rc);
10191 		return rc;
10192 	}
10193 
10194 	rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10195 			 0, LPFC_DRIVER_NAME, phba);
10196 	if (rc) {
10197 		pci_disable_msi(phba->pcidev);
10198 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10199 				"0490 MSI request_irq failed (%d)\n", rc);
10200 		return rc;
10201 	}
10202 
10203 	for (index = 0; index < phba->io_channel_irqs; index++) {
10204 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
10205 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10206 	}
10207 
10208 	if (phba->cfg_fof) {
10209 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
10210 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10211 	}
10212 	return 0;
10213 }
10214 
10215 /**
10216  * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
10217  * @phba: pointer to lpfc hba data structure.
10218  *
10219  * This routine is invoked to enable device interrupt and associate driver's
10220  * interrupt handler(s) to interrupt vector(s) to device with SLI-4
10221  * interface spec. Depends on the interrupt mode configured to the driver,
10222  * the driver will try to fallback from the configured interrupt mode to an
10223  * interrupt mode which is supported by the platform, kernel, and device in
10224  * the order of:
10225  * MSI-X -> MSI -> IRQ.
10226  *
10227  * Return codes
10228  * 	0 - successful
10229  * 	other values - error
10230  **/
10231 static uint32_t
lpfc_sli4_enable_intr(struct lpfc_hba * phba,uint32_t cfg_mode)10232 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10233 {
10234 	uint32_t intr_mode = LPFC_INTR_ERROR;
10235 	int retval, idx;
10236 
10237 	if (cfg_mode == 2) {
10238 		/* Preparation before conf_msi mbox cmd */
10239 		retval = 0;
10240 		if (!retval) {
10241 			/* Now, try to enable MSI-X interrupt mode */
10242 			retval = lpfc_sli4_enable_msix(phba);
10243 			if (!retval) {
10244 				/* Indicate initialization to MSI-X mode */
10245 				phba->intr_type = MSIX;
10246 				intr_mode = 2;
10247 			}
10248 		}
10249 	}
10250 
10251 	/* Fallback to MSI if MSI-X initialization failed */
10252 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
10253 		retval = lpfc_sli4_enable_msi(phba);
10254 		if (!retval) {
10255 			/* Indicate initialization to MSI mode */
10256 			phba->intr_type = MSI;
10257 			intr_mode = 1;
10258 		}
10259 	}
10260 
10261 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
10262 	if (phba->intr_type == NONE) {
10263 		retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10264 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10265 		if (!retval) {
10266 			struct lpfc_hba_eq_hdl *eqhdl;
10267 
10268 			/* Indicate initialization to INTx mode */
10269 			phba->intr_type = INTx;
10270 			intr_mode = 0;
10271 
10272 			for (idx = 0; idx < phba->io_channel_irqs; idx++) {
10273 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10274 				eqhdl->idx = idx;
10275 				eqhdl->phba = phba;
10276 				atomic_set(&eqhdl->hba_eq_in_use, 1);
10277 			}
10278 			if (phba->cfg_fof) {
10279 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10280 				eqhdl->idx = idx;
10281 				eqhdl->phba = phba;
10282 				atomic_set(&eqhdl->hba_eq_in_use, 1);
10283 			}
10284 		}
10285 	}
10286 	return intr_mode;
10287 }
10288 
10289 /**
10290  * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
10291  * @phba: pointer to lpfc hba data structure.
10292  *
10293  * This routine is invoked to disable device interrupt and disassociate
10294  * the driver's interrupt handler(s) from interrupt vector(s) to device
10295  * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10296  * will release the interrupt vector(s) for the message signaled interrupt.
10297  **/
10298 static void
lpfc_sli4_disable_intr(struct lpfc_hba * phba)10299 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10300 {
10301 	/* Disable the currently initialized interrupt mode */
10302 	if (phba->intr_type == MSIX) {
10303 		int index;
10304 
10305 		/* Free up MSI-X multi-message vectors */
10306 		for (index = 0; index < phba->io_channel_irqs; index++)
10307 			free_irq(pci_irq_vector(phba->pcidev, index),
10308 					&phba->sli4_hba.hba_eq_hdl[index]);
10309 
10310 		if (phba->cfg_fof)
10311 			free_irq(pci_irq_vector(phba->pcidev, index),
10312 					&phba->sli4_hba.hba_eq_hdl[index]);
10313 	} else {
10314 		free_irq(phba->pcidev->irq, phba);
10315 	}
10316 
10317 	pci_free_irq_vectors(phba->pcidev);
10318 
10319 	/* Reset interrupt management states */
10320 	phba->intr_type = NONE;
10321 	phba->sli.slistat.sli_intr = 0;
10322 }
10323 
10324 /**
10325  * lpfc_unset_hba - Unset SLI3 hba device initialization
10326  * @phba: pointer to lpfc hba data structure.
10327  *
10328  * This routine is invoked to unset the HBA device initialization steps to
10329  * a device with SLI-3 interface spec.
10330  **/
10331 static void
lpfc_unset_hba(struct lpfc_hba * phba)10332 lpfc_unset_hba(struct lpfc_hba *phba)
10333 {
10334 	struct lpfc_vport *vport = phba->pport;
10335 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10336 
10337 	spin_lock_irq(shost->host_lock);
10338 	vport->load_flag |= FC_UNLOADING;
10339 	spin_unlock_irq(shost->host_lock);
10340 
10341 	kfree(phba->vpi_bmask);
10342 	kfree(phba->vpi_ids);
10343 
10344 	lpfc_stop_hba_timers(phba);
10345 
10346 	phba->pport->work_port_events = 0;
10347 
10348 	lpfc_sli_hba_down(phba);
10349 
10350 	lpfc_sli_brdrestart(phba);
10351 
10352 	lpfc_sli_disable_intr(phba);
10353 
10354 	return;
10355 }
10356 
10357 /**
10358  * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10359  * @phba: Pointer to HBA context object.
10360  *
10361  * This function is called in the SLI4 code path to wait for completion
10362  * of device's XRIs exchange busy. It will check the XRI exchange busy
10363  * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10364  * that, it will check the XRI exchange busy on outstanding FCP and ELS
10365  * I/Os every 30 seconds, log error message, and wait forever. Only when
10366  * all XRI exchange busy complete, the driver unload shall proceed with
10367  * invoking the function reset ioctl mailbox command to the CNA and the
10368  * the rest of the driver unload resource release.
10369  **/
10370 static void
lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba * phba)10371 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10372 {
10373 	int wait_time = 0;
10374 	int nvme_xri_cmpl = 1;
10375 	int nvmet_xri_cmpl = 1;
10376 	int fcp_xri_cmpl = 1;
10377 	int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10378 
10379 	/* Driver just aborted IOs during the hba_unset process.  Pause
10380 	 * here to give the HBA time to complete the IO and get entries
10381 	 * into the abts lists.
10382 	 */
10383 	msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
10384 
10385 	/* Wait for NVME pending IO to flush back to transport. */
10386 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
10387 		lpfc_nvme_wait_for_io_drain(phba);
10388 
10389 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10390 		fcp_xri_cmpl =
10391 			list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10392 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10393 		nvme_xri_cmpl =
10394 			list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10395 		nvmet_xri_cmpl =
10396 			list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10397 	}
10398 
10399 	while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10400 	       !nvmet_xri_cmpl) {
10401 		if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
10402 			if (!nvmet_xri_cmpl)
10403 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10404 						"6424 NVMET XRI exchange busy "
10405 						"wait time: %d seconds.\n",
10406 						wait_time/1000);
10407 			if (!nvme_xri_cmpl)
10408 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10409 						"6100 NVME XRI exchange busy "
10410 						"wait time: %d seconds.\n",
10411 						wait_time/1000);
10412 			if (!fcp_xri_cmpl)
10413 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10414 						"2877 FCP XRI exchange busy "
10415 						"wait time: %d seconds.\n",
10416 						wait_time/1000);
10417 			if (!els_xri_cmpl)
10418 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10419 						"2878 ELS XRI exchange busy "
10420 						"wait time: %d seconds.\n",
10421 						wait_time/1000);
10422 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10423 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10424 		} else {
10425 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10426 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10427 		}
10428 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10429 			nvme_xri_cmpl = list_empty(
10430 				&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10431 			nvmet_xri_cmpl = list_empty(
10432 				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10433 		}
10434 
10435 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10436 			fcp_xri_cmpl = list_empty(
10437 				&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10438 
10439 		els_xri_cmpl =
10440 			list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10441 
10442 	}
10443 }
10444 
10445 /**
10446  * lpfc_sli4_hba_unset - Unset the fcoe hba
10447  * @phba: Pointer to HBA context object.
10448  *
10449  * This function is called in the SLI4 code path to reset the HBA's FCoE
10450  * function. The caller is not required to hold any lock. This routine
10451  * issues PCI function reset mailbox command to reset the FCoE function.
10452  * At the end of the function, it calls lpfc_hba_down_post function to
10453  * free any pending commands.
10454  **/
10455 static void
lpfc_sli4_hba_unset(struct lpfc_hba * phba)10456 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10457 {
10458 	int wait_cnt = 0;
10459 	LPFC_MBOXQ_t *mboxq;
10460 	struct pci_dev *pdev = phba->pcidev;
10461 
10462 	lpfc_stop_hba_timers(phba);
10463 	phba->sli4_hba.intr_enable = 0;
10464 
10465 	/*
10466 	 * Gracefully wait out the potential current outstanding asynchronous
10467 	 * mailbox command.
10468 	 */
10469 
10470 	/* First, block any pending async mailbox command from posted */
10471 	spin_lock_irq(&phba->hbalock);
10472 	phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10473 	spin_unlock_irq(&phba->hbalock);
10474 	/* Now, trying to wait it out if we can */
10475 	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10476 		msleep(10);
10477 		if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10478 			break;
10479 	}
10480 	/* Forcefully release the outstanding mailbox command if timed out */
10481 	if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10482 		spin_lock_irq(&phba->hbalock);
10483 		mboxq = phba->sli.mbox_active;
10484 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10485 		__lpfc_mbox_cmpl_put(phba, mboxq);
10486 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10487 		phba->sli.mbox_active = NULL;
10488 		spin_unlock_irq(&phba->hbalock);
10489 	}
10490 
10491 	/* Abort all iocbs associated with the hba */
10492 	lpfc_sli_hba_iocb_abort(phba);
10493 
10494 	/* Wait for completion of device XRI exchange busy */
10495 	lpfc_sli4_xri_exchange_busy_wait(phba);
10496 
10497 	/* Disable PCI subsystem interrupt */
10498 	lpfc_sli4_disable_intr(phba);
10499 
10500 	/* Disable SR-IOV if enabled */
10501 	if (phba->cfg_sriov_nr_virtfn)
10502 		pci_disable_sriov(pdev);
10503 
10504 	/* Stop kthread signal shall trigger work_done one more time */
10505 	kthread_stop(phba->worker_thread);
10506 
10507 	/* Unset the queues shared with the hardware then release all
10508 	 * allocated resources.
10509 	 */
10510 	lpfc_sli4_queue_unset(phba);
10511 	lpfc_sli4_queue_destroy(phba);
10512 
10513 	/* Reset SLI4 HBA FCoE function */
10514 	lpfc_pci_function_reset(phba);
10515 
10516 	/* Stop the SLI4 device port */
10517 	phba->pport->work_port_events = 0;
10518 }
10519 
10520  /**
10521  * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10522  * @phba: Pointer to HBA context object.
10523  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10524  *
10525  * This function is called in the SLI4 code path to read the port's
10526  * sli4 capabilities.
10527  *
10528  * This function may be be called from any context that can block-wait
10529  * for the completion.  The expectation is that this routine is called
10530  * typically from probe_one or from the online routine.
10531  **/
10532 int
lpfc_pc_sli4_params_get(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)10533 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10534 {
10535 	int rc;
10536 	struct lpfc_mqe *mqe;
10537 	struct lpfc_pc_sli4_params *sli4_params;
10538 	uint32_t mbox_tmo;
10539 
10540 	rc = 0;
10541 	mqe = &mboxq->u.mqe;
10542 
10543 	/* Read the port's SLI4 Parameters port capabilities */
10544 	lpfc_pc_sli4_params(mboxq);
10545 	if (!phba->sli4_hba.intr_enable)
10546 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10547 	else {
10548 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10549 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10550 	}
10551 
10552 	if (unlikely(rc))
10553 		return 1;
10554 
10555 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10556 	sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10557 	sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10558 	sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10559 	sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10560 					     &mqe->un.sli4_params);
10561 	sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10562 					     &mqe->un.sli4_params);
10563 	sli4_params->proto_types = mqe->un.sli4_params.word3;
10564 	sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10565 	sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10566 	sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10567 	sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10568 	sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10569 	sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10570 	sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10571 	sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10572 	sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10573 	sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10574 	sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10575 	sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10576 	sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10577 	sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10578 	sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10579 	sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10580 	sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10581 	sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10582 	sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10583 	sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
10584 
10585 	/* Make sure that sge_supp_len can be handled by the driver */
10586 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10587 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10588 
10589 	return rc;
10590 }
10591 
10592 /**
10593  * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10594  * @phba: Pointer to HBA context object.
10595  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10596  *
10597  * This function is called in the SLI4 code path to read the port's
10598  * sli4 capabilities.
10599  *
10600  * This function may be be called from any context that can block-wait
10601  * for the completion.  The expectation is that this routine is called
10602  * typically from probe_one or from the online routine.
10603  **/
10604 int
lpfc_get_sli4_parameters(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)10605 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10606 {
10607 	int rc;
10608 	struct lpfc_mqe *mqe = &mboxq->u.mqe;
10609 	struct lpfc_pc_sli4_params *sli4_params;
10610 	uint32_t mbox_tmo;
10611 	int length;
10612 	bool exp_wqcq_pages = true;
10613 	struct lpfc_sli4_parameters *mbx_sli4_parameters;
10614 
10615 	/*
10616 	 * By default, the driver assumes the SLI4 port requires RPI
10617 	 * header postings.  The SLI4_PARAM response will correct this
10618 	 * assumption.
10619 	 */
10620 	phba->sli4_hba.rpi_hdrs_in_use = 1;
10621 
10622 	/* Read the port's SLI4 Config Parameters */
10623 	length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10624 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10625 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10626 			 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10627 			 length, LPFC_SLI4_MBX_EMBED);
10628 	if (!phba->sli4_hba.intr_enable)
10629 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10630 	else {
10631 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10632 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10633 	}
10634 	if (unlikely(rc))
10635 		return rc;
10636 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10637 	mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10638 	sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10639 	sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10640 	sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10641 	sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10642 					     mbx_sli4_parameters);
10643 	sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10644 					     mbx_sli4_parameters);
10645 	if (bf_get(cfg_phwq, mbx_sli4_parameters))
10646 		phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10647 	else
10648 		phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10649 	sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10650 	sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
10651 	sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
10652 	sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10653 	sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10654 	sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10655 	sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
10656 	sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
10657 	sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
10658 	sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
10659 	sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
10660 	sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10661 					    mbx_sli4_parameters);
10662 	sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
10663 	sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10664 					   mbx_sli4_parameters);
10665 	phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10666 	phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
10667 	phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10668 			      bf_get(cfg_xib, mbx_sli4_parameters));
10669 
10670 	if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10671 	    !phba->nvme_support) {
10672 		phba->nvme_support = 0;
10673 		phba->nvmet_support = 0;
10674 		phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_OFF;
10675 		phba->cfg_nvme_io_channel = 0;
10676 		phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10677 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10678 				"6101 Disabling NVME support: "
10679 				"Not supported by firmware: %d %d\n",
10680 				bf_get(cfg_nvme, mbx_sli4_parameters),
10681 				bf_get(cfg_xib, mbx_sli4_parameters));
10682 
10683 		/* If firmware doesn't support NVME, just use SCSI support */
10684 		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10685 			return -ENODEV;
10686 		phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10687 	}
10688 
10689 	/* Only embed PBDE for if_type 6, PBDE support requires xib be set */
10690 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10691 	    LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
10692 		phba->cfg_enable_pbde = 0;
10693 
10694 	/*
10695 	 * To support Suppress Response feature we must satisfy 3 conditions.
10696 	 * lpfc_suppress_rsp module parameter must be set (default).
10697 	 * In SLI4-Parameters Descriptor:
10698 	 * Extended Inline Buffers (XIB) must be supported.
10699 	 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
10700 	 * (double negative).
10701 	 */
10702 	if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
10703 	    !(bf_get(cfg_nosr, mbx_sli4_parameters)))
10704 		phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10705 	else
10706 		phba->cfg_suppress_rsp = 0;
10707 
10708 	if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10709 		phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10710 
10711 	/* Make sure that sge_supp_len can be handled by the driver */
10712 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10713 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10714 
10715 	/*
10716 	 * Check whether the adapter supports an embedded copy of the
10717 	 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
10718 	 * to use this option, 128-byte WQEs must be used.
10719 	 */
10720 	if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10721 		phba->fcp_embed_io = 1;
10722 	else
10723 		phba->fcp_embed_io = 0;
10724 
10725 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
10726 			"6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
10727 			bf_get(cfg_xib, mbx_sli4_parameters),
10728 			phba->cfg_enable_pbde,
10729 			phba->fcp_embed_io, phba->nvme_support,
10730 			phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
10731 
10732 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
10733 	    LPFC_SLI_INTF_IF_TYPE_2) &&
10734 	    (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
10735 		 LPFC_SLI_INTF_FAMILY_LNCR_A0))
10736 		exp_wqcq_pages = false;
10737 
10738 	if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
10739 	    (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
10740 	    exp_wqcq_pages &&
10741 	    (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
10742 		phba->enab_exp_wqcq_pages = 1;
10743 	else
10744 		phba->enab_exp_wqcq_pages = 0;
10745 	/*
10746 	 * Check if the SLI port supports MDS Diagnostics
10747 	 */
10748 	if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10749 		phba->mds_diags_support = 1;
10750 	else
10751 		phba->mds_diags_support = 0;
10752 	return 0;
10753 }
10754 
10755 /**
10756  * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10757  * @pdev: pointer to PCI device
10758  * @pid: pointer to PCI device identifier
10759  *
10760  * This routine is to be called to attach a device with SLI-3 interface spec
10761  * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10762  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10763  * information of the device and driver to see if the driver state that it can
10764  * support this kind of device. If the match is successful, the driver core
10765  * invokes this routine. If this routine determines it can claim the HBA, it
10766  * does all the initialization that it needs to do to handle the HBA properly.
10767  *
10768  * Return code
10769  * 	0 - driver can claim the device
10770  * 	negative value - driver can not claim the device
10771  **/
10772 static int
lpfc_pci_probe_one_s3(struct pci_dev * pdev,const struct pci_device_id * pid)10773 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10774 {
10775 	struct lpfc_hba   *phba;
10776 	struct lpfc_vport *vport = NULL;
10777 	struct Scsi_Host  *shost = NULL;
10778 	int error;
10779 	uint32_t cfg_mode, intr_mode;
10780 
10781 	/* Allocate memory for HBA structure */
10782 	phba = lpfc_hba_alloc(pdev);
10783 	if (!phba)
10784 		return -ENOMEM;
10785 
10786 	/* Perform generic PCI device enabling operation */
10787 	error = lpfc_enable_pci_dev(phba);
10788 	if (error)
10789 		goto out_free_phba;
10790 
10791 	/* Set up SLI API function jump table for PCI-device group-0 HBAs */
10792 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10793 	if (error)
10794 		goto out_disable_pci_dev;
10795 
10796 	/* Set up SLI-3 specific device PCI memory space */
10797 	error = lpfc_sli_pci_mem_setup(phba);
10798 	if (error) {
10799 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10800 				"1402 Failed to set up pci memory space.\n");
10801 		goto out_disable_pci_dev;
10802 	}
10803 
10804 	/* Set up SLI-3 specific device driver resources */
10805 	error = lpfc_sli_driver_resource_setup(phba);
10806 	if (error) {
10807 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10808 				"1404 Failed to set up driver resource.\n");
10809 		goto out_unset_pci_mem_s3;
10810 	}
10811 
10812 	/* Initialize and populate the iocb list per host */
10813 
10814 	error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10815 	if (error) {
10816 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10817 				"1405 Failed to initialize iocb list.\n");
10818 		goto out_unset_driver_resource_s3;
10819 	}
10820 
10821 	/* Set up common device driver resources */
10822 	error = lpfc_setup_driver_resource_phase2(phba);
10823 	if (error) {
10824 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10825 				"1406 Failed to set up driver resource.\n");
10826 		goto out_free_iocb_list;
10827 	}
10828 
10829 	/* Get the default values for Model Name and Description */
10830 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10831 
10832 	/* Create SCSI host to the physical port */
10833 	error = lpfc_create_shost(phba);
10834 	if (error) {
10835 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10836 				"1407 Failed to create scsi host.\n");
10837 		goto out_unset_driver_resource;
10838 	}
10839 
10840 	/* Configure sysfs attributes */
10841 	vport = phba->pport;
10842 	error = lpfc_alloc_sysfs_attr(vport);
10843 	if (error) {
10844 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10845 				"1476 Failed to allocate sysfs attr\n");
10846 		goto out_destroy_shost;
10847 	}
10848 
10849 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
10850 	/* Now, trying to enable interrupt and bring up the device */
10851 	cfg_mode = phba->cfg_use_msi;
10852 	while (true) {
10853 		/* Put device to a known state before enabling interrupt */
10854 		lpfc_stop_port(phba);
10855 		/* Configure and enable interrupt */
10856 		intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10857 		if (intr_mode == LPFC_INTR_ERROR) {
10858 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10859 					"0431 Failed to enable interrupt.\n");
10860 			error = -ENODEV;
10861 			goto out_free_sysfs_attr;
10862 		}
10863 		/* SLI-3 HBA setup */
10864 		if (lpfc_sli_hba_setup(phba)) {
10865 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10866 					"1477 Failed to set up hba\n");
10867 			error = -ENODEV;
10868 			goto out_remove_device;
10869 		}
10870 
10871 		/* Wait 50ms for the interrupts of previous mailbox commands */
10872 		msleep(50);
10873 		/* Check active interrupts on message signaled interrupts */
10874 		if (intr_mode == 0 ||
10875 		    phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10876 			/* Log the current active interrupt mode */
10877 			phba->intr_mode = intr_mode;
10878 			lpfc_log_intr_mode(phba, intr_mode);
10879 			break;
10880 		} else {
10881 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10882 					"0447 Configure interrupt mode (%d) "
10883 					"failed active interrupt test.\n",
10884 					intr_mode);
10885 			/* Disable the current interrupt mode */
10886 			lpfc_sli_disable_intr(phba);
10887 			/* Try next level of interrupt mode */
10888 			cfg_mode = --intr_mode;
10889 		}
10890 	}
10891 
10892 	/* Perform post initialization setup */
10893 	lpfc_post_init_setup(phba);
10894 
10895 	/* Check if there are static vports to be created. */
10896 	lpfc_create_static_vport(phba);
10897 
10898 	return 0;
10899 
10900 out_remove_device:
10901 	lpfc_unset_hba(phba);
10902 out_free_sysfs_attr:
10903 	lpfc_free_sysfs_attr(vport);
10904 out_destroy_shost:
10905 	lpfc_destroy_shost(phba);
10906 out_unset_driver_resource:
10907 	lpfc_unset_driver_resource_phase2(phba);
10908 out_free_iocb_list:
10909 	lpfc_free_iocb_list(phba);
10910 out_unset_driver_resource_s3:
10911 	lpfc_sli_driver_resource_unset(phba);
10912 out_unset_pci_mem_s3:
10913 	lpfc_sli_pci_mem_unset(phba);
10914 out_disable_pci_dev:
10915 	lpfc_disable_pci_dev(phba);
10916 	if (shost)
10917 		scsi_host_put(shost);
10918 out_free_phba:
10919 	lpfc_hba_free(phba);
10920 	return error;
10921 }
10922 
10923 /**
10924  * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
10925  * @pdev: pointer to PCI device
10926  *
10927  * This routine is to be called to disattach a device with SLI-3 interface
10928  * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10929  * removed from PCI bus, it performs all the necessary cleanup for the HBA
10930  * device to be removed from the PCI subsystem properly.
10931  **/
10932 static void
lpfc_pci_remove_one_s3(struct pci_dev * pdev)10933 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
10934 {
10935 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
10936 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10937 	struct lpfc_vport **vports;
10938 	struct lpfc_hba   *phba = vport->phba;
10939 	int i;
10940 
10941 	spin_lock_irq(&phba->hbalock);
10942 	vport->load_flag |= FC_UNLOADING;
10943 	spin_unlock_irq(&phba->hbalock);
10944 
10945 	lpfc_free_sysfs_attr(vport);
10946 
10947 	/* Release all the vports against this physical port */
10948 	vports = lpfc_create_vport_work_array(phba);
10949 	if (vports != NULL)
10950 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10951 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10952 				continue;
10953 			fc_vport_terminate(vports[i]->fc_vport);
10954 		}
10955 	lpfc_destroy_vport_work_array(phba, vports);
10956 
10957 	/* Remove FC host and then SCSI host with the physical port */
10958 	fc_remove_host(shost);
10959 	scsi_remove_host(shost);
10960 
10961 	lpfc_cleanup(vport);
10962 
10963 	/*
10964 	 * Bring down the SLI Layer. This step disable all interrupts,
10965 	 * clears the rings, discards all mailbox commands, and resets
10966 	 * the HBA.
10967 	 */
10968 
10969 	/* HBA interrupt will be disabled after this call */
10970 	lpfc_sli_hba_down(phba);
10971 	/* Stop kthread signal shall trigger work_done one more time */
10972 	kthread_stop(phba->worker_thread);
10973 	/* Final cleanup of txcmplq and reset the HBA */
10974 	lpfc_sli_brdrestart(phba);
10975 
10976 	kfree(phba->vpi_bmask);
10977 	kfree(phba->vpi_ids);
10978 
10979 	lpfc_stop_hba_timers(phba);
10980 	spin_lock_irq(&phba->hbalock);
10981 	list_del_init(&vport->listentry);
10982 	spin_unlock_irq(&phba->hbalock);
10983 
10984 	lpfc_debugfs_terminate(vport);
10985 
10986 	/* Disable SR-IOV if enabled */
10987 	if (phba->cfg_sriov_nr_virtfn)
10988 		pci_disable_sriov(pdev);
10989 
10990 	/* Disable interrupt */
10991 	lpfc_sli_disable_intr(phba);
10992 
10993 	scsi_host_put(shost);
10994 
10995 	/*
10996 	 * Call scsi_free before mem_free since scsi bufs are released to their
10997 	 * corresponding pools here.
10998 	 */
10999 	lpfc_scsi_free(phba);
11000 	lpfc_mem_free_all(phba);
11001 
11002 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
11003 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
11004 
11005 	/* Free resources associated with SLI2 interface */
11006 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
11007 			  phba->slim2p.virt, phba->slim2p.phys);
11008 
11009 	/* unmap adapter SLIM and Control Registers */
11010 	iounmap(phba->ctrl_regs_memmap_p);
11011 	iounmap(phba->slim_memmap_p);
11012 
11013 	lpfc_hba_free(phba);
11014 
11015 	pci_release_mem_regions(pdev);
11016 	pci_disable_device(pdev);
11017 }
11018 
11019 /**
11020  * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
11021  * @pdev: pointer to PCI device
11022  * @msg: power management message
11023  *
11024  * This routine is to be called from the kernel's PCI subsystem to support
11025  * system Power Management (PM) to device with SLI-3 interface spec. When
11026  * PM invokes this method, it quiesces the device by stopping the driver's
11027  * worker thread for the device, turning off device's interrupt and DMA,
11028  * and bring the device offline. Note that as the driver implements the
11029  * minimum PM requirements to a power-aware driver's PM support for the
11030  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11031  * to the suspend() method call will be treated as SUSPEND and the driver will
11032  * fully reinitialize its device during resume() method call, the driver will
11033  * set device to PCI_D3hot state in PCI config space instead of setting it
11034  * according to the @msg provided by the PM.
11035  *
11036  * Return code
11037  * 	0 - driver suspended the device
11038  * 	Error otherwise
11039  **/
11040 static int
lpfc_pci_suspend_one_s3(struct pci_dev * pdev,pm_message_t msg)11041 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
11042 {
11043 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11044 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11045 
11046 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11047 			"0473 PCI device Power Management suspend.\n");
11048 
11049 	/* Bring down the device */
11050 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11051 	lpfc_offline(phba);
11052 	kthread_stop(phba->worker_thread);
11053 
11054 	/* Disable interrupt from device */
11055 	lpfc_sli_disable_intr(phba);
11056 
11057 	/* Save device state to PCI config space */
11058 	pci_save_state(pdev);
11059 	pci_set_power_state(pdev, PCI_D3hot);
11060 
11061 	return 0;
11062 }
11063 
11064 /**
11065  * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
11066  * @pdev: pointer to PCI device
11067  *
11068  * This routine is to be called from the kernel's PCI subsystem to support
11069  * system Power Management (PM) to device with SLI-3 interface spec. When PM
11070  * invokes this method, it restores the device's PCI config space state and
11071  * fully reinitializes the device and brings it online. Note that as the
11072  * driver implements the minimum PM requirements to a power-aware driver's
11073  * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
11074  * FREEZE) to the suspend() method call will be treated as SUSPEND and the
11075  * driver will fully reinitialize its device during resume() method call,
11076  * the device will be set to PCI_D0 directly in PCI config space before
11077  * restoring the state.
11078  *
11079  * Return code
11080  * 	0 - driver suspended the device
11081  * 	Error otherwise
11082  **/
11083 static int
lpfc_pci_resume_one_s3(struct pci_dev * pdev)11084 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
11085 {
11086 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11087 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11088 	uint32_t intr_mode;
11089 	int error;
11090 
11091 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11092 			"0452 PCI device Power Management resume.\n");
11093 
11094 	/* Restore device state from PCI config space */
11095 	pci_set_power_state(pdev, PCI_D0);
11096 	pci_restore_state(pdev);
11097 
11098 	/*
11099 	 * As the new kernel behavior of pci_restore_state() API call clears
11100 	 * device saved_state flag, need to save the restored state again.
11101 	 */
11102 	pci_save_state(pdev);
11103 
11104 	if (pdev->is_busmaster)
11105 		pci_set_master(pdev);
11106 
11107 	/* Startup the kernel thread for this host adapter. */
11108 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
11109 					"lpfc_worker_%d", phba->brd_no);
11110 	if (IS_ERR(phba->worker_thread)) {
11111 		error = PTR_ERR(phba->worker_thread);
11112 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11113 				"0434 PM resume failed to start worker "
11114 				"thread: error=x%x.\n", error);
11115 		return error;
11116 	}
11117 
11118 	/* Configure and enable interrupt */
11119 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
11120 	if (intr_mode == LPFC_INTR_ERROR) {
11121 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11122 				"0430 PM resume Failed to enable interrupt\n");
11123 		return -EIO;
11124 	} else
11125 		phba->intr_mode = intr_mode;
11126 
11127 	/* Restart HBA and bring it online */
11128 	lpfc_sli_brdrestart(phba);
11129 	lpfc_online(phba);
11130 
11131 	/* Log the current active interrupt mode */
11132 	lpfc_log_intr_mode(phba, phba->intr_mode);
11133 
11134 	return 0;
11135 }
11136 
11137 /**
11138  * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
11139  * @phba: pointer to lpfc hba data structure.
11140  *
11141  * This routine is called to prepare the SLI3 device for PCI slot recover. It
11142  * aborts all the outstanding SCSI I/Os to the pci device.
11143  **/
11144 static void
lpfc_sli_prep_dev_for_recover(struct lpfc_hba * phba)11145 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
11146 {
11147 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11148 			"2723 PCI channel I/O abort preparing for recovery\n");
11149 
11150 	/*
11151 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11152 	 * and let the SCSI mid-layer to retry them to recover.
11153 	 */
11154 	lpfc_sli_abort_fcp_rings(phba);
11155 }
11156 
11157 /**
11158  * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
11159  * @phba: pointer to lpfc hba data structure.
11160  *
11161  * This routine is called to prepare the SLI3 device for PCI slot reset. It
11162  * disables the device interrupt and pci device, and aborts the internal FCP
11163  * pending I/Os.
11164  **/
11165 static void
lpfc_sli_prep_dev_for_reset(struct lpfc_hba * phba)11166 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
11167 {
11168 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11169 			"2710 PCI channel disable preparing for reset\n");
11170 
11171 	/* Block any management I/Os to the device */
11172 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
11173 
11174 	/* Block all SCSI devices' I/Os on the host */
11175 	lpfc_scsi_dev_block(phba);
11176 
11177 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
11178 	lpfc_sli_flush_fcp_rings(phba);
11179 
11180 	/* stop all timers */
11181 	lpfc_stop_hba_timers(phba);
11182 
11183 	/* Disable interrupt and pci device */
11184 	lpfc_sli_disable_intr(phba);
11185 	pci_disable_device(phba->pcidev);
11186 }
11187 
11188 /**
11189  * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
11190  * @phba: pointer to lpfc hba data structure.
11191  *
11192  * This routine is called to prepare the SLI3 device for PCI slot permanently
11193  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11194  * pending I/Os.
11195  **/
11196 static void
lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba * phba)11197 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11198 {
11199 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11200 			"2711 PCI channel permanent disable for failure\n");
11201 	/* Block all SCSI devices' I/Os on the host */
11202 	lpfc_scsi_dev_block(phba);
11203 
11204 	/* stop all timers */
11205 	lpfc_stop_hba_timers(phba);
11206 
11207 	/* Clean up all driver's outstanding SCSI I/Os */
11208 	lpfc_sli_flush_fcp_rings(phba);
11209 }
11210 
11211 /**
11212  * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
11213  * @pdev: pointer to PCI device.
11214  * @state: the current PCI connection state.
11215  *
11216  * This routine is called from the PCI subsystem for I/O error handling to
11217  * device with SLI-3 interface spec. This function is called by the PCI
11218  * subsystem after a PCI bus error affecting this device has been detected.
11219  * When this function is invoked, it will need to stop all the I/Os and
11220  * interrupt(s) to the device. Once that is done, it will return
11221  * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
11222  * as desired.
11223  *
11224  * Return codes
11225  * 	PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
11226  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11227  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11228  **/
11229 static pci_ers_result_t
lpfc_io_error_detected_s3(struct pci_dev * pdev,pci_channel_state_t state)11230 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
11231 {
11232 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11233 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11234 
11235 	switch (state) {
11236 	case pci_channel_io_normal:
11237 		/* Non-fatal error, prepare for recovery */
11238 		lpfc_sli_prep_dev_for_recover(phba);
11239 		return PCI_ERS_RESULT_CAN_RECOVER;
11240 	case pci_channel_io_frozen:
11241 		/* Fatal error, prepare for slot reset */
11242 		lpfc_sli_prep_dev_for_reset(phba);
11243 		return PCI_ERS_RESULT_NEED_RESET;
11244 	case pci_channel_io_perm_failure:
11245 		/* Permanent failure, prepare for device down */
11246 		lpfc_sli_prep_dev_for_perm_failure(phba);
11247 		return PCI_ERS_RESULT_DISCONNECT;
11248 	default:
11249 		/* Unknown state, prepare and request slot reset */
11250 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11251 				"0472 Unknown PCI error state: x%x\n", state);
11252 		lpfc_sli_prep_dev_for_reset(phba);
11253 		return PCI_ERS_RESULT_NEED_RESET;
11254 	}
11255 }
11256 
11257 /**
11258  * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
11259  * @pdev: pointer to PCI device.
11260  *
11261  * This routine is called from the PCI subsystem for error handling to
11262  * device with SLI-3 interface spec. This is called after PCI bus has been
11263  * reset to restart the PCI card from scratch, as if from a cold-boot.
11264  * During the PCI subsystem error recovery, after driver returns
11265  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11266  * recovery and then call this routine before calling the .resume method
11267  * to recover the device. This function will initialize the HBA device,
11268  * enable the interrupt, but it will just put the HBA to offline state
11269  * without passing any I/O traffic.
11270  *
11271  * Return codes
11272  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11273  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11274  */
11275 static pci_ers_result_t
lpfc_io_slot_reset_s3(struct pci_dev * pdev)11276 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
11277 {
11278 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11279 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11280 	struct lpfc_sli *psli = &phba->sli;
11281 	uint32_t intr_mode;
11282 
11283 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11284 	if (pci_enable_device_mem(pdev)) {
11285 		printk(KERN_ERR "lpfc: Cannot re-enable "
11286 			"PCI device after reset.\n");
11287 		return PCI_ERS_RESULT_DISCONNECT;
11288 	}
11289 
11290 	pci_restore_state(pdev);
11291 
11292 	/*
11293 	 * As the new kernel behavior of pci_restore_state() API call clears
11294 	 * device saved_state flag, need to save the restored state again.
11295 	 */
11296 	pci_save_state(pdev);
11297 
11298 	if (pdev->is_busmaster)
11299 		pci_set_master(pdev);
11300 
11301 	spin_lock_irq(&phba->hbalock);
11302 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11303 	spin_unlock_irq(&phba->hbalock);
11304 
11305 	/* Configure and enable interrupt */
11306 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
11307 	if (intr_mode == LPFC_INTR_ERROR) {
11308 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11309 				"0427 Cannot re-enable interrupt after "
11310 				"slot reset.\n");
11311 		return PCI_ERS_RESULT_DISCONNECT;
11312 	} else
11313 		phba->intr_mode = intr_mode;
11314 
11315 	/* Take device offline, it will perform cleanup */
11316 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11317 	lpfc_offline(phba);
11318 	lpfc_sli_brdrestart(phba);
11319 
11320 	/* Log the current active interrupt mode */
11321 	lpfc_log_intr_mode(phba, phba->intr_mode);
11322 
11323 	return PCI_ERS_RESULT_RECOVERED;
11324 }
11325 
11326 /**
11327  * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
11328  * @pdev: pointer to PCI device
11329  *
11330  * This routine is called from the PCI subsystem for error handling to device
11331  * with SLI-3 interface spec. It is called when kernel error recovery tells
11332  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11333  * error recovery. After this call, traffic can start to flow from this device
11334  * again.
11335  */
11336 static void
lpfc_io_resume_s3(struct pci_dev * pdev)11337 lpfc_io_resume_s3(struct pci_dev *pdev)
11338 {
11339 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11340 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11341 
11342 	/* Bring device online, it will be no-op for non-fatal error resume */
11343 	lpfc_online(phba);
11344 
11345 	/* Clean up Advanced Error Reporting (AER) if needed */
11346 	if (phba->hba_flag & HBA_AER_ENABLED)
11347 		pci_cleanup_aer_uncorrect_error_status(pdev);
11348 }
11349 
11350 /**
11351  * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
11352  * @phba: pointer to lpfc hba data structure.
11353  *
11354  * returns the number of ELS/CT IOCBs to reserve
11355  **/
11356 int
lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba * phba)11357 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11358 {
11359 	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11360 
11361 	if (phba->sli_rev == LPFC_SLI_REV4) {
11362 		if (max_xri <= 100)
11363 			return 10;
11364 		else if (max_xri <= 256)
11365 			return 25;
11366 		else if (max_xri <= 512)
11367 			return 50;
11368 		else if (max_xri <= 1024)
11369 			return 100;
11370 		else if (max_xri <= 1536)
11371 			return 150;
11372 		else if (max_xri <= 2048)
11373 			return 200;
11374 		else
11375 			return 250;
11376 	} else
11377 		return 0;
11378 }
11379 
11380 /**
11381  * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11382  * @phba: pointer to lpfc hba data structure.
11383  *
11384  * returns the number of ELS/CT + NVMET IOCBs to reserve
11385  **/
11386 int
lpfc_sli4_get_iocb_cnt(struct lpfc_hba * phba)11387 lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11388 {
11389 	int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11390 
11391 	if (phba->nvmet_support)
11392 		max_xri += LPFC_NVMET_BUF_POST;
11393 	return max_xri;
11394 }
11395 
11396 
11397 static void
lpfc_log_write_firmware_error(struct lpfc_hba * phba,uint32_t offset,uint32_t magic_number,uint32_t ftype,uint32_t fid,uint32_t fsize,const struct firmware * fw)11398 lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
11399 	uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
11400 	const struct firmware *fw)
11401 {
11402 	if ((offset == ADD_STATUS_FW_NOT_SUPPORTED) ||
11403 	    (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
11404 	     magic_number != MAGIC_NUMER_G6) ||
11405 	    (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
11406 	     magic_number != MAGIC_NUMER_G7))
11407 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11408 			"3030 This firmware version is not supported on "
11409 			"this HBA model. Device:%x Magic:%x Type:%x "
11410 			"ID:%x Size %d %zd\n",
11411 			phba->pcidev->device, magic_number, ftype, fid,
11412 			fsize, fw->size);
11413 	else
11414 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11415 			"3022 FW Download failed. Device:%x Magic:%x Type:%x "
11416 			"ID:%x Size %d %zd\n",
11417 			phba->pcidev->device, magic_number, ftype, fid,
11418 			fsize, fw->size);
11419 }
11420 
11421 
11422 /**
11423  * lpfc_write_firmware - attempt to write a firmware image to the port
11424  * @fw: pointer to firmware image returned from request_firmware.
11425  * @phba: pointer to lpfc hba data structure.
11426  *
11427  **/
11428 static void
lpfc_write_firmware(const struct firmware * fw,void * context)11429 lpfc_write_firmware(const struct firmware *fw, void *context)
11430 {
11431 	struct lpfc_hba *phba = (struct lpfc_hba *)context;
11432 	char fwrev[FW_REV_STR_SIZE];
11433 	struct lpfc_grp_hdr *image;
11434 	struct list_head dma_buffer_list;
11435 	int i, rc = 0;
11436 	struct lpfc_dmabuf *dmabuf, *next;
11437 	uint32_t offset = 0, temp_offset = 0;
11438 	uint32_t magic_number, ftype, fid, fsize;
11439 
11440 	/* It can be null in no-wait mode, sanity check */
11441 	if (!fw) {
11442 		rc = -ENXIO;
11443 		goto out;
11444 	}
11445 	image = (struct lpfc_grp_hdr *)fw->data;
11446 
11447 	magic_number = be32_to_cpu(image->magic_number);
11448 	ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11449 	fid = bf_get_be32(lpfc_grp_hdr_id, image);
11450 	fsize = be32_to_cpu(image->size);
11451 
11452 	INIT_LIST_HEAD(&dma_buffer_list);
11453 	lpfc_decode_firmware_rev(phba, fwrev, 1);
11454 	if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
11455 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11456 				"3023 Updating Firmware, Current Version:%s "
11457 				"New Version:%s\n",
11458 				fwrev, image->revision);
11459 		for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11460 			dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11461 					 GFP_KERNEL);
11462 			if (!dmabuf) {
11463 				rc = -ENOMEM;
11464 				goto release_out;
11465 			}
11466 			dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11467 							  SLI4_PAGE_SIZE,
11468 							  &dmabuf->phys,
11469 							  GFP_KERNEL);
11470 			if (!dmabuf->virt) {
11471 				kfree(dmabuf);
11472 				rc = -ENOMEM;
11473 				goto release_out;
11474 			}
11475 			list_add_tail(&dmabuf->list, &dma_buffer_list);
11476 		}
11477 		while (offset < fw->size) {
11478 			temp_offset = offset;
11479 			list_for_each_entry(dmabuf, &dma_buffer_list, list) {
11480 				if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
11481 					memcpy(dmabuf->virt,
11482 					       fw->data + temp_offset,
11483 					       fw->size - temp_offset);
11484 					temp_offset = fw->size;
11485 					break;
11486 				}
11487 				memcpy(dmabuf->virt, fw->data + temp_offset,
11488 				       SLI4_PAGE_SIZE);
11489 				temp_offset += SLI4_PAGE_SIZE;
11490 			}
11491 			rc = lpfc_wr_object(phba, &dma_buffer_list,
11492 				    (fw->size - offset), &offset);
11493 			if (rc) {
11494 				lpfc_log_write_firmware_error(phba, offset,
11495 					magic_number, ftype, fid, fsize, fw);
11496 				goto release_out;
11497 			}
11498 		}
11499 		rc = offset;
11500 	} else
11501 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11502 				"3029 Skipped Firmware update, Current "
11503 				"Version:%s New Version:%s\n",
11504 				fwrev, image->revision);
11505 
11506 release_out:
11507 	list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11508 		list_del(&dmabuf->list);
11509 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11510 				  dmabuf->virt, dmabuf->phys);
11511 		kfree(dmabuf);
11512 	}
11513 	release_firmware(fw);
11514 out:
11515 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11516 			"3024 Firmware update done: %d.\n", rc);
11517 	return;
11518 }
11519 
11520 /**
11521  * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11522  * @phba: pointer to lpfc hba data structure.
11523  *
11524  * This routine is called to perform Linux generic firmware upgrade on device
11525  * that supports such feature.
11526  **/
11527 int
lpfc_sli4_request_firmware_update(struct lpfc_hba * phba,uint8_t fw_upgrade)11528 lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11529 {
11530 	uint8_t file_name[ELX_MODEL_NAME_SIZE];
11531 	int ret;
11532 	const struct firmware *fw;
11533 
11534 	/* Only supported on SLI4 interface type 2 for now */
11535 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
11536 	    LPFC_SLI_INTF_IF_TYPE_2)
11537 		return -EPERM;
11538 
11539 	snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11540 
11541 	if (fw_upgrade == INT_FW_UPGRADE) {
11542 		ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11543 					file_name, &phba->pcidev->dev,
11544 					GFP_KERNEL, (void *)phba,
11545 					lpfc_write_firmware);
11546 	} else if (fw_upgrade == RUN_FW_UPGRADE) {
11547 		ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11548 		if (!ret)
11549 			lpfc_write_firmware(fw, (void *)phba);
11550 	} else {
11551 		ret = -EINVAL;
11552 	}
11553 
11554 	return ret;
11555 }
11556 
11557 /**
11558  * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11559  * @pdev: pointer to PCI device
11560  * @pid: pointer to PCI device identifier
11561  *
11562  * This routine is called from the kernel's PCI subsystem to device with
11563  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11564  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11565  * information of the device and driver to see if the driver state that it
11566  * can support this kind of device. If the match is successful, the driver
11567  * core invokes this routine. If this routine determines it can claim the HBA,
11568  * it does all the initialization that it needs to do to handle the HBA
11569  * properly.
11570  *
11571  * Return code
11572  * 	0 - driver can claim the device
11573  * 	negative value - driver can not claim the device
11574  **/
11575 static int
lpfc_pci_probe_one_s4(struct pci_dev * pdev,const struct pci_device_id * pid)11576 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11577 {
11578 	struct lpfc_hba   *phba;
11579 	struct lpfc_vport *vport = NULL;
11580 	struct Scsi_Host  *shost = NULL;
11581 	int error;
11582 	uint32_t cfg_mode, intr_mode;
11583 
11584 	/* Allocate memory for HBA structure */
11585 	phba = lpfc_hba_alloc(pdev);
11586 	if (!phba)
11587 		return -ENOMEM;
11588 
11589 	/* Perform generic PCI device enabling operation */
11590 	error = lpfc_enable_pci_dev(phba);
11591 	if (error)
11592 		goto out_free_phba;
11593 
11594 	/* Set up SLI API function jump table for PCI-device group-1 HBAs */
11595 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11596 	if (error)
11597 		goto out_disable_pci_dev;
11598 
11599 	/* Set up SLI-4 specific device PCI memory space */
11600 	error = lpfc_sli4_pci_mem_setup(phba);
11601 	if (error) {
11602 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11603 				"1410 Failed to set up pci memory space.\n");
11604 		goto out_disable_pci_dev;
11605 	}
11606 
11607 	/* Set up SLI-4 Specific device driver resources */
11608 	error = lpfc_sli4_driver_resource_setup(phba);
11609 	if (error) {
11610 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11611 				"1412 Failed to set up driver resource.\n");
11612 		goto out_unset_pci_mem_s4;
11613 	}
11614 
11615 	INIT_LIST_HEAD(&phba->active_rrq_list);
11616 	INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
11617 
11618 	/* Set up common device driver resources */
11619 	error = lpfc_setup_driver_resource_phase2(phba);
11620 	if (error) {
11621 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11622 				"1414 Failed to set up driver resource.\n");
11623 		goto out_unset_driver_resource_s4;
11624 	}
11625 
11626 	/* Get the default values for Model Name and Description */
11627 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11628 
11629 	/* Create SCSI host to the physical port */
11630 	error = lpfc_create_shost(phba);
11631 	if (error) {
11632 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11633 				"1415 Failed to create scsi host.\n");
11634 		goto out_unset_driver_resource;
11635 	}
11636 
11637 	/* Configure sysfs attributes */
11638 	vport = phba->pport;
11639 	error = lpfc_alloc_sysfs_attr(vport);
11640 	if (error) {
11641 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11642 				"1416 Failed to allocate sysfs attr\n");
11643 		goto out_destroy_shost;
11644 	}
11645 
11646 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
11647 	/* Now, trying to enable interrupt and bring up the device */
11648 	cfg_mode = phba->cfg_use_msi;
11649 
11650 	/* Put device to a known state before enabling interrupt */
11651 	lpfc_stop_port(phba);
11652 
11653 	/* Configure and enable interrupt */
11654 	intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11655 	if (intr_mode == LPFC_INTR_ERROR) {
11656 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11657 				"0426 Failed to enable interrupt.\n");
11658 		error = -ENODEV;
11659 		goto out_free_sysfs_attr;
11660 	}
11661 	/* Default to single EQ for non-MSI-X */
11662 	if (phba->intr_type != MSIX) {
11663 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11664 			phba->cfg_fcp_io_channel = 1;
11665 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11666 			phba->cfg_nvme_io_channel = 1;
11667 			if (phba->nvmet_support)
11668 				phba->cfg_nvmet_mrq = 1;
11669 		}
11670 		phba->io_channel_irqs = 1;
11671 	}
11672 
11673 	/* Set up SLI-4 HBA */
11674 	if (lpfc_sli4_hba_setup(phba)) {
11675 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11676 				"1421 Failed to set up hba\n");
11677 		error = -ENODEV;
11678 		goto out_disable_intr;
11679 	}
11680 
11681 	/* Log the current active interrupt mode */
11682 	phba->intr_mode = intr_mode;
11683 	lpfc_log_intr_mode(phba, intr_mode);
11684 
11685 	/* Perform post initialization setup */
11686 	lpfc_post_init_setup(phba);
11687 
11688 	/* NVME support in FW earlier in the driver load corrects the
11689 	 * FC4 type making a check for nvme_support unnecessary.
11690 	 */
11691 	if ((phba->nvmet_support == 0) &&
11692 	    (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11693 		/* Create NVME binding with nvme_fc_transport. This
11694 		 * ensures the vport is initialized.  If the localport
11695 		 * create fails, it should not unload the driver to
11696 		 * support field issues.
11697 		 */
11698 		error = lpfc_nvme_create_localport(vport);
11699 		if (error) {
11700 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11701 					"6004 NVME registration failed, "
11702 					"error x%x\n",
11703 					error);
11704 		}
11705 	}
11706 
11707 	/* check for firmware upgrade or downgrade */
11708 	if (phba->cfg_request_firmware_upgrade)
11709 		lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
11710 
11711 	/* Check if there are static vports to be created. */
11712 	lpfc_create_static_vport(phba);
11713 	return 0;
11714 
11715 out_disable_intr:
11716 	lpfc_sli4_disable_intr(phba);
11717 out_free_sysfs_attr:
11718 	lpfc_free_sysfs_attr(vport);
11719 out_destroy_shost:
11720 	lpfc_destroy_shost(phba);
11721 out_unset_driver_resource:
11722 	lpfc_unset_driver_resource_phase2(phba);
11723 out_unset_driver_resource_s4:
11724 	lpfc_sli4_driver_resource_unset(phba);
11725 out_unset_pci_mem_s4:
11726 	lpfc_sli4_pci_mem_unset(phba);
11727 out_disable_pci_dev:
11728 	lpfc_disable_pci_dev(phba);
11729 	if (shost)
11730 		scsi_host_put(shost);
11731 out_free_phba:
11732 	lpfc_hba_free(phba);
11733 	return error;
11734 }
11735 
11736 /**
11737  * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11738  * @pdev: pointer to PCI device
11739  *
11740  * This routine is called from the kernel's PCI subsystem to device with
11741  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11742  * removed from PCI bus, it performs all the necessary cleanup for the HBA
11743  * device to be removed from the PCI subsystem properly.
11744  **/
11745 static void
lpfc_pci_remove_one_s4(struct pci_dev * pdev)11746 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11747 {
11748 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11749 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11750 	struct lpfc_vport **vports;
11751 	struct lpfc_hba *phba = vport->phba;
11752 	int i;
11753 
11754 	/* Mark the device unloading flag */
11755 	spin_lock_irq(&phba->hbalock);
11756 	vport->load_flag |= FC_UNLOADING;
11757 	spin_unlock_irq(&phba->hbalock);
11758 
11759 	/* Free the HBA sysfs attributes */
11760 	lpfc_free_sysfs_attr(vport);
11761 
11762 	/* Release all the vports against this physical port */
11763 	vports = lpfc_create_vport_work_array(phba);
11764 	if (vports != NULL)
11765 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11766 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11767 				continue;
11768 			fc_vport_terminate(vports[i]->fc_vport);
11769 		}
11770 	lpfc_destroy_vport_work_array(phba, vports);
11771 
11772 	/* Remove FC host and then SCSI host with the physical port */
11773 	fc_remove_host(shost);
11774 	scsi_remove_host(shost);
11775 
11776 	/* Perform ndlp cleanup on the physical port.  The nvme and nvmet
11777 	 * localports are destroyed after to cleanup all transport memory.
11778 	 */
11779 	lpfc_cleanup(vport);
11780 	lpfc_nvmet_destroy_targetport(phba);
11781 	lpfc_nvme_destroy_localport(vport);
11782 
11783 	/*
11784 	 * Bring down the SLI Layer. This step disables all interrupts,
11785 	 * clears the rings, discards all mailbox commands, and resets
11786 	 * the HBA FCoE function.
11787 	 */
11788 	lpfc_debugfs_terminate(vport);
11789 	lpfc_sli4_hba_unset(phba);
11790 
11791 	lpfc_stop_hba_timers(phba);
11792 	spin_lock_irq(&phba->hbalock);
11793 	list_del_init(&vport->listentry);
11794 	spin_unlock_irq(&phba->hbalock);
11795 
11796 	/* Perform scsi free before driver resource_unset since scsi
11797 	 * buffers are released to their corresponding pools here.
11798 	 */
11799 	lpfc_scsi_free(phba);
11800 	lpfc_nvme_free(phba);
11801 	lpfc_free_iocb_list(phba);
11802 
11803 	lpfc_unset_driver_resource_phase2(phba);
11804 	lpfc_sli4_driver_resource_unset(phba);
11805 
11806 	/* Unmap adapter Control and Doorbell registers */
11807 	lpfc_sli4_pci_mem_unset(phba);
11808 
11809 	/* Release PCI resources and disable device's PCI function */
11810 	scsi_host_put(shost);
11811 	lpfc_disable_pci_dev(phba);
11812 
11813 	/* Finally, free the driver's device data structure */
11814 	lpfc_hba_free(phba);
11815 
11816 	return;
11817 }
11818 
11819 /**
11820  * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11821  * @pdev: pointer to PCI device
11822  * @msg: power management message
11823  *
11824  * This routine is called from the kernel's PCI subsystem to support system
11825  * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11826  * this method, it quiesces the device by stopping the driver's worker
11827  * thread for the device, turning off device's interrupt and DMA, and bring
11828  * the device offline. Note that as the driver implements the minimum PM
11829  * requirements to a power-aware driver's PM support for suspend/resume -- all
11830  * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11831  * method call will be treated as SUSPEND and the driver will fully
11832  * reinitialize its device during resume() method call, the driver will set
11833  * device to PCI_D3hot state in PCI config space instead of setting it
11834  * according to the @msg provided by the PM.
11835  *
11836  * Return code
11837  * 	0 - driver suspended the device
11838  * 	Error otherwise
11839  **/
11840 static int
lpfc_pci_suspend_one_s4(struct pci_dev * pdev,pm_message_t msg)11841 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11842 {
11843 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11844 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11845 
11846 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11847 			"2843 PCI device Power Management suspend.\n");
11848 
11849 	/* Bring down the device */
11850 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11851 	lpfc_offline(phba);
11852 	kthread_stop(phba->worker_thread);
11853 
11854 	/* Disable interrupt from device */
11855 	lpfc_sli4_disable_intr(phba);
11856 	lpfc_sli4_queue_destroy(phba);
11857 
11858 	/* Save device state to PCI config space */
11859 	pci_save_state(pdev);
11860 	pci_set_power_state(pdev, PCI_D3hot);
11861 
11862 	return 0;
11863 }
11864 
11865 /**
11866  * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11867  * @pdev: pointer to PCI device
11868  *
11869  * This routine is called from the kernel's PCI subsystem to support system
11870  * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11871  * this method, it restores the device's PCI config space state and fully
11872  * reinitializes the device and brings it online. Note that as the driver
11873  * implements the minimum PM requirements to a power-aware driver's PM for
11874  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11875  * to the suspend() method call will be treated as SUSPEND and the driver
11876  * will fully reinitialize its device during resume() method call, the device
11877  * will be set to PCI_D0 directly in PCI config space before restoring the
11878  * state.
11879  *
11880  * Return code
11881  * 	0 - driver suspended the device
11882  * 	Error otherwise
11883  **/
11884 static int
lpfc_pci_resume_one_s4(struct pci_dev * pdev)11885 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11886 {
11887 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11888 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11889 	uint32_t intr_mode;
11890 	int error;
11891 
11892 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11893 			"0292 PCI device Power Management resume.\n");
11894 
11895 	/* Restore device state from PCI config space */
11896 	pci_set_power_state(pdev, PCI_D0);
11897 	pci_restore_state(pdev);
11898 
11899 	/*
11900 	 * As the new kernel behavior of pci_restore_state() API call clears
11901 	 * device saved_state flag, need to save the restored state again.
11902 	 */
11903 	pci_save_state(pdev);
11904 
11905 	if (pdev->is_busmaster)
11906 		pci_set_master(pdev);
11907 
11908 	 /* Startup the kernel thread for this host adapter. */
11909 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
11910 					"lpfc_worker_%d", phba->brd_no);
11911 	if (IS_ERR(phba->worker_thread)) {
11912 		error = PTR_ERR(phba->worker_thread);
11913 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11914 				"0293 PM resume failed to start worker "
11915 				"thread: error=x%x.\n", error);
11916 		return error;
11917 	}
11918 
11919 	/* Configure and enable interrupt */
11920 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11921 	if (intr_mode == LPFC_INTR_ERROR) {
11922 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11923 				"0294 PM resume Failed to enable interrupt\n");
11924 		return -EIO;
11925 	} else
11926 		phba->intr_mode = intr_mode;
11927 
11928 	/* Restart HBA and bring it online */
11929 	lpfc_sli_brdrestart(phba);
11930 	lpfc_online(phba);
11931 
11932 	/* Log the current active interrupt mode */
11933 	lpfc_log_intr_mode(phba, phba->intr_mode);
11934 
11935 	return 0;
11936 }
11937 
11938 /**
11939  * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11940  * @phba: pointer to lpfc hba data structure.
11941  *
11942  * This routine is called to prepare the SLI4 device for PCI slot recover. It
11943  * aborts all the outstanding SCSI I/Os to the pci device.
11944  **/
11945 static void
lpfc_sli4_prep_dev_for_recover(struct lpfc_hba * phba)11946 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11947 {
11948 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11949 			"2828 PCI channel I/O abort preparing for recovery\n");
11950 	/*
11951 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11952 	 * and let the SCSI mid-layer to retry them to recover.
11953 	 */
11954 	lpfc_sli_abort_fcp_rings(phba);
11955 }
11956 
11957 /**
11958  * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11959  * @phba: pointer to lpfc hba data structure.
11960  *
11961  * This routine is called to prepare the SLI4 device for PCI slot reset. It
11962  * disables the device interrupt and pci device, and aborts the internal FCP
11963  * pending I/Os.
11964  **/
11965 static void
lpfc_sli4_prep_dev_for_reset(struct lpfc_hba * phba)11966 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11967 {
11968 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11969 			"2826 PCI channel disable preparing for reset\n");
11970 
11971 	/* Block any management I/Os to the device */
11972 	lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
11973 
11974 	/* Block all SCSI devices' I/Os on the host */
11975 	lpfc_scsi_dev_block(phba);
11976 
11977 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
11978 	lpfc_sli_flush_fcp_rings(phba);
11979 
11980 	/* Flush the outstanding NVME IOs if fc4 type enabled. */
11981 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11982 		lpfc_sli_flush_nvme_rings(phba);
11983 
11984 	/* stop all timers */
11985 	lpfc_stop_hba_timers(phba);
11986 
11987 	/* Disable interrupt and pci device */
11988 	lpfc_sli4_disable_intr(phba);
11989 	lpfc_sli4_queue_destroy(phba);
11990 	pci_disable_device(phba->pcidev);
11991 }
11992 
11993 /**
11994  * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11995  * @phba: pointer to lpfc hba data structure.
11996  *
11997  * This routine is called to prepare the SLI4 device for PCI slot permanently
11998  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11999  * pending I/Os.
12000  **/
12001 static void
lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba * phba)12002 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
12003 {
12004 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12005 			"2827 PCI channel permanent disable for failure\n");
12006 
12007 	/* Block all SCSI devices' I/Os on the host */
12008 	lpfc_scsi_dev_block(phba);
12009 
12010 	/* stop all timers */
12011 	lpfc_stop_hba_timers(phba);
12012 
12013 	/* Clean up all driver's outstanding SCSI I/Os */
12014 	lpfc_sli_flush_fcp_rings(phba);
12015 
12016 	/* Flush the outstanding NVME IOs if fc4 type enabled. */
12017 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12018 		lpfc_sli_flush_nvme_rings(phba);
12019 }
12020 
12021 /**
12022  * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
12023  * @pdev: pointer to PCI device.
12024  * @state: the current PCI connection state.
12025  *
12026  * This routine is called from the PCI subsystem for error handling to device
12027  * with SLI-4 interface spec. This function is called by the PCI subsystem
12028  * after a PCI bus error affecting this device has been detected. When this
12029  * function is invoked, it will need to stop all the I/Os and interrupt(s)
12030  * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
12031  * for the PCI subsystem to perform proper recovery as desired.
12032  *
12033  * Return codes
12034  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12035  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12036  **/
12037 static pci_ers_result_t
lpfc_io_error_detected_s4(struct pci_dev * pdev,pci_channel_state_t state)12038 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
12039 {
12040 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12041 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12042 
12043 	switch (state) {
12044 	case pci_channel_io_normal:
12045 		/* Non-fatal error, prepare for recovery */
12046 		lpfc_sli4_prep_dev_for_recover(phba);
12047 		return PCI_ERS_RESULT_CAN_RECOVER;
12048 	case pci_channel_io_frozen:
12049 		/* Fatal error, prepare for slot reset */
12050 		lpfc_sli4_prep_dev_for_reset(phba);
12051 		return PCI_ERS_RESULT_NEED_RESET;
12052 	case pci_channel_io_perm_failure:
12053 		/* Permanent failure, prepare for device down */
12054 		lpfc_sli4_prep_dev_for_perm_failure(phba);
12055 		return PCI_ERS_RESULT_DISCONNECT;
12056 	default:
12057 		/* Unknown state, prepare and request slot reset */
12058 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12059 				"2825 Unknown PCI error state: x%x\n", state);
12060 		lpfc_sli4_prep_dev_for_reset(phba);
12061 		return PCI_ERS_RESULT_NEED_RESET;
12062 	}
12063 }
12064 
12065 /**
12066  * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
12067  * @pdev: pointer to PCI device.
12068  *
12069  * This routine is called from the PCI subsystem for error handling to device
12070  * with SLI-4 interface spec. It is called after PCI bus has been reset to
12071  * restart the PCI card from scratch, as if from a cold-boot. During the
12072  * PCI subsystem error recovery, after the driver returns
12073  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12074  * recovery and then call this routine before calling the .resume method to
12075  * recover the device. This function will initialize the HBA device, enable
12076  * the interrupt, but it will just put the HBA to offline state without
12077  * passing any I/O traffic.
12078  *
12079  * Return codes
12080  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
12081  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12082  */
12083 static pci_ers_result_t
lpfc_io_slot_reset_s4(struct pci_dev * pdev)12084 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
12085 {
12086 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12087 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12088 	struct lpfc_sli *psli = &phba->sli;
12089 	uint32_t intr_mode;
12090 
12091 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
12092 	if (pci_enable_device_mem(pdev)) {
12093 		printk(KERN_ERR "lpfc: Cannot re-enable "
12094 			"PCI device after reset.\n");
12095 		return PCI_ERS_RESULT_DISCONNECT;
12096 	}
12097 
12098 	pci_restore_state(pdev);
12099 
12100 	/*
12101 	 * As the new kernel behavior of pci_restore_state() API call clears
12102 	 * device saved_state flag, need to save the restored state again.
12103 	 */
12104 	pci_save_state(pdev);
12105 
12106 	if (pdev->is_busmaster)
12107 		pci_set_master(pdev);
12108 
12109 	spin_lock_irq(&phba->hbalock);
12110 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
12111 	spin_unlock_irq(&phba->hbalock);
12112 
12113 	/* Configure and enable interrupt */
12114 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12115 	if (intr_mode == LPFC_INTR_ERROR) {
12116 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12117 				"2824 Cannot re-enable interrupt after "
12118 				"slot reset.\n");
12119 		return PCI_ERS_RESULT_DISCONNECT;
12120 	} else
12121 		phba->intr_mode = intr_mode;
12122 
12123 	/* Log the current active interrupt mode */
12124 	lpfc_log_intr_mode(phba, phba->intr_mode);
12125 
12126 	return PCI_ERS_RESULT_RECOVERED;
12127 }
12128 
12129 /**
12130  * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
12131  * @pdev: pointer to PCI device
12132  *
12133  * This routine is called from the PCI subsystem for error handling to device
12134  * with SLI-4 interface spec. It is called when kernel error recovery tells
12135  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12136  * error recovery. After this call, traffic can start to flow from this device
12137  * again.
12138  **/
12139 static void
lpfc_io_resume_s4(struct pci_dev * pdev)12140 lpfc_io_resume_s4(struct pci_dev *pdev)
12141 {
12142 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12143 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12144 
12145 	/*
12146 	 * In case of slot reset, as function reset is performed through
12147 	 * mailbox command which needs DMA to be enabled, this operation
12148 	 * has to be moved to the io resume phase. Taking device offline
12149 	 * will perform the necessary cleanup.
12150 	 */
12151 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
12152 		/* Perform device reset */
12153 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
12154 		lpfc_offline(phba);
12155 		lpfc_sli_brdrestart(phba);
12156 		/* Bring the device back online */
12157 		lpfc_online(phba);
12158 	}
12159 
12160 	/* Clean up Advanced Error Reporting (AER) if needed */
12161 	if (phba->hba_flag & HBA_AER_ENABLED)
12162 		pci_cleanup_aer_uncorrect_error_status(pdev);
12163 }
12164 
12165 /**
12166  * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
12167  * @pdev: pointer to PCI device
12168  * @pid: pointer to PCI device identifier
12169  *
12170  * This routine is to be registered to the kernel's PCI subsystem. When an
12171  * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
12172  * at PCI device-specific information of the device and driver to see if the
12173  * driver state that it can support this kind of device. If the match is
12174  * successful, the driver core invokes this routine. This routine dispatches
12175  * the action to the proper SLI-3 or SLI-4 device probing routine, which will
12176  * do all the initialization that it needs to do to handle the HBA device
12177  * properly.
12178  *
12179  * Return code
12180  * 	0 - driver can claim the device
12181  * 	negative value - driver can not claim the device
12182  **/
12183 static int
lpfc_pci_probe_one(struct pci_dev * pdev,const struct pci_device_id * pid)12184 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
12185 {
12186 	int rc;
12187 	struct lpfc_sli_intf intf;
12188 
12189 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
12190 		return -ENODEV;
12191 
12192 	if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
12193 	    (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
12194 		rc = lpfc_pci_probe_one_s4(pdev, pid);
12195 	else
12196 		rc = lpfc_pci_probe_one_s3(pdev, pid);
12197 
12198 	return rc;
12199 }
12200 
12201 /**
12202  * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
12203  * @pdev: pointer to PCI device
12204  *
12205  * This routine is to be registered to the kernel's PCI subsystem. When an
12206  * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
12207  * This routine dispatches the action to the proper SLI-3 or SLI-4 device
12208  * remove routine, which will perform all the necessary cleanup for the
12209  * device to be removed from the PCI subsystem properly.
12210  **/
12211 static void
lpfc_pci_remove_one(struct pci_dev * pdev)12212 lpfc_pci_remove_one(struct pci_dev *pdev)
12213 {
12214 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12215 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12216 
12217 	switch (phba->pci_dev_grp) {
12218 	case LPFC_PCI_DEV_LP:
12219 		lpfc_pci_remove_one_s3(pdev);
12220 		break;
12221 	case LPFC_PCI_DEV_OC:
12222 		lpfc_pci_remove_one_s4(pdev);
12223 		break;
12224 	default:
12225 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12226 				"1424 Invalid PCI device group: 0x%x\n",
12227 				phba->pci_dev_grp);
12228 		break;
12229 	}
12230 	return;
12231 }
12232 
12233 /**
12234  * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
12235  * @pdev: pointer to PCI device
12236  * @msg: power management message
12237  *
12238  * This routine is to be registered to the kernel's PCI subsystem to support
12239  * system Power Management (PM). When PM invokes this method, it dispatches
12240  * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
12241  * suspend the device.
12242  *
12243  * Return code
12244  * 	0 - driver suspended the device
12245  * 	Error otherwise
12246  **/
12247 static int
lpfc_pci_suspend_one(struct pci_dev * pdev,pm_message_t msg)12248 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
12249 {
12250 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12251 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12252 	int rc = -ENODEV;
12253 
12254 	switch (phba->pci_dev_grp) {
12255 	case LPFC_PCI_DEV_LP:
12256 		rc = lpfc_pci_suspend_one_s3(pdev, msg);
12257 		break;
12258 	case LPFC_PCI_DEV_OC:
12259 		rc = lpfc_pci_suspend_one_s4(pdev, msg);
12260 		break;
12261 	default:
12262 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12263 				"1425 Invalid PCI device group: 0x%x\n",
12264 				phba->pci_dev_grp);
12265 		break;
12266 	}
12267 	return rc;
12268 }
12269 
12270 /**
12271  * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
12272  * @pdev: pointer to PCI device
12273  *
12274  * This routine is to be registered to the kernel's PCI subsystem to support
12275  * system Power Management (PM). When PM invokes this method, it dispatches
12276  * the action to the proper SLI-3 or SLI-4 device resume routine, which will
12277  * resume the device.
12278  *
12279  * Return code
12280  * 	0 - driver suspended the device
12281  * 	Error otherwise
12282  **/
12283 static int
lpfc_pci_resume_one(struct pci_dev * pdev)12284 lpfc_pci_resume_one(struct pci_dev *pdev)
12285 {
12286 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12287 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12288 	int rc = -ENODEV;
12289 
12290 	switch (phba->pci_dev_grp) {
12291 	case LPFC_PCI_DEV_LP:
12292 		rc = lpfc_pci_resume_one_s3(pdev);
12293 		break;
12294 	case LPFC_PCI_DEV_OC:
12295 		rc = lpfc_pci_resume_one_s4(pdev);
12296 		break;
12297 	default:
12298 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12299 				"1426 Invalid PCI device group: 0x%x\n",
12300 				phba->pci_dev_grp);
12301 		break;
12302 	}
12303 	return rc;
12304 }
12305 
12306 /**
12307  * lpfc_io_error_detected - lpfc method for handling PCI I/O error
12308  * @pdev: pointer to PCI device.
12309  * @state: the current PCI connection state.
12310  *
12311  * This routine is registered to the PCI subsystem for error handling. This
12312  * function is called by the PCI subsystem after a PCI bus error affecting
12313  * this device has been detected. When this routine is invoked, it dispatches
12314  * the action to the proper SLI-3 or SLI-4 device error detected handling
12315  * routine, which will perform the proper error detected operation.
12316  *
12317  * Return codes
12318  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12319  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12320  **/
12321 static pci_ers_result_t
lpfc_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)12322 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
12323 {
12324 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12325 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12326 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12327 
12328 	switch (phba->pci_dev_grp) {
12329 	case LPFC_PCI_DEV_LP:
12330 		rc = lpfc_io_error_detected_s3(pdev, state);
12331 		break;
12332 	case LPFC_PCI_DEV_OC:
12333 		rc = lpfc_io_error_detected_s4(pdev, state);
12334 		break;
12335 	default:
12336 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12337 				"1427 Invalid PCI device group: 0x%x\n",
12338 				phba->pci_dev_grp);
12339 		break;
12340 	}
12341 	return rc;
12342 }
12343 
12344 /**
12345  * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
12346  * @pdev: pointer to PCI device.
12347  *
12348  * This routine is registered to the PCI subsystem for error handling. This
12349  * function is called after PCI bus has been reset to restart the PCI card
12350  * from scratch, as if from a cold-boot. When this routine is invoked, it
12351  * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
12352  * routine, which will perform the proper device reset.
12353  *
12354  * Return codes
12355  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
12356  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12357  **/
12358 static pci_ers_result_t
lpfc_io_slot_reset(struct pci_dev * pdev)12359 lpfc_io_slot_reset(struct pci_dev *pdev)
12360 {
12361 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12362 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12363 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12364 
12365 	switch (phba->pci_dev_grp) {
12366 	case LPFC_PCI_DEV_LP:
12367 		rc = lpfc_io_slot_reset_s3(pdev);
12368 		break;
12369 	case LPFC_PCI_DEV_OC:
12370 		rc = lpfc_io_slot_reset_s4(pdev);
12371 		break;
12372 	default:
12373 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12374 				"1428 Invalid PCI device group: 0x%x\n",
12375 				phba->pci_dev_grp);
12376 		break;
12377 	}
12378 	return rc;
12379 }
12380 
12381 /**
12382  * lpfc_io_resume - lpfc method for resuming PCI I/O operation
12383  * @pdev: pointer to PCI device
12384  *
12385  * This routine is registered to the PCI subsystem for error handling. It
12386  * is called when kernel error recovery tells the lpfc driver that it is
12387  * OK to resume normal PCI operation after PCI bus error recovery. When
12388  * this routine is invoked, it dispatches the action to the proper SLI-3
12389  * or SLI-4 device io_resume routine, which will resume the device operation.
12390  **/
12391 static void
lpfc_io_resume(struct pci_dev * pdev)12392 lpfc_io_resume(struct pci_dev *pdev)
12393 {
12394 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12395 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12396 
12397 	switch (phba->pci_dev_grp) {
12398 	case LPFC_PCI_DEV_LP:
12399 		lpfc_io_resume_s3(pdev);
12400 		break;
12401 	case LPFC_PCI_DEV_OC:
12402 		lpfc_io_resume_s4(pdev);
12403 		break;
12404 	default:
12405 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12406 				"1429 Invalid PCI device group: 0x%x\n",
12407 				phba->pci_dev_grp);
12408 		break;
12409 	}
12410 	return;
12411 }
12412 
12413 /**
12414  * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12415  * @phba: pointer to lpfc hba data structure.
12416  *
12417  * This routine checks to see if OAS is supported for this adapter. If
12418  * supported, the configure Flash Optimized Fabric flag is set.  Otherwise,
12419  * the enable oas flag is cleared and the pool created for OAS device data
12420  * is destroyed.
12421  *
12422  **/
12423 void
lpfc_sli4_oas_verify(struct lpfc_hba * phba)12424 lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12425 {
12426 
12427 	if (!phba->cfg_EnableXLane)
12428 		return;
12429 
12430 	if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12431 		phba->cfg_fof = 1;
12432 	} else {
12433 		phba->cfg_fof = 0;
12434 		if (phba->device_data_mem_pool)
12435 			mempool_destroy(phba->device_data_mem_pool);
12436 		phba->device_data_mem_pool = NULL;
12437 	}
12438 
12439 	return;
12440 }
12441 
12442 /**
12443  * lpfc_fof_queue_setup - Set up all the fof queues
12444  * @phba: pointer to lpfc hba data structure.
12445  *
12446  * This routine is invoked to set up all the fof queues for the FC HBA
12447  * operation.
12448  *
12449  * Return codes
12450  *      0 - successful
12451  *      -ENOMEM - No available memory
12452  **/
12453 int
lpfc_fof_queue_setup(struct lpfc_hba * phba)12454 lpfc_fof_queue_setup(struct lpfc_hba *phba)
12455 {
12456 	struct lpfc_sli_ring *pring;
12457 	int rc;
12458 
12459 	rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12460 	if (rc)
12461 		return -ENOMEM;
12462 
12463 	if (phba->cfg_fof) {
12464 
12465 		rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12466 				    phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12467 		if (rc)
12468 			goto out_oas_cq;
12469 
12470 		rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12471 				    phba->sli4_hba.oas_cq, LPFC_FCP);
12472 		if (rc)
12473 			goto out_oas_wq;
12474 
12475 		/* Bind this CQ/WQ to the NVME ring */
12476 		pring = phba->sli4_hba.oas_wq->pring;
12477 		pring->sli.sli4.wqp =
12478 			(void *)phba->sli4_hba.oas_wq;
12479 		phba->sli4_hba.oas_cq->pring = pring;
12480 	}
12481 
12482 	return 0;
12483 
12484 out_oas_wq:
12485 	lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
12486 out_oas_cq:
12487 	lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12488 	return rc;
12489 
12490 }
12491 
12492 /**
12493  * lpfc_fof_queue_create - Create all the fof queues
12494  * @phba: pointer to lpfc hba data structure.
12495  *
12496  * This routine is invoked to allocate all the fof queues for the FC HBA
12497  * operation. For each SLI4 queue type, the parameters such as queue entry
12498  * count (queue depth) shall be taken from the module parameter. For now,
12499  * we just use some constant number as place holder.
12500  *
12501  * Return codes
12502  *      0 - successful
12503  *      -ENOMEM - No availble memory
12504  *      -EIO - The mailbox failed to complete successfully.
12505  **/
12506 int
lpfc_fof_queue_create(struct lpfc_hba * phba)12507 lpfc_fof_queue_create(struct lpfc_hba *phba)
12508 {
12509 	struct lpfc_queue *qdesc;
12510 	uint32_t wqesize;
12511 
12512 	/* Create FOF EQ */
12513 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
12514 				      phba->sli4_hba.eq_esize,
12515 				      phba->sli4_hba.eq_ecount);
12516 	if (!qdesc)
12517 		goto out_error;
12518 
12519 	qdesc->qe_valid = 1;
12520 	phba->sli4_hba.fof_eq = qdesc;
12521 
12522 	if (phba->cfg_fof) {
12523 
12524 		/* Create OAS CQ */
12525 		if (phba->enab_exp_wqcq_pages)
12526 			qdesc = lpfc_sli4_queue_alloc(phba,
12527 						      LPFC_EXPANDED_PAGE_SIZE,
12528 						      phba->sli4_hba.cq_esize,
12529 						      LPFC_CQE_EXP_COUNT);
12530 		else
12531 			qdesc = lpfc_sli4_queue_alloc(phba,
12532 						      LPFC_DEFAULT_PAGE_SIZE,
12533 						      phba->sli4_hba.cq_esize,
12534 						      phba->sli4_hba.cq_ecount);
12535 		if (!qdesc)
12536 			goto out_error;
12537 
12538 		qdesc->qe_valid = 1;
12539 		phba->sli4_hba.oas_cq = qdesc;
12540 
12541 		/* Create OAS WQ */
12542 		if (phba->enab_exp_wqcq_pages) {
12543 			wqesize = (phba->fcp_embed_io) ?
12544 				LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12545 			qdesc = lpfc_sli4_queue_alloc(phba,
12546 						      LPFC_EXPANDED_PAGE_SIZE,
12547 						      wqesize,
12548 						      LPFC_WQE_EXP_COUNT);
12549 		} else
12550 			qdesc = lpfc_sli4_queue_alloc(phba,
12551 						      LPFC_DEFAULT_PAGE_SIZE,
12552 						      phba->sli4_hba.wq_esize,
12553 						      phba->sli4_hba.wq_ecount);
12554 
12555 		if (!qdesc)
12556 			goto out_error;
12557 
12558 		phba->sli4_hba.oas_wq = qdesc;
12559 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
12560 
12561 	}
12562 	return 0;
12563 
12564 out_error:
12565 	lpfc_fof_queue_destroy(phba);
12566 	return -ENOMEM;
12567 }
12568 
12569 /**
12570  * lpfc_fof_queue_destroy - Destroy all the fof queues
12571  * @phba: pointer to lpfc hba data structure.
12572  *
12573  * This routine is invoked to release all the SLI4 queues with the FC HBA
12574  * operation.
12575  *
12576  * Return codes
12577  *      0 - successful
12578  **/
12579 int
lpfc_fof_queue_destroy(struct lpfc_hba * phba)12580 lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12581 {
12582 	/* Release FOF Event queue */
12583 	if (phba->sli4_hba.fof_eq != NULL) {
12584 		lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12585 		phba->sli4_hba.fof_eq = NULL;
12586 	}
12587 
12588 	/* Release OAS Completion queue */
12589 	if (phba->sli4_hba.oas_cq != NULL) {
12590 		lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12591 		phba->sli4_hba.oas_cq = NULL;
12592 	}
12593 
12594 	/* Release OAS Work queue */
12595 	if (phba->sli4_hba.oas_wq != NULL) {
12596 		lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12597 		phba->sli4_hba.oas_wq = NULL;
12598 	}
12599 	return 0;
12600 }
12601 
12602 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12603 
12604 static const struct pci_error_handlers lpfc_err_handler = {
12605 	.error_detected = lpfc_io_error_detected,
12606 	.slot_reset = lpfc_io_slot_reset,
12607 	.resume = lpfc_io_resume,
12608 };
12609 
12610 static struct pci_driver lpfc_driver = {
12611 	.name		= LPFC_DRIVER_NAME,
12612 	.id_table	= lpfc_id_table,
12613 	.probe		= lpfc_pci_probe_one,
12614 	.remove		= lpfc_pci_remove_one,
12615 	.shutdown	= lpfc_pci_remove_one,
12616 	.suspend        = lpfc_pci_suspend_one,
12617 	.resume		= lpfc_pci_resume_one,
12618 	.err_handler    = &lpfc_err_handler,
12619 };
12620 
12621 static const struct file_operations lpfc_mgmt_fop = {
12622 	.owner = THIS_MODULE,
12623 };
12624 
12625 static struct miscdevice lpfc_mgmt_dev = {
12626 	.minor = MISC_DYNAMIC_MINOR,
12627 	.name = "lpfcmgmt",
12628 	.fops = &lpfc_mgmt_fop,
12629 };
12630 
12631 /**
12632  * lpfc_init - lpfc module initialization routine
12633  *
12634  * This routine is to be invoked when the lpfc module is loaded into the
12635  * kernel. The special kernel macro module_init() is used to indicate the
12636  * role of this routine to the kernel as lpfc module entry point.
12637  *
12638  * Return codes
12639  *   0 - successful
12640  *   -ENOMEM - FC attach transport failed
12641  *   all others - failed
12642  */
12643 static int __init
lpfc_init(void)12644 lpfc_init(void)
12645 {
12646 	int error = 0;
12647 
12648 	printk(LPFC_MODULE_DESC "\n");
12649 	printk(LPFC_COPYRIGHT "\n");
12650 
12651 	error = misc_register(&lpfc_mgmt_dev);
12652 	if (error)
12653 		printk(KERN_ERR "Could not register lpfcmgmt device, "
12654 			"misc_register returned with status %d", error);
12655 
12656 	lpfc_transport_functions.vport_create = lpfc_vport_create;
12657 	lpfc_transport_functions.vport_delete = lpfc_vport_delete;
12658 	lpfc_transport_template =
12659 				fc_attach_transport(&lpfc_transport_functions);
12660 	if (lpfc_transport_template == NULL)
12661 		return -ENOMEM;
12662 	lpfc_vport_transport_template =
12663 		fc_attach_transport(&lpfc_vport_transport_functions);
12664 	if (lpfc_vport_transport_template == NULL) {
12665 		fc_release_transport(lpfc_transport_template);
12666 		return -ENOMEM;
12667 	}
12668 	lpfc_nvme_cmd_template();
12669 	lpfc_nvmet_cmd_template();
12670 
12671 	/* Initialize in case vector mapping is needed */
12672 	lpfc_used_cpu = NULL;
12673 	lpfc_present_cpu = num_present_cpus();
12674 
12675 	error = pci_register_driver(&lpfc_driver);
12676 	if (error) {
12677 		fc_release_transport(lpfc_transport_template);
12678 		fc_release_transport(lpfc_vport_transport_template);
12679 	}
12680 
12681 	return error;
12682 }
12683 
12684 /**
12685  * lpfc_exit - lpfc module removal routine
12686  *
12687  * This routine is invoked when the lpfc module is removed from the kernel.
12688  * The special kernel macro module_exit() is used to indicate the role of
12689  * this routine to the kernel as lpfc module exit point.
12690  */
12691 static void __exit
lpfc_exit(void)12692 lpfc_exit(void)
12693 {
12694 	misc_deregister(&lpfc_mgmt_dev);
12695 	pci_unregister_driver(&lpfc_driver);
12696 	fc_release_transport(lpfc_transport_template);
12697 	fc_release_transport(lpfc_vport_transport_template);
12698 	if (_dump_buf_data) {
12699 		printk(KERN_ERR	"9062 BLKGRD: freeing %lu pages for "
12700 				"_dump_buf_data at 0x%p\n",
12701 				(1L << _dump_buf_data_order), _dump_buf_data);
12702 		free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12703 	}
12704 
12705 	if (_dump_buf_dif) {
12706 		printk(KERN_ERR	"9049 BLKGRD: freeing %lu pages for "
12707 				"_dump_buf_dif at 0x%p\n",
12708 				(1L << _dump_buf_dif_order), _dump_buf_dif);
12709 		free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12710 	}
12711 	kfree(lpfc_used_cpu);
12712 	idr_destroy(&lpfc_hba_index);
12713 }
12714 
12715 module_init(lpfc_init);
12716 module_exit(lpfc_exit);
12717 MODULE_LICENSE("GPL");
12718 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
12719 MODULE_AUTHOR("Broadcom");
12720 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
12721