xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_mbox.c (revision da60fbe7)
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/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi.h>
32 #include <scsi/fc/fc_fs.h>
33 
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_compat.h"
45 
46 /**
47  * lpfc_dump_static_vport - Dump HBA's static vport information.
48  * @phba: pointer to lpfc hba data structure.
49  * @pmb: pointer to the driver internal queue element for mailbox command.
50  * @offset: offset for dumping vport info.
51  *
52  * The dump mailbox command provides a method for the device driver to obtain
53  * various types of information from the HBA device.
54  *
55  * This routine prepares the mailbox command for dumping list of static
56  * vports to be created.
57  **/
58 int
59 lpfc_dump_static_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
60 		uint16_t offset)
61 {
62 	MAILBOX_t *mb;
63 	struct lpfc_dmabuf *mp;
64 
65 	mb = &pmb->u.mb;
66 
67 	/* Setup to dump vport info region */
68 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
69 	mb->mbxCommand = MBX_DUMP_MEMORY;
70 	mb->un.varDmp.type = DMP_NV_PARAMS;
71 	mb->un.varDmp.entry_index = offset;
72 	mb->un.varDmp.region_id = DMP_REGION_VPORT;
73 	mb->mbxOwner = OWN_HOST;
74 
75 	/* For SLI3 HBAs data is embedded in mailbox */
76 	if (phba->sli_rev != LPFC_SLI_REV4) {
77 		mb->un.varDmp.cv = 1;
78 		mb->un.varDmp.word_cnt = DMP_RSP_SIZE/sizeof(uint32_t);
79 		return 0;
80 	}
81 
82 	/* For SLI4 HBAs driver need to allocate memory */
83 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
84 	if (mp)
85 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
86 
87 	if (!mp || !mp->virt) {
88 		kfree(mp);
89 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
90 			"2605 lpfc_dump_static_vport: memory"
91 			" allocation failed\n");
92 		return 1;
93 	}
94 	memset(mp->virt, 0, LPFC_BPL_SIZE);
95 	INIT_LIST_HEAD(&mp->list);
96 	/* save address for completion */
97 	pmb->ctx_buf = (uint8_t *)mp;
98 	mb->un.varWords[3] = putPaddrLow(mp->phys);
99 	mb->un.varWords[4] = putPaddrHigh(mp->phys);
100 	mb->un.varDmp.sli4_length = sizeof(struct static_vport_info);
101 
102 	return 0;
103 }
104 
105 /**
106  * lpfc_down_link - Bring down HBAs link.
107  * @phba: pointer to lpfc hba data structure.
108  * @pmb: pointer to the driver internal queue element for mailbox command.
109  *
110  * This routine prepares a mailbox command to bring down HBA link.
111  **/
112 void
113 lpfc_down_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
114 {
115 	MAILBOX_t *mb;
116 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
117 	mb = &pmb->u.mb;
118 	mb->mbxCommand = MBX_DOWN_LINK;
119 	mb->mbxOwner = OWN_HOST;
120 }
121 
122 /**
123  * lpfc_dump_mem - Prepare a mailbox command for reading a region.
124  * @phba: pointer to lpfc hba data structure.
125  * @pmb: pointer to the driver internal queue element for mailbox command.
126  * @offset: offset into the region.
127  * @region_id: config region id.
128  *
129  * The dump mailbox command provides a method for the device driver to obtain
130  * various types of information from the HBA device.
131  *
132  * This routine prepares the mailbox command for dumping HBA's config region.
133  **/
134 void
135 lpfc_dump_mem(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, uint16_t offset,
136 		uint16_t region_id)
137 {
138 	MAILBOX_t *mb;
139 	void *ctx;
140 
141 	mb = &pmb->u.mb;
142 	ctx = pmb->ctx_buf;
143 
144 	/* Setup to dump VPD region */
145 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
146 	mb->mbxCommand = MBX_DUMP_MEMORY;
147 	mb->un.varDmp.cv = 1;
148 	mb->un.varDmp.type = DMP_NV_PARAMS;
149 	mb->un.varDmp.entry_index = offset;
150 	mb->un.varDmp.region_id = region_id;
151 	mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
152 	mb->un.varDmp.co = 0;
153 	mb->un.varDmp.resp_offset = 0;
154 	pmb->ctx_buf = ctx;
155 	mb->mbxOwner = OWN_HOST;
156 	return;
157 }
158 
159 /**
160  * lpfc_dump_wakeup_param - Prepare mailbox command for retrieving wakeup params
161  * @phba: pointer to lpfc hba data structure.
162  * @pmb: pointer to the driver internal queue element for mailbox command.
163  *
164  * This function create a dump memory mailbox command to dump wake up
165  * parameters.
166  */
167 void
168 lpfc_dump_wakeup_param(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
169 {
170 	MAILBOX_t *mb;
171 	void *ctx;
172 
173 	mb = &pmb->u.mb;
174 	/* Save context so that we can restore after memset */
175 	ctx = pmb->ctx_buf;
176 
177 	/* Setup to dump VPD region */
178 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
179 	mb->mbxCommand = MBX_DUMP_MEMORY;
180 	mb->mbxOwner = OWN_HOST;
181 	mb->un.varDmp.cv = 1;
182 	mb->un.varDmp.type = DMP_NV_PARAMS;
183 	if (phba->sli_rev < LPFC_SLI_REV4)
184 		mb->un.varDmp.entry_index = 0;
185 	mb->un.varDmp.region_id = WAKE_UP_PARMS_REGION_ID;
186 	mb->un.varDmp.word_cnt = WAKE_UP_PARMS_WORD_SIZE;
187 	mb->un.varDmp.co = 0;
188 	mb->un.varDmp.resp_offset = 0;
189 	pmb->ctx_buf = ctx;
190 	return;
191 }
192 
193 /**
194  * lpfc_read_nv - Prepare a mailbox command for reading HBA's NVRAM param
195  * @phba: pointer to lpfc hba data structure.
196  * @pmb: pointer to the driver internal queue element for mailbox command.
197  *
198  * The read NVRAM mailbox command returns the HBA's non-volatile parameters
199  * that are used as defaults when the Fibre Channel link is brought on-line.
200  *
201  * This routine prepares the mailbox command for reading information stored
202  * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
203  **/
204 void
205 lpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
206 {
207 	MAILBOX_t *mb;
208 
209 	mb = &pmb->u.mb;
210 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
211 	mb->mbxCommand = MBX_READ_NV;
212 	mb->mbxOwner = OWN_HOST;
213 	return;
214 }
215 
216 /**
217  * lpfc_config_async - Prepare a mailbox command for enabling HBA async event
218  * @phba: pointer to lpfc hba data structure.
219  * @pmb: pointer to the driver internal queue element for mailbox command.
220  * @ring: ring number for the asynchronous event to be configured.
221  *
222  * The asynchronous event enable mailbox command is used to enable the
223  * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
224  * specifies the default ring to which events are posted.
225  *
226  * This routine prepares the mailbox command for enabling HBA asynchronous
227  * event support on a IOCB ring.
228  **/
229 void
230 lpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
231 		uint32_t ring)
232 {
233 	MAILBOX_t *mb;
234 
235 	mb = &pmb->u.mb;
236 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
237 	mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
238 	mb->un.varCfgAsyncEvent.ring = ring;
239 	mb->mbxOwner = OWN_HOST;
240 	return;
241 }
242 
243 /**
244  * lpfc_heart_beat - Prepare a mailbox command for heart beat
245  * @phba: pointer to lpfc hba data structure.
246  * @pmb: pointer to the driver internal queue element for mailbox command.
247  *
248  * The heart beat mailbox command is used to detect an unresponsive HBA, which
249  * is defined as any device where no error attention is sent and both mailbox
250  * and rings are not processed.
251  *
252  * This routine prepares the mailbox command for issuing a heart beat in the
253  * form of mailbox command to the HBA. The timely completion of the heart
254  * beat mailbox command indicates the health of the HBA.
255  **/
256 void
257 lpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
258 {
259 	MAILBOX_t *mb;
260 
261 	mb = &pmb->u.mb;
262 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
263 	mb->mbxCommand = MBX_HEARTBEAT;
264 	mb->mbxOwner = OWN_HOST;
265 	return;
266 }
267 
268 /**
269  * lpfc_read_topology - Prepare a mailbox command for reading HBA topology
270  * @phba: pointer to lpfc hba data structure.
271  * @pmb: pointer to the driver internal queue element for mailbox command.
272  * @mp: DMA buffer memory for reading the link attention information into.
273  *
274  * The read topology mailbox command is issued to read the link topology
275  * information indicated by the HBA port when the Link Event bit of the Host
276  * Attention (HSTATT) register is set to 1 (For SLI-3) or when an FC Link
277  * Attention ACQE is received from the port (For SLI-4). A Link Event
278  * Attention occurs based on an exception detected at the Fibre Channel link
279  * interface.
280  *
281  * This routine prepares the mailbox command for reading HBA link topology
282  * information. A DMA memory has been set aside and address passed to the
283  * HBA through @mp for the HBA to DMA link attention information into the
284  * memory as part of the execution of the mailbox command.
285  *
286  * Return codes
287  *    0 - Success (currently always return 0)
288  **/
289 int
290 lpfc_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
291 		   struct lpfc_dmabuf *mp)
292 {
293 	MAILBOX_t *mb;
294 
295 	mb = &pmb->u.mb;
296 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
297 
298 	INIT_LIST_HEAD(&mp->list);
299 	mb->mbxCommand = MBX_READ_TOPOLOGY;
300 	mb->un.varReadTop.lilpBde64.tus.f.bdeSize = LPFC_ALPA_MAP_SIZE;
301 	mb->un.varReadTop.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
302 	mb->un.varReadTop.lilpBde64.addrLow = putPaddrLow(mp->phys);
303 
304 	/* Save address for later completion and set the owner to host so that
305 	 * the FW knows this mailbox is available for processing.
306 	 */
307 	pmb->ctx_buf = (uint8_t *)mp;
308 	mb->mbxOwner = OWN_HOST;
309 	return (0);
310 }
311 
312 /**
313  * lpfc_clear_la - Prepare a mailbox command for clearing HBA link attention
314  * @phba: pointer to lpfc hba data structure.
315  * @pmb: pointer to the driver internal queue element for mailbox command.
316  *
317  * The clear link attention mailbox command is issued to clear the link event
318  * attention condition indicated by the Link Event bit of the Host Attention
319  * (HSTATT) register. The link event attention condition is cleared only if
320  * the event tag specified matches that of the current link event counter.
321  * The current event tag is read using the read link attention event mailbox
322  * command.
323  *
324  * This routine prepares the mailbox command for clearing HBA link attention
325  * information.
326  **/
327 void
328 lpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
329 {
330 	MAILBOX_t *mb;
331 
332 	mb = &pmb->u.mb;
333 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
334 
335 	mb->un.varClearLA.eventTag = phba->fc_eventTag;
336 	mb->mbxCommand = MBX_CLEAR_LA;
337 	mb->mbxOwner = OWN_HOST;
338 	return;
339 }
340 
341 /**
342  * lpfc_config_link - Prepare a mailbox command for configuring link on a HBA
343  * @phba: pointer to lpfc hba data structure.
344  * @pmb: pointer to the driver internal queue element for mailbox command.
345  *
346  * The configure link mailbox command is used before the initialize link
347  * mailbox command to override default value and to configure link-oriented
348  * parameters such as DID address and various timers. Typically, this
349  * command would be used after an F_Port login to set the returned DID address
350  * and the fabric timeout values. This command is not valid before a configure
351  * port command has configured the HBA port.
352  *
353  * This routine prepares the mailbox command for configuring link on a HBA.
354  **/
355 void
356 lpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
357 {
358 	struct lpfc_vport  *vport = phba->pport;
359 	MAILBOX_t *mb = &pmb->u.mb;
360 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
361 
362 	/* NEW_FEATURE
363 	 * SLI-2, Coalescing Response Feature.
364 	 */
365 	if (phba->cfg_cr_delay && (phba->sli_rev < LPFC_SLI_REV4)) {
366 		mb->un.varCfgLnk.cr = 1;
367 		mb->un.varCfgLnk.ci = 1;
368 		mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
369 		mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
370 	}
371 
372 	mb->un.varCfgLnk.myId = vport->fc_myDID;
373 	mb->un.varCfgLnk.edtov = phba->fc_edtov;
374 	mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
375 	mb->un.varCfgLnk.ratov = phba->fc_ratov;
376 	mb->un.varCfgLnk.rttov = phba->fc_rttov;
377 	mb->un.varCfgLnk.altov = phba->fc_altov;
378 	mb->un.varCfgLnk.crtov = phba->fc_crtov;
379 	mb->un.varCfgLnk.cscn = 0;
380 	if (phba->bbcredit_support && phba->cfg_enable_bbcr) {
381 		mb->un.varCfgLnk.cscn = 1;
382 		mb->un.varCfgLnk.bbscn = bf_get(lpfc_bbscn_def,
383 						 &phba->sli4_hba.bbscn_params);
384 	}
385 
386 	if (phba->cfg_ack0 && (phba->sli_rev < LPFC_SLI_REV4))
387 		mb->un.varCfgLnk.ack0_enable = 1;
388 
389 	mb->mbxCommand = MBX_CONFIG_LINK;
390 	mb->mbxOwner = OWN_HOST;
391 	return;
392 }
393 
394 /**
395  * lpfc_config_msi - Prepare a mailbox command for configuring msi-x
396  * @phba: pointer to lpfc hba data structure.
397  * @pmb: pointer to the driver internal queue element for mailbox command.
398  *
399  * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
400  * MSI-X multi-message interrupt vector association to interrupt attention
401  * conditions.
402  *
403  * Return codes
404  *    0 - Success
405  *    -EINVAL - Failure
406  **/
407 int
408 lpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
409 {
410 	MAILBOX_t *mb = &pmb->u.mb;
411 	uint32_t attentionConditions[2];
412 
413 	/* Sanity check */
414 	if (phba->cfg_use_msi != 2) {
415 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
416 				"0475 Not configured for supporting MSI-X "
417 				"cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
418 		return -EINVAL;
419 	}
420 
421 	if (phba->sli_rev < 3) {
422 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
423 				"0476 HBA not supporting SLI-3 or later "
424 				"SLI Revision: 0x%x\n", phba->sli_rev);
425 		return -EINVAL;
426 	}
427 
428 	/* Clear mailbox command fields */
429 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
430 
431 	/*
432 	 * SLI-3, Message Signaled Interrupt Fearure.
433 	 */
434 
435 	/* Multi-message attention configuration */
436 	attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
437 				  HA_LATT | HA_MBATT);
438 	attentionConditions[1] = 0;
439 
440 	mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
441 	mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
442 
443 	/*
444 	 * Set up message number to HA bit association
445 	 */
446 #ifdef __BIG_ENDIAN_BITFIELD
447 	/* RA0 (FCP Ring) */
448 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
449 	/* RA1 (Other Protocol Extra Ring) */
450 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
451 #else   /*  __LITTLE_ENDIAN_BITFIELD */
452 	/* RA0 (FCP Ring) */
453 	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
454 	/* RA1 (Other Protocol Extra Ring) */
455 	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
456 #endif
457 	/* Multi-message interrupt autoclear configuration*/
458 	mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
459 	mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
460 
461 	/* For now, HBA autoclear does not work reliably, disable it */
462 	mb->un.varCfgMSI.autoClearHA[0] = 0;
463 	mb->un.varCfgMSI.autoClearHA[1] = 0;
464 
465 	/* Set command and owner bit */
466 	mb->mbxCommand = MBX_CONFIG_MSI;
467 	mb->mbxOwner = OWN_HOST;
468 
469 	return 0;
470 }
471 
472 /**
473  * lpfc_init_link - Prepare a mailbox command for initialize link on a HBA
474  * @phba: pointer to lpfc hba data structure.
475  * @pmb: pointer to the driver internal queue element for mailbox command.
476  * @topology: the link topology for the link to be initialized to.
477  * @linkspeed: the link speed for the link to be initialized to.
478  *
479  * The initialize link mailbox command is used to initialize the Fibre
480  * Channel link. This command must follow a configure port command that
481  * establishes the mode of operation.
482  *
483  * This routine prepares the mailbox command for initializing link on a HBA
484  * with the specified link topology and speed.
485  **/
486 void
487 lpfc_init_link(struct lpfc_hba * phba,
488 	       LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
489 {
490 	lpfc_vpd_t *vpd;
491 	MAILBOX_t *mb;
492 
493 	mb = &pmb->u.mb;
494 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
495 
496 	switch (topology) {
497 	case FLAGS_TOPOLOGY_MODE_LOOP_PT:
498 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
499 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
500 		break;
501 	case FLAGS_TOPOLOGY_MODE_PT_PT:
502 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
503 		break;
504 	case FLAGS_TOPOLOGY_MODE_LOOP:
505 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
506 		break;
507 	case FLAGS_TOPOLOGY_MODE_PT_LOOP:
508 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
509 		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
510 		break;
511 	case FLAGS_LOCAL_LB:
512 		mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
513 		break;
514 	}
515 
516 	if ((phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC ||
517 	     phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC) &&
518 	    !(phba->sli4_hba.pc_sli4_params.pls) &&
519 	    mb->un.varInitLnk.link_flags & FLAGS_TOPOLOGY_MODE_LOOP) {
520 		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
521 		phba->cfg_topology = FLAGS_TOPOLOGY_MODE_PT_PT;
522 	}
523 
524 	/* Enable asynchronous ABTS responses from firmware */
525 	mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
526 
527 	/* NEW_FEATURE
528 	 * Setting up the link speed
529 	 */
530 	vpd = &phba->vpd;
531 	if (vpd->rev.feaLevelHigh >= 0x02){
532 		switch(linkspeed){
533 		case LPFC_USER_LINK_SPEED_1G:
534 			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
535 			mb->un.varInitLnk.link_speed = LINK_SPEED_1G;
536 			break;
537 		case LPFC_USER_LINK_SPEED_2G:
538 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
539 			mb->un.varInitLnk.link_speed = LINK_SPEED_2G;
540 			break;
541 		case LPFC_USER_LINK_SPEED_4G:
542 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
543 			mb->un.varInitLnk.link_speed = LINK_SPEED_4G;
544 			break;
545 		case LPFC_USER_LINK_SPEED_8G:
546 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
547 			mb->un.varInitLnk.link_speed = LINK_SPEED_8G;
548 			break;
549 		case LPFC_USER_LINK_SPEED_10G:
550 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
551 			mb->un.varInitLnk.link_speed = LINK_SPEED_10G;
552 			break;
553 		case LPFC_USER_LINK_SPEED_16G:
554 			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
555 			mb->un.varInitLnk.link_speed = LINK_SPEED_16G;
556 			break;
557 		case LPFC_USER_LINK_SPEED_32G:
558 			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
559 			mb->un.varInitLnk.link_speed = LINK_SPEED_32G;
560 			break;
561 		case LPFC_USER_LINK_SPEED_64G:
562 			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
563 			mb->un.varInitLnk.link_speed = LINK_SPEED_64G;
564 			break;
565 		case LPFC_USER_LINK_SPEED_AUTO:
566 		default:
567 			mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
568 			break;
569 		}
570 
571 	}
572 	else
573 		mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
574 
575 	mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
576 	mb->mbxOwner = OWN_HOST;
577 	mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
578 	return;
579 }
580 
581 /**
582  * lpfc_read_sparam - Prepare a mailbox command for reading HBA parameters
583  * @phba: pointer to lpfc hba data structure.
584  * @pmb: pointer to the driver internal queue element for mailbox command.
585  * @vpi: virtual N_Port identifier.
586  *
587  * The read service parameter mailbox command is used to read the HBA port
588  * service parameters. The service parameters are read into the buffer
589  * specified directly by a BDE in the mailbox command. These service
590  * parameters may then be used to build the payload of an N_Port/F_POrt
591  * login request and reply (LOGI/ACC).
592  *
593  * This routine prepares the mailbox command for reading HBA port service
594  * parameters. The DMA memory is allocated in this function and the addresses
595  * are populated into the mailbox command for the HBA to DMA the service
596  * parameters into.
597  *
598  * Return codes
599  *    0 - Success
600  *    1 - DMA memory allocation failed
601  **/
602 int
603 lpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
604 {
605 	struct lpfc_dmabuf *mp;
606 	MAILBOX_t *mb;
607 
608 	mb = &pmb->u.mb;
609 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
610 
611 	mb->mbxOwner = OWN_HOST;
612 
613 	/* Get a buffer to hold the HBAs Service Parameters */
614 
615 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
616 	if (mp)
617 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
618 	if (!mp || !mp->virt) {
619 		kfree(mp);
620 		mb->mbxCommand = MBX_READ_SPARM64;
621 		/* READ_SPARAM: no buffers */
622 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
623 			        "0301 READ_SPARAM: no buffers\n");
624 		return (1);
625 	}
626 	INIT_LIST_HEAD(&mp->list);
627 	mb->mbxCommand = MBX_READ_SPARM64;
628 	mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
629 	mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
630 	mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
631 	if (phba->sli_rev >= LPFC_SLI_REV3)
632 		mb->un.varRdSparm.vpi = phba->vpi_ids[vpi];
633 
634 	/* save address for completion */
635 	pmb->ctx_buf = mp;
636 
637 	return (0);
638 }
639 
640 /**
641  * lpfc_unreg_did - Prepare a mailbox command for unregistering DID
642  * @phba: pointer to lpfc hba data structure.
643  * @vpi: virtual N_Port identifier.
644  * @did: remote port identifier.
645  * @pmb: pointer to the driver internal queue element for mailbox command.
646  *
647  * The unregister DID mailbox command is used to unregister an N_Port/F_Port
648  * login for an unknown RPI by specifying the DID of a remote port. This
649  * command frees an RPI context in the HBA port. This has the effect of
650  * performing an implicit N_Port/F_Port logout.
651  *
652  * This routine prepares the mailbox command for unregistering a remote
653  * N_Port/F_Port (DID) login.
654  **/
655 void
656 lpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
657 	       LPFC_MBOXQ_t * pmb)
658 {
659 	MAILBOX_t *mb;
660 
661 	mb = &pmb->u.mb;
662 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
663 
664 	mb->un.varUnregDID.did = did;
665 	mb->un.varUnregDID.vpi = vpi;
666 	if ((vpi != 0xffff) &&
667 	    (phba->sli_rev == LPFC_SLI_REV4))
668 		mb->un.varUnregDID.vpi = phba->vpi_ids[vpi];
669 
670 	mb->mbxCommand = MBX_UNREG_D_ID;
671 	mb->mbxOwner = OWN_HOST;
672 	return;
673 }
674 
675 /**
676  * lpfc_read_config - Prepare a mailbox command for reading HBA configuration
677  * @phba: pointer to lpfc hba data structure.
678  * @pmb: pointer to the driver internal queue element for mailbox command.
679  *
680  * The read configuration mailbox command is used to read the HBA port
681  * configuration parameters. This mailbox command provides a method for
682  * seeing any parameters that may have changed via various configuration
683  * mailbox commands.
684  *
685  * This routine prepares the mailbox command for reading out HBA configuration
686  * parameters.
687  **/
688 void
689 lpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
690 {
691 	MAILBOX_t *mb;
692 
693 	mb = &pmb->u.mb;
694 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
695 
696 	mb->mbxCommand = MBX_READ_CONFIG;
697 	mb->mbxOwner = OWN_HOST;
698 	return;
699 }
700 
701 /**
702  * lpfc_read_lnk_stat - Prepare a mailbox command for reading HBA link stats
703  * @phba: pointer to lpfc hba data structure.
704  * @pmb: pointer to the driver internal queue element for mailbox command.
705  *
706  * The read link status mailbox command is used to read the link status from
707  * the HBA. Link status includes all link-related error counters. These
708  * counters are maintained by the HBA and originated in the link hardware
709  * unit. Note that all of these counters wrap.
710  *
711  * This routine prepares the mailbox command for reading out HBA link status.
712  **/
713 void
714 lpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
715 {
716 	MAILBOX_t *mb;
717 
718 	mb = &pmb->u.mb;
719 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
720 
721 	mb->mbxCommand = MBX_READ_LNK_STAT;
722 	mb->mbxOwner = OWN_HOST;
723 	return;
724 }
725 
726 /**
727  * lpfc_reg_rpi - Prepare a mailbox command for registering remote login
728  * @phba: pointer to lpfc hba data structure.
729  * @vpi: virtual N_Port identifier.
730  * @did: remote port identifier.
731  * @param: pointer to memory holding the server parameters.
732  * @pmb: pointer to the driver internal queue element for mailbox command.
733  * @rpi: the rpi to use in the registration (usually only used for SLI4.
734  *
735  * The registration login mailbox command is used to register an N_Port or
736  * F_Port login. This registration allows the HBA to cache the remote N_Port
737  * service parameters internally and thereby make the appropriate FC-2
738  * decisions. The remote port service parameters are handed off by the driver
739  * to the HBA using a descriptor entry that directly identifies a buffer in
740  * host memory. In exchange, the HBA returns an RPI identifier.
741  *
742  * This routine prepares the mailbox command for registering remote port login.
743  * The function allocates DMA buffer for passing the service parameters to the
744  * HBA with the mailbox command.
745  *
746  * Return codes
747  *    0 - Success
748  *    1 - DMA memory allocation failed
749  **/
750 int
751 lpfc_reg_rpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
752 	     uint8_t *param, LPFC_MBOXQ_t *pmb, uint16_t rpi)
753 {
754 	MAILBOX_t *mb = &pmb->u.mb;
755 	uint8_t *sparam;
756 	struct lpfc_dmabuf *mp;
757 
758 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
759 
760 	mb->un.varRegLogin.rpi = 0;
761 	if (phba->sli_rev == LPFC_SLI_REV4)
762 		mb->un.varRegLogin.rpi = phba->sli4_hba.rpi_ids[rpi];
763 	if (phba->sli_rev >= LPFC_SLI_REV3)
764 		mb->un.varRegLogin.vpi = phba->vpi_ids[vpi];
765 	mb->un.varRegLogin.did = did;
766 	mb->mbxOwner = OWN_HOST;
767 	/* Get a buffer to hold NPorts Service Parameters */
768 	mp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
769 	if (mp)
770 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
771 	if (!mp || !mp->virt) {
772 		kfree(mp);
773 		mb->mbxCommand = MBX_REG_LOGIN64;
774 		/* REG_LOGIN: no buffers */
775 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
776 				"0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
777 				"rpi x%x\n", vpi, did, rpi);
778 		return 1;
779 	}
780 	INIT_LIST_HEAD(&mp->list);
781 	sparam = mp->virt;
782 
783 	/* Copy param's into a new buffer */
784 	memcpy(sparam, param, sizeof (struct serv_parm));
785 
786 	/* save address for completion */
787 	pmb->ctx_buf = (uint8_t *)mp;
788 
789 	mb->mbxCommand = MBX_REG_LOGIN64;
790 	mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
791 	mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
792 	mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
793 
794 	return 0;
795 }
796 
797 /**
798  * lpfc_unreg_login - Prepare a mailbox command for unregistering remote login
799  * @phba: pointer to lpfc hba data structure.
800  * @vpi: virtual N_Port identifier.
801  * @rpi: remote port identifier
802  * @pmb: pointer to the driver internal queue element for mailbox command.
803  *
804  * The unregistration login mailbox command is used to unregister an N_Port
805  * or F_Port login. This command frees an RPI context in the HBA. It has the
806  * effect of performing an implicit N_Port/F_Port logout.
807  *
808  * This routine prepares the mailbox command for unregistering remote port
809  * login.
810  *
811  * For SLI4 ports, the rpi passed to this function must be the physical
812  * rpi value, not the logical index.
813  **/
814 void
815 lpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
816 		 LPFC_MBOXQ_t * pmb)
817 {
818 	MAILBOX_t *mb;
819 
820 	mb = &pmb->u.mb;
821 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
822 
823 	mb->un.varUnregLogin.rpi = rpi;
824 	mb->un.varUnregLogin.rsvd1 = 0;
825 	if (phba->sli_rev >= LPFC_SLI_REV3)
826 		mb->un.varUnregLogin.vpi = phba->vpi_ids[vpi];
827 
828 	mb->mbxCommand = MBX_UNREG_LOGIN;
829 	mb->mbxOwner = OWN_HOST;
830 
831 	return;
832 }
833 
834 /**
835  * lpfc_sli4_unreg_all_rpis - unregister all RPIs for a vport on SLI4 HBA.
836  * @vport: pointer to a vport object.
837  *
838  * This routine sends mailbox command to unregister all active RPIs for
839  * a vport.
840  **/
841 void
842 lpfc_sli4_unreg_all_rpis(struct lpfc_vport *vport)
843 {
844 	struct lpfc_hba  *phba  = vport->phba;
845 	LPFC_MBOXQ_t     *mbox;
846 	int rc;
847 
848 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
849 	if (mbox) {
850 		/*
851 		 * For SLI4 functions, the rpi field is overloaded for
852 		 * the vport context unreg all.  This routine passes
853 		 * 0 for the rpi field in lpfc_unreg_login for compatibility
854 		 * with SLI3 and then overrides the rpi field with the
855 		 * expected value for SLI4.
856 		 */
857 		lpfc_unreg_login(phba, vport->vpi, phba->vpi_ids[vport->vpi],
858 				 mbox);
859 		mbox->u.mb.un.varUnregLogin.rsvd1 = 0x4000;
860 		mbox->vport = vport;
861 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
862 		mbox->ctx_ndlp = NULL;
863 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
864 		if (rc == MBX_NOT_FINISHED)
865 			mempool_free(mbox, phba->mbox_mem_pool);
866 	}
867 }
868 
869 /**
870  * lpfc_reg_vpi - Prepare a mailbox command for registering vport identifier
871  * @phba: pointer to lpfc hba data structure.
872  * @vpi: virtual N_Port identifier.
873  * @sid: Fibre Channel S_ID (N_Port_ID assigned to a virtual N_Port).
874  * @pmb: pointer to the driver internal queue element for mailbox command.
875  *
876  * The registration vport identifier mailbox command is used to activate a
877  * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
878  * N_Port_ID against the information in the selected virtual N_Port context
879  * block and marks it active to allow normal processing of IOCB commands and
880  * received unsolicited exchanges.
881  *
882  * This routine prepares the mailbox command for registering a virtual N_Port.
883  **/
884 void
885 lpfc_reg_vpi(struct lpfc_vport *vport, LPFC_MBOXQ_t *pmb)
886 {
887 	MAILBOX_t *mb = &pmb->u.mb;
888 	struct lpfc_hba *phba = vport->phba;
889 
890 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
891 	/*
892 	 * Set the re-reg VPI bit for f/w to update the MAC address.
893 	 */
894 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
895 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI))
896 		mb->un.varRegVpi.upd = 1;
897 
898 	mb->un.varRegVpi.vpi = phba->vpi_ids[vport->vpi];
899 	mb->un.varRegVpi.sid = vport->fc_myDID;
900 	if (phba->sli_rev == LPFC_SLI_REV4)
901 		mb->un.varRegVpi.vfi = phba->sli4_hba.vfi_ids[vport->vfi];
902 	else
903 		mb->un.varRegVpi.vfi = vport->vfi + vport->phba->vfi_base;
904 	memcpy(mb->un.varRegVpi.wwn, &vport->fc_portname,
905 	       sizeof(struct lpfc_name));
906 	mb->un.varRegVpi.wwn[0] = cpu_to_le32(mb->un.varRegVpi.wwn[0]);
907 	mb->un.varRegVpi.wwn[1] = cpu_to_le32(mb->un.varRegVpi.wwn[1]);
908 
909 	mb->mbxCommand = MBX_REG_VPI;
910 	mb->mbxOwner = OWN_HOST;
911 	return;
912 
913 }
914 
915 /**
916  * lpfc_unreg_vpi - Prepare a mailbox command for unregistering vport id
917  * @phba: pointer to lpfc hba data structure.
918  * @vpi: virtual N_Port identifier.
919  * @pmb: pointer to the driver internal queue element for mailbox command.
920  *
921  * The unregistration vport identifier mailbox command is used to inactivate
922  * a virtual N_Port. The driver must have logged out and unregistered all
923  * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
924  * unregisters any default RPIs associated with the specified vpi, aborting
925  * any active exchanges. The HBA will post the mailbox response after making
926  * the virtual N_Port inactive.
927  *
928  * This routine prepares the mailbox command for unregistering a virtual
929  * N_Port.
930  **/
931 void
932 lpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
933 {
934 	MAILBOX_t *mb = &pmb->u.mb;
935 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
936 
937 	if (phba->sli_rev == LPFC_SLI_REV3)
938 		mb->un.varUnregVpi.vpi = phba->vpi_ids[vpi];
939 	else if (phba->sli_rev >= LPFC_SLI_REV4)
940 		mb->un.varUnregVpi.sli4_vpi = phba->vpi_ids[vpi];
941 
942 	mb->mbxCommand = MBX_UNREG_VPI;
943 	mb->mbxOwner = OWN_HOST;
944 	return;
945 
946 }
947 
948 /**
949  * lpfc_config_pcb_setup - Set up IOCB rings in the Port Control Block (PCB)
950  * @phba: pointer to lpfc hba data structure.
951  *
952  * This routine sets up and initializes the IOCB rings in the Port Control
953  * Block (PCB).
954  **/
955 static void
956 lpfc_config_pcb_setup(struct lpfc_hba * phba)
957 {
958 	struct lpfc_sli *psli = &phba->sli;
959 	struct lpfc_sli_ring *pring;
960 	PCB_t *pcbp = phba->pcb;
961 	dma_addr_t pdma_addr;
962 	uint32_t offset;
963 	uint32_t iocbCnt = 0;
964 	int i;
965 
966 	pcbp->maxRing = (psli->num_rings - 1);
967 
968 	for (i = 0; i < psli->num_rings; i++) {
969 		pring = &psli->sli3_ring[i];
970 
971 		pring->sli.sli3.sizeCiocb =
972 			phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE :
973 							SLI2_IOCB_CMD_SIZE;
974 		pring->sli.sli3.sizeRiocb =
975 			phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE :
976 							SLI2_IOCB_RSP_SIZE;
977 		/* A ring MUST have both cmd and rsp entries defined to be
978 		   valid */
979 		if ((pring->sli.sli3.numCiocb == 0) ||
980 			(pring->sli.sli3.numRiocb == 0)) {
981 			pcbp->rdsc[i].cmdEntries = 0;
982 			pcbp->rdsc[i].rspEntries = 0;
983 			pcbp->rdsc[i].cmdAddrHigh = 0;
984 			pcbp->rdsc[i].rspAddrHigh = 0;
985 			pcbp->rdsc[i].cmdAddrLow = 0;
986 			pcbp->rdsc[i].rspAddrLow = 0;
987 			pring->sli.sli3.cmdringaddr = NULL;
988 			pring->sli.sli3.rspringaddr = NULL;
989 			continue;
990 		}
991 		/* Command ring setup for ring */
992 		pring->sli.sli3.cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
993 		pcbp->rdsc[i].cmdEntries = pring->sli.sli3.numCiocb;
994 
995 		offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
996 			 (uint8_t *) phba->slim2p.virt;
997 		pdma_addr = phba->slim2p.phys + offset;
998 		pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
999 		pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
1000 		iocbCnt += pring->sli.sli3.numCiocb;
1001 
1002 		/* Response ring setup for ring */
1003 		pring->sli.sli3.rspringaddr = (void *) &phba->IOCBs[iocbCnt];
1004 
1005 		pcbp->rdsc[i].rspEntries = pring->sli.sli3.numRiocb;
1006 		offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
1007 			 (uint8_t *)phba->slim2p.virt;
1008 		pdma_addr = phba->slim2p.phys + offset;
1009 		pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
1010 		pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
1011 		iocbCnt += pring->sli.sli3.numRiocb;
1012 	}
1013 }
1014 
1015 /**
1016  * lpfc_read_rev - Prepare a mailbox command for reading HBA revision
1017  * @phba: pointer to lpfc hba data structure.
1018  * @pmb: pointer to the driver internal queue element for mailbox command.
1019  *
1020  * The read revision mailbox command is used to read the revision levels of
1021  * the HBA components. These components include hardware units, resident
1022  * firmware, and available firmware. HBAs that supports SLI-3 mode of
1023  * operation provide different response information depending on the version
1024  * requested by the driver.
1025  *
1026  * This routine prepares the mailbox command for reading HBA revision
1027  * information.
1028  **/
1029 void
1030 lpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1031 {
1032 	MAILBOX_t *mb = &pmb->u.mb;
1033 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1034 	mb->un.varRdRev.cv = 1;
1035 	mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
1036 	mb->mbxCommand = MBX_READ_REV;
1037 	mb->mbxOwner = OWN_HOST;
1038 	return;
1039 }
1040 
1041 void
1042 lpfc_sli4_swap_str(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1043 {
1044 	MAILBOX_t *mb = &pmb->u.mb;
1045 	struct lpfc_mqe *mqe;
1046 
1047 	switch (mb->mbxCommand) {
1048 	case  MBX_READ_REV:
1049 		 mqe = &pmb->u.mqe;
1050 		lpfc_sli_pcimem_bcopy(mqe->un.read_rev.fw_name,
1051 				 mqe->un.read_rev.fw_name, 16);
1052 		lpfc_sli_pcimem_bcopy(mqe->un.read_rev.ulp_fw_name,
1053 				 mqe->un.read_rev.ulp_fw_name, 16);
1054 		break;
1055 	default:
1056 		break;
1057 	}
1058 	return;
1059 }
1060 
1061 /**
1062  * lpfc_build_hbq_profile2 - Set up the HBQ Selection Profile 2
1063  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1064  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1065  *
1066  * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
1067  * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
1068  * the Sequence Length Test using the fields in the Selection Profile 2
1069  * extension in words 20:31.
1070  **/
1071 static void
1072 lpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
1073 			struct lpfc_hbq_init  *hbq_desc)
1074 {
1075 	hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
1076 	hbqmb->profiles.profile2.maxlen     = hbq_desc->maxlen;
1077 	hbqmb->profiles.profile2.seqlenoff  = hbq_desc->seqlenoff;
1078 }
1079 
1080 /**
1081  * lpfc_build_hbq_profile3 - Set up the HBQ Selection Profile 3
1082  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1083  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1084  *
1085  * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
1086  * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
1087  * the Sequence Length Test and Byte Field Test using the fields in the
1088  * Selection Profile 3 extension in words 20:31.
1089  **/
1090 static void
1091 lpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
1092 			struct lpfc_hbq_init  *hbq_desc)
1093 {
1094 	hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
1095 	hbqmb->profiles.profile3.maxlen     = hbq_desc->maxlen;
1096 	hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
1097 	hbqmb->profiles.profile3.seqlenoff  = hbq_desc->seqlenoff;
1098 	memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
1099 	       sizeof(hbqmb->profiles.profile3.cmdmatch));
1100 }
1101 
1102 /**
1103  * lpfc_build_hbq_profile5 - Set up the HBQ Selection Profile 5
1104  * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
1105  * @hbq_desc: pointer to the HBQ selection profile descriptor.
1106  *
1107  * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
1108  * HBA tests the initial frame of an incoming sequence using the frame's
1109  * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
1110  * and Byte Field Test using the fields in the Selection Profile 5 extension
1111  * words 20:31.
1112  **/
1113 static void
1114 lpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
1115 			struct lpfc_hbq_init  *hbq_desc)
1116 {
1117 	hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
1118 	hbqmb->profiles.profile5.maxlen     = hbq_desc->maxlen;
1119 	hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
1120 	hbqmb->profiles.profile5.seqlenoff  = hbq_desc->seqlenoff;
1121 	memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
1122 	       sizeof(hbqmb->profiles.profile5.cmdmatch));
1123 }
1124 
1125 /**
1126  * lpfc_config_hbq - Prepare a mailbox command for configuring an HBQ
1127  * @phba: pointer to lpfc hba data structure.
1128  * @id: HBQ identifier.
1129  * @hbq_desc: pointer to the HBA descriptor data structure.
1130  * @hbq_entry_index: index of the HBQ entry data structures.
1131  * @pmb: pointer to the driver internal queue element for mailbox command.
1132  *
1133  * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
1134  * an HBQ. The configuration binds events that require buffers to a particular
1135  * ring and HBQ based on a selection profile.
1136  *
1137  * This routine prepares the mailbox command for configuring an HBQ.
1138  **/
1139 void
1140 lpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
1141 		 struct lpfc_hbq_init *hbq_desc,
1142 		uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
1143 {
1144 	int i;
1145 	MAILBOX_t *mb = &pmb->u.mb;
1146 	struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
1147 
1148 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1149 	hbqmb->hbqId = id;
1150 	hbqmb->entry_count = hbq_desc->entry_count;   /* # entries in HBQ */
1151 	hbqmb->recvNotify = hbq_desc->rn;             /* Receive
1152 						       * Notification */
1153 	hbqmb->numMask    = hbq_desc->mask_count;     /* # R_CTL/TYPE masks
1154 						       * # in words 0-19 */
1155 	hbqmb->profile    = hbq_desc->profile;	      /* Selection profile:
1156 						       * 0 = all,
1157 						       * 7 = logentry */
1158 	hbqmb->ringMask   = hbq_desc->ring_mask;      /* Binds HBQ to a ring
1159 						       * e.g. Ring0=b0001,
1160 						       * ring2=b0100 */
1161 	hbqmb->headerLen  = hbq_desc->headerLen;      /* 0 if not profile 4
1162 						       * or 5 */
1163 	hbqmb->logEntry   = hbq_desc->logEntry;       /* Set to 1 if this
1164 						       * HBQ will be used
1165 						       * for LogEntry
1166 						       * buffers */
1167 	hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
1168 		hbq_entry_index * sizeof(struct lpfc_hbq_entry);
1169 	hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
1170 
1171 	mb->mbxCommand = MBX_CONFIG_HBQ;
1172 	mb->mbxOwner = OWN_HOST;
1173 
1174 				/* Copy info for profiles 2,3,5. Other
1175 				 * profiles this area is reserved
1176 				 */
1177 	if (hbq_desc->profile == 2)
1178 		lpfc_build_hbq_profile2(hbqmb, hbq_desc);
1179 	else if (hbq_desc->profile == 3)
1180 		lpfc_build_hbq_profile3(hbqmb, hbq_desc);
1181 	else if (hbq_desc->profile == 5)
1182 		lpfc_build_hbq_profile5(hbqmb, hbq_desc);
1183 
1184 	/* Return if no rctl / type masks for this HBQ */
1185 	if (!hbq_desc->mask_count)
1186 		return;
1187 
1188 	/* Otherwise we setup specific rctl / type masks for this HBQ */
1189 	for (i = 0; i < hbq_desc->mask_count; i++) {
1190 		hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
1191 		hbqmb->hbqMasks[i].tmask  = hbq_desc->hbqMasks[i].tmask;
1192 		hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
1193 		hbqmb->hbqMasks[i].rctlmask  = hbq_desc->hbqMasks[i].rctlmask;
1194 	}
1195 
1196 	return;
1197 }
1198 
1199 /**
1200  * lpfc_config_ring - Prepare a mailbox command for configuring an IOCB ring
1201  * @phba: pointer to lpfc hba data structure.
1202  * @ring:
1203  * @pmb: pointer to the driver internal queue element for mailbox command.
1204  *
1205  * The configure ring mailbox command is used to configure an IOCB ring. This
1206  * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
1207  * ring. This is used to map incoming sequences to a particular ring whose
1208  * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
1209  * attempt to configure a ring whose number is greater than the number
1210  * specified in the Port Control Block (PCB). It is an error to issue the
1211  * configure ring command more than once with the same ring number. The HBA
1212  * returns an error if the driver attempts this.
1213  *
1214  * This routine prepares the mailbox command for configuring IOCB ring.
1215  **/
1216 void
1217 lpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
1218 {
1219 	int i;
1220 	MAILBOX_t *mb = &pmb->u.mb;
1221 	struct lpfc_sli *psli;
1222 	struct lpfc_sli_ring *pring;
1223 
1224 	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
1225 
1226 	mb->un.varCfgRing.ring = ring;
1227 	mb->un.varCfgRing.maxOrigXchg = 0;
1228 	mb->un.varCfgRing.maxRespXchg = 0;
1229 	mb->un.varCfgRing.recvNotify = 1;
1230 
1231 	psli = &phba->sli;
1232 	pring = &psli->sli3_ring[ring];
1233 	mb->un.varCfgRing.numMask = pring->num_mask;
1234 	mb->mbxCommand = MBX_CONFIG_RING;
1235 	mb->mbxOwner = OWN_HOST;
1236 
1237 	/* Is this ring configured for a specific profile */
1238 	if (pring->prt[0].profile) {
1239 		mb->un.varCfgRing.profile = pring->prt[0].profile;
1240 		return;
1241 	}
1242 
1243 	/* Otherwise we setup specific rctl / type masks for this ring */
1244 	for (i = 0; i < pring->num_mask; i++) {
1245 		mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
1246 		if (mb->un.varCfgRing.rrRegs[i].rval != FC_RCTL_ELS_REQ)
1247 			mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
1248 		else
1249 			mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
1250 		mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
1251 		mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
1252 	}
1253 
1254 	return;
1255 }
1256 
1257 /**
1258  * lpfc_config_port - Prepare a mailbox command for configuring port
1259  * @phba: pointer to lpfc hba data structure.
1260  * @pmb: pointer to the driver internal queue element for mailbox command.
1261  *
1262  * The configure port mailbox command is used to identify the Port Control
1263  * Block (PCB) in the driver memory. After this command is issued, the
1264  * driver must not access the mailbox in the HBA without first resetting
1265  * the HBA. The HBA may copy the PCB information to internal storage for
1266  * subsequent use; the driver can not change the PCB information unless it
1267  * resets the HBA.
1268  *
1269  * This routine prepares the mailbox command for configuring port.
1270  **/
1271 void
1272 lpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1273 {
1274 	MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
1275 	MAILBOX_t *mb = &pmb->u.mb;
1276 	dma_addr_t pdma_addr;
1277 	uint32_t bar_low, bar_high;
1278 	size_t offset;
1279 	struct lpfc_hgp hgp;
1280 	int i;
1281 	uint32_t pgp_offset;
1282 
1283 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1284 	mb->mbxCommand = MBX_CONFIG_PORT;
1285 	mb->mbxOwner = OWN_HOST;
1286 
1287 	mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
1288 
1289 	offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
1290 	pdma_addr = phba->slim2p.phys + offset;
1291 	mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
1292 	mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
1293 
1294 	/* Always Host Group Pointer is in SLIM */
1295 	mb->un.varCfgPort.hps = 1;
1296 
1297 	/* If HBA supports SLI=3 ask for it */
1298 
1299 	if (phba->sli_rev == LPFC_SLI_REV3 && phba->vpd.sli3Feat.cerbm) {
1300 		if (phba->cfg_enable_bg)
1301 			mb->un.varCfgPort.cbg = 1; /* configure BlockGuard */
1302 		if (phba->cfg_enable_dss)
1303 			mb->un.varCfgPort.cdss = 1; /* Configure Security */
1304 		mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
1305 		mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
1306 		mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
1307 		if (phba->max_vpi && phba->cfg_enable_npiv &&
1308 		    phba->vpd.sli3Feat.cmv) {
1309 			mb->un.varCfgPort.max_vpi = LPFC_MAX_VPI;
1310 			mb->un.varCfgPort.cmv = 1;
1311 		} else
1312 			mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
1313 	} else
1314 		phba->sli_rev = LPFC_SLI_REV2;
1315 	mb->un.varCfgPort.sli_mode = phba->sli_rev;
1316 
1317 	/* If this is an SLI3 port, configure async status notification. */
1318 	if (phba->sli_rev == LPFC_SLI_REV3)
1319 		mb->un.varCfgPort.casabt = 1;
1320 
1321 	/* Now setup pcb */
1322 	phba->pcb->type = TYPE_NATIVE_SLI2;
1323 	phba->pcb->feature = FEATURE_INITIAL_SLI2;
1324 
1325 	/* Setup Mailbox pointers */
1326 	phba->pcb->mailBoxSize = sizeof(MAILBOX_t) + MAILBOX_EXT_SIZE;
1327 	offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
1328 	pdma_addr = phba->slim2p.phys + offset;
1329 	phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
1330 	phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
1331 
1332 	/*
1333 	 * Setup Host Group ring pointer.
1334 	 *
1335 	 * For efficiency reasons, the ring get/put pointers can be
1336 	 * placed in adapter memory (SLIM) rather than in host memory.
1337 	 * This allows firmware to avoid PCI reads/writes when updating
1338 	 * and checking pointers.
1339 	 *
1340 	 * The firmware recognizes the use of SLIM memory by comparing
1341 	 * the address of the get/put pointers structure with that of
1342 	 * the SLIM BAR (BAR0).
1343 	 *
1344 	 * Caution: be sure to use the PCI config space value of BAR0/BAR1
1345 	 * (the hardware's view of the base address), not the OS's
1346 	 * value of pci_resource_start() as the OS value may be a cookie
1347 	 * for ioremap/iomap.
1348 	 */
1349 
1350 
1351 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
1352 	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
1353 
1354 	/*
1355 	 * Set up HGP - Port Memory
1356 	 *
1357 	 * The port expects the host get/put pointers to reside in memory
1358 	 * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
1359 	 * area of SLIM.  In SLI-2 mode, there's an additional 16 reserved
1360 	 * words (0x40 bytes).  This area is not reserved if HBQs are
1361 	 * configured in SLI-3.
1362 	 *
1363 	 * CR0Put    - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
1364 	 * RR0Get                      0xc4              0x84
1365 	 * CR1Put                      0xc8              0x88
1366 	 * RR1Get                      0xcc              0x8c
1367 	 * CR2Put                      0xd0              0x90
1368 	 * RR2Get                      0xd4              0x94
1369 	 * CR3Put                      0xd8              0x98
1370 	 * RR3Get                      0xdc              0x9c
1371 	 *
1372 	 * Reserved                    0xa0-0xbf
1373 	 *    If HBQs configured:
1374 	 *                         HBQ 0 Put ptr  0xc0
1375 	 *                         HBQ 1 Put ptr  0xc4
1376 	 *                         HBQ 2 Put ptr  0xc8
1377 	 *                         ......
1378 	 *                         HBQ(M-1)Put Pointer 0xc0+(M-1)*4
1379 	 *
1380 	 */
1381 
1382 	if (phba->cfg_hostmem_hgp && phba->sli_rev != 3) {
1383 		phba->host_gp = &phba->mbox->us.s2.host[0];
1384 		phba->hbq_put = NULL;
1385 		offset = (uint8_t *)&phba->mbox->us.s2.host -
1386 			(uint8_t *)phba->slim2p.virt;
1387 		pdma_addr = phba->slim2p.phys + offset;
1388 		phba->pcb->hgpAddrHigh = putPaddrHigh(pdma_addr);
1389 		phba->pcb->hgpAddrLow = putPaddrLow(pdma_addr);
1390 	} else {
1391 		/* Always Host Group Pointer is in SLIM */
1392 		mb->un.varCfgPort.hps = 1;
1393 
1394 		if (phba->sli_rev == 3) {
1395 			phba->host_gp = &mb_slim->us.s3.host[0];
1396 			phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
1397 		} else {
1398 			phba->host_gp = &mb_slim->us.s2.host[0];
1399 			phba->hbq_put = NULL;
1400 		}
1401 
1402 		/* mask off BAR0's flag bits 0 - 3 */
1403 		phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
1404 			(void __iomem *)phba->host_gp -
1405 			(void __iomem *)phba->MBslimaddr;
1406 		if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
1407 			phba->pcb->hgpAddrHigh = bar_high;
1408 		else
1409 			phba->pcb->hgpAddrHigh = 0;
1410 		/* write HGP data to SLIM at the required longword offset */
1411 		memset(&hgp, 0, sizeof(struct lpfc_hgp));
1412 
1413 		for (i = 0; i < phba->sli.num_rings; i++) {
1414 			lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
1415 				    sizeof(*phba->host_gp));
1416 		}
1417 	}
1418 
1419 	/* Setup Port Group offset */
1420 	if (phba->sli_rev == 3)
1421 		pgp_offset = offsetof(struct lpfc_sli2_slim,
1422 				      mbx.us.s3_pgp.port);
1423 	else
1424 		pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
1425 	pdma_addr = phba->slim2p.phys + pgp_offset;
1426 	phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
1427 	phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
1428 
1429 	/* Use callback routine to setp rings in the pcb */
1430 	lpfc_config_pcb_setup(phba);
1431 
1432 	/* special handling for LC HBAs */
1433 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
1434 		uint32_t hbainit[5];
1435 
1436 		lpfc_hba_init(phba, hbainit);
1437 
1438 		memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
1439 	}
1440 
1441 	/* Swap PCB if needed */
1442 	lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
1443 }
1444 
1445 /**
1446  * lpfc_kill_board - Prepare a mailbox command for killing board
1447  * @phba: pointer to lpfc hba data structure.
1448  * @pmb: pointer to the driver internal queue element for mailbox command.
1449  *
1450  * The kill board mailbox command is used to tell firmware to perform a
1451  * graceful shutdown of a channel on a specified board to prepare for reset.
1452  * When the kill board mailbox command is received, the ER3 bit is set to 1
1453  * in the Host Status register and the ER Attention bit is set to 1 in the
1454  * Host Attention register of the HBA function that received the kill board
1455  * command.
1456  *
1457  * This routine prepares the mailbox command for killing the board in
1458  * preparation for a graceful shutdown.
1459  **/
1460 void
1461 lpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1462 {
1463 	MAILBOX_t *mb = &pmb->u.mb;
1464 
1465 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
1466 	mb->mbxCommand = MBX_KILL_BOARD;
1467 	mb->mbxOwner = OWN_HOST;
1468 	return;
1469 }
1470 
1471 /**
1472  * lpfc_mbox_put - Put a mailbox cmd into the tail of driver's mailbox queue
1473  * @phba: pointer to lpfc hba data structure.
1474  * @mbq: pointer to the driver internal queue element for mailbox command.
1475  *
1476  * Driver maintains a internal mailbox command queue implemented as a linked
1477  * list. When a mailbox command is issued, it shall be put into the mailbox
1478  * command queue such that they shall be processed orderly as HBA can process
1479  * one mailbox command at a time.
1480  **/
1481 void
1482 lpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
1483 {
1484 	struct lpfc_sli *psli;
1485 
1486 	psli = &phba->sli;
1487 
1488 	list_add_tail(&mbq->list, &psli->mboxq);
1489 
1490 	psli->mboxq_cnt++;
1491 
1492 	return;
1493 }
1494 
1495 /**
1496  * lpfc_mbox_get - Remove a mailbox cmd from the head of driver's mailbox queue
1497  * @phba: pointer to lpfc hba data structure.
1498  *
1499  * Driver maintains a internal mailbox command queue implemented as a linked
1500  * list. When a mailbox command is issued, it shall be put into the mailbox
1501  * command queue such that they shall be processed orderly as HBA can process
1502  * one mailbox command at a time. After HBA finished processing a mailbox
1503  * command, the driver will remove a pending mailbox command from the head of
1504  * the mailbox command queue and send to the HBA for processing.
1505  *
1506  * Return codes
1507  *    pointer to the driver internal queue element for mailbox command.
1508  **/
1509 LPFC_MBOXQ_t *
1510 lpfc_mbox_get(struct lpfc_hba * phba)
1511 {
1512 	LPFC_MBOXQ_t *mbq = NULL;
1513 	struct lpfc_sli *psli = &phba->sli;
1514 
1515 	list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
1516 	if (mbq)
1517 		psli->mboxq_cnt--;
1518 
1519 	return mbq;
1520 }
1521 
1522 /**
1523  * __lpfc_mbox_cmpl_put - Put mailbox cmd into mailbox cmd complete list
1524  * @phba: pointer to lpfc hba data structure.
1525  * @mbq: pointer to the driver internal queue element for mailbox command.
1526  *
1527  * This routine put the completed mailbox command into the mailbox command
1528  * complete list. This is the unlocked version of the routine. The mailbox
1529  * complete list is used by the driver worker thread to process mailbox
1530  * complete callback functions outside the driver interrupt handler.
1531  **/
1532 void
1533 __lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1534 {
1535 	list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
1536 }
1537 
1538 /**
1539  * lpfc_mbox_cmpl_put - Put mailbox command into mailbox command complete list
1540  * @phba: pointer to lpfc hba data structure.
1541  * @mbq: pointer to the driver internal queue element for mailbox command.
1542  *
1543  * This routine put the completed mailbox command into the mailbox command
1544  * complete list. This is the locked version of the routine. The mailbox
1545  * complete list is used by the driver worker thread to process mailbox
1546  * complete callback functions outside the driver interrupt handler.
1547  **/
1548 void
1549 lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
1550 {
1551 	unsigned long iflag;
1552 
1553 	/* This function expects to be called from interrupt context */
1554 	spin_lock_irqsave(&phba->hbalock, iflag);
1555 	__lpfc_mbox_cmpl_put(phba, mbq);
1556 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1557 	return;
1558 }
1559 
1560 /**
1561  * lpfc_mbox_cmd_check - Check the validality of a mailbox command
1562  * @phba: pointer to lpfc hba data structure.
1563  * @mboxq: pointer to the driver internal queue element for mailbox command.
1564  *
1565  * This routine is to check whether a mailbox command is valid to be issued.
1566  * This check will be performed by both the mailbox issue API when a client
1567  * is to issue a mailbox command to the mailbox transport.
1568  *
1569  * Return 0 - pass the check, -ENODEV - fail the check
1570  **/
1571 int
1572 lpfc_mbox_cmd_check(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1573 {
1574 	/* Mailbox command that have a completion handler must also have a
1575 	 * vport specified.
1576 	 */
1577 	if (mboxq->mbox_cmpl && mboxq->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
1578 	    mboxq->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
1579 		if (!mboxq->vport) {
1580 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1581 					"1814 Mbox x%x failed, no vport\n",
1582 					mboxq->u.mb.mbxCommand);
1583 			dump_stack();
1584 			return -ENODEV;
1585 		}
1586 	}
1587 	return 0;
1588 }
1589 
1590 /**
1591  * lpfc_mbox_dev_check - Check the device state for issuing a mailbox command
1592  * @phba: pointer to lpfc hba data structure.
1593  *
1594  * This routine is to check whether the HBA device is ready for posting a
1595  * mailbox command. It is used by the mailbox transport API at the time the
1596  * to post a mailbox command to the device.
1597  *
1598  * Return 0 - pass the check, -ENODEV - fail the check
1599  **/
1600 int
1601 lpfc_mbox_dev_check(struct lpfc_hba *phba)
1602 {
1603 	/* If the PCI channel is in offline state, do not issue mbox */
1604 	if (unlikely(pci_channel_offline(phba->pcidev)))
1605 		return -ENODEV;
1606 
1607 	/* If the HBA is in error state, do not issue mbox */
1608 	if (phba->link_state == LPFC_HBA_ERROR)
1609 		return -ENODEV;
1610 
1611 	return 0;
1612 }
1613 
1614 /**
1615  * lpfc_mbox_tmo_val - Retrieve mailbox command timeout value
1616  * @phba: pointer to lpfc hba data structure.
1617  * @cmd: mailbox command code.
1618  *
1619  * This routine retrieves the proper timeout value according to the mailbox
1620  * command code.
1621  *
1622  * Return codes
1623  *    Timeout value to be used for the given mailbox command
1624  **/
1625 int
1626 lpfc_mbox_tmo_val(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1627 {
1628 	MAILBOX_t *mbox = &mboxq->u.mb;
1629 	uint8_t subsys, opcode;
1630 
1631 	switch (mbox->mbxCommand) {
1632 	case MBX_WRITE_NV:	/* 0x03 */
1633 	case MBX_DUMP_MEMORY:	/* 0x17 */
1634 	case MBX_UPDATE_CFG:	/* 0x1B */
1635 	case MBX_DOWN_LOAD:	/* 0x1C */
1636 	case MBX_DEL_LD_ENTRY:	/* 0x1D */
1637 	case MBX_WRITE_VPARMS:	/* 0x32 */
1638 	case MBX_LOAD_AREA:	/* 0x81 */
1639 	case MBX_WRITE_WWN:     /* 0x98 */
1640 	case MBX_LOAD_EXP_ROM:	/* 0x9C */
1641 	case MBX_ACCESS_VDATA:	/* 0xA5 */
1642 		return LPFC_MBOX_TMO_FLASH_CMD;
1643 	case MBX_SLI4_CONFIG:	/* 0x9b */
1644 		subsys = lpfc_sli_config_mbox_subsys_get(phba, mboxq);
1645 		opcode = lpfc_sli_config_mbox_opcode_get(phba, mboxq);
1646 		if (subsys == LPFC_MBOX_SUBSYSTEM_COMMON) {
1647 			switch (opcode) {
1648 			case LPFC_MBOX_OPCODE_READ_OBJECT:
1649 			case LPFC_MBOX_OPCODE_WRITE_OBJECT:
1650 			case LPFC_MBOX_OPCODE_READ_OBJECT_LIST:
1651 			case LPFC_MBOX_OPCODE_DELETE_OBJECT:
1652 			case LPFC_MBOX_OPCODE_GET_PROFILE_LIST:
1653 			case LPFC_MBOX_OPCODE_SET_ACT_PROFILE:
1654 			case LPFC_MBOX_OPCODE_GET_PROFILE_CONFIG:
1655 			case LPFC_MBOX_OPCODE_SET_PROFILE_CONFIG:
1656 			case LPFC_MBOX_OPCODE_GET_FACTORY_PROFILE_CONFIG:
1657 			case LPFC_MBOX_OPCODE_GET_PROFILE_CAPACITIES:
1658 			case LPFC_MBOX_OPCODE_SEND_ACTIVATION:
1659 			case LPFC_MBOX_OPCODE_RESET_LICENSES:
1660 			case LPFC_MBOX_OPCODE_SET_BOOT_CONFIG:
1661 			case LPFC_MBOX_OPCODE_GET_VPD_DATA:
1662 			case LPFC_MBOX_OPCODE_SET_PHYSICAL_LINK_CONFIG:
1663 				return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
1664 			}
1665 		}
1666 		if (subsys == LPFC_MBOX_SUBSYSTEM_FCOE) {
1667 			switch (opcode) {
1668 			case LPFC_MBOX_OPCODE_FCOE_SET_FCLINK_SETTINGS:
1669 				return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
1670 			}
1671 		}
1672 		return LPFC_MBOX_SLI4_CONFIG_TMO;
1673 	}
1674 	return LPFC_MBOX_TMO;
1675 }
1676 
1677 /**
1678  * lpfc_sli4_mbx_sge_set - Set a sge entry in non-embedded mailbox command
1679  * @mbox: pointer to lpfc mbox command.
1680  * @sgentry: sge entry index.
1681  * @phyaddr: physical address for the sge
1682  * @length: Length of the sge.
1683  *
1684  * This routine sets up an entry in the non-embedded mailbox command at the sge
1685  * index location.
1686  **/
1687 void
1688 lpfc_sli4_mbx_sge_set(struct lpfcMboxq *mbox, uint32_t sgentry,
1689 		      dma_addr_t phyaddr, uint32_t length)
1690 {
1691 	struct lpfc_mbx_nembed_cmd *nembed_sge;
1692 
1693 	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1694 				&mbox->u.mqe.un.nembed_cmd;
1695 	nembed_sge->sge[sgentry].pa_lo = putPaddrLow(phyaddr);
1696 	nembed_sge->sge[sgentry].pa_hi = putPaddrHigh(phyaddr);
1697 	nembed_sge->sge[sgentry].length = length;
1698 }
1699 
1700 /**
1701  * lpfc_sli4_mbx_sge_get - Get a sge entry from non-embedded mailbox command
1702  * @mbox: pointer to lpfc mbox command.
1703  * @sgentry: sge entry index.
1704  *
1705  * This routine gets an entry from the non-embedded mailbox command at the sge
1706  * index location.
1707  **/
1708 void
1709 lpfc_sli4_mbx_sge_get(struct lpfcMboxq *mbox, uint32_t sgentry,
1710 		      struct lpfc_mbx_sge *sge)
1711 {
1712 	struct lpfc_mbx_nembed_cmd *nembed_sge;
1713 
1714 	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
1715 				&mbox->u.mqe.un.nembed_cmd;
1716 	sge->pa_lo = nembed_sge->sge[sgentry].pa_lo;
1717 	sge->pa_hi = nembed_sge->sge[sgentry].pa_hi;
1718 	sge->length = nembed_sge->sge[sgentry].length;
1719 }
1720 
1721 /**
1722  * lpfc_sli4_mbox_cmd_free - Free a sli4 mailbox command
1723  * @phba: pointer to lpfc hba data structure.
1724  * @mbox: pointer to lpfc mbox command.
1725  *
1726  * This routine frees SLI4 specific mailbox command for sending IOCTL command.
1727  **/
1728 void
1729 lpfc_sli4_mbox_cmd_free(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
1730 {
1731 	struct lpfc_mbx_sli4_config *sli4_cfg;
1732 	struct lpfc_mbx_sge sge;
1733 	dma_addr_t phyaddr;
1734 	uint32_t sgecount, sgentry;
1735 
1736 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
1737 
1738 	/* For embedded mbox command, just free the mbox command */
1739 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1740 		mempool_free(mbox, phba->mbox_mem_pool);
1741 		return;
1742 	}
1743 
1744 	/* For non-embedded mbox command, we need to free the pages first */
1745 	sgecount = bf_get(lpfc_mbox_hdr_sge_cnt, &sli4_cfg->header.cfg_mhdr);
1746 	/* There is nothing we can do if there is no sge address array */
1747 	if (unlikely(!mbox->sge_array)) {
1748 		mempool_free(mbox, phba->mbox_mem_pool);
1749 		return;
1750 	}
1751 	/* Each non-embedded DMA memory was allocated in the length of a page */
1752 	for (sgentry = 0; sgentry < sgecount; sgentry++) {
1753 		lpfc_sli4_mbx_sge_get(mbox, sgentry, &sge);
1754 		phyaddr = getPaddr(sge.pa_hi, sge.pa_lo);
1755 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
1756 				  mbox->sge_array->addr[sgentry], phyaddr);
1757 	}
1758 	/* Free the sge address array memory */
1759 	kfree(mbox->sge_array);
1760 	/* Finally, free the mailbox command itself */
1761 	mempool_free(mbox, phba->mbox_mem_pool);
1762 }
1763 
1764 /**
1765  * lpfc_sli4_config - Initialize the  SLI4 Config Mailbox command
1766  * @phba: pointer to lpfc hba data structure.
1767  * @mbox: pointer to lpfc mbox command.
1768  * @subsystem: The sli4 config sub mailbox subsystem.
1769  * @opcode: The sli4 config sub mailbox command opcode.
1770  * @length: Length of the sli4 config mailbox command (including sub-header).
1771  *
1772  * This routine sets up the header fields of SLI4 specific mailbox command
1773  * for sending IOCTL command.
1774  *
1775  * Return: the actual length of the mbox command allocated (mostly useful
1776  *         for none embedded mailbox command).
1777  **/
1778 int
1779 lpfc_sli4_config(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1780 		 uint8_t subsystem, uint8_t opcode, uint32_t length, bool emb)
1781 {
1782 	struct lpfc_mbx_sli4_config *sli4_config;
1783 	union lpfc_sli4_cfg_shdr *cfg_shdr = NULL;
1784 	uint32_t alloc_len;
1785 	uint32_t resid_len;
1786 	uint32_t pagen, pcount;
1787 	void *viraddr;
1788 	dma_addr_t phyaddr;
1789 
1790 	/* Set up SLI4 mailbox command header fields */
1791 	memset(mbox, 0, sizeof(*mbox));
1792 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_SLI4_CONFIG);
1793 
1794 	/* Set up SLI4 ioctl command header fields */
1795 	sli4_config = &mbox->u.mqe.un.sli4_config;
1796 
1797 	/* Setup for the embedded mbox command */
1798 	if (emb) {
1799 		/* Set up main header fields */
1800 		bf_set(lpfc_mbox_hdr_emb, &sli4_config->header.cfg_mhdr, 1);
1801 		sli4_config->header.cfg_mhdr.payload_length = length;
1802 		/* Set up sub-header fields following main header */
1803 		bf_set(lpfc_mbox_hdr_opcode,
1804 			&sli4_config->header.cfg_shdr.request, opcode);
1805 		bf_set(lpfc_mbox_hdr_subsystem,
1806 			&sli4_config->header.cfg_shdr.request, subsystem);
1807 		sli4_config->header.cfg_shdr.request.request_length =
1808 			length - LPFC_MBX_CMD_HDR_LENGTH;
1809 		return length;
1810 	}
1811 
1812 	/* Setup for the non-embedded mbox command */
1813 	pcount = (SLI4_PAGE_ALIGN(length))/SLI4_PAGE_SIZE;
1814 	pcount = (pcount > LPFC_SLI4_MBX_SGE_MAX_PAGES) ?
1815 				LPFC_SLI4_MBX_SGE_MAX_PAGES : pcount;
1816 	/* Allocate record for keeping SGE virtual addresses */
1817 	mbox->sge_array = kzalloc(sizeof(struct lpfc_mbx_nembed_sge_virt),
1818 				  GFP_KERNEL);
1819 	if (!mbox->sge_array) {
1820 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1821 				"2527 Failed to allocate non-embedded SGE "
1822 				"array.\n");
1823 		return 0;
1824 	}
1825 	for (pagen = 0, alloc_len = 0; pagen < pcount; pagen++) {
1826 		/* The DMA memory is always allocated in the length of a
1827 		 * page even though the last SGE might not fill up to a
1828 		 * page, this is used as a priori size of SLI4_PAGE_SIZE for
1829 		 * the later DMA memory free.
1830 		 */
1831 		viraddr = dma_alloc_coherent(&phba->pcidev->dev,
1832 					     SLI4_PAGE_SIZE, &phyaddr,
1833 					     GFP_KERNEL);
1834 		/* In case of malloc fails, proceed with whatever we have */
1835 		if (!viraddr)
1836 			break;
1837 		mbox->sge_array->addr[pagen] = viraddr;
1838 		/* Keep the first page for later sub-header construction */
1839 		if (pagen == 0)
1840 			cfg_shdr = (union lpfc_sli4_cfg_shdr *)viraddr;
1841 		resid_len = length - alloc_len;
1842 		if (resid_len > SLI4_PAGE_SIZE) {
1843 			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1844 					      SLI4_PAGE_SIZE);
1845 			alloc_len += SLI4_PAGE_SIZE;
1846 		} else {
1847 			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
1848 					      resid_len);
1849 			alloc_len = length;
1850 		}
1851 	}
1852 
1853 	/* Set up main header fields in mailbox command */
1854 	sli4_config->header.cfg_mhdr.payload_length = alloc_len;
1855 	bf_set(lpfc_mbox_hdr_sge_cnt, &sli4_config->header.cfg_mhdr, pagen);
1856 
1857 	/* Set up sub-header fields into the first page */
1858 	if (pagen > 0) {
1859 		bf_set(lpfc_mbox_hdr_opcode, &cfg_shdr->request, opcode);
1860 		bf_set(lpfc_mbox_hdr_subsystem, &cfg_shdr->request, subsystem);
1861 		cfg_shdr->request.request_length =
1862 				alloc_len - sizeof(union  lpfc_sli4_cfg_shdr);
1863 	}
1864 	/* The sub-header is in DMA memory, which needs endian converstion */
1865 	if (cfg_shdr)
1866 		lpfc_sli_pcimem_bcopy(cfg_shdr, cfg_shdr,
1867 				      sizeof(union  lpfc_sli4_cfg_shdr));
1868 	return alloc_len;
1869 }
1870 
1871 /**
1872  * lpfc_sli4_mbox_rsrc_extent - Initialize the opcode resource extent.
1873  * @phba: pointer to lpfc hba data structure.
1874  * @mbox: pointer to an allocated lpfc mbox resource.
1875  * @exts_count: the number of extents, if required, to allocate.
1876  * @rsrc_type: the resource extent type.
1877  * @emb: true if LPFC_SLI4_MBX_EMBED. false if LPFC_SLI4_MBX_NEMBED.
1878  *
1879  * This routine completes the subcommand header for SLI4 resource extent
1880  * mailbox commands.  It is called after lpfc_sli4_config.  The caller must
1881  * pass an allocated mailbox and the attributes required to initialize the
1882  * mailbox correctly.
1883  *
1884  * Return: the actual length of the mbox command allocated.
1885  **/
1886 int
1887 lpfc_sli4_mbox_rsrc_extent(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
1888 			   uint16_t exts_count, uint16_t rsrc_type, bool emb)
1889 {
1890 	uint8_t opcode = 0;
1891 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc_extnt = NULL;
1892 	void *virtaddr = NULL;
1893 
1894 	/* Set up SLI4 ioctl command header fields */
1895 	if (emb == LPFC_SLI4_MBX_NEMBED) {
1896 		/* Get the first SGE entry from the non-embedded DMA memory */
1897 		virtaddr = mbox->sge_array->addr[0];
1898 		if (virtaddr == NULL)
1899 			return 1;
1900 		n_rsrc_extnt = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
1901 	}
1902 
1903 	/*
1904 	 * The resource type is common to all extent Opcodes and resides in the
1905 	 * same position.
1906 	 */
1907 	if (emb == LPFC_SLI4_MBX_EMBED)
1908 		bf_set(lpfc_mbx_alloc_rsrc_extents_type,
1909 		       &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
1910 		       rsrc_type);
1911 	else {
1912 		/* This is DMA data.  Byteswap is required. */
1913 		bf_set(lpfc_mbx_alloc_rsrc_extents_type,
1914 		       n_rsrc_extnt, rsrc_type);
1915 		lpfc_sli_pcimem_bcopy(&n_rsrc_extnt->word4,
1916 				      &n_rsrc_extnt->word4,
1917 				      sizeof(uint32_t));
1918 	}
1919 
1920 	/* Complete the initialization for the particular Opcode. */
1921 	opcode = lpfc_sli_config_mbox_opcode_get(phba, mbox);
1922 	switch (opcode) {
1923 	case LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT:
1924 		if (emb == LPFC_SLI4_MBX_EMBED)
1925 			bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
1926 			       &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
1927 			       exts_count);
1928 		else
1929 			bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
1930 			       n_rsrc_extnt, exts_count);
1931 		break;
1932 	case LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT:
1933 	case LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO:
1934 	case LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT:
1935 		/* Initialization is complete.*/
1936 		break;
1937 	default:
1938 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1939 				"2929 Resource Extent Opcode x%x is "
1940 				"unsupported\n", opcode);
1941 		return 1;
1942 	}
1943 
1944 	return 0;
1945 }
1946 
1947 /**
1948  * lpfc_sli_config_mbox_subsys_get - Get subsystem from a sli_config mbox cmd
1949  * @phba: pointer to lpfc hba data structure.
1950  * @mbox: pointer to lpfc mbox command queue entry.
1951  *
1952  * This routine gets the subsystem from a SLI4 specific SLI_CONFIG mailbox
1953  * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if the
1954  * sub-header is not present, subsystem LPFC_MBOX_SUBSYSTEM_NA (0x0) shall
1955  * be returned.
1956  **/
1957 uint8_t
1958 lpfc_sli_config_mbox_subsys_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
1959 {
1960 	struct lpfc_mbx_sli4_config *sli4_cfg;
1961 	union lpfc_sli4_cfg_shdr *cfg_shdr;
1962 
1963 	if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
1964 		return LPFC_MBOX_SUBSYSTEM_NA;
1965 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
1966 
1967 	/* For embedded mbox command, get opcode from embedded sub-header*/
1968 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
1969 		cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
1970 		return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
1971 	}
1972 
1973 	/* For non-embedded mbox command, get opcode from first dma page */
1974 	if (unlikely(!mbox->sge_array))
1975 		return LPFC_MBOX_SUBSYSTEM_NA;
1976 	cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
1977 	return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
1978 }
1979 
1980 /**
1981  * lpfc_sli_config_mbox_opcode_get - Get opcode from a sli_config mbox cmd
1982  * @phba: pointer to lpfc hba data structure.
1983  * @mbox: pointer to lpfc mbox command queue entry.
1984  *
1985  * This routine gets the opcode from a SLI4 specific SLI_CONFIG mailbox
1986  * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if
1987  * the sub-header is not present, opcode LPFC_MBOX_OPCODE_NA (0x0) be
1988  * returned.
1989  **/
1990 uint8_t
1991 lpfc_sli_config_mbox_opcode_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
1992 {
1993 	struct lpfc_mbx_sli4_config *sli4_cfg;
1994 	union lpfc_sli4_cfg_shdr *cfg_shdr;
1995 
1996 	if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
1997 		return LPFC_MBOX_OPCODE_NA;
1998 	sli4_cfg = &mbox->u.mqe.un.sli4_config;
1999 
2000 	/* For embedded mbox command, get opcode from embedded sub-header*/
2001 	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
2002 		cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
2003 		return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
2004 	}
2005 
2006 	/* For non-embedded mbox command, get opcode from first dma page */
2007 	if (unlikely(!mbox->sge_array))
2008 		return LPFC_MBOX_OPCODE_NA;
2009 	cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
2010 	return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
2011 }
2012 
2013 /**
2014  * lpfc_sli4_mbx_read_fcf_rec - Allocate and construct read fcf mbox cmd
2015  * @phba: pointer to lpfc hba data structure.
2016  * @fcf_index: index to fcf table.
2017  *
2018  * This routine routine allocates and constructs non-embedded mailbox command
2019  * for reading a FCF table entry referred by @fcf_index.
2020  *
2021  * Return: pointer to the mailbox command constructed if successful, otherwise
2022  * NULL.
2023  **/
2024 int
2025 lpfc_sli4_mbx_read_fcf_rec(struct lpfc_hba *phba,
2026 			   struct lpfcMboxq *mboxq,
2027 			   uint16_t fcf_index)
2028 {
2029 	void *virt_addr;
2030 	uint8_t *bytep;
2031 	struct lpfc_mbx_sge sge;
2032 	uint32_t alloc_len, req_len;
2033 	struct lpfc_mbx_read_fcf_tbl *read_fcf;
2034 
2035 	if (!mboxq)
2036 		return -ENOMEM;
2037 
2038 	req_len = sizeof(struct fcf_record) +
2039 		  sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
2040 
2041 	/* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
2042 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2043 			LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
2044 			LPFC_SLI4_MBX_NEMBED);
2045 
2046 	if (alloc_len < req_len) {
2047 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2048 				"0291 Allocated DMA memory size (x%x) is "
2049 				"less than the requested DMA memory "
2050 				"size (x%x)\n", alloc_len, req_len);
2051 		return -ENOMEM;
2052 	}
2053 
2054 	/* Get the first SGE entry from the non-embedded DMA memory. This
2055 	 * routine only uses a single SGE.
2056 	 */
2057 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
2058 	virt_addr = mboxq->sge_array->addr[0];
2059 	read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
2060 
2061 	/* Set up command fields */
2062 	bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
2063 	/* Perform necessary endian conversion */
2064 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
2065 	lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
2066 
2067 	return 0;
2068 }
2069 
2070 /**
2071  * lpfc_request_features: Configure SLI4 REQUEST_FEATURES mailbox
2072  * @mboxq: pointer to lpfc mbox command.
2073  *
2074  * This routine sets up the mailbox for an SLI4 REQUEST_FEATURES
2075  * mailbox command.
2076  **/
2077 void
2078 lpfc_request_features(struct lpfc_hba *phba, struct lpfcMboxq *mboxq)
2079 {
2080 	/* Set up SLI4 mailbox command header fields */
2081 	memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
2082 	bf_set(lpfc_mqe_command, &mboxq->u.mqe, MBX_SLI4_REQ_FTRS);
2083 
2084 	/* Set up host requested features. */
2085 	bf_set(lpfc_mbx_rq_ftr_rq_fcpi, &mboxq->u.mqe.un.req_ftrs, 1);
2086 	bf_set(lpfc_mbx_rq_ftr_rq_perfh, &mboxq->u.mqe.un.req_ftrs, 1);
2087 
2088 	/* Enable DIF (block guard) only if configured to do so. */
2089 	if (phba->cfg_enable_bg)
2090 		bf_set(lpfc_mbx_rq_ftr_rq_dif, &mboxq->u.mqe.un.req_ftrs, 1);
2091 
2092 	/* Enable NPIV only if configured to do so. */
2093 	if (phba->max_vpi && phba->cfg_enable_npiv)
2094 		bf_set(lpfc_mbx_rq_ftr_rq_npiv, &mboxq->u.mqe.un.req_ftrs, 1);
2095 
2096 	if (phba->nvmet_support) {
2097 		bf_set(lpfc_mbx_rq_ftr_rq_mrqp, &mboxq->u.mqe.un.req_ftrs, 1);
2098 		/* iaab/iaar NOT set for now */
2099 		bf_set(lpfc_mbx_rq_ftr_rq_iaab, &mboxq->u.mqe.un.req_ftrs, 0);
2100 		bf_set(lpfc_mbx_rq_ftr_rq_iaar, &mboxq->u.mqe.un.req_ftrs, 0);
2101 	}
2102 	return;
2103 }
2104 
2105 /**
2106  * lpfc_init_vfi - Initialize the INIT_VFI mailbox command
2107  * @mbox: pointer to lpfc mbox command to initialize.
2108  * @vport: Vport associated with the VF.
2109  *
2110  * This routine initializes @mbox to all zeros and then fills in the mailbox
2111  * fields from @vport. INIT_VFI configures virtual fabrics identified by VFI
2112  * in the context of an FCF. The driver issues this command to setup a VFI
2113  * before issuing a FLOGI to login to the VSAN. The driver should also issue a
2114  * REG_VFI after a successful VSAN login.
2115  **/
2116 void
2117 lpfc_init_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
2118 {
2119 	struct lpfc_mbx_init_vfi *init_vfi;
2120 
2121 	memset(mbox, 0, sizeof(*mbox));
2122 	mbox->vport = vport;
2123 	init_vfi = &mbox->u.mqe.un.init_vfi;
2124 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VFI);
2125 	bf_set(lpfc_init_vfi_vr, init_vfi, 1);
2126 	bf_set(lpfc_init_vfi_vt, init_vfi, 1);
2127 	bf_set(lpfc_init_vfi_vp, init_vfi, 1);
2128 	bf_set(lpfc_init_vfi_vfi, init_vfi,
2129 	       vport->phba->sli4_hba.vfi_ids[vport->vfi]);
2130 	bf_set(lpfc_init_vfi_vpi, init_vfi,
2131 	       vport->phba->vpi_ids[vport->vpi]);
2132 	bf_set(lpfc_init_vfi_fcfi, init_vfi,
2133 	       vport->phba->fcf.fcfi);
2134 }
2135 
2136 /**
2137  * lpfc_reg_vfi - Initialize the REG_VFI mailbox command
2138  * @mbox: pointer to lpfc mbox command to initialize.
2139  * @vport: vport associated with the VF.
2140  * @phys: BDE DMA bus address used to send the service parameters to the HBA.
2141  *
2142  * This routine initializes @mbox to all zeros and then fills in the mailbox
2143  * fields from @vport, and uses @buf as a DMAable buffer to send the vport's
2144  * fc service parameters to the HBA for this VFI. REG_VFI configures virtual
2145  * fabrics identified by VFI in the context of an FCF.
2146  **/
2147 void
2148 lpfc_reg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport, dma_addr_t phys)
2149 {
2150 	struct lpfc_mbx_reg_vfi *reg_vfi;
2151 	struct lpfc_hba *phba = vport->phba;
2152 	uint8_t bbscn_fabric = 0, bbscn_max = 0, bbscn_def = 0;
2153 
2154 	memset(mbox, 0, sizeof(*mbox));
2155 	reg_vfi = &mbox->u.mqe.un.reg_vfi;
2156 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_VFI);
2157 	bf_set(lpfc_reg_vfi_vp, reg_vfi, 1);
2158 	bf_set(lpfc_reg_vfi_vfi, reg_vfi,
2159 	       phba->sli4_hba.vfi_ids[vport->vfi]);
2160 	bf_set(lpfc_reg_vfi_fcfi, reg_vfi, phba->fcf.fcfi);
2161 	bf_set(lpfc_reg_vfi_vpi, reg_vfi, phba->vpi_ids[vport->vpi]);
2162 	memcpy(reg_vfi->wwn, &vport->fc_portname, sizeof(struct lpfc_name));
2163 	reg_vfi->wwn[0] = cpu_to_le32(reg_vfi->wwn[0]);
2164 	reg_vfi->wwn[1] = cpu_to_le32(reg_vfi->wwn[1]);
2165 	reg_vfi->e_d_tov = phba->fc_edtov;
2166 	reg_vfi->r_a_tov = phba->fc_ratov;
2167 	if (phys) {
2168 		reg_vfi->bde.addrHigh = putPaddrHigh(phys);
2169 		reg_vfi->bde.addrLow = putPaddrLow(phys);
2170 		reg_vfi->bde.tus.f.bdeSize = sizeof(vport->fc_sparam);
2171 		reg_vfi->bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2172 	}
2173 	bf_set(lpfc_reg_vfi_nport_id, reg_vfi, vport->fc_myDID);
2174 
2175 	/* Only FC supports upd bit */
2176 	if ((phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC) &&
2177 	    (vport->fc_flag & FC_VFI_REGISTERED) &&
2178 	    (!phba->fc_topology_changed))
2179 		bf_set(lpfc_reg_vfi_upd, reg_vfi, 1);
2180 
2181 	bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 0);
2182 	bf_set(lpfc_reg_vfi_bbscn, reg_vfi, 0);
2183 	bbscn_fabric = (phba->fc_fabparam.cmn.bbRcvSizeMsb >> 4) & 0xF;
2184 
2185 	if (phba->bbcredit_support && phba->cfg_enable_bbcr  &&
2186 	    bbscn_fabric != 0) {
2187 		bbscn_max = bf_get(lpfc_bbscn_max,
2188 				   &phba->sli4_hba.bbscn_params);
2189 		if (bbscn_fabric <= bbscn_max) {
2190 			bbscn_def = bf_get(lpfc_bbscn_def,
2191 					   &phba->sli4_hba.bbscn_params);
2192 
2193 			if (bbscn_fabric > bbscn_def)
2194 				bf_set(lpfc_reg_vfi_bbscn, reg_vfi,
2195 				       bbscn_fabric);
2196 			else
2197 				bf_set(lpfc_reg_vfi_bbscn, reg_vfi, bbscn_def);
2198 
2199 			bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 1);
2200 		}
2201 	}
2202 	lpfc_printf_vlog(vport, KERN_INFO, LOG_MBOX,
2203 			"3134 Register VFI, mydid:x%x, fcfi:%d, "
2204 			" vfi:%d, vpi:%d, fc_pname:%x%x fc_flag:x%x"
2205 			" port_state:x%x topology chg:%d bbscn_fabric :%d\n",
2206 			vport->fc_myDID,
2207 			phba->fcf.fcfi,
2208 			phba->sli4_hba.vfi_ids[vport->vfi],
2209 			phba->vpi_ids[vport->vpi],
2210 			reg_vfi->wwn[0], reg_vfi->wwn[1], vport->fc_flag,
2211 			vport->port_state, phba->fc_topology_changed,
2212 			bbscn_fabric);
2213 }
2214 
2215 /**
2216  * lpfc_init_vpi - Initialize the INIT_VPI mailbox command
2217  * @phba: pointer to the hba structure to init the VPI for.
2218  * @mbox: pointer to lpfc mbox command to initialize.
2219  * @vpi: VPI to be initialized.
2220  *
2221  * The INIT_VPI mailbox command supports virtual N_Ports. The driver uses the
2222  * command to activate a virtual N_Port. The HBA assigns a MAC address to use
2223  * with the virtual N Port.  The SLI Host issues this command before issuing a
2224  * FDISC to connect to the Fabric. The SLI Host should issue a REG_VPI after a
2225  * successful virtual NPort login.
2226  **/
2227 void
2228 lpfc_init_vpi(struct lpfc_hba *phba, struct lpfcMboxq *mbox, uint16_t vpi)
2229 {
2230 	memset(mbox, 0, sizeof(*mbox));
2231 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VPI);
2232 	bf_set(lpfc_init_vpi_vpi, &mbox->u.mqe.un.init_vpi,
2233 	       phba->vpi_ids[vpi]);
2234 	bf_set(lpfc_init_vpi_vfi, &mbox->u.mqe.un.init_vpi,
2235 	       phba->sli4_hba.vfi_ids[phba->pport->vfi]);
2236 }
2237 
2238 /**
2239  * lpfc_unreg_vfi - Initialize the UNREG_VFI mailbox command
2240  * @mbox: pointer to lpfc mbox command to initialize.
2241  * @vport: vport associated with the VF.
2242  *
2243  * The UNREG_VFI mailbox command causes the SLI Host to put a virtual fabric
2244  * (logical NPort) into the inactive state. The SLI Host must have logged out
2245  * and unregistered all remote N_Ports to abort any activity on the virtual
2246  * fabric. The SLI Port posts the mailbox response after marking the virtual
2247  * fabric inactive.
2248  **/
2249 void
2250 lpfc_unreg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
2251 {
2252 	memset(mbox, 0, sizeof(*mbox));
2253 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_VFI);
2254 	bf_set(lpfc_unreg_vfi_vfi, &mbox->u.mqe.un.unreg_vfi,
2255 	       vport->phba->sli4_hba.vfi_ids[vport->vfi]);
2256 }
2257 
2258 /**
2259  * lpfc_sli4_dump_cfg_rg23 - Dump sli4 port config region 23
2260  * @phba: pointer to the hba structure containing.
2261  * @mbox: pointer to lpfc mbox command to initialize.
2262  *
2263  * This function create a SLI4 dump mailbox command to dump configure
2264  * region 23.
2265  **/
2266 int
2267 lpfc_sli4_dump_cfg_rg23(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2268 {
2269 	struct lpfc_dmabuf *mp = NULL;
2270 	MAILBOX_t *mb;
2271 
2272 	memset(mbox, 0, sizeof(*mbox));
2273 	mb = &mbox->u.mb;
2274 
2275 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2276 	if (mp)
2277 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2278 
2279 	if (!mp || !mp->virt) {
2280 		kfree(mp);
2281 		/* dump config region 23 failed to allocate memory */
2282 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2283 			"2569 lpfc dump config region 23: memory"
2284 			" allocation failed\n");
2285 		return 1;
2286 	}
2287 
2288 	memset(mp->virt, 0, LPFC_BPL_SIZE);
2289 	INIT_LIST_HEAD(&mp->list);
2290 
2291 	/* save address for completion */
2292 	mbox->ctx_buf = (uint8_t *)mp;
2293 
2294 	mb->mbxCommand = MBX_DUMP_MEMORY;
2295 	mb->un.varDmp.type = DMP_NV_PARAMS;
2296 	mb->un.varDmp.region_id = DMP_REGION_23;
2297 	mb->un.varDmp.sli4_length = DMP_RGN23_SIZE;
2298 	mb->un.varWords[3] = putPaddrLow(mp->phys);
2299 	mb->un.varWords[4] = putPaddrHigh(mp->phys);
2300 	return 0;
2301 }
2302 
2303 static void
2304 lpfc_mbx_cmpl_rdp_link_stat(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2305 {
2306 	MAILBOX_t *mb;
2307 	int rc = FAILURE;
2308 	struct lpfc_rdp_context *rdp_context =
2309 			(struct lpfc_rdp_context *)(mboxq->ctx_ndlp);
2310 
2311 	mb = &mboxq->u.mb;
2312 	if (mb->mbxStatus)
2313 		goto mbx_failed;
2314 
2315 	memcpy(&rdp_context->link_stat, &mb->un.varRdLnk, sizeof(READ_LNK_VAR));
2316 
2317 	rc = SUCCESS;
2318 
2319 mbx_failed:
2320 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2321 	rdp_context->cmpl(phba, rdp_context, rc);
2322 }
2323 
2324 static void
2325 lpfc_mbx_cmpl_rdp_page_a2(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2326 {
2327 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
2328 	struct lpfc_rdp_context *rdp_context =
2329 			(struct lpfc_rdp_context *)(mbox->ctx_ndlp);
2330 
2331 	if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
2332 		goto error_mbuf_free;
2333 
2334 	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a2,
2335 				DMP_SFF_PAGE_A2_SIZE);
2336 
2337 	/* We don't need dma buffer for link stat. */
2338 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2339 	kfree(mp);
2340 
2341 	memset(mbox, 0, sizeof(*mbox));
2342 	lpfc_read_lnk_stat(phba, mbox);
2343 	mbox->vport = rdp_context->ndlp->vport;
2344 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_link_stat;
2345 	mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
2346 	if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) == MBX_NOT_FINISHED)
2347 		goto error_cmd_free;
2348 
2349 	return;
2350 
2351 error_mbuf_free:
2352 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2353 	kfree(mp);
2354 error_cmd_free:
2355 	lpfc_sli4_mbox_cmd_free(phba, mbox);
2356 	rdp_context->cmpl(phba, rdp_context, FAILURE);
2357 }
2358 
2359 void
2360 lpfc_mbx_cmpl_rdp_page_a0(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
2361 {
2362 	int rc;
2363 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
2364 	struct lpfc_rdp_context *rdp_context =
2365 			(struct lpfc_rdp_context *)(mbox->ctx_ndlp);
2366 
2367 	if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
2368 		goto error;
2369 
2370 	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a0,
2371 				DMP_SFF_PAGE_A0_SIZE);
2372 
2373 	memset(mbox, 0, sizeof(*mbox));
2374 
2375 	memset(mp->virt, 0, DMP_SFF_PAGE_A2_SIZE);
2376 	INIT_LIST_HEAD(&mp->list);
2377 
2378 	/* save address for completion */
2379 	mbox->ctx_buf = mp;
2380 	mbox->vport = rdp_context->ndlp->vport;
2381 
2382 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
2383 	bf_set(lpfc_mbx_memory_dump_type3_type,
2384 		&mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
2385 	bf_set(lpfc_mbx_memory_dump_type3_link,
2386 		&mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
2387 	bf_set(lpfc_mbx_memory_dump_type3_page_no,
2388 		&mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A2);
2389 	bf_set(lpfc_mbx_memory_dump_type3_length,
2390 		&mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A2_SIZE);
2391 	mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
2392 	mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
2393 
2394 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a2;
2395 	mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
2396 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2397 	if (rc == MBX_NOT_FINISHED)
2398 		goto error;
2399 
2400 	return;
2401 
2402 error:
2403 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2404 	kfree(mp);
2405 	lpfc_sli4_mbox_cmd_free(phba, mbox);
2406 	rdp_context->cmpl(phba, rdp_context, FAILURE);
2407 }
2408 
2409 
2410 /*
2411  * lpfc_sli4_dump_sfp_pagea0 - Dump sli4 read SFP Diagnostic.
2412  * @phba: pointer to the hba structure containing.
2413  * @mbox: pointer to lpfc mbox command to initialize.
2414  *
2415  * This function create a SLI4 dump mailbox command to dump configure
2416  * type 3 page 0xA0.
2417  */
2418 int
2419 lpfc_sli4_dump_page_a0(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2420 {
2421 	struct lpfc_dmabuf *mp = NULL;
2422 
2423 	memset(mbox, 0, sizeof(*mbox));
2424 
2425 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2426 	if (mp)
2427 		mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2428 	if (!mp || !mp->virt) {
2429 		kfree(mp);
2430 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2431 			"3569 dump type 3 page 0xA0 allocation failed\n");
2432 		return 1;
2433 	}
2434 
2435 	memset(mp->virt, 0, LPFC_BPL_SIZE);
2436 	INIT_LIST_HEAD(&mp->list);
2437 
2438 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
2439 	/* save address for completion */
2440 	mbox->ctx_buf = mp;
2441 
2442 	bf_set(lpfc_mbx_memory_dump_type3_type,
2443 		&mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
2444 	bf_set(lpfc_mbx_memory_dump_type3_link,
2445 		&mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
2446 	bf_set(lpfc_mbx_memory_dump_type3_page_no,
2447 		&mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A0);
2448 	bf_set(lpfc_mbx_memory_dump_type3_length,
2449 		&mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A0_SIZE);
2450 	mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
2451 	mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
2452 
2453 	return 0;
2454 }
2455 
2456 /**
2457  * lpfc_reg_fcfi - Initialize the REG_FCFI mailbox command
2458  * @phba: pointer to the hba structure containing the FCF index and RQ ID.
2459  * @mbox: pointer to lpfc mbox command to initialize.
2460  *
2461  * The REG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs). The
2462  * SLI Host uses the command to activate an FCF after it has acquired FCF
2463  * information via a READ_FCF mailbox command. This mailbox command also is used
2464  * to indicate where received unsolicited frames from this FCF will be sent. By
2465  * default this routine will set up the FCF to forward all unsolicited frames
2466  * the the RQ ID passed in the @phba. This can be overridden by the caller for
2467  * more complicated setups.
2468  **/
2469 void
2470 lpfc_reg_fcfi(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
2471 {
2472 	struct lpfc_mbx_reg_fcfi *reg_fcfi;
2473 
2474 	memset(mbox, 0, sizeof(*mbox));
2475 	reg_fcfi = &mbox->u.mqe.un.reg_fcfi;
2476 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI);
2477 	if (phba->nvmet_support == 0) {
2478 		bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
2479 		       phba->sli4_hba.hdr_rq->queue_id);
2480 		/* Match everything - rq_id0 */
2481 		bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, 0);
2482 		bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0);
2483 		bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi, 0);
2484 		bf_set(lpfc_reg_fcfi_rctl_mask0, reg_fcfi, 0);
2485 
2486 		bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi, REG_FCF_INVALID_QID);
2487 
2488 		/* addr mode is bit wise inverted value of fcf addr_mode */
2489 		bf_set(lpfc_reg_fcfi_mam, reg_fcfi,
2490 		       (~phba->fcf.addr_mode) & 0x3);
2491 	} else {
2492 		/* This is ONLY for NVMET MRQ == 1 */
2493 		if (phba->cfg_nvmet_mrq != 1)
2494 			return;
2495 
2496 		bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
2497 		       phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
2498 		/* Match type FCP - rq_id0 */
2499 		bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, FC_TYPE_FCP);
2500 		bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0xff);
2501 		bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi,
2502 		       FC_RCTL_DD_UNSOL_CMD);
2503 
2504 		bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi,
2505 		       phba->sli4_hba.hdr_rq->queue_id);
2506 		/* Match everything else - rq_id1 */
2507 		bf_set(lpfc_reg_fcfi_type_match1, reg_fcfi, 0);
2508 		bf_set(lpfc_reg_fcfi_type_mask1, reg_fcfi, 0);
2509 		bf_set(lpfc_reg_fcfi_rctl_match1, reg_fcfi, 0);
2510 		bf_set(lpfc_reg_fcfi_rctl_mask1, reg_fcfi, 0);
2511 	}
2512 	bf_set(lpfc_reg_fcfi_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
2513 	bf_set(lpfc_reg_fcfi_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
2514 	bf_set(lpfc_reg_fcfi_info_index, reg_fcfi,
2515 	       phba->fcf.current_rec.fcf_indx);
2516 	if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
2517 		bf_set(lpfc_reg_fcfi_vv, reg_fcfi, 1);
2518 		bf_set(lpfc_reg_fcfi_vlan_tag, reg_fcfi,
2519 		       phba->fcf.current_rec.vlan_id);
2520 	}
2521 }
2522 
2523 /**
2524  * lpfc_reg_fcfi_mrq - Initialize the REG_FCFI_MRQ mailbox command
2525  * @phba: pointer to the hba structure containing the FCF index and RQ ID.
2526  * @mbox: pointer to lpfc mbox command to initialize.
2527  * @mode: 0 to register FCFI, 1 to register MRQs
2528  *
2529  * The REG_FCFI_MRQ mailbox command supports Fibre Channel Forwarders (FCFs).
2530  * The SLI Host uses the command to activate an FCF after it has acquired FCF
2531  * information via a READ_FCF mailbox command. This mailbox command also is used
2532  * to indicate where received unsolicited frames from this FCF will be sent. By
2533  * default this routine will set up the FCF to forward all unsolicited frames
2534  * the the RQ ID passed in the @phba. This can be overridden by the caller for
2535  * more complicated setups.
2536  **/
2537 void
2538 lpfc_reg_fcfi_mrq(struct lpfc_hba *phba, struct lpfcMboxq *mbox, int mode)
2539 {
2540 	struct lpfc_mbx_reg_fcfi_mrq *reg_fcfi;
2541 
2542 	/* This is ONLY for MRQ */
2543 	if (phba->cfg_nvmet_mrq <= 1)
2544 		return;
2545 
2546 	memset(mbox, 0, sizeof(*mbox));
2547 	reg_fcfi = &mbox->u.mqe.un.reg_fcfi_mrq;
2548 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI_MRQ);
2549 	if (mode == 0) {
2550 		bf_set(lpfc_reg_fcfi_mrq_info_index, reg_fcfi,
2551 		       phba->fcf.current_rec.fcf_indx);
2552 		if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
2553 			bf_set(lpfc_reg_fcfi_mrq_vv, reg_fcfi, 1);
2554 			bf_set(lpfc_reg_fcfi_mrq_vlan_tag, reg_fcfi,
2555 			       phba->fcf.current_rec.vlan_id);
2556 		}
2557 		return;
2558 	}
2559 
2560 	bf_set(lpfc_reg_fcfi_mrq_rq_id0, reg_fcfi,
2561 	       phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
2562 	/* Match NVME frames of type FCP (protocol NVME) - rq_id0 */
2563 	bf_set(lpfc_reg_fcfi_mrq_type_match0, reg_fcfi, FC_TYPE_FCP);
2564 	bf_set(lpfc_reg_fcfi_mrq_type_mask0, reg_fcfi, 0xff);
2565 	bf_set(lpfc_reg_fcfi_mrq_rctl_match0, reg_fcfi, FC_RCTL_DD_UNSOL_CMD);
2566 	bf_set(lpfc_reg_fcfi_mrq_rctl_mask0, reg_fcfi, 0xff);
2567 	bf_set(lpfc_reg_fcfi_mrq_ptc0, reg_fcfi, 1);
2568 	bf_set(lpfc_reg_fcfi_mrq_pt0, reg_fcfi, 1);
2569 
2570 	bf_set(lpfc_reg_fcfi_mrq_policy, reg_fcfi, 3); /* NVME connection id */
2571 	bf_set(lpfc_reg_fcfi_mrq_mode, reg_fcfi, 1);
2572 	bf_set(lpfc_reg_fcfi_mrq_filter, reg_fcfi, 1); /* rq_id0 */
2573 	bf_set(lpfc_reg_fcfi_mrq_npairs, reg_fcfi, phba->cfg_nvmet_mrq);
2574 
2575 	bf_set(lpfc_reg_fcfi_mrq_rq_id1, reg_fcfi,
2576 	       phba->sli4_hba.hdr_rq->queue_id);
2577 	/* Match everything - rq_id1 */
2578 	bf_set(lpfc_reg_fcfi_mrq_type_match1, reg_fcfi, 0);
2579 	bf_set(lpfc_reg_fcfi_mrq_type_mask1, reg_fcfi, 0);
2580 	bf_set(lpfc_reg_fcfi_mrq_rctl_match1, reg_fcfi, 0);
2581 	bf_set(lpfc_reg_fcfi_mrq_rctl_mask1, reg_fcfi, 0);
2582 
2583 	bf_set(lpfc_reg_fcfi_mrq_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
2584 	bf_set(lpfc_reg_fcfi_mrq_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
2585 }
2586 
2587 /**
2588  * lpfc_unreg_fcfi - Initialize the UNREG_FCFI mailbox command
2589  * @mbox: pointer to lpfc mbox command to initialize.
2590  * @fcfi: FCFI to be unregistered.
2591  *
2592  * The UNREG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs).
2593  * The SLI Host uses the command to inactivate an FCFI.
2594  **/
2595 void
2596 lpfc_unreg_fcfi(struct lpfcMboxq *mbox, uint16_t fcfi)
2597 {
2598 	memset(mbox, 0, sizeof(*mbox));
2599 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_FCFI);
2600 	bf_set(lpfc_unreg_fcfi, &mbox->u.mqe.un.unreg_fcfi, fcfi);
2601 }
2602 
2603 /**
2604  * lpfc_resume_rpi - Initialize the RESUME_RPI mailbox command
2605  * @mbox: pointer to lpfc mbox command to initialize.
2606  * @ndlp: The nodelist structure that describes the RPI to resume.
2607  *
2608  * The RESUME_RPI mailbox command is used to restart I/O to an RPI after a
2609  * link event.
2610  **/
2611 void
2612 lpfc_resume_rpi(struct lpfcMboxq *mbox, struct lpfc_nodelist *ndlp)
2613 {
2614 	struct lpfc_hba *phba = ndlp->phba;
2615 	struct lpfc_mbx_resume_rpi *resume_rpi;
2616 
2617 	memset(mbox, 0, sizeof(*mbox));
2618 	resume_rpi = &mbox->u.mqe.un.resume_rpi;
2619 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_RESUME_RPI);
2620 	bf_set(lpfc_resume_rpi_index, resume_rpi,
2621 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2622 	bf_set(lpfc_resume_rpi_ii, resume_rpi, RESUME_INDEX_RPI);
2623 	resume_rpi->event_tag = ndlp->phba->fc_eventTag;
2624 }
2625 
2626 /**
2627  * lpfc_supported_pages - Initialize the PORT_CAPABILITIES supported pages
2628  *                        mailbox command.
2629  * @mbox: pointer to lpfc mbox command to initialize.
2630  *
2631  * The PORT_CAPABILITIES supported pages mailbox command is issued to
2632  * retrieve the particular feature pages supported by the port.
2633  **/
2634 void
2635 lpfc_supported_pages(struct lpfcMboxq *mbox)
2636 {
2637 	struct lpfc_mbx_supp_pages *supp_pages;
2638 
2639 	memset(mbox, 0, sizeof(*mbox));
2640 	supp_pages = &mbox->u.mqe.un.supp_pages;
2641 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_PORT_CAPABILITIES);
2642 	bf_set(cpn, supp_pages, LPFC_SUPP_PAGES);
2643 }
2644 
2645 /**
2646  * lpfc_pc_sli4_params - Initialize the PORT_CAPABILITIES SLI4 Params mbox cmd.
2647  * @mbox: pointer to lpfc mbox command to initialize.
2648  *
2649  * The PORT_CAPABILITIES SLI4 parameters mailbox command is issued to
2650  * retrieve the particular SLI4 features supported by the port.
2651  **/
2652 void
2653 lpfc_pc_sli4_params(struct lpfcMboxq *mbox)
2654 {
2655 	struct lpfc_mbx_pc_sli4_params *sli4_params;
2656 
2657 	memset(mbox, 0, sizeof(*mbox));
2658 	sli4_params = &mbox->u.mqe.un.sli4_params;
2659 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_PORT_CAPABILITIES);
2660 	bf_set(cpn, sli4_params, LPFC_SLI4_PARAMETERS);
2661 }
2662