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