xref: /linux/drivers/scsi/lpfc/lpfc_sli.c (revision 08dbfad3f5040f5bdb6c529da20d6d4e81fefd72)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2026 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/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/seq_buf.h>
29 #include <linux/slab.h>
30 #include <linux/lockdep.h>
31 #include <linux/dmi.h>
32 #include <linux/of.h>
33 
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_cmnd.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport_fc.h>
39 #include <scsi/fc/fc_fs.h>
40 #include <linux/crash_dump.h>
41 #ifdef CONFIG_X86
42 #include <asm/set_memory.h>
43 #endif
44 
45 #include "lpfc_hw4.h"
46 #include "lpfc_hw.h"
47 #include "lpfc_sli.h"
48 #include "lpfc_sli4.h"
49 #include "lpfc_nl.h"
50 #include "lpfc_disc.h"
51 #include "lpfc.h"
52 #include "lpfc_scsi.h"
53 #include "lpfc_nvme.h"
54 #include "lpfc_crtn.h"
55 #include "lpfc_logmsg.h"
56 #include "lpfc_compat.h"
57 #include "lpfc_debugfs.h"
58 #include "lpfc_vport.h"
59 #include "lpfc_version.h"
60 
61 /* There are only four IOCB completion types. */
62 typedef enum _lpfc_iocb_type {
63 	LPFC_UNKNOWN_IOCB,
64 	LPFC_UNSOL_IOCB,
65 	LPFC_SOL_IOCB,
66 	LPFC_ABORT_IOCB
67 } lpfc_iocb_type;
68 
69 
70 /* Provide function prototypes local to this module. */
71 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
72 				  uint32_t);
73 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
74 			      uint8_t *, uint32_t *);
75 static struct lpfc_iocbq *
76 lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
77 				  struct lpfc_iocbq *rspiocbq);
78 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
79 				      struct hbq_dmabuf *);
80 static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
81 					  struct hbq_dmabuf *dmabuf);
82 static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
83 				   struct lpfc_queue *cq, struct lpfc_cqe *cqe);
84 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
85 				       int);
86 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
87 				     struct lpfc_queue *eq,
88 				     struct lpfc_eqe *eqe,
89 				     enum lpfc_poll_mode poll_mode);
90 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
91 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
92 static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);
93 static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,
94 				    struct lpfc_queue *cq,
95 				    struct lpfc_cqe *cqe);
96 static uint16_t lpfc_wqe_bpl2sgl(struct lpfc_hba *phba,
97 				 struct lpfc_iocbq *pwqeq,
98 				 struct lpfc_sglq *sglq);
99 
100 union lpfc_wqe128 lpfc_iread_cmd_template;
101 union lpfc_wqe128 lpfc_iwrite_cmd_template;
102 union lpfc_wqe128 lpfc_icmnd_cmd_template;
103 
104 /* Setup WQE templates for IOs */
lpfc_wqe_cmd_template(void)105 void lpfc_wqe_cmd_template(void)
106 {
107 	union lpfc_wqe128 *wqe;
108 
109 	/* IREAD template */
110 	wqe = &lpfc_iread_cmd_template;
111 	memset(wqe, 0, sizeof(union lpfc_wqe128));
112 
113 	/* Word 0, 1, 2 - BDE is variable */
114 
115 	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
116 
117 	/* Word 4 - total_xfer_len is variable */
118 
119 	/* Word 5 - is zero */
120 
121 	/* Word 6 - ctxt_tag, xri_tag is variable */
122 
123 	/* Word 7 */
124 	bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
125 	bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
126 	bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
127 	bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
128 
129 	/* Word 8 - abort_tag is variable */
130 
131 	/* Word 9  - reqtag is variable */
132 
133 	/* Word 10 - dbde, wqes is variable */
134 	bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
135 	bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
136 	bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
137 	bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
138 	bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
139 
140 	/* Word 11 */
141 	bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, COMMAND_DATA_IN);
142 	bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
143 
144 	/* Word 12 - is zero */
145 
146 	/* IWRITE template */
147 	wqe = &lpfc_iwrite_cmd_template;
148 	memset(wqe, 0, sizeof(union lpfc_wqe128));
149 
150 	/* Word 0, 1, 2 - BDE is variable */
151 
152 	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
153 
154 	/* Word 4 - total_xfer_len is variable */
155 
156 	/* Word 5 - initial_xfer_len is variable */
157 
158 	/* Word 6 - ctxt_tag, xri_tag is variable */
159 
160 	/* Word 7 */
161 	bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
162 	bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
163 	bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
164 	bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
165 
166 	/* Word 8 - abort_tag is variable */
167 
168 	/* Word 9  - reqtag is variable */
169 
170 	/* Word 10 - dbde, wqes is variable */
171 	bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
172 	bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
173 	bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
174 	bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
175 	bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
176 
177 	/* Word 11 */
178 	bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, COMMAND_DATA_OUT);
179 	bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
180 
181 	/* Word 12 - is zero */
182 
183 	/* ICMND template */
184 	wqe = &lpfc_icmnd_cmd_template;
185 	memset(wqe, 0, sizeof(union lpfc_wqe128));
186 
187 	/* Word 0, 1, 2 - BDE is variable */
188 
189 	/* Word 3 - payload_offset_len is variable */
190 
191 	/* Word 4, 5 - is zero */
192 
193 	/* Word 6 - ctxt_tag, xri_tag is variable */
194 
195 	/* Word 7 */
196 	bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
197 	bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
198 	bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
199 	bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
200 
201 	/* Word 8 - abort_tag is variable */
202 
203 	/* Word 9  - reqtag is variable */
204 
205 	/* Word 10 - dbde, wqes is variable */
206 	bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
207 	bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
208 	bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
209 	bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
210 	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
211 
212 	/* Word 11 */
213 	bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, COMMAND_DATA_IN);
214 	bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
215 
216 	/* Word 12, 13, 14, 15 - is zero */
217 }
218 
219 #if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
220 /**
221  * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
222  * @srcp: Source memory pointer.
223  * @destp: Destination memory pointer.
224  * @cnt: Number of words required to be copied.
225  *       Must be a multiple of sizeof(uint64_t)
226  *
227  * This function is used for copying data between driver memory
228  * and the SLI WQ. This function also changes the endianness
229  * of each word if native endianness is different from SLI
230  * endianness. This function can be called with or without
231  * lock.
232  **/
233 static void
lpfc_sli4_pcimem_bcopy(void * srcp,void * destp,uint32_t cnt)234 lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
235 {
236 	uint64_t *src = srcp;
237 	uint64_t *dest = destp;
238 	int i;
239 
240 	for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
241 		*dest++ = *src++;
242 }
243 #else
244 #define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
245 #endif
246 
247 /**
248  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
249  * @q: The Work Queue to operate on.
250  * @wqe: The work Queue Entry to put on the Work queue.
251  *
252  * This routine will copy the contents of @wqe to the next available entry on
253  * the @q. This function will then ring the Work Queue Doorbell to signal the
254  * HBA to start processing the Work Queue Entry. This function returns 0 if
255  * successful. If no entries are available on @q then this function will return
256  * -ENOMEM.
257  * The caller is expected to hold the hbalock when calling this routine.
258  **/
259 static int
lpfc_sli4_wq_put(struct lpfc_queue * q,union lpfc_wqe128 * wqe)260 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
261 {
262 	union lpfc_wqe *temp_wqe;
263 	struct lpfc_register doorbell;
264 	uint32_t host_index;
265 	uint32_t idx;
266 	uint32_t i = 0;
267 	uint8_t *tmp;
268 	u32 if_type;
269 
270 	/* sanity check on queue memory */
271 	if (unlikely(!q))
272 		return -ENOMEM;
273 
274 	temp_wqe = lpfc_sli4_qe(q, q->host_index);
275 
276 	/* If the host has not yet processed the next entry then we are done */
277 	idx = ((q->host_index + 1) % q->entry_count);
278 	if (idx == q->hba_index) {
279 		q->WQ_overflow++;
280 		return -EBUSY;
281 	}
282 	q->WQ_posted++;
283 	/* set consumption flag every once in a while */
284 	if (!((q->host_index + 1) % q->notify_interval))
285 		bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
286 	else
287 		bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
288 	if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
289 		bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
290 	lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
291 	if (q->dpp_enable && q->phba->cfg_enable_dpp) {
292 		/* write to DPP aperture taking advatage of Combined Writes */
293 		tmp = (uint8_t *)temp_wqe;
294 #ifdef __raw_writeq
295 		for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
296 			__raw_writeq(*((uint64_t *)(tmp + i)),
297 					q->dpp_regaddr + i);
298 #else
299 		for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
300 			__raw_writel(*((uint32_t *)(tmp + i)),
301 					q->dpp_regaddr + i);
302 #endif
303 	}
304 	/* ensure WQE bcopy and DPP flushed before doorbell write */
305 	wmb();
306 
307 	/* Update the host index before invoking device */
308 	host_index = q->host_index;
309 
310 	q->host_index = idx;
311 
312 	/* Ring Doorbell */
313 	doorbell.word0 = 0;
314 	if (q->db_format == LPFC_DB_LIST_FORMAT) {
315 		if (q->dpp_enable && q->phba->cfg_enable_dpp) {
316 			bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
317 			bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
318 			bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
319 			    q->dpp_id);
320 			bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
321 			    q->queue_id);
322 		} else {
323 			bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
324 			bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
325 
326 			/* Leave bits <23:16> clear for if_type 6 dpp */
327 			if_type = bf_get(lpfc_sli_intf_if_type,
328 					 &q->phba->sli4_hba.sli_intf);
329 			if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
330 				bf_set(lpfc_wq_db_list_fm_index, &doorbell,
331 				       host_index);
332 		}
333 	} else if (q->db_format == LPFC_DB_RING_FORMAT) {
334 		bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
335 		bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
336 	} else {
337 		return -EINVAL;
338 	}
339 	writel(doorbell.word0, q->db_regaddr);
340 
341 	return 0;
342 }
343 
344 /**
345  * lpfc_sli4_wq_release - Updates internal hba index for WQ
346  * @q: The Work Queue to operate on.
347  * @index: The index to advance the hba index to.
348  *
349  * This routine will update the HBA index of a queue to reflect consumption of
350  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
351  * an entry the host calls this function to update the queue's internal
352  * pointers.
353  **/
354 static void
lpfc_sli4_wq_release(struct lpfc_queue * q,uint32_t index)355 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
356 {
357 	/* sanity check on queue memory */
358 	if (unlikely(!q))
359 		return;
360 
361 	q->hba_index = index;
362 }
363 
364 /**
365  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
366  * @q: The Mailbox Queue to operate on.
367  * @mqe: The Mailbox Queue Entry to put on the Work queue.
368  *
369  * This routine will copy the contents of @mqe to the next available entry on
370  * the @q. This function will then ring the Work Queue Doorbell to signal the
371  * HBA to start processing the Work Queue Entry. This function returns 0 if
372  * successful. If no entries are available on @q then this function will return
373  * -ENOMEM.
374  * The caller is expected to hold the hbalock when calling this routine.
375  **/
376 static uint32_t
lpfc_sli4_mq_put(struct lpfc_queue * q,struct lpfc_mqe * mqe)377 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
378 {
379 	struct lpfc_mqe *temp_mqe;
380 	struct lpfc_register doorbell;
381 
382 	/* sanity check on queue memory */
383 	if (unlikely(!q))
384 		return -ENOMEM;
385 	temp_mqe = lpfc_sli4_qe(q, q->host_index);
386 
387 	/* If the host has not yet processed the next entry then we are done */
388 	if (((q->host_index + 1) % q->entry_count) == q->hba_index)
389 		return -ENOMEM;
390 	lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
391 	/* Save off the mailbox pointer for completion */
392 	q->phba->mbox = (MAILBOX_t *)temp_mqe;
393 
394 	/* Update the host index before invoking device */
395 	q->host_index = ((q->host_index + 1) % q->entry_count);
396 
397 	/* Ring Doorbell */
398 	doorbell.word0 = 0;
399 	bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
400 	bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
401 	writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
402 	return 0;
403 }
404 
405 /**
406  * lpfc_sli4_mq_release - Updates internal hba index for MQ
407  * @q: The Mailbox Queue to operate on.
408  *
409  * This routine will update the HBA index of a queue to reflect consumption of
410  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
411  * an entry the host calls this function to update the queue's internal
412  * pointers. This routine returns the number of entries that were consumed by
413  * the HBA.
414  **/
415 static uint32_t
lpfc_sli4_mq_release(struct lpfc_queue * q)416 lpfc_sli4_mq_release(struct lpfc_queue *q)
417 {
418 	/* sanity check on queue memory */
419 	if (unlikely(!q))
420 		return 0;
421 
422 	/* Clear the mailbox pointer for completion */
423 	q->phba->mbox = NULL;
424 	q->hba_index = ((q->hba_index + 1) % q->entry_count);
425 	return 1;
426 }
427 
428 /**
429  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
430  * @q: The Event Queue to get the first valid EQE from
431  *
432  * This routine will get the first valid Event Queue Entry from @q, update
433  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
434  * the Queue (no more work to do), or the Queue is full of EQEs that have been
435  * processed, but not popped back to the HBA then this routine will return NULL.
436  **/
437 static struct lpfc_eqe *
lpfc_sli4_eq_get(struct lpfc_queue * q)438 lpfc_sli4_eq_get(struct lpfc_queue *q)
439 {
440 	struct lpfc_eqe *eqe;
441 
442 	/* sanity check on queue memory */
443 	if (unlikely(!q))
444 		return NULL;
445 	eqe = lpfc_sli4_qe(q, q->host_index);
446 
447 	/* If the next EQE is not valid then we are done */
448 	if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
449 		return NULL;
450 
451 	/*
452 	 * insert barrier for instruction interlock : data from the hardware
453 	 * must have the valid bit checked before it can be copied and acted
454 	 * upon. Speculative instructions were allowing a bcopy at the start
455 	 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
456 	 * after our return, to copy data before the valid bit check above
457 	 * was done. As such, some of the copied data was stale. The barrier
458 	 * ensures the check is before any data is copied.
459 	 */
460 	mb();
461 	return eqe;
462 }
463 
464 /**
465  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
466  * @q: The Event Queue to disable interrupts
467  *
468  **/
469 void
lpfc_sli4_eq_clr_intr(struct lpfc_queue * q)470 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
471 {
472 	struct lpfc_register doorbell;
473 
474 	doorbell.word0 = 0;
475 	bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
476 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
477 	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
478 		(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
479 	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
480 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
481 }
482 
483 /**
484  * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
485  * @q: The Event Queue to disable interrupts
486  *
487  **/
488 void
lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue * q)489 lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
490 {
491 	struct lpfc_register doorbell;
492 
493 	doorbell.word0 = 0;
494 	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
495 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
496 }
497 
498 /**
499  * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
500  * @phba: adapter with EQ
501  * @q: The Event Queue that the host has completed processing for.
502  * @count: Number of elements that have been consumed
503  * @arm: Indicates whether the host wants to arms this CQ.
504  *
505  * This routine will notify the HBA, by ringing the doorbell, that count
506  * number of EQEs have been processed. The @arm parameter indicates whether
507  * the queue should be rearmed when ringing the doorbell.
508  **/
509 void
lpfc_sli4_write_eq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)510 lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
511 		     uint32_t count, bool arm)
512 {
513 	struct lpfc_register doorbell;
514 
515 	/* sanity check on queue memory */
516 	if (unlikely(!q || (count == 0 && !arm)))
517 		return;
518 
519 	/* ring doorbell for number popped */
520 	doorbell.word0 = 0;
521 	if (arm) {
522 		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
523 		bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
524 	}
525 	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
526 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
527 	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
528 			(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
529 	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
530 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
531 	/* PCI read to flush PCI pipeline on re-arming for INTx mode */
532 	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
533 		readl(q->phba->sli4_hba.EQDBregaddr);
534 }
535 
536 /**
537  * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
538  * @phba: adapter with EQ
539  * @q: The Event Queue that the host has completed processing for.
540  * @count: Number of elements that have been consumed
541  * @arm: Indicates whether the host wants to arms this CQ.
542  *
543  * This routine will notify the HBA, by ringing the doorbell, that count
544  * number of EQEs have been processed. The @arm parameter indicates whether
545  * the queue should be rearmed when ringing the doorbell.
546  **/
547 void
lpfc_sli4_if6_write_eq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)548 lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
549 			  uint32_t count, bool arm)
550 {
551 	struct lpfc_register doorbell;
552 
553 	/* sanity check on queue memory */
554 	if (unlikely(!q || (count == 0 && !arm)))
555 		return;
556 
557 	/* ring doorbell for number popped */
558 	doorbell.word0 = 0;
559 	if (arm)
560 		bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
561 	bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
562 	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
563 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
564 	/* PCI read to flush PCI pipeline on re-arming for INTx mode */
565 	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
566 		readl(q->phba->sli4_hba.EQDBregaddr);
567 }
568 
569 static void
__lpfc_sli4_consume_eqe(struct lpfc_hba * phba,struct lpfc_queue * eq,struct lpfc_eqe * eqe)570 __lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
571 			struct lpfc_eqe *eqe)
572 {
573 	if (!phba->sli4_hba.pc_sli4_params.eqav)
574 		bf_set_le32(lpfc_eqe_valid, eqe, 0);
575 
576 	eq->host_index = ((eq->host_index + 1) % eq->entry_count);
577 
578 	/* if the index wrapped around, toggle the valid bit */
579 	if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
580 		eq->qe_valid = (eq->qe_valid) ? 0 : 1;
581 }
582 
583 static void
lpfc_sli4_eqcq_flush(struct lpfc_hba * phba,struct lpfc_queue * eq)584 lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
585 {
586 	struct lpfc_eqe *eqe = NULL;
587 	u32 eq_count = 0, cq_count = 0;
588 	struct lpfc_cqe *cqe = NULL;
589 	struct lpfc_queue *cq = NULL, *childq = NULL;
590 	int cqid = 0;
591 
592 	/* walk all the EQ entries and drop on the floor */
593 	eqe = lpfc_sli4_eq_get(eq);
594 	while (eqe) {
595 		/* Get the reference to the corresponding CQ */
596 		cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
597 		cq = NULL;
598 
599 		list_for_each_entry(childq, &eq->child_list, list) {
600 			if (childq->queue_id == cqid) {
601 				cq = childq;
602 				break;
603 			}
604 		}
605 		/* If CQ is valid, iterate through it and drop all the CQEs */
606 		if (cq) {
607 			cqe = lpfc_sli4_cq_get(cq);
608 			while (cqe) {
609 				__lpfc_sli4_consume_cqe(phba, cq, cqe);
610 				cq_count++;
611 				cqe = lpfc_sli4_cq_get(cq);
612 			}
613 			/* Clear and re-arm the CQ */
614 			phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
615 			    LPFC_QUEUE_REARM);
616 			cq_count = 0;
617 		}
618 		__lpfc_sli4_consume_eqe(phba, eq, eqe);
619 		eq_count++;
620 		eqe = lpfc_sli4_eq_get(eq);
621 	}
622 
623 	/* Clear and re-arm the EQ */
624 	phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
625 }
626 
627 static int
lpfc_sli4_process_eq(struct lpfc_hba * phba,struct lpfc_queue * eq,u8 rearm,enum lpfc_poll_mode poll_mode)628 lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
629 		     u8 rearm, enum lpfc_poll_mode poll_mode)
630 {
631 	struct lpfc_eqe *eqe;
632 	int count = 0, consumed = 0;
633 
634 	if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
635 		goto rearm_and_exit;
636 
637 	eqe = lpfc_sli4_eq_get(eq);
638 	while (eqe) {
639 		lpfc_sli4_hba_handle_eqe(phba, eq, eqe, poll_mode);
640 		__lpfc_sli4_consume_eqe(phba, eq, eqe);
641 
642 		consumed++;
643 		if (!(++count % eq->max_proc_limit))
644 			break;
645 
646 		if (!(count % eq->notify_interval)) {
647 			phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
648 							LPFC_QUEUE_NOARM);
649 			consumed = 0;
650 		}
651 
652 		eqe = lpfc_sli4_eq_get(eq);
653 	}
654 	eq->EQ_processed += count;
655 
656 	/* Track the max number of EQEs processed in 1 intr */
657 	if (count > eq->EQ_max_eqe)
658 		eq->EQ_max_eqe = count;
659 
660 	xchg(&eq->queue_claimed, 0);
661 
662 rearm_and_exit:
663 	/* Always clear the EQ. */
664 	phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
665 
666 	return count;
667 }
668 
669 /**
670  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
671  * @q: The Completion Queue to get the first valid CQE from
672  *
673  * This routine will get the first valid Completion Queue Entry from @q, update
674  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
675  * the Queue (no more work to do), or the Queue is full of CQEs that have been
676  * processed, but not popped back to the HBA then this routine will return NULL.
677  **/
678 static struct lpfc_cqe *
lpfc_sli4_cq_get(struct lpfc_queue * q)679 lpfc_sli4_cq_get(struct lpfc_queue *q)
680 {
681 	struct lpfc_cqe *cqe;
682 
683 	/* sanity check on queue memory */
684 	if (unlikely(!q))
685 		return NULL;
686 	cqe = lpfc_sli4_qe(q, q->host_index);
687 
688 	/* If the next CQE is not valid then we are done */
689 	if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
690 		return NULL;
691 
692 	/*
693 	 * insert barrier for instruction interlock : data from the hardware
694 	 * must have the valid bit checked before it can be copied and acted
695 	 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
696 	 * instructions allowing action on content before valid bit checked,
697 	 * add barrier here as well. May not be needed as "content" is a
698 	 * single 32-bit entity here (vs multi word structure for cq's).
699 	 */
700 	mb();
701 	return cqe;
702 }
703 
704 static void
__lpfc_sli4_consume_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)705 __lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
706 			struct lpfc_cqe *cqe)
707 {
708 	if (!phba->sli4_hba.pc_sli4_params.cqav)
709 		bf_set_le32(lpfc_cqe_valid, cqe, 0);
710 
711 	cq->host_index = ((cq->host_index + 1) % cq->entry_count);
712 
713 	/* if the index wrapped around, toggle the valid bit */
714 	if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
715 		cq->qe_valid = (cq->qe_valid) ? 0 : 1;
716 }
717 
718 /**
719  * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
720  * @phba: the adapter with the CQ
721  * @q: The Completion Queue that the host has completed processing for.
722  * @count: the number of elements that were consumed
723  * @arm: Indicates whether the host wants to arms this CQ.
724  *
725  * This routine will notify the HBA, by ringing the doorbell, that the
726  * CQEs have been processed. The @arm parameter specifies whether the
727  * queue should be rearmed when ringing the doorbell.
728  **/
729 void
lpfc_sli4_write_cq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)730 lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
731 		     uint32_t count, bool arm)
732 {
733 	struct lpfc_register doorbell;
734 
735 	/* sanity check on queue memory */
736 	if (unlikely(!q || (count == 0 && !arm)))
737 		return;
738 
739 	/* ring doorbell for number popped */
740 	doorbell.word0 = 0;
741 	if (arm)
742 		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
743 	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
744 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
745 	bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
746 			(q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
747 	bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
748 	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
749 }
750 
751 /**
752  * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
753  * @phba: the adapter with the CQ
754  * @q: The Completion Queue that the host has completed processing for.
755  * @count: the number of elements that were consumed
756  * @arm: Indicates whether the host wants to arms this CQ.
757  *
758  * This routine will notify the HBA, by ringing the doorbell, that the
759  * CQEs have been processed. The @arm parameter specifies whether the
760  * queue should be rearmed when ringing the doorbell.
761  **/
762 void
lpfc_sli4_if6_write_cq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)763 lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
764 			 uint32_t count, bool arm)
765 {
766 	struct lpfc_register doorbell;
767 
768 	/* sanity check on queue memory */
769 	if (unlikely(!q || (count == 0 && !arm)))
770 		return;
771 
772 	/* ring doorbell for number popped */
773 	doorbell.word0 = 0;
774 	if (arm)
775 		bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
776 	bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
777 	bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
778 	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
779 }
780 
781 /*
782  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
783  *
784  * This routine will copy the contents of @wqe to the next available entry on
785  * the @q. This function will then ring the Receive Queue Doorbell to signal the
786  * HBA to start processing the Receive Queue Entry. This function returns the
787  * index that the rqe was copied to if successful. If no entries are available
788  * on @q then this function will return -ENOMEM.
789  * The caller is expected to hold the hbalock when calling this routine.
790  **/
791 int
lpfc_sli4_rq_put(struct lpfc_queue * hq,struct lpfc_queue * dq,struct lpfc_rqe * hrqe,struct lpfc_rqe * drqe)792 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
793 		 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
794 {
795 	struct lpfc_rqe *temp_hrqe;
796 	struct lpfc_rqe *temp_drqe;
797 	struct lpfc_register doorbell;
798 	int hq_put_index;
799 	int dq_put_index;
800 
801 	/* sanity check on queue memory */
802 	if (unlikely(!hq) || unlikely(!dq))
803 		return -ENOMEM;
804 	hq_put_index = hq->host_index;
805 	dq_put_index = dq->host_index;
806 	temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
807 	temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
808 
809 	if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
810 		return -EINVAL;
811 	if (hq_put_index != dq_put_index)
812 		return -EINVAL;
813 	/* If the host has not yet processed the next entry then we are done */
814 	if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
815 		return -EBUSY;
816 	lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
817 	lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
818 
819 	/* Update the host index to point to the next slot */
820 	hq->host_index = ((hq_put_index + 1) % hq->entry_count);
821 	dq->host_index = ((dq_put_index + 1) % dq->entry_count);
822 	hq->RQ_buf_posted++;
823 
824 	/* Ring The Header Receive Queue Doorbell */
825 	if (!(hq->host_index % hq->notify_interval)) {
826 		doorbell.word0 = 0;
827 		if (hq->db_format == LPFC_DB_RING_FORMAT) {
828 			bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
829 			       hq->notify_interval);
830 			bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
831 		} else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
832 			bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
833 			       hq->notify_interval);
834 			bf_set(lpfc_rq_db_list_fm_index, &doorbell,
835 			       hq->host_index);
836 			bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
837 		} else {
838 			return -EINVAL;
839 		}
840 		writel(doorbell.word0, hq->db_regaddr);
841 	}
842 	return hq_put_index;
843 }
844 
845 /*
846  * lpfc_sli4_rq_release - Updates internal hba index for RQ
847  *
848  * This routine will update the HBA index of a queue to reflect consumption of
849  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
850  * consumed an entry the host calls this function to update the queue's
851  * internal pointers. This routine returns the number of entries that were
852  * consumed by the HBA.
853  **/
854 static uint32_t
lpfc_sli4_rq_release(struct lpfc_queue * hq,struct lpfc_queue * dq)855 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
856 {
857 	/* sanity check on queue memory */
858 	if (unlikely(!hq) || unlikely(!dq))
859 		return 0;
860 
861 	if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
862 		return 0;
863 	hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
864 	dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
865 	return 1;
866 }
867 
868 /**
869  * lpfc_cmd_iocb - Get next command iocb entry in the ring
870  * @phba: Pointer to HBA context object.
871  * @pring: Pointer to driver SLI ring object.
872  *
873  * This function returns pointer to next command iocb entry
874  * in the command ring. The caller must hold hbalock to prevent
875  * other threads consume the next command iocb.
876  * SLI-2/SLI-3 provide different sized iocbs.
877  **/
878 static inline IOCB_t *
lpfc_cmd_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)879 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
880 {
881 	return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
882 			   pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
883 }
884 
885 /**
886  * lpfc_resp_iocb - Get next response iocb entry in the ring
887  * @phba: Pointer to HBA context object.
888  * @pring: Pointer to driver SLI ring object.
889  *
890  * This function returns pointer to next response iocb entry
891  * in the response ring. The caller must hold hbalock to make sure
892  * that no other thread consume the next response iocb.
893  * SLI-2/SLI-3 provide different sized iocbs.
894  **/
895 static inline IOCB_t *
lpfc_resp_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)896 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
897 {
898 	return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
899 			   pring->sli.sli3.rspidx * phba->iocb_rsp_size);
900 }
901 
902 /**
903  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
904  * @phba: Pointer to HBA context object.
905  *
906  * This function is called with hbalock held. This function
907  * allocates a new driver iocb object from the iocb pool. If the
908  * allocation is successful, it returns pointer to the newly
909  * allocated iocb object else it returns NULL.
910  **/
911 struct lpfc_iocbq *
__lpfc_sli_get_iocbq(struct lpfc_hba * phba)912 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
913 {
914 	struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
915 	struct lpfc_iocbq * iocbq = NULL;
916 
917 	lockdep_assert_held(&phba->hbalock);
918 
919 	list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
920 	if (iocbq)
921 		phba->iocb_cnt++;
922 	if (phba->iocb_cnt > phba->iocb_max)
923 		phba->iocb_max = phba->iocb_cnt;
924 	return iocbq;
925 }
926 
927 /**
928  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
929  * @phba: Pointer to HBA context object.
930  * @xritag: XRI value.
931  *
932  * This function clears the sglq pointer from the array of active
933  * sglq's. The xritag that is passed in is used to index into the
934  * array. Before the xritag can be used it needs to be adjusted
935  * by subtracting the xribase.
936  *
937  * Returns sglq ponter = success, NULL = Failure.
938  **/
939 struct lpfc_sglq *
__lpfc_clear_active_sglq(struct lpfc_hba * phba,uint16_t xritag)940 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
941 {
942 	struct lpfc_sglq *sglq;
943 
944 	sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
945 	phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
946 	return sglq;
947 }
948 
949 /**
950  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
951  * @phba: Pointer to HBA context object.
952  * @xritag: XRI value.
953  *
954  * This function returns the sglq pointer from the array of active
955  * sglq's. The xritag that is passed in is used to index into the
956  * array. Before the xritag can be used it needs to be adjusted
957  * by subtracting the xribase.
958  *
959  * Returns sglq ponter = success, NULL = Failure.
960  **/
961 struct lpfc_sglq *
__lpfc_get_active_sglq(struct lpfc_hba * phba,uint16_t xritag)962 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
963 {
964 	struct lpfc_sglq *sglq;
965 
966 	sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
967 	return sglq;
968 }
969 
970 /**
971  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
972  * @phba: Pointer to HBA context object.
973  * @xritag: xri used in this exchange.
974  * @rrq: The RRQ to be cleared.
975  *
976  **/
977 void
lpfc_clr_rrq_active(struct lpfc_hba * phba,uint16_t xritag,struct lpfc_node_rrq * rrq)978 lpfc_clr_rrq_active(struct lpfc_hba *phba,
979 		    uint16_t xritag,
980 		    struct lpfc_node_rrq *rrq)
981 {
982 	struct lpfc_nodelist *ndlp = NULL;
983 
984 	/* Lookup did to verify if did is still active on this vport */
985 	if (rrq->vport)
986 		ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
987 
988 	if (!ndlp)
989 		goto out;
990 
991 	if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
992 		rrq->send_rrq = 0;
993 		rrq->xritag = 0;
994 		rrq->rrq_stop_time = 0;
995 	}
996 out:
997 	mempool_free(rrq, phba->rrq_pool);
998 }
999 
1000 /**
1001  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
1002  * @phba: Pointer to HBA context object.
1003  *
1004  * This function is called with hbalock held. This function
1005  * Checks if stop_time (ratov from setting rrq active) has
1006  * been reached, if it has and the send_rrq flag is set then
1007  * it will call lpfc_send_rrq. If the send_rrq flag is not set
1008  * then it will just call the routine to clear the rrq and
1009  * free the rrq resource.
1010  * The timer is set to the next rrq that is going to expire before
1011  * leaving the routine.
1012  *
1013  **/
1014 void
lpfc_handle_rrq_active(struct lpfc_hba * phba)1015 lpfc_handle_rrq_active(struct lpfc_hba *phba)
1016 {
1017 	struct lpfc_node_rrq *rrq;
1018 	struct lpfc_node_rrq *nextrrq;
1019 	unsigned long next_time;
1020 	unsigned long iflags;
1021 	LIST_HEAD(send_rrq);
1022 
1023 	clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
1024 	next_time = jiffies + secs_to_jiffies(phba->fc_ratov + 1);
1025 	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
1026 	list_for_each_entry_safe(rrq, nextrrq,
1027 				 &phba->active_rrq_list, list) {
1028 		if (time_after(jiffies, rrq->rrq_stop_time))
1029 			list_move(&rrq->list, &send_rrq);
1030 		else if (time_before(rrq->rrq_stop_time, next_time))
1031 			next_time = rrq->rrq_stop_time;
1032 	}
1033 	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
1034 	if ((!list_empty(&phba->active_rrq_list)) &&
1035 	    (!test_bit(FC_UNLOADING, &phba->pport->load_flag)))
1036 		mod_timer(&phba->rrq_tmr, next_time);
1037 	list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
1038 		list_del(&rrq->list);
1039 		if (!rrq->send_rrq) {
1040 			/* this call will free the rrq */
1041 			lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1042 		} else if (lpfc_send_rrq(phba, rrq)) {
1043 			/* if we send the rrq then the completion handler
1044 			*  will clear the bit in the xribitmap.
1045 			*/
1046 			lpfc_clr_rrq_active(phba, rrq->xritag,
1047 					    rrq);
1048 		}
1049 	}
1050 }
1051 
1052 /**
1053  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
1054  * @vport: Pointer to vport context object.
1055  * @xri: The xri used in the exchange.
1056  * @did: The targets DID for this exchange.
1057  *
1058  * returns NULL = rrq not found in the phba->active_rrq_list.
1059  *         rrq = rrq for this xri and target.
1060  **/
1061 struct lpfc_node_rrq *
lpfc_get_active_rrq(struct lpfc_vport * vport,uint16_t xri,uint32_t did)1062 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
1063 {
1064 	struct lpfc_hba *phba = vport->phba;
1065 	struct lpfc_node_rrq *rrq;
1066 	struct lpfc_node_rrq *nextrrq;
1067 	unsigned long iflags;
1068 
1069 	if (phba->sli_rev != LPFC_SLI_REV4)
1070 		return NULL;
1071 	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
1072 	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1073 		if (rrq->vport == vport && rrq->xritag == xri &&
1074 				rrq->nlp_DID == did){
1075 			list_del(&rrq->list);
1076 			spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
1077 			return rrq;
1078 		}
1079 	}
1080 	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
1081 	return NULL;
1082 }
1083 
1084 /**
1085  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
1086  * @vport: Pointer to vport context object.
1087  * @ndlp: Pointer to the lpfc_node_list structure.
1088  * If ndlp is NULL Remove all active RRQs for this vport from the
1089  * phba->active_rrq_list and clear the rrq.
1090  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
1091  **/
1092 void
lpfc_cleanup_vports_rrqs(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)1093 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1094 
1095 {
1096 	struct lpfc_hba *phba = vport->phba;
1097 	struct lpfc_node_rrq *rrq;
1098 	struct lpfc_node_rrq *nextrrq;
1099 	unsigned long iflags;
1100 	LIST_HEAD(rrq_list);
1101 
1102 	if (phba->sli_rev != LPFC_SLI_REV4)
1103 		return;
1104 	if (!ndlp) {
1105 		lpfc_sli4_vport_delete_els_xri_aborted(vport);
1106 		lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
1107 	}
1108 	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
1109 	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1110 		if (rrq->vport != vport)
1111 			continue;
1112 
1113 		if (!ndlp || ndlp == lpfc_findnode_did(vport, rrq->nlp_DID))
1114 			list_move(&rrq->list, &rrq_list);
1115 
1116 	}
1117 	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
1118 
1119 	list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1120 		list_del(&rrq->list);
1121 		lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1122 	}
1123 }
1124 
1125 /**
1126  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
1127  * @phba: Pointer to HBA context object.
1128  * @ndlp: Targets nodelist pointer for this exchange.
1129  * @xritag: the xri in the bitmap to test.
1130  *
1131  * This function returns:
1132  * 0 = rrq not active for this xri
1133  * 1 = rrq is valid for this xri.
1134  **/
1135 int
lpfc_test_rrq_active(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,uint16_t xritag)1136 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1137 			uint16_t  xritag)
1138 {
1139 	if (!ndlp)
1140 		return 0;
1141 	if (!ndlp->active_rrqs_xri_bitmap)
1142 		return 0;
1143 	if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1144 		return 1;
1145 	else
1146 		return 0;
1147 }
1148 
1149 /**
1150  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1151  * @phba: Pointer to HBA context object.
1152  * @ndlp: nodelist pointer for this target.
1153  * @xritag: xri used in this exchange.
1154  * @rxid: Remote Exchange ID.
1155  * @send_rrq: Flag used to determine if we should send rrq els cmd.
1156  *
1157  * This function takes the hbalock.
1158  * The active bit is always set in the active rrq xri_bitmap even
1159  * if there is no slot available for the other rrq information.
1160  *
1161  * returns 0 rrq activated for this xri
1162  *         < 0 No memory or invalid ndlp.
1163  **/
1164 int
lpfc_set_rrq_active(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,uint16_t xritag,uint16_t rxid,uint16_t send_rrq)1165 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1166 		    uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
1167 {
1168 	unsigned long iflags;
1169 	struct lpfc_node_rrq *rrq;
1170 	int empty;
1171 
1172 	if (!ndlp)
1173 		return -EINVAL;
1174 
1175 	if (!phba->cfg_enable_rrq)
1176 		return -EINVAL;
1177 
1178 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
1179 		clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
1180 		goto outnl;
1181 	}
1182 
1183 	spin_lock_irqsave(&phba->hbalock, iflags);
1184 	if (ndlp->vport && test_bit(FC_UNLOADING, &ndlp->vport->load_flag))
1185 		goto out;
1186 
1187 	if (!ndlp->active_rrqs_xri_bitmap)
1188 		goto out;
1189 
1190 	if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1191 		goto out;
1192 
1193 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1194 	rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
1195 	if (!rrq) {
1196 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1197 				"3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1198 				" DID:0x%x Send:%d\n",
1199 				xritag, rxid, ndlp->nlp_DID, send_rrq);
1200 		return -EINVAL;
1201 	}
1202 	if (phba->cfg_enable_rrq == 1)
1203 		rrq->send_rrq = send_rrq;
1204 	else
1205 		rrq->send_rrq = 0;
1206 	rrq->xritag = xritag;
1207 	rrq->rrq_stop_time = jiffies + secs_to_jiffies(phba->fc_ratov + 1);
1208 	rrq->nlp_DID = ndlp->nlp_DID;
1209 	rrq->vport = ndlp->vport;
1210 	rrq->rxid = rxid;
1211 
1212 	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
1213 	empty = list_empty(&phba->active_rrq_list);
1214 	list_add_tail(&rrq->list, &phba->active_rrq_list);
1215 	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
1216 	set_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
1217 	if (empty)
1218 		lpfc_worker_wake_up(phba);
1219 	return 0;
1220 out:
1221 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1222 outnl:
1223 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1224 			"2921 Can't set rrq active xri:0x%x rxid:0x%x"
1225 			" DID:0x%x Send:%d\n",
1226 			xritag, rxid, ndlp->nlp_DID, send_rrq);
1227 	return -EINVAL;
1228 }
1229 
1230 /**
1231  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1232  * @phba: Pointer to HBA context object.
1233  * @piocbq: Pointer to the iocbq.
1234  *
1235  * The driver calls this function with either the nvme ls ring lock
1236  * or the fc els ring lock held depending on the iocb usage.  This function
1237  * gets a new driver sglq object from the sglq list. If the list is not empty
1238  * then it is successful, it returns pointer to the newly allocated sglq
1239  * object else it returns NULL.
1240  **/
1241 static struct lpfc_sglq *
__lpfc_sli_get_els_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1242 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1243 {
1244 	struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1245 	struct lpfc_sglq *sglq = NULL;
1246 	struct lpfc_sglq *start_sglq = NULL;
1247 	struct lpfc_io_buf *lpfc_cmd;
1248 	struct lpfc_nodelist *ndlp;
1249 	int found = 0;
1250 	u8 cmnd;
1251 
1252 	cmnd = get_job_cmnd(phba, piocbq);
1253 
1254 	if (piocbq->cmd_flag & LPFC_IO_FCP) {
1255 		lpfc_cmd = piocbq->io_buf;
1256 		ndlp = lpfc_cmd->rdata->pnode;
1257 	} else  if ((cmnd == CMD_GEN_REQUEST64_CR) &&
1258 			!(piocbq->cmd_flag & LPFC_IO_LIBDFC)) {
1259 		ndlp = piocbq->ndlp;
1260 	} else  if (piocbq->cmd_flag & LPFC_IO_LIBDFC) {
1261 		if (piocbq->cmd_flag & LPFC_IO_LOOPBACK)
1262 			ndlp = NULL;
1263 		else
1264 			ndlp = piocbq->ndlp;
1265 	} else {
1266 		ndlp = piocbq->ndlp;
1267 	}
1268 
1269 	spin_lock(&phba->sli4_hba.sgl_list_lock);
1270 	list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1271 	start_sglq = sglq;
1272 	while (!found) {
1273 		if (!sglq)
1274 			break;
1275 		if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1276 		    test_bit(sglq->sli4_lxritag,
1277 		    ndlp->active_rrqs_xri_bitmap)) {
1278 			/* This xri has an rrq outstanding for this DID.
1279 			 * put it back in the list and get another xri.
1280 			 */
1281 			list_add_tail(&sglq->list, lpfc_els_sgl_list);
1282 			sglq = NULL;
1283 			list_remove_head(lpfc_els_sgl_list, sglq,
1284 						struct lpfc_sglq, list);
1285 			if (sglq == start_sglq) {
1286 				list_add_tail(&sglq->list, lpfc_els_sgl_list);
1287 				sglq = NULL;
1288 				break;
1289 			} else
1290 				continue;
1291 		}
1292 		sglq->ndlp = ndlp;
1293 		found = 1;
1294 		phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1295 		sglq->state = SGL_ALLOCATED;
1296 	}
1297 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
1298 	return sglq;
1299 }
1300 
1301 /**
1302  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1303  * @phba: Pointer to HBA context object.
1304  * @piocbq: Pointer to the iocbq.
1305  *
1306  * This function is called with the sgl_list lock held. This function
1307  * gets a new driver sglq object from the sglq list. If the
1308  * list is not empty then it is successful, it returns pointer to the newly
1309  * allocated sglq object else it returns NULL.
1310  **/
1311 struct lpfc_sglq *
__lpfc_sli_get_nvmet_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1312 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1313 {
1314 	struct list_head *lpfc_nvmet_sgl_list;
1315 	struct lpfc_sglq *sglq = NULL;
1316 
1317 	lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1318 
1319 	lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1320 
1321 	list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1322 	if (!sglq)
1323 		return NULL;
1324 	phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1325 	sglq->state = SGL_ALLOCATED;
1326 	return sglq;
1327 }
1328 
1329 /**
1330  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1331  * @phba: Pointer to HBA context object.
1332  *
1333  * This function is called with no lock held. This function
1334  * allocates a new driver iocb object from the iocb pool. If the
1335  * allocation is successful, it returns pointer to the newly
1336  * allocated iocb object else it returns NULL.
1337  **/
1338 struct lpfc_iocbq *
lpfc_sli_get_iocbq(struct lpfc_hba * phba)1339 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1340 {
1341 	struct lpfc_iocbq * iocbq = NULL;
1342 	unsigned long iflags;
1343 
1344 	spin_lock_irqsave(&phba->hbalock, iflags);
1345 	iocbq = __lpfc_sli_get_iocbq(phba);
1346 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1347 	return iocbq;
1348 }
1349 
1350 /**
1351  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1352  * @phba: Pointer to HBA context object.
1353  * @iocbq: Pointer to driver iocb object.
1354  *
1355  * This function is called to release the driver iocb object
1356  * to the iocb pool. The iotag in the iocb object
1357  * does not change for each use of the iocb object. This function
1358  * clears all other fields of the iocb object when it is freed.
1359  * The sqlq structure that holds the xritag and phys and virtual
1360  * mappings for the scatter gather list is retrieved from the
1361  * active array of sglq. The get of the sglq pointer also clears
1362  * the entry in the array. If the status of the IO indiactes that
1363  * this IO was aborted then the sglq entry it put on the
1364  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1365  * IO has good status or fails for any other reason then the sglq
1366  * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1367  *  asserted held in the code path calling this routine.
1368  **/
1369 static void
__lpfc_sli_release_iocbq_s4(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1370 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1371 {
1372 	struct lpfc_sglq *sglq;
1373 	unsigned long iflag = 0;
1374 	struct lpfc_sli_ring *pring;
1375 
1376 	if (iocbq->sli4_xritag == NO_XRI)
1377 		sglq = NULL;
1378 	else
1379 		sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1380 
1381 
1382 	if (sglq)  {
1383 		if (iocbq->cmd_flag & LPFC_IO_NVMET) {
1384 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1385 					  iflag);
1386 			sglq->state = SGL_FREED;
1387 			sglq->ndlp = NULL;
1388 			list_add_tail(&sglq->list,
1389 				      &phba->sli4_hba.lpfc_nvmet_sgl_list);
1390 			spin_unlock_irqrestore(
1391 				&phba->sli4_hba.sgl_list_lock, iflag);
1392 			goto out;
1393 		}
1394 
1395 		if ((iocbq->cmd_flag & LPFC_EXCHANGE_BUSY) &&
1396 		    (!(unlikely(pci_channel_offline(phba->pcidev)))) &&
1397 		    sglq->state != SGL_XRI_ABORTED) {
1398 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1399 					  iflag);
1400 
1401 			/* Check if we can get a reference on ndlp */
1402 			if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1403 				sglq->ndlp = NULL;
1404 
1405 			list_add(&sglq->list,
1406 				 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1407 			spin_unlock_irqrestore(
1408 				&phba->sli4_hba.sgl_list_lock, iflag);
1409 		} else {
1410 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1411 					  iflag);
1412 			sglq->state = SGL_FREED;
1413 			sglq->ndlp = NULL;
1414 			list_add_tail(&sglq->list,
1415 				      &phba->sli4_hba.lpfc_els_sgl_list);
1416 			spin_unlock_irqrestore(
1417 				&phba->sli4_hba.sgl_list_lock, iflag);
1418 			pring = lpfc_phba_elsring(phba);
1419 			/* Check if TXQ queue needs to be serviced */
1420 			if (pring && (!list_empty(&pring->txq)))
1421 				lpfc_worker_wake_up(phba);
1422 		}
1423 	}
1424 
1425 out:
1426 	/*
1427 	 * Clean all volatile data fields, preserve iotag and node struct.
1428 	 */
1429 	memset_startat(iocbq, 0, wqe);
1430 	iocbq->sli4_lxritag = NO_XRI;
1431 	iocbq->sli4_xritag = NO_XRI;
1432 	iocbq->cmd_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |
1433 			      LPFC_IO_NVME_LS);
1434 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1435 }
1436 
1437 
1438 /**
1439  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1440  * @phba: Pointer to HBA context object.
1441  * @iocbq: Pointer to driver iocb object.
1442  *
1443  * This function is called to release the driver iocb object to the
1444  * iocb pool. The iotag in the iocb object does not change for each
1445  * use of the iocb object. This function clears all other fields of
1446  * the iocb object when it is freed. The hbalock is asserted held in
1447  * the code path calling this routine.
1448  **/
1449 static void
__lpfc_sli_release_iocbq_s3(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1450 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1451 {
1452 
1453 	/*
1454 	 * Clean all volatile data fields, preserve iotag and node struct.
1455 	 */
1456 	memset_startat(iocbq, 0, iocb);
1457 	iocbq->sli4_xritag = NO_XRI;
1458 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1459 }
1460 
1461 /**
1462  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1463  * @phba: Pointer to HBA context object.
1464  * @iocbq: Pointer to driver iocb object.
1465  *
1466  * This function is called with hbalock held to release driver
1467  * iocb object to the iocb pool. The iotag in the iocb object
1468  * does not change for each use of the iocb object. This function
1469  * clears all other fields of the iocb object when it is freed.
1470  **/
1471 static void
__lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1472 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1473 {
1474 	lockdep_assert_held(&phba->hbalock);
1475 
1476 	phba->__lpfc_sli_release_iocbq(phba, iocbq);
1477 	phba->iocb_cnt--;
1478 }
1479 
1480 /**
1481  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1482  * @phba: Pointer to HBA context object.
1483  * @iocbq: Pointer to driver iocb object.
1484  *
1485  * This function is called with no lock held to release the iocb to
1486  * iocb pool.
1487  **/
1488 void
lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1489 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1490 {
1491 	unsigned long iflags;
1492 
1493 	/*
1494 	 * Clean all volatile data fields, preserve iotag and node struct.
1495 	 */
1496 	spin_lock_irqsave(&phba->hbalock, iflags);
1497 	__lpfc_sli_release_iocbq(phba, iocbq);
1498 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1499 }
1500 
1501 /**
1502  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1503  * @phba: Pointer to HBA context object.
1504  * @iocblist: List of IOCBs.
1505  * @ulpstatus: ULP status in IOCB command field.
1506  * @ulpWord4: ULP word-4 in IOCB command field.
1507  *
1508  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1509  * on the list by invoking the complete callback function associated with the
1510  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1511  * fields.
1512  **/
1513 void
lpfc_sli_cancel_iocbs(struct lpfc_hba * phba,struct list_head * iocblist,uint32_t ulpstatus,uint32_t ulpWord4)1514 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1515 		      uint32_t ulpstatus, uint32_t ulpWord4)
1516 {
1517 	struct lpfc_iocbq *piocb;
1518 
1519 	while (!list_empty(iocblist)) {
1520 		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1521 		if (piocb->cmd_cmpl) {
1522 			if (piocb->cmd_flag & LPFC_IO_NVME) {
1523 				lpfc_nvme_cancel_iocb(phba, piocb,
1524 						      ulpstatus, ulpWord4);
1525 			} else {
1526 				if (phba->sli_rev == LPFC_SLI_REV4) {
1527 					bf_set(lpfc_wcqe_c_status,
1528 					       &piocb->wcqe_cmpl, ulpstatus);
1529 					piocb->wcqe_cmpl.parameter = ulpWord4;
1530 				} else {
1531 					piocb->iocb.ulpStatus = ulpstatus;
1532 					piocb->iocb.un.ulpWord[4] = ulpWord4;
1533 				}
1534 				(piocb->cmd_cmpl) (phba, piocb, piocb);
1535 			}
1536 		} else {
1537 			lpfc_sli_release_iocbq(phba, piocb);
1538 		}
1539 	}
1540 	return;
1541 }
1542 
1543 /**
1544  * lpfc_sli_iocb_cmd_type - Get the iocb type
1545  * @iocb_cmnd: iocb command code.
1546  *
1547  * This function is called by ring event handler function to get the iocb type.
1548  * This function translates the iocb command to an iocb command type used to
1549  * decide the final disposition of each completed IOCB.
1550  * The function returns
1551  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1552  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1553  * LPFC_ABORT_IOCB   if it is an abort iocb
1554  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1555  *
1556  * The caller is not required to hold any lock.
1557  **/
1558 static lpfc_iocb_type
lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)1559 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1560 {
1561 	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1562 
1563 	if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1564 		return 0;
1565 
1566 	switch (iocb_cmnd) {
1567 	case CMD_XMIT_SEQUENCE_CR:
1568 	case CMD_XMIT_SEQUENCE_CX:
1569 	case CMD_XMIT_BCAST_CN:
1570 	case CMD_XMIT_BCAST_CX:
1571 	case CMD_ELS_REQUEST_CR:
1572 	case CMD_ELS_REQUEST_CX:
1573 	case CMD_CREATE_XRI_CR:
1574 	case CMD_CREATE_XRI_CX:
1575 	case CMD_GET_RPI_CN:
1576 	case CMD_XMIT_ELS_RSP_CX:
1577 	case CMD_GET_RPI_CR:
1578 	case CMD_FCP_IWRITE_CR:
1579 	case CMD_FCP_IWRITE_CX:
1580 	case CMD_FCP_IREAD_CR:
1581 	case CMD_FCP_IREAD_CX:
1582 	case CMD_FCP_ICMND_CR:
1583 	case CMD_FCP_ICMND_CX:
1584 	case CMD_FCP_TSEND_CX:
1585 	case CMD_FCP_TRSP_CX:
1586 	case CMD_FCP_TRECEIVE_CX:
1587 	case CMD_FCP_AUTO_TRSP_CX:
1588 	case CMD_ADAPTER_MSG:
1589 	case CMD_ADAPTER_DUMP:
1590 	case CMD_XMIT_SEQUENCE64_CR:
1591 	case CMD_XMIT_SEQUENCE64_CX:
1592 	case CMD_XMIT_BCAST64_CN:
1593 	case CMD_XMIT_BCAST64_CX:
1594 	case CMD_ELS_REQUEST64_CR:
1595 	case CMD_ELS_REQUEST64_CX:
1596 	case CMD_FCP_IWRITE64_CR:
1597 	case CMD_FCP_IWRITE64_CX:
1598 	case CMD_FCP_IREAD64_CR:
1599 	case CMD_FCP_IREAD64_CX:
1600 	case CMD_FCP_ICMND64_CR:
1601 	case CMD_FCP_ICMND64_CX:
1602 	case CMD_FCP_TSEND64_CX:
1603 	case CMD_FCP_TRSP64_CX:
1604 	case CMD_FCP_TRECEIVE64_CX:
1605 	case CMD_GEN_REQUEST64_CR:
1606 	case CMD_GEN_REQUEST64_CX:
1607 	case CMD_XMIT_ELS_RSP64_CX:
1608 	case DSSCMD_IWRITE64_CR:
1609 	case DSSCMD_IWRITE64_CX:
1610 	case DSSCMD_IREAD64_CR:
1611 	case DSSCMD_IREAD64_CX:
1612 	case CMD_SEND_FRAME:
1613 		type = LPFC_SOL_IOCB;
1614 		break;
1615 	case CMD_ABORT_XRI_CN:
1616 	case CMD_ABORT_XRI_CX:
1617 	case CMD_CLOSE_XRI_CN:
1618 	case CMD_CLOSE_XRI_CX:
1619 	case CMD_XRI_ABORTED_CX:
1620 	case CMD_ABORT_MXRI64_CN:
1621 	case CMD_XMIT_BLS_RSP64_CX:
1622 		type = LPFC_ABORT_IOCB;
1623 		break;
1624 	case CMD_RCV_SEQUENCE_CX:
1625 	case CMD_RCV_ELS_REQ_CX:
1626 	case CMD_RCV_SEQUENCE64_CX:
1627 	case CMD_RCV_ELS_REQ64_CX:
1628 	case CMD_ASYNC_STATUS:
1629 	case CMD_IOCB_RCV_SEQ64_CX:
1630 	case CMD_IOCB_RCV_ELS64_CX:
1631 	case CMD_IOCB_RCV_CONT64_CX:
1632 	case CMD_IOCB_RET_XRI64_CX:
1633 		type = LPFC_UNSOL_IOCB;
1634 		break;
1635 	case CMD_IOCB_XMIT_MSEQ64_CR:
1636 	case CMD_IOCB_XMIT_MSEQ64_CX:
1637 	case CMD_IOCB_RCV_SEQ_LIST64_CX:
1638 	case CMD_IOCB_RCV_ELS_LIST64_CX:
1639 	case CMD_IOCB_CLOSE_EXTENDED_CN:
1640 	case CMD_IOCB_ABORT_EXTENDED_CN:
1641 	case CMD_IOCB_RET_HBQE64_CN:
1642 	case CMD_IOCB_FCP_IBIDIR64_CR:
1643 	case CMD_IOCB_FCP_IBIDIR64_CX:
1644 	case CMD_IOCB_FCP_ITASKMGT64_CX:
1645 	case CMD_IOCB_LOGENTRY_CN:
1646 	case CMD_IOCB_LOGENTRY_ASYNC_CN:
1647 		printk("%s - Unhandled SLI-3 Command x%x\n",
1648 				__func__, iocb_cmnd);
1649 		type = LPFC_UNKNOWN_IOCB;
1650 		break;
1651 	default:
1652 		type = LPFC_UNKNOWN_IOCB;
1653 		break;
1654 	}
1655 
1656 	return type;
1657 }
1658 
1659 /**
1660  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1661  * @phba: Pointer to HBA context object.
1662  *
1663  * This function is called from SLI initialization code
1664  * to configure every ring of the HBA's SLI interface. The
1665  * caller is not required to hold any lock. This function issues
1666  * a config_ring mailbox command for each ring.
1667  * This function returns zero if successful else returns a negative
1668  * error code.
1669  **/
1670 static int
lpfc_sli_ring_map(struct lpfc_hba * phba)1671 lpfc_sli_ring_map(struct lpfc_hba *phba)
1672 {
1673 	struct lpfc_sli *psli = &phba->sli;
1674 	LPFC_MBOXQ_t *pmb;
1675 	MAILBOX_t *pmbox;
1676 	int i, rc, ret = 0;
1677 
1678 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1679 	if (!pmb)
1680 		return -ENOMEM;
1681 	pmbox = &pmb->u.mb;
1682 	phba->link_state = LPFC_INIT_MBX_CMDS;
1683 	for (i = 0; i < psli->num_rings; i++) {
1684 		lpfc_config_ring(phba, i, pmb);
1685 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1686 		if (rc != MBX_SUCCESS) {
1687 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1688 					"0446 Adapter failed to init (%d), "
1689 					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
1690 					"ring %d\n",
1691 					rc, pmbox->mbxCommand,
1692 					pmbox->mbxStatus, i);
1693 			phba->link_state = LPFC_HBA_ERROR;
1694 			ret = -ENXIO;
1695 			break;
1696 		}
1697 	}
1698 	mempool_free(pmb, phba->mbox_mem_pool);
1699 	return ret;
1700 }
1701 
1702 /**
1703  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1704  * @phba: Pointer to HBA context object.
1705  * @pring: Pointer to driver SLI ring object.
1706  * @piocb: Pointer to the driver iocb object.
1707  *
1708  * The driver calls this function with the hbalock held for SLI3 ports or
1709  * the ring lock held for SLI4 ports. The function adds the
1710  * new iocb to txcmplq of the given ring. This function always returns
1711  * 0. If this function is called for ELS ring, this function checks if
1712  * there is a vport associated with the ELS command. This function also
1713  * starts els_tmofunc timer if this is an ELS command.
1714  **/
1715 static int
lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)1716 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1717 			struct lpfc_iocbq *piocb)
1718 {
1719 	u32 ulp_command = 0;
1720 
1721 	BUG_ON(!piocb);
1722 	ulp_command = get_job_cmnd(phba, piocb);
1723 
1724 	list_add_tail(&piocb->list, &pring->txcmplq);
1725 	piocb->cmd_flag |= LPFC_IO_ON_TXCMPLQ;
1726 	pring->txcmplq_cnt++;
1727 	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1728 	   (ulp_command != CMD_ABORT_XRI_WQE) &&
1729 	   (ulp_command != CMD_ABORT_XRI_CN) &&
1730 	   (ulp_command != CMD_CLOSE_XRI_CN)) {
1731 		BUG_ON(!piocb->vport);
1732 		if (!test_bit(FC_UNLOADING, &piocb->vport->load_flag))
1733 			mod_timer(&piocb->vport->els_tmofunc,
1734 				  jiffies + secs_to_jiffies(phba->fc_ratov << 1));
1735 	}
1736 
1737 	return 0;
1738 }
1739 
1740 /**
1741  * lpfc_sli_ringtx_get - Get first element of the txq
1742  * @phba: Pointer to HBA context object.
1743  * @pring: Pointer to driver SLI ring object.
1744  *
1745  * This function is called with hbalock held to get next
1746  * iocb in txq of the given ring. If there is any iocb in
1747  * the txq, the function returns first iocb in the list after
1748  * removing the iocb from the list, else it returns NULL.
1749  **/
1750 struct lpfc_iocbq *
lpfc_sli_ringtx_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)1751 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1752 {
1753 	struct lpfc_iocbq *cmd_iocb;
1754 
1755 	lockdep_assert_held(&phba->hbalock);
1756 
1757 	list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1758 	return cmd_iocb;
1759 }
1760 
1761 /**
1762  * lpfc_cmf_sync_cmpl - Process a CMF_SYNC_WQE cmpl
1763  * @phba: Pointer to HBA context object.
1764  * @cmdiocb: Pointer to driver command iocb object.
1765  * @rspiocb: Pointer to driver response iocb object.
1766  *
1767  * This routine will inform the driver of any BW adjustments we need
1768  * to make. These changes will be picked up during the next CMF
1769  * timer interrupt. In addition, any BW changes will be logged
1770  * with LOG_CGN_MGMT.
1771  **/
1772 static void
lpfc_cmf_sync_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1773 lpfc_cmf_sync_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1774 		   struct lpfc_iocbq *rspiocb)
1775 {
1776 	union lpfc_wqe128 *wqe;
1777 	uint32_t status, info;
1778 	struct lpfc_wcqe_complete *wcqe = &rspiocb->wcqe_cmpl;
1779 	uint64_t bw, bwdif, slop;
1780 	uint64_t pcent, bwpcent;
1781 	int asig, afpin, sigcnt, fpincnt;
1782 	int wsigmax, wfpinmax, cg, tdp;
1783 	char *s;
1784 
1785 	/* First check for error */
1786 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1787 	if (status) {
1788 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1789 				"6211 CMF_SYNC_WQE Error "
1790 				"req_tag x%x status x%x hwstatus x%x "
1791 				"tdatap x%x parm x%x\n",
1792 				bf_get(lpfc_wcqe_c_request_tag, wcqe),
1793 				bf_get(lpfc_wcqe_c_status, wcqe),
1794 				bf_get(lpfc_wcqe_c_hw_status, wcqe),
1795 				wcqe->total_data_placed,
1796 				wcqe->parameter);
1797 		goto out;
1798 	}
1799 
1800 	/* Gather congestion information on a successful cmpl */
1801 	info = wcqe->parameter;
1802 	phba->cmf_active_info = info;
1803 
1804 	/* See if firmware info count is valid or has changed */
1805 	if (info > LPFC_MAX_CMF_INFO || phba->cmf_info_per_interval == info)
1806 		info = 0;
1807 	else
1808 		phba->cmf_info_per_interval = info;
1809 
1810 	tdp = bf_get(lpfc_wcqe_c_cmf_bw, wcqe);
1811 	cg = bf_get(lpfc_wcqe_c_cmf_cg, wcqe);
1812 
1813 	/* Get BW requirement from firmware */
1814 	bw = (uint64_t)tdp * LPFC_CMF_BLK_SIZE;
1815 	if (!bw) {
1816 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1817 				"6212 CMF_SYNC_WQE x%x: NULL bw\n",
1818 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
1819 		goto out;
1820 	}
1821 
1822 	/* Gather information needed for logging if a BW change is required */
1823 	wqe = &cmdiocb->wqe;
1824 	asig = bf_get(cmf_sync_asig, &wqe->cmf_sync);
1825 	afpin = bf_get(cmf_sync_afpin, &wqe->cmf_sync);
1826 	fpincnt = bf_get(cmf_sync_wfpincnt, &wqe->cmf_sync);
1827 	sigcnt = bf_get(cmf_sync_wsigcnt, &wqe->cmf_sync);
1828 	if (phba->cmf_max_bytes_per_interval != bw ||
1829 	    (asig || afpin || sigcnt || fpincnt)) {
1830 		/* Are we increasing or decreasing BW */
1831 		if (phba->cmf_max_bytes_per_interval <  bw) {
1832 			bwdif = bw - phba->cmf_max_bytes_per_interval;
1833 			s = "Increase";
1834 		} else {
1835 			bwdif = phba->cmf_max_bytes_per_interval - bw;
1836 			s = "Decrease";
1837 		}
1838 
1839 		/* What is the change percentage */
1840 		slop = div_u64(phba->cmf_link_byte_count, 200); /*For rounding*/
1841 		pcent = div64_u64(bwdif * 100 + slop,
1842 				  phba->cmf_link_byte_count);
1843 		bwpcent = div64_u64(bw * 100 + slop,
1844 				    phba->cmf_link_byte_count);
1845 		/* Because of bytes adjustment due to shorter timer in
1846 		 * lpfc_cmf_timer() the cmf_link_byte_count can be shorter and
1847 		 * may seem like BW is above 100%.
1848 		 */
1849 		if (bwpcent > 100)
1850 			bwpcent = 100;
1851 
1852 		if (phba->cmf_max_bytes_per_interval < bw &&
1853 		    bwpcent > 95)
1854 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1855 					"6208 Congestion bandwidth "
1856 					"limits removed\n");
1857 		else if ((phba->cmf_max_bytes_per_interval > bw) &&
1858 			 ((bwpcent + pcent) <= 100) && ((bwpcent + pcent) > 95))
1859 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1860 					"6209 Congestion bandwidth "
1861 					"limits in effect\n");
1862 
1863 		if (asig) {
1864 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1865 					"6237 BW Threshold %lld%% (%lld): "
1866 					"%lld%% %s: Signal Alarm: cg:%d "
1867 					"Info:%u\n",
1868 					bwpcent, bw, pcent, s, cg,
1869 					phba->cmf_active_info);
1870 		} else if (afpin) {
1871 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1872 					"6238 BW Threshold %lld%% (%lld): "
1873 					"%lld%% %s: FPIN Alarm: cg:%d "
1874 					"Info:%u\n",
1875 					bwpcent, bw, pcent, s, cg,
1876 					phba->cmf_active_info);
1877 		} else if (sigcnt) {
1878 			wsigmax = bf_get(cmf_sync_wsigmax, &wqe->cmf_sync);
1879 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1880 					"6239 BW Threshold %lld%% (%lld): "
1881 					"%lld%% %s: Signal Warning: "
1882 					"Cnt %d Max %d: cg:%d Info:%u\n",
1883 					bwpcent, bw, pcent, s, sigcnt,
1884 					wsigmax, cg, phba->cmf_active_info);
1885 		} else if (fpincnt) {
1886 			wfpinmax = bf_get(cmf_sync_wfpinmax, &wqe->cmf_sync);
1887 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1888 					"6240 BW Threshold %lld%% (%lld): "
1889 					"%lld%% %s: FPIN Warning: "
1890 					"Cnt %d Max %d: cg:%d Info:%u\n",
1891 					bwpcent, bw, pcent, s, fpincnt,
1892 					wfpinmax, cg, phba->cmf_active_info);
1893 		} else {
1894 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1895 					"6241 BW Threshold %lld%% (%lld): "
1896 					"CMF %lld%% %s: cg:%d Info:%u\n",
1897 					bwpcent, bw, pcent, s, cg,
1898 					phba->cmf_active_info);
1899 		}
1900 	} else if (info) {
1901 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1902 				"6246 Info Threshold %u\n", info);
1903 	}
1904 
1905 	/* Save BW change to be picked up during next timer interrupt */
1906 	phba->cmf_last_sync_bw = bw;
1907 out:
1908 	lpfc_sli_release_iocbq(phba, cmdiocb);
1909 }
1910 
1911 /**
1912  * lpfc_issue_cmf_sync_wqe - Issue a CMF_SYNC_WQE
1913  * @phba: Pointer to HBA context object.
1914  * @ms:   ms to set in WQE interval, 0 means use init op
1915  * @total: Total rcv bytes for this interval
1916  *
1917  * This routine is called every CMF timer interrupt. Its purpose is
1918  * to issue a CMF_SYNC_WQE to the firmware to inform it of any events
1919  * that may indicate we have congestion (FPINs or Signals). Upon
1920  * completion, the firmware will indicate any BW restrictions the
1921  * driver may need to take.
1922  **/
1923 int
lpfc_issue_cmf_sync_wqe(struct lpfc_hba * phba,u32 ms,u64 total)1924 lpfc_issue_cmf_sync_wqe(struct lpfc_hba *phba, u32 ms, u64 total)
1925 {
1926 	union lpfc_wqe128 *wqe;
1927 	struct lpfc_iocbq *sync_buf;
1928 	unsigned long iflags;
1929 	u32 ret_val, cgn_sig_freq;
1930 	u32 atot, wtot, max;
1931 	u8 warn_sync_period = 0;
1932 
1933 	/* First address any alarm / warning activity */
1934 	atot = atomic_xchg(&phba->cgn_sync_alarm_cnt, 0);
1935 	wtot = atomic_xchg(&phba->cgn_sync_warn_cnt, 0);
1936 
1937 	spin_lock_irqsave(&phba->hbalock, iflags);
1938 
1939 	/* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
1940 	if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
1941 	    phba->link_state < LPFC_LINK_UP) {
1942 		ret_val = 0;
1943 		goto out_unlock;
1944 	}
1945 
1946 	sync_buf = __lpfc_sli_get_iocbq(phba);
1947 	if (!sync_buf) {
1948 		lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
1949 				"6244 No available WQEs for CMF_SYNC_WQE\n");
1950 		ret_val = ENOMEM;
1951 		goto out_unlock;
1952 	}
1953 
1954 	wqe = &sync_buf->wqe;
1955 
1956 	/* WQEs are reused.  Clear stale data and set key fields to zero */
1957 	memset(wqe, 0, sizeof(*wqe));
1958 
1959 	/* If this is the very first CMF_SYNC_WQE, issue an init operation */
1960 	if (!ms) {
1961 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1962 				"6441 CMF Init %d - CMF_SYNC_WQE\n",
1963 				phba->fc_eventTag);
1964 		bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
1965 		bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
1966 		goto initpath;
1967 	}
1968 
1969 	bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
1970 	bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
1971 
1972 	/* Check for alarms / warnings */
1973 	if (atot) {
1974 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1975 			/* We hit an Signal alarm condition */
1976 			bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
1977 		} else {
1978 			/* We hit a FPIN alarm condition */
1979 			bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
1980 		}
1981 	} else if (wtot) {
1982 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
1983 		    phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1984 			cgn_sig_freq = phba->cgn_sig_freq ? phba->cgn_sig_freq :
1985 					lpfc_fabric_cgn_frequency;
1986 			/* We hit an Signal warning condition */
1987 			max = LPFC_SEC_TO_MSEC / cgn_sig_freq *
1988 				lpfc_acqe_cgn_frequency;
1989 			bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
1990 			bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
1991 			warn_sync_period = lpfc_acqe_cgn_frequency;
1992 		} else {
1993 			/* We hit a FPIN warning condition */
1994 			bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
1995 			bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
1996 			if (phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ)
1997 				warn_sync_period =
1998 				LPFC_MSECS_TO_SECS(phba->cgn_fpin_frequency);
1999 		}
2000 	}
2001 
2002 	/* Update total read blocks during previous timer interval */
2003 	wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
2004 
2005 initpath:
2006 	bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
2007 	wqe->cmf_sync.event_tag = phba->fc_eventTag;
2008 	bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
2009 
2010 	/* Setup reqtag to match the wqe completion. */
2011 	bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
2012 
2013 	bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
2014 	bf_set(cmf_sync_period, &wqe->cmf_sync, warn_sync_period);
2015 
2016 	bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
2017 	bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
2018 	bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
2019 
2020 	sync_buf->vport = phba->pport;
2021 	sync_buf->cmd_cmpl = lpfc_cmf_sync_cmpl;
2022 	sync_buf->cmd_dmabuf = NULL;
2023 	sync_buf->rsp_dmabuf = NULL;
2024 	sync_buf->bpl_dmabuf = NULL;
2025 	sync_buf->sli4_xritag = NO_XRI;
2026 
2027 	sync_buf->cmd_flag |= LPFC_IO_CMF;
2028 	ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
2029 	if (ret_val) {
2030 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2031 				"6214 Cannot issue CMF_SYNC_WQE: x%x\n",
2032 				ret_val);
2033 		__lpfc_sli_release_iocbq(phba, sync_buf);
2034 	}
2035 out_unlock:
2036 	spin_unlock_irqrestore(&phba->hbalock, iflags);
2037 	return ret_val;
2038 }
2039 
2040 /**
2041  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
2042  * @phba: Pointer to HBA context object.
2043  * @pring: Pointer to driver SLI ring object.
2044  *
2045  * This function is called with hbalock held and the caller must post the
2046  * iocb without releasing the lock. If the caller releases the lock,
2047  * iocb slot returned by the function is not guaranteed to be available.
2048  * The function returns pointer to the next available iocb slot if there
2049  * is available slot in the ring, else it returns NULL.
2050  * If the get index of the ring is ahead of the put index, the function
2051  * will post an error attention event to the worker thread to take the
2052  * HBA to offline state.
2053  **/
2054 static IOCB_t *
lpfc_sli_next_iocb_slot(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2055 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2056 {
2057 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2058 	uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
2059 
2060 	lockdep_assert_held(&phba->hbalock);
2061 
2062 	if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
2063 	   (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
2064 		pring->sli.sli3.next_cmdidx = 0;
2065 
2066 	if (unlikely(pring->sli.sli3.local_getidx ==
2067 		pring->sli.sli3.next_cmdidx)) {
2068 
2069 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
2070 
2071 		if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
2072 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2073 					"0315 Ring %d issue: portCmdGet %d "
2074 					"is bigger than cmd ring %d\n",
2075 					pring->ringno,
2076 					pring->sli.sli3.local_getidx,
2077 					max_cmd_idx);
2078 
2079 			phba->link_state = LPFC_HBA_ERROR;
2080 			/*
2081 			 * All error attention handlers are posted to
2082 			 * worker thread
2083 			 */
2084 			phba->work_ha |= HA_ERATT;
2085 			phba->work_hs = HS_FFER3;
2086 
2087 			lpfc_worker_wake_up(phba);
2088 
2089 			return NULL;
2090 		}
2091 
2092 		if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
2093 			return NULL;
2094 	}
2095 
2096 	return lpfc_cmd_iocb(phba, pring);
2097 }
2098 
2099 /**
2100  * lpfc_sli_next_iotag - Get an iotag for the iocb
2101  * @phba: Pointer to HBA context object.
2102  * @iocbq: Pointer to driver iocb object.
2103  *
2104  * This function gets an iotag for the iocb. If there is no unused iotag and
2105  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
2106  * array and assigns a new iotag.
2107  * The function returns the allocated iotag if successful, else returns zero.
2108  * Zero is not a valid iotag.
2109  * The caller is not required to hold any lock.
2110  **/
2111 uint16_t
lpfc_sli_next_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)2112 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
2113 {
2114 	struct lpfc_iocbq **new_arr;
2115 	struct lpfc_iocbq **old_arr;
2116 	size_t new_len;
2117 	struct lpfc_sli *psli = &phba->sli;
2118 	uint16_t iotag;
2119 
2120 	spin_lock_irq(&phba->hbalock);
2121 	iotag = psli->last_iotag;
2122 	if(++iotag < psli->iocbq_lookup_len) {
2123 		psli->last_iotag = iotag;
2124 		psli->iocbq_lookup[iotag] = iocbq;
2125 		spin_unlock_irq(&phba->hbalock);
2126 		iocbq->iotag = iotag;
2127 		return iotag;
2128 	} else if (psli->iocbq_lookup_len < (0xffff
2129 					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
2130 		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2131 		spin_unlock_irq(&phba->hbalock);
2132 		new_arr = kzalloc_objs(struct lpfc_iocbq *, new_len);
2133 		if (new_arr) {
2134 			spin_lock_irq(&phba->hbalock);
2135 			old_arr = psli->iocbq_lookup;
2136 			if (new_len <= psli->iocbq_lookup_len) {
2137 				/* highly unprobable case */
2138 				kfree(new_arr);
2139 				iotag = psli->last_iotag;
2140 				if(++iotag < psli->iocbq_lookup_len) {
2141 					psli->last_iotag = iotag;
2142 					psli->iocbq_lookup[iotag] = iocbq;
2143 					spin_unlock_irq(&phba->hbalock);
2144 					iocbq->iotag = iotag;
2145 					return iotag;
2146 				}
2147 				spin_unlock_irq(&phba->hbalock);
2148 				return 0;
2149 			}
2150 			if (psli->iocbq_lookup)
2151 				memcpy(new_arr, old_arr,
2152 				       ((psli->last_iotag  + 1) *
2153 					sizeof (struct lpfc_iocbq *)));
2154 			psli->iocbq_lookup = new_arr;
2155 			psli->iocbq_lookup_len = new_len;
2156 			psli->last_iotag = iotag;
2157 			psli->iocbq_lookup[iotag] = iocbq;
2158 			spin_unlock_irq(&phba->hbalock);
2159 			iocbq->iotag = iotag;
2160 			kfree(old_arr);
2161 			return iotag;
2162 		}
2163 	} else
2164 		spin_unlock_irq(&phba->hbalock);
2165 
2166 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2167 			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",
2168 			psli->last_iotag);
2169 
2170 	return 0;
2171 }
2172 
2173 /**
2174  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
2175  * @phba: Pointer to HBA context object.
2176  * @pring: Pointer to driver SLI ring object.
2177  * @iocb: Pointer to iocb slot in the ring.
2178  * @nextiocb: Pointer to driver iocb object which need to be
2179  *            posted to firmware.
2180  *
2181  * This function is called to post a new iocb to the firmware. This
2182  * function copies the new iocb to ring iocb slot and updates the
2183  * ring pointers. It adds the new iocb to txcmplq if there is
2184  * a completion call back for this iocb else the function will free the
2185  * iocb object.  The hbalock is asserted held in the code path calling
2186  * this routine.
2187  **/
2188 static void
lpfc_sli_submit_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,IOCB_t * iocb,struct lpfc_iocbq * nextiocb)2189 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2190 		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
2191 {
2192 	/*
2193 	 * Set up an iotag
2194 	 */
2195 	nextiocb->iocb.ulpIoTag = (nextiocb->cmd_cmpl) ? nextiocb->iotag : 0;
2196 
2197 
2198 	if (pring->ringno == LPFC_ELS_RING) {
2199 		lpfc_debugfs_slow_ring_trc(phba,
2200 			"IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2201 			*(((uint32_t *) &nextiocb->iocb) + 4),
2202 			*(((uint32_t *) &nextiocb->iocb) + 6),
2203 			*(((uint32_t *) &nextiocb->iocb) + 7));
2204 	}
2205 
2206 	/*
2207 	 * Issue iocb command to adapter
2208 	 */
2209 	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
2210 	wmb();
2211 	pring->stats.iocb_cmd++;
2212 
2213 	/*
2214 	 * If there is no completion routine to call, we can release the
2215 	 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
2216 	 * that have no rsp ring completion, cmd_cmpl MUST be NULL.
2217 	 */
2218 	if (nextiocb->cmd_cmpl)
2219 		lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
2220 	else
2221 		__lpfc_sli_release_iocbq(phba, nextiocb);
2222 
2223 	/*
2224 	 * Let the HBA know what IOCB slot will be the next one the
2225 	 * driver will put a command into.
2226 	 */
2227 	pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
2228 	writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
2229 }
2230 
2231 /**
2232  * lpfc_sli_update_full_ring - Update the chip attention register
2233  * @phba: Pointer to HBA context object.
2234  * @pring: Pointer to driver SLI ring object.
2235  *
2236  * The caller is not required to hold any lock for calling this function.
2237  * This function updates the chip attention bits for the ring to inform firmware
2238  * that there are pending work to be done for this ring and requests an
2239  * interrupt when there is space available in the ring. This function is
2240  * called when the driver is unable to post more iocbs to the ring due
2241  * to unavailability of space in the ring.
2242  **/
2243 static void
lpfc_sli_update_full_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2244 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2245 {
2246 	int ringno = pring->ringno;
2247 
2248 	pring->flag |= LPFC_CALL_RING_AVAILABLE;
2249 
2250 	wmb();
2251 
2252 	/*
2253 	 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
2254 	 * The HBA will tell us when an IOCB entry is available.
2255 	 */
2256 	writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
2257 	readl(phba->CAregaddr); /* flush */
2258 
2259 	pring->stats.iocb_cmd_full++;
2260 }
2261 
2262 /**
2263  * lpfc_sli_update_ring - Update chip attention register
2264  * @phba: Pointer to HBA context object.
2265  * @pring: Pointer to driver SLI ring object.
2266  *
2267  * This function updates the chip attention register bit for the
2268  * given ring to inform HBA that there is more work to be done
2269  * in this ring. The caller is not required to hold any lock.
2270  **/
2271 static void
lpfc_sli_update_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2272 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2273 {
2274 	int ringno = pring->ringno;
2275 
2276 	/*
2277 	 * Tell the HBA that there is work to do in this ring.
2278 	 */
2279 	if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2280 		wmb();
2281 		writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2282 		readl(phba->CAregaddr); /* flush */
2283 	}
2284 }
2285 
2286 /**
2287  * lpfc_sli_resume_iocb - Process iocbs in the txq
2288  * @phba: Pointer to HBA context object.
2289  * @pring: Pointer to driver SLI ring object.
2290  *
2291  * This function is called with hbalock held to post pending iocbs
2292  * in the txq to the firmware. This function is called when driver
2293  * detects space available in the ring.
2294  **/
2295 static void
lpfc_sli_resume_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2296 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2297 {
2298 	IOCB_t *iocb;
2299 	struct lpfc_iocbq *nextiocb;
2300 
2301 	lockdep_assert_held(&phba->hbalock);
2302 
2303 	/*
2304 	 * Check to see if:
2305 	 *  (a) there is anything on the txq to send
2306 	 *  (b) link is up
2307 	 *  (c) link attention events can be processed (fcp ring only)
2308 	 *  (d) IOCB processing is not blocked by the outstanding mbox command.
2309 	 */
2310 
2311 	if (lpfc_is_link_up(phba) &&
2312 	    (!list_empty(&pring->txq)) &&
2313 	    (pring->ringno != LPFC_FCP_RING ||
2314 	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {
2315 
2316 		while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2317 		       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2318 			lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2319 
2320 		if (iocb)
2321 			lpfc_sli_update_ring(phba, pring);
2322 		else
2323 			lpfc_sli_update_full_ring(phba, pring);
2324 	}
2325 
2326 	return;
2327 }
2328 
2329 /**
2330  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
2331  * @phba: Pointer to HBA context object.
2332  * @hbqno: HBQ number.
2333  *
2334  * This function is called with hbalock held to get the next
2335  * available slot for the given HBQ. If there is free slot
2336  * available for the HBQ it will return pointer to the next available
2337  * HBQ entry else it will return NULL.
2338  **/
2339 static struct lpfc_hbq_entry *
lpfc_sli_next_hbq_slot(struct lpfc_hba * phba,uint32_t hbqno)2340 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2341 {
2342 	struct hbq_s *hbqp = &phba->hbqs[hbqno];
2343 
2344 	lockdep_assert_held(&phba->hbalock);
2345 
2346 	if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2347 	    ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2348 		hbqp->next_hbqPutIdx = 0;
2349 
2350 	if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
2351 		uint32_t raw_index = phba->hbq_get[hbqno];
2352 		uint32_t getidx = le32_to_cpu(raw_index);
2353 
2354 		hbqp->local_hbqGetIdx = getidx;
2355 
2356 		if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
2357 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2358 					"1802 HBQ %d: local_hbqGetIdx "
2359 					"%u is > than hbqp->entry_count %u\n",
2360 					hbqno, hbqp->local_hbqGetIdx,
2361 					hbqp->entry_count);
2362 
2363 			phba->link_state = LPFC_HBA_ERROR;
2364 			return NULL;
2365 		}
2366 
2367 		if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2368 			return NULL;
2369 	}
2370 
2371 	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2372 			hbqp->hbqPutIdx;
2373 }
2374 
2375 /**
2376  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
2377  * @phba: Pointer to HBA context object.
2378  *
2379  * This function is called with no lock held to free all the
2380  * hbq buffers while uninitializing the SLI interface. It also
2381  * frees the HBQ buffers returned by the firmware but not yet
2382  * processed by the upper layers.
2383  **/
2384 void
lpfc_sli_hbqbuf_free_all(struct lpfc_hba * phba)2385 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2386 {
2387 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2388 	struct hbq_dmabuf *hbq_buf;
2389 	unsigned long flags;
2390 	int i, hbq_count;
2391 
2392 	hbq_count = lpfc_sli_hbq_count();
2393 	/* Return all memory used by all HBQs */
2394 	spin_lock_irqsave(&phba->hbalock, flags);
2395 	for (i = 0; i < hbq_count; ++i) {
2396 		list_for_each_entry_safe(dmabuf, next_dmabuf,
2397 				&phba->hbqs[i].hbq_buffer_list, list) {
2398 			hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2399 			list_del(&hbq_buf->dbuf.list);
2400 			(phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2401 		}
2402 		phba->hbqs[i].buffer_count = 0;
2403 	}
2404 
2405 	/* Mark the HBQs not in use */
2406 	phba->hbq_in_use = 0;
2407 	spin_unlock_irqrestore(&phba->hbalock, flags);
2408 }
2409 
2410 /**
2411  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2412  * @phba: Pointer to HBA context object.
2413  * @hbqno: HBQ number.
2414  * @hbq_buf: Pointer to HBQ buffer.
2415  *
2416  * This function is called with the hbalock held to post a
2417  * hbq buffer to the firmware. If the function finds an empty
2418  * slot in the HBQ, it will post the buffer. The function will return
2419  * pointer to the hbq entry if it successfully post the buffer
2420  * else it will return NULL.
2421  **/
2422 static int
lpfc_sli_hbq_to_firmware(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2423 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2424 			 struct hbq_dmabuf *hbq_buf)
2425 {
2426 	lockdep_assert_held(&phba->hbalock);
2427 	return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2428 }
2429 
2430 /**
2431  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2432  * @phba: Pointer to HBA context object.
2433  * @hbqno: HBQ number.
2434  * @hbq_buf: Pointer to HBQ buffer.
2435  *
2436  * This function is called with the hbalock held to post a hbq buffer to the
2437  * firmware. If the function finds an empty slot in the HBQ, it will post the
2438  * buffer and place it on the hbq_buffer_list. The function will return zero if
2439  * it successfully post the buffer else it will return an error.
2440  **/
2441 static int
lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2442 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2443 			    struct hbq_dmabuf *hbq_buf)
2444 {
2445 	struct lpfc_hbq_entry *hbqe;
2446 	dma_addr_t physaddr = hbq_buf->dbuf.phys;
2447 
2448 	lockdep_assert_held(&phba->hbalock);
2449 	/* Get next HBQ entry slot to use */
2450 	hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2451 	if (hbqe) {
2452 		struct hbq_s *hbqp = &phba->hbqs[hbqno];
2453 
2454 		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2455 		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2456 		hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2457 		hbqe->bde.tus.f.bdeFlags = 0;
2458 		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2459 		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2460 				/* Sync SLIM */
2461 		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2462 		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2463 				/* flush */
2464 		readl(phba->hbq_put + hbqno);
2465 		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2466 		return 0;
2467 	} else
2468 		return -ENOMEM;
2469 }
2470 
2471 /**
2472  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2473  * @phba: Pointer to HBA context object.
2474  * @hbqno: HBQ number.
2475  * @hbq_buf: Pointer to HBQ buffer.
2476  *
2477  * This function is called with the hbalock held to post an RQE to the SLI4
2478  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2479  * the hbq_buffer_list and return zero, otherwise it will return an error.
2480  **/
2481 static int
lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2482 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2483 			    struct hbq_dmabuf *hbq_buf)
2484 {
2485 	int rc;
2486 	struct lpfc_rqe hrqe;
2487 	struct lpfc_rqe drqe;
2488 	struct lpfc_queue *hrq;
2489 	struct lpfc_queue *drq;
2490 
2491 	if (hbqno != LPFC_ELS_HBQ)
2492 		return 1;
2493 	hrq = phba->sli4_hba.hdr_rq;
2494 	drq = phba->sli4_hba.dat_rq;
2495 
2496 	lockdep_assert_held(&phba->hbalock);
2497 	hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2498 	hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2499 	drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2500 	drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2501 	rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2502 	if (rc < 0)
2503 		return rc;
2504 	hbq_buf->tag = (rc | (hbqno << 16));
2505 	list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2506 	return 0;
2507 }
2508 
2509 /* HBQ for ELS and CT traffic. */
2510 static struct lpfc_hbq_init lpfc_els_hbq = {
2511 	.rn = 1,
2512 	.entry_count = 256,
2513 	.mask_count = 0,
2514 	.profile = 0,
2515 	.ring_mask = (1 << LPFC_ELS_RING),
2516 	.buffer_count = 0,
2517 	.init_count = 40,
2518 	.add_count = 40,
2519 };
2520 
2521 /* Array of HBQs */
2522 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2523 	&lpfc_els_hbq,
2524 };
2525 
2526 /**
2527  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2528  * @phba: Pointer to HBA context object.
2529  * @hbqno: HBQ number.
2530  * @count: Number of HBQ buffers to be posted.
2531  *
2532  * This function is called with no lock held to post more hbq buffers to the
2533  * given HBQ. The function returns the number of HBQ buffers successfully
2534  * posted.
2535  **/
2536 static int
lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba * phba,uint32_t hbqno,uint32_t count)2537 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2538 {
2539 	uint32_t i, posted = 0;
2540 	unsigned long flags;
2541 	struct hbq_dmabuf *hbq_buffer;
2542 	LIST_HEAD(hbq_buf_list);
2543 	if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2544 		return 0;
2545 
2546 	if ((phba->hbqs[hbqno].buffer_count + count) >
2547 	    lpfc_hbq_defs[hbqno]->entry_count)
2548 		count = lpfc_hbq_defs[hbqno]->entry_count -
2549 					phba->hbqs[hbqno].buffer_count;
2550 	if (!count)
2551 		return 0;
2552 	/* Allocate HBQ entries */
2553 	for (i = 0; i < count; i++) {
2554 		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2555 		if (!hbq_buffer)
2556 			break;
2557 		list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2558 	}
2559 	/* Check whether HBQ is still in use */
2560 	spin_lock_irqsave(&phba->hbalock, flags);
2561 	if (!phba->hbq_in_use)
2562 		goto err;
2563 	while (!list_empty(&hbq_buf_list)) {
2564 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2565 				 dbuf.list);
2566 		hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2567 				      (hbqno << 16));
2568 		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2569 			phba->hbqs[hbqno].buffer_count++;
2570 			posted++;
2571 		} else
2572 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2573 	}
2574 	spin_unlock_irqrestore(&phba->hbalock, flags);
2575 	return posted;
2576 err:
2577 	spin_unlock_irqrestore(&phba->hbalock, flags);
2578 	while (!list_empty(&hbq_buf_list)) {
2579 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2580 				 dbuf.list);
2581 		(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2582 	}
2583 	return 0;
2584 }
2585 
2586 /**
2587  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2588  * @phba: Pointer to HBA context object.
2589  * @qno: HBQ number.
2590  *
2591  * This function posts more buffers to the HBQ. This function
2592  * is called with no lock held. The function returns the number of HBQ entries
2593  * successfully allocated.
2594  **/
2595 int
lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba * phba,uint32_t qno)2596 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2597 {
2598 	if (phba->sli_rev == LPFC_SLI_REV4)
2599 		return 0;
2600 	else
2601 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2602 					 lpfc_hbq_defs[qno]->add_count);
2603 }
2604 
2605 /**
2606  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2607  * @phba: Pointer to HBA context object.
2608  * @qno:  HBQ queue number.
2609  *
2610  * This function is called from SLI initialization code path with
2611  * no lock held to post initial HBQ buffers to firmware. The
2612  * function returns the number of HBQ entries successfully allocated.
2613  **/
2614 static int
lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba * phba,uint32_t qno)2615 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2616 {
2617 	if (phba->sli_rev == LPFC_SLI_REV4)
2618 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2619 					lpfc_hbq_defs[qno]->entry_count);
2620 	else
2621 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2622 					 lpfc_hbq_defs[qno]->init_count);
2623 }
2624 
2625 /*
2626  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2627  *
2628  * This function removes the first hbq buffer on an hbq list and returns a
2629  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2630  **/
2631 static struct hbq_dmabuf *
lpfc_sli_hbqbuf_get(struct list_head * rb_list)2632 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2633 {
2634 	struct lpfc_dmabuf *d_buf;
2635 
2636 	list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2637 	if (!d_buf)
2638 		return NULL;
2639 	return container_of(d_buf, struct hbq_dmabuf, dbuf);
2640 }
2641 
2642 /**
2643  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2644  * @phba: Pointer to HBA context object.
2645  * @hrq: HBQ number.
2646  *
2647  * This function removes the first RQ buffer on an RQ buffer list and returns a
2648  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2649  **/
2650 static struct rqb_dmabuf *
lpfc_sli_rqbuf_get(struct lpfc_hba * phba,struct lpfc_queue * hrq)2651 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2652 {
2653 	struct lpfc_dmabuf *h_buf;
2654 	struct lpfc_rqb *rqbp;
2655 
2656 	rqbp = hrq->rqbp;
2657 	list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2658 			 struct lpfc_dmabuf, list);
2659 	if (!h_buf)
2660 		return NULL;
2661 	rqbp->buffer_count--;
2662 	return container_of(h_buf, struct rqb_dmabuf, hbuf);
2663 }
2664 
2665 /**
2666  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2667  * @phba: Pointer to HBA context object.
2668  * @tag: Tag of the hbq buffer.
2669  *
2670  * This function searches for the hbq buffer associated with the given tag in
2671  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2672  * otherwise it returns NULL.
2673  **/
2674 static struct hbq_dmabuf *
lpfc_sli_hbqbuf_find(struct lpfc_hba * phba,uint32_t tag)2675 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2676 {
2677 	struct lpfc_dmabuf *d_buf;
2678 	struct hbq_dmabuf *hbq_buf;
2679 	uint32_t hbqno;
2680 
2681 	hbqno = tag >> 16;
2682 	if (hbqno >= LPFC_MAX_HBQS)
2683 		return NULL;
2684 
2685 	spin_lock_irq(&phba->hbalock);
2686 	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2687 		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2688 		if (hbq_buf->tag == tag) {
2689 			spin_unlock_irq(&phba->hbalock);
2690 			return hbq_buf;
2691 		}
2692 	}
2693 	spin_unlock_irq(&phba->hbalock);
2694 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2695 			"1803 Bad hbq tag. Data: x%x x%x\n",
2696 			tag, phba->hbqs[tag >> 16].buffer_count);
2697 	return NULL;
2698 }
2699 
2700 /**
2701  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2702  * @phba: Pointer to HBA context object.
2703  * @hbq_buffer: Pointer to HBQ buffer.
2704  *
2705  * This function is called with hbalock. This function gives back
2706  * the hbq buffer to firmware. If the HBQ does not have space to
2707  * post the buffer, it will free the buffer.
2708  **/
2709 void
lpfc_sli_free_hbq(struct lpfc_hba * phba,struct hbq_dmabuf * hbq_buffer)2710 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2711 {
2712 	uint32_t hbqno;
2713 
2714 	if (hbq_buffer) {
2715 		hbqno = hbq_buffer->tag >> 16;
2716 		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2717 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2718 	}
2719 }
2720 
2721 /**
2722  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2723  * @mbxCommand: mailbox command code.
2724  *
2725  * This function is called by the mailbox event handler function to verify
2726  * that the completed mailbox command is a legitimate mailbox command. If the
2727  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2728  * and the mailbox event handler will take the HBA offline.
2729  **/
2730 static int
lpfc_sli_chk_mbx_command(uint8_t mbxCommand)2731 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2732 {
2733 	uint8_t ret;
2734 
2735 	switch (mbxCommand) {
2736 	case MBX_LOAD_SM:
2737 	case MBX_READ_NV:
2738 	case MBX_WRITE_NV:
2739 	case MBX_WRITE_VPARMS:
2740 	case MBX_RUN_BIU_DIAG:
2741 	case MBX_INIT_LINK:
2742 	case MBX_DOWN_LINK:
2743 	case MBX_CONFIG_LINK:
2744 	case MBX_CONFIG_RING:
2745 	case MBX_RESET_RING:
2746 	case MBX_READ_CONFIG:
2747 	case MBX_READ_RCONFIG:
2748 	case MBX_READ_SPARM:
2749 	case MBX_READ_STATUS:
2750 	case MBX_READ_RPI:
2751 	case MBX_READ_XRI:
2752 	case MBX_READ_REV:
2753 	case MBX_READ_LNK_STAT:
2754 	case MBX_REG_LOGIN:
2755 	case MBX_UNREG_LOGIN:
2756 	case MBX_CLEAR_LA:
2757 	case MBX_DUMP_MEMORY:
2758 	case MBX_DUMP_CONTEXT:
2759 	case MBX_RUN_DIAGS:
2760 	case MBX_RESTART:
2761 	case MBX_UPDATE_CFG:
2762 	case MBX_DOWN_LOAD:
2763 	case MBX_DEL_LD_ENTRY:
2764 	case MBX_RUN_PROGRAM:
2765 	case MBX_SET_MASK:
2766 	case MBX_SET_VARIABLE:
2767 	case MBX_UNREG_D_ID:
2768 	case MBX_KILL_BOARD:
2769 	case MBX_CONFIG_FARP:
2770 	case MBX_BEACON:
2771 	case MBX_LOAD_AREA:
2772 	case MBX_RUN_BIU_DIAG64:
2773 	case MBX_CONFIG_PORT:
2774 	case MBX_READ_SPARM64:
2775 	case MBX_READ_RPI64:
2776 	case MBX_REG_LOGIN64:
2777 	case MBX_READ_TOPOLOGY:
2778 	case MBX_WRITE_WWN:
2779 	case MBX_SET_DEBUG:
2780 	case MBX_LOAD_EXP_ROM:
2781 	case MBX_ASYNCEVT_ENABLE:
2782 	case MBX_REG_VPI:
2783 	case MBX_UNREG_VPI:
2784 	case MBX_HEARTBEAT:
2785 	case MBX_PORT_CAPABILITIES:
2786 	case MBX_PORT_IOV_CONTROL:
2787 	case MBX_SLI4_CONFIG:
2788 	case MBX_SLI4_REQ_FTRS:
2789 	case MBX_REG_FCFI:
2790 	case MBX_UNREG_FCFI:
2791 	case MBX_REG_VFI:
2792 	case MBX_UNREG_VFI:
2793 	case MBX_INIT_VPI:
2794 	case MBX_INIT_VFI:
2795 	case MBX_RESUME_RPI:
2796 	case MBX_READ_EVENT_LOG_STATUS:
2797 	case MBX_READ_EVENT_LOG:
2798 	case MBX_SECURITY_MGMT:
2799 	case MBX_AUTH_PORT:
2800 	case MBX_ACCESS_VDATA:
2801 		ret = mbxCommand;
2802 		break;
2803 	default:
2804 		ret = MBX_SHUTDOWN;
2805 		break;
2806 	}
2807 	return ret;
2808 }
2809 
2810 /**
2811  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2812  * @phba: Pointer to HBA context object.
2813  * @pmboxq: Pointer to mailbox command.
2814  *
2815  * This is completion handler function for mailbox commands issued from
2816  * lpfc_sli_issue_mbox_wait function. This function is called by the
2817  * mailbox event handler function with no lock held. This function
2818  * will wake up thread waiting on the wait queue pointed by context1
2819  * of the mailbox.
2820  **/
2821 void
lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)2822 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2823 {
2824 	unsigned long drvr_flag;
2825 	struct completion *pmbox_done;
2826 
2827 	/*
2828 	 * If pmbox_done is empty, the driver thread gave up waiting and
2829 	 * continued running.
2830 	 */
2831 	pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2832 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
2833 	pmbox_done = pmboxq->ctx_u.mbox_wait;
2834 	if (pmbox_done)
2835 		complete(pmbox_done);
2836 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2837 	return;
2838 }
2839 
2840 /**
2841  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2842  * @phba: Pointer to HBA context object.
2843  * @pmb: Pointer to mailbox object.
2844  *
2845  * This function is the default mailbox completion handler. It
2846  * frees the memory resources associated with the completed mailbox
2847  * command. If the completed command is a REG_LOGIN mailbox command,
2848  * this function will issue a UREG_LOGIN to re-claim the RPI.
2849  **/
2850 void
lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2851 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2852 {
2853 	struct lpfc_vport  *vport = pmb->vport;
2854 	struct lpfc_dmabuf *mp;
2855 	struct lpfc_nodelist *ndlp;
2856 	struct Scsi_Host *shost;
2857 	uint16_t rpi, vpi;
2858 	int rc;
2859 
2860 	/*
2861 	 * If a REG_LOGIN succeeded  after node is destroyed or node
2862 	 * is in re-discovery driver need to cleanup the RPI.
2863 	 */
2864 	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
2865 	    pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2866 	    !pmb->u.mb.mbxStatus) {
2867 		mp = pmb->ctx_buf;
2868 		if (mp) {
2869 			pmb->ctx_buf = NULL;
2870 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
2871 			kfree(mp);
2872 		}
2873 		rpi = pmb->u.mb.un.varWords[0];
2874 		vpi = pmb->u.mb.un.varRegLogin.vpi;
2875 		if (phba->sli_rev == LPFC_SLI_REV4)
2876 			vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2877 		lpfc_unreg_login(phba, vpi, rpi, pmb);
2878 		pmb->vport = vport;
2879 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2880 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2881 		if (rc != MBX_NOT_FINISHED)
2882 			return;
2883 	}
2884 
2885 	if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2886 		!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
2887 		!pmb->u.mb.mbxStatus) {
2888 		shost = lpfc_shost_from_vport(vport);
2889 		spin_lock_irq(shost->host_lock);
2890 		vport->vpi_state |= LPFC_VPI_REGISTERED;
2891 		spin_unlock_irq(shost->host_lock);
2892 		clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
2893 	}
2894 
2895 	if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2896 		ndlp = pmb->ctx_ndlp;
2897 		lpfc_nlp_put(ndlp);
2898 	}
2899 
2900 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2901 		ndlp = pmb->ctx_ndlp;
2902 
2903 		/* Check to see if there are any deferred events to process */
2904 		if (ndlp) {
2905 			lpfc_printf_vlog(
2906 				vport,
2907 				KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2908 				"1438 UNREG cmpl deferred mbox x%x "
2909 				"on NPort x%x Data: x%lx x%x x%px x%lx x%x\n",
2910 				ndlp->nlp_rpi, ndlp->nlp_DID,
2911 				ndlp->nlp_flag, ndlp->nlp_defer_did,
2912 				ndlp, vport->load_flag, kref_read(&ndlp->kref));
2913 
2914 			if (test_bit(NLP_UNREG_INP, &ndlp->nlp_flag) &&
2915 			    ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING) {
2916 				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
2917 				ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2918 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2919 			} else {
2920 				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
2921 			}
2922 
2923 			/* The unreg_login mailbox is complete and had a
2924 			 * reference that has to be released.  The PLOGI
2925 			 * got its own ref.
2926 			 */
2927 			lpfc_nlp_put(ndlp);
2928 			pmb->ctx_ndlp = NULL;
2929 		}
2930 	}
2931 
2932 	/* This nlp_put pairs with lpfc_sli4_resume_rpi */
2933 	if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
2934 		ndlp = pmb->ctx_ndlp;
2935 		lpfc_nlp_put(ndlp);
2936 	}
2937 
2938 	/* Check security permission status on INIT_LINK mailbox command */
2939 	if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2940 	    (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2941 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2942 				"2860 SLI authentication is required "
2943 				"for INIT_LINK but has not done yet\n");
2944 
2945 	if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2946 		lpfc_sli4_mbox_cmd_free(phba, pmb);
2947 	else
2948 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
2949 }
2950  /**
2951  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2952  * @phba: Pointer to HBA context object.
2953  * @pmb: Pointer to mailbox object.
2954  *
2955  * This function is the unreg rpi mailbox completion handler. It
2956  * frees the memory resources associated with the completed mailbox
2957  * command. An additional reference is put on the ndlp to prevent
2958  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2959  * the unreg mailbox command completes, this routine puts the
2960  * reference back.
2961  *
2962  **/
2963 void
lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2964 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2965 {
2966 	struct lpfc_vport  *vport = pmb->vport;
2967 	struct lpfc_nodelist *ndlp;
2968 	bool unreg_inp;
2969 
2970 	ndlp = pmb->ctx_ndlp;
2971 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2972 		if (phba->sli_rev == LPFC_SLI_REV4 &&
2973 		    (bf_get(lpfc_sli_intf_if_type,
2974 		     &phba->sli4_hba.sli_intf) >=
2975 		     LPFC_SLI_INTF_IF_TYPE_2)) {
2976 			if (ndlp) {
2977 				lpfc_printf_vlog(
2978 					 vport, KERN_INFO,
2979 					 LOG_MBOX | LOG_SLI | LOG_NODE,
2980 					 "0010 UNREG_LOGIN vpi:x%x "
2981 					 "rpi:%x DID:%x defer x%x flg x%lx "
2982 					 "x%px\n",
2983 					 vport->vpi, ndlp->nlp_rpi,
2984 					 ndlp->nlp_DID, ndlp->nlp_defer_did,
2985 					 ndlp->nlp_flag,
2986 					 ndlp);
2987 
2988 				/* Cleanup the nlp_flag now that the UNREG RPI
2989 				 * has completed.
2990 				 */
2991 				unreg_inp = test_and_clear_bit(NLP_UNREG_INP,
2992 							       &ndlp->nlp_flag);
2993 				clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
2994 
2995 				/* Check to see if there are any deferred
2996 				 * events to process
2997 				 */
2998 				if (unreg_inp &&
2999 				    ndlp->nlp_defer_did !=
3000 				    NLP_EVT_NOTHING_PENDING) {
3001 					lpfc_printf_vlog(
3002 						vport, KERN_INFO,
3003 						LOG_MBOX | LOG_SLI | LOG_NODE,
3004 						"4111 UNREG cmpl deferred "
3005 						"clr x%x on "
3006 						"NPort x%x Data: x%x x%px\n",
3007 						ndlp->nlp_rpi, ndlp->nlp_DID,
3008 						ndlp->nlp_defer_did, ndlp);
3009 					ndlp->nlp_defer_did =
3010 						NLP_EVT_NOTHING_PENDING;
3011 					lpfc_issue_els_plogi(
3012 						vport, ndlp->nlp_DID, 0);
3013 				}
3014 
3015 				lpfc_nlp_put(ndlp);
3016 			}
3017 		}
3018 	}
3019 
3020 	mempool_free(pmb, phba->mbox_mem_pool);
3021 }
3022 
3023 /**
3024  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
3025  * @phba: Pointer to HBA context object.
3026  *
3027  * This function is called with no lock held. This function processes all
3028  * the completed mailbox commands and gives it to upper layers. The interrupt
3029  * service routine processes mailbox completion interrupt and adds completed
3030  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
3031  * Worker thread call lpfc_sli_handle_mb_event, which will return the
3032  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
3033  * function returns the mailbox commands to the upper layer by calling the
3034  * completion handler function of each mailbox.
3035  **/
3036 int
lpfc_sli_handle_mb_event(struct lpfc_hba * phba)3037 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
3038 {
3039 	MAILBOX_t *pmbox;
3040 	LPFC_MBOXQ_t *pmb;
3041 	int rc;
3042 	LIST_HEAD(cmplq);
3043 
3044 	phba->sli.slistat.mbox_event++;
3045 
3046 	/* Get all completed mailboxe buffers into the cmplq */
3047 	spin_lock_irq(&phba->hbalock);
3048 	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
3049 	spin_unlock_irq(&phba->hbalock);
3050 
3051 	/* Get a Mailbox buffer to setup mailbox commands for callback */
3052 	do {
3053 		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
3054 		if (pmb == NULL)
3055 			break;
3056 
3057 		pmbox = &pmb->u.mb;
3058 
3059 		if (pmbox->mbxCommand != MBX_HEARTBEAT) {
3060 			if (pmb->vport) {
3061 				lpfc_debugfs_disc_trc(pmb->vport,
3062 					LPFC_DISC_TRC_MBOX_VPORT,
3063 					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",
3064 					(uint32_t)pmbox->mbxCommand,
3065 					pmbox->un.varWords[0],
3066 					pmbox->un.varWords[1]);
3067 			}
3068 			else {
3069 				lpfc_debugfs_disc_trc(phba->pport,
3070 					LPFC_DISC_TRC_MBOX,
3071 					"MBOX cmpl:       cmd:x%x mb:x%x x%x",
3072 					(uint32_t)pmbox->mbxCommand,
3073 					pmbox->un.varWords[0],
3074 					pmbox->un.varWords[1]);
3075 			}
3076 		}
3077 
3078 		/*
3079 		 * It is a fatal error if unknown mbox command completion.
3080 		 */
3081 		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
3082 		    MBX_SHUTDOWN) {
3083 			/* Unknown mailbox command compl */
3084 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3085 					"(%d):0323 Unknown Mailbox command "
3086 					"x%x (x%x/x%x) Cmpl\n",
3087 					pmb->vport ? pmb->vport->vpi :
3088 					LPFC_VPORT_UNKNOWN,
3089 					pmbox->mbxCommand,
3090 					lpfc_sli_config_mbox_subsys_get(phba,
3091 									pmb),
3092 					lpfc_sli_config_mbox_opcode_get(phba,
3093 									pmb));
3094 			phba->link_state = LPFC_HBA_ERROR;
3095 			phba->work_hs = HS_FFER3;
3096 			lpfc_handle_eratt(phba);
3097 			continue;
3098 		}
3099 
3100 		if (pmbox->mbxStatus) {
3101 			phba->sli.slistat.mbox_stat_err++;
3102 			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
3103 				/* Mbox cmd cmpl error - RETRYing */
3104 				lpfc_printf_log(phba, KERN_INFO,
3105 					LOG_MBOX | LOG_SLI,
3106 					"(%d):0305 Mbox cmd cmpl "
3107 					"error - RETRYing Data: x%x "
3108 					"(x%x/x%x) x%x x%x x%x\n",
3109 					pmb->vport ? pmb->vport->vpi :
3110 					LPFC_VPORT_UNKNOWN,
3111 					pmbox->mbxCommand,
3112 					lpfc_sli_config_mbox_subsys_get(phba,
3113 									pmb),
3114 					lpfc_sli_config_mbox_opcode_get(phba,
3115 									pmb),
3116 					pmbox->mbxStatus,
3117 					pmbox->un.varWords[0],
3118 					pmb->vport ? pmb->vport->port_state :
3119 					LPFC_VPORT_UNKNOWN);
3120 				pmbox->mbxStatus = 0;
3121 				pmbox->mbxOwner = OWN_HOST;
3122 				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3123 				if (rc != MBX_NOT_FINISHED)
3124 					continue;
3125 			}
3126 		}
3127 
3128 		/* Mailbox cmd <cmd> Cmpl <cmpl> */
3129 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3130 				"(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
3131 				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3132 				"x%x x%x x%x\n",
3133 				pmb->vport ? pmb->vport->vpi : 0,
3134 				pmbox->mbxCommand,
3135 				lpfc_sli_config_mbox_subsys_get(phba, pmb),
3136 				lpfc_sli_config_mbox_opcode_get(phba, pmb),
3137 				pmb->mbox_cmpl,
3138 				*((uint32_t *) pmbox),
3139 				pmbox->un.varWords[0],
3140 				pmbox->un.varWords[1],
3141 				pmbox->un.varWords[2],
3142 				pmbox->un.varWords[3],
3143 				pmbox->un.varWords[4],
3144 				pmbox->un.varWords[5],
3145 				pmbox->un.varWords[6],
3146 				pmbox->un.varWords[7],
3147 				pmbox->un.varWords[8],
3148 				pmbox->un.varWords[9],
3149 				pmbox->un.varWords[10]);
3150 
3151 		if (pmb->mbox_cmpl)
3152 			pmb->mbox_cmpl(phba,pmb);
3153 	} while (1);
3154 	return 0;
3155 }
3156 
3157 /**
3158  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
3159  * @phba: Pointer to HBA context object.
3160  * @pring: Pointer to driver SLI ring object.
3161  * @tag: buffer tag.
3162  *
3163  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
3164  * is set in the tag the buffer is posted for a particular exchange,
3165  * the function will return the buffer without replacing the buffer.
3166  * If the buffer is for unsolicited ELS or CT traffic, this function
3167  * returns the buffer and also posts another buffer to the firmware.
3168  **/
3169 static struct lpfc_dmabuf *
lpfc_sli_get_buff(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)3170 lpfc_sli_get_buff(struct lpfc_hba *phba,
3171 		  struct lpfc_sli_ring *pring,
3172 		  uint32_t tag)
3173 {
3174 	struct hbq_dmabuf *hbq_entry;
3175 
3176 	if (tag & QUE_BUFTAG_BIT)
3177 		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
3178 	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
3179 	if (!hbq_entry)
3180 		return NULL;
3181 	return &hbq_entry->dbuf;
3182 }
3183 
3184 /**
3185  * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
3186  *                              containing a NVME LS request.
3187  * @phba: pointer to lpfc hba data structure.
3188  * @piocb: pointer to the iocbq struct representing the sequence starting
3189  *        frame.
3190  *
3191  * This routine initially validates the NVME LS, validates there is a login
3192  * with the port that sent the LS, and then calls the appropriate nvme host
3193  * or target LS request handler.
3194  **/
3195 static void
lpfc_nvme_unsol_ls_handler(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)3196 lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
3197 {
3198 	struct lpfc_nodelist *ndlp;
3199 	struct lpfc_dmabuf *d_buf;
3200 	struct hbq_dmabuf *nvmebuf;
3201 	struct fc_frame_header *fc_hdr;
3202 	struct lpfc_async_xchg_ctx *axchg = NULL;
3203 	char *failwhy = NULL;
3204 	uint32_t oxid, sid, did, fctl, size;
3205 	int ret = 1;
3206 
3207 	d_buf = piocb->cmd_dmabuf;
3208 
3209 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
3210 	fc_hdr = nvmebuf->hbuf.virt;
3211 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
3212 	sid = sli4_sid_from_fc_hdr(fc_hdr);
3213 	did = sli4_did_from_fc_hdr(fc_hdr);
3214 	fctl = (fc_hdr->fh_f_ctl[0] << 16 |
3215 		fc_hdr->fh_f_ctl[1] << 8 |
3216 		fc_hdr->fh_f_ctl[2]);
3217 	size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
3218 
3219 	lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
3220 			 oxid, size, sid);
3221 
3222 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
3223 		failwhy = "Driver Unloading";
3224 	} else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
3225 		failwhy = "NVME FC4 Disabled";
3226 	} else if (!phba->nvmet_support && !phba->pport->localport) {
3227 		failwhy = "No Localport";
3228 	} else if (phba->nvmet_support && !phba->targetport) {
3229 		failwhy = "No Targetport";
3230 	} else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
3231 		failwhy = "Bad NVME LS R_CTL";
3232 	} else if (unlikely((fctl & 0x00FF0000) !=
3233 			(FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
3234 		failwhy = "Bad NVME LS F_CTL";
3235 	} else {
3236 		axchg = kzalloc_obj(*axchg, GFP_ATOMIC);
3237 		if (!axchg)
3238 			failwhy = "No CTX memory";
3239 	}
3240 
3241 	if (unlikely(failwhy)) {
3242 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3243 				"6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
3244 				sid, oxid, failwhy);
3245 		goto out_fail;
3246 	}
3247 
3248 	/* validate the source of the LS is logged in */
3249 	ndlp = lpfc_findnode_did(phba->pport, sid);
3250 	if (!ndlp ||
3251 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3252 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3253 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
3254 				"6216 NVME Unsol rcv: No ndlp: "
3255 				"NPort_ID x%x oxid x%x\n",
3256 				sid, oxid);
3257 		goto out_fail;
3258 	}
3259 
3260 	axchg->phba = phba;
3261 	axchg->ndlp = ndlp;
3262 	axchg->size = size;
3263 	axchg->oxid = oxid;
3264 	axchg->sid = sid;
3265 	axchg->wqeq = NULL;
3266 	axchg->state = LPFC_NVME_STE_LS_RCV;
3267 	axchg->entry_cnt = 1;
3268 	axchg->rqb_buffer = (void *)nvmebuf;
3269 	axchg->hdwq = &phba->sli4_hba.hdwq[0];
3270 	axchg->payload = nvmebuf->dbuf.virt;
3271 	INIT_LIST_HEAD(&axchg->list);
3272 
3273 	if (phba->nvmet_support) {
3274 		ret = lpfc_nvmet_handle_lsreq(phba, axchg);
3275 		spin_lock_irq(&ndlp->lock);
3276 		if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3277 			ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3278 			spin_unlock_irq(&ndlp->lock);
3279 
3280 			/* This reference is a single occurrence to hold the
3281 			 * node valid until the nvmet transport calls
3282 			 * host_release.
3283 			 */
3284 			if (!lpfc_nlp_get(ndlp))
3285 				goto out_fail;
3286 
3287 			lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
3288 					"6206 NVMET unsol ls_req ndlp x%px "
3289 					"DID x%x xflags x%x refcnt %d\n",
3290 					ndlp, ndlp->nlp_DID,
3291 					ndlp->fc4_xpt_flags,
3292 					kref_read(&ndlp->kref));
3293 		} else {
3294 			spin_unlock_irq(&ndlp->lock);
3295 		}
3296 	} else {
3297 		ret = lpfc_nvme_handle_lsreq(phba, axchg);
3298 	}
3299 
3300 	/* if zero, LS was successfully handled. If non-zero, LS not handled */
3301 	if (!ret)
3302 		return;
3303 
3304 out_fail:
3305 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3306 			"6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3307 			"NVMe%s handler failed %d\n",
3308 			did, sid, oxid,
3309 			(phba->nvmet_support) ? "T" : "I", ret);
3310 
3311 	/* recycle receive buffer */
3312 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3313 
3314 	/* If start of new exchange, abort it */
3315 	if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3316 		ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3317 
3318 	if (ret)
3319 		kfree(axchg);
3320 }
3321 
3322 /**
3323  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3324  * @phba: Pointer to HBA context object.
3325  * @pring: Pointer to driver SLI ring object.
3326  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3327  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3328  * @fch_type: the type for the first frame of the sequence.
3329  *
3330  * This function is called with no lock held. This function uses the r_ctl and
3331  * type of the received sequence to find the correct callback function to call
3332  * to process the sequence.
3333  **/
3334 static int
lpfc_complete_unsol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq,uint32_t fch_r_ctl,uint32_t fch_type)3335 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3336 			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3337 			 uint32_t fch_type)
3338 {
3339 	int i;
3340 
3341 	switch (fch_type) {
3342 	case FC_TYPE_NVME:
3343 		lpfc_nvme_unsol_ls_handler(phba, saveq);
3344 		return 1;
3345 	default:
3346 		break;
3347 	}
3348 
3349 	/* unSolicited Responses */
3350 	if (pring->prt[0].profile) {
3351 		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3352 			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3353 									saveq);
3354 		return 1;
3355 	}
3356 	/* We must search, based on rctl / type
3357 	   for the right routine */
3358 	for (i = 0; i < pring->num_mask; i++) {
3359 		if ((pring->prt[i].rctl == fch_r_ctl) &&
3360 		    (pring->prt[i].type == fch_type)) {
3361 			if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3362 				(pring->prt[i].lpfc_sli_rcv_unsol_event)
3363 						(phba, pring, saveq);
3364 			return 1;
3365 		}
3366 	}
3367 	return 0;
3368 }
3369 
3370 static void
lpfc_sli_prep_unsol_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * saveq)3371 lpfc_sli_prep_unsol_wqe(struct lpfc_hba *phba,
3372 			struct lpfc_iocbq *saveq)
3373 {
3374 	IOCB_t *irsp;
3375 	union lpfc_wqe128 *wqe;
3376 	u16 i = 0;
3377 
3378 	irsp = &saveq->iocb;
3379 	wqe = &saveq->wqe;
3380 
3381 	/* Fill wcqe with the IOCB status fields */
3382 	bf_set(lpfc_wcqe_c_status, &saveq->wcqe_cmpl, irsp->ulpStatus);
3383 	saveq->wcqe_cmpl.word3 = irsp->ulpBdeCount;
3384 	saveq->wcqe_cmpl.parameter = irsp->un.ulpWord[4];
3385 	saveq->wcqe_cmpl.total_data_placed = irsp->unsli3.rcvsli3.acc_len;
3386 
3387 	/* Source ID */
3388 	bf_set(els_rsp64_sid, &wqe->xmit_els_rsp, irsp->un.rcvels.parmRo);
3389 
3390 	/* rx-id of the response frame */
3391 	bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com, irsp->ulpContext);
3392 
3393 	/* ox-id of the frame */
3394 	bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
3395 	       irsp->unsli3.rcvsli3.ox_id);
3396 
3397 	/* DID */
3398 	bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
3399 	       irsp->un.rcvels.remoteID);
3400 
3401 	/* unsol data len */
3402 	for (i = 0; i < irsp->ulpBdeCount; i++) {
3403 		struct lpfc_hbq_entry *hbqe = NULL;
3404 
3405 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3406 			if (i == 0) {
3407 				hbqe = (struct lpfc_hbq_entry *)
3408 					&irsp->un.ulpWord[0];
3409 				saveq->wqe.gen_req.bde.tus.f.bdeSize =
3410 					hbqe->bde.tus.f.bdeSize;
3411 			} else if (i == 1) {
3412 				hbqe = (struct lpfc_hbq_entry *)
3413 					&irsp->unsli3.sli3Words[4];
3414 				saveq->unsol_rcv_len = hbqe->bde.tus.f.bdeSize;
3415 			}
3416 		}
3417 	}
3418 }
3419 
3420 /**
3421  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
3422  * @phba: Pointer to HBA context object.
3423  * @pring: Pointer to driver SLI ring object.
3424  * @saveq: Pointer to the unsolicited iocb.
3425  *
3426  * This function is called with no lock held by the ring event handler
3427  * when there is an unsolicited iocb posted to the response ring by the
3428  * firmware. This function gets the buffer associated with the iocbs
3429  * and calls the event handler for the ring. This function handles both
3430  * qring buffers and hbq buffers.
3431  * When the function returns 1 the caller can free the iocb object otherwise
3432  * upper layer functions will free the iocb objects.
3433  **/
3434 static int
lpfc_sli_process_unsol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3435 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3436 			    struct lpfc_iocbq *saveq)
3437 {
3438 	IOCB_t           * irsp;
3439 	WORD5            * w5p;
3440 	dma_addr_t	 paddr;
3441 	uint32_t           Rctl, Type;
3442 	struct lpfc_iocbq *iocbq;
3443 	struct lpfc_dmabuf *dmzbuf;
3444 
3445 	irsp = &saveq->iocb;
3446 	saveq->vport = phba->pport;
3447 
3448 	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3449 		if (pring->lpfc_sli_rcv_async_status)
3450 			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3451 		else
3452 			lpfc_printf_log(phba,
3453 					KERN_WARNING,
3454 					LOG_SLI,
3455 					"0316 Ring %d handler: unexpected "
3456 					"ASYNC_STATUS iocb received evt_code "
3457 					"0x%x\n",
3458 					pring->ringno,
3459 					irsp->un.asyncstat.evt_code);
3460 		return 1;
3461 	}
3462 
3463 	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3464 	    (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3465 		if (irsp->ulpBdeCount > 0) {
3466 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3467 						   irsp->un.ulpWord[3]);
3468 			lpfc_in_buf_free(phba, dmzbuf);
3469 		}
3470 
3471 		if (irsp->ulpBdeCount > 1) {
3472 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3473 						   irsp->unsli3.sli3Words[3]);
3474 			lpfc_in_buf_free(phba, dmzbuf);
3475 		}
3476 
3477 		if (irsp->ulpBdeCount > 2) {
3478 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3479 						   irsp->unsli3.sli3Words[7]);
3480 			lpfc_in_buf_free(phba, dmzbuf);
3481 		}
3482 
3483 		return 1;
3484 	}
3485 
3486 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3487 		if (irsp->ulpBdeCount != 0) {
3488 			saveq->cmd_dmabuf = lpfc_sli_get_buff(phba, pring,
3489 						irsp->un.ulpWord[3]);
3490 			if (!saveq->cmd_dmabuf)
3491 				lpfc_printf_log(phba,
3492 					KERN_ERR,
3493 					LOG_SLI,
3494 					"0341 Ring %d Cannot find buffer for "
3495 					"an unsolicited iocb. tag 0x%x\n",
3496 					pring->ringno,
3497 					irsp->un.ulpWord[3]);
3498 		}
3499 		if (irsp->ulpBdeCount == 2) {
3500 			saveq->bpl_dmabuf = lpfc_sli_get_buff(phba, pring,
3501 						irsp->unsli3.sli3Words[7]);
3502 			if (!saveq->bpl_dmabuf)
3503 				lpfc_printf_log(phba,
3504 					KERN_ERR,
3505 					LOG_SLI,
3506 					"0342 Ring %d Cannot find buffer for an"
3507 					" unsolicited iocb. tag 0x%x\n",
3508 					pring->ringno,
3509 					irsp->unsli3.sli3Words[7]);
3510 		}
3511 		list_for_each_entry(iocbq, &saveq->list, list) {
3512 			irsp = &iocbq->iocb;
3513 			if (irsp->ulpBdeCount != 0) {
3514 				iocbq->cmd_dmabuf = lpfc_sli_get_buff(phba,
3515 							pring,
3516 							irsp->un.ulpWord[3]);
3517 				if (!iocbq->cmd_dmabuf)
3518 					lpfc_printf_log(phba,
3519 						KERN_ERR,
3520 						LOG_SLI,
3521 						"0343 Ring %d Cannot find "
3522 						"buffer for an unsolicited iocb"
3523 						". tag 0x%x\n", pring->ringno,
3524 						irsp->un.ulpWord[3]);
3525 			}
3526 			if (irsp->ulpBdeCount == 2) {
3527 				iocbq->bpl_dmabuf = lpfc_sli_get_buff(phba,
3528 						pring,
3529 						irsp->unsli3.sli3Words[7]);
3530 				if (!iocbq->bpl_dmabuf)
3531 					lpfc_printf_log(phba,
3532 						KERN_ERR,
3533 						LOG_SLI,
3534 						"0344 Ring %d Cannot find "
3535 						"buffer for an unsolicited "
3536 						"iocb. tag 0x%x\n",
3537 						pring->ringno,
3538 						irsp->unsli3.sli3Words[7]);
3539 			}
3540 		}
3541 	} else {
3542 		paddr = getPaddr(irsp->un.cont64[0].addrHigh,
3543 				 irsp->un.cont64[0].addrLow);
3544 		saveq->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
3545 							     paddr);
3546 		if (irsp->ulpBdeCount == 2) {
3547 			paddr = getPaddr(irsp->un.cont64[1].addrHigh,
3548 					 irsp->un.cont64[1].addrLow);
3549 			saveq->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
3550 								   pring,
3551 								   paddr);
3552 		}
3553 	}
3554 
3555 	if (irsp->ulpBdeCount != 0 &&
3556 	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3557 	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3558 		int found = 0;
3559 
3560 		/* search continue save q for same XRI */
3561 		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
3562 			if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3563 				saveq->iocb.unsli3.rcvsli3.ox_id) {
3564 				list_add_tail(&saveq->list, &iocbq->list);
3565 				found = 1;
3566 				break;
3567 			}
3568 		}
3569 		if (!found)
3570 			list_add_tail(&saveq->clist,
3571 				      &pring->iocb_continue_saveq);
3572 
3573 		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3574 			list_del_init(&iocbq->clist);
3575 			saveq = iocbq;
3576 			irsp = &saveq->iocb;
3577 		} else {
3578 			return 0;
3579 		}
3580 	}
3581 	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3582 	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3583 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
3584 		Rctl = FC_RCTL_ELS_REQ;
3585 		Type = FC_TYPE_ELS;
3586 	} else {
3587 		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3588 		Rctl = w5p->hcsw.Rctl;
3589 		Type = w5p->hcsw.Type;
3590 
3591 		/* Firmware Workaround */
3592 		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3593 			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3594 			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3595 			Rctl = FC_RCTL_ELS_REQ;
3596 			Type = FC_TYPE_ELS;
3597 			w5p->hcsw.Rctl = Rctl;
3598 			w5p->hcsw.Type = Type;
3599 		}
3600 	}
3601 
3602 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3603 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
3604 	    irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3605 		if (irsp->unsli3.rcvsli3.vpi == 0xffff)
3606 			saveq->vport = phba->pport;
3607 		else
3608 			saveq->vport = lpfc_find_vport_by_vpid(phba,
3609 					       irsp->unsli3.rcvsli3.vpi);
3610 	}
3611 
3612 	/* Prepare WQE with Unsol frame */
3613 	lpfc_sli_prep_unsol_wqe(phba, saveq);
3614 
3615 	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
3616 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3617 				"0313 Ring %d handler: unexpected Rctl x%x "
3618 				"Type x%x received\n",
3619 				pring->ringno, Rctl, Type);
3620 
3621 	return 1;
3622 }
3623 
3624 /**
3625  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
3626  * @phba: Pointer to HBA context object.
3627  * @pring: Pointer to driver SLI ring object.
3628  * @prspiocb: Pointer to response iocb object.
3629  *
3630  * This function looks up the iocb_lookup table to get the command iocb
3631  * corresponding to the given response iocb using the iotag of the
3632  * response iocb. The driver calls this function with the hbalock held
3633  * for SLI3 ports or the ring lock held for SLI4 ports.
3634  * This function returns the command iocb object if it finds the command
3635  * iocb else returns NULL.
3636  **/
3637 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * prspiocb)3638 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3639 		      struct lpfc_sli_ring *pring,
3640 		      struct lpfc_iocbq *prspiocb)
3641 {
3642 	struct lpfc_iocbq *cmd_iocb = NULL;
3643 	u16 iotag;
3644 
3645 	if (phba->sli_rev == LPFC_SLI_REV4)
3646 		iotag = get_wqe_reqtag(prspiocb);
3647 	else
3648 		iotag = prspiocb->iocb.ulpIoTag;
3649 
3650 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3651 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3652 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3653 			/* remove from txcmpl queue list */
3654 			list_del_init(&cmd_iocb->list);
3655 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3656 			pring->txcmplq_cnt--;
3657 			return cmd_iocb;
3658 		}
3659 	}
3660 
3661 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3662 			"0317 iotag x%x is out of "
3663 			"range: max iotag x%x\n",
3664 			iotag, phba->sli.last_iotag);
3665 	return NULL;
3666 }
3667 
3668 /**
3669  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3670  * @phba: Pointer to HBA context object.
3671  * @pring: Pointer to driver SLI ring object.
3672  * @iotag: IOCB tag.
3673  *
3674  * This function looks up the iocb_lookup table to get the command iocb
3675  * corresponding to the given iotag. The driver calls this function with
3676  * the ring lock held because this function is an SLI4 port only helper.
3677  * This function returns the command iocb object if it finds the command
3678  * iocb else returns NULL.
3679  **/
3680 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint16_t iotag)3681 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3682 			     struct lpfc_sli_ring *pring, uint16_t iotag)
3683 {
3684 	struct lpfc_iocbq *cmd_iocb = NULL;
3685 
3686 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3687 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3688 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3689 			/* remove from txcmpl queue list */
3690 			list_del_init(&cmd_iocb->list);
3691 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3692 			pring->txcmplq_cnt--;
3693 			return cmd_iocb;
3694 		}
3695 	}
3696 
3697 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3698 			"0372 iotag x%x lookup error: max iotag (x%x) "
3699 			"cmd_flag x%x\n",
3700 			iotag, phba->sli.last_iotag,
3701 			cmd_iocb ? cmd_iocb->cmd_flag : 0xffff);
3702 	return NULL;
3703 }
3704 
3705 /**
3706  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3707  * @phba: Pointer to HBA context object.
3708  * @pring: Pointer to driver SLI ring object.
3709  * @saveq: Pointer to the response iocb to be processed.
3710  *
3711  * This function is called by the ring event handler for non-fcp
3712  * rings when there is a new response iocb in the response ring.
3713  * The caller is not required to hold any locks. This function
3714  * gets the command iocb associated with the response iocb and
3715  * calls the completion handler for the command iocb. If there
3716  * is no completion handler, the function will free the resources
3717  * associated with command iocb. If the response iocb is for
3718  * an already aborted command iocb, the status of the completion
3719  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3720  * This function always returns 1.
3721  **/
3722 static int
lpfc_sli_process_sol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3723 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3724 			  struct lpfc_iocbq *saveq)
3725 {
3726 	struct lpfc_iocbq *cmdiocbp;
3727 	unsigned long iflag;
3728 	u32 ulp_command, ulp_status, ulp_word4, ulp_context, iotag;
3729 
3730 	if (phba->sli_rev == LPFC_SLI_REV4)
3731 		spin_lock_irqsave(&pring->ring_lock, iflag);
3732 	else
3733 		spin_lock_irqsave(&phba->hbalock, iflag);
3734 	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3735 	if (phba->sli_rev == LPFC_SLI_REV4)
3736 		spin_unlock_irqrestore(&pring->ring_lock, iflag);
3737 	else
3738 		spin_unlock_irqrestore(&phba->hbalock, iflag);
3739 
3740 	ulp_command = get_job_cmnd(phba, saveq);
3741 	ulp_status = get_job_ulpstatus(phba, saveq);
3742 	ulp_word4 = get_job_word4(phba, saveq);
3743 	ulp_context = get_job_ulpcontext(phba, saveq);
3744 	if (phba->sli_rev == LPFC_SLI_REV4)
3745 		iotag = get_wqe_reqtag(saveq);
3746 	else
3747 		iotag = saveq->iocb.ulpIoTag;
3748 
3749 	if (cmdiocbp) {
3750 		ulp_command = get_job_cmnd(phba, cmdiocbp);
3751 		if (cmdiocbp->cmd_cmpl) {
3752 			/*
3753 			 * If an ELS command failed send an event to mgmt
3754 			 * application.
3755 			 */
3756 			if (ulp_status &&
3757 			     (pring->ringno == LPFC_ELS_RING) &&
3758 			     (ulp_command == CMD_ELS_REQUEST64_CR))
3759 				lpfc_send_els_failure_event(phba,
3760 					cmdiocbp, saveq);
3761 
3762 			/*
3763 			 * Post all ELS completions to the worker thread.
3764 			 * All other are passed to the completion callback.
3765 			 */
3766 			if (pring->ringno == LPFC_ELS_RING) {
3767 				if ((phba->sli_rev < LPFC_SLI_REV4) &&
3768 				    (cmdiocbp->cmd_flag &
3769 							LPFC_DRIVER_ABORTED)) {
3770 					spin_lock_irqsave(&phba->hbalock,
3771 							  iflag);
3772 					cmdiocbp->cmd_flag &=
3773 						~LPFC_DRIVER_ABORTED;
3774 					spin_unlock_irqrestore(&phba->hbalock,
3775 							       iflag);
3776 					saveq->iocb.ulpStatus =
3777 						IOSTAT_LOCAL_REJECT;
3778 					saveq->iocb.un.ulpWord[4] =
3779 						IOERR_SLI_ABORTED;
3780 
3781 					/* Firmware could still be in progress
3782 					 * of DMAing payload, so don't free data
3783 					 * buffer till after a hbeat.
3784 					 */
3785 					spin_lock_irqsave(&phba->hbalock,
3786 							  iflag);
3787 					saveq->cmd_flag |= LPFC_DELAY_MEM_FREE;
3788 					spin_unlock_irqrestore(&phba->hbalock,
3789 							       iflag);
3790 				}
3791 				if (phba->sli_rev == LPFC_SLI_REV4) {
3792 					if (saveq->cmd_flag &
3793 					    LPFC_EXCHANGE_BUSY) {
3794 						/* Set cmdiocb flag for the
3795 						 * exchange busy so sgl (xri)
3796 						 * will not be released until
3797 						 * the abort xri is received
3798 						 * from hba.
3799 						 */
3800 						spin_lock_irqsave(
3801 							&phba->hbalock, iflag);
3802 						cmdiocbp->cmd_flag |=
3803 							LPFC_EXCHANGE_BUSY;
3804 						spin_unlock_irqrestore(
3805 							&phba->hbalock, iflag);
3806 					}
3807 					if (cmdiocbp->cmd_flag &
3808 					    LPFC_DRIVER_ABORTED) {
3809 						/*
3810 						 * Clear LPFC_DRIVER_ABORTED
3811 						 * bit in case it was driver
3812 						 * initiated abort.
3813 						 */
3814 						spin_lock_irqsave(
3815 							&phba->hbalock, iflag);
3816 						cmdiocbp->cmd_flag &=
3817 							~LPFC_DRIVER_ABORTED;
3818 						spin_unlock_irqrestore(
3819 							&phba->hbalock, iflag);
3820 						set_job_ulpstatus(cmdiocbp,
3821 								  IOSTAT_LOCAL_REJECT);
3822 						set_job_ulpword4(cmdiocbp,
3823 								 IOERR_ABORT_REQUESTED);
3824 						/*
3825 						 * For SLI4, irspiocb contains
3826 						 * NO_XRI in sli_xritag, it
3827 						 * shall not affect releasing
3828 						 * sgl (xri) process.
3829 						 */
3830 						set_job_ulpstatus(saveq,
3831 								  IOSTAT_LOCAL_REJECT);
3832 						set_job_ulpword4(saveq,
3833 								 IOERR_SLI_ABORTED);
3834 						spin_lock_irqsave(
3835 							&phba->hbalock, iflag);
3836 						saveq->cmd_flag |=
3837 							LPFC_DELAY_MEM_FREE;
3838 						spin_unlock_irqrestore(
3839 							&phba->hbalock, iflag);
3840 					}
3841 				}
3842 			}
3843 			cmdiocbp->cmd_cmpl(phba, cmdiocbp, saveq);
3844 		} else
3845 			lpfc_sli_release_iocbq(phba, cmdiocbp);
3846 	} else {
3847 		/*
3848 		 * Unknown initiating command based on the response iotag.
3849 		 * This could be the case on the ELS ring because of
3850 		 * lpfc_els_abort().
3851 		 */
3852 		if (pring->ringno != LPFC_ELS_RING) {
3853 			/*
3854 			 * Ring <ringno> handler: unexpected completion IoTag
3855 			 * <IoTag>
3856 			 */
3857 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3858 					 "0322 Ring %d handler: "
3859 					 "unexpected completion IoTag x%x "
3860 					 "Data: x%x x%x x%x x%x\n",
3861 					 pring->ringno, iotag, ulp_status,
3862 					 ulp_word4, ulp_command, ulp_context);
3863 		}
3864 	}
3865 
3866 	return 1;
3867 }
3868 
3869 /**
3870  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3871  * @phba: Pointer to HBA context object.
3872  * @pring: Pointer to driver SLI ring object.
3873  *
3874  * This function is called from the iocb ring event handlers when
3875  * put pointer is ahead of the get pointer for a ring. This function signal
3876  * an error attention condition to the worker thread and the worker
3877  * thread will transition the HBA to offline state.
3878  **/
3879 static void
lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)3880 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3881 {
3882 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3883 	/*
3884 	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3885 	 * rsp ring <portRspMax>
3886 	 */
3887 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3888 			"0312 Ring %d handler: portRspPut %d "
3889 			"is bigger than rsp ring %d\n",
3890 			pring->ringno, le32_to_cpu(pgp->rspPutInx),
3891 			pring->sli.sli3.numRiocb);
3892 
3893 	phba->link_state = LPFC_HBA_ERROR;
3894 
3895 	/*
3896 	 * All error attention handlers are posted to
3897 	 * worker thread
3898 	 */
3899 	phba->work_ha |= HA_ERATT;
3900 	phba->work_hs = HS_FFER3;
3901 
3902 	lpfc_worker_wake_up(phba);
3903 
3904 	return;
3905 }
3906 
3907 /**
3908  * lpfc_poll_eratt - Error attention polling timer timeout handler
3909  * @t: Context to fetch pointer to address of HBA context object from.
3910  *
3911  * This function is invoked by the Error Attention polling timer when the
3912  * timer times out. It will check the SLI Error Attention register for
3913  * possible attention events. If so, it will post an Error Attention event
3914  * and wake up worker thread to process it. Otherwise, it will set up the
3915  * Error Attention polling timer for the next poll.
3916  **/
lpfc_poll_eratt(struct timer_list * t)3917 void lpfc_poll_eratt(struct timer_list *t)
3918 {
3919 	struct lpfc_hba *phba;
3920 	uint32_t eratt = 0;
3921 	uint64_t sli_intr, cnt;
3922 
3923 	phba = timer_container_of(phba, t, eratt_poll);
3924 
3925 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
3926 		return;
3927 
3928 	if (phba->sli_rev == LPFC_SLI_REV4 &&
3929 	    !test_bit(HBA_SETUP, &phba->hba_flag)) {
3930 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3931 				"0663 HBA still initializing 0x%lx, restart "
3932 				"timer\n",
3933 				phba->hba_flag);
3934 		goto restart_timer;
3935 	}
3936 
3937 	/* Here we will also keep track of interrupts per sec of the hba */
3938 	sli_intr = phba->sli.slistat.sli_intr;
3939 
3940 	if (phba->sli.slistat.sli_prev_intr > sli_intr)
3941 		cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3942 			sli_intr);
3943 	else
3944 		cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3945 
3946 	/* 64-bit integer division not supported on 32-bit x86 - use do_div */
3947 	do_div(cnt, phba->eratt_poll_interval);
3948 	phba->sli.slistat.sli_ips = cnt;
3949 
3950 	phba->sli.slistat.sli_prev_intr = sli_intr;
3951 
3952 	/* Check chip HA register for error event */
3953 	eratt = lpfc_sli_check_eratt(phba);
3954 
3955 	if (eratt) {
3956 		/* Tell the worker thread there is work to do */
3957 		lpfc_worker_wake_up(phba);
3958 		return;
3959 	}
3960 
3961 restart_timer:
3962 	/* Restart the timer for next eratt poll */
3963 	mod_timer(&phba->eratt_poll,
3964 		  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
3965 	return;
3966 }
3967 
3968 
3969 /**
3970  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3971  * @phba: Pointer to HBA context object.
3972  * @pring: Pointer to driver SLI ring object.
3973  * @mask: Host attention register mask for this ring.
3974  *
3975  * This function is called from the interrupt context when there is a ring
3976  * event for the fcp ring. The caller does not hold any lock.
3977  * The function processes each response iocb in the response ring until it
3978  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3979  * LE bit set. The function will call the completion handler of the command iocb
3980  * if the response iocb indicates a completion for a command iocb or it is
3981  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3982  * function if this is an unsolicited iocb.
3983  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3984  * to check it explicitly.
3985  */
3986 int
lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)3987 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3988 				struct lpfc_sli_ring *pring, uint32_t mask)
3989 {
3990 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3991 	IOCB_t *irsp = NULL;
3992 	IOCB_t *entry = NULL;
3993 	struct lpfc_iocbq *cmdiocbq = NULL;
3994 	struct lpfc_iocbq rspiocbq;
3995 	uint32_t status;
3996 	uint32_t portRspPut, portRspMax;
3997 	int rc = 1;
3998 	lpfc_iocb_type type;
3999 	unsigned long iflag;
4000 	uint32_t rsp_cmpl = 0;
4001 
4002 	spin_lock_irqsave(&phba->hbalock, iflag);
4003 	pring->stats.iocb_event++;
4004 
4005 	/*
4006 	 * The next available response entry should never exceed the maximum
4007 	 * entries.  If it does, treat it as an adapter hardware error.
4008 	 */
4009 	portRspMax = pring->sli.sli3.numRiocb;
4010 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4011 	if (unlikely(portRspPut >= portRspMax)) {
4012 		lpfc_sli_rsp_pointers_error(phba, pring);
4013 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4014 		return 1;
4015 	}
4016 	if (phba->fcp_ring_in_use) {
4017 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4018 		return 1;
4019 	} else
4020 		phba->fcp_ring_in_use = 1;
4021 
4022 	rmb();
4023 	while (pring->sli.sli3.rspidx != portRspPut) {
4024 		/*
4025 		 * Fetch an entry off the ring and copy it into a local data
4026 		 * structure.  The copy involves a byte-swap since the
4027 		 * network byte order and pci byte orders are different.
4028 		 */
4029 		entry = lpfc_resp_iocb(phba, pring);
4030 		phba->last_completion_time = jiffies;
4031 
4032 		if (++pring->sli.sli3.rspidx >= portRspMax)
4033 			pring->sli.sli3.rspidx = 0;
4034 
4035 		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
4036 				      (uint32_t *) &rspiocbq.iocb,
4037 				      phba->iocb_rsp_size);
4038 		INIT_LIST_HEAD(&(rspiocbq.list));
4039 		irsp = &rspiocbq.iocb;
4040 
4041 		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
4042 		pring->stats.iocb_rsp++;
4043 		rsp_cmpl++;
4044 
4045 		if (unlikely(irsp->ulpStatus)) {
4046 			/*
4047 			 * If resource errors reported from HBA, reduce
4048 			 * queuedepths of the SCSI device.
4049 			 */
4050 			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
4051 			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4052 			     IOERR_NO_RESOURCES)) {
4053 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4054 				phba->lpfc_rampdown_queue_depth(phba);
4055 				spin_lock_irqsave(&phba->hbalock, iflag);
4056 			}
4057 
4058 			/* Rsp ring <ringno> error: IOCB */
4059 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4060 					"0336 Rsp Ring %d error: IOCB Data: "
4061 					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
4062 					pring->ringno,
4063 					irsp->un.ulpWord[0],
4064 					irsp->un.ulpWord[1],
4065 					irsp->un.ulpWord[2],
4066 					irsp->un.ulpWord[3],
4067 					irsp->un.ulpWord[4],
4068 					irsp->un.ulpWord[5],
4069 					*(uint32_t *)&irsp->un1,
4070 					*((uint32_t *)&irsp->un1 + 1));
4071 		}
4072 
4073 		switch (type) {
4074 		case LPFC_ABORT_IOCB:
4075 		case LPFC_SOL_IOCB:
4076 			/*
4077 			 * Idle exchange closed via ABTS from port.  No iocb
4078 			 * resources need to be recovered.
4079 			 */
4080 			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
4081 				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4082 						"0333 IOCB cmd 0x%x"
4083 						" processed. Skipping"
4084 						" completion\n",
4085 						irsp->ulpCommand);
4086 				break;
4087 			}
4088 
4089 			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
4090 							 &rspiocbq);
4091 			if (unlikely(!cmdiocbq))
4092 				break;
4093 			if (cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED)
4094 				cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
4095 			if (cmdiocbq->cmd_cmpl) {
4096 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4097 				cmdiocbq->cmd_cmpl(phba, cmdiocbq, &rspiocbq);
4098 				spin_lock_irqsave(&phba->hbalock, iflag);
4099 			}
4100 			break;
4101 		case LPFC_UNSOL_IOCB:
4102 			spin_unlock_irqrestore(&phba->hbalock, iflag);
4103 			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
4104 			spin_lock_irqsave(&phba->hbalock, iflag);
4105 			break;
4106 		default:
4107 			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4108 				char adaptermsg[LPFC_MAX_ADPTMSG];
4109 				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4110 				memcpy(&adaptermsg[0], (uint8_t *) irsp,
4111 				       MAX_MSG_DATA);
4112 				dev_warn(&((phba->pcidev)->dev),
4113 					 "lpfc%d: %s\n",
4114 					 phba->brd_no, adaptermsg);
4115 			} else {
4116 				/* Unknown IOCB command */
4117 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4118 						"0334 Unknown IOCB command "
4119 						"Data: x%x, x%x x%x x%x x%x\n",
4120 						type, irsp->ulpCommand,
4121 						irsp->ulpStatus,
4122 						irsp->ulpIoTag,
4123 						irsp->ulpContext);
4124 			}
4125 			break;
4126 		}
4127 
4128 		/*
4129 		 * The response IOCB has been processed.  Update the ring
4130 		 * pointer in SLIM.  If the port response put pointer has not
4131 		 * been updated, sync the pgp->rspPutInx and fetch the new port
4132 		 * response put pointer.
4133 		 */
4134 		writel(pring->sli.sli3.rspidx,
4135 			&phba->host_gp[pring->ringno].rspGetInx);
4136 
4137 		if (pring->sli.sli3.rspidx == portRspPut)
4138 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4139 	}
4140 
4141 	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4142 		pring->stats.iocb_rsp_full++;
4143 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4144 		writel(status, phba->CAregaddr);
4145 		readl(phba->CAregaddr);
4146 	}
4147 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4148 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4149 		pring->stats.iocb_cmd_empty++;
4150 
4151 		/* Force update of the local copy of cmdGetInx */
4152 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4153 		lpfc_sli_resume_iocb(phba, pring);
4154 
4155 		if ((pring->lpfc_sli_cmd_available))
4156 			(pring->lpfc_sli_cmd_available) (phba, pring);
4157 
4158 	}
4159 
4160 	phba->fcp_ring_in_use = 0;
4161 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4162 	return rc;
4163 }
4164 
4165 /**
4166  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4167  * @phba: Pointer to HBA context object.
4168  * @pring: Pointer to driver SLI ring object.
4169  * @rspiocbp: Pointer to driver response IOCB object.
4170  *
4171  * This function is called from the worker thread when there is a slow-path
4172  * response IOCB to process. This function chains all the response iocbs until
4173  * seeing the iocb with the LE bit set. The function will call
4174  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4175  * completion of a command iocb. The function will call the
4176  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4177  * The function frees the resources or calls the completion handler if this
4178  * iocb is an abort completion. The function returns NULL when the response
4179  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4180  * this function shall chain the iocb on to the iocb_continueq and return the
4181  * response iocb passed in.
4182  **/
4183 static struct lpfc_iocbq *
lpfc_sli_sp_handle_rspiocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * rspiocbp)4184 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4185 			struct lpfc_iocbq *rspiocbp)
4186 {
4187 	struct lpfc_iocbq *saveq;
4188 	struct lpfc_iocbq *cmdiocb;
4189 	struct lpfc_iocbq *next_iocb;
4190 	IOCB_t *irsp;
4191 	uint32_t free_saveq;
4192 	u8 cmd_type;
4193 	lpfc_iocb_type type;
4194 	unsigned long iflag;
4195 	u32 ulp_status = get_job_ulpstatus(phba, rspiocbp);
4196 	u32 ulp_word4 = get_job_word4(phba, rspiocbp);
4197 	u32 ulp_command = get_job_cmnd(phba, rspiocbp);
4198 	int rc;
4199 
4200 	spin_lock_irqsave(&phba->hbalock, iflag);
4201 	/* First add the response iocb to the countinueq list */
4202 	list_add_tail(&rspiocbp->list, &pring->iocb_continueq);
4203 	pring->iocb_continueq_cnt++;
4204 
4205 	/*
4206 	 * By default, the driver expects to free all resources
4207 	 * associated with this iocb completion.
4208 	 */
4209 	free_saveq = 1;
4210 	saveq = list_get_first(&pring->iocb_continueq,
4211 			       struct lpfc_iocbq, list);
4212 	list_del_init(&pring->iocb_continueq);
4213 	pring->iocb_continueq_cnt = 0;
4214 
4215 	pring->stats.iocb_rsp++;
4216 
4217 	/*
4218 	 * If resource errors reported from HBA, reduce
4219 	 * queuedepths of the SCSI device.
4220 	 */
4221 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
4222 	    ((ulp_word4 & IOERR_PARAM_MASK) ==
4223 	     IOERR_NO_RESOURCES)) {
4224 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4225 		phba->lpfc_rampdown_queue_depth(phba);
4226 		spin_lock_irqsave(&phba->hbalock, iflag);
4227 	}
4228 
4229 	if (ulp_status) {
4230 		/* Rsp ring <ringno> error: IOCB */
4231 		if (phba->sli_rev < LPFC_SLI_REV4) {
4232 			irsp = &rspiocbp->iocb;
4233 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4234 					"0328 Rsp Ring %d error: ulp_status x%x "
4235 					"IOCB Data: "
4236 					"x%08x x%08x x%08x x%08x "
4237 					"x%08x x%08x x%08x x%08x "
4238 					"x%08x x%08x x%08x x%08x "
4239 					"x%08x x%08x x%08x x%08x\n",
4240 					pring->ringno, ulp_status,
4241 					get_job_ulpword(rspiocbp, 0),
4242 					get_job_ulpword(rspiocbp, 1),
4243 					get_job_ulpword(rspiocbp, 2),
4244 					get_job_ulpword(rspiocbp, 3),
4245 					get_job_ulpword(rspiocbp, 4),
4246 					get_job_ulpword(rspiocbp, 5),
4247 					*(((uint32_t *)irsp) + 6),
4248 					*(((uint32_t *)irsp) + 7),
4249 					*(((uint32_t *)irsp) + 8),
4250 					*(((uint32_t *)irsp) + 9),
4251 					*(((uint32_t *)irsp) + 10),
4252 					*(((uint32_t *)irsp) + 11),
4253 					*(((uint32_t *)irsp) + 12),
4254 					*(((uint32_t *)irsp) + 13),
4255 					*(((uint32_t *)irsp) + 14),
4256 					*(((uint32_t *)irsp) + 15));
4257 		} else {
4258 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4259 					"0321 Rsp Ring %d error: "
4260 					"IOCB Data: "
4261 					"x%x x%x x%x x%x\n",
4262 					pring->ringno,
4263 					rspiocbp->wcqe_cmpl.word0,
4264 					rspiocbp->wcqe_cmpl.total_data_placed,
4265 					rspiocbp->wcqe_cmpl.parameter,
4266 					rspiocbp->wcqe_cmpl.word3);
4267 		}
4268 	}
4269 
4270 
4271 	/*
4272 	 * Fetch the iocb command type and call the correct completion
4273 	 * routine. Solicited and Unsolicited IOCBs on the ELS ring
4274 	 * get freed back to the lpfc_iocb_list by the discovery
4275 	 * kernel thread.
4276 	 */
4277 	cmd_type = ulp_command & CMD_IOCB_MASK;
4278 	type = lpfc_sli_iocb_cmd_type(cmd_type);
4279 	switch (type) {
4280 	case LPFC_SOL_IOCB:
4281 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4282 		rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4283 		spin_lock_irqsave(&phba->hbalock, iflag);
4284 		break;
4285 	case LPFC_UNSOL_IOCB:
4286 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4287 		rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4288 		spin_lock_irqsave(&phba->hbalock, iflag);
4289 		if (!rc)
4290 			free_saveq = 0;
4291 		break;
4292 	case LPFC_ABORT_IOCB:
4293 		cmdiocb = NULL;
4294 		if (ulp_command != CMD_XRI_ABORTED_CX)
4295 			cmdiocb = lpfc_sli_iocbq_lookup(phba, pring,
4296 							saveq);
4297 		if (cmdiocb) {
4298 			/* Call the specified completion routine */
4299 			if (cmdiocb->cmd_cmpl) {
4300 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4301 				cmdiocb->cmd_cmpl(phba, cmdiocb, saveq);
4302 				spin_lock_irqsave(&phba->hbalock, iflag);
4303 			} else {
4304 				__lpfc_sli_release_iocbq(phba, cmdiocb);
4305 			}
4306 		}
4307 		break;
4308 	case LPFC_UNKNOWN_IOCB:
4309 		if (ulp_command == CMD_ADAPTER_MSG) {
4310 			char adaptermsg[LPFC_MAX_ADPTMSG];
4311 
4312 			memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4313 			memcpy(&adaptermsg[0], (uint8_t *)&rspiocbp->wqe,
4314 			       MAX_MSG_DATA);
4315 			dev_warn(&((phba->pcidev)->dev),
4316 				 "lpfc%d: %s\n",
4317 				 phba->brd_no, adaptermsg);
4318 		} else {
4319 			/* Unknown command */
4320 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4321 					"0335 Unknown IOCB "
4322 					"command Data: x%x "
4323 					"x%x x%x x%x\n",
4324 					ulp_command,
4325 					ulp_status,
4326 					get_wqe_reqtag(rspiocbp),
4327 					get_job_ulpcontext(phba, rspiocbp));
4328 		}
4329 		break;
4330 	}
4331 
4332 	if (free_saveq) {
4333 		list_for_each_entry_safe(rspiocbp, next_iocb,
4334 					 &saveq->list, list) {
4335 			list_del_init(&rspiocbp->list);
4336 			__lpfc_sli_release_iocbq(phba, rspiocbp);
4337 		}
4338 		__lpfc_sli_release_iocbq(phba, saveq);
4339 	}
4340 	rspiocbp = NULL;
4341 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4342 	return rspiocbp;
4343 }
4344 
4345 /**
4346  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
4347  * @phba: Pointer to HBA context object.
4348  * @pring: Pointer to driver SLI ring object.
4349  * @mask: Host attention register mask for this ring.
4350  *
4351  * This routine wraps the actual slow_ring event process routine from the
4352  * API jump table function pointer from the lpfc_hba struct.
4353  **/
4354 void
lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4355 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4356 				struct lpfc_sli_ring *pring, uint32_t mask)
4357 {
4358 	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4359 }
4360 
4361 /**
4362  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4363  * @phba: Pointer to HBA context object.
4364  * @pring: Pointer to driver SLI ring object.
4365  * @mask: Host attention register mask for this ring.
4366  *
4367  * This function is called from the worker thread when there is a ring event
4368  * for non-fcp rings. The caller does not hold any lock. The function will
4369  * remove each response iocb in the response ring and calls the handle
4370  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4371  **/
4372 static void
lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4373 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4374 				   struct lpfc_sli_ring *pring, uint32_t mask)
4375 {
4376 	struct lpfc_pgp *pgp;
4377 	IOCB_t *entry;
4378 	IOCB_t *irsp = NULL;
4379 	struct lpfc_iocbq *rspiocbp = NULL;
4380 	uint32_t portRspPut, portRspMax;
4381 	unsigned long iflag;
4382 	uint32_t status;
4383 
4384 	pgp = &phba->port_gp[pring->ringno];
4385 	spin_lock_irqsave(&phba->hbalock, iflag);
4386 	pring->stats.iocb_event++;
4387 
4388 	/*
4389 	 * The next available response entry should never exceed the maximum
4390 	 * entries.  If it does, treat it as an adapter hardware error.
4391 	 */
4392 	portRspMax = pring->sli.sli3.numRiocb;
4393 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4394 	if (portRspPut >= portRspMax) {
4395 		/*
4396 		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4397 		 * rsp ring <portRspMax>
4398 		 */
4399 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4400 				"0303 Ring %d handler: portRspPut %d "
4401 				"is bigger than rsp ring %d\n",
4402 				pring->ringno, portRspPut, portRspMax);
4403 
4404 		phba->link_state = LPFC_HBA_ERROR;
4405 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4406 
4407 		phba->work_hs = HS_FFER3;
4408 		lpfc_handle_eratt(phba);
4409 
4410 		return;
4411 	}
4412 
4413 	rmb();
4414 	while (pring->sli.sli3.rspidx != portRspPut) {
4415 		/*
4416 		 * Build a completion list and call the appropriate handler.
4417 		 * The process is to get the next available response iocb, get
4418 		 * a free iocb from the list, copy the response data into the
4419 		 * free iocb, insert to the continuation list, and update the
4420 		 * next response index to slim.  This process makes response
4421 		 * iocb's in the ring available to DMA as fast as possible but
4422 		 * pays a penalty for a copy operation.  Since the iocb is
4423 		 * only 32 bytes, this penalty is considered small relative to
4424 		 * the PCI reads for register values and a slim write.  When
4425 		 * the ulpLe field is set, the entire Command has been
4426 		 * received.
4427 		 */
4428 		entry = lpfc_resp_iocb(phba, pring);
4429 
4430 		phba->last_completion_time = jiffies;
4431 		rspiocbp = __lpfc_sli_get_iocbq(phba);
4432 		if (rspiocbp == NULL) {
4433 			printk(KERN_ERR "%s: out of buffers! Failing "
4434 			       "completion.\n", __func__);
4435 			break;
4436 		}
4437 
4438 		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4439 				      phba->iocb_rsp_size);
4440 		irsp = &rspiocbp->iocb;
4441 
4442 		if (++pring->sli.sli3.rspidx >= portRspMax)
4443 			pring->sli.sli3.rspidx = 0;
4444 
4445 		if (pring->ringno == LPFC_ELS_RING) {
4446 			lpfc_debugfs_slow_ring_trc(phba,
4447 			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4448 				*(((uint32_t *) irsp) + 4),
4449 				*(((uint32_t *) irsp) + 6),
4450 				*(((uint32_t *) irsp) + 7));
4451 		}
4452 
4453 		writel(pring->sli.sli3.rspidx,
4454 			&phba->host_gp[pring->ringno].rspGetInx);
4455 
4456 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4457 		/* Handle the response IOCB */
4458 		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4459 		spin_lock_irqsave(&phba->hbalock, iflag);
4460 
4461 		/*
4462 		 * If the port response put pointer has not been updated, sync
4463 		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4464 		 * response put pointer.
4465 		 */
4466 		if (pring->sli.sli3.rspidx == portRspPut) {
4467 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4468 		}
4469 	} /* while (pring->sli.sli3.rspidx != portRspPut) */
4470 
4471 	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4472 		/* At least one response entry has been freed */
4473 		pring->stats.iocb_rsp_full++;
4474 		/* SET RxRE_RSP in Chip Att register */
4475 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4476 		writel(status, phba->CAregaddr);
4477 		readl(phba->CAregaddr); /* flush */
4478 	}
4479 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4480 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4481 		pring->stats.iocb_cmd_empty++;
4482 
4483 		/* Force update of the local copy of cmdGetInx */
4484 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4485 		lpfc_sli_resume_iocb(phba, pring);
4486 
4487 		if ((pring->lpfc_sli_cmd_available))
4488 			(pring->lpfc_sli_cmd_available) (phba, pring);
4489 
4490 	}
4491 
4492 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4493 	return;
4494 }
4495 
4496 /**
4497  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4498  * @phba: Pointer to HBA context object.
4499  * @pring: Pointer to driver SLI ring object.
4500  * @mask: Host attention register mask for this ring.
4501  *
4502  * This function is called from the worker thread when there is a pending
4503  * ELS response iocb on the driver internal slow-path response iocb worker
4504  * queue. The caller does not hold any lock. The function will remove each
4505  * response iocb from the response worker queue and calls the handle
4506  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4507  **/
4508 static void
lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4509 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4510 				   struct lpfc_sli_ring *pring, uint32_t mask)
4511 {
4512 	struct lpfc_iocbq *irspiocbq;
4513 	struct hbq_dmabuf *dmabuf;
4514 	struct lpfc_cq_event *cq_event;
4515 	unsigned long iflag;
4516 	int count = 0;
4517 
4518 	clear_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
4519 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4520 		/* Get the response iocb from the head of work queue */
4521 		spin_lock_irqsave(&phba->hbalock, iflag);
4522 		list_remove_head(&phba->sli4_hba.sp_queue_event,
4523 				 cq_event, struct lpfc_cq_event, list);
4524 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4525 
4526 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4527 		case CQE_CODE_COMPL_WQE:
4528 			irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4529 						 cq_event);
4530 			/* Translate ELS WCQE to response IOCBQ */
4531 			irspiocbq = lpfc_sli4_els_preprocess_rspiocbq(phba,
4532 								      irspiocbq);
4533 			if (irspiocbq)
4534 				lpfc_sli_sp_handle_rspiocb(phba, pring,
4535 							   irspiocbq);
4536 			count++;
4537 			break;
4538 		case CQE_CODE_RECEIVE:
4539 		case CQE_CODE_RECEIVE_V1:
4540 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
4541 					      cq_event);
4542 			lpfc_sli4_handle_received_buffer(phba, dmabuf);
4543 			count++;
4544 			break;
4545 		default:
4546 			break;
4547 		}
4548 
4549 		/* Limit the number of events to 64 to avoid soft lockups */
4550 		if (count == 64)
4551 			break;
4552 	}
4553 }
4554 
4555 /**
4556  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4557  * @phba: Pointer to HBA context object.
4558  * @pring: Pointer to driver SLI ring object.
4559  *
4560  * This function aborts all iocbs in the given ring and frees all the iocb
4561  * objects in txq. This function issues an abort iocb for all the iocb commands
4562  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4563  * the return of this function. The caller is not required to hold any locks.
4564  **/
4565 void
lpfc_sli_abort_iocb_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)4566 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4567 {
4568 	LIST_HEAD(tx_completions);
4569 	spinlock_t *plock;		/* for transmit queue access */
4570 	struct lpfc_iocbq *iocb, *next_iocb;
4571 	int offline;
4572 
4573 	if (phba->sli_rev >= LPFC_SLI_REV4)
4574 		plock = &pring->ring_lock;
4575 	else
4576 		plock = &phba->hbalock;
4577 
4578 	if (pring->ringno == LPFC_ELS_RING)
4579 		lpfc_fabric_abort_hba(phba);
4580 
4581 	offline = pci_channel_offline(phba->pcidev);
4582 
4583 	/* Cancel everything on txq */
4584 	spin_lock_irq(plock);
4585 	list_splice_init(&pring->txq, &tx_completions);
4586 	pring->txq_cnt = 0;
4587 
4588 	if (offline) {
4589 		/* Cancel everything on txcmplq */
4590 		list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4591 			iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4592 		list_splice_init(&pring->txcmplq, &tx_completions);
4593 		pring->txcmplq_cnt = 0;
4594 	} else {
4595 		/* Issue ABTS for everything on the txcmplq */
4596 		list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4597 			lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
4598 	}
4599 	spin_unlock_irq(plock);
4600 
4601 	if (!offline)
4602 		lpfc_issue_hb_tmo(phba);
4603 
4604 	/* Cancel all the IOCBs from the completions list */
4605 	lpfc_sli_cancel_iocbs(phba, &tx_completions, IOSTAT_LOCAL_REJECT,
4606 			      IOERR_SLI_ABORTED);
4607 }
4608 
4609 /**
4610  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4611  * @phba: Pointer to HBA context object.
4612  *
4613  * This function aborts all iocbs in FCP rings and frees all the iocb
4614  * objects in txq. This function issues an abort iocb for all the iocb commands
4615  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4616  * the return of this function. The caller is not required to hold any locks.
4617  **/
4618 void
lpfc_sli_abort_fcp_rings(struct lpfc_hba * phba)4619 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4620 {
4621 	struct lpfc_sli *psli = &phba->sli;
4622 	struct lpfc_sli_ring  *pring;
4623 	uint32_t i;
4624 
4625 	/* Look on all the FCP Rings for the iotag */
4626 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4627 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4628 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4629 			lpfc_sli_abort_iocb_ring(phba, pring);
4630 		}
4631 	} else {
4632 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4633 		lpfc_sli_abort_iocb_ring(phba, pring);
4634 	}
4635 }
4636 
4637 /**
4638  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4639  * @phba: Pointer to HBA context object.
4640  *
4641  * This function flushes all iocbs in the IO ring and frees all the iocb
4642  * objects in txq and txcmplq. This function will not issue abort iocbs
4643  * for all the iocb commands in txcmplq, they will just be returned with
4644  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4645  * slot has been permanently disabled.
4646  **/
4647 void
lpfc_sli_flush_io_rings(struct lpfc_hba * phba)4648 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4649 {
4650 	LIST_HEAD(txq);
4651 	LIST_HEAD(txcmplq);
4652 	struct lpfc_sli *psli = &phba->sli;
4653 	struct lpfc_sli_ring  *pring;
4654 	uint32_t i;
4655 	struct lpfc_iocbq *piocb, *next_iocb;
4656 
4657 	/* Indicate the I/O queues are flushed */
4658 	set_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
4659 
4660 	/* Look on all the FCP Rings for the iotag */
4661 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4662 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4663 			if (!phba->sli4_hba.hdwq ||
4664 			    !phba->sli4_hba.hdwq[i].io_wq) {
4665 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4666 						"7777 hdwq's deleted %lx "
4667 						"%lx %x %x\n",
4668 						phba->pport->load_flag,
4669 						phba->hba_flag,
4670 						phba->link_state,
4671 						phba->sli.sli_flag);
4672 				return;
4673 			}
4674 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4675 
4676 			spin_lock_irq(&pring->ring_lock);
4677 			/* Retrieve everything on txq */
4678 			list_splice_init(&pring->txq, &txq);
4679 			list_for_each_entry_safe(piocb, next_iocb,
4680 						 &pring->txcmplq, list)
4681 				piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4682 			/* Retrieve everything on the txcmplq */
4683 			list_splice_init(&pring->txcmplq, &txcmplq);
4684 			pring->txq_cnt = 0;
4685 			pring->txcmplq_cnt = 0;
4686 			spin_unlock_irq(&pring->ring_lock);
4687 
4688 			/* Flush the txq */
4689 			lpfc_sli_cancel_iocbs(phba, &txq,
4690 					      IOSTAT_LOCAL_REJECT,
4691 					      IOERR_SLI_DOWN);
4692 			/* Flush the txcmplq */
4693 			lpfc_sli_cancel_iocbs(phba, &txcmplq,
4694 					      IOSTAT_LOCAL_REJECT,
4695 					      IOERR_SLI_DOWN);
4696 			if (unlikely(pci_channel_offline(phba->pcidev)))
4697 				lpfc_sli4_io_xri_aborted(phba, NULL, 0);
4698 		}
4699 	} else {
4700 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4701 
4702 		spin_lock_irq(&phba->hbalock);
4703 		/* Retrieve everything on txq */
4704 		list_splice_init(&pring->txq, &txq);
4705 		list_for_each_entry_safe(piocb, next_iocb,
4706 					 &pring->txcmplq, list)
4707 			piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4708 		/* Retrieve everything on the txcmplq */
4709 		list_splice_init(&pring->txcmplq, &txcmplq);
4710 		pring->txq_cnt = 0;
4711 		pring->txcmplq_cnt = 0;
4712 		spin_unlock_irq(&phba->hbalock);
4713 
4714 		/* Flush the txq */
4715 		lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4716 				      IOERR_SLI_DOWN);
4717 		/* Flush the txcmpq */
4718 		lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4719 				      IOERR_SLI_DOWN);
4720 	}
4721 }
4722 
4723 /**
4724  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4725  * @phba: Pointer to HBA context object.
4726  * @mask: Bit mask to be checked.
4727  *
4728  * This function reads the host status register and compares
4729  * with the provided bit mask to check if HBA completed
4730  * the restart. This function will wait in a loop for the
4731  * HBA to complete restart. If the HBA does not restart within
4732  * 15 iterations, the function will reset the HBA again. The
4733  * function returns 1 when HBA fail to restart otherwise returns
4734  * zero.
4735  **/
4736 static int
lpfc_sli_brdready_s3(struct lpfc_hba * phba,uint32_t mask)4737 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4738 {
4739 	uint32_t status;
4740 	int i = 0;
4741 	int retval = 0;
4742 
4743 	/* Read the HBA Host Status Register */
4744 	if (lpfc_readl(phba->HSregaddr, &status))
4745 		return 1;
4746 
4747 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
4748 
4749 	/*
4750 	 * Check status register every 100ms for 5 retries, then every
4751 	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4752 	 * every 2.5 sec for 4.
4753 	 * Break our of the loop if errors occurred during init.
4754 	 */
4755 	while (((status & mask) != mask) &&
4756 	       !(status & HS_FFERM) &&
4757 	       i++ < 20) {
4758 
4759 		if (i <= 5)
4760 			msleep(10);
4761 		else if (i <= 10)
4762 			msleep(500);
4763 		else
4764 			msleep(2500);
4765 
4766 		if (i == 15) {
4767 				/* Do post */
4768 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4769 			lpfc_sli_brdrestart(phba);
4770 		}
4771 		/* Read the HBA Host Status Register */
4772 		if (lpfc_readl(phba->HSregaddr, &status)) {
4773 			retval = 1;
4774 			break;
4775 		}
4776 	}
4777 
4778 	/* Check to see if any errors occurred during init */
4779 	if ((status & HS_FFERM) || (i >= 20)) {
4780 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4781 				"2751 Adapter failed to restart, "
4782 				"status reg x%x, FW Data: A8 x%x AC x%x\n",
4783 				status,
4784 				readl(phba->MBslimaddr + 0xa8),
4785 				readl(phba->MBslimaddr + 0xac));
4786 		phba->link_state = LPFC_HBA_ERROR;
4787 		retval = 1;
4788 	}
4789 
4790 	return retval;
4791 }
4792 
4793 /**
4794  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4795  * @phba: Pointer to HBA context object.
4796  * @mask: Bit mask to be checked.
4797  *
4798  * This function checks the host status register to check if HBA is
4799  * ready. This function will wait in a loop for the HBA to be ready
4800  * If the HBA is not ready , the function will will reset the HBA PCI
4801  * function again. The function returns 1 when HBA fail to be ready
4802  * otherwise returns zero.
4803  **/
4804 static int
lpfc_sli_brdready_s4(struct lpfc_hba * phba,uint32_t mask)4805 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4806 {
4807 	uint32_t status;
4808 	int retval = 0;
4809 
4810 	/* Read the HBA Host Status Register */
4811 	status = lpfc_sli4_post_status_check(phba);
4812 
4813 	if (status) {
4814 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4815 		lpfc_sli_brdrestart(phba);
4816 		status = lpfc_sli4_post_status_check(phba);
4817 	}
4818 
4819 	/* Check to see if any errors occurred during init */
4820 	if (status) {
4821 		phba->link_state = LPFC_HBA_ERROR;
4822 		retval = 1;
4823 	} else
4824 		phba->sli4_hba.intr_enable = 0;
4825 
4826 	clear_bit(HBA_SETUP, &phba->hba_flag);
4827 	return retval;
4828 }
4829 
4830 /**
4831  * lpfc_sli_brdready - Wrapper func for checking the hba readiness
4832  * @phba: Pointer to HBA context object.
4833  * @mask: Bit mask to be checked.
4834  *
4835  * This routine wraps the actual SLI3 or SLI4 hba readiness check routine
4836  * from the API jump table function pointer from the lpfc_hba struct.
4837  **/
4838 int
lpfc_sli_brdready(struct lpfc_hba * phba,uint32_t mask)4839 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4840 {
4841 	return phba->lpfc_sli_brdready(phba, mask);
4842 }
4843 
4844 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4845 
4846 /**
4847  * lpfc_reset_barrier - Make HBA ready for HBA reset
4848  * @phba: Pointer to HBA context object.
4849  *
4850  * This function is called before resetting an HBA. This function is called
4851  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4852  **/
lpfc_reset_barrier(struct lpfc_hba * phba)4853 void lpfc_reset_barrier(struct lpfc_hba *phba)
4854 {
4855 	uint32_t __iomem *resp_buf;
4856 	uint32_t __iomem *mbox_buf;
4857 	volatile struct MAILBOX_word0 mbox;
4858 	uint32_t hc_copy, ha_copy, resp_data;
4859 	int  i;
4860 	uint8_t hdrtype;
4861 
4862 	lockdep_assert_held(&phba->hbalock);
4863 
4864 	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4865 	if (hdrtype != PCI_HEADER_TYPE_MFD ||
4866 	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4867 	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4868 		return;
4869 
4870 	/*
4871 	 * Tell the other part of the chip to suspend temporarily all
4872 	 * its DMA activity.
4873 	 */
4874 	resp_buf = phba->MBslimaddr;
4875 
4876 	/* Disable the error attention */
4877 	if (lpfc_readl(phba->HCregaddr, &hc_copy))
4878 		return;
4879 	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4880 	readl(phba->HCregaddr); /* flush */
4881 	phba->link_flag |= LS_IGNORE_ERATT;
4882 
4883 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
4884 		return;
4885 	if (ha_copy & HA_ERATT) {
4886 		/* Clear Chip error bit */
4887 		writel(HA_ERATT, phba->HAregaddr);
4888 		phba->pport->stopped = 1;
4889 	}
4890 
4891 	mbox.word0 = 0;
4892 	mbox.mbxCommand = MBX_KILL_BOARD;
4893 	mbox.mbxOwner = OWN_CHIP;
4894 
4895 	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4896 	mbox_buf = phba->MBslimaddr;
4897 	writel(mbox.word0, mbox_buf);
4898 
4899 	for (i = 0; i < 50; i++) {
4900 		if (lpfc_readl((resp_buf + 1), &resp_data))
4901 			return;
4902 		if (resp_data != ~(BARRIER_TEST_PATTERN))
4903 			mdelay(1);
4904 		else
4905 			break;
4906 	}
4907 	resp_data = 0;
4908 	if (lpfc_readl((resp_buf + 1), &resp_data))
4909 		return;
4910 	if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4911 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4912 		    phba->pport->stopped)
4913 			goto restore_hc;
4914 		else
4915 			goto clear_errat;
4916 	}
4917 
4918 	mbox.mbxOwner = OWN_HOST;
4919 	resp_data = 0;
4920 	for (i = 0; i < 500; i++) {
4921 		if (lpfc_readl(resp_buf, &resp_data))
4922 			return;
4923 		if (resp_data != mbox.word0)
4924 			mdelay(1);
4925 		else
4926 			break;
4927 	}
4928 
4929 clear_errat:
4930 
4931 	while (++i < 500) {
4932 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
4933 			return;
4934 		if (!(ha_copy & HA_ERATT))
4935 			mdelay(1);
4936 		else
4937 			break;
4938 	}
4939 
4940 	if (readl(phba->HAregaddr) & HA_ERATT) {
4941 		writel(HA_ERATT, phba->HAregaddr);
4942 		phba->pport->stopped = 1;
4943 	}
4944 
4945 restore_hc:
4946 	phba->link_flag &= ~LS_IGNORE_ERATT;
4947 	writel(hc_copy, phba->HCregaddr);
4948 	readl(phba->HCregaddr); /* flush */
4949 }
4950 
4951 /**
4952  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4953  * @phba: Pointer to HBA context object.
4954  *
4955  * This function issues a kill_board mailbox command and waits for
4956  * the error attention interrupt. This function is called for stopping
4957  * the firmware processing. The caller is not required to hold any
4958  * locks. This function calls lpfc_hba_down_post function to free
4959  * any pending commands after the kill. The function will return 1 when it
4960  * fails to kill the board else will return 0.
4961  **/
4962 int
lpfc_sli_brdkill(struct lpfc_hba * phba)4963 lpfc_sli_brdkill(struct lpfc_hba *phba)
4964 {
4965 	struct lpfc_sli *psli;
4966 	LPFC_MBOXQ_t *pmb;
4967 	uint32_t status;
4968 	uint32_t ha_copy;
4969 	int retval;
4970 	int i = 0;
4971 
4972 	psli = &phba->sli;
4973 
4974 	/* Kill HBA */
4975 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4976 			"0329 Kill HBA Data: x%x x%x\n",
4977 			phba->pport->port_state, psli->sli_flag);
4978 
4979 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4980 	if (!pmb)
4981 		return 1;
4982 
4983 	/* Disable the error attention */
4984 	spin_lock_irq(&phba->hbalock);
4985 	if (lpfc_readl(phba->HCregaddr, &status)) {
4986 		spin_unlock_irq(&phba->hbalock);
4987 		mempool_free(pmb, phba->mbox_mem_pool);
4988 		return 1;
4989 	}
4990 	status &= ~HC_ERINT_ENA;
4991 	writel(status, phba->HCregaddr);
4992 	readl(phba->HCregaddr); /* flush */
4993 	phba->link_flag |= LS_IGNORE_ERATT;
4994 	spin_unlock_irq(&phba->hbalock);
4995 
4996 	lpfc_kill_board(phba, pmb);
4997 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4998 	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4999 
5000 	if (retval != MBX_SUCCESS) {
5001 		if (retval != MBX_BUSY)
5002 			mempool_free(pmb, phba->mbox_mem_pool);
5003 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5004 				"2752 KILL_BOARD command failed retval %d\n",
5005 				retval);
5006 		spin_lock_irq(&phba->hbalock);
5007 		phba->link_flag &= ~LS_IGNORE_ERATT;
5008 		spin_unlock_irq(&phba->hbalock);
5009 		return 1;
5010 	}
5011 
5012 	spin_lock_irq(&phba->hbalock);
5013 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5014 	spin_unlock_irq(&phba->hbalock);
5015 
5016 	mempool_free(pmb, phba->mbox_mem_pool);
5017 
5018 	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
5019 	 * attention every 100ms for 3 seconds. If we don't get ERATT after
5020 	 * 3 seconds we still set HBA_ERROR state because the status of the
5021 	 * board is now undefined.
5022 	 */
5023 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
5024 		return 1;
5025 	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
5026 		mdelay(100);
5027 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
5028 			return 1;
5029 	}
5030 
5031 	timer_delete_sync(&psli->mbox_tmo);
5032 	if (ha_copy & HA_ERATT) {
5033 		writel(HA_ERATT, phba->HAregaddr);
5034 		phba->pport->stopped = 1;
5035 	}
5036 	spin_lock_irq(&phba->hbalock);
5037 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5038 	psli->mbox_active = NULL;
5039 	phba->link_flag &= ~LS_IGNORE_ERATT;
5040 	spin_unlock_irq(&phba->hbalock);
5041 
5042 	lpfc_hba_down_post(phba);
5043 	phba->link_state = LPFC_HBA_ERROR;
5044 
5045 	return ha_copy & HA_ERATT ? 0 : 1;
5046 }
5047 
5048 /**
5049  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
5050  * @phba: Pointer to HBA context object.
5051  *
5052  * This function resets the HBA by writing HC_INITFF to the control
5053  * register. After the HBA resets, this function resets all the iocb ring
5054  * indices. This function disables PCI layer parity checking during
5055  * the reset.
5056  * This function returns 0 always.
5057  * The caller is not required to hold any locks.
5058  **/
5059 int
lpfc_sli_brdreset(struct lpfc_hba * phba)5060 lpfc_sli_brdreset(struct lpfc_hba *phba)
5061 {
5062 	struct lpfc_sli *psli;
5063 	struct lpfc_sli_ring *pring;
5064 	uint16_t cfg_value;
5065 	int i;
5066 
5067 	psli = &phba->sli;
5068 
5069 	/* Reset HBA */
5070 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5071 			"0325 Reset HBA Data: x%x x%x\n",
5072 			(phba->pport) ? phba->pport->port_state : 0,
5073 			psli->sli_flag);
5074 
5075 	/* perform board reset */
5076 	phba->fc_eventTag = 0;
5077 	phba->link_events = 0;
5078 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5079 	if (phba->pport) {
5080 		phba->pport->fc_myDID = 0;
5081 		phba->pport->fc_prevDID = 0;
5082 	}
5083 
5084 	/* Turn off parity checking and serr during the physical reset */
5085 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
5086 		return -EIO;
5087 
5088 	pci_write_config_word(phba->pcidev, PCI_COMMAND,
5089 			      (cfg_value &
5090 			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5091 
5092 	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
5093 
5094 	/* Now toggle INITFF bit in the Host Control Register */
5095 	writel(HC_INITFF, phba->HCregaddr);
5096 	mdelay(1);
5097 	readl(phba->HCregaddr); /* flush */
5098 	writel(0, phba->HCregaddr);
5099 	readl(phba->HCregaddr); /* flush */
5100 
5101 	/* Restore PCI cmd register */
5102 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5103 
5104 	/* Initialize relevant SLI info */
5105 	for (i = 0; i < psli->num_rings; i++) {
5106 		pring = &psli->sli3_ring[i];
5107 		pring->flag = 0;
5108 		pring->sli.sli3.rspidx = 0;
5109 		pring->sli.sli3.next_cmdidx  = 0;
5110 		pring->sli.sli3.local_getidx = 0;
5111 		pring->sli.sli3.cmdidx = 0;
5112 		pring->missbufcnt = 0;
5113 	}
5114 
5115 	phba->link_state = LPFC_WARM_START;
5116 	return 0;
5117 }
5118 
5119 /**
5120  * lpfc_sli4_brdreset - Reset a sli-4 HBA
5121  * @phba: Pointer to HBA context object.
5122  *
5123  * This function resets a SLI4 HBA. This function disables PCI layer parity
5124  * checking during resets the device. The caller is not required to hold
5125  * any locks.
5126  *
5127  * This function returns 0 on success else returns negative error code.
5128  **/
5129 int
lpfc_sli4_brdreset(struct lpfc_hba * phba)5130 lpfc_sli4_brdreset(struct lpfc_hba *phba)
5131 {
5132 	struct lpfc_sli *psli = &phba->sli;
5133 	uint16_t cfg_value;
5134 	int rc = 0;
5135 
5136 	/* Reset HBA */
5137 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5138 			"0295 Reset HBA Data: x%x x%x x%lx\n",
5139 			phba->pport->port_state, psli->sli_flag,
5140 			phba->hba_flag);
5141 
5142 	/* perform board reset */
5143 	phba->fc_eventTag = 0;
5144 	phba->link_events = 0;
5145 	phba->pport->fc_myDID = 0;
5146 	phba->pport->fc_prevDID = 0;
5147 
5148 	spin_lock_irq(&phba->hbalock);
5149 	psli->sli_flag &= ~(LPFC_PROCESS_LA);
5150 	phba->fcf.fcf_flag = 0;
5151 	spin_unlock_irq(&phba->hbalock);
5152 
5153 	/* Now physically reset the device */
5154 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5155 			"0389 Performing PCI function reset!\n");
5156 
5157 	/* Turn off parity checking and serr during the physical reset */
5158 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5159 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5160 				"3205 PCI read Config failed\n");
5161 		return -EIO;
5162 	}
5163 
5164 	pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5165 			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5166 
5167 	/* Perform FCoE PCI function reset before freeing queue memory */
5168 	rc = lpfc_pci_function_reset(phba);
5169 
5170 	/* Restore PCI cmd register */
5171 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5172 
5173 	return rc;
5174 }
5175 
5176 /**
5177  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
5178  * @phba: Pointer to HBA context object.
5179  *
5180  * This function is called in the SLI initialization code path to
5181  * restart the HBA. The caller is not required to hold any lock.
5182  * This function writes MBX_RESTART mailbox command to the SLIM and
5183  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5184  * function to free any pending commands. The function enables
5185  * POST only during the first initialization. The function returns zero.
5186  * The function does not guarantee completion of MBX_RESTART mailbox
5187  * command before the return of this function.
5188  **/
5189 static int
lpfc_sli_brdrestart_s3(struct lpfc_hba * phba)5190 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
5191 {
5192 	volatile struct MAILBOX_word0 mb;
5193 	struct lpfc_sli *psli;
5194 	void __iomem *to_slim;
5195 
5196 	spin_lock_irq(&phba->hbalock);
5197 
5198 	psli = &phba->sli;
5199 
5200 	/* Restart HBA */
5201 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5202 			"0337 Restart HBA Data: x%x x%x\n",
5203 			(phba->pport) ? phba->pport->port_state : 0,
5204 			psli->sli_flag);
5205 
5206 	mb.word0 = 0;
5207 	mb.mbxCommand = MBX_RESTART;
5208 	mb.mbxHc = 1;
5209 
5210 	lpfc_reset_barrier(phba);
5211 
5212 	to_slim = phba->MBslimaddr;
5213 	writel(mb.word0, to_slim);
5214 	readl(to_slim); /* flush */
5215 
5216 	/* Only skip post after fc_ffinit is completed */
5217 	if (phba->pport && phba->pport->port_state)
5218 		mb.word0 = 1;	/* This is really setting up word1 */
5219 	else
5220 		mb.word0 = 0;	/* This is really setting up word1 */
5221 	to_slim = phba->MBslimaddr + sizeof (uint32_t);
5222 	writel(mb.word0, to_slim);
5223 	readl(to_slim); /* flush */
5224 
5225 	lpfc_sli_brdreset(phba);
5226 	if (phba->pport)
5227 		phba->pport->stopped = 0;
5228 	phba->link_state = LPFC_INIT_START;
5229 	phba->hba_flag = 0;
5230 	spin_unlock_irq(&phba->hbalock);
5231 
5232 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5233 	psli->stats_start = ktime_get_seconds();
5234 
5235 	/* Give the INITFF and Post time to settle. */
5236 	mdelay(100);
5237 
5238 	lpfc_hba_down_post(phba);
5239 
5240 	return 0;
5241 }
5242 
5243 /**
5244  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5245  * @phba: Pointer to HBA context object.
5246  *
5247  * This function is called in the SLI initialization code path to restart
5248  * a SLI4 HBA. The caller is not required to hold any lock.
5249  * At the end of the function, it calls lpfc_hba_down_post function to
5250  * free any pending commands.
5251  **/
5252 static int
lpfc_sli_brdrestart_s4(struct lpfc_hba * phba)5253 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5254 {
5255 	struct lpfc_sli *psli = &phba->sli;
5256 	int rc;
5257 
5258 	/* Restart HBA */
5259 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5260 			"0296 Restart HBA Data: x%x x%x\n",
5261 			phba->pport->port_state, psli->sli_flag);
5262 
5263 	clear_bit(HBA_SETUP, &phba->hba_flag);
5264 	lpfc_sli4_queue_unset(phba);
5265 
5266 	rc = lpfc_sli4_brdreset(phba);
5267 	if (rc) {
5268 		phba->link_state = LPFC_HBA_ERROR;
5269 		goto hba_down_queue;
5270 	}
5271 
5272 	spin_lock_irq(&phba->hbalock);
5273 	phba->pport->stopped = 0;
5274 	phba->link_state = LPFC_INIT_START;
5275 	phba->hba_flag = 0;
5276 	/* Preserve FA-PWWN expectation */
5277 	phba->sli4_hba.fawwpn_flag &= LPFC_FAWWPN_FABRIC;
5278 	spin_unlock_irq(&phba->hbalock);
5279 
5280 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5281 	psli->stats_start = ktime_get_seconds();
5282 
5283 hba_down_queue:
5284 	lpfc_hba_down_post(phba);
5285 	lpfc_sli4_queue_destroy(phba);
5286 
5287 	return rc;
5288 }
5289 
5290 /**
5291  * lpfc_sli_brdrestart - Wrapper func for restarting hba
5292  * @phba: Pointer to HBA context object.
5293  *
5294  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5295  * API jump table function pointer from the lpfc_hba struct.
5296 **/
5297 int
lpfc_sli_brdrestart(struct lpfc_hba * phba)5298 lpfc_sli_brdrestart(struct lpfc_hba *phba)
5299 {
5300 	return phba->lpfc_sli_brdrestart(phba);
5301 }
5302 
5303 /**
5304  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
5305  * @phba: Pointer to HBA context object.
5306  *
5307  * This function is called after a HBA restart to wait for successful
5308  * restart of the HBA. Successful restart of the HBA is indicated by
5309  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5310  * iteration, the function will restart the HBA again. The function returns
5311  * zero if HBA successfully restarted else returns negative error code.
5312  **/
5313 int
lpfc_sli_chipset_init(struct lpfc_hba * phba)5314 lpfc_sli_chipset_init(struct lpfc_hba *phba)
5315 {
5316 	uint32_t status, i = 0;
5317 
5318 	/* Read the HBA Host Status Register */
5319 	if (lpfc_readl(phba->HSregaddr, &status))
5320 		return -EIO;
5321 
5322 	/* Check status register to see what current state is */
5323 	i = 0;
5324 	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5325 
5326 		/* Check every 10ms for 10 retries, then every 100ms for 90
5327 		 * retries, then every 1 sec for 50 retires for a total of
5328 		 * ~60 seconds before reset the board again and check every
5329 		 * 1 sec for 50 retries. The up to 60 seconds before the
5330 		 * board ready is required by the Falcon FIPS zeroization
5331 		 * complete, and any reset the board in between shall cause
5332 		 * restart of zeroization, further delay the board ready.
5333 		 */
5334 		if (i++ >= 200) {
5335 			/* Adapter failed to init, timeout, status reg
5336 			   <status> */
5337 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5338 					"0436 Adapter failed to init, "
5339 					"timeout, status reg x%x, "
5340 					"FW Data: A8 x%x AC x%x\n", status,
5341 					readl(phba->MBslimaddr + 0xa8),
5342 					readl(phba->MBslimaddr + 0xac));
5343 			phba->link_state = LPFC_HBA_ERROR;
5344 			return -ETIMEDOUT;
5345 		}
5346 
5347 		/* Check to see if any errors occurred during init */
5348 		if (status & HS_FFERM) {
5349 			/* ERROR: During chipset initialization */
5350 			/* Adapter failed to init, chipset, status reg
5351 			   <status> */
5352 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5353 					"0437 Adapter failed to init, "
5354 					"chipset, status reg x%x, "
5355 					"FW Data: A8 x%x AC x%x\n", status,
5356 					readl(phba->MBslimaddr + 0xa8),
5357 					readl(phba->MBslimaddr + 0xac));
5358 			phba->link_state = LPFC_HBA_ERROR;
5359 			return -EIO;
5360 		}
5361 
5362 		if (i <= 10)
5363 			msleep(10);
5364 		else if (i <= 100)
5365 			msleep(100);
5366 		else
5367 			msleep(1000);
5368 
5369 		if (i == 150) {
5370 			/* Do post */
5371 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5372 			lpfc_sli_brdrestart(phba);
5373 		}
5374 		/* Read the HBA Host Status Register */
5375 		if (lpfc_readl(phba->HSregaddr, &status))
5376 			return -EIO;
5377 	}
5378 
5379 	/* Check to see if any errors occurred during init */
5380 	if (status & HS_FFERM) {
5381 		/* ERROR: During chipset initialization */
5382 		/* Adapter failed to init, chipset, status reg <status> */
5383 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5384 				"0438 Adapter failed to init, chipset, "
5385 				"status reg x%x, "
5386 				"FW Data: A8 x%x AC x%x\n", status,
5387 				readl(phba->MBslimaddr + 0xa8),
5388 				readl(phba->MBslimaddr + 0xac));
5389 		phba->link_state = LPFC_HBA_ERROR;
5390 		return -EIO;
5391 	}
5392 
5393 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5394 
5395 	/* Clear all interrupt enable conditions */
5396 	writel(0, phba->HCregaddr);
5397 	readl(phba->HCregaddr); /* flush */
5398 
5399 	/* setup host attn register */
5400 	writel(0xffffffff, phba->HAregaddr);
5401 	readl(phba->HAregaddr); /* flush */
5402 	return 0;
5403 }
5404 
5405 /**
5406  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5407  *
5408  * This function calculates and returns the number of HBQs required to be
5409  * configured.
5410  **/
5411 int
lpfc_sli_hbq_count(void)5412 lpfc_sli_hbq_count(void)
5413 {
5414 	return ARRAY_SIZE(lpfc_hbq_defs);
5415 }
5416 
5417 /**
5418  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5419  *
5420  * This function adds the number of hbq entries in every HBQ to get
5421  * the total number of hbq entries required for the HBA and returns
5422  * the total count.
5423  **/
5424 static int
lpfc_sli_hbq_entry_count(void)5425 lpfc_sli_hbq_entry_count(void)
5426 {
5427 	int  hbq_count = lpfc_sli_hbq_count();
5428 	int  count = 0;
5429 	int  i;
5430 
5431 	for (i = 0; i < hbq_count; ++i)
5432 		count += lpfc_hbq_defs[i]->entry_count;
5433 	return count;
5434 }
5435 
5436 /**
5437  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5438  *
5439  * This function calculates amount of memory required for all hbq entries
5440  * to be configured and returns the total memory required.
5441  **/
5442 int
lpfc_sli_hbq_size(void)5443 lpfc_sli_hbq_size(void)
5444 {
5445 	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5446 }
5447 
5448 /**
5449  * lpfc_sli_hbq_setup - configure and initialize HBQs
5450  * @phba: Pointer to HBA context object.
5451  *
5452  * This function is called during the SLI initialization to configure
5453  * all the HBQs and post buffers to the HBQ. The caller is not
5454  * required to hold any locks. This function will return zero if successful
5455  * else it will return negative error code.
5456  **/
5457 static int
lpfc_sli_hbq_setup(struct lpfc_hba * phba)5458 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5459 {
5460 	int  hbq_count = lpfc_sli_hbq_count();
5461 	LPFC_MBOXQ_t *pmb;
5462 	MAILBOX_t *pmbox;
5463 	uint32_t hbqno;
5464 	uint32_t hbq_entry_index;
5465 
5466 				/* Get a Mailbox buffer to setup mailbox
5467 				 * commands for HBA initialization
5468 				 */
5469 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5470 
5471 	if (!pmb)
5472 		return -ENOMEM;
5473 
5474 	pmbox = &pmb->u.mb;
5475 
5476 	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
5477 	phba->link_state = LPFC_INIT_MBX_CMDS;
5478 	phba->hbq_in_use = 1;
5479 
5480 	hbq_entry_index = 0;
5481 	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5482 		phba->hbqs[hbqno].next_hbqPutIdx = 0;
5483 		phba->hbqs[hbqno].hbqPutIdx      = 0;
5484 		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5485 		phba->hbqs[hbqno].entry_count =
5486 			lpfc_hbq_defs[hbqno]->entry_count;
5487 		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5488 			hbq_entry_index, pmb);
5489 		hbq_entry_index += phba->hbqs[hbqno].entry_count;
5490 
5491 		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5492 			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5493 			   mbxStatus <status>, ring <num> */
5494 
5495 			lpfc_printf_log(phba, KERN_ERR,
5496 					LOG_SLI | LOG_VPORT,
5497 					"1805 Adapter failed to init. "
5498 					"Data: x%x x%x x%x\n",
5499 					pmbox->mbxCommand,
5500 					pmbox->mbxStatus, hbqno);
5501 
5502 			phba->link_state = LPFC_HBA_ERROR;
5503 			mempool_free(pmb, phba->mbox_mem_pool);
5504 			return -ENXIO;
5505 		}
5506 	}
5507 	phba->hbq_count = hbq_count;
5508 
5509 	mempool_free(pmb, phba->mbox_mem_pool);
5510 
5511 	/* Initially populate or replenish the HBQs */
5512 	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5513 		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5514 	return 0;
5515 }
5516 
5517 /**
5518  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5519  * @phba: Pointer to HBA context object.
5520  *
5521  * This function is called during the SLI initialization to configure
5522  * all the HBQs and post buffers to the HBQ. The caller is not
5523  * required to hold any locks. This function will return zero if successful
5524  * else it will return negative error code.
5525  **/
5526 static int
lpfc_sli4_rb_setup(struct lpfc_hba * phba)5527 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5528 {
5529 	phba->hbq_in_use = 1;
5530 	/**
5531 	 * Specific case when the MDS diagnostics is enabled and supported.
5532 	 * The receive buffer count is truncated to manage the incoming
5533 	 * traffic.
5534 	 **/
5535 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5536 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5537 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5538 	else
5539 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5540 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5541 	phba->hbq_count = 1;
5542 	lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5543 	/* Initially populate or replenish the HBQs */
5544 	return 0;
5545 }
5546 
5547 /**
5548  * lpfc_sli_config_port - Issue config port mailbox command
5549  * @phba: Pointer to HBA context object.
5550  * @sli_mode: sli mode - 2/3
5551  *
5552  * This function is called by the sli initialization code path
5553  * to issue config_port mailbox command. This function restarts the
5554  * HBA firmware and issues a config_port mailbox command to configure
5555  * the SLI interface in the sli mode specified by sli_mode
5556  * variable. The caller is not required to hold any locks.
5557  * The function returns 0 if successful, else returns negative error
5558  * code.
5559  **/
5560 int
lpfc_sli_config_port(struct lpfc_hba * phba,int sli_mode)5561 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5562 {
5563 	LPFC_MBOXQ_t *pmb;
5564 	uint32_t resetcount = 0, rc = 0, done = 0;
5565 
5566 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5567 	if (!pmb) {
5568 		phba->link_state = LPFC_HBA_ERROR;
5569 		return -ENOMEM;
5570 	}
5571 
5572 	phba->sli_rev = sli_mode;
5573 	while (resetcount < 2 && !done) {
5574 		spin_lock_irq(&phba->hbalock);
5575 		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5576 		spin_unlock_irq(&phba->hbalock);
5577 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5578 		lpfc_sli_brdrestart(phba);
5579 		rc = lpfc_sli_chipset_init(phba);
5580 		if (rc)
5581 			break;
5582 
5583 		spin_lock_irq(&phba->hbalock);
5584 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5585 		spin_unlock_irq(&phba->hbalock);
5586 		resetcount++;
5587 
5588 		/* Call pre CONFIG_PORT mailbox command initialization.  A
5589 		 * value of 0 means the call was successful.  Any other
5590 		 * nonzero value is a failure, but if ERESTART is returned,
5591 		 * the driver may reset the HBA and try again.
5592 		 */
5593 		rc = lpfc_config_port_prep(phba);
5594 		if (rc == -ERESTART) {
5595 			phba->link_state = LPFC_LINK_UNKNOWN;
5596 			continue;
5597 		} else if (rc)
5598 			break;
5599 
5600 		phba->link_state = LPFC_INIT_MBX_CMDS;
5601 		lpfc_config_port(phba, pmb);
5602 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5603 		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5604 					LPFC_SLI3_HBQ_ENABLED |
5605 					LPFC_SLI3_CRP_ENABLED |
5606 					LPFC_SLI3_DSS_ENABLED);
5607 		if (rc != MBX_SUCCESS) {
5608 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5609 				"0442 Adapter failed to init, mbxCmd x%x "
5610 				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5611 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5612 			spin_lock_irq(&phba->hbalock);
5613 			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5614 			spin_unlock_irq(&phba->hbalock);
5615 			rc = -ENXIO;
5616 		} else {
5617 			/* Allow asynchronous mailbox command to go through */
5618 			spin_lock_irq(&phba->hbalock);
5619 			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5620 			spin_unlock_irq(&phba->hbalock);
5621 			done = 1;
5622 
5623 			if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5624 			    (pmb->u.mb.un.varCfgPort.gasabt == 0))
5625 				lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5626 					"3110 Port did not grant ASABT\n");
5627 		}
5628 	}
5629 	if (!done) {
5630 		rc = -EINVAL;
5631 		goto do_prep_failed;
5632 	}
5633 	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5634 		if (!pmb->u.mb.un.varCfgPort.cMA) {
5635 			rc = -ENXIO;
5636 			goto do_prep_failed;
5637 		}
5638 		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5639 			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5640 			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5641 			phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5642 				phba->max_vpi : phba->max_vports;
5643 
5644 		} else
5645 			phba->max_vpi = 0;
5646 		if (pmb->u.mb.un.varCfgPort.gerbm)
5647 			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5648 		if (pmb->u.mb.un.varCfgPort.gcrp)
5649 			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5650 
5651 		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5652 		phba->port_gp = phba->mbox->us.s3_pgp.port;
5653 
5654 		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5655 			if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5656 				phba->cfg_enable_bg = 0;
5657 				phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5658 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5659 						"0443 Adapter did not grant "
5660 						"BlockGuard\n");
5661 			}
5662 		}
5663 	} else {
5664 		phba->hbq_get = NULL;
5665 		phba->port_gp = phba->mbox->us.s2.port;
5666 		phba->max_vpi = 0;
5667 	}
5668 do_prep_failed:
5669 	mempool_free(pmb, phba->mbox_mem_pool);
5670 	return rc;
5671 }
5672 
5673 
5674 /**
5675  * lpfc_sli_hba_setup - SLI initialization function
5676  * @phba: Pointer to HBA context object.
5677  *
5678  * This function is the main SLI initialization function. This function
5679  * is called by the HBA initialization code, HBA reset code and HBA
5680  * error attention handler code. Caller is not required to hold any
5681  * locks. This function issues config_port mailbox command to configure
5682  * the SLI, setup iocb rings and HBQ rings. In the end the function
5683  * calls the config_port_post function to issue init_link mailbox
5684  * command and to start the discovery. The function will return zero
5685  * if successful, else it will return negative error code.
5686  **/
5687 int
lpfc_sli_hba_setup(struct lpfc_hba * phba)5688 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5689 {
5690 	uint32_t rc;
5691 	int  i;
5692 	int longs;
5693 
5694 	/* Enable ISR already does config_port because of config_msi mbx */
5695 	if (test_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag)) {
5696 		rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5697 		if (rc)
5698 			return -EIO;
5699 		clear_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5700 	}
5701 	phba->fcp_embed_io = 0;	/* SLI4 FC support only */
5702 
5703 	if (phba->sli_rev == 3) {
5704 		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5705 		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5706 	} else {
5707 		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5708 		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5709 		phba->sli3_options = 0;
5710 	}
5711 
5712 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5713 			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
5714 			phba->sli_rev, phba->max_vpi);
5715 	rc = lpfc_sli_ring_map(phba);
5716 
5717 	if (rc)
5718 		goto lpfc_sli_hba_setup_error;
5719 
5720 	/* Initialize VPIs. */
5721 	if (phba->sli_rev == LPFC_SLI_REV3) {
5722 		/*
5723 		 * The VPI bitmask and physical ID array are allocated
5724 		 * and initialized once only - at driver load.  A port
5725 		 * reset doesn't need to reinitialize this memory.
5726 		 */
5727 		if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5728 			longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5729 			phba->vpi_bmask = kcalloc(longs,
5730 						  sizeof(unsigned long),
5731 						  GFP_KERNEL);
5732 			if (!phba->vpi_bmask) {
5733 				rc = -ENOMEM;
5734 				goto lpfc_sli_hba_setup_error;
5735 			}
5736 
5737 			phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5738 						sizeof(uint16_t),
5739 						GFP_KERNEL);
5740 			if (!phba->vpi_ids) {
5741 				kfree(phba->vpi_bmask);
5742 				rc = -ENOMEM;
5743 				goto lpfc_sli_hba_setup_error;
5744 			}
5745 			for (i = 0; i < phba->max_vpi; i++)
5746 				phba->vpi_ids[i] = i;
5747 		}
5748 	}
5749 
5750 	/* Init HBQs */
5751 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5752 		rc = lpfc_sli_hbq_setup(phba);
5753 		if (rc)
5754 			goto lpfc_sli_hba_setup_error;
5755 	}
5756 	spin_lock_irq(&phba->hbalock);
5757 	phba->sli.sli_flag |= LPFC_PROCESS_LA;
5758 	spin_unlock_irq(&phba->hbalock);
5759 
5760 	rc = lpfc_config_port_post(phba);
5761 	if (rc)
5762 		goto lpfc_sli_hba_setup_error;
5763 
5764 	return rc;
5765 
5766 lpfc_sli_hba_setup_error:
5767 	phba->link_state = LPFC_HBA_ERROR;
5768 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5769 			"0445 Firmware initialization failed\n");
5770 	return rc;
5771 }
5772 
5773 /**
5774  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5775  * @phba: Pointer to HBA context object.
5776  *
5777  * This function issue a dump mailbox command to read config region
5778  * 23 and parse the records in the region and populate driver
5779  * data structure.
5780  **/
5781 static int
lpfc_sli4_read_fcoe_params(struct lpfc_hba * phba)5782 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5783 {
5784 	LPFC_MBOXQ_t *mboxq;
5785 	struct lpfc_dmabuf *mp;
5786 	struct lpfc_mqe *mqe;
5787 	uint32_t data_length;
5788 	int rc;
5789 
5790 	/* Program the default value of vlan_id and fc_map */
5791 	phba->valid_vlan = 0;
5792 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5793 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5794 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5795 
5796 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5797 	if (!mboxq)
5798 		return -ENOMEM;
5799 
5800 	mqe = &mboxq->u.mqe;
5801 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5802 		rc = -ENOMEM;
5803 		goto out_free_mboxq;
5804 	}
5805 
5806 	mp = mboxq->ctx_buf;
5807 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5808 
5809 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5810 			"(%d):2571 Mailbox cmd x%x Status x%x "
5811 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5812 			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5813 			"CQ: x%x x%x x%x x%x\n",
5814 			mboxq->vport ? mboxq->vport->vpi : 0,
5815 			bf_get(lpfc_mqe_command, mqe),
5816 			bf_get(lpfc_mqe_status, mqe),
5817 			mqe->un.mb_words[0], mqe->un.mb_words[1],
5818 			mqe->un.mb_words[2], mqe->un.mb_words[3],
5819 			mqe->un.mb_words[4], mqe->un.mb_words[5],
5820 			mqe->un.mb_words[6], mqe->un.mb_words[7],
5821 			mqe->un.mb_words[8], mqe->un.mb_words[9],
5822 			mqe->un.mb_words[10], mqe->un.mb_words[11],
5823 			mqe->un.mb_words[12], mqe->un.mb_words[13],
5824 			mqe->un.mb_words[14], mqe->un.mb_words[15],
5825 			mqe->un.mb_words[16], mqe->un.mb_words[50],
5826 			mboxq->mcqe.word0,
5827 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
5828 			mboxq->mcqe.trailer);
5829 
5830 	if (rc) {
5831 		rc = -EIO;
5832 		goto out_free_mboxq;
5833 	}
5834 	data_length = mqe->un.mb_words[5];
5835 	if (data_length > DMP_RGN23_SIZE) {
5836 		rc = -EIO;
5837 		goto out_free_mboxq;
5838 	}
5839 
5840 	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5841 	rc = 0;
5842 
5843 out_free_mboxq:
5844 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
5845 	return rc;
5846 }
5847 
5848 /**
5849  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5850  * @phba: pointer to lpfc hba data structure.
5851  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5852  * @vpd: pointer to the memory to hold resulting port vpd data.
5853  * @vpd_size: On input, the number of bytes allocated to @vpd.
5854  *	      On output, the number of data bytes in @vpd.
5855  *
5856  * This routine executes a READ_REV SLI4 mailbox command.  In
5857  * addition, this routine gets the port vpd data.
5858  *
5859  * Return codes
5860  * 	0 - successful
5861  * 	-ENOMEM - could not allocated memory.
5862  **/
5863 static int
lpfc_sli4_read_rev(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint8_t * vpd,uint32_t * vpd_size)5864 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5865 		    uint8_t *vpd, uint32_t *vpd_size)
5866 {
5867 	int rc = 0;
5868 	uint32_t dma_size;
5869 	struct lpfc_dmabuf *dmabuf;
5870 	struct lpfc_mqe *mqe;
5871 
5872 	dmabuf = kzalloc_obj(struct lpfc_dmabuf);
5873 	if (!dmabuf)
5874 		return -ENOMEM;
5875 
5876 	/*
5877 	 * Get a DMA buffer for the vpd data resulting from the READ_REV
5878 	 * mailbox command.
5879 	 */
5880 	dma_size = *vpd_size;
5881 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5882 					  &dmabuf->phys, GFP_KERNEL);
5883 	if (!dmabuf->virt) {
5884 		kfree(dmabuf);
5885 		return -ENOMEM;
5886 	}
5887 
5888 	/*
5889 	 * The SLI4 implementation of READ_REV conflicts at word1,
5890 	 * bits 31:16 and SLI4 adds vpd functionality not present
5891 	 * in SLI3.  This code corrects the conflicts.
5892 	 */
5893 	lpfc_read_rev(phba, mboxq);
5894 	mqe = &mboxq->u.mqe;
5895 	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5896 	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5897 	mqe->un.read_rev.word1 &= 0x0000FFFF;
5898 	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5899 	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5900 
5901 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5902 	if (rc) {
5903 		dma_free_coherent(&phba->pcidev->dev, dma_size,
5904 				  dmabuf->virt, dmabuf->phys);
5905 		kfree(dmabuf);
5906 		return -EIO;
5907 	}
5908 
5909 	/*
5910 	 * The available vpd length cannot be bigger than the
5911 	 * DMA buffer passed to the port.  Catch the less than
5912 	 * case and update the caller's size.
5913 	 */
5914 	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5915 		*vpd_size = mqe->un.read_rev.avail_vpd_len;
5916 
5917 	memcpy(vpd, dmabuf->virt, *vpd_size);
5918 
5919 	dma_free_coherent(&phba->pcidev->dev, dma_size,
5920 			  dmabuf->virt, dmabuf->phys);
5921 	kfree(dmabuf);
5922 	return 0;
5923 }
5924 
5925 /**
5926  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5927  * @phba: pointer to lpfc hba data structure.
5928  *
5929  * This routine retrieves SLI4 device physical port name this PCI function
5930  * is attached to.
5931  *
5932  * Return codes
5933  *      0 - successful
5934  *      otherwise - failed to retrieve controller attributes
5935  **/
5936 static int
lpfc_sli4_get_ctl_attr(struct lpfc_hba * phba)5937 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5938 {
5939 	LPFC_MBOXQ_t *mboxq;
5940 	struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5941 	struct lpfc_controller_attribute *cntl_attr;
5942 	void *virtaddr = NULL;
5943 	uint32_t alloclen, reqlen;
5944 	uint32_t shdr_status, shdr_add_status;
5945 	union lpfc_sli4_cfg_shdr *shdr;
5946 	int rc;
5947 
5948 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5949 	if (!mboxq)
5950 		return -ENOMEM;
5951 
5952 	/* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5953 	reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5954 	alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5955 			LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5956 			LPFC_SLI4_MBX_NEMBED);
5957 
5958 	if (alloclen < reqlen) {
5959 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5960 				"3084 Allocated DMA memory size (%d) is "
5961 				"less than the requested DMA memory size "
5962 				"(%d)\n", alloclen, reqlen);
5963 		rc = -ENOMEM;
5964 		goto out_free_mboxq;
5965 	}
5966 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5967 	virtaddr = mboxq->sge_array->addr[0];
5968 	mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5969 	shdr = &mbx_cntl_attr->cfg_shdr;
5970 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5971 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5972 	if (shdr_status || shdr_add_status || rc) {
5973 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5974 				"3085 Mailbox x%x (x%x/x%x) failed, "
5975 				"rc:x%x, status:x%x, add_status:x%x\n",
5976 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5977 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5978 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5979 				rc, shdr_status, shdr_add_status);
5980 		rc = -ENXIO;
5981 		goto out_free_mboxq;
5982 	}
5983 
5984 	cntl_attr = &mbx_cntl_attr->cntl_attr;
5985 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5986 	phba->sli4_hba.lnk_info.lnk_tp =
5987 		bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5988 	phba->sli4_hba.lnk_info.lnk_no =
5989 		bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5990 	phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
5991 	phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
5992 
5993 	memcpy(phba->BIOSVersion, cntl_attr->bios_ver_str,
5994 		sizeof(phba->BIOSVersion));
5995 	phba->BIOSVersion[sizeof(phba->BIOSVersion) - 1] = '\0';
5996 
5997 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5998 			"3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
5999 			"flash_id: x%02x, asic_rev: x%02x\n",
6000 			phba->sli4_hba.lnk_info.lnk_tp,
6001 			phba->sli4_hba.lnk_info.lnk_no,
6002 			phba->BIOSVersion, phba->sli4_hba.flash_id,
6003 			phba->sli4_hba.asic_rev);
6004 out_free_mboxq:
6005 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6006 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6007 	else
6008 		mempool_free(mboxq, phba->mbox_mem_pool);
6009 	return rc;
6010 }
6011 
6012 /**
6013  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
6014  * @phba: pointer to lpfc hba data structure.
6015  *
6016  * This routine retrieves SLI4 device physical port name this PCI function
6017  * is attached to.
6018  *
6019  * Return codes
6020  *      0 - successful
6021  *      otherwise - failed to retrieve physical port name
6022  **/
6023 static int
lpfc_sli4_retrieve_pport_name(struct lpfc_hba * phba)6024 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
6025 {
6026 	LPFC_MBOXQ_t *mboxq;
6027 	struct lpfc_mbx_get_port_name *get_port_name;
6028 	uint32_t shdr_status, shdr_add_status;
6029 	union lpfc_sli4_cfg_shdr *shdr;
6030 	char cport_name = 0;
6031 	int rc;
6032 
6033 	/* We assume nothing at this point */
6034 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6035 	phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
6036 
6037 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6038 	if (!mboxq)
6039 		return -ENOMEM;
6040 	/* obtain link type and link number via READ_CONFIG */
6041 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6042 	lpfc_sli4_read_config(phba);
6043 
6044 	if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG)
6045 		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
6046 
6047 	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
6048 		goto retrieve_ppname;
6049 
6050 	/* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
6051 	rc = lpfc_sli4_get_ctl_attr(phba);
6052 	if (rc)
6053 		goto out_free_mboxq;
6054 
6055 retrieve_ppname:
6056 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6057 		LPFC_MBOX_OPCODE_GET_PORT_NAME,
6058 		sizeof(struct lpfc_mbx_get_port_name) -
6059 		sizeof(struct lpfc_sli4_cfg_mhdr),
6060 		LPFC_SLI4_MBX_EMBED);
6061 	get_port_name = &mboxq->u.mqe.un.get_port_name;
6062 	shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
6063 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
6064 	bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
6065 		phba->sli4_hba.lnk_info.lnk_tp);
6066 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6067 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6068 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6069 	if (shdr_status || shdr_add_status || rc) {
6070 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6071 				"3087 Mailbox x%x (x%x/x%x) failed: "
6072 				"rc:x%x, status:x%x, add_status:x%x\n",
6073 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6074 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6075 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6076 				rc, shdr_status, shdr_add_status);
6077 		rc = -ENXIO;
6078 		goto out_free_mboxq;
6079 	}
6080 	switch (phba->sli4_hba.lnk_info.lnk_no) {
6081 	case LPFC_LINK_NUMBER_0:
6082 		cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6083 				&get_port_name->u.response);
6084 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6085 		break;
6086 	case LPFC_LINK_NUMBER_1:
6087 		cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6088 				&get_port_name->u.response);
6089 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6090 		break;
6091 	case LPFC_LINK_NUMBER_2:
6092 		cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6093 				&get_port_name->u.response);
6094 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6095 		break;
6096 	case LPFC_LINK_NUMBER_3:
6097 		cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6098 				&get_port_name->u.response);
6099 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6100 		break;
6101 	default:
6102 		break;
6103 	}
6104 
6105 	if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6106 		phba->Port[0] = cport_name;
6107 		phba->Port[1] = '\0';
6108 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6109 				"3091 SLI get port name: %s\n", phba->Port);
6110 	}
6111 
6112 out_free_mboxq:
6113 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6114 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6115 	else
6116 		mempool_free(mboxq, phba->mbox_mem_pool);
6117 	return rc;
6118 }
6119 
6120 /**
6121  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6122  * @phba: pointer to lpfc hba data structure.
6123  *
6124  * This routine is called to explicitly arm the SLI4 device's completion and
6125  * event queues
6126  **/
6127 static void
lpfc_sli4_arm_cqeq_intr(struct lpfc_hba * phba)6128 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6129 {
6130 	int qidx;
6131 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
6132 	struct lpfc_sli4_hdw_queue *qp;
6133 	struct lpfc_queue *eq;
6134 
6135 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6136 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
6137 	if (sli4_hba->nvmels_cq)
6138 		sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6139 					   LPFC_QUEUE_REARM);
6140 
6141 	if (sli4_hba->hdwq) {
6142 		/* Loop thru all Hardware Queues */
6143 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
6144 			qp = &sli4_hba->hdwq[qidx];
6145 			/* ARM the corresponding CQ */
6146 			sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
6147 						LPFC_QUEUE_REARM);
6148 		}
6149 
6150 		/* Loop thru all IRQ vectors */
6151 		for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6152 			eq = sli4_hba->hba_eq_hdl[qidx].eq;
6153 			/* ARM the corresponding EQ */
6154 			sli4_hba->sli4_write_eq_db(phba, eq,
6155 						   0, LPFC_QUEUE_REARM);
6156 		}
6157 	}
6158 
6159 	if (phba->nvmet_support) {
6160 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
6161 			sli4_hba->sli4_write_cq_db(phba,
6162 				sli4_hba->nvmet_cqset[qidx], 0,
6163 				LPFC_QUEUE_REARM);
6164 		}
6165 	}
6166 }
6167 
6168 /**
6169  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6170  * @phba: Pointer to HBA context object.
6171  * @type: The resource extent type.
6172  * @extnt_count: buffer to hold port available extent count.
6173  * @extnt_size: buffer to hold element count per extent.
6174  *
6175  * This function calls the port and retrievs the number of available
6176  * extents and their size for a particular extent type.
6177  *
6178  * Returns: 0 if successful.  Nonzero otherwise.
6179  **/
6180 int
lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_count,uint16_t * extnt_size)6181 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6182 			       uint16_t *extnt_count, uint16_t *extnt_size)
6183 {
6184 	int rc = 0;
6185 	uint32_t length;
6186 	uint32_t mbox_tmo;
6187 	struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6188 	LPFC_MBOXQ_t *mbox;
6189 
6190 	*extnt_count = 0;
6191 	*extnt_size = 0;
6192 
6193 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6194 	if (!mbox)
6195 		return -ENOMEM;
6196 
6197 	/* Find out how many extents are available for this resource type */
6198 	length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6199 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6200 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6201 			 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6202 			 length, LPFC_SLI4_MBX_EMBED);
6203 
6204 	/* Send an extents count of 0 - the GET doesn't use it. */
6205 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6206 					LPFC_SLI4_MBX_EMBED);
6207 	if (unlikely(rc)) {
6208 		rc = -EIO;
6209 		goto err_exit;
6210 	}
6211 
6212 	if (!phba->sli4_hba.intr_enable)
6213 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6214 	else {
6215 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6216 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6217 	}
6218 	if (unlikely(rc)) {
6219 		rc = -EIO;
6220 		goto err_exit;
6221 	}
6222 
6223 	rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6224 	if (bf_get(lpfc_mbox_hdr_status,
6225 		   &rsrc_info->header.cfg_shdr.response)) {
6226 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6227 				"2930 Failed to get resource extents "
6228 				"Status 0x%x Add'l Status 0x%x\n",
6229 				bf_get(lpfc_mbox_hdr_status,
6230 				       &rsrc_info->header.cfg_shdr.response),
6231 				bf_get(lpfc_mbox_hdr_add_status,
6232 				       &rsrc_info->header.cfg_shdr.response));
6233 		rc = -EIO;
6234 		goto err_exit;
6235 	}
6236 
6237 	*extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6238 			      &rsrc_info->u.rsp);
6239 	*extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6240 			     &rsrc_info->u.rsp);
6241 
6242 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6243 			"3162 Retrieved extents type-%d from port: count:%d, "
6244 			"size:%d\n", type, *extnt_count, *extnt_size);
6245 
6246 err_exit:
6247 	mempool_free(mbox, phba->mbox_mem_pool);
6248 	return rc;
6249 }
6250 
6251 /**
6252  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6253  * @phba: Pointer to HBA context object.
6254  * @type: The extent type to check.
6255  *
6256  * This function reads the current available extents from the port and checks
6257  * if the extent count or extent size has changed since the last access.
6258  * Callers use this routine post port reset to understand if there is a
6259  * extent reprovisioning requirement.
6260  *
6261  * Returns:
6262  *   -Error: error indicates problem.
6263  *   1: Extent count or size has changed.
6264  *   0: No changes.
6265  **/
6266 static int
lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type)6267 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6268 {
6269 	uint16_t curr_ext_cnt, rsrc_ext_cnt;
6270 	uint16_t size_diff, rsrc_ext_size;
6271 	int rc = 0;
6272 	struct lpfc_rsrc_blks *rsrc_entry;
6273 	struct list_head *rsrc_blk_list = NULL;
6274 
6275 	size_diff = 0;
6276 	curr_ext_cnt = 0;
6277 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6278 					    &rsrc_ext_cnt,
6279 					    &rsrc_ext_size);
6280 	if (unlikely(rc))
6281 		return -EIO;
6282 
6283 	switch (type) {
6284 	case LPFC_RSC_TYPE_FCOE_RPI:
6285 		rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6286 		break;
6287 	case LPFC_RSC_TYPE_FCOE_VPI:
6288 		rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6289 		break;
6290 	case LPFC_RSC_TYPE_FCOE_XRI:
6291 		rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6292 		break;
6293 	case LPFC_RSC_TYPE_FCOE_VFI:
6294 		rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6295 		break;
6296 	default:
6297 		break;
6298 	}
6299 
6300 	list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6301 		curr_ext_cnt++;
6302 		if (rsrc_entry->rsrc_size != rsrc_ext_size)
6303 			size_diff++;
6304 	}
6305 
6306 	if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6307 		rc = 1;
6308 
6309 	return rc;
6310 }
6311 
6312 /**
6313  * lpfc_sli4_cfg_post_extnts -
6314  * @phba: Pointer to HBA context object.
6315  * @extnt_cnt: number of available extents.
6316  * @type: the extent type (rpi, xri, vfi, vpi).
6317  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6318  * @mbox: pointer to the caller's allocated mailbox structure.
6319  *
6320  * This function executes the extents allocation request.  It also
6321  * takes care of the amount of memory needed to allocate or get the
6322  * allocated extents. It is the caller's responsibility to evaluate
6323  * the response.
6324  *
6325  * Returns:
6326  *   -Error:  Error value describes the condition found.
6327  *   0: if successful
6328  **/
6329 static int
lpfc_sli4_cfg_post_extnts(struct lpfc_hba * phba,uint16_t extnt_cnt,uint16_t type,bool * emb,LPFC_MBOXQ_t * mbox)6330 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6331 			  uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6332 {
6333 	int rc = 0;
6334 	uint32_t req_len;
6335 	uint32_t emb_len;
6336 	uint32_t alloc_len, mbox_tmo;
6337 
6338 	/* Calculate the total requested length of the dma memory */
6339 	req_len = extnt_cnt * sizeof(uint16_t);
6340 
6341 	/*
6342 	 * Calculate the size of an embedded mailbox.  The uint32_t
6343 	 * accounts for extents-specific word.
6344 	 */
6345 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6346 		sizeof(uint32_t);
6347 
6348 	/*
6349 	 * Presume the allocation and response will fit into an embedded
6350 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6351 	 */
6352 	*emb = LPFC_SLI4_MBX_EMBED;
6353 	if (req_len > emb_len) {
6354 		req_len = extnt_cnt * sizeof(uint16_t) +
6355 			sizeof(union lpfc_sli4_cfg_shdr) +
6356 			sizeof(uint32_t);
6357 		*emb = LPFC_SLI4_MBX_NEMBED;
6358 	}
6359 
6360 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6361 				     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6362 				     req_len, *emb);
6363 	if (alloc_len < req_len) {
6364 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6365 			"2982 Allocated DMA memory size (x%x) is "
6366 			"less than the requested DMA memory "
6367 			"size (x%x)\n", alloc_len, req_len);
6368 		return -ENOMEM;
6369 	}
6370 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6371 	if (unlikely(rc))
6372 		return -EIO;
6373 
6374 	if (!phba->sli4_hba.intr_enable)
6375 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6376 	else {
6377 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6378 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6379 	}
6380 
6381 	if (unlikely(rc))
6382 		rc = -EIO;
6383 	return rc;
6384 }
6385 
6386 /**
6387  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6388  * @phba: Pointer to HBA context object.
6389  * @type:  The resource extent type to allocate.
6390  *
6391  * This function allocates the number of elements for the specified
6392  * resource type.
6393  **/
6394 static int
lpfc_sli4_alloc_extent(struct lpfc_hba * phba,uint16_t type)6395 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6396 {
6397 	bool emb = false;
6398 	uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6399 	uint16_t rsrc_id, rsrc_start, j, k;
6400 	uint16_t *ids;
6401 	int i, rc;
6402 	unsigned long longs;
6403 	unsigned long *bmask;
6404 	struct lpfc_rsrc_blks *rsrc_blks;
6405 	LPFC_MBOXQ_t *mbox;
6406 	uint32_t length;
6407 	struct lpfc_id_range *id_array = NULL;
6408 	void *virtaddr = NULL;
6409 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6410 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6411 	struct list_head *ext_blk_list;
6412 
6413 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6414 					    &rsrc_cnt,
6415 					    &rsrc_size);
6416 	if (unlikely(rc))
6417 		return -EIO;
6418 
6419 	if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6420 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6421 			"3009 No available Resource Extents "
6422 			"for resource type 0x%x: Count: 0x%x, "
6423 			"Size 0x%x\n", type, rsrc_cnt,
6424 			rsrc_size);
6425 		return -ENOMEM;
6426 	}
6427 
6428 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6429 			"2903 Post resource extents type-0x%x: "
6430 			"count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6431 
6432 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6433 	if (!mbox)
6434 		return -ENOMEM;
6435 
6436 	rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6437 	if (unlikely(rc)) {
6438 		rc = -EIO;
6439 		goto err_exit;
6440 	}
6441 
6442 	/*
6443 	 * Figure out where the response is located.  Then get local pointers
6444 	 * to the response data.  The port does not guarantee to respond to
6445 	 * all extents counts request so update the local variable with the
6446 	 * allocated count from the port.
6447 	 */
6448 	if (emb == LPFC_SLI4_MBX_EMBED) {
6449 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6450 		id_array = &rsrc_ext->u.rsp.id[0];
6451 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6452 	} else {
6453 		virtaddr = mbox->sge_array->addr[0];
6454 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6455 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6456 		id_array = &n_rsrc->id;
6457 	}
6458 
6459 	longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6460 	rsrc_id_cnt = rsrc_cnt * rsrc_size;
6461 
6462 	/*
6463 	 * Based on the resource size and count, correct the base and max
6464 	 * resource values.
6465 	 */
6466 	length = sizeof(struct lpfc_rsrc_blks);
6467 	switch (type) {
6468 	case LPFC_RSC_TYPE_FCOE_RPI:
6469 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
6470 						   sizeof(unsigned long),
6471 						   GFP_KERNEL);
6472 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6473 			rc = -ENOMEM;
6474 			goto err_exit;
6475 		}
6476 		phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6477 						 sizeof(uint16_t),
6478 						 GFP_KERNEL);
6479 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
6480 			kfree(phba->sli4_hba.rpi_bmask);
6481 			rc = -ENOMEM;
6482 			goto err_exit;
6483 		}
6484 
6485 		/*
6486 		 * The next_rpi was initialized with the maximum available
6487 		 * count but the port may allocate a smaller number.  Catch
6488 		 * that case and update the next_rpi.
6489 		 */
6490 		phba->sli4_hba.next_rpi = rsrc_id_cnt;
6491 
6492 		/* Initialize local ptrs for common extent processing later. */
6493 		bmask = phba->sli4_hba.rpi_bmask;
6494 		ids = phba->sli4_hba.rpi_ids;
6495 		ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6496 		break;
6497 	case LPFC_RSC_TYPE_FCOE_VPI:
6498 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6499 					  GFP_KERNEL);
6500 		if (unlikely(!phba->vpi_bmask)) {
6501 			rc = -ENOMEM;
6502 			goto err_exit;
6503 		}
6504 		phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6505 					 GFP_KERNEL);
6506 		if (unlikely(!phba->vpi_ids)) {
6507 			kfree(phba->vpi_bmask);
6508 			rc = -ENOMEM;
6509 			goto err_exit;
6510 		}
6511 
6512 		/* Initialize local ptrs for common extent processing later. */
6513 		bmask = phba->vpi_bmask;
6514 		ids = phba->vpi_ids;
6515 		ext_blk_list = &phba->lpfc_vpi_blk_list;
6516 		break;
6517 	case LPFC_RSC_TYPE_FCOE_XRI:
6518 		phba->sli4_hba.xri_bmask = kcalloc(longs,
6519 						   sizeof(unsigned long),
6520 						   GFP_KERNEL);
6521 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
6522 			rc = -ENOMEM;
6523 			goto err_exit;
6524 		}
6525 		phba->sli4_hba.max_cfg_param.xri_used = 0;
6526 		phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6527 						 sizeof(uint16_t),
6528 						 GFP_KERNEL);
6529 		if (unlikely(!phba->sli4_hba.xri_ids)) {
6530 			kfree(phba->sli4_hba.xri_bmask);
6531 			rc = -ENOMEM;
6532 			goto err_exit;
6533 		}
6534 
6535 		/* Initialize local ptrs for common extent processing later. */
6536 		bmask = phba->sli4_hba.xri_bmask;
6537 		ids = phba->sli4_hba.xri_ids;
6538 		ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6539 		break;
6540 	case LPFC_RSC_TYPE_FCOE_VFI:
6541 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
6542 						   sizeof(unsigned long),
6543 						   GFP_KERNEL);
6544 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6545 			rc = -ENOMEM;
6546 			goto err_exit;
6547 		}
6548 		phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6549 						 sizeof(uint16_t),
6550 						 GFP_KERNEL);
6551 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
6552 			kfree(phba->sli4_hba.vfi_bmask);
6553 			rc = -ENOMEM;
6554 			goto err_exit;
6555 		}
6556 
6557 		/* Initialize local ptrs for common extent processing later. */
6558 		bmask = phba->sli4_hba.vfi_bmask;
6559 		ids = phba->sli4_hba.vfi_ids;
6560 		ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6561 		break;
6562 	default:
6563 		/* Unsupported Opcode.  Fail call. */
6564 		id_array = NULL;
6565 		bmask = NULL;
6566 		ids = NULL;
6567 		ext_blk_list = NULL;
6568 		goto err_exit;
6569 	}
6570 
6571 	/*
6572 	 * Complete initializing the extent configuration with the
6573 	 * allocated ids assigned to this function.  The bitmask serves
6574 	 * as an index into the array and manages the available ids.  The
6575 	 * array just stores the ids communicated to the port via the wqes.
6576 	 */
6577 	for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6578 		if ((i % 2) == 0)
6579 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6580 					 &id_array[k]);
6581 		else
6582 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6583 					 &id_array[k]);
6584 
6585 		rsrc_blks = kzalloc(length, GFP_KERNEL);
6586 		if (unlikely(!rsrc_blks)) {
6587 			rc = -ENOMEM;
6588 			kfree(bmask);
6589 			kfree(ids);
6590 			goto err_exit;
6591 		}
6592 		rsrc_blks->rsrc_start = rsrc_id;
6593 		rsrc_blks->rsrc_size = rsrc_size;
6594 		list_add_tail(&rsrc_blks->list, ext_blk_list);
6595 		rsrc_start = rsrc_id;
6596 		if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6597 			phba->sli4_hba.io_xri_start = rsrc_start +
6598 				lpfc_sli4_get_iocb_cnt(phba);
6599 		}
6600 
6601 		while (rsrc_id < (rsrc_start + rsrc_size)) {
6602 			ids[j] = rsrc_id;
6603 			rsrc_id++;
6604 			j++;
6605 		}
6606 		/* Entire word processed.  Get next word.*/
6607 		if ((i % 2) == 1)
6608 			k++;
6609 	}
6610  err_exit:
6611 	lpfc_sli4_mbox_cmd_free(phba, mbox);
6612 	return rc;
6613 }
6614 
6615 
6616 
6617 /**
6618  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6619  * @phba: Pointer to HBA context object.
6620  * @type: the extent's type.
6621  *
6622  * This function deallocates all extents of a particular resource type.
6623  * SLI4 does not allow for deallocating a particular extent range.  It
6624  * is the caller's responsibility to release all kernel memory resources.
6625  **/
6626 static int
lpfc_sli4_dealloc_extent(struct lpfc_hba * phba,uint16_t type)6627 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6628 {
6629 	int rc;
6630 	uint32_t length, mbox_tmo = 0;
6631 	LPFC_MBOXQ_t *mbox;
6632 	struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6633 	struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6634 
6635 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6636 	if (!mbox)
6637 		return -ENOMEM;
6638 
6639 	/*
6640 	 * This function sends an embedded mailbox because it only sends the
6641 	 * the resource type.  All extents of this type are released by the
6642 	 * port.
6643 	 */
6644 	length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6645 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6646 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6647 			 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6648 			 length, LPFC_SLI4_MBX_EMBED);
6649 
6650 	/* Send an extents count of 0 - the dealloc doesn't use it. */
6651 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6652 					LPFC_SLI4_MBX_EMBED);
6653 	if (unlikely(rc)) {
6654 		rc = -EIO;
6655 		goto out_free_mbox;
6656 	}
6657 	if (!phba->sli4_hba.intr_enable)
6658 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6659 	else {
6660 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6661 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6662 	}
6663 	if (unlikely(rc)) {
6664 		rc = -EIO;
6665 		goto out_free_mbox;
6666 	}
6667 
6668 	dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6669 	if (bf_get(lpfc_mbox_hdr_status,
6670 		   &dealloc_rsrc->header.cfg_shdr.response)) {
6671 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6672 				"2919 Failed to release resource extents "
6673 				"for type %d - Status 0x%x Add'l Status 0x%x. "
6674 				"Resource memory not released.\n",
6675 				type,
6676 				bf_get(lpfc_mbox_hdr_status,
6677 				    &dealloc_rsrc->header.cfg_shdr.response),
6678 				bf_get(lpfc_mbox_hdr_add_status,
6679 				    &dealloc_rsrc->header.cfg_shdr.response));
6680 		rc = -EIO;
6681 		goto out_free_mbox;
6682 	}
6683 
6684 	/* Release kernel memory resources for the specific type. */
6685 	switch (type) {
6686 	case LPFC_RSC_TYPE_FCOE_VPI:
6687 		kfree(phba->vpi_bmask);
6688 		kfree(phba->vpi_ids);
6689 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6690 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6691 				    &phba->lpfc_vpi_blk_list, list) {
6692 			list_del_init(&rsrc_blk->list);
6693 			kfree(rsrc_blk);
6694 		}
6695 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
6696 		break;
6697 	case LPFC_RSC_TYPE_FCOE_XRI:
6698 		kfree(phba->sli4_hba.xri_bmask);
6699 		kfree(phba->sli4_hba.xri_ids);
6700 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6701 				    &phba->sli4_hba.lpfc_xri_blk_list, list) {
6702 			list_del_init(&rsrc_blk->list);
6703 			kfree(rsrc_blk);
6704 		}
6705 		break;
6706 	case LPFC_RSC_TYPE_FCOE_VFI:
6707 		kfree(phba->sli4_hba.vfi_bmask);
6708 		kfree(phba->sli4_hba.vfi_ids);
6709 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6710 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6711 				    &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6712 			list_del_init(&rsrc_blk->list);
6713 			kfree(rsrc_blk);
6714 		}
6715 		break;
6716 	case LPFC_RSC_TYPE_FCOE_RPI:
6717 		/* RPI bitmask and physical id array are cleaned up earlier. */
6718 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6719 				    &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6720 			list_del_init(&rsrc_blk->list);
6721 			kfree(rsrc_blk);
6722 		}
6723 		break;
6724 	default:
6725 		break;
6726 	}
6727 
6728 	bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6729 
6730  out_free_mbox:
6731 	mempool_free(mbox, phba->mbox_mem_pool);
6732 	return rc;
6733 }
6734 
6735 static void
lpfc_set_features(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox,uint32_t feature)6736 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6737 		  uint32_t feature)
6738 {
6739 	uint32_t len;
6740 	u32 sig_freq = 0;
6741 
6742 	len = sizeof(struct lpfc_mbx_set_feature) -
6743 		sizeof(struct lpfc_sli4_cfg_mhdr);
6744 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6745 			 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6746 			 LPFC_SLI4_MBX_EMBED);
6747 
6748 	switch (feature) {
6749 	case LPFC_SET_UE_RECOVERY:
6750 		bf_set(lpfc_mbx_set_feature_UER,
6751 		       &mbox->u.mqe.un.set_feature, 1);
6752 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6753 		mbox->u.mqe.un.set_feature.param_len = 8;
6754 		break;
6755 	case LPFC_SET_MDS_DIAGS:
6756 		bf_set(lpfc_mbx_set_feature_mds,
6757 		       &mbox->u.mqe.un.set_feature, 1);
6758 		bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6759 		       &mbox->u.mqe.un.set_feature, 1);
6760 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6761 		mbox->u.mqe.un.set_feature.param_len = 8;
6762 		break;
6763 	case LPFC_SET_CGN_SIGNAL:
6764 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
6765 			sig_freq = 0;
6766 		else
6767 			sig_freq = phba->cgn_sig_freq;
6768 
6769 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6770 			bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6771 			       &mbox->u.mqe.un.set_feature, sig_freq);
6772 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6773 			       &mbox->u.mqe.un.set_feature, sig_freq);
6774 		}
6775 
6776 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6777 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6778 			       &mbox->u.mqe.un.set_feature, sig_freq);
6779 
6780 		if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6781 		    phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6782 			sig_freq = 0;
6783 		else
6784 			sig_freq = lpfc_acqe_cgn_frequency;
6785 
6786 		bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6787 		       &mbox->u.mqe.un.set_feature, sig_freq);
6788 
6789 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6790 		mbox->u.mqe.un.set_feature.param_len = 12;
6791 		break;
6792 	case LPFC_SET_DUAL_DUMP:
6793 		bf_set(lpfc_mbx_set_feature_dd,
6794 		       &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6795 		bf_set(lpfc_mbx_set_feature_ddquery,
6796 		       &mbox->u.mqe.un.set_feature, 0);
6797 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6798 		mbox->u.mqe.un.set_feature.param_len = 4;
6799 		break;
6800 	case LPFC_SET_ENABLE_MI:
6801 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6802 		mbox->u.mqe.un.set_feature.param_len = 4;
6803 		bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6804 		       phba->pport->cfg_lun_queue_depth);
6805 		bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6806 		       phba->sli4_hba.pc_sli4_params.mi_ver);
6807 		break;
6808 	case LPFC_SET_LD_SIGNAL:
6809 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_LD_SIGNAL;
6810 		mbox->u.mqe.un.set_feature.param_len = 16;
6811 		bf_set(lpfc_mbx_set_feature_lds_qry,
6812 		       &mbox->u.mqe.un.set_feature, LPFC_QUERY_LDS_OP);
6813 		break;
6814 	case LPFC_SET_ENABLE_CMF:
6815 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6816 		mbox->u.mqe.un.set_feature.param_len = 4;
6817 		bf_set(lpfc_mbx_set_feature_cmf,
6818 		       &mbox->u.mqe.un.set_feature, 1);
6819 		break;
6820 	}
6821 	return;
6822 }
6823 
6824 /**
6825  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6826  * @phba: Pointer to HBA context object.
6827  *
6828  * Disable FW logging into host memory on the adapter. To
6829  * be done before reading logs from the host memory.
6830  **/
6831 void
lpfc_ras_stop_fwlog(struct lpfc_hba * phba)6832 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6833 {
6834 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6835 
6836 	spin_lock_irq(&phba->ras_fwlog_lock);
6837 	ras_fwlog->state = INACTIVE;
6838 	spin_unlock_irq(&phba->ras_fwlog_lock);
6839 
6840 	/* Disable FW logging to host memory */
6841 	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6842 	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6843 
6844 	/* Wait 10ms for firmware to stop using DMA buffer */
6845 	usleep_range(10 * 1000, 20 * 1000);
6846 }
6847 
6848 /**
6849  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6850  * @phba: Pointer to HBA context object.
6851  *
6852  * This function is called to free memory allocated for RAS FW logging
6853  * support in the driver.
6854  **/
6855 void
lpfc_sli4_ras_dma_free(struct lpfc_hba * phba)6856 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6857 {
6858 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6859 	struct lpfc_dmabuf *dmabuf, *next;
6860 
6861 	if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6862 		list_for_each_entry_safe(dmabuf, next,
6863 				    &ras_fwlog->fwlog_buff_list,
6864 				    list) {
6865 			list_del(&dmabuf->list);
6866 			dma_free_coherent(&phba->pcidev->dev,
6867 					  LPFC_RAS_MAX_ENTRY_SIZE,
6868 					  dmabuf->virt, dmabuf->phys);
6869 			kfree(dmabuf);
6870 		}
6871 	}
6872 
6873 	if (ras_fwlog->lwpd.virt) {
6874 		dma_free_coherent(&phba->pcidev->dev,
6875 				  sizeof(uint32_t) * 2,
6876 				  ras_fwlog->lwpd.virt,
6877 				  ras_fwlog->lwpd.phys);
6878 		ras_fwlog->lwpd.virt = NULL;
6879 	}
6880 
6881 	spin_lock_irq(&phba->ras_fwlog_lock);
6882 	ras_fwlog->state = INACTIVE;
6883 	spin_unlock_irq(&phba->ras_fwlog_lock);
6884 }
6885 
6886 /**
6887  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6888  * @phba: Pointer to HBA context object.
6889  * @fwlog_buff_count: Count of buffers to be created.
6890  *
6891  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6892  * to update FW log is posted to the adapter.
6893  * Buffer count is calculated based on module param ras_fwlog_buffsize
6894  * Size of each buffer posted to FW is 64K.
6895  **/
6896 
6897 static int
lpfc_sli4_ras_dma_alloc(struct lpfc_hba * phba,uint32_t fwlog_buff_count)6898 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6899 			uint32_t fwlog_buff_count)
6900 {
6901 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6902 	struct lpfc_dmabuf *dmabuf;
6903 	int rc = 0, i = 0;
6904 
6905 	/* Initialize List */
6906 	INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6907 
6908 	/* Allocate memory for the LWPD */
6909 	ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6910 					    sizeof(uint32_t) * 2,
6911 					    &ras_fwlog->lwpd.phys,
6912 					    GFP_KERNEL);
6913 	if (!ras_fwlog->lwpd.virt) {
6914 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6915 				"6185 LWPD Memory Alloc Failed\n");
6916 
6917 		return -ENOMEM;
6918 	}
6919 
6920 	ras_fwlog->fw_buffcount = fwlog_buff_count;
6921 	for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6922 		dmabuf = kzalloc_obj(struct lpfc_dmabuf);
6923 		if (!dmabuf) {
6924 			rc = -ENOMEM;
6925 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6926 					"6186 Memory Alloc failed FW logging");
6927 			goto free_mem;
6928 		}
6929 
6930 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6931 						  LPFC_RAS_MAX_ENTRY_SIZE,
6932 						  &dmabuf->phys, GFP_KERNEL);
6933 		if (!dmabuf->virt) {
6934 			kfree(dmabuf);
6935 			rc = -ENOMEM;
6936 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6937 					"6187 DMA Alloc Failed FW logging");
6938 			goto free_mem;
6939 		}
6940 		dmabuf->buffer_tag = i;
6941 		list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6942 	}
6943 
6944 free_mem:
6945 	if (rc)
6946 		lpfc_sli4_ras_dma_free(phba);
6947 
6948 	return rc;
6949 }
6950 
6951 /**
6952  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6953  * @phba: pointer to lpfc hba data structure.
6954  * @pmb: pointer to the driver internal queue element for mailbox command.
6955  *
6956  * Completion handler for driver's RAS MBX command to the device.
6957  **/
6958 static void
lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)6959 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6960 {
6961 	MAILBOX_t *mb;
6962 	union lpfc_sli4_cfg_shdr *shdr;
6963 	uint32_t shdr_status, shdr_add_status;
6964 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6965 
6966 	mb = &pmb->u.mb;
6967 
6968 	shdr = (union lpfc_sli4_cfg_shdr *)
6969 		&pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6970 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6971 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6972 
6973 	if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6974 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6975 				"6188 FW LOG mailbox "
6976 				"completed with status x%x add_status x%x,"
6977 				" mbx status x%x\n",
6978 				shdr_status, shdr_add_status, mb->mbxStatus);
6979 
6980 		ras_fwlog->ras_hwsupport = false;
6981 		goto disable_ras;
6982 	}
6983 
6984 	spin_lock_irq(&phba->ras_fwlog_lock);
6985 	ras_fwlog->state = ACTIVE;
6986 	spin_unlock_irq(&phba->ras_fwlog_lock);
6987 	mempool_free(pmb, phba->mbox_mem_pool);
6988 
6989 	return;
6990 
6991 disable_ras:
6992 	/* Free RAS DMA memory */
6993 	lpfc_sli4_ras_dma_free(phba);
6994 	mempool_free(pmb, phba->mbox_mem_pool);
6995 }
6996 
6997 /**
6998  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6999  * @phba: pointer to lpfc hba data structure.
7000  * @fwlog_level: Logging verbosity level.
7001  * @fwlog_enable: Enable/Disable logging.
7002  *
7003  * Initialize memory and post mailbox command to enable FW logging in host
7004  * memory.
7005  **/
7006 int
lpfc_sli4_ras_fwlog_init(struct lpfc_hba * phba,uint32_t fwlog_level,uint32_t fwlog_enable)7007 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
7008 			 uint32_t fwlog_level,
7009 			 uint32_t fwlog_enable)
7010 {
7011 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
7012 	struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
7013 	struct lpfc_dmabuf *dmabuf;
7014 	LPFC_MBOXQ_t *mbox;
7015 	uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
7016 	int rc = 0;
7017 
7018 	spin_lock_irq(&phba->ras_fwlog_lock);
7019 	ras_fwlog->state = INACTIVE;
7020 	spin_unlock_irq(&phba->ras_fwlog_lock);
7021 
7022 	fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
7023 			  phba->cfg_ras_fwlog_buffsize);
7024 	fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
7025 
7026 	/*
7027 	 * If re-enabling FW logging support use earlier allocated
7028 	 * DMA buffers while posting MBX command.
7029 	 **/
7030 	if (!ras_fwlog->lwpd.virt) {
7031 		rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
7032 		if (rc) {
7033 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7034 					"6189 FW Log Memory Allocation Failed");
7035 			return rc;
7036 		}
7037 	}
7038 
7039 	/* Setup Mailbox command */
7040 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7041 	if (!mbox) {
7042 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7043 				"6190 RAS MBX Alloc Failed");
7044 		rc = -ENOMEM;
7045 		goto mem_free;
7046 	}
7047 
7048 	ras_fwlog->fw_loglevel = fwlog_level;
7049 	len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
7050 		sizeof(struct lpfc_sli4_cfg_mhdr));
7051 
7052 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
7053 			 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
7054 			 len, LPFC_SLI4_MBX_EMBED);
7055 
7056 	mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
7057 	bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
7058 	       fwlog_enable);
7059 	bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
7060 	       ras_fwlog->fw_loglevel);
7061 	bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
7062 	       ras_fwlog->fw_buffcount);
7063 	bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
7064 	       LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
7065 
7066 	/* Update DMA buffer address */
7067 	list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
7068 		memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
7069 
7070 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
7071 			putPaddrLow(dmabuf->phys);
7072 
7073 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
7074 			putPaddrHigh(dmabuf->phys);
7075 	}
7076 
7077 	/* Update LPWD address */
7078 	mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7079 	mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7080 
7081 	spin_lock_irq(&phba->ras_fwlog_lock);
7082 	ras_fwlog->state = REG_INPROGRESS;
7083 	spin_unlock_irq(&phba->ras_fwlog_lock);
7084 	mbox->vport = phba->pport;
7085 	mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7086 
7087 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7088 
7089 	if (rc == MBX_NOT_FINISHED) {
7090 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7091 				"6191 FW-Log Mailbox failed. "
7092 				"status %d mbxStatus : x%x", rc,
7093 				bf_get(lpfc_mqe_status, &mbox->u.mqe));
7094 		mempool_free(mbox, phba->mbox_mem_pool);
7095 		rc = -EIO;
7096 		goto mem_free;
7097 	} else
7098 		rc = 0;
7099 mem_free:
7100 	if (rc)
7101 		lpfc_sli4_ras_dma_free(phba);
7102 
7103 	return rc;
7104 }
7105 
7106 /**
7107  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7108  * @phba: Pointer to HBA context object.
7109  *
7110  * Check if RAS is supported on the adapter and initialize it.
7111  **/
7112 void
lpfc_sli4_ras_setup(struct lpfc_hba * phba)7113 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7114 {
7115 	/* Check RAS FW Log needs to be enabled or not */
7116 	if (lpfc_check_fwlog_support(phba))
7117 		return;
7118 
7119 	lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7120 				 LPFC_RAS_ENABLE_LOGGING);
7121 }
7122 
7123 /**
7124  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7125  * @phba: Pointer to HBA context object.
7126  *
7127  * This function allocates all SLI4 resource identifiers.
7128  **/
7129 int
lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba * phba)7130 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7131 {
7132 	int i, rc, error = 0;
7133 	uint16_t count, base;
7134 	unsigned long longs;
7135 
7136 	if (!phba->sli4_hba.rpi_hdrs_in_use)
7137 		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
7138 	if (phba->sli4_hba.extents_in_use) {
7139 		/*
7140 		 * The port supports resource extents. The XRI, VPI, VFI, RPI
7141 		 * resource extent count must be read and allocated before
7142 		 * provisioning the resource id arrays.
7143 		 */
7144 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7145 		    LPFC_IDX_RSRC_RDY) {
7146 			/*
7147 			 * Extent-based resources are set - the driver could
7148 			 * be in a port reset. Figure out if any corrective
7149 			 * actions need to be taken.
7150 			 */
7151 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7152 						 LPFC_RSC_TYPE_FCOE_VFI);
7153 			if (rc != 0)
7154 				error++;
7155 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7156 						 LPFC_RSC_TYPE_FCOE_VPI);
7157 			if (rc != 0)
7158 				error++;
7159 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7160 						 LPFC_RSC_TYPE_FCOE_XRI);
7161 			if (rc != 0)
7162 				error++;
7163 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7164 						 LPFC_RSC_TYPE_FCOE_RPI);
7165 			if (rc != 0)
7166 				error++;
7167 
7168 			/*
7169 			 * It's possible that the number of resources
7170 			 * provided to this port instance changed between
7171 			 * resets.  Detect this condition and reallocate
7172 			 * resources.  Otherwise, there is no action.
7173 			 */
7174 			if (error) {
7175 				lpfc_printf_log(phba, KERN_INFO,
7176 						LOG_MBOX | LOG_INIT,
7177 						"2931 Detected extent resource "
7178 						"change.  Reallocating all "
7179 						"extents.\n");
7180 				rc = lpfc_sli4_dealloc_extent(phba,
7181 						 LPFC_RSC_TYPE_FCOE_VFI);
7182 				rc = lpfc_sli4_dealloc_extent(phba,
7183 						 LPFC_RSC_TYPE_FCOE_VPI);
7184 				rc = lpfc_sli4_dealloc_extent(phba,
7185 						 LPFC_RSC_TYPE_FCOE_XRI);
7186 				rc = lpfc_sli4_dealloc_extent(phba,
7187 						 LPFC_RSC_TYPE_FCOE_RPI);
7188 			} else
7189 				return 0;
7190 		}
7191 
7192 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7193 		if (unlikely(rc))
7194 			goto err_exit;
7195 
7196 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7197 		if (unlikely(rc))
7198 			goto err_exit;
7199 
7200 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7201 		if (unlikely(rc))
7202 			goto err_exit;
7203 
7204 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7205 		if (unlikely(rc))
7206 			goto err_exit;
7207 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7208 		       LPFC_IDX_RSRC_RDY);
7209 		return rc;
7210 	} else {
7211 		/*
7212 		 * The port does not support resource extents.  The XRI, VPI,
7213 		 * VFI, RPI resource ids were determined from READ_CONFIG.
7214 		 * Just allocate the bitmasks and provision the resource id
7215 		 * arrays.  If a port reset is active, the resources don't
7216 		 * need any action - just exit.
7217 		 */
7218 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7219 		    LPFC_IDX_RSRC_RDY) {
7220 			lpfc_sli4_dealloc_resource_identifiers(phba);
7221 			lpfc_sli4_remove_rpis(phba);
7222 		}
7223 		/* RPIs. */
7224 		count = phba->sli4_hba.max_cfg_param.max_rpi;
7225 		if (count <= 0) {
7226 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7227 					"3279 Invalid provisioning of "
7228 					"rpi:%d\n", count);
7229 			rc = -EINVAL;
7230 			goto err_exit;
7231 		}
7232 		base = phba->sli4_hba.max_cfg_param.rpi_base;
7233 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7234 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
7235 						   sizeof(unsigned long),
7236 						   GFP_KERNEL);
7237 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7238 			rc = -ENOMEM;
7239 			goto err_exit;
7240 		}
7241 		phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
7242 						 GFP_KERNEL);
7243 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
7244 			rc = -ENOMEM;
7245 			goto free_rpi_bmask;
7246 		}
7247 
7248 		for (i = 0; i < count; i++)
7249 			phba->sli4_hba.rpi_ids[i] = base + i;
7250 
7251 		/* VPIs. */
7252 		count = phba->sli4_hba.max_cfg_param.max_vpi;
7253 		if (count <= 0) {
7254 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7255 					"3280 Invalid provisioning of "
7256 					"vpi:%d\n", count);
7257 			rc = -EINVAL;
7258 			goto free_rpi_ids;
7259 		}
7260 		base = phba->sli4_hba.max_cfg_param.vpi_base;
7261 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7262 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
7263 					  GFP_KERNEL);
7264 		if (unlikely(!phba->vpi_bmask)) {
7265 			rc = -ENOMEM;
7266 			goto free_rpi_ids;
7267 		}
7268 		phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
7269 					GFP_KERNEL);
7270 		if (unlikely(!phba->vpi_ids)) {
7271 			rc = -ENOMEM;
7272 			goto free_vpi_bmask;
7273 		}
7274 
7275 		for (i = 0; i < count; i++)
7276 			phba->vpi_ids[i] = base + i;
7277 
7278 		/* XRIs. */
7279 		count = phba->sli4_hba.max_cfg_param.max_xri;
7280 		if (count <= 0) {
7281 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7282 					"3281 Invalid provisioning of "
7283 					"xri:%d\n", count);
7284 			rc = -EINVAL;
7285 			goto free_vpi_ids;
7286 		}
7287 		base = phba->sli4_hba.max_cfg_param.xri_base;
7288 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7289 		phba->sli4_hba.xri_bmask = kcalloc(longs,
7290 						   sizeof(unsigned long),
7291 						   GFP_KERNEL);
7292 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
7293 			rc = -ENOMEM;
7294 			goto free_vpi_ids;
7295 		}
7296 		phba->sli4_hba.max_cfg_param.xri_used = 0;
7297 		phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
7298 						 GFP_KERNEL);
7299 		if (unlikely(!phba->sli4_hba.xri_ids)) {
7300 			rc = -ENOMEM;
7301 			goto free_xri_bmask;
7302 		}
7303 
7304 		for (i = 0; i < count; i++)
7305 			phba->sli4_hba.xri_ids[i] = base + i;
7306 
7307 		/* VFIs. */
7308 		count = phba->sli4_hba.max_cfg_param.max_vfi;
7309 		if (count <= 0) {
7310 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7311 					"3282 Invalid provisioning of "
7312 					"vfi:%d\n", count);
7313 			rc = -EINVAL;
7314 			goto free_xri_ids;
7315 		}
7316 		base = phba->sli4_hba.max_cfg_param.vfi_base;
7317 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7318 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
7319 						   sizeof(unsigned long),
7320 						   GFP_KERNEL);
7321 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7322 			rc = -ENOMEM;
7323 			goto free_xri_ids;
7324 		}
7325 		phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
7326 						 GFP_KERNEL);
7327 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
7328 			rc = -ENOMEM;
7329 			goto free_vfi_bmask;
7330 		}
7331 
7332 		for (i = 0; i < count; i++)
7333 			phba->sli4_hba.vfi_ids[i] = base + i;
7334 
7335 		/*
7336 		 * Mark all resources ready.  An HBA reset doesn't need
7337 		 * to reset the initialization.
7338 		 */
7339 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7340 		       LPFC_IDX_RSRC_RDY);
7341 		return 0;
7342 	}
7343 
7344  free_vfi_bmask:
7345 	kfree(phba->sli4_hba.vfi_bmask);
7346 	phba->sli4_hba.vfi_bmask = NULL;
7347  free_xri_ids:
7348 	kfree(phba->sli4_hba.xri_ids);
7349 	phba->sli4_hba.xri_ids = NULL;
7350  free_xri_bmask:
7351 	kfree(phba->sli4_hba.xri_bmask);
7352 	phba->sli4_hba.xri_bmask = NULL;
7353  free_vpi_ids:
7354 	kfree(phba->vpi_ids);
7355 	phba->vpi_ids = NULL;
7356  free_vpi_bmask:
7357 	kfree(phba->vpi_bmask);
7358 	phba->vpi_bmask = NULL;
7359  free_rpi_ids:
7360 	kfree(phba->sli4_hba.rpi_ids);
7361 	phba->sli4_hba.rpi_ids = NULL;
7362  free_rpi_bmask:
7363 	kfree(phba->sli4_hba.rpi_bmask);
7364 	phba->sli4_hba.rpi_bmask = NULL;
7365  err_exit:
7366 	return rc;
7367 }
7368 
7369 /**
7370  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7371  * @phba: Pointer to HBA context object.
7372  *
7373  * This function allocates the number of elements for the specified
7374  * resource type.
7375  **/
7376 int
lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba * phba)7377 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7378 {
7379 	if (phba->sli4_hba.extents_in_use) {
7380 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7381 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7382 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7383 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7384 	} else {
7385 		kfree(phba->vpi_bmask);
7386 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
7387 		kfree(phba->vpi_ids);
7388 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7389 		kfree(phba->sli4_hba.xri_bmask);
7390 		kfree(phba->sli4_hba.xri_ids);
7391 		kfree(phba->sli4_hba.vfi_bmask);
7392 		kfree(phba->sli4_hba.vfi_ids);
7393 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7394 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7395 	}
7396 
7397 	return 0;
7398 }
7399 
7400 /**
7401  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7402  * @phba: Pointer to HBA context object.
7403  * @type: The resource extent type.
7404  * @extnt_cnt: buffer to hold port extent count response
7405  * @extnt_size: buffer to hold port extent size response.
7406  *
7407  * This function calls the port to read the host allocated extents
7408  * for a particular type.
7409  **/
7410 int
lpfc_sli4_get_allocated_extnts(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_cnt,uint16_t * extnt_size)7411 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7412 			       uint16_t *extnt_cnt, uint16_t *extnt_size)
7413 {
7414 	bool emb;
7415 	int rc = 0;
7416 	uint16_t curr_blks = 0;
7417 	uint32_t req_len, emb_len;
7418 	uint32_t alloc_len, mbox_tmo;
7419 	struct list_head *blk_list_head;
7420 	struct lpfc_rsrc_blks *rsrc_blk;
7421 	LPFC_MBOXQ_t *mbox;
7422 	void *virtaddr = NULL;
7423 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7424 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7425 	union  lpfc_sli4_cfg_shdr *shdr;
7426 
7427 	switch (type) {
7428 	case LPFC_RSC_TYPE_FCOE_VPI:
7429 		blk_list_head = &phba->lpfc_vpi_blk_list;
7430 		break;
7431 	case LPFC_RSC_TYPE_FCOE_XRI:
7432 		blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7433 		break;
7434 	case LPFC_RSC_TYPE_FCOE_VFI:
7435 		blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7436 		break;
7437 	case LPFC_RSC_TYPE_FCOE_RPI:
7438 		blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7439 		break;
7440 	default:
7441 		return -EIO;
7442 	}
7443 
7444 	/* Count the number of extents currently allocatd for this type. */
7445 	list_for_each_entry(rsrc_blk, blk_list_head, list) {
7446 		if (curr_blks == 0) {
7447 			/*
7448 			 * The GET_ALLOCATED mailbox does not return the size,
7449 			 * just the count.  The size should be just the size
7450 			 * stored in the current allocated block and all sizes
7451 			 * for an extent type are the same so set the return
7452 			 * value now.
7453 			 */
7454 			*extnt_size = rsrc_blk->rsrc_size;
7455 		}
7456 		curr_blks++;
7457 	}
7458 
7459 	/*
7460 	 * Calculate the size of an embedded mailbox.  The uint32_t
7461 	 * accounts for extents-specific word.
7462 	 */
7463 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7464 		sizeof(uint32_t);
7465 
7466 	/*
7467 	 * Presume the allocation and response will fit into an embedded
7468 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7469 	 */
7470 	emb = LPFC_SLI4_MBX_EMBED;
7471 	req_len = emb_len;
7472 	if (req_len > emb_len) {
7473 		req_len = curr_blks * sizeof(uint16_t) +
7474 			sizeof(union lpfc_sli4_cfg_shdr) +
7475 			sizeof(uint32_t);
7476 		emb = LPFC_SLI4_MBX_NEMBED;
7477 	}
7478 
7479 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7480 	if (!mbox)
7481 		return -ENOMEM;
7482 	memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7483 
7484 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7485 				     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7486 				     req_len, emb);
7487 	if (alloc_len < req_len) {
7488 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7489 			"2983 Allocated DMA memory size (x%x) is "
7490 			"less than the requested DMA memory "
7491 			"size (x%x)\n", alloc_len, req_len);
7492 		rc = -ENOMEM;
7493 		goto err_exit;
7494 	}
7495 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7496 	if (unlikely(rc)) {
7497 		rc = -EIO;
7498 		goto err_exit;
7499 	}
7500 
7501 	if (!phba->sli4_hba.intr_enable)
7502 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7503 	else {
7504 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7505 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7506 	}
7507 
7508 	if (unlikely(rc)) {
7509 		rc = -EIO;
7510 		goto err_exit;
7511 	}
7512 
7513 	/*
7514 	 * Figure out where the response is located.  Then get local pointers
7515 	 * to the response data.  The port does not guarantee to respond to
7516 	 * all extents counts request so update the local variable with the
7517 	 * allocated count from the port.
7518 	 */
7519 	if (emb == LPFC_SLI4_MBX_EMBED) {
7520 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7521 		shdr = &rsrc_ext->header.cfg_shdr;
7522 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7523 	} else {
7524 		virtaddr = mbox->sge_array->addr[0];
7525 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7526 		shdr = &n_rsrc->cfg_shdr;
7527 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7528 	}
7529 
7530 	if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7531 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7532 			"2984 Failed to read allocated resources "
7533 			"for type %d - Status 0x%x Add'l Status 0x%x.\n",
7534 			type,
7535 			bf_get(lpfc_mbox_hdr_status, &shdr->response),
7536 			bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7537 		rc = -EIO;
7538 		goto err_exit;
7539 	}
7540  err_exit:
7541 	lpfc_sli4_mbox_cmd_free(phba, mbox);
7542 	return rc;
7543 }
7544 
7545 /**
7546  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7547  * @phba: pointer to lpfc hba data structure.
7548  * @sgl_list: linked link of sgl buffers to post
7549  * @cnt: number of linked list buffers
7550  *
7551  * This routine walks the list of buffers that have been allocated and
7552  * repost them to the port by using SGL block post. This is needed after a
7553  * pci_function_reset/warm_start or start. It attempts to construct blocks
7554  * of buffer sgls which contains contiguous xris and uses the non-embedded
7555  * SGL block post mailbox commands to post them to the port. For single
7556  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7557  * mailbox command for posting.
7558  *
7559  * Returns: 0 = success, non-zero failure.
7560  **/
7561 static int
lpfc_sli4_repost_sgl_list(struct lpfc_hba * phba,struct list_head * sgl_list,int cnt)7562 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7563 			  struct list_head *sgl_list, int cnt)
7564 {
7565 	struct lpfc_sglq *sglq_entry = NULL;
7566 	struct lpfc_sglq *sglq_entry_next = NULL;
7567 	struct lpfc_sglq *sglq_entry_first = NULL;
7568 	int status = 0, total_cnt;
7569 	int post_cnt = 0, num_posted = 0, block_cnt = 0;
7570 	int last_xritag = NO_XRI;
7571 	LIST_HEAD(prep_sgl_list);
7572 	LIST_HEAD(blck_sgl_list);
7573 	LIST_HEAD(allc_sgl_list);
7574 	LIST_HEAD(post_sgl_list);
7575 	LIST_HEAD(free_sgl_list);
7576 
7577 	spin_lock_irq(&phba->hbalock);
7578 	spin_lock(&phba->sli4_hba.sgl_list_lock);
7579 	list_splice_init(sgl_list, &allc_sgl_list);
7580 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
7581 	spin_unlock_irq(&phba->hbalock);
7582 
7583 	total_cnt = cnt;
7584 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7585 				 &allc_sgl_list, list) {
7586 		list_del_init(&sglq_entry->list);
7587 		block_cnt++;
7588 		if ((last_xritag != NO_XRI) &&
7589 		    (sglq_entry->sli4_xritag != last_xritag + 1)) {
7590 			/* a hole in xri block, form a sgl posting block */
7591 			list_splice_init(&prep_sgl_list, &blck_sgl_list);
7592 			post_cnt = block_cnt - 1;
7593 			/* prepare list for next posting block */
7594 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7595 			block_cnt = 1;
7596 		} else {
7597 			/* prepare list for next posting block */
7598 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7599 			/* enough sgls for non-embed sgl mbox command */
7600 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7601 				list_splice_init(&prep_sgl_list,
7602 						 &blck_sgl_list);
7603 				post_cnt = block_cnt;
7604 				block_cnt = 0;
7605 			}
7606 		}
7607 		num_posted++;
7608 
7609 		/* keep track of last sgl's xritag */
7610 		last_xritag = sglq_entry->sli4_xritag;
7611 
7612 		/* end of repost sgl list condition for buffers */
7613 		if (num_posted == total_cnt) {
7614 			if (post_cnt == 0) {
7615 				list_splice_init(&prep_sgl_list,
7616 						 &blck_sgl_list);
7617 				post_cnt = block_cnt;
7618 			} else if (block_cnt == 1) {
7619 				status = lpfc_sli4_post_sgl(phba,
7620 						sglq_entry->phys, 0,
7621 						sglq_entry->sli4_xritag);
7622 				if (!status) {
7623 					/* successful, put sgl to posted list */
7624 					list_add_tail(&sglq_entry->list,
7625 						      &post_sgl_list);
7626 				} else {
7627 					/* Failure, put sgl to free list */
7628 					lpfc_printf_log(phba, KERN_WARNING,
7629 						LOG_SLI,
7630 						"3159 Failed to post "
7631 						"sgl, xritag:x%x\n",
7632 						sglq_entry->sli4_xritag);
7633 					list_add_tail(&sglq_entry->list,
7634 						      &free_sgl_list);
7635 					total_cnt--;
7636 				}
7637 			}
7638 		}
7639 
7640 		/* continue until a nembed page worth of sgls */
7641 		if (post_cnt == 0)
7642 			continue;
7643 
7644 		/* post the buffer list sgls as a block */
7645 		status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7646 						 post_cnt);
7647 
7648 		if (!status) {
7649 			/* success, put sgl list to posted sgl list */
7650 			list_splice_init(&blck_sgl_list, &post_sgl_list);
7651 		} else {
7652 			/* Failure, put sgl list to free sgl list */
7653 			sglq_entry_first = list_first_entry(&blck_sgl_list,
7654 							    struct lpfc_sglq,
7655 							    list);
7656 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7657 					"3160 Failed to post sgl-list, "
7658 					"xritag:x%x-x%x\n",
7659 					sglq_entry_first->sli4_xritag,
7660 					(sglq_entry_first->sli4_xritag +
7661 					 post_cnt - 1));
7662 			list_splice_init(&blck_sgl_list, &free_sgl_list);
7663 			total_cnt -= post_cnt;
7664 		}
7665 
7666 		/* don't reset xirtag due to hole in xri block */
7667 		if (block_cnt == 0)
7668 			last_xritag = NO_XRI;
7669 
7670 		/* reset sgl post count for next round of posting */
7671 		post_cnt = 0;
7672 	}
7673 
7674 	/* free the sgls failed to post */
7675 	lpfc_free_sgl_list(phba, &free_sgl_list);
7676 
7677 	/* push sgls posted to the available list */
7678 	if (!list_empty(&post_sgl_list)) {
7679 		spin_lock_irq(&phba->hbalock);
7680 		spin_lock(&phba->sli4_hba.sgl_list_lock);
7681 		list_splice_init(&post_sgl_list, sgl_list);
7682 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
7683 		spin_unlock_irq(&phba->hbalock);
7684 	} else {
7685 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7686 				"3161 Failure to post sgl to port,status %x "
7687 				"blkcnt %d totalcnt %d postcnt %d\n",
7688 				status, block_cnt, total_cnt, post_cnt);
7689 		return -EIO;
7690 	}
7691 
7692 	/* return the number of XRIs actually posted */
7693 	return total_cnt;
7694 }
7695 
7696 /**
7697  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7698  * @phba: pointer to lpfc hba data structure.
7699  *
7700  * This routine walks the list of nvme buffers that have been allocated and
7701  * repost them to the port by using SGL block post. This is needed after a
7702  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7703  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7704  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7705  *
7706  * Returns: 0 = success, non-zero failure.
7707  **/
7708 static int
lpfc_sli4_repost_io_sgl_list(struct lpfc_hba * phba)7709 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7710 {
7711 	LIST_HEAD(post_nblist);
7712 	int num_posted, rc = 0;
7713 
7714 	/* get all NVME buffers need to repost to a local list */
7715 	lpfc_io_buf_flush(phba, &post_nblist);
7716 
7717 	/* post the list of nvme buffer sgls to port if available */
7718 	if (!list_empty(&post_nblist)) {
7719 		num_posted = lpfc_sli4_post_io_sgl_list(
7720 			phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7721 		/* failed to post any nvme buffer, return error */
7722 		if (num_posted == 0)
7723 			rc = -EIO;
7724 	}
7725 	return rc;
7726 }
7727 
7728 static void
lpfc_set_host_data(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)7729 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7730 {
7731 	uint32_t len;
7732 
7733 	len = sizeof(struct lpfc_mbx_set_host_data) -
7734 		sizeof(struct lpfc_sli4_cfg_mhdr);
7735 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7736 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7737 			 LPFC_SLI4_MBX_EMBED);
7738 
7739 	mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7740 	mbox->u.mqe.un.set_host_data.param_len =
7741 					LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7742 	snprintf(mbox->u.mqe.un.set_host_data.un.data,
7743 		 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7744 		 "Linux %s v"LPFC_DRIVER_VERSION,
7745 		 test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? "FCoE" : "FC");
7746 }
7747 
7748 int
lpfc_post_rq_buffer(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,int count,int idx)7749 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7750 		    struct lpfc_queue *drq, int count, int idx)
7751 {
7752 	int rc, i;
7753 	struct lpfc_rqe hrqe;
7754 	struct lpfc_rqe drqe;
7755 	struct lpfc_rqb *rqbp;
7756 	unsigned long flags;
7757 	struct rqb_dmabuf *rqb_buffer;
7758 	LIST_HEAD(rqb_buf_list);
7759 
7760 	rqbp = hrq->rqbp;
7761 	for (i = 0; i < count; i++) {
7762 		spin_lock_irqsave(&phba->hbalock, flags);
7763 		/* IF RQ is already full, don't bother */
7764 		if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7765 			spin_unlock_irqrestore(&phba->hbalock, flags);
7766 			break;
7767 		}
7768 		spin_unlock_irqrestore(&phba->hbalock, flags);
7769 
7770 		rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7771 		if (!rqb_buffer)
7772 			break;
7773 		rqb_buffer->hrq = hrq;
7774 		rqb_buffer->drq = drq;
7775 		rqb_buffer->idx = idx;
7776 		list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7777 	}
7778 
7779 	spin_lock_irqsave(&phba->hbalock, flags);
7780 	while (!list_empty(&rqb_buf_list)) {
7781 		list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7782 				 hbuf.list);
7783 
7784 		hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7785 		hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7786 		drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7787 		drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7788 		rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7789 		if (rc < 0) {
7790 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7791 					"6421 Cannot post to HRQ %d: %x %x %x "
7792 					"DRQ %x %x\n",
7793 					hrq->queue_id,
7794 					hrq->host_index,
7795 					hrq->hba_index,
7796 					hrq->entry_count,
7797 					drq->host_index,
7798 					drq->hba_index);
7799 			rqbp->rqb_free_buffer(phba, rqb_buffer);
7800 		} else {
7801 			list_add_tail(&rqb_buffer->hbuf.list,
7802 				      &rqbp->rqb_buffer_list);
7803 			rqbp->buffer_count++;
7804 		}
7805 	}
7806 	spin_unlock_irqrestore(&phba->hbalock, flags);
7807 	return 1;
7808 }
7809 
7810 static void
lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7811 lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7812 {
7813 	union lpfc_sli4_cfg_shdr *shdr;
7814 	u32 shdr_status, shdr_add_status;
7815 
7816 	shdr = (union lpfc_sli4_cfg_shdr *)
7817 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7818 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7819 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7820 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7821 		lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT | LOG_MBOX,
7822 				"4622 SET_FEATURE (x%x) mbox failed, "
7823 				"status x%x add_status x%x, mbx status x%x\n",
7824 				LPFC_SET_LD_SIGNAL, shdr_status,
7825 				shdr_add_status, pmb->u.mb.mbxStatus);
7826 		phba->degrade_activate_threshold = 0;
7827 		phba->degrade_deactivate_threshold = 0;
7828 		phba->fec_degrade_interval = 0;
7829 		goto out;
7830 	}
7831 
7832 	phba->degrade_activate_threshold = pmb->u.mqe.un.set_feature.word7;
7833 	phba->degrade_deactivate_threshold = pmb->u.mqe.un.set_feature.word8;
7834 	phba->fec_degrade_interval = pmb->u.mqe.un.set_feature.word10;
7835 
7836 	lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT,
7837 			"4624 Success: da x%x dd x%x interval x%x\n",
7838 			phba->degrade_activate_threshold,
7839 			phba->degrade_deactivate_threshold,
7840 			phba->fec_degrade_interval);
7841 out:
7842 	mempool_free(pmb, phba->mbox_mem_pool);
7843 }
7844 
7845 int
lpfc_read_lds_params(struct lpfc_hba * phba)7846 lpfc_read_lds_params(struct lpfc_hba *phba)
7847 {
7848 	LPFC_MBOXQ_t *mboxq;
7849 	int rc;
7850 
7851 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7852 	if (!mboxq)
7853 		return -ENOMEM;
7854 
7855 	lpfc_set_features(phba, mboxq, LPFC_SET_LD_SIGNAL);
7856 	mboxq->vport = phba->pport;
7857 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_lds_params;
7858 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7859 	if (rc == MBX_NOT_FINISHED) {
7860 		mempool_free(mboxq, phba->mbox_mem_pool);
7861 		return -EIO;
7862 	}
7863 	return 0;
7864 }
7865 
7866 static void
lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7867 lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7868 {
7869 	struct lpfc_vport *vport = pmb->vport;
7870 	union lpfc_sli4_cfg_shdr *shdr;
7871 	u32 shdr_status, shdr_add_status;
7872 	u32 sig, acqe;
7873 
7874 	/* Two outcomes. (1) Set featurs was successul and EDC negotiation
7875 	 * is done. (2) Mailbox failed and send FPIN support only.
7876 	 */
7877 	shdr = (union lpfc_sli4_cfg_shdr *)
7878 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7879 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7880 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7881 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7882 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7883 				"2516 CGN SET_FEATURE mbox failed with "
7884 				"status x%x add_status x%x, mbx status x%x "
7885 				"Reset Congestion to FPINs only\n",
7886 				shdr_status, shdr_add_status,
7887 				pmb->u.mb.mbxStatus);
7888 		/* If there is a mbox error, move on to RDF */
7889 		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7890 		phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7891 		goto out;
7892 	}
7893 
7894 	/* Zero out Congestion Signal ACQE counter */
7895 	phba->cgn_acqe_cnt = 0;
7896 
7897 	acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7898 		      &pmb->u.mqe.un.set_feature);
7899 	sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7900 		     &pmb->u.mqe.un.set_feature);
7901 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7902 			"4620 SET_FEATURES Success: Freq: %ds %dms "
7903 			" Reg: x%x x%x\n", acqe, sig,
7904 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7905 out:
7906 	mempool_free(pmb, phba->mbox_mem_pool);
7907 
7908 	/* Register for FPIN events from the fabric now that the
7909 	 * EDC common_set_features has completed.
7910 	 */
7911 	lpfc_issue_els_rdf(vport, 0);
7912 }
7913 
7914 int
lpfc_config_cgn_signal(struct lpfc_hba * phba)7915 lpfc_config_cgn_signal(struct lpfc_hba *phba)
7916 {
7917 	LPFC_MBOXQ_t *mboxq;
7918 	u32 rc;
7919 
7920 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7921 	if (!mboxq)
7922 		goto out_rdf;
7923 
7924 	lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7925 	mboxq->vport = phba->pport;
7926 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7927 
7928 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7929 			"4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7930 			"Reg: x%x x%x\n",
7931 			phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7932 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7933 
7934 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7935 	if (rc == MBX_NOT_FINISHED)
7936 		goto out;
7937 	return 0;
7938 
7939 out:
7940 	mempool_free(mboxq, phba->mbox_mem_pool);
7941 out_rdf:
7942 	/* If there is a mbox error, move on to RDF */
7943 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7944 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7945 	lpfc_issue_els_rdf(phba->pport, 0);
7946 	return -EIO;
7947 }
7948 
7949 /**
7950  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7951  * @phba: pointer to lpfc hba data structure.
7952  *
7953  * This routine initializes the per-eq idle_stat to dynamically dictate
7954  * polling decisions.
7955  *
7956  * Return codes:
7957  *   None
7958  **/
lpfc_init_idle_stat_hb(struct lpfc_hba * phba)7959 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7960 {
7961 	int i;
7962 	struct lpfc_sli4_hdw_queue *hdwq;
7963 	struct lpfc_queue *eq;
7964 	struct lpfc_idle_stat *idle_stat;
7965 	u64 wall;
7966 
7967 	for_each_present_cpu(i) {
7968 		hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7969 		eq = hdwq->hba_eq;
7970 
7971 		/* Skip if we've already handled this eq's primary CPU */
7972 		if (eq->chann != i)
7973 			continue;
7974 
7975 		idle_stat = &phba->sli4_hba.idle_stat[i];
7976 
7977 		idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7978 		idle_stat->prev_wall = wall;
7979 
7980 		if (phba->nvmet_support ||
7981 		    phba->cmf_active_mode != LPFC_CFG_OFF ||
7982 		    phba->intr_type != MSIX)
7983 			eq->poll_mode = LPFC_QUEUE_WORK;
7984 		else
7985 			eq->poll_mode = LPFC_THREADED_IRQ;
7986 	}
7987 
7988 	if (!phba->nvmet_support && phba->intr_type == MSIX)
7989 		schedule_delayed_work(&phba->idle_stat_delay_work,
7990 				      msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7991 }
7992 
lpfc_sli4_dip(struct lpfc_hba * phba)7993 static void lpfc_sli4_dip(struct lpfc_hba *phba)
7994 {
7995 	uint32_t if_type;
7996 
7997 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7998 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7999 	    if_type == LPFC_SLI_INTF_IF_TYPE_6) {
8000 		struct lpfc_register reg_data;
8001 
8002 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8003 			       &reg_data.word0))
8004 			return;
8005 
8006 		if (bf_get(lpfc_sliport_status_dip, &reg_data))
8007 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8008 					"2904 Firmware Dump Image Present"
8009 					" on Adapter");
8010 	}
8011 }
8012 
8013 /**
8014  * lpfc_rx_monitor_create_ring - Initialize ring buffer for rx_monitor
8015  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8016  * @entries: Number of rx_info_entry objects to allocate in ring
8017  *
8018  * Return:
8019  * 0 - Success
8020  * ENOMEM - Failure to kmalloc
8021  **/
lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor * rx_monitor,u32 entries)8022 int lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor *rx_monitor,
8023 				u32 entries)
8024 {
8025 	rx_monitor->ring = kmalloc_objs(struct rx_info_entry, entries);
8026 	if (!rx_monitor->ring)
8027 		return -ENOMEM;
8028 
8029 	rx_monitor->head_idx = 0;
8030 	rx_monitor->tail_idx = 0;
8031 	spin_lock_init(&rx_monitor->lock);
8032 	rx_monitor->entries = entries;
8033 
8034 	return 0;
8035 }
8036 
8037 /**
8038  * lpfc_rx_monitor_destroy_ring - Free ring buffer for rx_monitor
8039  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8040  *
8041  * Called after cancellation of cmf_timer.
8042  **/
lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor * rx_monitor)8043 void lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor *rx_monitor)
8044 {
8045 	kfree(rx_monitor->ring);
8046 	rx_monitor->ring = NULL;
8047 	rx_monitor->entries = 0;
8048 	rx_monitor->head_idx = 0;
8049 	rx_monitor->tail_idx = 0;
8050 }
8051 
8052 /**
8053  * lpfc_rx_monitor_record - Insert an entry into rx_monitor's ring
8054  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8055  * @entry: Pointer to rx_info_entry
8056  *
8057  * Used to insert an rx_info_entry into rx_monitor's ring.  Note that this is a
8058  * deep copy of rx_info_entry not a shallow copy of the rx_info_entry ptr.
8059  *
8060  * This is called from lpfc_cmf_timer, which is in timer/softirq context.
8061  *
8062  * In cases of old data overflow, we do a best effort of FIFO order.
8063  **/
lpfc_rx_monitor_record(struct lpfc_rx_info_monitor * rx_monitor,struct rx_info_entry * entry)8064 void lpfc_rx_monitor_record(struct lpfc_rx_info_monitor *rx_monitor,
8065 			    struct rx_info_entry *entry)
8066 {
8067 	struct rx_info_entry *ring = rx_monitor->ring;
8068 	u32 *head_idx = &rx_monitor->head_idx;
8069 	u32 *tail_idx = &rx_monitor->tail_idx;
8070 	spinlock_t *ring_lock = &rx_monitor->lock;
8071 	u32 ring_size = rx_monitor->entries;
8072 
8073 	spin_lock(ring_lock);
8074 	memcpy(&ring[*tail_idx], entry, sizeof(*entry));
8075 	*tail_idx = (*tail_idx + 1) % ring_size;
8076 
8077 	/* Best effort of FIFO saved data */
8078 	if (*tail_idx == *head_idx)
8079 		*head_idx = (*head_idx + 1) % ring_size;
8080 
8081 	spin_unlock(ring_lock);
8082 }
8083 
8084 /**
8085  * lpfc_rx_monitor_report - Read out rx_monitor's ring
8086  * @phba: Pointer to lpfc_hba object
8087  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8088  * @buf: Pointer to char buffer that will contain rx monitor info data
8089  * @buf_len: Length buf including null char
8090  * @max_read_entries: Maximum number of entries to read out of ring
8091  *
8092  * Used to dump/read what's in rx_monitor's ring buffer.
8093  *
8094  * If buf is NULL || buf_len == 0, then it is implied that we want to log the
8095  * information to kmsg instead of filling out buf.
8096  *
8097  * Return:
8098  * Number of entries read out of the ring
8099  **/
lpfc_rx_monitor_report(struct lpfc_hba * phba,struct lpfc_rx_info_monitor * rx_monitor,char * buf,u32 buf_len,u32 max_read_entries)8100 u32 lpfc_rx_monitor_report(struct lpfc_hba *phba,
8101 			   struct lpfc_rx_info_monitor *rx_monitor, char *buf,
8102 			   u32 buf_len, u32 max_read_entries)
8103 {
8104 	struct rx_info_entry *ring = rx_monitor->ring;
8105 	struct rx_info_entry *entry;
8106 	u32 *head_idx = &rx_monitor->head_idx;
8107 	u32 *tail_idx = &rx_monitor->tail_idx;
8108 	spinlock_t *ring_lock = &rx_monitor->lock;
8109 	u32 ring_size = rx_monitor->entries;
8110 	u32 cnt = 0;
8111 	char tmp[DBG_LOG_STR_SZ] = {0};
8112 	bool log_to_kmsg = (!buf || !buf_len) ? true : false;
8113 	struct seq_buf s;
8114 
8115 	if (!log_to_kmsg) {
8116 		/* clear the buffer to be sure */
8117 		memset(buf, 0, buf_len);
8118 
8119 		seq_buf_init(&s, buf, buf_len);
8120 		seq_buf_printf(&s, "\t%-16s%-16s%-16s%-16s%-8s%-8s%-8s%-8s%-8s%-8s%-16s\n",
8121 			       "MaxBPI", "Tot_Data_CMF",
8122 			       "Tot_Data_Cmd", "Tot_Data_Cmpl",
8123 			       "Lat(us)", "Avg_IO", "Max_IO", "Bsy",
8124 			       "IO_cnt", "Info", "BWutil(ms)");
8125 	}
8126 
8127 	/* Needs to be _irq because record is called from timer interrupt
8128 	 * context
8129 	 */
8130 	spin_lock_irq(ring_lock);
8131 	while (*head_idx != *tail_idx) {
8132 		entry = &ring[*head_idx];
8133 
8134 		/* Read out this entry's data. */
8135 		if (!log_to_kmsg) {
8136 			/*
8137 			 * Drop a record whole if it does not fit, without
8138 			 * consuming its ring entry.
8139 			 */
8140 			scnprintf(tmp, sizeof(tmp),
8141 				  "%03d:\t%-16llu%-16llu%-16llu%-16llu%-8llu%-8llu%-8llu%-8u%-8u%-8u%u(%u)\n",
8142 				  *head_idx,
8143 				  entry->max_bytes_per_interval,
8144 				  entry->cmf_bytes,
8145 				  entry->total_bytes,
8146 				  entry->rcv_bytes,
8147 				  entry->avg_io_latency,
8148 				  entry->avg_io_size,
8149 				  entry->max_read_cnt,
8150 				  entry->cmf_busy, entry->io_cnt,
8151 				  entry->cmf_info,
8152 				  entry->timer_utilization,
8153 				  entry->timer_interval);
8154 
8155 			if (seq_buf_puts(&s, tmp) < 0)
8156 				break;
8157 		} else {
8158 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
8159 					"4410 %02u: MBPI %llu Xmit %llu "
8160 					"Cmpl %llu Lat %llu ASz %llu Info %02u "
8161 					"BWUtil %u Int %u slot %u\n",
8162 					cnt, entry->max_bytes_per_interval,
8163 					entry->total_bytes, entry->rcv_bytes,
8164 					entry->avg_io_latency,
8165 					entry->avg_io_size, entry->cmf_info,
8166 					entry->timer_utilization,
8167 					entry->timer_interval, *head_idx);
8168 		}
8169 
8170 		*head_idx = (*head_idx + 1) % ring_size;
8171 
8172 		/* Don't feed more than max_read_entries */
8173 		cnt++;
8174 		if (cnt >= max_read_entries)
8175 			break;
8176 	}
8177 	spin_unlock_irq(ring_lock);
8178 
8179 	return cnt;
8180 }
8181 
8182 /**
8183  * lpfc_cmf_setup - Initialize idle_stat tracking
8184  * @phba: Pointer to HBA context object.
8185  *
8186  * This is called from HBA setup during driver load or when the HBA
8187  * comes online. this does all the initialization to support CMF and MI.
8188  **/
8189 static int
lpfc_cmf_setup(struct lpfc_hba * phba)8190 lpfc_cmf_setup(struct lpfc_hba *phba)
8191 {
8192 	LPFC_MBOXQ_t *mboxq;
8193 	struct lpfc_dmabuf *mp;
8194 	struct lpfc_pc_sli4_params *sli4_params;
8195 	int rc, cmf, mi_ver;
8196 
8197 	rc = lpfc_sli4_refresh_params(phba);
8198 	if (unlikely(rc))
8199 		return rc;
8200 
8201 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8202 	if (!mboxq)
8203 		return -ENOMEM;
8204 
8205 	sli4_params = &phba->sli4_hba.pc_sli4_params;
8206 
8207 	/* Always try to enable MI feature if we can */
8208 	if (sli4_params->mi_ver) {
8209 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
8210 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8211 		mi_ver = bf_get(lpfc_mbx_set_feature_mi,
8212 				 &mboxq->u.mqe.un.set_feature);
8213 
8214 		if (rc == MBX_SUCCESS) {
8215 			if (mi_ver) {
8216 				lpfc_printf_log(phba,
8217 						KERN_WARNING, LOG_CGN_MGMT,
8218 						"6215 MI is enabled\n");
8219 				sli4_params->mi_ver = mi_ver;
8220 			} else {
8221 				lpfc_printf_log(phba,
8222 						KERN_WARNING, LOG_CGN_MGMT,
8223 						"6338 MI is disabled\n");
8224 				sli4_params->mi_ver = 0;
8225 			}
8226 		} else {
8227 			/* mi_ver is already set from GET_SLI4_PARAMETERS */
8228 			lpfc_printf_log(phba, KERN_INFO,
8229 					LOG_CGN_MGMT | LOG_INIT,
8230 					"6245 Enable MI Mailbox x%x (x%x/x%x) "
8231 					"failed, rc:x%x mi:x%x\n",
8232 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8233 					lpfc_sli_config_mbox_subsys_get
8234 						(phba, mboxq),
8235 					lpfc_sli_config_mbox_opcode_get
8236 						(phba, mboxq),
8237 					rc, sli4_params->mi_ver);
8238 		}
8239 	} else {
8240 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8241 				"6217 MI is disabled\n");
8242 	}
8243 
8244 	/* Ensure FDMI is enabled for MI if enable_mi is set */
8245 	if (sli4_params->mi_ver)
8246 		phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
8247 
8248 	/* Always try to enable CMF feature if we can */
8249 	if (sli4_params->cmf) {
8250 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
8251 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8252 		cmf = bf_get(lpfc_mbx_set_feature_cmf,
8253 			     &mboxq->u.mqe.un.set_feature);
8254 		if (rc == MBX_SUCCESS && cmf) {
8255 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8256 					"6218 CMF is enabled: mode %d\n",
8257 					phba->cmf_active_mode);
8258 		} else {
8259 			lpfc_printf_log(phba, KERN_WARNING,
8260 					LOG_CGN_MGMT | LOG_INIT,
8261 					"6219 Enable CMF Mailbox x%x (x%x/x%x) "
8262 					"failed, rc:x%x dd:x%x\n",
8263 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8264 					lpfc_sli_config_mbox_subsys_get
8265 						(phba, mboxq),
8266 					lpfc_sli_config_mbox_opcode_get
8267 						(phba, mboxq),
8268 					rc, cmf);
8269 			sli4_params->cmf = 0;
8270 			phba->cmf_active_mode = LPFC_CFG_OFF;
8271 			goto no_cmf;
8272 		}
8273 
8274 		/* Allocate Congestion Information Buffer */
8275 		if (!phba->cgn_i) {
8276 			mp = kmalloc_obj(*mp);
8277 			if (mp)
8278 				mp->virt = dma_alloc_coherent
8279 						(&phba->pcidev->dev,
8280 						sizeof(struct lpfc_cgn_info),
8281 						&mp->phys, GFP_KERNEL);
8282 			if (!mp || !mp->virt) {
8283 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8284 						"2640 Failed to alloc memory "
8285 						"for Congestion Info\n");
8286 				kfree(mp);
8287 				sli4_params->cmf = 0;
8288 				phba->cmf_active_mode = LPFC_CFG_OFF;
8289 				goto no_cmf;
8290 			}
8291 			phba->cgn_i = mp;
8292 
8293 			/* initialize congestion buffer info */
8294 			lpfc_init_congestion_buf(phba);
8295 			lpfc_init_congestion_stat(phba);
8296 
8297 			/* Zero out Congestion Signal counters */
8298 			atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
8299 			atomic64_set(&phba->cgn_acqe_stat.warn, 0);
8300 		}
8301 
8302 		rc = lpfc_sli4_cgn_params_read(phba);
8303 		if (rc < 0) {
8304 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8305 					"6242 Error reading Cgn Params (%d)\n",
8306 					rc);
8307 			/* Ensure CGN Mode is off */
8308 			sli4_params->cmf = 0;
8309 		} else if (!rc) {
8310 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8311 					"6243 CGN Event empty object.\n");
8312 			/* Ensure CGN Mode is off */
8313 			sli4_params->cmf = 0;
8314 		}
8315 	} else {
8316 no_cmf:
8317 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8318 				"6220 CMF is disabled\n");
8319 	}
8320 
8321 	/* Only register congestion buffer with firmware if BOTH
8322 	 * CMF and E2E are enabled.
8323 	 */
8324 	if (sli4_params->cmf && sli4_params->mi_ver) {
8325 		rc = lpfc_reg_congestion_buf(phba);
8326 		if (rc) {
8327 			dma_free_coherent(&phba->pcidev->dev,
8328 					  sizeof(struct lpfc_cgn_info),
8329 					  phba->cgn_i->virt, phba->cgn_i->phys);
8330 			kfree(phba->cgn_i);
8331 			phba->cgn_i = NULL;
8332 			/* Ensure CGN Mode is off */
8333 			phba->cmf_active_mode = LPFC_CFG_OFF;
8334 			sli4_params->cmf = 0;
8335 			return 0;
8336 		}
8337 	}
8338 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8339 			"6470 Setup MI version %d CMF %d mode %d\n",
8340 			sli4_params->mi_ver, sli4_params->cmf,
8341 			phba->cmf_active_mode);
8342 
8343 	mempool_free(mboxq, phba->mbox_mem_pool);
8344 
8345 	/* Initialize atomic counters */
8346 	atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8347 	atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8348 	atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8349 	atomic_set(&phba->cgn_sync_warn_cnt, 0);
8350 	atomic_set(&phba->cgn_driver_evt_cnt, 0);
8351 	atomic_set(&phba->cgn_latency_evt_cnt, 0);
8352 	atomic64_set(&phba->cgn_latency_evt, 0);
8353 
8354 	phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
8355 
8356 	/* Allocate RX Monitor Buffer */
8357 	if (!phba->rx_monitor) {
8358 		phba->rx_monitor = kzalloc_obj(*phba->rx_monitor);
8359 
8360 		if (!phba->rx_monitor) {
8361 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8362 					"2644 Failed to alloc memory "
8363 					"for RX Monitor Buffer\n");
8364 			return -ENOMEM;
8365 		}
8366 
8367 		/* Instruct the rx_monitor object to instantiate its ring */
8368 		if (lpfc_rx_monitor_create_ring(phba->rx_monitor,
8369 						LPFC_MAX_RXMONITOR_ENTRY)) {
8370 			kfree(phba->rx_monitor);
8371 			phba->rx_monitor = NULL;
8372 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8373 					"2645 Failed to alloc memory "
8374 					"for RX Monitor's Ring\n");
8375 			return -ENOMEM;
8376 		}
8377 	}
8378 
8379 	return 0;
8380 }
8381 
8382 static int
lpfc_set_host_tm(struct lpfc_hba * phba)8383 lpfc_set_host_tm(struct lpfc_hba *phba)
8384 {
8385 	LPFC_MBOXQ_t *mboxq;
8386 	uint32_t len, rc;
8387 	struct timespec64 cur_time;
8388 	struct tm broken;
8389 	uint32_t month, day, year;
8390 	uint32_t hour, minute, second;
8391 	struct lpfc_mbx_set_host_date_time *tm;
8392 
8393 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8394 	if (!mboxq)
8395 		return -ENOMEM;
8396 
8397 	len = sizeof(struct lpfc_mbx_set_host_data) -
8398 		sizeof(struct lpfc_sli4_cfg_mhdr);
8399 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8400 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8401 			 LPFC_SLI4_MBX_EMBED);
8402 
8403 	mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8404 	mboxq->u.mqe.un.set_host_data.param_len =
8405 			sizeof(struct lpfc_mbx_set_host_date_time);
8406 	tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8407 	ktime_get_real_ts64(&cur_time);
8408 	time64_to_tm(cur_time.tv_sec, 0, &broken);
8409 	month = broken.tm_mon + 1;
8410 	day = broken.tm_mday;
8411 	year = broken.tm_year - 100;
8412 	hour = broken.tm_hour;
8413 	minute = broken.tm_min;
8414 	second = broken.tm_sec;
8415 	bf_set(lpfc_mbx_set_host_month, tm, month);
8416 	bf_set(lpfc_mbx_set_host_day, tm, day);
8417 	bf_set(lpfc_mbx_set_host_year, tm, year);
8418 	bf_set(lpfc_mbx_set_host_hour, tm, hour);
8419 	bf_set(lpfc_mbx_set_host_min, tm, minute);
8420 	bf_set(lpfc_mbx_set_host_sec, tm, second);
8421 
8422 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8423 	mempool_free(mboxq, phba->mbox_mem_pool);
8424 	return rc;
8425 }
8426 
8427 /**
8428  * lpfc_get_platform_uuid - Attempts to extract a platform uuid
8429  * @phba: pointer to lpfc hba data structure.
8430  *
8431  * This routine attempts to first read SMBIOS DMI data for the System
8432  * Information structure offset 08h called System UUID.  Else, no platform
8433  * UUID will be advertised.
8434  **/
8435 static void
lpfc_get_platform_uuid(struct lpfc_hba * phba)8436 lpfc_get_platform_uuid(struct lpfc_hba *phba)
8437 {
8438 	int rc;
8439 	const char *uuid;
8440 	char pni[17] = {0}; /* 16 characters + '\0' */
8441 	bool is_ff = true, is_00 = true;
8442 	u8 i;
8443 
8444 	/* First attempt SMBIOS DMI */
8445 	uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
8446 	if (uuid) {
8447 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8448 				"2088 SMBIOS UUID %s\n",
8449 				uuid);
8450 	} else {
8451 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8452 				"2099 Could not extract UUID\n");
8453 	}
8454 
8455 	if (uuid && uuid_is_valid(uuid)) {
8456 		/* Generate PNI from UUID format.
8457 		 *
8458 		 * 1.) Extract lower 64 bits from UUID format.
8459 		 * 2.) Set 3h for NAA Locally Assigned Name Identifier format.
8460 		 *
8461 		 * e.g. xxxxxxxx-xxxx-xxxx-yyyy-yyyyyyyyyyyy
8462 		 *
8463 		 * extract the yyyy-yyyyyyyyyyyy portion
8464 		 * final PNI   3yyyyyyyyyyyyyyy
8465 		 */
8466 		scnprintf(pni, sizeof(pni), "3%c%c%c%s",
8467 			  uuid[20], uuid[21], uuid[22], &uuid[24]);
8468 
8469 		/* Sanitize the converted PNI */
8470 		for (i = 1; i < 16 && (is_ff || is_00); i++) {
8471 			if (pni[i] != '0')
8472 				is_00 = false;
8473 			if (pni[i] != 'f' && pni[i] != 'F')
8474 				is_ff = false;
8475 		}
8476 
8477 		/* Convert from char* to unsigned long */
8478 		rc = kstrtoul(pni, 16, &phba->pni);
8479 		if (!rc && !is_ff && !is_00) {
8480 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8481 					"2100 PNI 0x%016lx\n", phba->pni);
8482 		} else {
8483 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8484 					"2101 PNI %s generation status %d\n",
8485 					pni, rc);
8486 			phba->pni = 0;
8487 		}
8488 	}
8489 }
8490 
8491 /**
8492  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
8493  * @phba: Pointer to HBA context object.
8494  *
8495  * This function is the main SLI4 device initialization PCI function. This
8496  * function is called by the HBA initialization code, HBA reset code and
8497  * HBA error attention handler code. Caller is not required to hold any
8498  * locks.
8499  **/
8500 int
lpfc_sli4_hba_setup(struct lpfc_hba * phba)8501 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8502 {
8503 	int rc, i, cnt, len, dd;
8504 	LPFC_MBOXQ_t *mboxq;
8505 	struct lpfc_mqe *mqe;
8506 	uint8_t *vpd;
8507 	uint32_t vpd_size;
8508 	uint32_t ftr_rsp = 0;
8509 	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8510 	struct lpfc_vport *vport = phba->pport;
8511 	struct lpfc_dmabuf *mp;
8512 	struct lpfc_rqb *rqbp;
8513 	u32 flg;
8514 
8515 	/* Perform a PCI function reset to start from clean */
8516 	rc = lpfc_pci_function_reset(phba);
8517 	if (unlikely(rc))
8518 		return -ENODEV;
8519 
8520 	/* Check the HBA Host Status Register for readiness */
8521 	rc = lpfc_sli4_post_status_check(phba);
8522 	if (unlikely(rc))
8523 		return -ENODEV;
8524 	else {
8525 		spin_lock_irq(&phba->hbalock);
8526 		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
8527 		flg = phba->sli.sli_flag;
8528 		spin_unlock_irq(&phba->hbalock);
8529 		/* Allow a little time after setting SLI_ACTIVE for any polled
8530 		 * MBX commands to complete via BSG.
8531 		 */
8532 		for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {
8533 			msleep(20);
8534 			spin_lock_irq(&phba->hbalock);
8535 			flg = phba->sli.sli_flag;
8536 			spin_unlock_irq(&phba->hbalock);
8537 		}
8538 	}
8539 	clear_bit(HBA_SETUP, &phba->hba_flag);
8540 
8541 	lpfc_sli4_dip(phba);
8542 
8543 	/*
8544 	 * Allocate a single mailbox container for initializing the
8545 	 * port.
8546 	 */
8547 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8548 	if (!mboxq)
8549 		return -ENOMEM;
8550 
8551 	/* Issue READ_REV to collect vpd and FW information. */
8552 	vpd_size = SLI4_PAGE_SIZE;
8553 	vpd = kzalloc(vpd_size, GFP_KERNEL);
8554 	if (!vpd) {
8555 		rc = -ENOMEM;
8556 		goto out_free_mbox;
8557 	}
8558 
8559 	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
8560 	if (unlikely(rc)) {
8561 		kfree(vpd);
8562 		goto out_free_mbox;
8563 	}
8564 
8565 	mqe = &mboxq->u.mqe;
8566 	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
8567 	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
8568 		set_bit(HBA_FCOE_MODE, &phba->hba_flag);
8569 		phba->fcp_embed_io = 0;	/* SLI4 FC support only */
8570 	} else {
8571 		clear_bit(HBA_FCOE_MODE, &phba->hba_flag);
8572 	}
8573 
8574 	/* Obtain platform UUID, only for SLI4 FC adapters */
8575 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag))
8576 		lpfc_get_platform_uuid(phba);
8577 
8578 	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8579 		LPFC_DCBX_CEE_MODE)
8580 		set_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8581 	else
8582 		clear_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8583 
8584 	clear_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
8585 
8586 	if (phba->sli_rev != LPFC_SLI_REV4) {
8587 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8588 			"0376 READ_REV Error. SLI Level %d "
8589 			"FCoE enabled %d\n",
8590 			phba->sli_rev,
8591 			test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? 1 : 0);
8592 		rc = -EIO;
8593 		kfree(vpd);
8594 		goto out_free_mbox;
8595 	}
8596 
8597 	rc = lpfc_set_host_tm(phba);
8598 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
8599 			"6468 Set host date / time: Status x%x:\n", rc);
8600 
8601 	/*
8602 	 * Continue initialization with default values even if driver failed
8603 	 * to read FCoE param config regions, only read parameters if the
8604 	 * board is FCoE
8605 	 */
8606 	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
8607 	    lpfc_sli4_read_fcoe_params(phba))
8608 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
8609 			"2570 Failed to read FCoE parameters\n");
8610 
8611 	/*
8612 	 * Retrieve sli4 device physical port name, failure of doing it
8613 	 * is considered as non-fatal.
8614 	 */
8615 	rc = lpfc_sli4_retrieve_pport_name(phba);
8616 	if (!rc)
8617 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8618 				"3080 Successful retrieving SLI4 device "
8619 				"physical port name: %s.\n", phba->Port);
8620 
8621 	rc = lpfc_sli4_get_ctl_attr(phba);
8622 	if (!rc)
8623 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8624 				"8351 Successful retrieving SLI4 device "
8625 				"CTL ATTR\n");
8626 
8627 	/*
8628 	 * Evaluate the read rev and vpd data. Populate the driver
8629 	 * state with the results. If this routine fails, the failure
8630 	 * is not fatal as the driver will use generic values.
8631 	 */
8632 	rc = lpfc_parse_vpd(phba, vpd, vpd_size);
8633 	if (unlikely(!rc))
8634 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8635 				"0377 Error %d parsing vpd. "
8636 				"Using defaults.\n", rc);
8637 	kfree(vpd);
8638 
8639 	/* Save information as VPD data */
8640 	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8641 	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
8642 
8643 	/*
8644 	 * This is because first G7 ASIC doesn't support the standard
8645 	 * 0x5a NVME cmd descriptor type/subtype
8646 	 */
8647 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8648 			LPFC_SLI_INTF_IF_TYPE_6) &&
8649 	    (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8650 	    (phba->vpd.rev.smRev == 0) &&
8651 	    (phba->cfg_nvme_embed_cmd == 1))
8652 		phba->cfg_nvme_embed_cmd = 0;
8653 
8654 	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8655 	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8656 					 &mqe->un.read_rev);
8657 	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8658 				       &mqe->un.read_rev);
8659 	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8660 					    &mqe->un.read_rev);
8661 	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8662 					   &mqe->un.read_rev);
8663 	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8664 	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8665 	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8666 	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8667 	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8668 	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8669 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8670 			"(%d):0380 READ_REV Status x%x "
8671 			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8672 			mboxq->vport ? mboxq->vport->vpi : 0,
8673 			bf_get(lpfc_mqe_status, mqe),
8674 			phba->vpd.rev.opFwName,
8675 			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8676 			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
8677 
8678 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8679 	    LPFC_SLI_INTF_IF_TYPE_0) {
8680 		lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8681 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8682 		if (rc == MBX_SUCCESS) {
8683 			set_bit(HBA_RECOVERABLE_UE, &phba->hba_flag);
8684 			/* Set 1Sec interval to detect UE */
8685 			phba->eratt_poll_interval = 1;
8686 			phba->sli4_hba.ue_to_sr = bf_get(
8687 					lpfc_mbx_set_feature_UESR,
8688 					&mboxq->u.mqe.un.set_feature);
8689 			phba->sli4_hba.ue_to_rp = bf_get(
8690 					lpfc_mbx_set_feature_UERP,
8691 					&mboxq->u.mqe.un.set_feature);
8692 		}
8693 	}
8694 
8695 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8696 		/* Enable MDS Diagnostics only if the SLI Port supports it */
8697 		lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8698 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8699 		if (rc != MBX_SUCCESS)
8700 			phba->mds_diags_support = 0;
8701 	}
8702 
8703 	/*
8704 	 * Discover the port's supported feature set and match it against the
8705 	 * hosts requests.
8706 	 */
8707 	lpfc_request_features(phba, mboxq);
8708 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8709 	if (unlikely(rc)) {
8710 		rc = -EIO;
8711 		goto out_free_mbox;
8712 	}
8713 
8714 	/* Disable VMID if app header is not supported */
8715 	if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8716 						  &mqe->un.req_ftrs))) {
8717 		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8718 		phba->cfg_vmid_app_header = 0;
8719 		lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8720 				"1242 vmid feature not supported\n");
8721 	}
8722 
8723 	/*
8724 	 * The port must support FCP initiator mode as this is the
8725 	 * only mode running in the host.
8726 	 */
8727 	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8728 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8729 				"0378 No support for fcpi mode.\n");
8730 		ftr_rsp++;
8731 	}
8732 
8733 	/*
8734 	 * If the port cannot support the host's requested features
8735 	 * then turn off the global config parameters to disable the
8736 	 * feature in the driver.  This is not a fatal error.
8737 	 */
8738 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8739 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8740 			phba->cfg_enable_bg = 0;
8741 			phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
8742 			ftr_rsp++;
8743 		}
8744 	}
8745 
8746 	if (phba->max_vpi && phba->cfg_enable_npiv &&
8747 	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8748 		ftr_rsp++;
8749 
8750 	if (ftr_rsp) {
8751 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8752 				"0379 Feature Mismatch Data: x%08x %08x "
8753 				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8754 				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8755 				phba->cfg_enable_npiv, phba->max_vpi);
8756 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8757 			phba->cfg_enable_bg = 0;
8758 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8759 			phba->cfg_enable_npiv = 0;
8760 	}
8761 
8762 	/* These SLI3 features are assumed in SLI4 */
8763 	spin_lock_irq(&phba->hbalock);
8764 	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8765 	spin_unlock_irq(&phba->hbalock);
8766 
8767 	/* Always try to enable dual dump feature if we can */
8768 	lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8769 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8770 	dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8771 	if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
8772 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8773 				"6448 Dual Dump is enabled\n");
8774 	else
8775 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8776 				"6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8777 				"rc:x%x dd:x%x\n",
8778 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8779 				lpfc_sli_config_mbox_subsys_get(
8780 					phba, mboxq),
8781 				lpfc_sli_config_mbox_opcode_get(
8782 					phba, mboxq),
8783 				rc, dd);
8784 
8785 	/*
8786 	 * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
8787 	 * calls depends on these resources to complete port setup.
8788 	 */
8789 	rc = lpfc_sli4_alloc_resource_identifiers(phba);
8790 	if (rc) {
8791 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8792 				"2920 Failed to alloc Resource IDs "
8793 				"rc = x%x\n", rc);
8794 		goto out_free_mbox;
8795 	}
8796 
8797 	lpfc_sli4_node_rpi_restore(phba);
8798 
8799 	lpfc_set_host_data(phba, mboxq);
8800 
8801 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8802 	if (rc) {
8803 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8804 				"2134 Failed to set host os driver version %x",
8805 				rc);
8806 	}
8807 
8808 	/* Read the port's service parameters. */
8809 	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8810 	if (rc) {
8811 		phba->link_state = LPFC_HBA_ERROR;
8812 		rc = -ENOMEM;
8813 		goto out_free_mbox;
8814 	}
8815 
8816 	mboxq->vport = vport;
8817 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8818 	mp = mboxq->ctx_buf;
8819 	if (rc == MBX_SUCCESS) {
8820 		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8821 		rc = 0;
8822 	}
8823 
8824 	/*
8825 	 * This memory was allocated by the lpfc_read_sparam routine but is
8826 	 * no longer needed.  It is released and ctx_buf NULLed to prevent
8827 	 * unintended pointer access as the mbox is reused.
8828 	 */
8829 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
8830 	kfree(mp);
8831 	mboxq->ctx_buf = NULL;
8832 	if (unlikely(rc)) {
8833 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8834 				"0382 READ_SPARAM command failed "
8835 				"status %d, mbxStatus x%x\n",
8836 				rc, bf_get(lpfc_mqe_status, mqe));
8837 		phba->link_state = LPFC_HBA_ERROR;
8838 		rc = -EIO;
8839 		goto out_free_mbox;
8840 	}
8841 
8842 	lpfc_update_vport_wwn(vport);
8843 
8844 	/* Update the fc_host data structures with new wwn. */
8845 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8846 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8847 
8848 	/* Create all the SLI4 queues */
8849 	rc = lpfc_sli4_queue_create(phba);
8850 	if (rc) {
8851 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8852 				"3089 Failed to allocate queues\n");
8853 		rc = -ENODEV;
8854 		goto out_free_mbox;
8855 	}
8856 	/* Set up all the queues to the device */
8857 	rc = lpfc_sli4_queue_setup(phba);
8858 	if (unlikely(rc)) {
8859 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8860 				"0381 Error %d during queue setup.\n", rc);
8861 		goto out_destroy_queue;
8862 	}
8863 	/* Initialize the driver internal SLI layer lists. */
8864 	lpfc_sli4_setup(phba);
8865 	lpfc_sli4_queue_init(phba);
8866 
8867 	/* update host els xri-sgl sizes and mappings */
8868 	rc = lpfc_sli4_els_sgl_update(phba);
8869 	if (unlikely(rc)) {
8870 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8871 				"1400 Failed to update xri-sgl size and "
8872 				"mapping: %d\n", rc);
8873 		goto out_destroy_queue;
8874 	}
8875 
8876 	/* register the els sgl pool to the port */
8877 	rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8878 				       phba->sli4_hba.els_xri_cnt);
8879 	if (unlikely(rc < 0)) {
8880 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8881 				"0582 Error %d during els sgl post "
8882 				"operation\n", rc);
8883 		rc = -ENODEV;
8884 		goto out_destroy_queue;
8885 	}
8886 	phba->sli4_hba.els_xri_cnt = rc;
8887 
8888 	if (phba->nvmet_support) {
8889 		/* update host nvmet xri-sgl sizes and mappings */
8890 		rc = lpfc_sli4_nvmet_sgl_update(phba);
8891 		if (unlikely(rc)) {
8892 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8893 					"6308 Failed to update nvmet-sgl size "
8894 					"and mapping: %d\n", rc);
8895 			goto out_destroy_queue;
8896 		}
8897 
8898 		/* register the nvmet sgl pool to the port */
8899 		rc = lpfc_sli4_repost_sgl_list(
8900 			phba,
8901 			&phba->sli4_hba.lpfc_nvmet_sgl_list,
8902 			phba->sli4_hba.nvmet_xri_cnt);
8903 		if (unlikely(rc < 0)) {
8904 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8905 					"3117 Error %d during nvmet "
8906 					"sgl post\n", rc);
8907 			rc = -ENODEV;
8908 			goto out_destroy_queue;
8909 		}
8910 		phba->sli4_hba.nvmet_xri_cnt = rc;
8911 
8912 		/* We allocate an iocbq for every receive context SGL.
8913 		 * The additional allocation is for abort and ls handling.
8914 		 */
8915 		cnt = phba->sli4_hba.nvmet_xri_cnt +
8916 			phba->sli4_hba.max_cfg_param.max_xri;
8917 	} else {
8918 		/* update host common xri-sgl sizes and mappings */
8919 		rc = lpfc_sli4_io_sgl_update(phba);
8920 		if (unlikely(rc)) {
8921 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8922 					"6082 Failed to update nvme-sgl size "
8923 					"and mapping: %d\n", rc);
8924 			goto out_destroy_queue;
8925 		}
8926 
8927 		/* register the allocated common sgl pool to the port */
8928 		rc = lpfc_sli4_repost_io_sgl_list(phba);
8929 		if (unlikely(rc)) {
8930 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8931 					"6116 Error %d during nvme sgl post "
8932 					"operation\n", rc);
8933 			/* Some NVME buffers were moved to abort nvme list */
8934 			/* A pci function reset will repost them */
8935 			rc = -ENODEV;
8936 			goto out_destroy_queue;
8937 		}
8938 		/* Each lpfc_io_buf job structure has an iocbq element.
8939 		 * This cnt provides for abort, els, ct and ls requests.
8940 		 */
8941 		cnt = phba->sli4_hba.max_cfg_param.max_xri;
8942 	}
8943 
8944 	if (!phba->sli.iocbq_lookup) {
8945 		/* Initialize and populate the iocb list per host */
8946 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8947 				"2821 initialize iocb list with %d entries\n",
8948 				cnt);
8949 		rc = lpfc_init_iocb_list(phba, cnt);
8950 		if (rc) {
8951 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8952 					"1413 Failed to init iocb list.\n");
8953 			goto out_destroy_queue;
8954 		}
8955 	}
8956 
8957 	if (phba->nvmet_support)
8958 		lpfc_nvmet_create_targetport(phba);
8959 
8960 	if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
8961 		/* Post initial buffers to all RQs created */
8962 		for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8963 			rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8964 			INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8965 			rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8966 			rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
8967 			rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
8968 			rqbp->buffer_count = 0;
8969 
8970 			lpfc_post_rq_buffer(
8971 				phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8972 				phba->sli4_hba.nvmet_mrq_data[i],
8973 				phba->cfg_nvmet_mrq_post, i);
8974 		}
8975 	}
8976 
8977 	/* Post the rpi header region to the device. */
8978 	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8979 	if (unlikely(rc)) {
8980 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8981 				"0393 Error %d during rpi post operation\n",
8982 				rc);
8983 		rc = -ENODEV;
8984 		goto out_free_iocblist;
8985 	}
8986 
8987 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
8988 		if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
8989 			/*
8990 			 * The FC Port needs to register FCFI (index 0)
8991 			 */
8992 			lpfc_reg_fcfi(phba, mboxq);
8993 			mboxq->vport = phba->pport;
8994 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8995 			if (rc != MBX_SUCCESS)
8996 				goto out_unset_queue;
8997 			rc = 0;
8998 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
8999 						&mboxq->u.mqe.un.reg_fcfi);
9000 		} else {
9001 			/* We are a NVME Target mode with MRQ > 1 */
9002 
9003 			/* First register the FCFI */
9004 			lpfc_reg_fcfi_mrq(phba, mboxq, 0);
9005 			mboxq->vport = phba->pport;
9006 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9007 			if (rc != MBX_SUCCESS)
9008 				goto out_unset_queue;
9009 			rc = 0;
9010 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
9011 						&mboxq->u.mqe.un.reg_fcfi_mrq);
9012 
9013 			/* Next register the MRQs */
9014 			lpfc_reg_fcfi_mrq(phba, mboxq, 1);
9015 			mboxq->vport = phba->pport;
9016 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9017 			if (rc != MBX_SUCCESS)
9018 				goto out_unset_queue;
9019 			rc = 0;
9020 		}
9021 		/* Check if the port is configured to be disabled */
9022 		lpfc_sli_read_link_ste(phba);
9023 	}
9024 
9025 	/* Don't post more new bufs if repost already recovered
9026 	 * the nvme sgls.
9027 	 */
9028 	if (phba->nvmet_support == 0) {
9029 		if (phba->sli4_hba.io_xri_cnt == 0) {
9030 			len = lpfc_new_io_buf(
9031 					      phba, phba->sli4_hba.io_xri_max);
9032 			if (len == 0) {
9033 				rc = -ENOMEM;
9034 				goto out_unset_queue;
9035 			}
9036 
9037 			if (phba->cfg_xri_rebalancing)
9038 				lpfc_create_multixri_pools(phba);
9039 		}
9040 	} else {
9041 		phba->cfg_xri_rebalancing = 0;
9042 	}
9043 
9044 	/* Allow asynchronous mailbox command to go through */
9045 	spin_lock_irq(&phba->hbalock);
9046 	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9047 	spin_unlock_irq(&phba->hbalock);
9048 
9049 	/* Post receive buffers to the device */
9050 	lpfc_sli4_rb_setup(phba);
9051 
9052 	/* Reset HBA FCF states after HBA reset */
9053 	phba->fcf.fcf_flag = 0;
9054 	phba->fcf.current_rec.flag = 0;
9055 
9056 	/* Start the ELS watchdog timer */
9057 	mod_timer(&vport->els_tmofunc,
9058 			jiffies + secs_to_jiffies(phba->fc_ratov * 2));
9059 
9060 	/* Start heart beat timer */
9061 	mod_timer(&phba->hb_tmofunc,
9062 		  jiffies + secs_to_jiffies(LPFC_HB_MBOX_INTERVAL));
9063 	clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
9064 	clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
9065 	phba->last_completion_time = jiffies;
9066 
9067 	/* start eq_delay heartbeat */
9068 	if (phba->cfg_auto_imax)
9069 		queue_delayed_work(phba->wq, &phba->eq_delay_work,
9070 				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
9071 
9072 	/* start per phba idle_stat_delay heartbeat */
9073 	lpfc_init_idle_stat_hb(phba);
9074 
9075 	/* Start error attention (ERATT) polling timer */
9076 	mod_timer(&phba->eratt_poll,
9077 		  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
9078 
9079 	/*
9080 	 * The port is ready, set the host's link state to LINK_DOWN
9081 	 * in preparation for link interrupts.
9082 	 */
9083 	spin_lock_irq(&phba->hbalock);
9084 	phba->link_state = LPFC_LINK_DOWN;
9085 
9086 	/* Check if physical ports are trunked */
9087 	if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
9088 		phba->trunk_link.link0.state = LPFC_LINK_DOWN;
9089 	if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
9090 		phba->trunk_link.link1.state = LPFC_LINK_DOWN;
9091 	if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
9092 		phba->trunk_link.link2.state = LPFC_LINK_DOWN;
9093 	if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
9094 		phba->trunk_link.link3.state = LPFC_LINK_DOWN;
9095 	spin_unlock_irq(&phba->hbalock);
9096 
9097 	/* Arm the CQs and then EQs on device */
9098 	lpfc_sli4_arm_cqeq_intr(phba);
9099 
9100 	/* Indicate device interrupt mode */
9101 	phba->sli4_hba.intr_enable = 1;
9102 
9103 	/* Setup CMF after HBA is initialized */
9104 	lpfc_cmf_setup(phba);
9105 
9106 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
9107 	    test_bit(LINK_DISABLED, &phba->hba_flag)) {
9108 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9109 				"3103 Adapter Link is disabled.\n");
9110 		lpfc_down_link(phba, mboxq);
9111 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9112 		if (rc != MBX_SUCCESS) {
9113 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9114 					"3104 Adapter failed to issue "
9115 					"DOWN_LINK mbox cmd, rc:x%x\n", rc);
9116 			goto out_io_buff_free;
9117 		}
9118 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
9119 		/* don't perform init_link on SLI4 FC port loopback test */
9120 		if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
9121 			rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
9122 			if (rc)
9123 				goto out_io_buff_free;
9124 		}
9125 	}
9126 	mempool_free(mboxq, phba->mbox_mem_pool);
9127 
9128 	/* Enable RAS FW log support */
9129 	lpfc_sli4_ras_setup(phba);
9130 
9131 	set_bit(HBA_SETUP, &phba->hba_flag);
9132 	return rc;
9133 
9134 out_io_buff_free:
9135 	/* Free allocated IO Buffers */
9136 	lpfc_io_free(phba);
9137 out_unset_queue:
9138 	/* Unset all the queues set up in this routine when error out */
9139 	lpfc_sli4_queue_unset(phba);
9140 out_free_iocblist:
9141 	lpfc_free_iocb_list(phba);
9142 out_destroy_queue:
9143 	lpfc_sli4_queue_destroy(phba);
9144 	lpfc_stop_hba_timers(phba);
9145 out_free_mbox:
9146 	mempool_free(mboxq, phba->mbox_mem_pool);
9147 	return rc;
9148 }
9149 
9150 /**
9151  * lpfc_mbox_timeout - Timeout call back function for mbox timer
9152  * @t: Context to fetch pointer to hba structure from.
9153  *
9154  * This is the callback function for mailbox timer. The mailbox
9155  * timer is armed when a new mailbox command is issued and the timer
9156  * is deleted when the mailbox complete. The function is called by
9157  * the kernel timer code when a mailbox does not complete within
9158  * expected time. This function wakes up the worker thread to
9159  * process the mailbox timeout and returns. All the processing is
9160  * done by the worker thread function lpfc_mbox_timeout_handler.
9161  **/
9162 void
lpfc_mbox_timeout(struct timer_list * t)9163 lpfc_mbox_timeout(struct timer_list *t)
9164 {
9165 	struct lpfc_hba  *phba = timer_container_of(phba, t, sli.mbox_tmo);
9166 	unsigned long iflag;
9167 	uint32_t tmo_posted;
9168 
9169 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
9170 	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
9171 	if (!tmo_posted)
9172 		phba->pport->work_port_events |= WORKER_MBOX_TMO;
9173 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
9174 
9175 	if (!tmo_posted)
9176 		lpfc_worker_wake_up(phba);
9177 	return;
9178 }
9179 
9180 /**
9181  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
9182  *                                    are pending
9183  * @phba: Pointer to HBA context object.
9184  *
9185  * This function checks if any mailbox completions are present on the mailbox
9186  * completion queue.
9187  **/
9188 static bool
lpfc_sli4_mbox_completions_pending(struct lpfc_hba * phba)9189 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
9190 {
9191 
9192 	uint32_t idx;
9193 	struct lpfc_queue *mcq;
9194 	struct lpfc_mcqe *mcqe;
9195 	bool pending_completions = false;
9196 	uint8_t	qe_valid;
9197 
9198 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9199 		return false;
9200 
9201 	/* Check for completions on mailbox completion queue */
9202 
9203 	mcq = phba->sli4_hba.mbx_cq;
9204 	idx = mcq->hba_index;
9205 	qe_valid = mcq->qe_valid;
9206 	while (bf_get_le32(lpfc_cqe_valid,
9207 	       (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
9208 		mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
9209 		if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
9210 		    (!bf_get_le32(lpfc_trailer_async, mcqe))) {
9211 			pending_completions = true;
9212 			break;
9213 		}
9214 		idx = (idx + 1) % mcq->entry_count;
9215 		if (mcq->hba_index == idx)
9216 			break;
9217 
9218 		/* if the index wrapped around, toggle the valid bit */
9219 		if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
9220 			qe_valid = (qe_valid) ? 0 : 1;
9221 	}
9222 	return pending_completions;
9223 
9224 }
9225 
9226 /**
9227  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
9228  *					      that were missed.
9229  * @phba: Pointer to HBA context object.
9230  *
9231  * For sli4, it is possible to miss an interrupt. As such mbox completions
9232  * maybe missed causing erroneous mailbox timeouts to occur. This function
9233  * checks to see if mbox completions are on the mailbox completion queue
9234  * and will process all the completions associated with the eq for the
9235  * mailbox completion queue.
9236  **/
9237 static bool
lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba * phba)9238 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
9239 {
9240 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
9241 	uint32_t eqidx;
9242 	struct lpfc_queue *fpeq = NULL;
9243 	struct lpfc_queue *eq;
9244 	bool mbox_pending;
9245 
9246 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9247 		return false;
9248 
9249 	/* Find the EQ associated with the mbox CQ */
9250 	if (sli4_hba->hdwq) {
9251 		for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
9252 			eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
9253 			if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
9254 				fpeq = eq;
9255 				break;
9256 			}
9257 		}
9258 	}
9259 	if (!fpeq)
9260 		return false;
9261 
9262 	/* Turn off interrupts from this EQ */
9263 
9264 	sli4_hba->sli4_eq_clr_intr(fpeq);
9265 
9266 	/* Check to see if a mbox completion is pending */
9267 
9268 	mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
9269 
9270 	/*
9271 	 * If a mbox completion is pending, process all the events on EQ
9272 	 * associated with the mbox completion queue (this could include
9273 	 * mailbox commands, async events, els commands, receive queue data
9274 	 * and fcp commands)
9275 	 */
9276 
9277 	if (mbox_pending)
9278 		/* process and rearm the EQ */
9279 		lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
9280 				     LPFC_QUEUE_WORK);
9281 	else
9282 		/* Always clear and re-arm the EQ */
9283 		sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
9284 
9285 	return mbox_pending;
9286 
9287 }
9288 
9289 /**
9290  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
9291  * @phba: Pointer to HBA context object.
9292  *
9293  * This function is called from worker thread when a mailbox command times out.
9294  * The caller is not required to hold any locks. This function will reset the
9295  * HBA and recover all the pending commands.
9296  **/
9297 void
lpfc_mbox_timeout_handler(struct lpfc_hba * phba)9298 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
9299 {
9300 	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
9301 	MAILBOX_t *mb = NULL;
9302 
9303 	struct lpfc_sli *psli = &phba->sli;
9304 
9305 	/* If the mailbox completed, process the completion */
9306 	lpfc_sli4_process_missed_mbox_completions(phba);
9307 
9308 	if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
9309 		return;
9310 
9311 	if (pmbox != NULL)
9312 		mb = &pmbox->u.mb;
9313 	/* Check the pmbox pointer first.  There is a race condition
9314 	 * between the mbox timeout handler getting executed in the
9315 	 * worklist and the mailbox actually completing. When this
9316 	 * race condition occurs, the mbox_active will be NULL.
9317 	 */
9318 	spin_lock_irq(&phba->hbalock);
9319 	if (pmbox == NULL) {
9320 		lpfc_printf_log(phba, KERN_WARNING,
9321 				LOG_MBOX | LOG_SLI,
9322 				"0353 Active Mailbox cleared - mailbox timeout "
9323 				"exiting\n");
9324 		spin_unlock_irq(&phba->hbalock);
9325 		return;
9326 	}
9327 
9328 	/* Mbox cmd <mbxCommand> timeout */
9329 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9330 			"0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
9331 			mb->mbxCommand,
9332 			phba->pport->port_state,
9333 			phba->sli.sli_flag,
9334 			phba->sli.mbox_active);
9335 	spin_unlock_irq(&phba->hbalock);
9336 
9337 	/* Setting state unknown so lpfc_sli_abort_iocb_ring
9338 	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
9339 	 * it to fail all outstanding SCSI IO.
9340 	 */
9341 	set_bit(MBX_TMO_ERR, &phba->bit_flags);
9342 	spin_lock_irq(&phba->pport->work_port_lock);
9343 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9344 	spin_unlock_irq(&phba->pport->work_port_lock);
9345 	spin_lock_irq(&phba->hbalock);
9346 	phba->link_state = LPFC_LINK_UNKNOWN;
9347 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9348 	spin_unlock_irq(&phba->hbalock);
9349 
9350 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9351 			"0345 Resetting board due to mailbox timeout\n");
9352 
9353 	/* Reset the HBA device */
9354 	lpfc_reset_hba(phba);
9355 }
9356 
9357 /**
9358  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
9359  * @phba: Pointer to HBA context object.
9360  * @pmbox: Pointer to mailbox object.
9361  * @flag: Flag indicating how the mailbox need to be processed.
9362  *
9363  * This function is called by discovery code and HBA management code
9364  * to submit a mailbox command to firmware with SLI-3 interface spec. This
9365  * function gets the hbalock to protect the data structures.
9366  * The mailbox command can be submitted in polling mode, in which case
9367  * this function will wait in a polling loop for the completion of the
9368  * mailbox.
9369  * If the mailbox is submitted in no_wait mode (not polling) the
9370  * function will submit the command and returns immediately without waiting
9371  * for the mailbox completion. The no_wait is supported only when HBA
9372  * is in SLI2/SLI3 mode - interrupts are enabled.
9373  * The SLI interface allows only one mailbox pending at a time. If the
9374  * mailbox is issued in polling mode and there is already a mailbox
9375  * pending, then the function will return an error. If the mailbox is issued
9376  * in NO_WAIT mode and there is a mailbox pending already, the function
9377  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9378  * The sli layer owns the mailbox object until the completion of mailbox
9379  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9380  * return codes the caller owns the mailbox command after the return of
9381  * the function.
9382  **/
9383 static int
lpfc_sli_issue_mbox_s3(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)9384 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9385 		       uint32_t flag)
9386 {
9387 	MAILBOX_t *mbx;
9388 	struct lpfc_sli *psli = &phba->sli;
9389 	uint32_t status, evtctr;
9390 	uint32_t ha_copy, hc_copy;
9391 	int i;
9392 	unsigned long timeout;
9393 	unsigned long drvr_flag = 0;
9394 	uint32_t word0, ldata;
9395 	void __iomem *to_slim;
9396 	int processing_queue = 0;
9397 
9398 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
9399 	if (!pmbox) {
9400 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9401 		/* processing mbox queue from intr_handler */
9402 		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9403 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9404 			return MBX_SUCCESS;
9405 		}
9406 		processing_queue = 1;
9407 		pmbox = lpfc_mbox_get(phba);
9408 		if (!pmbox) {
9409 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9410 			return MBX_SUCCESS;
9411 		}
9412 	}
9413 
9414 	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
9415 		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
9416 		if(!pmbox->vport) {
9417 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9418 			lpfc_printf_log(phba, KERN_ERR,
9419 					LOG_MBOX | LOG_VPORT,
9420 					"1806 Mbox x%x failed. No vport\n",
9421 					pmbox->u.mb.mbxCommand);
9422 			dump_stack();
9423 			goto out_not_finished;
9424 		}
9425 	}
9426 
9427 	/* If the PCI channel is in offline state, do not post mbox. */
9428 	if (unlikely(pci_channel_offline(phba->pcidev))) {
9429 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9430 		goto out_not_finished;
9431 	}
9432 
9433 	/* If HBA has a deferred error attention, fail the iocb. */
9434 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
9435 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9436 		goto out_not_finished;
9437 	}
9438 
9439 	psli = &phba->sli;
9440 
9441 	mbx = &pmbox->u.mb;
9442 	status = MBX_SUCCESS;
9443 
9444 	if (phba->link_state == LPFC_HBA_ERROR) {
9445 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9446 
9447 		/* Mbox command <mbxCommand> cannot issue */
9448 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9449 				"(%d):0311 Mailbox command x%x cannot "
9450 				"issue Data: x%x x%x\n",
9451 				pmbox->vport ? pmbox->vport->vpi : 0,
9452 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9453 		goto out_not_finished;
9454 	}
9455 
9456 	if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9457 		if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9458 			!(hc_copy & HC_MBINT_ENA)) {
9459 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9460 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9461 				"(%d):2528 Mailbox command x%x cannot "
9462 				"issue Data: x%x x%x\n",
9463 				pmbox->vport ? pmbox->vport->vpi : 0,
9464 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9465 			goto out_not_finished;
9466 		}
9467 	}
9468 
9469 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9470 		/* Polling for a mbox command when another one is already active
9471 		 * is not allowed in SLI. Also, the driver must have established
9472 		 * SLI2 mode to queue and process multiple mbox commands.
9473 		 */
9474 
9475 		if (flag & MBX_POLL) {
9476 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9477 
9478 			/* Mbox command <mbxCommand> cannot issue */
9479 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9480 					"(%d):2529 Mailbox command x%x "
9481 					"cannot issue Data: x%x x%x\n",
9482 					pmbox->vport ? pmbox->vport->vpi : 0,
9483 					pmbox->u.mb.mbxCommand,
9484 					psli->sli_flag, flag);
9485 			goto out_not_finished;
9486 		}
9487 
9488 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
9489 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9490 			/* Mbox command <mbxCommand> cannot issue */
9491 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9492 					"(%d):2530 Mailbox command x%x "
9493 					"cannot issue Data: x%x x%x\n",
9494 					pmbox->vport ? pmbox->vport->vpi : 0,
9495 					pmbox->u.mb.mbxCommand,
9496 					psli->sli_flag, flag);
9497 			goto out_not_finished;
9498 		}
9499 
9500 		/* Another mailbox command is still being processed, queue this
9501 		 * command to be processed later.
9502 		 */
9503 		lpfc_mbox_put(phba, pmbox);
9504 
9505 		/* Mbox cmd issue - BUSY */
9506 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9507 				"(%d):0308 Mbox cmd issue - BUSY Data: "
9508 				"x%x x%x x%x x%x\n",
9509 				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
9510 				mbx->mbxCommand,
9511 				phba->pport ? phba->pport->port_state : 0xff,
9512 				psli->sli_flag, flag);
9513 
9514 		psli->slistat.mbox_busy++;
9515 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9516 
9517 		if (pmbox->vport) {
9518 			lpfc_debugfs_disc_trc(pmbox->vport,
9519 				LPFC_DISC_TRC_MBOX_VPORT,
9520 				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
9521 				(uint32_t)mbx->mbxCommand,
9522 				mbx->un.varWords[0], mbx->un.varWords[1]);
9523 		}
9524 		else {
9525 			lpfc_debugfs_disc_trc(phba->pport,
9526 				LPFC_DISC_TRC_MBOX,
9527 				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
9528 				(uint32_t)mbx->mbxCommand,
9529 				mbx->un.varWords[0], mbx->un.varWords[1]);
9530 		}
9531 
9532 		return MBX_BUSY;
9533 	}
9534 
9535 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9536 
9537 	/* If we are not polling, we MUST be in SLI2 mode */
9538 	if (flag != MBX_POLL) {
9539 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
9540 		    (mbx->mbxCommand != MBX_KILL_BOARD)) {
9541 			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9542 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9543 			/* Mbox command <mbxCommand> cannot issue */
9544 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9545 					"(%d):2531 Mailbox command x%x "
9546 					"cannot issue Data: x%x x%x\n",
9547 					pmbox->vport ? pmbox->vport->vpi : 0,
9548 					pmbox->u.mb.mbxCommand,
9549 					psli->sli_flag, flag);
9550 			goto out_not_finished;
9551 		}
9552 		/* timeout active mbox command */
9553 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox));
9554 		mod_timer(&psli->mbox_tmo, jiffies + timeout);
9555 	}
9556 
9557 	/* Mailbox cmd <cmd> issue */
9558 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9559 			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
9560 			"x%x\n",
9561 			pmbox->vport ? pmbox->vport->vpi : 0,
9562 			mbx->mbxCommand,
9563 			phba->pport ? phba->pport->port_state : 0xff,
9564 			psli->sli_flag, flag);
9565 
9566 	if (mbx->mbxCommand != MBX_HEARTBEAT) {
9567 		if (pmbox->vport) {
9568 			lpfc_debugfs_disc_trc(pmbox->vport,
9569 				LPFC_DISC_TRC_MBOX_VPORT,
9570 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
9571 				(uint32_t)mbx->mbxCommand,
9572 				mbx->un.varWords[0], mbx->un.varWords[1]);
9573 		}
9574 		else {
9575 			lpfc_debugfs_disc_trc(phba->pport,
9576 				LPFC_DISC_TRC_MBOX,
9577 				"MBOX Send:       cmd:x%x mb:x%x x%x",
9578 				(uint32_t)mbx->mbxCommand,
9579 				mbx->un.varWords[0], mbx->un.varWords[1]);
9580 		}
9581 	}
9582 
9583 	psli->slistat.mbox_cmd++;
9584 	evtctr = psli->slistat.mbox_event;
9585 
9586 	/* next set own bit for the adapter and copy over command word */
9587 	mbx->mbxOwner = OWN_CHIP;
9588 
9589 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9590 		/* Populate mbox extension offset word. */
9591 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
9592 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9593 				= (uint8_t *)phba->mbox_ext
9594 				  - (uint8_t *)phba->mbox;
9595 		}
9596 
9597 		/* Copy the mailbox extension data */
9598 		if (pmbox->in_ext_byte_len && pmbox->ext_buf) {
9599 			lpfc_sli_pcimem_bcopy(pmbox->ext_buf,
9600 					      (uint8_t *)phba->mbox_ext,
9601 					      pmbox->in_ext_byte_len);
9602 		}
9603 		/* Copy command data to host SLIM area */
9604 		lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
9605 	} else {
9606 		/* Populate mbox extension offset word. */
9607 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
9608 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9609 				= MAILBOX_HBA_EXT_OFFSET;
9610 
9611 		/* Copy the mailbox extension data */
9612 		if (pmbox->in_ext_byte_len && pmbox->ext_buf)
9613 			lpfc_memcpy_to_slim(phba->MBslimaddr +
9614 				MAILBOX_HBA_EXT_OFFSET,
9615 				pmbox->ext_buf, pmbox->in_ext_byte_len);
9616 
9617 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9618 			/* copy command data into host mbox for cmpl */
9619 			lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
9620 					      MAILBOX_CMD_SIZE);
9621 
9622 		/* First copy mbox command data to HBA SLIM, skip past first
9623 		   word */
9624 		to_slim = phba->MBslimaddr + sizeof (uint32_t);
9625 		lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
9626 			    MAILBOX_CMD_SIZE - sizeof (uint32_t));
9627 
9628 		/* Next copy over first word, with mbxOwner set */
9629 		ldata = *((uint32_t *)mbx);
9630 		to_slim = phba->MBslimaddr;
9631 		writel(ldata, to_slim);
9632 		readl(to_slim); /* flush */
9633 
9634 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9635 			/* switch over to host mailbox */
9636 			psli->sli_flag |= LPFC_SLI_ACTIVE;
9637 	}
9638 
9639 	wmb();
9640 
9641 	switch (flag) {
9642 	case MBX_NOWAIT:
9643 		/* Set up reference to mailbox command */
9644 		psli->mbox_active = pmbox;
9645 		/* Interrupt board to do it */
9646 		writel(CA_MBATT, phba->CAregaddr);
9647 		readl(phba->CAregaddr); /* flush */
9648 		/* Don't wait for it to finish, just return */
9649 		break;
9650 
9651 	case MBX_POLL:
9652 		/* Set up null reference to mailbox command */
9653 		psli->mbox_active = NULL;
9654 		/* Interrupt board to do it */
9655 		writel(CA_MBATT, phba->CAregaddr);
9656 		readl(phba->CAregaddr); /* flush */
9657 
9658 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9659 			/* First read mbox status word */
9660 			word0 = *((uint32_t *)phba->mbox);
9661 			word0 = le32_to_cpu(word0);
9662 		} else {
9663 			/* First read mbox status word */
9664 			if (lpfc_readl(phba->MBslimaddr, &word0)) {
9665 				spin_unlock_irqrestore(&phba->hbalock,
9666 						       drvr_flag);
9667 				goto out_not_finished;
9668 			}
9669 		}
9670 
9671 		/* Read the HBA Host Attention Register */
9672 		if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9673 			spin_unlock_irqrestore(&phba->hbalock,
9674 						       drvr_flag);
9675 			goto out_not_finished;
9676 		}
9677 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox)) + jiffies;
9678 		i = 0;
9679 		/* Wait for command to complete */
9680 		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9681 		       (!(ha_copy & HA_MBATT) &&
9682 			(phba->link_state > LPFC_WARM_START))) {
9683 			if (time_after(jiffies, timeout)) {
9684 				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9685 				spin_unlock_irqrestore(&phba->hbalock,
9686 						       drvr_flag);
9687 				goto out_not_finished;
9688 			}
9689 
9690 			/* Check if we took a mbox interrupt while we were
9691 			   polling */
9692 			if (((word0 & OWN_CHIP) != OWN_CHIP)
9693 			    && (evtctr != psli->slistat.mbox_event))
9694 				break;
9695 
9696 			if (i++ > 10) {
9697 				spin_unlock_irqrestore(&phba->hbalock,
9698 						       drvr_flag);
9699 				msleep(1);
9700 				spin_lock_irqsave(&phba->hbalock, drvr_flag);
9701 			}
9702 
9703 			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9704 				/* First copy command data */
9705 				word0 = *((uint32_t *)phba->mbox);
9706 				word0 = le32_to_cpu(word0);
9707 				if (mbx->mbxCommand == MBX_CONFIG_PORT) {
9708 					MAILBOX_t *slimmb;
9709 					uint32_t slimword0;
9710 					/* Check real SLIM for any errors */
9711 					slimword0 = readl(phba->MBslimaddr);
9712 					slimmb = (MAILBOX_t *) & slimword0;
9713 					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9714 					    && slimmb->mbxStatus) {
9715 						psli->sli_flag &=
9716 						    ~LPFC_SLI_ACTIVE;
9717 						word0 = slimword0;
9718 					}
9719 				}
9720 			} else {
9721 				/* First copy command data */
9722 				word0 = readl(phba->MBslimaddr);
9723 			}
9724 			/* Read the HBA Host Attention Register */
9725 			if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9726 				spin_unlock_irqrestore(&phba->hbalock,
9727 						       drvr_flag);
9728 				goto out_not_finished;
9729 			}
9730 		}
9731 
9732 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9733 			/* copy results back to user */
9734 			lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9735 						MAILBOX_CMD_SIZE);
9736 			/* Copy the mailbox extension data */
9737 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9738 				lpfc_sli_pcimem_bcopy(phba->mbox_ext,
9739 						      pmbox->ext_buf,
9740 						      pmbox->out_ext_byte_len);
9741 			}
9742 		} else {
9743 			/* First copy command data */
9744 			lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
9745 						MAILBOX_CMD_SIZE);
9746 			/* Copy the mailbox extension data */
9747 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9748 				lpfc_memcpy_from_slim(
9749 					pmbox->ext_buf,
9750 					phba->MBslimaddr +
9751 					MAILBOX_HBA_EXT_OFFSET,
9752 					pmbox->out_ext_byte_len);
9753 			}
9754 		}
9755 
9756 		writel(HA_MBATT, phba->HAregaddr);
9757 		readl(phba->HAregaddr); /* flush */
9758 
9759 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9760 		status = mbx->mbxStatus;
9761 	}
9762 
9763 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9764 	return status;
9765 
9766 out_not_finished:
9767 	if (processing_queue) {
9768 		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
9769 		lpfc_mbox_cmpl_put(phba, pmbox);
9770 	}
9771 	return MBX_NOT_FINISHED;
9772 }
9773 
9774 /**
9775  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9776  * @phba: Pointer to HBA context object.
9777  *
9778  * The function blocks the posting of SLI4 asynchronous mailbox commands from
9779  * the driver internal pending mailbox queue. It will then try to wait out the
9780  * possible outstanding mailbox command before return.
9781  *
9782  * Returns:
9783  * 	0 - the outstanding mailbox command completed; otherwise, the wait for
9784  * 	the outstanding mailbox command timed out.
9785  **/
9786 static int
lpfc_sli4_async_mbox_block(struct lpfc_hba * phba)9787 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9788 {
9789 	struct lpfc_sli *psli = &phba->sli;
9790 	LPFC_MBOXQ_t *mboxq;
9791 	int rc = 0;
9792 	unsigned long timeout = 0;
9793 	u32 sli_flag;
9794 	u8 cmd, subsys, opcode;
9795 
9796 	/* Mark the asynchronous mailbox command posting as blocked */
9797 	spin_lock_irq(&phba->hbalock);
9798 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9799 	/* Determine how long we might wait for the active mailbox
9800 	 * command to be gracefully completed by firmware.
9801 	 */
9802 	if (phba->sli.mbox_active)
9803 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
9804 						phba->sli.mbox_active)) + jiffies;
9805 	spin_unlock_irq(&phba->hbalock);
9806 
9807 	/* Make sure the mailbox is really active */
9808 	if (timeout)
9809 		lpfc_sli4_process_missed_mbox_completions(phba);
9810 
9811 	/* Wait for the outstanding mailbox command to complete */
9812 	while (phba->sli.mbox_active) {
9813 		/* Check active mailbox complete status every 2ms */
9814 		msleep(2);
9815 		if (time_after(jiffies, timeout)) {
9816 			/* Timeout, mark the outstanding cmd not complete */
9817 
9818 			/* Sanity check sli.mbox_active has not completed or
9819 			 * cancelled from another context during last 2ms sleep,
9820 			 * so take hbalock to be sure before logging.
9821 			 */
9822 			spin_lock_irq(&phba->hbalock);
9823 			if (phba->sli.mbox_active) {
9824 				mboxq = phba->sli.mbox_active;
9825 				cmd = mboxq->u.mb.mbxCommand;
9826 				subsys = lpfc_sli_config_mbox_subsys_get(phba,
9827 									 mboxq);
9828 				opcode = lpfc_sli_config_mbox_opcode_get(phba,
9829 									 mboxq);
9830 				sli_flag = psli->sli_flag;
9831 				spin_unlock_irq(&phba->hbalock);
9832 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9833 						"2352 Mailbox command x%x "
9834 						"(x%x/x%x) sli_flag x%x could "
9835 						"not complete\n",
9836 						cmd, subsys, opcode,
9837 						sli_flag);
9838 			} else {
9839 				spin_unlock_irq(&phba->hbalock);
9840 			}
9841 
9842 			rc = 1;
9843 			break;
9844 		}
9845 	}
9846 
9847 	/* Can not cleanly block async mailbox command, fails it */
9848 	if (rc) {
9849 		spin_lock_irq(&phba->hbalock);
9850 		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9851 		spin_unlock_irq(&phba->hbalock);
9852 	}
9853 	return rc;
9854 }
9855 
9856 /**
9857  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9858  * @phba: Pointer to HBA context object.
9859  *
9860  * The function unblocks and resume posting of SLI4 asynchronous mailbox
9861  * commands from the driver internal pending mailbox queue. It makes sure
9862  * that there is no outstanding mailbox command before resuming posting
9863  * asynchronous mailbox commands. If, for any reason, there is outstanding
9864  * mailbox command, it will try to wait it out before resuming asynchronous
9865  * mailbox command posting.
9866  **/
9867 static void
lpfc_sli4_async_mbox_unblock(struct lpfc_hba * phba)9868 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9869 {
9870 	struct lpfc_sli *psli = &phba->sli;
9871 
9872 	spin_lock_irq(&phba->hbalock);
9873 	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9874 		/* Asynchronous mailbox posting is not blocked, do nothing */
9875 		spin_unlock_irq(&phba->hbalock);
9876 		return;
9877 	}
9878 
9879 	/* Outstanding synchronous mailbox command is guaranteed to be done,
9880 	 * successful or timeout, after timing-out the outstanding mailbox
9881 	 * command shall always be removed, so just unblock posting async
9882 	 * mailbox command and resume
9883 	 */
9884 	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9885 	spin_unlock_irq(&phba->hbalock);
9886 
9887 	/* wake up worker thread to post asynchronous mailbox command */
9888 	lpfc_worker_wake_up(phba);
9889 }
9890 
9891 /**
9892  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9893  * @phba: Pointer to HBA context object.
9894  * @mboxq: Pointer to mailbox object.
9895  *
9896  * The function waits for the bootstrap mailbox register ready bit from
9897  * port for twice the regular mailbox command timeout value.
9898  *
9899  *      0 - no timeout on waiting for bootstrap mailbox register ready.
9900  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out or port
9901  *                     is in an unrecoverable state.
9902  **/
9903 static int
lpfc_sli4_wait_bmbx_ready(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9904 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9905 {
9906 	uint32_t db_ready;
9907 	unsigned long timeout;
9908 	struct lpfc_register bmbx_reg;
9909 	struct lpfc_register portstat_reg = {-1};
9910 
9911 	/* Sanity check - there is no point to wait if the port is in an
9912 	 * unrecoverable state.
9913 	 */
9914 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
9915 	    LPFC_SLI_INTF_IF_TYPE_2) {
9916 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9917 			       &portstat_reg.word0) ||
9918 		    lpfc_sli4_unrecoverable_port(&portstat_reg)) {
9919 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9920 					"3858 Skipping bmbx ready because "
9921 					"Port Status x%x\n",
9922 					portstat_reg.word0);
9923 			return MBXERR_ERROR;
9924 		}
9925 	}
9926 
9927 	timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)) + jiffies;
9928 
9929 	do {
9930 		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9931 		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9932 		if (!db_ready)
9933 			mdelay(2);
9934 
9935 		if (time_after(jiffies, timeout))
9936 			return MBXERR_ERROR;
9937 	} while (!db_ready);
9938 
9939 	return 0;
9940 }
9941 
9942 /**
9943  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9944  * @phba: Pointer to HBA context object.
9945  * @mboxq: Pointer to mailbox object.
9946  *
9947  * The function posts a mailbox to the port.  The mailbox is expected
9948  * to be comletely filled in and ready for the port to operate on it.
9949  * This routine executes a synchronous completion operation on the
9950  * mailbox by polling for its completion.
9951  *
9952  * The caller must not be holding any locks when calling this routine.
9953  *
9954  * Returns:
9955  *	MBX_SUCCESS - mailbox posted successfully
9956  *	Any of the MBX error values.
9957  **/
9958 static int
lpfc_sli4_post_sync_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9959 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9960 {
9961 	int rc = MBX_SUCCESS;
9962 	unsigned long iflag;
9963 	uint32_t mcqe_status;
9964 	uint32_t mbx_cmnd;
9965 	struct lpfc_sli *psli = &phba->sli;
9966 	struct lpfc_mqe *mb = &mboxq->u.mqe;
9967 	struct lpfc_bmbx_create *mbox_rgn;
9968 	struct dma_address *dma_address;
9969 
9970 	/*
9971 	 * Only one mailbox can be active to the bootstrap mailbox region
9972 	 * at a time and there is no queueing provided.
9973 	 */
9974 	spin_lock_irqsave(&phba->hbalock, iflag);
9975 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9976 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9977 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9978 				"(%d):2532 Mailbox command x%x (x%x/x%x) "
9979 				"cannot issue Data: x%x x%x\n",
9980 				mboxq->vport ? mboxq->vport->vpi : 0,
9981 				mboxq->u.mb.mbxCommand,
9982 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9983 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9984 				psli->sli_flag, MBX_POLL);
9985 		return MBXERR_ERROR;
9986 	}
9987 	/* The server grabs the token and owns it until release */
9988 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9989 	phba->sli.mbox_active = mboxq;
9990 	spin_unlock_irqrestore(&phba->hbalock, iflag);
9991 
9992 	/* wait for bootstrap mbox register for readiness */
9993 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9994 	if (rc)
9995 		goto exit;
9996 	/*
9997 	 * Initialize the bootstrap memory region to avoid stale data areas
9998 	 * in the mailbox post.  Then copy the caller's mailbox contents to
9999 	 * the bmbx mailbox region.
10000 	 */
10001 	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
10002 	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
10003 	lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
10004 			       sizeof(struct lpfc_mqe));
10005 
10006 	/* Post the high mailbox dma address to the port and wait for ready. */
10007 	dma_address = &phba->sli4_hba.bmbx.dma_address;
10008 	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
10009 
10010 	/* wait for bootstrap mbox register for hi-address write done */
10011 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
10012 	if (rc)
10013 		goto exit;
10014 
10015 	/* Post the low mailbox dma address to the port. */
10016 	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
10017 
10018 	/* wait for bootstrap mbox register for low address write done */
10019 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
10020 	if (rc)
10021 		goto exit;
10022 
10023 	/*
10024 	 * Read the CQ to ensure the mailbox has completed.
10025 	 * If so, update the mailbox status so that the upper layers
10026 	 * can complete the request normally.
10027 	 */
10028 	lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
10029 			       sizeof(struct lpfc_mqe));
10030 	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
10031 	lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
10032 			       sizeof(struct lpfc_mcqe));
10033 	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
10034 	/*
10035 	 * When the CQE status indicates a failure and the mailbox status
10036 	 * indicates success then copy the CQE status into the mailbox status
10037 	 * (and prefix it with x4000).
10038 	 */
10039 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10040 		if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
10041 			bf_set(lpfc_mqe_status, mb,
10042 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
10043 		rc = MBXERR_ERROR;
10044 	} else
10045 		lpfc_sli4_swap_str(phba, mboxq);
10046 
10047 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10048 			"(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
10049 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
10050 			" x%x x%x CQ: x%x x%x x%x x%x\n",
10051 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10052 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10053 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10054 			bf_get(lpfc_mqe_status, mb),
10055 			mb->un.mb_words[0], mb->un.mb_words[1],
10056 			mb->un.mb_words[2], mb->un.mb_words[3],
10057 			mb->un.mb_words[4], mb->un.mb_words[5],
10058 			mb->un.mb_words[6], mb->un.mb_words[7],
10059 			mb->un.mb_words[8], mb->un.mb_words[9],
10060 			mb->un.mb_words[10], mb->un.mb_words[11],
10061 			mb->un.mb_words[12], mboxq->mcqe.word0,
10062 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
10063 			mboxq->mcqe.trailer);
10064 exit:
10065 	/* We are holding the token, no needed for lock when release */
10066 	spin_lock_irqsave(&phba->hbalock, iflag);
10067 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10068 	phba->sli.mbox_active = NULL;
10069 	spin_unlock_irqrestore(&phba->hbalock, iflag);
10070 	return rc;
10071 }
10072 
10073 /**
10074  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
10075  * @phba: Pointer to HBA context object.
10076  * @mboxq: Pointer to mailbox object.
10077  * @flag: Flag indicating how the mailbox need to be processed.
10078  *
10079  * This function is called by discovery code and HBA management code to submit
10080  * a mailbox command to firmware with SLI-4 interface spec.
10081  *
10082  * Return codes the caller owns the mailbox command after the return of the
10083  * function.
10084  **/
10085 static int
lpfc_sli_issue_mbox_s4(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint32_t flag)10086 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
10087 		       uint32_t flag)
10088 {
10089 	struct lpfc_sli *psli = &phba->sli;
10090 	unsigned long iflags;
10091 	int rc;
10092 
10093 	/* dump from issue mailbox command if setup */
10094 	lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
10095 
10096 	rc = lpfc_mbox_dev_check(phba);
10097 	if (unlikely(rc)) {
10098 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10099 				"(%d):2544 Mailbox command x%x (x%x/x%x) "
10100 				"cannot issue Data: x%x x%x\n",
10101 				mboxq->vport ? mboxq->vport->vpi : 0,
10102 				mboxq->u.mb.mbxCommand,
10103 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10104 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10105 				psli->sli_flag, flag);
10106 		goto out_not_finished;
10107 	}
10108 
10109 	/* Detect polling mode and jump to a handler */
10110 	if (!phba->sli4_hba.intr_enable) {
10111 		if (flag == MBX_POLL)
10112 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10113 		else
10114 			rc = -EIO;
10115 		if (rc != MBX_SUCCESS)
10116 			lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10117 					"(%d):2541 Mailbox command x%x "
10118 					"(x%x/x%x) failure: "
10119 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10120 					"Data: x%x x%x\n",
10121 					mboxq->vport ? mboxq->vport->vpi : 0,
10122 					mboxq->u.mb.mbxCommand,
10123 					lpfc_sli_config_mbox_subsys_get(phba,
10124 									mboxq),
10125 					lpfc_sli_config_mbox_opcode_get(phba,
10126 									mboxq),
10127 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10128 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10129 					bf_get(lpfc_mcqe_ext_status,
10130 					       &mboxq->mcqe),
10131 					psli->sli_flag, flag);
10132 		return rc;
10133 	} else if (flag == MBX_POLL) {
10134 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10135 				"(%d):2542 Try to issue mailbox command "
10136 				"x%x (x%x/x%x) synchronously ahead of async "
10137 				"mailbox command queue: x%x x%x\n",
10138 				mboxq->vport ? mboxq->vport->vpi : 0,
10139 				mboxq->u.mb.mbxCommand,
10140 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10141 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10142 				psli->sli_flag, flag);
10143 		/* Try to block the asynchronous mailbox posting */
10144 		rc = lpfc_sli4_async_mbox_block(phba);
10145 		if (!rc) {
10146 			/* Successfully blocked, now issue sync mbox cmd */
10147 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10148 			if (rc != MBX_SUCCESS)
10149 				lpfc_printf_log(phba, KERN_WARNING,
10150 					LOG_MBOX | LOG_SLI,
10151 					"(%d):2597 Sync Mailbox command "
10152 					"x%x (x%x/x%x) failure: "
10153 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10154 					"Data: x%x x%x\n",
10155 					mboxq->vport ? mboxq->vport->vpi : 0,
10156 					mboxq->u.mb.mbxCommand,
10157 					lpfc_sli_config_mbox_subsys_get(phba,
10158 									mboxq),
10159 					lpfc_sli_config_mbox_opcode_get(phba,
10160 									mboxq),
10161 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10162 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10163 					bf_get(lpfc_mcqe_ext_status,
10164 					       &mboxq->mcqe),
10165 					psli->sli_flag, flag);
10166 			/* Unblock the async mailbox posting afterward */
10167 			lpfc_sli4_async_mbox_unblock(phba);
10168 		}
10169 		return rc;
10170 	}
10171 
10172 	/* Now, interrupt mode asynchronous mailbox command */
10173 	rc = lpfc_mbox_cmd_check(phba, mboxq);
10174 	if (rc) {
10175 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10176 				"(%d):2543 Mailbox command x%x (x%x/x%x) "
10177 				"cannot issue Data: x%x x%x\n",
10178 				mboxq->vport ? mboxq->vport->vpi : 0,
10179 				mboxq->u.mb.mbxCommand,
10180 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10181 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10182 				psli->sli_flag, flag);
10183 		goto out_not_finished;
10184 	}
10185 
10186 	/* Put the mailbox command to the driver internal FIFO */
10187 	psli->slistat.mbox_busy++;
10188 	spin_lock_irqsave(&phba->hbalock, iflags);
10189 	lpfc_mbox_put(phba, mboxq);
10190 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10191 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10192 			"(%d):0354 Mbox cmd issue - Enqueue Data: "
10193 			"x%x (x%x/x%x) x%x x%x x%x x%x\n",
10194 			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
10195 			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
10196 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10197 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10198 			mboxq->u.mb.un.varUnregLogin.rpi,
10199 			phba->pport->port_state,
10200 			psli->sli_flag, MBX_NOWAIT);
10201 	/* Wake up worker thread to transport mailbox command from head */
10202 	lpfc_worker_wake_up(phba);
10203 
10204 	return MBX_BUSY;
10205 
10206 out_not_finished:
10207 	return MBX_NOT_FINISHED;
10208 }
10209 
10210 /**
10211  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
10212  * @phba: Pointer to HBA context object.
10213  *
10214  * This function is called by worker thread to send a mailbox command to
10215  * SLI4 HBA firmware.
10216  *
10217  **/
10218 int
lpfc_sli4_post_async_mbox(struct lpfc_hba * phba)10219 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
10220 {
10221 	struct lpfc_sli *psli = &phba->sli;
10222 	LPFC_MBOXQ_t *mboxq;
10223 	int rc = MBX_SUCCESS;
10224 	unsigned long iflags;
10225 	struct lpfc_mqe *mqe;
10226 	uint32_t mbx_cmnd;
10227 
10228 	/* Check interrupt mode before post async mailbox command */
10229 	if (unlikely(!phba->sli4_hba.intr_enable))
10230 		return MBX_NOT_FINISHED;
10231 
10232 	/* Check for mailbox command service token */
10233 	spin_lock_irqsave(&phba->hbalock, iflags);
10234 	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
10235 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10236 		return MBX_NOT_FINISHED;
10237 	}
10238 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10239 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10240 		return MBX_NOT_FINISHED;
10241 	}
10242 	if (unlikely(phba->sli.mbox_active)) {
10243 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10244 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10245 				"0384 There is pending active mailbox cmd\n");
10246 		return MBX_NOT_FINISHED;
10247 	}
10248 	/* Take the mailbox command service token */
10249 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
10250 
10251 	/* Get the next mailbox command from head of queue */
10252 	mboxq = lpfc_mbox_get(phba);
10253 
10254 	/* If no more mailbox command waiting for post, we're done */
10255 	if (!mboxq) {
10256 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10257 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10258 		return MBX_SUCCESS;
10259 	}
10260 	phba->sli.mbox_active = mboxq;
10261 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10262 
10263 	/* Check device readiness for posting mailbox command */
10264 	rc = lpfc_mbox_dev_check(phba);
10265 	if (unlikely(rc))
10266 		/* Driver clean routine will clean up pending mailbox */
10267 		goto out_not_finished;
10268 
10269 	/* Prepare the mbox command to be posted */
10270 	mqe = &mboxq->u.mqe;
10271 	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
10272 
10273 	/* Start timer for the mbox_tmo and log some mailbox post messages */
10274 	mod_timer(&psli->mbox_tmo, (jiffies +
10275 		  secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq))));
10276 
10277 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10278 			"(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
10279 			"x%x x%x\n",
10280 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10281 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10282 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10283 			phba->pport->port_state, psli->sli_flag);
10284 
10285 	if (mbx_cmnd != MBX_HEARTBEAT) {
10286 		if (mboxq->vport) {
10287 			lpfc_debugfs_disc_trc(mboxq->vport,
10288 				LPFC_DISC_TRC_MBOX_VPORT,
10289 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
10290 				mbx_cmnd, mqe->un.mb_words[0],
10291 				mqe->un.mb_words[1]);
10292 		} else {
10293 			lpfc_debugfs_disc_trc(phba->pport,
10294 				LPFC_DISC_TRC_MBOX,
10295 				"MBOX Send: cmd:x%x mb:x%x x%x",
10296 				mbx_cmnd, mqe->un.mb_words[0],
10297 				mqe->un.mb_words[1]);
10298 		}
10299 	}
10300 	psli->slistat.mbox_cmd++;
10301 
10302 	/* Post the mailbox command to the port */
10303 	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
10304 	if (rc != MBX_SUCCESS) {
10305 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10306 				"(%d):2533 Mailbox command x%x (x%x/x%x) "
10307 				"cannot issue Data: x%x x%x\n",
10308 				mboxq->vport ? mboxq->vport->vpi : 0,
10309 				mboxq->u.mb.mbxCommand,
10310 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10311 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10312 				psli->sli_flag, MBX_NOWAIT);
10313 		goto out_not_finished;
10314 	}
10315 
10316 	return rc;
10317 
10318 out_not_finished:
10319 	spin_lock_irqsave(&phba->hbalock, iflags);
10320 	if (phba->sli.mbox_active) {
10321 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10322 		__lpfc_mbox_cmpl_put(phba, mboxq);
10323 		/* Release the token */
10324 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10325 		phba->sli.mbox_active = NULL;
10326 	}
10327 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10328 
10329 	return MBX_NOT_FINISHED;
10330 }
10331 
10332 /**
10333  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
10334  * @phba: Pointer to HBA context object.
10335  * @pmbox: Pointer to mailbox object.
10336  * @flag: Flag indicating how the mailbox need to be processed.
10337  *
10338  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
10339  * the API jump table function pointer from the lpfc_hba struct.
10340  *
10341  * Return codes the caller owns the mailbox command after the return of the
10342  * function.
10343  **/
10344 int
lpfc_sli_issue_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)10345 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
10346 {
10347 	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
10348 }
10349 
10350 /**
10351  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
10352  * @phba: The hba struct for which this call is being executed.
10353  * @dev_grp: The HBA PCI-Device group number.
10354  *
10355  * This routine sets up the mbox interface API function jump table in @phba
10356  * struct.
10357  * Returns: 0 - success, -ENODEV - failure.
10358  **/
10359 int
lpfc_mbox_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)10360 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10361 {
10362 
10363 	switch (dev_grp) {
10364 	case LPFC_PCI_DEV_LP:
10365 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
10366 		phba->lpfc_sli_handle_slow_ring_event =
10367 				lpfc_sli_handle_slow_ring_event_s3;
10368 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
10369 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
10370 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
10371 		break;
10372 	case LPFC_PCI_DEV_OC:
10373 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
10374 		phba->lpfc_sli_handle_slow_ring_event =
10375 				lpfc_sli_handle_slow_ring_event_s4;
10376 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10377 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10378 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10379 		break;
10380 	default:
10381 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10382 				"1420 Invalid HBA PCI-device group: 0x%x\n",
10383 				dev_grp);
10384 		return -ENODEV;
10385 	}
10386 	return 0;
10387 }
10388 
10389 /**
10390  * __lpfc_sli_ringtx_put - Add an iocb to the txq
10391  * @phba: Pointer to HBA context object.
10392  * @pring: Pointer to driver SLI ring object.
10393  * @piocb: Pointer to address of newly added command iocb.
10394  *
10395  * This function is called with hbalock held for SLI3 ports or
10396  * the ring lock held for SLI4 ports to add a command
10397  * iocb to the txq when SLI layer cannot submit the command iocb
10398  * to the ring.
10399  **/
10400 void
__lpfc_sli_ringtx_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)10401 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10402 		    struct lpfc_iocbq *piocb)
10403 {
10404 	if (phba->sli_rev == LPFC_SLI_REV4)
10405 		lockdep_assert_held(&pring->ring_lock);
10406 	else
10407 		lockdep_assert_held(&phba->hbalock);
10408 	/* Insert the caller's iocb in the txq tail for later processing. */
10409 	list_add_tail(&piocb->list, &pring->txq);
10410 }
10411 
10412 /**
10413  * lpfc_sli_next_iocb - Get the next iocb in the txq
10414  * @phba: Pointer to HBA context object.
10415  * @pring: Pointer to driver SLI ring object.
10416  * @piocb: Pointer to address of newly added command iocb.
10417  *
10418  * This function is called with hbalock held before a new
10419  * iocb is submitted to the firmware. This function checks
10420  * txq to flush the iocbs in txq to Firmware before
10421  * submitting new iocbs to the Firmware.
10422  * If there are iocbs in the txq which need to be submitted
10423  * to firmware, lpfc_sli_next_iocb returns the first element
10424  * of the txq after dequeuing it from txq.
10425  * If there is no iocb in the txq then the function will return
10426  * *piocb and *piocb is set to NULL. Caller needs to check
10427  * *piocb to find if there are more commands in the txq.
10428  **/
10429 static struct lpfc_iocbq *
lpfc_sli_next_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq ** piocb)10430 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10431 		   struct lpfc_iocbq **piocb)
10432 {
10433 	struct lpfc_iocbq * nextiocb;
10434 
10435 	lockdep_assert_held(&phba->hbalock);
10436 
10437 	nextiocb = lpfc_sli_ringtx_get(phba, pring);
10438 	if (!nextiocb) {
10439 		nextiocb = *piocb;
10440 		*piocb = NULL;
10441 	}
10442 
10443 	return nextiocb;
10444 }
10445 
10446 /**
10447  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
10448  * @phba: Pointer to HBA context object.
10449  * @ring_number: SLI ring number to issue iocb on.
10450  * @piocb: Pointer to command iocb.
10451  * @flag: Flag indicating if this command can be put into txq.
10452  *
10453  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10454  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10455  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10456  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10457  * this function allows only iocbs for posting buffers. This function finds
10458  * next available slot in the command ring and posts the command to the
10459  * available slot and writes the port attention register to request HBA start
10460  * processing new iocb. If there is no slot available in the ring and
10461  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10462  * the function returns IOCB_BUSY.
10463  *
10464  * This function is called with hbalock held. The function will return success
10465  * after it successfully submit the iocb to firmware or after adding to the
10466  * txq.
10467  **/
10468 static int
__lpfc_sli_issue_iocb_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10469 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
10470 		    struct lpfc_iocbq *piocb, uint32_t flag)
10471 {
10472 	struct lpfc_iocbq *nextiocb;
10473 	IOCB_t *iocb;
10474 	struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
10475 
10476 	lockdep_assert_held(&phba->hbalock);
10477 
10478 	if (piocb->cmd_cmpl && (!piocb->vport) &&
10479 	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10480 	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
10481 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10482 				"1807 IOCB x%x failed. No vport\n",
10483 				piocb->iocb.ulpCommand);
10484 		dump_stack();
10485 		return IOCB_ERROR;
10486 	}
10487 
10488 
10489 	/* If the PCI channel is in offline state, do not post iocbs. */
10490 	if (unlikely(pci_channel_offline(phba->pcidev)))
10491 		return IOCB_ERROR;
10492 
10493 	/* If HBA has a deferred error attention, fail the iocb. */
10494 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
10495 		return IOCB_ERROR;
10496 
10497 	/*
10498 	 * We should never get an IOCB if we are in a < LINK_DOWN state
10499 	 */
10500 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10501 		return IOCB_ERROR;
10502 
10503 	/*
10504 	 * Check to see if we are blocking IOCB processing because of a
10505 	 * outstanding event.
10506 	 */
10507 	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
10508 		goto iocb_busy;
10509 
10510 	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
10511 		/*
10512 		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
10513 		 * can be issued if the link is not up.
10514 		 */
10515 		switch (piocb->iocb.ulpCommand) {
10516 		case CMD_QUE_RING_BUF_CN:
10517 		case CMD_QUE_RING_BUF64_CN:
10518 			/*
10519 			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10520 			 * completion, cmd_cmpl MUST be 0.
10521 			 */
10522 			if (piocb->cmd_cmpl)
10523 				piocb->cmd_cmpl = NULL;
10524 			fallthrough;
10525 		case CMD_CREATE_XRI_CR:
10526 		case CMD_CLOSE_XRI_CN:
10527 		case CMD_CLOSE_XRI_CX:
10528 			break;
10529 		default:
10530 			goto iocb_busy;
10531 		}
10532 
10533 	/*
10534 	 * For FCP commands, we must be in a state where we can process link
10535 	 * attention events.
10536 	 */
10537 	} else if (unlikely(pring->ringno == LPFC_FCP_RING &&
10538 			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
10539 		goto iocb_busy;
10540 	}
10541 
10542 	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10543 	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10544 		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10545 
10546 	if (iocb)
10547 		lpfc_sli_update_ring(phba, pring);
10548 	else
10549 		lpfc_sli_update_full_ring(phba, pring);
10550 
10551 	if (!piocb)
10552 		return IOCB_SUCCESS;
10553 
10554 	goto out_busy;
10555 
10556  iocb_busy:
10557 	pring->stats.iocb_cmd_delay++;
10558 
10559  out_busy:
10560 
10561 	if (!(flag & SLI_IOCB_RET_IOCB)) {
10562 		__lpfc_sli_ringtx_put(phba, pring, piocb);
10563 		return IOCB_SUCCESS;
10564 	}
10565 
10566 	return IOCB_BUSY;
10567 }
10568 
10569 /**
10570  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10571  * @phba: Pointer to HBA context object.
10572  * @ring_number: SLI ring number to issue wqe on.
10573  * @piocb: Pointer to command iocb.
10574  * @flag: Flag indicating if this command can be put into txq.
10575  *
10576  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10577  * send  an iocb command to an HBA with SLI-3 interface spec.
10578  *
10579  * This function takes the hbalock before invoking the lockless version.
10580  * The function will return success after it successfully submit the wqe to
10581  * firmware or after adding to the txq.
10582  **/
10583 static int
__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10584 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10585 			   struct lpfc_iocbq *piocb, uint32_t flag)
10586 {
10587 	unsigned long iflags;
10588 	int rc;
10589 
10590 	spin_lock_irqsave(&phba->hbalock, iflags);
10591 	rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10592 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10593 
10594 	return rc;
10595 }
10596 
10597 /**
10598  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10599  * @phba: Pointer to HBA context object.
10600  * @ring_number: SLI ring number to issue wqe on.
10601  * @piocb: Pointer to command iocb.
10602  * @flag: Flag indicating if this command can be put into txq.
10603  *
10604  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10605  * an wqe command to an HBA with SLI-4 interface spec.
10606  *
10607  * This function is a lockless version. The function will return success
10608  * after it successfully submit the wqe to firmware or after adding to the
10609  * txq.
10610  **/
10611 static int
__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10612 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10613 			   struct lpfc_iocbq *piocb, uint32_t flag)
10614 {
10615 	struct lpfc_io_buf *lpfc_cmd = piocb->io_buf;
10616 
10617 	lpfc_prep_embed_io(phba, lpfc_cmd);
10618 	return lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10619 }
10620 
10621 void
lpfc_prep_embed_io(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_cmd)10622 lpfc_prep_embed_io(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
10623 {
10624 	struct lpfc_iocbq *piocb = &lpfc_cmd->cur_iocbq;
10625 	union lpfc_wqe128 *wqe = &lpfc_cmd->cur_iocbq.wqe;
10626 	struct sli4_sge_le *sgl;
10627 	u32 type_size;
10628 
10629 	/* 128 byte wqe support here */
10630 	sgl = (struct sli4_sge_le *)lpfc_cmd->dma_sgl;
10631 
10632 	if (phba->fcp_embed_io) {
10633 		struct fcp_cmnd *fcp_cmnd;
10634 		u32 *ptr;
10635 
10636 		fcp_cmnd = lpfc_cmd->fcp_cmnd;
10637 
10638 		/* Word 0-2 - FCP_CMND */
10639 		type_size = le32_to_cpu(sgl->sge_len);
10640 		type_size |= ULP_BDE64_TYPE_BDE_IMMED;
10641 		wqe->generic.bde.tus.w = type_size;
10642 		wqe->generic.bde.addrHigh = 0;
10643 		wqe->generic.bde.addrLow =  72;  /* Word 18 */
10644 
10645 		bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10646 		bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10647 
10648 		/* Word 18-29  FCP CMND Payload */
10649 		ptr = &wqe->words[18];
10650 		lpfc_sli_pcimem_bcopy(fcp_cmnd, ptr, le32_to_cpu(sgl->sge_len));
10651 	} else {
10652 		/* Word 0-2 - Inline BDE */
10653 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10654 		wqe->generic.bde.tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
10655 		wqe->generic.bde.addrHigh = le32_to_cpu(sgl->addr_hi);
10656 		wqe->generic.bde.addrLow = le32_to_cpu(sgl->addr_lo);
10657 
10658 		/* Word 10 */
10659 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10660 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10661 	}
10662 
10663 	/* add the VMID tags as per switch response */
10664 	if (unlikely(piocb->cmd_flag & LPFC_IO_VMID)) {
10665 		if (phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
10666 			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10667 			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10668 					(piocb->vmid_tag.cs_ctl_vmid));
10669 		} else if (phba->cfg_vmid_app_header) {
10670 			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
10671 			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10672 			wqe->words[31] = piocb->vmid_tag.app_id;
10673 		}
10674 	}
10675 }
10676 
10677 /**
10678  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10679  * @phba: Pointer to HBA context object.
10680  * @ring_number: SLI ring number to issue iocb on.
10681  * @piocb: Pointer to command iocb.
10682  * @flag: Flag indicating if this command can be put into txq.
10683  *
10684  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10685  * an iocb command to an HBA with SLI-4 interface spec.
10686  *
10687  * This function is called with ringlock held. The function will return success
10688  * after it successfully submit the iocb to firmware or after adding to the
10689  * txq.
10690  **/
10691 static int
__lpfc_sli_issue_iocb_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10692 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10693 			 struct lpfc_iocbq *piocb, uint32_t flag)
10694 {
10695 	struct lpfc_sglq *sglq;
10696 	union lpfc_wqe128 *wqe;
10697 	struct lpfc_queue *wq;
10698 	struct lpfc_sli_ring *pring;
10699 	u32 ulp_command = get_job_cmnd(phba, piocb);
10700 
10701 	/* Get the WQ */
10702 	if ((piocb->cmd_flag & LPFC_IO_FCP) ||
10703 	    (piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
10704 		wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10705 	} else {
10706 		wq = phba->sli4_hba.els_wq;
10707 	}
10708 
10709 	/* Get corresponding ring */
10710 	pring = wq->pring;
10711 
10712 	/*
10713 	 * The WQE can be either 64 or 128 bytes,
10714 	 */
10715 
10716 	lockdep_assert_held(&pring->ring_lock);
10717 	wqe = &piocb->wqe;
10718 	if (piocb->sli4_xritag == NO_XRI) {
10719 		if (ulp_command == CMD_ABORT_XRI_CX)
10720 			sglq = NULL;
10721 		else {
10722 			sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10723 			if (!sglq) {
10724 				if (!(flag & SLI_IOCB_RET_IOCB)) {
10725 					__lpfc_sli_ringtx_put(phba,
10726 							pring,
10727 							piocb);
10728 					return IOCB_SUCCESS;
10729 				} else {
10730 					return IOCB_BUSY;
10731 				}
10732 			}
10733 		}
10734 	} else if (piocb->cmd_flag &  LPFC_IO_FCP) {
10735 		/* These IO's already have an XRI and a mapped sgl. */
10736 		sglq = NULL;
10737 	}
10738 	else {
10739 		/*
10740 		 * This is a continuation of a commandi,(CX) so this
10741 		 * sglq is on the active list
10742 		 */
10743 		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10744 		if (!sglq)
10745 			return IOCB_ERROR;
10746 	}
10747 
10748 	if (sglq) {
10749 		piocb->sli4_lxritag = sglq->sli4_lxritag;
10750 		piocb->sli4_xritag = sglq->sli4_xritag;
10751 
10752 		/* ABTS sent by initiator to CT exchange, the
10753 		 * RX_ID field will be filled with the newly
10754 		 * allocated responder XRI.
10755 		 */
10756 		if (ulp_command == CMD_XMIT_BLS_RSP64_CX &&
10757 		    piocb->abort_bls == LPFC_ABTS_UNSOL_INT)
10758 			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10759 			       piocb->sli4_xritag);
10760 
10761 		bf_set(wqe_xri_tag, &wqe->generic.wqe_com,
10762 		       piocb->sli4_xritag);
10763 
10764 		if (lpfc_wqe_bpl2sgl(phba, piocb, sglq) == NO_XRI)
10765 			return IOCB_ERROR;
10766 	}
10767 
10768 	if (lpfc_sli4_wq_put(wq, wqe))
10769 		return IOCB_ERROR;
10770 
10771 	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10772 
10773 	return 0;
10774 }
10775 
10776 /*
10777  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10778  *
10779  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10780  * or IOCB for sli-3  function.
10781  * pointer from the lpfc_hba struct.
10782  *
10783  * Return codes:
10784  * IOCB_ERROR - Error
10785  * IOCB_SUCCESS - Success
10786  * IOCB_BUSY - Busy
10787  **/
10788 int
lpfc_sli_issue_fcp_io(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10789 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10790 		      struct lpfc_iocbq *piocb, uint32_t flag)
10791 {
10792 	return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10793 }
10794 
10795 /*
10796  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10797  *
10798  * This routine wraps the actual lockless version for issusing IOCB function
10799  * pointer from the lpfc_hba struct.
10800  *
10801  * Return codes:
10802  * IOCB_ERROR - Error
10803  * IOCB_SUCCESS - Success
10804  * IOCB_BUSY - Busy
10805  **/
10806 int
__lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10807 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10808 		struct lpfc_iocbq *piocb, uint32_t flag)
10809 {
10810 	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10811 }
10812 
10813 static void
__lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq * cmdiocbq,struct lpfc_vport * vport,struct lpfc_dmabuf * bmp,u16 cmd_size,u32 did,u32 elscmd,u8 tmo,u8 expect_rsp)10814 __lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq *cmdiocbq,
10815 			       struct lpfc_vport *vport,
10816 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10817 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10818 {
10819 	struct lpfc_hba *phba = vport->phba;
10820 	IOCB_t *cmd;
10821 
10822 	cmd = &cmdiocbq->iocb;
10823 	memset(cmd, 0, sizeof(*cmd));
10824 
10825 	cmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10826 	cmd->un.elsreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10827 	cmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10828 
10829 	if (expect_rsp) {
10830 		cmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
10831 		cmd->un.elsreq64.remoteID = did; /* DID */
10832 		cmd->ulpCommand = CMD_ELS_REQUEST64_CR;
10833 		cmd->ulpTimeout = tmo;
10834 	} else {
10835 		cmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
10836 		cmd->un.genreq64.xmit_els_remoteID = did; /* DID */
10837 		cmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
10838 		cmd->ulpPU = PARM_NPIV_DID;
10839 	}
10840 	cmd->ulpBdeCount = 1;
10841 	cmd->ulpLe = 1;
10842 	cmd->ulpClass = CLASS3;
10843 
10844 	/* If we have NPIV enabled, we want to send ELS traffic by VPI. */
10845 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
10846 		if (expect_rsp) {
10847 			cmd->un.elsreq64.myID = vport->fc_myDID;
10848 
10849 			/* For ELS_REQUEST64_CR, use the VPI by default */
10850 			cmd->ulpContext = phba->vpi_ids[vport->vpi];
10851 		}
10852 
10853 		cmd->ulpCt_h = 0;
10854 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10855 		if (elscmd == ELS_CMD_ECHO)
10856 			cmd->ulpCt_l = 0; /* context = invalid RPI */
10857 		else
10858 			cmd->ulpCt_l = 1; /* context = VPI */
10859 	}
10860 }
10861 
10862 static void
__lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq * cmdiocbq,struct lpfc_vport * vport,struct lpfc_dmabuf * bmp,u16 cmd_size,u32 did,u32 elscmd,u8 tmo,u8 expect_rsp)10863 __lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq *cmdiocbq,
10864 			       struct lpfc_vport *vport,
10865 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10866 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10867 {
10868 	struct lpfc_hba  *phba = vport->phba;
10869 	union lpfc_wqe128 *wqe;
10870 	struct ulp_bde64_le *bde;
10871 	u8 els_id;
10872 
10873 	wqe = &cmdiocbq->wqe;
10874 	memset(wqe, 0, sizeof(*wqe));
10875 
10876 	/* Word 0 - 2 BDE */
10877 	bde = (struct ulp_bde64_le *)&wqe->generic.bde;
10878 	bde->addr_low = cpu_to_le32(putPaddrLow(bmp->phys));
10879 	bde->addr_high = cpu_to_le32(putPaddrHigh(bmp->phys));
10880 	bde->type_size = cpu_to_le32(cmd_size);
10881 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10882 
10883 	if (expect_rsp) {
10884 		bf_set(wqe_cmnd, &wqe->els_req.wqe_com, CMD_ELS_REQUEST64_WQE);
10885 
10886 		/* Transfer length */
10887 		wqe->els_req.payload_len = cmd_size;
10888 		wqe->els_req.max_response_payload_len = FCELSSIZE;
10889 
10890 		/* DID */
10891 		bf_set(wqe_els_did, &wqe->els_req.wqe_dest, did);
10892 
10893 		/* Word 11 - ELS_ID */
10894 		switch (elscmd) {
10895 		case ELS_CMD_PLOGI:
10896 			els_id = LPFC_ELS_ID_PLOGI;
10897 			break;
10898 		case ELS_CMD_FLOGI:
10899 			els_id = LPFC_ELS_ID_FLOGI;
10900 			break;
10901 		case ELS_CMD_LOGO:
10902 			els_id = LPFC_ELS_ID_LOGO;
10903 			break;
10904 		case ELS_CMD_FDISC:
10905 			if (!vport->fc_myDID) {
10906 				els_id = LPFC_ELS_ID_FDISC;
10907 				break;
10908 			}
10909 			fallthrough;
10910 		default:
10911 			els_id = LPFC_ELS_ID_DEFAULT;
10912 			break;
10913 		}
10914 
10915 		bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10916 	} else {
10917 		/* DID */
10918 		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest, did);
10919 
10920 		/* Transfer length */
10921 		wqe->xmit_els_rsp.response_payload_len = cmd_size;
10922 
10923 		bf_set(wqe_cmnd, &wqe->xmit_els_rsp.wqe_com,
10924 		       CMD_XMIT_ELS_RSP64_WQE);
10925 	}
10926 
10927 	bf_set(wqe_tmo, &wqe->generic.wqe_com, tmo);
10928 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, cmdiocbq->iotag);
10929 	bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
10930 
10931 	/* If we have NPIV enabled, we want to send ELS traffic by VPI.
10932 	 * For SLI4, since the driver controls VPIs we also want to include
10933 	 * all ELS pt2pt protocol traffic as well.
10934 	 */
10935 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
10936 	    test_bit(FC_PT2PT, &vport->fc_flag)) {
10937 		if (expect_rsp) {
10938 			bf_set(els_req64_sid, &wqe->els_req, vport->fc_myDID);
10939 
10940 			/* For ELS_REQUEST64_WQE, use the VPI by default */
10941 			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10942 			       phba->vpi_ids[vport->vpi]);
10943 		}
10944 
10945 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10946 		if (elscmd == ELS_CMD_ECHO)
10947 			bf_set(wqe_ct, &wqe->generic.wqe_com, 0);
10948 		else
10949 			bf_set(wqe_ct, &wqe->generic.wqe_com, 1);
10950 	}
10951 }
10952 
10953 void
lpfc_sli_prep_els_req_rsp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_vport * vport,struct lpfc_dmabuf * bmp,u16 cmd_size,u32 did,u32 elscmd,u8 tmo,u8 expect_rsp)10954 lpfc_sli_prep_els_req_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
10955 			  struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
10956 			  u16 cmd_size, u32 did, u32 elscmd, u8 tmo,
10957 			  u8 expect_rsp)
10958 {
10959 	phba->__lpfc_sli_prep_els_req_rsp(cmdiocbq, vport, bmp, cmd_size, did,
10960 					  elscmd, tmo, expect_rsp);
10961 }
10962 
10963 static void
__lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10964 __lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10965 			   u16 rpi, u32 num_entry, u8 tmo)
10966 {
10967 	IOCB_t *cmd;
10968 
10969 	cmd = &cmdiocbq->iocb;
10970 	memset(cmd, 0, sizeof(*cmd));
10971 
10972 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10973 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10974 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10975 	cmd->un.genreq64.bdl.bdeSize = num_entry * sizeof(struct ulp_bde64);
10976 
10977 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
10978 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
10979 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
10980 
10981 	cmd->ulpContext = rpi;
10982 	cmd->ulpClass = CLASS3;
10983 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
10984 	cmd->ulpBdeCount = 1;
10985 	cmd->ulpLe = 1;
10986 	cmd->ulpOwner = OWN_CHIP;
10987 	cmd->ulpTimeout = tmo;
10988 }
10989 
10990 static void
__lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10991 __lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10992 			   u16 rpi, u32 num_entry, u8 tmo)
10993 {
10994 	union lpfc_wqe128 *cmdwqe;
10995 	struct ulp_bde64_le *bde, *bpl;
10996 	u32 xmit_len = 0, total_len = 0, size, type, i;
10997 
10998 	cmdwqe = &cmdiocbq->wqe;
10999 	memset(cmdwqe, 0, sizeof(*cmdwqe));
11000 
11001 	/* Calculate total_len and xmit_len */
11002 	bpl = (struct ulp_bde64_le *)bmp->virt;
11003 	for (i = 0; i < num_entry; i++) {
11004 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
11005 		total_len += size;
11006 	}
11007 	for (i = 0; i < num_entry; i++) {
11008 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
11009 		type = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_TYPE_MASK;
11010 		if (type != ULP_BDE64_TYPE_BDE_64)
11011 			break;
11012 		xmit_len += size;
11013 	}
11014 
11015 	/* Words 0 - 2 */
11016 	bde = (struct ulp_bde64_le *)&cmdwqe->generic.bde;
11017 	bde->addr_low = bpl->addr_low;
11018 	bde->addr_high = bpl->addr_high;
11019 	bde->type_size = cpu_to_le32(xmit_len);
11020 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
11021 
11022 	/* Word 3 */
11023 	cmdwqe->gen_req.request_payload_len = xmit_len;
11024 
11025 	/* Word 5 */
11026 	bf_set(wqe_type, &cmdwqe->gen_req.wge_ctl, FC_TYPE_CT);
11027 	bf_set(wqe_rctl, &cmdwqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);
11028 	bf_set(wqe_si, &cmdwqe->gen_req.wge_ctl, 1);
11029 	bf_set(wqe_la, &cmdwqe->gen_req.wge_ctl, 1);
11030 
11031 	/* Word 6 */
11032 	bf_set(wqe_ctxt_tag, &cmdwqe->gen_req.wqe_com, rpi);
11033 
11034 	/* Word 7 */
11035 	bf_set(wqe_tmo, &cmdwqe->gen_req.wqe_com, tmo);
11036 	bf_set(wqe_class, &cmdwqe->gen_req.wqe_com, CLASS3);
11037 	bf_set(wqe_cmnd, &cmdwqe->gen_req.wqe_com, CMD_GEN_REQUEST64_CR);
11038 	bf_set(wqe_ct, &cmdwqe->gen_req.wqe_com, SLI4_CT_RPI);
11039 
11040 	/* Word 12 */
11041 	cmdwqe->gen_req.max_response_payload_len = total_len - xmit_len;
11042 }
11043 
11044 void
lpfc_sli_prep_gen_req(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)11045 lpfc_sli_prep_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11046 		      struct lpfc_dmabuf *bmp, u16 rpi, u32 num_entry, u8 tmo)
11047 {
11048 	phba->__lpfc_sli_prep_gen_req(cmdiocbq, bmp, rpi, num_entry, tmo);
11049 }
11050 
11051 static void
__lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u16 ox_id,u32 num_entry,u8 rctl,u8 last_seq,u8 cr_cx_cmd)11052 __lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq *cmdiocbq,
11053 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11054 			      u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11055 {
11056 	IOCB_t *icmd;
11057 
11058 	icmd = &cmdiocbq->iocb;
11059 	memset(icmd, 0, sizeof(*icmd));
11060 
11061 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
11062 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
11063 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
11064 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
11065 	icmd->un.xseq64.w5.hcsw.Fctl = LA;
11066 	if (last_seq)
11067 		icmd->un.xseq64.w5.hcsw.Fctl |= LS;
11068 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11069 	icmd->un.xseq64.w5.hcsw.Rctl = rctl;
11070 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
11071 
11072 	icmd->ulpBdeCount = 1;
11073 	icmd->ulpLe = 1;
11074 	icmd->ulpClass = CLASS3;
11075 
11076 	switch (cr_cx_cmd) {
11077 	case CMD_XMIT_SEQUENCE64_CR:
11078 		icmd->ulpContext = rpi;
11079 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
11080 		break;
11081 	case CMD_XMIT_SEQUENCE64_CX:
11082 		icmd->ulpContext = ox_id;
11083 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
11084 		break;
11085 	default:
11086 		break;
11087 	}
11088 }
11089 
11090 static void
__lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u16 ox_id,u32 full_size,u8 rctl,u8 last_seq,u8 cr_cx_cmd)11091 __lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq *cmdiocbq,
11092 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11093 			      u32 full_size, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11094 {
11095 	union lpfc_wqe128 *wqe;
11096 	struct ulp_bde64 *bpl;
11097 
11098 	wqe = &cmdiocbq->wqe;
11099 	memset(wqe, 0, sizeof(*wqe));
11100 
11101 	/* Words 0 - 2 */
11102 	bpl = (struct ulp_bde64 *)bmp->virt;
11103 	wqe->xmit_sequence.bde.addrHigh = bpl->addrHigh;
11104 	wqe->xmit_sequence.bde.addrLow = bpl->addrLow;
11105 	wqe->xmit_sequence.bde.tus.w = bpl->tus.w;
11106 
11107 	/* Word 5 */
11108 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, last_seq);
11109 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 1);
11110 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
11111 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, rctl);
11112 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_CT);
11113 
11114 	/* Word 6 */
11115 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com, rpi);
11116 
11117 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
11118 	       CMD_XMIT_SEQUENCE64_WQE);
11119 
11120 	/* Word 7 */
11121 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
11122 
11123 	/* Word 9 */
11124 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ox_id);
11125 
11126 	if (cmdiocbq->cmd_flag & (LPFC_IO_LIBDFC | LPFC_IO_LOOPBACK)) {
11127 		/* Word 10 */
11128 		if (cmdiocbq->cmd_flag & LPFC_IO_VMID) {
11129 			bf_set(wqe_appid, &wqe->xmit_sequence.wqe_com, 1);
11130 			bf_set(wqe_wqes, &wqe->xmit_sequence.wqe_com, 1);
11131 			wqe->words[31] = LOOPBACK_SRC_APPID;
11132 		}
11133 
11134 		/* Word 12 */
11135 		wqe->xmit_sequence.xmit_len = full_size;
11136 	}
11137 	else
11138 		wqe->xmit_sequence.xmit_len =
11139 			wqe->xmit_sequence.bde.tus.f.bdeSize;
11140 }
11141 
11142 void
lpfc_sli_prep_xmit_seq64(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u16 ox_id,u32 num_entry,u8 rctl,u8 last_seq,u8 cr_cx_cmd)11143 lpfc_sli_prep_xmit_seq64(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11144 			 struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11145 			 u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11146 {
11147 	phba->__lpfc_sli_prep_xmit_seq64(cmdiocbq, bmp, rpi, ox_id, num_entry,
11148 					 rctl, last_seq, cr_cx_cmd);
11149 }
11150 
11151 static void
__lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq * cmdiocbq,u16 ulp_context,u16 iotag,u8 ulp_class,u16 cqid,bool ia,bool wqec)11152 __lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11153 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11154 			     bool wqec)
11155 {
11156 	IOCB_t *icmd = NULL;
11157 
11158 	icmd = &cmdiocbq->iocb;
11159 	memset(icmd, 0, sizeof(*icmd));
11160 
11161 	/* Word 5 */
11162 	icmd->un.acxri.abortContextTag = ulp_context;
11163 	icmd->un.acxri.abortIoTag = iotag;
11164 
11165 	if (ia) {
11166 		/* Word 7 */
11167 		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
11168 	} else {
11169 		/* Word 3 */
11170 		icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
11171 
11172 		/* Word 7 */
11173 		icmd->ulpClass = ulp_class;
11174 		icmd->ulpCommand = CMD_ABORT_XRI_CN;
11175 	}
11176 
11177 	/* Word 7 */
11178 	icmd->ulpLe = 1;
11179 }
11180 
11181 static void
__lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq * cmdiocbq,u16 ulp_context,u16 iotag,u8 ulp_class,u16 cqid,bool ia,bool wqec)11182 __lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11183 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11184 			     bool wqec)
11185 {
11186 	union lpfc_wqe128 *wqe;
11187 
11188 	wqe = &cmdiocbq->wqe;
11189 	memset(wqe, 0, sizeof(*wqe));
11190 
11191 	/* Word 3 */
11192 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
11193 	if (ia)
11194 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
11195 	else
11196 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
11197 
11198 	/* Word 7 */
11199 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_WQE);
11200 
11201 	/* Word 8 */
11202 	wqe->abort_cmd.wqe_com.abort_tag = ulp_context;
11203 
11204 	/* Word 9 */
11205 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, iotag);
11206 
11207 	/* Word 10 */
11208 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
11209 
11210 	/* Word 11 */
11211 	if (wqec)
11212 		bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
11213 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, cqid);
11214 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11215 }
11216 
11217 void
lpfc_sli_prep_abort_xri(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,u16 ulp_context,u16 iotag,u8 ulp_class,u16 cqid,bool ia,bool wqec)11218 lpfc_sli_prep_abort_xri(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11219 			u16 ulp_context, u16 iotag, u8 ulp_class, u16 cqid,
11220 			bool ia, bool wqec)
11221 {
11222 	phba->__lpfc_sli_prep_abort_xri(cmdiocbq, ulp_context, iotag, ulp_class,
11223 					cqid, ia, wqec);
11224 }
11225 
11226 /**
11227  * lpfc_sli_api_table_setup - Set up sli api function jump table
11228  * @phba: The hba struct for which this call is being executed.
11229  * @dev_grp: The HBA PCI-Device group number.
11230  *
11231  * This routine sets up the SLI interface API function jump table in @phba
11232  * struct.
11233  * Returns: 0 - success, -ENODEV - failure.
11234  **/
11235 int
lpfc_sli_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)11236 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11237 {
11238 
11239 	switch (dev_grp) {
11240 	case LPFC_PCI_DEV_LP:
11241 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11242 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
11243 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
11244 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s3;
11245 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s3;
11246 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s3;
11247 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s3;
11248 		break;
11249 	case LPFC_PCI_DEV_OC:
11250 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11251 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
11252 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
11253 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s4;
11254 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s4;
11255 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s4;
11256 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s4;
11257 		break;
11258 	default:
11259 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11260 				"1419 Invalid HBA PCI-device group: 0x%x\n",
11261 				dev_grp);
11262 		return -ENODEV;
11263 	}
11264 	return 0;
11265 }
11266 
11267 /**
11268  * lpfc_sli4_calc_ring - Calculates which ring to use
11269  * @phba: Pointer to HBA context object.
11270  * @piocb: Pointer to command iocb.
11271  *
11272  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11273  * hba_wqidx, thus we need to calculate the corresponding ring.
11274  * Since ABORTS must go on the same WQ of the command they are
11275  * aborting, we use command's hba_wqidx.
11276  */
11277 struct lpfc_sli_ring *
lpfc_sli4_calc_ring(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)11278 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
11279 {
11280 	struct lpfc_io_buf *lpfc_cmd;
11281 
11282 	if (piocb->cmd_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
11283 		if (unlikely(!phba->sli4_hba.hdwq))
11284 			return NULL;
11285 		/*
11286 		 * for abort iocb hba_wqidx should already
11287 		 * be setup based on what work queue we used.
11288 		 */
11289 		if (!(piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
11290 			lpfc_cmd = piocb->io_buf;
11291 			piocb->hba_wqidx = lpfc_cmd->hdwq_no;
11292 		}
11293 		return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
11294 	} else {
11295 		if (unlikely(!phba->sli4_hba.els_wq))
11296 			return NULL;
11297 		piocb->hba_wqidx = 0;
11298 		return phba->sli4_hba.els_wq->pring;
11299 	}
11300 }
11301 
lpfc_sli4_poll_eq(struct lpfc_queue * eq)11302 inline void lpfc_sli4_poll_eq(struct lpfc_queue *eq)
11303 {
11304 	struct lpfc_hba *phba = eq->phba;
11305 
11306 	/*
11307 	 * Unlocking an irq is one of the entry point to check
11308 	 * for re-schedule, but we are good for io submission
11309 	 * path as midlayer does a get_cpu to glue us in. Flush
11310 	 * out the invalidate queue so we can see the updated
11311 	 * value for flag.
11312 	 */
11313 	smp_rmb();
11314 
11315 	if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
11316 		/* We will not likely get the completion for the caller
11317 		 * during this iteration but i guess that's fine.
11318 		 * Future io's coming on this eq should be able to
11319 		 * pick it up.  As for the case of single io's, they
11320 		 * will be handled through a sched from polling timer
11321 		 * function which is currently triggered every 1msec.
11322 		 */
11323 		lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM,
11324 				     LPFC_QUEUE_WORK);
11325 }
11326 
11327 /**
11328  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
11329  * @phba: Pointer to HBA context object.
11330  * @ring_number: Ring number
11331  * @piocb: Pointer to command iocb.
11332  * @flag: Flag indicating if this command can be put into txq.
11333  *
11334  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11335  * function. This function gets the hbalock and calls
11336  * __lpfc_sli_issue_iocb function and will return the error returned
11337  * by __lpfc_sli_issue_iocb function. This wrapper is used by
11338  * functions which do not hold hbalock.
11339  **/
11340 int
lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)11341 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11342 		    struct lpfc_iocbq *piocb, uint32_t flag)
11343 {
11344 	struct lpfc_sli_ring *pring;
11345 	struct lpfc_queue *eq;
11346 	unsigned long iflags;
11347 	int rc;
11348 
11349 	/* If the PCI channel is in offline state, do not post iocbs. */
11350 	if (unlikely(pci_channel_offline(phba->pcidev)))
11351 		return IOCB_ERROR;
11352 
11353 	if (phba->sli_rev == LPFC_SLI_REV4) {
11354 		lpfc_sli_prep_wqe(phba, piocb);
11355 
11356 		eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11357 
11358 		pring = lpfc_sli4_calc_ring(phba, piocb);
11359 		if (unlikely(pring == NULL))
11360 			return IOCB_ERROR;
11361 
11362 		spin_lock_irqsave(&pring->ring_lock, iflags);
11363 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11364 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
11365 
11366 		lpfc_sli4_poll_eq(eq);
11367 	} else {
11368 		/* For now, SLI2/3 will still use hbalock */
11369 		spin_lock_irqsave(&phba->hbalock, iflags);
11370 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11371 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11372 	}
11373 	return rc;
11374 }
11375 
11376 /**
11377  * lpfc_extra_ring_setup - Extra ring setup function
11378  * @phba: Pointer to HBA context object.
11379  *
11380  * This function is called while driver attaches with the
11381  * HBA to setup the extra ring. The extra ring is used
11382  * only when driver needs to support target mode functionality
11383  * or IP over FC functionalities.
11384  *
11385  * This function is called with no lock held. SLI3 only.
11386  **/
11387 static int
lpfc_extra_ring_setup(struct lpfc_hba * phba)11388 lpfc_extra_ring_setup( struct lpfc_hba *phba)
11389 {
11390 	struct lpfc_sli *psli;
11391 	struct lpfc_sli_ring *pring;
11392 
11393 	psli = &phba->sli;
11394 
11395 	/* Adjust cmd/rsp ring iocb entries more evenly */
11396 
11397 	/* Take some away from the FCP ring */
11398 	pring = &psli->sli3_ring[LPFC_FCP_RING];
11399 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11400 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11401 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11402 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11403 
11404 	/* and give them to the extra ring */
11405 	pring = &psli->sli3_ring[LPFC_EXTRA_RING];
11406 
11407 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11408 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11409 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11410 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11411 
11412 	/* Setup default profile for this ring */
11413 	pring->iotag_max = 4096;
11414 	pring->num_mask = 1;
11415 	pring->prt[0].profile = 0;      /* Mask 0 */
11416 	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11417 	pring->prt[0].type = phba->cfg_multi_ring_type;
11418 	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11419 	return 0;
11420 }
11421 
11422 static void
lpfc_sli_post_recovery_event(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)11423 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11424 			     struct lpfc_nodelist *ndlp)
11425 {
11426 	unsigned long iflags;
11427 	struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
11428 
11429 	/* Hold a node reference for outstanding queued work */
11430 	if (!lpfc_nlp_get(ndlp))
11431 		return;
11432 
11433 	spin_lock_irqsave(&phba->hbalock, iflags);
11434 	if (!list_empty(&evtp->evt_listp)) {
11435 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11436 		lpfc_nlp_put(ndlp);
11437 		return;
11438 	}
11439 
11440 	evtp->evt_arg1 = ndlp;
11441 	evtp->evt = LPFC_EVT_RECOVER_PORT;
11442 	list_add_tail(&evtp->evt_listp, &phba->work_list);
11443 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11444 
11445 	lpfc_worker_wake_up(phba);
11446 }
11447 
11448 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11449  * @phba: Pointer to HBA context object.
11450  * @iocbq: Pointer to iocb object.
11451  *
11452  * The async_event handler calls this routine when it receives
11453  * an ASYNC_STATUS_CN event from the port.  The port generates
11454  * this event when an Abort Sequence request to an rport fails
11455  * twice in succession.  The abort could be originated by the
11456  * driver or by the port.  The ABTS could have been for an ELS
11457  * or FCP IO.  The port only generates this event when an ABTS
11458  * fails to complete after one retry.
11459  */
11460 static void
lpfc_sli_abts_err_handler(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)11461 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11462 			  struct lpfc_iocbq *iocbq)
11463 {
11464 	struct lpfc_nodelist *ndlp = NULL;
11465 	uint16_t rpi = 0, vpi = 0;
11466 	struct lpfc_vport *vport = NULL;
11467 
11468 	/* The rpi in the ulpContext is vport-sensitive. */
11469 	vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11470 	rpi = iocbq->iocb.ulpContext;
11471 
11472 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11473 			"3092 Port generated ABTS async event "
11474 			"on vpi %d rpi %d status 0x%x\n",
11475 			vpi, rpi, iocbq->iocb.ulpStatus);
11476 
11477 	vport = lpfc_find_vport_by_vpid(phba, vpi);
11478 	if (!vport)
11479 		goto err_exit;
11480 	ndlp = lpfc_findnode_rpi(vport, rpi);
11481 	if (!ndlp)
11482 		goto err_exit;
11483 
11484 	if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11485 		lpfc_sli_abts_recover_port(vport, ndlp);
11486 	return;
11487 
11488  err_exit:
11489 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11490 			"3095 Event Context not found, no "
11491 			"action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11492 			vpi, rpi, iocbq->iocb.ulpStatus,
11493 			iocbq->iocb.ulpContext);
11494 }
11495 
11496 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11497  * @phba: pointer to HBA context object.
11498  * @ndlp: nodelist pointer for the impacted rport.
11499  * @axri: pointer to the wcqe containing the failed exchange.
11500  *
11501  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11502  * port.  The port generates this event when an abort exchange request to an
11503  * rport fails twice in succession with no reply.  The abort could be originated
11504  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
11505  */
11506 void
lpfc_sli4_abts_err_handler(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct sli4_wcqe_xri_aborted * axri)11507 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11508 			   struct lpfc_nodelist *ndlp,
11509 			   struct sli4_wcqe_xri_aborted *axri)
11510 {
11511 	uint32_t ext_status = 0;
11512 
11513 	if (!ndlp) {
11514 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11515 				"3115 Node Context not found, driver "
11516 				"ignoring abts err event\n");
11517 		return;
11518 	}
11519 
11520 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11521 			"3116 Port generated FCP XRI ABORT event on "
11522 			"vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
11523 			ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
11524 			bf_get(lpfc_wcqe_xa_xri, axri),
11525 			bf_get(lpfc_wcqe_xa_status, axri),
11526 			axri->parameter);
11527 
11528 	/*
11529 	 * Catch the ABTS protocol failure case.  Older OCe FW releases returned
11530 	 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11531 	 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11532 	 */
11533 	ext_status = axri->parameter & IOERR_PARAM_MASK;
11534 	if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11535 	    ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
11536 		lpfc_sli_post_recovery_event(phba, ndlp);
11537 }
11538 
11539 /**
11540  * lpfc_sli_async_event_handler - ASYNC iocb handler function
11541  * @phba: Pointer to HBA context object.
11542  * @pring: Pointer to driver SLI ring object.
11543  * @iocbq: Pointer to iocb object.
11544  *
11545  * This function is called by the slow ring event handler
11546  * function when there is an ASYNC event iocb in the ring.
11547  * This function is called with no lock held.
11548  * Currently this function handles only temperature related
11549  * ASYNC events. The function decodes the temperature sensor
11550  * event message and posts events for the management applications.
11551  **/
11552 static void
lpfc_sli_async_event_handler(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * iocbq)11553 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11554 	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11555 {
11556 	IOCB_t *icmd;
11557 	uint16_t evt_code;
11558 	struct temp_event temp_event_data;
11559 	struct Scsi_Host *shost;
11560 	uint32_t *iocb_w;
11561 
11562 	icmd = &iocbq->iocb;
11563 	evt_code = icmd->un.asyncstat.evt_code;
11564 
11565 	switch (evt_code) {
11566 	case ASYNC_TEMP_WARN:
11567 	case ASYNC_TEMP_SAFE:
11568 		temp_event_data.data = (uint32_t) icmd->ulpContext;
11569 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11570 		if (evt_code == ASYNC_TEMP_WARN) {
11571 			temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
11572 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11573 				"0347 Adapter is very hot, please take "
11574 				"corrective action. temperature : %d Celsius\n",
11575 				(uint32_t) icmd->ulpContext);
11576 		} else {
11577 			temp_event_data.event_code = LPFC_NORMAL_TEMP;
11578 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11579 				"0340 Adapter temperature is OK now. "
11580 				"temperature : %d Celsius\n",
11581 				(uint32_t) icmd->ulpContext);
11582 		}
11583 
11584 		/* Send temperature change event to applications */
11585 		shost = lpfc_shost_from_vport(phba->pport);
11586 		fc_host_post_vendor_event(shost, fc_get_event_number(),
11587 			sizeof(temp_event_data), (char *) &temp_event_data,
11588 			LPFC_NL_VENDOR_ID);
11589 		break;
11590 	case ASYNC_STATUS_CN:
11591 		lpfc_sli_abts_err_handler(phba, iocbq);
11592 		break;
11593 	default:
11594 		iocb_w = (uint32_t *) icmd;
11595 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11596 			"0346 Ring %d handler: unexpected ASYNC_STATUS"
11597 			" evt_code 0x%x\n"
11598 			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
11599 			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
11600 			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
11601 			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
11602 			pring->ringno, icmd->un.asyncstat.evt_code,
11603 			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11604 			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11605 			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11606 			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11607 
11608 		break;
11609 	}
11610 }
11611 
11612 
11613 /**
11614  * lpfc_sli4_setup - SLI ring setup function
11615  * @phba: Pointer to HBA context object.
11616  *
11617  * lpfc_sli_setup sets up rings of the SLI interface with
11618  * number of iocbs per ring and iotags. This function is
11619  * called while driver attach to the HBA and before the
11620  * interrupts are enabled. So there is no need for locking.
11621  *
11622  * This function always returns 0.
11623  **/
11624 int
lpfc_sli4_setup(struct lpfc_hba * phba)11625 lpfc_sli4_setup(struct lpfc_hba *phba)
11626 {
11627 	struct lpfc_sli_ring *pring;
11628 
11629 	pring = phba->sli4_hba.els_wq->pring;
11630 	pring->num_mask = LPFC_MAX_RING_MASK;
11631 	pring->prt[0].profile = 0;	/* Mask 0 */
11632 	pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11633 	pring->prt[0].type = FC_TYPE_ELS;
11634 	pring->prt[0].lpfc_sli_rcv_unsol_event =
11635 	    lpfc_els_unsol_event;
11636 	pring->prt[1].profile = 0;	/* Mask 1 */
11637 	pring->prt[1].rctl = FC_RCTL_ELS_REP;
11638 	pring->prt[1].type = FC_TYPE_ELS;
11639 	pring->prt[1].lpfc_sli_rcv_unsol_event =
11640 	    lpfc_els_unsol_event;
11641 	pring->prt[2].profile = 0;	/* Mask 2 */
11642 	/* NameServer Inquiry */
11643 	pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11644 	/* NameServer */
11645 	pring->prt[2].type = FC_TYPE_CT;
11646 	pring->prt[2].lpfc_sli_rcv_unsol_event =
11647 	    lpfc_ct_unsol_event;
11648 	pring->prt[3].profile = 0;	/* Mask 3 */
11649 	/* NameServer response */
11650 	pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11651 	/* NameServer */
11652 	pring->prt[3].type = FC_TYPE_CT;
11653 	pring->prt[3].lpfc_sli_rcv_unsol_event =
11654 	    lpfc_ct_unsol_event;
11655 	return 0;
11656 }
11657 
11658 /**
11659  * lpfc_sli_setup - SLI ring setup function
11660  * @phba: Pointer to HBA context object.
11661  *
11662  * lpfc_sli_setup sets up rings of the SLI interface with
11663  * number of iocbs per ring and iotags. This function is
11664  * called while driver attach to the HBA and before the
11665  * interrupts are enabled. So there is no need for locking.
11666  *
11667  * This function always returns 0. SLI3 only.
11668  **/
11669 int
lpfc_sli_setup(struct lpfc_hba * phba)11670 lpfc_sli_setup(struct lpfc_hba *phba)
11671 {
11672 	int i, totiocbsize = 0;
11673 	struct lpfc_sli *psli = &phba->sli;
11674 	struct lpfc_sli_ring *pring;
11675 
11676 	psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
11677 	psli->sli_flag = 0;
11678 
11679 	psli->iocbq_lookup = NULL;
11680 	psli->iocbq_lookup_len = 0;
11681 	psli->last_iotag = 0;
11682 
11683 	for (i = 0; i < psli->num_rings; i++) {
11684 		pring = &psli->sli3_ring[i];
11685 		switch (i) {
11686 		case LPFC_FCP_RING:	/* ring 0 - FCP */
11687 			/* numCiocb and numRiocb are used in config_port */
11688 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11689 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11690 			pring->sli.sli3.numCiocb +=
11691 				SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11692 			pring->sli.sli3.numRiocb +=
11693 				SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11694 			pring->sli.sli3.numCiocb +=
11695 				SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11696 			pring->sli.sli3.numRiocb +=
11697 				SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11698 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11699 							SLI3_IOCB_CMD_SIZE :
11700 							SLI2_IOCB_CMD_SIZE;
11701 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11702 							SLI3_IOCB_RSP_SIZE :
11703 							SLI2_IOCB_RSP_SIZE;
11704 			pring->iotag_ctr = 0;
11705 			pring->iotag_max =
11706 			    (phba->cfg_hba_queue_depth * 2);
11707 			pring->fast_iotag = pring->iotag_max;
11708 			pring->num_mask = 0;
11709 			break;
11710 		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
11711 			/* numCiocb and numRiocb are used in config_port */
11712 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11713 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11714 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11715 							SLI3_IOCB_CMD_SIZE :
11716 							SLI2_IOCB_CMD_SIZE;
11717 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11718 							SLI3_IOCB_RSP_SIZE :
11719 							SLI2_IOCB_RSP_SIZE;
11720 			pring->iotag_max = phba->cfg_hba_queue_depth;
11721 			pring->num_mask = 0;
11722 			break;
11723 		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
11724 			/* numCiocb and numRiocb are used in config_port */
11725 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11726 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11727 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11728 							SLI3_IOCB_CMD_SIZE :
11729 							SLI2_IOCB_CMD_SIZE;
11730 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11731 							SLI3_IOCB_RSP_SIZE :
11732 							SLI2_IOCB_RSP_SIZE;
11733 			pring->fast_iotag = 0;
11734 			pring->iotag_ctr = 0;
11735 			pring->iotag_max = 4096;
11736 			pring->lpfc_sli_rcv_async_status =
11737 				lpfc_sli_async_event_handler;
11738 			pring->num_mask = LPFC_MAX_RING_MASK;
11739 			pring->prt[0].profile = 0;	/* Mask 0 */
11740 			pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11741 			pring->prt[0].type = FC_TYPE_ELS;
11742 			pring->prt[0].lpfc_sli_rcv_unsol_event =
11743 			    lpfc_els_unsol_event;
11744 			pring->prt[1].profile = 0;	/* Mask 1 */
11745 			pring->prt[1].rctl = FC_RCTL_ELS_REP;
11746 			pring->prt[1].type = FC_TYPE_ELS;
11747 			pring->prt[1].lpfc_sli_rcv_unsol_event =
11748 			    lpfc_els_unsol_event;
11749 			pring->prt[2].profile = 0;	/* Mask 2 */
11750 			/* NameServer Inquiry */
11751 			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11752 			/* NameServer */
11753 			pring->prt[2].type = FC_TYPE_CT;
11754 			pring->prt[2].lpfc_sli_rcv_unsol_event =
11755 			    lpfc_ct_unsol_event;
11756 			pring->prt[3].profile = 0;	/* Mask 3 */
11757 			/* NameServer response */
11758 			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11759 			/* NameServer */
11760 			pring->prt[3].type = FC_TYPE_CT;
11761 			pring->prt[3].lpfc_sli_rcv_unsol_event =
11762 			    lpfc_ct_unsol_event;
11763 			break;
11764 		}
11765 		totiocbsize += (pring->sli.sli3.numCiocb *
11766 			pring->sli.sli3.sizeCiocb) +
11767 			(pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
11768 	}
11769 	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
11770 		/* Too many cmd / rsp ring entries in SLI2 SLIM */
11771 		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11772 		       "SLI2 SLIM Data: x%x x%lx\n",
11773 		       phba->brd_no, totiocbsize,
11774 		       (unsigned long) MAX_SLIM_IOCB_SIZE);
11775 	}
11776 	if (phba->cfg_multi_ring_support == 2)
11777 		lpfc_extra_ring_setup(phba);
11778 
11779 	return 0;
11780 }
11781 
11782 /**
11783  * lpfc_sli4_queue_init - Queue initialization function
11784  * @phba: Pointer to HBA context object.
11785  *
11786  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
11787  * ring. This function also initializes ring indices of each ring.
11788  * This function is called during the initialization of the SLI
11789  * interface of an HBA.
11790  * This function is called with no lock held and always returns
11791  * 1.
11792  **/
11793 void
lpfc_sli4_queue_init(struct lpfc_hba * phba)11794 lpfc_sli4_queue_init(struct lpfc_hba *phba)
11795 {
11796 	struct lpfc_sli *psli;
11797 	struct lpfc_sli_ring *pring;
11798 	int i;
11799 
11800 	psli = &phba->sli;
11801 	spin_lock_irq(&phba->hbalock);
11802 	INIT_LIST_HEAD(&psli->mboxq);
11803 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11804 	/* Initialize list headers for txq and txcmplq as double linked lists */
11805 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
11806 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11807 		pring->flag = 0;
11808 		pring->ringno = LPFC_FCP_RING;
11809 		pring->txcmplq_cnt = 0;
11810 		INIT_LIST_HEAD(&pring->txq);
11811 		INIT_LIST_HEAD(&pring->txcmplq);
11812 		INIT_LIST_HEAD(&pring->iocb_continueq);
11813 		spin_lock_init(&pring->ring_lock);
11814 	}
11815 	pring = phba->sli4_hba.els_wq->pring;
11816 	pring->flag = 0;
11817 	pring->ringno = LPFC_ELS_RING;
11818 	pring->txcmplq_cnt = 0;
11819 	INIT_LIST_HEAD(&pring->txq);
11820 	INIT_LIST_HEAD(&pring->txcmplq);
11821 	INIT_LIST_HEAD(&pring->iocb_continueq);
11822 	spin_lock_init(&pring->ring_lock);
11823 
11824 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11825 		pring = phba->sli4_hba.nvmels_wq->pring;
11826 		pring->flag = 0;
11827 		pring->ringno = LPFC_ELS_RING;
11828 		pring->txcmplq_cnt = 0;
11829 		INIT_LIST_HEAD(&pring->txq);
11830 		INIT_LIST_HEAD(&pring->txcmplq);
11831 		INIT_LIST_HEAD(&pring->iocb_continueq);
11832 		spin_lock_init(&pring->ring_lock);
11833 	}
11834 
11835 	spin_unlock_irq(&phba->hbalock);
11836 }
11837 
11838 /**
11839  * lpfc_sli_queue_init - Queue initialization function
11840  * @phba: Pointer to HBA context object.
11841  *
11842  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11843  * ring. This function also initializes ring indices of each ring.
11844  * This function is called during the initialization of the SLI
11845  * interface of an HBA.
11846  * This function is called with no lock held and always returns
11847  * 1.
11848  **/
11849 void
lpfc_sli_queue_init(struct lpfc_hba * phba)11850 lpfc_sli_queue_init(struct lpfc_hba *phba)
11851 {
11852 	struct lpfc_sli *psli;
11853 	struct lpfc_sli_ring *pring;
11854 	int i;
11855 
11856 	psli = &phba->sli;
11857 	spin_lock_irq(&phba->hbalock);
11858 	INIT_LIST_HEAD(&psli->mboxq);
11859 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11860 	/* Initialize list headers for txq and txcmplq as double linked lists */
11861 	for (i = 0; i < psli->num_rings; i++) {
11862 		pring = &psli->sli3_ring[i];
11863 		pring->ringno = i;
11864 		pring->sli.sli3.next_cmdidx  = 0;
11865 		pring->sli.sli3.local_getidx = 0;
11866 		pring->sli.sli3.cmdidx = 0;
11867 		INIT_LIST_HEAD(&pring->iocb_continueq);
11868 		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11869 		INIT_LIST_HEAD(&pring->postbufq);
11870 		pring->flag = 0;
11871 		INIT_LIST_HEAD(&pring->txq);
11872 		INIT_LIST_HEAD(&pring->txcmplq);
11873 		spin_lock_init(&pring->ring_lock);
11874 	}
11875 	spin_unlock_irq(&phba->hbalock);
11876 }
11877 
11878 /**
11879  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11880  * @phba: Pointer to HBA context object.
11881  *
11882  * This routine flushes the mailbox command subsystem. It will unconditionally
11883  * flush all the mailbox commands in the three possible stages in the mailbox
11884  * command sub-system: pending mailbox command queue; the outstanding mailbox
11885  * command; and completed mailbox command queue. It is caller's responsibility
11886  * to make sure that the driver is in the proper state to flush the mailbox
11887  * command sub-system. Namely, the posting of mailbox commands into the
11888  * pending mailbox command queue from the various clients must be stopped;
11889  * either the HBA is in a state that it will never works on the outstanding
11890  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11891  * mailbox command has been completed.
11892  **/
11893 static void
lpfc_sli_mbox_sys_flush(struct lpfc_hba * phba)11894 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11895 {
11896 	LIST_HEAD(completions);
11897 	struct lpfc_sli *psli = &phba->sli;
11898 	LPFC_MBOXQ_t *pmb;
11899 	unsigned long iflag;
11900 
11901 	/* Disable softirqs, including timers from obtaining phba->hbalock */
11902 	local_bh_disable();
11903 
11904 	/* Flush all the mailbox commands in the mbox system */
11905 	spin_lock_irqsave(&phba->hbalock, iflag);
11906 
11907 	/* The pending mailbox command queue */
11908 	list_splice_init(&phba->sli.mboxq, &completions);
11909 	/* The outstanding active mailbox command */
11910 	if (psli->mbox_active) {
11911 		list_add_tail(&psli->mbox_active->list, &completions);
11912 		psli->mbox_active = NULL;
11913 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11914 	}
11915 	/* The completed mailbox command queue */
11916 	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11917 	spin_unlock_irqrestore(&phba->hbalock, iflag);
11918 
11919 	/* Enable softirqs again, done with phba->hbalock */
11920 	local_bh_enable();
11921 
11922 	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11923 	while (!list_empty(&completions)) {
11924 		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11925 		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11926 		if (pmb->mbox_cmpl)
11927 			pmb->mbox_cmpl(phba, pmb);
11928 	}
11929 }
11930 
11931 /**
11932  * lpfc_sli_host_down - Vport cleanup function
11933  * @vport: Pointer to virtual port object.
11934  *
11935  * lpfc_sli_host_down is called to clean up the resources
11936  * associated with a vport before destroying virtual
11937  * port data structures.
11938  * This function does following operations:
11939  * - Free discovery resources associated with this virtual
11940  *   port.
11941  * - Free iocbs associated with this virtual port in
11942  *   the txq.
11943  * - Send abort for all iocb commands associated with this
11944  *   vport in txcmplq.
11945  *
11946  * This function is called with no lock held and always returns 1.
11947  **/
11948 int
lpfc_sli_host_down(struct lpfc_vport * vport)11949 lpfc_sli_host_down(struct lpfc_vport *vport)
11950 {
11951 	LIST_HEAD(completions);
11952 	struct lpfc_hba *phba = vport->phba;
11953 	struct lpfc_sli *psli = &phba->sli;
11954 	struct lpfc_queue *qp = NULL;
11955 	struct lpfc_sli_ring *pring;
11956 	struct lpfc_iocbq *iocb, *next_iocb;
11957 	int i;
11958 	unsigned long flags = 0;
11959 	uint16_t prev_pring_flag;
11960 
11961 	lpfc_cleanup_discovery_resources(vport);
11962 
11963 	spin_lock_irqsave(&phba->hbalock, flags);
11964 
11965 	/*
11966 	 * Error everything on the txq since these iocbs
11967 	 * have not been given to the FW yet.
11968 	 * Also issue ABTS for everything on the txcmplq
11969 	 */
11970 	if (phba->sli_rev != LPFC_SLI_REV4) {
11971 		for (i = 0; i < psli->num_rings; i++) {
11972 			pring = &psli->sli3_ring[i];
11973 			prev_pring_flag = pring->flag;
11974 			/* Only slow rings */
11975 			if (pring->ringno == LPFC_ELS_RING) {
11976 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11977 				/* Set the lpfc data pending flag */
11978 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11979 			}
11980 			list_for_each_entry_safe(iocb, next_iocb,
11981 						 &pring->txq, list) {
11982 				if (iocb->vport != vport)
11983 					continue;
11984 				list_move_tail(&iocb->list, &completions);
11985 			}
11986 			list_for_each_entry_safe(iocb, next_iocb,
11987 						 &pring->txcmplq, list) {
11988 				if (iocb->vport != vport)
11989 					continue;
11990 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11991 							   NULL);
11992 			}
11993 			pring->flag = prev_pring_flag;
11994 		}
11995 	} else {
11996 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11997 			pring = qp->pring;
11998 			if (!pring)
11999 				continue;
12000 			if (pring == phba->sli4_hba.els_wq->pring) {
12001 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12002 				/* Set the lpfc data pending flag */
12003 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12004 			}
12005 			prev_pring_flag = pring->flag;
12006 			spin_lock(&pring->ring_lock);
12007 			list_for_each_entry_safe(iocb, next_iocb,
12008 						 &pring->txq, list) {
12009 				if (iocb->vport != vport)
12010 					continue;
12011 				list_move_tail(&iocb->list, &completions);
12012 			}
12013 			spin_unlock(&pring->ring_lock);
12014 			list_for_each_entry_safe(iocb, next_iocb,
12015 						 &pring->txcmplq, list) {
12016 				if (iocb->vport != vport)
12017 					continue;
12018 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
12019 							   NULL);
12020 			}
12021 			pring->flag = prev_pring_flag;
12022 		}
12023 	}
12024 	spin_unlock_irqrestore(&phba->hbalock, flags);
12025 
12026 	/* Make sure HBA is alive */
12027 	lpfc_issue_hb_tmo(phba);
12028 
12029 	/* Cancel all the IOCBs from the completions list */
12030 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12031 			      IOERR_SLI_DOWN);
12032 	return 1;
12033 }
12034 
12035 /**
12036  * lpfc_sli_hba_down - Resource cleanup function for the HBA
12037  * @phba: Pointer to HBA context object.
12038  *
12039  * This function cleans up all iocb, buffers, mailbox commands
12040  * while shutting down the HBA. This function is called with no
12041  * lock held and always returns 1.
12042  * This function does the following to cleanup driver resources:
12043  * - Free discovery resources for each virtual port
12044  * - Cleanup any pending fabric iocbs
12045  * - Iterate through the iocb txq and free each entry
12046  *   in the list.
12047  * - Free up any buffer posted to the HBA
12048  * - Free mailbox commands in the mailbox queue.
12049  **/
12050 int
lpfc_sli_hba_down(struct lpfc_hba * phba)12051 lpfc_sli_hba_down(struct lpfc_hba *phba)
12052 {
12053 	LIST_HEAD(completions);
12054 	struct lpfc_sli *psli = &phba->sli;
12055 	struct lpfc_queue *qp = NULL;
12056 	struct lpfc_sli_ring *pring;
12057 	struct lpfc_dmabuf *buf_ptr;
12058 	unsigned long flags = 0;
12059 	int i;
12060 
12061 	/* Shutdown the mailbox command sub-system */
12062 	lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
12063 
12064 	lpfc_hba_down_prep(phba);
12065 
12066 	/* Disable softirqs, including timers from obtaining phba->hbalock */
12067 	local_bh_disable();
12068 
12069 	lpfc_fabric_abort_hba(phba);
12070 
12071 	spin_lock_irqsave(&phba->hbalock, flags);
12072 
12073 	/*
12074 	 * Error everything on the txq since these iocbs
12075 	 * have not been given to the FW yet.
12076 	 */
12077 	if (phba->sli_rev != LPFC_SLI_REV4) {
12078 		for (i = 0; i < psli->num_rings; i++) {
12079 			pring = &psli->sli3_ring[i];
12080 			/* Only slow rings */
12081 			if (pring->ringno == LPFC_ELS_RING) {
12082 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12083 				/* Set the lpfc data pending flag */
12084 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12085 			}
12086 			list_splice_init(&pring->txq, &completions);
12087 		}
12088 	} else {
12089 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12090 			pring = qp->pring;
12091 			if (!pring)
12092 				continue;
12093 			spin_lock(&pring->ring_lock);
12094 			list_splice_init(&pring->txq, &completions);
12095 			spin_unlock(&pring->ring_lock);
12096 			if (pring == phba->sli4_hba.els_wq->pring) {
12097 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12098 				/* Set the lpfc data pending flag */
12099 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12100 			}
12101 		}
12102 	}
12103 	spin_unlock_irqrestore(&phba->hbalock, flags);
12104 
12105 	/* Cancel all the IOCBs from the completions list */
12106 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12107 			      IOERR_SLI_DOWN);
12108 
12109 	spin_lock_irqsave(&phba->hbalock, flags);
12110 	list_splice_init(&phba->elsbuf, &completions);
12111 	phba->elsbuf_cnt = 0;
12112 	phba->elsbuf_prev_cnt = 0;
12113 	spin_unlock_irqrestore(&phba->hbalock, flags);
12114 
12115 	while (!list_empty(&completions)) {
12116 		list_remove_head(&completions, buf_ptr,
12117 			struct lpfc_dmabuf, list);
12118 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12119 		kfree(buf_ptr);
12120 	}
12121 
12122 	/* Enable softirqs again, done with phba->hbalock */
12123 	local_bh_enable();
12124 
12125 	/* Return any active mbox cmds */
12126 	timer_delete_sync(&psli->mbox_tmo);
12127 
12128 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
12129 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12130 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
12131 
12132 	return 1;
12133 }
12134 
12135 /**
12136  * lpfc_sli_pcimem_bcopy - SLI memory copy function
12137  * @srcp: Source memory pointer.
12138  * @destp: Destination memory pointer.
12139  * @cnt: Number of words required to be copied.
12140  *
12141  * This function is used for copying data between driver memory
12142  * and the SLI memory. This function also changes the endianness
12143  * of each word if native endianness is different from SLI
12144  * endianness. This function can be called with or without
12145  * lock.
12146  **/
12147 void
lpfc_sli_pcimem_bcopy(void * srcp,void * destp,uint32_t cnt)12148 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12149 {
12150 	uint32_t *src = srcp;
12151 	uint32_t *dest = destp;
12152 	uint32_t ldata;
12153 	int i;
12154 
12155 	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12156 		ldata = *src;
12157 		ldata = le32_to_cpu(ldata);
12158 		*dest = ldata;
12159 		src++;
12160 		dest++;
12161 	}
12162 }
12163 
12164 
12165 /**
12166  * lpfc_sli_bemem_bcopy - SLI memory copy function
12167  * @srcp: Source memory pointer.
12168  * @destp: Destination memory pointer.
12169  * @cnt: Number of words required to be copied.
12170  *
12171  * This function is used for copying data between a data structure
12172  * with big endian representation to local endianness.
12173  * This function can be called with or without lock.
12174  **/
12175 void
lpfc_sli_bemem_bcopy(void * srcp,void * destp,uint32_t cnt)12176 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12177 {
12178 	uint32_t *src = srcp;
12179 	uint32_t *dest = destp;
12180 	uint32_t ldata;
12181 	int i;
12182 
12183 	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12184 		ldata = *src;
12185 		ldata = be32_to_cpu(ldata);
12186 		*dest = ldata;
12187 		src++;
12188 		dest++;
12189 	}
12190 }
12191 
12192 /**
12193  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
12194  * @phba: Pointer to HBA context object.
12195  * @pring: Pointer to driver SLI ring object.
12196  * @mp: Pointer to driver buffer object.
12197  *
12198  * This function is called with no lock held.
12199  * It always return zero after adding the buffer to the postbufq
12200  * buffer list.
12201  **/
12202 int
lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_dmabuf * mp)12203 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12204 			 struct lpfc_dmabuf *mp)
12205 {
12206 	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12207 	   later */
12208 	spin_lock_irq(&phba->hbalock);
12209 	list_add_tail(&mp->list, &pring->postbufq);
12210 	pring->postbufq_cnt++;
12211 	spin_unlock_irq(&phba->hbalock);
12212 	return 0;
12213 }
12214 
12215 /**
12216  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
12217  * @phba: Pointer to HBA context object.
12218  *
12219  * When HBQ is enabled, buffers are searched based on tags. This function
12220  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12221  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12222  * does not conflict with tags of buffer posted for unsolicited events.
12223  * The function returns the allocated tag. The function is called with
12224  * no locks held.
12225  **/
12226 uint32_t
lpfc_sli_get_buffer_tag(struct lpfc_hba * phba)12227 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12228 {
12229 	spin_lock_irq(&phba->hbalock);
12230 	phba->buffer_tag_count++;
12231 	/*
12232 	 * Always set the QUE_BUFTAG_BIT to distiguish between
12233 	 * a tag assigned by HBQ.
12234 	 */
12235 	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12236 	spin_unlock_irq(&phba->hbalock);
12237 	return phba->buffer_tag_count;
12238 }
12239 
12240 /**
12241  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
12242  * @phba: Pointer to HBA context object.
12243  * @pring: Pointer to driver SLI ring object.
12244  * @tag: Buffer tag.
12245  *
12246  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12247  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12248  * iocb is posted to the response ring with the tag of the buffer.
12249  * This function searches the pring->postbufq list using the tag
12250  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12251  * iocb. If the buffer is found then lpfc_dmabuf object of the
12252  * buffer is returned to the caller else NULL is returned.
12253  * This function is called with no lock held.
12254  **/
12255 struct lpfc_dmabuf *
lpfc_sli_ring_taggedbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)12256 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12257 			uint32_t tag)
12258 {
12259 	struct lpfc_dmabuf *mp, *next_mp;
12260 	struct list_head *slp = &pring->postbufq;
12261 
12262 	/* Search postbufq, from the beginning, looking for a match on tag */
12263 	spin_lock_irq(&phba->hbalock);
12264 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12265 		if (mp->buffer_tag == tag) {
12266 			list_del_init(&mp->list);
12267 			pring->postbufq_cnt--;
12268 			spin_unlock_irq(&phba->hbalock);
12269 			return mp;
12270 		}
12271 	}
12272 
12273 	spin_unlock_irq(&phba->hbalock);
12274 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12275 			"0402 Cannot find virtual addr for buffer tag on "
12276 			"ring %d Data x%lx x%px x%px x%x\n",
12277 			pring->ringno, (unsigned long) tag,
12278 			slp->next, slp->prev, pring->postbufq_cnt);
12279 
12280 	return NULL;
12281 }
12282 
12283 /**
12284  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
12285  * @phba: Pointer to HBA context object.
12286  * @pring: Pointer to driver SLI ring object.
12287  * @phys: DMA address of the buffer.
12288  *
12289  * This function searches the buffer list using the dma_address
12290  * of unsolicited event to find the driver's lpfc_dmabuf object
12291  * corresponding to the dma_address. The function returns the
12292  * lpfc_dmabuf object if a buffer is found else it returns NULL.
12293  * This function is called by the ct and els unsolicited event
12294  * handlers to get the buffer associated with the unsolicited
12295  * event.
12296  *
12297  * This function is called with no lock held.
12298  **/
12299 struct lpfc_dmabuf *
lpfc_sli_ringpostbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,dma_addr_t phys)12300 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12301 			 dma_addr_t phys)
12302 {
12303 	struct lpfc_dmabuf *mp, *next_mp;
12304 	struct list_head *slp = &pring->postbufq;
12305 
12306 	/* Search postbufq, from the beginning, looking for a match on phys */
12307 	spin_lock_irq(&phba->hbalock);
12308 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12309 		if (mp->phys == phys) {
12310 			list_del_init(&mp->list);
12311 			pring->postbufq_cnt--;
12312 			spin_unlock_irq(&phba->hbalock);
12313 			return mp;
12314 		}
12315 	}
12316 
12317 	spin_unlock_irq(&phba->hbalock);
12318 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12319 			"0410 Cannot find virtual addr for mapped buf on "
12320 			"ring %d Data x%llx x%px x%px x%x\n",
12321 			pring->ringno, (unsigned long long)phys,
12322 			slp->next, slp->prev, pring->postbufq_cnt);
12323 	return NULL;
12324 }
12325 
12326 /**
12327  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
12328  * @phba: Pointer to HBA context object.
12329  * @cmdiocb: Pointer to driver command iocb object.
12330  * @rspiocb: Pointer to driver response iocb object.
12331  *
12332  * This function is the completion handler for the abort iocbs for
12333  * ELS commands. This function is called from the ELS ring event
12334  * handler with no lock held. This function frees memory resources
12335  * associated with the abort iocb.
12336  **/
12337 static void
lpfc_sli_abort_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12338 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12339 			struct lpfc_iocbq *rspiocb)
12340 {
12341 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12342 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12343 	u8 cmnd = get_job_cmnd(phba, cmdiocb);
12344 
12345 	if (ulp_status) {
12346 		/*
12347 		 * Assume that the port already completed and returned, or
12348 		 * will return the iocb. Just Log the message.
12349 		 */
12350 		if (phba->sli_rev < LPFC_SLI_REV4) {
12351 			if (cmnd == CMD_ABORT_XRI_CX &&
12352 			    ulp_status == IOSTAT_LOCAL_REJECT &&
12353 			    ulp_word4 == IOERR_ABORT_REQUESTED) {
12354 				goto release_iocb;
12355 			}
12356 		}
12357 	}
12358 
12359 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
12360 			"0327 Abort els iocb complete x%px with io cmd xri %x "
12361 			"abort tag x%x abort status %x abort code %x\n",
12362 			cmdiocb, get_job_abtsiotag(phba, cmdiocb),
12363 			(phba->sli_rev == LPFC_SLI_REV4) ?
12364 			get_wqe_reqtag(cmdiocb) :
12365 			cmdiocb->iocb.ulpIoTag,
12366 			ulp_status, ulp_word4);
12367 release_iocb:
12368 	lpfc_sli_release_iocbq(phba, cmdiocb);
12369 	return;
12370 }
12371 
12372 /**
12373  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
12374  * @phba: Pointer to HBA context object.
12375  * @cmdiocb: Pointer to driver command iocb object.
12376  * @rspiocb: Pointer to driver response iocb object.
12377  *
12378  * The function is called from SLI ring event handler with no
12379  * lock held. This function is the completion handler for ELS commands
12380  * which are aborted. The function frees memory resources used for
12381  * the aborted ELS commands.
12382  **/
12383 void
lpfc_ignore_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12384 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12385 		     struct lpfc_iocbq *rspiocb)
12386 {
12387 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12388 	IOCB_t *irsp;
12389 	LPFC_MBOXQ_t *mbox;
12390 	u32 ulp_command, ulp_status, ulp_word4, iotag;
12391 
12392 	ulp_command = get_job_cmnd(phba, cmdiocb);
12393 	ulp_status = get_job_ulpstatus(phba, rspiocb);
12394 	ulp_word4 = get_job_word4(phba, rspiocb);
12395 
12396 	if (phba->sli_rev == LPFC_SLI_REV4) {
12397 		iotag = get_wqe_reqtag(cmdiocb);
12398 	} else {
12399 		irsp = &rspiocb->iocb;
12400 		iotag = irsp->ulpIoTag;
12401 
12402 		/* It is possible a PLOGI_RJT for NPIV ports to get aborted.
12403 		 * The MBX_REG_LOGIN64 mbox command is freed back to the
12404 		 * mbox_mem_pool here.
12405 		 */
12406 		if (cmdiocb->context_un.mbox) {
12407 			mbox = cmdiocb->context_un.mbox;
12408 			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
12409 			cmdiocb->context_un.mbox = NULL;
12410 		}
12411 	}
12412 
12413 	/* ELS cmd tag <ulpIoTag> completes */
12414 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12415 			"0139 Ignoring ELS cmd code x%x ref cnt x%x Data: "
12416 			"x%x x%x x%x x%px\n",
12417 			ulp_command, kref_read(&cmdiocb->ndlp->kref),
12418 			ulp_status, ulp_word4, iotag, cmdiocb->ndlp);
12419 	/*
12420 	 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12421 	 * if exchange is busy.
12422 	 */
12423 	if (ulp_command == CMD_GEN_REQUEST64_CR)
12424 		lpfc_ct_free_iocb(phba, cmdiocb);
12425 	else
12426 		lpfc_els_free_iocb(phba, cmdiocb);
12427 
12428 	lpfc_nlp_put(ndlp);
12429 }
12430 
12431 /**
12432  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
12433  * @phba: Pointer to HBA context object.
12434  * @pring: Pointer to driver SLI ring object.
12435  * @cmdiocb: Pointer to driver command iocb object.
12436  * @cmpl: completion function.
12437  *
12438  * This function issues an abort iocb for the provided command iocb. In case
12439  * of unloading, the abort iocb will not be issued to commands on the ELS
12440  * ring. Instead, the callback function shall be changed to those commands
12441  * so that nothing happens when them finishes. This function is called with
12442  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12443  * when the command iocb is an abort request.
12444  *
12445  **/
12446 int
lpfc_sli_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * cmdiocb,void * cmpl)12447 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12448 			   struct lpfc_iocbq *cmdiocb, void *cmpl)
12449 {
12450 	struct lpfc_vport *vport = cmdiocb->vport;
12451 	struct lpfc_iocbq *abtsiocbp;
12452 	int retval = IOCB_ERROR;
12453 	unsigned long iflags;
12454 	struct lpfc_nodelist *ndlp = NULL;
12455 	u32 ulp_command = get_job_cmnd(phba, cmdiocb);
12456 	u16 ulp_context, iotag;
12457 	bool ia;
12458 
12459 	/*
12460 	 * There are certain command types we don't want to abort.  And we
12461 	 * don't want to abort commands that are already in the process of
12462 	 * being aborted.
12463 	 */
12464 	if (ulp_command == CMD_ABORT_XRI_WQE ||
12465 	    ulp_command == CMD_ABORT_XRI_CN ||
12466 	    ulp_command == CMD_CLOSE_XRI_CN ||
12467 	    cmdiocb->cmd_flag & LPFC_DRIVER_ABORTED)
12468 		return IOCB_ABORTING;
12469 
12470 	if (!pring) {
12471 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12472 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12473 		else
12474 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12475 		return retval;
12476 	}
12477 
12478 	/*
12479 	 * Always abort the outstanding WQE and set the IA bit correctly
12480 	 * for the context.  This is necessary for correctly removing
12481 	 * outstanding ndlp reference counts when the CQE completes with
12482 	 * the XB bit set.
12483 	 */
12484 	abtsiocbp = __lpfc_sli_get_iocbq(phba);
12485 	if (abtsiocbp == NULL)
12486 		return IOCB_NORESOURCE;
12487 
12488 	/* This signals the response to set the correct status
12489 	 * before calling the completion handler
12490 	 */
12491 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
12492 
12493 	if (phba->sli_rev == LPFC_SLI_REV4) {
12494 		ulp_context = cmdiocb->sli4_xritag;
12495 		iotag = abtsiocbp->iotag;
12496 	} else {
12497 		iotag = cmdiocb->iocb.ulpIoTag;
12498 		if (pring->ringno == LPFC_ELS_RING) {
12499 			ndlp = cmdiocb->ndlp;
12500 			ulp_context = ndlp->nlp_rpi;
12501 		} else {
12502 			ulp_context = cmdiocb->iocb.ulpContext;
12503 		}
12504 	}
12505 
12506 	/* Just close the exchange under certain conditions. */
12507 	if (test_bit(FC_UNLOADING, &vport->load_flag) ||
12508 	    phba->link_state < LPFC_LINK_UP ||
12509 	    (phba->sli_rev == LPFC_SLI_REV4 &&
12510 	     phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN) ||
12511 	    (phba->link_flag & LS_EXTERNAL_LOOPBACK))
12512 		ia = true;
12513 	else
12514 		ia = false;
12515 
12516 	lpfc_sli_prep_abort_xri(phba, abtsiocbp, ulp_context, iotag,
12517 				cmdiocb->iocb.ulpClass,
12518 				LPFC_WQE_CQ_ID_DEFAULT, ia, false);
12519 
12520 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12521 	abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
12522 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
12523 		abtsiocbp->cmd_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);
12524 
12525 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
12526 		abtsiocbp->cmd_flag |= LPFC_IO_FOF;
12527 
12528 	if (cmpl)
12529 		abtsiocbp->cmd_cmpl = cmpl;
12530 	else
12531 		abtsiocbp->cmd_cmpl = lpfc_sli_abort_els_cmpl;
12532 	abtsiocbp->vport = vport;
12533 
12534 	if (phba->sli_rev == LPFC_SLI_REV4) {
12535 		pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12536 		if (unlikely(pring == NULL))
12537 			goto abort_iotag_exit;
12538 		/* Note: both hbalock and ring_lock need to be set here */
12539 		spin_lock_irqsave(&pring->ring_lock, iflags);
12540 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12541 			abtsiocbp, 0);
12542 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
12543 	} else {
12544 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12545 			abtsiocbp, 0);
12546 	}
12547 
12548 abort_iotag_exit:
12549 
12550 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12551 			 "0339 Abort IO XRI x%x, Original iotag x%x, "
12552 			 "abort tag x%x Cmdjob : x%px Abortjob : x%px "
12553 			 "retval x%x : IA %d cmd_cmpl %ps\n",
12554 			 ulp_context, (phba->sli_rev == LPFC_SLI_REV4) ?
12555 			 cmdiocb->iotag : iotag, iotag, cmdiocb, abtsiocbp,
12556 			 retval, ia, abtsiocbp->cmd_cmpl);
12557 	if (retval) {
12558 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
12559 		__lpfc_sli_release_iocbq(phba, abtsiocbp);
12560 	}
12561 
12562 	/*
12563 	 * Caller to this routine should check for IOCB_ERROR
12564 	 * and handle it properly.  This routine no longer removes
12565 	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
12566 	 */
12567 	return retval;
12568 }
12569 
12570 /**
12571  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12572  * @phba: pointer to lpfc HBA data structure.
12573  *
12574  * This routine will abort all pending and outstanding iocbs to an HBA.
12575  **/
12576 void
lpfc_sli_hba_iocb_abort(struct lpfc_hba * phba)12577 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12578 {
12579 	struct lpfc_sli *psli = &phba->sli;
12580 	struct lpfc_sli_ring *pring;
12581 	struct lpfc_queue *qp = NULL;
12582 	int i;
12583 
12584 	if (phba->sli_rev != LPFC_SLI_REV4) {
12585 		for (i = 0; i < psli->num_rings; i++) {
12586 			pring = &psli->sli3_ring[i];
12587 			lpfc_sli_abort_iocb_ring(phba, pring);
12588 		}
12589 		return;
12590 	}
12591 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12592 		pring = qp->pring;
12593 		if (!pring)
12594 			continue;
12595 		lpfc_sli_abort_iocb_ring(phba, pring);
12596 	}
12597 }
12598 
12599 /**
12600  * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts
12601  * @iocbq: Pointer to iocb object.
12602  * @vport: Pointer to driver virtual port object.
12603  *
12604  * This function acts as an iocb filter for functions which abort FCP iocbs.
12605  *
12606  * Return values
12607  * -ENODEV, if a null iocb or vport ptr is encountered
12608  * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as
12609  *          driver already started the abort process, or is an abort iocb itself
12610  * 0, passes criteria for aborting the FCP I/O iocb
12611  **/
12612 static int
lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq * iocbq,struct lpfc_vport * vport)12613 lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,
12614 				     struct lpfc_vport *vport)
12615 {
12616 	u8 ulp_command;
12617 
12618 	/* No null ptr vports */
12619 	if (!iocbq || iocbq->vport != vport)
12620 		return -ENODEV;
12621 
12622 	/* iocb must be for FCP IO, already exists on the TX cmpl queue,
12623 	 * can't be premarked as driver aborted, nor be an ABORT iocb itself
12624 	 */
12625 	ulp_command = get_job_cmnd(vport->phba, iocbq);
12626 	if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12627 	    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ) ||
12628 	    (iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12629 	    (ulp_command == CMD_ABORT_XRI_CN ||
12630 	     ulp_command == CMD_CLOSE_XRI_CN ||
12631 	     ulp_command == CMD_ABORT_XRI_WQE))
12632 		return -EINVAL;
12633 
12634 	return 0;
12635 }
12636 
12637 /**
12638  * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target
12639  * @iocbq: Pointer to driver iocb object.
12640  * @vport: Pointer to driver virtual port object.
12641  * @tgt_id: SCSI ID of the target.
12642  * @lun_id: LUN ID of the scsi device.
12643  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12644  *
12645  * This function acts as an iocb filter for validating a lun/SCSI target/SCSI
12646  * host.
12647  *
12648  * It will return
12649  * 0 if the filtering criteria is met for the given iocb and will return
12650  * 1 if the filtering criteria is not met.
12651  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12652  * given iocb is for the SCSI device specified by vport, tgt_id and
12653  * lun_id parameter.
12654  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
12655  * given iocb is for the SCSI target specified by vport and tgt_id
12656  * parameters.
12657  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12658  * given iocb is for the SCSI host associated with the given vport.
12659  * This function is called with no locks held.
12660  **/
12661 static int
lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq * iocbq,struct lpfc_vport * vport,uint16_t tgt_id,uint64_t lun_id,lpfc_ctx_cmd ctx_cmd)12662 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12663 			   uint16_t tgt_id, uint64_t lun_id,
12664 			   lpfc_ctx_cmd ctx_cmd)
12665 {
12666 	struct lpfc_io_buf *lpfc_cmd;
12667 	int rc = 1;
12668 
12669 	lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12670 
12671 	if (lpfc_cmd->pCmd == NULL)
12672 		return rc;
12673 
12674 	switch (ctx_cmd) {
12675 	case LPFC_CTX_LUN:
12676 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12677 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12678 		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
12679 			rc = 0;
12680 		break;
12681 	case LPFC_CTX_TGT:
12682 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12683 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
12684 			rc = 0;
12685 		break;
12686 	case LPFC_CTX_HOST:
12687 		rc = 0;
12688 		break;
12689 	default:
12690 		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
12691 			__func__, ctx_cmd);
12692 		break;
12693 	}
12694 
12695 	return rc;
12696 }
12697 
12698 /**
12699  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
12700  * @vport: Pointer to virtual port.
12701  * @tgt_id: SCSI ID of the target.
12702  * @lun_id: LUN ID of the scsi device.
12703  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12704  *
12705  * This function returns number of FCP commands pending for the vport.
12706  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12707  * commands pending on the vport associated with SCSI device specified
12708  * by tgt_id and lun_id parameters.
12709  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12710  * commands pending on the vport associated with SCSI target specified
12711  * by tgt_id parameter.
12712  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12713  * commands pending on the vport.
12714  * This function returns the number of iocbs which satisfy the filter.
12715  * This function is called without any lock held.
12716  **/
12717 int
lpfc_sli_sum_iocb(struct lpfc_vport * vport,uint16_t tgt_id,uint64_t lun_id,lpfc_ctx_cmd ctx_cmd)12718 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12719 		  lpfc_ctx_cmd ctx_cmd)
12720 {
12721 	struct lpfc_hba *phba = vport->phba;
12722 	struct lpfc_iocbq *iocbq;
12723 	int sum, i;
12724 	unsigned long iflags;
12725 	u8 ulp_command;
12726 
12727 	spin_lock_irqsave(&phba->hbalock, iflags);
12728 	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12729 		iocbq = phba->sli.iocbq_lookup[i];
12730 
12731 		if (!iocbq || iocbq->vport != vport)
12732 			continue;
12733 		if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12734 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ))
12735 			continue;
12736 
12737 		/* Include counting outstanding aborts */
12738 		ulp_command = get_job_cmnd(phba, iocbq);
12739 		if (ulp_command == CMD_ABORT_XRI_CN ||
12740 		    ulp_command == CMD_CLOSE_XRI_CN ||
12741 		    ulp_command == CMD_ABORT_XRI_WQE) {
12742 			sum++;
12743 			continue;
12744 		}
12745 
12746 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12747 					       ctx_cmd) == 0)
12748 			sum++;
12749 	}
12750 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12751 
12752 	return sum;
12753 }
12754 
12755 /**
12756  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12757  * @phba: Pointer to HBA context object
12758  * @cmdiocb: Pointer to command iocb object.
12759  * @rspiocb: Pointer to response iocb object.
12760  *
12761  * This function is called when an aborted FCP iocb completes. This
12762  * function is called by the ring event handler with no lock held.
12763  * This function frees the iocb.
12764  **/
12765 void
lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12766 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12767 			struct lpfc_iocbq *rspiocb)
12768 {
12769 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12770 			"3096 ABORT_XRI_CX completing on rpi x%x "
12771 			"original iotag x%x, abort cmd iotag x%x "
12772 			"status 0x%x, reason 0x%x\n",
12773 			(phba->sli_rev == LPFC_SLI_REV4) ?
12774 			cmdiocb->sli4_xritag :
12775 			cmdiocb->iocb.un.acxri.abortContextTag,
12776 			get_job_abtsiotag(phba, cmdiocb),
12777 			cmdiocb->iotag, get_job_ulpstatus(phba, rspiocb),
12778 			get_job_word4(phba, rspiocb));
12779 	lpfc_sli_release_iocbq(phba, cmdiocb);
12780 	return;
12781 }
12782 
12783 /**
12784  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
12785  * @vport: Pointer to virtual port.
12786  * @tgt_id: SCSI ID of the target.
12787  * @lun_id: LUN ID of the scsi device.
12788  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12789  *
12790  * This function sends an abort command for every SCSI command
12791  * associated with the given virtual port pending on the ring
12792  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12793  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12794  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12795  * followed by lpfc_sli_validate_fcp_iocb.
12796  *
12797  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12798  * FCP iocbs associated with lun specified by tgt_id and lun_id
12799  * parameters
12800  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12801  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12802  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12803  * FCP iocbs associated with virtual port.
12804  * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12805  * lpfc_sli4_calc_ring is used.
12806  * This function returns number of iocbs it failed to abort.
12807  * This function is called with no locks held.
12808  **/
12809 int
lpfc_sli_abort_iocb(struct lpfc_vport * vport,u16 tgt_id,u64 lun_id,lpfc_ctx_cmd abort_cmd)12810 lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12811 		    lpfc_ctx_cmd abort_cmd)
12812 {
12813 	struct lpfc_hba *phba = vport->phba;
12814 	struct lpfc_sli_ring *pring = NULL;
12815 	struct lpfc_iocbq *iocbq;
12816 	int errcnt = 0, ret_val = 0;
12817 	unsigned long iflags;
12818 	int i;
12819 
12820 	/* all I/Os are in process of being flushed */
12821 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12822 		return errcnt;
12823 
12824 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12825 		iocbq = phba->sli.iocbq_lookup[i];
12826 
12827 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12828 			continue;
12829 
12830 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12831 					       abort_cmd) != 0)
12832 			continue;
12833 
12834 		spin_lock_irqsave(&phba->hbalock, iflags);
12835 		if (phba->sli_rev == LPFC_SLI_REV3) {
12836 			pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12837 		} else if (phba->sli_rev == LPFC_SLI_REV4) {
12838 			pring = lpfc_sli4_calc_ring(phba, iocbq);
12839 		}
12840 		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
12841 						     lpfc_sli_abort_fcp_cmpl);
12842 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12843 		if (ret_val != IOCB_SUCCESS)
12844 			errcnt++;
12845 	}
12846 
12847 	return errcnt;
12848 }
12849 
12850 /**
12851  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12852  * @vport: Pointer to virtual port.
12853  * @pring: Pointer to driver SLI ring object.
12854  * @tgt_id: SCSI ID of the target.
12855  * @lun_id: LUN ID of the scsi device.
12856  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12857  *
12858  * This function sends an abort command for every SCSI command
12859  * associated with the given virtual port pending on the ring
12860  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12861  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12862  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12863  * followed by lpfc_sli_validate_fcp_iocb.
12864  *
12865  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12866  * FCP iocbs associated with lun specified by tgt_id and lun_id
12867  * parameters
12868  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12869  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12870  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12871  * FCP iocbs associated with virtual port.
12872  * This function returns number of iocbs it aborted .
12873  * This function is called with no locks held right after a taskmgmt
12874  * command is sent.
12875  **/
12876 int
lpfc_sli_abort_taskmgmt(struct lpfc_vport * vport,struct lpfc_sli_ring * pring,uint16_t tgt_id,uint64_t lun_id,lpfc_ctx_cmd cmd)12877 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12878 			uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12879 {
12880 	struct lpfc_hba *phba = vport->phba;
12881 	struct lpfc_io_buf *lpfc_cmd;
12882 	struct lpfc_iocbq *abtsiocbq;
12883 	struct lpfc_nodelist *ndlp = NULL;
12884 	struct lpfc_iocbq *iocbq;
12885 	int sum, i, ret_val;
12886 	unsigned long iflags;
12887 	struct lpfc_sli_ring *pring_s4 = NULL;
12888 	u16 ulp_context, iotag, cqid = LPFC_WQE_CQ_ID_DEFAULT;
12889 	bool ia;
12890 
12891 	/* all I/Os are in process of being flushed */
12892 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12893 		return 0;
12894 
12895 	sum = 0;
12896 
12897 	spin_lock_irqsave(&phba->hbalock, iflags);
12898 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12899 		iocbq = phba->sli.iocbq_lookup[i];
12900 
12901 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12902 			continue;
12903 
12904 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12905 					       cmd) != 0)
12906 			continue;
12907 
12908 		/* Guard against IO completion being called at same time */
12909 		lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12910 		spin_lock(&lpfc_cmd->buf_lock);
12911 
12912 		if (!lpfc_cmd->pCmd) {
12913 			spin_unlock(&lpfc_cmd->buf_lock);
12914 			continue;
12915 		}
12916 
12917 		if (phba->sli_rev == LPFC_SLI_REV4) {
12918 			pring_s4 =
12919 			    phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12920 			if (!pring_s4) {
12921 				spin_unlock(&lpfc_cmd->buf_lock);
12922 				continue;
12923 			}
12924 			/* Note: both hbalock and ring_lock must be set here */
12925 			spin_lock(&pring_s4->ring_lock);
12926 		}
12927 
12928 		/*
12929 		 * If the iocbq is already being aborted, don't take a second
12930 		 * action, but do count it.
12931 		 */
12932 		if ((iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12933 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
12934 			if (phba->sli_rev == LPFC_SLI_REV4)
12935 				spin_unlock(&pring_s4->ring_lock);
12936 			spin_unlock(&lpfc_cmd->buf_lock);
12937 			continue;
12938 		}
12939 
12940 		/* issue ABTS for this IOCB based on iotag */
12941 		abtsiocbq = __lpfc_sli_get_iocbq(phba);
12942 		if (!abtsiocbq) {
12943 			if (phba->sli_rev == LPFC_SLI_REV4)
12944 				spin_unlock(&pring_s4->ring_lock);
12945 			spin_unlock(&lpfc_cmd->buf_lock);
12946 			continue;
12947 		}
12948 
12949 		if (phba->sli_rev == LPFC_SLI_REV4) {
12950 			iotag = abtsiocbq->iotag;
12951 			ulp_context = iocbq->sli4_xritag;
12952 			cqid = lpfc_cmd->hdwq->io_cq_map;
12953 		} else {
12954 			iotag = iocbq->iocb.ulpIoTag;
12955 			if (pring->ringno == LPFC_ELS_RING) {
12956 				ndlp = iocbq->ndlp;
12957 				ulp_context = ndlp->nlp_rpi;
12958 			} else {
12959 				ulp_context = iocbq->iocb.ulpContext;
12960 			}
12961 		}
12962 
12963 		ndlp = lpfc_cmd->rdata->pnode;
12964 
12965 		if (lpfc_is_link_up(phba) &&
12966 		    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE) &&
12967 		    !(phba->link_flag & LS_EXTERNAL_LOOPBACK))
12968 			ia = false;
12969 		else
12970 			ia = true;
12971 
12972 		lpfc_sli_prep_abort_xri(phba, abtsiocbq, ulp_context, iotag,
12973 					iocbq->iocb.ulpClass, cqid,
12974 					ia, false);
12975 
12976 		abtsiocbq->vport = vport;
12977 
12978 		/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12979 		abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12980 		if (iocbq->cmd_flag & LPFC_IO_FCP)
12981 			abtsiocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
12982 		if (iocbq->cmd_flag & LPFC_IO_FOF)
12983 			abtsiocbq->cmd_flag |= LPFC_IO_FOF;
12984 
12985 		/* Setup callback routine and issue the command. */
12986 		abtsiocbq->cmd_cmpl = lpfc_sli_abort_fcp_cmpl;
12987 
12988 		/*
12989 		 * Indicate the IO is being aborted by the driver and set
12990 		 * the caller's flag into the aborted IO.
12991 		 */
12992 		iocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
12993 
12994 		if (phba->sli_rev == LPFC_SLI_REV4) {
12995 			ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12996 							abtsiocbq, 0);
12997 			spin_unlock(&pring_s4->ring_lock);
12998 		} else {
12999 			ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
13000 							abtsiocbq, 0);
13001 		}
13002 
13003 		spin_unlock(&lpfc_cmd->buf_lock);
13004 
13005 		if (ret_val == IOCB_ERROR)
13006 			__lpfc_sli_release_iocbq(phba, abtsiocbq);
13007 		else
13008 			sum++;
13009 	}
13010 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13011 	return sum;
13012 }
13013 
13014 /**
13015  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
13016  * @phba: Pointer to HBA context object.
13017  * @cmdiocbq: Pointer to command iocb.
13018  * @rspiocbq: Pointer to response iocb.
13019  *
13020  * This function is the completion handler for iocbs issued using
13021  * lpfc_sli_issue_iocb_wait function. This function is called by the
13022  * ring event handler function without any lock held. This function
13023  * can be called from both worker thread context and interrupt
13024  * context. This function also can be called from other thread which
13025  * cleans up the SLI layer objects.
13026  * This function copy the contents of the response iocb to the
13027  * response iocb memory object provided by the caller of
13028  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
13029  * sleeps for the iocb completion.
13030  **/
13031 static void
lpfc_sli_wake_iocb_wait(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_iocbq * rspiocbq)13032 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
13033 			struct lpfc_iocbq *cmdiocbq,
13034 			struct lpfc_iocbq *rspiocbq)
13035 {
13036 	wait_queue_head_t *pdone_q;
13037 	unsigned long iflags;
13038 	struct lpfc_io_buf *lpfc_cmd;
13039 	size_t offset = offsetof(struct lpfc_iocbq, wqe);
13040 
13041 	spin_lock_irqsave(&phba->hbalock, iflags);
13042 	if (cmdiocbq->cmd_flag & LPFC_IO_WAKE_TMO) {
13043 
13044 		/*
13045 		 * A time out has occurred for the iocb.  If a time out
13046 		 * completion handler has been supplied, call it.  Otherwise,
13047 		 * just free the iocbq.
13048 		 */
13049 
13050 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13051 		cmdiocbq->cmd_cmpl = cmdiocbq->wait_cmd_cmpl;
13052 		cmdiocbq->wait_cmd_cmpl = NULL;
13053 		if (cmdiocbq->cmd_cmpl)
13054 			cmdiocbq->cmd_cmpl(phba, cmdiocbq, NULL);
13055 		else
13056 			lpfc_sli_release_iocbq(phba, cmdiocbq);
13057 		return;
13058 	}
13059 
13060 	/* Copy the contents of the local rspiocb into the caller's buffer. */
13061 	cmdiocbq->cmd_flag |= LPFC_IO_WAKE;
13062 	if (cmdiocbq->rsp_iocb && rspiocbq)
13063 		memcpy((char *)cmdiocbq->rsp_iocb + offset,
13064 		       (char *)rspiocbq + offset, sizeof(*rspiocbq) - offset);
13065 
13066 	/* Set the exchange busy flag for task management commands */
13067 	if ((cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
13068 	    !(cmdiocbq->cmd_flag & LPFC_IO_LIBDFC)) {
13069 		lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
13070 					cur_iocbq);
13071 		if (rspiocbq && (rspiocbq->cmd_flag & LPFC_EXCHANGE_BUSY))
13072 			lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
13073 		else
13074 			lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
13075 	}
13076 
13077 	pdone_q = cmdiocbq->context_un.wait_queue;
13078 	if (pdone_q)
13079 		wake_up(pdone_q);
13080 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13081 	return;
13082 }
13083 
13084 /**
13085  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
13086  * @phba: Pointer to HBA context object..
13087  * @piocbq: Pointer to command iocb.
13088  * @flag: Flag to test.
13089  *
13090  * This routine grabs the hbalock and then test the cmd_flag to
13091  * see if the passed in flag is set.
13092  * Returns:
13093  * 1 if flag is set.
13094  * 0 if flag is not set.
13095  **/
13096 static int
lpfc_chk_iocb_flg(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq,uint32_t flag)13097 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
13098 		 struct lpfc_iocbq *piocbq, uint32_t flag)
13099 {
13100 	unsigned long iflags;
13101 	int ret;
13102 
13103 	spin_lock_irqsave(&phba->hbalock, iflags);
13104 	ret = piocbq->cmd_flag & flag;
13105 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13106 	return ret;
13107 
13108 }
13109 
13110 /**
13111  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
13112  * @phba: Pointer to HBA context object..
13113  * @ring_number: Ring number
13114  * @piocb: Pointer to command iocb.
13115  * @prspiocbq: Pointer to response iocb.
13116  * @timeout: Timeout in number of seconds.
13117  *
13118  * This function issues the iocb to firmware and waits for the
13119  * iocb to complete. The cmd_cmpl field of the shall be used
13120  * to handle iocbs which time out. If the field is NULL, the
13121  * function shall free the iocbq structure.  If more clean up is
13122  * needed, the caller is expected to provide a completion function
13123  * that will provide the needed clean up.  If the iocb command is
13124  * not completed within timeout seconds, the function will either
13125  * free the iocbq structure (if cmd_cmpl == NULL) or execute the
13126  * completion function set in the cmd_cmpl field and then return
13127  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
13128  * resources if this function returns IOCB_TIMEDOUT.
13129  * The function waits for the iocb completion using an
13130  * non-interruptible wait.
13131  * This function will sleep while waiting for iocb completion.
13132  * So, this function should not be called from any context which
13133  * does not allow sleeping. Due to the same reason, this function
13134  * cannot be called with interrupt disabled.
13135  * This function assumes that the iocb completions occur while
13136  * this function sleep. So, this function cannot be called from
13137  * the thread which process iocb completion for this ring.
13138  * This function clears the cmd_flag of the iocb object before
13139  * issuing the iocb and the iocb completion handler sets this
13140  * flag and wakes this thread when the iocb completes.
13141  * The contents of the response iocb will be copied to prspiocbq
13142  * by the completion handler when the command completes.
13143  * This function returns IOCB_SUCCESS when success.
13144  * This function is called with no lock held.
13145  **/
13146 int
lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,struct lpfc_iocbq * prspiocbq,uint32_t timeout)13147 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
13148 			 uint32_t ring_number,
13149 			 struct lpfc_iocbq *piocb,
13150 			 struct lpfc_iocbq *prspiocbq,
13151 			 uint32_t timeout)
13152 {
13153 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
13154 	long timeleft, timeout_req = 0;
13155 	int retval = IOCB_SUCCESS;
13156 	uint32_t creg_val;
13157 	struct lpfc_iocbq *iocb;
13158 	int txq_cnt = 0;
13159 	int txcmplq_cnt = 0;
13160 	struct lpfc_sli_ring *pring;
13161 	unsigned long iflags;
13162 	bool iocb_completed = true;
13163 
13164 	if (phba->sli_rev >= LPFC_SLI_REV4) {
13165 		lpfc_sli_prep_wqe(phba, piocb);
13166 
13167 		pring = lpfc_sli4_calc_ring(phba, piocb);
13168 	} else
13169 		pring = &phba->sli.sli3_ring[ring_number];
13170 	/*
13171 	 * If the caller has provided a response iocbq buffer, then rsp_iocb
13172 	 * is NULL or its an error.
13173 	 */
13174 	if (prspiocbq) {
13175 		if (piocb->rsp_iocb)
13176 			return IOCB_ERROR;
13177 		piocb->rsp_iocb = prspiocbq;
13178 	}
13179 
13180 	piocb->wait_cmd_cmpl = piocb->cmd_cmpl;
13181 	piocb->cmd_cmpl = lpfc_sli_wake_iocb_wait;
13182 	piocb->context_un.wait_queue = &done_q;
13183 	piocb->cmd_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
13184 
13185 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13186 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13187 			return IOCB_ERROR;
13188 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13189 		writel(creg_val, phba->HCregaddr);
13190 		readl(phba->HCregaddr); /* flush */
13191 	}
13192 
13193 	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13194 				     SLI_IOCB_RET_IOCB);
13195 	if (retval == IOCB_SUCCESS) {
13196 		timeout_req = secs_to_jiffies(timeout);
13197 		timeleft = wait_event_timeout(done_q,
13198 				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
13199 				timeout_req);
13200 		spin_lock_irqsave(&phba->hbalock, iflags);
13201 		if (!(piocb->cmd_flag & LPFC_IO_WAKE)) {
13202 
13203 			/*
13204 			 * IOCB timed out.  Inform the wake iocb wait
13205 			 * completion function and set local status
13206 			 */
13207 
13208 			iocb_completed = false;
13209 			piocb->cmd_flag |= LPFC_IO_WAKE_TMO;
13210 		}
13211 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13212 		if (iocb_completed) {
13213 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13214 					"0331 IOCB wake signaled\n");
13215 			/* Note: we are not indicating if the IOCB has a success
13216 			 * status or not - that's for the caller to check.
13217 			 * IOCB_SUCCESS means just that the command was sent and
13218 			 * completed. Not that it completed successfully.
13219 			 * */
13220 		} else if (timeleft == 0) {
13221 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13222 					"0338 IOCB wait timeout error - no "
13223 					"wake response Data x%x\n", timeout);
13224 			retval = IOCB_TIMEDOUT;
13225 		} else {
13226 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13227 					"0330 IOCB wake NOT set, "
13228 					"Data x%x x%lx\n",
13229 					timeout, (timeleft / jiffies));
13230 			retval = IOCB_TIMEDOUT;
13231 		}
13232 	} else if (retval == IOCB_BUSY) {
13233 		if (phba->cfg_log_verbose & LOG_SLI) {
13234 			list_for_each_entry(iocb, &pring->txq, list) {
13235 				txq_cnt++;
13236 			}
13237 			list_for_each_entry(iocb, &pring->txcmplq, list) {
13238 				txcmplq_cnt++;
13239 			}
13240 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13241 				"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13242 				phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13243 		}
13244 		return retval;
13245 	} else {
13246 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13247 				"0332 IOCB wait issue failed, Data x%x\n",
13248 				retval);
13249 		retval = IOCB_ERROR;
13250 	}
13251 
13252 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13253 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13254 			return IOCB_ERROR;
13255 		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13256 		writel(creg_val, phba->HCregaddr);
13257 		readl(phba->HCregaddr); /* flush */
13258 	}
13259 
13260 	if (prspiocbq)
13261 		piocb->rsp_iocb = NULL;
13262 
13263 	piocb->context_un.wait_queue = NULL;
13264 	piocb->cmd_cmpl = NULL;
13265 	return retval;
13266 }
13267 
13268 /**
13269  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
13270  * @phba: Pointer to HBA context object.
13271  * @pmboxq: Pointer to driver mailbox object.
13272  * @timeout: Timeout in number of seconds.
13273  *
13274  * This function issues the mailbox to firmware and waits for the
13275  * mailbox command to complete. If the mailbox command is not
13276  * completed within timeout seconds, it returns MBX_TIMEOUT.
13277  * The function waits for the mailbox completion using an
13278  * interruptible wait. If the thread is woken up due to a
13279  * signal, MBX_TIMEOUT error is returned to the caller. Caller
13280  * should not free the mailbox resources, if this function returns
13281  * MBX_TIMEOUT.
13282  * This function will sleep while waiting for mailbox completion.
13283  * So, this function should not be called from any context which
13284  * does not allow sleeping. Due to the same reason, this function
13285  * cannot be called with interrupt disabled.
13286  * This function assumes that the mailbox completion occurs while
13287  * this function sleep. So, this function cannot be called from
13288  * the worker thread which processes mailbox completion.
13289  * This function is called in the context of HBA management
13290  * applications.
13291  * This function returns MBX_SUCCESS when successful.
13292  * This function is called with no lock held.
13293  **/
13294 int
lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq,uint32_t timeout)13295 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
13296 			 uint32_t timeout)
13297 {
13298 	struct completion mbox_done;
13299 	int retval;
13300 	unsigned long flag;
13301 
13302 	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
13303 	/* setup wake call as IOCB callback */
13304 	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
13305 
13306 	/* setup ctx_u field to pass wait_queue pointer to wake function  */
13307 	init_completion(&mbox_done);
13308 	pmboxq->ctx_u.mbox_wait = &mbox_done;
13309 	/* now issue the command */
13310 	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
13311 	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
13312 		wait_for_completion_timeout(&mbox_done, secs_to_jiffies(timeout));
13313 
13314 		spin_lock_irqsave(&phba->hbalock, flag);
13315 		pmboxq->ctx_u.mbox_wait = NULL;
13316 		/*
13317 		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13318 		 * else do not free the resources.
13319 		 */
13320 		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
13321 			retval = MBX_SUCCESS;
13322 		} else {
13323 			retval = MBX_TIMEOUT;
13324 			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13325 		}
13326 		spin_unlock_irqrestore(&phba->hbalock, flag);
13327 	}
13328 	return retval;
13329 }
13330 
13331 /**
13332  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
13333  * @phba: Pointer to HBA context.
13334  * @mbx_action: Mailbox shutdown options.
13335  *
13336  * This function is called to shutdown the driver's mailbox sub-system.
13337  * It first marks the mailbox sub-system is in a block state to prevent
13338  * the asynchronous mailbox command from issued off the pending mailbox
13339  * command queue. If the mailbox command sub-system shutdown is due to
13340  * HBA error conditions such as EEH or ERATT, this routine shall invoke
13341  * the mailbox sub-system flush routine to forcefully bring down the
13342  * mailbox sub-system. Otherwise, if it is due to normal condition (such
13343  * as with offline or HBA function reset), this routine will wait for the
13344  * outstanding mailbox command to complete before invoking the mailbox
13345  * sub-system flush routine to gracefully bring down mailbox sub-system.
13346  **/
13347 void
lpfc_sli_mbox_sys_shutdown(struct lpfc_hba * phba,int mbx_action)13348 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
13349 {
13350 	struct lpfc_sli *psli = &phba->sli;
13351 	unsigned long timeout;
13352 
13353 	if (mbx_action == LPFC_MBX_NO_WAIT) {
13354 		/* delay 100ms for port state */
13355 		msleep(100);
13356 		lpfc_sli_mbox_sys_flush(phba);
13357 		return;
13358 	}
13359 	timeout = secs_to_jiffies(LPFC_MBOX_TMO) + jiffies;
13360 
13361 	/* Disable softirqs, including timers from obtaining phba->hbalock */
13362 	local_bh_disable();
13363 
13364 	spin_lock_irq(&phba->hbalock);
13365 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13366 
13367 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
13368 		/* Determine how long we might wait for the active mailbox
13369 		 * command to be gracefully completed by firmware.
13370 		 */
13371 		if (phba->sli.mbox_active)
13372 			timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
13373 						phba->sli.mbox_active)) + jiffies;
13374 		spin_unlock_irq(&phba->hbalock);
13375 
13376 		/* Enable softirqs again, done with phba->hbalock */
13377 		local_bh_enable();
13378 
13379 		while (phba->sli.mbox_active) {
13380 			/* Check active mailbox complete status every 2ms */
13381 			msleep(2);
13382 			if (time_after(jiffies, timeout))
13383 				/* Timeout, let the mailbox flush routine to
13384 				 * forcefully release active mailbox command
13385 				 */
13386 				break;
13387 		}
13388 	} else {
13389 		spin_unlock_irq(&phba->hbalock);
13390 
13391 		/* Enable softirqs again, done with phba->hbalock */
13392 		local_bh_enable();
13393 	}
13394 
13395 	lpfc_sli_mbox_sys_flush(phba);
13396 }
13397 
13398 /**
13399  * lpfc_sli_eratt_read - read sli-3 error attention events
13400  * @phba: Pointer to HBA context.
13401  *
13402  * This function is called to read the SLI3 device error attention registers
13403  * for possible error attention events. The caller must hold the hostlock
13404  * with spin_lock_irq().
13405  *
13406  * This function returns 1 when there is Error Attention in the Host Attention
13407  * Register and returns 0 otherwise.
13408  **/
13409 static int
lpfc_sli_eratt_read(struct lpfc_hba * phba)13410 lpfc_sli_eratt_read(struct lpfc_hba *phba)
13411 {
13412 	uint32_t ha_copy;
13413 
13414 	/* Read chip Host Attention (HA) register */
13415 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
13416 		goto unplug_err;
13417 
13418 	if (ha_copy & HA_ERATT) {
13419 		/* Read host status register to retrieve error event */
13420 		if (lpfc_sli_read_hs(phba))
13421 			goto unplug_err;
13422 
13423 		/* Check if there is a deferred error condition is active */
13424 		if ((HS_FFER1 & phba->work_hs) &&
13425 		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13426 		      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
13427 			set_bit(DEFER_ERATT, &phba->hba_flag);
13428 			/* Clear all interrupt enable conditions */
13429 			writel(0, phba->HCregaddr);
13430 			readl(phba->HCregaddr);
13431 		}
13432 
13433 		/* Set the driver HA work bitmap */
13434 		phba->work_ha |= HA_ERATT;
13435 		/* Indicate polling handles this ERATT */
13436 		set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13437 		return 1;
13438 	}
13439 	return 0;
13440 
13441 unplug_err:
13442 	/* Set the driver HS work bitmap */
13443 	phba->work_hs |= UNPLUG_ERR;
13444 	/* Set the driver HA work bitmap */
13445 	phba->work_ha |= HA_ERATT;
13446 	/* Indicate polling handles this ERATT */
13447 	set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13448 	return 1;
13449 }
13450 
13451 /**
13452  * lpfc_sli4_eratt_read - read sli-4 error attention events
13453  * @phba: Pointer to HBA context.
13454  *
13455  * This function is called to read the SLI4 device error attention registers
13456  * for possible error attention events. The caller must hold the hostlock
13457  * with spin_lock_irq().
13458  *
13459  * This function returns 1 when there is Error Attention in the Host Attention
13460  * Register and returns 0 otherwise.
13461  **/
13462 static int
lpfc_sli4_eratt_read(struct lpfc_hba * phba)13463 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13464 {
13465 	uint32_t uerr_sta_hi, uerr_sta_lo;
13466 	uint32_t if_type, portsmphr;
13467 	struct lpfc_register portstat_reg;
13468 	u32 logmask;
13469 
13470 	/*
13471 	 * For now, use the SLI4 device internal unrecoverable error
13472 	 * registers for error attention. This can be changed later.
13473 	 */
13474 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13475 	switch (if_type) {
13476 	case LPFC_SLI_INTF_IF_TYPE_0:
13477 		if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13478 			&uerr_sta_lo) ||
13479 			lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13480 			&uerr_sta_hi)) {
13481 			phba->work_hs |= UNPLUG_ERR;
13482 			phba->work_ha |= HA_ERATT;
13483 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13484 			return 1;
13485 		}
13486 		if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13487 		    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
13488 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13489 					"1423 HBA Unrecoverable error: "
13490 					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13491 					"ue_mask_lo_reg=0x%x, "
13492 					"ue_mask_hi_reg=0x%x\n",
13493 					uerr_sta_lo, uerr_sta_hi,
13494 					phba->sli4_hba.ue_mask_lo,
13495 					phba->sli4_hba.ue_mask_hi);
13496 			phba->work_status[0] = uerr_sta_lo;
13497 			phba->work_status[1] = uerr_sta_hi;
13498 			phba->work_ha |= HA_ERATT;
13499 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13500 			return 1;
13501 		}
13502 		break;
13503 	case LPFC_SLI_INTF_IF_TYPE_2:
13504 	case LPFC_SLI_INTF_IF_TYPE_6:
13505 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13506 			&portstat_reg.word0) ||
13507 			lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13508 			&portsmphr)){
13509 			phba->work_hs |= UNPLUG_ERR;
13510 			phba->work_ha |= HA_ERATT;
13511 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13512 			return 1;
13513 		}
13514 		if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13515 			phba->work_status[0] =
13516 				readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13517 			phba->work_status[1] =
13518 				readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
13519 			logmask = LOG_TRACE_EVENT;
13520 			if (phba->work_status[0] ==
13521 				SLIPORT_ERR1_REG_ERR_CODE_2 &&
13522 			    phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)
13523 				logmask = LOG_SLI;
13524 			lpfc_printf_log(phba, KERN_ERR, logmask,
13525 					"2885 Port Status Event: "
13526 					"port status reg 0x%x, "
13527 					"port smphr reg 0x%x, "
13528 					"error 1=0x%x, error 2=0x%x\n",
13529 					portstat_reg.word0,
13530 					portsmphr,
13531 					phba->work_status[0],
13532 					phba->work_status[1]);
13533 			phba->work_ha |= HA_ERATT;
13534 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13535 			return 1;
13536 		}
13537 		break;
13538 	case LPFC_SLI_INTF_IF_TYPE_1:
13539 	default:
13540 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13541 				"2886 HBA Error Attention on unsupported "
13542 				"if type %d.", if_type);
13543 		return 1;
13544 	}
13545 
13546 	return 0;
13547 }
13548 
13549 /**
13550  * lpfc_sli_check_eratt - check error attention events
13551  * @phba: Pointer to HBA context.
13552  *
13553  * This function is called from timer soft interrupt context to check HBA's
13554  * error attention register bit for error attention events.
13555  *
13556  * This function returns 1 when there is Error Attention in the Host Attention
13557  * Register and returns 0 otherwise.
13558  **/
13559 int
lpfc_sli_check_eratt(struct lpfc_hba * phba)13560 lpfc_sli_check_eratt(struct lpfc_hba *phba)
13561 {
13562 	uint32_t ha_copy;
13563 
13564 	/* If somebody is waiting to handle an eratt, don't process it
13565 	 * here. The brdkill function will do this.
13566 	 */
13567 	if (phba->link_flag & LS_IGNORE_ERATT)
13568 		return 0;
13569 
13570 	/* Check if interrupt handler handles this ERATT */
13571 	if (test_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
13572 		/* Interrupt handler has handled ERATT */
13573 		return 0;
13574 
13575 	/*
13576 	 * If there is deferred error attention, do not check for error
13577 	 * attention
13578 	 */
13579 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13580 		return 0;
13581 
13582 	spin_lock_irq(&phba->hbalock);
13583 	/* If PCI channel is offline, don't process it */
13584 	if (unlikely(pci_channel_offline(phba->pcidev))) {
13585 		spin_unlock_irq(&phba->hbalock);
13586 		return 0;
13587 	}
13588 
13589 	switch (phba->sli_rev) {
13590 	case LPFC_SLI_REV2:
13591 	case LPFC_SLI_REV3:
13592 		/* Read chip Host Attention (HA) register */
13593 		ha_copy = lpfc_sli_eratt_read(phba);
13594 		break;
13595 	case LPFC_SLI_REV4:
13596 		/* Read device Uncoverable Error (UERR) registers */
13597 		ha_copy = lpfc_sli4_eratt_read(phba);
13598 		break;
13599 	default:
13600 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13601 				"0299 Invalid SLI revision (%d)\n",
13602 				phba->sli_rev);
13603 		ha_copy = 0;
13604 		break;
13605 	}
13606 	spin_unlock_irq(&phba->hbalock);
13607 
13608 	return ha_copy;
13609 }
13610 
13611 /**
13612  * lpfc_intr_state_check - Check device state for interrupt handling
13613  * @phba: Pointer to HBA context.
13614  *
13615  * This inline routine checks whether a device or its PCI slot is in a state
13616  * that the interrupt should be handled.
13617  *
13618  * This function returns 0 if the device or the PCI slot is in a state that
13619  * interrupt should be handled, otherwise -EIO.
13620  */
13621 static inline int
lpfc_intr_state_check(struct lpfc_hba * phba)13622 lpfc_intr_state_check(struct lpfc_hba *phba)
13623 {
13624 	/* If the pci channel is offline, ignore all the interrupts */
13625 	if (unlikely(pci_channel_offline(phba->pcidev)))
13626 		return -EIO;
13627 
13628 	/* Update device level interrupt statistics */
13629 	phba->sli.slistat.sli_intr++;
13630 
13631 	/* Ignore all interrupts during initialization. */
13632 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13633 		return -EIO;
13634 
13635 	return 0;
13636 }
13637 
13638 /**
13639  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
13640  * @irq: Interrupt number.
13641  * @dev_id: The device context pointer.
13642  *
13643  * This function is directly called from the PCI layer as an interrupt
13644  * service routine when device with SLI-3 interface spec is enabled with
13645  * MSI-X multi-message interrupt mode and there are slow-path events in
13646  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13647  * interrupt mode, this function is called as part of the device-level
13648  * interrupt handler. When the PCI slot is in error recovery or the HBA
13649  * is undergoing initialization, the interrupt handler will not process
13650  * the interrupt. The link attention and ELS ring attention events are
13651  * handled by the worker thread. The interrupt handler signals the worker
13652  * thread and returns for these events. This function is called without
13653  * any lock held. It gets the hbalock to access and update SLI data
13654  * structures.
13655  *
13656  * This function returns IRQ_HANDLED when interrupt is handled else it
13657  * returns IRQ_NONE.
13658  **/
13659 irqreturn_t
lpfc_sli_sp_intr_handler(int irq,void * dev_id)13660 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
13661 {
13662 	struct lpfc_hba  *phba;
13663 	uint32_t ha_copy, hc_copy;
13664 	uint32_t work_ha_copy;
13665 	unsigned long status;
13666 	unsigned long iflag;
13667 	uint32_t control;
13668 
13669 	MAILBOX_t *mbox, *pmbox;
13670 	struct lpfc_vport *vport;
13671 	struct lpfc_nodelist *ndlp;
13672 	struct lpfc_dmabuf *mp;
13673 	LPFC_MBOXQ_t *pmb;
13674 	int rc;
13675 
13676 	/*
13677 	 * Get the driver's phba structure from the dev_id and
13678 	 * assume the HBA is not interrupting.
13679 	 */
13680 	phba = (struct lpfc_hba *)dev_id;
13681 
13682 	if (unlikely(!phba))
13683 		return IRQ_NONE;
13684 
13685 	/*
13686 	 * Stuff needs to be attented to when this function is invoked as an
13687 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13688 	 */
13689 	if (phba->intr_type == MSIX) {
13690 		/* Check device state for handling interrupt */
13691 		if (lpfc_intr_state_check(phba))
13692 			return IRQ_NONE;
13693 		/* Need to read HA REG for slow-path events */
13694 		spin_lock_irqsave(&phba->hbalock, iflag);
13695 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13696 			goto unplug_error;
13697 		/* If somebody is waiting to handle an eratt don't process it
13698 		 * here. The brdkill function will do this.
13699 		 */
13700 		if (phba->link_flag & LS_IGNORE_ERATT)
13701 			ha_copy &= ~HA_ERATT;
13702 		/* Check the need for handling ERATT in interrupt handler */
13703 		if (ha_copy & HA_ERATT) {
13704 			if (test_and_set_bit(HBA_ERATT_HANDLED,
13705 					     &phba->hba_flag))
13706 				/* ERATT polling has handled ERATT */
13707 				ha_copy &= ~HA_ERATT;
13708 		}
13709 
13710 		/*
13711 		 * If there is deferred error attention, do not check for any
13712 		 * interrupt.
13713 		 */
13714 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
13715 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13716 			return IRQ_NONE;
13717 		}
13718 
13719 		/* Clear up only attention source related to slow-path */
13720 		if (lpfc_readl(phba->HCregaddr, &hc_copy))
13721 			goto unplug_error;
13722 
13723 		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13724 			HC_LAINT_ENA | HC_ERINT_ENA),
13725 			phba->HCregaddr);
13726 		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13727 			phba->HAregaddr);
13728 		writel(hc_copy, phba->HCregaddr);
13729 		readl(phba->HAregaddr); /* flush */
13730 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13731 	} else
13732 		ha_copy = phba->ha_copy;
13733 
13734 	work_ha_copy = ha_copy & phba->work_ha_mask;
13735 
13736 	if (work_ha_copy) {
13737 		if (work_ha_copy & HA_LATT) {
13738 			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13739 				/*
13740 				 * Turn off Link Attention interrupts
13741 				 * until CLEAR_LA done
13742 				 */
13743 				spin_lock_irqsave(&phba->hbalock, iflag);
13744 				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
13745 				if (lpfc_readl(phba->HCregaddr, &control))
13746 					goto unplug_error;
13747 				control &= ~HC_LAINT_ENA;
13748 				writel(control, phba->HCregaddr);
13749 				readl(phba->HCregaddr); /* flush */
13750 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13751 			}
13752 			else
13753 				work_ha_copy &= ~HA_LATT;
13754 		}
13755 
13756 		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
13757 			/*
13758 			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13759 			 * the only slow ring.
13760 			 */
13761 			status = (work_ha_copy &
13762 				(HA_RXMASK  << (4*LPFC_ELS_RING)));
13763 			status >>= (4*LPFC_ELS_RING);
13764 			if (status & HA_RXMASK) {
13765 				spin_lock_irqsave(&phba->hbalock, iflag);
13766 				if (lpfc_readl(phba->HCregaddr, &control))
13767 					goto unplug_error;
13768 
13769 				lpfc_debugfs_slow_ring_trc(phba,
13770 				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
13771 				control, status,
13772 				(uint32_t)phba->sli.slistat.sli_intr);
13773 
13774 				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
13775 					lpfc_debugfs_slow_ring_trc(phba,
13776 						"ISR Disable ring:"
13777 						"pwork:x%x hawork:x%x wait:x%x",
13778 						phba->work_ha, work_ha_copy,
13779 						(uint32_t)((unsigned long)
13780 						&phba->work_waitq));
13781 
13782 					control &=
13783 					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
13784 					writel(control, phba->HCregaddr);
13785 					readl(phba->HCregaddr); /* flush */
13786 				}
13787 				else {
13788 					lpfc_debugfs_slow_ring_trc(phba,
13789 						"ISR slow ring:   pwork:"
13790 						"x%x hawork:x%x wait:x%x",
13791 						phba->work_ha, work_ha_copy,
13792 						(uint32_t)((unsigned long)
13793 						&phba->work_waitq));
13794 				}
13795 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13796 			}
13797 		}
13798 		spin_lock_irqsave(&phba->hbalock, iflag);
13799 		if (work_ha_copy & HA_ERATT) {
13800 			if (lpfc_sli_read_hs(phba))
13801 				goto unplug_error;
13802 			/*
13803 			 * Check if there is a deferred error condition
13804 			 * is active
13805 			 */
13806 			if ((HS_FFER1 & phba->work_hs) &&
13807 				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13808 				  HS_FFER6 | HS_FFER7 | HS_FFER8) &
13809 				  phba->work_hs)) {
13810 				set_bit(DEFER_ERATT, &phba->hba_flag);
13811 				/* Clear all interrupt enable conditions */
13812 				writel(0, phba->HCregaddr);
13813 				readl(phba->HCregaddr);
13814 			}
13815 		}
13816 
13817 		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
13818 			pmb = phba->sli.mbox_active;
13819 			pmbox = &pmb->u.mb;
13820 			mbox = phba->mbox;
13821 			vport = pmb->vport;
13822 
13823 			/* First check out the status word */
13824 			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13825 			if (pmbox->mbxOwner != OWN_HOST) {
13826 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13827 				/*
13828 				 * Stray Mailbox Interrupt, mbxCommand <cmd>
13829 				 * mbxStatus <status>
13830 				 */
13831 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13832 						"(%d):0304 Stray Mailbox "
13833 						"Interrupt mbxCommand x%x "
13834 						"mbxStatus x%x\n",
13835 						(vport ? vport->vpi : 0),
13836 						pmbox->mbxCommand,
13837 						pmbox->mbxStatus);
13838 				/* clear mailbox attention bit */
13839 				work_ha_copy &= ~HA_MBATT;
13840 			} else {
13841 				phba->sli.mbox_active = NULL;
13842 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13843 				phba->last_completion_time = jiffies;
13844 				timer_delete(&phba->sli.mbox_tmo);
13845 				if (pmb->mbox_cmpl) {
13846 					lpfc_sli_pcimem_bcopy(mbox, pmbox,
13847 							MAILBOX_CMD_SIZE);
13848 					if (pmb->out_ext_byte_len &&
13849 						pmb->ext_buf)
13850 						lpfc_sli_pcimem_bcopy(
13851 						phba->mbox_ext,
13852 						pmb->ext_buf,
13853 						pmb->out_ext_byte_len);
13854 				}
13855 				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13856 					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13857 
13858 					lpfc_debugfs_disc_trc(vport,
13859 						LPFC_DISC_TRC_MBOX_VPORT,
13860 						"MBOX dflt rpi: : "
13861 						"status:x%x rpi:x%x",
13862 						(uint32_t)pmbox->mbxStatus,
13863 						pmbox->un.varWords[0], 0);
13864 
13865 					if (!pmbox->mbxStatus) {
13866 						mp = pmb->ctx_buf;
13867 						ndlp = pmb->ctx_ndlp;
13868 
13869 						/* Reg_LOGIN of dflt RPI was
13870 						 * successful. new lets get
13871 						 * rid of the RPI using the
13872 						 * same mbox buffer.
13873 						 */
13874 						lpfc_unreg_login(phba,
13875 							vport->vpi,
13876 							pmbox->un.varWords[0],
13877 							pmb);
13878 						pmb->mbox_cmpl =
13879 							lpfc_mbx_cmpl_dflt_rpi;
13880 						pmb->ctx_buf = mp;
13881 						pmb->ctx_ndlp = ndlp;
13882 						pmb->vport = vport;
13883 						rc = lpfc_sli_issue_mbox(phba,
13884 								pmb,
13885 								MBX_NOWAIT);
13886 						if (rc != MBX_BUSY)
13887 							lpfc_printf_log(phba,
13888 							KERN_ERR,
13889 							LOG_TRACE_EVENT,
13890 							"0350 rc should have"
13891 							"been MBX_BUSY\n");
13892 						if (rc != MBX_NOT_FINISHED)
13893 							goto send_current_mbox;
13894 					}
13895 				}
13896 				spin_lock_irqsave(
13897 						&phba->pport->work_port_lock,
13898 						iflag);
13899 				phba->pport->work_port_events &=
13900 					~WORKER_MBOX_TMO;
13901 				spin_unlock_irqrestore(
13902 						&phba->pport->work_port_lock,
13903 						iflag);
13904 
13905 				/* Do NOT queue MBX_HEARTBEAT to the worker
13906 				 * thread for processing.
13907 				 */
13908 				if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13909 					/* Process mbox now */
13910 					phba->sli.mbox_active = NULL;
13911 					phba->sli.sli_flag &=
13912 						~LPFC_SLI_MBOX_ACTIVE;
13913 					if (pmb->mbox_cmpl)
13914 						pmb->mbox_cmpl(phba, pmb);
13915 				} else {
13916 					/* Queue to worker thread to process */
13917 					lpfc_mbox_cmpl_put(phba, pmb);
13918 				}
13919 			}
13920 		} else
13921 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13922 
13923 		if ((work_ha_copy & HA_MBATT) &&
13924 		    (phba->sli.mbox_active == NULL)) {
13925 send_current_mbox:
13926 			/* Process next mailbox command if there is one */
13927 			do {
13928 				rc = lpfc_sli_issue_mbox(phba, NULL,
13929 							 MBX_NOWAIT);
13930 			} while (rc == MBX_NOT_FINISHED);
13931 			if (rc != MBX_SUCCESS)
13932 				lpfc_printf_log(phba, KERN_ERR,
13933 						LOG_TRACE_EVENT,
13934 						"0349 rc should be "
13935 						"MBX_SUCCESS\n");
13936 		}
13937 
13938 		spin_lock_irqsave(&phba->hbalock, iflag);
13939 		phba->work_ha |= work_ha_copy;
13940 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13941 		lpfc_worker_wake_up(phba);
13942 	}
13943 	return IRQ_HANDLED;
13944 unplug_error:
13945 	spin_unlock_irqrestore(&phba->hbalock, iflag);
13946 	return IRQ_HANDLED;
13947 
13948 } /* lpfc_sli_sp_intr_handler */
13949 
13950 /**
13951  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13952  * @irq: Interrupt number.
13953  * @dev_id: The device context pointer.
13954  *
13955  * This function is directly called from the PCI layer as an interrupt
13956  * service routine when device with SLI-3 interface spec is enabled with
13957  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13958  * ring event in the HBA. However, when the device is enabled with either
13959  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13960  * device-level interrupt handler. When the PCI slot is in error recovery
13961  * or the HBA is undergoing initialization, the interrupt handler will not
13962  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13963  * the interrupt context. This function is called without any lock held.
13964  * It gets the hbalock to access and update SLI data structures.
13965  *
13966  * This function returns IRQ_HANDLED when interrupt is handled else it
13967  * returns IRQ_NONE.
13968  **/
13969 irqreturn_t
lpfc_sli_fp_intr_handler(int irq,void * dev_id)13970 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13971 {
13972 	struct lpfc_hba  *phba;
13973 	uint32_t ha_copy;
13974 	unsigned long status;
13975 	unsigned long iflag;
13976 	struct lpfc_sli_ring *pring;
13977 
13978 	/* Get the driver's phba structure from the dev_id and
13979 	 * assume the HBA is not interrupting.
13980 	 */
13981 	phba = (struct lpfc_hba *) dev_id;
13982 
13983 	if (unlikely(!phba))
13984 		return IRQ_NONE;
13985 
13986 	/*
13987 	 * Stuff needs to be attented to when this function is invoked as an
13988 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13989 	 */
13990 	if (phba->intr_type == MSIX) {
13991 		/* Check device state for handling interrupt */
13992 		if (lpfc_intr_state_check(phba))
13993 			return IRQ_NONE;
13994 		/* Need to read HA REG for FCP ring and other ring events */
13995 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13996 			return IRQ_HANDLED;
13997 
13998 		/*
13999 		 * If there is deferred error attention, do not check for
14000 		 * any interrupt.
14001 		 */
14002 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
14003 			return IRQ_NONE;
14004 
14005 		/* Clear up only attention source related to fast-path */
14006 		spin_lock_irqsave(&phba->hbalock, iflag);
14007 		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
14008 			phba->HAregaddr);
14009 		readl(phba->HAregaddr); /* flush */
14010 		spin_unlock_irqrestore(&phba->hbalock, iflag);
14011 	} else
14012 		ha_copy = phba->ha_copy;
14013 
14014 	/*
14015 	 * Process all events on FCP ring. Take the optimized path for FCP IO.
14016 	 */
14017 	ha_copy &= ~(phba->work_ha_mask);
14018 
14019 	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14020 	status >>= (4*LPFC_FCP_RING);
14021 	pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
14022 	if (status & HA_RXMASK)
14023 		lpfc_sli_handle_fast_ring_event(phba, pring, status);
14024 
14025 	if (phba->cfg_multi_ring_support == 2) {
14026 		/*
14027 		 * Process all events on extra ring. Take the optimized path
14028 		 * for extra ring IO.
14029 		 */
14030 		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14031 		status >>= (4*LPFC_EXTRA_RING);
14032 		if (status & HA_RXMASK) {
14033 			lpfc_sli_handle_fast_ring_event(phba,
14034 					&phba->sli.sli3_ring[LPFC_EXTRA_RING],
14035 					status);
14036 		}
14037 	}
14038 	return IRQ_HANDLED;
14039 }  /* lpfc_sli_fp_intr_handler */
14040 
14041 /**
14042  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
14043  * @irq: Interrupt number.
14044  * @dev_id: The device context pointer.
14045  *
14046  * This function is the HBA device-level interrupt handler to device with
14047  * SLI-3 interface spec, called from the PCI layer when either MSI or
14048  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
14049  * requires driver attention. This function invokes the slow-path interrupt
14050  * attention handling function and fast-path interrupt attention handling
14051  * function in turn to process the relevant HBA attention events. This
14052  * function is called without any lock held. It gets the hbalock to access
14053  * and update SLI data structures.
14054  *
14055  * This function returns IRQ_HANDLED when interrupt is handled, else it
14056  * returns IRQ_NONE.
14057  **/
14058 irqreturn_t
lpfc_sli_intr_handler(int irq,void * dev_id)14059 lpfc_sli_intr_handler(int irq, void *dev_id)
14060 {
14061 	struct lpfc_hba  *phba;
14062 	irqreturn_t sp_irq_rc, fp_irq_rc;
14063 	unsigned long status1, status2;
14064 	uint32_t hc_copy;
14065 
14066 	/*
14067 	 * Get the driver's phba structure from the dev_id and
14068 	 * assume the HBA is not interrupting.
14069 	 */
14070 	phba = (struct lpfc_hba *) dev_id;
14071 
14072 	if (unlikely(!phba))
14073 		return IRQ_NONE;
14074 
14075 	/* Check device state for handling interrupt */
14076 	if (lpfc_intr_state_check(phba))
14077 		return IRQ_NONE;
14078 
14079 	spin_lock(&phba->hbalock);
14080 	if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
14081 		spin_unlock(&phba->hbalock);
14082 		return IRQ_HANDLED;
14083 	}
14084 
14085 	if (unlikely(!phba->ha_copy)) {
14086 		spin_unlock(&phba->hbalock);
14087 		return IRQ_NONE;
14088 	} else if (phba->ha_copy & HA_ERATT) {
14089 		if (test_and_set_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
14090 			/* ERATT polling has handled ERATT */
14091 			phba->ha_copy &= ~HA_ERATT;
14092 	}
14093 
14094 	/*
14095 	 * If there is deferred error attention, do not check for any interrupt.
14096 	 */
14097 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
14098 		spin_unlock(&phba->hbalock);
14099 		return IRQ_NONE;
14100 	}
14101 
14102 	/* Clear attention sources except link and error attentions */
14103 	if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
14104 		spin_unlock(&phba->hbalock);
14105 		return IRQ_HANDLED;
14106 	}
14107 	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
14108 		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
14109 		phba->HCregaddr);
14110 	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
14111 	writel(hc_copy, phba->HCregaddr);
14112 	readl(phba->HAregaddr); /* flush */
14113 	spin_unlock(&phba->hbalock);
14114 
14115 	/*
14116 	 * Invokes slow-path host attention interrupt handling as appropriate.
14117 	 */
14118 
14119 	/* status of events with mailbox and link attention */
14120 	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
14121 
14122 	/* status of events with ELS ring */
14123 	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
14124 	status2 >>= (4*LPFC_ELS_RING);
14125 
14126 	if (status1 || (status2 & HA_RXMASK))
14127 		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
14128 	else
14129 		sp_irq_rc = IRQ_NONE;
14130 
14131 	/*
14132 	 * Invoke fast-path host attention interrupt handling as appropriate.
14133 	 */
14134 
14135 	/* status of events with FCP ring */
14136 	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14137 	status1 >>= (4*LPFC_FCP_RING);
14138 
14139 	/* status of events with extra ring */
14140 	if (phba->cfg_multi_ring_support == 2) {
14141 		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14142 		status2 >>= (4*LPFC_EXTRA_RING);
14143 	} else
14144 		status2 = 0;
14145 
14146 	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
14147 		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
14148 	else
14149 		fp_irq_rc = IRQ_NONE;
14150 
14151 	/* Return device-level interrupt handling status */
14152 	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
14153 }  /* lpfc_sli_intr_handler */
14154 
14155 /**
14156  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14157  * @phba: pointer to lpfc hba data structure.
14158  *
14159  * This routine is invoked by the worker thread to process all the pending
14160  * SLI4 els abort xri events.
14161  **/
lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba * phba)14162 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14163 {
14164 	struct lpfc_cq_event *cq_event;
14165 	unsigned long iflags;
14166 
14167 	/* First, declare the els xri abort event has been handled */
14168 	clear_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14169 
14170 	/* Now, handle all the els xri abort events */
14171 	spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14172 	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14173 		/* Get the first event from the head of the event queue */
14174 		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14175 				 cq_event, struct lpfc_cq_event, list);
14176 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14177 				       iflags);
14178 		/* Notify aborted XRI for ELS work queue */
14179 		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
14180 
14181 		/* Free the event processed back to the free pool */
14182 		lpfc_sli4_cq_event_release(phba, cq_event);
14183 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14184 				  iflags);
14185 	}
14186 	spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14187 }
14188 
14189 /**
14190  * lpfc_sli4_els_preprocess_rspiocbq - Get response iocbq from els wcqe
14191  * @phba: Pointer to HBA context object.
14192  * @irspiocbq: Pointer to work-queue completion queue entry.
14193  *
14194  * This routine handles an ELS work-queue completion event and construct
14195  * a pseudo response ELS IOCBQ from the SLI4 ELS WCQE for the common
14196  * discovery engine to handle.
14197  *
14198  * Return: Pointer to the receive IOCBQ, NULL otherwise.
14199  **/
14200 static struct lpfc_iocbq *
lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba * phba,struct lpfc_iocbq * irspiocbq)14201 lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
14202 				  struct lpfc_iocbq *irspiocbq)
14203 {
14204 	struct lpfc_sli_ring *pring;
14205 	struct lpfc_iocbq *cmdiocbq;
14206 	struct lpfc_wcqe_complete *wcqe;
14207 	unsigned long iflags;
14208 
14209 	pring = lpfc_phba_elsring(phba);
14210 	if (unlikely(!pring))
14211 		return NULL;
14212 
14213 	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
14214 	spin_lock_irqsave(&pring->ring_lock, iflags);
14215 	pring->stats.iocb_event++;
14216 	/* Look up the ELS command IOCB and create pseudo response IOCB */
14217 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14218 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
14219 	if (unlikely(!cmdiocbq)) {
14220 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
14221 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14222 				"0386 ELS complete with no corresponding "
14223 				"cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14224 				wcqe->word0, wcqe->total_data_placed,
14225 				wcqe->parameter, wcqe->word3);
14226 		lpfc_sli_release_iocbq(phba, irspiocbq);
14227 		return NULL;
14228 	}
14229 
14230 	memcpy(&irspiocbq->wqe, &cmdiocbq->wqe, sizeof(union lpfc_wqe128));
14231 	memcpy(&irspiocbq->wcqe_cmpl, wcqe, sizeof(*wcqe));
14232 
14233 	/* Put the iocb back on the txcmplq */
14234 	lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14235 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
14236 
14237 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14238 		spin_lock_irqsave(&phba->hbalock, iflags);
14239 		irspiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
14240 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14241 	}
14242 
14243 	return irspiocbq;
14244 }
14245 
14246 inline struct lpfc_cq_event *
lpfc_cq_event_setup(struct lpfc_hba * phba,void * entry,int size)14247 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14248 {
14249 	struct lpfc_cq_event *cq_event;
14250 
14251 	/* Allocate a new internal CQ_EVENT entry */
14252 	cq_event = lpfc_sli4_cq_event_alloc(phba);
14253 	if (!cq_event) {
14254 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14255 				"0602 Failed to alloc CQ_EVENT entry\n");
14256 		return NULL;
14257 	}
14258 
14259 	/* Move the CQE into the event */
14260 	memcpy(&cq_event->cqe, entry, size);
14261 	return cq_event;
14262 }
14263 
14264 /**
14265  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
14266  * @phba: Pointer to HBA context object.
14267  * @mcqe: Pointer to mailbox completion queue entry.
14268  *
14269  * This routine process a mailbox completion queue entry with asynchronous
14270  * event.
14271  *
14272  * Return: true if work posted to worker thread, otherwise false.
14273  **/
14274 static bool
lpfc_sli4_sp_handle_async_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14275 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14276 {
14277 	struct lpfc_cq_event *cq_event;
14278 	unsigned long iflags;
14279 
14280 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14281 			"0392 Async Event: word0:x%x, word1:x%x, "
14282 			"word2:x%x, word3:x%x\n", mcqe->word0,
14283 			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14284 
14285 	cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14286 	if (!cq_event)
14287 		return false;
14288 
14289 	spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
14290 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
14291 	spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14292 
14293 	/* Set the async event flag */
14294 	set_bit(ASYNC_EVENT, &phba->hba_flag);
14295 
14296 	return true;
14297 }
14298 
14299 /**
14300  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14301  * @phba: Pointer to HBA context object.
14302  * @mcqe: Pointer to mailbox completion queue entry.
14303  *
14304  * This routine process a mailbox completion queue entry with mailbox
14305  * completion event.
14306  *
14307  * Return: true if work posted to worker thread, otherwise false.
14308  **/
14309 static bool
lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14310 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14311 {
14312 	uint32_t mcqe_status;
14313 	MAILBOX_t *mbox, *pmbox;
14314 	struct lpfc_mqe *mqe;
14315 	struct lpfc_vport *vport;
14316 	struct lpfc_nodelist *ndlp;
14317 	struct lpfc_dmabuf *mp;
14318 	unsigned long iflags;
14319 	LPFC_MBOXQ_t *pmb;
14320 	bool workposted = false;
14321 	int rc;
14322 
14323 	/* If not a mailbox complete MCQE, out by checking mailbox consume */
14324 	if (!bf_get(lpfc_trailer_completed, mcqe))
14325 		goto out_no_mqe_complete;
14326 
14327 	/* Get the reference to the active mbox command */
14328 	spin_lock_irqsave(&phba->hbalock, iflags);
14329 	pmb = phba->sli.mbox_active;
14330 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14331 	if (unlikely(!pmb)) {
14332 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14333 				"1832 No pending MBOX command to handle, "
14334 				"mcqe: x%08x x%08x x%08x x%08x\n",
14335 				mcqe->word0, mcqe->mcqe_tag0,
14336 				mcqe->mcqe_tag1, mcqe->trailer);
14337 		goto out_no_mqe_complete;
14338 	}
14339 	mqe = &pmb->u.mqe;
14340 	pmbox = (MAILBOX_t *)&pmb->u.mqe;
14341 	mbox = phba->mbox;
14342 	vport = pmb->vport;
14343 
14344 	/* Reset heartbeat timer */
14345 	phba->last_completion_time = jiffies;
14346 	timer_delete(&phba->sli.mbox_tmo);
14347 
14348 	/* Move mbox data to caller's mailbox region, do endian swapping */
14349 	if (pmb->mbox_cmpl && mbox)
14350 		lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
14351 
14352 	/*
14353 	 * For mcqe errors, conditionally move a modified error code to
14354 	 * the mbox so that the error will not be missed.
14355 	 */
14356 	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14357 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14358 		if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14359 			bf_set(lpfc_mqe_status, mqe,
14360 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
14361 	}
14362 	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14363 		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14364 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14365 				      "MBOX dflt rpi: status:x%x rpi:x%x",
14366 				      mcqe_status,
14367 				      pmbox->un.varWords[0], 0);
14368 		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
14369 			mp = pmb->ctx_buf;
14370 			ndlp = pmb->ctx_ndlp;
14371 
14372 			/* Reg_LOGIN of dflt RPI was successful. Mark the
14373 			 * node as having an UNREG_LOGIN in progress to stop
14374 			 * an unsolicited PLOGI from the same NPortId from
14375 			 * starting another mailbox transaction.
14376 			 */
14377 			set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
14378 			lpfc_unreg_login(phba, vport->vpi,
14379 					 pmbox->un.varWords[0], pmb);
14380 			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
14381 			pmb->ctx_buf = mp;
14382 
14383 			/* No reference taken here.  This is a default
14384 			 * RPI reg/immediate unreg cycle. The reference was
14385 			 * taken in the reg rpi path and is released when
14386 			 * this mailbox completes.
14387 			 */
14388 			pmb->ctx_ndlp = ndlp;
14389 			pmb->vport = vport;
14390 			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14391 			if (rc != MBX_BUSY)
14392 				lpfc_printf_log(phba, KERN_ERR,
14393 						LOG_TRACE_EVENT,
14394 						"0385 rc should "
14395 						"have been MBX_BUSY\n");
14396 			if (rc != MBX_NOT_FINISHED)
14397 				goto send_current_mbox;
14398 		}
14399 	}
14400 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14401 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14402 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14403 
14404 	/* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14405 	if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14406 		spin_lock_irqsave(&phba->hbalock, iflags);
14407 		/* Release the mailbox command posting token */
14408 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14409 		phba->sli.mbox_active = NULL;
14410 		if (bf_get(lpfc_trailer_consumed, mcqe))
14411 			lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14412 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14413 
14414 		/* Post the next mbox command, if there is one */
14415 		lpfc_sli4_post_async_mbox(phba);
14416 
14417 		/* Process cmpl now */
14418 		if (pmb->mbox_cmpl)
14419 			pmb->mbox_cmpl(phba, pmb);
14420 		return false;
14421 	}
14422 
14423 	/* There is mailbox completion work to queue to the worker thread */
14424 	spin_lock_irqsave(&phba->hbalock, iflags);
14425 	__lpfc_mbox_cmpl_put(phba, pmb);
14426 	phba->work_ha |= HA_MBATT;
14427 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14428 	workposted = true;
14429 
14430 send_current_mbox:
14431 	spin_lock_irqsave(&phba->hbalock, iflags);
14432 	/* Release the mailbox command posting token */
14433 	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14434 	/* Setting active mailbox pointer need to be in sync to flag clear */
14435 	phba->sli.mbox_active = NULL;
14436 	if (bf_get(lpfc_trailer_consumed, mcqe))
14437 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14438 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14439 	/* Wake up worker thread to post the next pending mailbox command */
14440 	lpfc_worker_wake_up(phba);
14441 	return workposted;
14442 
14443 out_no_mqe_complete:
14444 	spin_lock_irqsave(&phba->hbalock, iflags);
14445 	if (bf_get(lpfc_trailer_consumed, mcqe))
14446 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14447 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14448 	return false;
14449 }
14450 
14451 /**
14452  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14453  * @phba: Pointer to HBA context object.
14454  * @cq: Pointer to associated CQ
14455  * @cqe: Pointer to mailbox completion queue entry.
14456  *
14457  * This routine process a mailbox completion queue entry, it invokes the
14458  * proper mailbox complete handling or asynchronous event handling routine
14459  * according to the MCQE's async bit.
14460  *
14461  * Return: true if work posted to worker thread, otherwise false.
14462  **/
14463 static bool
lpfc_sli4_sp_handle_mcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14464 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14465 			 struct lpfc_cqe *cqe)
14466 {
14467 	struct lpfc_mcqe mcqe;
14468 	bool workposted;
14469 
14470 	cq->CQ_mbox++;
14471 
14472 	/* Copy the mailbox MCQE and convert endian order as needed */
14473 	lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
14474 
14475 	/* Invoke the proper event handling routine */
14476 	if (!bf_get(lpfc_trailer_async, &mcqe))
14477 		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14478 	else
14479 		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14480 	return workposted;
14481 }
14482 
14483 /**
14484  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14485  * @phba: Pointer to HBA context object.
14486  * @cq: Pointer to associated CQ
14487  * @wcqe: Pointer to work-queue completion queue entry.
14488  *
14489  * This routine handles an ELS work-queue completion event.
14490  *
14491  * Return: true if work posted to worker thread, otherwise false.
14492  **/
14493 static bool
lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)14494 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14495 			     struct lpfc_wcqe_complete *wcqe)
14496 {
14497 	struct lpfc_iocbq *irspiocbq;
14498 	unsigned long iflags;
14499 	struct lpfc_sli_ring *pring = cq->pring;
14500 	int txq_cnt = 0;
14501 	int txcmplq_cnt = 0;
14502 
14503 	/* Check for response status */
14504 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14505 		/* Log the error status */
14506 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14507 				"0357 ELS CQE error: status=x%x: "
14508 				"CQE: %08x %08x %08x %08x\n",
14509 				bf_get(lpfc_wcqe_c_status, wcqe),
14510 				wcqe->word0, wcqe->total_data_placed,
14511 				wcqe->parameter, wcqe->word3);
14512 	}
14513 
14514 	/* Get an irspiocbq for later ELS response processing use */
14515 	irspiocbq = lpfc_sli_get_iocbq(phba);
14516 	if (!irspiocbq) {
14517 		if (!list_empty(&pring->txq))
14518 			txq_cnt++;
14519 		if (!list_empty(&pring->txcmplq))
14520 			txcmplq_cnt++;
14521 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14522 			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
14523 			"els_txcmplq_cnt=%d\n",
14524 			txq_cnt, phba->iocb_cnt,
14525 			txcmplq_cnt);
14526 		return false;
14527 	}
14528 
14529 	/* Save off the slow-path queue event for work thread to process */
14530 	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
14531 	spin_lock_irqsave(&phba->hbalock, iflags);
14532 	list_add_tail(&irspiocbq->cq_event.list,
14533 		      &phba->sli4_hba.sp_queue_event);
14534 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14535 	set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14536 
14537 	return true;
14538 }
14539 
14540 /**
14541  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14542  * @phba: Pointer to HBA context object.
14543  * @wcqe: Pointer to work-queue completion queue entry.
14544  *
14545  * This routine handles slow-path WQ entry consumed event by invoking the
14546  * proper WQ release routine to the slow-path WQ.
14547  **/
14548 static void
lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_wcqe_release * wcqe)14549 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14550 			     struct lpfc_wcqe_release *wcqe)
14551 {
14552 	/* sanity check on queue memory */
14553 	if (unlikely(!phba->sli4_hba.els_wq))
14554 		return;
14555 	/* Check for the slow-path ELS work queue */
14556 	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14557 		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14558 				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14559 	else
14560 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14561 				"2579 Slow-path wqe consume event carries "
14562 				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14563 				bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14564 				phba->sli4_hba.els_wq->queue_id);
14565 }
14566 
14567 /**
14568  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14569  * @phba: Pointer to HBA context object.
14570  * @cq: Pointer to a WQ completion queue.
14571  * @wcqe: Pointer to work-queue completion queue entry.
14572  *
14573  * This routine handles an XRI abort event.
14574  *
14575  * Return: true if work posted to worker thread, otherwise false.
14576  **/
14577 static bool
lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct sli4_wcqe_xri_aborted * wcqe)14578 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14579 				   struct lpfc_queue *cq,
14580 				   struct sli4_wcqe_xri_aborted *wcqe)
14581 {
14582 	bool workposted = false;
14583 	struct lpfc_cq_event *cq_event;
14584 	unsigned long iflags;
14585 
14586 	switch (cq->subtype) {
14587 	case LPFC_IO:
14588 		lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14589 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14590 			/* Notify aborted XRI for NVME work queue */
14591 			if (phba->nvmet_support)
14592 				lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14593 		}
14594 		workposted = false;
14595 		break;
14596 	case LPFC_NVME_LS: /* NVME LS uses ELS resources */
14597 	case LPFC_ELS:
14598 		cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14599 		if (!cq_event) {
14600 			workposted = false;
14601 			break;
14602 		}
14603 		cq_event->hdwq = cq->hdwq;
14604 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14605 				  iflags);
14606 		list_add_tail(&cq_event->list,
14607 			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14608 		/* Set the els xri abort event flag */
14609 		set_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14610 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14611 				       iflags);
14612 		workposted = true;
14613 		break;
14614 	default:
14615 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14616 				"0603 Invalid CQ subtype %d: "
14617 				"%08x %08x %08x %08x\n",
14618 				cq->subtype, wcqe->word0, wcqe->parameter,
14619 				wcqe->word2, wcqe->word3);
14620 		workposted = false;
14621 		break;
14622 	}
14623 	return workposted;
14624 }
14625 
14626 #define FC_RCTL_MDS_DIAGS	0xF4
14627 
14628 /**
14629  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
14630  * @phba: Pointer to HBA context object.
14631  * @rcqe: Pointer to receive-queue completion queue entry.
14632  *
14633  * This routine process a receive-queue completion queue entry.
14634  *
14635  * Return: true if work posted to worker thread, otherwise false.
14636  **/
14637 static bool
lpfc_sli4_sp_handle_rcqe(struct lpfc_hba * phba,struct lpfc_rcqe * rcqe)14638 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14639 {
14640 	bool workposted = false;
14641 	struct fc_frame_header *fc_hdr;
14642 	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14643 	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
14644 	struct lpfc_nvmet_tgtport *tgtp;
14645 	struct hbq_dmabuf *dma_buf;
14646 	uint32_t status, rq_id;
14647 	unsigned long iflags;
14648 
14649 	/* sanity check on queue memory */
14650 	if (unlikely(!hrq) || unlikely(!drq))
14651 		return workposted;
14652 
14653 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14654 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14655 	else
14656 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14657 	if (rq_id != hrq->queue_id)
14658 		goto out;
14659 
14660 	status = bf_get(lpfc_rcqe_status, rcqe);
14661 	switch (status) {
14662 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14663 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14664 				"2537 Receive Frame Truncated!!\n");
14665 		fallthrough;
14666 	case FC_STATUS_RQ_SUCCESS:
14667 		spin_lock_irqsave(&phba->hbalock, iflags);
14668 		lpfc_sli4_rq_release(hrq, drq);
14669 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14670 		if (!dma_buf) {
14671 			hrq->RQ_no_buf_found++;
14672 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14673 			goto out;
14674 		}
14675 		hrq->RQ_rcv_buf++;
14676 		hrq->RQ_buf_posted--;
14677 		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
14678 
14679 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14680 
14681 		if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14682 		    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14683 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14684 			/* Handle MDS Loopback frames */
14685 			if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
14686 				lpfc_sli4_handle_mds_loopback(phba->pport,
14687 							      dma_buf);
14688 			else
14689 				lpfc_in_buf_free(phba, &dma_buf->dbuf);
14690 			break;
14691 		}
14692 
14693 		/* save off the frame for the work thread to process */
14694 		list_add_tail(&dma_buf->cq_event.list,
14695 			      &phba->sli4_hba.sp_queue_event);
14696 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14697 		/* Frame received */
14698 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14699 		workposted = true;
14700 		break;
14701 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
14702 		if (phba->nvmet_support) {
14703 			tgtp = phba->targetport->private;
14704 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14705 					"6402 RQE Error x%x, posted %d err_cnt "
14706 					"%d: %x %x %x\n",
14707 					status, hrq->RQ_buf_posted,
14708 					hrq->RQ_no_posted_buf,
14709 					atomic_read(&tgtp->rcv_fcp_cmd_in),
14710 					atomic_read(&tgtp->rcv_fcp_cmd_out),
14711 					atomic_read(&tgtp->xmt_fcp_release));
14712 		}
14713 		hrq->RQ_discard_frm++;
14714 		fallthrough;
14715 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
14716 		/* Unexpected event - bump the counter for support. */
14717 		hrq->RQ_no_posted_buf++;
14718 
14719 		lpfc_log_msg(phba, KERN_WARNING,
14720 			     LOG_ELS | LOG_DISCOVERY | LOG_SLI,
14721 			     "6423 RQE completion Status x%x, needed x%x "
14722 			     "discarded x%x\n", status,
14723 			     hrq->RQ_no_posted_buf - hrq->RQ_discard_frm,
14724 			     hrq->RQ_discard_frm);
14725 
14726 		/* For SLI3, post more buffers if possible. No action for SLI4.
14727 		 * SLI4 is reposting immediately after processing the RQE.
14728 		 */
14729 		set_bit(HBA_POST_RECEIVE_BUFFER, &phba->hba_flag);
14730 		workposted = true;
14731 		break;
14732 	case FC_STATUS_RQ_DMA_FAILURE:
14733 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14734 				"2564 RQE DMA Error x%x, x%08x x%08x x%08x "
14735 				"x%08x\n",
14736 				status, rcqe->word0, rcqe->word1,
14737 				rcqe->word2, rcqe->word3);
14738 
14739 		/* If IV set, no further recovery */
14740 		if (bf_get(lpfc_rcqe_iv, rcqe))
14741 			break;
14742 
14743 		/* recycle consumed resource */
14744 		spin_lock_irqsave(&phba->hbalock, iflags);
14745 		lpfc_sli4_rq_release(hrq, drq);
14746 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14747 		if (!dma_buf) {
14748 			hrq->RQ_no_buf_found++;
14749 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14750 			break;
14751 		}
14752 		hrq->RQ_rcv_buf++;
14753 		hrq->RQ_buf_posted--;
14754 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14755 		lpfc_in_buf_free(phba, &dma_buf->dbuf);
14756 		break;
14757 	default:
14758 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14759 				"2565 Unexpected RQE Status x%x, w0-3 x%08x "
14760 				"x%08x x%08x x%08x\n",
14761 				status, rcqe->word0, rcqe->word1,
14762 				rcqe->word2, rcqe->word3);
14763 		break;
14764 	}
14765 out:
14766 	return workposted;
14767 }
14768 
14769 /**
14770  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14771  * @phba: Pointer to HBA context object.
14772  * @cq: Pointer to the completion queue.
14773  * @cqe: Pointer to a completion queue entry.
14774  *
14775  * This routine process a slow-path work-queue or receive queue completion queue
14776  * entry.
14777  *
14778  * Return: true if work posted to worker thread, otherwise false.
14779  **/
14780 static bool
lpfc_sli4_sp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14781 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14782 			 struct lpfc_cqe *cqe)
14783 {
14784 	struct lpfc_cqe cqevt;
14785 	bool workposted = false;
14786 
14787 	/* Copy the work queue CQE and convert endian order if needed */
14788 	lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
14789 
14790 	/* Check and process for different type of WCQE and dispatch */
14791 	switch (bf_get(lpfc_cqe_code, &cqevt)) {
14792 	case CQE_CODE_COMPL_WQE:
14793 		/* Process the WQ/RQ complete event */
14794 		phba->last_completion_time = jiffies;
14795 		workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
14796 				(struct lpfc_wcqe_complete *)&cqevt);
14797 		break;
14798 	case CQE_CODE_RELEASE_WQE:
14799 		/* Process the WQ release event */
14800 		lpfc_sli4_sp_handle_rel_wcqe(phba,
14801 				(struct lpfc_wcqe_release *)&cqevt);
14802 		break;
14803 	case CQE_CODE_XRI_ABORTED:
14804 		/* Process the WQ XRI abort event */
14805 		phba->last_completion_time = jiffies;
14806 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14807 				(struct sli4_wcqe_xri_aborted *)&cqevt);
14808 		break;
14809 	case CQE_CODE_RECEIVE:
14810 	case CQE_CODE_RECEIVE_V1:
14811 		/* Process the RQ event */
14812 		phba->last_completion_time = jiffies;
14813 		workposted = lpfc_sli4_sp_handle_rcqe(phba,
14814 				(struct lpfc_rcqe *)&cqevt);
14815 		break;
14816 	default:
14817 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14818 				"0388 Not a valid WCQE code: x%x\n",
14819 				bf_get(lpfc_cqe_code, &cqevt));
14820 		break;
14821 	}
14822 	return workposted;
14823 }
14824 
14825 /**
14826  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14827  * @phba: Pointer to HBA context object.
14828  * @eqe: Pointer to fast-path event queue entry.
14829  * @speq: Pointer to slow-path event queue.
14830  *
14831  * This routine process a event queue entry from the slow-path event queue.
14832  * It will check the MajorCode and MinorCode to determine this is for a
14833  * completion event on a completion queue, if not, an error shall be logged
14834  * and just return. Otherwise, it will get to the corresponding completion
14835  * queue and process all the entries on that completion queue, rearm the
14836  * completion queue, and then return.
14837  *
14838  **/
14839 static void
lpfc_sli4_sp_handle_eqe(struct lpfc_hba * phba,struct lpfc_eqe * eqe,struct lpfc_queue * speq)14840 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14841 	struct lpfc_queue *speq)
14842 {
14843 	struct lpfc_queue *cq = NULL, *childq;
14844 	uint16_t cqid;
14845 	int ret = 0;
14846 
14847 	/* Get the reference to the corresponding CQ */
14848 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14849 
14850 	list_for_each_entry(childq, &speq->child_list, list) {
14851 		if (childq->queue_id == cqid) {
14852 			cq = childq;
14853 			break;
14854 		}
14855 	}
14856 	if (unlikely(!cq)) {
14857 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14858 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14859 					"0365 Slow-path CQ identifier "
14860 					"(%d) does not exist\n", cqid);
14861 		return;
14862 	}
14863 
14864 	/* Save EQ associated with this CQ */
14865 	cq->assoc_qp = speq;
14866 
14867 	if (is_kdump_kernel())
14868 		ret = queue_work(phba->wq, &cq->spwork);
14869 	else
14870 		ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14871 
14872 	if (!ret)
14873 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14874 				"0390 Cannot schedule queue work "
14875 				"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14876 				cqid, cq->queue_id, raw_smp_processor_id());
14877 }
14878 
14879 /**
14880  * __lpfc_sli4_process_cq - Process elements of a CQ
14881  * @phba: Pointer to HBA context object.
14882  * @cq: Pointer to CQ to be processed
14883  * @handler: Routine to process each cqe
14884  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14885  *
14886  * This routine processes completion queue entries in a CQ. While a valid
14887  * queue element is found, the handler is called. During processing checks
14888  * are made for periodic doorbell writes to let the hardware know of
14889  * element consumption.
14890  *
14891  * If the max limit on cqes to process is hit, or there are no more valid
14892  * entries, the loop stops. If we processed a sufficient number of elements,
14893  * meaning there is sufficient load, rather than rearming and generating
14894  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14895  * indicates no rescheduling.
14896  *
14897  * Returns True if work scheduled, False otherwise.
14898  **/
14899 static bool
__lpfc_sli4_process_cq(struct lpfc_hba * phba,struct lpfc_queue * cq,bool (* handler)(struct lpfc_hba *,struct lpfc_queue *,struct lpfc_cqe *),unsigned long * delay)14900 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14901 	bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14902 			struct lpfc_cqe *), unsigned long *delay)
14903 {
14904 	struct lpfc_cqe *cqe;
14905 	bool workposted = false;
14906 	int count = 0, consumed = 0;
14907 	bool arm = true;
14908 
14909 	/* default - no reschedule */
14910 	*delay = 0;
14911 
14912 	if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14913 		goto rearm_and_exit;
14914 
14915 	/* Process all the entries to the CQ */
14916 	cq->q_flag = 0;
14917 	cqe = lpfc_sli4_cq_get(cq);
14918 	while (cqe) {
14919 		workposted |= handler(phba, cq, cqe);
14920 		__lpfc_sli4_consume_cqe(phba, cq, cqe);
14921 
14922 		consumed++;
14923 		if (!(++count % cq->max_proc_limit))
14924 			break;
14925 
14926 		if (!(count % cq->notify_interval)) {
14927 			phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14928 						LPFC_QUEUE_NOARM);
14929 			consumed = 0;
14930 			cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14931 		}
14932 
14933 		if (count == LPFC_NVMET_CQ_NOTIFY)
14934 			cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14935 
14936 		cqe = lpfc_sli4_cq_get(cq);
14937 	}
14938 	if (count >= phba->cfg_cq_poll_threshold) {
14939 		*delay = 1;
14940 		arm = false;
14941 	}
14942 
14943 	/* Track the max number of CQEs processed in 1 EQ */
14944 	if (count > cq->CQ_max_cqe)
14945 		cq->CQ_max_cqe = count;
14946 
14947 	cq->assoc_qp->EQ_cqe_cnt += count;
14948 
14949 	/* Catch the no cq entry condition */
14950 	if (unlikely(count == 0))
14951 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14952 				"0369 No entry from completion queue "
14953 				"qid=%d\n", cq->queue_id);
14954 
14955 	xchg(&cq->queue_claimed, 0);
14956 
14957 rearm_and_exit:
14958 	phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14959 			arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14960 
14961 	return workposted;
14962 }
14963 
14964 /**
14965  * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14966  * @cq: pointer to CQ to process
14967  *
14968  * This routine calls the cq processing routine with a handler specific
14969  * to the type of queue bound to it.
14970  *
14971  * The CQ routine returns two values: the first is the calling status,
14972  * which indicates whether work was queued to the  background discovery
14973  * thread. If true, the routine should wakeup the discovery thread;
14974  * the second is the delay parameter. If non-zero, rather than rearming
14975  * the CQ and yet another interrupt, the CQ handler should be queued so
14976  * that it is processed in a subsequent polling action. The value of
14977  * the delay indicates when to reschedule it.
14978  **/
14979 static void
__lpfc_sli4_sp_process_cq(struct lpfc_queue * cq)14980 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14981 {
14982 	struct lpfc_hba *phba = cq->phba;
14983 	unsigned long delay;
14984 	bool workposted = false;
14985 	int ret = 0;
14986 
14987 	/* Process and rearm the CQ */
14988 	switch (cq->type) {
14989 	case LPFC_MCQ:
14990 		workposted |= __lpfc_sli4_process_cq(phba, cq,
14991 						lpfc_sli4_sp_handle_mcqe,
14992 						&delay);
14993 		break;
14994 	case LPFC_WCQ:
14995 		if (cq->subtype == LPFC_IO)
14996 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14997 						lpfc_sli4_fp_handle_cqe,
14998 						&delay);
14999 		else
15000 			workposted |= __lpfc_sli4_process_cq(phba, cq,
15001 						lpfc_sli4_sp_handle_cqe,
15002 						&delay);
15003 		break;
15004 	default:
15005 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15006 				"0370 Invalid completion queue type (%d)\n",
15007 				cq->type);
15008 		return;
15009 	}
15010 
15011 	if (delay) {
15012 		if (is_kdump_kernel())
15013 			ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
15014 						delay);
15015 		else
15016 			ret = queue_delayed_work_on(cq->chann, phba->wq,
15017 						&cq->sched_spwork, delay);
15018 		if (!ret)
15019 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15020 				"0394 Cannot schedule queue work "
15021 				"for cqid=%d on CPU %d\n",
15022 				cq->queue_id, cq->chann);
15023 	}
15024 
15025 	/* wake up worker thread if there are works to be done */
15026 	if (workposted)
15027 		lpfc_worker_wake_up(phba);
15028 }
15029 
15030 /**
15031  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
15032  *   interrupt
15033  * @work: pointer to work element
15034  *
15035  * translates from the work handler and calls the slow-path handler.
15036  **/
15037 static void
lpfc_sli4_sp_process_cq(struct work_struct * work)15038 lpfc_sli4_sp_process_cq(struct work_struct *work)
15039 {
15040 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
15041 
15042 	__lpfc_sli4_sp_process_cq(cq);
15043 }
15044 
15045 /**
15046  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
15047  * @work: pointer to work element
15048  *
15049  * translates from the work handler and calls the slow-path handler.
15050  **/
15051 static void
lpfc_sli4_dly_sp_process_cq(struct work_struct * work)15052 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
15053 {
15054 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15055 					struct lpfc_queue, sched_spwork);
15056 
15057 	__lpfc_sli4_sp_process_cq(cq);
15058 }
15059 
15060 /**
15061  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
15062  * @phba: Pointer to HBA context object.
15063  * @cq: Pointer to associated CQ
15064  * @wcqe: Pointer to work-queue completion queue entry.
15065  *
15066  * This routine process a fast-path work queue completion entry from fast-path
15067  * event queue for FCP command response completion.
15068  **/
15069 static void
lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)15070 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15071 			     struct lpfc_wcqe_complete *wcqe)
15072 {
15073 	struct lpfc_sli_ring *pring = cq->pring;
15074 	struct lpfc_iocbq *cmdiocbq;
15075 	unsigned long iflags;
15076 
15077 	/* Check for response status */
15078 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15079 		/* If resource errors reported from HBA, reduce queue
15080 		 * depth of the SCSI device.
15081 		 */
15082 		if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15083 		     IOSTAT_LOCAL_REJECT)) &&
15084 		    ((wcqe->parameter & IOERR_PARAM_MASK) ==
15085 		     IOERR_NO_RESOURCES))
15086 			phba->lpfc_rampdown_queue_depth(phba);
15087 
15088 		/* Log the cmpl status */
15089 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15090 				"0373 FCP CQE cmpl: status=x%x: "
15091 				"CQE: %08x %08x %08x %08x\n",
15092 				bf_get(lpfc_wcqe_c_status, wcqe),
15093 				wcqe->word0, wcqe->total_data_placed,
15094 				wcqe->parameter, wcqe->word3);
15095 	}
15096 
15097 	/* Look up the FCP command IOCB and create pseudo response IOCB */
15098 	spin_lock_irqsave(&pring->ring_lock, iflags);
15099 	pring->stats.iocb_event++;
15100 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15101 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15102 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
15103 	if (unlikely(!cmdiocbq)) {
15104 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15105 				"0374 FCP complete with no corresponding "
15106 				"cmdiocb: iotag (%d)\n",
15107 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15108 		return;
15109 	}
15110 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15111 	cmdiocbq->isr_timestamp = cq->isr_timestamp;
15112 #endif
15113 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
15114 		spin_lock_irqsave(&phba->hbalock, iflags);
15115 		cmdiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
15116 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15117 	}
15118 
15119 	if (cmdiocbq->cmd_cmpl) {
15120 		/* For FCP the flag is cleared in cmd_cmpl */
15121 		if (!(cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
15122 		    cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED) {
15123 			spin_lock_irqsave(&phba->hbalock, iflags);
15124 			cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
15125 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15126 		}
15127 
15128 		/* Pass the cmd_iocb and the wcqe to the upper layer */
15129 		memcpy(&cmdiocbq->wcqe_cmpl, wcqe,
15130 		       sizeof(struct lpfc_wcqe_complete));
15131 		cmdiocbq->cmd_cmpl(phba, cmdiocbq, cmdiocbq);
15132 	} else {
15133 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15134 				"0375 FCP cmdiocb not callback function "
15135 				"iotag: (%d)\n",
15136 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15137 	}
15138 }
15139 
15140 /**
15141  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15142  * @phba: Pointer to HBA context object.
15143  * @cq: Pointer to completion queue.
15144  * @wcqe: Pointer to work-queue completion queue entry.
15145  *
15146  * This routine handles an fast-path WQ entry consumed event by invoking the
15147  * proper WQ release routine to the slow-path WQ.
15148  **/
15149 static void
lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_release * wcqe)15150 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15151 			     struct lpfc_wcqe_release *wcqe)
15152 {
15153 	struct lpfc_queue *childwq;
15154 	bool wqid_matched = false;
15155 	uint16_t hba_wqid;
15156 
15157 	/* Check for fast-path FCP work queue release */
15158 	hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
15159 	list_for_each_entry(childwq, &cq->child_list, list) {
15160 		if (childwq->queue_id == hba_wqid) {
15161 			lpfc_sli4_wq_release(childwq,
15162 					bf_get(lpfc_wcqe_r_wqe_index, wcqe));
15163 			if (childwq->q_flag & HBA_NVMET_WQFULL)
15164 				lpfc_nvmet_wqfull_process(phba, childwq);
15165 			wqid_matched = true;
15166 			break;
15167 		}
15168 	}
15169 	/* Report warning log message if no match found */
15170 	if (wqid_matched != true)
15171 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15172 				"2580 Fast-path wqe consume event carries "
15173 				"miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
15174 }
15175 
15176 /**
15177  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15178  * @phba: Pointer to HBA context object.
15179  * @cq: Pointer to completion queue.
15180  * @rcqe: Pointer to receive-queue completion queue entry.
15181  *
15182  * This routine process a receive-queue completion queue entry.
15183  *
15184  * Return: true if work posted to worker thread, otherwise false.
15185  **/
15186 static bool
lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_rcqe * rcqe)15187 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15188 			    struct lpfc_rcqe *rcqe)
15189 {
15190 	bool workposted = false;
15191 	struct lpfc_queue *hrq;
15192 	struct lpfc_queue *drq;
15193 	struct rqb_dmabuf *dma_buf;
15194 	struct fc_frame_header *fc_hdr;
15195 	struct lpfc_nvmet_tgtport *tgtp;
15196 	uint32_t status, rq_id;
15197 	unsigned long iflags;
15198 	uint32_t fctl, idx;
15199 
15200 	if ((phba->nvmet_support == 0) ||
15201 	    (phba->sli4_hba.nvmet_cqset == NULL))
15202 		return workposted;
15203 
15204 	idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15205 	hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15206 	drq = phba->sli4_hba.nvmet_mrq_data[idx];
15207 
15208 	/* sanity check on queue memory */
15209 	if (unlikely(!hrq) || unlikely(!drq))
15210 		return workposted;
15211 
15212 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15213 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15214 	else
15215 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15216 
15217 	if ((phba->nvmet_support == 0) ||
15218 	    (rq_id != hrq->queue_id))
15219 		return workposted;
15220 
15221 	status = bf_get(lpfc_rcqe_status, rcqe);
15222 	switch (status) {
15223 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
15224 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15225 				"6126 Receive Frame Truncated!!\n");
15226 		fallthrough;
15227 	case FC_STATUS_RQ_SUCCESS:
15228 		spin_lock_irqsave(&phba->hbalock, iflags);
15229 		lpfc_sli4_rq_release(hrq, drq);
15230 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15231 		if (!dma_buf) {
15232 			hrq->RQ_no_buf_found++;
15233 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15234 			goto out;
15235 		}
15236 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15237 		hrq->RQ_rcv_buf++;
15238 		hrq->RQ_buf_posted--;
15239 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15240 
15241 		/* Just some basic sanity checks on FCP Command frame */
15242 		fctl = (fc_hdr->fh_f_ctl[0] << 16 |
15243 			fc_hdr->fh_f_ctl[1] << 8 |
15244 			fc_hdr->fh_f_ctl[2]);
15245 		if (((fctl &
15246 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15247 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15248 		    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15249 			goto drop;
15250 
15251 		if (fc_hdr->fh_type == FC_TYPE_FCP) {
15252 			dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
15253 			lpfc_nvmet_unsol_fcp_event(
15254 				phba, idx, dma_buf, cq->isr_timestamp,
15255 				cq->q_flag & HBA_NVMET_CQ_NOTIFY);
15256 			return false;
15257 		}
15258 drop:
15259 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15260 		break;
15261 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
15262 		if (phba->nvmet_support) {
15263 			tgtp = phba->targetport->private;
15264 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15265 					"6401 RQE Error x%x, posted %d err_cnt "
15266 					"%d: %x %x %x\n",
15267 					status, hrq->RQ_buf_posted,
15268 					hrq->RQ_no_posted_buf,
15269 					atomic_read(&tgtp->rcv_fcp_cmd_in),
15270 					atomic_read(&tgtp->rcv_fcp_cmd_out),
15271 					atomic_read(&tgtp->xmt_fcp_release));
15272 		}
15273 		fallthrough;
15274 
15275 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
15276 		hrq->RQ_no_posted_buf++;
15277 		/* Post more buffers if possible */
15278 		break;
15279 	case FC_STATUS_RQ_DMA_FAILURE:
15280 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15281 				"2575 RQE DMA Error x%x, x%08x x%08x x%08x "
15282 				"x%08x\n",
15283 				status, rcqe->word0, rcqe->word1,
15284 				rcqe->word2, rcqe->word3);
15285 
15286 		/* If IV set, no further recovery */
15287 		if (bf_get(lpfc_rcqe_iv, rcqe))
15288 			break;
15289 
15290 		/* recycle consumed resource */
15291 		spin_lock_irqsave(&phba->hbalock, iflags);
15292 		lpfc_sli4_rq_release(hrq, drq);
15293 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15294 		if (!dma_buf) {
15295 			hrq->RQ_no_buf_found++;
15296 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15297 			break;
15298 		}
15299 		hrq->RQ_rcv_buf++;
15300 		hrq->RQ_buf_posted--;
15301 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15302 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15303 		break;
15304 	default:
15305 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15306 				"2576 Unexpected RQE Status x%x, w0-3 x%08x "
15307 				"x%08x x%08x x%08x\n",
15308 				status, rcqe->word0, rcqe->word1,
15309 				rcqe->word2, rcqe->word3);
15310 		break;
15311 	}
15312 out:
15313 	return workposted;
15314 }
15315 
15316 /**
15317  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
15318  * @phba: adapter with cq
15319  * @cq: Pointer to the completion queue.
15320  * @cqe: Pointer to fast-path completion queue entry.
15321  *
15322  * This routine process a fast-path work queue completion entry from fast-path
15323  * event queue for FCP command response completion.
15324  *
15325  * Return: true if work posted to worker thread, otherwise false.
15326  **/
15327 static bool
lpfc_sli4_fp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)15328 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15329 			 struct lpfc_cqe *cqe)
15330 {
15331 	struct lpfc_wcqe_release wcqe;
15332 	bool workposted = false;
15333 
15334 	/* Copy the work queue CQE and convert endian order if needed */
15335 	lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
15336 
15337 	/* Check and process for different type of WCQE and dispatch */
15338 	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15339 	case CQE_CODE_COMPL_WQE:
15340 	case CQE_CODE_NVME_ERSP:
15341 		cq->CQ_wq++;
15342 		/* Process the WQ complete event */
15343 		phba->last_completion_time = jiffies;
15344 		if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
15345 			lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
15346 				(struct lpfc_wcqe_complete *)&wcqe);
15347 		break;
15348 	case CQE_CODE_RELEASE_WQE:
15349 		cq->CQ_release_wqe++;
15350 		/* Process the WQ release event */
15351 		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15352 				(struct lpfc_wcqe_release *)&wcqe);
15353 		break;
15354 	case CQE_CODE_XRI_ABORTED:
15355 		cq->CQ_xri_aborted++;
15356 		/* Process the WQ XRI abort event */
15357 		phba->last_completion_time = jiffies;
15358 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15359 				(struct sli4_wcqe_xri_aborted *)&wcqe);
15360 		break;
15361 	case CQE_CODE_RECEIVE_V1:
15362 	case CQE_CODE_RECEIVE:
15363 		phba->last_completion_time = jiffies;
15364 		if (cq->subtype == LPFC_NVMET) {
15365 			workposted = lpfc_sli4_nvmet_handle_rcqe(
15366 				phba, cq, (struct lpfc_rcqe *)&wcqe);
15367 		}
15368 		break;
15369 	default:
15370 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15371 				"0144 Not a valid CQE code: x%x\n",
15372 				bf_get(lpfc_wcqe_c_code, &wcqe));
15373 		break;
15374 	}
15375 	return workposted;
15376 }
15377 
15378 /**
15379  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15380  * @cq: Pointer to CQ to be processed
15381  *
15382  * This routine calls the cq processing routine with the handler for
15383  * fast path CQEs.
15384  *
15385  * The CQ routine returns two values: the first is the calling status,
15386  * which indicates whether work was queued to the  background discovery
15387  * thread. If true, the routine should wakeup the discovery thread;
15388  * the second is the delay parameter. If non-zero, rather than rearming
15389  * the CQ and yet another interrupt, the CQ handler should be queued so
15390  * that it is processed in a subsequent polling action. The value of
15391  * the delay indicates when to reschedule it.
15392  **/
15393 static void
__lpfc_sli4_hba_process_cq(struct lpfc_queue * cq)15394 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
15395 {
15396 	struct lpfc_hba *phba = cq->phba;
15397 	unsigned long delay;
15398 	bool workposted = false;
15399 	int ret;
15400 
15401 	/* process and rearm the CQ */
15402 	workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
15403 					     &delay);
15404 
15405 	if (delay) {
15406 		if (is_kdump_kernel())
15407 			ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15408 						delay);
15409 		else
15410 			ret = queue_delayed_work_on(cq->chann, phba->wq,
15411 						&cq->sched_irqwork, delay);
15412 		if (!ret)
15413 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15414 					"0367 Cannot schedule queue work "
15415 					"for cqid=%d on CPU %d\n",
15416 					cq->queue_id, cq->chann);
15417 	}
15418 
15419 	/* wake up worker thread if there are works to be done */
15420 	if (workposted)
15421 		lpfc_worker_wake_up(phba);
15422 }
15423 
15424 /**
15425  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15426  *   interrupt
15427  * @work: pointer to work element
15428  *
15429  * translates from the work handler and calls the fast-path handler.
15430  **/
15431 static void
lpfc_sli4_hba_process_cq(struct work_struct * work)15432 lpfc_sli4_hba_process_cq(struct work_struct *work)
15433 {
15434 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
15435 
15436 	__lpfc_sli4_hba_process_cq(cq);
15437 }
15438 
15439 /**
15440  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
15441  * @phba: Pointer to HBA context object.
15442  * @eq: Pointer to the queue structure.
15443  * @eqe: Pointer to fast-path event queue entry.
15444  * @poll_mode: poll_mode to execute processing the cq.
15445  *
15446  * This routine process a event queue entry from the fast-path event queue.
15447  * It will check the MajorCode and MinorCode to determine this is for a
15448  * completion event on a completion queue, if not, an error shall be logged
15449  * and just return. Otherwise, it will get to the corresponding completion
15450  * queue and process all the entries on the completion queue, rearm the
15451  * completion queue, and then return.
15452  **/
15453 static void
lpfc_sli4_hba_handle_eqe(struct lpfc_hba * phba,struct lpfc_queue * eq,struct lpfc_eqe * eqe,enum lpfc_poll_mode poll_mode)15454 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15455 			 struct lpfc_eqe *eqe, enum lpfc_poll_mode poll_mode)
15456 {
15457 	struct lpfc_queue *cq = NULL;
15458 	uint32_t qidx = eq->hdwq;
15459 	uint16_t cqid, id;
15460 	int ret;
15461 
15462 	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
15463 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15464 				"0366 Not a valid completion "
15465 				"event: majorcode=x%x, minorcode=x%x\n",
15466 				bf_get_le32(lpfc_eqe_major_code, eqe),
15467 				bf_get_le32(lpfc_eqe_minor_code, eqe));
15468 		return;
15469 	}
15470 
15471 	/* Get the reference to the corresponding CQ */
15472 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15473 
15474 	/* Use the fast lookup method first */
15475 	if (cqid <= phba->sli4_hba.cq_max) {
15476 		cq = phba->sli4_hba.cq_lookup[cqid];
15477 		if (cq)
15478 			goto  work_cq;
15479 	}
15480 
15481 	/* Next check for NVMET completion */
15482 	if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15483 		id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15484 		if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15485 			/* Process NVMET unsol rcv */
15486 			cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15487 			goto  process_cq;
15488 		}
15489 	}
15490 
15491 	if (phba->sli4_hba.nvmels_cq &&
15492 	    (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15493 		/* Process NVME unsol rcv */
15494 		cq = phba->sli4_hba.nvmels_cq;
15495 	}
15496 
15497 	/* Otherwise this is a Slow path event */
15498 	if (cq == NULL) {
15499 		lpfc_sli4_sp_handle_eqe(phba, eqe,
15500 					phba->sli4_hba.hdwq[qidx].hba_eq);
15501 		return;
15502 	}
15503 
15504 process_cq:
15505 	if (unlikely(cqid != cq->queue_id)) {
15506 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15507 				"0368 Miss-matched fast-path completion "
15508 				"queue identifier: eqcqid=%d, fcpcqid=%d\n",
15509 				cqid, cq->queue_id);
15510 		return;
15511 	}
15512 
15513 work_cq:
15514 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15515 	if (phba->ktime_on)
15516 		cq->isr_timestamp = ktime_get_ns();
15517 	else
15518 		cq->isr_timestamp = 0;
15519 #endif
15520 
15521 	switch (poll_mode) {
15522 	case LPFC_THREADED_IRQ:
15523 		__lpfc_sli4_hba_process_cq(cq);
15524 		break;
15525 	case LPFC_QUEUE_WORK:
15526 	default:
15527 		if (is_kdump_kernel())
15528 			ret = queue_work(phba->wq, &cq->irqwork);
15529 		else
15530 			ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15531 		if (!ret)
15532 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15533 					"0383 Cannot schedule queue work "
15534 					"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15535 					cqid, cq->queue_id,
15536 					raw_smp_processor_id());
15537 		break;
15538 	}
15539 }
15540 
15541 /**
15542  * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
15543  * @work: pointer to work element
15544  *
15545  * translates from the work handler and calls the fast-path handler.
15546  **/
15547 static void
lpfc_sli4_dly_hba_process_cq(struct work_struct * work)15548 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
15549 {
15550 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15551 					struct lpfc_queue, sched_irqwork);
15552 
15553 	__lpfc_sli4_hba_process_cq(cq);
15554 }
15555 
15556 /**
15557  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
15558  * @irq: Interrupt number.
15559  * @dev_id: The device context pointer.
15560  *
15561  * This function is directly called from the PCI layer as an interrupt
15562  * service routine when device with SLI-4 interface spec is enabled with
15563  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15564  * ring event in the HBA. However, when the device is enabled with either
15565  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15566  * device-level interrupt handler. When the PCI slot is in error recovery
15567  * or the HBA is undergoing initialization, the interrupt handler will not
15568  * process the interrupt. The SCSI FCP fast-path ring event are handled in
15569  * the interrupt context. This function is called without any lock held.
15570  * It gets the hbalock to access and update SLI data structures. Note that,
15571  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15572  * equal to that of FCP CQ index.
15573  *
15574  * The link attention and ELS ring attention events are handled
15575  * by the worker thread. The interrupt handler signals the worker thread
15576  * and returns for these events. This function is called without any lock
15577  * held. It gets the hbalock to access and update SLI data structures.
15578  *
15579  * This function returns IRQ_HANDLED when interrupt is handled, IRQ_WAKE_THREAD
15580  * when interrupt is scheduled to be handled from a threaded irq context, or
15581  * else returns IRQ_NONE.
15582  **/
15583 irqreturn_t
lpfc_sli4_hba_intr_handler(int irq,void * dev_id)15584 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
15585 {
15586 	struct lpfc_hba *phba;
15587 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
15588 	struct lpfc_queue *fpeq;
15589 	unsigned long iflag;
15590 	int hba_eqidx;
15591 	int ecount = 0;
15592 	struct lpfc_eq_intr_info *eqi;
15593 
15594 	/* Get the driver's phba structure from the dev_id */
15595 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15596 	phba = hba_eq_hdl->phba;
15597 	hba_eqidx = hba_eq_hdl->idx;
15598 
15599 	if (unlikely(!phba))
15600 		return IRQ_NONE;
15601 	if (unlikely(!phba->sli4_hba.hdwq))
15602 		return IRQ_NONE;
15603 
15604 	/* Get to the EQ struct associated with this vector */
15605 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
15606 	if (unlikely(!fpeq))
15607 		return IRQ_NONE;
15608 
15609 	/* Check device state for handling interrupt */
15610 	if (unlikely(lpfc_intr_state_check(phba))) {
15611 		/* Check again for link_state with lock held */
15612 		spin_lock_irqsave(&phba->hbalock, iflag);
15613 		if (phba->link_state < LPFC_LINK_DOWN)
15614 			/* Flush, clear interrupt, and rearm the EQ */
15615 			lpfc_sli4_eqcq_flush(phba, fpeq);
15616 		spin_unlock_irqrestore(&phba->hbalock, iflag);
15617 		return IRQ_NONE;
15618 	}
15619 
15620 	switch (fpeq->poll_mode) {
15621 	case LPFC_THREADED_IRQ:
15622 		/* CGN mgmt is mutually exclusive from irq processing */
15623 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
15624 			return IRQ_WAKE_THREAD;
15625 		fallthrough;
15626 	case LPFC_QUEUE_WORK:
15627 	default:
15628 		eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15629 		eqi->icnt++;
15630 
15631 		fpeq->last_cpu = raw_smp_processor_id();
15632 
15633 		if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
15634 		    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
15635 		    phba->cfg_auto_imax &&
15636 		    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15637 		    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15638 			lpfc_sli4_mod_hba_eq_delay(phba, fpeq,
15639 						   LPFC_MAX_AUTO_EQ_DELAY);
15640 
15641 		/* process and rearm the EQ */
15642 		ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
15643 					      LPFC_QUEUE_WORK);
15644 
15645 		if (unlikely(ecount == 0)) {
15646 			fpeq->EQ_no_entry++;
15647 			if (phba->intr_type == MSIX)
15648 				/* MSI-X treated interrupt served as no EQ share INT */
15649 				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15650 						"0358 MSI-X interrupt with no EQE\n");
15651 			else
15652 				/* Non MSI-X treated on interrupt as EQ share INT */
15653 				return IRQ_NONE;
15654 		}
15655 	}
15656 
15657 	return IRQ_HANDLED;
15658 } /* lpfc_sli4_hba_intr_handler */
15659 
15660 /**
15661  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15662  * @irq: Interrupt number.
15663  * @dev_id: The device context pointer.
15664  *
15665  * This function is the device-level interrupt handler to device with SLI-4
15666  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15667  * interrupt mode is enabled and there is an event in the HBA which requires
15668  * driver attention. This function invokes the slow-path interrupt attention
15669  * handling function and fast-path interrupt attention handling function in
15670  * turn to process the relevant HBA attention events. This function is called
15671  * without any lock held. It gets the hbalock to access and update SLI data
15672  * structures.
15673  *
15674  * This function returns IRQ_HANDLED when interrupt is handled, else it
15675  * returns IRQ_NONE.
15676  **/
15677 irqreturn_t
lpfc_sli4_intr_handler(int irq,void * dev_id)15678 lpfc_sli4_intr_handler(int irq, void *dev_id)
15679 {
15680 	struct lpfc_hba  *phba;
15681 	irqreturn_t hba_irq_rc;
15682 	bool hba_handled = false;
15683 	int qidx;
15684 
15685 	/* Get the driver's phba structure from the dev_id */
15686 	phba = (struct lpfc_hba *)dev_id;
15687 
15688 	if (unlikely(!phba))
15689 		return IRQ_NONE;
15690 
15691 	/*
15692 	 * Invoke fast-path host attention interrupt handling as appropriate.
15693 	 */
15694 	for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
15695 		hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
15696 					&phba->sli4_hba.hba_eq_hdl[qidx]);
15697 		if (hba_irq_rc == IRQ_HANDLED)
15698 			hba_handled |= true;
15699 	}
15700 
15701 	return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
15702 } /* lpfc_sli4_intr_handler */
15703 
lpfc_sli4_poll_hbtimer(struct timer_list * t)15704 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15705 {
15706 	struct lpfc_hba *phba = timer_container_of(phba, t, cpuhp_poll_timer);
15707 	struct lpfc_queue *eq;
15708 
15709 	rcu_read_lock();
15710 
15711 	list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15712 		lpfc_sli4_poll_eq(eq);
15713 	if (!list_empty(&phba->poll_list))
15714 		mod_timer(&phba->cpuhp_poll_timer,
15715 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15716 
15717 	rcu_read_unlock();
15718 }
15719 
lpfc_sli4_add_to_poll_list(struct lpfc_queue * eq)15720 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15721 {
15722 	struct lpfc_hba *phba = eq->phba;
15723 
15724 	/* kickstart slowpath processing if needed */
15725 	if (list_empty(&phba->poll_list))
15726 		mod_timer(&phba->cpuhp_poll_timer,
15727 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15728 
15729 	list_add_rcu(&eq->_poll_list, &phba->poll_list);
15730 	synchronize_rcu();
15731 }
15732 
lpfc_sli4_remove_from_poll_list(struct lpfc_queue * eq)15733 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15734 {
15735 	struct lpfc_hba *phba = eq->phba;
15736 
15737 	/* Disable slowpath processing for this eq.  Kick start the eq
15738 	 * by RE-ARMING the eq's ASAP
15739 	 */
15740 	list_del_rcu(&eq->_poll_list);
15741 	synchronize_rcu();
15742 
15743 	if (list_empty(&phba->poll_list))
15744 		timer_delete_sync(&phba->cpuhp_poll_timer);
15745 }
15746 
lpfc_sli4_cleanup_poll_list(struct lpfc_hba * phba)15747 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
15748 {
15749 	struct lpfc_queue *eq, *next;
15750 
15751 	list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15752 		list_del(&eq->_poll_list);
15753 
15754 	INIT_LIST_HEAD(&phba->poll_list);
15755 	synchronize_rcu();
15756 }
15757 
15758 static inline void
__lpfc_sli4_switch_eqmode(struct lpfc_queue * eq,uint8_t mode)15759 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15760 {
15761 	if (mode == eq->mode)
15762 		return;
15763 	/*
15764 	 * currently this function is only called during a hotplug
15765 	 * event and the cpu on which this function is executing
15766 	 * is going offline.  By now the hotplug has instructed
15767 	 * the scheduler to remove this cpu from cpu active mask.
15768 	 * So we don't need to work about being put aside by the
15769 	 * scheduler for a high priority process.  Yes, the inte-
15770 	 * rrupts could come but they are known to retire ASAP.
15771 	 */
15772 
15773 	/* Disable polling in the fastpath */
15774 	WRITE_ONCE(eq->mode, mode);
15775 	/* flush out the store buffer */
15776 	smp_wmb();
15777 
15778 	/*
15779 	 * Add this eq to the polling list and start polling. For
15780 	 * a grace period both interrupt handler and poller will
15781 	 * try to process the eq _but_ that's fine.  We have a
15782 	 * synchronization mechanism in place (queue_claimed) to
15783 	 * deal with it.  This is just a draining phase for int-
15784 	 * errupt handler (not eq's) as we have guranteed through
15785 	 * barrier that all the CPUs have seen the new CQ_POLLED
15786 	 * state. which will effectively disable the REARMING of
15787 	 * the EQ.  The whole idea is eq's die off eventually as
15788 	 * we are not rearming EQ's anymore.
15789 	 */
15790 	mode ? lpfc_sli4_add_to_poll_list(eq) :
15791 	       lpfc_sli4_remove_from_poll_list(eq);
15792 }
15793 
lpfc_sli4_start_polling(struct lpfc_queue * eq)15794 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15795 {
15796 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15797 }
15798 
lpfc_sli4_stop_polling(struct lpfc_queue * eq)15799 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15800 {
15801 	struct lpfc_hba *phba = eq->phba;
15802 
15803 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15804 
15805 	/* Kick start for the pending io's in h/w.
15806 	 * Once we switch back to interrupt processing on a eq
15807 	 * the io path completion will only arm eq's when it
15808 	 * receives a completion.  But since eq's are in disa-
15809 	 * rmed state it doesn't receive a completion.  This
15810 	 * creates a deadlock scenaro.
15811 	 */
15812 	phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15813 }
15814 
15815 /**
15816  * lpfc_sli4_queue_free - free a queue structure and associated memory
15817  * @queue: The queue structure to free.
15818  *
15819  * This function frees a queue structure and the DMAable memory used for
15820  * the host resident queue. This function must be called after destroying the
15821  * queue on the HBA.
15822  **/
15823 void
lpfc_sli4_queue_free(struct lpfc_queue * queue)15824 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15825 {
15826 	struct lpfc_dmabuf *dmabuf;
15827 
15828 	if (!queue)
15829 		return;
15830 
15831 	if (!list_empty(&queue->wq_list))
15832 		list_del(&queue->wq_list);
15833 
15834 	while (!list_empty(&queue->page_list)) {
15835 		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15836 				 list);
15837 		dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15838 				  dmabuf->virt, dmabuf->phys);
15839 		kfree(dmabuf);
15840 	}
15841 	if (queue->rqbp) {
15842 		lpfc_free_rq_buffer(queue->phba, queue);
15843 		kfree(queue->rqbp);
15844 	}
15845 
15846 	if (!list_empty(&queue->cpu_list))
15847 		list_del(&queue->cpu_list);
15848 
15849 	kfree(queue);
15850 	return;
15851 }
15852 
15853 /**
15854  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15855  * @phba: The HBA that this queue is being created on.
15856  * @page_size: The size of a queue page
15857  * @entry_size: The size of each queue entry for this queue.
15858  * @entry_count: The number of entries that this queue will handle.
15859  * @cpu: The cpu that will primarily utilize this queue.
15860  *
15861  * This function allocates a queue structure and the DMAable memory used for
15862  * the host resident queue. This function must be called before creating the
15863  * queue on the HBA.
15864  **/
15865 struct lpfc_queue *
lpfc_sli4_queue_alloc(struct lpfc_hba * phba,uint32_t page_size,uint32_t entry_size,uint32_t entry_count,int cpu)15866 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15867 		      uint32_t entry_size, uint32_t entry_count, int cpu)
15868 {
15869 	struct lpfc_queue *queue;
15870 	struct lpfc_dmabuf *dmabuf;
15871 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15872 	uint16_t x, pgcnt;
15873 
15874 	if (!phba->sli4_hba.pc_sli4_params.supported)
15875 		hw_page_size = page_size;
15876 
15877 	pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15878 
15879 	/* If needed, Adjust page count to match the max the adapter supports */
15880 	if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15881 		pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15882 
15883 	queue = kzalloc_node(struct_size(queue, q_pgs, pgcnt),
15884 			     GFP_KERNEL, cpu_to_node(cpu));
15885 	if (!queue)
15886 		return NULL;
15887 
15888 	INIT_LIST_HEAD(&queue->list);
15889 	INIT_LIST_HEAD(&queue->_poll_list);
15890 	INIT_LIST_HEAD(&queue->wq_list);
15891 	INIT_LIST_HEAD(&queue->wqfull_list);
15892 	INIT_LIST_HEAD(&queue->page_list);
15893 	INIT_LIST_HEAD(&queue->child_list);
15894 	INIT_LIST_HEAD(&queue->cpu_list);
15895 
15896 	/* Set queue parameters now.  If the system cannot provide memory
15897 	 * resources, the free routine needs to know what was allocated.
15898 	 */
15899 	queue->page_count = pgcnt;
15900 	queue->entry_cnt_per_pg = hw_page_size / entry_size;
15901 	queue->entry_size = entry_size;
15902 	queue->entry_count = entry_count;
15903 	queue->page_size = hw_page_size;
15904 	queue->phba = phba;
15905 
15906 	for (x = 0; x < queue->page_count; x++) {
15907 		dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15908 				      dev_to_node(&phba->pcidev->dev));
15909 		if (!dmabuf)
15910 			goto out_fail;
15911 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15912 						  hw_page_size, &dmabuf->phys,
15913 						  GFP_KERNEL);
15914 		if (!dmabuf->virt) {
15915 			kfree(dmabuf);
15916 			goto out_fail;
15917 		}
15918 		dmabuf->buffer_tag = x;
15919 		list_add_tail(&dmabuf->list, &queue->page_list);
15920 		/* use lpfc_sli4_qe to index a paritcular entry in this page */
15921 		queue->q_pgs[x] = dmabuf->virt;
15922 	}
15923 	INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15924 	INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15925 	INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15926 	INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15927 
15928 	/* notify_interval will be set during q creation */
15929 
15930 	return queue;
15931 out_fail:
15932 	lpfc_sli4_queue_free(queue);
15933 	return NULL;
15934 }
15935 
15936 /**
15937  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15938  * @phba: HBA structure that indicates port to create a queue on.
15939  * @pci_barset: PCI BAR set flag.
15940  *
15941  * This function shall perform iomap of the specified PCI BAR address to host
15942  * memory address if not already done so and return it. The returned host
15943  * memory address can be NULL.
15944  */
15945 static void __iomem *
lpfc_dual_chute_pci_bar_map(struct lpfc_hba * phba,uint16_t pci_barset)15946 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15947 {
15948 	if (!phba->pcidev)
15949 		return NULL;
15950 
15951 	switch (pci_barset) {
15952 	case WQ_PCI_BAR_0_AND_1:
15953 		return phba->pci_bar0_memmap_p;
15954 	case WQ_PCI_BAR_2_AND_3:
15955 		return phba->pci_bar2_memmap_p;
15956 	case WQ_PCI_BAR_4_AND_5:
15957 		return phba->pci_bar4_memmap_p;
15958 	default:
15959 		break;
15960 	}
15961 	return NULL;
15962 }
15963 
15964 static __maybe_unused void __iomem *
lpfc_dpp_wc_map(struct lpfc_hba * phba,uint8_t dpp_barset)15965 lpfc_dpp_wc_map(struct lpfc_hba *phba, uint8_t dpp_barset)
15966 {
15967 
15968 	/* DPP region is supposed to cover 64-bit BAR2 */
15969 	if (dpp_barset != WQ_PCI_BAR_4_AND_5) {
15970 		lpfc_log_msg(phba, KERN_WARNING, LOG_INIT,
15971 			     "3273 dpp_barset x%x != WQ_PCI_BAR_4_AND_5\n",
15972 			     dpp_barset);
15973 		return NULL;
15974 	}
15975 
15976 	if (!phba->sli4_hba.dpp_regs_memmap_wc_p) {
15977 		void __iomem *dpp_map;
15978 
15979 		dpp_map = ioremap_wc(phba->pci_bar2_map,
15980 				     pci_resource_len(phba->pcidev,
15981 						      PCI_64BIT_BAR4));
15982 
15983 		if (dpp_map)
15984 			phba->sli4_hba.dpp_regs_memmap_wc_p = dpp_map;
15985 	}
15986 
15987 	return phba->sli4_hba.dpp_regs_memmap_wc_p;
15988 }
15989 
15990 /**
15991  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15992  * @phba: HBA structure that EQs are on.
15993  * @startq: The starting EQ index to modify
15994  * @numq: The number of EQs (consecutive indexes) to modify
15995  * @usdelay: amount of delay
15996  *
15997  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15998  * is set either by writing to a register (if supported by the SLI Port)
15999  * or by mailbox command. The mailbox command allows several EQs to be
16000  * updated at once.
16001  *
16002  * The @phba struct is used to send a mailbox command to HBA. The @startq
16003  * is used to get the starting EQ index to change. The @numq value is
16004  * used to specify how many consecutive EQ indexes, starting at EQ index,
16005  * are to be changed. This function is asynchronous and will wait for any
16006  * mailbox commands to finish before returning.
16007  *
16008  * On success this function will return a zero. If unable to allocate
16009  * enough memory this function will return -ENOMEM. If a mailbox command
16010  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
16011  * have had their delay multiplier changed.
16012  **/
16013 void
lpfc_modify_hba_eq_delay(struct lpfc_hba * phba,uint32_t startq,uint32_t numq,uint32_t usdelay)16014 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
16015 			 uint32_t numq, uint32_t usdelay)
16016 {
16017 	struct lpfc_mbx_modify_eq_delay *eq_delay;
16018 	LPFC_MBOXQ_t *mbox;
16019 	struct lpfc_queue *eq;
16020 	int cnt = 0, rc, length;
16021 	uint32_t shdr_status, shdr_add_status;
16022 	uint32_t dmult;
16023 	int qidx;
16024 	union lpfc_sli4_cfg_shdr *shdr;
16025 
16026 	if (startq >= phba->cfg_irq_chann)
16027 		return;
16028 
16029 	if (usdelay > 0xFFFF) {
16030 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
16031 				"6429 usdelay %d too large. Scaled down to "
16032 				"0xFFFF.\n", usdelay);
16033 		usdelay = 0xFFFF;
16034 	}
16035 
16036 	/* set values by EQ_DELAY register if supported */
16037 	if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
16038 		for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16039 			eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16040 			if (!eq)
16041 				continue;
16042 
16043 			lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
16044 
16045 			if (++cnt >= numq)
16046 				break;
16047 		}
16048 		return;
16049 	}
16050 
16051 	/* Otherwise, set values by mailbox cmd */
16052 
16053 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16054 	if (!mbox) {
16055 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16056 				"6428 Failed allocating mailbox cmd buffer."
16057 				" EQ delay was not set.\n");
16058 		return;
16059 	}
16060 	length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
16061 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16062 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16063 			 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
16064 			 length, LPFC_SLI4_MBX_EMBED);
16065 	eq_delay = &mbox->u.mqe.un.eq_delay;
16066 
16067 	/* Calculate delay multiper from maximum interrupt per second */
16068 	dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
16069 	if (dmult)
16070 		dmult--;
16071 	if (dmult > LPFC_DMULT_MAX)
16072 		dmult = LPFC_DMULT_MAX;
16073 
16074 	for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16075 		eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16076 		if (!eq)
16077 			continue;
16078 		eq->q_mode = usdelay;
16079 		eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16080 		eq_delay->u.request.eq[cnt].phase = 0;
16081 		eq_delay->u.request.eq[cnt].delay_multi = dmult;
16082 
16083 		if (++cnt >= numq)
16084 			break;
16085 	}
16086 	eq_delay->u.request.num_eq = cnt;
16087 
16088 	mbox->vport = phba->pport;
16089 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16090 	mbox->ctx_ndlp = NULL;
16091 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16092 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16093 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16094 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16095 	if (shdr_status || shdr_add_status || rc) {
16096 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16097 				"2512 MODIFY_EQ_DELAY mailbox failed with "
16098 				"status x%x add_status x%x, mbx status x%x\n",
16099 				shdr_status, shdr_add_status, rc);
16100 	}
16101 	mempool_free(mbox, phba->mbox_mem_pool);
16102 	return;
16103 }
16104 
16105 /**
16106  * lpfc_eq_create - Create an Event Queue on the HBA
16107  * @phba: HBA structure that indicates port to create a queue on.
16108  * @eq: The queue structure to use to create the event queue.
16109  * @imax: The maximum interrupt per second limit.
16110  *
16111  * This function creates an event queue, as detailed in @eq, on a port,
16112  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16113  *
16114  * The @phba struct is used to send mailbox command to HBA. The @eq struct
16115  * is used to get the entry count and entry size that are necessary to
16116  * determine the number of pages to allocate and use for this queue. This
16117  * function will send the EQ_CREATE mailbox command to the HBA to setup the
16118  * event queue. This function is asynchronous and will wait for the mailbox
16119  * command to finish before continuing.
16120  *
16121  * On success this function will return a zero. If unable to allocate enough
16122  * memory this function will return -ENOMEM. If the queue create mailbox command
16123  * fails this function will return -ENXIO.
16124  **/
16125 int
lpfc_eq_create(struct lpfc_hba * phba,struct lpfc_queue * eq,uint32_t imax)16126 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
16127 {
16128 	struct lpfc_mbx_eq_create *eq_create;
16129 	LPFC_MBOXQ_t *mbox;
16130 	int rc, length, status = 0;
16131 	struct lpfc_dmabuf *dmabuf;
16132 	uint32_t shdr_status, shdr_add_status;
16133 	union lpfc_sli4_cfg_shdr *shdr;
16134 	uint16_t dmult;
16135 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16136 
16137 	/* sanity check on queue memory */
16138 	if (!eq)
16139 		return -ENODEV;
16140 	if (!phba->sli4_hba.pc_sli4_params.supported)
16141 		hw_page_size = SLI4_PAGE_SIZE;
16142 
16143 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16144 	if (!mbox)
16145 		return -ENOMEM;
16146 	length = (sizeof(struct lpfc_mbx_eq_create) -
16147 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16148 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16149 			 LPFC_MBOX_OPCODE_EQ_CREATE,
16150 			 length, LPFC_SLI4_MBX_EMBED);
16151 	eq_create = &mbox->u.mqe.un.eq_create;
16152 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
16153 	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16154 	       eq->page_count);
16155 	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16156 	       LPFC_EQE_SIZE);
16157 	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
16158 
16159 	/* Use version 2 of CREATE_EQ if eqav is set */
16160 	if (phba->sli4_hba.pc_sli4_params.eqav) {
16161 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16162 		       LPFC_Q_CREATE_VERSION_2);
16163 		bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16164 		       phba->sli4_hba.pc_sli4_params.eqav);
16165 	}
16166 
16167 	/* don't setup delay multiplier using EQ_CREATE */
16168 	dmult = 0;
16169 	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16170 	       dmult);
16171 	switch (eq->entry_count) {
16172 	default:
16173 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16174 				"0360 Unsupported EQ count. (%d)\n",
16175 				eq->entry_count);
16176 		if (eq->entry_count < 256) {
16177 			status = -EINVAL;
16178 			goto out;
16179 		}
16180 		fallthrough;	/* otherwise default to smallest count */
16181 	case 256:
16182 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16183 		       LPFC_EQ_CNT_256);
16184 		break;
16185 	case 512:
16186 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16187 		       LPFC_EQ_CNT_512);
16188 		break;
16189 	case 1024:
16190 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16191 		       LPFC_EQ_CNT_1024);
16192 		break;
16193 	case 2048:
16194 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16195 		       LPFC_EQ_CNT_2048);
16196 		break;
16197 	case 4096:
16198 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16199 		       LPFC_EQ_CNT_4096);
16200 		break;
16201 	}
16202 	list_for_each_entry(dmabuf, &eq->page_list, list) {
16203 		memset(dmabuf->virt, 0, hw_page_size);
16204 		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16205 					putPaddrLow(dmabuf->phys);
16206 		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16207 					putPaddrHigh(dmabuf->phys);
16208 	}
16209 	mbox->vport = phba->pport;
16210 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16211 	mbox->ctx_buf = NULL;
16212 	mbox->ctx_ndlp = NULL;
16213 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16214 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16215 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16216 	if (shdr_status || shdr_add_status || rc) {
16217 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16218 				"2500 EQ_CREATE mailbox failed with "
16219 				"status x%x add_status x%x, mbx status x%x\n",
16220 				shdr_status, shdr_add_status, rc);
16221 		status = -ENXIO;
16222 	}
16223 	eq->type = LPFC_EQ;
16224 	eq->subtype = LPFC_NONE;
16225 	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16226 	if (eq->queue_id == 0xFFFF)
16227 		status = -ENXIO;
16228 	eq->host_index = 0;
16229 	eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16230 	eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
16231 out:
16232 	mempool_free(mbox, phba->mbox_mem_pool);
16233 	return status;
16234 }
16235 
16236 /**
16237  * lpfc_sli4_hba_intr_handler_th - SLI4 HBA threaded interrupt handler
16238  * @irq: Interrupt number.
16239  * @dev_id: The device context pointer.
16240  *
16241  * This routine is a mirror of lpfc_sli4_hba_intr_handler, but executed within
16242  * threaded irq context.
16243  *
16244  * Returns
16245  * IRQ_HANDLED - interrupt is handled
16246  * IRQ_NONE - otherwise
16247  **/
lpfc_sli4_hba_intr_handler_th(int irq,void * dev_id)16248 irqreturn_t lpfc_sli4_hba_intr_handler_th(int irq, void *dev_id)
16249 {
16250 	struct lpfc_hba *phba;
16251 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
16252 	struct lpfc_queue *fpeq;
16253 	int ecount = 0;
16254 	int hba_eqidx;
16255 	struct lpfc_eq_intr_info *eqi;
16256 
16257 	/* Get the driver's phba structure from the dev_id */
16258 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
16259 	phba = hba_eq_hdl->phba;
16260 	hba_eqidx = hba_eq_hdl->idx;
16261 
16262 	if (unlikely(!phba))
16263 		return IRQ_NONE;
16264 	if (unlikely(!phba->sli4_hba.hdwq))
16265 		return IRQ_NONE;
16266 
16267 	/* Get to the EQ struct associated with this vector */
16268 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
16269 	if (unlikely(!fpeq))
16270 		return IRQ_NONE;
16271 
16272 	eqi = per_cpu_ptr(phba->sli4_hba.eq_info, raw_smp_processor_id());
16273 	eqi->icnt++;
16274 
16275 	fpeq->last_cpu = raw_smp_processor_id();
16276 
16277 	if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
16278 	    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
16279 	    phba->cfg_auto_imax &&
16280 	    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
16281 	    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
16282 		lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
16283 
16284 	/* process and rearm the EQ */
16285 	ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
16286 				      LPFC_THREADED_IRQ);
16287 
16288 	if (unlikely(ecount == 0)) {
16289 		fpeq->EQ_no_entry++;
16290 		if (phba->intr_type == MSIX)
16291 			/* MSI-X treated interrupt served as no EQ share INT */
16292 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16293 					"3358 MSI-X interrupt with no EQE\n");
16294 		else
16295 			/* Non MSI-X treated on interrupt as EQ share INT */
16296 			return IRQ_NONE;
16297 	}
16298 	return IRQ_HANDLED;
16299 }
16300 
16301 /**
16302  * lpfc_cq_create - Create a Completion Queue on the HBA
16303  * @phba: HBA structure that indicates port to create a queue on.
16304  * @cq: The queue structure to use to create the completion queue.
16305  * @eq: The event queue to bind this completion queue to.
16306  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16307  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16308  *
16309  * This function creates a completion queue, as detailed in @wq, on a port,
16310  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16311  *
16312  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16313  * is used to get the entry count and entry size that are necessary to
16314  * determine the number of pages to allocate and use for this queue. The @eq
16315  * is used to indicate which event queue to bind this completion queue to. This
16316  * function will send the CQ_CREATE mailbox command to the HBA to setup the
16317  * completion queue. This function is asynchronous and will wait for the mailbox
16318  * command to finish before continuing.
16319  *
16320  * On success this function will return a zero. If unable to allocate enough
16321  * memory this function will return -ENOMEM. If the queue create mailbox command
16322  * fails this function will return -ENXIO.
16323  **/
16324 int
lpfc_cq_create(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_queue * eq,uint32_t type,uint32_t subtype)16325 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16326 	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16327 {
16328 	struct lpfc_mbx_cq_create *cq_create;
16329 	struct lpfc_dmabuf *dmabuf;
16330 	LPFC_MBOXQ_t *mbox;
16331 	int rc, length, status = 0;
16332 	uint32_t shdr_status, shdr_add_status;
16333 	union lpfc_sli4_cfg_shdr *shdr;
16334 
16335 	/* sanity check on queue memory */
16336 	if (!cq || !eq)
16337 		return -ENODEV;
16338 
16339 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16340 	if (!mbox)
16341 		return -ENOMEM;
16342 	length = (sizeof(struct lpfc_mbx_cq_create) -
16343 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16344 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16345 			 LPFC_MBOX_OPCODE_CQ_CREATE,
16346 			 length, LPFC_SLI4_MBX_EMBED);
16347 	cq_create = &mbox->u.mqe.un.cq_create;
16348 	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
16349 	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16350 		    cq->page_count);
16351 	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16352 	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
16353 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16354 	       phba->sli4_hba.pc_sli4_params.cqv);
16355 	if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
16356 		bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16357 		       (cq->page_size / SLI4_PAGE_SIZE));
16358 		bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16359 		       eq->queue_id);
16360 		bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16361 		       phba->sli4_hba.pc_sli4_params.cqav);
16362 	} else {
16363 		bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16364 		       eq->queue_id);
16365 	}
16366 	switch (cq->entry_count) {
16367 	case 2048:
16368 	case 4096:
16369 		if (phba->sli4_hba.pc_sli4_params.cqv ==
16370 		    LPFC_Q_CREATE_VERSION_2) {
16371 			cq_create->u.request.context.lpfc_cq_context_count =
16372 				cq->entry_count;
16373 			bf_set(lpfc_cq_context_count,
16374 			       &cq_create->u.request.context,
16375 			       LPFC_CQ_CNT_WORD7);
16376 			break;
16377 		}
16378 		fallthrough;
16379 	default:
16380 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16381 				"0361 Unsupported CQ count: "
16382 				"entry cnt %d sz %d pg cnt %d\n",
16383 				cq->entry_count, cq->entry_size,
16384 				cq->page_count);
16385 		if (cq->entry_count < 256) {
16386 			status = -EINVAL;
16387 			goto out;
16388 		}
16389 		fallthrough;	/* otherwise default to smallest count */
16390 	case 256:
16391 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16392 		       LPFC_CQ_CNT_256);
16393 		break;
16394 	case 512:
16395 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16396 		       LPFC_CQ_CNT_512);
16397 		break;
16398 	case 1024:
16399 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16400 		       LPFC_CQ_CNT_1024);
16401 		break;
16402 	}
16403 	list_for_each_entry(dmabuf, &cq->page_list, list) {
16404 		memset(dmabuf->virt, 0, cq->page_size);
16405 		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16406 					putPaddrLow(dmabuf->phys);
16407 		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16408 					putPaddrHigh(dmabuf->phys);
16409 	}
16410 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16411 
16412 	/* The IOCTL status is embedded in the mailbox subheader. */
16413 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16414 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16415 	if (shdr_status || shdr_add_status || rc) {
16416 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16417 				"2501 CQ_CREATE mailbox failed with "
16418 				"status x%x add_status x%x, mbx status x%x\n",
16419 				shdr_status, shdr_add_status, rc);
16420 		status = -ENXIO;
16421 		goto out;
16422 	}
16423 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16424 	if (cq->queue_id == 0xFFFF) {
16425 		status = -ENXIO;
16426 		goto out;
16427 	}
16428 	/* link the cq onto the parent eq child list */
16429 	list_add_tail(&cq->list, &eq->child_list);
16430 	/* Set up completion queue's type and subtype */
16431 	cq->type = type;
16432 	cq->subtype = subtype;
16433 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16434 	cq->assoc_qid = eq->queue_id;
16435 	cq->assoc_qp = eq;
16436 	cq->host_index = 0;
16437 	cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16438 	cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
16439 
16440 	if (cq->queue_id > phba->sli4_hba.cq_max)
16441 		phba->sli4_hba.cq_max = cq->queue_id;
16442 out:
16443 	mempool_free(mbox, phba->mbox_mem_pool);
16444 	return status;
16445 }
16446 
16447 /**
16448  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16449  * @phba: HBA structure that indicates port to create a queue on.
16450  * @cqp: The queue structure array to use to create the completion queues.
16451  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
16452  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16453  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16454  *
16455  * This function creates a set of  completion queue, s to support MRQ
16456  * as detailed in @cqp, on a port,
16457  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16458  *
16459  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16460  * is used to get the entry count and entry size that are necessary to
16461  * determine the number of pages to allocate and use for this queue. The @eq
16462  * is used to indicate which event queue to bind this completion queue to. This
16463  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16464  * completion queue. This function is asynchronous and will wait for the mailbox
16465  * command to finish before continuing.
16466  *
16467  * On success this function will return a zero. If unable to allocate enough
16468  * memory this function will return -ENOMEM. If the queue create mailbox command
16469  * fails this function will return -ENXIO.
16470  **/
16471 int
lpfc_cq_create_set(struct lpfc_hba * phba,struct lpfc_queue ** cqp,struct lpfc_sli4_hdw_queue * hdwq,uint32_t type,uint32_t subtype)16472 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
16473 		   struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16474 		   uint32_t subtype)
16475 {
16476 	struct lpfc_queue *cq;
16477 	struct lpfc_queue *eq;
16478 	struct lpfc_mbx_cq_create_set *cq_set;
16479 	struct lpfc_dmabuf *dmabuf;
16480 	LPFC_MBOXQ_t *mbox;
16481 	int rc, length, alloclen, status = 0;
16482 	int cnt, idx, numcq, page_idx = 0;
16483 	uint32_t shdr_status, shdr_add_status;
16484 	union lpfc_sli4_cfg_shdr *shdr;
16485 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16486 
16487 	/* sanity check on queue memory */
16488 	numcq = phba->cfg_nvmet_mrq;
16489 	if (!cqp || !hdwq || !numcq)
16490 		return -ENODEV;
16491 
16492 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16493 	if (!mbox)
16494 		return -ENOMEM;
16495 
16496 	length = sizeof(struct lpfc_mbx_cq_create_set);
16497 	length += ((numcq * cqp[0]->page_count) *
16498 		   sizeof(struct dma_address));
16499 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16500 			LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16501 			LPFC_SLI4_MBX_NEMBED);
16502 	if (alloclen < length) {
16503 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16504 				"3098 Allocated DMA memory size (%d) is "
16505 				"less than the requested DMA memory size "
16506 				"(%d)\n", alloclen, length);
16507 		status = -ENOMEM;
16508 		goto out;
16509 	}
16510 	cq_set = mbox->sge_array->addr[0];
16511 	shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16512 	bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16513 
16514 	for (idx = 0; idx < numcq; idx++) {
16515 		cq = cqp[idx];
16516 		eq = hdwq[idx].hba_eq;
16517 		if (!cq || !eq) {
16518 			status = -ENOMEM;
16519 			goto out;
16520 		}
16521 		if (!phba->sli4_hba.pc_sli4_params.supported)
16522 			hw_page_size = cq->page_size;
16523 
16524 		switch (idx) {
16525 		case 0:
16526 			bf_set(lpfc_mbx_cq_create_set_page_size,
16527 			       &cq_set->u.request,
16528 			       (hw_page_size / SLI4_PAGE_SIZE));
16529 			bf_set(lpfc_mbx_cq_create_set_num_pages,
16530 			       &cq_set->u.request, cq->page_count);
16531 			bf_set(lpfc_mbx_cq_create_set_evt,
16532 			       &cq_set->u.request, 1);
16533 			bf_set(lpfc_mbx_cq_create_set_valid,
16534 			       &cq_set->u.request, 1);
16535 			bf_set(lpfc_mbx_cq_create_set_cqe_size,
16536 			       &cq_set->u.request, 0);
16537 			bf_set(lpfc_mbx_cq_create_set_num_cq,
16538 			       &cq_set->u.request, numcq);
16539 			bf_set(lpfc_mbx_cq_create_set_autovalid,
16540 			       &cq_set->u.request,
16541 			       phba->sli4_hba.pc_sli4_params.cqav);
16542 			switch (cq->entry_count) {
16543 			case 2048:
16544 			case 4096:
16545 				if (phba->sli4_hba.pc_sli4_params.cqv ==
16546 				    LPFC_Q_CREATE_VERSION_2) {
16547 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt_lo,
16548 					       &cq_set->u.request,
16549 					       cq->entry_count);
16550 					bf_set(lpfc_mbx_cq_create_set_cqecnt,
16551 					       &cq_set->u.request,
16552 					       LPFC_CQ_CNT_WORD7);
16553 					break;
16554 				}
16555 				fallthrough;
16556 			default:
16557 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16558 						"3118 Bad CQ count. (%d)\n",
16559 						cq->entry_count);
16560 				if (cq->entry_count < 256) {
16561 					status = -EINVAL;
16562 					goto out;
16563 				}
16564 				fallthrough;	/* otherwise default to smallest */
16565 			case 256:
16566 				bf_set(lpfc_mbx_cq_create_set_cqecnt,
16567 				       &cq_set->u.request, LPFC_CQ_CNT_256);
16568 				break;
16569 			case 512:
16570 				bf_set(lpfc_mbx_cq_create_set_cqecnt,
16571 				       &cq_set->u.request, LPFC_CQ_CNT_512);
16572 				break;
16573 			case 1024:
16574 				bf_set(lpfc_mbx_cq_create_set_cqecnt,
16575 				       &cq_set->u.request, LPFC_CQ_CNT_1024);
16576 				break;
16577 			}
16578 			bf_set(lpfc_mbx_cq_create_set_eq_id0,
16579 			       &cq_set->u.request, eq->queue_id);
16580 			break;
16581 		case 1:
16582 			bf_set(lpfc_mbx_cq_create_set_eq_id1,
16583 			       &cq_set->u.request, eq->queue_id);
16584 			break;
16585 		case 2:
16586 			bf_set(lpfc_mbx_cq_create_set_eq_id2,
16587 			       &cq_set->u.request, eq->queue_id);
16588 			break;
16589 		case 3:
16590 			bf_set(lpfc_mbx_cq_create_set_eq_id3,
16591 			       &cq_set->u.request, eq->queue_id);
16592 			break;
16593 		case 4:
16594 			bf_set(lpfc_mbx_cq_create_set_eq_id4,
16595 			       &cq_set->u.request, eq->queue_id);
16596 			break;
16597 		case 5:
16598 			bf_set(lpfc_mbx_cq_create_set_eq_id5,
16599 			       &cq_set->u.request, eq->queue_id);
16600 			break;
16601 		case 6:
16602 			bf_set(lpfc_mbx_cq_create_set_eq_id6,
16603 			       &cq_set->u.request, eq->queue_id);
16604 			break;
16605 		case 7:
16606 			bf_set(lpfc_mbx_cq_create_set_eq_id7,
16607 			       &cq_set->u.request, eq->queue_id);
16608 			break;
16609 		case 8:
16610 			bf_set(lpfc_mbx_cq_create_set_eq_id8,
16611 			       &cq_set->u.request, eq->queue_id);
16612 			break;
16613 		case 9:
16614 			bf_set(lpfc_mbx_cq_create_set_eq_id9,
16615 			       &cq_set->u.request, eq->queue_id);
16616 			break;
16617 		case 10:
16618 			bf_set(lpfc_mbx_cq_create_set_eq_id10,
16619 			       &cq_set->u.request, eq->queue_id);
16620 			break;
16621 		case 11:
16622 			bf_set(lpfc_mbx_cq_create_set_eq_id11,
16623 			       &cq_set->u.request, eq->queue_id);
16624 			break;
16625 		case 12:
16626 			bf_set(lpfc_mbx_cq_create_set_eq_id12,
16627 			       &cq_set->u.request, eq->queue_id);
16628 			break;
16629 		case 13:
16630 			bf_set(lpfc_mbx_cq_create_set_eq_id13,
16631 			       &cq_set->u.request, eq->queue_id);
16632 			break;
16633 		case 14:
16634 			bf_set(lpfc_mbx_cq_create_set_eq_id14,
16635 			       &cq_set->u.request, eq->queue_id);
16636 			break;
16637 		case 15:
16638 			bf_set(lpfc_mbx_cq_create_set_eq_id15,
16639 			       &cq_set->u.request, eq->queue_id);
16640 			break;
16641 		}
16642 
16643 		/* link the cq onto the parent eq child list */
16644 		list_add_tail(&cq->list, &eq->child_list);
16645 		/* Set up completion queue's type and subtype */
16646 		cq->type = type;
16647 		cq->subtype = subtype;
16648 		cq->assoc_qid = eq->queue_id;
16649 		cq->assoc_qp = eq;
16650 		cq->host_index = 0;
16651 		cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16652 		cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16653 					 cq->entry_count);
16654 		cq->chann = idx;
16655 
16656 		rc = 0;
16657 		list_for_each_entry(dmabuf, &cq->page_list, list) {
16658 			memset(dmabuf->virt, 0, hw_page_size);
16659 			cnt = page_idx + dmabuf->buffer_tag;
16660 			cq_set->u.request.page[cnt].addr_lo =
16661 					putPaddrLow(dmabuf->phys);
16662 			cq_set->u.request.page[cnt].addr_hi =
16663 					putPaddrHigh(dmabuf->phys);
16664 			rc++;
16665 		}
16666 		page_idx += rc;
16667 	}
16668 
16669 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16670 
16671 	/* The IOCTL status is embedded in the mailbox subheader. */
16672 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16673 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16674 	if (shdr_status || shdr_add_status || rc) {
16675 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16676 				"3119 CQ_CREATE_SET mailbox failed with "
16677 				"status x%x add_status x%x, mbx status x%x\n",
16678 				shdr_status, shdr_add_status, rc);
16679 		status = -ENXIO;
16680 		goto out;
16681 	}
16682 	rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16683 	if (rc == 0xFFFF) {
16684 		status = -ENXIO;
16685 		goto out;
16686 	}
16687 
16688 	for (idx = 0; idx < numcq; idx++) {
16689 		cq = cqp[idx];
16690 		cq->queue_id = rc + idx;
16691 		if (cq->queue_id > phba->sli4_hba.cq_max)
16692 			phba->sli4_hba.cq_max = cq->queue_id;
16693 	}
16694 
16695 out:
16696 	lpfc_sli4_mbox_cmd_free(phba, mbox);
16697 	return status;
16698 }
16699 
16700 /**
16701  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
16702  * @phba: HBA structure that indicates port to create a queue on.
16703  * @mq: The queue structure to use to create the mailbox queue.
16704  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16705  * @cq: The completion queue to associate with this cq.
16706  *
16707  * This function provides failback (fb) functionality when the
16708  * mq_create_ext fails on older FW generations.  It's purpose is identical
16709  * to mq_create_ext otherwise.
16710  *
16711  * This routine cannot fail as all attributes were previously accessed and
16712  * initialized in mq_create_ext.
16713  **/
16714 static void
lpfc_mq_create_fb_init(struct lpfc_hba * phba,struct lpfc_queue * mq,LPFC_MBOXQ_t * mbox,struct lpfc_queue * cq)16715 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16716 		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
16717 {
16718 	struct lpfc_mbx_mq_create *mq_create;
16719 	struct lpfc_dmabuf *dmabuf;
16720 	int length;
16721 
16722 	length = (sizeof(struct lpfc_mbx_mq_create) -
16723 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16724 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16725 			 LPFC_MBOX_OPCODE_MQ_CREATE,
16726 			 length, LPFC_SLI4_MBX_EMBED);
16727 	mq_create = &mbox->u.mqe.un.mq_create;
16728 	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
16729 	       mq->page_count);
16730 	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
16731 	       cq->queue_id);
16732 	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16733 	switch (mq->entry_count) {
16734 	case 16:
16735 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16736 		       LPFC_MQ_RING_SIZE_16);
16737 		break;
16738 	case 32:
16739 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16740 		       LPFC_MQ_RING_SIZE_32);
16741 		break;
16742 	case 64:
16743 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16744 		       LPFC_MQ_RING_SIZE_64);
16745 		break;
16746 	case 128:
16747 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16748 		       LPFC_MQ_RING_SIZE_128);
16749 		break;
16750 	}
16751 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16752 		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16753 			putPaddrLow(dmabuf->phys);
16754 		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16755 			putPaddrHigh(dmabuf->phys);
16756 	}
16757 }
16758 
16759 /**
16760  * lpfc_mq_create - Create a mailbox Queue on the HBA
16761  * @phba: HBA structure that indicates port to create a queue on.
16762  * @mq: The queue structure to use to create the mailbox queue.
16763  * @cq: The completion queue to associate with this cq.
16764  * @subtype: The queue's subtype.
16765  *
16766  * This function creates a mailbox queue, as detailed in @mq, on a port,
16767  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16768  *
16769  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16770  * is used to get the entry count and entry size that are necessary to
16771  * determine the number of pages to allocate and use for this queue. This
16772  * function will send the MQ_CREATE mailbox command to the HBA to setup the
16773  * mailbox queue. This function is asynchronous and will wait for the mailbox
16774  * command to finish before continuing.
16775  *
16776  * On success this function will return a zero. If unable to allocate enough
16777  * memory this function will return -ENOMEM. If the queue create mailbox command
16778  * fails this function will return -ENXIO.
16779  **/
16780 int32_t
lpfc_mq_create(struct lpfc_hba * phba,struct lpfc_queue * mq,struct lpfc_queue * cq,uint32_t subtype)16781 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16782 	       struct lpfc_queue *cq, uint32_t subtype)
16783 {
16784 	struct lpfc_mbx_mq_create *mq_create;
16785 	struct lpfc_mbx_mq_create_ext *mq_create_ext;
16786 	struct lpfc_dmabuf *dmabuf;
16787 	LPFC_MBOXQ_t *mbox;
16788 	int rc, length, status = 0;
16789 	uint32_t shdr_status, shdr_add_status;
16790 	union lpfc_sli4_cfg_shdr *shdr;
16791 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16792 
16793 	/* sanity check on queue memory */
16794 	if (!mq || !cq)
16795 		return -ENODEV;
16796 	if (!phba->sli4_hba.pc_sli4_params.supported)
16797 		hw_page_size = SLI4_PAGE_SIZE;
16798 
16799 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16800 	if (!mbox)
16801 		return -ENOMEM;
16802 	length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16803 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16804 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16805 			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16806 			 length, LPFC_SLI4_MBX_EMBED);
16807 
16808 	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
16809 	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
16810 	bf_set(lpfc_mbx_mq_create_ext_num_pages,
16811 	       &mq_create_ext->u.request, mq->page_count);
16812 	bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16813 	       &mq_create_ext->u.request, 1);
16814 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
16815 	       &mq_create_ext->u.request, 1);
16816 	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16817 	       &mq_create_ext->u.request, 1);
16818 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16819 	       &mq_create_ext->u.request, 1);
16820 	bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16821 	       &mq_create_ext->u.request, 1);
16822 	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
16823 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16824 	       phba->sli4_hba.pc_sli4_params.mqv);
16825 	if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16826 		bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16827 		       cq->queue_id);
16828 	else
16829 		bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16830 		       cq->queue_id);
16831 	switch (mq->entry_count) {
16832 	default:
16833 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16834 				"0362 Unsupported MQ count. (%d)\n",
16835 				mq->entry_count);
16836 		if (mq->entry_count < 16) {
16837 			status = -EINVAL;
16838 			goto out;
16839 		}
16840 		fallthrough;	/* otherwise default to smallest count */
16841 	case 16:
16842 		bf_set(lpfc_mq_context_ring_size,
16843 		       &mq_create_ext->u.request.context,
16844 		       LPFC_MQ_RING_SIZE_16);
16845 		break;
16846 	case 32:
16847 		bf_set(lpfc_mq_context_ring_size,
16848 		       &mq_create_ext->u.request.context,
16849 		       LPFC_MQ_RING_SIZE_32);
16850 		break;
16851 	case 64:
16852 		bf_set(lpfc_mq_context_ring_size,
16853 		       &mq_create_ext->u.request.context,
16854 		       LPFC_MQ_RING_SIZE_64);
16855 		break;
16856 	case 128:
16857 		bf_set(lpfc_mq_context_ring_size,
16858 		       &mq_create_ext->u.request.context,
16859 		       LPFC_MQ_RING_SIZE_128);
16860 		break;
16861 	}
16862 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16863 		memset(dmabuf->virt, 0, hw_page_size);
16864 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16865 					putPaddrLow(dmabuf->phys);
16866 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16867 					putPaddrHigh(dmabuf->phys);
16868 	}
16869 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16870 	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16871 			      &mq_create_ext->u.response);
16872 	if (rc != MBX_SUCCESS) {
16873 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16874 				"2795 MQ_CREATE_EXT failed with "
16875 				"status x%x. Failback to MQ_CREATE.\n",
16876 				rc);
16877 		lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16878 		mq_create = &mbox->u.mqe.un.mq_create;
16879 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16880 		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16881 		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16882 				      &mq_create->u.response);
16883 	}
16884 
16885 	/* The IOCTL status is embedded in the mailbox subheader. */
16886 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16887 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16888 	if (shdr_status || shdr_add_status || rc) {
16889 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16890 				"2502 MQ_CREATE mailbox failed with "
16891 				"status x%x add_status x%x, mbx status x%x\n",
16892 				shdr_status, shdr_add_status, rc);
16893 		status = -ENXIO;
16894 		goto out;
16895 	}
16896 	if (mq->queue_id == 0xFFFF) {
16897 		status = -ENXIO;
16898 		goto out;
16899 	}
16900 	mq->type = LPFC_MQ;
16901 	mq->assoc_qid = cq->queue_id;
16902 	mq->subtype = subtype;
16903 	mq->host_index = 0;
16904 	mq->hba_index = 0;
16905 
16906 	/* link the mq onto the parent cq child list */
16907 	list_add_tail(&mq->list, &cq->child_list);
16908 out:
16909 	mempool_free(mbox, phba->mbox_mem_pool);
16910 	return status;
16911 }
16912 
16913 /**
16914  * lpfc_wq_create - Create a Work Queue on the HBA
16915  * @phba: HBA structure that indicates port to create a queue on.
16916  * @wq: The queue structure to use to create the work queue.
16917  * @cq: The completion queue to bind this work queue to.
16918  * @subtype: The subtype of the work queue indicating its functionality.
16919  *
16920  * This function creates a work queue, as detailed in @wq, on a port, described
16921  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16922  *
16923  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16924  * is used to get the entry count and entry size that are necessary to
16925  * determine the number of pages to allocate and use for this queue. The @cq
16926  * is used to indicate which completion queue to bind this work queue to. This
16927  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16928  * work queue. This function is asynchronous and will wait for the mailbox
16929  * command to finish before continuing.
16930  *
16931  * On success this function will return a zero. If unable to allocate enough
16932  * memory this function will return -ENOMEM. If the queue create mailbox command
16933  * fails this function will return -ENXIO.
16934  **/
16935 int
lpfc_wq_create(struct lpfc_hba * phba,struct lpfc_queue * wq,struct lpfc_queue * cq,uint32_t subtype)16936 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16937 	       struct lpfc_queue *cq, uint32_t subtype)
16938 {
16939 	struct lpfc_mbx_wq_create *wq_create;
16940 	struct lpfc_dmabuf *dmabuf;
16941 	LPFC_MBOXQ_t *mbox;
16942 	int rc, length, status = 0;
16943 	uint32_t shdr_status, shdr_add_status;
16944 	union lpfc_sli4_cfg_shdr *shdr;
16945 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16946 	struct dma_address *page;
16947 	void __iomem *bar_memmap_p;
16948 	uint32_t db_offset;
16949 	uint16_t pci_barset;
16950 	uint8_t dpp_barset;
16951 	uint32_t dpp_offset;
16952 	uint8_t wq_create_version;
16953 
16954 	/* sanity check on queue memory */
16955 	if (!wq || !cq)
16956 		return -ENODEV;
16957 	if (!phba->sli4_hba.pc_sli4_params.supported)
16958 		hw_page_size = wq->page_size;
16959 
16960 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16961 	if (!mbox)
16962 		return -ENOMEM;
16963 	length = (sizeof(struct lpfc_mbx_wq_create) -
16964 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16965 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16966 			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16967 			 length, LPFC_SLI4_MBX_EMBED);
16968 	wq_create = &mbox->u.mqe.un.wq_create;
16969 	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16970 	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16971 		    wq->page_count);
16972 	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16973 		    cq->queue_id);
16974 
16975 	/* wqv is the earliest version supported, NOT the latest */
16976 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16977 	       phba->sli4_hba.pc_sli4_params.wqv);
16978 
16979 	if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16980 	    (wq->page_size > SLI4_PAGE_SIZE))
16981 		wq_create_version = LPFC_Q_CREATE_VERSION_1;
16982 	else
16983 		wq_create_version = LPFC_Q_CREATE_VERSION_0;
16984 
16985 	switch (wq_create_version) {
16986 	case LPFC_Q_CREATE_VERSION_1:
16987 		bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16988 		       wq->entry_count);
16989 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16990 		       LPFC_Q_CREATE_VERSION_1);
16991 
16992 		switch (wq->entry_size) {
16993 		default:
16994 		case 64:
16995 			bf_set(lpfc_mbx_wq_create_wqe_size,
16996 			       &wq_create->u.request_1,
16997 			       LPFC_WQ_WQE_SIZE_64);
16998 			break;
16999 		case 128:
17000 			bf_set(lpfc_mbx_wq_create_wqe_size,
17001 			       &wq_create->u.request_1,
17002 			       LPFC_WQ_WQE_SIZE_128);
17003 			break;
17004 		}
17005 		/* Request DPP by default */
17006 		bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
17007 		bf_set(lpfc_mbx_wq_create_page_size,
17008 		       &wq_create->u.request_1,
17009 		       (wq->page_size / SLI4_PAGE_SIZE));
17010 		page = wq_create->u.request_1.page;
17011 		break;
17012 	default:
17013 		page = wq_create->u.request.page;
17014 		break;
17015 	}
17016 
17017 	list_for_each_entry(dmabuf, &wq->page_list, list) {
17018 		memset(dmabuf->virt, 0, hw_page_size);
17019 		page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
17020 		page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
17021 	}
17022 
17023 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17024 		bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
17025 
17026 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17027 	/* The IOCTL status is embedded in the mailbox subheader. */
17028 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17029 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17030 	if (shdr_status || shdr_add_status || rc) {
17031 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17032 				"2503 WQ_CREATE mailbox failed with "
17033 				"status x%x add_status x%x, mbx status x%x\n",
17034 				shdr_status, shdr_add_status, rc);
17035 		status = -ENXIO;
17036 		goto out;
17037 	}
17038 
17039 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
17040 		wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
17041 					&wq_create->u.response);
17042 	else
17043 		wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
17044 					&wq_create->u.response_1);
17045 
17046 	if (wq->queue_id == 0xFFFF) {
17047 		status = -ENXIO;
17048 		goto out;
17049 	}
17050 
17051 	wq->db_format = LPFC_DB_LIST_FORMAT;
17052 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
17053 		if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17054 			wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
17055 					       &wq_create->u.response);
17056 			if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
17057 			    (wq->db_format != LPFC_DB_RING_FORMAT)) {
17058 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17059 						"3265 WQ[%d] doorbell format "
17060 						"not supported: x%x\n",
17061 						wq->queue_id, wq->db_format);
17062 				status = -EINVAL;
17063 				goto out;
17064 			}
17065 			pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
17066 					    &wq_create->u.response);
17067 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17068 								   pci_barset);
17069 			if (!bar_memmap_p) {
17070 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17071 						"3263 WQ[%d] failed to memmap "
17072 						"pci barset:x%x\n",
17073 						wq->queue_id, pci_barset);
17074 				status = -ENOMEM;
17075 				goto out;
17076 			}
17077 			db_offset = wq_create->u.response.doorbell_offset;
17078 			if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
17079 			    (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
17080 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17081 						"3252 WQ[%d] doorbell offset "
17082 						"not supported: x%x\n",
17083 						wq->queue_id, db_offset);
17084 				status = -EINVAL;
17085 				goto out;
17086 			}
17087 			wq->db_regaddr = bar_memmap_p + db_offset;
17088 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17089 					"3264 WQ[%d]: barset:x%x, offset:x%x, "
17090 					"format:x%x\n", wq->queue_id,
17091 					pci_barset, db_offset, wq->db_format);
17092 		} else
17093 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17094 	} else {
17095 		/* Check if DPP was honored by the firmware */
17096 		wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
17097 				    &wq_create->u.response_1);
17098 		if (wq->dpp_enable) {
17099 			pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
17100 					    &wq_create->u.response_1);
17101 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17102 								   pci_barset);
17103 			if (!bar_memmap_p) {
17104 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17105 						"3267 WQ[%d] failed to memmap "
17106 						"pci barset:x%x\n",
17107 						wq->queue_id, pci_barset);
17108 				status = -ENOMEM;
17109 				goto out;
17110 			}
17111 			db_offset = wq_create->u.response_1.doorbell_offset;
17112 			wq->db_regaddr = bar_memmap_p + db_offset;
17113 			wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
17114 					    &wq_create->u.response_1);
17115 			dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
17116 					    &wq_create->u.response_1);
17117 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17118 								   dpp_barset);
17119 			if (!bar_memmap_p) {
17120 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17121 						"3268 WQ[%d] failed to memmap "
17122 						"pci barset:x%x\n",
17123 						wq->queue_id, dpp_barset);
17124 				status = -ENOMEM;
17125 				goto out;
17126 			}
17127 			dpp_offset = wq_create->u.response_1.dpp_offset;
17128 			wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17129 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17130 					"3271 WQ[%d]: barset:x%x, offset:x%x, "
17131 					"dpp_id:x%x dpp_barset:x%x "
17132 					"dpp_offset:x%x\n",
17133 					wq->queue_id, pci_barset, db_offset,
17134 					wq->dpp_id, dpp_barset, dpp_offset);
17135 
17136 #ifdef CONFIG_X86
17137 			/* Enable combined writes for DPP aperture */
17138 			bar_memmap_p = lpfc_dpp_wc_map(phba, dpp_barset);
17139 			if (!bar_memmap_p) {
17140 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17141 					"3272 Cannot setup Combined "
17142 					"Write on WQ[%d] - disable DPP\n",
17143 					wq->queue_id);
17144 				phba->cfg_enable_dpp = 0;
17145 			} else {
17146 				wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17147 			}
17148 #else
17149 			phba->cfg_enable_dpp = 0;
17150 #endif
17151 		} else
17152 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17153 	}
17154 	wq->pring = kzalloc_obj(struct lpfc_sli_ring);
17155 	if (wq->pring == NULL) {
17156 		status = -ENOMEM;
17157 		goto out;
17158 	}
17159 	wq->type = LPFC_WQ;
17160 	wq->assoc_qid = cq->queue_id;
17161 	wq->subtype = subtype;
17162 	wq->host_index = 0;
17163 	wq->hba_index = 0;
17164 	wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
17165 
17166 	/* link the wq onto the parent cq child list */
17167 	list_add_tail(&wq->list, &cq->child_list);
17168 out:
17169 	mempool_free(mbox, phba->mbox_mem_pool);
17170 	return status;
17171 }
17172 
17173 /**
17174  * lpfc_rq_create - Create a Receive Queue on the HBA
17175  * @phba: HBA structure that indicates port to create a queue on.
17176  * @hrq: The queue structure to use to create the header receive queue.
17177  * @drq: The queue structure to use to create the data receive queue.
17178  * @cq: The completion queue to bind this work queue to.
17179  * @subtype: The subtype of the work queue indicating its functionality.
17180  *
17181  * This function creates a receive buffer queue pair , as detailed in @hrq and
17182  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17183  * to the HBA.
17184  *
17185  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17186  * struct is used to get the entry count that is necessary to determine the
17187  * number of pages to use for this queue. The @cq is used to indicate which
17188  * completion queue to bind received buffers that are posted to these queues to.
17189  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17190  * receive queue pair. This function is asynchronous and will wait for the
17191  * mailbox command to finish before continuing.
17192  *
17193  * On success this function will return a zero. If unable to allocate enough
17194  * memory this function will return -ENOMEM. If the queue create mailbox command
17195  * fails this function will return -ENXIO.
17196  **/
17197 int
lpfc_rq_create(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,struct lpfc_queue * cq,uint32_t subtype)17198 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17199 	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17200 {
17201 	struct lpfc_mbx_rq_create *rq_create;
17202 	struct lpfc_dmabuf *dmabuf;
17203 	LPFC_MBOXQ_t *mbox;
17204 	int rc, length, status = 0;
17205 	uint32_t shdr_status, shdr_add_status;
17206 	union lpfc_sli4_cfg_shdr *shdr;
17207 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17208 	void __iomem *bar_memmap_p;
17209 	uint32_t db_offset;
17210 	uint16_t pci_barset;
17211 
17212 	/* sanity check on queue memory */
17213 	if (!hrq || !drq || !cq)
17214 		return -ENODEV;
17215 	if (!phba->sli4_hba.pc_sli4_params.supported)
17216 		hw_page_size = SLI4_PAGE_SIZE;
17217 
17218 	if (hrq->entry_count != drq->entry_count)
17219 		return -EINVAL;
17220 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17221 	if (!mbox)
17222 		return -ENOMEM;
17223 	length = (sizeof(struct lpfc_mbx_rq_create) -
17224 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17225 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17226 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17227 			 length, LPFC_SLI4_MBX_EMBED);
17228 	rq_create = &mbox->u.mqe.un.rq_create;
17229 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17230 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17231 	       phba->sli4_hba.pc_sli4_params.rqv);
17232 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17233 		bf_set(lpfc_rq_context_rqe_count_1,
17234 		       &rq_create->u.request.context,
17235 		       hrq->entry_count);
17236 		rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
17237 		bf_set(lpfc_rq_context_rqe_size,
17238 		       &rq_create->u.request.context,
17239 		       LPFC_RQE_SIZE_8);
17240 		bf_set(lpfc_rq_context_page_size,
17241 		       &rq_create->u.request.context,
17242 		       LPFC_RQ_PAGE_SIZE_4096);
17243 	} else {
17244 		switch (hrq->entry_count) {
17245 		default:
17246 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17247 					"2535 Unsupported RQ count. (%d)\n",
17248 					hrq->entry_count);
17249 			if (hrq->entry_count < 512) {
17250 				status = -EINVAL;
17251 				goto out;
17252 			}
17253 			fallthrough;	/* otherwise default to smallest count */
17254 		case 512:
17255 			bf_set(lpfc_rq_context_rqe_count,
17256 			       &rq_create->u.request.context,
17257 			       LPFC_RQ_RING_SIZE_512);
17258 			break;
17259 		case 1024:
17260 			bf_set(lpfc_rq_context_rqe_count,
17261 			       &rq_create->u.request.context,
17262 			       LPFC_RQ_RING_SIZE_1024);
17263 			break;
17264 		case 2048:
17265 			bf_set(lpfc_rq_context_rqe_count,
17266 			       &rq_create->u.request.context,
17267 			       LPFC_RQ_RING_SIZE_2048);
17268 			break;
17269 		case 4096:
17270 			bf_set(lpfc_rq_context_rqe_count,
17271 			       &rq_create->u.request.context,
17272 			       LPFC_RQ_RING_SIZE_4096);
17273 			break;
17274 		}
17275 		bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17276 		       LPFC_HDR_BUF_SIZE);
17277 	}
17278 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17279 	       cq->queue_id);
17280 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17281 	       hrq->page_count);
17282 	list_for_each_entry(dmabuf, &hrq->page_list, list) {
17283 		memset(dmabuf->virt, 0, hw_page_size);
17284 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17285 					putPaddrLow(dmabuf->phys);
17286 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17287 					putPaddrHigh(dmabuf->phys);
17288 	}
17289 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17290 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17291 
17292 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17293 	/* The IOCTL status is embedded in the mailbox subheader. */
17294 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17295 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17296 	if (shdr_status || shdr_add_status || rc) {
17297 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17298 				"2504 RQ_CREATE mailbox failed with "
17299 				"status x%x add_status x%x, mbx status x%x\n",
17300 				shdr_status, shdr_add_status, rc);
17301 		status = -ENXIO;
17302 		goto out;
17303 	}
17304 	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17305 	if (hrq->queue_id == 0xFFFF) {
17306 		status = -ENXIO;
17307 		goto out;
17308 	}
17309 
17310 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17311 		hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17312 					&rq_create->u.response);
17313 		if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17314 		    (hrq->db_format != LPFC_DB_RING_FORMAT)) {
17315 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17316 					"3262 RQ [%d] doorbell format not "
17317 					"supported: x%x\n", hrq->queue_id,
17318 					hrq->db_format);
17319 			status = -EINVAL;
17320 			goto out;
17321 		}
17322 
17323 		pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17324 				    &rq_create->u.response);
17325 		bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17326 		if (!bar_memmap_p) {
17327 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17328 					"3269 RQ[%d] failed to memmap pci "
17329 					"barset:x%x\n", hrq->queue_id,
17330 					pci_barset);
17331 			status = -ENOMEM;
17332 			goto out;
17333 		}
17334 
17335 		db_offset = rq_create->u.response.doorbell_offset;
17336 		if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17337 		    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
17338 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17339 					"3270 RQ[%d] doorbell offset not "
17340 					"supported: x%x\n", hrq->queue_id,
17341 					db_offset);
17342 			status = -EINVAL;
17343 			goto out;
17344 		}
17345 		hrq->db_regaddr = bar_memmap_p + db_offset;
17346 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17347 				"3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17348 				"format:x%x\n", hrq->queue_id, pci_barset,
17349 				db_offset, hrq->db_format);
17350 	} else {
17351 		hrq->db_format = LPFC_DB_RING_FORMAT;
17352 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17353 	}
17354 	hrq->type = LPFC_HRQ;
17355 	hrq->assoc_qid = cq->queue_id;
17356 	hrq->subtype = subtype;
17357 	hrq->host_index = 0;
17358 	hrq->hba_index = 0;
17359 	hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17360 
17361 	/* now create the data queue */
17362 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17363 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17364 			 length, LPFC_SLI4_MBX_EMBED);
17365 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17366 	       phba->sli4_hba.pc_sli4_params.rqv);
17367 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17368 		bf_set(lpfc_rq_context_rqe_count_1,
17369 		       &rq_create->u.request.context, hrq->entry_count);
17370 		if (subtype == LPFC_NVMET)
17371 			rq_create->u.request.context.buffer_size =
17372 				LPFC_NVMET_DATA_BUF_SIZE;
17373 		else
17374 			rq_create->u.request.context.buffer_size =
17375 				LPFC_DATA_BUF_SIZE;
17376 		bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17377 		       LPFC_RQE_SIZE_8);
17378 		bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17379 		       (PAGE_SIZE/SLI4_PAGE_SIZE));
17380 	} else {
17381 		switch (drq->entry_count) {
17382 		default:
17383 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17384 					"2536 Unsupported RQ count. (%d)\n",
17385 					drq->entry_count);
17386 			if (drq->entry_count < 512) {
17387 				status = -EINVAL;
17388 				goto out;
17389 			}
17390 			fallthrough;	/* otherwise default to smallest count */
17391 		case 512:
17392 			bf_set(lpfc_rq_context_rqe_count,
17393 			       &rq_create->u.request.context,
17394 			       LPFC_RQ_RING_SIZE_512);
17395 			break;
17396 		case 1024:
17397 			bf_set(lpfc_rq_context_rqe_count,
17398 			       &rq_create->u.request.context,
17399 			       LPFC_RQ_RING_SIZE_1024);
17400 			break;
17401 		case 2048:
17402 			bf_set(lpfc_rq_context_rqe_count,
17403 			       &rq_create->u.request.context,
17404 			       LPFC_RQ_RING_SIZE_2048);
17405 			break;
17406 		case 4096:
17407 			bf_set(lpfc_rq_context_rqe_count,
17408 			       &rq_create->u.request.context,
17409 			       LPFC_RQ_RING_SIZE_4096);
17410 			break;
17411 		}
17412 		if (subtype == LPFC_NVMET)
17413 			bf_set(lpfc_rq_context_buf_size,
17414 			       &rq_create->u.request.context,
17415 			       LPFC_NVMET_DATA_BUF_SIZE);
17416 		else
17417 			bf_set(lpfc_rq_context_buf_size,
17418 			       &rq_create->u.request.context,
17419 			       LPFC_DATA_BUF_SIZE);
17420 	}
17421 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17422 	       cq->queue_id);
17423 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17424 	       drq->page_count);
17425 	list_for_each_entry(dmabuf, &drq->page_list, list) {
17426 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17427 					putPaddrLow(dmabuf->phys);
17428 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17429 					putPaddrHigh(dmabuf->phys);
17430 	}
17431 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17432 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17433 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17434 	/* The IOCTL status is embedded in the mailbox subheader. */
17435 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17436 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17437 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17438 	if (shdr_status || shdr_add_status || rc) {
17439 		status = -ENXIO;
17440 		goto out;
17441 	}
17442 	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17443 	if (drq->queue_id == 0xFFFF) {
17444 		status = -ENXIO;
17445 		goto out;
17446 	}
17447 	drq->type = LPFC_DRQ;
17448 	drq->assoc_qid = cq->queue_id;
17449 	drq->subtype = subtype;
17450 	drq->host_index = 0;
17451 	drq->hba_index = 0;
17452 	drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17453 
17454 	/* link the header and data RQs onto the parent cq child list */
17455 	list_add_tail(&hrq->list, &cq->child_list);
17456 	list_add_tail(&drq->list, &cq->child_list);
17457 
17458 out:
17459 	mempool_free(mbox, phba->mbox_mem_pool);
17460 	return status;
17461 }
17462 
17463 /**
17464  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17465  * @phba: HBA structure that indicates port to create a queue on.
17466  * @hrqp: The queue structure array to use to create the header receive queues.
17467  * @drqp: The queue structure array to use to create the data receive queues.
17468  * @cqp: The completion queue array to bind these receive queues to.
17469  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
17470  *
17471  * This function creates a receive buffer queue pair , as detailed in @hrq and
17472  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17473  * to the HBA.
17474  *
17475  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17476  * struct is used to get the entry count that is necessary to determine the
17477  * number of pages to use for this queue. The @cq is used to indicate which
17478  * completion queue to bind received buffers that are posted to these queues to.
17479  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17480  * receive queue pair. This function is asynchronous and will wait for the
17481  * mailbox command to finish before continuing.
17482  *
17483  * On success this function will return a zero. If unable to allocate enough
17484  * memory this function will return -ENOMEM. If the queue create mailbox command
17485  * fails this function will return -ENXIO.
17486  **/
17487 int
lpfc_mrq_create(struct lpfc_hba * phba,struct lpfc_queue ** hrqp,struct lpfc_queue ** drqp,struct lpfc_queue ** cqp,uint32_t subtype)17488 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17489 		struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17490 		uint32_t subtype)
17491 {
17492 	struct lpfc_queue *hrq, *drq, *cq;
17493 	struct lpfc_mbx_rq_create_v2 *rq_create;
17494 	struct lpfc_dmabuf *dmabuf;
17495 	LPFC_MBOXQ_t *mbox;
17496 	int rc, length, alloclen, status = 0;
17497 	int cnt, idx, numrq, page_idx = 0;
17498 	uint32_t shdr_status, shdr_add_status;
17499 	union lpfc_sli4_cfg_shdr *shdr;
17500 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17501 
17502 	numrq = phba->cfg_nvmet_mrq;
17503 	/* sanity check on array memory */
17504 	if (!hrqp || !drqp || !cqp || !numrq)
17505 		return -ENODEV;
17506 	if (!phba->sli4_hba.pc_sli4_params.supported)
17507 		hw_page_size = SLI4_PAGE_SIZE;
17508 
17509 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17510 	if (!mbox)
17511 		return -ENOMEM;
17512 
17513 	length = sizeof(struct lpfc_mbx_rq_create_v2);
17514 	length += ((2 * numrq * hrqp[0]->page_count) *
17515 		   sizeof(struct dma_address));
17516 
17517 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17518 				    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17519 				    LPFC_SLI4_MBX_NEMBED);
17520 	if (alloclen < length) {
17521 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17522 				"3099 Allocated DMA memory size (%d) is "
17523 				"less than the requested DMA memory size "
17524 				"(%d)\n", alloclen, length);
17525 		status = -ENOMEM;
17526 		goto out;
17527 	}
17528 
17529 
17530 
17531 	rq_create = mbox->sge_array->addr[0];
17532 	shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17533 
17534 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17535 	cnt = 0;
17536 
17537 	for (idx = 0; idx < numrq; idx++) {
17538 		hrq = hrqp[idx];
17539 		drq = drqp[idx];
17540 		cq  = cqp[idx];
17541 
17542 		/* sanity check on queue memory */
17543 		if (!hrq || !drq || !cq) {
17544 			status = -ENODEV;
17545 			goto out;
17546 		}
17547 
17548 		if (hrq->entry_count != drq->entry_count) {
17549 			status = -EINVAL;
17550 			goto out;
17551 		}
17552 
17553 		if (idx == 0) {
17554 			bf_set(lpfc_mbx_rq_create_num_pages,
17555 			       &rq_create->u.request,
17556 			       hrq->page_count);
17557 			bf_set(lpfc_mbx_rq_create_rq_cnt,
17558 			       &rq_create->u.request, (numrq * 2));
17559 			bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17560 			       1);
17561 			bf_set(lpfc_rq_context_base_cq,
17562 			       &rq_create->u.request.context,
17563 			       cq->queue_id);
17564 			bf_set(lpfc_rq_context_data_size,
17565 			       &rq_create->u.request.context,
17566 			       LPFC_NVMET_DATA_BUF_SIZE);
17567 			bf_set(lpfc_rq_context_hdr_size,
17568 			       &rq_create->u.request.context,
17569 			       LPFC_HDR_BUF_SIZE);
17570 			bf_set(lpfc_rq_context_rqe_count_1,
17571 			       &rq_create->u.request.context,
17572 			       hrq->entry_count);
17573 			bf_set(lpfc_rq_context_rqe_size,
17574 			       &rq_create->u.request.context,
17575 			       LPFC_RQE_SIZE_8);
17576 			bf_set(lpfc_rq_context_page_size,
17577 			       &rq_create->u.request.context,
17578 			       (PAGE_SIZE/SLI4_PAGE_SIZE));
17579 		}
17580 		rc = 0;
17581 		list_for_each_entry(dmabuf, &hrq->page_list, list) {
17582 			memset(dmabuf->virt, 0, hw_page_size);
17583 			cnt = page_idx + dmabuf->buffer_tag;
17584 			rq_create->u.request.page[cnt].addr_lo =
17585 					putPaddrLow(dmabuf->phys);
17586 			rq_create->u.request.page[cnt].addr_hi =
17587 					putPaddrHigh(dmabuf->phys);
17588 			rc++;
17589 		}
17590 		page_idx += rc;
17591 
17592 		rc = 0;
17593 		list_for_each_entry(dmabuf, &drq->page_list, list) {
17594 			memset(dmabuf->virt, 0, hw_page_size);
17595 			cnt = page_idx + dmabuf->buffer_tag;
17596 			rq_create->u.request.page[cnt].addr_lo =
17597 					putPaddrLow(dmabuf->phys);
17598 			rq_create->u.request.page[cnt].addr_hi =
17599 					putPaddrHigh(dmabuf->phys);
17600 			rc++;
17601 		}
17602 		page_idx += rc;
17603 
17604 		hrq->db_format = LPFC_DB_RING_FORMAT;
17605 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17606 		hrq->type = LPFC_HRQ;
17607 		hrq->assoc_qid = cq->queue_id;
17608 		hrq->subtype = subtype;
17609 		hrq->host_index = 0;
17610 		hrq->hba_index = 0;
17611 		hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17612 
17613 		drq->db_format = LPFC_DB_RING_FORMAT;
17614 		drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17615 		drq->type = LPFC_DRQ;
17616 		drq->assoc_qid = cq->queue_id;
17617 		drq->subtype = subtype;
17618 		drq->host_index = 0;
17619 		drq->hba_index = 0;
17620 		drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17621 
17622 		list_add_tail(&hrq->list, &cq->child_list);
17623 		list_add_tail(&drq->list, &cq->child_list);
17624 	}
17625 
17626 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17627 	/* The IOCTL status is embedded in the mailbox subheader. */
17628 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17629 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17630 	if (shdr_status || shdr_add_status || rc) {
17631 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17632 				"3120 RQ_CREATE mailbox failed with "
17633 				"status x%x add_status x%x, mbx status x%x\n",
17634 				shdr_status, shdr_add_status, rc);
17635 		status = -ENXIO;
17636 		goto out;
17637 	}
17638 	rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17639 	if (rc == 0xFFFF) {
17640 		status = -ENXIO;
17641 		goto out;
17642 	}
17643 
17644 	/* Initialize all RQs with associated queue id */
17645 	for (idx = 0; idx < numrq; idx++) {
17646 		hrq = hrqp[idx];
17647 		hrq->queue_id = rc + (2 * idx);
17648 		drq = drqp[idx];
17649 		drq->queue_id = rc + (2 * idx) + 1;
17650 	}
17651 
17652 out:
17653 	lpfc_sli4_mbox_cmd_free(phba, mbox);
17654 	return status;
17655 }
17656 
17657 /**
17658  * lpfc_eq_destroy - Destroy an event Queue on the HBA
17659  * @phba: HBA structure that indicates port to destroy a queue on.
17660  * @eq: The queue structure associated with the queue to destroy.
17661  *
17662  * This function destroys a queue, as detailed in @eq by sending an mailbox
17663  * command, specific to the type of queue, to the HBA.
17664  *
17665  * The @eq struct is used to get the queue ID of the queue to destroy.
17666  *
17667  * On success this function will return a zero. If the queue destroy mailbox
17668  * command fails this function will return -ENXIO.
17669  **/
17670 int
lpfc_eq_destroy(struct lpfc_hba * phba,struct lpfc_queue * eq)17671 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17672 {
17673 	LPFC_MBOXQ_t *mbox;
17674 	int rc, length, status = 0;
17675 	uint32_t shdr_status, shdr_add_status;
17676 	union lpfc_sli4_cfg_shdr *shdr;
17677 
17678 	/* sanity check on queue memory */
17679 	if (!eq)
17680 		return -ENODEV;
17681 
17682 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17683 		goto list_remove;
17684 
17685 	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17686 	if (!mbox)
17687 		return -ENOMEM;
17688 	length = (sizeof(struct lpfc_mbx_eq_destroy) -
17689 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17690 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17691 			 LPFC_MBOX_OPCODE_EQ_DESTROY,
17692 			 length, LPFC_SLI4_MBX_EMBED);
17693 	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17694 	       eq->queue_id);
17695 	mbox->vport = eq->phba->pport;
17696 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17697 
17698 	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17699 	/* The IOCTL status is embedded in the mailbox subheader. */
17700 	shdr = (union lpfc_sli4_cfg_shdr *)
17701 		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17702 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17703 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17704 	if (shdr_status || shdr_add_status || rc) {
17705 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17706 				"2505 EQ_DESTROY mailbox failed with "
17707 				"status x%x add_status x%x, mbx status x%x\n",
17708 				shdr_status, shdr_add_status, rc);
17709 		status = -ENXIO;
17710 	}
17711 	mempool_free(mbox, eq->phba->mbox_mem_pool);
17712 
17713 list_remove:
17714 	/* Remove eq from any list */
17715 	list_del_init(&eq->list);
17716 
17717 	return status;
17718 }
17719 
17720 /**
17721  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
17722  * @phba: HBA structure that indicates port to destroy a queue on.
17723  * @cq: The queue structure associated with the queue to destroy.
17724  *
17725  * This function destroys a queue, as detailed in @cq by sending an mailbox
17726  * command, specific to the type of queue, to the HBA.
17727  *
17728  * The @cq struct is used to get the queue ID of the queue to destroy.
17729  *
17730  * On success this function will return a zero. If the queue destroy mailbox
17731  * command fails this function will return -ENXIO.
17732  **/
17733 int
lpfc_cq_destroy(struct lpfc_hba * phba,struct lpfc_queue * cq)17734 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17735 {
17736 	LPFC_MBOXQ_t *mbox;
17737 	int rc, length, status = 0;
17738 	uint32_t shdr_status, shdr_add_status;
17739 	union lpfc_sli4_cfg_shdr *shdr;
17740 
17741 	/* sanity check on queue memory */
17742 	if (!cq)
17743 		return -ENODEV;
17744 
17745 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17746 		goto list_remove;
17747 
17748 	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17749 	if (!mbox)
17750 		return -ENOMEM;
17751 	length = (sizeof(struct lpfc_mbx_cq_destroy) -
17752 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17753 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17754 			 LPFC_MBOX_OPCODE_CQ_DESTROY,
17755 			 length, LPFC_SLI4_MBX_EMBED);
17756 	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17757 	       cq->queue_id);
17758 	mbox->vport = cq->phba->pport;
17759 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17760 	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17761 	/* The IOCTL status is embedded in the mailbox subheader. */
17762 	shdr = (union lpfc_sli4_cfg_shdr *)
17763 		&mbox->u.mqe.un.wq_create.header.cfg_shdr;
17764 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17765 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17766 	if (shdr_status || shdr_add_status || rc) {
17767 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17768 				"2506 CQ_DESTROY mailbox failed with "
17769 				"status x%x add_status x%x, mbx status x%x\n",
17770 				shdr_status, shdr_add_status, rc);
17771 		status = -ENXIO;
17772 	}
17773 	mempool_free(mbox, cq->phba->mbox_mem_pool);
17774 
17775 list_remove:
17776 	/* Remove cq from any list */
17777 	list_del_init(&cq->list);
17778 	return status;
17779 }
17780 
17781 /**
17782  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
17783  * @phba: HBA structure that indicates port to destroy a queue on.
17784  * @mq: The queue structure associated with the queue to destroy.
17785  *
17786  * This function destroys a queue, as detailed in @mq by sending an mailbox
17787  * command, specific to the type of queue, to the HBA.
17788  *
17789  * The @mq struct is used to get the queue ID of the queue to destroy.
17790  *
17791  * On success this function will return a zero. If the queue destroy mailbox
17792  * command fails this function will return -ENXIO.
17793  **/
17794 int
lpfc_mq_destroy(struct lpfc_hba * phba,struct lpfc_queue * mq)17795 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17796 {
17797 	LPFC_MBOXQ_t *mbox;
17798 	int rc, length, status = 0;
17799 	uint32_t shdr_status, shdr_add_status;
17800 	union lpfc_sli4_cfg_shdr *shdr;
17801 
17802 	/* sanity check on queue memory */
17803 	if (!mq)
17804 		return -ENODEV;
17805 
17806 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17807 		goto list_remove;
17808 
17809 	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17810 	if (!mbox)
17811 		return -ENOMEM;
17812 	length = (sizeof(struct lpfc_mbx_mq_destroy) -
17813 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17814 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17815 			 LPFC_MBOX_OPCODE_MQ_DESTROY,
17816 			 length, LPFC_SLI4_MBX_EMBED);
17817 	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17818 	       mq->queue_id);
17819 	mbox->vport = mq->phba->pport;
17820 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17821 	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17822 	/* The IOCTL status is embedded in the mailbox subheader. */
17823 	shdr = (union lpfc_sli4_cfg_shdr *)
17824 		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17825 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17826 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17827 	if (shdr_status || shdr_add_status || rc) {
17828 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17829 				"2507 MQ_DESTROY mailbox failed with "
17830 				"status x%x add_status x%x, mbx status x%x\n",
17831 				shdr_status, shdr_add_status, rc);
17832 		status = -ENXIO;
17833 	}
17834 	mempool_free(mbox, mq->phba->mbox_mem_pool);
17835 
17836 list_remove:
17837 	/* Remove mq from any list */
17838 	list_del_init(&mq->list);
17839 	return status;
17840 }
17841 
17842 /**
17843  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
17844  * @phba: HBA structure that indicates port to destroy a queue on.
17845  * @wq: The queue structure associated with the queue to destroy.
17846  *
17847  * This function destroys a queue, as detailed in @wq by sending an mailbox
17848  * command, specific to the type of queue, to the HBA.
17849  *
17850  * The @wq struct is used to get the queue ID of the queue to destroy.
17851  *
17852  * On success this function will return a zero. If the queue destroy mailbox
17853  * command fails this function will return -ENXIO.
17854  **/
17855 int
lpfc_wq_destroy(struct lpfc_hba * phba,struct lpfc_queue * wq)17856 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17857 {
17858 	LPFC_MBOXQ_t *mbox;
17859 	int rc, length, status = 0;
17860 	uint32_t shdr_status, shdr_add_status;
17861 	union lpfc_sli4_cfg_shdr *shdr;
17862 
17863 	/* sanity check on queue memory */
17864 	if (!wq)
17865 		return -ENODEV;
17866 
17867 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17868 		goto list_remove;
17869 
17870 	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17871 	if (!mbox)
17872 		return -ENOMEM;
17873 	length = (sizeof(struct lpfc_mbx_wq_destroy) -
17874 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17875 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17876 			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17877 			 length, LPFC_SLI4_MBX_EMBED);
17878 	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17879 	       wq->queue_id);
17880 	mbox->vport = wq->phba->pport;
17881 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17882 	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17883 	shdr = (union lpfc_sli4_cfg_shdr *)
17884 		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17885 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17886 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17887 	if (shdr_status || shdr_add_status || rc) {
17888 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17889 				"2508 WQ_DESTROY mailbox failed with "
17890 				"status x%x add_status x%x, mbx status x%x\n",
17891 				shdr_status, shdr_add_status, rc);
17892 		status = -ENXIO;
17893 	}
17894 	mempool_free(mbox, wq->phba->mbox_mem_pool);
17895 
17896 list_remove:
17897 	/* Remove wq from any list */
17898 	list_del_init(&wq->list);
17899 	kfree(wq->pring);
17900 	wq->pring = NULL;
17901 	return status;
17902 }
17903 
17904 /**
17905  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17906  * @phba: HBA structure that indicates port to destroy a queue on.
17907  * @hrq: The queue structure associated with the queue to destroy.
17908  * @drq: The queue structure associated with the queue to destroy.
17909  *
17910  * This function destroys a queue, as detailed in @rq by sending an mailbox
17911  * command, specific to the type of queue, to the HBA.
17912  *
17913  * The @rq struct is used to get the queue ID of the queue to destroy.
17914  *
17915  * On success this function will return a zero. If the queue destroy mailbox
17916  * command fails this function will return -ENXIO.
17917  **/
17918 int
lpfc_rq_destroy(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq)17919 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17920 		struct lpfc_queue *drq)
17921 {
17922 	LPFC_MBOXQ_t *mbox;
17923 	int rc, length, status = 0;
17924 	uint32_t shdr_status, shdr_add_status;
17925 	union lpfc_sli4_cfg_shdr *shdr;
17926 
17927 	/* sanity check on queue memory */
17928 	if (!hrq || !drq)
17929 		return -ENODEV;
17930 
17931 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17932 		goto list_remove;
17933 
17934 	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17935 	if (!mbox)
17936 		return -ENOMEM;
17937 	length = (sizeof(struct lpfc_mbx_rq_destroy) -
17938 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17939 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17940 			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17941 			 length, LPFC_SLI4_MBX_EMBED);
17942 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17943 	       hrq->queue_id);
17944 	mbox->vport = hrq->phba->pport;
17945 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17946 	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17947 	/* The IOCTL status is embedded in the mailbox subheader. */
17948 	shdr = (union lpfc_sli4_cfg_shdr *)
17949 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17950 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17951 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17952 	if (shdr_status || shdr_add_status || rc) {
17953 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17954 				"2509 RQ_DESTROY mailbox failed with "
17955 				"status x%x add_status x%x, mbx status x%x\n",
17956 				shdr_status, shdr_add_status, rc);
17957 		mempool_free(mbox, hrq->phba->mbox_mem_pool);
17958 		return -ENXIO;
17959 	}
17960 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17961 	       drq->queue_id);
17962 	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17963 	shdr = (union lpfc_sli4_cfg_shdr *)
17964 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17965 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17966 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17967 	if (shdr_status || shdr_add_status || rc) {
17968 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17969 				"2510 RQ_DESTROY mailbox failed with "
17970 				"status x%x add_status x%x, mbx status x%x\n",
17971 				shdr_status, shdr_add_status, rc);
17972 		status = -ENXIO;
17973 	}
17974 	mempool_free(mbox, hrq->phba->mbox_mem_pool);
17975 
17976 list_remove:
17977 	list_del_init(&hrq->list);
17978 	list_del_init(&drq->list);
17979 	return status;
17980 }
17981 
17982 /**
17983  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17984  * @phba: The virtual port for which this call being executed.
17985  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17986  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17987  * @xritag: the xritag that ties this io to the SGL pages.
17988  *
17989  * This routine will post the sgl pages for the IO that has the xritag
17990  * that is in the iocbq structure. The xritag is assigned during iocbq
17991  * creation and persists for as long as the driver is loaded.
17992  * if the caller has fewer than 256 scatter gather segments to map then
17993  * pdma_phys_addr1 should be 0.
17994  * If the caller needs to map more than 256 scatter gather segment then
17995  * pdma_phys_addr1 should be a valid physical address.
17996  * physical address for SGLs must be 64 byte aligned.
17997  * If you are going to map 2 SGL's then the first one must have 256 entries
17998  * the second sgl can have between 1 and 256 entries.
17999  *
18000  * Return codes:
18001  * 	0 - Success
18002  * 	-ENXIO, -ENOMEM - Failure
18003  **/
18004 int
lpfc_sli4_post_sgl(struct lpfc_hba * phba,dma_addr_t pdma_phys_addr0,dma_addr_t pdma_phys_addr1,uint16_t xritag)18005 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
18006 		dma_addr_t pdma_phys_addr0,
18007 		dma_addr_t pdma_phys_addr1,
18008 		uint16_t xritag)
18009 {
18010 	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
18011 	LPFC_MBOXQ_t *mbox;
18012 	int rc;
18013 	uint32_t shdr_status, shdr_add_status;
18014 	uint32_t mbox_tmo;
18015 	union lpfc_sli4_cfg_shdr *shdr;
18016 
18017 	if (xritag == NO_XRI) {
18018 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18019 				"0364 Invalid param:\n");
18020 		return -EINVAL;
18021 	}
18022 
18023 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18024 	if (!mbox)
18025 		return -ENOMEM;
18026 
18027 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18028 			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18029 			sizeof(struct lpfc_mbx_post_sgl_pages) -
18030 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18031 
18032 	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
18033 				&mbox->u.mqe.un.post_sgl_pages;
18034 	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
18035 	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
18036 
18037 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=
18038 				cpu_to_le32(putPaddrLow(pdma_phys_addr0));
18039 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
18040 				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
18041 
18042 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=
18043 				cpu_to_le32(putPaddrLow(pdma_phys_addr1));
18044 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
18045 				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
18046 	if (!phba->sli4_hba.intr_enable)
18047 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18048 	else {
18049 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18050 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18051 	}
18052 	/* The IOCTL status is embedded in the mailbox subheader. */
18053 	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
18054 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18055 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18056 	if (!phba->sli4_hba.intr_enable)
18057 		mempool_free(mbox, phba->mbox_mem_pool);
18058 	else if (rc != MBX_TIMEOUT)
18059 		mempool_free(mbox, phba->mbox_mem_pool);
18060 	if (shdr_status || shdr_add_status || rc) {
18061 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18062 				"2511 POST_SGL mailbox failed with "
18063 				"status x%x add_status x%x, mbx status x%x\n",
18064 				shdr_status, shdr_add_status, rc);
18065 	}
18066 	return 0;
18067 }
18068 
18069 /**
18070  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
18071  * @phba: pointer to lpfc hba data structure.
18072  *
18073  * This routine is invoked to post rpi header templates to the
18074  * HBA consistent with the SLI-4 interface spec.  This routine
18075  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18076  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18077  *
18078  * Returns
18079  *	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18080  *	LPFC_RPI_ALLOC_ERROR if no rpis are available.
18081  **/
18082 static uint16_t
lpfc_sli4_alloc_xri(struct lpfc_hba * phba)18083 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
18084 {
18085 	unsigned long xri;
18086 
18087 	/*
18088 	 * Fetch the next logical xri.  Because this index is logical,
18089 	 * the driver starts at 0 each time.
18090 	 */
18091 	spin_lock_irq(&phba->hbalock);
18092 	xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,
18093 				 phba->sli4_hba.max_cfg_param.max_xri);
18094 	if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
18095 		spin_unlock_irq(&phba->hbalock);
18096 		return NO_XRI;
18097 	} else {
18098 		set_bit(xri, phba->sli4_hba.xri_bmask);
18099 		phba->sli4_hba.max_cfg_param.xri_used++;
18100 	}
18101 	spin_unlock_irq(&phba->hbalock);
18102 	return xri;
18103 }
18104 
18105 /**
18106  * __lpfc_sli4_free_xri - Release an xri for reuse.
18107  * @phba: pointer to lpfc hba data structure.
18108  * @xri: xri to release.
18109  *
18110  * This routine is invoked to release an xri to the pool of
18111  * available rpis maintained by the driver.
18112  **/
18113 static void
__lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18114 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18115 {
18116 	if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
18117 		phba->sli4_hba.max_cfg_param.xri_used--;
18118 	}
18119 }
18120 
18121 /**
18122  * lpfc_sli4_free_xri - Release an xri for reuse.
18123  * @phba: pointer to lpfc hba data structure.
18124  * @xri: xri to release.
18125  *
18126  * This routine is invoked to release an xri to the pool of
18127  * available rpis maintained by the driver.
18128  **/
18129 void
lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18130 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18131 {
18132 	spin_lock_irq(&phba->hbalock);
18133 	__lpfc_sli4_free_xri(phba, xri);
18134 	spin_unlock_irq(&phba->hbalock);
18135 }
18136 
18137 /**
18138  * lpfc_sli4_next_xritag - Get an xritag for the io
18139  * @phba: Pointer to HBA context object.
18140  *
18141  * This function gets an xritag for the iocb. If there is no unused xritag
18142  * it will return 0xffff.
18143  * The function returns the allocated xritag if successful, else returns zero.
18144  * Zero is not a valid xritag.
18145  * The caller is not required to hold any lock.
18146  **/
18147 uint16_t
lpfc_sli4_next_xritag(struct lpfc_hba * phba)18148 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
18149 {
18150 	uint16_t xri_index;
18151 
18152 	xri_index = lpfc_sli4_alloc_xri(phba);
18153 	if (xri_index == NO_XRI)
18154 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18155 				"2004 Failed to allocate XRI.last XRITAG is %d"
18156 				" Max XRI is %d, Used XRI is %d\n",
18157 				xri_index,
18158 				phba->sli4_hba.max_cfg_param.max_xri,
18159 				phba->sli4_hba.max_cfg_param.xri_used);
18160 	return xri_index;
18161 }
18162 
18163 /**
18164  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
18165  * @phba: pointer to lpfc hba data structure.
18166  * @post_sgl_list: pointer to els sgl entry list.
18167  * @post_cnt: number of els sgl entries on the list.
18168  *
18169  * This routine is invoked to post a block of driver's sgl pages to the
18170  * HBA using non-embedded mailbox command. No Lock is held. This routine
18171  * is only called when the driver is loading and after all IO has been
18172  * stopped.
18173  **/
18174 static int
lpfc_sli4_post_sgl_list(struct lpfc_hba * phba,struct list_head * post_sgl_list,int post_cnt)18175 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
18176 			    struct list_head *post_sgl_list,
18177 			    int post_cnt)
18178 {
18179 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
18180 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18181 	struct sgl_page_pairs *sgl_pg_pairs;
18182 	void *viraddr;
18183 	LPFC_MBOXQ_t *mbox;
18184 	uint32_t reqlen, alloclen, pg_pairs;
18185 	uint32_t mbox_tmo;
18186 	uint16_t xritag_start = 0;
18187 	int rc = 0;
18188 	uint32_t shdr_status, shdr_add_status;
18189 	union lpfc_sli4_cfg_shdr *shdr;
18190 
18191 	reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
18192 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18193 	if (reqlen > SLI4_PAGE_SIZE) {
18194 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18195 				"2559 Block sgl registration required DMA "
18196 				"size (%d) great than a page\n", reqlen);
18197 		return -ENOMEM;
18198 	}
18199 
18200 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18201 	if (!mbox)
18202 		return -ENOMEM;
18203 
18204 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18205 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18206 			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18207 			 LPFC_SLI4_MBX_NEMBED);
18208 
18209 	if (alloclen < reqlen) {
18210 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18211 				"0285 Allocated DMA memory size (%d) is "
18212 				"less than the requested DMA memory "
18213 				"size (%d)\n", alloclen, reqlen);
18214 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18215 		return -ENOMEM;
18216 	}
18217 	/* Set up the SGL pages in the non-embedded DMA pages */
18218 	viraddr = mbox->sge_array->addr[0];
18219 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18220 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18221 
18222 	pg_pairs = 0;
18223 	list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
18224 		/* Set up the sge entry */
18225 		sgl_pg_pairs->sgl_pg0_addr_lo =
18226 				cpu_to_le32(putPaddrLow(sglq_entry->phys));
18227 		sgl_pg_pairs->sgl_pg0_addr_hi =
18228 				cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18229 		sgl_pg_pairs->sgl_pg1_addr_lo =
18230 				cpu_to_le32(putPaddrLow(0));
18231 		sgl_pg_pairs->sgl_pg1_addr_hi =
18232 				cpu_to_le32(putPaddrHigh(0));
18233 
18234 		/* Keep the first xritag on the list */
18235 		if (pg_pairs == 0)
18236 			xritag_start = sglq_entry->sli4_xritag;
18237 		sgl_pg_pairs++;
18238 		pg_pairs++;
18239 	}
18240 
18241 	/* Complete initialization and perform endian conversion. */
18242 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18243 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
18244 	sgl->word0 = cpu_to_le32(sgl->word0);
18245 
18246 	if (!phba->sli4_hba.intr_enable)
18247 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18248 	else {
18249 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18250 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18251 	}
18252 	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18253 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18254 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18255 	if (!phba->sli4_hba.intr_enable)
18256 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18257 	else if (rc != MBX_TIMEOUT)
18258 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18259 	if (shdr_status || shdr_add_status || rc) {
18260 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18261 				"2513 POST_SGL_BLOCK mailbox command failed "
18262 				"status x%x add_status x%x mbx status x%x\n",
18263 				shdr_status, shdr_add_status, rc);
18264 		rc = -ENXIO;
18265 	}
18266 	return rc;
18267 }
18268 
18269 /**
18270  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
18271  * @phba: pointer to lpfc hba data structure.
18272  * @nblist: pointer to nvme buffer list.
18273  * @count: number of scsi buffers on the list.
18274  *
18275  * This routine is invoked to post a block of @count scsi sgl pages from a
18276  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
18277  * No Lock is held.
18278  *
18279  **/
18280 static int
lpfc_sli4_post_io_sgl_block(struct lpfc_hba * phba,struct list_head * nblist,int count)18281 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18282 			    int count)
18283 {
18284 	struct lpfc_io_buf *lpfc_ncmd;
18285 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18286 	struct sgl_page_pairs *sgl_pg_pairs;
18287 	void *viraddr;
18288 	LPFC_MBOXQ_t *mbox;
18289 	uint32_t reqlen, alloclen, pg_pairs;
18290 	uint32_t mbox_tmo;
18291 	uint16_t xritag_start = 0;
18292 	int rc = 0;
18293 	uint32_t shdr_status, shdr_add_status;
18294 	dma_addr_t pdma_phys_bpl1;
18295 	union lpfc_sli4_cfg_shdr *shdr;
18296 
18297 	/* Calculate the requested length of the dma memory */
18298 	reqlen = count * sizeof(struct sgl_page_pairs) +
18299 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18300 	if (reqlen > SLI4_PAGE_SIZE) {
18301 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
18302 				"6118 Block sgl registration required DMA "
18303 				"size (%d) great than a page\n", reqlen);
18304 		return -ENOMEM;
18305 	}
18306 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18307 	if (!mbox) {
18308 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18309 				"6119 Failed to allocate mbox cmd memory\n");
18310 		return -ENOMEM;
18311 	}
18312 
18313 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18314 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18315 				    LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18316 				    reqlen, LPFC_SLI4_MBX_NEMBED);
18317 
18318 	if (alloclen < reqlen) {
18319 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18320 				"6120 Allocated DMA memory size (%d) is "
18321 				"less than the requested DMA memory "
18322 				"size (%d)\n", alloclen, reqlen);
18323 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18324 		return -ENOMEM;
18325 	}
18326 
18327 	/* Get the first SGE entry from the non-embedded DMA memory */
18328 	viraddr = mbox->sge_array->addr[0];
18329 
18330 	/* Set up the SGL pages in the non-embedded DMA pages */
18331 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18332 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18333 
18334 	pg_pairs = 0;
18335 	list_for_each_entry(lpfc_ncmd, nblist, list) {
18336 		/* Set up the sge entry */
18337 		sgl_pg_pairs->sgl_pg0_addr_lo =
18338 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
18339 		sgl_pg_pairs->sgl_pg0_addr_hi =
18340 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
18341 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
18342 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18343 						SGL_PAGE_SIZE;
18344 		else
18345 			pdma_phys_bpl1 = 0;
18346 		sgl_pg_pairs->sgl_pg1_addr_lo =
18347 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18348 		sgl_pg_pairs->sgl_pg1_addr_hi =
18349 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18350 		/* Keep the first xritag on the list */
18351 		if (pg_pairs == 0)
18352 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
18353 		sgl_pg_pairs++;
18354 		pg_pairs++;
18355 	}
18356 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18357 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18358 	/* Perform endian conversion if necessary */
18359 	sgl->word0 = cpu_to_le32(sgl->word0);
18360 
18361 	if (!phba->sli4_hba.intr_enable) {
18362 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18363 	} else {
18364 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18365 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18366 	}
18367 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
18368 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18369 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18370 	if (!phba->sli4_hba.intr_enable)
18371 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18372 	else if (rc != MBX_TIMEOUT)
18373 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18374 	if (shdr_status || shdr_add_status || rc) {
18375 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18376 				"6125 POST_SGL_BLOCK mailbox command failed "
18377 				"status x%x add_status x%x mbx status x%x\n",
18378 				shdr_status, shdr_add_status, rc);
18379 		rc = -ENXIO;
18380 	}
18381 	return rc;
18382 }
18383 
18384 /**
18385  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
18386  * @phba: pointer to lpfc hba data structure.
18387  * @post_nblist: pointer to the nvme buffer list.
18388  * @sb_count: number of nvme buffers.
18389  *
18390  * This routine walks a list of nvme buffers that was passed in. It attempts
18391  * to construct blocks of nvme buffer sgls which contains contiguous xris and
18392  * uses the non-embedded SGL block post mailbox commands to post to the port.
18393  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18394  * embedded SGL post mailbox command for posting. The @post_nblist passed in
18395  * must be local list, thus no lock is needed when manipulate the list.
18396  *
18397  * Returns: 0 = failure, non-zero number of successfully posted buffers.
18398  **/
18399 int
lpfc_sli4_post_io_sgl_list(struct lpfc_hba * phba,struct list_head * post_nblist,int sb_count)18400 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18401 			   struct list_head *post_nblist, int sb_count)
18402 {
18403 	struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
18404 	int status, sgl_size;
18405 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18406 	dma_addr_t pdma_phys_sgl1;
18407 	int last_xritag = NO_XRI;
18408 	int cur_xritag;
18409 	LIST_HEAD(prep_nblist);
18410 	LIST_HEAD(blck_nblist);
18411 	LIST_HEAD(nvme_nblist);
18412 
18413 	/* sanity check */
18414 	if (sb_count <= 0)
18415 		return -EINVAL;
18416 
18417 	sgl_size = phba->cfg_sg_dma_buf_size;
18418 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18419 		list_del_init(&lpfc_ncmd->list);
18420 		block_cnt++;
18421 		if ((last_xritag != NO_XRI) &&
18422 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18423 			/* a hole in xri block, form a sgl posting block */
18424 			list_splice_init(&prep_nblist, &blck_nblist);
18425 			post_cnt = block_cnt - 1;
18426 			/* prepare list for next posting block */
18427 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18428 			block_cnt = 1;
18429 		} else {
18430 			/* prepare list for next posting block */
18431 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18432 			/* enough sgls for non-embed sgl mbox command */
18433 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18434 				list_splice_init(&prep_nblist, &blck_nblist);
18435 				post_cnt = block_cnt;
18436 				block_cnt = 0;
18437 			}
18438 		}
18439 		num_posting++;
18440 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18441 
18442 		/* end of repost sgl list condition for NVME buffers */
18443 		if (num_posting == sb_count) {
18444 			if (post_cnt == 0) {
18445 				/* last sgl posting block */
18446 				list_splice_init(&prep_nblist, &blck_nblist);
18447 				post_cnt = block_cnt;
18448 			} else if (block_cnt == 1) {
18449 				/* last single sgl with non-contiguous xri */
18450 				if (sgl_size > SGL_PAGE_SIZE)
18451 					pdma_phys_sgl1 =
18452 						lpfc_ncmd->dma_phys_sgl +
18453 						SGL_PAGE_SIZE;
18454 				else
18455 					pdma_phys_sgl1 = 0;
18456 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18457 				status = lpfc_sli4_post_sgl(
18458 						phba, lpfc_ncmd->dma_phys_sgl,
18459 						pdma_phys_sgl1, cur_xritag);
18460 				if (status) {
18461 					/* Post error.  Buffer unavailable. */
18462 					lpfc_ncmd->flags |=
18463 						LPFC_SBUF_NOT_POSTED;
18464 				} else {
18465 					/* Post success. Bffer available. */
18466 					lpfc_ncmd->flags &=
18467 						~LPFC_SBUF_NOT_POSTED;
18468 					lpfc_ncmd->status = IOSTAT_SUCCESS;
18469 					num_posted++;
18470 				}
18471 				/* success, put on NVME buffer sgl list */
18472 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18473 			}
18474 		}
18475 
18476 		/* continue until a nembed page worth of sgls */
18477 		if (post_cnt == 0)
18478 			continue;
18479 
18480 		/* post block of NVME buffer list sgls */
18481 		status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18482 						     post_cnt);
18483 
18484 		/* don't reset xirtag due to hole in xri block */
18485 		if (block_cnt == 0)
18486 			last_xritag = NO_XRI;
18487 
18488 		/* reset NVME buffer post count for next round of posting */
18489 		post_cnt = 0;
18490 
18491 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18492 		while (!list_empty(&blck_nblist)) {
18493 			list_remove_head(&blck_nblist, lpfc_ncmd,
18494 					 struct lpfc_io_buf, list);
18495 			if (status) {
18496 				/* Post error.  Mark buffer unavailable. */
18497 				lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
18498 			} else {
18499 				/* Post success, Mark buffer available. */
18500 				lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
18501 				lpfc_ncmd->status = IOSTAT_SUCCESS;
18502 				num_posted++;
18503 			}
18504 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18505 		}
18506 	}
18507 	/* Push NVME buffers with sgl posted to the available list */
18508 	lpfc_io_buf_replenish(phba, &nvme_nblist);
18509 
18510 	return num_posted;
18511 }
18512 
18513 /**
18514  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18515  * @phba: pointer to lpfc_hba struct that the frame was received on
18516  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18517  *
18518  * This function checks the fields in the @fc_hdr to see if the FC frame is a
18519  * valid type of frame that the LPFC driver will handle. This function will
18520  * return a zero if the frame is a valid frame or a non zero value when the
18521  * frame does not pass the check.
18522  **/
18523 static int
lpfc_fc_frame_check(struct lpfc_hba * phba,struct fc_frame_header * fc_hdr)18524 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18525 {
18526 	/*  make rctl_names static to save stack space */
18527 	struct fc_vft_header *fc_vft_hdr;
18528 	struct fc_app_header *fc_app_hdr;
18529 	uint32_t *header = (uint32_t *) fc_hdr;
18530 
18531 #define FC_RCTL_MDS_DIAGS	0xF4
18532 
18533 	switch (fc_hdr->fh_r_ctl) {
18534 	case FC_RCTL_DD_UNCAT:		/* uncategorized information */
18535 	case FC_RCTL_DD_SOL_DATA:	/* solicited data */
18536 	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */
18537 	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */
18538 	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */
18539 	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */
18540 	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */
18541 	case FC_RCTL_DD_CMD_STATUS:	/* command status */
18542 	case FC_RCTL_ELS_REQ:	/* extended link services request */
18543 	case FC_RCTL_ELS_REP:	/* extended link services reply */
18544 	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */
18545 	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */
18546 	case FC_RCTL_BA_ABTS: 	/* basic link service abort */
18547 	case FC_RCTL_BA_RMC: 	/* remove connection */
18548 	case FC_RCTL_BA_ACC:	/* basic accept */
18549 	case FC_RCTL_BA_RJT:	/* basic reject */
18550 	case FC_RCTL_BA_PRMT:
18551 	case FC_RCTL_ACK_1:	/* acknowledge_1 */
18552 	case FC_RCTL_ACK_0:	/* acknowledge_0 */
18553 	case FC_RCTL_P_RJT:	/* port reject */
18554 	case FC_RCTL_F_RJT:	/* fabric reject */
18555 	case FC_RCTL_P_BSY:	/* port busy */
18556 	case FC_RCTL_F_BSY:	/* fabric busy to data frame */
18557 	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */
18558 	case FC_RCTL_LCR:	/* link credit reset */
18559 	case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
18560 	case FC_RCTL_END:	/* end */
18561 		break;
18562 	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */
18563 		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18564 		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18565 		return lpfc_fc_frame_check(phba, fc_hdr);
18566 	case FC_RCTL_BA_NOP:	/* basic link service NOP */
18567 	default:
18568 		goto drop;
18569 	}
18570 
18571 	switch (fc_hdr->fh_type) {
18572 	case FC_TYPE_BLS:
18573 	case FC_TYPE_ELS:
18574 	case FC_TYPE_FCP:
18575 	case FC_TYPE_CT:
18576 	case FC_TYPE_NVME:
18577 		break;
18578 	case FC_TYPE_IP:
18579 	case FC_TYPE_ILS:
18580 	default:
18581 		goto drop;
18582 	}
18583 
18584 	if (unlikely(phba->link_flag == LS_LOOPBACK_MODE &&
18585 				phba->cfg_vmid_app_header)) {
18586 		/* Application header is 16B device header */
18587 		if (fc_hdr->fh_df_ctl & LPFC_FC_16B_DEVICE_HEADER) {
18588 			fc_app_hdr = (struct fc_app_header *) (fc_hdr + 1);
18589 			if (be32_to_cpu(fc_app_hdr->src_app_id) !=
18590 					LOOPBACK_SRC_APPID) {
18591 				lpfc_printf_log(phba, KERN_WARNING,
18592 						LOG_ELS | LOG_LIBDFC,
18593 						"1932 Loopback src app id "
18594 						"not matched, app_id:x%x\n",
18595 						be32_to_cpu(fc_app_hdr->src_app_id));
18596 
18597 				goto drop;
18598 			}
18599 		} else {
18600 			lpfc_printf_log(phba, KERN_WARNING,
18601 					LOG_ELS | LOG_LIBDFC,
18602 					"1933 Loopback df_ctl bit not set, "
18603 					"df_ctl:x%x\n",
18604 					fc_hdr->fh_df_ctl);
18605 
18606 			goto drop;
18607 		}
18608 	}
18609 
18610 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
18611 			"2538 Received frame rctl:x%x, type:x%x, "
18612 			"frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
18613 			fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18614 			be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18615 			be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18616 			be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18617 			be32_to_cpu(header[6]));
18618 	return 0;
18619 drop:
18620 	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
18621 			"2539 Dropped frame rctl:x%x type:x%x\n",
18622 			fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18623 	return 1;
18624 }
18625 
18626 /**
18627  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18628  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18629  *
18630  * This function processes the FC header to retrieve the VFI from the VF
18631  * header, if one exists. This function will return the VFI if one exists
18632  * or 0 if no VSAN Header exists.
18633  **/
18634 static uint32_t
lpfc_fc_hdr_get_vfi(struct fc_frame_header * fc_hdr)18635 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18636 {
18637 	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18638 
18639 	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18640 		return 0;
18641 	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18642 }
18643 
18644 /**
18645  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18646  * @phba: Pointer to the HBA structure to search for the vport on
18647  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18648  * @fcfi: The FC Fabric ID that the frame came from
18649  * @did: Destination ID to match against
18650  *
18651  * This function searches the @phba for a vport that matches the content of the
18652  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18653  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18654  * returns the matching vport pointer or NULL if unable to match frame to a
18655  * vport.
18656  **/
18657 static struct lpfc_vport *
lpfc_fc_frame_to_vport(struct lpfc_hba * phba,struct fc_frame_header * fc_hdr,uint16_t fcfi,uint32_t did)18658 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
18659 		       uint16_t fcfi, uint32_t did)
18660 {
18661 	struct lpfc_vport **vports;
18662 	struct lpfc_vport *vport = NULL;
18663 	int i;
18664 
18665 	if (did == Fabric_DID)
18666 		return phba->pport;
18667 	if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
18668 	    phba->link_state != LPFC_HBA_READY)
18669 		return phba->pport;
18670 
18671 	vports = lpfc_create_vport_work_array(phba);
18672 	if (vports != NULL) {
18673 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18674 			if (phba->fcf.fcfi == fcfi &&
18675 			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18676 			    vports[i]->fc_myDID == did) {
18677 				vport = vports[i];
18678 				break;
18679 			}
18680 		}
18681 	}
18682 	lpfc_destroy_vport_work_array(phba, vports);
18683 	return vport;
18684 }
18685 
18686 /**
18687  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18688  * @vport: The vport to work on.
18689  *
18690  * This function updates the receive sequence time stamp for this vport. The
18691  * receive sequence time stamp indicates the time that the last frame of the
18692  * the sequence that has been idle for the longest amount of time was received.
18693  * the driver uses this time stamp to indicate if any received sequences have
18694  * timed out.
18695  **/
18696 static void
lpfc_update_rcv_time_stamp(struct lpfc_vport * vport)18697 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18698 {
18699 	struct lpfc_dmabuf *h_buf;
18700 	struct hbq_dmabuf *dmabuf = NULL;
18701 
18702 	/* get the oldest sequence on the rcv list */
18703 	h_buf = list_get_first(&vport->rcv_buffer_list,
18704 			       struct lpfc_dmabuf, list);
18705 	if (!h_buf)
18706 		return;
18707 	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18708 	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18709 }
18710 
18711 /**
18712  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18713  * @vport: The vport that the received sequences were sent to.
18714  *
18715  * This function cleans up all outstanding received sequences. This is called
18716  * by the driver when a link event or user action invalidates all the received
18717  * sequences.
18718  **/
18719 void
lpfc_cleanup_rcv_buffers(struct lpfc_vport * vport)18720 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18721 {
18722 	struct lpfc_dmabuf *h_buf, *hnext;
18723 	struct lpfc_dmabuf *d_buf, *dnext;
18724 	struct hbq_dmabuf *dmabuf = NULL;
18725 
18726 	/* start with the oldest sequence on the rcv list */
18727 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18728 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18729 		list_del_init(&dmabuf->hbuf.list);
18730 		list_for_each_entry_safe(d_buf, dnext,
18731 					 &dmabuf->dbuf.list, list) {
18732 			list_del_init(&d_buf->list);
18733 			lpfc_in_buf_free(vport->phba, d_buf);
18734 		}
18735 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18736 	}
18737 }
18738 
18739 /**
18740  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18741  * @vport: The vport that the received sequences were sent to.
18742  *
18743  * This function determines whether any received sequences have timed out by
18744  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18745  * indicates that there is at least one timed out sequence this routine will
18746  * go through the received sequences one at a time from most inactive to most
18747  * active to determine which ones need to be cleaned up. Once it has determined
18748  * that a sequence needs to be cleaned up it will simply free up the resources
18749  * without sending an abort.
18750  **/
18751 void
lpfc_rcv_seq_check_edtov(struct lpfc_vport * vport)18752 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18753 {
18754 	struct lpfc_dmabuf *h_buf, *hnext;
18755 	struct lpfc_dmabuf *d_buf, *dnext;
18756 	struct hbq_dmabuf *dmabuf = NULL;
18757 	unsigned long timeout;
18758 	int abort_count = 0;
18759 
18760 	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18761 		   vport->rcv_buffer_time_stamp);
18762 	if (list_empty(&vport->rcv_buffer_list) ||
18763 	    time_before(jiffies, timeout))
18764 		return;
18765 	/* start with the oldest sequence on the rcv list */
18766 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18767 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18768 		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18769 			   dmabuf->time_stamp);
18770 		if (time_before(jiffies, timeout))
18771 			break;
18772 		abort_count++;
18773 		list_del_init(&dmabuf->hbuf.list);
18774 		list_for_each_entry_safe(d_buf, dnext,
18775 					 &dmabuf->dbuf.list, list) {
18776 			list_del_init(&d_buf->list);
18777 			lpfc_in_buf_free(vport->phba, d_buf);
18778 		}
18779 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18780 	}
18781 	if (abort_count)
18782 		lpfc_update_rcv_time_stamp(vport);
18783 }
18784 
18785 /**
18786  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
18787  * @vport: pointer to a vitural port
18788  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18789  *
18790  * This function searches through the existing incomplete sequences that have
18791  * been sent to this @vport. If the frame matches one of the incomplete
18792  * sequences then the dbuf in the @dmabuf is added to the list of frames that
18793  * make up that sequence. If no sequence is found that matches this frame then
18794  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18795  * This function returns a pointer to the first dmabuf in the sequence list that
18796  * the frame was linked to.
18797  **/
18798 static struct hbq_dmabuf *
lpfc_fc_frame_add(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18799 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18800 {
18801 	struct fc_frame_header *new_hdr;
18802 	struct fc_frame_header *temp_hdr;
18803 	struct lpfc_dmabuf *d_buf;
18804 	struct lpfc_dmabuf *h_buf;
18805 	struct hbq_dmabuf *seq_dmabuf = NULL;
18806 	struct hbq_dmabuf *temp_dmabuf = NULL;
18807 	uint8_t	found = 0;
18808 
18809 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18810 	dmabuf->time_stamp = jiffies;
18811 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18812 
18813 	/* Use the hdr_buf to find the sequence that this frame belongs to */
18814 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18815 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18816 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18817 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18818 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18819 			continue;
18820 		/* found a pending sequence that matches this frame */
18821 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18822 		break;
18823 	}
18824 	if (!seq_dmabuf) {
18825 		/*
18826 		 * This indicates first frame received for this sequence.
18827 		 * Queue the buffer on the vport's rcv_buffer_list.
18828 		 */
18829 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18830 		lpfc_update_rcv_time_stamp(vport);
18831 		return dmabuf;
18832 	}
18833 	temp_hdr = seq_dmabuf->hbuf.virt;
18834 	if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18835 		be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18836 		list_del_init(&seq_dmabuf->hbuf.list);
18837 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18838 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18839 		lpfc_update_rcv_time_stamp(vport);
18840 		return dmabuf;
18841 	}
18842 	/* move this sequence to the tail to indicate a young sequence */
18843 	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18844 	seq_dmabuf->time_stamp = jiffies;
18845 	lpfc_update_rcv_time_stamp(vport);
18846 	if (list_empty(&seq_dmabuf->dbuf.list)) {
18847 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18848 		return seq_dmabuf;
18849 	}
18850 	/* find the correct place in the sequence to insert this frame */
18851 	d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18852 	while (!found) {
18853 		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18854 		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18855 		/*
18856 		 * If the frame's sequence count is greater than the frame on
18857 		 * the list then insert the frame right after this frame
18858 		 */
18859 		if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18860 			be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18861 			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
18862 			found = 1;
18863 			break;
18864 		}
18865 
18866 		if (&d_buf->list == &seq_dmabuf->dbuf.list)
18867 			break;
18868 		d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
18869 	}
18870 
18871 	if (found)
18872 		return seq_dmabuf;
18873 	return NULL;
18874 }
18875 
18876 /**
18877  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18878  * @vport: pointer to a vitural port
18879  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18880  *
18881  * This function tries to abort from the partially assembed sequence, described
18882  * by the information from basic abbort @dmabuf. It checks to see whether such
18883  * partially assembled sequence held by the driver. If so, it shall free up all
18884  * the frames from the partially assembled sequence.
18885  *
18886  * Return
18887  * true  -- if there is matching partially assembled sequence present and all
18888  *          the frames freed with the sequence;
18889  * false -- if there is no matching partially assembled sequence present so
18890  *          nothing got aborted in the lower layer driver
18891  **/
18892 static bool
lpfc_sli4_abort_partial_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18893 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18894 			    struct hbq_dmabuf *dmabuf)
18895 {
18896 	struct fc_frame_header *new_hdr;
18897 	struct fc_frame_header *temp_hdr;
18898 	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18899 	struct hbq_dmabuf *seq_dmabuf = NULL;
18900 
18901 	/* Use the hdr_buf to find the sequence that matches this frame */
18902 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18903 	INIT_LIST_HEAD(&dmabuf->hbuf.list);
18904 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18905 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18906 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18907 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18908 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18909 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18910 			continue;
18911 		/* found a pending sequence that matches this frame */
18912 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18913 		break;
18914 	}
18915 
18916 	/* Free up all the frames from the partially assembled sequence */
18917 	if (seq_dmabuf) {
18918 		list_for_each_entry_safe(d_buf, n_buf,
18919 					 &seq_dmabuf->dbuf.list, list) {
18920 			list_del_init(&d_buf->list);
18921 			lpfc_in_buf_free(vport->phba, d_buf);
18922 		}
18923 		return true;
18924 	}
18925 	return false;
18926 }
18927 
18928 /**
18929  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18930  * @vport: pointer to a vitural port
18931  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18932  *
18933  * This function tries to abort from the assembed sequence from upper level
18934  * protocol, described by the information from basic abbort @dmabuf. It
18935  * checks to see whether such pending context exists at upper level protocol.
18936  * If so, it shall clean up the pending context.
18937  *
18938  * Return
18939  * true  -- if there is matching pending context of the sequence cleaned
18940  *          at ulp;
18941  * false -- if there is no matching pending context of the sequence present
18942  *          at ulp.
18943  **/
18944 static bool
lpfc_sli4_abort_ulp_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18945 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18946 {
18947 	struct lpfc_hba *phba = vport->phba;
18948 	int handled;
18949 
18950 	/* Accepting abort at ulp with SLI4 only */
18951 	if (phba->sli_rev < LPFC_SLI_REV4)
18952 		return false;
18953 
18954 	/* Register all caring upper level protocols to attend abort */
18955 	handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18956 	if (handled)
18957 		return true;
18958 
18959 	return false;
18960 }
18961 
18962 /**
18963  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18964  * @phba: Pointer to HBA context object.
18965  * @cmd_iocbq: pointer to the command iocbq structure.
18966  * @rsp_iocbq: pointer to the response iocbq structure.
18967  *
18968  * This function handles the sequence abort response iocb command complete
18969  * event. It properly releases the memory allocated to the sequence abort
18970  * accept iocb.
18971  **/
18972 static void
lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmd_iocbq,struct lpfc_iocbq * rsp_iocbq)18973 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18974 			     struct lpfc_iocbq *cmd_iocbq,
18975 			     struct lpfc_iocbq *rsp_iocbq)
18976 {
18977 	if (cmd_iocbq) {
18978 		lpfc_nlp_put(cmd_iocbq->ndlp);
18979 		lpfc_sli_release_iocbq(phba, cmd_iocbq);
18980 	}
18981 
18982 	/* Failure means BLS ABORT RSP did not get delivered to remote node*/
18983 	if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18984 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18985 			"3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18986 			get_job_ulpstatus(phba, rsp_iocbq),
18987 			get_job_word4(phba, rsp_iocbq));
18988 }
18989 
18990 /**
18991  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18992  * @phba: Pointer to HBA context object.
18993  * @xri: xri id in transaction.
18994  *
18995  * This function validates the xri maps to the known range of XRIs allocated an
18996  * used by the driver.
18997  **/
18998 uint16_t
lpfc_sli4_xri_inrange(struct lpfc_hba * phba,uint16_t xri)18999 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
19000 		      uint16_t xri)
19001 {
19002 	uint16_t i;
19003 
19004 	for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
19005 		if (xri == phba->sli4_hba.xri_ids[i])
19006 			return i;
19007 	}
19008 	return NO_XRI;
19009 }
19010 
19011 /**
19012  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
19013  * @vport: pointer to a virtual port.
19014  * @fc_hdr: pointer to a FC frame header.
19015  * @aborted: was the partially assembled receive sequence successfully aborted
19016  *
19017  * This function sends a basic response to a previous unsol sequence abort
19018  * event after aborting the sequence handling.
19019  **/
19020 void
lpfc_sli4_seq_abort_rsp(struct lpfc_vport * vport,struct fc_frame_header * fc_hdr,bool aborted)19021 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
19022 			struct fc_frame_header *fc_hdr, bool aborted)
19023 {
19024 	struct lpfc_hba *phba = vport->phba;
19025 	struct lpfc_iocbq *ctiocb = NULL;
19026 	struct lpfc_nodelist *ndlp;
19027 	uint16_t oxid, rxid, xri, lxri;
19028 	uint32_t sid, fctl;
19029 	union lpfc_wqe128 *icmd;
19030 	int rc;
19031 
19032 	if (!lpfc_is_link_up(phba))
19033 		return;
19034 
19035 	sid = sli4_sid_from_fc_hdr(fc_hdr);
19036 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
19037 	rxid = be16_to_cpu(fc_hdr->fh_rx_id);
19038 
19039 	ndlp = lpfc_findnode_did(vport, sid);
19040 	if (!ndlp) {
19041 		ndlp = lpfc_nlp_init(vport, sid);
19042 		if (!ndlp) {
19043 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
19044 					 "1268 Failed to allocate ndlp for "
19045 					 "oxid:x%x SID:x%x\n", oxid, sid);
19046 			return;
19047 		}
19048 		/* Put ndlp onto vport node list */
19049 		lpfc_enqueue_node(vport, ndlp);
19050 	}
19051 
19052 	/* Allocate buffer for rsp iocb */
19053 	ctiocb = lpfc_sli_get_iocbq(phba);
19054 	if (!ctiocb)
19055 		return;
19056 
19057 	icmd = &ctiocb->wqe;
19058 
19059 	/* Extract the F_CTL field from FC_HDR */
19060 	fctl = sli4_fctl_from_fc_hdr(fc_hdr);
19061 
19062 	ctiocb->ndlp = lpfc_nlp_get(ndlp);
19063 	if (!ctiocb->ndlp) {
19064 		lpfc_sli_release_iocbq(phba, ctiocb);
19065 		return;
19066 	}
19067 
19068 	ctiocb->vport = vport;
19069 	ctiocb->cmd_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
19070 	ctiocb->sli4_lxritag = NO_XRI;
19071 	ctiocb->sli4_xritag = NO_XRI;
19072 	ctiocb->abort_rctl = FC_RCTL_BA_ACC;
19073 
19074 	if (fctl & FC_FC_EX_CTX)
19075 		/* Exchange responder sent the abort so we
19076 		 * own the oxid.
19077 		 */
19078 		xri = oxid;
19079 	else
19080 		xri = rxid;
19081 	lxri = lpfc_sli4_xri_inrange(phba, xri);
19082 	if (lxri != NO_XRI)
19083 		lpfc_set_rrq_active(phba, ndlp, lxri,
19084 			(xri == oxid) ? rxid : oxid, 0);
19085 	/* For BA_ABTS from exchange responder, if the logical xri with
19086 	 * the oxid maps to the FCP XRI range, the port no longer has
19087 	 * that exchange context, send a BLS_RJT. Override the IOCB for
19088 	 * a BA_RJT.
19089 	 */
19090 	if ((fctl & FC_FC_EX_CTX) &&
19091 	    (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
19092 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19093 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19094 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19095 		       FC_BA_RJT_INV_XID);
19096 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19097 		       FC_BA_RJT_UNABLE);
19098 	}
19099 
19100 	/* If BA_ABTS failed to abort a partially assembled receive sequence,
19101 	 * the driver no longer has that exchange, send a BLS_RJT. Override
19102 	 * the IOCB for a BA_RJT.
19103 	 */
19104 	if (aborted == false) {
19105 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19106 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19107 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19108 		       FC_BA_RJT_INV_XID);
19109 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19110 		       FC_BA_RJT_UNABLE);
19111 	}
19112 
19113 	if (fctl & FC_FC_EX_CTX) {
19114 		/* ABTS sent by responder to CT exchange, construction
19115 		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
19116 		 * field and RX_ID from ABTS for RX_ID field.
19117 		 */
19118 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_RSP;
19119 		bf_set(xmit_bls_rsp64_rxid, &icmd->xmit_bls_rsp, rxid);
19120 	} else {
19121 		/* ABTS sent by initiator to CT exchange, construction
19122 		 * of BA_ACC will need to allocate a new XRI as for the
19123 		 * XRI_TAG field.
19124 		 */
19125 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_INT;
19126 	}
19127 
19128 	/* OX_ID is invariable to who sent ABTS to CT exchange */
19129 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, oxid);
19130 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, rxid);
19131 
19132 	/* Use CT=VPI */
19133 	bf_set(wqe_els_did, &icmd->xmit_bls_rsp.wqe_dest,
19134 	       ndlp->nlp_DID);
19135 	bf_set(xmit_bls_rsp64_temprpi, &icmd->xmit_bls_rsp,
19136 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
19137 	bf_set(wqe_cmnd, &icmd->generic.wqe_com, CMD_XMIT_BLS_RSP64_CX);
19138 
19139 	/* Xmit CT abts response on exchange <xid> */
19140 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
19141 			 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
19142 			 ctiocb->abort_rctl, oxid, phba->link_state);
19143 
19144 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
19145 	if (rc == IOCB_ERROR) {
19146 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19147 				 "2925 Failed to issue CT ABTS RSP x%x on "
19148 				 "xri x%x, Data x%x\n",
19149 				 ctiocb->abort_rctl, oxid,
19150 				 phba->link_state);
19151 		lpfc_nlp_put(ndlp);
19152 		ctiocb->ndlp = NULL;
19153 		lpfc_sli_release_iocbq(phba, ctiocb);
19154 	}
19155 
19156 	/* if only usage of this nodelist is BLS response, release initial ref
19157 	 * to free ndlp when transmit completes
19158 	 */
19159 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE &&
19160 	    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
19161 	    !(ndlp->fc4_xpt_flags & (NVME_XPT_REGD | SCSI_XPT_REGD))) {
19162 		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
19163 		lpfc_nlp_put(ndlp);
19164 	}
19165 }
19166 
19167 /**
19168  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
19169  * @vport: Pointer to the vport on which this sequence was received
19170  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19171  *
19172  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
19173  * receive sequence is only partially assembed by the driver, it shall abort
19174  * the partially assembled frames for the sequence. Otherwise, if the
19175  * unsolicited receive sequence has been completely assembled and passed to
19176  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
19177  * unsolicited sequence has been aborted. After that, it will issue a basic
19178  * accept to accept the abort.
19179  **/
19180 static void
lpfc_sli4_handle_unsol_abort(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19181 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
19182 			     struct hbq_dmabuf *dmabuf)
19183 {
19184 	struct lpfc_hba *phba = vport->phba;
19185 	struct fc_frame_header fc_hdr;
19186 	uint32_t fctl;
19187 	bool aborted;
19188 
19189 	/* Make a copy of fc_hdr before the dmabuf being released */
19190 	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
19191 	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
19192 
19193 	if (fctl & FC_FC_EX_CTX) {
19194 		/* ABTS by responder to exchange, no cleanup needed */
19195 		aborted = true;
19196 	} else {
19197 		/* ABTS by initiator to exchange, need to do cleanup */
19198 		aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19199 		if (aborted == false)
19200 			aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
19201 	}
19202 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19203 
19204 	if (phba->nvmet_support) {
19205 		lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19206 		return;
19207 	}
19208 
19209 	/* Respond with BA_ACC or BA_RJT accordingly */
19210 	lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
19211 }
19212 
19213 /**
19214  * lpfc_seq_complete - Indicates if a sequence is complete
19215  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19216  *
19217  * This function checks the sequence, starting with the frame described by
19218  * @dmabuf, to see if all the frames associated with this sequence are present.
19219  * the frames associated with this sequence are linked to the @dmabuf using the
19220  * dbuf list. This function looks for two major things. 1) That the first frame
19221  * has a sequence count of zero. 2) There is a frame with last frame of sequence
19222  * set. 3) That there are no holes in the sequence count. The function will
19223  * return 1 when the sequence is complete, otherwise it will return 0.
19224  **/
19225 static int
lpfc_seq_complete(struct hbq_dmabuf * dmabuf)19226 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19227 {
19228 	struct fc_frame_header *hdr;
19229 	struct lpfc_dmabuf *d_buf;
19230 	struct hbq_dmabuf *seq_dmabuf;
19231 	uint32_t fctl;
19232 	int seq_count = 0;
19233 
19234 	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19235 	/* make sure first fame of sequence has a sequence count of zero */
19236 	if (hdr->fh_seq_cnt != seq_count)
19237 		return 0;
19238 	fctl = (hdr->fh_f_ctl[0] << 16 |
19239 		hdr->fh_f_ctl[1] << 8 |
19240 		hdr->fh_f_ctl[2]);
19241 	/* If last frame of sequence we can return success. */
19242 	if (fctl & FC_FC_END_SEQ)
19243 		return 1;
19244 	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19245 		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19246 		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19247 		/* If there is a hole in the sequence count then fail. */
19248 		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
19249 			return 0;
19250 		fctl = (hdr->fh_f_ctl[0] << 16 |
19251 			hdr->fh_f_ctl[1] << 8 |
19252 			hdr->fh_f_ctl[2]);
19253 		/* If last frame of sequence we can return success. */
19254 		if (fctl & FC_FC_END_SEQ)
19255 			return 1;
19256 	}
19257 	return 0;
19258 }
19259 
19260 /**
19261  * lpfc_prep_seq - Prep sequence for ULP processing
19262  * @vport: Pointer to the vport on which this sequence was received
19263  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
19264  *
19265  * This function takes a sequence, described by a list of frames, and creates
19266  * a list of iocbq structures to describe the sequence. This iocbq list will be
19267  * used to issue to the generic unsolicited sequence handler. This routine
19268  * returns a pointer to the first iocbq in the list. If the function is unable
19269  * to allocate an iocbq then it throw out the received frames that were not
19270  * able to be described and return a pointer to the first iocbq. If unable to
19271  * allocate any iocbqs (including the first) this function will return NULL.
19272  **/
19273 static struct lpfc_iocbq *
lpfc_prep_seq(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19274 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19275 {
19276 	struct hbq_dmabuf *hbq_buf;
19277 	struct lpfc_dmabuf *d_buf, *n_buf;
19278 	struct lpfc_iocbq *first_iocbq, *iocbq;
19279 	struct fc_frame_header *fc_hdr;
19280 	uint32_t sid;
19281 	uint32_t len, tot_len;
19282 
19283 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19284 	/* remove from receive buffer list */
19285 	list_del_init(&seq_dmabuf->hbuf.list);
19286 	lpfc_update_rcv_time_stamp(vport);
19287 	/* get the Remote Port's SID */
19288 	sid = sli4_sid_from_fc_hdr(fc_hdr);
19289 	tot_len = 0;
19290 	/* Get an iocbq struct to fill in. */
19291 	first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19292 	if (first_iocbq) {
19293 		/* Initialize the first IOCB. */
19294 		first_iocbq->wcqe_cmpl.total_data_placed = 0;
19295 		bf_set(lpfc_wcqe_c_status, &first_iocbq->wcqe_cmpl,
19296 		       IOSTAT_SUCCESS);
19297 		first_iocbq->vport = vport;
19298 
19299 		/* Check FC Header to see what TYPE of frame we are rcv'ing */
19300 		if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19301 			bf_set(els_rsp64_sid, &first_iocbq->wqe.xmit_els_rsp,
19302 			       sli4_did_from_fc_hdr(fc_hdr));
19303 		}
19304 
19305 		bf_set(wqe_ctxt_tag, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19306 		       NO_XRI);
19307 		bf_set(wqe_rcvoxid, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19308 		       be16_to_cpu(fc_hdr->fh_ox_id));
19309 
19310 		/* put the first buffer into the first iocb */
19311 		tot_len = bf_get(lpfc_rcqe_length,
19312 				 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19313 
19314 		first_iocbq->cmd_dmabuf = &seq_dmabuf->dbuf;
19315 		first_iocbq->bpl_dmabuf = NULL;
19316 		/* Keep track of the BDE count */
19317 		first_iocbq->wcqe_cmpl.word3 = 1;
19318 
19319 		if (tot_len > LPFC_DATA_BUF_SIZE)
19320 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize =
19321 				LPFC_DATA_BUF_SIZE;
19322 		else
19323 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize = tot_len;
19324 
19325 		first_iocbq->wcqe_cmpl.total_data_placed = tot_len;
19326 		bf_set(wqe_els_did, &first_iocbq->wqe.xmit_els_rsp.wqe_dest,
19327 		       sid);
19328 	}
19329 	iocbq = first_iocbq;
19330 	/*
19331 	 * Each IOCBq can have two Buffers assigned, so go through the list
19332 	 * of buffers for this sequence and save two buffers in each IOCBq
19333 	 */
19334 	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19335 		if (!iocbq) {
19336 			lpfc_in_buf_free(vport->phba, d_buf);
19337 			continue;
19338 		}
19339 		if (!iocbq->bpl_dmabuf) {
19340 			iocbq->bpl_dmabuf = d_buf;
19341 			iocbq->wcqe_cmpl.word3++;
19342 			/* We need to get the size out of the right CQE */
19343 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19344 			len = bf_get(lpfc_rcqe_length,
19345 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19346 			iocbq->unsol_rcv_len = len;
19347 			iocbq->wcqe_cmpl.total_data_placed += len;
19348 			tot_len += len;
19349 		} else {
19350 			iocbq = lpfc_sli_get_iocbq(vport->phba);
19351 			if (!iocbq) {
19352 				if (first_iocbq) {
19353 					bf_set(lpfc_wcqe_c_status,
19354 					       &first_iocbq->wcqe_cmpl,
19355 					       IOSTAT_SUCCESS);
19356 					first_iocbq->wcqe_cmpl.parameter =
19357 						IOERR_NO_RESOURCES;
19358 				}
19359 				lpfc_in_buf_free(vport->phba, d_buf);
19360 				continue;
19361 			}
19362 			/* We need to get the size out of the right CQE */
19363 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19364 			len = bf_get(lpfc_rcqe_length,
19365 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19366 			iocbq->cmd_dmabuf = d_buf;
19367 			iocbq->bpl_dmabuf = NULL;
19368 			iocbq->wcqe_cmpl.word3 = 1;
19369 
19370 			if (len > LPFC_DATA_BUF_SIZE)
19371 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19372 					LPFC_DATA_BUF_SIZE;
19373 			else
19374 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19375 					len;
19376 
19377 			tot_len += len;
19378 			iocbq->wcqe_cmpl.total_data_placed = tot_len;
19379 			bf_set(wqe_els_did, &iocbq->wqe.xmit_els_rsp.wqe_dest,
19380 			       sid);
19381 			list_add_tail(&iocbq->list, &first_iocbq->list);
19382 		}
19383 	}
19384 	/* Free the sequence's header buffer */
19385 	if (!first_iocbq)
19386 		lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19387 
19388 	return first_iocbq;
19389 }
19390 
19391 static void
lpfc_sli4_send_seq_to_ulp(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19392 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19393 			  struct hbq_dmabuf *seq_dmabuf)
19394 {
19395 	struct fc_frame_header *fc_hdr;
19396 	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19397 	struct lpfc_hba *phba = vport->phba;
19398 
19399 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19400 	iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19401 	if (!iocbq) {
19402 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19403 				"2707 Ring %d handler: Failed to allocate "
19404 				"iocb Rctl x%x Type x%x received\n",
19405 				LPFC_ELS_RING,
19406 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19407 		return;
19408 	}
19409 	if (!lpfc_complete_unsol_iocb(phba,
19410 				      phba->sli4_hba.els_wq->pring,
19411 				      iocbq, fc_hdr->fh_r_ctl,
19412 				      fc_hdr->fh_type)) {
19413 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19414 				"2540 Ring %d handler: unexpected Rctl "
19415 				"x%x Type x%x received\n",
19416 				LPFC_ELS_RING,
19417 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19418 		lpfc_in_buf_free(phba, &seq_dmabuf->dbuf);
19419 	}
19420 
19421 	/* Free iocb created in lpfc_prep_seq */
19422 	list_for_each_entry_safe(curr_iocb, next_iocb,
19423 				 &iocbq->list, list) {
19424 		list_del_init(&curr_iocb->list);
19425 		lpfc_sli_release_iocbq(phba, curr_iocb);
19426 	}
19427 	lpfc_sli_release_iocbq(phba, iocbq);
19428 }
19429 
19430 static void
lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)19431 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19432 			    struct lpfc_iocbq *rspiocb)
19433 {
19434 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
19435 
19436 	if (pcmd && pcmd->virt)
19437 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19438 	kfree(pcmd);
19439 	lpfc_sli_release_iocbq(phba, cmdiocb);
19440 	lpfc_drain_txq(phba);
19441 }
19442 
19443 static void
lpfc_sli4_handle_mds_loopback(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19444 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19445 			      struct hbq_dmabuf *dmabuf)
19446 {
19447 	struct fc_frame_header *fc_hdr;
19448 	struct lpfc_hba *phba = vport->phba;
19449 	struct lpfc_iocbq *iocbq = NULL;
19450 	union  lpfc_wqe128 *pwqe;
19451 	struct lpfc_dmabuf *pcmd = NULL;
19452 	uint32_t frame_len;
19453 	int rc;
19454 	unsigned long iflags;
19455 
19456 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19457 	frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19458 
19459 	/* Send the received frame back */
19460 	iocbq = lpfc_sli_get_iocbq(phba);
19461 	if (!iocbq) {
19462 		/* Queue cq event and wakeup worker thread to process it */
19463 		spin_lock_irqsave(&phba->hbalock, iflags);
19464 		list_add_tail(&dmabuf->cq_event.list,
19465 			      &phba->sli4_hba.sp_queue_event);
19466 		spin_unlock_irqrestore(&phba->hbalock, iflags);
19467 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
19468 		lpfc_worker_wake_up(phba);
19469 		return;
19470 	}
19471 
19472 	/* Allocate buffer for command payload */
19473 	pcmd = kmalloc_obj(struct lpfc_dmabuf);
19474 	if (pcmd)
19475 		pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
19476 					    &pcmd->phys);
19477 	if (!pcmd || !pcmd->virt)
19478 		goto exit;
19479 
19480 	INIT_LIST_HEAD(&pcmd->list);
19481 
19482 	/* copyin the payload */
19483 	memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19484 
19485 	iocbq->cmd_dmabuf = pcmd;
19486 	iocbq->vport = vport;
19487 	iocbq->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
19488 	iocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
19489 	iocbq->num_bdes = 0;
19490 
19491 	pwqe = &iocbq->wqe;
19492 	/* fill in BDE's for command */
19493 	pwqe->gen_req.bde.addrHigh = putPaddrHigh(pcmd->phys);
19494 	pwqe->gen_req.bde.addrLow = putPaddrLow(pcmd->phys);
19495 	pwqe->gen_req.bde.tus.f.bdeSize = frame_len;
19496 	pwqe->gen_req.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
19497 
19498 	pwqe->send_frame.frame_len = frame_len;
19499 	pwqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((__be32 *)fc_hdr));
19500 	pwqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((__be32 *)fc_hdr + 1));
19501 	pwqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((__be32 *)fc_hdr + 2));
19502 	pwqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((__be32 *)fc_hdr + 3));
19503 	pwqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((__be32 *)fc_hdr + 4));
19504 	pwqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((__be32 *)fc_hdr + 5));
19505 
19506 	pwqe->generic.wqe_com.word7 = 0;
19507 	pwqe->generic.wqe_com.word10 = 0;
19508 
19509 	bf_set(wqe_cmnd, &pwqe->generic.wqe_com, CMD_SEND_FRAME);
19510 	bf_set(wqe_sof, &pwqe->generic.wqe_com, 0x2E); /* SOF byte */
19511 	bf_set(wqe_eof, &pwqe->generic.wqe_com, 0x41); /* EOF byte */
19512 	bf_set(wqe_lenloc, &pwqe->generic.wqe_com, 1);
19513 	bf_set(wqe_xbl, &pwqe->generic.wqe_com, 1);
19514 	bf_set(wqe_dbde, &pwqe->generic.wqe_com, 1);
19515 	bf_set(wqe_xc, &pwqe->generic.wqe_com, 1);
19516 	bf_set(wqe_cmd_type, &pwqe->generic.wqe_com, 0xA);
19517 	bf_set(wqe_cqid, &pwqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
19518 	bf_set(wqe_xri_tag, &pwqe->generic.wqe_com, iocbq->sli4_xritag);
19519 	bf_set(wqe_reqtag, &pwqe->generic.wqe_com, iocbq->iotag);
19520 	bf_set(wqe_class, &pwqe->generic.wqe_com, CLASS3);
19521 	pwqe->generic.wqe_com.abort_tag = iocbq->iotag;
19522 
19523 	iocbq->cmd_cmpl = lpfc_sli4_mds_loopback_cmpl;
19524 
19525 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19526 	if (rc == IOCB_ERROR)
19527 		goto exit;
19528 
19529 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19530 	return;
19531 
19532 exit:
19533 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19534 			"2023 Unable to process MDS loopback frame\n");
19535 	if (pcmd && pcmd->virt)
19536 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19537 	kfree(pcmd);
19538 	if (iocbq)
19539 		lpfc_sli_release_iocbq(phba, iocbq);
19540 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19541 }
19542 
19543 /**
19544  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19545  * @phba: Pointer to HBA context object.
19546  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
19547  *
19548  * This function is called with no lock held. This function processes all
19549  * the received buffers and gives it to upper layers when a received buffer
19550  * indicates that it is the final frame in the sequence. The interrupt
19551  * service routine processes received buffers at interrupt contexts.
19552  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19553  * appropriate receive function when the final frame in a sequence is received.
19554  **/
19555 void
lpfc_sli4_handle_received_buffer(struct lpfc_hba * phba,struct hbq_dmabuf * dmabuf)19556 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19557 				 struct hbq_dmabuf *dmabuf)
19558 {
19559 	struct hbq_dmabuf *seq_dmabuf;
19560 	struct fc_frame_header *fc_hdr;
19561 	struct lpfc_vport *vport;
19562 	uint32_t fcfi;
19563 	uint32_t did;
19564 
19565 	/* Process each received buffer */
19566 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19567 
19568 	if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19569 	    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19570 		vport = phba->pport;
19571 		/* Handle MDS Loopback frames */
19572 		if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
19573 			lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19574 		else
19575 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19576 		return;
19577 	}
19578 
19579 	/* check to see if this a valid type of frame */
19580 	if (lpfc_fc_frame_check(phba, fc_hdr)) {
19581 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19582 		return;
19583 	}
19584 
19585 	if ((bf_get(lpfc_cqe_code,
19586 		    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19587 		fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19588 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19589 	else
19590 		fcfi = bf_get(lpfc_rcqe_fcf_id,
19591 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19592 
19593 	if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19594 		vport = phba->pport;
19595 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19596 				"2023 MDS Loopback %d bytes\n",
19597 				bf_get(lpfc_rcqe_length,
19598 				       &dmabuf->cq_event.cqe.rcqe_cmpl));
19599 		/* Handle MDS Loopback frames */
19600 		lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19601 		return;
19602 	}
19603 
19604 	/* d_id this frame is directed to */
19605 	did = sli4_did_from_fc_hdr(fc_hdr);
19606 
19607 	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
19608 	if (!vport) {
19609 		/* throw out the frame */
19610 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19611 		return;
19612 	}
19613 
19614 	/* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19615 	if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19616 		(did != Fabric_DID)) {
19617 		/*
19618 		 * Throw out the frame if we are not pt2pt.
19619 		 * The pt2pt protocol allows for discovery frames
19620 		 * to be received without a registered VPI.
19621 		 */
19622 		if (!test_bit(FC_PT2PT, &vport->fc_flag) ||
19623 		    phba->link_state == LPFC_HBA_READY) {
19624 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19625 			return;
19626 		}
19627 	}
19628 
19629 	/* Handle the basic abort sequence (BA_ABTS) event */
19630 	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19631 		lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19632 		return;
19633 	}
19634 
19635 	/* Link this frame */
19636 	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19637 	if (!seq_dmabuf) {
19638 		/* unable to add frame to vport - throw it out */
19639 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19640 		return;
19641 	}
19642 	/* If not last frame in sequence continue processing frames. */
19643 	if (!lpfc_seq_complete(seq_dmabuf))
19644 		return;
19645 
19646 	/* Send the complete sequence to the upper layer protocol */
19647 	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
19648 }
19649 
19650 /**
19651  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19652  * @phba: pointer to lpfc hba data structure.
19653  *
19654  * This routine is invoked to post rpi header templates to the
19655  * HBA consistent with the SLI-4 interface spec.  This routine
19656  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19657  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19658  *
19659  * This routine does not require any locks.  It's usage is expected
19660  * to be driver load or reset recovery when the driver is
19661  * sequential.
19662  *
19663  * Return codes
19664  * 	0 - successful
19665  *      -EIO - The mailbox failed to complete successfully.
19666  * 	When this error occurs, the driver is not guaranteed
19667  *	to have any rpi regions posted to the device and
19668  *	must either attempt to repost the regions or take a
19669  *	fatal error.
19670  **/
19671 int
lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba * phba)19672 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19673 {
19674 	struct lpfc_rpi_hdr *rpi_page;
19675 	uint32_t rc = 0;
19676 	uint16_t lrpi = 0;
19677 
19678 	/* SLI4 ports that support extents do not require RPI headers. */
19679 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19680 		goto exit;
19681 	if (phba->sli4_hba.extents_in_use)
19682 		return -EIO;
19683 
19684 	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
19685 		/*
19686 		 * Assign the rpi headers a physical rpi only if the driver
19687 		 * has not initialized those resources.  A port reset only
19688 		 * needs the headers posted.
19689 		 */
19690 		if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19691 		    LPFC_RPI_RSRC_RDY)
19692 			rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19693 
19694 		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19695 		if (rc != MBX_SUCCESS) {
19696 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19697 					"2008 Error %d posting all rpi "
19698 					"headers\n", rc);
19699 			rc = -EIO;
19700 			break;
19701 		}
19702 	}
19703 
19704  exit:
19705 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19706 	       LPFC_RPI_RSRC_RDY);
19707 	return rc;
19708 }
19709 
19710 /**
19711  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19712  * @phba: pointer to lpfc hba data structure.
19713  * @rpi_page:  pointer to the rpi memory region.
19714  *
19715  * This routine is invoked to post a single rpi header to the
19716  * HBA consistent with the SLI-4 interface spec.  This memory region
19717  * maps up to 64 rpi context regions.
19718  *
19719  * Return codes
19720  * 	0 - successful
19721  * 	-ENOMEM - No available memory
19722  *      -EIO - The mailbox failed to complete successfully.
19723  **/
19724 int
lpfc_sli4_post_rpi_hdr(struct lpfc_hba * phba,struct lpfc_rpi_hdr * rpi_page)19725 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19726 {
19727 	LPFC_MBOXQ_t *mboxq;
19728 	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19729 	uint32_t rc = 0;
19730 	uint32_t shdr_status, shdr_add_status;
19731 	union lpfc_sli4_cfg_shdr *shdr;
19732 
19733 	/* SLI4 ports that support extents do not require RPI headers. */
19734 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19735 		return rc;
19736 	if (phba->sli4_hba.extents_in_use)
19737 		return -EIO;
19738 
19739 	/* The port is notified of the header region via a mailbox command. */
19740 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19741 	if (!mboxq) {
19742 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19743 				"2001 Unable to allocate memory for issuing "
19744 				"SLI_CONFIG_SPECIAL mailbox command\n");
19745 		return -ENOMEM;
19746 	}
19747 
19748 	/* Post all rpi memory regions to the port. */
19749 	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
19750 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19751 			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19752 			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
19753 			 sizeof(struct lpfc_sli4_cfg_mhdr),
19754 			 LPFC_SLI4_MBX_EMBED);
19755 
19756 
19757 	/* Post the physical rpi to the port for this rpi header. */
19758 	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19759 	       rpi_page->start_rpi);
19760 	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19761 	       hdr_tmpl, rpi_page->page_count);
19762 
19763 	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19764 	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
19765 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19766 	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19767 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19768 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19769 	mempool_free(mboxq, phba->mbox_mem_pool);
19770 	if (shdr_status || shdr_add_status || rc) {
19771 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19772 				"2514 POST_RPI_HDR mailbox failed with "
19773 				"status x%x add_status x%x, mbx status x%x\n",
19774 				shdr_status, shdr_add_status, rc);
19775 		rc = -ENXIO;
19776 	} else {
19777 		/*
19778 		 * The next_rpi stores the next logical module-64 rpi value used
19779 		 * to post physical rpis in subsequent rpi postings.
19780 		 */
19781 		spin_lock_irq(&phba->hbalock);
19782 		phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19783 		spin_unlock_irq(&phba->hbalock);
19784 	}
19785 	return rc;
19786 }
19787 
19788 /**
19789  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19790  * @phba: pointer to lpfc hba data structure.
19791  *
19792  * This routine is invoked to post rpi header templates to the
19793  * HBA consistent with the SLI-4 interface spec.  This routine
19794  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19795  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19796  *
19797  * Returns
19798  * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
19799  * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.
19800  **/
19801 int
lpfc_sli4_alloc_rpi(struct lpfc_hba * phba)19802 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19803 {
19804 	unsigned long rpi;
19805 	uint16_t max_rpi, rpi_limit;
19806 	uint16_t rpi_remaining, lrpi = 0;
19807 	struct lpfc_rpi_hdr *rpi_hdr;
19808 	unsigned long iflag;
19809 
19810 	/*
19811 	 * Fetch the next logical rpi.  Because this index is logical,
19812 	 * the  driver starts at 0 each time.
19813 	 */
19814 	spin_lock_irqsave(&phba->hbalock, iflag);
19815 	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19816 	rpi_limit = phba->sli4_hba.next_rpi;
19817 
19818 	rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);
19819 	if (rpi >= rpi_limit)
19820 		rpi = LPFC_RPI_ALLOC_ERROR;
19821 	else {
19822 		set_bit(rpi, phba->sli4_hba.rpi_bmask);
19823 		phba->sli4_hba.max_cfg_param.rpi_used++;
19824 		phba->sli4_hba.rpi_count++;
19825 	}
19826 	lpfc_printf_log(phba, KERN_INFO,
19827 			LOG_NODE | LOG_DISCOVERY,
19828 			"0001 Allocated rpi:x%x max:x%x lim:x%x\n",
19829 			(int) rpi, max_rpi, rpi_limit);
19830 
19831 	/*
19832 	 * Don't try to allocate more rpi header regions if the device limit
19833 	 * has been exhausted.
19834 	 */
19835 	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19836 	    (phba->sli4_hba.rpi_count >= max_rpi)) {
19837 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19838 		return rpi;
19839 	}
19840 
19841 	/*
19842 	 * RPI header postings are not required for SLI4 ports capable of
19843 	 * extents.
19844 	 */
19845 	if (!phba->sli4_hba.rpi_hdrs_in_use) {
19846 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19847 		return rpi;
19848 	}
19849 
19850 	/*
19851 	 * If the driver is running low on rpi resources, allocate another
19852 	 * page now.  Note that the next_rpi value is used because
19853 	 * it represents how many are actually in use whereas max_rpi notes
19854 	 * how many are supported max by the device.
19855 	 */
19856 	rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
19857 	spin_unlock_irqrestore(&phba->hbalock, iflag);
19858 	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19859 		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19860 		if (!rpi_hdr) {
19861 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19862 					"2002 Error Could not grow rpi "
19863 					"count\n");
19864 		} else {
19865 			lrpi = rpi_hdr->start_rpi;
19866 			rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19867 			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19868 		}
19869 	}
19870 
19871 	return rpi;
19872 }
19873 
19874 /**
19875  * __lpfc_sli4_free_rpi - Release an rpi for reuse.
19876  * @phba: pointer to lpfc hba data structure.
19877  * @rpi: rpi to free
19878  *
19879  * This routine is invoked to release an rpi to the pool of
19880  * available rpis maintained by the driver.
19881  **/
19882 static void
__lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19883 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19884 {
19885 	/*
19886 	 * if the rpi value indicates a prior unreg has already
19887 	 * been done, skip the unreg.
19888 	 */
19889 	if (rpi == LPFC_RPI_ALLOC_ERROR)
19890 		return;
19891 
19892 	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19893 		phba->sli4_hba.rpi_count--;
19894 		phba->sli4_hba.max_cfg_param.rpi_used--;
19895 	} else {
19896 		lpfc_printf_log(phba, KERN_INFO,
19897 				LOG_NODE | LOG_DISCOVERY,
19898 				"2016 rpi %x not inuse\n",
19899 				rpi);
19900 	}
19901 }
19902 
19903 /**
19904  * lpfc_sli4_free_rpi - Release an rpi for reuse.
19905  * @phba: pointer to lpfc hba data structure.
19906  * @rpi: rpi to free
19907  *
19908  * This routine is invoked to release an rpi to the pool of
19909  * available rpis maintained by the driver.
19910  **/
19911 void
lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19912 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19913 {
19914 	spin_lock_irq(&phba->hbalock);
19915 	__lpfc_sli4_free_rpi(phba, rpi);
19916 	spin_unlock_irq(&phba->hbalock);
19917 }
19918 
19919 /**
19920  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19921  * @phba: pointer to lpfc hba data structure.
19922  *
19923  * This routine is invoked to remove the memory region that
19924  * provided rpi via a bitmask.
19925  **/
19926 void
lpfc_sli4_remove_rpis(struct lpfc_hba * phba)19927 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19928 {
19929 	kfree(phba->sli4_hba.rpi_bmask);
19930 	kfree(phba->sli4_hba.rpi_ids);
19931 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
19932 }
19933 
19934 /**
19935  * lpfc_sli4_resume_rpi - Resume traffic relative to an RPI
19936  * @ndlp: pointer to lpfc nodelist data structure.
19937  * @cmpl: completion call-back.
19938  * @iocbq: data to load as mbox ctx_u information
19939  *
19940  * Return codes
19941  *	0 - successful
19942  *	-ENOMEM - No available memory
19943  *	-EIO - The mailbox failed to complete successfully.
19944  **/
19945 int
lpfc_sli4_resume_rpi(struct lpfc_nodelist * ndlp,void (* cmpl)(struct lpfc_hba *,LPFC_MBOXQ_t *),struct lpfc_iocbq * iocbq)19946 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19947 		     void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *),
19948 		     struct lpfc_iocbq *iocbq)
19949 {
19950 	LPFC_MBOXQ_t *mboxq;
19951 	struct lpfc_hba *phba = ndlp->phba;
19952 	int rc;
19953 
19954 	/* The port is notified of the header region via a mailbox command. */
19955 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19956 	if (!mboxq)
19957 		return -ENOMEM;
19958 
19959 	/* If cmpl assigned, then this nlp_get pairs with
19960 	 * lpfc_mbx_cmpl_resume_rpi.
19961 	 *
19962 	 * Else cmpl is NULL, then this nlp_get pairs with
19963 	 * lpfc_sli_def_mbox_cmpl.
19964 	 */
19965 	if (!lpfc_nlp_get(ndlp)) {
19966 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19967 				"2122 %s: Failed to get nlp ref\n",
19968 				__func__);
19969 		mempool_free(mboxq, phba->mbox_mem_pool);
19970 		return -EIO;
19971 	}
19972 
19973 	lpfc_resume_rpi(mboxq, ndlp);
19974 	if (cmpl) {
19975 		mboxq->mbox_cmpl = cmpl;
19976 		mboxq->ctx_u.save_iocb = iocbq;
19977 	} else
19978 		mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19979 	mboxq->ctx_ndlp = ndlp;
19980 	mboxq->vport = ndlp->vport;
19981 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19982 	if (rc == MBX_NOT_FINISHED) {
19983 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19984 				"2010 Resume RPI Mailbox failed "
19985 				"status %d, mbxStatus x%x\n", rc,
19986 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19987 		lpfc_nlp_put(ndlp);
19988 		mempool_free(mboxq, phba->mbox_mem_pool);
19989 		return -EIO;
19990 	}
19991 	return 0;
19992 }
19993 
19994 /**
19995  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19996  * @vport: Pointer to the vport for which the vpi is being initialized
19997  *
19998  * This routine is invoked to activate a vpi with the port.
19999  *
20000  * Returns:
20001  *    0 success
20002  *    -Evalue otherwise
20003  **/
20004 int
lpfc_sli4_init_vpi(struct lpfc_vport * vport)20005 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
20006 {
20007 	LPFC_MBOXQ_t *mboxq;
20008 	int rc = 0;
20009 	int retval = MBX_SUCCESS;
20010 	uint32_t mbox_tmo;
20011 	struct lpfc_hba *phba = vport->phba;
20012 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20013 	if (!mboxq)
20014 		return -ENOMEM;
20015 	lpfc_init_vpi(phba, mboxq, vport->vpi);
20016 	mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
20017 	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
20018 	if (rc != MBX_SUCCESS) {
20019 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
20020 				"2022 INIT VPI Mailbox failed "
20021 				"status %d, mbxStatus x%x\n", rc,
20022 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
20023 		retval = -EIO;
20024 	}
20025 	if (rc != MBX_TIMEOUT)
20026 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
20027 
20028 	return retval;
20029 }
20030 
20031 /**
20032  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
20033  * @phba: pointer to lpfc hba data structure.
20034  * @mboxq: Pointer to mailbox object.
20035  *
20036  * This routine is invoked to manually add a single FCF record. The caller
20037  * must pass a completely initialized FCF_Record.  This routine takes
20038  * care of the nonembedded mailbox operations.
20039  **/
20040 static void
lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)20041 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
20042 {
20043 	void *virt_addr;
20044 	union lpfc_sli4_cfg_shdr *shdr;
20045 	uint32_t shdr_status, shdr_add_status;
20046 
20047 	virt_addr = mboxq->sge_array->addr[0];
20048 	/* The IOCTL status is embedded in the mailbox subheader. */
20049 	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
20050 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
20051 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
20052 
20053 	if ((shdr_status || shdr_add_status) &&
20054 		(shdr_status != STATUS_FCF_IN_USE))
20055 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20056 			"2558 ADD_FCF_RECORD mailbox failed with "
20057 			"status x%x add_status x%x\n",
20058 			shdr_status, shdr_add_status);
20059 
20060 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
20061 }
20062 
20063 /**
20064  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
20065  * @phba: pointer to lpfc hba data structure.
20066  * @fcf_record:  pointer to the initialized fcf record to add.
20067  *
20068  * This routine is invoked to manually add a single FCF record. The caller
20069  * must pass a completely initialized FCF_Record.  This routine takes
20070  * care of the nonembedded mailbox operations.
20071  **/
20072 int
lpfc_sli4_add_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record)20073 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
20074 {
20075 	int rc = 0;
20076 	LPFC_MBOXQ_t *mboxq;
20077 	uint8_t *bytep;
20078 	void *virt_addr;
20079 	struct lpfc_mbx_sge sge;
20080 	uint32_t alloc_len, req_len;
20081 	uint32_t fcfindex;
20082 
20083 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20084 	if (!mboxq) {
20085 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20086 			"2009 Failed to allocate mbox for ADD_FCF cmd\n");
20087 		return -ENOMEM;
20088 	}
20089 
20090 	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
20091 		  sizeof(uint32_t);
20092 
20093 	/* Allocate DMA memory and set up the non-embedded mailbox command */
20094 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
20095 				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
20096 				     req_len, LPFC_SLI4_MBX_NEMBED);
20097 	if (alloc_len < req_len) {
20098 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20099 			"2523 Allocated DMA memory size (x%x) is "
20100 			"less than the requested DMA memory "
20101 			"size (x%x)\n", alloc_len, req_len);
20102 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20103 		return -ENOMEM;
20104 	}
20105 
20106 	/*
20107 	 * Get the first SGE entry from the non-embedded DMA memory.  This
20108 	 * routine only uses a single SGE.
20109 	 */
20110 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
20111 	virt_addr = mboxq->sge_array->addr[0];
20112 	/*
20113 	 * Configure the FCF record for FCFI 0.  This is the driver's
20114 	 * hardcoded default and gets used in nonFIP mode.
20115 	 */
20116 	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
20117 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
20118 	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
20119 
20120 	/*
20121 	 * Copy the fcf_index and the FCF Record Data. The data starts after
20122 	 * the FCoE header plus word10. The data copy needs to be endian
20123 	 * correct.
20124 	 */
20125 	bytep += sizeof(uint32_t);
20126 	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
20127 	mboxq->vport = phba->pport;
20128 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
20129 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20130 	if (rc == MBX_NOT_FINISHED) {
20131 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20132 			"2515 ADD_FCF_RECORD mailbox failed with "
20133 			"status 0x%x\n", rc);
20134 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20135 		rc = -EIO;
20136 	} else
20137 		rc = 0;
20138 
20139 	return rc;
20140 }
20141 
20142 /**
20143  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
20144  * @phba: pointer to lpfc hba data structure.
20145  * @fcf_record:  pointer to the fcf record to write the default data.
20146  * @fcf_index: FCF table entry index.
20147  *
20148  * This routine is invoked to build the driver's default FCF record.  The
20149  * values used are hardcoded.  This routine handles memory initialization.
20150  *
20151  **/
20152 void
lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record,uint16_t fcf_index)20153 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
20154 				struct fcf_record *fcf_record,
20155 				uint16_t fcf_index)
20156 {
20157 	memset(fcf_record, 0, sizeof(struct fcf_record));
20158 	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
20159 	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
20160 	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
20161 	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
20162 	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
20163 	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
20164 	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
20165 	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
20166 	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
20167 	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
20168 	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
20169 	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
20170 	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
20171 	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
20172 	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
20173 	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
20174 		LPFC_FCF_FPMA | LPFC_FCF_SPMA);
20175 	/* Set the VLAN bit map */
20176 	if (phba->valid_vlan) {
20177 		fcf_record->vlan_bitmap[phba->vlan_id / 8]
20178 			= 1 << (phba->vlan_id % 8);
20179 	}
20180 }
20181 
20182 /**
20183  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
20184  * @phba: pointer to lpfc hba data structure.
20185  * @fcf_index: FCF table entry offset.
20186  *
20187  * This routine is invoked to scan the entire FCF table by reading FCF
20188  * record and processing it one at a time starting from the @fcf_index
20189  * for initial FCF discovery or fast FCF failover rediscovery.
20190  *
20191  * Return 0 if the mailbox command is submitted successfully, none 0
20192  * otherwise.
20193  **/
20194 int
lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20195 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20196 {
20197 	int rc = 0, error;
20198 	LPFC_MBOXQ_t *mboxq;
20199 
20200 	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
20201 	phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
20202 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20203 	if (!mboxq) {
20204 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20205 				"2000 Failed to allocate mbox for "
20206 				"READ_FCF cmd\n");
20207 		error = -ENOMEM;
20208 		goto fail_fcf_scan;
20209 	}
20210 	/* Construct the read FCF record mailbox command */
20211 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20212 	if (rc) {
20213 		error = -EINVAL;
20214 		goto fail_fcf_scan;
20215 	}
20216 	/* Issue the mailbox command asynchronously */
20217 	mboxq->vport = phba->pport;
20218 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
20219 
20220 	set_bit(FCF_TS_INPROG, &phba->hba_flag);
20221 
20222 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20223 	if (rc == MBX_NOT_FINISHED)
20224 		error = -EIO;
20225 	else {
20226 		/* Reset eligible FCF count for new scan */
20227 		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
20228 			phba->fcf.eligible_fcf_cnt = 0;
20229 		error = 0;
20230 	}
20231 fail_fcf_scan:
20232 	if (error) {
20233 		if (mboxq)
20234 			lpfc_sli4_mbox_cmd_free(phba, mboxq);
20235 		/* FCF scan failed, clear FCF_TS_INPROG flag */
20236 		clear_bit(FCF_TS_INPROG, &phba->hba_flag);
20237 	}
20238 	return error;
20239 }
20240 
20241 /**
20242  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
20243  * @phba: pointer to lpfc hba data structure.
20244  * @fcf_index: FCF table entry offset.
20245  *
20246  * This routine is invoked to read an FCF record indicated by @fcf_index
20247  * and to use it for FLOGI roundrobin FCF failover.
20248  *
20249  * Return 0 if the mailbox command is submitted successfully, none 0
20250  * otherwise.
20251  **/
20252 int
lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20253 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20254 {
20255 	int rc = 0, error;
20256 	LPFC_MBOXQ_t *mboxq;
20257 
20258 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20259 	if (!mboxq) {
20260 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20261 				"2763 Failed to allocate mbox for "
20262 				"READ_FCF cmd\n");
20263 		error = -ENOMEM;
20264 		goto fail_fcf_read;
20265 	}
20266 	/* Construct the read FCF record mailbox command */
20267 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20268 	if (rc) {
20269 		error = -EINVAL;
20270 		goto fail_fcf_read;
20271 	}
20272 	/* Issue the mailbox command asynchronously */
20273 	mboxq->vport = phba->pport;
20274 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20275 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20276 	if (rc == MBX_NOT_FINISHED)
20277 		error = -EIO;
20278 	else
20279 		error = 0;
20280 
20281 fail_fcf_read:
20282 	if (error && mboxq)
20283 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20284 	return error;
20285 }
20286 
20287 /**
20288  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20289  * @phba: pointer to lpfc hba data structure.
20290  * @fcf_index: FCF table entry offset.
20291  *
20292  * This routine is invoked to read an FCF record indicated by @fcf_index to
20293  * determine whether it's eligible for FLOGI roundrobin failover list.
20294  *
20295  * Return 0 if the mailbox command is submitted successfully, none 0
20296  * otherwise.
20297  **/
20298 int
lpfc_sli4_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20299 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20300 {
20301 	int rc = 0, error;
20302 	LPFC_MBOXQ_t *mboxq;
20303 
20304 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20305 	if (!mboxq) {
20306 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20307 				"2758 Failed to allocate mbox for "
20308 				"READ_FCF cmd\n");
20309 				error = -ENOMEM;
20310 				goto fail_fcf_read;
20311 	}
20312 	/* Construct the read FCF record mailbox command */
20313 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20314 	if (rc) {
20315 		error = -EINVAL;
20316 		goto fail_fcf_read;
20317 	}
20318 	/* Issue the mailbox command asynchronously */
20319 	mboxq->vport = phba->pport;
20320 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20321 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20322 	if (rc == MBX_NOT_FINISHED)
20323 		error = -EIO;
20324 	else
20325 		error = 0;
20326 
20327 fail_fcf_read:
20328 	if (error && mboxq)
20329 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20330 	return error;
20331 }
20332 
20333 /**
20334  * lpfc_check_next_fcf_pri_level
20335  * @phba: pointer to the lpfc_hba struct for this port.
20336  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20337  * routine when the rr_bmask is empty. The FCF indecies are put into the
20338  * rr_bmask based on their priority level. Starting from the highest priority
20339  * to the lowest. The most likely FCF candidate will be in the highest
20340  * priority group. When this routine is called it searches the fcf_pri list for
20341  * next lowest priority group and repopulates the rr_bmask with only those
20342  * fcf_indexes.
20343  * returns:
20344  * 1=success 0=failure
20345  **/
20346 static int
lpfc_check_next_fcf_pri_level(struct lpfc_hba * phba)20347 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20348 {
20349 	uint16_t next_fcf_pri;
20350 	uint16_t last_index;
20351 	struct lpfc_fcf_pri *fcf_pri;
20352 	int rc;
20353 	int ret = 0;
20354 
20355 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20356 			LPFC_SLI4_FCF_TBL_INDX_MAX);
20357 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20358 			"3060 Last IDX %d\n", last_index);
20359 
20360 	/* Verify the priority list has 2 or more entries */
20361 	spin_lock_irq(&phba->hbalock);
20362 	if (list_empty(&phba->fcf.fcf_pri_list) ||
20363 	    list_is_singular(&phba->fcf.fcf_pri_list)) {
20364 		spin_unlock_irq(&phba->hbalock);
20365 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20366 			"3061 Last IDX %d\n", last_index);
20367 		return 0; /* Empty rr list */
20368 	}
20369 	spin_unlock_irq(&phba->hbalock);
20370 
20371 	next_fcf_pri = 0;
20372 	/*
20373 	 * Clear the rr_bmask and set all of the bits that are at this
20374 	 * priority.
20375 	 */
20376 	memset(phba->fcf.fcf_rr_bmask, 0,
20377 			sizeof(*phba->fcf.fcf_rr_bmask));
20378 	spin_lock_irq(&phba->hbalock);
20379 	list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20380 		if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20381 			continue;
20382 		/*
20383 		 * the 1st priority that has not FLOGI failed
20384 		 * will be the highest.
20385 		 */
20386 		if (!next_fcf_pri)
20387 			next_fcf_pri = fcf_pri->fcf_rec.priority;
20388 		spin_unlock_irq(&phba->hbalock);
20389 		if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20390 			rc = lpfc_sli4_fcf_rr_index_set(phba,
20391 						fcf_pri->fcf_rec.fcf_index);
20392 			if (rc)
20393 				return 0;
20394 		}
20395 		spin_lock_irq(&phba->hbalock);
20396 	}
20397 	/*
20398 	 * if next_fcf_pri was not set above and the list is not empty then
20399 	 * we have failed flogis on all of them. So reset flogi failed
20400 	 * and start at the beginning.
20401 	 */
20402 	if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20403 		list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20404 			fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20405 			/*
20406 			 * the 1st priority that has not FLOGI failed
20407 			 * will be the highest.
20408 			 */
20409 			if (!next_fcf_pri)
20410 				next_fcf_pri = fcf_pri->fcf_rec.priority;
20411 			spin_unlock_irq(&phba->hbalock);
20412 			if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20413 				rc = lpfc_sli4_fcf_rr_index_set(phba,
20414 						fcf_pri->fcf_rec.fcf_index);
20415 				if (rc)
20416 					return 0;
20417 			}
20418 			spin_lock_irq(&phba->hbalock);
20419 		}
20420 	} else
20421 		ret = 1;
20422 	spin_unlock_irq(&phba->hbalock);
20423 
20424 	return ret;
20425 }
20426 /**
20427  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20428  * @phba: pointer to lpfc hba data structure.
20429  *
20430  * This routine is to get the next eligible FCF record index in a round
20431  * robin fashion. If the next eligible FCF record index equals to the
20432  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
20433  * shall be returned, otherwise, the next eligible FCF record's index
20434  * shall be returned.
20435  **/
20436 uint16_t
lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba * phba)20437 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20438 {
20439 	uint16_t next;
20440 
20441 	do {
20442 		for_each_set_bit_wrap(next, phba->fcf.fcf_rr_bmask,
20443 				LPFC_SLI4_FCF_TBL_INDX_MAX, phba->fcf.current_rec.fcf_indx) {
20444 			if (next == phba->fcf.current_rec.fcf_indx)
20445 				continue;
20446 
20447 			if (!(phba->fcf.fcf_pri[next].fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)) {
20448 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20449 					"2845 Get next roundrobin failover FCF (x%x)\n", next);
20450 				return next;
20451 			}
20452 
20453 			if (list_is_singular(&phba->fcf.fcf_pri_list))
20454 				return LPFC_FCOE_FCF_NEXT_NONE;
20455 		}
20456 
20457 		/*
20458 		 * If next fcf index is not found check if there are lower
20459 		 * Priority level fcf's in the fcf_priority list.
20460 		 * Set up the rr_bmask with all of the available fcf bits
20461 		 * at that level and continue the selection process.
20462 		 */
20463 	} while (lpfc_check_next_fcf_pri_level(phba));
20464 
20465 	lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20466 			"2844 No roundrobin failover FCF available\n");
20467 
20468 	return LPFC_FCOE_FCF_NEXT_NONE;
20469 }
20470 
20471 /**
20472  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20473  * @phba: pointer to lpfc hba data structure.
20474  * @fcf_index: index into the FCF table to 'set'
20475  *
20476  * This routine sets the FCF record index in to the eligible bmask for
20477  * roundrobin failover search. It checks to make sure that the index
20478  * does not go beyond the range of the driver allocated bmask dimension
20479  * before setting the bit.
20480  *
20481  * Returns 0 if the index bit successfully set, otherwise, it returns
20482  * -EINVAL.
20483  **/
20484 int
lpfc_sli4_fcf_rr_index_set(struct lpfc_hba * phba,uint16_t fcf_index)20485 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20486 {
20487 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20488 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20489 				"2610 FCF (x%x) reached driver's book "
20490 				"keeping dimension:x%x\n",
20491 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20492 		return -EINVAL;
20493 	}
20494 	/* Set the eligible FCF record index bmask */
20495 	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20496 
20497 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20498 			"2790 Set FCF (x%x) to roundrobin FCF failover "
20499 			"bmask\n", fcf_index);
20500 
20501 	return 0;
20502 }
20503 
20504 /**
20505  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
20506  * @phba: pointer to lpfc hba data structure.
20507  * @fcf_index: index into the FCF table to 'clear'
20508  *
20509  * This routine clears the FCF record index from the eligible bmask for
20510  * roundrobin failover search. It checks to make sure that the index
20511  * does not go beyond the range of the driver allocated bmask dimension
20512  * before clearing the bit.
20513  **/
20514 void
lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba * phba,uint16_t fcf_index)20515 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20516 {
20517 	struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
20518 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20519 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20520 				"2762 FCF (x%x) reached driver's book "
20521 				"keeping dimension:x%x\n",
20522 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20523 		return;
20524 	}
20525 	/* Clear the eligible FCF record index bmask */
20526 	spin_lock_irq(&phba->hbalock);
20527 	list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20528 				 list) {
20529 		if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20530 			list_del_init(&fcf_pri->list);
20531 			break;
20532 		}
20533 	}
20534 	spin_unlock_irq(&phba->hbalock);
20535 	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20536 
20537 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20538 			"2791 Clear FCF (x%x) from roundrobin failover "
20539 			"bmask\n", fcf_index);
20540 }
20541 
20542 /**
20543  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20544  * @phba: pointer to lpfc hba data structure.
20545  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
20546  *
20547  * This routine is the completion routine for the rediscover FCF table mailbox
20548  * command. If the mailbox command returned failure, it will try to stop the
20549  * FCF rediscover wait timer.
20550  **/
20551 static void
lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)20552 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20553 {
20554 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20555 	uint32_t shdr_status, shdr_add_status;
20556 
20557 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20558 
20559 	shdr_status = bf_get(lpfc_mbox_hdr_status,
20560 			     &redisc_fcf->header.cfg_shdr.response);
20561 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20562 			     &redisc_fcf->header.cfg_shdr.response);
20563 	if (shdr_status || shdr_add_status) {
20564 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20565 				"2746 Requesting for FCF rediscovery failed "
20566 				"status x%x add_status x%x\n",
20567 				shdr_status, shdr_add_status);
20568 		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
20569 			spin_lock_irq(&phba->hbalock);
20570 			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
20571 			spin_unlock_irq(&phba->hbalock);
20572 			/*
20573 			 * CVL event triggered FCF rediscover request failed,
20574 			 * last resort to re-try current registered FCF entry.
20575 			 */
20576 			lpfc_retry_pport_discovery(phba);
20577 		} else {
20578 			spin_lock_irq(&phba->hbalock);
20579 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
20580 			spin_unlock_irq(&phba->hbalock);
20581 			/*
20582 			 * DEAD FCF event triggered FCF rediscover request
20583 			 * failed, last resort to fail over as a link down
20584 			 * to FCF registration.
20585 			 */
20586 			lpfc_sli4_fcf_dead_failthrough(phba);
20587 		}
20588 	} else {
20589 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20590 				"2775 Start FCF rediscover quiescent timer\n");
20591 		/*
20592 		 * Start FCF rediscovery wait timer for pending FCF
20593 		 * before rescan FCF record table.
20594 		 */
20595 		lpfc_fcf_redisc_wait_start_timer(phba);
20596 	}
20597 
20598 	mempool_free(mbox, phba->mbox_mem_pool);
20599 }
20600 
20601 /**
20602  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
20603  * @phba: pointer to lpfc hba data structure.
20604  *
20605  * This routine is invoked to request for rediscovery of the entire FCF table
20606  * by the port.
20607  **/
20608 int
lpfc_sli4_redisc_fcf_table(struct lpfc_hba * phba)20609 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20610 {
20611 	LPFC_MBOXQ_t *mbox;
20612 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20613 	int rc, length;
20614 
20615 	/* Cancel retry delay timers to all vports before FCF rediscover */
20616 	lpfc_cancel_all_vport_retry_delay_timer(phba);
20617 
20618 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20619 	if (!mbox) {
20620 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20621 				"2745 Failed to allocate mbox for "
20622 				"requesting FCF rediscover.\n");
20623 		return -ENOMEM;
20624 	}
20625 
20626 	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20627 		  sizeof(struct lpfc_sli4_cfg_mhdr));
20628 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20629 			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20630 			 length, LPFC_SLI4_MBX_EMBED);
20631 
20632 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20633 	/* Set count to 0 for invalidating the entire FCF database */
20634 	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20635 
20636 	/* Issue the mailbox command asynchronously */
20637 	mbox->vport = phba->pport;
20638 	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20639 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20640 
20641 	if (rc == MBX_NOT_FINISHED) {
20642 		mempool_free(mbox, phba->mbox_mem_pool);
20643 		return -EIO;
20644 	}
20645 	return 0;
20646 }
20647 
20648 /**
20649  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20650  * @phba: pointer to lpfc hba data structure.
20651  *
20652  * This function is the failover routine as a last resort to the FCF DEAD
20653  * event when driver failed to perform fast FCF failover.
20654  **/
20655 void
lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba * phba)20656 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20657 {
20658 	uint32_t link_state;
20659 
20660 	/*
20661 	 * Last resort as FCF DEAD event failover will treat this as
20662 	 * a link down, but save the link state because we don't want
20663 	 * it to be changed to Link Down unless it is already down.
20664 	 */
20665 	link_state = phba->link_state;
20666 	lpfc_linkdown(phba);
20667 	phba->link_state = link_state;
20668 
20669 	/* Unregister FCF if no devices connected to it */
20670 	lpfc_unregister_unused_fcf(phba);
20671 }
20672 
20673 /**
20674  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
20675  * @phba: pointer to lpfc hba data structure.
20676  * @rgn23_data: pointer to configure region 23 data.
20677  *
20678  * This function gets SLI3 port configure region 23 data through memory dump
20679  * mailbox command. When it successfully retrieves data, the size of the data
20680  * will be returned, otherwise, 0 will be returned.
20681  **/
20682 static uint32_t
lpfc_sli_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20683 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20684 {
20685 	LPFC_MBOXQ_t *pmb = NULL;
20686 	MAILBOX_t *mb;
20687 	uint32_t offset = 0;
20688 	int rc;
20689 
20690 	if (!rgn23_data)
20691 		return 0;
20692 
20693 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20694 	if (!pmb) {
20695 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20696 				"2600 failed to allocate mailbox memory\n");
20697 		return 0;
20698 	}
20699 	mb = &pmb->u.mb;
20700 
20701 	do {
20702 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20703 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20704 
20705 		if (rc != MBX_SUCCESS) {
20706 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20707 					"2601 failed to read config "
20708 					"region 23, rc 0x%x Status 0x%x\n",
20709 					rc, mb->mbxStatus);
20710 			mb->un.varDmp.word_cnt = 0;
20711 		}
20712 		/*
20713 		 * dump mem may return a zero when finished or we got a
20714 		 * mailbox error, either way we are done.
20715 		 */
20716 		if (mb->un.varDmp.word_cnt == 0)
20717 			break;
20718 
20719 		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20720 			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20721 
20722 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
20723 				       rgn23_data + offset,
20724 				       mb->un.varDmp.word_cnt);
20725 		offset += mb->un.varDmp.word_cnt;
20726 	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
20727 
20728 	mempool_free(pmb, phba->mbox_mem_pool);
20729 	return offset;
20730 }
20731 
20732 /**
20733  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20734  * @phba: pointer to lpfc hba data structure.
20735  * @rgn23_data: pointer to configure region 23 data.
20736  *
20737  * This function gets SLI4 port configure region 23 data through memory dump
20738  * mailbox command. When it successfully retrieves data, the size of the data
20739  * will be returned, otherwise, 0 will be returned.
20740  **/
20741 static uint32_t
lpfc_sli4_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20742 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20743 {
20744 	LPFC_MBOXQ_t *mboxq = NULL;
20745 	struct lpfc_dmabuf *mp = NULL;
20746 	struct lpfc_mqe *mqe;
20747 	uint32_t data_length = 0;
20748 	int rc;
20749 
20750 	if (!rgn23_data)
20751 		return 0;
20752 
20753 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20754 	if (!mboxq) {
20755 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20756 				"3105 failed to allocate mailbox memory\n");
20757 		return 0;
20758 	}
20759 
20760 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20761 		goto out;
20762 	mqe = &mboxq->u.mqe;
20763 	mp = mboxq->ctx_buf;
20764 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20765 	if (rc)
20766 		goto out;
20767 	data_length = mqe->un.mb_words[5];
20768 	if (data_length == 0)
20769 		goto out;
20770 	if (data_length > DMP_RGN23_SIZE) {
20771 		data_length = 0;
20772 		goto out;
20773 	}
20774 	lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20775 out:
20776 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
20777 	return data_length;
20778 }
20779 
20780 /**
20781  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20782  * @phba: pointer to lpfc hba data structure.
20783  *
20784  * This function read region 23 and parse TLV for port status to
20785  * decide if the user disaled the port. If the TLV indicates the
20786  * port is disabled, the hba_flag is set accordingly.
20787  **/
20788 void
lpfc_sli_read_link_ste(struct lpfc_hba * phba)20789 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20790 {
20791 	uint8_t *rgn23_data = NULL;
20792 	uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20793 	uint32_t offset = 0;
20794 
20795 	/* Get adapter Region 23 data */
20796 	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20797 	if (!rgn23_data)
20798 		goto out;
20799 
20800 	if (phba->sli_rev < LPFC_SLI_REV4)
20801 		data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20802 	else {
20803 		if_type = bf_get(lpfc_sli_intf_if_type,
20804 				 &phba->sli4_hba.sli_intf);
20805 		if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20806 			goto out;
20807 		data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20808 	}
20809 
20810 	if (!data_size)
20811 		goto out;
20812 
20813 	/* Check the region signature first */
20814 	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
20815 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20816 			"2619 Config region 23 has bad signature\n");
20817 			goto out;
20818 	}
20819 	offset += 4;
20820 
20821 	/* Check the data structure version */
20822 	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
20823 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20824 			"2620 Config region 23 has bad version\n");
20825 		goto out;
20826 	}
20827 	offset += 4;
20828 
20829 	/* Parse TLV entries in the region */
20830 	while (offset < data_size) {
20831 		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20832 			break;
20833 		/*
20834 		 * If the TLV is not driver specific TLV or driver id is
20835 		 * not linux driver id, skip the record.
20836 		 */
20837 		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20838 		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20839 		    (rgn23_data[offset + 3] != 0)) {
20840 			offset += rgn23_data[offset + 1] * 4 + 4;
20841 			continue;
20842 		}
20843 
20844 		/* Driver found a driver specific TLV in the config region */
20845 		sub_tlv_len = rgn23_data[offset + 1] * 4;
20846 		offset += 4;
20847 		tlv_offset = 0;
20848 
20849 		/*
20850 		 * Search for configured port state sub-TLV.
20851 		 */
20852 		while ((offset < data_size) &&
20853 			(tlv_offset < sub_tlv_len)) {
20854 			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20855 				offset += 4;
20856 				tlv_offset += 4;
20857 				break;
20858 			}
20859 			if (rgn23_data[offset] != PORT_STE_TYPE) {
20860 				offset += rgn23_data[offset + 1] * 4 + 4;
20861 				tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20862 				continue;
20863 			}
20864 
20865 			/* This HBA contains PORT_STE configured */
20866 			if (!rgn23_data[offset + 2])
20867 				set_bit(LINK_DISABLED, &phba->hba_flag);
20868 
20869 			goto out;
20870 		}
20871 	}
20872 
20873 out:
20874 	kfree(rgn23_data);
20875 	return;
20876 }
20877 
20878 /**
20879  * lpfc_log_fw_write_cmpl - logs firmware write completion status
20880  * @phba: pointer to lpfc hba data structure
20881  * @shdr_status: wr_object rsp's status field
20882  * @shdr_add_status: wr_object rsp's add_status field
20883  * @shdr_add_status_2: wr_object rsp's add_status_2 field
20884  * @shdr_change_status: wr_object rsp's change_status field
20885  * @shdr_csf: wr_object rsp's csf bit
20886  *
20887  * This routine is intended to be called after a firmware write completes.
20888  * It will log next action items to be performed by the user to instantiate
20889  * the newly downloaded firmware or reason for incompatibility.
20890  **/
20891 static void
lpfc_log_fw_write_cmpl(struct lpfc_hba * phba,u32 shdr_status,u32 shdr_add_status,u32 shdr_add_status_2,u32 shdr_change_status,u32 shdr_csf)20892 lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20893 		       u32 shdr_add_status, u32 shdr_add_status_2,
20894 		       u32 shdr_change_status, u32 shdr_csf)
20895 {
20896 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20897 			"4198 %s: flash_id x%02x, asic_rev x%02x, "
20898 			"status x%02x, add_status x%02x, add_status_2 x%02x, "
20899 			"change_status x%02x, csf %01x\n", __func__,
20900 			phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20901 			shdr_status, shdr_add_status, shdr_add_status_2,
20902 			shdr_change_status, shdr_csf);
20903 
20904 	if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20905 		switch (shdr_add_status_2) {
20906 		case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20907 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20908 				     "4199 Firmware write failed: "
20909 				     "image incompatible with flash x%02x\n",
20910 				     phba->sli4_hba.flash_id);
20911 			break;
20912 		case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20913 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20914 				     "4200 Firmware write failed: "
20915 				     "image incompatible with ASIC "
20916 				     "architecture x%02x\n",
20917 				     phba->sli4_hba.asic_rev);
20918 			break;
20919 		default:
20920 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20921 				     "4210 Firmware write failed: "
20922 				     "add_status_2 x%02x\n",
20923 				     shdr_add_status_2);
20924 			break;
20925 		}
20926 	} else if (!shdr_status && !shdr_add_status) {
20927 		if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20928 		    shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20929 			if (shdr_csf)
20930 				shdr_change_status =
20931 						   LPFC_CHANGE_STATUS_PCI_RESET;
20932 		}
20933 
20934 		switch (shdr_change_status) {
20935 		case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20936 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20937 				     "3198 Firmware write complete: System "
20938 				     "reboot required to instantiate\n");
20939 			break;
20940 		case (LPFC_CHANGE_STATUS_FW_RESET):
20941 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20942 				     "3199 Firmware write complete: "
20943 				     "Firmware reset required to "
20944 				     "instantiate\n");
20945 			break;
20946 		case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20947 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20948 				     "3200 Firmware write complete: Port "
20949 				     "Migration or PCI Reset required to "
20950 				     "instantiate\n");
20951 			break;
20952 		case (LPFC_CHANGE_STATUS_PCI_RESET):
20953 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20954 				     "3201 Firmware write complete: PCI "
20955 				     "Reset required to instantiate\n");
20956 			break;
20957 		default:
20958 			break;
20959 		}
20960 	}
20961 }
20962 
20963 /**
20964  * lpfc_wr_object - write an object to the firmware
20965  * @phba: HBA structure that indicates port to create a queue on.
20966  * @dmabuf_list: list of dmabufs to write to the port.
20967  * @size: the total byte value of the objects to write to the port.
20968  * @offset: the current offset to be used to start the transfer.
20969  *
20970  * This routine will create a wr_object mailbox command to send to the port.
20971  * the mailbox command will be constructed using the dma buffers described in
20972  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20973  * BDEs that the imbedded mailbox can support. The @offset variable will be
20974  * used to indicate the starting offset of the transfer and will also return
20975  * the offset after the write object mailbox has completed. @size is used to
20976  * determine the end of the object and whether the eof bit should be set.
20977  *
20978  * Return 0 is successful and offset will contain the new offset to use
20979  * for the next write.
20980  * Return negative value for error cases.
20981  **/
20982 int
lpfc_wr_object(struct lpfc_hba * phba,struct list_head * dmabuf_list,uint32_t size,uint32_t * offset)20983 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20984 	       uint32_t size, uint32_t *offset)
20985 {
20986 	struct lpfc_mbx_wr_object *wr_object;
20987 	LPFC_MBOXQ_t *mbox;
20988 	int rc = 0, i = 0;
20989 	int mbox_status = 0;
20990 	uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20991 	uint32_t shdr_change_status = 0, shdr_csf = 0;
20992 	uint32_t mbox_tmo;
20993 	struct lpfc_dmabuf *dmabuf;
20994 	uint32_t written = 0;
20995 	bool check_change_status = false;
20996 
20997 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20998 	if (!mbox)
20999 		return -ENOMEM;
21000 
21001 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
21002 			LPFC_MBOX_OPCODE_WRITE_OBJECT,
21003 			sizeof(struct lpfc_mbx_wr_object) -
21004 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
21005 
21006 	wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
21007 	wr_object->u.request.write_offset = *offset;
21008 	sprintf((uint8_t *)wr_object->u.request.object_name, "/");
21009 	wr_object->u.request.object_name[0] =
21010 		cpu_to_le32(wr_object->u.request.object_name[0]);
21011 	bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
21012 	list_for_each_entry(dmabuf, dmabuf_list, list) {
21013 		if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
21014 			break;
21015 		wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
21016 		wr_object->u.request.bde[i].addrHigh =
21017 			putPaddrHigh(dmabuf->phys);
21018 		if (written + SLI4_PAGE_SIZE >= size) {
21019 			wr_object->u.request.bde[i].tus.f.bdeSize =
21020 				(size - written);
21021 			written += (size - written);
21022 			bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
21023 			bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
21024 			check_change_status = true;
21025 		} else {
21026 			wr_object->u.request.bde[i].tus.f.bdeSize =
21027 				SLI4_PAGE_SIZE;
21028 			written += SLI4_PAGE_SIZE;
21029 		}
21030 		i++;
21031 	}
21032 	wr_object->u.request.bde_count = i;
21033 	bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
21034 	if (!phba->sli4_hba.intr_enable)
21035 		mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
21036 	else {
21037 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
21038 		mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
21039 	}
21040 
21041 	/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */
21042 	rc = mbox_status;
21043 
21044 	/* The IOCTL status is embedded in the mailbox subheader. */
21045 	shdr_status = bf_get(lpfc_mbox_hdr_status,
21046 			     &wr_object->header.cfg_shdr.response);
21047 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
21048 				 &wr_object->header.cfg_shdr.response);
21049 	shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
21050 				   &wr_object->header.cfg_shdr.response);
21051 	if (check_change_status) {
21052 		shdr_change_status = bf_get(lpfc_wr_object_change_status,
21053 					    &wr_object->u.response);
21054 		shdr_csf = bf_get(lpfc_wr_object_csf,
21055 				  &wr_object->u.response);
21056 	}
21057 
21058 	if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
21059 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21060 				"3025 Write Object mailbox failed with "
21061 				"status x%x add_status x%x, add_status_2 x%x, "
21062 				"mbx status x%x\n",
21063 				shdr_status, shdr_add_status, shdr_add_status_2,
21064 				rc);
21065 		rc = -ENXIO;
21066 		*offset = shdr_add_status;
21067 	} else {
21068 		*offset += wr_object->u.response.actual_write_length;
21069 	}
21070 
21071 	if (rc || check_change_status)
21072 		lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
21073 				       shdr_add_status_2, shdr_change_status,
21074 				       shdr_csf);
21075 
21076 	if (!phba->sli4_hba.intr_enable)
21077 		mempool_free(mbox, phba->mbox_mem_pool);
21078 	else if (mbox_status != MBX_TIMEOUT)
21079 		mempool_free(mbox, phba->mbox_mem_pool);
21080 
21081 	return rc;
21082 }
21083 
21084 /**
21085  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
21086  * @vport: pointer to vport data structure.
21087  *
21088  * This function iterate through the mailboxq and clean up all REG_LOGIN
21089  * and REG_VPI mailbox commands associated with the vport. This function
21090  * is called when driver want to restart discovery of the vport due to
21091  * a Clear Virtual Link event.
21092  **/
21093 void
lpfc_cleanup_pending_mbox(struct lpfc_vport * vport)21094 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
21095 {
21096 	struct lpfc_hba *phba = vport->phba;
21097 	LPFC_MBOXQ_t *mb, *nextmb;
21098 	struct lpfc_nodelist *ndlp;
21099 	struct lpfc_nodelist *act_mbx_ndlp = NULL;
21100 	LIST_HEAD(mbox_cmd_list);
21101 	uint8_t restart_loop;
21102 
21103 	/* Clean up internally queued mailbox commands with the vport */
21104 	spin_lock_irq(&phba->hbalock);
21105 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
21106 		if (mb->vport != vport)
21107 			continue;
21108 
21109 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21110 			(mb->u.mb.mbxCommand != MBX_REG_VPI))
21111 			continue;
21112 
21113 		list_move_tail(&mb->list, &mbox_cmd_list);
21114 	}
21115 	/* Clean up active mailbox command with the vport */
21116 	mb = phba->sli.mbox_active;
21117 	if (mb && (mb->vport == vport)) {
21118 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
21119 			(mb->u.mb.mbxCommand == MBX_REG_VPI))
21120 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21121 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21122 			act_mbx_ndlp = mb->ctx_ndlp;
21123 
21124 			/* This reference is local to this routine.  The
21125 			 * reference is removed at routine exit.
21126 			 */
21127 			act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
21128 
21129 			/* Unregister the RPI when mailbox complete */
21130 			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21131 		}
21132 	}
21133 	/* Cleanup any mailbox completions which are not yet processed */
21134 	do {
21135 		restart_loop = 0;
21136 		list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
21137 			/*
21138 			 * If this mailox is already processed or it is
21139 			 * for another vport ignore it.
21140 			 */
21141 			if ((mb->vport != vport) ||
21142 				(mb->mbox_flag & LPFC_MBX_IMED_UNREG))
21143 				continue;
21144 
21145 			if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21146 				(mb->u.mb.mbxCommand != MBX_REG_VPI))
21147 				continue;
21148 
21149 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21150 			if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21151 				ndlp = mb->ctx_ndlp;
21152 				/* Unregister the RPI when mailbox complete */
21153 				mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21154 				restart_loop = 1;
21155 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21156 				break;
21157 			}
21158 		}
21159 	} while (restart_loop);
21160 
21161 	spin_unlock_irq(&phba->hbalock);
21162 
21163 	/* Release the cleaned-up mailbox commands */
21164 	while (!list_empty(&mbox_cmd_list)) {
21165 		list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
21166 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21167 			ndlp = mb->ctx_ndlp;
21168 			mb->ctx_ndlp = NULL;
21169 			if (ndlp) {
21170 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21171 				lpfc_nlp_put(ndlp);
21172 			}
21173 		}
21174 		lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_UNLOCKED);
21175 	}
21176 
21177 	/* Release the ndlp with the cleaned-up active mailbox command */
21178 	if (act_mbx_ndlp) {
21179 		clear_bit(NLP_IGNR_REG_CMPL, &act_mbx_ndlp->nlp_flag);
21180 		lpfc_nlp_put(act_mbx_ndlp);
21181 	}
21182 }
21183 
21184 /**
21185  * lpfc_drain_txq - Drain the txq
21186  * @phba: Pointer to HBA context object.
21187  *
21188  * This function attempt to submit IOCBs on the txq
21189  * to the adapter.  For SLI4 adapters, the txq contains
21190  * ELS IOCBs that have been deferred because the there
21191  * are no SGLs.  This congestion can occur with large
21192  * vport counts during node discovery.
21193  **/
21194 
21195 uint32_t
lpfc_drain_txq(struct lpfc_hba * phba)21196 lpfc_drain_txq(struct lpfc_hba *phba)
21197 {
21198 	LIST_HEAD(completions);
21199 	struct lpfc_sli_ring *pring;
21200 	struct lpfc_iocbq *piocbq = NULL;
21201 	unsigned long iflags = 0;
21202 	char *fail_msg = NULL;
21203 	uint32_t txq_cnt = 0;
21204 	struct lpfc_queue *wq;
21205 	int ret = 0;
21206 
21207 	if (phba->link_flag & LS_MDS_LOOPBACK) {
21208 		/* MDS WQE are posted only to first WQ*/
21209 		wq = phba->sli4_hba.hdwq[0].io_wq;
21210 		if (unlikely(!wq))
21211 			return 0;
21212 		pring = wq->pring;
21213 	} else {
21214 		wq = phba->sli4_hba.els_wq;
21215 		if (unlikely(!wq))
21216 			return 0;
21217 		pring = lpfc_phba_elsring(phba);
21218 	}
21219 
21220 	if (unlikely(!pring) || list_empty(&pring->txq))
21221 		return 0;
21222 
21223 	spin_lock_irqsave(&pring->ring_lock, iflags);
21224 	list_for_each_entry(piocbq, &pring->txq, list) {
21225 		txq_cnt++;
21226 	}
21227 
21228 	if (txq_cnt > pring->txq_max)
21229 		pring->txq_max = txq_cnt;
21230 
21231 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
21232 
21233 	while (!list_empty(&pring->txq)) {
21234 		spin_lock_irqsave(&pring->ring_lock, iflags);
21235 
21236 		piocbq = lpfc_sli_ringtx_get(phba, pring);
21237 		if (!piocbq) {
21238 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21239 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21240 				"2823 txq empty and txq_cnt is %d\n",
21241 				txq_cnt);
21242 			break;
21243 		}
21244 		txq_cnt--;
21245 
21246 		ret = __lpfc_sli_issue_iocb(phba, pring->ringno, piocbq, 0);
21247 
21248 		if (ret && ret != IOCB_BUSY) {
21249 			fail_msg = " - Cannot send IO ";
21250 			piocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21251 		}
21252 		if (fail_msg) {
21253 			piocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
21254 			/* Failed means we can't issue and need to cancel */
21255 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21256 					"2822 IOCB failed %s iotag 0x%x "
21257 					"xri 0x%x %d flg x%x\n",
21258 					fail_msg, piocbq->iotag,
21259 					piocbq->sli4_xritag, ret,
21260 					piocbq->cmd_flag);
21261 			list_add_tail(&piocbq->list, &completions);
21262 			fail_msg = NULL;
21263 		}
21264 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21265 		if (txq_cnt == 0 || ret == IOCB_BUSY)
21266 			break;
21267 	}
21268 	/* Cancel all the IOCBs that cannot be issued */
21269 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21270 			      IOERR_SLI_ABORTED);
21271 
21272 	return txq_cnt;
21273 }
21274 
21275 /**
21276  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21277  * @phba: Pointer to HBA context object.
21278  * @pwqeq: Pointer to command WQE.
21279  * @sglq: Pointer to the scatter gather queue object.
21280  *
21281  * This routine converts the bpl or bde that is in the WQE
21282  * to a sgl list for the sli4 hardware. The physical address
21283  * of the bpl/bde is converted back to a virtual address.
21284  * If the WQE contains a BPL then the list of BDE's is
21285  * converted to sli4_sge's. If the WQE contains a single
21286  * BDE then it is converted to a single sli_sge.
21287  * The WQE is still in cpu endianness so the contents of
21288  * the bpl can be used without byte swapping.
21289  *
21290  * Returns valid XRI = Success, NO_XRI = Failure.
21291  */
21292 static uint16_t
lpfc_wqe_bpl2sgl(struct lpfc_hba * phba,struct lpfc_iocbq * pwqeq,struct lpfc_sglq * sglq)21293 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21294 		 struct lpfc_sglq *sglq)
21295 {
21296 	uint16_t xritag = NO_XRI;
21297 	struct ulp_bde64 *bpl = NULL;
21298 	struct ulp_bde64 bde;
21299 	struct sli4_sge *sgl  = NULL;
21300 	struct lpfc_dmabuf *dmabuf;
21301 	union lpfc_wqe128 *wqe;
21302 	int numBdes = 0;
21303 	int i = 0;
21304 	uint32_t offset = 0; /* accumulated offset in the sg request list */
21305 	int inbound = 0; /* number of sg reply entries inbound from firmware */
21306 	uint32_t cmd;
21307 
21308 	if (!pwqeq || !sglq)
21309 		return xritag;
21310 
21311 	sgl  = (struct sli4_sge *)sglq->sgl;
21312 	wqe = &pwqeq->wqe;
21313 	pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21314 
21315 	cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21316 	if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21317 		return sglq->sli4_xritag;
21318 	numBdes = pwqeq->num_bdes;
21319 	if (numBdes) {
21320 		/* The addrHigh and addrLow fields within the WQE
21321 		 * have not been byteswapped yet so there is no
21322 		 * need to swap them back.
21323 		 */
21324 		if (pwqeq->bpl_dmabuf)
21325 			dmabuf = pwqeq->bpl_dmabuf;
21326 		else
21327 			return xritag;
21328 
21329 		bpl  = (struct ulp_bde64 *)dmabuf->virt;
21330 		if (!bpl)
21331 			return xritag;
21332 
21333 		for (i = 0; i < numBdes; i++) {
21334 			/* Should already be byte swapped. */
21335 			sgl->addr_hi = bpl->addrHigh;
21336 			sgl->addr_lo = bpl->addrLow;
21337 
21338 			sgl->word2 = le32_to_cpu(sgl->word2);
21339 			if ((i+1) == numBdes)
21340 				bf_set(lpfc_sli4_sge_last, sgl, 1);
21341 			else
21342 				bf_set(lpfc_sli4_sge_last, sgl, 0);
21343 			/* swap the size field back to the cpu so we
21344 			 * can assign it to the sgl.
21345 			 */
21346 			bde.tus.w = le32_to_cpu(bpl->tus.w);
21347 			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21348 			/* The offsets in the sgl need to be accumulated
21349 			 * separately for the request and reply lists.
21350 			 * The request is always first, the reply follows.
21351 			 */
21352 			switch (cmd) {
21353 			case CMD_GEN_REQUEST64_WQE:
21354 				/* add up the reply sg entries */
21355 				if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21356 					inbound++;
21357 				/* first inbound? reset the offset */
21358 				if (inbound == 1)
21359 					offset = 0;
21360 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21361 				bf_set(lpfc_sli4_sge_type, sgl,
21362 					LPFC_SGE_TYPE_DATA);
21363 				offset += bde.tus.f.bdeSize;
21364 				break;
21365 			case CMD_FCP_TRSP64_WQE:
21366 				bf_set(lpfc_sli4_sge_offset, sgl, 0);
21367 				bf_set(lpfc_sli4_sge_type, sgl,
21368 					LPFC_SGE_TYPE_DATA);
21369 				break;
21370 			case CMD_FCP_TSEND64_WQE:
21371 			case CMD_FCP_TRECEIVE64_WQE:
21372 				bf_set(lpfc_sli4_sge_type, sgl,
21373 					bpl->tus.f.bdeFlags);
21374 				if (i < 3)
21375 					offset = 0;
21376 				else
21377 					offset += bde.tus.f.bdeSize;
21378 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21379 				break;
21380 			}
21381 			sgl->word2 = cpu_to_le32(sgl->word2);
21382 			bpl++;
21383 			sgl++;
21384 		}
21385 	} else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21386 		/* The addrHigh and addrLow fields of the BDE have not
21387 		 * been byteswapped yet so they need to be swapped
21388 		 * before putting them in the sgl.
21389 		 */
21390 		sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21391 		sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21392 		sgl->word2 = le32_to_cpu(sgl->word2);
21393 		bf_set(lpfc_sli4_sge_last, sgl, 1);
21394 		sgl->word2 = cpu_to_le32(sgl->word2);
21395 		sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21396 	}
21397 	return sglq->sli4_xritag;
21398 }
21399 
21400 /**
21401  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21402  * @phba: Pointer to HBA context object.
21403  * @qp: Pointer to HDW queue.
21404  * @pwqe: Pointer to command WQE.
21405  **/
21406 int
lpfc_sli4_issue_wqe(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * qp,struct lpfc_iocbq * pwqe)21407 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21408 		    struct lpfc_iocbq *pwqe)
21409 {
21410 	union lpfc_wqe128 *wqe = &pwqe->wqe;
21411 	struct lpfc_async_xchg_ctx *ctxp;
21412 	struct lpfc_queue *wq;
21413 	struct lpfc_sglq *sglq;
21414 	struct lpfc_sli_ring *pring;
21415 	unsigned long iflags;
21416 	int ret = 0;
21417 
21418 	/* NVME_LS and NVME_LS ABTS requests. */
21419 	if (pwqe->cmd_flag & LPFC_IO_NVME_LS) {
21420 		pring =  phba->sli4_hba.nvmels_wq->pring;
21421 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21422 					  qp, wq_access);
21423 		sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21424 		if (!sglq) {
21425 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21426 			return WQE_BUSY;
21427 		}
21428 		pwqe->sli4_lxritag = sglq->sli4_lxritag;
21429 		pwqe->sli4_xritag = sglq->sli4_xritag;
21430 		if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21431 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21432 			return WQE_ERROR;
21433 		}
21434 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21435 		       pwqe->sli4_xritag);
21436 		ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21437 		if (ret) {
21438 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21439 			return ret;
21440 		}
21441 
21442 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21443 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21444 
21445 		lpfc_sli4_poll_eq(qp->hba_eq);
21446 		return 0;
21447 	}
21448 
21449 	/* NVME_FCREQ and NVME_ABTS requests */
21450 	if (pwqe->cmd_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
21451 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21452 		wq = qp->io_wq;
21453 		pring = wq->pring;
21454 
21455 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21456 
21457 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21458 					  qp, wq_access);
21459 		ret = lpfc_sli4_wq_put(wq, wqe);
21460 		if (ret) {
21461 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21462 			return ret;
21463 		}
21464 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21465 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21466 
21467 		lpfc_sli4_poll_eq(qp->hba_eq);
21468 		return 0;
21469 	}
21470 
21471 	/* NVMET requests */
21472 	if (pwqe->cmd_flag & LPFC_IO_NVMET) {
21473 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21474 		wq = qp->io_wq;
21475 		pring = wq->pring;
21476 
21477 		ctxp = pwqe->context_un.axchg;
21478 		sglq = ctxp->ctxbuf->sglq;
21479 		if (pwqe->sli4_xritag ==  NO_XRI) {
21480 			pwqe->sli4_lxritag = sglq->sli4_lxritag;
21481 			pwqe->sli4_xritag = sglq->sli4_xritag;
21482 		}
21483 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21484 		       pwqe->sli4_xritag);
21485 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21486 
21487 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21488 					  qp, wq_access);
21489 		ret = lpfc_sli4_wq_put(wq, wqe);
21490 		if (ret) {
21491 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21492 			return ret;
21493 		}
21494 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21495 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21496 
21497 		lpfc_sli4_poll_eq(qp->hba_eq);
21498 		return 0;
21499 	}
21500 	return WQE_ERROR;
21501 }
21502 
21503 /**
21504  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21505  * @phba: Pointer to HBA context object.
21506  * @cmdiocb: Pointer to driver command iocb object.
21507  * @cmpl: completion function.
21508  *
21509  * Fill the appropriate fields for the abort WQE and call
21510  * internal routine lpfc_sli4_issue_wqe to send the WQE
21511  * This function is called with hbalock held and no ring_lock held.
21512  *
21513  * RETURNS 0 - SUCCESS
21514  **/
21515 
21516 int
lpfc_sli4_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,void * cmpl)21517 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21518 			    void *cmpl)
21519 {
21520 	struct lpfc_vport *vport = cmdiocb->vport;
21521 	struct lpfc_iocbq *abtsiocb = NULL;
21522 	union lpfc_wqe128 *abtswqe;
21523 	struct lpfc_io_buf *lpfc_cmd;
21524 	int retval = IOCB_ERROR;
21525 	u16 xritag = cmdiocb->sli4_xritag;
21526 
21527 	/*
21528 	 * The scsi command can not be in txq and it is in flight because the
21529 	 * pCmd is still pointing at the SCSI command we have to abort. There
21530 	 * is no need to search the txcmplq. Just send an abort to the FW.
21531 	 */
21532 
21533 	abtsiocb = __lpfc_sli_get_iocbq(phba);
21534 	if (!abtsiocb)
21535 		return WQE_NORESOURCE;
21536 
21537 	/* Indicate the IO is being aborted by the driver. */
21538 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
21539 
21540 	abtswqe = &abtsiocb->wqe;
21541 	memset(abtswqe, 0, sizeof(*abtswqe));
21542 
21543 	if (!lpfc_is_link_up(phba) || (phba->link_flag & LS_EXTERNAL_LOOPBACK))
21544 		bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
21545 	bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21546 	abtswqe->abort_cmd.rsrvd5 = 0;
21547 	abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21548 	bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21549 	bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21550 	bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21551 	bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21552 	bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21553 	bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21554 
21555 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
21556 	abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21557 	abtsiocb->cmd_flag |= LPFC_USE_FCPWQIDX;
21558 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
21559 		abtsiocb->cmd_flag |= LPFC_IO_FCP;
21560 	if (cmdiocb->cmd_flag & LPFC_IO_NVME)
21561 		abtsiocb->cmd_flag |= LPFC_IO_NVME;
21562 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
21563 		abtsiocb->cmd_flag |= LPFC_IO_FOF;
21564 	abtsiocb->vport = vport;
21565 	abtsiocb->cmd_cmpl = cmpl;
21566 
21567 	lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21568 	retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21569 
21570 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21571 			 "0359 Abort xri x%x, original iotag x%x, "
21572 			 "abort cmd iotag x%x retval x%x\n",
21573 			 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21574 
21575 	if (retval) {
21576 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21577 		__lpfc_sli_release_iocbq(phba, abtsiocb);
21578 	}
21579 
21580 	return retval;
21581 }
21582 
21583 #ifdef LPFC_MXP_STAT
21584 /**
21585  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21586  * @phba: pointer to lpfc hba data structure.
21587  * @hwqid: belong to which HWQ.
21588  *
21589  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21590  * 15 seconds after a test case is running.
21591  *
21592  * The user should call lpfc_debugfs_multixripools_write before running a test
21593  * case to clear stat_snapshot_taken. Then the user starts a test case. During
21594  * test case is running, stat_snapshot_taken is incremented by 1 every time when
21595  * this routine is called from heartbeat timer. When stat_snapshot_taken is
21596  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21597  **/
lpfc_snapshot_mxp(struct lpfc_hba * phba,u32 hwqid)21598 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21599 {
21600 	struct lpfc_sli4_hdw_queue *qp;
21601 	struct lpfc_multixri_pool *multixri_pool;
21602 	struct lpfc_pvt_pool *pvt_pool;
21603 	struct lpfc_pbl_pool *pbl_pool;
21604 	u32 txcmplq_cnt;
21605 
21606 	qp = &phba->sli4_hba.hdwq[hwqid];
21607 	multixri_pool = qp->p_multixri_pool;
21608 	if (!multixri_pool)
21609 		return;
21610 
21611 	if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21612 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21613 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21614 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21615 
21616 		multixri_pool->stat_pbl_count = pbl_pool->count;
21617 		multixri_pool->stat_pvt_count = pvt_pool->count;
21618 		multixri_pool->stat_busy_count = txcmplq_cnt;
21619 	}
21620 
21621 	multixri_pool->stat_snapshot_taken++;
21622 }
21623 #endif
21624 
21625 /**
21626  * lpfc_adjust_pvt_pool_count - Adjust private pool count
21627  * @phba: pointer to lpfc hba data structure.
21628  * @hwqid: belong to which HWQ.
21629  *
21630  * This routine moves some XRIs from private to public pool when private pool
21631  * is not busy.
21632  **/
lpfc_adjust_pvt_pool_count(struct lpfc_hba * phba,u32 hwqid)21633 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21634 {
21635 	struct lpfc_multixri_pool *multixri_pool;
21636 	u32 io_req_count;
21637 	u32 prev_io_req_count;
21638 
21639 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21640 	if (!multixri_pool)
21641 		return;
21642 	io_req_count = multixri_pool->io_req_count;
21643 	prev_io_req_count = multixri_pool->prev_io_req_count;
21644 
21645 	if (prev_io_req_count != io_req_count) {
21646 		/* Private pool is busy */
21647 		multixri_pool->prev_io_req_count = io_req_count;
21648 	} else {
21649 		/* Private pool is not busy.
21650 		 * Move XRIs from private to public pool.
21651 		 */
21652 		lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21653 	}
21654 }
21655 
21656 /**
21657  * lpfc_adjust_high_watermark - Adjust high watermark
21658  * @phba: pointer to lpfc hba data structure.
21659  * @hwqid: belong to which HWQ.
21660  *
21661  * This routine sets high watermark as number of outstanding XRIs,
21662  * but make sure the new value is between xri_limit/2 and xri_limit.
21663  **/
lpfc_adjust_high_watermark(struct lpfc_hba * phba,u32 hwqid)21664 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21665 {
21666 	u32 new_watermark;
21667 	u32 watermark_max;
21668 	u32 watermark_min;
21669 	u32 xri_limit;
21670 	u32 txcmplq_cnt;
21671 	u32 abts_io_bufs;
21672 	struct lpfc_multixri_pool *multixri_pool;
21673 	struct lpfc_sli4_hdw_queue *qp;
21674 
21675 	qp = &phba->sli4_hba.hdwq[hwqid];
21676 	multixri_pool = qp->p_multixri_pool;
21677 	if (!multixri_pool)
21678 		return;
21679 	xri_limit = multixri_pool->xri_limit;
21680 
21681 	watermark_max = xri_limit;
21682 	watermark_min = xri_limit / 2;
21683 
21684 	txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21685 	abts_io_bufs = qp->abts_scsi_io_bufs;
21686 	abts_io_bufs += qp->abts_nvme_io_bufs;
21687 
21688 	new_watermark = txcmplq_cnt + abts_io_bufs;
21689 	new_watermark = min(watermark_max, new_watermark);
21690 	new_watermark = max(watermark_min, new_watermark);
21691 	multixri_pool->pvt_pool.high_watermark = new_watermark;
21692 
21693 #ifdef LPFC_MXP_STAT
21694 	multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21695 					  new_watermark);
21696 #endif
21697 }
21698 
21699 /**
21700  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21701  * @phba: pointer to lpfc hba data structure.
21702  * @hwqid: belong to which HWQ.
21703  *
21704  * This routine is called from heartbeat timer when pvt_pool is idle.
21705  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21706  * The first step moves (all - low_watermark) amount of XRIs.
21707  * The second step moves the rest of XRIs.
21708  **/
lpfc_move_xri_pvt_to_pbl(struct lpfc_hba * phba,u32 hwqid)21709 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21710 {
21711 	struct lpfc_pbl_pool *pbl_pool;
21712 	struct lpfc_pvt_pool *pvt_pool;
21713 	struct lpfc_sli4_hdw_queue *qp;
21714 	struct lpfc_io_buf *lpfc_ncmd;
21715 	struct lpfc_io_buf *lpfc_ncmd_next;
21716 	unsigned long iflag;
21717 	struct list_head tmp_list;
21718 	u32 tmp_count;
21719 
21720 	qp = &phba->sli4_hba.hdwq[hwqid];
21721 	pbl_pool = &qp->p_multixri_pool->pbl_pool;
21722 	pvt_pool = &qp->p_multixri_pool->pvt_pool;
21723 	tmp_count = 0;
21724 
21725 	lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21726 	lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
21727 
21728 	if (pvt_pool->count > pvt_pool->low_watermark) {
21729 		/* Step 1: move (all - low_watermark) from pvt_pool
21730 		 * to pbl_pool
21731 		 */
21732 
21733 		/* Move low watermark of bufs from pvt_pool to tmp_list */
21734 		INIT_LIST_HEAD(&tmp_list);
21735 		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21736 					 &pvt_pool->list, list) {
21737 			list_move_tail(&lpfc_ncmd->list, &tmp_list);
21738 			tmp_count++;
21739 			if (tmp_count >= pvt_pool->low_watermark)
21740 				break;
21741 		}
21742 
21743 		/* Move all bufs from pvt_pool to pbl_pool */
21744 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21745 
21746 		/* Move all bufs from tmp_list to pvt_pool */
21747 		list_splice(&tmp_list, &pvt_pool->list);
21748 
21749 		pbl_pool->count += (pvt_pool->count - tmp_count);
21750 		pvt_pool->count = tmp_count;
21751 	} else {
21752 		/* Step 2: move the rest from pvt_pool to pbl_pool */
21753 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21754 		pbl_pool->count += pvt_pool->count;
21755 		pvt_pool->count = 0;
21756 	}
21757 
21758 	spin_unlock(&pvt_pool->lock);
21759 	spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21760 }
21761 
21762 /**
21763  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21764  * @phba: pointer to lpfc hba data structure
21765  * @qp: pointer to HDW queue
21766  * @pbl_pool: specified public free XRI pool
21767  * @pvt_pool: specified private free XRI pool
21768  * @count: number of XRIs to move
21769  *
21770  * This routine tries to move some free common bufs from the specified pbl_pool
21771  * to the specified pvt_pool. It might move less than count XRIs if there's not
21772  * enough in public pool.
21773  *
21774  * Return:
21775  *   true - if XRIs are successfully moved from the specified pbl_pool to the
21776  *          specified pvt_pool
21777  *   false - if the specified pbl_pool is empty or locked by someone else
21778  **/
21779 static bool
_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * qp,struct lpfc_pbl_pool * pbl_pool,struct lpfc_pvt_pool * pvt_pool,u32 count)21780 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21781 			  struct lpfc_pbl_pool *pbl_pool,
21782 			  struct lpfc_pvt_pool *pvt_pool, u32 count)
21783 {
21784 	struct lpfc_io_buf *lpfc_ncmd;
21785 	struct lpfc_io_buf *lpfc_ncmd_next;
21786 	unsigned long iflag;
21787 	int ret;
21788 
21789 	ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21790 	if (ret) {
21791 		if (pbl_pool->count) {
21792 			/* Move a batch of XRIs from public to private pool */
21793 			lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
21794 			list_for_each_entry_safe(lpfc_ncmd,
21795 						 lpfc_ncmd_next,
21796 						 &pbl_pool->list,
21797 						 list) {
21798 				list_move_tail(&lpfc_ncmd->list,
21799 					       &pvt_pool->list);
21800 				pvt_pool->count++;
21801 				pbl_pool->count--;
21802 				count--;
21803 				if (count == 0)
21804 					break;
21805 			}
21806 
21807 			spin_unlock(&pvt_pool->lock);
21808 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21809 			return true;
21810 		}
21811 		spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21812 	}
21813 
21814 	return false;
21815 }
21816 
21817 /**
21818  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21819  * @phba: pointer to lpfc hba data structure.
21820  * @hwqid: belong to which HWQ.
21821  * @count: number of XRIs to move
21822  *
21823  * This routine tries to find some free common bufs in one of public pools with
21824  * Round Robin method. The search always starts from local hwqid, then the next
21825  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21826  * a batch of free common bufs are moved to private pool on hwqid.
21827  * It might move less than count XRIs if there's not enough in public pool.
21828  **/
lpfc_move_xri_pbl_to_pvt(struct lpfc_hba * phba,u32 hwqid,u32 count)21829 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21830 {
21831 	struct lpfc_multixri_pool *multixri_pool;
21832 	struct lpfc_multixri_pool *next_multixri_pool;
21833 	struct lpfc_pvt_pool *pvt_pool;
21834 	struct lpfc_pbl_pool *pbl_pool;
21835 	struct lpfc_sli4_hdw_queue *qp;
21836 	u32 next_hwqid;
21837 	u32 hwq_count;
21838 	int ret;
21839 
21840 	qp = &phba->sli4_hba.hdwq[hwqid];
21841 	multixri_pool = qp->p_multixri_pool;
21842 	pvt_pool = &multixri_pool->pvt_pool;
21843 	pbl_pool = &multixri_pool->pbl_pool;
21844 
21845 	/* Check if local pbl_pool is available */
21846 	ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
21847 	if (ret) {
21848 #ifdef LPFC_MXP_STAT
21849 		multixri_pool->local_pbl_hit_count++;
21850 #endif
21851 		return;
21852 	}
21853 
21854 	hwq_count = phba->cfg_hdw_queue;
21855 
21856 	/* Get the next hwqid which was found last time */
21857 	next_hwqid = multixri_pool->rrb_next_hwqid;
21858 
21859 	do {
21860 		/* Go to next hwq */
21861 		next_hwqid = (next_hwqid + 1) % hwq_count;
21862 
21863 		next_multixri_pool =
21864 			phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21865 		pbl_pool = &next_multixri_pool->pbl_pool;
21866 
21867 		/* Check if the public free xri pool is available */
21868 		ret = _lpfc_move_xri_pbl_to_pvt(
21869 			phba, qp, pbl_pool, pvt_pool, count);
21870 
21871 		/* Exit while-loop if success or all hwqid are checked */
21872 	} while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21873 
21874 	/* Starting point for the next time */
21875 	multixri_pool->rrb_next_hwqid = next_hwqid;
21876 
21877 	if (!ret) {
21878 		/* stats: all public pools are empty*/
21879 		multixri_pool->pbl_empty_count++;
21880 	}
21881 
21882 #ifdef LPFC_MXP_STAT
21883 	if (ret) {
21884 		if (next_hwqid == hwqid)
21885 			multixri_pool->local_pbl_hit_count++;
21886 		else
21887 			multixri_pool->other_pbl_hit_count++;
21888 	}
21889 #endif
21890 }
21891 
21892 /**
21893  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21894  * @phba: pointer to lpfc hba data structure.
21895  * @hwqid: belong to which HWQ.
21896  *
21897  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21898  * low watermark.
21899  **/
lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba * phba,u32 hwqid)21900 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21901 {
21902 	struct lpfc_multixri_pool *multixri_pool;
21903 	struct lpfc_pvt_pool *pvt_pool;
21904 
21905 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21906 	pvt_pool = &multixri_pool->pvt_pool;
21907 
21908 	if (pvt_pool->count < pvt_pool->low_watermark)
21909 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21910 }
21911 
21912 /**
21913  * lpfc_release_io_buf - Return one IO buf back to free pool
21914  * @phba: pointer to lpfc hba data structure.
21915  * @lpfc_ncmd: IO buf to be returned.
21916  * @qp: belong to which HWQ.
21917  *
21918  * This routine returns one IO buf back to free pool. If this is an urgent IO,
21919  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21920  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21921  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
21922  * lpfc_io_buf_list_put.
21923  **/
lpfc_release_io_buf(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_ncmd,struct lpfc_sli4_hdw_queue * qp)21924 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21925 			 struct lpfc_sli4_hdw_queue *qp)
21926 {
21927 	unsigned long iflag;
21928 	struct lpfc_pbl_pool *pbl_pool;
21929 	struct lpfc_pvt_pool *pvt_pool;
21930 	struct lpfc_epd_pool *epd_pool;
21931 	u32 txcmplq_cnt;
21932 	u32 xri_owned;
21933 	u32 xri_limit;
21934 	u32 abts_io_bufs;
21935 
21936 	/* MUST zero fields if buffer is reused by another protocol */
21937 	lpfc_ncmd->nvmeCmd = NULL;
21938 	lpfc_ncmd->cur_iocbq.cmd_cmpl = NULL;
21939 
21940 	if (phba->cfg_xpsgl && !phba->nvmet_support &&
21941 	    !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21942 		lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21943 
21944 	if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21945 		lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21946 
21947 	if (phba->cfg_xri_rebalancing) {
21948 		if (lpfc_ncmd->expedite) {
21949 			/* Return to expedite pool */
21950 			epd_pool = &phba->epd_pool;
21951 			spin_lock_irqsave(&epd_pool->lock, iflag);
21952 			list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21953 			epd_pool->count++;
21954 			spin_unlock_irqrestore(&epd_pool->lock, iflag);
21955 			return;
21956 		}
21957 
21958 		/* Avoid invalid access if an IO sneaks in and is being rejected
21959 		 * just _after_ xri pools are destroyed in lpfc_offline.
21960 		 * Nothing much can be done at this point.
21961 		 */
21962 		if (!qp->p_multixri_pool)
21963 			return;
21964 
21965 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21966 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21967 
21968 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21969 		abts_io_bufs = qp->abts_scsi_io_bufs;
21970 		abts_io_bufs += qp->abts_nvme_io_bufs;
21971 
21972 		xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21973 		xri_limit = qp->p_multixri_pool->xri_limit;
21974 
21975 #ifdef LPFC_MXP_STAT
21976 		if (xri_owned <= xri_limit)
21977 			qp->p_multixri_pool->below_limit_count++;
21978 		else
21979 			qp->p_multixri_pool->above_limit_count++;
21980 #endif
21981 
21982 		/* XRI goes to either public or private free xri pool
21983 		 *     based on watermark and xri_limit
21984 		 */
21985 		if ((pvt_pool->count < pvt_pool->low_watermark) ||
21986 		    (xri_owned < xri_limit &&
21987 		     pvt_pool->count < pvt_pool->high_watermark)) {
21988 			lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21989 						  qp, free_pvt_pool);
21990 			list_add_tail(&lpfc_ncmd->list,
21991 				      &pvt_pool->list);
21992 			pvt_pool->count++;
21993 			spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21994 		} else {
21995 			lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21996 						  qp, free_pub_pool);
21997 			list_add_tail(&lpfc_ncmd->list,
21998 				      &pbl_pool->list);
21999 			pbl_pool->count++;
22000 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
22001 		}
22002 	} else {
22003 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
22004 					  qp, free_xri);
22005 		list_add_tail(&lpfc_ncmd->list,
22006 			      &qp->lpfc_io_buf_list_put);
22007 		qp->put_io_bufs++;
22008 		spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
22009 				       iflag);
22010 	}
22011 }
22012 
22013 /**
22014  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
22015  * @phba: pointer to lpfc hba data structure.
22016  * @qp: pointer to HDW queue
22017  * @pvt_pool: pointer to private pool data structure.
22018  * @ndlp: pointer to lpfc nodelist data structure.
22019  *
22020  * This routine tries to get one free IO buf from private pool.
22021  *
22022  * Return:
22023  *   pointer to one free IO buf - if private pool is not empty
22024  *   NULL - if private pool is empty
22025  **/
22026 static struct lpfc_io_buf *
lpfc_get_io_buf_from_private_pool(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * qp,struct lpfc_pvt_pool * pvt_pool,struct lpfc_nodelist * ndlp)22027 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
22028 				  struct lpfc_sli4_hdw_queue *qp,
22029 				  struct lpfc_pvt_pool *pvt_pool,
22030 				  struct lpfc_nodelist *ndlp)
22031 {
22032 	struct lpfc_io_buf *lpfc_ncmd;
22033 	struct lpfc_io_buf *lpfc_ncmd_next;
22034 	unsigned long iflag;
22035 
22036 	lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
22037 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
22038 				 &pvt_pool->list, list) {
22039 		if (lpfc_test_rrq_active(
22040 			phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
22041 			continue;
22042 		list_del(&lpfc_ncmd->list);
22043 		pvt_pool->count--;
22044 		spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22045 		return lpfc_ncmd;
22046 	}
22047 	spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22048 
22049 	return NULL;
22050 }
22051 
22052 /**
22053  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
22054  * @phba: pointer to lpfc hba data structure.
22055  *
22056  * This routine tries to get one free IO buf from expedite pool.
22057  *
22058  * Return:
22059  *   pointer to one free IO buf - if expedite pool is not empty
22060  *   NULL - if expedite pool is empty
22061  **/
22062 static struct lpfc_io_buf *
lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba * phba)22063 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
22064 {
22065 	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
22066 	struct lpfc_io_buf *lpfc_ncmd_next;
22067 	unsigned long iflag;
22068 	struct lpfc_epd_pool *epd_pool;
22069 
22070 	epd_pool = &phba->epd_pool;
22071 
22072 	spin_lock_irqsave(&epd_pool->lock, iflag);
22073 	if (epd_pool->count > 0) {
22074 		list_for_each_entry_safe(iter, lpfc_ncmd_next,
22075 					 &epd_pool->list, list) {
22076 			list_del(&iter->list);
22077 			epd_pool->count--;
22078 			lpfc_ncmd = iter;
22079 			break;
22080 		}
22081 	}
22082 	spin_unlock_irqrestore(&epd_pool->lock, iflag);
22083 
22084 	return lpfc_ncmd;
22085 }
22086 
22087 /**
22088  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
22089  * @phba: pointer to lpfc hba data structure.
22090  * @ndlp: pointer to lpfc nodelist data structure.
22091  * @hwqid: belong to which HWQ
22092  * @expedite: 1 means this request is urgent.
22093  *
22094  * This routine will do the following actions and then return a pointer to
22095  * one free IO buf.
22096  *
22097  * 1. If private free xri count is empty, move some XRIs from public to
22098  *    private pool.
22099  * 2. Get one XRI from private free xri pool.
22100  * 3. If we fail to get one from pvt_pool and this is an expedite request,
22101  *    get one free xri from expedite pool.
22102  *
22103  * Note: ndlp is only used on SCSI side for RRQ testing.
22104  *       The caller should pass NULL for ndlp on NVME side.
22105  *
22106  * Return:
22107  *   pointer to one free IO buf - if private pool is not empty
22108  *   NULL - if private pool is empty
22109  **/
22110 static struct lpfc_io_buf *
lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int hwqid,int expedite)22111 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
22112 				    struct lpfc_nodelist *ndlp,
22113 				    int hwqid, int expedite)
22114 {
22115 	struct lpfc_sli4_hdw_queue *qp;
22116 	struct lpfc_multixri_pool *multixri_pool;
22117 	struct lpfc_pvt_pool *pvt_pool;
22118 	struct lpfc_io_buf *lpfc_ncmd;
22119 
22120 	qp = &phba->sli4_hba.hdwq[hwqid];
22121 	lpfc_ncmd = NULL;
22122 	if (!qp) {
22123 		lpfc_printf_log(phba, KERN_INFO,
22124 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22125 				"5556 NULL qp for hwqid  x%x\n", hwqid);
22126 		return lpfc_ncmd;
22127 	}
22128 	multixri_pool = qp->p_multixri_pool;
22129 	if (!multixri_pool) {
22130 		lpfc_printf_log(phba, KERN_INFO,
22131 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22132 				"5557 NULL multixri for hwqid  x%x\n", hwqid);
22133 		return lpfc_ncmd;
22134 	}
22135 	pvt_pool = &multixri_pool->pvt_pool;
22136 	if (!pvt_pool) {
22137 		lpfc_printf_log(phba, KERN_INFO,
22138 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22139 				"5558 NULL pvt_pool for hwqid  x%x\n", hwqid);
22140 		return lpfc_ncmd;
22141 	}
22142 	multixri_pool->io_req_count++;
22143 
22144 	/* If pvt_pool is empty, move some XRIs from public to private pool */
22145 	if (pvt_pool->count == 0)
22146 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
22147 
22148 	/* Get one XRI from private free xri pool */
22149 	lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
22150 
22151 	if (lpfc_ncmd) {
22152 		lpfc_ncmd->hdwq = qp;
22153 		lpfc_ncmd->hdwq_no = hwqid;
22154 	} else if (expedite) {
22155 		/* If we fail to get one from pvt_pool and this is an expedite
22156 		 * request, get one free xri from expedite pool.
22157 		 */
22158 		lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
22159 	}
22160 
22161 	return lpfc_ncmd;
22162 }
22163 
22164 static inline struct lpfc_io_buf *
lpfc_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int idx)22165 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
22166 {
22167 	struct lpfc_sli4_hdw_queue *qp;
22168 	struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
22169 
22170 	qp = &phba->sli4_hba.hdwq[idx];
22171 	list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22172 				 &qp->lpfc_io_buf_list_get, list) {
22173 		if (lpfc_test_rrq_active(phba, ndlp,
22174 					 lpfc_cmd->cur_iocbq.sli4_lxritag))
22175 			continue;
22176 
22177 		if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22178 			continue;
22179 
22180 		list_del_init(&lpfc_cmd->list);
22181 		qp->get_io_bufs--;
22182 		lpfc_cmd->hdwq = qp;
22183 		lpfc_cmd->hdwq_no = idx;
22184 		return lpfc_cmd;
22185 	}
22186 	return NULL;
22187 }
22188 
22189 /**
22190  * lpfc_get_io_buf - Get one IO buffer from free pool
22191  * @phba: The HBA for which this call is being executed.
22192  * @ndlp: pointer to lpfc nodelist data structure.
22193  * @hwqid: belong to which HWQ
22194  * @expedite: 1 means this request is urgent.
22195  *
22196  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22197  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22198  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22199  *
22200  * Note: ndlp is only used on SCSI side for RRQ testing.
22201  *       The caller should pass NULL for ndlp on NVME side.
22202  *
22203  * Return codes:
22204  *   NULL - Error
22205  *   Pointer to lpfc_io_buf - Success
22206  **/
lpfc_get_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,u32 hwqid,int expedite)22207 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22208 				    struct lpfc_nodelist *ndlp,
22209 				    u32 hwqid, int expedite)
22210 {
22211 	struct lpfc_sli4_hdw_queue *qp;
22212 	unsigned long iflag;
22213 	struct lpfc_io_buf *lpfc_cmd;
22214 
22215 	qp = &phba->sli4_hba.hdwq[hwqid];
22216 	lpfc_cmd = NULL;
22217 	if (!qp) {
22218 		lpfc_printf_log(phba, KERN_WARNING,
22219 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22220 				"5555 NULL qp for hwqid  x%x\n", hwqid);
22221 		return lpfc_cmd;
22222 	}
22223 
22224 	if (phba->cfg_xri_rebalancing)
22225 		lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22226 			phba, ndlp, hwqid, expedite);
22227 	else {
22228 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22229 					  qp, alloc_xri_get);
22230 		if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22231 			lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22232 		if (!lpfc_cmd) {
22233 			lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22234 					  qp, alloc_xri_put);
22235 			list_splice(&qp->lpfc_io_buf_list_put,
22236 				    &qp->lpfc_io_buf_list_get);
22237 			qp->get_io_bufs += qp->put_io_bufs;
22238 			INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22239 			qp->put_io_bufs = 0;
22240 			spin_unlock(&qp->io_buf_list_put_lock);
22241 			if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22242 			    expedite)
22243 				lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22244 		}
22245 		spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22246 	}
22247 
22248 	return lpfc_cmd;
22249 }
22250 
22251 /**
22252  * lpfc_read_object - Retrieve object data from HBA
22253  * @phba: The HBA for which this call is being executed.
22254  * @rdobject: Pathname of object data we want to read.
22255  * @datap: Pointer to where data will be copied to.
22256  * @datasz: size of data area
22257  *
22258  * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22259  * The data will be truncated if datasz is not large enough.
22260  * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22261  * Returns the actual bytes read from the object.
22262  *
22263  * This routine is hard coded to use a poll completion.  Unlike other
22264  * sli4_config mailboxes, it uses lpfc_mbuf memory which is not
22265  * cleaned up in lpfc_sli4_cmd_mbox_free.  If this routine is modified
22266  * to use interrupt-based completions, code is needed to fully cleanup
22267  * the memory.
22268  */
22269 int
lpfc_read_object(struct lpfc_hba * phba,char * rdobject,uint32_t * datap,uint32_t datasz)22270 lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22271 		 uint32_t datasz)
22272 {
22273 	struct lpfc_mbx_read_object *read_object;
22274 	LPFC_MBOXQ_t *mbox;
22275 	int rc, length, eof, j, byte_cnt = 0;
22276 	uint32_t shdr_status, shdr_add_status;
22277 	union lpfc_sli4_cfg_shdr *shdr;
22278 	struct lpfc_dmabuf *pcmd;
22279 	u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
22280 
22281 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22282 	if (!mbox)
22283 		return -ENOMEM;
22284 	length = (sizeof(struct lpfc_mbx_read_object) -
22285 		  sizeof(struct lpfc_sli4_cfg_mhdr));
22286 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22287 			 LPFC_MBOX_OPCODE_READ_OBJECT,
22288 			 length, LPFC_SLI4_MBX_EMBED);
22289 	read_object = &mbox->u.mqe.un.read_object;
22290 	shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22291 
22292 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22293 	bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22294 	read_object->u.request.rd_object_offset = 0;
22295 	read_object->u.request.rd_object_cnt = 1;
22296 
22297 	memset((void *)read_object->u.request.rd_object_name, 0,
22298 	       LPFC_OBJ_NAME_SZ);
22299 	scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
22300 	for (j = 0; j < strlen(rdobject); j++)
22301 		read_object->u.request.rd_object_name[j] =
22302 			cpu_to_le32(rd_object_name[j]);
22303 
22304 	pcmd = kmalloc_obj(*pcmd);
22305 	if (pcmd)
22306 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22307 	if (!pcmd || !pcmd->virt) {
22308 		kfree(pcmd);
22309 		mempool_free(mbox, phba->mbox_mem_pool);
22310 		return -ENOMEM;
22311 	}
22312 	memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22313 	read_object->u.request.rd_object_hbuf[0].pa_lo =
22314 		putPaddrLow(pcmd->phys);
22315 	read_object->u.request.rd_object_hbuf[0].pa_hi =
22316 		putPaddrHigh(pcmd->phys);
22317 	read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22318 
22319 	mbox->vport = phba->pport;
22320 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22321 	mbox->ctx_ndlp = NULL;
22322 
22323 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22324 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22325 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22326 
22327 	if (shdr_status == STATUS_FAILED &&
22328 	    shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22329 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22330 				"4674 No port cfg file in FW.\n");
22331 		byte_cnt = -ENOENT;
22332 	} else if (shdr_status || shdr_add_status || rc) {
22333 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22334 				"2625 READ_OBJECT mailbox failed with "
22335 				"status x%x add_status x%x, mbx status x%x\n",
22336 				shdr_status, shdr_add_status, rc);
22337 		byte_cnt = -ENXIO;
22338 	} else {
22339 		/* Success */
22340 		length = read_object->u.response.rd_object_actual_rlen;
22341 		eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22342 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22343 				"2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22344 				length, datasz, eof);
22345 
22346 		/* Detect the port config file exists but is empty */
22347 		if (!length && eof) {
22348 			byte_cnt = 0;
22349 			goto exit;
22350 		}
22351 
22352 		byte_cnt = length;
22353 		lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22354 	}
22355 
22356  exit:
22357 	/* This is an embedded SLI4 mailbox with an external buffer allocated.
22358 	 * Free the pcmd and then cleanup with the correct routine.
22359 	 */
22360 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22361 	kfree(pcmd);
22362 	lpfc_sli4_mbox_cmd_free(phba, mbox);
22363 	return byte_cnt;
22364 }
22365 
22366 /**
22367  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22368  * @phba: The HBA for which this call is being executed.
22369  * @lpfc_buf: IO buf structure to append the SGL chunk
22370  *
22371  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22372  * and will allocate an SGL chunk if the pool is empty.
22373  *
22374  * Return codes:
22375  *   NULL - Error
22376  *   Pointer to sli4_hybrid_sgl - Success
22377  **/
22378 struct sli4_hybrid_sgl *
lpfc_get_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22379 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22380 {
22381 	struct sli4_hybrid_sgl *list_entry = NULL;
22382 	struct sli4_hybrid_sgl *tmp = NULL;
22383 	struct sli4_hybrid_sgl *allocated_sgl = NULL;
22384 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22385 	struct list_head *buf_list = &hdwq->sgl_list;
22386 	unsigned long iflags;
22387 
22388 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22389 
22390 	if (likely(!list_empty(buf_list))) {
22391 		/* break off 1 chunk from the sgl_list */
22392 		list_for_each_entry_safe(list_entry, tmp,
22393 					 buf_list, list_node) {
22394 			list_move_tail(&list_entry->list_node,
22395 				       &lpfc_buf->dma_sgl_xtra_list);
22396 			break;
22397 		}
22398 	} else {
22399 		/* allocate more */
22400 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22401 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22402 				   cpu_to_node(hdwq->io_wq->chann));
22403 		if (!tmp) {
22404 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22405 					"8353 error kmalloc memory for HDWQ "
22406 					"%d %s\n",
22407 					lpfc_buf->hdwq_no, __func__);
22408 			return NULL;
22409 		}
22410 
22411 		tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22412 					      GFP_ATOMIC, &tmp->dma_phys_sgl);
22413 		if (!tmp->dma_sgl) {
22414 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22415 					"8354 error pool_alloc memory for HDWQ "
22416 					"%d %s\n",
22417 					lpfc_buf->hdwq_no, __func__);
22418 			kfree(tmp);
22419 			return NULL;
22420 		}
22421 
22422 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22423 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22424 	}
22425 
22426 	allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22427 					struct sli4_hybrid_sgl,
22428 					list_node);
22429 
22430 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22431 
22432 	return allocated_sgl;
22433 }
22434 
22435 /**
22436  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22437  * @phba: The HBA for which this call is being executed.
22438  * @lpfc_buf: IO buf structure with the SGL chunk
22439  *
22440  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22441  *
22442  * Return codes:
22443  *   0 - Success
22444  *   -EINVAL - Error
22445  **/
22446 int
lpfc_put_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22447 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22448 {
22449 	int rc = 0;
22450 	struct sli4_hybrid_sgl *list_entry = NULL;
22451 	struct sli4_hybrid_sgl *tmp = NULL;
22452 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22453 	struct list_head *buf_list = &hdwq->sgl_list;
22454 	unsigned long iflags;
22455 
22456 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22457 
22458 	if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22459 		list_for_each_entry_safe(list_entry, tmp,
22460 					 &lpfc_buf->dma_sgl_xtra_list,
22461 					 list_node) {
22462 			list_move_tail(&list_entry->list_node,
22463 				       buf_list);
22464 		}
22465 	} else {
22466 		rc = -EINVAL;
22467 	}
22468 
22469 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22470 	return rc;
22471 }
22472 
22473 /**
22474  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22475  * @phba: phba object
22476  * @hdwq: hdwq to cleanup sgl buff resources on
22477  *
22478  * This routine frees all SGL chunks of hdwq SGL chunk pool.
22479  *
22480  * Return codes:
22481  *   None
22482  **/
22483 void
lpfc_free_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22484 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22485 		       struct lpfc_sli4_hdw_queue *hdwq)
22486 {
22487 	struct list_head *buf_list = &hdwq->sgl_list;
22488 	struct sli4_hybrid_sgl *list_entry = NULL;
22489 	struct sli4_hybrid_sgl *tmp = NULL;
22490 	unsigned long iflags;
22491 
22492 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22493 
22494 	/* Free sgl pool */
22495 	list_for_each_entry_safe(list_entry, tmp,
22496 				 buf_list, list_node) {
22497 		list_del(&list_entry->list_node);
22498 		dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22499 			      list_entry->dma_sgl,
22500 			      list_entry->dma_phys_sgl);
22501 		kfree(list_entry);
22502 	}
22503 
22504 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22505 }
22506 
22507 /**
22508  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22509  * @phba: The HBA for which this call is being executed.
22510  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22511  *
22512  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22513  * and will allocate an CMD/RSP buffer if the pool is empty.
22514  *
22515  * Return codes:
22516  *   NULL - Error
22517  *   Pointer to fcp_cmd_rsp_buf - Success
22518  **/
22519 struct fcp_cmd_rsp_buf *
lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22520 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22521 			      struct lpfc_io_buf *lpfc_buf)
22522 {
22523 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22524 	struct fcp_cmd_rsp_buf *tmp = NULL;
22525 	struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22526 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22527 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22528 	unsigned long iflags;
22529 
22530 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22531 
22532 	if (likely(!list_empty(buf_list))) {
22533 		/* break off 1 chunk from the list */
22534 		list_for_each_entry_safe(list_entry, tmp,
22535 					 buf_list,
22536 					 list_node) {
22537 			list_move_tail(&list_entry->list_node,
22538 				       &lpfc_buf->dma_cmd_rsp_list);
22539 			break;
22540 		}
22541 	} else {
22542 		/* allocate more */
22543 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22544 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22545 				   cpu_to_node(hdwq->io_wq->chann));
22546 		if (!tmp) {
22547 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22548 					"8355 error kmalloc memory for HDWQ "
22549 					"%d %s\n",
22550 					lpfc_buf->hdwq_no, __func__);
22551 			return NULL;
22552 		}
22553 
22554 		tmp->fcp_cmnd = dma_pool_zalloc(phba->lpfc_cmd_rsp_buf_pool,
22555 						GFP_ATOMIC,
22556 						&tmp->fcp_cmd_rsp_dma_handle);
22557 
22558 		if (!tmp->fcp_cmnd) {
22559 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22560 					"8356 error pool_alloc memory for HDWQ "
22561 					"%d %s\n",
22562 					lpfc_buf->hdwq_no, __func__);
22563 			kfree(tmp);
22564 			return NULL;
22565 		}
22566 
22567 		tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22568 				sizeof(struct fcp_cmnd32));
22569 
22570 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22571 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22572 	}
22573 
22574 	allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22575 					struct fcp_cmd_rsp_buf,
22576 					list_node);
22577 
22578 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22579 
22580 	return allocated_buf;
22581 }
22582 
22583 /**
22584  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22585  * @phba: The HBA for which this call is being executed.
22586  * @lpfc_buf: IO buf structure with the CMD/RSP buf
22587  *
22588  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22589  *
22590  * Return codes:
22591  *   0 - Success
22592  *   -EINVAL - Error
22593  **/
22594 int
lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22595 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22596 			      struct lpfc_io_buf *lpfc_buf)
22597 {
22598 	int rc = 0;
22599 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22600 	struct fcp_cmd_rsp_buf *tmp = NULL;
22601 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22602 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22603 	unsigned long iflags;
22604 
22605 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22606 
22607 	if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22608 		list_for_each_entry_safe(list_entry, tmp,
22609 					 &lpfc_buf->dma_cmd_rsp_list,
22610 					 list_node) {
22611 			list_move_tail(&list_entry->list_node,
22612 				       buf_list);
22613 		}
22614 	} else {
22615 		rc = -EINVAL;
22616 	}
22617 
22618 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22619 	return rc;
22620 }
22621 
22622 /**
22623  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22624  * @phba: phba object
22625  * @hdwq: hdwq to cleanup cmd rsp buff resources on
22626  *
22627  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22628  *
22629  * Return codes:
22630  *   None
22631  **/
22632 void
lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22633 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22634 			       struct lpfc_sli4_hdw_queue *hdwq)
22635 {
22636 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22637 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22638 	struct fcp_cmd_rsp_buf *tmp = NULL;
22639 	unsigned long iflags;
22640 
22641 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22642 
22643 	/* Free cmd_rsp buf pool */
22644 	list_for_each_entry_safe(list_entry, tmp,
22645 				 buf_list,
22646 				 list_node) {
22647 		list_del(&list_entry->list_node);
22648 		dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22649 			      list_entry->fcp_cmnd,
22650 			      list_entry->fcp_cmd_rsp_dma_handle);
22651 		kfree(list_entry);
22652 	}
22653 
22654 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22655 }
22656 
22657 /**
22658  * lpfc_sli_prep_wqe - Prepare WQE for the command to be posted
22659  * @phba: phba object
22660  * @job: job entry of the command to be posted.
22661  *
22662  * Fill the common fields of the wqe for each of the command.
22663  *
22664  * Return codes:
22665  *	None
22666  **/
22667 void
lpfc_sli_prep_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * job)22668 lpfc_sli_prep_wqe(struct lpfc_hba *phba, struct lpfc_iocbq *job)
22669 {
22670 	u8 cmnd;
22671 	u32 *pcmd;
22672 	u32 if_type = 0;
22673 	u32 abort_tag;
22674 	bool fip;
22675 	struct lpfc_nodelist *ndlp = NULL;
22676 	union lpfc_wqe128 *wqe = &job->wqe;
22677 	u8 command_type = ELS_COMMAND_NON_FIP;
22678 
22679 	fip = test_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
22680 	/* The fcp commands will set command type */
22681 	if (job->cmd_flag &  LPFC_IO_FCP)
22682 		command_type = FCP_COMMAND;
22683 	else if (fip && (job->cmd_flag & LPFC_FIP_ELS_ID_MASK))
22684 		command_type = ELS_COMMAND_FIP;
22685 	else
22686 		command_type = ELS_COMMAND_NON_FIP;
22687 
22688 	abort_tag = job->iotag;
22689 	cmnd = bf_get(wqe_cmnd, &wqe->els_req.wqe_com);
22690 
22691 	switch (cmnd) {
22692 	case CMD_ELS_REQUEST64_WQE:
22693 		ndlp = job->ndlp;
22694 
22695 		if_type = bf_get(lpfc_sli_intf_if_type,
22696 				 &phba->sli4_hba.sli_intf);
22697 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22698 			pcmd = (u32 *)job->cmd_dmabuf->virt;
22699 			if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
22700 				     *pcmd == ELS_CMD_SCR ||
22701 				     *pcmd == ELS_CMD_RDF ||
22702 				     *pcmd == ELS_CMD_EDC ||
22703 				     *pcmd == ELS_CMD_RSCN_XMT ||
22704 				     *pcmd == ELS_CMD_FDISC ||
22705 				     *pcmd == ELS_CMD_LOGO ||
22706 				     *pcmd == ELS_CMD_QFPA ||
22707 				     *pcmd == ELS_CMD_UVEM ||
22708 				     *pcmd == ELS_CMD_PLOGI)) {
22709 				bf_set(els_req64_sp, &wqe->els_req, 1);
22710 				bf_set(els_req64_sid, &wqe->els_req,
22711 				       job->vport->fc_myDID);
22712 
22713 				if ((*pcmd == ELS_CMD_FLOGI) &&
22714 				    !(phba->fc_topology ==
22715 				      LPFC_TOPOLOGY_LOOP))
22716 					bf_set(els_req64_sid, &wqe->els_req, 0);
22717 
22718 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
22719 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22720 				       phba->vpi_ids[job->vport->vpi]);
22721 			} else if (pcmd) {
22722 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
22723 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22724 				       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22725 			}
22726 		}
22727 
22728 		bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
22729 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22730 
22731 		bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
22732 		bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
22733 		bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
22734 		bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22735 		bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
22736 		break;
22737 	case CMD_XMIT_ELS_RSP64_WQE:
22738 		ndlp = job->ndlp;
22739 
22740 		/* word4 */
22741 		wqe->xmit_els_rsp.word4 = 0;
22742 
22743 		if_type = bf_get(lpfc_sli_intf_if_type,
22744 				 &phba->sli4_hba.sli_intf);
22745 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22746 			if (test_bit(FC_PT2PT, &job->vport->fc_flag)) {
22747 				bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22748 				bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22749 				       job->vport->fc_myDID);
22750 				if (job->vport->fc_myDID == Fabric_DID) {
22751 					bf_set(wqe_els_did,
22752 					       &wqe->xmit_els_rsp.wqe_dest, 0);
22753 				}
22754 			}
22755 		}
22756 
22757 		bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
22758 		bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
22759 		bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
22760 		bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
22761 		       LPFC_WQE_LENLOC_WORD3);
22762 		bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
22763 
22764 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
22765 			bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22766 			bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22767 			       job->vport->fc_myDID);
22768 			bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
22769 		}
22770 
22771 		if (phba->sli_rev == LPFC_SLI_REV4) {
22772 			bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
22773 			       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22774 
22775 			if (bf_get(wqe_ct, &wqe->xmit_els_rsp.wqe_com))
22776 				bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
22777 				       phba->vpi_ids[job->vport->vpi]);
22778 		}
22779 		command_type = OTHER_COMMAND;
22780 		break;
22781 	case CMD_GEN_REQUEST64_WQE:
22782 		/* Word 10 */
22783 		bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
22784 		bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
22785 		bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
22786 		bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22787 		bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
22788 		command_type = OTHER_COMMAND;
22789 		break;
22790 	case CMD_XMIT_SEQUENCE64_WQE:
22791 		if (phba->link_flag & LS_LOOPBACK_MODE)
22792 			bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
22793 
22794 		wqe->xmit_sequence.rsvd3 = 0;
22795 		bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
22796 		bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
22797 		bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
22798 		       LPFC_WQE_IOD_WRITE);
22799 		bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
22800 		       LPFC_WQE_LENLOC_WORD12);
22801 		bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
22802 		command_type = OTHER_COMMAND;
22803 		break;
22804 	case CMD_XMIT_BLS_RSP64_WQE:
22805 		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
22806 		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
22807 		bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
22808 		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
22809 		       phba->vpi_ids[phba->pport->vpi]);
22810 		bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
22811 		bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
22812 		       LPFC_WQE_LENLOC_NONE);
22813 		/* Overwrite the pre-set comnd type with OTHER_COMMAND */
22814 		command_type = OTHER_COMMAND;
22815 		break;
22816 	case CMD_FCP_ICMND64_WQE:	/* task mgmt commands */
22817 	case CMD_ABORT_XRI_WQE:		/* abort iotag */
22818 	case CMD_SEND_FRAME:		/* mds loopback */
22819 		/* cases already formatted for sli4 wqe - no chgs necessary */
22820 		return;
22821 	default:
22822 		dump_stack();
22823 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
22824 				"6207 Invalid command 0x%x\n",
22825 				cmnd);
22826 		break;
22827 	}
22828 
22829 	wqe->generic.wqe_com.abort_tag = abort_tag;
22830 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, job->iotag);
22831 	bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
22832 	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
22833 }
22834