xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_sli.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2023 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/slab.h>
29 #include <linux/lockdep.h>
30 
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
36 #include <scsi/fc/fc_fs.h>
37 #include <linux/crash_dump.h>
38 #ifdef CONFIG_X86
39 #include <asm/set_memory.h>
40 #endif
41 
42 #include "lpfc_hw4.h"
43 #include "lpfc_hw.h"
44 #include "lpfc_sli.h"
45 #include "lpfc_sli4.h"
46 #include "lpfc_nl.h"
47 #include "lpfc_disc.h"
48 #include "lpfc.h"
49 #include "lpfc_scsi.h"
50 #include "lpfc_nvme.h"
51 #include "lpfc_crtn.h"
52 #include "lpfc_logmsg.h"
53 #include "lpfc_compat.h"
54 #include "lpfc_debugfs.h"
55 #include "lpfc_vport.h"
56 #include "lpfc_version.h"
57 
58 /* There are only four IOCB completion types. */
59 typedef enum _lpfc_iocb_type {
60 	LPFC_UNKNOWN_IOCB,
61 	LPFC_UNSOL_IOCB,
62 	LPFC_SOL_IOCB,
63 	LPFC_ABORT_IOCB
64 } lpfc_iocb_type;
65 
66 
67 /* Provide function prototypes local to this module. */
68 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
69 				  uint32_t);
70 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
71 			      uint8_t *, uint32_t *);
72 static struct lpfc_iocbq *
73 lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
74 				  struct lpfc_iocbq *rspiocbq);
75 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 				      struct hbq_dmabuf *);
77 static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78 					  struct hbq_dmabuf *dmabuf);
79 static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
80 				   struct lpfc_queue *cq, struct lpfc_cqe *cqe);
81 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
82 				       int);
83 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
84 				     struct lpfc_queue *eq,
85 				     struct lpfc_eqe *eqe,
86 				     enum lpfc_poll_mode poll_mode);
87 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
88 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
89 static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);
90 static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,
91 				    struct lpfc_queue *cq,
92 				    struct lpfc_cqe *cqe);
93 static uint16_t lpfc_wqe_bpl2sgl(struct lpfc_hba *phba,
94 				 struct lpfc_iocbq *pwqeq,
95 				 struct lpfc_sglq *sglq);
96 
97 union lpfc_wqe128 lpfc_iread_cmd_template;
98 union lpfc_wqe128 lpfc_iwrite_cmd_template;
99 union lpfc_wqe128 lpfc_icmnd_cmd_template;
100 
101 /* Setup WQE templates for IOs */
lpfc_wqe_cmd_template(void)102 void lpfc_wqe_cmd_template(void)
103 {
104 	union lpfc_wqe128 *wqe;
105 
106 	/* IREAD template */
107 	wqe = &lpfc_iread_cmd_template;
108 	memset(wqe, 0, sizeof(union lpfc_wqe128));
109 
110 	/* Word 0, 1, 2 - BDE is variable */
111 
112 	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
113 
114 	/* Word 4 - total_xfer_len is variable */
115 
116 	/* Word 5 - is zero */
117 
118 	/* Word 6 - ctxt_tag, xri_tag is variable */
119 
120 	/* Word 7 */
121 	bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
122 	bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
123 	bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
124 	bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
125 
126 	/* Word 8 - abort_tag is variable */
127 
128 	/* Word 9  - reqtag is variable */
129 
130 	/* Word 10 - dbde, wqes is variable */
131 	bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
132 	bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
133 	bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
134 	bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
135 	bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
136 
137 	/* Word 11 - pbde is variable */
138 	bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, COMMAND_DATA_IN);
139 	bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
140 	bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
141 
142 	/* Word 12 - is zero */
143 
144 	/* Word 13, 14, 15 - PBDE is variable */
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 - pbde is variable */
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 	bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
181 
182 	/* Word 12 - is zero */
183 
184 	/* Word 13, 14, 15 - PBDE is variable */
185 
186 	/* ICMND template */
187 	wqe = &lpfc_icmnd_cmd_template;
188 	memset(wqe, 0, sizeof(union lpfc_wqe128));
189 
190 	/* Word 0, 1, 2 - BDE is variable */
191 
192 	/* Word 3 - payload_offset_len is variable */
193 
194 	/* Word 4, 5 - is zero */
195 
196 	/* Word 6 - ctxt_tag, xri_tag is variable */
197 
198 	/* Word 7 */
199 	bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
200 	bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
201 	bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
202 	bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
203 
204 	/* Word 8 - abort_tag is variable */
205 
206 	/* Word 9  - reqtag is variable */
207 
208 	/* Word 10 - dbde, wqes is variable */
209 	bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
210 	bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
211 	bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
212 	bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
213 	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
214 
215 	/* Word 11 */
216 	bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, COMMAND_DATA_IN);
217 	bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
218 	bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
219 
220 	/* Word 12, 13, 14, 15 - is zero */
221 }
222 
223 #if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
224 /**
225  * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
226  * @srcp: Source memory pointer.
227  * @destp: Destination memory pointer.
228  * @cnt: Number of words required to be copied.
229  *       Must be a multiple of sizeof(uint64_t)
230  *
231  * This function is used for copying data between driver memory
232  * and the SLI WQ. This function also changes the endianness
233  * of each word if native endianness is different from SLI
234  * endianness. This function can be called with or without
235  * lock.
236  **/
237 static void
lpfc_sli4_pcimem_bcopy(void * srcp,void * destp,uint32_t cnt)238 lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
239 {
240 	uint64_t *src = srcp;
241 	uint64_t *dest = destp;
242 	int i;
243 
244 	for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
245 		*dest++ = *src++;
246 }
247 #else
248 #define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
249 #endif
250 
251 /**
252  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
253  * @q: The Work Queue to operate on.
254  * @wqe: The work Queue Entry to put on the Work queue.
255  *
256  * This routine will copy the contents of @wqe to the next available entry on
257  * the @q. This function will then ring the Work Queue Doorbell to signal the
258  * HBA to start processing the Work Queue Entry. This function returns 0 if
259  * successful. If no entries are available on @q then this function will return
260  * -ENOMEM.
261  * The caller is expected to hold the hbalock when calling this routine.
262  **/
263 static int
lpfc_sli4_wq_put(struct lpfc_queue * q,union lpfc_wqe128 * wqe)264 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
265 {
266 	union lpfc_wqe *temp_wqe;
267 	struct lpfc_register doorbell;
268 	uint32_t host_index;
269 	uint32_t idx;
270 	uint32_t i = 0;
271 	uint8_t *tmp;
272 	u32 if_type;
273 
274 	/* sanity check on queue memory */
275 	if (unlikely(!q))
276 		return -ENOMEM;
277 
278 	temp_wqe = lpfc_sli4_qe(q, q->host_index);
279 
280 	/* If the host has not yet processed the next entry then we are done */
281 	idx = ((q->host_index + 1) % q->entry_count);
282 	if (idx == q->hba_index) {
283 		q->WQ_overflow++;
284 		return -EBUSY;
285 	}
286 	q->WQ_posted++;
287 	/* set consumption flag every once in a while */
288 	if (!((q->host_index + 1) % q->notify_interval))
289 		bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
290 	else
291 		bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
292 	if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
293 		bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
294 	lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
295 	if (q->dpp_enable && q->phba->cfg_enable_dpp) {
296 		/* write to DPP aperture taking advatage of Combined Writes */
297 		tmp = (uint8_t *)temp_wqe;
298 #ifdef __raw_writeq
299 		for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
300 			__raw_writeq(*((uint64_t *)(tmp + i)),
301 					q->dpp_regaddr + i);
302 #else
303 		for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
304 			__raw_writel(*((uint32_t *)(tmp + i)),
305 					q->dpp_regaddr + i);
306 #endif
307 	}
308 	/* ensure WQE bcopy and DPP flushed before doorbell write */
309 	wmb();
310 
311 	/* Update the host index before invoking device */
312 	host_index = q->host_index;
313 
314 	q->host_index = idx;
315 
316 	/* Ring Doorbell */
317 	doorbell.word0 = 0;
318 	if (q->db_format == LPFC_DB_LIST_FORMAT) {
319 		if (q->dpp_enable && q->phba->cfg_enable_dpp) {
320 			bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
321 			bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
322 			bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
323 			    q->dpp_id);
324 			bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
325 			    q->queue_id);
326 		} else {
327 			bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
328 			bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
329 
330 			/* Leave bits <23:16> clear for if_type 6 dpp */
331 			if_type = bf_get(lpfc_sli_intf_if_type,
332 					 &q->phba->sli4_hba.sli_intf);
333 			if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
334 				bf_set(lpfc_wq_db_list_fm_index, &doorbell,
335 				       host_index);
336 		}
337 	} else if (q->db_format == LPFC_DB_RING_FORMAT) {
338 		bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
339 		bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
340 	} else {
341 		return -EINVAL;
342 	}
343 	writel(doorbell.word0, q->db_regaddr);
344 
345 	return 0;
346 }
347 
348 /**
349  * lpfc_sli4_wq_release - Updates internal hba index for WQ
350  * @q: The Work Queue to operate on.
351  * @index: The index to advance the hba index to.
352  *
353  * This routine will update the HBA index of a queue to reflect consumption of
354  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
355  * an entry the host calls this function to update the queue's internal
356  * pointers.
357  **/
358 static void
lpfc_sli4_wq_release(struct lpfc_queue * q,uint32_t index)359 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
360 {
361 	/* sanity check on queue memory */
362 	if (unlikely(!q))
363 		return;
364 
365 	q->hba_index = index;
366 }
367 
368 /**
369  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
370  * @q: The Mailbox Queue to operate on.
371  * @mqe: The Mailbox Queue Entry to put on the Work queue.
372  *
373  * This routine will copy the contents of @mqe to the next available entry on
374  * the @q. This function will then ring the Work Queue Doorbell to signal the
375  * HBA to start processing the Work Queue Entry. This function returns 0 if
376  * successful. If no entries are available on @q then this function will return
377  * -ENOMEM.
378  * The caller is expected to hold the hbalock when calling this routine.
379  **/
380 static uint32_t
lpfc_sli4_mq_put(struct lpfc_queue * q,struct lpfc_mqe * mqe)381 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
382 {
383 	struct lpfc_mqe *temp_mqe;
384 	struct lpfc_register doorbell;
385 
386 	/* sanity check on queue memory */
387 	if (unlikely(!q))
388 		return -ENOMEM;
389 	temp_mqe = lpfc_sli4_qe(q, q->host_index);
390 
391 	/* If the host has not yet processed the next entry then we are done */
392 	if (((q->host_index + 1) % q->entry_count) == q->hba_index)
393 		return -ENOMEM;
394 	lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
395 	/* Save off the mailbox pointer for completion */
396 	q->phba->mbox = (MAILBOX_t *)temp_mqe;
397 
398 	/* Update the host index before invoking device */
399 	q->host_index = ((q->host_index + 1) % q->entry_count);
400 
401 	/* Ring Doorbell */
402 	doorbell.word0 = 0;
403 	bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
404 	bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
405 	writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
406 	return 0;
407 }
408 
409 /**
410  * lpfc_sli4_mq_release - Updates internal hba index for MQ
411  * @q: The Mailbox Queue to operate on.
412  *
413  * This routine will update the HBA index of a queue to reflect consumption of
414  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
415  * an entry the host calls this function to update the queue's internal
416  * pointers. This routine returns the number of entries that were consumed by
417  * the HBA.
418  **/
419 static uint32_t
lpfc_sli4_mq_release(struct lpfc_queue * q)420 lpfc_sli4_mq_release(struct lpfc_queue *q)
421 {
422 	/* sanity check on queue memory */
423 	if (unlikely(!q))
424 		return 0;
425 
426 	/* Clear the mailbox pointer for completion */
427 	q->phba->mbox = NULL;
428 	q->hba_index = ((q->hba_index + 1) % q->entry_count);
429 	return 1;
430 }
431 
432 /**
433  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
434  * @q: The Event Queue to get the first valid EQE from
435  *
436  * This routine will get the first valid Event Queue Entry from @q, update
437  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
438  * the Queue (no more work to do), or the Queue is full of EQEs that have been
439  * processed, but not popped back to the HBA then this routine will return NULL.
440  **/
441 static struct lpfc_eqe *
lpfc_sli4_eq_get(struct lpfc_queue * q)442 lpfc_sli4_eq_get(struct lpfc_queue *q)
443 {
444 	struct lpfc_eqe *eqe;
445 
446 	/* sanity check on queue memory */
447 	if (unlikely(!q))
448 		return NULL;
449 	eqe = lpfc_sli4_qe(q, q->host_index);
450 
451 	/* If the next EQE is not valid then we are done */
452 	if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
453 		return NULL;
454 
455 	/*
456 	 * insert barrier for instruction interlock : data from the hardware
457 	 * must have the valid bit checked before it can be copied and acted
458 	 * upon. Speculative instructions were allowing a bcopy at the start
459 	 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
460 	 * after our return, to copy data before the valid bit check above
461 	 * was done. As such, some of the copied data was stale. The barrier
462 	 * ensures the check is before any data is copied.
463 	 */
464 	mb();
465 	return eqe;
466 }
467 
468 /**
469  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
470  * @q: The Event Queue to disable interrupts
471  *
472  **/
473 void
lpfc_sli4_eq_clr_intr(struct lpfc_queue * q)474 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
475 {
476 	struct lpfc_register doorbell;
477 
478 	doorbell.word0 = 0;
479 	bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
480 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
481 	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
482 		(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
483 	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
484 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
485 }
486 
487 /**
488  * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
489  * @q: The Event Queue to disable interrupts
490  *
491  **/
492 void
lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue * q)493 lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
494 {
495 	struct lpfc_register doorbell;
496 
497 	doorbell.word0 = 0;
498 	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
499 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
500 }
501 
502 /**
503  * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
504  * @phba: adapter with EQ
505  * @q: The Event Queue that the host has completed processing for.
506  * @count: Number of elements that have been consumed
507  * @arm: Indicates whether the host wants to arms this CQ.
508  *
509  * This routine will notify the HBA, by ringing the doorbell, that count
510  * number of EQEs have been processed. The @arm parameter indicates whether
511  * the queue should be rearmed when ringing the doorbell.
512  **/
513 void
lpfc_sli4_write_eq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)514 lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
515 		     uint32_t count, bool arm)
516 {
517 	struct lpfc_register doorbell;
518 
519 	/* sanity check on queue memory */
520 	if (unlikely(!q || (count == 0 && !arm)))
521 		return;
522 
523 	/* ring doorbell for number popped */
524 	doorbell.word0 = 0;
525 	if (arm) {
526 		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
527 		bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
528 	}
529 	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
530 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
531 	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
532 			(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
533 	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
534 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
535 	/* PCI read to flush PCI pipeline on re-arming for INTx mode */
536 	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
537 		readl(q->phba->sli4_hba.EQDBregaddr);
538 }
539 
540 /**
541  * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
542  * @phba: adapter with EQ
543  * @q: The Event Queue that the host has completed processing for.
544  * @count: Number of elements that have been consumed
545  * @arm: Indicates whether the host wants to arms this CQ.
546  *
547  * This routine will notify the HBA, by ringing the doorbell, that count
548  * number of EQEs have been processed. The @arm parameter indicates whether
549  * the queue should be rearmed when ringing the doorbell.
550  **/
551 void
lpfc_sli4_if6_write_eq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)552 lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
553 			  uint32_t count, bool arm)
554 {
555 	struct lpfc_register doorbell;
556 
557 	/* sanity check on queue memory */
558 	if (unlikely(!q || (count == 0 && !arm)))
559 		return;
560 
561 	/* ring doorbell for number popped */
562 	doorbell.word0 = 0;
563 	if (arm)
564 		bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
565 	bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
566 	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
567 	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
568 	/* PCI read to flush PCI pipeline on re-arming for INTx mode */
569 	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
570 		readl(q->phba->sli4_hba.EQDBregaddr);
571 }
572 
573 static void
__lpfc_sli4_consume_eqe(struct lpfc_hba * phba,struct lpfc_queue * eq,struct lpfc_eqe * eqe)574 __lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
575 			struct lpfc_eqe *eqe)
576 {
577 	if (!phba->sli4_hba.pc_sli4_params.eqav)
578 		bf_set_le32(lpfc_eqe_valid, eqe, 0);
579 
580 	eq->host_index = ((eq->host_index + 1) % eq->entry_count);
581 
582 	/* if the index wrapped around, toggle the valid bit */
583 	if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
584 		eq->qe_valid = (eq->qe_valid) ? 0 : 1;
585 }
586 
587 static void
lpfc_sli4_eqcq_flush(struct lpfc_hba * phba,struct lpfc_queue * eq)588 lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
589 {
590 	struct lpfc_eqe *eqe = NULL;
591 	u32 eq_count = 0, cq_count = 0;
592 	struct lpfc_cqe *cqe = NULL;
593 	struct lpfc_queue *cq = NULL, *childq = NULL;
594 	int cqid = 0;
595 
596 	/* walk all the EQ entries and drop on the floor */
597 	eqe = lpfc_sli4_eq_get(eq);
598 	while (eqe) {
599 		/* Get the reference to the corresponding CQ */
600 		cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
601 		cq = NULL;
602 
603 		list_for_each_entry(childq, &eq->child_list, list) {
604 			if (childq->queue_id == cqid) {
605 				cq = childq;
606 				break;
607 			}
608 		}
609 		/* If CQ is valid, iterate through it and drop all the CQEs */
610 		if (cq) {
611 			cqe = lpfc_sli4_cq_get(cq);
612 			while (cqe) {
613 				__lpfc_sli4_consume_cqe(phba, cq, cqe);
614 				cq_count++;
615 				cqe = lpfc_sli4_cq_get(cq);
616 			}
617 			/* Clear and re-arm the CQ */
618 			phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
619 			    LPFC_QUEUE_REARM);
620 			cq_count = 0;
621 		}
622 		__lpfc_sli4_consume_eqe(phba, eq, eqe);
623 		eq_count++;
624 		eqe = lpfc_sli4_eq_get(eq);
625 	}
626 
627 	/* Clear and re-arm the EQ */
628 	phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
629 }
630 
631 static int
lpfc_sli4_process_eq(struct lpfc_hba * phba,struct lpfc_queue * eq,u8 rearm,enum lpfc_poll_mode poll_mode)632 lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
633 		     u8 rearm, enum lpfc_poll_mode poll_mode)
634 {
635 	struct lpfc_eqe *eqe;
636 	int count = 0, consumed = 0;
637 
638 	if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
639 		goto rearm_and_exit;
640 
641 	eqe = lpfc_sli4_eq_get(eq);
642 	while (eqe) {
643 		lpfc_sli4_hba_handle_eqe(phba, eq, eqe, poll_mode);
644 		__lpfc_sli4_consume_eqe(phba, eq, eqe);
645 
646 		consumed++;
647 		if (!(++count % eq->max_proc_limit))
648 			break;
649 
650 		if (!(count % eq->notify_interval)) {
651 			phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
652 							LPFC_QUEUE_NOARM);
653 			consumed = 0;
654 		}
655 
656 		eqe = lpfc_sli4_eq_get(eq);
657 	}
658 	eq->EQ_processed += count;
659 
660 	/* Track the max number of EQEs processed in 1 intr */
661 	if (count > eq->EQ_max_eqe)
662 		eq->EQ_max_eqe = count;
663 
664 	xchg(&eq->queue_claimed, 0);
665 
666 rearm_and_exit:
667 	/* Always clear the EQ. */
668 	phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
669 
670 	return count;
671 }
672 
673 /**
674  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
675  * @q: The Completion Queue to get the first valid CQE from
676  *
677  * This routine will get the first valid Completion Queue Entry from @q, update
678  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
679  * the Queue (no more work to do), or the Queue is full of CQEs that have been
680  * processed, but not popped back to the HBA then this routine will return NULL.
681  **/
682 static struct lpfc_cqe *
lpfc_sli4_cq_get(struct lpfc_queue * q)683 lpfc_sli4_cq_get(struct lpfc_queue *q)
684 {
685 	struct lpfc_cqe *cqe;
686 
687 	/* sanity check on queue memory */
688 	if (unlikely(!q))
689 		return NULL;
690 	cqe = lpfc_sli4_qe(q, q->host_index);
691 
692 	/* If the next CQE is not valid then we are done */
693 	if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
694 		return NULL;
695 
696 	/*
697 	 * insert barrier for instruction interlock : data from the hardware
698 	 * must have the valid bit checked before it can be copied and acted
699 	 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
700 	 * instructions allowing action on content before valid bit checked,
701 	 * add barrier here as well. May not be needed as "content" is a
702 	 * single 32-bit entity here (vs multi word structure for cq's).
703 	 */
704 	mb();
705 	return cqe;
706 }
707 
708 static void
__lpfc_sli4_consume_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)709 __lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
710 			struct lpfc_cqe *cqe)
711 {
712 	if (!phba->sli4_hba.pc_sli4_params.cqav)
713 		bf_set_le32(lpfc_cqe_valid, cqe, 0);
714 
715 	cq->host_index = ((cq->host_index + 1) % cq->entry_count);
716 
717 	/* if the index wrapped around, toggle the valid bit */
718 	if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
719 		cq->qe_valid = (cq->qe_valid) ? 0 : 1;
720 }
721 
722 /**
723  * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
724  * @phba: the adapter with the CQ
725  * @q: The Completion Queue that the host has completed processing for.
726  * @count: the number of elements that were consumed
727  * @arm: Indicates whether the host wants to arms this CQ.
728  *
729  * This routine will notify the HBA, by ringing the doorbell, that the
730  * CQEs have been processed. The @arm parameter specifies whether the
731  * queue should be rearmed when ringing the doorbell.
732  **/
733 void
lpfc_sli4_write_cq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)734 lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
735 		     uint32_t count, bool arm)
736 {
737 	struct lpfc_register doorbell;
738 
739 	/* sanity check on queue memory */
740 	if (unlikely(!q || (count == 0 && !arm)))
741 		return;
742 
743 	/* ring doorbell for number popped */
744 	doorbell.word0 = 0;
745 	if (arm)
746 		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
747 	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
748 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
749 	bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
750 			(q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
751 	bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
752 	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
753 }
754 
755 /**
756  * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
757  * @phba: the adapter with the CQ
758  * @q: The Completion Queue that the host has completed processing for.
759  * @count: the number of elements that were consumed
760  * @arm: Indicates whether the host wants to arms this CQ.
761  *
762  * This routine will notify the HBA, by ringing the doorbell, that the
763  * CQEs have been processed. The @arm parameter specifies whether the
764  * queue should be rearmed when ringing the doorbell.
765  **/
766 void
lpfc_sli4_if6_write_cq_db(struct lpfc_hba * phba,struct lpfc_queue * q,uint32_t count,bool arm)767 lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
768 			 uint32_t count, bool arm)
769 {
770 	struct lpfc_register doorbell;
771 
772 	/* sanity check on queue memory */
773 	if (unlikely(!q || (count == 0 && !arm)))
774 		return;
775 
776 	/* ring doorbell for number popped */
777 	doorbell.word0 = 0;
778 	if (arm)
779 		bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
780 	bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
781 	bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
782 	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
783 }
784 
785 /*
786  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
787  *
788  * This routine will copy the contents of @wqe to the next available entry on
789  * the @q. This function will then ring the Receive Queue Doorbell to signal the
790  * HBA to start processing the Receive Queue Entry. This function returns the
791  * index that the rqe was copied to if successful. If no entries are available
792  * on @q then this function will return -ENOMEM.
793  * The caller is expected to hold the hbalock when calling this routine.
794  **/
795 int
lpfc_sli4_rq_put(struct lpfc_queue * hq,struct lpfc_queue * dq,struct lpfc_rqe * hrqe,struct lpfc_rqe * drqe)796 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
797 		 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
798 {
799 	struct lpfc_rqe *temp_hrqe;
800 	struct lpfc_rqe *temp_drqe;
801 	struct lpfc_register doorbell;
802 	int hq_put_index;
803 	int dq_put_index;
804 
805 	/* sanity check on queue memory */
806 	if (unlikely(!hq) || unlikely(!dq))
807 		return -ENOMEM;
808 	hq_put_index = hq->host_index;
809 	dq_put_index = dq->host_index;
810 	temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
811 	temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
812 
813 	if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
814 		return -EINVAL;
815 	if (hq_put_index != dq_put_index)
816 		return -EINVAL;
817 	/* If the host has not yet processed the next entry then we are done */
818 	if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
819 		return -EBUSY;
820 	lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
821 	lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
822 
823 	/* Update the host index to point to the next slot */
824 	hq->host_index = ((hq_put_index + 1) % hq->entry_count);
825 	dq->host_index = ((dq_put_index + 1) % dq->entry_count);
826 	hq->RQ_buf_posted++;
827 
828 	/* Ring The Header Receive Queue Doorbell */
829 	if (!(hq->host_index % hq->notify_interval)) {
830 		doorbell.word0 = 0;
831 		if (hq->db_format == LPFC_DB_RING_FORMAT) {
832 			bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
833 			       hq->notify_interval);
834 			bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
835 		} else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
836 			bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
837 			       hq->notify_interval);
838 			bf_set(lpfc_rq_db_list_fm_index, &doorbell,
839 			       hq->host_index);
840 			bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
841 		} else {
842 			return -EINVAL;
843 		}
844 		writel(doorbell.word0, hq->db_regaddr);
845 	}
846 	return hq_put_index;
847 }
848 
849 /*
850  * lpfc_sli4_rq_release - Updates internal hba index for RQ
851  *
852  * This routine will update the HBA index of a queue to reflect consumption of
853  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
854  * consumed an entry the host calls this function to update the queue's
855  * internal pointers. This routine returns the number of entries that were
856  * consumed by the HBA.
857  **/
858 static uint32_t
lpfc_sli4_rq_release(struct lpfc_queue * hq,struct lpfc_queue * dq)859 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
860 {
861 	/* sanity check on queue memory */
862 	if (unlikely(!hq) || unlikely(!dq))
863 		return 0;
864 
865 	if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
866 		return 0;
867 	hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
868 	dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
869 	return 1;
870 }
871 
872 /**
873  * lpfc_cmd_iocb - Get next command iocb entry in the ring
874  * @phba: Pointer to HBA context object.
875  * @pring: Pointer to driver SLI ring object.
876  *
877  * This function returns pointer to next command iocb entry
878  * in the command ring. The caller must hold hbalock to prevent
879  * other threads consume the next command iocb.
880  * SLI-2/SLI-3 provide different sized iocbs.
881  **/
882 static inline IOCB_t *
lpfc_cmd_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)883 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
884 {
885 	return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
886 			   pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
887 }
888 
889 /**
890  * lpfc_resp_iocb - Get next response iocb entry in the ring
891  * @phba: Pointer to HBA context object.
892  * @pring: Pointer to driver SLI ring object.
893  *
894  * This function returns pointer to next response iocb entry
895  * in the response ring. The caller must hold hbalock to make sure
896  * that no other thread consume the next response iocb.
897  * SLI-2/SLI-3 provide different sized iocbs.
898  **/
899 static inline IOCB_t *
lpfc_resp_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)900 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
901 {
902 	return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
903 			   pring->sli.sli3.rspidx * phba->iocb_rsp_size);
904 }
905 
906 /**
907  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
908  * @phba: Pointer to HBA context object.
909  *
910  * This function is called with hbalock held. This function
911  * allocates a new driver iocb object from the iocb pool. If the
912  * allocation is successful, it returns pointer to the newly
913  * allocated iocb object else it returns NULL.
914  **/
915 struct lpfc_iocbq *
__lpfc_sli_get_iocbq(struct lpfc_hba * phba)916 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
917 {
918 	struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
919 	struct lpfc_iocbq * iocbq = NULL;
920 
921 	lockdep_assert_held(&phba->hbalock);
922 
923 	list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
924 	if (iocbq)
925 		phba->iocb_cnt++;
926 	if (phba->iocb_cnt > phba->iocb_max)
927 		phba->iocb_max = phba->iocb_cnt;
928 	return iocbq;
929 }
930 
931 /**
932  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
933  * @phba: Pointer to HBA context object.
934  * @xritag: XRI value.
935  *
936  * This function clears the sglq pointer from the array of active
937  * sglq's. The xritag that is passed in is used to index into the
938  * array. Before the xritag can be used it needs to be adjusted
939  * by subtracting the xribase.
940  *
941  * Returns sglq ponter = success, NULL = Failure.
942  **/
943 struct lpfc_sglq *
__lpfc_clear_active_sglq(struct lpfc_hba * phba,uint16_t xritag)944 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
945 {
946 	struct lpfc_sglq *sglq;
947 
948 	sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
949 	phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
950 	return sglq;
951 }
952 
953 /**
954  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
955  * @phba: Pointer to HBA context object.
956  * @xritag: XRI value.
957  *
958  * This function returns the sglq pointer from the array of active
959  * sglq's. The xritag that is passed in is used to index into the
960  * array. Before the xritag can be used it needs to be adjusted
961  * by subtracting the xribase.
962  *
963  * Returns sglq ponter = success, NULL = Failure.
964  **/
965 struct lpfc_sglq *
__lpfc_get_active_sglq(struct lpfc_hba * phba,uint16_t xritag)966 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
967 {
968 	struct lpfc_sglq *sglq;
969 
970 	sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
971 	return sglq;
972 }
973 
974 /**
975  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
976  * @phba: Pointer to HBA context object.
977  * @xritag: xri used in this exchange.
978  * @rrq: The RRQ to be cleared.
979  *
980  **/
981 void
lpfc_clr_rrq_active(struct lpfc_hba * phba,uint16_t xritag,struct lpfc_node_rrq * rrq)982 lpfc_clr_rrq_active(struct lpfc_hba *phba,
983 		    uint16_t xritag,
984 		    struct lpfc_node_rrq *rrq)
985 {
986 	struct lpfc_nodelist *ndlp = NULL;
987 
988 	/* Lookup did to verify if did is still active on this vport */
989 	if (rrq->vport)
990 		ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
991 
992 	if (!ndlp)
993 		goto out;
994 
995 	if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
996 		rrq->send_rrq = 0;
997 		rrq->xritag = 0;
998 		rrq->rrq_stop_time = 0;
999 	}
1000 out:
1001 	mempool_free(rrq, phba->rrq_pool);
1002 }
1003 
1004 /**
1005  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
1006  * @phba: Pointer to HBA context object.
1007  *
1008  * This function is called with hbalock held. This function
1009  * Checks if stop_time (ratov from setting rrq active) has
1010  * been reached, if it has and the send_rrq flag is set then
1011  * it will call lpfc_send_rrq. If the send_rrq flag is not set
1012  * then it will just call the routine to clear the rrq and
1013  * free the rrq resource.
1014  * The timer is set to the next rrq that is going to expire before
1015  * leaving the routine.
1016  *
1017  **/
1018 void
lpfc_handle_rrq_active(struct lpfc_hba * phba)1019 lpfc_handle_rrq_active(struct lpfc_hba *phba)
1020 {
1021 	struct lpfc_node_rrq *rrq;
1022 	struct lpfc_node_rrq *nextrrq;
1023 	unsigned long next_time;
1024 	unsigned long iflags;
1025 	LIST_HEAD(send_rrq);
1026 
1027 	spin_lock_irqsave(&phba->hbalock, iflags);
1028 	phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1029 	next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1030 	list_for_each_entry_safe(rrq, nextrrq,
1031 				 &phba->active_rrq_list, list) {
1032 		if (time_after(jiffies, rrq->rrq_stop_time))
1033 			list_move(&rrq->list, &send_rrq);
1034 		else if (time_before(rrq->rrq_stop_time, next_time))
1035 			next_time = rrq->rrq_stop_time;
1036 	}
1037 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1038 	if ((!list_empty(&phba->active_rrq_list)) &&
1039 	    (!(phba->pport->load_flag & FC_UNLOADING)))
1040 		mod_timer(&phba->rrq_tmr, next_time);
1041 	list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
1042 		list_del(&rrq->list);
1043 		if (!rrq->send_rrq) {
1044 			/* this call will free the rrq */
1045 			lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1046 		} else if (lpfc_send_rrq(phba, rrq)) {
1047 			/* if we send the rrq then the completion handler
1048 			*  will clear the bit in the xribitmap.
1049 			*/
1050 			lpfc_clr_rrq_active(phba, rrq->xritag,
1051 					    rrq);
1052 		}
1053 	}
1054 }
1055 
1056 /**
1057  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
1058  * @vport: Pointer to vport context object.
1059  * @xri: The xri used in the exchange.
1060  * @did: The targets DID for this exchange.
1061  *
1062  * returns NULL = rrq not found in the phba->active_rrq_list.
1063  *         rrq = rrq for this xri and target.
1064  **/
1065 struct lpfc_node_rrq *
lpfc_get_active_rrq(struct lpfc_vport * vport,uint16_t xri,uint32_t did)1066 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
1067 {
1068 	struct lpfc_hba *phba = vport->phba;
1069 	struct lpfc_node_rrq *rrq;
1070 	struct lpfc_node_rrq *nextrrq;
1071 	unsigned long iflags;
1072 
1073 	if (phba->sli_rev != LPFC_SLI_REV4)
1074 		return NULL;
1075 	spin_lock_irqsave(&phba->hbalock, iflags);
1076 	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1077 		if (rrq->vport == vport && rrq->xritag == xri &&
1078 				rrq->nlp_DID == did){
1079 			list_del(&rrq->list);
1080 			spin_unlock_irqrestore(&phba->hbalock, iflags);
1081 			return rrq;
1082 		}
1083 	}
1084 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1085 	return NULL;
1086 }
1087 
1088 /**
1089  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
1090  * @vport: Pointer to vport context object.
1091  * @ndlp: Pointer to the lpfc_node_list structure.
1092  * If ndlp is NULL Remove all active RRQs for this vport from the
1093  * phba->active_rrq_list and clear the rrq.
1094  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
1095  **/
1096 void
lpfc_cleanup_vports_rrqs(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)1097 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1098 
1099 {
1100 	struct lpfc_hba *phba = vport->phba;
1101 	struct lpfc_node_rrq *rrq;
1102 	struct lpfc_node_rrq *nextrrq;
1103 	unsigned long iflags;
1104 	LIST_HEAD(rrq_list);
1105 
1106 	if (phba->sli_rev != LPFC_SLI_REV4)
1107 		return;
1108 	if (!ndlp) {
1109 		lpfc_sli4_vport_delete_els_xri_aborted(vport);
1110 		lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
1111 	}
1112 	spin_lock_irqsave(&phba->hbalock, iflags);
1113 	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1114 		if (rrq->vport != vport)
1115 			continue;
1116 
1117 		if (!ndlp || ndlp == lpfc_findnode_did(vport, rrq->nlp_DID))
1118 			list_move(&rrq->list, &rrq_list);
1119 
1120 	}
1121 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1122 
1123 	list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1124 		list_del(&rrq->list);
1125 		lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1126 	}
1127 }
1128 
1129 /**
1130  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
1131  * @phba: Pointer to HBA context object.
1132  * @ndlp: Targets nodelist pointer for this exchange.
1133  * @xritag: the xri in the bitmap to test.
1134  *
1135  * This function returns:
1136  * 0 = rrq not active for this xri
1137  * 1 = rrq is valid for this xri.
1138  **/
1139 int
lpfc_test_rrq_active(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,uint16_t xritag)1140 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1141 			uint16_t  xritag)
1142 {
1143 	if (!ndlp)
1144 		return 0;
1145 	if (!ndlp->active_rrqs_xri_bitmap)
1146 		return 0;
1147 	if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1148 		return 1;
1149 	else
1150 		return 0;
1151 }
1152 
1153 /**
1154  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1155  * @phba: Pointer to HBA context object.
1156  * @ndlp: nodelist pointer for this target.
1157  * @xritag: xri used in this exchange.
1158  * @rxid: Remote Exchange ID.
1159  * @send_rrq: Flag used to determine if we should send rrq els cmd.
1160  *
1161  * This function takes the hbalock.
1162  * The active bit is always set in the active rrq xri_bitmap even
1163  * if there is no slot avaiable for the other rrq information.
1164  *
1165  * returns 0 rrq actived for this xri
1166  *         < 0 No memory or invalid ndlp.
1167  **/
1168 int
lpfc_set_rrq_active(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,uint16_t xritag,uint16_t rxid,uint16_t send_rrq)1169 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1170 		    uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
1171 {
1172 	unsigned long iflags;
1173 	struct lpfc_node_rrq *rrq;
1174 	int empty;
1175 
1176 	if (!ndlp)
1177 		return -EINVAL;
1178 
1179 	if (!phba->cfg_enable_rrq)
1180 		return -EINVAL;
1181 
1182 	spin_lock_irqsave(&phba->hbalock, iflags);
1183 	if (phba->pport->load_flag & FC_UNLOADING) {
1184 		phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1185 		goto out;
1186 	}
1187 
1188 	if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1189 		goto out;
1190 
1191 	if (!ndlp->active_rrqs_xri_bitmap)
1192 		goto out;
1193 
1194 	if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1195 		goto out;
1196 
1197 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1198 	rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
1199 	if (!rrq) {
1200 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1201 				"3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1202 				" DID:0x%x Send:%d\n",
1203 				xritag, rxid, ndlp->nlp_DID, send_rrq);
1204 		return -EINVAL;
1205 	}
1206 	if (phba->cfg_enable_rrq == 1)
1207 		rrq->send_rrq = send_rrq;
1208 	else
1209 		rrq->send_rrq = 0;
1210 	rrq->xritag = xritag;
1211 	rrq->rrq_stop_time = jiffies +
1212 				msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1213 	rrq->nlp_DID = ndlp->nlp_DID;
1214 	rrq->vport = ndlp->vport;
1215 	rrq->rxid = rxid;
1216 	spin_lock_irqsave(&phba->hbalock, iflags);
1217 	empty = list_empty(&phba->active_rrq_list);
1218 	list_add_tail(&rrq->list, &phba->active_rrq_list);
1219 	phba->hba_flag |= HBA_RRQ_ACTIVE;
1220 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1221 	if (empty)
1222 		lpfc_worker_wake_up(phba);
1223 	return 0;
1224 out:
1225 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1226 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1227 			"2921 Can't set rrq active xri:0x%x rxid:0x%x"
1228 			" DID:0x%x Send:%d\n",
1229 			xritag, rxid, ndlp->nlp_DID, send_rrq);
1230 	return -EINVAL;
1231 }
1232 
1233 /**
1234  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1235  * @phba: Pointer to HBA context object.
1236  * @piocbq: Pointer to the iocbq.
1237  *
1238  * The driver calls this function with either the nvme ls ring lock
1239  * or the fc els ring lock held depending on the iocb usage.  This function
1240  * gets a new driver sglq object from the sglq list. If the list is not empty
1241  * then it is successful, it returns pointer to the newly allocated sglq
1242  * object else it returns NULL.
1243  **/
1244 static struct lpfc_sglq *
__lpfc_sli_get_els_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1245 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1246 {
1247 	struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1248 	struct lpfc_sglq *sglq = NULL;
1249 	struct lpfc_sglq *start_sglq = NULL;
1250 	struct lpfc_io_buf *lpfc_cmd;
1251 	struct lpfc_nodelist *ndlp;
1252 	int found = 0;
1253 	u8 cmnd;
1254 
1255 	cmnd = get_job_cmnd(phba, piocbq);
1256 
1257 	if (piocbq->cmd_flag & LPFC_IO_FCP) {
1258 		lpfc_cmd = piocbq->io_buf;
1259 		ndlp = lpfc_cmd->rdata->pnode;
1260 	} else  if ((cmnd == CMD_GEN_REQUEST64_CR) &&
1261 			!(piocbq->cmd_flag & LPFC_IO_LIBDFC)) {
1262 		ndlp = piocbq->ndlp;
1263 	} else  if (piocbq->cmd_flag & LPFC_IO_LIBDFC) {
1264 		if (piocbq->cmd_flag & LPFC_IO_LOOPBACK)
1265 			ndlp = NULL;
1266 		else
1267 			ndlp = piocbq->ndlp;
1268 	} else {
1269 		ndlp = piocbq->ndlp;
1270 	}
1271 
1272 	spin_lock(&phba->sli4_hba.sgl_list_lock);
1273 	list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1274 	start_sglq = sglq;
1275 	while (!found) {
1276 		if (!sglq)
1277 			break;
1278 		if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1279 		    test_bit(sglq->sli4_lxritag,
1280 		    ndlp->active_rrqs_xri_bitmap)) {
1281 			/* This xri has an rrq outstanding for this DID.
1282 			 * put it back in the list and get another xri.
1283 			 */
1284 			list_add_tail(&sglq->list, lpfc_els_sgl_list);
1285 			sglq = NULL;
1286 			list_remove_head(lpfc_els_sgl_list, sglq,
1287 						struct lpfc_sglq, list);
1288 			if (sglq == start_sglq) {
1289 				list_add_tail(&sglq->list, lpfc_els_sgl_list);
1290 				sglq = NULL;
1291 				break;
1292 			} else
1293 				continue;
1294 		}
1295 		sglq->ndlp = ndlp;
1296 		found = 1;
1297 		phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1298 		sglq->state = SGL_ALLOCATED;
1299 	}
1300 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
1301 	return sglq;
1302 }
1303 
1304 /**
1305  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1306  * @phba: Pointer to HBA context object.
1307  * @piocbq: Pointer to the iocbq.
1308  *
1309  * This function is called with the sgl_list lock held. This function
1310  * gets a new driver sglq object from the sglq list. If the
1311  * list is not empty then it is successful, it returns pointer to the newly
1312  * allocated sglq object else it returns NULL.
1313  **/
1314 struct lpfc_sglq *
__lpfc_sli_get_nvmet_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1315 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1316 {
1317 	struct list_head *lpfc_nvmet_sgl_list;
1318 	struct lpfc_sglq *sglq = NULL;
1319 
1320 	lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1321 
1322 	lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1323 
1324 	list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1325 	if (!sglq)
1326 		return NULL;
1327 	phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1328 	sglq->state = SGL_ALLOCATED;
1329 	return sglq;
1330 }
1331 
1332 /**
1333  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1334  * @phba: Pointer to HBA context object.
1335  *
1336  * This function is called with no lock held. This function
1337  * allocates a new driver iocb object from the iocb pool. If the
1338  * allocation is successful, it returns pointer to the newly
1339  * allocated iocb object else it returns NULL.
1340  **/
1341 struct lpfc_iocbq *
lpfc_sli_get_iocbq(struct lpfc_hba * phba)1342 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1343 {
1344 	struct lpfc_iocbq * iocbq = NULL;
1345 	unsigned long iflags;
1346 
1347 	spin_lock_irqsave(&phba->hbalock, iflags);
1348 	iocbq = __lpfc_sli_get_iocbq(phba);
1349 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1350 	return iocbq;
1351 }
1352 
1353 /**
1354  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1355  * @phba: Pointer to HBA context object.
1356  * @iocbq: Pointer to driver iocb object.
1357  *
1358  * This function is called to release the driver iocb object
1359  * to the iocb pool. The iotag in the iocb object
1360  * does not change for each use of the iocb object. This function
1361  * clears all other fields of the iocb object when it is freed.
1362  * The sqlq structure that holds the xritag and phys and virtual
1363  * mappings for the scatter gather list is retrieved from the
1364  * active array of sglq. The get of the sglq pointer also clears
1365  * the entry in the array. If the status of the IO indiactes that
1366  * this IO was aborted then the sglq entry it put on the
1367  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1368  * IO has good status or fails for any other reason then the sglq
1369  * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1370  *  asserted held in the code path calling this routine.
1371  **/
1372 static void
__lpfc_sli_release_iocbq_s4(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1373 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1374 {
1375 	struct lpfc_sglq *sglq;
1376 	unsigned long iflag = 0;
1377 	struct lpfc_sli_ring *pring;
1378 
1379 	if (iocbq->sli4_xritag == NO_XRI)
1380 		sglq = NULL;
1381 	else
1382 		sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1383 
1384 
1385 	if (sglq)  {
1386 		if (iocbq->cmd_flag & LPFC_IO_NVMET) {
1387 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1388 					  iflag);
1389 			sglq->state = SGL_FREED;
1390 			sglq->ndlp = NULL;
1391 			list_add_tail(&sglq->list,
1392 				      &phba->sli4_hba.lpfc_nvmet_sgl_list);
1393 			spin_unlock_irqrestore(
1394 				&phba->sli4_hba.sgl_list_lock, iflag);
1395 			goto out;
1396 		}
1397 
1398 		if ((iocbq->cmd_flag & LPFC_EXCHANGE_BUSY) &&
1399 		    (!(unlikely(pci_channel_offline(phba->pcidev)))) &&
1400 		    sglq->state != SGL_XRI_ABORTED) {
1401 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1402 					  iflag);
1403 
1404 			/* Check if we can get a reference on ndlp */
1405 			if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1406 				sglq->ndlp = NULL;
1407 
1408 			list_add(&sglq->list,
1409 				 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1410 			spin_unlock_irqrestore(
1411 				&phba->sli4_hba.sgl_list_lock, iflag);
1412 		} else {
1413 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1414 					  iflag);
1415 			sglq->state = SGL_FREED;
1416 			sglq->ndlp = NULL;
1417 			list_add_tail(&sglq->list,
1418 				      &phba->sli4_hba.lpfc_els_sgl_list);
1419 			spin_unlock_irqrestore(
1420 				&phba->sli4_hba.sgl_list_lock, iflag);
1421 			pring = lpfc_phba_elsring(phba);
1422 			/* Check if TXQ queue needs to be serviced */
1423 			if (pring && (!list_empty(&pring->txq)))
1424 				lpfc_worker_wake_up(phba);
1425 		}
1426 	}
1427 
1428 out:
1429 	/*
1430 	 * Clean all volatile data fields, preserve iotag and node struct.
1431 	 */
1432 	memset_startat(iocbq, 0, wqe);
1433 	iocbq->sli4_lxritag = NO_XRI;
1434 	iocbq->sli4_xritag = NO_XRI;
1435 	iocbq->cmd_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |
1436 			      LPFC_IO_NVME_LS);
1437 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1438 }
1439 
1440 
1441 /**
1442  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1443  * @phba: Pointer to HBA context object.
1444  * @iocbq: Pointer to driver iocb object.
1445  *
1446  * This function is called to release the driver iocb object to the
1447  * iocb pool. The iotag in the iocb object does not change for each
1448  * use of the iocb object. This function clears all other fields of
1449  * the iocb object when it is freed. The hbalock is asserted held in
1450  * the code path calling this routine.
1451  **/
1452 static void
__lpfc_sli_release_iocbq_s3(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1453 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1454 {
1455 
1456 	/*
1457 	 * Clean all volatile data fields, preserve iotag and node struct.
1458 	 */
1459 	memset_startat(iocbq, 0, iocb);
1460 	iocbq->sli4_xritag = NO_XRI;
1461 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1462 }
1463 
1464 /**
1465  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1466  * @phba: Pointer to HBA context object.
1467  * @iocbq: Pointer to driver iocb object.
1468  *
1469  * This function is called with hbalock held to release driver
1470  * iocb object to the iocb pool. The iotag in the iocb object
1471  * does not change for each use of the iocb object. This function
1472  * clears all other fields of the iocb object when it is freed.
1473  **/
1474 static void
__lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1475 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1476 {
1477 	lockdep_assert_held(&phba->hbalock);
1478 
1479 	phba->__lpfc_sli_release_iocbq(phba, iocbq);
1480 	phba->iocb_cnt--;
1481 }
1482 
1483 /**
1484  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1485  * @phba: Pointer to HBA context object.
1486  * @iocbq: Pointer to driver iocb object.
1487  *
1488  * This function is called with no lock held to release the iocb to
1489  * iocb pool.
1490  **/
1491 void
lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1492 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1493 {
1494 	unsigned long iflags;
1495 
1496 	/*
1497 	 * Clean all volatile data fields, preserve iotag and node struct.
1498 	 */
1499 	spin_lock_irqsave(&phba->hbalock, iflags);
1500 	__lpfc_sli_release_iocbq(phba, iocbq);
1501 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1502 }
1503 
1504 /**
1505  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1506  * @phba: Pointer to HBA context object.
1507  * @iocblist: List of IOCBs.
1508  * @ulpstatus: ULP status in IOCB command field.
1509  * @ulpWord4: ULP word-4 in IOCB command field.
1510  *
1511  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1512  * on the list by invoking the complete callback function associated with the
1513  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1514  * fields.
1515  **/
1516 void
lpfc_sli_cancel_iocbs(struct lpfc_hba * phba,struct list_head * iocblist,uint32_t ulpstatus,uint32_t ulpWord4)1517 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1518 		      uint32_t ulpstatus, uint32_t ulpWord4)
1519 {
1520 	struct lpfc_iocbq *piocb;
1521 
1522 	while (!list_empty(iocblist)) {
1523 		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1524 		if (piocb->cmd_cmpl) {
1525 			if (piocb->cmd_flag & LPFC_IO_NVME) {
1526 				lpfc_nvme_cancel_iocb(phba, piocb,
1527 						      ulpstatus, ulpWord4);
1528 			} else {
1529 				if (phba->sli_rev == LPFC_SLI_REV4) {
1530 					bf_set(lpfc_wcqe_c_status,
1531 					       &piocb->wcqe_cmpl, ulpstatus);
1532 					piocb->wcqe_cmpl.parameter = ulpWord4;
1533 				} else {
1534 					piocb->iocb.ulpStatus = ulpstatus;
1535 					piocb->iocb.un.ulpWord[4] = ulpWord4;
1536 				}
1537 				(piocb->cmd_cmpl) (phba, piocb, piocb);
1538 			}
1539 		} else {
1540 			lpfc_sli_release_iocbq(phba, piocb);
1541 		}
1542 	}
1543 	return;
1544 }
1545 
1546 /**
1547  * lpfc_sli_iocb_cmd_type - Get the iocb type
1548  * @iocb_cmnd: iocb command code.
1549  *
1550  * This function is called by ring event handler function to get the iocb type.
1551  * This function translates the iocb command to an iocb command type used to
1552  * decide the final disposition of each completed IOCB.
1553  * The function returns
1554  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1555  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1556  * LPFC_ABORT_IOCB   if it is an abort iocb
1557  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1558  *
1559  * The caller is not required to hold any lock.
1560  **/
1561 static lpfc_iocb_type
lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)1562 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1563 {
1564 	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1565 
1566 	if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1567 		return 0;
1568 
1569 	switch (iocb_cmnd) {
1570 	case CMD_XMIT_SEQUENCE_CR:
1571 	case CMD_XMIT_SEQUENCE_CX:
1572 	case CMD_XMIT_BCAST_CN:
1573 	case CMD_XMIT_BCAST_CX:
1574 	case CMD_ELS_REQUEST_CR:
1575 	case CMD_ELS_REQUEST_CX:
1576 	case CMD_CREATE_XRI_CR:
1577 	case CMD_CREATE_XRI_CX:
1578 	case CMD_GET_RPI_CN:
1579 	case CMD_XMIT_ELS_RSP_CX:
1580 	case CMD_GET_RPI_CR:
1581 	case CMD_FCP_IWRITE_CR:
1582 	case CMD_FCP_IWRITE_CX:
1583 	case CMD_FCP_IREAD_CR:
1584 	case CMD_FCP_IREAD_CX:
1585 	case CMD_FCP_ICMND_CR:
1586 	case CMD_FCP_ICMND_CX:
1587 	case CMD_FCP_TSEND_CX:
1588 	case CMD_FCP_TRSP_CX:
1589 	case CMD_FCP_TRECEIVE_CX:
1590 	case CMD_FCP_AUTO_TRSP_CX:
1591 	case CMD_ADAPTER_MSG:
1592 	case CMD_ADAPTER_DUMP:
1593 	case CMD_XMIT_SEQUENCE64_CR:
1594 	case CMD_XMIT_SEQUENCE64_CX:
1595 	case CMD_XMIT_BCAST64_CN:
1596 	case CMD_XMIT_BCAST64_CX:
1597 	case CMD_ELS_REQUEST64_CR:
1598 	case CMD_ELS_REQUEST64_CX:
1599 	case CMD_FCP_IWRITE64_CR:
1600 	case CMD_FCP_IWRITE64_CX:
1601 	case CMD_FCP_IREAD64_CR:
1602 	case CMD_FCP_IREAD64_CX:
1603 	case CMD_FCP_ICMND64_CR:
1604 	case CMD_FCP_ICMND64_CX:
1605 	case CMD_FCP_TSEND64_CX:
1606 	case CMD_FCP_TRSP64_CX:
1607 	case CMD_FCP_TRECEIVE64_CX:
1608 	case CMD_GEN_REQUEST64_CR:
1609 	case CMD_GEN_REQUEST64_CX:
1610 	case CMD_XMIT_ELS_RSP64_CX:
1611 	case DSSCMD_IWRITE64_CR:
1612 	case DSSCMD_IWRITE64_CX:
1613 	case DSSCMD_IREAD64_CR:
1614 	case DSSCMD_IREAD64_CX:
1615 	case CMD_SEND_FRAME:
1616 		type = LPFC_SOL_IOCB;
1617 		break;
1618 	case CMD_ABORT_XRI_CN:
1619 	case CMD_ABORT_XRI_CX:
1620 	case CMD_CLOSE_XRI_CN:
1621 	case CMD_CLOSE_XRI_CX:
1622 	case CMD_XRI_ABORTED_CX:
1623 	case CMD_ABORT_MXRI64_CN:
1624 	case CMD_XMIT_BLS_RSP64_CX:
1625 		type = LPFC_ABORT_IOCB;
1626 		break;
1627 	case CMD_RCV_SEQUENCE_CX:
1628 	case CMD_RCV_ELS_REQ_CX:
1629 	case CMD_RCV_SEQUENCE64_CX:
1630 	case CMD_RCV_ELS_REQ64_CX:
1631 	case CMD_ASYNC_STATUS:
1632 	case CMD_IOCB_RCV_SEQ64_CX:
1633 	case CMD_IOCB_RCV_ELS64_CX:
1634 	case CMD_IOCB_RCV_CONT64_CX:
1635 	case CMD_IOCB_RET_XRI64_CX:
1636 		type = LPFC_UNSOL_IOCB;
1637 		break;
1638 	case CMD_IOCB_XMIT_MSEQ64_CR:
1639 	case CMD_IOCB_XMIT_MSEQ64_CX:
1640 	case CMD_IOCB_RCV_SEQ_LIST64_CX:
1641 	case CMD_IOCB_RCV_ELS_LIST64_CX:
1642 	case CMD_IOCB_CLOSE_EXTENDED_CN:
1643 	case CMD_IOCB_ABORT_EXTENDED_CN:
1644 	case CMD_IOCB_RET_HBQE64_CN:
1645 	case CMD_IOCB_FCP_IBIDIR64_CR:
1646 	case CMD_IOCB_FCP_IBIDIR64_CX:
1647 	case CMD_IOCB_FCP_ITASKMGT64_CX:
1648 	case CMD_IOCB_LOGENTRY_CN:
1649 	case CMD_IOCB_LOGENTRY_ASYNC_CN:
1650 		printk("%s - Unhandled SLI-3 Command x%x\n",
1651 				__func__, iocb_cmnd);
1652 		type = LPFC_UNKNOWN_IOCB;
1653 		break;
1654 	default:
1655 		type = LPFC_UNKNOWN_IOCB;
1656 		break;
1657 	}
1658 
1659 	return type;
1660 }
1661 
1662 /**
1663  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1664  * @phba: Pointer to HBA context object.
1665  *
1666  * This function is called from SLI initialization code
1667  * to configure every ring of the HBA's SLI interface. The
1668  * caller is not required to hold any lock. This function issues
1669  * a config_ring mailbox command for each ring.
1670  * This function returns zero if successful else returns a negative
1671  * error code.
1672  **/
1673 static int
lpfc_sli_ring_map(struct lpfc_hba * phba)1674 lpfc_sli_ring_map(struct lpfc_hba *phba)
1675 {
1676 	struct lpfc_sli *psli = &phba->sli;
1677 	LPFC_MBOXQ_t *pmb;
1678 	MAILBOX_t *pmbox;
1679 	int i, rc, ret = 0;
1680 
1681 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1682 	if (!pmb)
1683 		return -ENOMEM;
1684 	pmbox = &pmb->u.mb;
1685 	phba->link_state = LPFC_INIT_MBX_CMDS;
1686 	for (i = 0; i < psli->num_rings; i++) {
1687 		lpfc_config_ring(phba, i, pmb);
1688 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1689 		if (rc != MBX_SUCCESS) {
1690 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1691 					"0446 Adapter failed to init (%d), "
1692 					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
1693 					"ring %d\n",
1694 					rc, pmbox->mbxCommand,
1695 					pmbox->mbxStatus, i);
1696 			phba->link_state = LPFC_HBA_ERROR;
1697 			ret = -ENXIO;
1698 			break;
1699 		}
1700 	}
1701 	mempool_free(pmb, phba->mbox_mem_pool);
1702 	return ret;
1703 }
1704 
1705 /**
1706  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1707  * @phba: Pointer to HBA context object.
1708  * @pring: Pointer to driver SLI ring object.
1709  * @piocb: Pointer to the driver iocb object.
1710  *
1711  * The driver calls this function with the hbalock held for SLI3 ports or
1712  * the ring lock held for SLI4 ports. The function adds the
1713  * new iocb to txcmplq of the given ring. This function always returns
1714  * 0. If this function is called for ELS ring, this function checks if
1715  * there is a vport associated with the ELS command. This function also
1716  * starts els_tmofunc timer if this is an ELS command.
1717  **/
1718 static int
lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)1719 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1720 			struct lpfc_iocbq *piocb)
1721 {
1722 	u32 ulp_command = 0;
1723 
1724 	BUG_ON(!piocb);
1725 	ulp_command = get_job_cmnd(phba, piocb);
1726 
1727 	list_add_tail(&piocb->list, &pring->txcmplq);
1728 	piocb->cmd_flag |= LPFC_IO_ON_TXCMPLQ;
1729 	pring->txcmplq_cnt++;
1730 	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1731 	   (ulp_command != CMD_ABORT_XRI_WQE) &&
1732 	   (ulp_command != CMD_ABORT_XRI_CN) &&
1733 	   (ulp_command != CMD_CLOSE_XRI_CN)) {
1734 		BUG_ON(!piocb->vport);
1735 		if (!(piocb->vport->load_flag & FC_UNLOADING))
1736 			mod_timer(&piocb->vport->els_tmofunc,
1737 				  jiffies +
1738 				  msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1739 	}
1740 
1741 	return 0;
1742 }
1743 
1744 /**
1745  * lpfc_sli_ringtx_get - Get first element of the txq
1746  * @phba: Pointer to HBA context object.
1747  * @pring: Pointer to driver SLI ring object.
1748  *
1749  * This function is called with hbalock held to get next
1750  * iocb in txq of the given ring. If there is any iocb in
1751  * the txq, the function returns first iocb in the list after
1752  * removing the iocb from the list, else it returns NULL.
1753  **/
1754 struct lpfc_iocbq *
lpfc_sli_ringtx_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)1755 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1756 {
1757 	struct lpfc_iocbq *cmd_iocb;
1758 
1759 	lockdep_assert_held(&phba->hbalock);
1760 
1761 	list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1762 	return cmd_iocb;
1763 }
1764 
1765 /**
1766  * lpfc_cmf_sync_cmpl - Process a CMF_SYNC_WQE cmpl
1767  * @phba: Pointer to HBA context object.
1768  * @cmdiocb: Pointer to driver command iocb object.
1769  * @rspiocb: Pointer to driver response iocb object.
1770  *
1771  * This routine will inform the driver of any BW adjustments we need
1772  * to make. These changes will be picked up during the next CMF
1773  * timer interrupt. In addition, any BW changes will be logged
1774  * with LOG_CGN_MGMT.
1775  **/
1776 static void
lpfc_cmf_sync_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1777 lpfc_cmf_sync_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1778 		   struct lpfc_iocbq *rspiocb)
1779 {
1780 	union lpfc_wqe128 *wqe;
1781 	uint32_t status, info;
1782 	struct lpfc_wcqe_complete *wcqe = &rspiocb->wcqe_cmpl;
1783 	uint64_t bw, bwdif, slop;
1784 	uint64_t pcent, bwpcent;
1785 	int asig, afpin, sigcnt, fpincnt;
1786 	int wsigmax, wfpinmax, cg, tdp;
1787 	char *s;
1788 
1789 	/* First check for error */
1790 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1791 	if (status) {
1792 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1793 				"6211 CMF_SYNC_WQE Error "
1794 				"req_tag x%x status x%x hwstatus x%x "
1795 				"tdatap x%x parm x%x\n",
1796 				bf_get(lpfc_wcqe_c_request_tag, wcqe),
1797 				bf_get(lpfc_wcqe_c_status, wcqe),
1798 				bf_get(lpfc_wcqe_c_hw_status, wcqe),
1799 				wcqe->total_data_placed,
1800 				wcqe->parameter);
1801 		goto out;
1802 	}
1803 
1804 	/* Gather congestion information on a successful cmpl */
1805 	info = wcqe->parameter;
1806 	phba->cmf_active_info = info;
1807 
1808 	/* See if firmware info count is valid or has changed */
1809 	if (info > LPFC_MAX_CMF_INFO || phba->cmf_info_per_interval == info)
1810 		info = 0;
1811 	else
1812 		phba->cmf_info_per_interval = info;
1813 
1814 	tdp = bf_get(lpfc_wcqe_c_cmf_bw, wcqe);
1815 	cg = bf_get(lpfc_wcqe_c_cmf_cg, wcqe);
1816 
1817 	/* Get BW requirement from firmware */
1818 	bw = (uint64_t)tdp * LPFC_CMF_BLK_SIZE;
1819 	if (!bw) {
1820 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1821 				"6212 CMF_SYNC_WQE x%x: NULL bw\n",
1822 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
1823 		goto out;
1824 	}
1825 
1826 	/* Gather information needed for logging if a BW change is required */
1827 	wqe = &cmdiocb->wqe;
1828 	asig = bf_get(cmf_sync_asig, &wqe->cmf_sync);
1829 	afpin = bf_get(cmf_sync_afpin, &wqe->cmf_sync);
1830 	fpincnt = bf_get(cmf_sync_wfpincnt, &wqe->cmf_sync);
1831 	sigcnt = bf_get(cmf_sync_wsigcnt, &wqe->cmf_sync);
1832 	if (phba->cmf_max_bytes_per_interval != bw ||
1833 	    (asig || afpin || sigcnt || fpincnt)) {
1834 		/* Are we increasing or decreasing BW */
1835 		if (phba->cmf_max_bytes_per_interval <  bw) {
1836 			bwdif = bw - phba->cmf_max_bytes_per_interval;
1837 			s = "Increase";
1838 		} else {
1839 			bwdif = phba->cmf_max_bytes_per_interval - bw;
1840 			s = "Decrease";
1841 		}
1842 
1843 		/* What is the change percentage */
1844 		slop = div_u64(phba->cmf_link_byte_count, 200); /*For rounding*/
1845 		pcent = div64_u64(bwdif * 100 + slop,
1846 				  phba->cmf_link_byte_count);
1847 		bwpcent = div64_u64(bw * 100 + slop,
1848 				    phba->cmf_link_byte_count);
1849 		/* Because of bytes adjustment due to shorter timer in
1850 		 * lpfc_cmf_timer() the cmf_link_byte_count can be shorter and
1851 		 * may seem like BW is above 100%.
1852 		 */
1853 		if (bwpcent > 100)
1854 			bwpcent = 100;
1855 
1856 		if (phba->cmf_max_bytes_per_interval < bw &&
1857 		    bwpcent > 95)
1858 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1859 					"6208 Congestion bandwidth "
1860 					"limits removed\n");
1861 		else if ((phba->cmf_max_bytes_per_interval > bw) &&
1862 			 ((bwpcent + pcent) <= 100) && ((bwpcent + pcent) > 95))
1863 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1864 					"6209 Congestion bandwidth "
1865 					"limits in effect\n");
1866 
1867 		if (asig) {
1868 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1869 					"6237 BW Threshold %lld%% (%lld): "
1870 					"%lld%% %s: Signal Alarm: cg:%d "
1871 					"Info:%u\n",
1872 					bwpcent, bw, pcent, s, cg,
1873 					phba->cmf_active_info);
1874 		} else if (afpin) {
1875 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1876 					"6238 BW Threshold %lld%% (%lld): "
1877 					"%lld%% %s: FPIN Alarm: cg:%d "
1878 					"Info:%u\n",
1879 					bwpcent, bw, pcent, s, cg,
1880 					phba->cmf_active_info);
1881 		} else if (sigcnt) {
1882 			wsigmax = bf_get(cmf_sync_wsigmax, &wqe->cmf_sync);
1883 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1884 					"6239 BW Threshold %lld%% (%lld): "
1885 					"%lld%% %s: Signal Warning: "
1886 					"Cnt %d Max %d: cg:%d Info:%u\n",
1887 					bwpcent, bw, pcent, s, sigcnt,
1888 					wsigmax, cg, phba->cmf_active_info);
1889 		} else if (fpincnt) {
1890 			wfpinmax = bf_get(cmf_sync_wfpinmax, &wqe->cmf_sync);
1891 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1892 					"6240 BW Threshold %lld%% (%lld): "
1893 					"%lld%% %s: FPIN Warning: "
1894 					"Cnt %d Max %d: cg:%d Info:%u\n",
1895 					bwpcent, bw, pcent, s, fpincnt,
1896 					wfpinmax, cg, phba->cmf_active_info);
1897 		} else {
1898 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1899 					"6241 BW Threshold %lld%% (%lld): "
1900 					"CMF %lld%% %s: cg:%d Info:%u\n",
1901 					bwpcent, bw, pcent, s, cg,
1902 					phba->cmf_active_info);
1903 		}
1904 	} else if (info) {
1905 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1906 				"6246 Info Threshold %u\n", info);
1907 	}
1908 
1909 	/* Save BW change to be picked up during next timer interrupt */
1910 	phba->cmf_last_sync_bw = bw;
1911 out:
1912 	lpfc_sli_release_iocbq(phba, cmdiocb);
1913 }
1914 
1915 /**
1916  * lpfc_issue_cmf_sync_wqe - Issue a CMF_SYNC_WQE
1917  * @phba: Pointer to HBA context object.
1918  * @ms:   ms to set in WQE interval, 0 means use init op
1919  * @total: Total rcv bytes for this interval
1920  *
1921  * This routine is called every CMF timer interrupt. Its purpose is
1922  * to issue a CMF_SYNC_WQE to the firmware to inform it of any events
1923  * that may indicate we have congestion (FPINs or Signals). Upon
1924  * completion, the firmware will indicate any BW restrictions the
1925  * driver may need to take.
1926  **/
1927 int
lpfc_issue_cmf_sync_wqe(struct lpfc_hba * phba,u32 ms,u64 total)1928 lpfc_issue_cmf_sync_wqe(struct lpfc_hba *phba, u32 ms, u64 total)
1929 {
1930 	union lpfc_wqe128 *wqe;
1931 	struct lpfc_iocbq *sync_buf;
1932 	unsigned long iflags;
1933 	u32 ret_val;
1934 	u32 atot, wtot, max;
1935 	u8 warn_sync_period = 0;
1936 
1937 	/* First address any alarm / warning activity */
1938 	atot = atomic_xchg(&phba->cgn_sync_alarm_cnt, 0);
1939 	wtot = atomic_xchg(&phba->cgn_sync_warn_cnt, 0);
1940 
1941 	/* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
1942 	if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
1943 	    phba->link_state == LPFC_LINK_DOWN)
1944 		return 0;
1945 
1946 	spin_lock_irqsave(&phba->hbalock, iflags);
1947 	sync_buf = __lpfc_sli_get_iocbq(phba);
1948 	if (!sync_buf) {
1949 		lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
1950 				"6244 No available WQEs for CMF_SYNC_WQE\n");
1951 		ret_val = ENOMEM;
1952 		goto out_unlock;
1953 	}
1954 
1955 	wqe = &sync_buf->wqe;
1956 
1957 	/* WQEs are reused.  Clear stale data and set key fields to zero */
1958 	memset(wqe, 0, sizeof(*wqe));
1959 
1960 	/* If this is the very first CMF_SYNC_WQE, issue an init operation */
1961 	if (!ms) {
1962 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1963 				"6441 CMF Init %d - CMF_SYNC_WQE\n",
1964 				phba->fc_eventTag);
1965 		bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
1966 		bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
1967 		goto initpath;
1968 	}
1969 
1970 	bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
1971 	bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
1972 
1973 	/* Check for alarms / warnings */
1974 	if (atot) {
1975 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1976 			/* We hit an Signal alarm condition */
1977 			bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
1978 		} else {
1979 			/* We hit a FPIN alarm condition */
1980 			bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
1981 		}
1982 	} else if (wtot) {
1983 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
1984 		    phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1985 			/* We hit an Signal warning condition */
1986 			max = LPFC_SEC_TO_MSEC / lpfc_fabric_cgn_frequency *
1987 				lpfc_acqe_cgn_frequency;
1988 			bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
1989 			bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
1990 			warn_sync_period = lpfc_acqe_cgn_frequency;
1991 		} else {
1992 			/* We hit a FPIN warning condition */
1993 			bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
1994 			bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
1995 			if (phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ)
1996 				warn_sync_period =
1997 				LPFC_MSECS_TO_SECS(phba->cgn_fpin_frequency);
1998 		}
1999 	}
2000 
2001 	/* Update total read blocks during previous timer interval */
2002 	wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
2003 
2004 initpath:
2005 	bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
2006 	wqe->cmf_sync.event_tag = phba->fc_eventTag;
2007 	bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
2008 
2009 	/* Setup reqtag to match the wqe completion. */
2010 	bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
2011 
2012 	bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
2013 	bf_set(cmf_sync_period, &wqe->cmf_sync, warn_sync_period);
2014 
2015 	bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
2016 	bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
2017 	bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
2018 
2019 	sync_buf->vport = phba->pport;
2020 	sync_buf->cmd_cmpl = lpfc_cmf_sync_cmpl;
2021 	sync_buf->cmd_dmabuf = NULL;
2022 	sync_buf->rsp_dmabuf = NULL;
2023 	sync_buf->bpl_dmabuf = NULL;
2024 	sync_buf->sli4_xritag = NO_XRI;
2025 
2026 	sync_buf->cmd_flag |= LPFC_IO_CMF;
2027 	ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
2028 	if (ret_val) {
2029 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2030 				"6214 Cannot issue CMF_SYNC_WQE: x%x\n",
2031 				ret_val);
2032 		__lpfc_sli_release_iocbq(phba, sync_buf);
2033 	}
2034 out_unlock:
2035 	spin_unlock_irqrestore(&phba->hbalock, iflags);
2036 	return ret_val;
2037 }
2038 
2039 /**
2040  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
2041  * @phba: Pointer to HBA context object.
2042  * @pring: Pointer to driver SLI ring object.
2043  *
2044  * This function is called with hbalock held and the caller must post the
2045  * iocb without releasing the lock. If the caller releases the lock,
2046  * iocb slot returned by the function is not guaranteed to be available.
2047  * The function returns pointer to the next available iocb slot if there
2048  * is available slot in the ring, else it returns NULL.
2049  * If the get index of the ring is ahead of the put index, the function
2050  * will post an error attention event to the worker thread to take the
2051  * HBA to offline state.
2052  **/
2053 static IOCB_t *
lpfc_sli_next_iocb_slot(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2054 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2055 {
2056 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2057 	uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
2058 
2059 	lockdep_assert_held(&phba->hbalock);
2060 
2061 	if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
2062 	   (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
2063 		pring->sli.sli3.next_cmdidx = 0;
2064 
2065 	if (unlikely(pring->sli.sli3.local_getidx ==
2066 		pring->sli.sli3.next_cmdidx)) {
2067 
2068 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
2069 
2070 		if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
2071 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2072 					"0315 Ring %d issue: portCmdGet %d "
2073 					"is bigger than cmd ring %d\n",
2074 					pring->ringno,
2075 					pring->sli.sli3.local_getidx,
2076 					max_cmd_idx);
2077 
2078 			phba->link_state = LPFC_HBA_ERROR;
2079 			/*
2080 			 * All error attention handlers are posted to
2081 			 * worker thread
2082 			 */
2083 			phba->work_ha |= HA_ERATT;
2084 			phba->work_hs = HS_FFER3;
2085 
2086 			lpfc_worker_wake_up(phba);
2087 
2088 			return NULL;
2089 		}
2090 
2091 		if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
2092 			return NULL;
2093 	}
2094 
2095 	return lpfc_cmd_iocb(phba, pring);
2096 }
2097 
2098 /**
2099  * lpfc_sli_next_iotag - Get an iotag for the iocb
2100  * @phba: Pointer to HBA context object.
2101  * @iocbq: Pointer to driver iocb object.
2102  *
2103  * This function gets an iotag for the iocb. If there is no unused iotag and
2104  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
2105  * array and assigns a new iotag.
2106  * The function returns the allocated iotag if successful, else returns zero.
2107  * Zero is not a valid iotag.
2108  * The caller is not required to hold any lock.
2109  **/
2110 uint16_t
lpfc_sli_next_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)2111 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
2112 {
2113 	struct lpfc_iocbq **new_arr;
2114 	struct lpfc_iocbq **old_arr;
2115 	size_t new_len;
2116 	struct lpfc_sli *psli = &phba->sli;
2117 	uint16_t iotag;
2118 
2119 	spin_lock_irq(&phba->hbalock);
2120 	iotag = psli->last_iotag;
2121 	if(++iotag < psli->iocbq_lookup_len) {
2122 		psli->last_iotag = iotag;
2123 		psli->iocbq_lookup[iotag] = iocbq;
2124 		spin_unlock_irq(&phba->hbalock);
2125 		iocbq->iotag = iotag;
2126 		return iotag;
2127 	} else if (psli->iocbq_lookup_len < (0xffff
2128 					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
2129 		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2130 		spin_unlock_irq(&phba->hbalock);
2131 		new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
2132 				  GFP_KERNEL);
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 = (struct completion *)pmboxq->context3;
2834 	if (pmbox_done)
2835 		complete(pmbox_done);
2836 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2837 	return;
2838 }
2839 
2840 static void
__lpfc_sli_rpi_release(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)2841 __lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2842 {
2843 	unsigned long iflags;
2844 
2845 	if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2846 		lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
2847 		spin_lock_irqsave(&ndlp->lock, iflags);
2848 		ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2849 		ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
2850 		spin_unlock_irqrestore(&ndlp->lock, iflags);
2851 	}
2852 	ndlp->nlp_flag &= ~NLP_UNREG_INP;
2853 }
2854 
2855 void
lpfc_sli_rpi_release(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)2856 lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2857 {
2858 	__lpfc_sli_rpi_release(vport, ndlp);
2859 }
2860 
2861 /**
2862  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2863  * @phba: Pointer to HBA context object.
2864  * @pmb: Pointer to mailbox object.
2865  *
2866  * This function is the default mailbox completion handler. It
2867  * frees the memory resources associated with the completed mailbox
2868  * command. If the completed command is a REG_LOGIN mailbox command,
2869  * this function will issue a UREG_LOGIN to re-claim the RPI.
2870  **/
2871 void
lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2872 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2873 {
2874 	struct lpfc_vport  *vport = pmb->vport;
2875 	struct lpfc_dmabuf *mp;
2876 	struct lpfc_nodelist *ndlp;
2877 	struct Scsi_Host *shost;
2878 	uint16_t rpi, vpi;
2879 	int rc;
2880 
2881 	/*
2882 	 * If a REG_LOGIN succeeded  after node is destroyed or node
2883 	 * is in re-discovery driver need to cleanup the RPI.
2884 	 */
2885 	if (!(phba->pport->load_flag & FC_UNLOADING) &&
2886 	    pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2887 	    !pmb->u.mb.mbxStatus) {
2888 		mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
2889 		if (mp) {
2890 			pmb->ctx_buf = NULL;
2891 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
2892 			kfree(mp);
2893 		}
2894 		rpi = pmb->u.mb.un.varWords[0];
2895 		vpi = pmb->u.mb.un.varRegLogin.vpi;
2896 		if (phba->sli_rev == LPFC_SLI_REV4)
2897 			vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2898 		lpfc_unreg_login(phba, vpi, rpi, pmb);
2899 		pmb->vport = vport;
2900 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2901 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2902 		if (rc != MBX_NOT_FINISHED)
2903 			return;
2904 	}
2905 
2906 	if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2907 		!(phba->pport->load_flag & FC_UNLOADING) &&
2908 		!pmb->u.mb.mbxStatus) {
2909 		shost = lpfc_shost_from_vport(vport);
2910 		spin_lock_irq(shost->host_lock);
2911 		vport->vpi_state |= LPFC_VPI_REGISTERED;
2912 		vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2913 		spin_unlock_irq(shost->host_lock);
2914 	}
2915 
2916 	if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2917 		ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2918 		lpfc_nlp_put(ndlp);
2919 	}
2920 
2921 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2922 		ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2923 
2924 		/* Check to see if there are any deferred events to process */
2925 		if (ndlp) {
2926 			lpfc_printf_vlog(
2927 				vport,
2928 				KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2929 				"1438 UNREG cmpl deferred mbox x%x "
2930 				"on NPort x%x Data: x%x x%x x%px x%x x%x\n",
2931 				ndlp->nlp_rpi, ndlp->nlp_DID,
2932 				ndlp->nlp_flag, ndlp->nlp_defer_did,
2933 				ndlp, vport->load_flag, kref_read(&ndlp->kref));
2934 
2935 			if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2936 			    (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
2937 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
2938 				ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2939 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2940 			} else {
2941 				__lpfc_sli_rpi_release(vport, ndlp);
2942 			}
2943 
2944 			/* The unreg_login mailbox is complete and had a
2945 			 * reference that has to be released.  The PLOGI
2946 			 * got its own ref.
2947 			 */
2948 			lpfc_nlp_put(ndlp);
2949 			pmb->ctx_ndlp = NULL;
2950 		}
2951 	}
2952 
2953 	/* This nlp_put pairs with lpfc_sli4_resume_rpi */
2954 	if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
2955 		ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2956 		lpfc_nlp_put(ndlp);
2957 	}
2958 
2959 	/* Check security permission status on INIT_LINK mailbox command */
2960 	if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2961 	    (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2962 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2963 				"2860 SLI authentication is required "
2964 				"for INIT_LINK but has not done yet\n");
2965 
2966 	if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2967 		lpfc_sli4_mbox_cmd_free(phba, pmb);
2968 	else
2969 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
2970 }
2971  /**
2972  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2973  * @phba: Pointer to HBA context object.
2974  * @pmb: Pointer to mailbox object.
2975  *
2976  * This function is the unreg rpi mailbox completion handler. It
2977  * frees the memory resources associated with the completed mailbox
2978  * command. An additional reference is put on the ndlp to prevent
2979  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2980  * the unreg mailbox command completes, this routine puts the
2981  * reference back.
2982  *
2983  **/
2984 void
lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2985 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2986 {
2987 	struct lpfc_vport  *vport = pmb->vport;
2988 	struct lpfc_nodelist *ndlp;
2989 
2990 	ndlp = pmb->ctx_ndlp;
2991 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2992 		if (phba->sli_rev == LPFC_SLI_REV4 &&
2993 		    (bf_get(lpfc_sli_intf_if_type,
2994 		     &phba->sli4_hba.sli_intf) >=
2995 		     LPFC_SLI_INTF_IF_TYPE_2)) {
2996 			if (ndlp) {
2997 				lpfc_printf_vlog(
2998 					 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2999 					 "0010 UNREG_LOGIN vpi:%x "
3000 					 "rpi:%x DID:%x defer x%x flg x%x "
3001 					 "x%px\n",
3002 					 vport->vpi, ndlp->nlp_rpi,
3003 					 ndlp->nlp_DID, ndlp->nlp_defer_did,
3004 					 ndlp->nlp_flag,
3005 					 ndlp);
3006 				ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3007 
3008 				/* Check to see if there are any deferred
3009 				 * events to process
3010 				 */
3011 				if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
3012 				    (ndlp->nlp_defer_did !=
3013 				    NLP_EVT_NOTHING_PENDING)) {
3014 					lpfc_printf_vlog(
3015 						vport, KERN_INFO, LOG_DISCOVERY,
3016 						"4111 UNREG cmpl deferred "
3017 						"clr x%x on "
3018 						"NPort x%x Data: x%x x%px\n",
3019 						ndlp->nlp_rpi, ndlp->nlp_DID,
3020 						ndlp->nlp_defer_did, ndlp);
3021 					ndlp->nlp_flag &= ~NLP_UNREG_INP;
3022 					ndlp->nlp_defer_did =
3023 						NLP_EVT_NOTHING_PENDING;
3024 					lpfc_issue_els_plogi(
3025 						vport, ndlp->nlp_DID, 0);
3026 				} else {
3027 					__lpfc_sli_rpi_release(vport, ndlp);
3028 				}
3029 				lpfc_nlp_put(ndlp);
3030 			}
3031 		}
3032 	}
3033 
3034 	mempool_free(pmb, phba->mbox_mem_pool);
3035 }
3036 
3037 /**
3038  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
3039  * @phba: Pointer to HBA context object.
3040  *
3041  * This function is called with no lock held. This function processes all
3042  * the completed mailbox commands and gives it to upper layers. The interrupt
3043  * service routine processes mailbox completion interrupt and adds completed
3044  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
3045  * Worker thread call lpfc_sli_handle_mb_event, which will return the
3046  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
3047  * function returns the mailbox commands to the upper layer by calling the
3048  * completion handler function of each mailbox.
3049  **/
3050 int
lpfc_sli_handle_mb_event(struct lpfc_hba * phba)3051 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
3052 {
3053 	MAILBOX_t *pmbox;
3054 	LPFC_MBOXQ_t *pmb;
3055 	int rc;
3056 	LIST_HEAD(cmplq);
3057 
3058 	phba->sli.slistat.mbox_event++;
3059 
3060 	/* Get all completed mailboxe buffers into the cmplq */
3061 	spin_lock_irq(&phba->hbalock);
3062 	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
3063 	spin_unlock_irq(&phba->hbalock);
3064 
3065 	/* Get a Mailbox buffer to setup mailbox commands for callback */
3066 	do {
3067 		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
3068 		if (pmb == NULL)
3069 			break;
3070 
3071 		pmbox = &pmb->u.mb;
3072 
3073 		if (pmbox->mbxCommand != MBX_HEARTBEAT) {
3074 			if (pmb->vport) {
3075 				lpfc_debugfs_disc_trc(pmb->vport,
3076 					LPFC_DISC_TRC_MBOX_VPORT,
3077 					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",
3078 					(uint32_t)pmbox->mbxCommand,
3079 					pmbox->un.varWords[0],
3080 					pmbox->un.varWords[1]);
3081 			}
3082 			else {
3083 				lpfc_debugfs_disc_trc(phba->pport,
3084 					LPFC_DISC_TRC_MBOX,
3085 					"MBOX cmpl:       cmd:x%x mb:x%x x%x",
3086 					(uint32_t)pmbox->mbxCommand,
3087 					pmbox->un.varWords[0],
3088 					pmbox->un.varWords[1]);
3089 			}
3090 		}
3091 
3092 		/*
3093 		 * It is a fatal error if unknown mbox command completion.
3094 		 */
3095 		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
3096 		    MBX_SHUTDOWN) {
3097 			/* Unknown mailbox command compl */
3098 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3099 					"(%d):0323 Unknown Mailbox command "
3100 					"x%x (x%x/x%x) Cmpl\n",
3101 					pmb->vport ? pmb->vport->vpi :
3102 					LPFC_VPORT_UNKNOWN,
3103 					pmbox->mbxCommand,
3104 					lpfc_sli_config_mbox_subsys_get(phba,
3105 									pmb),
3106 					lpfc_sli_config_mbox_opcode_get(phba,
3107 									pmb));
3108 			phba->link_state = LPFC_HBA_ERROR;
3109 			phba->work_hs = HS_FFER3;
3110 			lpfc_handle_eratt(phba);
3111 			continue;
3112 		}
3113 
3114 		if (pmbox->mbxStatus) {
3115 			phba->sli.slistat.mbox_stat_err++;
3116 			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
3117 				/* Mbox cmd cmpl error - RETRYing */
3118 				lpfc_printf_log(phba, KERN_INFO,
3119 					LOG_MBOX | LOG_SLI,
3120 					"(%d):0305 Mbox cmd cmpl "
3121 					"error - RETRYing Data: x%x "
3122 					"(x%x/x%x) x%x x%x x%x\n",
3123 					pmb->vport ? pmb->vport->vpi :
3124 					LPFC_VPORT_UNKNOWN,
3125 					pmbox->mbxCommand,
3126 					lpfc_sli_config_mbox_subsys_get(phba,
3127 									pmb),
3128 					lpfc_sli_config_mbox_opcode_get(phba,
3129 									pmb),
3130 					pmbox->mbxStatus,
3131 					pmbox->un.varWords[0],
3132 					pmb->vport ? pmb->vport->port_state :
3133 					LPFC_VPORT_UNKNOWN);
3134 				pmbox->mbxStatus = 0;
3135 				pmbox->mbxOwner = OWN_HOST;
3136 				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3137 				if (rc != MBX_NOT_FINISHED)
3138 					continue;
3139 			}
3140 		}
3141 
3142 		/* Mailbox cmd <cmd> Cmpl <cmpl> */
3143 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3144 				"(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
3145 				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3146 				"x%x x%x x%x\n",
3147 				pmb->vport ? pmb->vport->vpi : 0,
3148 				pmbox->mbxCommand,
3149 				lpfc_sli_config_mbox_subsys_get(phba, pmb),
3150 				lpfc_sli_config_mbox_opcode_get(phba, pmb),
3151 				pmb->mbox_cmpl,
3152 				*((uint32_t *) pmbox),
3153 				pmbox->un.varWords[0],
3154 				pmbox->un.varWords[1],
3155 				pmbox->un.varWords[2],
3156 				pmbox->un.varWords[3],
3157 				pmbox->un.varWords[4],
3158 				pmbox->un.varWords[5],
3159 				pmbox->un.varWords[6],
3160 				pmbox->un.varWords[7],
3161 				pmbox->un.varWords[8],
3162 				pmbox->un.varWords[9],
3163 				pmbox->un.varWords[10]);
3164 
3165 		if (pmb->mbox_cmpl)
3166 			pmb->mbox_cmpl(phba,pmb);
3167 	} while (1);
3168 	return 0;
3169 }
3170 
3171 /**
3172  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
3173  * @phba: Pointer to HBA context object.
3174  * @pring: Pointer to driver SLI ring object.
3175  * @tag: buffer tag.
3176  *
3177  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
3178  * is set in the tag the buffer is posted for a particular exchange,
3179  * the function will return the buffer without replacing the buffer.
3180  * If the buffer is for unsolicited ELS or CT traffic, this function
3181  * returns the buffer and also posts another buffer to the firmware.
3182  **/
3183 static struct lpfc_dmabuf *
lpfc_sli_get_buff(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)3184 lpfc_sli_get_buff(struct lpfc_hba *phba,
3185 		  struct lpfc_sli_ring *pring,
3186 		  uint32_t tag)
3187 {
3188 	struct hbq_dmabuf *hbq_entry;
3189 
3190 	if (tag & QUE_BUFTAG_BIT)
3191 		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
3192 	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
3193 	if (!hbq_entry)
3194 		return NULL;
3195 	return &hbq_entry->dbuf;
3196 }
3197 
3198 /**
3199  * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
3200  *                              containing a NVME LS request.
3201  * @phba: pointer to lpfc hba data structure.
3202  * @piocb: pointer to the iocbq struct representing the sequence starting
3203  *        frame.
3204  *
3205  * This routine initially validates the NVME LS, validates there is a login
3206  * with the port that sent the LS, and then calls the appropriate nvme host
3207  * or target LS request handler.
3208  **/
3209 static void
lpfc_nvme_unsol_ls_handler(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)3210 lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
3211 {
3212 	struct lpfc_nodelist *ndlp;
3213 	struct lpfc_dmabuf *d_buf;
3214 	struct hbq_dmabuf *nvmebuf;
3215 	struct fc_frame_header *fc_hdr;
3216 	struct lpfc_async_xchg_ctx *axchg = NULL;
3217 	char *failwhy = NULL;
3218 	uint32_t oxid, sid, did, fctl, size;
3219 	int ret = 1;
3220 
3221 	d_buf = piocb->cmd_dmabuf;
3222 
3223 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
3224 	fc_hdr = nvmebuf->hbuf.virt;
3225 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
3226 	sid = sli4_sid_from_fc_hdr(fc_hdr);
3227 	did = sli4_did_from_fc_hdr(fc_hdr);
3228 	fctl = (fc_hdr->fh_f_ctl[0] << 16 |
3229 		fc_hdr->fh_f_ctl[1] << 8 |
3230 		fc_hdr->fh_f_ctl[2]);
3231 	size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
3232 
3233 	lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
3234 			 oxid, size, sid);
3235 
3236 	if (phba->pport->load_flag & FC_UNLOADING) {
3237 		failwhy = "Driver Unloading";
3238 	} else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
3239 		failwhy = "NVME FC4 Disabled";
3240 	} else if (!phba->nvmet_support && !phba->pport->localport) {
3241 		failwhy = "No Localport";
3242 	} else if (phba->nvmet_support && !phba->targetport) {
3243 		failwhy = "No Targetport";
3244 	} else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
3245 		failwhy = "Bad NVME LS R_CTL";
3246 	} else if (unlikely((fctl & 0x00FF0000) !=
3247 			(FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
3248 		failwhy = "Bad NVME LS F_CTL";
3249 	} else {
3250 		axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
3251 		if (!axchg)
3252 			failwhy = "No CTX memory";
3253 	}
3254 
3255 	if (unlikely(failwhy)) {
3256 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3257 				"6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
3258 				sid, oxid, failwhy);
3259 		goto out_fail;
3260 	}
3261 
3262 	/* validate the source of the LS is logged in */
3263 	ndlp = lpfc_findnode_did(phba->pport, sid);
3264 	if (!ndlp ||
3265 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3266 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3267 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
3268 				"6216 NVME Unsol rcv: No ndlp: "
3269 				"NPort_ID x%x oxid x%x\n",
3270 				sid, oxid);
3271 		goto out_fail;
3272 	}
3273 
3274 	axchg->phba = phba;
3275 	axchg->ndlp = ndlp;
3276 	axchg->size = size;
3277 	axchg->oxid = oxid;
3278 	axchg->sid = sid;
3279 	axchg->wqeq = NULL;
3280 	axchg->state = LPFC_NVME_STE_LS_RCV;
3281 	axchg->entry_cnt = 1;
3282 	axchg->rqb_buffer = (void *)nvmebuf;
3283 	axchg->hdwq = &phba->sli4_hba.hdwq[0];
3284 	axchg->payload = nvmebuf->dbuf.virt;
3285 	INIT_LIST_HEAD(&axchg->list);
3286 
3287 	if (phba->nvmet_support) {
3288 		ret = lpfc_nvmet_handle_lsreq(phba, axchg);
3289 		spin_lock_irq(&ndlp->lock);
3290 		if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3291 			ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3292 			spin_unlock_irq(&ndlp->lock);
3293 
3294 			/* This reference is a single occurrence to hold the
3295 			 * node valid until the nvmet transport calls
3296 			 * host_release.
3297 			 */
3298 			if (!lpfc_nlp_get(ndlp))
3299 				goto out_fail;
3300 
3301 			lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
3302 					"6206 NVMET unsol ls_req ndlp x%px "
3303 					"DID x%x xflags x%x refcnt %d\n",
3304 					ndlp, ndlp->nlp_DID,
3305 					ndlp->fc4_xpt_flags,
3306 					kref_read(&ndlp->kref));
3307 		} else {
3308 			spin_unlock_irq(&ndlp->lock);
3309 		}
3310 	} else {
3311 		ret = lpfc_nvme_handle_lsreq(phba, axchg);
3312 	}
3313 
3314 	/* if zero, LS was successfully handled. If non-zero, LS not handled */
3315 	if (!ret)
3316 		return;
3317 
3318 out_fail:
3319 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3320 			"6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3321 			"NVMe%s handler failed %d\n",
3322 			did, sid, oxid,
3323 			(phba->nvmet_support) ? "T" : "I", ret);
3324 
3325 	/* recycle receive buffer */
3326 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3327 
3328 	/* If start of new exchange, abort it */
3329 	if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3330 		ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3331 
3332 	if (ret)
3333 		kfree(axchg);
3334 }
3335 
3336 /**
3337  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3338  * @phba: Pointer to HBA context object.
3339  * @pring: Pointer to driver SLI ring object.
3340  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3341  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3342  * @fch_type: the type for the first frame of the sequence.
3343  *
3344  * This function is called with no lock held. This function uses the r_ctl and
3345  * type of the received sequence to find the correct callback function to call
3346  * to process the sequence.
3347  **/
3348 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)3349 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3350 			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3351 			 uint32_t fch_type)
3352 {
3353 	int i;
3354 
3355 	switch (fch_type) {
3356 	case FC_TYPE_NVME:
3357 		lpfc_nvme_unsol_ls_handler(phba, saveq);
3358 		return 1;
3359 	default:
3360 		break;
3361 	}
3362 
3363 	/* unSolicited Responses */
3364 	if (pring->prt[0].profile) {
3365 		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3366 			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3367 									saveq);
3368 		return 1;
3369 	}
3370 	/* We must search, based on rctl / type
3371 	   for the right routine */
3372 	for (i = 0; i < pring->num_mask; i++) {
3373 		if ((pring->prt[i].rctl == fch_r_ctl) &&
3374 		    (pring->prt[i].type == fch_type)) {
3375 			if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3376 				(pring->prt[i].lpfc_sli_rcv_unsol_event)
3377 						(phba, pring, saveq);
3378 			return 1;
3379 		}
3380 	}
3381 	return 0;
3382 }
3383 
3384 static void
lpfc_sli_prep_unsol_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * saveq)3385 lpfc_sli_prep_unsol_wqe(struct lpfc_hba *phba,
3386 			struct lpfc_iocbq *saveq)
3387 {
3388 	IOCB_t *irsp;
3389 	union lpfc_wqe128 *wqe;
3390 	u16 i = 0;
3391 
3392 	irsp = &saveq->iocb;
3393 	wqe = &saveq->wqe;
3394 
3395 	/* Fill wcqe with the IOCB status fields */
3396 	bf_set(lpfc_wcqe_c_status, &saveq->wcqe_cmpl, irsp->ulpStatus);
3397 	saveq->wcqe_cmpl.word3 = irsp->ulpBdeCount;
3398 	saveq->wcqe_cmpl.parameter = irsp->un.ulpWord[4];
3399 	saveq->wcqe_cmpl.total_data_placed = irsp->unsli3.rcvsli3.acc_len;
3400 
3401 	/* Source ID */
3402 	bf_set(els_rsp64_sid, &wqe->xmit_els_rsp, irsp->un.rcvels.parmRo);
3403 
3404 	/* rx-id of the response frame */
3405 	bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com, irsp->ulpContext);
3406 
3407 	/* ox-id of the frame */
3408 	bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
3409 	       irsp->unsli3.rcvsli3.ox_id);
3410 
3411 	/* DID */
3412 	bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
3413 	       irsp->un.rcvels.remoteID);
3414 
3415 	/* unsol data len */
3416 	for (i = 0; i < irsp->ulpBdeCount; i++) {
3417 		struct lpfc_hbq_entry *hbqe = NULL;
3418 
3419 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3420 			if (i == 0) {
3421 				hbqe = (struct lpfc_hbq_entry *)
3422 					&irsp->un.ulpWord[0];
3423 				saveq->wqe.gen_req.bde.tus.f.bdeSize =
3424 					hbqe->bde.tus.f.bdeSize;
3425 			} else if (i == 1) {
3426 				hbqe = (struct lpfc_hbq_entry *)
3427 					&irsp->unsli3.sli3Words[4];
3428 				saveq->unsol_rcv_len = hbqe->bde.tus.f.bdeSize;
3429 			}
3430 		}
3431 	}
3432 }
3433 
3434 /**
3435  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
3436  * @phba: Pointer to HBA context object.
3437  * @pring: Pointer to driver SLI ring object.
3438  * @saveq: Pointer to the unsolicited iocb.
3439  *
3440  * This function is called with no lock held by the ring event handler
3441  * when there is an unsolicited iocb posted to the response ring by the
3442  * firmware. This function gets the buffer associated with the iocbs
3443  * and calls the event handler for the ring. This function handles both
3444  * qring buffers and hbq buffers.
3445  * When the function returns 1 the caller can free the iocb object otherwise
3446  * upper layer functions will free the iocb objects.
3447  **/
3448 static int
lpfc_sli_process_unsol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3449 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3450 			    struct lpfc_iocbq *saveq)
3451 {
3452 	IOCB_t           * irsp;
3453 	WORD5            * w5p;
3454 	dma_addr_t	 paddr;
3455 	uint32_t           Rctl, Type;
3456 	struct lpfc_iocbq *iocbq;
3457 	struct lpfc_dmabuf *dmzbuf;
3458 
3459 	irsp = &saveq->iocb;
3460 	saveq->vport = phba->pport;
3461 
3462 	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3463 		if (pring->lpfc_sli_rcv_async_status)
3464 			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3465 		else
3466 			lpfc_printf_log(phba,
3467 					KERN_WARNING,
3468 					LOG_SLI,
3469 					"0316 Ring %d handler: unexpected "
3470 					"ASYNC_STATUS iocb received evt_code "
3471 					"0x%x\n",
3472 					pring->ringno,
3473 					irsp->un.asyncstat.evt_code);
3474 		return 1;
3475 	}
3476 
3477 	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3478 	    (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3479 		if (irsp->ulpBdeCount > 0) {
3480 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3481 						   irsp->un.ulpWord[3]);
3482 			lpfc_in_buf_free(phba, dmzbuf);
3483 		}
3484 
3485 		if (irsp->ulpBdeCount > 1) {
3486 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3487 						   irsp->unsli3.sli3Words[3]);
3488 			lpfc_in_buf_free(phba, dmzbuf);
3489 		}
3490 
3491 		if (irsp->ulpBdeCount > 2) {
3492 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3493 						   irsp->unsli3.sli3Words[7]);
3494 			lpfc_in_buf_free(phba, dmzbuf);
3495 		}
3496 
3497 		return 1;
3498 	}
3499 
3500 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3501 		if (irsp->ulpBdeCount != 0) {
3502 			saveq->cmd_dmabuf = lpfc_sli_get_buff(phba, pring,
3503 						irsp->un.ulpWord[3]);
3504 			if (!saveq->cmd_dmabuf)
3505 				lpfc_printf_log(phba,
3506 					KERN_ERR,
3507 					LOG_SLI,
3508 					"0341 Ring %d Cannot find buffer for "
3509 					"an unsolicited iocb. tag 0x%x\n",
3510 					pring->ringno,
3511 					irsp->un.ulpWord[3]);
3512 		}
3513 		if (irsp->ulpBdeCount == 2) {
3514 			saveq->bpl_dmabuf = lpfc_sli_get_buff(phba, pring,
3515 						irsp->unsli3.sli3Words[7]);
3516 			if (!saveq->bpl_dmabuf)
3517 				lpfc_printf_log(phba,
3518 					KERN_ERR,
3519 					LOG_SLI,
3520 					"0342 Ring %d Cannot find buffer for an"
3521 					" unsolicited iocb. tag 0x%x\n",
3522 					pring->ringno,
3523 					irsp->unsli3.sli3Words[7]);
3524 		}
3525 		list_for_each_entry(iocbq, &saveq->list, list) {
3526 			irsp = &iocbq->iocb;
3527 			if (irsp->ulpBdeCount != 0) {
3528 				iocbq->cmd_dmabuf = lpfc_sli_get_buff(phba,
3529 							pring,
3530 							irsp->un.ulpWord[3]);
3531 				if (!iocbq->cmd_dmabuf)
3532 					lpfc_printf_log(phba,
3533 						KERN_ERR,
3534 						LOG_SLI,
3535 						"0343 Ring %d Cannot find "
3536 						"buffer for an unsolicited iocb"
3537 						". tag 0x%x\n", pring->ringno,
3538 						irsp->un.ulpWord[3]);
3539 			}
3540 			if (irsp->ulpBdeCount == 2) {
3541 				iocbq->bpl_dmabuf = lpfc_sli_get_buff(phba,
3542 						pring,
3543 						irsp->unsli3.sli3Words[7]);
3544 				if (!iocbq->bpl_dmabuf)
3545 					lpfc_printf_log(phba,
3546 						KERN_ERR,
3547 						LOG_SLI,
3548 						"0344 Ring %d Cannot find "
3549 						"buffer for an unsolicited "
3550 						"iocb. tag 0x%x\n",
3551 						pring->ringno,
3552 						irsp->unsli3.sli3Words[7]);
3553 			}
3554 		}
3555 	} else {
3556 		paddr = getPaddr(irsp->un.cont64[0].addrHigh,
3557 				 irsp->un.cont64[0].addrLow);
3558 		saveq->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
3559 							     paddr);
3560 		if (irsp->ulpBdeCount == 2) {
3561 			paddr = getPaddr(irsp->un.cont64[1].addrHigh,
3562 					 irsp->un.cont64[1].addrLow);
3563 			saveq->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
3564 								   pring,
3565 								   paddr);
3566 		}
3567 	}
3568 
3569 	if (irsp->ulpBdeCount != 0 &&
3570 	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3571 	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3572 		int found = 0;
3573 
3574 		/* search continue save q for same XRI */
3575 		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
3576 			if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3577 				saveq->iocb.unsli3.rcvsli3.ox_id) {
3578 				list_add_tail(&saveq->list, &iocbq->list);
3579 				found = 1;
3580 				break;
3581 			}
3582 		}
3583 		if (!found)
3584 			list_add_tail(&saveq->clist,
3585 				      &pring->iocb_continue_saveq);
3586 
3587 		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3588 			list_del_init(&iocbq->clist);
3589 			saveq = iocbq;
3590 			irsp = &saveq->iocb;
3591 		} else {
3592 			return 0;
3593 		}
3594 	}
3595 	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3596 	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3597 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
3598 		Rctl = FC_RCTL_ELS_REQ;
3599 		Type = FC_TYPE_ELS;
3600 	} else {
3601 		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3602 		Rctl = w5p->hcsw.Rctl;
3603 		Type = w5p->hcsw.Type;
3604 
3605 		/* Firmware Workaround */
3606 		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3607 			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3608 			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3609 			Rctl = FC_RCTL_ELS_REQ;
3610 			Type = FC_TYPE_ELS;
3611 			w5p->hcsw.Rctl = Rctl;
3612 			w5p->hcsw.Type = Type;
3613 		}
3614 	}
3615 
3616 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3617 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
3618 	    irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3619 		if (irsp->unsli3.rcvsli3.vpi == 0xffff)
3620 			saveq->vport = phba->pport;
3621 		else
3622 			saveq->vport = lpfc_find_vport_by_vpid(phba,
3623 					       irsp->unsli3.rcvsli3.vpi);
3624 	}
3625 
3626 	/* Prepare WQE with Unsol frame */
3627 	lpfc_sli_prep_unsol_wqe(phba, saveq);
3628 
3629 	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
3630 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3631 				"0313 Ring %d handler: unexpected Rctl x%x "
3632 				"Type x%x received\n",
3633 				pring->ringno, Rctl, Type);
3634 
3635 	return 1;
3636 }
3637 
3638 /**
3639  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
3640  * @phba: Pointer to HBA context object.
3641  * @pring: Pointer to driver SLI ring object.
3642  * @prspiocb: Pointer to response iocb object.
3643  *
3644  * This function looks up the iocb_lookup table to get the command iocb
3645  * corresponding to the given response iocb using the iotag of the
3646  * response iocb. The driver calls this function with the hbalock held
3647  * for SLI3 ports or the ring lock held for SLI4 ports.
3648  * This function returns the command iocb object if it finds the command
3649  * iocb else returns NULL.
3650  **/
3651 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * prspiocb)3652 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3653 		      struct lpfc_sli_ring *pring,
3654 		      struct lpfc_iocbq *prspiocb)
3655 {
3656 	struct lpfc_iocbq *cmd_iocb = NULL;
3657 	u16 iotag;
3658 
3659 	if (phba->sli_rev == LPFC_SLI_REV4)
3660 		iotag = get_wqe_reqtag(prspiocb);
3661 	else
3662 		iotag = prspiocb->iocb.ulpIoTag;
3663 
3664 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3665 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3666 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3667 			/* remove from txcmpl queue list */
3668 			list_del_init(&cmd_iocb->list);
3669 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3670 			pring->txcmplq_cnt--;
3671 			return cmd_iocb;
3672 		}
3673 	}
3674 
3675 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3676 			"0317 iotag x%x is out of "
3677 			"range: max iotag x%x\n",
3678 			iotag, phba->sli.last_iotag);
3679 	return NULL;
3680 }
3681 
3682 /**
3683  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3684  * @phba: Pointer to HBA context object.
3685  * @pring: Pointer to driver SLI ring object.
3686  * @iotag: IOCB tag.
3687  *
3688  * This function looks up the iocb_lookup table to get the command iocb
3689  * corresponding to the given iotag. The driver calls this function with
3690  * the ring lock held because this function is an SLI4 port only helper.
3691  * This function returns the command iocb object if it finds the command
3692  * iocb else returns NULL.
3693  **/
3694 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint16_t iotag)3695 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3696 			     struct lpfc_sli_ring *pring, uint16_t iotag)
3697 {
3698 	struct lpfc_iocbq *cmd_iocb = NULL;
3699 
3700 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3701 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3702 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3703 			/* remove from txcmpl queue list */
3704 			list_del_init(&cmd_iocb->list);
3705 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3706 			pring->txcmplq_cnt--;
3707 			return cmd_iocb;
3708 		}
3709 	}
3710 
3711 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3712 			"0372 iotag x%x lookup error: max iotag (x%x) "
3713 			"cmd_flag x%x\n",
3714 			iotag, phba->sli.last_iotag,
3715 			cmd_iocb ? cmd_iocb->cmd_flag : 0xffff);
3716 	return NULL;
3717 }
3718 
3719 /**
3720  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3721  * @phba: Pointer to HBA context object.
3722  * @pring: Pointer to driver SLI ring object.
3723  * @saveq: Pointer to the response iocb to be processed.
3724  *
3725  * This function is called by the ring event handler for non-fcp
3726  * rings when there is a new response iocb in the response ring.
3727  * The caller is not required to hold any locks. This function
3728  * gets the command iocb associated with the response iocb and
3729  * calls the completion handler for the command iocb. If there
3730  * is no completion handler, the function will free the resources
3731  * associated with command iocb. If the response iocb is for
3732  * an already aborted command iocb, the status of the completion
3733  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3734  * This function always returns 1.
3735  **/
3736 static int
lpfc_sli_process_sol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3737 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3738 			  struct lpfc_iocbq *saveq)
3739 {
3740 	struct lpfc_iocbq *cmdiocbp;
3741 	unsigned long iflag;
3742 	u32 ulp_command, ulp_status, ulp_word4, ulp_context, iotag;
3743 
3744 	if (phba->sli_rev == LPFC_SLI_REV4)
3745 		spin_lock_irqsave(&pring->ring_lock, iflag);
3746 	else
3747 		spin_lock_irqsave(&phba->hbalock, iflag);
3748 	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3749 	if (phba->sli_rev == LPFC_SLI_REV4)
3750 		spin_unlock_irqrestore(&pring->ring_lock, iflag);
3751 	else
3752 		spin_unlock_irqrestore(&phba->hbalock, iflag);
3753 
3754 	ulp_command = get_job_cmnd(phba, saveq);
3755 	ulp_status = get_job_ulpstatus(phba, saveq);
3756 	ulp_word4 = get_job_word4(phba, saveq);
3757 	ulp_context = get_job_ulpcontext(phba, saveq);
3758 	if (phba->sli_rev == LPFC_SLI_REV4)
3759 		iotag = get_wqe_reqtag(saveq);
3760 	else
3761 		iotag = saveq->iocb.ulpIoTag;
3762 
3763 	if (cmdiocbp) {
3764 		ulp_command = get_job_cmnd(phba, cmdiocbp);
3765 		if (cmdiocbp->cmd_cmpl) {
3766 			/*
3767 			 * If an ELS command failed send an event to mgmt
3768 			 * application.
3769 			 */
3770 			if (ulp_status &&
3771 			     (pring->ringno == LPFC_ELS_RING) &&
3772 			     (ulp_command == CMD_ELS_REQUEST64_CR))
3773 				lpfc_send_els_failure_event(phba,
3774 					cmdiocbp, saveq);
3775 
3776 			/*
3777 			 * Post all ELS completions to the worker thread.
3778 			 * All other are passed to the completion callback.
3779 			 */
3780 			if (pring->ringno == LPFC_ELS_RING) {
3781 				if ((phba->sli_rev < LPFC_SLI_REV4) &&
3782 				    (cmdiocbp->cmd_flag &
3783 							LPFC_DRIVER_ABORTED)) {
3784 					spin_lock_irqsave(&phba->hbalock,
3785 							  iflag);
3786 					cmdiocbp->cmd_flag &=
3787 						~LPFC_DRIVER_ABORTED;
3788 					spin_unlock_irqrestore(&phba->hbalock,
3789 							       iflag);
3790 					saveq->iocb.ulpStatus =
3791 						IOSTAT_LOCAL_REJECT;
3792 					saveq->iocb.un.ulpWord[4] =
3793 						IOERR_SLI_ABORTED;
3794 
3795 					/* Firmware could still be in progress
3796 					 * of DMAing payload, so don't free data
3797 					 * buffer till after a hbeat.
3798 					 */
3799 					spin_lock_irqsave(&phba->hbalock,
3800 							  iflag);
3801 					saveq->cmd_flag |= LPFC_DELAY_MEM_FREE;
3802 					spin_unlock_irqrestore(&phba->hbalock,
3803 							       iflag);
3804 				}
3805 				if (phba->sli_rev == LPFC_SLI_REV4) {
3806 					if (saveq->cmd_flag &
3807 					    LPFC_EXCHANGE_BUSY) {
3808 						/* Set cmdiocb flag for the
3809 						 * exchange busy so sgl (xri)
3810 						 * will not be released until
3811 						 * the abort xri is received
3812 						 * from hba.
3813 						 */
3814 						spin_lock_irqsave(
3815 							&phba->hbalock, iflag);
3816 						cmdiocbp->cmd_flag |=
3817 							LPFC_EXCHANGE_BUSY;
3818 						spin_unlock_irqrestore(
3819 							&phba->hbalock, iflag);
3820 					}
3821 					if (cmdiocbp->cmd_flag &
3822 					    LPFC_DRIVER_ABORTED) {
3823 						/*
3824 						 * Clear LPFC_DRIVER_ABORTED
3825 						 * bit in case it was driver
3826 						 * initiated abort.
3827 						 */
3828 						spin_lock_irqsave(
3829 							&phba->hbalock, iflag);
3830 						cmdiocbp->cmd_flag &=
3831 							~LPFC_DRIVER_ABORTED;
3832 						spin_unlock_irqrestore(
3833 							&phba->hbalock, iflag);
3834 						set_job_ulpstatus(cmdiocbp,
3835 								  IOSTAT_LOCAL_REJECT);
3836 						set_job_ulpword4(cmdiocbp,
3837 								 IOERR_ABORT_REQUESTED);
3838 						/*
3839 						 * For SLI4, irspiocb contains
3840 						 * NO_XRI in sli_xritag, it
3841 						 * shall not affect releasing
3842 						 * sgl (xri) process.
3843 						 */
3844 						set_job_ulpstatus(saveq,
3845 								  IOSTAT_LOCAL_REJECT);
3846 						set_job_ulpword4(saveq,
3847 								 IOERR_SLI_ABORTED);
3848 						spin_lock_irqsave(
3849 							&phba->hbalock, iflag);
3850 						saveq->cmd_flag |=
3851 							LPFC_DELAY_MEM_FREE;
3852 						spin_unlock_irqrestore(
3853 							&phba->hbalock, iflag);
3854 					}
3855 				}
3856 			}
3857 			cmdiocbp->cmd_cmpl(phba, cmdiocbp, saveq);
3858 		} else
3859 			lpfc_sli_release_iocbq(phba, cmdiocbp);
3860 	} else {
3861 		/*
3862 		 * Unknown initiating command based on the response iotag.
3863 		 * This could be the case on the ELS ring because of
3864 		 * lpfc_els_abort().
3865 		 */
3866 		if (pring->ringno != LPFC_ELS_RING) {
3867 			/*
3868 			 * Ring <ringno> handler: unexpected completion IoTag
3869 			 * <IoTag>
3870 			 */
3871 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3872 					 "0322 Ring %d handler: "
3873 					 "unexpected completion IoTag x%x "
3874 					 "Data: x%x x%x x%x x%x\n",
3875 					 pring->ringno, iotag, ulp_status,
3876 					 ulp_word4, ulp_command, ulp_context);
3877 		}
3878 	}
3879 
3880 	return 1;
3881 }
3882 
3883 /**
3884  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3885  * @phba: Pointer to HBA context object.
3886  * @pring: Pointer to driver SLI ring object.
3887  *
3888  * This function is called from the iocb ring event handlers when
3889  * put pointer is ahead of the get pointer for a ring. This function signal
3890  * an error attention condition to the worker thread and the worker
3891  * thread will transition the HBA to offline state.
3892  **/
3893 static void
lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)3894 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3895 {
3896 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3897 	/*
3898 	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3899 	 * rsp ring <portRspMax>
3900 	 */
3901 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3902 			"0312 Ring %d handler: portRspPut %d "
3903 			"is bigger than rsp ring %d\n",
3904 			pring->ringno, le32_to_cpu(pgp->rspPutInx),
3905 			pring->sli.sli3.numRiocb);
3906 
3907 	phba->link_state = LPFC_HBA_ERROR;
3908 
3909 	/*
3910 	 * All error attention handlers are posted to
3911 	 * worker thread
3912 	 */
3913 	phba->work_ha |= HA_ERATT;
3914 	phba->work_hs = HS_FFER3;
3915 
3916 	lpfc_worker_wake_up(phba);
3917 
3918 	return;
3919 }
3920 
3921 /**
3922  * lpfc_poll_eratt - Error attention polling timer timeout handler
3923  * @t: Context to fetch pointer to address of HBA context object from.
3924  *
3925  * This function is invoked by the Error Attention polling timer when the
3926  * timer times out. It will check the SLI Error Attention register for
3927  * possible attention events. If so, it will post an Error Attention event
3928  * and wake up worker thread to process it. Otherwise, it will set up the
3929  * Error Attention polling timer for the next poll.
3930  **/
lpfc_poll_eratt(struct timer_list * t)3931 void lpfc_poll_eratt(struct timer_list *t)
3932 {
3933 	struct lpfc_hba *phba;
3934 	uint32_t eratt = 0;
3935 	uint64_t sli_intr, cnt;
3936 
3937 	phba = from_timer(phba, t, eratt_poll);
3938 	if (!(phba->hba_flag & HBA_SETUP))
3939 		return;
3940 
3941 	/* Here we will also keep track of interrupts per sec of the hba */
3942 	sli_intr = phba->sli.slistat.sli_intr;
3943 
3944 	if (phba->sli.slistat.sli_prev_intr > sli_intr)
3945 		cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3946 			sli_intr);
3947 	else
3948 		cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3949 
3950 	/* 64-bit integer division not supported on 32-bit x86 - use do_div */
3951 	do_div(cnt, phba->eratt_poll_interval);
3952 	phba->sli.slistat.sli_ips = cnt;
3953 
3954 	phba->sli.slistat.sli_prev_intr = sli_intr;
3955 
3956 	/* Check chip HA register for error event */
3957 	eratt = lpfc_sli_check_eratt(phba);
3958 
3959 	if (eratt)
3960 		/* Tell the worker thread there is work to do */
3961 		lpfc_worker_wake_up(phba);
3962 	else
3963 		/* Restart the timer for next eratt poll */
3964 		mod_timer(&phba->eratt_poll,
3965 			  jiffies +
3966 			  msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3967 	return;
3968 }
3969 
3970 
3971 /**
3972  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3973  * @phba: Pointer to HBA context object.
3974  * @pring: Pointer to driver SLI ring object.
3975  * @mask: Host attention register mask for this ring.
3976  *
3977  * This function is called from the interrupt context when there is a ring
3978  * event for the fcp ring. The caller does not hold any lock.
3979  * The function processes each response iocb in the response ring until it
3980  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3981  * LE bit set. The function will call the completion handler of the command iocb
3982  * if the response iocb indicates a completion for a command iocb or it is
3983  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3984  * function if this is an unsolicited iocb.
3985  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3986  * to check it explicitly.
3987  */
3988 int
lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)3989 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3990 				struct lpfc_sli_ring *pring, uint32_t mask)
3991 {
3992 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3993 	IOCB_t *irsp = NULL;
3994 	IOCB_t *entry = NULL;
3995 	struct lpfc_iocbq *cmdiocbq = NULL;
3996 	struct lpfc_iocbq rspiocbq;
3997 	uint32_t status;
3998 	uint32_t portRspPut, portRspMax;
3999 	int rc = 1;
4000 	lpfc_iocb_type type;
4001 	unsigned long iflag;
4002 	uint32_t rsp_cmpl = 0;
4003 
4004 	spin_lock_irqsave(&phba->hbalock, iflag);
4005 	pring->stats.iocb_event++;
4006 
4007 	/*
4008 	 * The next available response entry should never exceed the maximum
4009 	 * entries.  If it does, treat it as an adapter hardware error.
4010 	 */
4011 	portRspMax = pring->sli.sli3.numRiocb;
4012 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4013 	if (unlikely(portRspPut >= portRspMax)) {
4014 		lpfc_sli_rsp_pointers_error(phba, pring);
4015 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4016 		return 1;
4017 	}
4018 	if (phba->fcp_ring_in_use) {
4019 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4020 		return 1;
4021 	} else
4022 		phba->fcp_ring_in_use = 1;
4023 
4024 	rmb();
4025 	while (pring->sli.sli3.rspidx != portRspPut) {
4026 		/*
4027 		 * Fetch an entry off the ring and copy it into a local data
4028 		 * structure.  The copy involves a byte-swap since the
4029 		 * network byte order and pci byte orders are different.
4030 		 */
4031 		entry = lpfc_resp_iocb(phba, pring);
4032 		phba->last_completion_time = jiffies;
4033 
4034 		if (++pring->sli.sli3.rspidx >= portRspMax)
4035 			pring->sli.sli3.rspidx = 0;
4036 
4037 		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
4038 				      (uint32_t *) &rspiocbq.iocb,
4039 				      phba->iocb_rsp_size);
4040 		INIT_LIST_HEAD(&(rspiocbq.list));
4041 		irsp = &rspiocbq.iocb;
4042 
4043 		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
4044 		pring->stats.iocb_rsp++;
4045 		rsp_cmpl++;
4046 
4047 		if (unlikely(irsp->ulpStatus)) {
4048 			/*
4049 			 * If resource errors reported from HBA, reduce
4050 			 * queuedepths of the SCSI device.
4051 			 */
4052 			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
4053 			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4054 			     IOERR_NO_RESOURCES)) {
4055 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4056 				phba->lpfc_rampdown_queue_depth(phba);
4057 				spin_lock_irqsave(&phba->hbalock, iflag);
4058 			}
4059 
4060 			/* Rsp ring <ringno> error: IOCB */
4061 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4062 					"0336 Rsp Ring %d error: IOCB Data: "
4063 					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
4064 					pring->ringno,
4065 					irsp->un.ulpWord[0],
4066 					irsp->un.ulpWord[1],
4067 					irsp->un.ulpWord[2],
4068 					irsp->un.ulpWord[3],
4069 					irsp->un.ulpWord[4],
4070 					irsp->un.ulpWord[5],
4071 					*(uint32_t *)&irsp->un1,
4072 					*((uint32_t *)&irsp->un1 + 1));
4073 		}
4074 
4075 		switch (type) {
4076 		case LPFC_ABORT_IOCB:
4077 		case LPFC_SOL_IOCB:
4078 			/*
4079 			 * Idle exchange closed via ABTS from port.  No iocb
4080 			 * resources need to be recovered.
4081 			 */
4082 			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
4083 				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4084 						"0333 IOCB cmd 0x%x"
4085 						" processed. Skipping"
4086 						" completion\n",
4087 						irsp->ulpCommand);
4088 				break;
4089 			}
4090 
4091 			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
4092 							 &rspiocbq);
4093 			if (unlikely(!cmdiocbq))
4094 				break;
4095 			if (cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED)
4096 				cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
4097 			if (cmdiocbq->cmd_cmpl) {
4098 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4099 				cmdiocbq->cmd_cmpl(phba, cmdiocbq, &rspiocbq);
4100 				spin_lock_irqsave(&phba->hbalock, iflag);
4101 			}
4102 			break;
4103 		case LPFC_UNSOL_IOCB:
4104 			spin_unlock_irqrestore(&phba->hbalock, iflag);
4105 			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
4106 			spin_lock_irqsave(&phba->hbalock, iflag);
4107 			break;
4108 		default:
4109 			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4110 				char adaptermsg[LPFC_MAX_ADPTMSG];
4111 				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4112 				memcpy(&adaptermsg[0], (uint8_t *) irsp,
4113 				       MAX_MSG_DATA);
4114 				dev_warn(&((phba->pcidev)->dev),
4115 					 "lpfc%d: %s\n",
4116 					 phba->brd_no, adaptermsg);
4117 			} else {
4118 				/* Unknown IOCB command */
4119 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4120 						"0334 Unknown IOCB command "
4121 						"Data: x%x, x%x x%x x%x x%x\n",
4122 						type, irsp->ulpCommand,
4123 						irsp->ulpStatus,
4124 						irsp->ulpIoTag,
4125 						irsp->ulpContext);
4126 			}
4127 			break;
4128 		}
4129 
4130 		/*
4131 		 * The response IOCB has been processed.  Update the ring
4132 		 * pointer in SLIM.  If the port response put pointer has not
4133 		 * been updated, sync the pgp->rspPutInx and fetch the new port
4134 		 * response put pointer.
4135 		 */
4136 		writel(pring->sli.sli3.rspidx,
4137 			&phba->host_gp[pring->ringno].rspGetInx);
4138 
4139 		if (pring->sli.sli3.rspidx == portRspPut)
4140 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4141 	}
4142 
4143 	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4144 		pring->stats.iocb_rsp_full++;
4145 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4146 		writel(status, phba->CAregaddr);
4147 		readl(phba->CAregaddr);
4148 	}
4149 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4150 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4151 		pring->stats.iocb_cmd_empty++;
4152 
4153 		/* Force update of the local copy of cmdGetInx */
4154 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4155 		lpfc_sli_resume_iocb(phba, pring);
4156 
4157 		if ((pring->lpfc_sli_cmd_available))
4158 			(pring->lpfc_sli_cmd_available) (phba, pring);
4159 
4160 	}
4161 
4162 	phba->fcp_ring_in_use = 0;
4163 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4164 	return rc;
4165 }
4166 
4167 /**
4168  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4169  * @phba: Pointer to HBA context object.
4170  * @pring: Pointer to driver SLI ring object.
4171  * @rspiocbp: Pointer to driver response IOCB object.
4172  *
4173  * This function is called from the worker thread when there is a slow-path
4174  * response IOCB to process. This function chains all the response iocbs until
4175  * seeing the iocb with the LE bit set. The function will call
4176  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4177  * completion of a command iocb. The function will call the
4178  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4179  * The function frees the resources or calls the completion handler if this
4180  * iocb is an abort completion. The function returns NULL when the response
4181  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4182  * this function shall chain the iocb on to the iocb_continueq and return the
4183  * response iocb passed in.
4184  **/
4185 static struct lpfc_iocbq *
lpfc_sli_sp_handle_rspiocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * rspiocbp)4186 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4187 			struct lpfc_iocbq *rspiocbp)
4188 {
4189 	struct lpfc_iocbq *saveq;
4190 	struct lpfc_iocbq *cmdiocb;
4191 	struct lpfc_iocbq *next_iocb;
4192 	IOCB_t *irsp;
4193 	uint32_t free_saveq;
4194 	u8 cmd_type;
4195 	lpfc_iocb_type type;
4196 	unsigned long iflag;
4197 	u32 ulp_status = get_job_ulpstatus(phba, rspiocbp);
4198 	u32 ulp_word4 = get_job_word4(phba, rspiocbp);
4199 	u32 ulp_command = get_job_cmnd(phba, rspiocbp);
4200 	int rc;
4201 
4202 	spin_lock_irqsave(&phba->hbalock, iflag);
4203 	/* First add the response iocb to the countinueq list */
4204 	list_add_tail(&rspiocbp->list, &pring->iocb_continueq);
4205 	pring->iocb_continueq_cnt++;
4206 
4207 	/*
4208 	 * By default, the driver expects to free all resources
4209 	 * associated with this iocb completion.
4210 	 */
4211 	free_saveq = 1;
4212 	saveq = list_get_first(&pring->iocb_continueq,
4213 			       struct lpfc_iocbq, list);
4214 	list_del_init(&pring->iocb_continueq);
4215 	pring->iocb_continueq_cnt = 0;
4216 
4217 	pring->stats.iocb_rsp++;
4218 
4219 	/*
4220 	 * If resource errors reported from HBA, reduce
4221 	 * queuedepths of the SCSI device.
4222 	 */
4223 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
4224 	    ((ulp_word4 & IOERR_PARAM_MASK) ==
4225 	     IOERR_NO_RESOURCES)) {
4226 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4227 		phba->lpfc_rampdown_queue_depth(phba);
4228 		spin_lock_irqsave(&phba->hbalock, iflag);
4229 	}
4230 
4231 	if (ulp_status) {
4232 		/* Rsp ring <ringno> error: IOCB */
4233 		if (phba->sli_rev < LPFC_SLI_REV4) {
4234 			irsp = &rspiocbp->iocb;
4235 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4236 					"0328 Rsp Ring %d error: ulp_status x%x "
4237 					"IOCB Data: "
4238 					"x%08x x%08x x%08x x%08x "
4239 					"x%08x x%08x x%08x x%08x "
4240 					"x%08x x%08x x%08x x%08x "
4241 					"x%08x x%08x x%08x x%08x\n",
4242 					pring->ringno, ulp_status,
4243 					get_job_ulpword(rspiocbp, 0),
4244 					get_job_ulpword(rspiocbp, 1),
4245 					get_job_ulpword(rspiocbp, 2),
4246 					get_job_ulpword(rspiocbp, 3),
4247 					get_job_ulpword(rspiocbp, 4),
4248 					get_job_ulpword(rspiocbp, 5),
4249 					*(((uint32_t *)irsp) + 6),
4250 					*(((uint32_t *)irsp) + 7),
4251 					*(((uint32_t *)irsp) + 8),
4252 					*(((uint32_t *)irsp) + 9),
4253 					*(((uint32_t *)irsp) + 10),
4254 					*(((uint32_t *)irsp) + 11),
4255 					*(((uint32_t *)irsp) + 12),
4256 					*(((uint32_t *)irsp) + 13),
4257 					*(((uint32_t *)irsp) + 14),
4258 					*(((uint32_t *)irsp) + 15));
4259 		} else {
4260 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4261 					"0321 Rsp Ring %d error: "
4262 					"IOCB Data: "
4263 					"x%x x%x x%x x%x\n",
4264 					pring->ringno,
4265 					rspiocbp->wcqe_cmpl.word0,
4266 					rspiocbp->wcqe_cmpl.total_data_placed,
4267 					rspiocbp->wcqe_cmpl.parameter,
4268 					rspiocbp->wcqe_cmpl.word3);
4269 		}
4270 	}
4271 
4272 
4273 	/*
4274 	 * Fetch the iocb command type and call the correct completion
4275 	 * routine. Solicited and Unsolicited IOCBs on the ELS ring
4276 	 * get freed back to the lpfc_iocb_list by the discovery
4277 	 * kernel thread.
4278 	 */
4279 	cmd_type = ulp_command & CMD_IOCB_MASK;
4280 	type = lpfc_sli_iocb_cmd_type(cmd_type);
4281 	switch (type) {
4282 	case LPFC_SOL_IOCB:
4283 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4284 		rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4285 		spin_lock_irqsave(&phba->hbalock, iflag);
4286 		break;
4287 	case LPFC_UNSOL_IOCB:
4288 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4289 		rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4290 		spin_lock_irqsave(&phba->hbalock, iflag);
4291 		if (!rc)
4292 			free_saveq = 0;
4293 		break;
4294 	case LPFC_ABORT_IOCB:
4295 		cmdiocb = NULL;
4296 		if (ulp_command != CMD_XRI_ABORTED_CX)
4297 			cmdiocb = lpfc_sli_iocbq_lookup(phba, pring,
4298 							saveq);
4299 		if (cmdiocb) {
4300 			/* Call the specified completion routine */
4301 			if (cmdiocb->cmd_cmpl) {
4302 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4303 				cmdiocb->cmd_cmpl(phba, cmdiocb, saveq);
4304 				spin_lock_irqsave(&phba->hbalock, iflag);
4305 			} else {
4306 				__lpfc_sli_release_iocbq(phba, cmdiocb);
4307 			}
4308 		}
4309 		break;
4310 	case LPFC_UNKNOWN_IOCB:
4311 		if (ulp_command == CMD_ADAPTER_MSG) {
4312 			char adaptermsg[LPFC_MAX_ADPTMSG];
4313 
4314 			memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4315 			memcpy(&adaptermsg[0], (uint8_t *)&rspiocbp->wqe,
4316 			       MAX_MSG_DATA);
4317 			dev_warn(&((phba->pcidev)->dev),
4318 				 "lpfc%d: %s\n",
4319 				 phba->brd_no, adaptermsg);
4320 		} else {
4321 			/* Unknown command */
4322 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4323 					"0335 Unknown IOCB "
4324 					"command Data: x%x "
4325 					"x%x x%x x%x\n",
4326 					ulp_command,
4327 					ulp_status,
4328 					get_wqe_reqtag(rspiocbp),
4329 					get_job_ulpcontext(phba, rspiocbp));
4330 		}
4331 		break;
4332 	}
4333 
4334 	if (free_saveq) {
4335 		list_for_each_entry_safe(rspiocbp, next_iocb,
4336 					 &saveq->list, list) {
4337 			list_del_init(&rspiocbp->list);
4338 			__lpfc_sli_release_iocbq(phba, rspiocbp);
4339 		}
4340 		__lpfc_sli_release_iocbq(phba, saveq);
4341 	}
4342 	rspiocbp = NULL;
4343 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4344 	return rspiocbp;
4345 }
4346 
4347 /**
4348  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
4349  * @phba: Pointer to HBA context object.
4350  * @pring: Pointer to driver SLI ring object.
4351  * @mask: Host attention register mask for this ring.
4352  *
4353  * This routine wraps the actual slow_ring event process routine from the
4354  * API jump table function pointer from the lpfc_hba struct.
4355  **/
4356 void
lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4357 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4358 				struct lpfc_sli_ring *pring, uint32_t mask)
4359 {
4360 	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4361 }
4362 
4363 /**
4364  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4365  * @phba: Pointer to HBA context object.
4366  * @pring: Pointer to driver SLI ring object.
4367  * @mask: Host attention register mask for this ring.
4368  *
4369  * This function is called from the worker thread when there is a ring event
4370  * for non-fcp rings. The caller does not hold any lock. The function will
4371  * remove each response iocb in the response ring and calls the handle
4372  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4373  **/
4374 static void
lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4375 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4376 				   struct lpfc_sli_ring *pring, uint32_t mask)
4377 {
4378 	struct lpfc_pgp *pgp;
4379 	IOCB_t *entry;
4380 	IOCB_t *irsp = NULL;
4381 	struct lpfc_iocbq *rspiocbp = NULL;
4382 	uint32_t portRspPut, portRspMax;
4383 	unsigned long iflag;
4384 	uint32_t status;
4385 
4386 	pgp = &phba->port_gp[pring->ringno];
4387 	spin_lock_irqsave(&phba->hbalock, iflag);
4388 	pring->stats.iocb_event++;
4389 
4390 	/*
4391 	 * The next available response entry should never exceed the maximum
4392 	 * entries.  If it does, treat it as an adapter hardware error.
4393 	 */
4394 	portRspMax = pring->sli.sli3.numRiocb;
4395 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4396 	if (portRspPut >= portRspMax) {
4397 		/*
4398 		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4399 		 * rsp ring <portRspMax>
4400 		 */
4401 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4402 				"0303 Ring %d handler: portRspPut %d "
4403 				"is bigger than rsp ring %d\n",
4404 				pring->ringno, portRspPut, portRspMax);
4405 
4406 		phba->link_state = LPFC_HBA_ERROR;
4407 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4408 
4409 		phba->work_hs = HS_FFER3;
4410 		lpfc_handle_eratt(phba);
4411 
4412 		return;
4413 	}
4414 
4415 	rmb();
4416 	while (pring->sli.sli3.rspidx != portRspPut) {
4417 		/*
4418 		 * Build a completion list and call the appropriate handler.
4419 		 * The process is to get the next available response iocb, get
4420 		 * a free iocb from the list, copy the response data into the
4421 		 * free iocb, insert to the continuation list, and update the
4422 		 * next response index to slim.  This process makes response
4423 		 * iocb's in the ring available to DMA as fast as possible but
4424 		 * pays a penalty for a copy operation.  Since the iocb is
4425 		 * only 32 bytes, this penalty is considered small relative to
4426 		 * the PCI reads for register values and a slim write.  When
4427 		 * the ulpLe field is set, the entire Command has been
4428 		 * received.
4429 		 */
4430 		entry = lpfc_resp_iocb(phba, pring);
4431 
4432 		phba->last_completion_time = jiffies;
4433 		rspiocbp = __lpfc_sli_get_iocbq(phba);
4434 		if (rspiocbp == NULL) {
4435 			printk(KERN_ERR "%s: out of buffers! Failing "
4436 			       "completion.\n", __func__);
4437 			break;
4438 		}
4439 
4440 		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4441 				      phba->iocb_rsp_size);
4442 		irsp = &rspiocbp->iocb;
4443 
4444 		if (++pring->sli.sli3.rspidx >= portRspMax)
4445 			pring->sli.sli3.rspidx = 0;
4446 
4447 		if (pring->ringno == LPFC_ELS_RING) {
4448 			lpfc_debugfs_slow_ring_trc(phba,
4449 			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4450 				*(((uint32_t *) irsp) + 4),
4451 				*(((uint32_t *) irsp) + 6),
4452 				*(((uint32_t *) irsp) + 7));
4453 		}
4454 
4455 		writel(pring->sli.sli3.rspidx,
4456 			&phba->host_gp[pring->ringno].rspGetInx);
4457 
4458 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4459 		/* Handle the response IOCB */
4460 		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4461 		spin_lock_irqsave(&phba->hbalock, iflag);
4462 
4463 		/*
4464 		 * If the port response put pointer has not been updated, sync
4465 		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4466 		 * response put pointer.
4467 		 */
4468 		if (pring->sli.sli3.rspidx == portRspPut) {
4469 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4470 		}
4471 	} /* while (pring->sli.sli3.rspidx != portRspPut) */
4472 
4473 	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4474 		/* At least one response entry has been freed */
4475 		pring->stats.iocb_rsp_full++;
4476 		/* SET RxRE_RSP in Chip Att register */
4477 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4478 		writel(status, phba->CAregaddr);
4479 		readl(phba->CAregaddr); /* flush */
4480 	}
4481 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4482 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4483 		pring->stats.iocb_cmd_empty++;
4484 
4485 		/* Force update of the local copy of cmdGetInx */
4486 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4487 		lpfc_sli_resume_iocb(phba, pring);
4488 
4489 		if ((pring->lpfc_sli_cmd_available))
4490 			(pring->lpfc_sli_cmd_available) (phba, pring);
4491 
4492 	}
4493 
4494 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4495 	return;
4496 }
4497 
4498 /**
4499  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4500  * @phba: Pointer to HBA context object.
4501  * @pring: Pointer to driver SLI ring object.
4502  * @mask: Host attention register mask for this ring.
4503  *
4504  * This function is called from the worker thread when there is a pending
4505  * ELS response iocb on the driver internal slow-path response iocb worker
4506  * queue. The caller does not hold any lock. The function will remove each
4507  * response iocb from the response worker queue and calls the handle
4508  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4509  **/
4510 static void
lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4511 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4512 				   struct lpfc_sli_ring *pring, uint32_t mask)
4513 {
4514 	struct lpfc_iocbq *irspiocbq;
4515 	struct hbq_dmabuf *dmabuf;
4516 	struct lpfc_cq_event *cq_event;
4517 	unsigned long iflag;
4518 	int count = 0;
4519 
4520 	spin_lock_irqsave(&phba->hbalock, iflag);
4521 	phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4522 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4523 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4524 		/* Get the response iocb from the head of work queue */
4525 		spin_lock_irqsave(&phba->hbalock, iflag);
4526 		list_remove_head(&phba->sli4_hba.sp_queue_event,
4527 				 cq_event, struct lpfc_cq_event, list);
4528 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4529 
4530 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4531 		case CQE_CODE_COMPL_WQE:
4532 			irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4533 						 cq_event);
4534 			/* Translate ELS WCQE to response IOCBQ */
4535 			irspiocbq = lpfc_sli4_els_preprocess_rspiocbq(phba,
4536 								      irspiocbq);
4537 			if (irspiocbq)
4538 				lpfc_sli_sp_handle_rspiocb(phba, pring,
4539 							   irspiocbq);
4540 			count++;
4541 			break;
4542 		case CQE_CODE_RECEIVE:
4543 		case CQE_CODE_RECEIVE_V1:
4544 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
4545 					      cq_event);
4546 			lpfc_sli4_handle_received_buffer(phba, dmabuf);
4547 			count++;
4548 			break;
4549 		default:
4550 			break;
4551 		}
4552 
4553 		/* Limit the number of events to 64 to avoid soft lockups */
4554 		if (count == 64)
4555 			break;
4556 	}
4557 }
4558 
4559 /**
4560  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4561  * @phba: Pointer to HBA context object.
4562  * @pring: Pointer to driver SLI ring object.
4563  *
4564  * This function aborts all iocbs in the given ring and frees all the iocb
4565  * objects in txq. This function issues an abort iocb for all the iocb commands
4566  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4567  * the return of this function. The caller is not required to hold any locks.
4568  **/
4569 void
lpfc_sli_abort_iocb_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)4570 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4571 {
4572 	LIST_HEAD(tx_completions);
4573 	LIST_HEAD(txcmplq_completions);
4574 	struct lpfc_iocbq *iocb, *next_iocb;
4575 	int offline;
4576 
4577 	if (pring->ringno == LPFC_ELS_RING) {
4578 		lpfc_fabric_abort_hba(phba);
4579 	}
4580 	offline = pci_channel_offline(phba->pcidev);
4581 
4582 	/* Error everything on txq and txcmplq
4583 	 * First do the txq.
4584 	 */
4585 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4586 		spin_lock_irq(&pring->ring_lock);
4587 		list_splice_init(&pring->txq, &tx_completions);
4588 		pring->txq_cnt = 0;
4589 
4590 		if (offline) {
4591 			list_splice_init(&pring->txcmplq,
4592 					 &txcmplq_completions);
4593 		} else {
4594 			/* Next issue ABTS for everything on the txcmplq */
4595 			list_for_each_entry_safe(iocb, next_iocb,
4596 						 &pring->txcmplq, list)
4597 				lpfc_sli_issue_abort_iotag(phba, pring,
4598 							   iocb, NULL);
4599 		}
4600 		spin_unlock_irq(&pring->ring_lock);
4601 	} else {
4602 		spin_lock_irq(&phba->hbalock);
4603 		list_splice_init(&pring->txq, &tx_completions);
4604 		pring->txq_cnt = 0;
4605 
4606 		if (offline) {
4607 			list_splice_init(&pring->txcmplq, &txcmplq_completions);
4608 		} else {
4609 			/* Next issue ABTS for everything on the txcmplq */
4610 			list_for_each_entry_safe(iocb, next_iocb,
4611 						 &pring->txcmplq, list)
4612 				lpfc_sli_issue_abort_iotag(phba, pring,
4613 							   iocb, NULL);
4614 		}
4615 		spin_unlock_irq(&phba->hbalock);
4616 	}
4617 
4618 	if (offline) {
4619 		/* Cancel all the IOCBs from the completions list */
4620 		lpfc_sli_cancel_iocbs(phba, &txcmplq_completions,
4621 				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
4622 	} else {
4623 		/* Make sure HBA is alive */
4624 		lpfc_issue_hb_tmo(phba);
4625 	}
4626 	/* Cancel all the IOCBs from the completions list */
4627 	lpfc_sli_cancel_iocbs(phba, &tx_completions, IOSTAT_LOCAL_REJECT,
4628 			      IOERR_SLI_ABORTED);
4629 }
4630 
4631 /**
4632  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4633  * @phba: Pointer to HBA context object.
4634  *
4635  * This function aborts all iocbs in FCP rings and frees all the iocb
4636  * objects in txq. This function issues an abort iocb for all the iocb commands
4637  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4638  * the return of this function. The caller is not required to hold any locks.
4639  **/
4640 void
lpfc_sli_abort_fcp_rings(struct lpfc_hba * phba)4641 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4642 {
4643 	struct lpfc_sli *psli = &phba->sli;
4644 	struct lpfc_sli_ring  *pring;
4645 	uint32_t i;
4646 
4647 	/* Look on all the FCP Rings for the iotag */
4648 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4649 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4650 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4651 			lpfc_sli_abort_iocb_ring(phba, pring);
4652 		}
4653 	} else {
4654 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4655 		lpfc_sli_abort_iocb_ring(phba, pring);
4656 	}
4657 }
4658 
4659 /**
4660  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4661  * @phba: Pointer to HBA context object.
4662  *
4663  * This function flushes all iocbs in the IO ring and frees all the iocb
4664  * objects in txq and txcmplq. This function will not issue abort iocbs
4665  * for all the iocb commands in txcmplq, they will just be returned with
4666  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4667  * slot has been permanently disabled.
4668  **/
4669 void
lpfc_sli_flush_io_rings(struct lpfc_hba * phba)4670 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4671 {
4672 	LIST_HEAD(txq);
4673 	LIST_HEAD(txcmplq);
4674 	struct lpfc_sli *psli = &phba->sli;
4675 	struct lpfc_sli_ring  *pring;
4676 	uint32_t i;
4677 	struct lpfc_iocbq *piocb, *next_iocb;
4678 
4679 	spin_lock_irq(&phba->hbalock);
4680 	/* Indicate the I/O queues are flushed */
4681 	phba->hba_flag |= HBA_IOQ_FLUSH;
4682 	spin_unlock_irq(&phba->hbalock);
4683 
4684 	/* Look on all the FCP Rings for the iotag */
4685 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4686 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4687 			if (!phba->sli4_hba.hdwq ||
4688 			    !phba->sli4_hba.hdwq[i].io_wq) {
4689 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4690 						"7777 hdwq's deleted %lx "
4691 						"%lx %x %x\n",
4692 						(unsigned long)phba->pport->load_flag,
4693 						(unsigned long)phba->hba_flag,
4694 						phba->link_state,
4695 						phba->sli.sli_flag);
4696 				return;
4697 			}
4698 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4699 
4700 			spin_lock_irq(&pring->ring_lock);
4701 			/* Retrieve everything on txq */
4702 			list_splice_init(&pring->txq, &txq);
4703 			list_for_each_entry_safe(piocb, next_iocb,
4704 						 &pring->txcmplq, list)
4705 				piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4706 			/* Retrieve everything on the txcmplq */
4707 			list_splice_init(&pring->txcmplq, &txcmplq);
4708 			pring->txq_cnt = 0;
4709 			pring->txcmplq_cnt = 0;
4710 			spin_unlock_irq(&pring->ring_lock);
4711 
4712 			/* Flush the txq */
4713 			lpfc_sli_cancel_iocbs(phba, &txq,
4714 					      IOSTAT_LOCAL_REJECT,
4715 					      IOERR_SLI_DOWN);
4716 			/* Flush the txcmplq */
4717 			lpfc_sli_cancel_iocbs(phba, &txcmplq,
4718 					      IOSTAT_LOCAL_REJECT,
4719 					      IOERR_SLI_DOWN);
4720 			if (unlikely(pci_channel_offline(phba->pcidev)))
4721 				lpfc_sli4_io_xri_aborted(phba, NULL, 0);
4722 		}
4723 	} else {
4724 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4725 
4726 		spin_lock_irq(&phba->hbalock);
4727 		/* Retrieve everything on txq */
4728 		list_splice_init(&pring->txq, &txq);
4729 		list_for_each_entry_safe(piocb, next_iocb,
4730 					 &pring->txcmplq, list)
4731 			piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4732 		/* Retrieve everything on the txcmplq */
4733 		list_splice_init(&pring->txcmplq, &txcmplq);
4734 		pring->txq_cnt = 0;
4735 		pring->txcmplq_cnt = 0;
4736 		spin_unlock_irq(&phba->hbalock);
4737 
4738 		/* Flush the txq */
4739 		lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4740 				      IOERR_SLI_DOWN);
4741 		/* Flush the txcmpq */
4742 		lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4743 				      IOERR_SLI_DOWN);
4744 	}
4745 }
4746 
4747 /**
4748  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4749  * @phba: Pointer to HBA context object.
4750  * @mask: Bit mask to be checked.
4751  *
4752  * This function reads the host status register and compares
4753  * with the provided bit mask to check if HBA completed
4754  * the restart. This function will wait in a loop for the
4755  * HBA to complete restart. If the HBA does not restart within
4756  * 15 iterations, the function will reset the HBA again. The
4757  * function returns 1 when HBA fail to restart otherwise returns
4758  * zero.
4759  **/
4760 static int
lpfc_sli_brdready_s3(struct lpfc_hba * phba,uint32_t mask)4761 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4762 {
4763 	uint32_t status;
4764 	int i = 0;
4765 	int retval = 0;
4766 
4767 	/* Read the HBA Host Status Register */
4768 	if (lpfc_readl(phba->HSregaddr, &status))
4769 		return 1;
4770 
4771 	phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4772 
4773 	/*
4774 	 * Check status register every 100ms for 5 retries, then every
4775 	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4776 	 * every 2.5 sec for 4.
4777 	 * Break our of the loop if errors occurred during init.
4778 	 */
4779 	while (((status & mask) != mask) &&
4780 	       !(status & HS_FFERM) &&
4781 	       i++ < 20) {
4782 
4783 		if (i <= 5)
4784 			msleep(10);
4785 		else if (i <= 10)
4786 			msleep(500);
4787 		else
4788 			msleep(2500);
4789 
4790 		if (i == 15) {
4791 				/* Do post */
4792 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4793 			lpfc_sli_brdrestart(phba);
4794 		}
4795 		/* Read the HBA Host Status Register */
4796 		if (lpfc_readl(phba->HSregaddr, &status)) {
4797 			retval = 1;
4798 			break;
4799 		}
4800 	}
4801 
4802 	/* Check to see if any errors occurred during init */
4803 	if ((status & HS_FFERM) || (i >= 20)) {
4804 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4805 				"2751 Adapter failed to restart, "
4806 				"status reg x%x, FW Data: A8 x%x AC x%x\n",
4807 				status,
4808 				readl(phba->MBslimaddr + 0xa8),
4809 				readl(phba->MBslimaddr + 0xac));
4810 		phba->link_state = LPFC_HBA_ERROR;
4811 		retval = 1;
4812 	}
4813 
4814 	return retval;
4815 }
4816 
4817 /**
4818  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4819  * @phba: Pointer to HBA context object.
4820  * @mask: Bit mask to be checked.
4821  *
4822  * This function checks the host status register to check if HBA is
4823  * ready. This function will wait in a loop for the HBA to be ready
4824  * If the HBA is not ready , the function will will reset the HBA PCI
4825  * function again. The function returns 1 when HBA fail to be ready
4826  * otherwise returns zero.
4827  **/
4828 static int
lpfc_sli_brdready_s4(struct lpfc_hba * phba,uint32_t mask)4829 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4830 {
4831 	uint32_t status;
4832 	int retval = 0;
4833 
4834 	/* Read the HBA Host Status Register */
4835 	status = lpfc_sli4_post_status_check(phba);
4836 
4837 	if (status) {
4838 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4839 		lpfc_sli_brdrestart(phba);
4840 		status = lpfc_sli4_post_status_check(phba);
4841 	}
4842 
4843 	/* Check to see if any errors occurred during init */
4844 	if (status) {
4845 		phba->link_state = LPFC_HBA_ERROR;
4846 		retval = 1;
4847 	} else
4848 		phba->sli4_hba.intr_enable = 0;
4849 
4850 	phba->hba_flag &= ~HBA_SETUP;
4851 	return retval;
4852 }
4853 
4854 /**
4855  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4856  * @phba: Pointer to HBA context object.
4857  * @mask: Bit mask to be checked.
4858  *
4859  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4860  * from the API jump table function pointer from the lpfc_hba struct.
4861  **/
4862 int
lpfc_sli_brdready(struct lpfc_hba * phba,uint32_t mask)4863 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4864 {
4865 	return phba->lpfc_sli_brdready(phba, mask);
4866 }
4867 
4868 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4869 
4870 /**
4871  * lpfc_reset_barrier - Make HBA ready for HBA reset
4872  * @phba: Pointer to HBA context object.
4873  *
4874  * This function is called before resetting an HBA. This function is called
4875  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4876  **/
lpfc_reset_barrier(struct lpfc_hba * phba)4877 void lpfc_reset_barrier(struct lpfc_hba *phba)
4878 {
4879 	uint32_t __iomem *resp_buf;
4880 	uint32_t __iomem *mbox_buf;
4881 	volatile struct MAILBOX_word0 mbox;
4882 	uint32_t hc_copy, ha_copy, resp_data;
4883 	int  i;
4884 	uint8_t hdrtype;
4885 
4886 	lockdep_assert_held(&phba->hbalock);
4887 
4888 	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4889 	if (hdrtype != 0x80 ||
4890 	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4891 	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4892 		return;
4893 
4894 	/*
4895 	 * Tell the other part of the chip to suspend temporarily all
4896 	 * its DMA activity.
4897 	 */
4898 	resp_buf = phba->MBslimaddr;
4899 
4900 	/* Disable the error attention */
4901 	if (lpfc_readl(phba->HCregaddr, &hc_copy))
4902 		return;
4903 	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4904 	readl(phba->HCregaddr); /* flush */
4905 	phba->link_flag |= LS_IGNORE_ERATT;
4906 
4907 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
4908 		return;
4909 	if (ha_copy & HA_ERATT) {
4910 		/* Clear Chip error bit */
4911 		writel(HA_ERATT, phba->HAregaddr);
4912 		phba->pport->stopped = 1;
4913 	}
4914 
4915 	mbox.word0 = 0;
4916 	mbox.mbxCommand = MBX_KILL_BOARD;
4917 	mbox.mbxOwner = OWN_CHIP;
4918 
4919 	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4920 	mbox_buf = phba->MBslimaddr;
4921 	writel(mbox.word0, mbox_buf);
4922 
4923 	for (i = 0; i < 50; i++) {
4924 		if (lpfc_readl((resp_buf + 1), &resp_data))
4925 			return;
4926 		if (resp_data != ~(BARRIER_TEST_PATTERN))
4927 			mdelay(1);
4928 		else
4929 			break;
4930 	}
4931 	resp_data = 0;
4932 	if (lpfc_readl((resp_buf + 1), &resp_data))
4933 		return;
4934 	if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4935 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4936 		    phba->pport->stopped)
4937 			goto restore_hc;
4938 		else
4939 			goto clear_errat;
4940 	}
4941 
4942 	mbox.mbxOwner = OWN_HOST;
4943 	resp_data = 0;
4944 	for (i = 0; i < 500; i++) {
4945 		if (lpfc_readl(resp_buf, &resp_data))
4946 			return;
4947 		if (resp_data != mbox.word0)
4948 			mdelay(1);
4949 		else
4950 			break;
4951 	}
4952 
4953 clear_errat:
4954 
4955 	while (++i < 500) {
4956 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
4957 			return;
4958 		if (!(ha_copy & HA_ERATT))
4959 			mdelay(1);
4960 		else
4961 			break;
4962 	}
4963 
4964 	if (readl(phba->HAregaddr) & HA_ERATT) {
4965 		writel(HA_ERATT, phba->HAregaddr);
4966 		phba->pport->stopped = 1;
4967 	}
4968 
4969 restore_hc:
4970 	phba->link_flag &= ~LS_IGNORE_ERATT;
4971 	writel(hc_copy, phba->HCregaddr);
4972 	readl(phba->HCregaddr); /* flush */
4973 }
4974 
4975 /**
4976  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4977  * @phba: Pointer to HBA context object.
4978  *
4979  * This function issues a kill_board mailbox command and waits for
4980  * the error attention interrupt. This function is called for stopping
4981  * the firmware processing. The caller is not required to hold any
4982  * locks. This function calls lpfc_hba_down_post function to free
4983  * any pending commands after the kill. The function will return 1 when it
4984  * fails to kill the board else will return 0.
4985  **/
4986 int
lpfc_sli_brdkill(struct lpfc_hba * phba)4987 lpfc_sli_brdkill(struct lpfc_hba *phba)
4988 {
4989 	struct lpfc_sli *psli;
4990 	LPFC_MBOXQ_t *pmb;
4991 	uint32_t status;
4992 	uint32_t ha_copy;
4993 	int retval;
4994 	int i = 0;
4995 
4996 	psli = &phba->sli;
4997 
4998 	/* Kill HBA */
4999 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5000 			"0329 Kill HBA Data: x%x x%x\n",
5001 			phba->pport->port_state, psli->sli_flag);
5002 
5003 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5004 	if (!pmb)
5005 		return 1;
5006 
5007 	/* Disable the error attention */
5008 	spin_lock_irq(&phba->hbalock);
5009 	if (lpfc_readl(phba->HCregaddr, &status)) {
5010 		spin_unlock_irq(&phba->hbalock);
5011 		mempool_free(pmb, phba->mbox_mem_pool);
5012 		return 1;
5013 	}
5014 	status &= ~HC_ERINT_ENA;
5015 	writel(status, phba->HCregaddr);
5016 	readl(phba->HCregaddr); /* flush */
5017 	phba->link_flag |= LS_IGNORE_ERATT;
5018 	spin_unlock_irq(&phba->hbalock);
5019 
5020 	lpfc_kill_board(phba, pmb);
5021 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5022 	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5023 
5024 	if (retval != MBX_SUCCESS) {
5025 		if (retval != MBX_BUSY)
5026 			mempool_free(pmb, phba->mbox_mem_pool);
5027 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5028 				"2752 KILL_BOARD command failed retval %d\n",
5029 				retval);
5030 		spin_lock_irq(&phba->hbalock);
5031 		phba->link_flag &= ~LS_IGNORE_ERATT;
5032 		spin_unlock_irq(&phba->hbalock);
5033 		return 1;
5034 	}
5035 
5036 	spin_lock_irq(&phba->hbalock);
5037 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5038 	spin_unlock_irq(&phba->hbalock);
5039 
5040 	mempool_free(pmb, phba->mbox_mem_pool);
5041 
5042 	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
5043 	 * attention every 100ms for 3 seconds. If we don't get ERATT after
5044 	 * 3 seconds we still set HBA_ERROR state because the status of the
5045 	 * board is now undefined.
5046 	 */
5047 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
5048 		return 1;
5049 	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
5050 		mdelay(100);
5051 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
5052 			return 1;
5053 	}
5054 
5055 	del_timer_sync(&psli->mbox_tmo);
5056 	if (ha_copy & HA_ERATT) {
5057 		writel(HA_ERATT, phba->HAregaddr);
5058 		phba->pport->stopped = 1;
5059 	}
5060 	spin_lock_irq(&phba->hbalock);
5061 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5062 	psli->mbox_active = NULL;
5063 	phba->link_flag &= ~LS_IGNORE_ERATT;
5064 	spin_unlock_irq(&phba->hbalock);
5065 
5066 	lpfc_hba_down_post(phba);
5067 	phba->link_state = LPFC_HBA_ERROR;
5068 
5069 	return ha_copy & HA_ERATT ? 0 : 1;
5070 }
5071 
5072 /**
5073  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
5074  * @phba: Pointer to HBA context object.
5075  *
5076  * This function resets the HBA by writing HC_INITFF to the control
5077  * register. After the HBA resets, this function resets all the iocb ring
5078  * indices. This function disables PCI layer parity checking during
5079  * the reset.
5080  * This function returns 0 always.
5081  * The caller is not required to hold any locks.
5082  **/
5083 int
lpfc_sli_brdreset(struct lpfc_hba * phba)5084 lpfc_sli_brdreset(struct lpfc_hba *phba)
5085 {
5086 	struct lpfc_sli *psli;
5087 	struct lpfc_sli_ring *pring;
5088 	uint16_t cfg_value;
5089 	int i;
5090 
5091 	psli = &phba->sli;
5092 
5093 	/* Reset HBA */
5094 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5095 			"0325 Reset HBA Data: x%x x%x\n",
5096 			(phba->pport) ? phba->pport->port_state : 0,
5097 			psli->sli_flag);
5098 
5099 	/* perform board reset */
5100 	phba->fc_eventTag = 0;
5101 	phba->link_events = 0;
5102 	phba->hba_flag |= HBA_NEEDS_CFG_PORT;
5103 	if (phba->pport) {
5104 		phba->pport->fc_myDID = 0;
5105 		phba->pport->fc_prevDID = 0;
5106 	}
5107 
5108 	/* Turn off parity checking and serr during the physical reset */
5109 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
5110 		return -EIO;
5111 
5112 	pci_write_config_word(phba->pcidev, PCI_COMMAND,
5113 			      (cfg_value &
5114 			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5115 
5116 	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
5117 
5118 	/* Now toggle INITFF bit in the Host Control Register */
5119 	writel(HC_INITFF, phba->HCregaddr);
5120 	mdelay(1);
5121 	readl(phba->HCregaddr); /* flush */
5122 	writel(0, phba->HCregaddr);
5123 	readl(phba->HCregaddr); /* flush */
5124 
5125 	/* Restore PCI cmd register */
5126 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5127 
5128 	/* Initialize relevant SLI info */
5129 	for (i = 0; i < psli->num_rings; i++) {
5130 		pring = &psli->sli3_ring[i];
5131 		pring->flag = 0;
5132 		pring->sli.sli3.rspidx = 0;
5133 		pring->sli.sli3.next_cmdidx  = 0;
5134 		pring->sli.sli3.local_getidx = 0;
5135 		pring->sli.sli3.cmdidx = 0;
5136 		pring->missbufcnt = 0;
5137 	}
5138 
5139 	phba->link_state = LPFC_WARM_START;
5140 	return 0;
5141 }
5142 
5143 /**
5144  * lpfc_sli4_brdreset - Reset a sli-4 HBA
5145  * @phba: Pointer to HBA context object.
5146  *
5147  * This function resets a SLI4 HBA. This function disables PCI layer parity
5148  * checking during resets the device. The caller is not required to hold
5149  * any locks.
5150  *
5151  * This function returns 0 on success else returns negative error code.
5152  **/
5153 int
lpfc_sli4_brdreset(struct lpfc_hba * phba)5154 lpfc_sli4_brdreset(struct lpfc_hba *phba)
5155 {
5156 	struct lpfc_sli *psli = &phba->sli;
5157 	uint16_t cfg_value;
5158 	int rc = 0;
5159 
5160 	/* Reset HBA */
5161 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5162 			"0295 Reset HBA Data: x%x x%x x%x\n",
5163 			phba->pport->port_state, psli->sli_flag,
5164 			phba->hba_flag);
5165 
5166 	/* perform board reset */
5167 	phba->fc_eventTag = 0;
5168 	phba->link_events = 0;
5169 	phba->pport->fc_myDID = 0;
5170 	phba->pport->fc_prevDID = 0;
5171 	phba->hba_flag &= ~HBA_SETUP;
5172 
5173 	spin_lock_irq(&phba->hbalock);
5174 	psli->sli_flag &= ~(LPFC_PROCESS_LA);
5175 	phba->fcf.fcf_flag = 0;
5176 	spin_unlock_irq(&phba->hbalock);
5177 
5178 	/* Now physically reset the device */
5179 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5180 			"0389 Performing PCI function reset!\n");
5181 
5182 	/* Turn off parity checking and serr during the physical reset */
5183 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5184 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5185 				"3205 PCI read Config failed\n");
5186 		return -EIO;
5187 	}
5188 
5189 	pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5190 			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5191 
5192 	/* Perform FCoE PCI function reset before freeing queue memory */
5193 	rc = lpfc_pci_function_reset(phba);
5194 
5195 	/* Restore PCI cmd register */
5196 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5197 
5198 	return rc;
5199 }
5200 
5201 /**
5202  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
5203  * @phba: Pointer to HBA context object.
5204  *
5205  * This function is called in the SLI initialization code path to
5206  * restart the HBA. The caller is not required to hold any lock.
5207  * This function writes MBX_RESTART mailbox command to the SLIM and
5208  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5209  * function to free any pending commands. The function enables
5210  * POST only during the first initialization. The function returns zero.
5211  * The function does not guarantee completion of MBX_RESTART mailbox
5212  * command before the return of this function.
5213  **/
5214 static int
lpfc_sli_brdrestart_s3(struct lpfc_hba * phba)5215 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
5216 {
5217 	volatile struct MAILBOX_word0 mb;
5218 	struct lpfc_sli *psli;
5219 	void __iomem *to_slim;
5220 
5221 	spin_lock_irq(&phba->hbalock);
5222 
5223 	psli = &phba->sli;
5224 
5225 	/* Restart HBA */
5226 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5227 			"0337 Restart HBA Data: x%x x%x\n",
5228 			(phba->pport) ? phba->pport->port_state : 0,
5229 			psli->sli_flag);
5230 
5231 	mb.word0 = 0;
5232 	mb.mbxCommand = MBX_RESTART;
5233 	mb.mbxHc = 1;
5234 
5235 	lpfc_reset_barrier(phba);
5236 
5237 	to_slim = phba->MBslimaddr;
5238 	writel(mb.word0, to_slim);
5239 	readl(to_slim); /* flush */
5240 
5241 	/* Only skip post after fc_ffinit is completed */
5242 	if (phba->pport && phba->pport->port_state)
5243 		mb.word0 = 1;	/* This is really setting up word1 */
5244 	else
5245 		mb.word0 = 0;	/* This is really setting up word1 */
5246 	to_slim = phba->MBslimaddr + sizeof (uint32_t);
5247 	writel(mb.word0, to_slim);
5248 	readl(to_slim); /* flush */
5249 
5250 	lpfc_sli_brdreset(phba);
5251 	if (phba->pport)
5252 		phba->pport->stopped = 0;
5253 	phba->link_state = LPFC_INIT_START;
5254 	phba->hba_flag = 0;
5255 	spin_unlock_irq(&phba->hbalock);
5256 
5257 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5258 	psli->stats_start = ktime_get_seconds();
5259 
5260 	/* Give the INITFF and Post time to settle. */
5261 	mdelay(100);
5262 
5263 	lpfc_hba_down_post(phba);
5264 
5265 	return 0;
5266 }
5267 
5268 /**
5269  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5270  * @phba: Pointer to HBA context object.
5271  *
5272  * This function is called in the SLI initialization code path to restart
5273  * a SLI4 HBA. The caller is not required to hold any lock.
5274  * At the end of the function, it calls lpfc_hba_down_post function to
5275  * free any pending commands.
5276  **/
5277 static int
lpfc_sli_brdrestart_s4(struct lpfc_hba * phba)5278 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5279 {
5280 	struct lpfc_sli *psli = &phba->sli;
5281 	int rc;
5282 
5283 	/* Restart HBA */
5284 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5285 			"0296 Restart HBA Data: x%x x%x\n",
5286 			phba->pport->port_state, psli->sli_flag);
5287 
5288 	lpfc_sli4_queue_unset(phba);
5289 
5290 	rc = lpfc_sli4_brdreset(phba);
5291 	if (rc) {
5292 		phba->link_state = LPFC_HBA_ERROR;
5293 		goto hba_down_queue;
5294 	}
5295 
5296 	spin_lock_irq(&phba->hbalock);
5297 	phba->pport->stopped = 0;
5298 	phba->link_state = LPFC_INIT_START;
5299 	phba->hba_flag = 0;
5300 	/* Preserve FA-PWWN expectation */
5301 	phba->sli4_hba.fawwpn_flag &= LPFC_FAWWPN_FABRIC;
5302 	spin_unlock_irq(&phba->hbalock);
5303 
5304 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5305 	psli->stats_start = ktime_get_seconds();
5306 
5307 hba_down_queue:
5308 	lpfc_hba_down_post(phba);
5309 	lpfc_sli4_queue_destroy(phba);
5310 
5311 	return rc;
5312 }
5313 
5314 /**
5315  * lpfc_sli_brdrestart - Wrapper func for restarting hba
5316  * @phba: Pointer to HBA context object.
5317  *
5318  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5319  * API jump table function pointer from the lpfc_hba struct.
5320 **/
5321 int
lpfc_sli_brdrestart(struct lpfc_hba * phba)5322 lpfc_sli_brdrestart(struct lpfc_hba *phba)
5323 {
5324 	return phba->lpfc_sli_brdrestart(phba);
5325 }
5326 
5327 /**
5328  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
5329  * @phba: Pointer to HBA context object.
5330  *
5331  * This function is called after a HBA restart to wait for successful
5332  * restart of the HBA. Successful restart of the HBA is indicated by
5333  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5334  * iteration, the function will restart the HBA again. The function returns
5335  * zero if HBA successfully restarted else returns negative error code.
5336  **/
5337 int
lpfc_sli_chipset_init(struct lpfc_hba * phba)5338 lpfc_sli_chipset_init(struct lpfc_hba *phba)
5339 {
5340 	uint32_t status, i = 0;
5341 
5342 	/* Read the HBA Host Status Register */
5343 	if (lpfc_readl(phba->HSregaddr, &status))
5344 		return -EIO;
5345 
5346 	/* Check status register to see what current state is */
5347 	i = 0;
5348 	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5349 
5350 		/* Check every 10ms for 10 retries, then every 100ms for 90
5351 		 * retries, then every 1 sec for 50 retires for a total of
5352 		 * ~60 seconds before reset the board again and check every
5353 		 * 1 sec for 50 retries. The up to 60 seconds before the
5354 		 * board ready is required by the Falcon FIPS zeroization
5355 		 * complete, and any reset the board in between shall cause
5356 		 * restart of zeroization, further delay the board ready.
5357 		 */
5358 		if (i++ >= 200) {
5359 			/* Adapter failed to init, timeout, status reg
5360 			   <status> */
5361 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5362 					"0436 Adapter failed to init, "
5363 					"timeout, status reg x%x, "
5364 					"FW Data: A8 x%x AC x%x\n", status,
5365 					readl(phba->MBslimaddr + 0xa8),
5366 					readl(phba->MBslimaddr + 0xac));
5367 			phba->link_state = LPFC_HBA_ERROR;
5368 			return -ETIMEDOUT;
5369 		}
5370 
5371 		/* Check to see if any errors occurred during init */
5372 		if (status & HS_FFERM) {
5373 			/* ERROR: During chipset initialization */
5374 			/* Adapter failed to init, chipset, status reg
5375 			   <status> */
5376 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5377 					"0437 Adapter failed to init, "
5378 					"chipset, status reg x%x, "
5379 					"FW Data: A8 x%x AC x%x\n", status,
5380 					readl(phba->MBslimaddr + 0xa8),
5381 					readl(phba->MBslimaddr + 0xac));
5382 			phba->link_state = LPFC_HBA_ERROR;
5383 			return -EIO;
5384 		}
5385 
5386 		if (i <= 10)
5387 			msleep(10);
5388 		else if (i <= 100)
5389 			msleep(100);
5390 		else
5391 			msleep(1000);
5392 
5393 		if (i == 150) {
5394 			/* Do post */
5395 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5396 			lpfc_sli_brdrestart(phba);
5397 		}
5398 		/* Read the HBA Host Status Register */
5399 		if (lpfc_readl(phba->HSregaddr, &status))
5400 			return -EIO;
5401 	}
5402 
5403 	/* Check to see if any errors occurred during init */
5404 	if (status & HS_FFERM) {
5405 		/* ERROR: During chipset initialization */
5406 		/* Adapter failed to init, chipset, status reg <status> */
5407 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5408 				"0438 Adapter failed to init, chipset, "
5409 				"status reg x%x, "
5410 				"FW Data: A8 x%x AC x%x\n", status,
5411 				readl(phba->MBslimaddr + 0xa8),
5412 				readl(phba->MBslimaddr + 0xac));
5413 		phba->link_state = LPFC_HBA_ERROR;
5414 		return -EIO;
5415 	}
5416 
5417 	phba->hba_flag |= HBA_NEEDS_CFG_PORT;
5418 
5419 	/* Clear all interrupt enable conditions */
5420 	writel(0, phba->HCregaddr);
5421 	readl(phba->HCregaddr); /* flush */
5422 
5423 	/* setup host attn register */
5424 	writel(0xffffffff, phba->HAregaddr);
5425 	readl(phba->HAregaddr); /* flush */
5426 	return 0;
5427 }
5428 
5429 /**
5430  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5431  *
5432  * This function calculates and returns the number of HBQs required to be
5433  * configured.
5434  **/
5435 int
lpfc_sli_hbq_count(void)5436 lpfc_sli_hbq_count(void)
5437 {
5438 	return ARRAY_SIZE(lpfc_hbq_defs);
5439 }
5440 
5441 /**
5442  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5443  *
5444  * This function adds the number of hbq entries in every HBQ to get
5445  * the total number of hbq entries required for the HBA and returns
5446  * the total count.
5447  **/
5448 static int
lpfc_sli_hbq_entry_count(void)5449 lpfc_sli_hbq_entry_count(void)
5450 {
5451 	int  hbq_count = lpfc_sli_hbq_count();
5452 	int  count = 0;
5453 	int  i;
5454 
5455 	for (i = 0; i < hbq_count; ++i)
5456 		count += lpfc_hbq_defs[i]->entry_count;
5457 	return count;
5458 }
5459 
5460 /**
5461  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5462  *
5463  * This function calculates amount of memory required for all hbq entries
5464  * to be configured and returns the total memory required.
5465  **/
5466 int
lpfc_sli_hbq_size(void)5467 lpfc_sli_hbq_size(void)
5468 {
5469 	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5470 }
5471 
5472 /**
5473  * lpfc_sli_hbq_setup - configure and initialize HBQs
5474  * @phba: Pointer to HBA context object.
5475  *
5476  * This function is called during the SLI initialization to configure
5477  * all the HBQs and post buffers to the HBQ. The caller is not
5478  * required to hold any locks. This function will return zero if successful
5479  * else it will return negative error code.
5480  **/
5481 static int
lpfc_sli_hbq_setup(struct lpfc_hba * phba)5482 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5483 {
5484 	int  hbq_count = lpfc_sli_hbq_count();
5485 	LPFC_MBOXQ_t *pmb;
5486 	MAILBOX_t *pmbox;
5487 	uint32_t hbqno;
5488 	uint32_t hbq_entry_index;
5489 
5490 				/* Get a Mailbox buffer to setup mailbox
5491 				 * commands for HBA initialization
5492 				 */
5493 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5494 
5495 	if (!pmb)
5496 		return -ENOMEM;
5497 
5498 	pmbox = &pmb->u.mb;
5499 
5500 	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
5501 	phba->link_state = LPFC_INIT_MBX_CMDS;
5502 	phba->hbq_in_use = 1;
5503 
5504 	hbq_entry_index = 0;
5505 	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5506 		phba->hbqs[hbqno].next_hbqPutIdx = 0;
5507 		phba->hbqs[hbqno].hbqPutIdx      = 0;
5508 		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5509 		phba->hbqs[hbqno].entry_count =
5510 			lpfc_hbq_defs[hbqno]->entry_count;
5511 		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5512 			hbq_entry_index, pmb);
5513 		hbq_entry_index += phba->hbqs[hbqno].entry_count;
5514 
5515 		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5516 			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5517 			   mbxStatus <status>, ring <num> */
5518 
5519 			lpfc_printf_log(phba, KERN_ERR,
5520 					LOG_SLI | LOG_VPORT,
5521 					"1805 Adapter failed to init. "
5522 					"Data: x%x x%x x%x\n",
5523 					pmbox->mbxCommand,
5524 					pmbox->mbxStatus, hbqno);
5525 
5526 			phba->link_state = LPFC_HBA_ERROR;
5527 			mempool_free(pmb, phba->mbox_mem_pool);
5528 			return -ENXIO;
5529 		}
5530 	}
5531 	phba->hbq_count = hbq_count;
5532 
5533 	mempool_free(pmb, phba->mbox_mem_pool);
5534 
5535 	/* Initially populate or replenish the HBQs */
5536 	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5537 		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5538 	return 0;
5539 }
5540 
5541 /**
5542  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5543  * @phba: Pointer to HBA context object.
5544  *
5545  * This function is called during the SLI initialization to configure
5546  * all the HBQs and post buffers to the HBQ. The caller is not
5547  * required to hold any locks. This function will return zero if successful
5548  * else it will return negative error code.
5549  **/
5550 static int
lpfc_sli4_rb_setup(struct lpfc_hba * phba)5551 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5552 {
5553 	phba->hbq_in_use = 1;
5554 	/**
5555 	 * Specific case when the MDS diagnostics is enabled and supported.
5556 	 * The receive buffer count is truncated to manage the incoming
5557 	 * traffic.
5558 	 **/
5559 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5560 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5561 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5562 	else
5563 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5564 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5565 	phba->hbq_count = 1;
5566 	lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5567 	/* Initially populate or replenish the HBQs */
5568 	return 0;
5569 }
5570 
5571 /**
5572  * lpfc_sli_config_port - Issue config port mailbox command
5573  * @phba: Pointer to HBA context object.
5574  * @sli_mode: sli mode - 2/3
5575  *
5576  * This function is called by the sli initialization code path
5577  * to issue config_port mailbox command. This function restarts the
5578  * HBA firmware and issues a config_port mailbox command to configure
5579  * the SLI interface in the sli mode specified by sli_mode
5580  * variable. The caller is not required to hold any locks.
5581  * The function returns 0 if successful, else returns negative error
5582  * code.
5583  **/
5584 int
lpfc_sli_config_port(struct lpfc_hba * phba,int sli_mode)5585 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5586 {
5587 	LPFC_MBOXQ_t *pmb;
5588 	uint32_t resetcount = 0, rc = 0, done = 0;
5589 
5590 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5591 	if (!pmb) {
5592 		phba->link_state = LPFC_HBA_ERROR;
5593 		return -ENOMEM;
5594 	}
5595 
5596 	phba->sli_rev = sli_mode;
5597 	while (resetcount < 2 && !done) {
5598 		spin_lock_irq(&phba->hbalock);
5599 		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5600 		spin_unlock_irq(&phba->hbalock);
5601 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5602 		lpfc_sli_brdrestart(phba);
5603 		rc = lpfc_sli_chipset_init(phba);
5604 		if (rc)
5605 			break;
5606 
5607 		spin_lock_irq(&phba->hbalock);
5608 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5609 		spin_unlock_irq(&phba->hbalock);
5610 		resetcount++;
5611 
5612 		/* Call pre CONFIG_PORT mailbox command initialization.  A
5613 		 * value of 0 means the call was successful.  Any other
5614 		 * nonzero value is a failure, but if ERESTART is returned,
5615 		 * the driver may reset the HBA and try again.
5616 		 */
5617 		rc = lpfc_config_port_prep(phba);
5618 		if (rc == -ERESTART) {
5619 			phba->link_state = LPFC_LINK_UNKNOWN;
5620 			continue;
5621 		} else if (rc)
5622 			break;
5623 
5624 		phba->link_state = LPFC_INIT_MBX_CMDS;
5625 		lpfc_config_port(phba, pmb);
5626 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5627 		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5628 					LPFC_SLI3_HBQ_ENABLED |
5629 					LPFC_SLI3_CRP_ENABLED |
5630 					LPFC_SLI3_DSS_ENABLED);
5631 		if (rc != MBX_SUCCESS) {
5632 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5633 				"0442 Adapter failed to init, mbxCmd x%x "
5634 				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5635 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5636 			spin_lock_irq(&phba->hbalock);
5637 			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5638 			spin_unlock_irq(&phba->hbalock);
5639 			rc = -ENXIO;
5640 		} else {
5641 			/* Allow asynchronous mailbox command to go through */
5642 			spin_lock_irq(&phba->hbalock);
5643 			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5644 			spin_unlock_irq(&phba->hbalock);
5645 			done = 1;
5646 
5647 			if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5648 			    (pmb->u.mb.un.varCfgPort.gasabt == 0))
5649 				lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5650 					"3110 Port did not grant ASABT\n");
5651 		}
5652 	}
5653 	if (!done) {
5654 		rc = -EINVAL;
5655 		goto do_prep_failed;
5656 	}
5657 	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5658 		if (!pmb->u.mb.un.varCfgPort.cMA) {
5659 			rc = -ENXIO;
5660 			goto do_prep_failed;
5661 		}
5662 		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5663 			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5664 			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5665 			phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5666 				phba->max_vpi : phba->max_vports;
5667 
5668 		} else
5669 			phba->max_vpi = 0;
5670 		if (pmb->u.mb.un.varCfgPort.gerbm)
5671 			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5672 		if (pmb->u.mb.un.varCfgPort.gcrp)
5673 			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5674 
5675 		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5676 		phba->port_gp = phba->mbox->us.s3_pgp.port;
5677 
5678 		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5679 			if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5680 				phba->cfg_enable_bg = 0;
5681 				phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5682 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5683 						"0443 Adapter did not grant "
5684 						"BlockGuard\n");
5685 			}
5686 		}
5687 	} else {
5688 		phba->hbq_get = NULL;
5689 		phba->port_gp = phba->mbox->us.s2.port;
5690 		phba->max_vpi = 0;
5691 	}
5692 do_prep_failed:
5693 	mempool_free(pmb, phba->mbox_mem_pool);
5694 	return rc;
5695 }
5696 
5697 
5698 /**
5699  * lpfc_sli_hba_setup - SLI initialization function
5700  * @phba: Pointer to HBA context object.
5701  *
5702  * This function is the main SLI initialization function. This function
5703  * is called by the HBA initialization code, HBA reset code and HBA
5704  * error attention handler code. Caller is not required to hold any
5705  * locks. This function issues config_port mailbox command to configure
5706  * the SLI, setup iocb rings and HBQ rings. In the end the function
5707  * calls the config_port_post function to issue init_link mailbox
5708  * command and to start the discovery. The function will return zero
5709  * if successful, else it will return negative error code.
5710  **/
5711 int
lpfc_sli_hba_setup(struct lpfc_hba * phba)5712 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5713 {
5714 	uint32_t rc;
5715 	int  i;
5716 	int longs;
5717 
5718 	/* Enable ISR already does config_port because of config_msi mbx */
5719 	if (phba->hba_flag & HBA_NEEDS_CFG_PORT) {
5720 		rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5721 		if (rc)
5722 			return -EIO;
5723 		phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
5724 	}
5725 	phba->fcp_embed_io = 0;	/* SLI4 FC support only */
5726 
5727 	if (phba->sli_rev == 3) {
5728 		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5729 		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5730 	} else {
5731 		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5732 		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5733 		phba->sli3_options = 0;
5734 	}
5735 
5736 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5737 			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
5738 			phba->sli_rev, phba->max_vpi);
5739 	rc = lpfc_sli_ring_map(phba);
5740 
5741 	if (rc)
5742 		goto lpfc_sli_hba_setup_error;
5743 
5744 	/* Initialize VPIs. */
5745 	if (phba->sli_rev == LPFC_SLI_REV3) {
5746 		/*
5747 		 * The VPI bitmask and physical ID array are allocated
5748 		 * and initialized once only - at driver load.  A port
5749 		 * reset doesn't need to reinitialize this memory.
5750 		 */
5751 		if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5752 			longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5753 			phba->vpi_bmask = kcalloc(longs,
5754 						  sizeof(unsigned long),
5755 						  GFP_KERNEL);
5756 			if (!phba->vpi_bmask) {
5757 				rc = -ENOMEM;
5758 				goto lpfc_sli_hba_setup_error;
5759 			}
5760 
5761 			phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5762 						sizeof(uint16_t),
5763 						GFP_KERNEL);
5764 			if (!phba->vpi_ids) {
5765 				kfree(phba->vpi_bmask);
5766 				rc = -ENOMEM;
5767 				goto lpfc_sli_hba_setup_error;
5768 			}
5769 			for (i = 0; i < phba->max_vpi; i++)
5770 				phba->vpi_ids[i] = i;
5771 		}
5772 	}
5773 
5774 	/* Init HBQs */
5775 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5776 		rc = lpfc_sli_hbq_setup(phba);
5777 		if (rc)
5778 			goto lpfc_sli_hba_setup_error;
5779 	}
5780 	spin_lock_irq(&phba->hbalock);
5781 	phba->sli.sli_flag |= LPFC_PROCESS_LA;
5782 	spin_unlock_irq(&phba->hbalock);
5783 
5784 	rc = lpfc_config_port_post(phba);
5785 	if (rc)
5786 		goto lpfc_sli_hba_setup_error;
5787 
5788 	return rc;
5789 
5790 lpfc_sli_hba_setup_error:
5791 	phba->link_state = LPFC_HBA_ERROR;
5792 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5793 			"0445 Firmware initialization failed\n");
5794 	return rc;
5795 }
5796 
5797 /**
5798  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5799  * @phba: Pointer to HBA context object.
5800  *
5801  * This function issue a dump mailbox command to read config region
5802  * 23 and parse the records in the region and populate driver
5803  * data structure.
5804  **/
5805 static int
lpfc_sli4_read_fcoe_params(struct lpfc_hba * phba)5806 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5807 {
5808 	LPFC_MBOXQ_t *mboxq;
5809 	struct lpfc_dmabuf *mp;
5810 	struct lpfc_mqe *mqe;
5811 	uint32_t data_length;
5812 	int rc;
5813 
5814 	/* Program the default value of vlan_id and fc_map */
5815 	phba->valid_vlan = 0;
5816 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5817 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5818 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5819 
5820 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5821 	if (!mboxq)
5822 		return -ENOMEM;
5823 
5824 	mqe = &mboxq->u.mqe;
5825 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5826 		rc = -ENOMEM;
5827 		goto out_free_mboxq;
5828 	}
5829 
5830 	mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
5831 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5832 
5833 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5834 			"(%d):2571 Mailbox cmd x%x Status x%x "
5835 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5836 			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5837 			"CQ: x%x x%x x%x x%x\n",
5838 			mboxq->vport ? mboxq->vport->vpi : 0,
5839 			bf_get(lpfc_mqe_command, mqe),
5840 			bf_get(lpfc_mqe_status, mqe),
5841 			mqe->un.mb_words[0], mqe->un.mb_words[1],
5842 			mqe->un.mb_words[2], mqe->un.mb_words[3],
5843 			mqe->un.mb_words[4], mqe->un.mb_words[5],
5844 			mqe->un.mb_words[6], mqe->un.mb_words[7],
5845 			mqe->un.mb_words[8], mqe->un.mb_words[9],
5846 			mqe->un.mb_words[10], mqe->un.mb_words[11],
5847 			mqe->un.mb_words[12], mqe->un.mb_words[13],
5848 			mqe->un.mb_words[14], mqe->un.mb_words[15],
5849 			mqe->un.mb_words[16], mqe->un.mb_words[50],
5850 			mboxq->mcqe.word0,
5851 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
5852 			mboxq->mcqe.trailer);
5853 
5854 	if (rc) {
5855 		rc = -EIO;
5856 		goto out_free_mboxq;
5857 	}
5858 	data_length = mqe->un.mb_words[5];
5859 	if (data_length > DMP_RGN23_SIZE) {
5860 		rc = -EIO;
5861 		goto out_free_mboxq;
5862 	}
5863 
5864 	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5865 	rc = 0;
5866 
5867 out_free_mboxq:
5868 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
5869 	return rc;
5870 }
5871 
5872 /**
5873  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5874  * @phba: pointer to lpfc hba data structure.
5875  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5876  * @vpd: pointer to the memory to hold resulting port vpd data.
5877  * @vpd_size: On input, the number of bytes allocated to @vpd.
5878  *	      On output, the number of data bytes in @vpd.
5879  *
5880  * This routine executes a READ_REV SLI4 mailbox command.  In
5881  * addition, this routine gets the port vpd data.
5882  *
5883  * Return codes
5884  * 	0 - successful
5885  * 	-ENOMEM - could not allocated memory.
5886  **/
5887 static int
lpfc_sli4_read_rev(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint8_t * vpd,uint32_t * vpd_size)5888 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5889 		    uint8_t *vpd, uint32_t *vpd_size)
5890 {
5891 	int rc = 0;
5892 	uint32_t dma_size;
5893 	struct lpfc_dmabuf *dmabuf;
5894 	struct lpfc_mqe *mqe;
5895 
5896 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5897 	if (!dmabuf)
5898 		return -ENOMEM;
5899 
5900 	/*
5901 	 * Get a DMA buffer for the vpd data resulting from the READ_REV
5902 	 * mailbox command.
5903 	 */
5904 	dma_size = *vpd_size;
5905 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5906 					  &dmabuf->phys, GFP_KERNEL);
5907 	if (!dmabuf->virt) {
5908 		kfree(dmabuf);
5909 		return -ENOMEM;
5910 	}
5911 
5912 	/*
5913 	 * The SLI4 implementation of READ_REV conflicts at word1,
5914 	 * bits 31:16 and SLI4 adds vpd functionality not present
5915 	 * in SLI3.  This code corrects the conflicts.
5916 	 */
5917 	lpfc_read_rev(phba, mboxq);
5918 	mqe = &mboxq->u.mqe;
5919 	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5920 	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5921 	mqe->un.read_rev.word1 &= 0x0000FFFF;
5922 	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5923 	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5924 
5925 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5926 	if (rc) {
5927 		dma_free_coherent(&phba->pcidev->dev, dma_size,
5928 				  dmabuf->virt, dmabuf->phys);
5929 		kfree(dmabuf);
5930 		return -EIO;
5931 	}
5932 
5933 	/*
5934 	 * The available vpd length cannot be bigger than the
5935 	 * DMA buffer passed to the port.  Catch the less than
5936 	 * case and update the caller's size.
5937 	 */
5938 	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5939 		*vpd_size = mqe->un.read_rev.avail_vpd_len;
5940 
5941 	memcpy(vpd, dmabuf->virt, *vpd_size);
5942 
5943 	dma_free_coherent(&phba->pcidev->dev, dma_size,
5944 			  dmabuf->virt, dmabuf->phys);
5945 	kfree(dmabuf);
5946 	return 0;
5947 }
5948 
5949 /**
5950  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5951  * @phba: pointer to lpfc hba data structure.
5952  *
5953  * This routine retrieves SLI4 device physical port name this PCI function
5954  * is attached to.
5955  *
5956  * Return codes
5957  *      0 - successful
5958  *      otherwise - failed to retrieve controller attributes
5959  **/
5960 static int
lpfc_sli4_get_ctl_attr(struct lpfc_hba * phba)5961 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5962 {
5963 	LPFC_MBOXQ_t *mboxq;
5964 	struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5965 	struct lpfc_controller_attribute *cntl_attr;
5966 	void *virtaddr = NULL;
5967 	uint32_t alloclen, reqlen;
5968 	uint32_t shdr_status, shdr_add_status;
5969 	union lpfc_sli4_cfg_shdr *shdr;
5970 	int rc;
5971 
5972 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5973 	if (!mboxq)
5974 		return -ENOMEM;
5975 
5976 	/* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5977 	reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5978 	alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5979 			LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5980 			LPFC_SLI4_MBX_NEMBED);
5981 
5982 	if (alloclen < reqlen) {
5983 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5984 				"3084 Allocated DMA memory size (%d) is "
5985 				"less than the requested DMA memory size "
5986 				"(%d)\n", alloclen, reqlen);
5987 		rc = -ENOMEM;
5988 		goto out_free_mboxq;
5989 	}
5990 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5991 	virtaddr = mboxq->sge_array->addr[0];
5992 	mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5993 	shdr = &mbx_cntl_attr->cfg_shdr;
5994 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5995 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5996 	if (shdr_status || shdr_add_status || rc) {
5997 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5998 				"3085 Mailbox x%x (x%x/x%x) failed, "
5999 				"rc:x%x, status:x%x, add_status:x%x\n",
6000 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6001 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6002 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6003 				rc, shdr_status, shdr_add_status);
6004 		rc = -ENXIO;
6005 		goto out_free_mboxq;
6006 	}
6007 
6008 	cntl_attr = &mbx_cntl_attr->cntl_attr;
6009 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6010 	phba->sli4_hba.lnk_info.lnk_tp =
6011 		bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
6012 	phba->sli4_hba.lnk_info.lnk_no =
6013 		bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
6014 	phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
6015 	phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
6016 
6017 	memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
6018 	strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
6019 		sizeof(phba->BIOSVersion));
6020 
6021 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6022 			"3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
6023 			"flash_id: x%02x, asic_rev: x%02x\n",
6024 			phba->sli4_hba.lnk_info.lnk_tp,
6025 			phba->sli4_hba.lnk_info.lnk_no,
6026 			phba->BIOSVersion, phba->sli4_hba.flash_id,
6027 			phba->sli4_hba.asic_rev);
6028 out_free_mboxq:
6029 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6030 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6031 	else
6032 		mempool_free(mboxq, phba->mbox_mem_pool);
6033 	return rc;
6034 }
6035 
6036 /**
6037  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
6038  * @phba: pointer to lpfc hba data structure.
6039  *
6040  * This routine retrieves SLI4 device physical port name this PCI function
6041  * is attached to.
6042  *
6043  * Return codes
6044  *      0 - successful
6045  *      otherwise - failed to retrieve physical port name
6046  **/
6047 static int
lpfc_sli4_retrieve_pport_name(struct lpfc_hba * phba)6048 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
6049 {
6050 	LPFC_MBOXQ_t *mboxq;
6051 	struct lpfc_mbx_get_port_name *get_port_name;
6052 	uint32_t shdr_status, shdr_add_status;
6053 	union lpfc_sli4_cfg_shdr *shdr;
6054 	char cport_name = 0;
6055 	int rc;
6056 
6057 	/* We assume nothing at this point */
6058 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6059 	phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
6060 
6061 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6062 	if (!mboxq)
6063 		return -ENOMEM;
6064 	/* obtain link type and link number via READ_CONFIG */
6065 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6066 	lpfc_sli4_read_config(phba);
6067 
6068 	if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG)
6069 		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
6070 
6071 	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
6072 		goto retrieve_ppname;
6073 
6074 	/* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
6075 	rc = lpfc_sli4_get_ctl_attr(phba);
6076 	if (rc)
6077 		goto out_free_mboxq;
6078 
6079 retrieve_ppname:
6080 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6081 		LPFC_MBOX_OPCODE_GET_PORT_NAME,
6082 		sizeof(struct lpfc_mbx_get_port_name) -
6083 		sizeof(struct lpfc_sli4_cfg_mhdr),
6084 		LPFC_SLI4_MBX_EMBED);
6085 	get_port_name = &mboxq->u.mqe.un.get_port_name;
6086 	shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
6087 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
6088 	bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
6089 		phba->sli4_hba.lnk_info.lnk_tp);
6090 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6091 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6092 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6093 	if (shdr_status || shdr_add_status || rc) {
6094 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6095 				"3087 Mailbox x%x (x%x/x%x) failed: "
6096 				"rc:x%x, status:x%x, add_status:x%x\n",
6097 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6098 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6099 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6100 				rc, shdr_status, shdr_add_status);
6101 		rc = -ENXIO;
6102 		goto out_free_mboxq;
6103 	}
6104 	switch (phba->sli4_hba.lnk_info.lnk_no) {
6105 	case LPFC_LINK_NUMBER_0:
6106 		cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6107 				&get_port_name->u.response);
6108 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6109 		break;
6110 	case LPFC_LINK_NUMBER_1:
6111 		cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6112 				&get_port_name->u.response);
6113 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6114 		break;
6115 	case LPFC_LINK_NUMBER_2:
6116 		cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6117 				&get_port_name->u.response);
6118 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6119 		break;
6120 	case LPFC_LINK_NUMBER_3:
6121 		cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6122 				&get_port_name->u.response);
6123 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6124 		break;
6125 	default:
6126 		break;
6127 	}
6128 
6129 	if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6130 		phba->Port[0] = cport_name;
6131 		phba->Port[1] = '\0';
6132 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6133 				"3091 SLI get port name: %s\n", phba->Port);
6134 	}
6135 
6136 out_free_mboxq:
6137 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6138 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6139 	else
6140 		mempool_free(mboxq, phba->mbox_mem_pool);
6141 	return rc;
6142 }
6143 
6144 /**
6145  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6146  * @phba: pointer to lpfc hba data structure.
6147  *
6148  * This routine is called to explicitly arm the SLI4 device's completion and
6149  * event queues
6150  **/
6151 static void
lpfc_sli4_arm_cqeq_intr(struct lpfc_hba * phba)6152 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6153 {
6154 	int qidx;
6155 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
6156 	struct lpfc_sli4_hdw_queue *qp;
6157 	struct lpfc_queue *eq;
6158 
6159 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6160 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
6161 	if (sli4_hba->nvmels_cq)
6162 		sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6163 					   LPFC_QUEUE_REARM);
6164 
6165 	if (sli4_hba->hdwq) {
6166 		/* Loop thru all Hardware Queues */
6167 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
6168 			qp = &sli4_hba->hdwq[qidx];
6169 			/* ARM the corresponding CQ */
6170 			sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
6171 						LPFC_QUEUE_REARM);
6172 		}
6173 
6174 		/* Loop thru all IRQ vectors */
6175 		for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6176 			eq = sli4_hba->hba_eq_hdl[qidx].eq;
6177 			/* ARM the corresponding EQ */
6178 			sli4_hba->sli4_write_eq_db(phba, eq,
6179 						   0, LPFC_QUEUE_REARM);
6180 		}
6181 	}
6182 
6183 	if (phba->nvmet_support) {
6184 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
6185 			sli4_hba->sli4_write_cq_db(phba,
6186 				sli4_hba->nvmet_cqset[qidx], 0,
6187 				LPFC_QUEUE_REARM);
6188 		}
6189 	}
6190 }
6191 
6192 /**
6193  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6194  * @phba: Pointer to HBA context object.
6195  * @type: The resource extent type.
6196  * @extnt_count: buffer to hold port available extent count.
6197  * @extnt_size: buffer to hold element count per extent.
6198  *
6199  * This function calls the port and retrievs the number of available
6200  * extents and their size for a particular extent type.
6201  *
6202  * Returns: 0 if successful.  Nonzero otherwise.
6203  **/
6204 int
lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_count,uint16_t * extnt_size)6205 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6206 			       uint16_t *extnt_count, uint16_t *extnt_size)
6207 {
6208 	int rc = 0;
6209 	uint32_t length;
6210 	uint32_t mbox_tmo;
6211 	struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6212 	LPFC_MBOXQ_t *mbox;
6213 
6214 	*extnt_count = 0;
6215 	*extnt_size = 0;
6216 
6217 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6218 	if (!mbox)
6219 		return -ENOMEM;
6220 
6221 	/* Find out how many extents are available for this resource type */
6222 	length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6223 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6224 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6225 			 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6226 			 length, LPFC_SLI4_MBX_EMBED);
6227 
6228 	/* Send an extents count of 0 - the GET doesn't use it. */
6229 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6230 					LPFC_SLI4_MBX_EMBED);
6231 	if (unlikely(rc)) {
6232 		rc = -EIO;
6233 		goto err_exit;
6234 	}
6235 
6236 	if (!phba->sli4_hba.intr_enable)
6237 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6238 	else {
6239 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6240 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6241 	}
6242 	if (unlikely(rc)) {
6243 		rc = -EIO;
6244 		goto err_exit;
6245 	}
6246 
6247 	rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6248 	if (bf_get(lpfc_mbox_hdr_status,
6249 		   &rsrc_info->header.cfg_shdr.response)) {
6250 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6251 				"2930 Failed to get resource extents "
6252 				"Status 0x%x Add'l Status 0x%x\n",
6253 				bf_get(lpfc_mbox_hdr_status,
6254 				       &rsrc_info->header.cfg_shdr.response),
6255 				bf_get(lpfc_mbox_hdr_add_status,
6256 				       &rsrc_info->header.cfg_shdr.response));
6257 		rc = -EIO;
6258 		goto err_exit;
6259 	}
6260 
6261 	*extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6262 			      &rsrc_info->u.rsp);
6263 	*extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6264 			     &rsrc_info->u.rsp);
6265 
6266 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6267 			"3162 Retrieved extents type-%d from port: count:%d, "
6268 			"size:%d\n", type, *extnt_count, *extnt_size);
6269 
6270 err_exit:
6271 	mempool_free(mbox, phba->mbox_mem_pool);
6272 	return rc;
6273 }
6274 
6275 /**
6276  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6277  * @phba: Pointer to HBA context object.
6278  * @type: The extent type to check.
6279  *
6280  * This function reads the current available extents from the port and checks
6281  * if the extent count or extent size has changed since the last access.
6282  * Callers use this routine post port reset to understand if there is a
6283  * extent reprovisioning requirement.
6284  *
6285  * Returns:
6286  *   -Error: error indicates problem.
6287  *   1: Extent count or size has changed.
6288  *   0: No changes.
6289  **/
6290 static int
lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type)6291 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6292 {
6293 	uint16_t curr_ext_cnt, rsrc_ext_cnt;
6294 	uint16_t size_diff, rsrc_ext_size;
6295 	int rc = 0;
6296 	struct lpfc_rsrc_blks *rsrc_entry;
6297 	struct list_head *rsrc_blk_list = NULL;
6298 
6299 	size_diff = 0;
6300 	curr_ext_cnt = 0;
6301 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6302 					    &rsrc_ext_cnt,
6303 					    &rsrc_ext_size);
6304 	if (unlikely(rc))
6305 		return -EIO;
6306 
6307 	switch (type) {
6308 	case LPFC_RSC_TYPE_FCOE_RPI:
6309 		rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6310 		break;
6311 	case LPFC_RSC_TYPE_FCOE_VPI:
6312 		rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6313 		break;
6314 	case LPFC_RSC_TYPE_FCOE_XRI:
6315 		rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6316 		break;
6317 	case LPFC_RSC_TYPE_FCOE_VFI:
6318 		rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6319 		break;
6320 	default:
6321 		break;
6322 	}
6323 
6324 	list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6325 		curr_ext_cnt++;
6326 		if (rsrc_entry->rsrc_size != rsrc_ext_size)
6327 			size_diff++;
6328 	}
6329 
6330 	if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6331 		rc = 1;
6332 
6333 	return rc;
6334 }
6335 
6336 /**
6337  * lpfc_sli4_cfg_post_extnts -
6338  * @phba: Pointer to HBA context object.
6339  * @extnt_cnt: number of available extents.
6340  * @type: the extent type (rpi, xri, vfi, vpi).
6341  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6342  * @mbox: pointer to the caller's allocated mailbox structure.
6343  *
6344  * This function executes the extents allocation request.  It also
6345  * takes care of the amount of memory needed to allocate or get the
6346  * allocated extents. It is the caller's responsibility to evaluate
6347  * the response.
6348  *
6349  * Returns:
6350  *   -Error:  Error value describes the condition found.
6351  *   0: if successful
6352  **/
6353 static int
lpfc_sli4_cfg_post_extnts(struct lpfc_hba * phba,uint16_t extnt_cnt,uint16_t type,bool * emb,LPFC_MBOXQ_t * mbox)6354 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6355 			  uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6356 {
6357 	int rc = 0;
6358 	uint32_t req_len;
6359 	uint32_t emb_len;
6360 	uint32_t alloc_len, mbox_tmo;
6361 
6362 	/* Calculate the total requested length of the dma memory */
6363 	req_len = extnt_cnt * sizeof(uint16_t);
6364 
6365 	/*
6366 	 * Calculate the size of an embedded mailbox.  The uint32_t
6367 	 * accounts for extents-specific word.
6368 	 */
6369 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6370 		sizeof(uint32_t);
6371 
6372 	/*
6373 	 * Presume the allocation and response will fit into an embedded
6374 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6375 	 */
6376 	*emb = LPFC_SLI4_MBX_EMBED;
6377 	if (req_len > emb_len) {
6378 		req_len = extnt_cnt * sizeof(uint16_t) +
6379 			sizeof(union lpfc_sli4_cfg_shdr) +
6380 			sizeof(uint32_t);
6381 		*emb = LPFC_SLI4_MBX_NEMBED;
6382 	}
6383 
6384 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6385 				     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6386 				     req_len, *emb);
6387 	if (alloc_len < req_len) {
6388 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6389 			"2982 Allocated DMA memory size (x%x) is "
6390 			"less than the requested DMA memory "
6391 			"size (x%x)\n", alloc_len, req_len);
6392 		return -ENOMEM;
6393 	}
6394 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6395 	if (unlikely(rc))
6396 		return -EIO;
6397 
6398 	if (!phba->sli4_hba.intr_enable)
6399 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6400 	else {
6401 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6402 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6403 	}
6404 
6405 	if (unlikely(rc))
6406 		rc = -EIO;
6407 	return rc;
6408 }
6409 
6410 /**
6411  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6412  * @phba: Pointer to HBA context object.
6413  * @type:  The resource extent type to allocate.
6414  *
6415  * This function allocates the number of elements for the specified
6416  * resource type.
6417  **/
6418 static int
lpfc_sli4_alloc_extent(struct lpfc_hba * phba,uint16_t type)6419 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6420 {
6421 	bool emb = false;
6422 	uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6423 	uint16_t rsrc_id, rsrc_start, j, k;
6424 	uint16_t *ids;
6425 	int i, rc;
6426 	unsigned long longs;
6427 	unsigned long *bmask;
6428 	struct lpfc_rsrc_blks *rsrc_blks;
6429 	LPFC_MBOXQ_t *mbox;
6430 	uint32_t length;
6431 	struct lpfc_id_range *id_array = NULL;
6432 	void *virtaddr = NULL;
6433 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6434 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6435 	struct list_head *ext_blk_list;
6436 
6437 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6438 					    &rsrc_cnt,
6439 					    &rsrc_size);
6440 	if (unlikely(rc))
6441 		return -EIO;
6442 
6443 	if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6444 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6445 			"3009 No available Resource Extents "
6446 			"for resource type 0x%x: Count: 0x%x, "
6447 			"Size 0x%x\n", type, rsrc_cnt,
6448 			rsrc_size);
6449 		return -ENOMEM;
6450 	}
6451 
6452 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6453 			"2903 Post resource extents type-0x%x: "
6454 			"count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6455 
6456 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6457 	if (!mbox)
6458 		return -ENOMEM;
6459 
6460 	rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6461 	if (unlikely(rc)) {
6462 		rc = -EIO;
6463 		goto err_exit;
6464 	}
6465 
6466 	/*
6467 	 * Figure out where the response is located.  Then get local pointers
6468 	 * to the response data.  The port does not guarantee to respond to
6469 	 * all extents counts request so update the local variable with the
6470 	 * allocated count from the port.
6471 	 */
6472 	if (emb == LPFC_SLI4_MBX_EMBED) {
6473 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6474 		id_array = &rsrc_ext->u.rsp.id[0];
6475 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6476 	} else {
6477 		virtaddr = mbox->sge_array->addr[0];
6478 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6479 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6480 		id_array = &n_rsrc->id;
6481 	}
6482 
6483 	longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6484 	rsrc_id_cnt = rsrc_cnt * rsrc_size;
6485 
6486 	/*
6487 	 * Based on the resource size and count, correct the base and max
6488 	 * resource values.
6489 	 */
6490 	length = sizeof(struct lpfc_rsrc_blks);
6491 	switch (type) {
6492 	case LPFC_RSC_TYPE_FCOE_RPI:
6493 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
6494 						   sizeof(unsigned long),
6495 						   GFP_KERNEL);
6496 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6497 			rc = -ENOMEM;
6498 			goto err_exit;
6499 		}
6500 		phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6501 						 sizeof(uint16_t),
6502 						 GFP_KERNEL);
6503 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
6504 			kfree(phba->sli4_hba.rpi_bmask);
6505 			rc = -ENOMEM;
6506 			goto err_exit;
6507 		}
6508 
6509 		/*
6510 		 * The next_rpi was initialized with the maximum available
6511 		 * count but the port may allocate a smaller number.  Catch
6512 		 * that case and update the next_rpi.
6513 		 */
6514 		phba->sli4_hba.next_rpi = rsrc_id_cnt;
6515 
6516 		/* Initialize local ptrs for common extent processing later. */
6517 		bmask = phba->sli4_hba.rpi_bmask;
6518 		ids = phba->sli4_hba.rpi_ids;
6519 		ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6520 		break;
6521 	case LPFC_RSC_TYPE_FCOE_VPI:
6522 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6523 					  GFP_KERNEL);
6524 		if (unlikely(!phba->vpi_bmask)) {
6525 			rc = -ENOMEM;
6526 			goto err_exit;
6527 		}
6528 		phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6529 					 GFP_KERNEL);
6530 		if (unlikely(!phba->vpi_ids)) {
6531 			kfree(phba->vpi_bmask);
6532 			rc = -ENOMEM;
6533 			goto err_exit;
6534 		}
6535 
6536 		/* Initialize local ptrs for common extent processing later. */
6537 		bmask = phba->vpi_bmask;
6538 		ids = phba->vpi_ids;
6539 		ext_blk_list = &phba->lpfc_vpi_blk_list;
6540 		break;
6541 	case LPFC_RSC_TYPE_FCOE_XRI:
6542 		phba->sli4_hba.xri_bmask = kcalloc(longs,
6543 						   sizeof(unsigned long),
6544 						   GFP_KERNEL);
6545 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
6546 			rc = -ENOMEM;
6547 			goto err_exit;
6548 		}
6549 		phba->sli4_hba.max_cfg_param.xri_used = 0;
6550 		phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6551 						 sizeof(uint16_t),
6552 						 GFP_KERNEL);
6553 		if (unlikely(!phba->sli4_hba.xri_ids)) {
6554 			kfree(phba->sli4_hba.xri_bmask);
6555 			rc = -ENOMEM;
6556 			goto err_exit;
6557 		}
6558 
6559 		/* Initialize local ptrs for common extent processing later. */
6560 		bmask = phba->sli4_hba.xri_bmask;
6561 		ids = phba->sli4_hba.xri_ids;
6562 		ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6563 		break;
6564 	case LPFC_RSC_TYPE_FCOE_VFI:
6565 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
6566 						   sizeof(unsigned long),
6567 						   GFP_KERNEL);
6568 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6569 			rc = -ENOMEM;
6570 			goto err_exit;
6571 		}
6572 		phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6573 						 sizeof(uint16_t),
6574 						 GFP_KERNEL);
6575 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
6576 			kfree(phba->sli4_hba.vfi_bmask);
6577 			rc = -ENOMEM;
6578 			goto err_exit;
6579 		}
6580 
6581 		/* Initialize local ptrs for common extent processing later. */
6582 		bmask = phba->sli4_hba.vfi_bmask;
6583 		ids = phba->sli4_hba.vfi_ids;
6584 		ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6585 		break;
6586 	default:
6587 		/* Unsupported Opcode.  Fail call. */
6588 		id_array = NULL;
6589 		bmask = NULL;
6590 		ids = NULL;
6591 		ext_blk_list = NULL;
6592 		goto err_exit;
6593 	}
6594 
6595 	/*
6596 	 * Complete initializing the extent configuration with the
6597 	 * allocated ids assigned to this function.  The bitmask serves
6598 	 * as an index into the array and manages the available ids.  The
6599 	 * array just stores the ids communicated to the port via the wqes.
6600 	 */
6601 	for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6602 		if ((i % 2) == 0)
6603 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6604 					 &id_array[k]);
6605 		else
6606 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6607 					 &id_array[k]);
6608 
6609 		rsrc_blks = kzalloc(length, GFP_KERNEL);
6610 		if (unlikely(!rsrc_blks)) {
6611 			rc = -ENOMEM;
6612 			kfree(bmask);
6613 			kfree(ids);
6614 			goto err_exit;
6615 		}
6616 		rsrc_blks->rsrc_start = rsrc_id;
6617 		rsrc_blks->rsrc_size = rsrc_size;
6618 		list_add_tail(&rsrc_blks->list, ext_blk_list);
6619 		rsrc_start = rsrc_id;
6620 		if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6621 			phba->sli4_hba.io_xri_start = rsrc_start +
6622 				lpfc_sli4_get_iocb_cnt(phba);
6623 		}
6624 
6625 		while (rsrc_id < (rsrc_start + rsrc_size)) {
6626 			ids[j] = rsrc_id;
6627 			rsrc_id++;
6628 			j++;
6629 		}
6630 		/* Entire word processed.  Get next word.*/
6631 		if ((i % 2) == 1)
6632 			k++;
6633 	}
6634  err_exit:
6635 	lpfc_sli4_mbox_cmd_free(phba, mbox);
6636 	return rc;
6637 }
6638 
6639 
6640 
6641 /**
6642  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6643  * @phba: Pointer to HBA context object.
6644  * @type: the extent's type.
6645  *
6646  * This function deallocates all extents of a particular resource type.
6647  * SLI4 does not allow for deallocating a particular extent range.  It
6648  * is the caller's responsibility to release all kernel memory resources.
6649  **/
6650 static int
lpfc_sli4_dealloc_extent(struct lpfc_hba * phba,uint16_t type)6651 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6652 {
6653 	int rc;
6654 	uint32_t length, mbox_tmo = 0;
6655 	LPFC_MBOXQ_t *mbox;
6656 	struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6657 	struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6658 
6659 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6660 	if (!mbox)
6661 		return -ENOMEM;
6662 
6663 	/*
6664 	 * This function sends an embedded mailbox because it only sends the
6665 	 * the resource type.  All extents of this type are released by the
6666 	 * port.
6667 	 */
6668 	length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6669 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6670 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6671 			 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6672 			 length, LPFC_SLI4_MBX_EMBED);
6673 
6674 	/* Send an extents count of 0 - the dealloc doesn't use it. */
6675 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6676 					LPFC_SLI4_MBX_EMBED);
6677 	if (unlikely(rc)) {
6678 		rc = -EIO;
6679 		goto out_free_mbox;
6680 	}
6681 	if (!phba->sli4_hba.intr_enable)
6682 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6683 	else {
6684 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6685 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6686 	}
6687 	if (unlikely(rc)) {
6688 		rc = -EIO;
6689 		goto out_free_mbox;
6690 	}
6691 
6692 	dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6693 	if (bf_get(lpfc_mbox_hdr_status,
6694 		   &dealloc_rsrc->header.cfg_shdr.response)) {
6695 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6696 				"2919 Failed to release resource extents "
6697 				"for type %d - Status 0x%x Add'l Status 0x%x. "
6698 				"Resource memory not released.\n",
6699 				type,
6700 				bf_get(lpfc_mbox_hdr_status,
6701 				    &dealloc_rsrc->header.cfg_shdr.response),
6702 				bf_get(lpfc_mbox_hdr_add_status,
6703 				    &dealloc_rsrc->header.cfg_shdr.response));
6704 		rc = -EIO;
6705 		goto out_free_mbox;
6706 	}
6707 
6708 	/* Release kernel memory resources for the specific type. */
6709 	switch (type) {
6710 	case LPFC_RSC_TYPE_FCOE_VPI:
6711 		kfree(phba->vpi_bmask);
6712 		kfree(phba->vpi_ids);
6713 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6714 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6715 				    &phba->lpfc_vpi_blk_list, list) {
6716 			list_del_init(&rsrc_blk->list);
6717 			kfree(rsrc_blk);
6718 		}
6719 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
6720 		break;
6721 	case LPFC_RSC_TYPE_FCOE_XRI:
6722 		kfree(phba->sli4_hba.xri_bmask);
6723 		kfree(phba->sli4_hba.xri_ids);
6724 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6725 				    &phba->sli4_hba.lpfc_xri_blk_list, list) {
6726 			list_del_init(&rsrc_blk->list);
6727 			kfree(rsrc_blk);
6728 		}
6729 		break;
6730 	case LPFC_RSC_TYPE_FCOE_VFI:
6731 		kfree(phba->sli4_hba.vfi_bmask);
6732 		kfree(phba->sli4_hba.vfi_ids);
6733 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6734 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6735 				    &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6736 			list_del_init(&rsrc_blk->list);
6737 			kfree(rsrc_blk);
6738 		}
6739 		break;
6740 	case LPFC_RSC_TYPE_FCOE_RPI:
6741 		/* RPI bitmask and physical id array are cleaned up earlier. */
6742 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6743 				    &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6744 			list_del_init(&rsrc_blk->list);
6745 			kfree(rsrc_blk);
6746 		}
6747 		break;
6748 	default:
6749 		break;
6750 	}
6751 
6752 	bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6753 
6754  out_free_mbox:
6755 	mempool_free(mbox, phba->mbox_mem_pool);
6756 	return rc;
6757 }
6758 
6759 static void
lpfc_set_features(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox,uint32_t feature)6760 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6761 		  uint32_t feature)
6762 {
6763 	uint32_t len;
6764 	u32 sig_freq = 0;
6765 
6766 	len = sizeof(struct lpfc_mbx_set_feature) -
6767 		sizeof(struct lpfc_sli4_cfg_mhdr);
6768 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6769 			 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6770 			 LPFC_SLI4_MBX_EMBED);
6771 
6772 	switch (feature) {
6773 	case LPFC_SET_UE_RECOVERY:
6774 		bf_set(lpfc_mbx_set_feature_UER,
6775 		       &mbox->u.mqe.un.set_feature, 1);
6776 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6777 		mbox->u.mqe.un.set_feature.param_len = 8;
6778 		break;
6779 	case LPFC_SET_MDS_DIAGS:
6780 		bf_set(lpfc_mbx_set_feature_mds,
6781 		       &mbox->u.mqe.un.set_feature, 1);
6782 		bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6783 		       &mbox->u.mqe.un.set_feature, 1);
6784 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6785 		mbox->u.mqe.un.set_feature.param_len = 8;
6786 		break;
6787 	case LPFC_SET_CGN_SIGNAL:
6788 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
6789 			sig_freq = 0;
6790 		else
6791 			sig_freq = phba->cgn_sig_freq;
6792 
6793 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6794 			bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6795 			       &mbox->u.mqe.un.set_feature, sig_freq);
6796 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6797 			       &mbox->u.mqe.un.set_feature, sig_freq);
6798 		}
6799 
6800 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6801 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6802 			       &mbox->u.mqe.un.set_feature, sig_freq);
6803 
6804 		if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6805 		    phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6806 			sig_freq = 0;
6807 		else
6808 			sig_freq = lpfc_acqe_cgn_frequency;
6809 
6810 		bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6811 		       &mbox->u.mqe.un.set_feature, sig_freq);
6812 
6813 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6814 		mbox->u.mqe.un.set_feature.param_len = 12;
6815 		break;
6816 	case LPFC_SET_DUAL_DUMP:
6817 		bf_set(lpfc_mbx_set_feature_dd,
6818 		       &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6819 		bf_set(lpfc_mbx_set_feature_ddquery,
6820 		       &mbox->u.mqe.un.set_feature, 0);
6821 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6822 		mbox->u.mqe.un.set_feature.param_len = 4;
6823 		break;
6824 	case LPFC_SET_ENABLE_MI:
6825 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6826 		mbox->u.mqe.un.set_feature.param_len = 4;
6827 		bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6828 		       phba->pport->cfg_lun_queue_depth);
6829 		bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6830 		       phba->sli4_hba.pc_sli4_params.mi_ver);
6831 		break;
6832 	case LPFC_SET_LD_SIGNAL:
6833 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_LD_SIGNAL;
6834 		mbox->u.mqe.un.set_feature.param_len = 16;
6835 		bf_set(lpfc_mbx_set_feature_lds_qry,
6836 		       &mbox->u.mqe.un.set_feature, LPFC_QUERY_LDS_OP);
6837 		break;
6838 	case LPFC_SET_ENABLE_CMF:
6839 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6840 		mbox->u.mqe.un.set_feature.param_len = 4;
6841 		bf_set(lpfc_mbx_set_feature_cmf,
6842 		       &mbox->u.mqe.un.set_feature, 1);
6843 		break;
6844 	}
6845 	return;
6846 }
6847 
6848 /**
6849  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6850  * @phba: Pointer to HBA context object.
6851  *
6852  * Disable FW logging into host memory on the adapter. To
6853  * be done before reading logs from the host memory.
6854  **/
6855 void
lpfc_ras_stop_fwlog(struct lpfc_hba * phba)6856 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6857 {
6858 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6859 
6860 	spin_lock_irq(&phba->ras_fwlog_lock);
6861 	ras_fwlog->state = INACTIVE;
6862 	spin_unlock_irq(&phba->ras_fwlog_lock);
6863 
6864 	/* Disable FW logging to host memory */
6865 	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6866 	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6867 
6868 	/* Wait 10ms for firmware to stop using DMA buffer */
6869 	usleep_range(10 * 1000, 20 * 1000);
6870 }
6871 
6872 /**
6873  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6874  * @phba: Pointer to HBA context object.
6875  *
6876  * This function is called to free memory allocated for RAS FW logging
6877  * support in the driver.
6878  **/
6879 void
lpfc_sli4_ras_dma_free(struct lpfc_hba * phba)6880 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6881 {
6882 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6883 	struct lpfc_dmabuf *dmabuf, *next;
6884 
6885 	if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6886 		list_for_each_entry_safe(dmabuf, next,
6887 				    &ras_fwlog->fwlog_buff_list,
6888 				    list) {
6889 			list_del(&dmabuf->list);
6890 			dma_free_coherent(&phba->pcidev->dev,
6891 					  LPFC_RAS_MAX_ENTRY_SIZE,
6892 					  dmabuf->virt, dmabuf->phys);
6893 			kfree(dmabuf);
6894 		}
6895 	}
6896 
6897 	if (ras_fwlog->lwpd.virt) {
6898 		dma_free_coherent(&phba->pcidev->dev,
6899 				  sizeof(uint32_t) * 2,
6900 				  ras_fwlog->lwpd.virt,
6901 				  ras_fwlog->lwpd.phys);
6902 		ras_fwlog->lwpd.virt = NULL;
6903 	}
6904 
6905 	spin_lock_irq(&phba->ras_fwlog_lock);
6906 	ras_fwlog->state = INACTIVE;
6907 	spin_unlock_irq(&phba->ras_fwlog_lock);
6908 }
6909 
6910 /**
6911  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6912  * @phba: Pointer to HBA context object.
6913  * @fwlog_buff_count: Count of buffers to be created.
6914  *
6915  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6916  * to update FW log is posted to the adapter.
6917  * Buffer count is calculated based on module param ras_fwlog_buffsize
6918  * Size of each buffer posted to FW is 64K.
6919  **/
6920 
6921 static int
lpfc_sli4_ras_dma_alloc(struct lpfc_hba * phba,uint32_t fwlog_buff_count)6922 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6923 			uint32_t fwlog_buff_count)
6924 {
6925 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6926 	struct lpfc_dmabuf *dmabuf;
6927 	int rc = 0, i = 0;
6928 
6929 	/* Initialize List */
6930 	INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6931 
6932 	/* Allocate memory for the LWPD */
6933 	ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6934 					    sizeof(uint32_t) * 2,
6935 					    &ras_fwlog->lwpd.phys,
6936 					    GFP_KERNEL);
6937 	if (!ras_fwlog->lwpd.virt) {
6938 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6939 				"6185 LWPD Memory Alloc Failed\n");
6940 
6941 		return -ENOMEM;
6942 	}
6943 
6944 	ras_fwlog->fw_buffcount = fwlog_buff_count;
6945 	for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6946 		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6947 				 GFP_KERNEL);
6948 		if (!dmabuf) {
6949 			rc = -ENOMEM;
6950 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6951 					"6186 Memory Alloc failed FW logging");
6952 			goto free_mem;
6953 		}
6954 
6955 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6956 						  LPFC_RAS_MAX_ENTRY_SIZE,
6957 						  &dmabuf->phys, GFP_KERNEL);
6958 		if (!dmabuf->virt) {
6959 			kfree(dmabuf);
6960 			rc = -ENOMEM;
6961 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6962 					"6187 DMA Alloc Failed FW logging");
6963 			goto free_mem;
6964 		}
6965 		dmabuf->buffer_tag = i;
6966 		list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6967 	}
6968 
6969 free_mem:
6970 	if (rc)
6971 		lpfc_sli4_ras_dma_free(phba);
6972 
6973 	return rc;
6974 }
6975 
6976 /**
6977  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6978  * @phba: pointer to lpfc hba data structure.
6979  * @pmb: pointer to the driver internal queue element for mailbox command.
6980  *
6981  * Completion handler for driver's RAS MBX command to the device.
6982  **/
6983 static void
lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)6984 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6985 {
6986 	MAILBOX_t *mb;
6987 	union lpfc_sli4_cfg_shdr *shdr;
6988 	uint32_t shdr_status, shdr_add_status;
6989 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6990 
6991 	mb = &pmb->u.mb;
6992 
6993 	shdr = (union lpfc_sli4_cfg_shdr *)
6994 		&pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6995 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6996 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6997 
6998 	if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6999 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7000 				"6188 FW LOG mailbox "
7001 				"completed with status x%x add_status x%x,"
7002 				" mbx status x%x\n",
7003 				shdr_status, shdr_add_status, mb->mbxStatus);
7004 
7005 		ras_fwlog->ras_hwsupport = false;
7006 		goto disable_ras;
7007 	}
7008 
7009 	spin_lock_irq(&phba->ras_fwlog_lock);
7010 	ras_fwlog->state = ACTIVE;
7011 	spin_unlock_irq(&phba->ras_fwlog_lock);
7012 	mempool_free(pmb, phba->mbox_mem_pool);
7013 
7014 	return;
7015 
7016 disable_ras:
7017 	/* Free RAS DMA memory */
7018 	lpfc_sli4_ras_dma_free(phba);
7019 	mempool_free(pmb, phba->mbox_mem_pool);
7020 }
7021 
7022 /**
7023  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
7024  * @phba: pointer to lpfc hba data structure.
7025  * @fwlog_level: Logging verbosity level.
7026  * @fwlog_enable: Enable/Disable logging.
7027  *
7028  * Initialize memory and post mailbox command to enable FW logging in host
7029  * memory.
7030  **/
7031 int
lpfc_sli4_ras_fwlog_init(struct lpfc_hba * phba,uint32_t fwlog_level,uint32_t fwlog_enable)7032 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
7033 			 uint32_t fwlog_level,
7034 			 uint32_t fwlog_enable)
7035 {
7036 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
7037 	struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
7038 	struct lpfc_dmabuf *dmabuf;
7039 	LPFC_MBOXQ_t *mbox;
7040 	uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
7041 	int rc = 0;
7042 
7043 	spin_lock_irq(&phba->ras_fwlog_lock);
7044 	ras_fwlog->state = INACTIVE;
7045 	spin_unlock_irq(&phba->ras_fwlog_lock);
7046 
7047 	fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
7048 			  phba->cfg_ras_fwlog_buffsize);
7049 	fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
7050 
7051 	/*
7052 	 * If re-enabling FW logging support use earlier allocated
7053 	 * DMA buffers while posting MBX command.
7054 	 **/
7055 	if (!ras_fwlog->lwpd.virt) {
7056 		rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
7057 		if (rc) {
7058 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7059 					"6189 FW Log Memory Allocation Failed");
7060 			return rc;
7061 		}
7062 	}
7063 
7064 	/* Setup Mailbox command */
7065 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7066 	if (!mbox) {
7067 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7068 				"6190 RAS MBX Alloc Failed");
7069 		rc = -ENOMEM;
7070 		goto mem_free;
7071 	}
7072 
7073 	ras_fwlog->fw_loglevel = fwlog_level;
7074 	len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
7075 		sizeof(struct lpfc_sli4_cfg_mhdr));
7076 
7077 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
7078 			 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
7079 			 len, LPFC_SLI4_MBX_EMBED);
7080 
7081 	mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
7082 	bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
7083 	       fwlog_enable);
7084 	bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
7085 	       ras_fwlog->fw_loglevel);
7086 	bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
7087 	       ras_fwlog->fw_buffcount);
7088 	bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
7089 	       LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
7090 
7091 	/* Update DMA buffer address */
7092 	list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
7093 		memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
7094 
7095 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
7096 			putPaddrLow(dmabuf->phys);
7097 
7098 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
7099 			putPaddrHigh(dmabuf->phys);
7100 	}
7101 
7102 	/* Update LPWD address */
7103 	mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7104 	mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7105 
7106 	spin_lock_irq(&phba->ras_fwlog_lock);
7107 	ras_fwlog->state = REG_INPROGRESS;
7108 	spin_unlock_irq(&phba->ras_fwlog_lock);
7109 	mbox->vport = phba->pport;
7110 	mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7111 
7112 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7113 
7114 	if (rc == MBX_NOT_FINISHED) {
7115 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7116 				"6191 FW-Log Mailbox failed. "
7117 				"status %d mbxStatus : x%x", rc,
7118 				bf_get(lpfc_mqe_status, &mbox->u.mqe));
7119 		mempool_free(mbox, phba->mbox_mem_pool);
7120 		rc = -EIO;
7121 		goto mem_free;
7122 	} else
7123 		rc = 0;
7124 mem_free:
7125 	if (rc)
7126 		lpfc_sli4_ras_dma_free(phba);
7127 
7128 	return rc;
7129 }
7130 
7131 /**
7132  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7133  * @phba: Pointer to HBA context object.
7134  *
7135  * Check if RAS is supported on the adapter and initialize it.
7136  **/
7137 void
lpfc_sli4_ras_setup(struct lpfc_hba * phba)7138 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7139 {
7140 	/* Check RAS FW Log needs to be enabled or not */
7141 	if (lpfc_check_fwlog_support(phba))
7142 		return;
7143 
7144 	lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7145 				 LPFC_RAS_ENABLE_LOGGING);
7146 }
7147 
7148 /**
7149  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7150  * @phba: Pointer to HBA context object.
7151  *
7152  * This function allocates all SLI4 resource identifiers.
7153  **/
7154 int
lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba * phba)7155 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7156 {
7157 	int i, rc, error = 0;
7158 	uint16_t count, base;
7159 	unsigned long longs;
7160 
7161 	if (!phba->sli4_hba.rpi_hdrs_in_use)
7162 		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
7163 	if (phba->sli4_hba.extents_in_use) {
7164 		/*
7165 		 * The port supports resource extents. The XRI, VPI, VFI, RPI
7166 		 * resource extent count must be read and allocated before
7167 		 * provisioning the resource id arrays.
7168 		 */
7169 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7170 		    LPFC_IDX_RSRC_RDY) {
7171 			/*
7172 			 * Extent-based resources are set - the driver could
7173 			 * be in a port reset. Figure out if any corrective
7174 			 * actions need to be taken.
7175 			 */
7176 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7177 						 LPFC_RSC_TYPE_FCOE_VFI);
7178 			if (rc != 0)
7179 				error++;
7180 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7181 						 LPFC_RSC_TYPE_FCOE_VPI);
7182 			if (rc != 0)
7183 				error++;
7184 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7185 						 LPFC_RSC_TYPE_FCOE_XRI);
7186 			if (rc != 0)
7187 				error++;
7188 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7189 						 LPFC_RSC_TYPE_FCOE_RPI);
7190 			if (rc != 0)
7191 				error++;
7192 
7193 			/*
7194 			 * It's possible that the number of resources
7195 			 * provided to this port instance changed between
7196 			 * resets.  Detect this condition and reallocate
7197 			 * resources.  Otherwise, there is no action.
7198 			 */
7199 			if (error) {
7200 				lpfc_printf_log(phba, KERN_INFO,
7201 						LOG_MBOX | LOG_INIT,
7202 						"2931 Detected extent resource "
7203 						"change.  Reallocating all "
7204 						"extents.\n");
7205 				rc = lpfc_sli4_dealloc_extent(phba,
7206 						 LPFC_RSC_TYPE_FCOE_VFI);
7207 				rc = lpfc_sli4_dealloc_extent(phba,
7208 						 LPFC_RSC_TYPE_FCOE_VPI);
7209 				rc = lpfc_sli4_dealloc_extent(phba,
7210 						 LPFC_RSC_TYPE_FCOE_XRI);
7211 				rc = lpfc_sli4_dealloc_extent(phba,
7212 						 LPFC_RSC_TYPE_FCOE_RPI);
7213 			} else
7214 				return 0;
7215 		}
7216 
7217 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7218 		if (unlikely(rc))
7219 			goto err_exit;
7220 
7221 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7222 		if (unlikely(rc))
7223 			goto err_exit;
7224 
7225 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7226 		if (unlikely(rc))
7227 			goto err_exit;
7228 
7229 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7230 		if (unlikely(rc))
7231 			goto err_exit;
7232 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7233 		       LPFC_IDX_RSRC_RDY);
7234 		return rc;
7235 	} else {
7236 		/*
7237 		 * The port does not support resource extents.  The XRI, VPI,
7238 		 * VFI, RPI resource ids were determined from READ_CONFIG.
7239 		 * Just allocate the bitmasks and provision the resource id
7240 		 * arrays.  If a port reset is active, the resources don't
7241 		 * need any action - just exit.
7242 		 */
7243 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7244 		    LPFC_IDX_RSRC_RDY) {
7245 			lpfc_sli4_dealloc_resource_identifiers(phba);
7246 			lpfc_sli4_remove_rpis(phba);
7247 		}
7248 		/* RPIs. */
7249 		count = phba->sli4_hba.max_cfg_param.max_rpi;
7250 		if (count <= 0) {
7251 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7252 					"3279 Invalid provisioning of "
7253 					"rpi:%d\n", count);
7254 			rc = -EINVAL;
7255 			goto err_exit;
7256 		}
7257 		base = phba->sli4_hba.max_cfg_param.rpi_base;
7258 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7259 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
7260 						   sizeof(unsigned long),
7261 						   GFP_KERNEL);
7262 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7263 			rc = -ENOMEM;
7264 			goto err_exit;
7265 		}
7266 		phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
7267 						 GFP_KERNEL);
7268 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
7269 			rc = -ENOMEM;
7270 			goto free_rpi_bmask;
7271 		}
7272 
7273 		for (i = 0; i < count; i++)
7274 			phba->sli4_hba.rpi_ids[i] = base + i;
7275 
7276 		/* VPIs. */
7277 		count = phba->sli4_hba.max_cfg_param.max_vpi;
7278 		if (count <= 0) {
7279 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7280 					"3280 Invalid provisioning of "
7281 					"vpi:%d\n", count);
7282 			rc = -EINVAL;
7283 			goto free_rpi_ids;
7284 		}
7285 		base = phba->sli4_hba.max_cfg_param.vpi_base;
7286 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7287 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
7288 					  GFP_KERNEL);
7289 		if (unlikely(!phba->vpi_bmask)) {
7290 			rc = -ENOMEM;
7291 			goto free_rpi_ids;
7292 		}
7293 		phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
7294 					GFP_KERNEL);
7295 		if (unlikely(!phba->vpi_ids)) {
7296 			rc = -ENOMEM;
7297 			goto free_vpi_bmask;
7298 		}
7299 
7300 		for (i = 0; i < count; i++)
7301 			phba->vpi_ids[i] = base + i;
7302 
7303 		/* XRIs. */
7304 		count = phba->sli4_hba.max_cfg_param.max_xri;
7305 		if (count <= 0) {
7306 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7307 					"3281 Invalid provisioning of "
7308 					"xri:%d\n", count);
7309 			rc = -EINVAL;
7310 			goto free_vpi_ids;
7311 		}
7312 		base = phba->sli4_hba.max_cfg_param.xri_base;
7313 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7314 		phba->sli4_hba.xri_bmask = kcalloc(longs,
7315 						   sizeof(unsigned long),
7316 						   GFP_KERNEL);
7317 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
7318 			rc = -ENOMEM;
7319 			goto free_vpi_ids;
7320 		}
7321 		phba->sli4_hba.max_cfg_param.xri_used = 0;
7322 		phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
7323 						 GFP_KERNEL);
7324 		if (unlikely(!phba->sli4_hba.xri_ids)) {
7325 			rc = -ENOMEM;
7326 			goto free_xri_bmask;
7327 		}
7328 
7329 		for (i = 0; i < count; i++)
7330 			phba->sli4_hba.xri_ids[i] = base + i;
7331 
7332 		/* VFIs. */
7333 		count = phba->sli4_hba.max_cfg_param.max_vfi;
7334 		if (count <= 0) {
7335 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7336 					"3282 Invalid provisioning of "
7337 					"vfi:%d\n", count);
7338 			rc = -EINVAL;
7339 			goto free_xri_ids;
7340 		}
7341 		base = phba->sli4_hba.max_cfg_param.vfi_base;
7342 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7343 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
7344 						   sizeof(unsigned long),
7345 						   GFP_KERNEL);
7346 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7347 			rc = -ENOMEM;
7348 			goto free_xri_ids;
7349 		}
7350 		phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
7351 						 GFP_KERNEL);
7352 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
7353 			rc = -ENOMEM;
7354 			goto free_vfi_bmask;
7355 		}
7356 
7357 		for (i = 0; i < count; i++)
7358 			phba->sli4_hba.vfi_ids[i] = base + i;
7359 
7360 		/*
7361 		 * Mark all resources ready.  An HBA reset doesn't need
7362 		 * to reset the initialization.
7363 		 */
7364 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7365 		       LPFC_IDX_RSRC_RDY);
7366 		return 0;
7367 	}
7368 
7369  free_vfi_bmask:
7370 	kfree(phba->sli4_hba.vfi_bmask);
7371 	phba->sli4_hba.vfi_bmask = NULL;
7372  free_xri_ids:
7373 	kfree(phba->sli4_hba.xri_ids);
7374 	phba->sli4_hba.xri_ids = NULL;
7375  free_xri_bmask:
7376 	kfree(phba->sli4_hba.xri_bmask);
7377 	phba->sli4_hba.xri_bmask = NULL;
7378  free_vpi_ids:
7379 	kfree(phba->vpi_ids);
7380 	phba->vpi_ids = NULL;
7381  free_vpi_bmask:
7382 	kfree(phba->vpi_bmask);
7383 	phba->vpi_bmask = NULL;
7384  free_rpi_ids:
7385 	kfree(phba->sli4_hba.rpi_ids);
7386 	phba->sli4_hba.rpi_ids = NULL;
7387  free_rpi_bmask:
7388 	kfree(phba->sli4_hba.rpi_bmask);
7389 	phba->sli4_hba.rpi_bmask = NULL;
7390  err_exit:
7391 	return rc;
7392 }
7393 
7394 /**
7395  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7396  * @phba: Pointer to HBA context object.
7397  *
7398  * This function allocates the number of elements for the specified
7399  * resource type.
7400  **/
7401 int
lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba * phba)7402 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7403 {
7404 	if (phba->sli4_hba.extents_in_use) {
7405 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7406 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7407 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7408 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7409 	} else {
7410 		kfree(phba->vpi_bmask);
7411 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
7412 		kfree(phba->vpi_ids);
7413 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7414 		kfree(phba->sli4_hba.xri_bmask);
7415 		kfree(phba->sli4_hba.xri_ids);
7416 		kfree(phba->sli4_hba.vfi_bmask);
7417 		kfree(phba->sli4_hba.vfi_ids);
7418 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7419 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7420 	}
7421 
7422 	return 0;
7423 }
7424 
7425 /**
7426  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7427  * @phba: Pointer to HBA context object.
7428  * @type: The resource extent type.
7429  * @extnt_cnt: buffer to hold port extent count response
7430  * @extnt_size: buffer to hold port extent size response.
7431  *
7432  * This function calls the port to read the host allocated extents
7433  * for a particular type.
7434  **/
7435 int
lpfc_sli4_get_allocated_extnts(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_cnt,uint16_t * extnt_size)7436 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7437 			       uint16_t *extnt_cnt, uint16_t *extnt_size)
7438 {
7439 	bool emb;
7440 	int rc = 0;
7441 	uint16_t curr_blks = 0;
7442 	uint32_t req_len, emb_len;
7443 	uint32_t alloc_len, mbox_tmo;
7444 	struct list_head *blk_list_head;
7445 	struct lpfc_rsrc_blks *rsrc_blk;
7446 	LPFC_MBOXQ_t *mbox;
7447 	void *virtaddr = NULL;
7448 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7449 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7450 	union  lpfc_sli4_cfg_shdr *shdr;
7451 
7452 	switch (type) {
7453 	case LPFC_RSC_TYPE_FCOE_VPI:
7454 		blk_list_head = &phba->lpfc_vpi_blk_list;
7455 		break;
7456 	case LPFC_RSC_TYPE_FCOE_XRI:
7457 		blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7458 		break;
7459 	case LPFC_RSC_TYPE_FCOE_VFI:
7460 		blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7461 		break;
7462 	case LPFC_RSC_TYPE_FCOE_RPI:
7463 		blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7464 		break;
7465 	default:
7466 		return -EIO;
7467 	}
7468 
7469 	/* Count the number of extents currently allocatd for this type. */
7470 	list_for_each_entry(rsrc_blk, blk_list_head, list) {
7471 		if (curr_blks == 0) {
7472 			/*
7473 			 * The GET_ALLOCATED mailbox does not return the size,
7474 			 * just the count.  The size should be just the size
7475 			 * stored in the current allocated block and all sizes
7476 			 * for an extent type are the same so set the return
7477 			 * value now.
7478 			 */
7479 			*extnt_size = rsrc_blk->rsrc_size;
7480 		}
7481 		curr_blks++;
7482 	}
7483 
7484 	/*
7485 	 * Calculate the size of an embedded mailbox.  The uint32_t
7486 	 * accounts for extents-specific word.
7487 	 */
7488 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7489 		sizeof(uint32_t);
7490 
7491 	/*
7492 	 * Presume the allocation and response will fit into an embedded
7493 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7494 	 */
7495 	emb = LPFC_SLI4_MBX_EMBED;
7496 	req_len = emb_len;
7497 	if (req_len > emb_len) {
7498 		req_len = curr_blks * sizeof(uint16_t) +
7499 			sizeof(union lpfc_sli4_cfg_shdr) +
7500 			sizeof(uint32_t);
7501 		emb = LPFC_SLI4_MBX_NEMBED;
7502 	}
7503 
7504 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7505 	if (!mbox)
7506 		return -ENOMEM;
7507 	memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7508 
7509 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7510 				     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7511 				     req_len, emb);
7512 	if (alloc_len < req_len) {
7513 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7514 			"2983 Allocated DMA memory size (x%x) is "
7515 			"less than the requested DMA memory "
7516 			"size (x%x)\n", alloc_len, req_len);
7517 		rc = -ENOMEM;
7518 		goto err_exit;
7519 	}
7520 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7521 	if (unlikely(rc)) {
7522 		rc = -EIO;
7523 		goto err_exit;
7524 	}
7525 
7526 	if (!phba->sli4_hba.intr_enable)
7527 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7528 	else {
7529 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7530 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7531 	}
7532 
7533 	if (unlikely(rc)) {
7534 		rc = -EIO;
7535 		goto err_exit;
7536 	}
7537 
7538 	/*
7539 	 * Figure out where the response is located.  Then get local pointers
7540 	 * to the response data.  The port does not guarantee to respond to
7541 	 * all extents counts request so update the local variable with the
7542 	 * allocated count from the port.
7543 	 */
7544 	if (emb == LPFC_SLI4_MBX_EMBED) {
7545 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7546 		shdr = &rsrc_ext->header.cfg_shdr;
7547 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7548 	} else {
7549 		virtaddr = mbox->sge_array->addr[0];
7550 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7551 		shdr = &n_rsrc->cfg_shdr;
7552 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7553 	}
7554 
7555 	if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7556 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7557 			"2984 Failed to read allocated resources "
7558 			"for type %d - Status 0x%x Add'l Status 0x%x.\n",
7559 			type,
7560 			bf_get(lpfc_mbox_hdr_status, &shdr->response),
7561 			bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7562 		rc = -EIO;
7563 		goto err_exit;
7564 	}
7565  err_exit:
7566 	lpfc_sli4_mbox_cmd_free(phba, mbox);
7567 	return rc;
7568 }
7569 
7570 /**
7571  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7572  * @phba: pointer to lpfc hba data structure.
7573  * @sgl_list: linked link of sgl buffers to post
7574  * @cnt: number of linked list buffers
7575  *
7576  * This routine walks the list of buffers that have been allocated and
7577  * repost them to the port by using SGL block post. This is needed after a
7578  * pci_function_reset/warm_start or start. It attempts to construct blocks
7579  * of buffer sgls which contains contiguous xris and uses the non-embedded
7580  * SGL block post mailbox commands to post them to the port. For single
7581  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7582  * mailbox command for posting.
7583  *
7584  * Returns: 0 = success, non-zero failure.
7585  **/
7586 static int
lpfc_sli4_repost_sgl_list(struct lpfc_hba * phba,struct list_head * sgl_list,int cnt)7587 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7588 			  struct list_head *sgl_list, int cnt)
7589 {
7590 	struct lpfc_sglq *sglq_entry = NULL;
7591 	struct lpfc_sglq *sglq_entry_next = NULL;
7592 	struct lpfc_sglq *sglq_entry_first = NULL;
7593 	int status = 0, total_cnt;
7594 	int post_cnt = 0, num_posted = 0, block_cnt = 0;
7595 	int last_xritag = NO_XRI;
7596 	LIST_HEAD(prep_sgl_list);
7597 	LIST_HEAD(blck_sgl_list);
7598 	LIST_HEAD(allc_sgl_list);
7599 	LIST_HEAD(post_sgl_list);
7600 	LIST_HEAD(free_sgl_list);
7601 
7602 	spin_lock_irq(&phba->hbalock);
7603 	spin_lock(&phba->sli4_hba.sgl_list_lock);
7604 	list_splice_init(sgl_list, &allc_sgl_list);
7605 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
7606 	spin_unlock_irq(&phba->hbalock);
7607 
7608 	total_cnt = cnt;
7609 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7610 				 &allc_sgl_list, list) {
7611 		list_del_init(&sglq_entry->list);
7612 		block_cnt++;
7613 		if ((last_xritag != NO_XRI) &&
7614 		    (sglq_entry->sli4_xritag != last_xritag + 1)) {
7615 			/* a hole in xri block, form a sgl posting block */
7616 			list_splice_init(&prep_sgl_list, &blck_sgl_list);
7617 			post_cnt = block_cnt - 1;
7618 			/* prepare list for next posting block */
7619 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7620 			block_cnt = 1;
7621 		} else {
7622 			/* prepare list for next posting block */
7623 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7624 			/* enough sgls for non-embed sgl mbox command */
7625 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7626 				list_splice_init(&prep_sgl_list,
7627 						 &blck_sgl_list);
7628 				post_cnt = block_cnt;
7629 				block_cnt = 0;
7630 			}
7631 		}
7632 		num_posted++;
7633 
7634 		/* keep track of last sgl's xritag */
7635 		last_xritag = sglq_entry->sli4_xritag;
7636 
7637 		/* end of repost sgl list condition for buffers */
7638 		if (num_posted == total_cnt) {
7639 			if (post_cnt == 0) {
7640 				list_splice_init(&prep_sgl_list,
7641 						 &blck_sgl_list);
7642 				post_cnt = block_cnt;
7643 			} else if (block_cnt == 1) {
7644 				status = lpfc_sli4_post_sgl(phba,
7645 						sglq_entry->phys, 0,
7646 						sglq_entry->sli4_xritag);
7647 				if (!status) {
7648 					/* successful, put sgl to posted list */
7649 					list_add_tail(&sglq_entry->list,
7650 						      &post_sgl_list);
7651 				} else {
7652 					/* Failure, put sgl to free list */
7653 					lpfc_printf_log(phba, KERN_WARNING,
7654 						LOG_SLI,
7655 						"3159 Failed to post "
7656 						"sgl, xritag:x%x\n",
7657 						sglq_entry->sli4_xritag);
7658 					list_add_tail(&sglq_entry->list,
7659 						      &free_sgl_list);
7660 					total_cnt--;
7661 				}
7662 			}
7663 		}
7664 
7665 		/* continue until a nembed page worth of sgls */
7666 		if (post_cnt == 0)
7667 			continue;
7668 
7669 		/* post the buffer list sgls as a block */
7670 		status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7671 						 post_cnt);
7672 
7673 		if (!status) {
7674 			/* success, put sgl list to posted sgl list */
7675 			list_splice_init(&blck_sgl_list, &post_sgl_list);
7676 		} else {
7677 			/* Failure, put sgl list to free sgl list */
7678 			sglq_entry_first = list_first_entry(&blck_sgl_list,
7679 							    struct lpfc_sglq,
7680 							    list);
7681 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7682 					"3160 Failed to post sgl-list, "
7683 					"xritag:x%x-x%x\n",
7684 					sglq_entry_first->sli4_xritag,
7685 					(sglq_entry_first->sli4_xritag +
7686 					 post_cnt - 1));
7687 			list_splice_init(&blck_sgl_list, &free_sgl_list);
7688 			total_cnt -= post_cnt;
7689 		}
7690 
7691 		/* don't reset xirtag due to hole in xri block */
7692 		if (block_cnt == 0)
7693 			last_xritag = NO_XRI;
7694 
7695 		/* reset sgl post count for next round of posting */
7696 		post_cnt = 0;
7697 	}
7698 
7699 	/* free the sgls failed to post */
7700 	lpfc_free_sgl_list(phba, &free_sgl_list);
7701 
7702 	/* push sgls posted to the available list */
7703 	if (!list_empty(&post_sgl_list)) {
7704 		spin_lock_irq(&phba->hbalock);
7705 		spin_lock(&phba->sli4_hba.sgl_list_lock);
7706 		list_splice_init(&post_sgl_list, sgl_list);
7707 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
7708 		spin_unlock_irq(&phba->hbalock);
7709 	} else {
7710 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7711 				"3161 Failure to post sgl to port,status %x "
7712 				"blkcnt %d totalcnt %d postcnt %d\n",
7713 				status, block_cnt, total_cnt, post_cnt);
7714 		return -EIO;
7715 	}
7716 
7717 	/* return the number of XRIs actually posted */
7718 	return total_cnt;
7719 }
7720 
7721 /**
7722  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7723  * @phba: pointer to lpfc hba data structure.
7724  *
7725  * This routine walks the list of nvme buffers that have been allocated and
7726  * repost them to the port by using SGL block post. This is needed after a
7727  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7728  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7729  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7730  *
7731  * Returns: 0 = success, non-zero failure.
7732  **/
7733 static int
lpfc_sli4_repost_io_sgl_list(struct lpfc_hba * phba)7734 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7735 {
7736 	LIST_HEAD(post_nblist);
7737 	int num_posted, rc = 0;
7738 
7739 	/* get all NVME buffers need to repost to a local list */
7740 	lpfc_io_buf_flush(phba, &post_nblist);
7741 
7742 	/* post the list of nvme buffer sgls to port if available */
7743 	if (!list_empty(&post_nblist)) {
7744 		num_posted = lpfc_sli4_post_io_sgl_list(
7745 			phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7746 		/* failed to post any nvme buffer, return error */
7747 		if (num_posted == 0)
7748 			rc = -EIO;
7749 	}
7750 	return rc;
7751 }
7752 
7753 static void
lpfc_set_host_data(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)7754 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7755 {
7756 	uint32_t len;
7757 
7758 	len = sizeof(struct lpfc_mbx_set_host_data) -
7759 		sizeof(struct lpfc_sli4_cfg_mhdr);
7760 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7761 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7762 			 LPFC_SLI4_MBX_EMBED);
7763 
7764 	mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7765 	mbox->u.mqe.un.set_host_data.param_len =
7766 					LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7767 	snprintf(mbox->u.mqe.un.set_host_data.un.data,
7768 		 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7769 		 "Linux %s v"LPFC_DRIVER_VERSION,
7770 		 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7771 }
7772 
7773 int
lpfc_post_rq_buffer(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,int count,int idx)7774 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7775 		    struct lpfc_queue *drq, int count, int idx)
7776 {
7777 	int rc, i;
7778 	struct lpfc_rqe hrqe;
7779 	struct lpfc_rqe drqe;
7780 	struct lpfc_rqb *rqbp;
7781 	unsigned long flags;
7782 	struct rqb_dmabuf *rqb_buffer;
7783 	LIST_HEAD(rqb_buf_list);
7784 
7785 	rqbp = hrq->rqbp;
7786 	for (i = 0; i < count; i++) {
7787 		spin_lock_irqsave(&phba->hbalock, flags);
7788 		/* IF RQ is already full, don't bother */
7789 		if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7790 			spin_unlock_irqrestore(&phba->hbalock, flags);
7791 			break;
7792 		}
7793 		spin_unlock_irqrestore(&phba->hbalock, flags);
7794 
7795 		rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7796 		if (!rqb_buffer)
7797 			break;
7798 		rqb_buffer->hrq = hrq;
7799 		rqb_buffer->drq = drq;
7800 		rqb_buffer->idx = idx;
7801 		list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7802 	}
7803 
7804 	spin_lock_irqsave(&phba->hbalock, flags);
7805 	while (!list_empty(&rqb_buf_list)) {
7806 		list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7807 				 hbuf.list);
7808 
7809 		hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7810 		hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7811 		drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7812 		drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7813 		rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7814 		if (rc < 0) {
7815 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7816 					"6421 Cannot post to HRQ %d: %x %x %x "
7817 					"DRQ %x %x\n",
7818 					hrq->queue_id,
7819 					hrq->host_index,
7820 					hrq->hba_index,
7821 					hrq->entry_count,
7822 					drq->host_index,
7823 					drq->hba_index);
7824 			rqbp->rqb_free_buffer(phba, rqb_buffer);
7825 		} else {
7826 			list_add_tail(&rqb_buffer->hbuf.list,
7827 				      &rqbp->rqb_buffer_list);
7828 			rqbp->buffer_count++;
7829 		}
7830 	}
7831 	spin_unlock_irqrestore(&phba->hbalock, flags);
7832 	return 1;
7833 }
7834 
7835 static void
lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7836 lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7837 {
7838 	union lpfc_sli4_cfg_shdr *shdr;
7839 	u32 shdr_status, shdr_add_status;
7840 
7841 	shdr = (union lpfc_sli4_cfg_shdr *)
7842 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7843 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7844 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7845 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7846 		lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT | LOG_MBOX,
7847 				"4622 SET_FEATURE (x%x) mbox failed, "
7848 				"status x%x add_status x%x, mbx status x%x\n",
7849 				LPFC_SET_LD_SIGNAL, shdr_status,
7850 				shdr_add_status, pmb->u.mb.mbxStatus);
7851 		phba->degrade_activate_threshold = 0;
7852 		phba->degrade_deactivate_threshold = 0;
7853 		phba->fec_degrade_interval = 0;
7854 		goto out;
7855 	}
7856 
7857 	phba->degrade_activate_threshold = pmb->u.mqe.un.set_feature.word7;
7858 	phba->degrade_deactivate_threshold = pmb->u.mqe.un.set_feature.word8;
7859 	phba->fec_degrade_interval = pmb->u.mqe.un.set_feature.word10;
7860 
7861 	lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT,
7862 			"4624 Success: da x%x dd x%x interval x%x\n",
7863 			phba->degrade_activate_threshold,
7864 			phba->degrade_deactivate_threshold,
7865 			phba->fec_degrade_interval);
7866 out:
7867 	mempool_free(pmb, phba->mbox_mem_pool);
7868 }
7869 
7870 int
lpfc_read_lds_params(struct lpfc_hba * phba)7871 lpfc_read_lds_params(struct lpfc_hba *phba)
7872 {
7873 	LPFC_MBOXQ_t *mboxq;
7874 	int rc;
7875 
7876 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7877 	if (!mboxq)
7878 		return -ENOMEM;
7879 
7880 	lpfc_set_features(phba, mboxq, LPFC_SET_LD_SIGNAL);
7881 	mboxq->vport = phba->pport;
7882 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_lds_params;
7883 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7884 	if (rc == MBX_NOT_FINISHED) {
7885 		mempool_free(mboxq, phba->mbox_mem_pool);
7886 		return -EIO;
7887 	}
7888 	return 0;
7889 }
7890 
7891 static void
lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7892 lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7893 {
7894 	struct lpfc_vport *vport = pmb->vport;
7895 	union lpfc_sli4_cfg_shdr *shdr;
7896 	u32 shdr_status, shdr_add_status;
7897 	u32 sig, acqe;
7898 
7899 	/* Two outcomes. (1) Set featurs was successul and EDC negotiation
7900 	 * is done. (2) Mailbox failed and send FPIN support only.
7901 	 */
7902 	shdr = (union lpfc_sli4_cfg_shdr *)
7903 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7904 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7905 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7906 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7907 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7908 				"2516 CGN SET_FEATURE mbox failed with "
7909 				"status x%x add_status x%x, mbx status x%x "
7910 				"Reset Congestion to FPINs only\n",
7911 				shdr_status, shdr_add_status,
7912 				pmb->u.mb.mbxStatus);
7913 		/* If there is a mbox error, move on to RDF */
7914 		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7915 		phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7916 		goto out;
7917 	}
7918 
7919 	/* Zero out Congestion Signal ACQE counter */
7920 	phba->cgn_acqe_cnt = 0;
7921 
7922 	acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7923 		      &pmb->u.mqe.un.set_feature);
7924 	sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7925 		     &pmb->u.mqe.un.set_feature);
7926 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7927 			"4620 SET_FEATURES Success: Freq: %ds %dms "
7928 			" Reg: x%x x%x\n", acqe, sig,
7929 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7930 out:
7931 	mempool_free(pmb, phba->mbox_mem_pool);
7932 
7933 	/* Register for FPIN events from the fabric now that the
7934 	 * EDC common_set_features has completed.
7935 	 */
7936 	lpfc_issue_els_rdf(vport, 0);
7937 }
7938 
7939 int
lpfc_config_cgn_signal(struct lpfc_hba * phba)7940 lpfc_config_cgn_signal(struct lpfc_hba *phba)
7941 {
7942 	LPFC_MBOXQ_t *mboxq;
7943 	u32 rc;
7944 
7945 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7946 	if (!mboxq)
7947 		goto out_rdf;
7948 
7949 	lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7950 	mboxq->vport = phba->pport;
7951 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7952 
7953 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7954 			"4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7955 			"Reg: x%x x%x\n",
7956 			phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7957 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7958 
7959 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7960 	if (rc == MBX_NOT_FINISHED)
7961 		goto out;
7962 	return 0;
7963 
7964 out:
7965 	mempool_free(mboxq, phba->mbox_mem_pool);
7966 out_rdf:
7967 	/* If there is a mbox error, move on to RDF */
7968 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7969 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7970 	lpfc_issue_els_rdf(phba->pport, 0);
7971 	return -EIO;
7972 }
7973 
7974 /**
7975  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7976  * @phba: pointer to lpfc hba data structure.
7977  *
7978  * This routine initializes the per-eq idle_stat to dynamically dictate
7979  * polling decisions.
7980  *
7981  * Return codes:
7982  *   None
7983  **/
lpfc_init_idle_stat_hb(struct lpfc_hba * phba)7984 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7985 {
7986 	int i;
7987 	struct lpfc_sli4_hdw_queue *hdwq;
7988 	struct lpfc_queue *eq;
7989 	struct lpfc_idle_stat *idle_stat;
7990 	u64 wall;
7991 
7992 	for_each_present_cpu(i) {
7993 		hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7994 		eq = hdwq->hba_eq;
7995 
7996 		/* Skip if we've already handled this eq's primary CPU */
7997 		if (eq->chann != i)
7998 			continue;
7999 
8000 		idle_stat = &phba->sli4_hba.idle_stat[i];
8001 
8002 		idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
8003 		idle_stat->prev_wall = wall;
8004 
8005 		if (phba->nvmet_support ||
8006 		    phba->cmf_active_mode != LPFC_CFG_OFF ||
8007 		    phba->intr_type != MSIX)
8008 			eq->poll_mode = LPFC_QUEUE_WORK;
8009 		else
8010 			eq->poll_mode = LPFC_THREADED_IRQ;
8011 	}
8012 
8013 	if (!phba->nvmet_support && phba->intr_type == MSIX)
8014 		schedule_delayed_work(&phba->idle_stat_delay_work,
8015 				      msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
8016 }
8017 
lpfc_sli4_dip(struct lpfc_hba * phba)8018 static void lpfc_sli4_dip(struct lpfc_hba *phba)
8019 {
8020 	uint32_t if_type;
8021 
8022 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8023 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
8024 	    if_type == LPFC_SLI_INTF_IF_TYPE_6) {
8025 		struct lpfc_register reg_data;
8026 
8027 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8028 			       &reg_data.word0))
8029 			return;
8030 
8031 		if (bf_get(lpfc_sliport_status_dip, &reg_data))
8032 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8033 					"2904 Firmware Dump Image Present"
8034 					" on Adapter");
8035 	}
8036 }
8037 
8038 /**
8039  * lpfc_rx_monitor_create_ring - Initialize ring buffer for rx_monitor
8040  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8041  * @entries: Number of rx_info_entry objects to allocate in ring
8042  *
8043  * Return:
8044  * 0 - Success
8045  * ENOMEM - Failure to kmalloc
8046  **/
lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor * rx_monitor,u32 entries)8047 int lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor *rx_monitor,
8048 				u32 entries)
8049 {
8050 	rx_monitor->ring = kmalloc_array(entries, sizeof(struct rx_info_entry),
8051 					 GFP_KERNEL);
8052 	if (!rx_monitor->ring)
8053 		return -ENOMEM;
8054 
8055 	rx_monitor->head_idx = 0;
8056 	rx_monitor->tail_idx = 0;
8057 	spin_lock_init(&rx_monitor->lock);
8058 	rx_monitor->entries = entries;
8059 
8060 	return 0;
8061 }
8062 
8063 /**
8064  * lpfc_rx_monitor_destroy_ring - Free ring buffer for rx_monitor
8065  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8066  *
8067  * Called after cancellation of cmf_timer.
8068  **/
lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor * rx_monitor)8069 void lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor *rx_monitor)
8070 {
8071 	kfree(rx_monitor->ring);
8072 	rx_monitor->ring = NULL;
8073 	rx_monitor->entries = 0;
8074 	rx_monitor->head_idx = 0;
8075 	rx_monitor->tail_idx = 0;
8076 }
8077 
8078 /**
8079  * lpfc_rx_monitor_record - Insert an entry into rx_monitor's ring
8080  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8081  * @entry: Pointer to rx_info_entry
8082  *
8083  * Used to insert an rx_info_entry into rx_monitor's ring.  Note that this is a
8084  * deep copy of rx_info_entry not a shallow copy of the rx_info_entry ptr.
8085  *
8086  * This is called from lpfc_cmf_timer, which is in timer/softirq context.
8087  *
8088  * In cases of old data overflow, we do a best effort of FIFO order.
8089  **/
lpfc_rx_monitor_record(struct lpfc_rx_info_monitor * rx_monitor,struct rx_info_entry * entry)8090 void lpfc_rx_monitor_record(struct lpfc_rx_info_monitor *rx_monitor,
8091 			    struct rx_info_entry *entry)
8092 {
8093 	struct rx_info_entry *ring = rx_monitor->ring;
8094 	u32 *head_idx = &rx_monitor->head_idx;
8095 	u32 *tail_idx = &rx_monitor->tail_idx;
8096 	spinlock_t *ring_lock = &rx_monitor->lock;
8097 	u32 ring_size = rx_monitor->entries;
8098 
8099 	spin_lock(ring_lock);
8100 	memcpy(&ring[*tail_idx], entry, sizeof(*entry));
8101 	*tail_idx = (*tail_idx + 1) % ring_size;
8102 
8103 	/* Best effort of FIFO saved data */
8104 	if (*tail_idx == *head_idx)
8105 		*head_idx = (*head_idx + 1) % ring_size;
8106 
8107 	spin_unlock(ring_lock);
8108 }
8109 
8110 /**
8111  * lpfc_rx_monitor_report - Read out rx_monitor's ring
8112  * @phba: Pointer to lpfc_hba object
8113  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8114  * @buf: Pointer to char buffer that will contain rx monitor info data
8115  * @buf_len: Length buf including null char
8116  * @max_read_entries: Maximum number of entries to read out of ring
8117  *
8118  * Used to dump/read what's in rx_monitor's ring buffer.
8119  *
8120  * If buf is NULL || buf_len == 0, then it is implied that we want to log the
8121  * information to kmsg instead of filling out buf.
8122  *
8123  * Return:
8124  * Number of entries read out of the ring
8125  **/
lpfc_rx_monitor_report(struct lpfc_hba * phba,struct lpfc_rx_info_monitor * rx_monitor,char * buf,u32 buf_len,u32 max_read_entries)8126 u32 lpfc_rx_monitor_report(struct lpfc_hba *phba,
8127 			   struct lpfc_rx_info_monitor *rx_monitor, char *buf,
8128 			   u32 buf_len, u32 max_read_entries)
8129 {
8130 	struct rx_info_entry *ring = rx_monitor->ring;
8131 	struct rx_info_entry *entry;
8132 	u32 *head_idx = &rx_monitor->head_idx;
8133 	u32 *tail_idx = &rx_monitor->tail_idx;
8134 	spinlock_t *ring_lock = &rx_monitor->lock;
8135 	u32 ring_size = rx_monitor->entries;
8136 	u32 cnt = 0;
8137 	char tmp[DBG_LOG_STR_SZ] = {0};
8138 	bool log_to_kmsg = (!buf || !buf_len) ? true : false;
8139 
8140 	if (!log_to_kmsg) {
8141 		/* clear the buffer to be sure */
8142 		memset(buf, 0, buf_len);
8143 
8144 		scnprintf(buf, buf_len, "\t%-16s%-16s%-16s%-16s%-8s%-8s%-8s"
8145 					"%-8s%-8s%-8s%-16s\n",
8146 					"MaxBPI", "Tot_Data_CMF",
8147 					"Tot_Data_Cmd", "Tot_Data_Cmpl",
8148 					"Lat(us)", "Avg_IO", "Max_IO", "Bsy",
8149 					"IO_cnt", "Info", "BWutil(ms)");
8150 	}
8151 
8152 	/* Needs to be _irq because record is called from timer interrupt
8153 	 * context
8154 	 */
8155 	spin_lock_irq(ring_lock);
8156 	while (*head_idx != *tail_idx) {
8157 		entry = &ring[*head_idx];
8158 
8159 		/* Read out this entry's data. */
8160 		if (!log_to_kmsg) {
8161 			/* If !log_to_kmsg, then store to buf. */
8162 			scnprintf(tmp, sizeof(tmp),
8163 				  "%03d:\t%-16llu%-16llu%-16llu%-16llu%-8llu"
8164 				  "%-8llu%-8llu%-8u%-8u%-8u%u(%u)\n",
8165 				  *head_idx, entry->max_bytes_per_interval,
8166 				  entry->cmf_bytes, entry->total_bytes,
8167 				  entry->rcv_bytes, entry->avg_io_latency,
8168 				  entry->avg_io_size, entry->max_read_cnt,
8169 				  entry->cmf_busy, entry->io_cnt,
8170 				  entry->cmf_info, entry->timer_utilization,
8171 				  entry->timer_interval);
8172 
8173 			/* Check for buffer overflow */
8174 			if ((strlen(buf) + strlen(tmp)) >= buf_len)
8175 				break;
8176 
8177 			/* Append entry's data to buffer */
8178 			strlcat(buf, tmp, buf_len);
8179 		} else {
8180 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
8181 					"4410 %02u: MBPI %llu Xmit %llu "
8182 					"Cmpl %llu Lat %llu ASz %llu Info %02u "
8183 					"BWUtil %u Int %u slot %u\n",
8184 					cnt, entry->max_bytes_per_interval,
8185 					entry->total_bytes, entry->rcv_bytes,
8186 					entry->avg_io_latency,
8187 					entry->avg_io_size, entry->cmf_info,
8188 					entry->timer_utilization,
8189 					entry->timer_interval, *head_idx);
8190 		}
8191 
8192 		*head_idx = (*head_idx + 1) % ring_size;
8193 
8194 		/* Don't feed more than max_read_entries */
8195 		cnt++;
8196 		if (cnt >= max_read_entries)
8197 			break;
8198 	}
8199 	spin_unlock_irq(ring_lock);
8200 
8201 	return cnt;
8202 }
8203 
8204 /**
8205  * lpfc_cmf_setup - Initialize idle_stat tracking
8206  * @phba: Pointer to HBA context object.
8207  *
8208  * This is called from HBA setup during driver load or when the HBA
8209  * comes online. this does all the initialization to support CMF and MI.
8210  **/
8211 static int
lpfc_cmf_setup(struct lpfc_hba * phba)8212 lpfc_cmf_setup(struct lpfc_hba *phba)
8213 {
8214 	LPFC_MBOXQ_t *mboxq;
8215 	struct lpfc_dmabuf *mp;
8216 	struct lpfc_pc_sli4_params *sli4_params;
8217 	int rc, cmf, mi_ver;
8218 
8219 	rc = lpfc_sli4_refresh_params(phba);
8220 	if (unlikely(rc))
8221 		return rc;
8222 
8223 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8224 	if (!mboxq)
8225 		return -ENOMEM;
8226 
8227 	sli4_params = &phba->sli4_hba.pc_sli4_params;
8228 
8229 	/* Always try to enable MI feature if we can */
8230 	if (sli4_params->mi_ver) {
8231 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
8232 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8233 		mi_ver = bf_get(lpfc_mbx_set_feature_mi,
8234 				 &mboxq->u.mqe.un.set_feature);
8235 
8236 		if (rc == MBX_SUCCESS) {
8237 			if (mi_ver) {
8238 				lpfc_printf_log(phba,
8239 						KERN_WARNING, LOG_CGN_MGMT,
8240 						"6215 MI is enabled\n");
8241 				sli4_params->mi_ver = mi_ver;
8242 			} else {
8243 				lpfc_printf_log(phba,
8244 						KERN_WARNING, LOG_CGN_MGMT,
8245 						"6338 MI is disabled\n");
8246 				sli4_params->mi_ver = 0;
8247 			}
8248 		} else {
8249 			/* mi_ver is already set from GET_SLI4_PARAMETERS */
8250 			lpfc_printf_log(phba, KERN_INFO,
8251 					LOG_CGN_MGMT | LOG_INIT,
8252 					"6245 Enable MI Mailbox x%x (x%x/x%x) "
8253 					"failed, rc:x%x mi:x%x\n",
8254 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8255 					lpfc_sli_config_mbox_subsys_get
8256 						(phba, mboxq),
8257 					lpfc_sli_config_mbox_opcode_get
8258 						(phba, mboxq),
8259 					rc, sli4_params->mi_ver);
8260 		}
8261 	} else {
8262 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8263 				"6217 MI is disabled\n");
8264 	}
8265 
8266 	/* Ensure FDMI is enabled for MI if enable_mi is set */
8267 	if (sli4_params->mi_ver)
8268 		phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
8269 
8270 	/* Always try to enable CMF feature if we can */
8271 	if (sli4_params->cmf) {
8272 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
8273 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8274 		cmf = bf_get(lpfc_mbx_set_feature_cmf,
8275 			     &mboxq->u.mqe.un.set_feature);
8276 		if (rc == MBX_SUCCESS && cmf) {
8277 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8278 					"6218 CMF is enabled: mode %d\n",
8279 					phba->cmf_active_mode);
8280 		} else {
8281 			lpfc_printf_log(phba, KERN_WARNING,
8282 					LOG_CGN_MGMT | LOG_INIT,
8283 					"6219 Enable CMF Mailbox x%x (x%x/x%x) "
8284 					"failed, rc:x%x dd:x%x\n",
8285 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8286 					lpfc_sli_config_mbox_subsys_get
8287 						(phba, mboxq),
8288 					lpfc_sli_config_mbox_opcode_get
8289 						(phba, mboxq),
8290 					rc, cmf);
8291 			sli4_params->cmf = 0;
8292 			phba->cmf_active_mode = LPFC_CFG_OFF;
8293 			goto no_cmf;
8294 		}
8295 
8296 		/* Allocate Congestion Information Buffer */
8297 		if (!phba->cgn_i) {
8298 			mp = kmalloc(sizeof(*mp), GFP_KERNEL);
8299 			if (mp)
8300 				mp->virt = dma_alloc_coherent
8301 						(&phba->pcidev->dev,
8302 						sizeof(struct lpfc_cgn_info),
8303 						&mp->phys, GFP_KERNEL);
8304 			if (!mp || !mp->virt) {
8305 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8306 						"2640 Failed to alloc memory "
8307 						"for Congestion Info\n");
8308 				kfree(mp);
8309 				sli4_params->cmf = 0;
8310 				phba->cmf_active_mode = LPFC_CFG_OFF;
8311 				goto no_cmf;
8312 			}
8313 			phba->cgn_i = mp;
8314 
8315 			/* initialize congestion buffer info */
8316 			lpfc_init_congestion_buf(phba);
8317 			lpfc_init_congestion_stat(phba);
8318 
8319 			/* Zero out Congestion Signal counters */
8320 			atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
8321 			atomic64_set(&phba->cgn_acqe_stat.warn, 0);
8322 		}
8323 
8324 		rc = lpfc_sli4_cgn_params_read(phba);
8325 		if (rc < 0) {
8326 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8327 					"6242 Error reading Cgn Params (%d)\n",
8328 					rc);
8329 			/* Ensure CGN Mode is off */
8330 			sli4_params->cmf = 0;
8331 		} else if (!rc) {
8332 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8333 					"6243 CGN Event empty object.\n");
8334 			/* Ensure CGN Mode is off */
8335 			sli4_params->cmf = 0;
8336 		}
8337 	} else {
8338 no_cmf:
8339 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8340 				"6220 CMF is disabled\n");
8341 	}
8342 
8343 	/* Only register congestion buffer with firmware if BOTH
8344 	 * CMF and E2E are enabled.
8345 	 */
8346 	if (sli4_params->cmf && sli4_params->mi_ver) {
8347 		rc = lpfc_reg_congestion_buf(phba);
8348 		if (rc) {
8349 			dma_free_coherent(&phba->pcidev->dev,
8350 					  sizeof(struct lpfc_cgn_info),
8351 					  phba->cgn_i->virt, phba->cgn_i->phys);
8352 			kfree(phba->cgn_i);
8353 			phba->cgn_i = NULL;
8354 			/* Ensure CGN Mode is off */
8355 			phba->cmf_active_mode = LPFC_CFG_OFF;
8356 			sli4_params->cmf = 0;
8357 			return 0;
8358 		}
8359 	}
8360 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8361 			"6470 Setup MI version %d CMF %d mode %d\n",
8362 			sli4_params->mi_ver, sli4_params->cmf,
8363 			phba->cmf_active_mode);
8364 
8365 	mempool_free(mboxq, phba->mbox_mem_pool);
8366 
8367 	/* Initialize atomic counters */
8368 	atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8369 	atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8370 	atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8371 	atomic_set(&phba->cgn_sync_warn_cnt, 0);
8372 	atomic_set(&phba->cgn_driver_evt_cnt, 0);
8373 	atomic_set(&phba->cgn_latency_evt_cnt, 0);
8374 	atomic64_set(&phba->cgn_latency_evt, 0);
8375 
8376 	phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
8377 
8378 	/* Allocate RX Monitor Buffer */
8379 	if (!phba->rx_monitor) {
8380 		phba->rx_monitor = kzalloc(sizeof(*phba->rx_monitor),
8381 					   GFP_KERNEL);
8382 
8383 		if (!phba->rx_monitor) {
8384 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8385 					"2644 Failed to alloc memory "
8386 					"for RX Monitor Buffer\n");
8387 			return -ENOMEM;
8388 		}
8389 
8390 		/* Instruct the rx_monitor object to instantiate its ring */
8391 		if (lpfc_rx_monitor_create_ring(phba->rx_monitor,
8392 						LPFC_MAX_RXMONITOR_ENTRY)) {
8393 			kfree(phba->rx_monitor);
8394 			phba->rx_monitor = NULL;
8395 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8396 					"2645 Failed to alloc memory "
8397 					"for RX Monitor's Ring\n");
8398 			return -ENOMEM;
8399 		}
8400 	}
8401 
8402 	return 0;
8403 }
8404 
8405 static int
lpfc_set_host_tm(struct lpfc_hba * phba)8406 lpfc_set_host_tm(struct lpfc_hba *phba)
8407 {
8408 	LPFC_MBOXQ_t *mboxq;
8409 	uint32_t len, rc;
8410 	struct timespec64 cur_time;
8411 	struct tm broken;
8412 	uint32_t month, day, year;
8413 	uint32_t hour, minute, second;
8414 	struct lpfc_mbx_set_host_date_time *tm;
8415 
8416 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8417 	if (!mboxq)
8418 		return -ENOMEM;
8419 
8420 	len = sizeof(struct lpfc_mbx_set_host_data) -
8421 		sizeof(struct lpfc_sli4_cfg_mhdr);
8422 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8423 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8424 			 LPFC_SLI4_MBX_EMBED);
8425 
8426 	mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8427 	mboxq->u.mqe.un.set_host_data.param_len =
8428 			sizeof(struct lpfc_mbx_set_host_date_time);
8429 	tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8430 	ktime_get_real_ts64(&cur_time);
8431 	time64_to_tm(cur_time.tv_sec, 0, &broken);
8432 	month = broken.tm_mon + 1;
8433 	day = broken.tm_mday;
8434 	year = broken.tm_year - 100;
8435 	hour = broken.tm_hour;
8436 	minute = broken.tm_min;
8437 	second = broken.tm_sec;
8438 	bf_set(lpfc_mbx_set_host_month, tm, month);
8439 	bf_set(lpfc_mbx_set_host_day, tm, day);
8440 	bf_set(lpfc_mbx_set_host_year, tm, year);
8441 	bf_set(lpfc_mbx_set_host_hour, tm, hour);
8442 	bf_set(lpfc_mbx_set_host_min, tm, minute);
8443 	bf_set(lpfc_mbx_set_host_sec, tm, second);
8444 
8445 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8446 	mempool_free(mboxq, phba->mbox_mem_pool);
8447 	return rc;
8448 }
8449 
8450 /**
8451  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
8452  * @phba: Pointer to HBA context object.
8453  *
8454  * This function is the main SLI4 device initialization PCI function. This
8455  * function is called by the HBA initialization code, HBA reset code and
8456  * HBA error attention handler code. Caller is not required to hold any
8457  * locks.
8458  **/
8459 int
lpfc_sli4_hba_setup(struct lpfc_hba * phba)8460 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8461 {
8462 	int rc, i, cnt, len, dd;
8463 	LPFC_MBOXQ_t *mboxq;
8464 	struct lpfc_mqe *mqe;
8465 	uint8_t *vpd;
8466 	uint32_t vpd_size;
8467 	uint32_t ftr_rsp = 0;
8468 	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8469 	struct lpfc_vport *vport = phba->pport;
8470 	struct lpfc_dmabuf *mp;
8471 	struct lpfc_rqb *rqbp;
8472 	u32 flg;
8473 
8474 	/* Perform a PCI function reset to start from clean */
8475 	rc = lpfc_pci_function_reset(phba);
8476 	if (unlikely(rc))
8477 		return -ENODEV;
8478 
8479 	/* Check the HBA Host Status Register for readyness */
8480 	rc = lpfc_sli4_post_status_check(phba);
8481 	if (unlikely(rc))
8482 		return -ENODEV;
8483 	else {
8484 		spin_lock_irq(&phba->hbalock);
8485 		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
8486 		flg = phba->sli.sli_flag;
8487 		spin_unlock_irq(&phba->hbalock);
8488 		/* Allow a little time after setting SLI_ACTIVE for any polled
8489 		 * MBX commands to complete via BSG.
8490 		 */
8491 		for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {
8492 			msleep(20);
8493 			spin_lock_irq(&phba->hbalock);
8494 			flg = phba->sli.sli_flag;
8495 			spin_unlock_irq(&phba->hbalock);
8496 		}
8497 	}
8498 	phba->hba_flag &= ~HBA_SETUP;
8499 
8500 	lpfc_sli4_dip(phba);
8501 
8502 	/*
8503 	 * Allocate a single mailbox container for initializing the
8504 	 * port.
8505 	 */
8506 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8507 	if (!mboxq)
8508 		return -ENOMEM;
8509 
8510 	/* Issue READ_REV to collect vpd and FW information. */
8511 	vpd_size = SLI4_PAGE_SIZE;
8512 	vpd = kzalloc(vpd_size, GFP_KERNEL);
8513 	if (!vpd) {
8514 		rc = -ENOMEM;
8515 		goto out_free_mbox;
8516 	}
8517 
8518 	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
8519 	if (unlikely(rc)) {
8520 		kfree(vpd);
8521 		goto out_free_mbox;
8522 	}
8523 
8524 	mqe = &mboxq->u.mqe;
8525 	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
8526 	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
8527 		phba->hba_flag |= HBA_FCOE_MODE;
8528 		phba->fcp_embed_io = 0;	/* SLI4 FC support only */
8529 	} else {
8530 		phba->hba_flag &= ~HBA_FCOE_MODE;
8531 	}
8532 
8533 	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8534 		LPFC_DCBX_CEE_MODE)
8535 		phba->hba_flag |= HBA_FIP_SUPPORT;
8536 	else
8537 		phba->hba_flag &= ~HBA_FIP_SUPPORT;
8538 
8539 	phba->hba_flag &= ~HBA_IOQ_FLUSH;
8540 
8541 	if (phba->sli_rev != LPFC_SLI_REV4) {
8542 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8543 			"0376 READ_REV Error. SLI Level %d "
8544 			"FCoE enabled %d\n",
8545 			phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
8546 		rc = -EIO;
8547 		kfree(vpd);
8548 		goto out_free_mbox;
8549 	}
8550 
8551 	rc = lpfc_set_host_tm(phba);
8552 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
8553 			"6468 Set host date / time: Status x%x:\n", rc);
8554 
8555 	/*
8556 	 * Continue initialization with default values even if driver failed
8557 	 * to read FCoE param config regions, only read parameters if the
8558 	 * board is FCoE
8559 	 */
8560 	if (phba->hba_flag & HBA_FCOE_MODE &&
8561 	    lpfc_sli4_read_fcoe_params(phba))
8562 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
8563 			"2570 Failed to read FCoE parameters\n");
8564 
8565 	/*
8566 	 * Retrieve sli4 device physical port name, failure of doing it
8567 	 * is considered as non-fatal.
8568 	 */
8569 	rc = lpfc_sli4_retrieve_pport_name(phba);
8570 	if (!rc)
8571 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8572 				"3080 Successful retrieving SLI4 device "
8573 				"physical port name: %s.\n", phba->Port);
8574 
8575 	rc = lpfc_sli4_get_ctl_attr(phba);
8576 	if (!rc)
8577 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8578 				"8351 Successful retrieving SLI4 device "
8579 				"CTL ATTR\n");
8580 
8581 	/*
8582 	 * Evaluate the read rev and vpd data. Populate the driver
8583 	 * state with the results. If this routine fails, the failure
8584 	 * is not fatal as the driver will use generic values.
8585 	 */
8586 	rc = lpfc_parse_vpd(phba, vpd, vpd_size);
8587 	if (unlikely(!rc)) {
8588 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8589 				"0377 Error %d parsing vpd. "
8590 				"Using defaults.\n", rc);
8591 		rc = 0;
8592 	}
8593 	kfree(vpd);
8594 
8595 	/* Save information as VPD data */
8596 	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8597 	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
8598 
8599 	/*
8600 	 * This is because first G7 ASIC doesn't support the standard
8601 	 * 0x5a NVME cmd descriptor type/subtype
8602 	 */
8603 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8604 			LPFC_SLI_INTF_IF_TYPE_6) &&
8605 	    (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8606 	    (phba->vpd.rev.smRev == 0) &&
8607 	    (phba->cfg_nvme_embed_cmd == 1))
8608 		phba->cfg_nvme_embed_cmd = 0;
8609 
8610 	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8611 	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8612 					 &mqe->un.read_rev);
8613 	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8614 				       &mqe->un.read_rev);
8615 	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8616 					    &mqe->un.read_rev);
8617 	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8618 					   &mqe->un.read_rev);
8619 	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8620 	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8621 	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8622 	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8623 	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8624 	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8625 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8626 			"(%d):0380 READ_REV Status x%x "
8627 			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8628 			mboxq->vport ? mboxq->vport->vpi : 0,
8629 			bf_get(lpfc_mqe_status, mqe),
8630 			phba->vpd.rev.opFwName,
8631 			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8632 			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
8633 
8634 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8635 	    LPFC_SLI_INTF_IF_TYPE_0) {
8636 		lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8637 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8638 		if (rc == MBX_SUCCESS) {
8639 			phba->hba_flag |= HBA_RECOVERABLE_UE;
8640 			/* Set 1Sec interval to detect UE */
8641 			phba->eratt_poll_interval = 1;
8642 			phba->sli4_hba.ue_to_sr = bf_get(
8643 					lpfc_mbx_set_feature_UESR,
8644 					&mboxq->u.mqe.un.set_feature);
8645 			phba->sli4_hba.ue_to_rp = bf_get(
8646 					lpfc_mbx_set_feature_UERP,
8647 					&mboxq->u.mqe.un.set_feature);
8648 		}
8649 	}
8650 
8651 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8652 		/* Enable MDS Diagnostics only if the SLI Port supports it */
8653 		lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8654 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8655 		if (rc != MBX_SUCCESS)
8656 			phba->mds_diags_support = 0;
8657 	}
8658 
8659 	/*
8660 	 * Discover the port's supported feature set and match it against the
8661 	 * hosts requests.
8662 	 */
8663 	lpfc_request_features(phba, mboxq);
8664 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8665 	if (unlikely(rc)) {
8666 		rc = -EIO;
8667 		goto out_free_mbox;
8668 	}
8669 
8670 	/* Disable VMID if app header is not supported */
8671 	if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8672 						  &mqe->un.req_ftrs))) {
8673 		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8674 		phba->cfg_vmid_app_header = 0;
8675 		lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8676 				"1242 vmid feature not supported\n");
8677 	}
8678 
8679 	/*
8680 	 * The port must support FCP initiator mode as this is the
8681 	 * only mode running in the host.
8682 	 */
8683 	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8684 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8685 				"0378 No support for fcpi mode.\n");
8686 		ftr_rsp++;
8687 	}
8688 
8689 	/* Performance Hints are ONLY for FCoE */
8690 	if (phba->hba_flag & HBA_FCOE_MODE) {
8691 		if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
8692 			phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
8693 		else
8694 			phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
8695 	}
8696 
8697 	/*
8698 	 * If the port cannot support the host's requested features
8699 	 * then turn off the global config parameters to disable the
8700 	 * feature in the driver.  This is not a fatal error.
8701 	 */
8702 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8703 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8704 			phba->cfg_enable_bg = 0;
8705 			phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
8706 			ftr_rsp++;
8707 		}
8708 	}
8709 
8710 	if (phba->max_vpi && phba->cfg_enable_npiv &&
8711 	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8712 		ftr_rsp++;
8713 
8714 	if (ftr_rsp) {
8715 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8716 				"0379 Feature Mismatch Data: x%08x %08x "
8717 				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8718 				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8719 				phba->cfg_enable_npiv, phba->max_vpi);
8720 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8721 			phba->cfg_enable_bg = 0;
8722 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8723 			phba->cfg_enable_npiv = 0;
8724 	}
8725 
8726 	/* These SLI3 features are assumed in SLI4 */
8727 	spin_lock_irq(&phba->hbalock);
8728 	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8729 	spin_unlock_irq(&phba->hbalock);
8730 
8731 	/* Always try to enable dual dump feature if we can */
8732 	lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8733 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8734 	dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8735 	if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
8736 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8737 				"6448 Dual Dump is enabled\n");
8738 	else
8739 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8740 				"6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8741 				"rc:x%x dd:x%x\n",
8742 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8743 				lpfc_sli_config_mbox_subsys_get(
8744 					phba, mboxq),
8745 				lpfc_sli_config_mbox_opcode_get(
8746 					phba, mboxq),
8747 				rc, dd);
8748 	/*
8749 	 * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
8750 	 * calls depends on these resources to complete port setup.
8751 	 */
8752 	rc = lpfc_sli4_alloc_resource_identifiers(phba);
8753 	if (rc) {
8754 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8755 				"2920 Failed to alloc Resource IDs "
8756 				"rc = x%x\n", rc);
8757 		goto out_free_mbox;
8758 	}
8759 
8760 	lpfc_set_host_data(phba, mboxq);
8761 
8762 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8763 	if (rc) {
8764 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8765 				"2134 Failed to set host os driver version %x",
8766 				rc);
8767 	}
8768 
8769 	/* Read the port's service parameters. */
8770 	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8771 	if (rc) {
8772 		phba->link_state = LPFC_HBA_ERROR;
8773 		rc = -ENOMEM;
8774 		goto out_free_mbox;
8775 	}
8776 
8777 	mboxq->vport = vport;
8778 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8779 	mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
8780 	if (rc == MBX_SUCCESS) {
8781 		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8782 		rc = 0;
8783 	}
8784 
8785 	/*
8786 	 * This memory was allocated by the lpfc_read_sparam routine but is
8787 	 * no longer needed.  It is released and ctx_buf NULLed to prevent
8788 	 * unintended pointer access as the mbox is reused.
8789 	 */
8790 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
8791 	kfree(mp);
8792 	mboxq->ctx_buf = NULL;
8793 	if (unlikely(rc)) {
8794 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8795 				"0382 READ_SPARAM command failed "
8796 				"status %d, mbxStatus x%x\n",
8797 				rc, bf_get(lpfc_mqe_status, mqe));
8798 		phba->link_state = LPFC_HBA_ERROR;
8799 		rc = -EIO;
8800 		goto out_free_mbox;
8801 	}
8802 
8803 	lpfc_update_vport_wwn(vport);
8804 
8805 	/* Update the fc_host data structures with new wwn. */
8806 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8807 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8808 
8809 	/* Create all the SLI4 queues */
8810 	rc = lpfc_sli4_queue_create(phba);
8811 	if (rc) {
8812 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8813 				"3089 Failed to allocate queues\n");
8814 		rc = -ENODEV;
8815 		goto out_free_mbox;
8816 	}
8817 	/* Set up all the queues to the device */
8818 	rc = lpfc_sli4_queue_setup(phba);
8819 	if (unlikely(rc)) {
8820 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8821 				"0381 Error %d during queue setup.\n ", rc);
8822 		goto out_stop_timers;
8823 	}
8824 	/* Initialize the driver internal SLI layer lists. */
8825 	lpfc_sli4_setup(phba);
8826 	lpfc_sli4_queue_init(phba);
8827 
8828 	/* update host els xri-sgl sizes and mappings */
8829 	rc = lpfc_sli4_els_sgl_update(phba);
8830 	if (unlikely(rc)) {
8831 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8832 				"1400 Failed to update xri-sgl size and "
8833 				"mapping: %d\n", rc);
8834 		goto out_destroy_queue;
8835 	}
8836 
8837 	/* register the els sgl pool to the port */
8838 	rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8839 				       phba->sli4_hba.els_xri_cnt);
8840 	if (unlikely(rc < 0)) {
8841 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8842 				"0582 Error %d during els sgl post "
8843 				"operation\n", rc);
8844 		rc = -ENODEV;
8845 		goto out_destroy_queue;
8846 	}
8847 	phba->sli4_hba.els_xri_cnt = rc;
8848 
8849 	if (phba->nvmet_support) {
8850 		/* update host nvmet xri-sgl sizes and mappings */
8851 		rc = lpfc_sli4_nvmet_sgl_update(phba);
8852 		if (unlikely(rc)) {
8853 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8854 					"6308 Failed to update nvmet-sgl size "
8855 					"and mapping: %d\n", rc);
8856 			goto out_destroy_queue;
8857 		}
8858 
8859 		/* register the nvmet sgl pool to the port */
8860 		rc = lpfc_sli4_repost_sgl_list(
8861 			phba,
8862 			&phba->sli4_hba.lpfc_nvmet_sgl_list,
8863 			phba->sli4_hba.nvmet_xri_cnt);
8864 		if (unlikely(rc < 0)) {
8865 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8866 					"3117 Error %d during nvmet "
8867 					"sgl post\n", rc);
8868 			rc = -ENODEV;
8869 			goto out_destroy_queue;
8870 		}
8871 		phba->sli4_hba.nvmet_xri_cnt = rc;
8872 
8873 		/* We allocate an iocbq for every receive context SGL.
8874 		 * The additional allocation is for abort and ls handling.
8875 		 */
8876 		cnt = phba->sli4_hba.nvmet_xri_cnt +
8877 			phba->sli4_hba.max_cfg_param.max_xri;
8878 	} else {
8879 		/* update host common xri-sgl sizes and mappings */
8880 		rc = lpfc_sli4_io_sgl_update(phba);
8881 		if (unlikely(rc)) {
8882 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8883 					"6082 Failed to update nvme-sgl size "
8884 					"and mapping: %d\n", rc);
8885 			goto out_destroy_queue;
8886 		}
8887 
8888 		/* register the allocated common sgl pool to the port */
8889 		rc = lpfc_sli4_repost_io_sgl_list(phba);
8890 		if (unlikely(rc)) {
8891 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8892 					"6116 Error %d during nvme sgl post "
8893 					"operation\n", rc);
8894 			/* Some NVME buffers were moved to abort nvme list */
8895 			/* A pci function reset will repost them */
8896 			rc = -ENODEV;
8897 			goto out_destroy_queue;
8898 		}
8899 		/* Each lpfc_io_buf job structure has an iocbq element.
8900 		 * This cnt provides for abort, els, ct and ls requests.
8901 		 */
8902 		cnt = phba->sli4_hba.max_cfg_param.max_xri;
8903 	}
8904 
8905 	if (!phba->sli.iocbq_lookup) {
8906 		/* Initialize and populate the iocb list per host */
8907 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8908 				"2821 initialize iocb list with %d entries\n",
8909 				cnt);
8910 		rc = lpfc_init_iocb_list(phba, cnt);
8911 		if (rc) {
8912 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8913 					"1413 Failed to init iocb list.\n");
8914 			goto out_destroy_queue;
8915 		}
8916 	}
8917 
8918 	if (phba->nvmet_support)
8919 		lpfc_nvmet_create_targetport(phba);
8920 
8921 	if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
8922 		/* Post initial buffers to all RQs created */
8923 		for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8924 			rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8925 			INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8926 			rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8927 			rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
8928 			rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
8929 			rqbp->buffer_count = 0;
8930 
8931 			lpfc_post_rq_buffer(
8932 				phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8933 				phba->sli4_hba.nvmet_mrq_data[i],
8934 				phba->cfg_nvmet_mrq_post, i);
8935 		}
8936 	}
8937 
8938 	/* Post the rpi header region to the device. */
8939 	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8940 	if (unlikely(rc)) {
8941 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8942 				"0393 Error %d during rpi post operation\n",
8943 				rc);
8944 		rc = -ENODEV;
8945 		goto out_free_iocblist;
8946 	}
8947 	lpfc_sli4_node_prep(phba);
8948 
8949 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
8950 		if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
8951 			/*
8952 			 * The FC Port needs to register FCFI (index 0)
8953 			 */
8954 			lpfc_reg_fcfi(phba, mboxq);
8955 			mboxq->vport = phba->pport;
8956 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8957 			if (rc != MBX_SUCCESS)
8958 				goto out_unset_queue;
8959 			rc = 0;
8960 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
8961 						&mboxq->u.mqe.un.reg_fcfi);
8962 		} else {
8963 			/* We are a NVME Target mode with MRQ > 1 */
8964 
8965 			/* First register the FCFI */
8966 			lpfc_reg_fcfi_mrq(phba, mboxq, 0);
8967 			mboxq->vport = phba->pport;
8968 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8969 			if (rc != MBX_SUCCESS)
8970 				goto out_unset_queue;
8971 			rc = 0;
8972 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
8973 						&mboxq->u.mqe.un.reg_fcfi_mrq);
8974 
8975 			/* Next register the MRQs */
8976 			lpfc_reg_fcfi_mrq(phba, mboxq, 1);
8977 			mboxq->vport = phba->pport;
8978 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8979 			if (rc != MBX_SUCCESS)
8980 				goto out_unset_queue;
8981 			rc = 0;
8982 		}
8983 		/* Check if the port is configured to be disabled */
8984 		lpfc_sli_read_link_ste(phba);
8985 	}
8986 
8987 	/* Don't post more new bufs if repost already recovered
8988 	 * the nvme sgls.
8989 	 */
8990 	if (phba->nvmet_support == 0) {
8991 		if (phba->sli4_hba.io_xri_cnt == 0) {
8992 			len = lpfc_new_io_buf(
8993 					      phba, phba->sli4_hba.io_xri_max);
8994 			if (len == 0) {
8995 				rc = -ENOMEM;
8996 				goto out_unset_queue;
8997 			}
8998 
8999 			if (phba->cfg_xri_rebalancing)
9000 				lpfc_create_multixri_pools(phba);
9001 		}
9002 	} else {
9003 		phba->cfg_xri_rebalancing = 0;
9004 	}
9005 
9006 	/* Allow asynchronous mailbox command to go through */
9007 	spin_lock_irq(&phba->hbalock);
9008 	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9009 	spin_unlock_irq(&phba->hbalock);
9010 
9011 	/* Post receive buffers to the device */
9012 	lpfc_sli4_rb_setup(phba);
9013 
9014 	/* Reset HBA FCF states after HBA reset */
9015 	phba->fcf.fcf_flag = 0;
9016 	phba->fcf.current_rec.flag = 0;
9017 
9018 	/* Start the ELS watchdog timer */
9019 	mod_timer(&vport->els_tmofunc,
9020 		  jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
9021 
9022 	/* Start heart beat timer */
9023 	mod_timer(&phba->hb_tmofunc,
9024 		  jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
9025 	phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
9026 	phba->last_completion_time = jiffies;
9027 
9028 	/* start eq_delay heartbeat */
9029 	if (phba->cfg_auto_imax)
9030 		queue_delayed_work(phba->wq, &phba->eq_delay_work,
9031 				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
9032 
9033 	/* start per phba idle_stat_delay heartbeat */
9034 	lpfc_init_idle_stat_hb(phba);
9035 
9036 	/* Start error attention (ERATT) polling timer */
9037 	mod_timer(&phba->eratt_poll,
9038 		  jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9039 
9040 	/*
9041 	 * The port is ready, set the host's link state to LINK_DOWN
9042 	 * in preparation for link interrupts.
9043 	 */
9044 	spin_lock_irq(&phba->hbalock);
9045 	phba->link_state = LPFC_LINK_DOWN;
9046 
9047 	/* Check if physical ports are trunked */
9048 	if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
9049 		phba->trunk_link.link0.state = LPFC_LINK_DOWN;
9050 	if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
9051 		phba->trunk_link.link1.state = LPFC_LINK_DOWN;
9052 	if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
9053 		phba->trunk_link.link2.state = LPFC_LINK_DOWN;
9054 	if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
9055 		phba->trunk_link.link3.state = LPFC_LINK_DOWN;
9056 	spin_unlock_irq(&phba->hbalock);
9057 
9058 	/* Arm the CQs and then EQs on device */
9059 	lpfc_sli4_arm_cqeq_intr(phba);
9060 
9061 	/* Indicate device interrupt mode */
9062 	phba->sli4_hba.intr_enable = 1;
9063 
9064 	/* Setup CMF after HBA is initialized */
9065 	lpfc_cmf_setup(phba);
9066 
9067 	if (!(phba->hba_flag & HBA_FCOE_MODE) &&
9068 	    (phba->hba_flag & LINK_DISABLED)) {
9069 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9070 				"3103 Adapter Link is disabled.\n");
9071 		lpfc_down_link(phba, mboxq);
9072 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9073 		if (rc != MBX_SUCCESS) {
9074 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9075 					"3104 Adapter failed to issue "
9076 					"DOWN_LINK mbox cmd, rc:x%x\n", rc);
9077 			goto out_io_buff_free;
9078 		}
9079 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
9080 		/* don't perform init_link on SLI4 FC port loopback test */
9081 		if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
9082 			rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
9083 			if (rc)
9084 				goto out_io_buff_free;
9085 		}
9086 	}
9087 	mempool_free(mboxq, phba->mbox_mem_pool);
9088 
9089 	/* Enable RAS FW log support */
9090 	lpfc_sli4_ras_setup(phba);
9091 
9092 	phba->hba_flag |= HBA_SETUP;
9093 	return rc;
9094 
9095 out_io_buff_free:
9096 	/* Free allocated IO Buffers */
9097 	lpfc_io_free(phba);
9098 out_unset_queue:
9099 	/* Unset all the queues set up in this routine when error out */
9100 	lpfc_sli4_queue_unset(phba);
9101 out_free_iocblist:
9102 	lpfc_free_iocb_list(phba);
9103 out_destroy_queue:
9104 	lpfc_sli4_queue_destroy(phba);
9105 out_stop_timers:
9106 	lpfc_stop_hba_timers(phba);
9107 out_free_mbox:
9108 	mempool_free(mboxq, phba->mbox_mem_pool);
9109 	return rc;
9110 }
9111 
9112 /**
9113  * lpfc_mbox_timeout - Timeout call back function for mbox timer
9114  * @t: Context to fetch pointer to hba structure from.
9115  *
9116  * This is the callback function for mailbox timer. The mailbox
9117  * timer is armed when a new mailbox command is issued and the timer
9118  * is deleted when the mailbox complete. The function is called by
9119  * the kernel timer code when a mailbox does not complete within
9120  * expected time. This function wakes up the worker thread to
9121  * process the mailbox timeout and returns. All the processing is
9122  * done by the worker thread function lpfc_mbox_timeout_handler.
9123  **/
9124 void
lpfc_mbox_timeout(struct timer_list * t)9125 lpfc_mbox_timeout(struct timer_list *t)
9126 {
9127 	struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
9128 	unsigned long iflag;
9129 	uint32_t tmo_posted;
9130 
9131 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
9132 	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
9133 	if (!tmo_posted)
9134 		phba->pport->work_port_events |= WORKER_MBOX_TMO;
9135 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
9136 
9137 	if (!tmo_posted)
9138 		lpfc_worker_wake_up(phba);
9139 	return;
9140 }
9141 
9142 /**
9143  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
9144  *                                    are pending
9145  * @phba: Pointer to HBA context object.
9146  *
9147  * This function checks if any mailbox completions are present on the mailbox
9148  * completion queue.
9149  **/
9150 static bool
lpfc_sli4_mbox_completions_pending(struct lpfc_hba * phba)9151 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
9152 {
9153 
9154 	uint32_t idx;
9155 	struct lpfc_queue *mcq;
9156 	struct lpfc_mcqe *mcqe;
9157 	bool pending_completions = false;
9158 	uint8_t	qe_valid;
9159 
9160 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9161 		return false;
9162 
9163 	/* Check for completions on mailbox completion queue */
9164 
9165 	mcq = phba->sli4_hba.mbx_cq;
9166 	idx = mcq->hba_index;
9167 	qe_valid = mcq->qe_valid;
9168 	while (bf_get_le32(lpfc_cqe_valid,
9169 	       (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
9170 		mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
9171 		if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
9172 		    (!bf_get_le32(lpfc_trailer_async, mcqe))) {
9173 			pending_completions = true;
9174 			break;
9175 		}
9176 		idx = (idx + 1) % mcq->entry_count;
9177 		if (mcq->hba_index == idx)
9178 			break;
9179 
9180 		/* if the index wrapped around, toggle the valid bit */
9181 		if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
9182 			qe_valid = (qe_valid) ? 0 : 1;
9183 	}
9184 	return pending_completions;
9185 
9186 }
9187 
9188 /**
9189  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
9190  *					      that were missed.
9191  * @phba: Pointer to HBA context object.
9192  *
9193  * For sli4, it is possible to miss an interrupt. As such mbox completions
9194  * maybe missed causing erroneous mailbox timeouts to occur. This function
9195  * checks to see if mbox completions are on the mailbox completion queue
9196  * and will process all the completions associated with the eq for the
9197  * mailbox completion queue.
9198  **/
9199 static bool
lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba * phba)9200 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
9201 {
9202 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
9203 	uint32_t eqidx;
9204 	struct lpfc_queue *fpeq = NULL;
9205 	struct lpfc_queue *eq;
9206 	bool mbox_pending;
9207 
9208 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9209 		return false;
9210 
9211 	/* Find the EQ associated with the mbox CQ */
9212 	if (sli4_hba->hdwq) {
9213 		for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
9214 			eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
9215 			if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
9216 				fpeq = eq;
9217 				break;
9218 			}
9219 		}
9220 	}
9221 	if (!fpeq)
9222 		return false;
9223 
9224 	/* Turn off interrupts from this EQ */
9225 
9226 	sli4_hba->sli4_eq_clr_intr(fpeq);
9227 
9228 	/* Check to see if a mbox completion is pending */
9229 
9230 	mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
9231 
9232 	/*
9233 	 * If a mbox completion is pending, process all the events on EQ
9234 	 * associated with the mbox completion queue (this could include
9235 	 * mailbox commands, async events, els commands, receive queue data
9236 	 * and fcp commands)
9237 	 */
9238 
9239 	if (mbox_pending)
9240 		/* process and rearm the EQ */
9241 		lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
9242 				     LPFC_QUEUE_WORK);
9243 	else
9244 		/* Always clear and re-arm the EQ */
9245 		sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
9246 
9247 	return mbox_pending;
9248 
9249 }
9250 
9251 /**
9252  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
9253  * @phba: Pointer to HBA context object.
9254  *
9255  * This function is called from worker thread when a mailbox command times out.
9256  * The caller is not required to hold any locks. This function will reset the
9257  * HBA and recover all the pending commands.
9258  **/
9259 void
lpfc_mbox_timeout_handler(struct lpfc_hba * phba)9260 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
9261 {
9262 	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
9263 	MAILBOX_t *mb = NULL;
9264 
9265 	struct lpfc_sli *psli = &phba->sli;
9266 
9267 	/* If the mailbox completed, process the completion */
9268 	lpfc_sli4_process_missed_mbox_completions(phba);
9269 
9270 	if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
9271 		return;
9272 
9273 	if (pmbox != NULL)
9274 		mb = &pmbox->u.mb;
9275 	/* Check the pmbox pointer first.  There is a race condition
9276 	 * between the mbox timeout handler getting executed in the
9277 	 * worklist and the mailbox actually completing. When this
9278 	 * race condition occurs, the mbox_active will be NULL.
9279 	 */
9280 	spin_lock_irq(&phba->hbalock);
9281 	if (pmbox == NULL) {
9282 		lpfc_printf_log(phba, KERN_WARNING,
9283 				LOG_MBOX | LOG_SLI,
9284 				"0353 Active Mailbox cleared - mailbox timeout "
9285 				"exiting\n");
9286 		spin_unlock_irq(&phba->hbalock);
9287 		return;
9288 	}
9289 
9290 	/* Mbox cmd <mbxCommand> timeout */
9291 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9292 			"0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
9293 			mb->mbxCommand,
9294 			phba->pport->port_state,
9295 			phba->sli.sli_flag,
9296 			phba->sli.mbox_active);
9297 	spin_unlock_irq(&phba->hbalock);
9298 
9299 	/* Setting state unknown so lpfc_sli_abort_iocb_ring
9300 	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
9301 	 * it to fail all outstanding SCSI IO.
9302 	 */
9303 	set_bit(MBX_TMO_ERR, &phba->bit_flags);
9304 	spin_lock_irq(&phba->pport->work_port_lock);
9305 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9306 	spin_unlock_irq(&phba->pport->work_port_lock);
9307 	spin_lock_irq(&phba->hbalock);
9308 	phba->link_state = LPFC_LINK_UNKNOWN;
9309 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9310 	spin_unlock_irq(&phba->hbalock);
9311 
9312 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9313 			"0345 Resetting board due to mailbox timeout\n");
9314 
9315 	/* Reset the HBA device */
9316 	lpfc_reset_hba(phba);
9317 }
9318 
9319 /**
9320  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
9321  * @phba: Pointer to HBA context object.
9322  * @pmbox: Pointer to mailbox object.
9323  * @flag: Flag indicating how the mailbox need to be processed.
9324  *
9325  * This function is called by discovery code and HBA management code
9326  * to submit a mailbox command to firmware with SLI-3 interface spec. This
9327  * function gets the hbalock to protect the data structures.
9328  * The mailbox command can be submitted in polling mode, in which case
9329  * this function will wait in a polling loop for the completion of the
9330  * mailbox.
9331  * If the mailbox is submitted in no_wait mode (not polling) the
9332  * function will submit the command and returns immediately without waiting
9333  * for the mailbox completion. The no_wait is supported only when HBA
9334  * is in SLI2/SLI3 mode - interrupts are enabled.
9335  * The SLI interface allows only one mailbox pending at a time. If the
9336  * mailbox is issued in polling mode and there is already a mailbox
9337  * pending, then the function will return an error. If the mailbox is issued
9338  * in NO_WAIT mode and there is a mailbox pending already, the function
9339  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9340  * The sli layer owns the mailbox object until the completion of mailbox
9341  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9342  * return codes the caller owns the mailbox command after the return of
9343  * the function.
9344  **/
9345 static int
lpfc_sli_issue_mbox_s3(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)9346 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9347 		       uint32_t flag)
9348 {
9349 	MAILBOX_t *mbx;
9350 	struct lpfc_sli *psli = &phba->sli;
9351 	uint32_t status, evtctr;
9352 	uint32_t ha_copy, hc_copy;
9353 	int i;
9354 	unsigned long timeout;
9355 	unsigned long drvr_flag = 0;
9356 	uint32_t word0, ldata;
9357 	void __iomem *to_slim;
9358 	int processing_queue = 0;
9359 
9360 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
9361 	if (!pmbox) {
9362 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9363 		/* processing mbox queue from intr_handler */
9364 		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9365 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9366 			return MBX_SUCCESS;
9367 		}
9368 		processing_queue = 1;
9369 		pmbox = lpfc_mbox_get(phba);
9370 		if (!pmbox) {
9371 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9372 			return MBX_SUCCESS;
9373 		}
9374 	}
9375 
9376 	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
9377 		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
9378 		if(!pmbox->vport) {
9379 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9380 			lpfc_printf_log(phba, KERN_ERR,
9381 					LOG_MBOX | LOG_VPORT,
9382 					"1806 Mbox x%x failed. No vport\n",
9383 					pmbox->u.mb.mbxCommand);
9384 			dump_stack();
9385 			goto out_not_finished;
9386 		}
9387 	}
9388 
9389 	/* If the PCI channel is in offline state, do not post mbox. */
9390 	if (unlikely(pci_channel_offline(phba->pcidev))) {
9391 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9392 		goto out_not_finished;
9393 	}
9394 
9395 	/* If HBA has a deferred error attention, fail the iocb. */
9396 	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9397 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9398 		goto out_not_finished;
9399 	}
9400 
9401 	psli = &phba->sli;
9402 
9403 	mbx = &pmbox->u.mb;
9404 	status = MBX_SUCCESS;
9405 
9406 	if (phba->link_state == LPFC_HBA_ERROR) {
9407 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9408 
9409 		/* Mbox command <mbxCommand> cannot issue */
9410 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9411 				"(%d):0311 Mailbox command x%x cannot "
9412 				"issue Data: x%x x%x\n",
9413 				pmbox->vport ? pmbox->vport->vpi : 0,
9414 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9415 		goto out_not_finished;
9416 	}
9417 
9418 	if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9419 		if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9420 			!(hc_copy & HC_MBINT_ENA)) {
9421 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9422 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9423 				"(%d):2528 Mailbox command x%x cannot "
9424 				"issue Data: x%x x%x\n",
9425 				pmbox->vport ? pmbox->vport->vpi : 0,
9426 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9427 			goto out_not_finished;
9428 		}
9429 	}
9430 
9431 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9432 		/* Polling for a mbox command when another one is already active
9433 		 * is not allowed in SLI. Also, the driver must have established
9434 		 * SLI2 mode to queue and process multiple mbox commands.
9435 		 */
9436 
9437 		if (flag & MBX_POLL) {
9438 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9439 
9440 			/* Mbox command <mbxCommand> cannot issue */
9441 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9442 					"(%d):2529 Mailbox command x%x "
9443 					"cannot issue Data: x%x x%x\n",
9444 					pmbox->vport ? pmbox->vport->vpi : 0,
9445 					pmbox->u.mb.mbxCommand,
9446 					psli->sli_flag, flag);
9447 			goto out_not_finished;
9448 		}
9449 
9450 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
9451 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9452 			/* Mbox command <mbxCommand> cannot issue */
9453 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9454 					"(%d):2530 Mailbox command x%x "
9455 					"cannot issue Data: x%x x%x\n",
9456 					pmbox->vport ? pmbox->vport->vpi : 0,
9457 					pmbox->u.mb.mbxCommand,
9458 					psli->sli_flag, flag);
9459 			goto out_not_finished;
9460 		}
9461 
9462 		/* Another mailbox command is still being processed, queue this
9463 		 * command to be processed later.
9464 		 */
9465 		lpfc_mbox_put(phba, pmbox);
9466 
9467 		/* Mbox cmd issue - BUSY */
9468 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9469 				"(%d):0308 Mbox cmd issue - BUSY Data: "
9470 				"x%x x%x x%x x%x\n",
9471 				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
9472 				mbx->mbxCommand,
9473 				phba->pport ? phba->pport->port_state : 0xff,
9474 				psli->sli_flag, flag);
9475 
9476 		psli->slistat.mbox_busy++;
9477 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9478 
9479 		if (pmbox->vport) {
9480 			lpfc_debugfs_disc_trc(pmbox->vport,
9481 				LPFC_DISC_TRC_MBOX_VPORT,
9482 				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
9483 				(uint32_t)mbx->mbxCommand,
9484 				mbx->un.varWords[0], mbx->un.varWords[1]);
9485 		}
9486 		else {
9487 			lpfc_debugfs_disc_trc(phba->pport,
9488 				LPFC_DISC_TRC_MBOX,
9489 				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
9490 				(uint32_t)mbx->mbxCommand,
9491 				mbx->un.varWords[0], mbx->un.varWords[1]);
9492 		}
9493 
9494 		return MBX_BUSY;
9495 	}
9496 
9497 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9498 
9499 	/* If we are not polling, we MUST be in SLI2 mode */
9500 	if (flag != MBX_POLL) {
9501 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
9502 		    (mbx->mbxCommand != MBX_KILL_BOARD)) {
9503 			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9504 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9505 			/* Mbox command <mbxCommand> cannot issue */
9506 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9507 					"(%d):2531 Mailbox command x%x "
9508 					"cannot issue Data: x%x x%x\n",
9509 					pmbox->vport ? pmbox->vport->vpi : 0,
9510 					pmbox->u.mb.mbxCommand,
9511 					psli->sli_flag, flag);
9512 			goto out_not_finished;
9513 		}
9514 		/* timeout active mbox command */
9515 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9516 					   1000);
9517 		mod_timer(&psli->mbox_tmo, jiffies + timeout);
9518 	}
9519 
9520 	/* Mailbox cmd <cmd> issue */
9521 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9522 			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
9523 			"x%x\n",
9524 			pmbox->vport ? pmbox->vport->vpi : 0,
9525 			mbx->mbxCommand,
9526 			phba->pport ? phba->pport->port_state : 0xff,
9527 			psli->sli_flag, flag);
9528 
9529 	if (mbx->mbxCommand != MBX_HEARTBEAT) {
9530 		if (pmbox->vport) {
9531 			lpfc_debugfs_disc_trc(pmbox->vport,
9532 				LPFC_DISC_TRC_MBOX_VPORT,
9533 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
9534 				(uint32_t)mbx->mbxCommand,
9535 				mbx->un.varWords[0], mbx->un.varWords[1]);
9536 		}
9537 		else {
9538 			lpfc_debugfs_disc_trc(phba->pport,
9539 				LPFC_DISC_TRC_MBOX,
9540 				"MBOX Send:       cmd:x%x mb:x%x x%x",
9541 				(uint32_t)mbx->mbxCommand,
9542 				mbx->un.varWords[0], mbx->un.varWords[1]);
9543 		}
9544 	}
9545 
9546 	psli->slistat.mbox_cmd++;
9547 	evtctr = psli->slistat.mbox_event;
9548 
9549 	/* next set own bit for the adapter and copy over command word */
9550 	mbx->mbxOwner = OWN_CHIP;
9551 
9552 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9553 		/* Populate mbox extension offset word. */
9554 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
9555 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9556 				= (uint8_t *)phba->mbox_ext
9557 				  - (uint8_t *)phba->mbox;
9558 		}
9559 
9560 		/* Copy the mailbox extension data */
9561 		if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
9562 			lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
9563 					      (uint8_t *)phba->mbox_ext,
9564 					      pmbox->in_ext_byte_len);
9565 		}
9566 		/* Copy command data to host SLIM area */
9567 		lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
9568 	} else {
9569 		/* Populate mbox extension offset word. */
9570 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
9571 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9572 				= MAILBOX_HBA_EXT_OFFSET;
9573 
9574 		/* Copy the mailbox extension data */
9575 		if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
9576 			lpfc_memcpy_to_slim(phba->MBslimaddr +
9577 				MAILBOX_HBA_EXT_OFFSET,
9578 				pmbox->ctx_buf, pmbox->in_ext_byte_len);
9579 
9580 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9581 			/* copy command data into host mbox for cmpl */
9582 			lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
9583 					      MAILBOX_CMD_SIZE);
9584 
9585 		/* First copy mbox command data to HBA SLIM, skip past first
9586 		   word */
9587 		to_slim = phba->MBslimaddr + sizeof (uint32_t);
9588 		lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
9589 			    MAILBOX_CMD_SIZE - sizeof (uint32_t));
9590 
9591 		/* Next copy over first word, with mbxOwner set */
9592 		ldata = *((uint32_t *)mbx);
9593 		to_slim = phba->MBslimaddr;
9594 		writel(ldata, to_slim);
9595 		readl(to_slim); /* flush */
9596 
9597 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9598 			/* switch over to host mailbox */
9599 			psli->sli_flag |= LPFC_SLI_ACTIVE;
9600 	}
9601 
9602 	wmb();
9603 
9604 	switch (flag) {
9605 	case MBX_NOWAIT:
9606 		/* Set up reference to mailbox command */
9607 		psli->mbox_active = pmbox;
9608 		/* Interrupt board to do it */
9609 		writel(CA_MBATT, phba->CAregaddr);
9610 		readl(phba->CAregaddr); /* flush */
9611 		/* Don't wait for it to finish, just return */
9612 		break;
9613 
9614 	case MBX_POLL:
9615 		/* Set up null reference to mailbox command */
9616 		psli->mbox_active = NULL;
9617 		/* Interrupt board to do it */
9618 		writel(CA_MBATT, phba->CAregaddr);
9619 		readl(phba->CAregaddr); /* flush */
9620 
9621 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9622 			/* First read mbox status word */
9623 			word0 = *((uint32_t *)phba->mbox);
9624 			word0 = le32_to_cpu(word0);
9625 		} else {
9626 			/* First read mbox status word */
9627 			if (lpfc_readl(phba->MBslimaddr, &word0)) {
9628 				spin_unlock_irqrestore(&phba->hbalock,
9629 						       drvr_flag);
9630 				goto out_not_finished;
9631 			}
9632 		}
9633 
9634 		/* Read the HBA Host Attention Register */
9635 		if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9636 			spin_unlock_irqrestore(&phba->hbalock,
9637 						       drvr_flag);
9638 			goto out_not_finished;
9639 		}
9640 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9641 							1000) + jiffies;
9642 		i = 0;
9643 		/* Wait for command to complete */
9644 		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9645 		       (!(ha_copy & HA_MBATT) &&
9646 			(phba->link_state > LPFC_WARM_START))) {
9647 			if (time_after(jiffies, timeout)) {
9648 				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9649 				spin_unlock_irqrestore(&phba->hbalock,
9650 						       drvr_flag);
9651 				goto out_not_finished;
9652 			}
9653 
9654 			/* Check if we took a mbox interrupt while we were
9655 			   polling */
9656 			if (((word0 & OWN_CHIP) != OWN_CHIP)
9657 			    && (evtctr != psli->slistat.mbox_event))
9658 				break;
9659 
9660 			if (i++ > 10) {
9661 				spin_unlock_irqrestore(&phba->hbalock,
9662 						       drvr_flag);
9663 				msleep(1);
9664 				spin_lock_irqsave(&phba->hbalock, drvr_flag);
9665 			}
9666 
9667 			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9668 				/* First copy command data */
9669 				word0 = *((uint32_t *)phba->mbox);
9670 				word0 = le32_to_cpu(word0);
9671 				if (mbx->mbxCommand == MBX_CONFIG_PORT) {
9672 					MAILBOX_t *slimmb;
9673 					uint32_t slimword0;
9674 					/* Check real SLIM for any errors */
9675 					slimword0 = readl(phba->MBslimaddr);
9676 					slimmb = (MAILBOX_t *) & slimword0;
9677 					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9678 					    && slimmb->mbxStatus) {
9679 						psli->sli_flag &=
9680 						    ~LPFC_SLI_ACTIVE;
9681 						word0 = slimword0;
9682 					}
9683 				}
9684 			} else {
9685 				/* First copy command data */
9686 				word0 = readl(phba->MBslimaddr);
9687 			}
9688 			/* Read the HBA Host Attention Register */
9689 			if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9690 				spin_unlock_irqrestore(&phba->hbalock,
9691 						       drvr_flag);
9692 				goto out_not_finished;
9693 			}
9694 		}
9695 
9696 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9697 			/* copy results back to user */
9698 			lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9699 						MAILBOX_CMD_SIZE);
9700 			/* Copy the mailbox extension data */
9701 			if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
9702 				lpfc_sli_pcimem_bcopy(phba->mbox_ext,
9703 						      pmbox->ctx_buf,
9704 						      pmbox->out_ext_byte_len);
9705 			}
9706 		} else {
9707 			/* First copy command data */
9708 			lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
9709 						MAILBOX_CMD_SIZE);
9710 			/* Copy the mailbox extension data */
9711 			if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
9712 				lpfc_memcpy_from_slim(
9713 					pmbox->ctx_buf,
9714 					phba->MBslimaddr +
9715 					MAILBOX_HBA_EXT_OFFSET,
9716 					pmbox->out_ext_byte_len);
9717 			}
9718 		}
9719 
9720 		writel(HA_MBATT, phba->HAregaddr);
9721 		readl(phba->HAregaddr); /* flush */
9722 
9723 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9724 		status = mbx->mbxStatus;
9725 	}
9726 
9727 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9728 	return status;
9729 
9730 out_not_finished:
9731 	if (processing_queue) {
9732 		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
9733 		lpfc_mbox_cmpl_put(phba, pmbox);
9734 	}
9735 	return MBX_NOT_FINISHED;
9736 }
9737 
9738 /**
9739  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9740  * @phba: Pointer to HBA context object.
9741  *
9742  * The function blocks the posting of SLI4 asynchronous mailbox commands from
9743  * the driver internal pending mailbox queue. It will then try to wait out the
9744  * possible outstanding mailbox command before return.
9745  *
9746  * Returns:
9747  * 	0 - the outstanding mailbox command completed; otherwise, the wait for
9748  * 	the outstanding mailbox command timed out.
9749  **/
9750 static int
lpfc_sli4_async_mbox_block(struct lpfc_hba * phba)9751 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9752 {
9753 	struct lpfc_sli *psli = &phba->sli;
9754 	LPFC_MBOXQ_t *mboxq;
9755 	int rc = 0;
9756 	unsigned long timeout = 0;
9757 	u32 sli_flag;
9758 	u8 cmd, subsys, opcode;
9759 
9760 	/* Mark the asynchronous mailbox command posting as blocked */
9761 	spin_lock_irq(&phba->hbalock);
9762 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9763 	/* Determine how long we might wait for the active mailbox
9764 	 * command to be gracefully completed by firmware.
9765 	 */
9766 	if (phba->sli.mbox_active)
9767 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9768 						phba->sli.mbox_active) *
9769 						1000) + jiffies;
9770 	spin_unlock_irq(&phba->hbalock);
9771 
9772 	/* Make sure the mailbox is really active */
9773 	if (timeout)
9774 		lpfc_sli4_process_missed_mbox_completions(phba);
9775 
9776 	/* Wait for the outstanding mailbox command to complete */
9777 	while (phba->sli.mbox_active) {
9778 		/* Check active mailbox complete status every 2ms */
9779 		msleep(2);
9780 		if (time_after(jiffies, timeout)) {
9781 			/* Timeout, mark the outstanding cmd not complete */
9782 
9783 			/* Sanity check sli.mbox_active has not completed or
9784 			 * cancelled from another context during last 2ms sleep,
9785 			 * so take hbalock to be sure before logging.
9786 			 */
9787 			spin_lock_irq(&phba->hbalock);
9788 			if (phba->sli.mbox_active) {
9789 				mboxq = phba->sli.mbox_active;
9790 				cmd = mboxq->u.mb.mbxCommand;
9791 				subsys = lpfc_sli_config_mbox_subsys_get(phba,
9792 									 mboxq);
9793 				opcode = lpfc_sli_config_mbox_opcode_get(phba,
9794 									 mboxq);
9795 				sli_flag = psli->sli_flag;
9796 				spin_unlock_irq(&phba->hbalock);
9797 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9798 						"2352 Mailbox command x%x "
9799 						"(x%x/x%x) sli_flag x%x could "
9800 						"not complete\n",
9801 						cmd, subsys, opcode,
9802 						sli_flag);
9803 			} else {
9804 				spin_unlock_irq(&phba->hbalock);
9805 			}
9806 
9807 			rc = 1;
9808 			break;
9809 		}
9810 	}
9811 
9812 	/* Can not cleanly block async mailbox command, fails it */
9813 	if (rc) {
9814 		spin_lock_irq(&phba->hbalock);
9815 		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9816 		spin_unlock_irq(&phba->hbalock);
9817 	}
9818 	return rc;
9819 }
9820 
9821 /**
9822  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9823  * @phba: Pointer to HBA context object.
9824  *
9825  * The function unblocks and resume posting of SLI4 asynchronous mailbox
9826  * commands from the driver internal pending mailbox queue. It makes sure
9827  * that there is no outstanding mailbox command before resuming posting
9828  * asynchronous mailbox commands. If, for any reason, there is outstanding
9829  * mailbox command, it will try to wait it out before resuming asynchronous
9830  * mailbox command posting.
9831  **/
9832 static void
lpfc_sli4_async_mbox_unblock(struct lpfc_hba * phba)9833 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9834 {
9835 	struct lpfc_sli *psli = &phba->sli;
9836 
9837 	spin_lock_irq(&phba->hbalock);
9838 	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9839 		/* Asynchronous mailbox posting is not blocked, do nothing */
9840 		spin_unlock_irq(&phba->hbalock);
9841 		return;
9842 	}
9843 
9844 	/* Outstanding synchronous mailbox command is guaranteed to be done,
9845 	 * successful or timeout, after timing-out the outstanding mailbox
9846 	 * command shall always be removed, so just unblock posting async
9847 	 * mailbox command and resume
9848 	 */
9849 	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9850 	spin_unlock_irq(&phba->hbalock);
9851 
9852 	/* wake up worker thread to post asynchronous mailbox command */
9853 	lpfc_worker_wake_up(phba);
9854 }
9855 
9856 /**
9857  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9858  * @phba: Pointer to HBA context object.
9859  * @mboxq: Pointer to mailbox object.
9860  *
9861  * The function waits for the bootstrap mailbox register ready bit from
9862  * port for twice the regular mailbox command timeout value.
9863  *
9864  *      0 - no timeout on waiting for bootstrap mailbox register ready.
9865  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out or port
9866  *                     is in an unrecoverable state.
9867  **/
9868 static int
lpfc_sli4_wait_bmbx_ready(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9869 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9870 {
9871 	uint32_t db_ready;
9872 	unsigned long timeout;
9873 	struct lpfc_register bmbx_reg;
9874 	struct lpfc_register portstat_reg = {-1};
9875 
9876 	/* Sanity check - there is no point to wait if the port is in an
9877 	 * unrecoverable state.
9878 	 */
9879 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
9880 	    LPFC_SLI_INTF_IF_TYPE_2) {
9881 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9882 			       &portstat_reg.word0) ||
9883 		    lpfc_sli4_unrecoverable_port(&portstat_reg)) {
9884 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9885 					"3858 Skipping bmbx ready because "
9886 					"Port Status x%x\n",
9887 					portstat_reg.word0);
9888 			return MBXERR_ERROR;
9889 		}
9890 	}
9891 
9892 	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
9893 				   * 1000) + jiffies;
9894 
9895 	do {
9896 		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9897 		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9898 		if (!db_ready)
9899 			mdelay(2);
9900 
9901 		if (time_after(jiffies, timeout))
9902 			return MBXERR_ERROR;
9903 	} while (!db_ready);
9904 
9905 	return 0;
9906 }
9907 
9908 /**
9909  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9910  * @phba: Pointer to HBA context object.
9911  * @mboxq: Pointer to mailbox object.
9912  *
9913  * The function posts a mailbox to the port.  The mailbox is expected
9914  * to be comletely filled in and ready for the port to operate on it.
9915  * This routine executes a synchronous completion operation on the
9916  * mailbox by polling for its completion.
9917  *
9918  * The caller must not be holding any locks when calling this routine.
9919  *
9920  * Returns:
9921  *	MBX_SUCCESS - mailbox posted successfully
9922  *	Any of the MBX error values.
9923  **/
9924 static int
lpfc_sli4_post_sync_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9925 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9926 {
9927 	int rc = MBX_SUCCESS;
9928 	unsigned long iflag;
9929 	uint32_t mcqe_status;
9930 	uint32_t mbx_cmnd;
9931 	struct lpfc_sli *psli = &phba->sli;
9932 	struct lpfc_mqe *mb = &mboxq->u.mqe;
9933 	struct lpfc_bmbx_create *mbox_rgn;
9934 	struct dma_address *dma_address;
9935 
9936 	/*
9937 	 * Only one mailbox can be active to the bootstrap mailbox region
9938 	 * at a time and there is no queueing provided.
9939 	 */
9940 	spin_lock_irqsave(&phba->hbalock, iflag);
9941 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9942 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9943 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9944 				"(%d):2532 Mailbox command x%x (x%x/x%x) "
9945 				"cannot issue Data: x%x x%x\n",
9946 				mboxq->vport ? mboxq->vport->vpi : 0,
9947 				mboxq->u.mb.mbxCommand,
9948 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9949 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9950 				psli->sli_flag, MBX_POLL);
9951 		return MBXERR_ERROR;
9952 	}
9953 	/* The server grabs the token and owns it until release */
9954 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9955 	phba->sli.mbox_active = mboxq;
9956 	spin_unlock_irqrestore(&phba->hbalock, iflag);
9957 
9958 	/* wait for bootstrap mbox register for readyness */
9959 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9960 	if (rc)
9961 		goto exit;
9962 	/*
9963 	 * Initialize the bootstrap memory region to avoid stale data areas
9964 	 * in the mailbox post.  Then copy the caller's mailbox contents to
9965 	 * the bmbx mailbox region.
9966 	 */
9967 	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
9968 	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
9969 	lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
9970 			       sizeof(struct lpfc_mqe));
9971 
9972 	/* Post the high mailbox dma address to the port and wait for ready. */
9973 	dma_address = &phba->sli4_hba.bmbx.dma_address;
9974 	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
9975 
9976 	/* wait for bootstrap mbox register for hi-address write done */
9977 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9978 	if (rc)
9979 		goto exit;
9980 
9981 	/* Post the low mailbox dma address to the port. */
9982 	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
9983 
9984 	/* wait for bootstrap mbox register for low address write done */
9985 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9986 	if (rc)
9987 		goto exit;
9988 
9989 	/*
9990 	 * Read the CQ to ensure the mailbox has completed.
9991 	 * If so, update the mailbox status so that the upper layers
9992 	 * can complete the request normally.
9993 	 */
9994 	lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
9995 			       sizeof(struct lpfc_mqe));
9996 	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
9997 	lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
9998 			       sizeof(struct lpfc_mcqe));
9999 	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
10000 	/*
10001 	 * When the CQE status indicates a failure and the mailbox status
10002 	 * indicates success then copy the CQE status into the mailbox status
10003 	 * (and prefix it with x4000).
10004 	 */
10005 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10006 		if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
10007 			bf_set(lpfc_mqe_status, mb,
10008 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
10009 		rc = MBXERR_ERROR;
10010 	} else
10011 		lpfc_sli4_swap_str(phba, mboxq);
10012 
10013 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10014 			"(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
10015 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
10016 			" x%x x%x CQ: x%x x%x x%x x%x\n",
10017 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10018 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10019 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10020 			bf_get(lpfc_mqe_status, mb),
10021 			mb->un.mb_words[0], mb->un.mb_words[1],
10022 			mb->un.mb_words[2], mb->un.mb_words[3],
10023 			mb->un.mb_words[4], mb->un.mb_words[5],
10024 			mb->un.mb_words[6], mb->un.mb_words[7],
10025 			mb->un.mb_words[8], mb->un.mb_words[9],
10026 			mb->un.mb_words[10], mb->un.mb_words[11],
10027 			mb->un.mb_words[12], mboxq->mcqe.word0,
10028 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
10029 			mboxq->mcqe.trailer);
10030 exit:
10031 	/* We are holding the token, no needed for lock when release */
10032 	spin_lock_irqsave(&phba->hbalock, iflag);
10033 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10034 	phba->sli.mbox_active = NULL;
10035 	spin_unlock_irqrestore(&phba->hbalock, iflag);
10036 	return rc;
10037 }
10038 
10039 /**
10040  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
10041  * @phba: Pointer to HBA context object.
10042  * @mboxq: Pointer to mailbox object.
10043  * @flag: Flag indicating how the mailbox need to be processed.
10044  *
10045  * This function is called by discovery code and HBA management code to submit
10046  * a mailbox command to firmware with SLI-4 interface spec.
10047  *
10048  * Return codes the caller owns the mailbox command after the return of the
10049  * function.
10050  **/
10051 static int
lpfc_sli_issue_mbox_s4(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint32_t flag)10052 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
10053 		       uint32_t flag)
10054 {
10055 	struct lpfc_sli *psli = &phba->sli;
10056 	unsigned long iflags;
10057 	int rc;
10058 
10059 	/* dump from issue mailbox command if setup */
10060 	lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
10061 
10062 	rc = lpfc_mbox_dev_check(phba);
10063 	if (unlikely(rc)) {
10064 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10065 				"(%d):2544 Mailbox command x%x (x%x/x%x) "
10066 				"cannot issue Data: x%x x%x\n",
10067 				mboxq->vport ? mboxq->vport->vpi : 0,
10068 				mboxq->u.mb.mbxCommand,
10069 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10070 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10071 				psli->sli_flag, flag);
10072 		goto out_not_finished;
10073 	}
10074 
10075 	/* Detect polling mode and jump to a handler */
10076 	if (!phba->sli4_hba.intr_enable) {
10077 		if (flag == MBX_POLL)
10078 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10079 		else
10080 			rc = -EIO;
10081 		if (rc != MBX_SUCCESS)
10082 			lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10083 					"(%d):2541 Mailbox command x%x "
10084 					"(x%x/x%x) failure: "
10085 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10086 					"Data: x%x x%x\n",
10087 					mboxq->vport ? mboxq->vport->vpi : 0,
10088 					mboxq->u.mb.mbxCommand,
10089 					lpfc_sli_config_mbox_subsys_get(phba,
10090 									mboxq),
10091 					lpfc_sli_config_mbox_opcode_get(phba,
10092 									mboxq),
10093 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10094 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10095 					bf_get(lpfc_mcqe_ext_status,
10096 					       &mboxq->mcqe),
10097 					psli->sli_flag, flag);
10098 		return rc;
10099 	} else if (flag == MBX_POLL) {
10100 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10101 				"(%d):2542 Try to issue mailbox command "
10102 				"x%x (x%x/x%x) synchronously ahead of async "
10103 				"mailbox command queue: x%x x%x\n",
10104 				mboxq->vport ? mboxq->vport->vpi : 0,
10105 				mboxq->u.mb.mbxCommand,
10106 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10107 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10108 				psli->sli_flag, flag);
10109 		/* Try to block the asynchronous mailbox posting */
10110 		rc = lpfc_sli4_async_mbox_block(phba);
10111 		if (!rc) {
10112 			/* Successfully blocked, now issue sync mbox cmd */
10113 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10114 			if (rc != MBX_SUCCESS)
10115 				lpfc_printf_log(phba, KERN_WARNING,
10116 					LOG_MBOX | LOG_SLI,
10117 					"(%d):2597 Sync Mailbox command "
10118 					"x%x (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 			/* Unblock the async mailbox posting afterward */
10133 			lpfc_sli4_async_mbox_unblock(phba);
10134 		}
10135 		return rc;
10136 	}
10137 
10138 	/* Now, interrupt mode asynchronous mailbox command */
10139 	rc = lpfc_mbox_cmd_check(phba, mboxq);
10140 	if (rc) {
10141 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10142 				"(%d):2543 Mailbox command x%x (x%x/x%x) "
10143 				"cannot issue Data: x%x x%x\n",
10144 				mboxq->vport ? mboxq->vport->vpi : 0,
10145 				mboxq->u.mb.mbxCommand,
10146 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10147 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10148 				psli->sli_flag, flag);
10149 		goto out_not_finished;
10150 	}
10151 
10152 	/* Put the mailbox command to the driver internal FIFO */
10153 	psli->slistat.mbox_busy++;
10154 	spin_lock_irqsave(&phba->hbalock, iflags);
10155 	lpfc_mbox_put(phba, mboxq);
10156 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10157 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10158 			"(%d):0354 Mbox cmd issue - Enqueue Data: "
10159 			"x%x (x%x/x%x) x%x x%x x%x\n",
10160 			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
10161 			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
10162 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10163 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10164 			phba->pport->port_state,
10165 			psli->sli_flag, MBX_NOWAIT);
10166 	/* Wake up worker thread to transport mailbox command from head */
10167 	lpfc_worker_wake_up(phba);
10168 
10169 	return MBX_BUSY;
10170 
10171 out_not_finished:
10172 	return MBX_NOT_FINISHED;
10173 }
10174 
10175 /**
10176  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
10177  * @phba: Pointer to HBA context object.
10178  *
10179  * This function is called by worker thread to send a mailbox command to
10180  * SLI4 HBA firmware.
10181  *
10182  **/
10183 int
lpfc_sli4_post_async_mbox(struct lpfc_hba * phba)10184 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
10185 {
10186 	struct lpfc_sli *psli = &phba->sli;
10187 	LPFC_MBOXQ_t *mboxq;
10188 	int rc = MBX_SUCCESS;
10189 	unsigned long iflags;
10190 	struct lpfc_mqe *mqe;
10191 	uint32_t mbx_cmnd;
10192 
10193 	/* Check interrupt mode before post async mailbox command */
10194 	if (unlikely(!phba->sli4_hba.intr_enable))
10195 		return MBX_NOT_FINISHED;
10196 
10197 	/* Check for mailbox command service token */
10198 	spin_lock_irqsave(&phba->hbalock, iflags);
10199 	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
10200 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10201 		return MBX_NOT_FINISHED;
10202 	}
10203 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10204 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10205 		return MBX_NOT_FINISHED;
10206 	}
10207 	if (unlikely(phba->sli.mbox_active)) {
10208 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10209 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10210 				"0384 There is pending active mailbox cmd\n");
10211 		return MBX_NOT_FINISHED;
10212 	}
10213 	/* Take the mailbox command service token */
10214 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
10215 
10216 	/* Get the next mailbox command from head of queue */
10217 	mboxq = lpfc_mbox_get(phba);
10218 
10219 	/* If no more mailbox command waiting for post, we're done */
10220 	if (!mboxq) {
10221 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10222 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10223 		return MBX_SUCCESS;
10224 	}
10225 	phba->sli.mbox_active = mboxq;
10226 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10227 
10228 	/* Check device readiness for posting mailbox command */
10229 	rc = lpfc_mbox_dev_check(phba);
10230 	if (unlikely(rc))
10231 		/* Driver clean routine will clean up pending mailbox */
10232 		goto out_not_finished;
10233 
10234 	/* Prepare the mbox command to be posted */
10235 	mqe = &mboxq->u.mqe;
10236 	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
10237 
10238 	/* Start timer for the mbox_tmo and log some mailbox post messages */
10239 	mod_timer(&psli->mbox_tmo, (jiffies +
10240 		  msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
10241 
10242 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10243 			"(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
10244 			"x%x x%x\n",
10245 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10246 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10247 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10248 			phba->pport->port_state, psli->sli_flag);
10249 
10250 	if (mbx_cmnd != MBX_HEARTBEAT) {
10251 		if (mboxq->vport) {
10252 			lpfc_debugfs_disc_trc(mboxq->vport,
10253 				LPFC_DISC_TRC_MBOX_VPORT,
10254 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
10255 				mbx_cmnd, mqe->un.mb_words[0],
10256 				mqe->un.mb_words[1]);
10257 		} else {
10258 			lpfc_debugfs_disc_trc(phba->pport,
10259 				LPFC_DISC_TRC_MBOX,
10260 				"MBOX Send: cmd:x%x mb:x%x x%x",
10261 				mbx_cmnd, mqe->un.mb_words[0],
10262 				mqe->un.mb_words[1]);
10263 		}
10264 	}
10265 	psli->slistat.mbox_cmd++;
10266 
10267 	/* Post the mailbox command to the port */
10268 	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
10269 	if (rc != MBX_SUCCESS) {
10270 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10271 				"(%d):2533 Mailbox command x%x (x%x/x%x) "
10272 				"cannot issue Data: x%x x%x\n",
10273 				mboxq->vport ? mboxq->vport->vpi : 0,
10274 				mboxq->u.mb.mbxCommand,
10275 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10276 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10277 				psli->sli_flag, MBX_NOWAIT);
10278 		goto out_not_finished;
10279 	}
10280 
10281 	return rc;
10282 
10283 out_not_finished:
10284 	spin_lock_irqsave(&phba->hbalock, iflags);
10285 	if (phba->sli.mbox_active) {
10286 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10287 		__lpfc_mbox_cmpl_put(phba, mboxq);
10288 		/* Release the token */
10289 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10290 		phba->sli.mbox_active = NULL;
10291 	}
10292 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10293 
10294 	return MBX_NOT_FINISHED;
10295 }
10296 
10297 /**
10298  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
10299  * @phba: Pointer to HBA context object.
10300  * @pmbox: Pointer to mailbox object.
10301  * @flag: Flag indicating how the mailbox need to be processed.
10302  *
10303  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
10304  * the API jump table function pointer from the lpfc_hba struct.
10305  *
10306  * Return codes the caller owns the mailbox command after the return of the
10307  * function.
10308  **/
10309 int
lpfc_sli_issue_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)10310 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
10311 {
10312 	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
10313 }
10314 
10315 /**
10316  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
10317  * @phba: The hba struct for which this call is being executed.
10318  * @dev_grp: The HBA PCI-Device group number.
10319  *
10320  * This routine sets up the mbox interface API function jump table in @phba
10321  * struct.
10322  * Returns: 0 - success, -ENODEV - failure.
10323  **/
10324 int
lpfc_mbox_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)10325 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10326 {
10327 
10328 	switch (dev_grp) {
10329 	case LPFC_PCI_DEV_LP:
10330 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
10331 		phba->lpfc_sli_handle_slow_ring_event =
10332 				lpfc_sli_handle_slow_ring_event_s3;
10333 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
10334 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
10335 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
10336 		break;
10337 	case LPFC_PCI_DEV_OC:
10338 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
10339 		phba->lpfc_sli_handle_slow_ring_event =
10340 				lpfc_sli_handle_slow_ring_event_s4;
10341 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10342 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10343 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10344 		break;
10345 	default:
10346 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10347 				"1420 Invalid HBA PCI-device group: 0x%x\n",
10348 				dev_grp);
10349 		return -ENODEV;
10350 	}
10351 	return 0;
10352 }
10353 
10354 /**
10355  * __lpfc_sli_ringtx_put - Add an iocb to the txq
10356  * @phba: Pointer to HBA context object.
10357  * @pring: Pointer to driver SLI ring object.
10358  * @piocb: Pointer to address of newly added command iocb.
10359  *
10360  * This function is called with hbalock held for SLI3 ports or
10361  * the ring lock held for SLI4 ports to add a command
10362  * iocb to the txq when SLI layer cannot submit the command iocb
10363  * to the ring.
10364  **/
10365 void
__lpfc_sli_ringtx_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)10366 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10367 		    struct lpfc_iocbq *piocb)
10368 {
10369 	if (phba->sli_rev == LPFC_SLI_REV4)
10370 		lockdep_assert_held(&pring->ring_lock);
10371 	else
10372 		lockdep_assert_held(&phba->hbalock);
10373 	/* Insert the caller's iocb in the txq tail for later processing. */
10374 	list_add_tail(&piocb->list, &pring->txq);
10375 }
10376 
10377 /**
10378  * lpfc_sli_next_iocb - Get the next iocb in the txq
10379  * @phba: Pointer to HBA context object.
10380  * @pring: Pointer to driver SLI ring object.
10381  * @piocb: Pointer to address of newly added command iocb.
10382  *
10383  * This function is called with hbalock held before a new
10384  * iocb is submitted to the firmware. This function checks
10385  * txq to flush the iocbs in txq to Firmware before
10386  * submitting new iocbs to the Firmware.
10387  * If there are iocbs in the txq which need to be submitted
10388  * to firmware, lpfc_sli_next_iocb returns the first element
10389  * of the txq after dequeuing it from txq.
10390  * If there is no iocb in the txq then the function will return
10391  * *piocb and *piocb is set to NULL. Caller needs to check
10392  * *piocb to find if there are more commands in the txq.
10393  **/
10394 static struct lpfc_iocbq *
lpfc_sli_next_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq ** piocb)10395 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10396 		   struct lpfc_iocbq **piocb)
10397 {
10398 	struct lpfc_iocbq * nextiocb;
10399 
10400 	lockdep_assert_held(&phba->hbalock);
10401 
10402 	nextiocb = lpfc_sli_ringtx_get(phba, pring);
10403 	if (!nextiocb) {
10404 		nextiocb = *piocb;
10405 		*piocb = NULL;
10406 	}
10407 
10408 	return nextiocb;
10409 }
10410 
10411 /**
10412  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
10413  * @phba: Pointer to HBA context object.
10414  * @ring_number: SLI ring number to issue iocb on.
10415  * @piocb: Pointer to command iocb.
10416  * @flag: Flag indicating if this command can be put into txq.
10417  *
10418  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10419  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10420  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10421  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10422  * this function allows only iocbs for posting buffers. This function finds
10423  * next available slot in the command ring and posts the command to the
10424  * available slot and writes the port attention register to request HBA start
10425  * processing new iocb. If there is no slot available in the ring and
10426  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10427  * the function returns IOCB_BUSY.
10428  *
10429  * This function is called with hbalock held. The function will return success
10430  * after it successfully submit the iocb to firmware or after adding to the
10431  * txq.
10432  **/
10433 static int
__lpfc_sli_issue_iocb_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10434 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
10435 		    struct lpfc_iocbq *piocb, uint32_t flag)
10436 {
10437 	struct lpfc_iocbq *nextiocb;
10438 	IOCB_t *iocb;
10439 	struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
10440 
10441 	lockdep_assert_held(&phba->hbalock);
10442 
10443 	if (piocb->cmd_cmpl && (!piocb->vport) &&
10444 	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10445 	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
10446 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10447 				"1807 IOCB x%x failed. No vport\n",
10448 				piocb->iocb.ulpCommand);
10449 		dump_stack();
10450 		return IOCB_ERROR;
10451 	}
10452 
10453 
10454 	/* If the PCI channel is in offline state, do not post iocbs. */
10455 	if (unlikely(pci_channel_offline(phba->pcidev)))
10456 		return IOCB_ERROR;
10457 
10458 	/* If HBA has a deferred error attention, fail the iocb. */
10459 	if (unlikely(phba->hba_flag & DEFER_ERATT))
10460 		return IOCB_ERROR;
10461 
10462 	/*
10463 	 * We should never get an IOCB if we are in a < LINK_DOWN state
10464 	 */
10465 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10466 		return IOCB_ERROR;
10467 
10468 	/*
10469 	 * Check to see if we are blocking IOCB processing because of a
10470 	 * outstanding event.
10471 	 */
10472 	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
10473 		goto iocb_busy;
10474 
10475 	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
10476 		/*
10477 		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
10478 		 * can be issued if the link is not up.
10479 		 */
10480 		switch (piocb->iocb.ulpCommand) {
10481 		case CMD_QUE_RING_BUF_CN:
10482 		case CMD_QUE_RING_BUF64_CN:
10483 			/*
10484 			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10485 			 * completion, cmd_cmpl MUST be 0.
10486 			 */
10487 			if (piocb->cmd_cmpl)
10488 				piocb->cmd_cmpl = NULL;
10489 			fallthrough;
10490 		case CMD_CREATE_XRI_CR:
10491 		case CMD_CLOSE_XRI_CN:
10492 		case CMD_CLOSE_XRI_CX:
10493 			break;
10494 		default:
10495 			goto iocb_busy;
10496 		}
10497 
10498 	/*
10499 	 * For FCP commands, we must be in a state where we can process link
10500 	 * attention events.
10501 	 */
10502 	} else if (unlikely(pring->ringno == LPFC_FCP_RING &&
10503 			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
10504 		goto iocb_busy;
10505 	}
10506 
10507 	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10508 	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10509 		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10510 
10511 	if (iocb)
10512 		lpfc_sli_update_ring(phba, pring);
10513 	else
10514 		lpfc_sli_update_full_ring(phba, pring);
10515 
10516 	if (!piocb)
10517 		return IOCB_SUCCESS;
10518 
10519 	goto out_busy;
10520 
10521  iocb_busy:
10522 	pring->stats.iocb_cmd_delay++;
10523 
10524  out_busy:
10525 
10526 	if (!(flag & SLI_IOCB_RET_IOCB)) {
10527 		__lpfc_sli_ringtx_put(phba, pring, piocb);
10528 		return IOCB_SUCCESS;
10529 	}
10530 
10531 	return IOCB_BUSY;
10532 }
10533 
10534 /**
10535  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10536  * @phba: Pointer to HBA context object.
10537  * @ring_number: SLI ring number to issue wqe on.
10538  * @piocb: Pointer to command iocb.
10539  * @flag: Flag indicating if this command can be put into txq.
10540  *
10541  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10542  * send  an iocb command to an HBA with SLI-3 interface spec.
10543  *
10544  * This function takes the hbalock before invoking the lockless version.
10545  * The function will return success after it successfully submit the wqe to
10546  * firmware or after adding to the txq.
10547  **/
10548 static int
__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10549 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10550 			   struct lpfc_iocbq *piocb, uint32_t flag)
10551 {
10552 	unsigned long iflags;
10553 	int rc;
10554 
10555 	spin_lock_irqsave(&phba->hbalock, iflags);
10556 	rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10557 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10558 
10559 	return rc;
10560 }
10561 
10562 /**
10563  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10564  * @phba: Pointer to HBA context object.
10565  * @ring_number: SLI ring number to issue wqe on.
10566  * @piocb: Pointer to command iocb.
10567  * @flag: Flag indicating if this command can be put into txq.
10568  *
10569  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10570  * an wqe command to an HBA with SLI-4 interface spec.
10571  *
10572  * This function is a lockless version. The function will return success
10573  * after it successfully submit the wqe to firmware or after adding to the
10574  * txq.
10575  **/
10576 static int
__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10577 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10578 			   struct lpfc_iocbq *piocb, uint32_t flag)
10579 {
10580 	struct lpfc_io_buf *lpfc_cmd = piocb->io_buf;
10581 
10582 	lpfc_prep_embed_io(phba, lpfc_cmd);
10583 	return lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10584 }
10585 
10586 void
lpfc_prep_embed_io(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_cmd)10587 lpfc_prep_embed_io(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
10588 {
10589 	struct lpfc_iocbq *piocb = &lpfc_cmd->cur_iocbq;
10590 	union lpfc_wqe128 *wqe = &lpfc_cmd->cur_iocbq.wqe;
10591 	struct sli4_sge *sgl;
10592 
10593 	/* 128 byte wqe support here */
10594 	sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
10595 
10596 	if (phba->fcp_embed_io) {
10597 		struct fcp_cmnd *fcp_cmnd;
10598 		u32 *ptr;
10599 
10600 		fcp_cmnd = lpfc_cmd->fcp_cmnd;
10601 
10602 		/* Word 0-2 - FCP_CMND */
10603 		wqe->generic.bde.tus.f.bdeFlags =
10604 			BUFF_TYPE_BDE_IMMED;
10605 		wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10606 		wqe->generic.bde.addrHigh = 0;
10607 		wqe->generic.bde.addrLow =  88;  /* Word 22 */
10608 
10609 		bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10610 		bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10611 
10612 		/* Word 22-29  FCP CMND Payload */
10613 		ptr = &wqe->words[22];
10614 		memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10615 	} else {
10616 		/* Word 0-2 - Inline BDE */
10617 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10618 		wqe->generic.bde.tus.f.bdeSize = sizeof(struct fcp_cmnd);
10619 		wqe->generic.bde.addrHigh = sgl->addr_hi;
10620 		wqe->generic.bde.addrLow =  sgl->addr_lo;
10621 
10622 		/* Word 10 */
10623 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10624 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10625 	}
10626 
10627 	/* add the VMID tags as per switch response */
10628 	if (unlikely(piocb->cmd_flag & LPFC_IO_VMID)) {
10629 		if (phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
10630 			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10631 			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10632 					(piocb->vmid_tag.cs_ctl_vmid));
10633 		} else if (phba->cfg_vmid_app_header) {
10634 			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
10635 			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10636 			wqe->words[31] = piocb->vmid_tag.app_id;
10637 		}
10638 	}
10639 }
10640 
10641 /**
10642  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10643  * @phba: Pointer to HBA context object.
10644  * @ring_number: SLI ring number to issue iocb on.
10645  * @piocb: Pointer to command iocb.
10646  * @flag: Flag indicating if this command can be put into txq.
10647  *
10648  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10649  * an iocb command to an HBA with SLI-4 interface spec.
10650  *
10651  * This function is called with ringlock held. The function will return success
10652  * after it successfully submit the iocb to firmware or after adding to the
10653  * txq.
10654  **/
10655 static int
__lpfc_sli_issue_iocb_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10656 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10657 			 struct lpfc_iocbq *piocb, uint32_t flag)
10658 {
10659 	struct lpfc_sglq *sglq;
10660 	union lpfc_wqe128 *wqe;
10661 	struct lpfc_queue *wq;
10662 	struct lpfc_sli_ring *pring;
10663 	u32 ulp_command = get_job_cmnd(phba, piocb);
10664 
10665 	/* Get the WQ */
10666 	if ((piocb->cmd_flag & LPFC_IO_FCP) ||
10667 	    (piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
10668 		wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10669 	} else {
10670 		wq = phba->sli4_hba.els_wq;
10671 	}
10672 
10673 	/* Get corresponding ring */
10674 	pring = wq->pring;
10675 
10676 	/*
10677 	 * The WQE can be either 64 or 128 bytes,
10678 	 */
10679 
10680 	lockdep_assert_held(&pring->ring_lock);
10681 	wqe = &piocb->wqe;
10682 	if (piocb->sli4_xritag == NO_XRI) {
10683 		if (ulp_command == CMD_ABORT_XRI_CX)
10684 			sglq = NULL;
10685 		else {
10686 			sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10687 			if (!sglq) {
10688 				if (!(flag & SLI_IOCB_RET_IOCB)) {
10689 					__lpfc_sli_ringtx_put(phba,
10690 							pring,
10691 							piocb);
10692 					return IOCB_SUCCESS;
10693 				} else {
10694 					return IOCB_BUSY;
10695 				}
10696 			}
10697 		}
10698 	} else if (piocb->cmd_flag &  LPFC_IO_FCP) {
10699 		/* These IO's already have an XRI and a mapped sgl. */
10700 		sglq = NULL;
10701 	}
10702 	else {
10703 		/*
10704 		 * This is a continuation of a commandi,(CX) so this
10705 		 * sglq is on the active list
10706 		 */
10707 		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10708 		if (!sglq)
10709 			return IOCB_ERROR;
10710 	}
10711 
10712 	if (sglq) {
10713 		piocb->sli4_lxritag = sglq->sli4_lxritag;
10714 		piocb->sli4_xritag = sglq->sli4_xritag;
10715 
10716 		/* ABTS sent by initiator to CT exchange, the
10717 		 * RX_ID field will be filled with the newly
10718 		 * allocated responder XRI.
10719 		 */
10720 		if (ulp_command == CMD_XMIT_BLS_RSP64_CX &&
10721 		    piocb->abort_bls == LPFC_ABTS_UNSOL_INT)
10722 			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10723 			       piocb->sli4_xritag);
10724 
10725 		bf_set(wqe_xri_tag, &wqe->generic.wqe_com,
10726 		       piocb->sli4_xritag);
10727 
10728 		if (lpfc_wqe_bpl2sgl(phba, piocb, sglq) == NO_XRI)
10729 			return IOCB_ERROR;
10730 	}
10731 
10732 	if (lpfc_sli4_wq_put(wq, wqe))
10733 		return IOCB_ERROR;
10734 
10735 	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10736 
10737 	return 0;
10738 }
10739 
10740 /*
10741  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10742  *
10743  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10744  * or IOCB for sli-3  function.
10745  * pointer from the lpfc_hba struct.
10746  *
10747  * Return codes:
10748  * IOCB_ERROR - Error
10749  * IOCB_SUCCESS - Success
10750  * IOCB_BUSY - Busy
10751  **/
10752 int
lpfc_sli_issue_fcp_io(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10753 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10754 		      struct lpfc_iocbq *piocb, uint32_t flag)
10755 {
10756 	return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10757 }
10758 
10759 /*
10760  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10761  *
10762  * This routine wraps the actual lockless version for issusing IOCB function
10763  * pointer from the lpfc_hba struct.
10764  *
10765  * Return codes:
10766  * IOCB_ERROR - Error
10767  * IOCB_SUCCESS - Success
10768  * IOCB_BUSY - Busy
10769  **/
10770 int
__lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10771 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10772 		struct lpfc_iocbq *piocb, uint32_t flag)
10773 {
10774 	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10775 }
10776 
10777 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)10778 __lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq *cmdiocbq,
10779 			       struct lpfc_vport *vport,
10780 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10781 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10782 {
10783 	struct lpfc_hba *phba = vport->phba;
10784 	IOCB_t *cmd;
10785 
10786 	cmd = &cmdiocbq->iocb;
10787 	memset(cmd, 0, sizeof(*cmd));
10788 
10789 	cmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10790 	cmd->un.elsreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10791 	cmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10792 
10793 	if (expect_rsp) {
10794 		cmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
10795 		cmd->un.elsreq64.remoteID = did; /* DID */
10796 		cmd->ulpCommand = CMD_ELS_REQUEST64_CR;
10797 		cmd->ulpTimeout = tmo;
10798 	} else {
10799 		cmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
10800 		cmd->un.genreq64.xmit_els_remoteID = did; /* DID */
10801 		cmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
10802 		cmd->ulpPU = PARM_NPIV_DID;
10803 	}
10804 	cmd->ulpBdeCount = 1;
10805 	cmd->ulpLe = 1;
10806 	cmd->ulpClass = CLASS3;
10807 
10808 	/* If we have NPIV enabled, we want to send ELS traffic by VPI. */
10809 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
10810 		if (expect_rsp) {
10811 			cmd->un.elsreq64.myID = vport->fc_myDID;
10812 
10813 			/* For ELS_REQUEST64_CR, use the VPI by default */
10814 			cmd->ulpContext = phba->vpi_ids[vport->vpi];
10815 		}
10816 
10817 		cmd->ulpCt_h = 0;
10818 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10819 		if (elscmd == ELS_CMD_ECHO)
10820 			cmd->ulpCt_l = 0; /* context = invalid RPI */
10821 		else
10822 			cmd->ulpCt_l = 1; /* context = VPI */
10823 	}
10824 }
10825 
10826 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)10827 __lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq *cmdiocbq,
10828 			       struct lpfc_vport *vport,
10829 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10830 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10831 {
10832 	struct lpfc_hba  *phba = vport->phba;
10833 	union lpfc_wqe128 *wqe;
10834 	struct ulp_bde64_le *bde;
10835 	u8 els_id;
10836 
10837 	wqe = &cmdiocbq->wqe;
10838 	memset(wqe, 0, sizeof(*wqe));
10839 
10840 	/* Word 0 - 2 BDE */
10841 	bde = (struct ulp_bde64_le *)&wqe->generic.bde;
10842 	bde->addr_low = cpu_to_le32(putPaddrLow(bmp->phys));
10843 	bde->addr_high = cpu_to_le32(putPaddrHigh(bmp->phys));
10844 	bde->type_size = cpu_to_le32(cmd_size);
10845 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10846 
10847 	if (expect_rsp) {
10848 		bf_set(wqe_cmnd, &wqe->els_req.wqe_com, CMD_ELS_REQUEST64_WQE);
10849 
10850 		/* Transfer length */
10851 		wqe->els_req.payload_len = cmd_size;
10852 		wqe->els_req.max_response_payload_len = FCELSSIZE;
10853 
10854 		/* DID */
10855 		bf_set(wqe_els_did, &wqe->els_req.wqe_dest, did);
10856 
10857 		/* Word 11 - ELS_ID */
10858 		switch (elscmd) {
10859 		case ELS_CMD_PLOGI:
10860 			els_id = LPFC_ELS_ID_PLOGI;
10861 			break;
10862 		case ELS_CMD_FLOGI:
10863 			els_id = LPFC_ELS_ID_FLOGI;
10864 			break;
10865 		case ELS_CMD_LOGO:
10866 			els_id = LPFC_ELS_ID_LOGO;
10867 			break;
10868 		case ELS_CMD_FDISC:
10869 			if (!vport->fc_myDID) {
10870 				els_id = LPFC_ELS_ID_FDISC;
10871 				break;
10872 			}
10873 			fallthrough;
10874 		default:
10875 			els_id = LPFC_ELS_ID_DEFAULT;
10876 			break;
10877 		}
10878 
10879 		bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10880 	} else {
10881 		/* DID */
10882 		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest, did);
10883 
10884 		/* Transfer length */
10885 		wqe->xmit_els_rsp.response_payload_len = cmd_size;
10886 
10887 		bf_set(wqe_cmnd, &wqe->xmit_els_rsp.wqe_com,
10888 		       CMD_XMIT_ELS_RSP64_WQE);
10889 	}
10890 
10891 	bf_set(wqe_tmo, &wqe->generic.wqe_com, tmo);
10892 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, cmdiocbq->iotag);
10893 	bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
10894 
10895 	/* If we have NPIV enabled, we want to send ELS traffic by VPI.
10896 	 * For SLI4, since the driver controls VPIs we also want to include
10897 	 * all ELS pt2pt protocol traffic as well.
10898 	 */
10899 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
10900 	    (vport->fc_flag & FC_PT2PT)) {
10901 		if (expect_rsp) {
10902 			bf_set(els_req64_sid, &wqe->els_req, vport->fc_myDID);
10903 
10904 			/* For ELS_REQUEST64_WQE, use the VPI by default */
10905 			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10906 			       phba->vpi_ids[vport->vpi]);
10907 		}
10908 
10909 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10910 		if (elscmd == ELS_CMD_ECHO)
10911 			bf_set(wqe_ct, &wqe->generic.wqe_com, 0);
10912 		else
10913 			bf_set(wqe_ct, &wqe->generic.wqe_com, 1);
10914 	}
10915 }
10916 
10917 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)10918 lpfc_sli_prep_els_req_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
10919 			  struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
10920 			  u16 cmd_size, u32 did, u32 elscmd, u8 tmo,
10921 			  u8 expect_rsp)
10922 {
10923 	phba->__lpfc_sli_prep_els_req_rsp(cmdiocbq, vport, bmp, cmd_size, did,
10924 					  elscmd, tmo, expect_rsp);
10925 }
10926 
10927 static void
__lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10928 __lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10929 			   u16 rpi, u32 num_entry, u8 tmo)
10930 {
10931 	IOCB_t *cmd;
10932 
10933 	cmd = &cmdiocbq->iocb;
10934 	memset(cmd, 0, sizeof(*cmd));
10935 
10936 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10937 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10938 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10939 	cmd->un.genreq64.bdl.bdeSize = num_entry * sizeof(struct ulp_bde64);
10940 
10941 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
10942 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
10943 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
10944 
10945 	cmd->ulpContext = rpi;
10946 	cmd->ulpClass = CLASS3;
10947 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
10948 	cmd->ulpBdeCount = 1;
10949 	cmd->ulpLe = 1;
10950 	cmd->ulpOwner = OWN_CHIP;
10951 	cmd->ulpTimeout = tmo;
10952 }
10953 
10954 static void
__lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10955 __lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10956 			   u16 rpi, u32 num_entry, u8 tmo)
10957 {
10958 	union lpfc_wqe128 *cmdwqe;
10959 	struct ulp_bde64_le *bde, *bpl;
10960 	u32 xmit_len = 0, total_len = 0, size, type, i;
10961 
10962 	cmdwqe = &cmdiocbq->wqe;
10963 	memset(cmdwqe, 0, sizeof(*cmdwqe));
10964 
10965 	/* Calculate total_len and xmit_len */
10966 	bpl = (struct ulp_bde64_le *)bmp->virt;
10967 	for (i = 0; i < num_entry; i++) {
10968 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10969 		total_len += size;
10970 	}
10971 	for (i = 0; i < num_entry; i++) {
10972 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10973 		type = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_TYPE_MASK;
10974 		if (type != ULP_BDE64_TYPE_BDE_64)
10975 			break;
10976 		xmit_len += size;
10977 	}
10978 
10979 	/* Words 0 - 2 */
10980 	bde = (struct ulp_bde64_le *)&cmdwqe->generic.bde;
10981 	bde->addr_low = bpl->addr_low;
10982 	bde->addr_high = bpl->addr_high;
10983 	bde->type_size = cpu_to_le32(xmit_len);
10984 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10985 
10986 	/* Word 3 */
10987 	cmdwqe->gen_req.request_payload_len = xmit_len;
10988 
10989 	/* Word 5 */
10990 	bf_set(wqe_type, &cmdwqe->gen_req.wge_ctl, FC_TYPE_CT);
10991 	bf_set(wqe_rctl, &cmdwqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);
10992 	bf_set(wqe_si, &cmdwqe->gen_req.wge_ctl, 1);
10993 	bf_set(wqe_la, &cmdwqe->gen_req.wge_ctl, 1);
10994 
10995 	/* Word 6 */
10996 	bf_set(wqe_ctxt_tag, &cmdwqe->gen_req.wqe_com, rpi);
10997 
10998 	/* Word 7 */
10999 	bf_set(wqe_tmo, &cmdwqe->gen_req.wqe_com, tmo);
11000 	bf_set(wqe_class, &cmdwqe->gen_req.wqe_com, CLASS3);
11001 	bf_set(wqe_cmnd, &cmdwqe->gen_req.wqe_com, CMD_GEN_REQUEST64_CR);
11002 	bf_set(wqe_ct, &cmdwqe->gen_req.wqe_com, SLI4_CT_RPI);
11003 
11004 	/* Word 12 */
11005 	cmdwqe->gen_req.max_response_payload_len = total_len - xmit_len;
11006 }
11007 
11008 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)11009 lpfc_sli_prep_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11010 		      struct lpfc_dmabuf *bmp, u16 rpi, u32 num_entry, u8 tmo)
11011 {
11012 	phba->__lpfc_sli_prep_gen_req(cmdiocbq, bmp, rpi, num_entry, tmo);
11013 }
11014 
11015 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)11016 __lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq *cmdiocbq,
11017 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11018 			      u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11019 {
11020 	IOCB_t *icmd;
11021 
11022 	icmd = &cmdiocbq->iocb;
11023 	memset(icmd, 0, sizeof(*icmd));
11024 
11025 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
11026 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
11027 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
11028 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
11029 	icmd->un.xseq64.w5.hcsw.Fctl = LA;
11030 	if (last_seq)
11031 		icmd->un.xseq64.w5.hcsw.Fctl |= LS;
11032 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11033 	icmd->un.xseq64.w5.hcsw.Rctl = rctl;
11034 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
11035 
11036 	icmd->ulpBdeCount = 1;
11037 	icmd->ulpLe = 1;
11038 	icmd->ulpClass = CLASS3;
11039 
11040 	switch (cr_cx_cmd) {
11041 	case CMD_XMIT_SEQUENCE64_CR:
11042 		icmd->ulpContext = rpi;
11043 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
11044 		break;
11045 	case CMD_XMIT_SEQUENCE64_CX:
11046 		icmd->ulpContext = ox_id;
11047 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
11048 		break;
11049 	default:
11050 		break;
11051 	}
11052 }
11053 
11054 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)11055 __lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq *cmdiocbq,
11056 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11057 			      u32 full_size, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11058 {
11059 	union lpfc_wqe128 *wqe;
11060 	struct ulp_bde64 *bpl;
11061 
11062 	wqe = &cmdiocbq->wqe;
11063 	memset(wqe, 0, sizeof(*wqe));
11064 
11065 	/* Words 0 - 2 */
11066 	bpl = (struct ulp_bde64 *)bmp->virt;
11067 	wqe->xmit_sequence.bde.addrHigh = bpl->addrHigh;
11068 	wqe->xmit_sequence.bde.addrLow = bpl->addrLow;
11069 	wqe->xmit_sequence.bde.tus.w = bpl->tus.w;
11070 
11071 	/* Word 5 */
11072 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, last_seq);
11073 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 1);
11074 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
11075 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, rctl);
11076 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_CT);
11077 
11078 	/* Word 6 */
11079 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com, rpi);
11080 
11081 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
11082 	       CMD_XMIT_SEQUENCE64_WQE);
11083 
11084 	/* Word 7 */
11085 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
11086 
11087 	/* Word 9 */
11088 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ox_id);
11089 
11090 	/* Word 12 */
11091 	if (cmdiocbq->cmd_flag & (LPFC_IO_LIBDFC | LPFC_IO_LOOPBACK))
11092 		wqe->xmit_sequence.xmit_len = full_size;
11093 	else
11094 		wqe->xmit_sequence.xmit_len =
11095 			wqe->xmit_sequence.bde.tus.f.bdeSize;
11096 }
11097 
11098 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)11099 lpfc_sli_prep_xmit_seq64(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11100 			 struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11101 			 u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11102 {
11103 	phba->__lpfc_sli_prep_xmit_seq64(cmdiocbq, bmp, rpi, ox_id, num_entry,
11104 					 rctl, last_seq, cr_cx_cmd);
11105 }
11106 
11107 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)11108 __lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11109 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11110 			     bool wqec)
11111 {
11112 	IOCB_t *icmd = NULL;
11113 
11114 	icmd = &cmdiocbq->iocb;
11115 	memset(icmd, 0, sizeof(*icmd));
11116 
11117 	/* Word 5 */
11118 	icmd->un.acxri.abortContextTag = ulp_context;
11119 	icmd->un.acxri.abortIoTag = iotag;
11120 
11121 	if (ia) {
11122 		/* Word 7 */
11123 		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
11124 	} else {
11125 		/* Word 3 */
11126 		icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
11127 
11128 		/* Word 7 */
11129 		icmd->ulpClass = ulp_class;
11130 		icmd->ulpCommand = CMD_ABORT_XRI_CN;
11131 	}
11132 
11133 	/* Word 7 */
11134 	icmd->ulpLe = 1;
11135 }
11136 
11137 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)11138 __lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11139 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11140 			     bool wqec)
11141 {
11142 	union lpfc_wqe128 *wqe;
11143 
11144 	wqe = &cmdiocbq->wqe;
11145 	memset(wqe, 0, sizeof(*wqe));
11146 
11147 	/* Word 3 */
11148 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
11149 	if (ia)
11150 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
11151 	else
11152 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
11153 
11154 	/* Word 7 */
11155 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_WQE);
11156 
11157 	/* Word 8 */
11158 	wqe->abort_cmd.wqe_com.abort_tag = ulp_context;
11159 
11160 	/* Word 9 */
11161 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, iotag);
11162 
11163 	/* Word 10 */
11164 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
11165 
11166 	/* Word 11 */
11167 	if (wqec)
11168 		bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
11169 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, cqid);
11170 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11171 }
11172 
11173 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)11174 lpfc_sli_prep_abort_xri(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11175 			u16 ulp_context, u16 iotag, u8 ulp_class, u16 cqid,
11176 			bool ia, bool wqec)
11177 {
11178 	phba->__lpfc_sli_prep_abort_xri(cmdiocbq, ulp_context, iotag, ulp_class,
11179 					cqid, ia, wqec);
11180 }
11181 
11182 /**
11183  * lpfc_sli_api_table_setup - Set up sli api function jump table
11184  * @phba: The hba struct for which this call is being executed.
11185  * @dev_grp: The HBA PCI-Device group number.
11186  *
11187  * This routine sets up the SLI interface API function jump table in @phba
11188  * struct.
11189  * Returns: 0 - success, -ENODEV - failure.
11190  **/
11191 int
lpfc_sli_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)11192 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11193 {
11194 
11195 	switch (dev_grp) {
11196 	case LPFC_PCI_DEV_LP:
11197 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11198 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
11199 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
11200 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s3;
11201 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s3;
11202 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s3;
11203 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s3;
11204 		break;
11205 	case LPFC_PCI_DEV_OC:
11206 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11207 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
11208 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
11209 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s4;
11210 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s4;
11211 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s4;
11212 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s4;
11213 		break;
11214 	default:
11215 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11216 				"1419 Invalid HBA PCI-device group: 0x%x\n",
11217 				dev_grp);
11218 		return -ENODEV;
11219 	}
11220 	return 0;
11221 }
11222 
11223 /**
11224  * lpfc_sli4_calc_ring - Calculates which ring to use
11225  * @phba: Pointer to HBA context object.
11226  * @piocb: Pointer to command iocb.
11227  *
11228  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11229  * hba_wqidx, thus we need to calculate the corresponding ring.
11230  * Since ABORTS must go on the same WQ of the command they are
11231  * aborting, we use command's hba_wqidx.
11232  */
11233 struct lpfc_sli_ring *
lpfc_sli4_calc_ring(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)11234 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
11235 {
11236 	struct lpfc_io_buf *lpfc_cmd;
11237 
11238 	if (piocb->cmd_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
11239 		if (unlikely(!phba->sli4_hba.hdwq))
11240 			return NULL;
11241 		/*
11242 		 * for abort iocb hba_wqidx should already
11243 		 * be setup based on what work queue we used.
11244 		 */
11245 		if (!(piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
11246 			lpfc_cmd = piocb->io_buf;
11247 			piocb->hba_wqidx = lpfc_cmd->hdwq_no;
11248 		}
11249 		return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
11250 	} else {
11251 		if (unlikely(!phba->sli4_hba.els_wq))
11252 			return NULL;
11253 		piocb->hba_wqidx = 0;
11254 		return phba->sli4_hba.els_wq->pring;
11255 	}
11256 }
11257 
lpfc_sli4_poll_eq(struct lpfc_queue * eq)11258 inline void lpfc_sli4_poll_eq(struct lpfc_queue *eq)
11259 {
11260 	struct lpfc_hba *phba = eq->phba;
11261 
11262 	/*
11263 	 * Unlocking an irq is one of the entry point to check
11264 	 * for re-schedule, but we are good for io submission
11265 	 * path as midlayer does a get_cpu to glue us in. Flush
11266 	 * out the invalidate queue so we can see the updated
11267 	 * value for flag.
11268 	 */
11269 	smp_rmb();
11270 
11271 	if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
11272 		/* We will not likely get the completion for the caller
11273 		 * during this iteration but i guess that's fine.
11274 		 * Future io's coming on this eq should be able to
11275 		 * pick it up.  As for the case of single io's, they
11276 		 * will be handled through a sched from polling timer
11277 		 * function which is currently triggered every 1msec.
11278 		 */
11279 		lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM,
11280 				     LPFC_QUEUE_WORK);
11281 }
11282 
11283 /**
11284  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
11285  * @phba: Pointer to HBA context object.
11286  * @ring_number: Ring number
11287  * @piocb: Pointer to command iocb.
11288  * @flag: Flag indicating if this command can be put into txq.
11289  *
11290  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11291  * function. This function gets the hbalock and calls
11292  * __lpfc_sli_issue_iocb function and will return the error returned
11293  * by __lpfc_sli_issue_iocb function. This wrapper is used by
11294  * functions which do not hold hbalock.
11295  **/
11296 int
lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)11297 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11298 		    struct lpfc_iocbq *piocb, uint32_t flag)
11299 {
11300 	struct lpfc_sli_ring *pring;
11301 	struct lpfc_queue *eq;
11302 	unsigned long iflags;
11303 	int rc;
11304 
11305 	/* If the PCI channel is in offline state, do not post iocbs. */
11306 	if (unlikely(pci_channel_offline(phba->pcidev)))
11307 		return IOCB_ERROR;
11308 
11309 	if (phba->sli_rev == LPFC_SLI_REV4) {
11310 		lpfc_sli_prep_wqe(phba, piocb);
11311 
11312 		eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11313 
11314 		pring = lpfc_sli4_calc_ring(phba, piocb);
11315 		if (unlikely(pring == NULL))
11316 			return IOCB_ERROR;
11317 
11318 		spin_lock_irqsave(&pring->ring_lock, iflags);
11319 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11320 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
11321 
11322 		lpfc_sli4_poll_eq(eq);
11323 	} else {
11324 		/* For now, SLI2/3 will still use hbalock */
11325 		spin_lock_irqsave(&phba->hbalock, iflags);
11326 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11327 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11328 	}
11329 	return rc;
11330 }
11331 
11332 /**
11333  * lpfc_extra_ring_setup - Extra ring setup function
11334  * @phba: Pointer to HBA context object.
11335  *
11336  * This function is called while driver attaches with the
11337  * HBA to setup the extra ring. The extra ring is used
11338  * only when driver needs to support target mode functionality
11339  * or IP over FC functionalities.
11340  *
11341  * This function is called with no lock held. SLI3 only.
11342  **/
11343 static int
lpfc_extra_ring_setup(struct lpfc_hba * phba)11344 lpfc_extra_ring_setup( struct lpfc_hba *phba)
11345 {
11346 	struct lpfc_sli *psli;
11347 	struct lpfc_sli_ring *pring;
11348 
11349 	psli = &phba->sli;
11350 
11351 	/* Adjust cmd/rsp ring iocb entries more evenly */
11352 
11353 	/* Take some away from the FCP ring */
11354 	pring = &psli->sli3_ring[LPFC_FCP_RING];
11355 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11356 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11357 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11358 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11359 
11360 	/* and give them to the extra ring */
11361 	pring = &psli->sli3_ring[LPFC_EXTRA_RING];
11362 
11363 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11364 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11365 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11366 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11367 
11368 	/* Setup default profile for this ring */
11369 	pring->iotag_max = 4096;
11370 	pring->num_mask = 1;
11371 	pring->prt[0].profile = 0;      /* Mask 0 */
11372 	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11373 	pring->prt[0].type = phba->cfg_multi_ring_type;
11374 	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11375 	return 0;
11376 }
11377 
11378 static void
lpfc_sli_post_recovery_event(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)11379 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11380 			     struct lpfc_nodelist *ndlp)
11381 {
11382 	unsigned long iflags;
11383 	struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
11384 
11385 	/* Hold a node reference for outstanding queued work */
11386 	if (!lpfc_nlp_get(ndlp))
11387 		return;
11388 
11389 	spin_lock_irqsave(&phba->hbalock, iflags);
11390 	if (!list_empty(&evtp->evt_listp)) {
11391 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11392 		lpfc_nlp_put(ndlp);
11393 		return;
11394 	}
11395 
11396 	evtp->evt_arg1 = ndlp;
11397 	evtp->evt = LPFC_EVT_RECOVER_PORT;
11398 	list_add_tail(&evtp->evt_listp, &phba->work_list);
11399 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11400 
11401 	lpfc_worker_wake_up(phba);
11402 }
11403 
11404 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11405  * @phba: Pointer to HBA context object.
11406  * @iocbq: Pointer to iocb object.
11407  *
11408  * The async_event handler calls this routine when it receives
11409  * an ASYNC_STATUS_CN event from the port.  The port generates
11410  * this event when an Abort Sequence request to an rport fails
11411  * twice in succession.  The abort could be originated by the
11412  * driver or by the port.  The ABTS could have been for an ELS
11413  * or FCP IO.  The port only generates this event when an ABTS
11414  * fails to complete after one retry.
11415  */
11416 static void
lpfc_sli_abts_err_handler(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)11417 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11418 			  struct lpfc_iocbq *iocbq)
11419 {
11420 	struct lpfc_nodelist *ndlp = NULL;
11421 	uint16_t rpi = 0, vpi = 0;
11422 	struct lpfc_vport *vport = NULL;
11423 
11424 	/* The rpi in the ulpContext is vport-sensitive. */
11425 	vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11426 	rpi = iocbq->iocb.ulpContext;
11427 
11428 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11429 			"3092 Port generated ABTS async event "
11430 			"on vpi %d rpi %d status 0x%x\n",
11431 			vpi, rpi, iocbq->iocb.ulpStatus);
11432 
11433 	vport = lpfc_find_vport_by_vpid(phba, vpi);
11434 	if (!vport)
11435 		goto err_exit;
11436 	ndlp = lpfc_findnode_rpi(vport, rpi);
11437 	if (!ndlp)
11438 		goto err_exit;
11439 
11440 	if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11441 		lpfc_sli_abts_recover_port(vport, ndlp);
11442 	return;
11443 
11444  err_exit:
11445 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11446 			"3095 Event Context not found, no "
11447 			"action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11448 			vpi, rpi, iocbq->iocb.ulpStatus,
11449 			iocbq->iocb.ulpContext);
11450 }
11451 
11452 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11453  * @phba: pointer to HBA context object.
11454  * @ndlp: nodelist pointer for the impacted rport.
11455  * @axri: pointer to the wcqe containing the failed exchange.
11456  *
11457  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11458  * port.  The port generates this event when an abort exchange request to an
11459  * rport fails twice in succession with no reply.  The abort could be originated
11460  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
11461  */
11462 void
lpfc_sli4_abts_err_handler(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct sli4_wcqe_xri_aborted * axri)11463 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11464 			   struct lpfc_nodelist *ndlp,
11465 			   struct sli4_wcqe_xri_aborted *axri)
11466 {
11467 	uint32_t ext_status = 0;
11468 
11469 	if (!ndlp) {
11470 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11471 				"3115 Node Context not found, driver "
11472 				"ignoring abts err event\n");
11473 		return;
11474 	}
11475 
11476 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11477 			"3116 Port generated FCP XRI ABORT event on "
11478 			"vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
11479 			ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
11480 			bf_get(lpfc_wcqe_xa_xri, axri),
11481 			bf_get(lpfc_wcqe_xa_status, axri),
11482 			axri->parameter);
11483 
11484 	/*
11485 	 * Catch the ABTS protocol failure case.  Older OCe FW releases returned
11486 	 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11487 	 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11488 	 */
11489 	ext_status = axri->parameter & IOERR_PARAM_MASK;
11490 	if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11491 	    ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
11492 		lpfc_sli_post_recovery_event(phba, ndlp);
11493 }
11494 
11495 /**
11496  * lpfc_sli_async_event_handler - ASYNC iocb handler function
11497  * @phba: Pointer to HBA context object.
11498  * @pring: Pointer to driver SLI ring object.
11499  * @iocbq: Pointer to iocb object.
11500  *
11501  * This function is called by the slow ring event handler
11502  * function when there is an ASYNC event iocb in the ring.
11503  * This function is called with no lock held.
11504  * Currently this function handles only temperature related
11505  * ASYNC events. The function decodes the temperature sensor
11506  * event message and posts events for the management applications.
11507  **/
11508 static void
lpfc_sli_async_event_handler(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * iocbq)11509 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11510 	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11511 {
11512 	IOCB_t *icmd;
11513 	uint16_t evt_code;
11514 	struct temp_event temp_event_data;
11515 	struct Scsi_Host *shost;
11516 	uint32_t *iocb_w;
11517 
11518 	icmd = &iocbq->iocb;
11519 	evt_code = icmd->un.asyncstat.evt_code;
11520 
11521 	switch (evt_code) {
11522 	case ASYNC_TEMP_WARN:
11523 	case ASYNC_TEMP_SAFE:
11524 		temp_event_data.data = (uint32_t) icmd->ulpContext;
11525 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11526 		if (evt_code == ASYNC_TEMP_WARN) {
11527 			temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
11528 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11529 				"0347 Adapter is very hot, please take "
11530 				"corrective action. temperature : %d Celsius\n",
11531 				(uint32_t) icmd->ulpContext);
11532 		} else {
11533 			temp_event_data.event_code = LPFC_NORMAL_TEMP;
11534 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11535 				"0340 Adapter temperature is OK now. "
11536 				"temperature : %d Celsius\n",
11537 				(uint32_t) icmd->ulpContext);
11538 		}
11539 
11540 		/* Send temperature change event to applications */
11541 		shost = lpfc_shost_from_vport(phba->pport);
11542 		fc_host_post_vendor_event(shost, fc_get_event_number(),
11543 			sizeof(temp_event_data), (char *) &temp_event_data,
11544 			LPFC_NL_VENDOR_ID);
11545 		break;
11546 	case ASYNC_STATUS_CN:
11547 		lpfc_sli_abts_err_handler(phba, iocbq);
11548 		break;
11549 	default:
11550 		iocb_w = (uint32_t *) icmd;
11551 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11552 			"0346 Ring %d handler: unexpected ASYNC_STATUS"
11553 			" evt_code 0x%x\n"
11554 			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
11555 			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
11556 			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
11557 			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
11558 			pring->ringno, icmd->un.asyncstat.evt_code,
11559 			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11560 			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11561 			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11562 			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11563 
11564 		break;
11565 	}
11566 }
11567 
11568 
11569 /**
11570  * lpfc_sli4_setup - SLI ring setup function
11571  * @phba: Pointer to HBA context object.
11572  *
11573  * lpfc_sli_setup sets up rings of the SLI interface with
11574  * number of iocbs per ring and iotags. This function is
11575  * called while driver attach to the HBA and before the
11576  * interrupts are enabled. So there is no need for locking.
11577  *
11578  * This function always returns 0.
11579  **/
11580 int
lpfc_sli4_setup(struct lpfc_hba * phba)11581 lpfc_sli4_setup(struct lpfc_hba *phba)
11582 {
11583 	struct lpfc_sli_ring *pring;
11584 
11585 	pring = phba->sli4_hba.els_wq->pring;
11586 	pring->num_mask = LPFC_MAX_RING_MASK;
11587 	pring->prt[0].profile = 0;	/* Mask 0 */
11588 	pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11589 	pring->prt[0].type = FC_TYPE_ELS;
11590 	pring->prt[0].lpfc_sli_rcv_unsol_event =
11591 	    lpfc_els_unsol_event;
11592 	pring->prt[1].profile = 0;	/* Mask 1 */
11593 	pring->prt[1].rctl = FC_RCTL_ELS_REP;
11594 	pring->prt[1].type = FC_TYPE_ELS;
11595 	pring->prt[1].lpfc_sli_rcv_unsol_event =
11596 	    lpfc_els_unsol_event;
11597 	pring->prt[2].profile = 0;	/* Mask 2 */
11598 	/* NameServer Inquiry */
11599 	pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11600 	/* NameServer */
11601 	pring->prt[2].type = FC_TYPE_CT;
11602 	pring->prt[2].lpfc_sli_rcv_unsol_event =
11603 	    lpfc_ct_unsol_event;
11604 	pring->prt[3].profile = 0;	/* Mask 3 */
11605 	/* NameServer response */
11606 	pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11607 	/* NameServer */
11608 	pring->prt[3].type = FC_TYPE_CT;
11609 	pring->prt[3].lpfc_sli_rcv_unsol_event =
11610 	    lpfc_ct_unsol_event;
11611 	return 0;
11612 }
11613 
11614 /**
11615  * lpfc_sli_setup - SLI ring setup function
11616  * @phba: Pointer to HBA context object.
11617  *
11618  * lpfc_sli_setup sets up rings of the SLI interface with
11619  * number of iocbs per ring and iotags. This function is
11620  * called while driver attach to the HBA and before the
11621  * interrupts are enabled. So there is no need for locking.
11622  *
11623  * This function always returns 0. SLI3 only.
11624  **/
11625 int
lpfc_sli_setup(struct lpfc_hba * phba)11626 lpfc_sli_setup(struct lpfc_hba *phba)
11627 {
11628 	int i, totiocbsize = 0;
11629 	struct lpfc_sli *psli = &phba->sli;
11630 	struct lpfc_sli_ring *pring;
11631 
11632 	psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
11633 	psli->sli_flag = 0;
11634 
11635 	psli->iocbq_lookup = NULL;
11636 	psli->iocbq_lookup_len = 0;
11637 	psli->last_iotag = 0;
11638 
11639 	for (i = 0; i < psli->num_rings; i++) {
11640 		pring = &psli->sli3_ring[i];
11641 		switch (i) {
11642 		case LPFC_FCP_RING:	/* ring 0 - FCP */
11643 			/* numCiocb and numRiocb are used in config_port */
11644 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11645 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11646 			pring->sli.sli3.numCiocb +=
11647 				SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11648 			pring->sli.sli3.numRiocb +=
11649 				SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11650 			pring->sli.sli3.numCiocb +=
11651 				SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11652 			pring->sli.sli3.numRiocb +=
11653 				SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11654 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11655 							SLI3_IOCB_CMD_SIZE :
11656 							SLI2_IOCB_CMD_SIZE;
11657 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11658 							SLI3_IOCB_RSP_SIZE :
11659 							SLI2_IOCB_RSP_SIZE;
11660 			pring->iotag_ctr = 0;
11661 			pring->iotag_max =
11662 			    (phba->cfg_hba_queue_depth * 2);
11663 			pring->fast_iotag = pring->iotag_max;
11664 			pring->num_mask = 0;
11665 			break;
11666 		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
11667 			/* numCiocb and numRiocb are used in config_port */
11668 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11669 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11670 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11671 							SLI3_IOCB_CMD_SIZE :
11672 							SLI2_IOCB_CMD_SIZE;
11673 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11674 							SLI3_IOCB_RSP_SIZE :
11675 							SLI2_IOCB_RSP_SIZE;
11676 			pring->iotag_max = phba->cfg_hba_queue_depth;
11677 			pring->num_mask = 0;
11678 			break;
11679 		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
11680 			/* numCiocb and numRiocb are used in config_port */
11681 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11682 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11683 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11684 							SLI3_IOCB_CMD_SIZE :
11685 							SLI2_IOCB_CMD_SIZE;
11686 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11687 							SLI3_IOCB_RSP_SIZE :
11688 							SLI2_IOCB_RSP_SIZE;
11689 			pring->fast_iotag = 0;
11690 			pring->iotag_ctr = 0;
11691 			pring->iotag_max = 4096;
11692 			pring->lpfc_sli_rcv_async_status =
11693 				lpfc_sli_async_event_handler;
11694 			pring->num_mask = LPFC_MAX_RING_MASK;
11695 			pring->prt[0].profile = 0;	/* Mask 0 */
11696 			pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11697 			pring->prt[0].type = FC_TYPE_ELS;
11698 			pring->prt[0].lpfc_sli_rcv_unsol_event =
11699 			    lpfc_els_unsol_event;
11700 			pring->prt[1].profile = 0;	/* Mask 1 */
11701 			pring->prt[1].rctl = FC_RCTL_ELS_REP;
11702 			pring->prt[1].type = FC_TYPE_ELS;
11703 			pring->prt[1].lpfc_sli_rcv_unsol_event =
11704 			    lpfc_els_unsol_event;
11705 			pring->prt[2].profile = 0;	/* Mask 2 */
11706 			/* NameServer Inquiry */
11707 			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11708 			/* NameServer */
11709 			pring->prt[2].type = FC_TYPE_CT;
11710 			pring->prt[2].lpfc_sli_rcv_unsol_event =
11711 			    lpfc_ct_unsol_event;
11712 			pring->prt[3].profile = 0;	/* Mask 3 */
11713 			/* NameServer response */
11714 			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11715 			/* NameServer */
11716 			pring->prt[3].type = FC_TYPE_CT;
11717 			pring->prt[3].lpfc_sli_rcv_unsol_event =
11718 			    lpfc_ct_unsol_event;
11719 			break;
11720 		}
11721 		totiocbsize += (pring->sli.sli3.numCiocb *
11722 			pring->sli.sli3.sizeCiocb) +
11723 			(pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
11724 	}
11725 	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
11726 		/* Too many cmd / rsp ring entries in SLI2 SLIM */
11727 		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11728 		       "SLI2 SLIM Data: x%x x%lx\n",
11729 		       phba->brd_no, totiocbsize,
11730 		       (unsigned long) MAX_SLIM_IOCB_SIZE);
11731 	}
11732 	if (phba->cfg_multi_ring_support == 2)
11733 		lpfc_extra_ring_setup(phba);
11734 
11735 	return 0;
11736 }
11737 
11738 /**
11739  * lpfc_sli4_queue_init - Queue initialization function
11740  * @phba: Pointer to HBA context object.
11741  *
11742  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
11743  * ring. This function also initializes ring indices of each ring.
11744  * This function is called during the initialization of the SLI
11745  * interface of an HBA.
11746  * This function is called with no lock held and always returns
11747  * 1.
11748  **/
11749 void
lpfc_sli4_queue_init(struct lpfc_hba * phba)11750 lpfc_sli4_queue_init(struct lpfc_hba *phba)
11751 {
11752 	struct lpfc_sli *psli;
11753 	struct lpfc_sli_ring *pring;
11754 	int i;
11755 
11756 	psli = &phba->sli;
11757 	spin_lock_irq(&phba->hbalock);
11758 	INIT_LIST_HEAD(&psli->mboxq);
11759 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11760 	/* Initialize list headers for txq and txcmplq as double linked lists */
11761 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
11762 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11763 		pring->flag = 0;
11764 		pring->ringno = LPFC_FCP_RING;
11765 		pring->txcmplq_cnt = 0;
11766 		INIT_LIST_HEAD(&pring->txq);
11767 		INIT_LIST_HEAD(&pring->txcmplq);
11768 		INIT_LIST_HEAD(&pring->iocb_continueq);
11769 		spin_lock_init(&pring->ring_lock);
11770 	}
11771 	pring = phba->sli4_hba.els_wq->pring;
11772 	pring->flag = 0;
11773 	pring->ringno = LPFC_ELS_RING;
11774 	pring->txcmplq_cnt = 0;
11775 	INIT_LIST_HEAD(&pring->txq);
11776 	INIT_LIST_HEAD(&pring->txcmplq);
11777 	INIT_LIST_HEAD(&pring->iocb_continueq);
11778 	spin_lock_init(&pring->ring_lock);
11779 
11780 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11781 		pring = phba->sli4_hba.nvmels_wq->pring;
11782 		pring->flag = 0;
11783 		pring->ringno = LPFC_ELS_RING;
11784 		pring->txcmplq_cnt = 0;
11785 		INIT_LIST_HEAD(&pring->txq);
11786 		INIT_LIST_HEAD(&pring->txcmplq);
11787 		INIT_LIST_HEAD(&pring->iocb_continueq);
11788 		spin_lock_init(&pring->ring_lock);
11789 	}
11790 
11791 	spin_unlock_irq(&phba->hbalock);
11792 }
11793 
11794 /**
11795  * lpfc_sli_queue_init - Queue initialization function
11796  * @phba: Pointer to HBA context object.
11797  *
11798  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11799  * ring. This function also initializes ring indices of each ring.
11800  * This function is called during the initialization of the SLI
11801  * interface of an HBA.
11802  * This function is called with no lock held and always returns
11803  * 1.
11804  **/
11805 void
lpfc_sli_queue_init(struct lpfc_hba * phba)11806 lpfc_sli_queue_init(struct lpfc_hba *phba)
11807 {
11808 	struct lpfc_sli *psli;
11809 	struct lpfc_sli_ring *pring;
11810 	int i;
11811 
11812 	psli = &phba->sli;
11813 	spin_lock_irq(&phba->hbalock);
11814 	INIT_LIST_HEAD(&psli->mboxq);
11815 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11816 	/* Initialize list headers for txq and txcmplq as double linked lists */
11817 	for (i = 0; i < psli->num_rings; i++) {
11818 		pring = &psli->sli3_ring[i];
11819 		pring->ringno = i;
11820 		pring->sli.sli3.next_cmdidx  = 0;
11821 		pring->sli.sli3.local_getidx = 0;
11822 		pring->sli.sli3.cmdidx = 0;
11823 		INIT_LIST_HEAD(&pring->iocb_continueq);
11824 		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11825 		INIT_LIST_HEAD(&pring->postbufq);
11826 		pring->flag = 0;
11827 		INIT_LIST_HEAD(&pring->txq);
11828 		INIT_LIST_HEAD(&pring->txcmplq);
11829 		spin_lock_init(&pring->ring_lock);
11830 	}
11831 	spin_unlock_irq(&phba->hbalock);
11832 }
11833 
11834 /**
11835  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11836  * @phba: Pointer to HBA context object.
11837  *
11838  * This routine flushes the mailbox command subsystem. It will unconditionally
11839  * flush all the mailbox commands in the three possible stages in the mailbox
11840  * command sub-system: pending mailbox command queue; the outstanding mailbox
11841  * command; and completed mailbox command queue. It is caller's responsibility
11842  * to make sure that the driver is in the proper state to flush the mailbox
11843  * command sub-system. Namely, the posting of mailbox commands into the
11844  * pending mailbox command queue from the various clients must be stopped;
11845  * either the HBA is in a state that it will never works on the outstanding
11846  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11847  * mailbox command has been completed.
11848  **/
11849 static void
lpfc_sli_mbox_sys_flush(struct lpfc_hba * phba)11850 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11851 {
11852 	LIST_HEAD(completions);
11853 	struct lpfc_sli *psli = &phba->sli;
11854 	LPFC_MBOXQ_t *pmb;
11855 	unsigned long iflag;
11856 
11857 	/* Disable softirqs, including timers from obtaining phba->hbalock */
11858 	local_bh_disable();
11859 
11860 	/* Flush all the mailbox commands in the mbox system */
11861 	spin_lock_irqsave(&phba->hbalock, iflag);
11862 
11863 	/* The pending mailbox command queue */
11864 	list_splice_init(&phba->sli.mboxq, &completions);
11865 	/* The outstanding active mailbox command */
11866 	if (psli->mbox_active) {
11867 		list_add_tail(&psli->mbox_active->list, &completions);
11868 		psli->mbox_active = NULL;
11869 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11870 	}
11871 	/* The completed mailbox command queue */
11872 	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11873 	spin_unlock_irqrestore(&phba->hbalock, iflag);
11874 
11875 	/* Enable softirqs again, done with phba->hbalock */
11876 	local_bh_enable();
11877 
11878 	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11879 	while (!list_empty(&completions)) {
11880 		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11881 		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11882 		if (pmb->mbox_cmpl)
11883 			pmb->mbox_cmpl(phba, pmb);
11884 	}
11885 }
11886 
11887 /**
11888  * lpfc_sli_host_down - Vport cleanup function
11889  * @vport: Pointer to virtual port object.
11890  *
11891  * lpfc_sli_host_down is called to clean up the resources
11892  * associated with a vport before destroying virtual
11893  * port data structures.
11894  * This function does following operations:
11895  * - Free discovery resources associated with this virtual
11896  *   port.
11897  * - Free iocbs associated with this virtual port in
11898  *   the txq.
11899  * - Send abort for all iocb commands associated with this
11900  *   vport in txcmplq.
11901  *
11902  * This function is called with no lock held and always returns 1.
11903  **/
11904 int
lpfc_sli_host_down(struct lpfc_vport * vport)11905 lpfc_sli_host_down(struct lpfc_vport *vport)
11906 {
11907 	LIST_HEAD(completions);
11908 	struct lpfc_hba *phba = vport->phba;
11909 	struct lpfc_sli *psli = &phba->sli;
11910 	struct lpfc_queue *qp = NULL;
11911 	struct lpfc_sli_ring *pring;
11912 	struct lpfc_iocbq *iocb, *next_iocb;
11913 	int i;
11914 	unsigned long flags = 0;
11915 	uint16_t prev_pring_flag;
11916 
11917 	lpfc_cleanup_discovery_resources(vport);
11918 
11919 	spin_lock_irqsave(&phba->hbalock, flags);
11920 
11921 	/*
11922 	 * Error everything on the txq since these iocbs
11923 	 * have not been given to the FW yet.
11924 	 * Also issue ABTS for everything on the txcmplq
11925 	 */
11926 	if (phba->sli_rev != LPFC_SLI_REV4) {
11927 		for (i = 0; i < psli->num_rings; i++) {
11928 			pring = &psli->sli3_ring[i];
11929 			prev_pring_flag = pring->flag;
11930 			/* Only slow rings */
11931 			if (pring->ringno == LPFC_ELS_RING) {
11932 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11933 				/* Set the lpfc data pending flag */
11934 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11935 			}
11936 			list_for_each_entry_safe(iocb, next_iocb,
11937 						 &pring->txq, list) {
11938 				if (iocb->vport != vport)
11939 					continue;
11940 				list_move_tail(&iocb->list, &completions);
11941 			}
11942 			list_for_each_entry_safe(iocb, next_iocb,
11943 						 &pring->txcmplq, list) {
11944 				if (iocb->vport != vport)
11945 					continue;
11946 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11947 							   NULL);
11948 			}
11949 			pring->flag = prev_pring_flag;
11950 		}
11951 	} else {
11952 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11953 			pring = qp->pring;
11954 			if (!pring)
11955 				continue;
11956 			if (pring == phba->sli4_hba.els_wq->pring) {
11957 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11958 				/* Set the lpfc data pending flag */
11959 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11960 			}
11961 			prev_pring_flag = pring->flag;
11962 			spin_lock(&pring->ring_lock);
11963 			list_for_each_entry_safe(iocb, next_iocb,
11964 						 &pring->txq, list) {
11965 				if (iocb->vport != vport)
11966 					continue;
11967 				list_move_tail(&iocb->list, &completions);
11968 			}
11969 			spin_unlock(&pring->ring_lock);
11970 			list_for_each_entry_safe(iocb, next_iocb,
11971 						 &pring->txcmplq, list) {
11972 				if (iocb->vport != vport)
11973 					continue;
11974 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11975 							   NULL);
11976 			}
11977 			pring->flag = prev_pring_flag;
11978 		}
11979 	}
11980 	spin_unlock_irqrestore(&phba->hbalock, flags);
11981 
11982 	/* Make sure HBA is alive */
11983 	lpfc_issue_hb_tmo(phba);
11984 
11985 	/* Cancel all the IOCBs from the completions list */
11986 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11987 			      IOERR_SLI_DOWN);
11988 	return 1;
11989 }
11990 
11991 /**
11992  * lpfc_sli_hba_down - Resource cleanup function for the HBA
11993  * @phba: Pointer to HBA context object.
11994  *
11995  * This function cleans up all iocb, buffers, mailbox commands
11996  * while shutting down the HBA. This function is called with no
11997  * lock held and always returns 1.
11998  * This function does the following to cleanup driver resources:
11999  * - Free discovery resources for each virtual port
12000  * - Cleanup any pending fabric iocbs
12001  * - Iterate through the iocb txq and free each entry
12002  *   in the list.
12003  * - Free up any buffer posted to the HBA
12004  * - Free mailbox commands in the mailbox queue.
12005  **/
12006 int
lpfc_sli_hba_down(struct lpfc_hba * phba)12007 lpfc_sli_hba_down(struct lpfc_hba *phba)
12008 {
12009 	LIST_HEAD(completions);
12010 	struct lpfc_sli *psli = &phba->sli;
12011 	struct lpfc_queue *qp = NULL;
12012 	struct lpfc_sli_ring *pring;
12013 	struct lpfc_dmabuf *buf_ptr;
12014 	unsigned long flags = 0;
12015 	int i;
12016 
12017 	/* Shutdown the mailbox command sub-system */
12018 	lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
12019 
12020 	lpfc_hba_down_prep(phba);
12021 
12022 	/* Disable softirqs, including timers from obtaining phba->hbalock */
12023 	local_bh_disable();
12024 
12025 	lpfc_fabric_abort_hba(phba);
12026 
12027 	spin_lock_irqsave(&phba->hbalock, flags);
12028 
12029 	/*
12030 	 * Error everything on the txq since these iocbs
12031 	 * have not been given to the FW yet.
12032 	 */
12033 	if (phba->sli_rev != LPFC_SLI_REV4) {
12034 		for (i = 0; i < psli->num_rings; i++) {
12035 			pring = &psli->sli3_ring[i];
12036 			/* Only slow rings */
12037 			if (pring->ringno == LPFC_ELS_RING) {
12038 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12039 				/* Set the lpfc data pending flag */
12040 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12041 			}
12042 			list_splice_init(&pring->txq, &completions);
12043 		}
12044 	} else {
12045 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12046 			pring = qp->pring;
12047 			if (!pring)
12048 				continue;
12049 			spin_lock(&pring->ring_lock);
12050 			list_splice_init(&pring->txq, &completions);
12051 			spin_unlock(&pring->ring_lock);
12052 			if (pring == phba->sli4_hba.els_wq->pring) {
12053 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12054 				/* Set the lpfc data pending flag */
12055 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12056 			}
12057 		}
12058 	}
12059 	spin_unlock_irqrestore(&phba->hbalock, flags);
12060 
12061 	/* Cancel all the IOCBs from the completions list */
12062 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12063 			      IOERR_SLI_DOWN);
12064 
12065 	spin_lock_irqsave(&phba->hbalock, flags);
12066 	list_splice_init(&phba->elsbuf, &completions);
12067 	phba->elsbuf_cnt = 0;
12068 	phba->elsbuf_prev_cnt = 0;
12069 	spin_unlock_irqrestore(&phba->hbalock, flags);
12070 
12071 	while (!list_empty(&completions)) {
12072 		list_remove_head(&completions, buf_ptr,
12073 			struct lpfc_dmabuf, list);
12074 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12075 		kfree(buf_ptr);
12076 	}
12077 
12078 	/* Enable softirqs again, done with phba->hbalock */
12079 	local_bh_enable();
12080 
12081 	/* Return any active mbox cmds */
12082 	del_timer_sync(&psli->mbox_tmo);
12083 
12084 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
12085 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12086 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
12087 
12088 	return 1;
12089 }
12090 
12091 /**
12092  * lpfc_sli_pcimem_bcopy - SLI memory copy function
12093  * @srcp: Source memory pointer.
12094  * @destp: Destination memory pointer.
12095  * @cnt: Number of words required to be copied.
12096  *
12097  * This function is used for copying data between driver memory
12098  * and the SLI memory. This function also changes the endianness
12099  * of each word if native endianness is different from SLI
12100  * endianness. This function can be called with or without
12101  * lock.
12102  **/
12103 void
lpfc_sli_pcimem_bcopy(void * srcp,void * destp,uint32_t cnt)12104 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12105 {
12106 	uint32_t *src = srcp;
12107 	uint32_t *dest = destp;
12108 	uint32_t ldata;
12109 	int i;
12110 
12111 	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12112 		ldata = *src;
12113 		ldata = le32_to_cpu(ldata);
12114 		*dest = ldata;
12115 		src++;
12116 		dest++;
12117 	}
12118 }
12119 
12120 
12121 /**
12122  * lpfc_sli_bemem_bcopy - SLI memory copy function
12123  * @srcp: Source memory pointer.
12124  * @destp: Destination memory pointer.
12125  * @cnt: Number of words required to be copied.
12126  *
12127  * This function is used for copying data between a data structure
12128  * with big endian representation to local endianness.
12129  * This function can be called with or without lock.
12130  **/
12131 void
lpfc_sli_bemem_bcopy(void * srcp,void * destp,uint32_t cnt)12132 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12133 {
12134 	uint32_t *src = srcp;
12135 	uint32_t *dest = destp;
12136 	uint32_t ldata;
12137 	int i;
12138 
12139 	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12140 		ldata = *src;
12141 		ldata = be32_to_cpu(ldata);
12142 		*dest = ldata;
12143 		src++;
12144 		dest++;
12145 	}
12146 }
12147 
12148 /**
12149  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
12150  * @phba: Pointer to HBA context object.
12151  * @pring: Pointer to driver SLI ring object.
12152  * @mp: Pointer to driver buffer object.
12153  *
12154  * This function is called with no lock held.
12155  * It always return zero after adding the buffer to the postbufq
12156  * buffer list.
12157  **/
12158 int
lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_dmabuf * mp)12159 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12160 			 struct lpfc_dmabuf *mp)
12161 {
12162 	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12163 	   later */
12164 	spin_lock_irq(&phba->hbalock);
12165 	list_add_tail(&mp->list, &pring->postbufq);
12166 	pring->postbufq_cnt++;
12167 	spin_unlock_irq(&phba->hbalock);
12168 	return 0;
12169 }
12170 
12171 /**
12172  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
12173  * @phba: Pointer to HBA context object.
12174  *
12175  * When HBQ is enabled, buffers are searched based on tags. This function
12176  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12177  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12178  * does not conflict with tags of buffer posted for unsolicited events.
12179  * The function returns the allocated tag. The function is called with
12180  * no locks held.
12181  **/
12182 uint32_t
lpfc_sli_get_buffer_tag(struct lpfc_hba * phba)12183 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12184 {
12185 	spin_lock_irq(&phba->hbalock);
12186 	phba->buffer_tag_count++;
12187 	/*
12188 	 * Always set the QUE_BUFTAG_BIT to distiguish between
12189 	 * a tag assigned by HBQ.
12190 	 */
12191 	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12192 	spin_unlock_irq(&phba->hbalock);
12193 	return phba->buffer_tag_count;
12194 }
12195 
12196 /**
12197  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
12198  * @phba: Pointer to HBA context object.
12199  * @pring: Pointer to driver SLI ring object.
12200  * @tag: Buffer tag.
12201  *
12202  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12203  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12204  * iocb is posted to the response ring with the tag of the buffer.
12205  * This function searches the pring->postbufq list using the tag
12206  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12207  * iocb. If the buffer is found then lpfc_dmabuf object of the
12208  * buffer is returned to the caller else NULL is returned.
12209  * This function is called with no lock held.
12210  **/
12211 struct lpfc_dmabuf *
lpfc_sli_ring_taggedbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)12212 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12213 			uint32_t tag)
12214 {
12215 	struct lpfc_dmabuf *mp, *next_mp;
12216 	struct list_head *slp = &pring->postbufq;
12217 
12218 	/* Search postbufq, from the beginning, looking for a match on tag */
12219 	spin_lock_irq(&phba->hbalock);
12220 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12221 		if (mp->buffer_tag == tag) {
12222 			list_del_init(&mp->list);
12223 			pring->postbufq_cnt--;
12224 			spin_unlock_irq(&phba->hbalock);
12225 			return mp;
12226 		}
12227 	}
12228 
12229 	spin_unlock_irq(&phba->hbalock);
12230 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12231 			"0402 Cannot find virtual addr for buffer tag on "
12232 			"ring %d Data x%lx x%px x%px x%x\n",
12233 			pring->ringno, (unsigned long) tag,
12234 			slp->next, slp->prev, pring->postbufq_cnt);
12235 
12236 	return NULL;
12237 }
12238 
12239 /**
12240  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
12241  * @phba: Pointer to HBA context object.
12242  * @pring: Pointer to driver SLI ring object.
12243  * @phys: DMA address of the buffer.
12244  *
12245  * This function searches the buffer list using the dma_address
12246  * of unsolicited event to find the driver's lpfc_dmabuf object
12247  * corresponding to the dma_address. The function returns the
12248  * lpfc_dmabuf object if a buffer is found else it returns NULL.
12249  * This function is called by the ct and els unsolicited event
12250  * handlers to get the buffer associated with the unsolicited
12251  * event.
12252  *
12253  * This function is called with no lock held.
12254  **/
12255 struct lpfc_dmabuf *
lpfc_sli_ringpostbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,dma_addr_t phys)12256 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12257 			 dma_addr_t phys)
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 phys */
12263 	spin_lock_irq(&phba->hbalock);
12264 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12265 		if (mp->phys == phys) {
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 			"0410 Cannot find virtual addr for mapped buf on "
12276 			"ring %d Data x%llx x%px x%px x%x\n",
12277 			pring->ringno, (unsigned long long)phys,
12278 			slp->next, slp->prev, pring->postbufq_cnt);
12279 	return NULL;
12280 }
12281 
12282 /**
12283  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
12284  * @phba: Pointer to HBA context object.
12285  * @cmdiocb: Pointer to driver command iocb object.
12286  * @rspiocb: Pointer to driver response iocb object.
12287  *
12288  * This function is the completion handler for the abort iocbs for
12289  * ELS commands. This function is called from the ELS ring event
12290  * handler with no lock held. This function frees memory resources
12291  * associated with the abort iocb.
12292  **/
12293 static void
lpfc_sli_abort_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12294 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12295 			struct lpfc_iocbq *rspiocb)
12296 {
12297 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12298 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12299 	u8 cmnd = get_job_cmnd(phba, cmdiocb);
12300 
12301 	if (ulp_status) {
12302 		/*
12303 		 * Assume that the port already completed and returned, or
12304 		 * will return the iocb. Just Log the message.
12305 		 */
12306 		if (phba->sli_rev < LPFC_SLI_REV4) {
12307 			if (cmnd == CMD_ABORT_XRI_CX &&
12308 			    ulp_status == IOSTAT_LOCAL_REJECT &&
12309 			    ulp_word4 == IOERR_ABORT_REQUESTED) {
12310 				goto release_iocb;
12311 			}
12312 		}
12313 
12314 		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
12315 				"0327 Cannot abort els iocb x%px "
12316 				"with io cmd xri %x abort tag : x%x, "
12317 				"abort status %x abort code %x\n",
12318 				cmdiocb, get_job_abtsiotag(phba, cmdiocb),
12319 				(phba->sli_rev == LPFC_SLI_REV4) ?
12320 				get_wqe_reqtag(cmdiocb) :
12321 				cmdiocb->iocb.un.acxri.abortContextTag,
12322 				ulp_status, ulp_word4);
12323 
12324 	}
12325 release_iocb:
12326 	lpfc_sli_release_iocbq(phba, cmdiocb);
12327 	return;
12328 }
12329 
12330 /**
12331  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
12332  * @phba: Pointer to HBA context object.
12333  * @cmdiocb: Pointer to driver command iocb object.
12334  * @rspiocb: Pointer to driver response iocb object.
12335  *
12336  * The function is called from SLI ring event handler with no
12337  * lock held. This function is the completion handler for ELS commands
12338  * which are aborted. The function frees memory resources used for
12339  * the aborted ELS commands.
12340  **/
12341 void
lpfc_ignore_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12342 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12343 		     struct lpfc_iocbq *rspiocb)
12344 {
12345 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12346 	IOCB_t *irsp;
12347 	LPFC_MBOXQ_t *mbox;
12348 	u32 ulp_command, ulp_status, ulp_word4, iotag;
12349 
12350 	ulp_command = get_job_cmnd(phba, cmdiocb);
12351 	ulp_status = get_job_ulpstatus(phba, rspiocb);
12352 	ulp_word4 = get_job_word4(phba, rspiocb);
12353 
12354 	if (phba->sli_rev == LPFC_SLI_REV4) {
12355 		iotag = get_wqe_reqtag(cmdiocb);
12356 	} else {
12357 		irsp = &rspiocb->iocb;
12358 		iotag = irsp->ulpIoTag;
12359 
12360 		/* It is possible a PLOGI_RJT for NPIV ports to get aborted.
12361 		 * The MBX_REG_LOGIN64 mbox command is freed back to the
12362 		 * mbox_mem_pool here.
12363 		 */
12364 		if (cmdiocb->context_un.mbox) {
12365 			mbox = cmdiocb->context_un.mbox;
12366 			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
12367 			cmdiocb->context_un.mbox = NULL;
12368 		}
12369 	}
12370 
12371 	/* ELS cmd tag <ulpIoTag> completes */
12372 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12373 			"0139 Ignoring ELS cmd code x%x completion Data: "
12374 			"x%x x%x x%x x%px\n",
12375 			ulp_command, ulp_status, ulp_word4, iotag,
12376 			cmdiocb->ndlp);
12377 	/*
12378 	 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12379 	 * if exchange is busy.
12380 	 */
12381 	if (ulp_command == CMD_GEN_REQUEST64_CR)
12382 		lpfc_ct_free_iocb(phba, cmdiocb);
12383 	else
12384 		lpfc_els_free_iocb(phba, cmdiocb);
12385 
12386 	lpfc_nlp_put(ndlp);
12387 }
12388 
12389 /**
12390  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
12391  * @phba: Pointer to HBA context object.
12392  * @pring: Pointer to driver SLI ring object.
12393  * @cmdiocb: Pointer to driver command iocb object.
12394  * @cmpl: completion function.
12395  *
12396  * This function issues an abort iocb for the provided command iocb. In case
12397  * of unloading, the abort iocb will not be issued to commands on the ELS
12398  * ring. Instead, the callback function shall be changed to those commands
12399  * so that nothing happens when them finishes. This function is called with
12400  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12401  * when the command iocb is an abort request.
12402  *
12403  **/
12404 int
lpfc_sli_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * cmdiocb,void * cmpl)12405 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12406 			   struct lpfc_iocbq *cmdiocb, void *cmpl)
12407 {
12408 	struct lpfc_vport *vport = cmdiocb->vport;
12409 	struct lpfc_iocbq *abtsiocbp;
12410 	int retval = IOCB_ERROR;
12411 	unsigned long iflags;
12412 	struct lpfc_nodelist *ndlp = NULL;
12413 	u32 ulp_command = get_job_cmnd(phba, cmdiocb);
12414 	u16 ulp_context, iotag;
12415 	bool ia;
12416 
12417 	/*
12418 	 * There are certain command types we don't want to abort.  And we
12419 	 * don't want to abort commands that are already in the process of
12420 	 * being aborted.
12421 	 */
12422 	if (ulp_command == CMD_ABORT_XRI_WQE ||
12423 	    ulp_command == CMD_ABORT_XRI_CN ||
12424 	    ulp_command == CMD_CLOSE_XRI_CN ||
12425 	    cmdiocb->cmd_flag & LPFC_DRIVER_ABORTED)
12426 		return IOCB_ABORTING;
12427 
12428 	if (!pring) {
12429 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12430 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12431 		else
12432 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12433 		return retval;
12434 	}
12435 
12436 	/*
12437 	 * If we're unloading, don't abort iocb on the ELS ring, but change
12438 	 * the callback so that nothing happens when it finishes.
12439 	 */
12440 	if ((vport->load_flag & FC_UNLOADING) &&
12441 	    pring->ringno == LPFC_ELS_RING) {
12442 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12443 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12444 		else
12445 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12446 		return retval;
12447 	}
12448 
12449 	/* issue ABTS for this IOCB based on iotag */
12450 	abtsiocbp = __lpfc_sli_get_iocbq(phba);
12451 	if (abtsiocbp == NULL)
12452 		return IOCB_NORESOURCE;
12453 
12454 	/* This signals the response to set the correct status
12455 	 * before calling the completion handler
12456 	 */
12457 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
12458 
12459 	if (phba->sli_rev == LPFC_SLI_REV4) {
12460 		ulp_context = cmdiocb->sli4_xritag;
12461 		iotag = abtsiocbp->iotag;
12462 	} else {
12463 		iotag = cmdiocb->iocb.ulpIoTag;
12464 		if (pring->ringno == LPFC_ELS_RING) {
12465 			ndlp = cmdiocb->ndlp;
12466 			ulp_context = ndlp->nlp_rpi;
12467 		} else {
12468 			ulp_context = cmdiocb->iocb.ulpContext;
12469 		}
12470 	}
12471 
12472 	if (phba->link_state < LPFC_LINK_UP ||
12473 	    (phba->sli_rev == LPFC_SLI_REV4 &&
12474 	     phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN) ||
12475 	    (phba->link_flag & LS_EXTERNAL_LOOPBACK))
12476 		ia = true;
12477 	else
12478 		ia = false;
12479 
12480 	lpfc_sli_prep_abort_xri(phba, abtsiocbp, ulp_context, iotag,
12481 				cmdiocb->iocb.ulpClass,
12482 				LPFC_WQE_CQ_ID_DEFAULT, ia, false);
12483 
12484 	abtsiocbp->vport = vport;
12485 
12486 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12487 	abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
12488 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
12489 		abtsiocbp->cmd_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);
12490 
12491 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
12492 		abtsiocbp->cmd_flag |= LPFC_IO_FOF;
12493 
12494 	if (cmpl)
12495 		abtsiocbp->cmd_cmpl = cmpl;
12496 	else
12497 		abtsiocbp->cmd_cmpl = lpfc_sli_abort_els_cmpl;
12498 	abtsiocbp->vport = vport;
12499 
12500 	if (phba->sli_rev == LPFC_SLI_REV4) {
12501 		pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12502 		if (unlikely(pring == NULL))
12503 			goto abort_iotag_exit;
12504 		/* Note: both hbalock and ring_lock need to be set here */
12505 		spin_lock_irqsave(&pring->ring_lock, iflags);
12506 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12507 			abtsiocbp, 0);
12508 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
12509 	} else {
12510 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12511 			abtsiocbp, 0);
12512 	}
12513 
12514 abort_iotag_exit:
12515 
12516 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12517 			 "0339 Abort IO XRI x%x, Original iotag x%x, "
12518 			 "abort tag x%x Cmdjob : x%px Abortjob : x%px "
12519 			 "retval x%x\n",
12520 			 ulp_context, (phba->sli_rev == LPFC_SLI_REV4) ?
12521 			 cmdiocb->iotag : iotag, iotag, cmdiocb, abtsiocbp,
12522 			 retval);
12523 	if (retval) {
12524 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
12525 		__lpfc_sli_release_iocbq(phba, abtsiocbp);
12526 	}
12527 
12528 	/*
12529 	 * Caller to this routine should check for IOCB_ERROR
12530 	 * and handle it properly.  This routine no longer removes
12531 	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
12532 	 */
12533 	return retval;
12534 }
12535 
12536 /**
12537  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12538  * @phba: pointer to lpfc HBA data structure.
12539  *
12540  * This routine will abort all pending and outstanding iocbs to an HBA.
12541  **/
12542 void
lpfc_sli_hba_iocb_abort(struct lpfc_hba * phba)12543 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12544 {
12545 	struct lpfc_sli *psli = &phba->sli;
12546 	struct lpfc_sli_ring *pring;
12547 	struct lpfc_queue *qp = NULL;
12548 	int i;
12549 
12550 	if (phba->sli_rev != LPFC_SLI_REV4) {
12551 		for (i = 0; i < psli->num_rings; i++) {
12552 			pring = &psli->sli3_ring[i];
12553 			lpfc_sli_abort_iocb_ring(phba, pring);
12554 		}
12555 		return;
12556 	}
12557 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12558 		pring = qp->pring;
12559 		if (!pring)
12560 			continue;
12561 		lpfc_sli_abort_iocb_ring(phba, pring);
12562 	}
12563 }
12564 
12565 /**
12566  * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts
12567  * @iocbq: Pointer to iocb object.
12568  * @vport: Pointer to driver virtual port object.
12569  *
12570  * This function acts as an iocb filter for functions which abort FCP iocbs.
12571  *
12572  * Return values
12573  * -ENODEV, if a null iocb or vport ptr is encountered
12574  * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as
12575  *          driver already started the abort process, or is an abort iocb itself
12576  * 0, passes criteria for aborting the FCP I/O iocb
12577  **/
12578 static int
lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq * iocbq,struct lpfc_vport * vport)12579 lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,
12580 				     struct lpfc_vport *vport)
12581 {
12582 	u8 ulp_command;
12583 
12584 	/* No null ptr vports */
12585 	if (!iocbq || iocbq->vport != vport)
12586 		return -ENODEV;
12587 
12588 	/* iocb must be for FCP IO, already exists on the TX cmpl queue,
12589 	 * can't be premarked as driver aborted, nor be an ABORT iocb itself
12590 	 */
12591 	ulp_command = get_job_cmnd(vport->phba, iocbq);
12592 	if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12593 	    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ) ||
12594 	    (iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12595 	    (ulp_command == CMD_ABORT_XRI_CN ||
12596 	     ulp_command == CMD_CLOSE_XRI_CN ||
12597 	     ulp_command == CMD_ABORT_XRI_WQE))
12598 		return -EINVAL;
12599 
12600 	return 0;
12601 }
12602 
12603 /**
12604  * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target
12605  * @iocbq: Pointer to driver iocb object.
12606  * @vport: Pointer to driver virtual port object.
12607  * @tgt_id: SCSI ID of the target.
12608  * @lun_id: LUN ID of the scsi device.
12609  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12610  *
12611  * This function acts as an iocb filter for validating a lun/SCSI target/SCSI
12612  * host.
12613  *
12614  * It will return
12615  * 0 if the filtering criteria is met for the given iocb and will return
12616  * 1 if the filtering criteria is not met.
12617  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12618  * given iocb is for the SCSI device specified by vport, tgt_id and
12619  * lun_id parameter.
12620  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
12621  * given iocb is for the SCSI target specified by vport and tgt_id
12622  * parameters.
12623  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12624  * given iocb is for the SCSI host associated with the given vport.
12625  * This function is called with no locks held.
12626  **/
12627 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)12628 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12629 			   uint16_t tgt_id, uint64_t lun_id,
12630 			   lpfc_ctx_cmd ctx_cmd)
12631 {
12632 	struct lpfc_io_buf *lpfc_cmd;
12633 	int rc = 1;
12634 
12635 	lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12636 
12637 	if (lpfc_cmd->pCmd == NULL)
12638 		return rc;
12639 
12640 	switch (ctx_cmd) {
12641 	case LPFC_CTX_LUN:
12642 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12643 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12644 		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
12645 			rc = 0;
12646 		break;
12647 	case LPFC_CTX_TGT:
12648 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12649 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
12650 			rc = 0;
12651 		break;
12652 	case LPFC_CTX_HOST:
12653 		rc = 0;
12654 		break;
12655 	default:
12656 		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
12657 			__func__, ctx_cmd);
12658 		break;
12659 	}
12660 
12661 	return rc;
12662 }
12663 
12664 /**
12665  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
12666  * @vport: Pointer to virtual port.
12667  * @tgt_id: SCSI ID of the target.
12668  * @lun_id: LUN ID of the scsi device.
12669  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12670  *
12671  * This function returns number of FCP commands pending for the vport.
12672  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12673  * commands pending on the vport associated with SCSI device specified
12674  * by tgt_id and lun_id parameters.
12675  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12676  * commands pending on the vport associated with SCSI target specified
12677  * by tgt_id parameter.
12678  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12679  * commands pending on the vport.
12680  * This function returns the number of iocbs which satisfy the filter.
12681  * This function is called without any lock held.
12682  **/
12683 int
lpfc_sli_sum_iocb(struct lpfc_vport * vport,uint16_t tgt_id,uint64_t lun_id,lpfc_ctx_cmd ctx_cmd)12684 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12685 		  lpfc_ctx_cmd ctx_cmd)
12686 {
12687 	struct lpfc_hba *phba = vport->phba;
12688 	struct lpfc_iocbq *iocbq;
12689 	int sum, i;
12690 	unsigned long iflags;
12691 	u8 ulp_command;
12692 
12693 	spin_lock_irqsave(&phba->hbalock, iflags);
12694 	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12695 		iocbq = phba->sli.iocbq_lookup[i];
12696 
12697 		if (!iocbq || iocbq->vport != vport)
12698 			continue;
12699 		if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12700 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ))
12701 			continue;
12702 
12703 		/* Include counting outstanding aborts */
12704 		ulp_command = get_job_cmnd(phba, iocbq);
12705 		if (ulp_command == CMD_ABORT_XRI_CN ||
12706 		    ulp_command == CMD_CLOSE_XRI_CN ||
12707 		    ulp_command == CMD_ABORT_XRI_WQE) {
12708 			sum++;
12709 			continue;
12710 		}
12711 
12712 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12713 					       ctx_cmd) == 0)
12714 			sum++;
12715 	}
12716 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12717 
12718 	return sum;
12719 }
12720 
12721 /**
12722  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12723  * @phba: Pointer to HBA context object
12724  * @cmdiocb: Pointer to command iocb object.
12725  * @rspiocb: Pointer to response iocb object.
12726  *
12727  * This function is called when an aborted FCP iocb completes. This
12728  * function is called by the ring event handler with no lock held.
12729  * This function frees the iocb.
12730  **/
12731 void
lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12732 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12733 			struct lpfc_iocbq *rspiocb)
12734 {
12735 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12736 			"3096 ABORT_XRI_CX completing on rpi x%x "
12737 			"original iotag x%x, abort cmd iotag x%x "
12738 			"status 0x%x, reason 0x%x\n",
12739 			(phba->sli_rev == LPFC_SLI_REV4) ?
12740 			cmdiocb->sli4_xritag :
12741 			cmdiocb->iocb.un.acxri.abortContextTag,
12742 			get_job_abtsiotag(phba, cmdiocb),
12743 			cmdiocb->iotag, get_job_ulpstatus(phba, rspiocb),
12744 			get_job_word4(phba, rspiocb));
12745 	lpfc_sli_release_iocbq(phba, cmdiocb);
12746 	return;
12747 }
12748 
12749 /**
12750  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
12751  * @vport: Pointer to virtual port.
12752  * @tgt_id: SCSI ID of the target.
12753  * @lun_id: LUN ID of the scsi device.
12754  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12755  *
12756  * This function sends an abort command for every SCSI command
12757  * associated with the given virtual port pending on the ring
12758  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12759  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12760  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12761  * followed by lpfc_sli_validate_fcp_iocb.
12762  *
12763  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12764  * FCP iocbs associated with lun specified by tgt_id and lun_id
12765  * parameters
12766  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12767  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12768  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12769  * FCP iocbs associated with virtual port.
12770  * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12771  * lpfc_sli4_calc_ring is used.
12772  * This function returns number of iocbs it failed to abort.
12773  * This function is called with no locks held.
12774  **/
12775 int
lpfc_sli_abort_iocb(struct lpfc_vport * vport,u16 tgt_id,u64 lun_id,lpfc_ctx_cmd abort_cmd)12776 lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12777 		    lpfc_ctx_cmd abort_cmd)
12778 {
12779 	struct lpfc_hba *phba = vport->phba;
12780 	struct lpfc_sli_ring *pring = NULL;
12781 	struct lpfc_iocbq *iocbq;
12782 	int errcnt = 0, ret_val = 0;
12783 	unsigned long iflags;
12784 	int i;
12785 
12786 	/* all I/Os are in process of being flushed */
12787 	if (phba->hba_flag & HBA_IOQ_FLUSH)
12788 		return errcnt;
12789 
12790 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12791 		iocbq = phba->sli.iocbq_lookup[i];
12792 
12793 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12794 			continue;
12795 
12796 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12797 					       abort_cmd) != 0)
12798 			continue;
12799 
12800 		spin_lock_irqsave(&phba->hbalock, iflags);
12801 		if (phba->sli_rev == LPFC_SLI_REV3) {
12802 			pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12803 		} else if (phba->sli_rev == LPFC_SLI_REV4) {
12804 			pring = lpfc_sli4_calc_ring(phba, iocbq);
12805 		}
12806 		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
12807 						     lpfc_sli_abort_fcp_cmpl);
12808 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12809 		if (ret_val != IOCB_SUCCESS)
12810 			errcnt++;
12811 	}
12812 
12813 	return errcnt;
12814 }
12815 
12816 /**
12817  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12818  * @vport: Pointer to virtual port.
12819  * @pring: Pointer to driver SLI ring object.
12820  * @tgt_id: SCSI ID of the target.
12821  * @lun_id: LUN ID of the scsi device.
12822  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12823  *
12824  * This function sends an abort command for every SCSI command
12825  * associated with the given virtual port pending on the ring
12826  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12827  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12828  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12829  * followed by lpfc_sli_validate_fcp_iocb.
12830  *
12831  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12832  * FCP iocbs associated with lun specified by tgt_id and lun_id
12833  * parameters
12834  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12835  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12836  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12837  * FCP iocbs associated with virtual port.
12838  * This function returns number of iocbs it aborted .
12839  * This function is called with no locks held right after a taskmgmt
12840  * command is sent.
12841  **/
12842 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)12843 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12844 			uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12845 {
12846 	struct lpfc_hba *phba = vport->phba;
12847 	struct lpfc_io_buf *lpfc_cmd;
12848 	struct lpfc_iocbq *abtsiocbq;
12849 	struct lpfc_nodelist *ndlp = NULL;
12850 	struct lpfc_iocbq *iocbq;
12851 	int sum, i, ret_val;
12852 	unsigned long iflags;
12853 	struct lpfc_sli_ring *pring_s4 = NULL;
12854 	u16 ulp_context, iotag, cqid = LPFC_WQE_CQ_ID_DEFAULT;
12855 	bool ia;
12856 
12857 	spin_lock_irqsave(&phba->hbalock, iflags);
12858 
12859 	/* all I/Os are in process of being flushed */
12860 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
12861 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12862 		return 0;
12863 	}
12864 	sum = 0;
12865 
12866 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12867 		iocbq = phba->sli.iocbq_lookup[i];
12868 
12869 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12870 			continue;
12871 
12872 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12873 					       cmd) != 0)
12874 			continue;
12875 
12876 		/* Guard against IO completion being called at same time */
12877 		lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12878 		spin_lock(&lpfc_cmd->buf_lock);
12879 
12880 		if (!lpfc_cmd->pCmd) {
12881 			spin_unlock(&lpfc_cmd->buf_lock);
12882 			continue;
12883 		}
12884 
12885 		if (phba->sli_rev == LPFC_SLI_REV4) {
12886 			pring_s4 =
12887 			    phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12888 			if (!pring_s4) {
12889 				spin_unlock(&lpfc_cmd->buf_lock);
12890 				continue;
12891 			}
12892 			/* Note: both hbalock and ring_lock must be set here */
12893 			spin_lock(&pring_s4->ring_lock);
12894 		}
12895 
12896 		/*
12897 		 * If the iocbq is already being aborted, don't take a second
12898 		 * action, but do count it.
12899 		 */
12900 		if ((iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12901 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
12902 			if (phba->sli_rev == LPFC_SLI_REV4)
12903 				spin_unlock(&pring_s4->ring_lock);
12904 			spin_unlock(&lpfc_cmd->buf_lock);
12905 			continue;
12906 		}
12907 
12908 		/* issue ABTS for this IOCB based on iotag */
12909 		abtsiocbq = __lpfc_sli_get_iocbq(phba);
12910 		if (!abtsiocbq) {
12911 			if (phba->sli_rev == LPFC_SLI_REV4)
12912 				spin_unlock(&pring_s4->ring_lock);
12913 			spin_unlock(&lpfc_cmd->buf_lock);
12914 			continue;
12915 		}
12916 
12917 		if (phba->sli_rev == LPFC_SLI_REV4) {
12918 			iotag = abtsiocbq->iotag;
12919 			ulp_context = iocbq->sli4_xritag;
12920 			cqid = lpfc_cmd->hdwq->io_cq_map;
12921 		} else {
12922 			iotag = iocbq->iocb.ulpIoTag;
12923 			if (pring->ringno == LPFC_ELS_RING) {
12924 				ndlp = iocbq->ndlp;
12925 				ulp_context = ndlp->nlp_rpi;
12926 			} else {
12927 				ulp_context = iocbq->iocb.ulpContext;
12928 			}
12929 		}
12930 
12931 		ndlp = lpfc_cmd->rdata->pnode;
12932 
12933 		if (lpfc_is_link_up(phba) &&
12934 		    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE) &&
12935 		    !(phba->link_flag & LS_EXTERNAL_LOOPBACK))
12936 			ia = false;
12937 		else
12938 			ia = true;
12939 
12940 		lpfc_sli_prep_abort_xri(phba, abtsiocbq, ulp_context, iotag,
12941 					iocbq->iocb.ulpClass, cqid,
12942 					ia, false);
12943 
12944 		abtsiocbq->vport = vport;
12945 
12946 		/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12947 		abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12948 		if (iocbq->cmd_flag & LPFC_IO_FCP)
12949 			abtsiocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
12950 		if (iocbq->cmd_flag & LPFC_IO_FOF)
12951 			abtsiocbq->cmd_flag |= LPFC_IO_FOF;
12952 
12953 		/* Setup callback routine and issue the command. */
12954 		abtsiocbq->cmd_cmpl = lpfc_sli_abort_fcp_cmpl;
12955 
12956 		/*
12957 		 * Indicate the IO is being aborted by the driver and set
12958 		 * the caller's flag into the aborted IO.
12959 		 */
12960 		iocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
12961 
12962 		if (phba->sli_rev == LPFC_SLI_REV4) {
12963 			ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12964 							abtsiocbq, 0);
12965 			spin_unlock(&pring_s4->ring_lock);
12966 		} else {
12967 			ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12968 							abtsiocbq, 0);
12969 		}
12970 
12971 		spin_unlock(&lpfc_cmd->buf_lock);
12972 
12973 		if (ret_val == IOCB_ERROR)
12974 			__lpfc_sli_release_iocbq(phba, abtsiocbq);
12975 		else
12976 			sum++;
12977 	}
12978 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12979 	return sum;
12980 }
12981 
12982 /**
12983  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
12984  * @phba: Pointer to HBA context object.
12985  * @cmdiocbq: Pointer to command iocb.
12986  * @rspiocbq: Pointer to response iocb.
12987  *
12988  * This function is the completion handler for iocbs issued using
12989  * lpfc_sli_issue_iocb_wait function. This function is called by the
12990  * ring event handler function without any lock held. This function
12991  * can be called from both worker thread context and interrupt
12992  * context. This function also can be called from other thread which
12993  * cleans up the SLI layer objects.
12994  * This function copy the contents of the response iocb to the
12995  * response iocb memory object provided by the caller of
12996  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12997  * sleeps for the iocb completion.
12998  **/
12999 static void
lpfc_sli_wake_iocb_wait(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_iocbq * rspiocbq)13000 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
13001 			struct lpfc_iocbq *cmdiocbq,
13002 			struct lpfc_iocbq *rspiocbq)
13003 {
13004 	wait_queue_head_t *pdone_q;
13005 	unsigned long iflags;
13006 	struct lpfc_io_buf *lpfc_cmd;
13007 	size_t offset = offsetof(struct lpfc_iocbq, wqe);
13008 
13009 	spin_lock_irqsave(&phba->hbalock, iflags);
13010 	if (cmdiocbq->cmd_flag & LPFC_IO_WAKE_TMO) {
13011 
13012 		/*
13013 		 * A time out has occurred for the iocb.  If a time out
13014 		 * completion handler has been supplied, call it.  Otherwise,
13015 		 * just free the iocbq.
13016 		 */
13017 
13018 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13019 		cmdiocbq->cmd_cmpl = cmdiocbq->wait_cmd_cmpl;
13020 		cmdiocbq->wait_cmd_cmpl = NULL;
13021 		if (cmdiocbq->cmd_cmpl)
13022 			cmdiocbq->cmd_cmpl(phba, cmdiocbq, NULL);
13023 		else
13024 			lpfc_sli_release_iocbq(phba, cmdiocbq);
13025 		return;
13026 	}
13027 
13028 	/* Copy the contents of the local rspiocb into the caller's buffer. */
13029 	cmdiocbq->cmd_flag |= LPFC_IO_WAKE;
13030 	if (cmdiocbq->rsp_iocb && rspiocbq)
13031 		memcpy((char *)cmdiocbq->rsp_iocb + offset,
13032 		       (char *)rspiocbq + offset, sizeof(*rspiocbq) - offset);
13033 
13034 	/* Set the exchange busy flag for task management commands */
13035 	if ((cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
13036 	    !(cmdiocbq->cmd_flag & LPFC_IO_LIBDFC)) {
13037 		lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
13038 					cur_iocbq);
13039 		if (rspiocbq && (rspiocbq->cmd_flag & LPFC_EXCHANGE_BUSY))
13040 			lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
13041 		else
13042 			lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
13043 	}
13044 
13045 	pdone_q = cmdiocbq->context_un.wait_queue;
13046 	if (pdone_q)
13047 		wake_up(pdone_q);
13048 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13049 	return;
13050 }
13051 
13052 /**
13053  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
13054  * @phba: Pointer to HBA context object..
13055  * @piocbq: Pointer to command iocb.
13056  * @flag: Flag to test.
13057  *
13058  * This routine grabs the hbalock and then test the cmd_flag to
13059  * see if the passed in flag is set.
13060  * Returns:
13061  * 1 if flag is set.
13062  * 0 if flag is not set.
13063  **/
13064 static int
lpfc_chk_iocb_flg(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq,uint32_t flag)13065 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
13066 		 struct lpfc_iocbq *piocbq, uint32_t flag)
13067 {
13068 	unsigned long iflags;
13069 	int ret;
13070 
13071 	spin_lock_irqsave(&phba->hbalock, iflags);
13072 	ret = piocbq->cmd_flag & flag;
13073 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13074 	return ret;
13075 
13076 }
13077 
13078 /**
13079  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
13080  * @phba: Pointer to HBA context object..
13081  * @ring_number: Ring number
13082  * @piocb: Pointer to command iocb.
13083  * @prspiocbq: Pointer to response iocb.
13084  * @timeout: Timeout in number of seconds.
13085  *
13086  * This function issues the iocb to firmware and waits for the
13087  * iocb to complete. The cmd_cmpl field of the shall be used
13088  * to handle iocbs which time out. If the field is NULL, the
13089  * function shall free the iocbq structure.  If more clean up is
13090  * needed, the caller is expected to provide a completion function
13091  * that will provide the needed clean up.  If the iocb command is
13092  * not completed within timeout seconds, the function will either
13093  * free the iocbq structure (if cmd_cmpl == NULL) or execute the
13094  * completion function set in the cmd_cmpl field and then return
13095  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
13096  * resources if this function returns IOCB_TIMEDOUT.
13097  * The function waits for the iocb completion using an
13098  * non-interruptible wait.
13099  * This function will sleep while waiting for iocb completion.
13100  * So, this function should not be called from any context which
13101  * does not allow sleeping. Due to the same reason, this function
13102  * cannot be called with interrupt disabled.
13103  * This function assumes that the iocb completions occur while
13104  * this function sleep. So, this function cannot be called from
13105  * the thread which process iocb completion for this ring.
13106  * This function clears the cmd_flag of the iocb object before
13107  * issuing the iocb and the iocb completion handler sets this
13108  * flag and wakes this thread when the iocb completes.
13109  * The contents of the response iocb will be copied to prspiocbq
13110  * by the completion handler when the command completes.
13111  * This function returns IOCB_SUCCESS when success.
13112  * This function is called with no lock held.
13113  **/
13114 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)13115 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
13116 			 uint32_t ring_number,
13117 			 struct lpfc_iocbq *piocb,
13118 			 struct lpfc_iocbq *prspiocbq,
13119 			 uint32_t timeout)
13120 {
13121 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
13122 	long timeleft, timeout_req = 0;
13123 	int retval = IOCB_SUCCESS;
13124 	uint32_t creg_val;
13125 	struct lpfc_iocbq *iocb;
13126 	int txq_cnt = 0;
13127 	int txcmplq_cnt = 0;
13128 	struct lpfc_sli_ring *pring;
13129 	unsigned long iflags;
13130 	bool iocb_completed = true;
13131 
13132 	if (phba->sli_rev >= LPFC_SLI_REV4) {
13133 		lpfc_sli_prep_wqe(phba, piocb);
13134 
13135 		pring = lpfc_sli4_calc_ring(phba, piocb);
13136 	} else
13137 		pring = &phba->sli.sli3_ring[ring_number];
13138 	/*
13139 	 * If the caller has provided a response iocbq buffer, then rsp_iocb
13140 	 * is NULL or its an error.
13141 	 */
13142 	if (prspiocbq) {
13143 		if (piocb->rsp_iocb)
13144 			return IOCB_ERROR;
13145 		piocb->rsp_iocb = prspiocbq;
13146 	}
13147 
13148 	piocb->wait_cmd_cmpl = piocb->cmd_cmpl;
13149 	piocb->cmd_cmpl = lpfc_sli_wake_iocb_wait;
13150 	piocb->context_un.wait_queue = &done_q;
13151 	piocb->cmd_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
13152 
13153 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13154 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13155 			return IOCB_ERROR;
13156 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13157 		writel(creg_val, phba->HCregaddr);
13158 		readl(phba->HCregaddr); /* flush */
13159 	}
13160 
13161 	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13162 				     SLI_IOCB_RET_IOCB);
13163 	if (retval == IOCB_SUCCESS) {
13164 		timeout_req = msecs_to_jiffies(timeout * 1000);
13165 		timeleft = wait_event_timeout(done_q,
13166 				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
13167 				timeout_req);
13168 		spin_lock_irqsave(&phba->hbalock, iflags);
13169 		if (!(piocb->cmd_flag & LPFC_IO_WAKE)) {
13170 
13171 			/*
13172 			 * IOCB timed out.  Inform the wake iocb wait
13173 			 * completion function and set local status
13174 			 */
13175 
13176 			iocb_completed = false;
13177 			piocb->cmd_flag |= LPFC_IO_WAKE_TMO;
13178 		}
13179 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13180 		if (iocb_completed) {
13181 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13182 					"0331 IOCB wake signaled\n");
13183 			/* Note: we are not indicating if the IOCB has a success
13184 			 * status or not - that's for the caller to check.
13185 			 * IOCB_SUCCESS means just that the command was sent and
13186 			 * completed. Not that it completed successfully.
13187 			 * */
13188 		} else if (timeleft == 0) {
13189 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13190 					"0338 IOCB wait timeout error - no "
13191 					"wake response Data x%x\n", timeout);
13192 			retval = IOCB_TIMEDOUT;
13193 		} else {
13194 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13195 					"0330 IOCB wake NOT set, "
13196 					"Data x%x x%lx\n",
13197 					timeout, (timeleft / jiffies));
13198 			retval = IOCB_TIMEDOUT;
13199 		}
13200 	} else if (retval == IOCB_BUSY) {
13201 		if (phba->cfg_log_verbose & LOG_SLI) {
13202 			list_for_each_entry(iocb, &pring->txq, list) {
13203 				txq_cnt++;
13204 			}
13205 			list_for_each_entry(iocb, &pring->txcmplq, list) {
13206 				txcmplq_cnt++;
13207 			}
13208 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13209 				"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13210 				phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13211 		}
13212 		return retval;
13213 	} else {
13214 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13215 				"0332 IOCB wait issue failed, Data x%x\n",
13216 				retval);
13217 		retval = IOCB_ERROR;
13218 	}
13219 
13220 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13221 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13222 			return IOCB_ERROR;
13223 		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13224 		writel(creg_val, phba->HCregaddr);
13225 		readl(phba->HCregaddr); /* flush */
13226 	}
13227 
13228 	if (prspiocbq)
13229 		piocb->rsp_iocb = NULL;
13230 
13231 	piocb->context_un.wait_queue = NULL;
13232 	piocb->cmd_cmpl = NULL;
13233 	return retval;
13234 }
13235 
13236 /**
13237  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
13238  * @phba: Pointer to HBA context object.
13239  * @pmboxq: Pointer to driver mailbox object.
13240  * @timeout: Timeout in number of seconds.
13241  *
13242  * This function issues the mailbox to firmware and waits for the
13243  * mailbox command to complete. If the mailbox command is not
13244  * completed within timeout seconds, it returns MBX_TIMEOUT.
13245  * The function waits for the mailbox completion using an
13246  * interruptible wait. If the thread is woken up due to a
13247  * signal, MBX_TIMEOUT error is returned to the caller. Caller
13248  * should not free the mailbox resources, if this function returns
13249  * MBX_TIMEOUT.
13250  * This function will sleep while waiting for mailbox completion.
13251  * So, this function should not be called from any context which
13252  * does not allow sleeping. Due to the same reason, this function
13253  * cannot be called with interrupt disabled.
13254  * This function assumes that the mailbox completion occurs while
13255  * this function sleep. So, this function cannot be called from
13256  * the worker thread which processes mailbox completion.
13257  * This function is called in the context of HBA management
13258  * applications.
13259  * This function returns MBX_SUCCESS when successful.
13260  * This function is called with no lock held.
13261  **/
13262 int
lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq,uint32_t timeout)13263 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
13264 			 uint32_t timeout)
13265 {
13266 	struct completion mbox_done;
13267 	int retval;
13268 	unsigned long flag;
13269 
13270 	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
13271 	/* setup wake call as IOCB callback */
13272 	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
13273 
13274 	/* setup context3 field to pass wait_queue pointer to wake function  */
13275 	init_completion(&mbox_done);
13276 	pmboxq->context3 = &mbox_done;
13277 	/* now issue the command */
13278 	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
13279 	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
13280 		wait_for_completion_timeout(&mbox_done,
13281 					    msecs_to_jiffies(timeout * 1000));
13282 
13283 		spin_lock_irqsave(&phba->hbalock, flag);
13284 		pmboxq->context3 = NULL;
13285 		/*
13286 		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13287 		 * else do not free the resources.
13288 		 */
13289 		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
13290 			retval = MBX_SUCCESS;
13291 		} else {
13292 			retval = MBX_TIMEOUT;
13293 			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13294 		}
13295 		spin_unlock_irqrestore(&phba->hbalock, flag);
13296 	}
13297 	return retval;
13298 }
13299 
13300 /**
13301  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
13302  * @phba: Pointer to HBA context.
13303  * @mbx_action: Mailbox shutdown options.
13304  *
13305  * This function is called to shutdown the driver's mailbox sub-system.
13306  * It first marks the mailbox sub-system is in a block state to prevent
13307  * the asynchronous mailbox command from issued off the pending mailbox
13308  * command queue. If the mailbox command sub-system shutdown is due to
13309  * HBA error conditions such as EEH or ERATT, this routine shall invoke
13310  * the mailbox sub-system flush routine to forcefully bring down the
13311  * mailbox sub-system. Otherwise, if it is due to normal condition (such
13312  * as with offline or HBA function reset), this routine will wait for the
13313  * outstanding mailbox command to complete before invoking the mailbox
13314  * sub-system flush routine to gracefully bring down mailbox sub-system.
13315  **/
13316 void
lpfc_sli_mbox_sys_shutdown(struct lpfc_hba * phba,int mbx_action)13317 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
13318 {
13319 	struct lpfc_sli *psli = &phba->sli;
13320 	unsigned long timeout;
13321 
13322 	if (mbx_action == LPFC_MBX_NO_WAIT) {
13323 		/* delay 100ms for port state */
13324 		msleep(100);
13325 		lpfc_sli_mbox_sys_flush(phba);
13326 		return;
13327 	}
13328 	timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
13329 
13330 	/* Disable softirqs, including timers from obtaining phba->hbalock */
13331 	local_bh_disable();
13332 
13333 	spin_lock_irq(&phba->hbalock);
13334 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13335 
13336 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
13337 		/* Determine how long we might wait for the active mailbox
13338 		 * command to be gracefully completed by firmware.
13339 		 */
13340 		if (phba->sli.mbox_active)
13341 			timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
13342 						phba->sli.mbox_active) *
13343 						1000) + jiffies;
13344 		spin_unlock_irq(&phba->hbalock);
13345 
13346 		/* Enable softirqs again, done with phba->hbalock */
13347 		local_bh_enable();
13348 
13349 		while (phba->sli.mbox_active) {
13350 			/* Check active mailbox complete status every 2ms */
13351 			msleep(2);
13352 			if (time_after(jiffies, timeout))
13353 				/* Timeout, let the mailbox flush routine to
13354 				 * forcefully release active mailbox command
13355 				 */
13356 				break;
13357 		}
13358 	} else {
13359 		spin_unlock_irq(&phba->hbalock);
13360 
13361 		/* Enable softirqs again, done with phba->hbalock */
13362 		local_bh_enable();
13363 	}
13364 
13365 	lpfc_sli_mbox_sys_flush(phba);
13366 }
13367 
13368 /**
13369  * lpfc_sli_eratt_read - read sli-3 error attention events
13370  * @phba: Pointer to HBA context.
13371  *
13372  * This function is called to read the SLI3 device error attention registers
13373  * for possible error attention events. The caller must hold the hostlock
13374  * with spin_lock_irq().
13375  *
13376  * This function returns 1 when there is Error Attention in the Host Attention
13377  * Register and returns 0 otherwise.
13378  **/
13379 static int
lpfc_sli_eratt_read(struct lpfc_hba * phba)13380 lpfc_sli_eratt_read(struct lpfc_hba *phba)
13381 {
13382 	uint32_t ha_copy;
13383 
13384 	/* Read chip Host Attention (HA) register */
13385 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
13386 		goto unplug_err;
13387 
13388 	if (ha_copy & HA_ERATT) {
13389 		/* Read host status register to retrieve error event */
13390 		if (lpfc_sli_read_hs(phba))
13391 			goto unplug_err;
13392 
13393 		/* Check if there is a deferred error condition is active */
13394 		if ((HS_FFER1 & phba->work_hs) &&
13395 		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13396 		      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
13397 			phba->hba_flag |= DEFER_ERATT;
13398 			/* Clear all interrupt enable conditions */
13399 			writel(0, phba->HCregaddr);
13400 			readl(phba->HCregaddr);
13401 		}
13402 
13403 		/* Set the driver HA work bitmap */
13404 		phba->work_ha |= HA_ERATT;
13405 		/* Indicate polling handles this ERATT */
13406 		phba->hba_flag |= HBA_ERATT_HANDLED;
13407 		return 1;
13408 	}
13409 	return 0;
13410 
13411 unplug_err:
13412 	/* Set the driver HS work bitmap */
13413 	phba->work_hs |= UNPLUG_ERR;
13414 	/* Set the driver HA work bitmap */
13415 	phba->work_ha |= HA_ERATT;
13416 	/* Indicate polling handles this ERATT */
13417 	phba->hba_flag |= HBA_ERATT_HANDLED;
13418 	return 1;
13419 }
13420 
13421 /**
13422  * lpfc_sli4_eratt_read - read sli-4 error attention events
13423  * @phba: Pointer to HBA context.
13424  *
13425  * This function is called to read the SLI4 device error attention registers
13426  * for possible error attention events. The caller must hold the hostlock
13427  * with spin_lock_irq().
13428  *
13429  * This function returns 1 when there is Error Attention in the Host Attention
13430  * Register and returns 0 otherwise.
13431  **/
13432 static int
lpfc_sli4_eratt_read(struct lpfc_hba * phba)13433 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13434 {
13435 	uint32_t uerr_sta_hi, uerr_sta_lo;
13436 	uint32_t if_type, portsmphr;
13437 	struct lpfc_register portstat_reg;
13438 	u32 logmask;
13439 
13440 	/*
13441 	 * For now, use the SLI4 device internal unrecoverable error
13442 	 * registers for error attention. This can be changed later.
13443 	 */
13444 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13445 	switch (if_type) {
13446 	case LPFC_SLI_INTF_IF_TYPE_0:
13447 		if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13448 			&uerr_sta_lo) ||
13449 			lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13450 			&uerr_sta_hi)) {
13451 			phba->work_hs |= UNPLUG_ERR;
13452 			phba->work_ha |= HA_ERATT;
13453 			phba->hba_flag |= HBA_ERATT_HANDLED;
13454 			return 1;
13455 		}
13456 		if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13457 		    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
13458 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13459 					"1423 HBA Unrecoverable error: "
13460 					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13461 					"ue_mask_lo_reg=0x%x, "
13462 					"ue_mask_hi_reg=0x%x\n",
13463 					uerr_sta_lo, uerr_sta_hi,
13464 					phba->sli4_hba.ue_mask_lo,
13465 					phba->sli4_hba.ue_mask_hi);
13466 			phba->work_status[0] = uerr_sta_lo;
13467 			phba->work_status[1] = uerr_sta_hi;
13468 			phba->work_ha |= HA_ERATT;
13469 			phba->hba_flag |= HBA_ERATT_HANDLED;
13470 			return 1;
13471 		}
13472 		break;
13473 	case LPFC_SLI_INTF_IF_TYPE_2:
13474 	case LPFC_SLI_INTF_IF_TYPE_6:
13475 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13476 			&portstat_reg.word0) ||
13477 			lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13478 			&portsmphr)){
13479 			phba->work_hs |= UNPLUG_ERR;
13480 			phba->work_ha |= HA_ERATT;
13481 			phba->hba_flag |= HBA_ERATT_HANDLED;
13482 			return 1;
13483 		}
13484 		if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13485 			phba->work_status[0] =
13486 				readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13487 			phba->work_status[1] =
13488 				readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
13489 			logmask = LOG_TRACE_EVENT;
13490 			if (phba->work_status[0] ==
13491 				SLIPORT_ERR1_REG_ERR_CODE_2 &&
13492 			    phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)
13493 				logmask = LOG_SLI;
13494 			lpfc_printf_log(phba, KERN_ERR, logmask,
13495 					"2885 Port Status Event: "
13496 					"port status reg 0x%x, "
13497 					"port smphr reg 0x%x, "
13498 					"error 1=0x%x, error 2=0x%x\n",
13499 					portstat_reg.word0,
13500 					portsmphr,
13501 					phba->work_status[0],
13502 					phba->work_status[1]);
13503 			phba->work_ha |= HA_ERATT;
13504 			phba->hba_flag |= HBA_ERATT_HANDLED;
13505 			return 1;
13506 		}
13507 		break;
13508 	case LPFC_SLI_INTF_IF_TYPE_1:
13509 	default:
13510 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13511 				"2886 HBA Error Attention on unsupported "
13512 				"if type %d.", if_type);
13513 		return 1;
13514 	}
13515 
13516 	return 0;
13517 }
13518 
13519 /**
13520  * lpfc_sli_check_eratt - check error attention events
13521  * @phba: Pointer to HBA context.
13522  *
13523  * This function is called from timer soft interrupt context to check HBA's
13524  * error attention register bit for error attention events.
13525  *
13526  * This function returns 1 when there is Error Attention in the Host Attention
13527  * Register and returns 0 otherwise.
13528  **/
13529 int
lpfc_sli_check_eratt(struct lpfc_hba * phba)13530 lpfc_sli_check_eratt(struct lpfc_hba *phba)
13531 {
13532 	uint32_t ha_copy;
13533 
13534 	/* If somebody is waiting to handle an eratt, don't process it
13535 	 * here. The brdkill function will do this.
13536 	 */
13537 	if (phba->link_flag & LS_IGNORE_ERATT)
13538 		return 0;
13539 
13540 	/* Check if interrupt handler handles this ERATT */
13541 	spin_lock_irq(&phba->hbalock);
13542 	if (phba->hba_flag & HBA_ERATT_HANDLED) {
13543 		/* Interrupt handler has handled ERATT */
13544 		spin_unlock_irq(&phba->hbalock);
13545 		return 0;
13546 	}
13547 
13548 	/*
13549 	 * If there is deferred error attention, do not check for error
13550 	 * attention
13551 	 */
13552 	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13553 		spin_unlock_irq(&phba->hbalock);
13554 		return 0;
13555 	}
13556 
13557 	/* If PCI channel is offline, don't process it */
13558 	if (unlikely(pci_channel_offline(phba->pcidev))) {
13559 		spin_unlock_irq(&phba->hbalock);
13560 		return 0;
13561 	}
13562 
13563 	switch (phba->sli_rev) {
13564 	case LPFC_SLI_REV2:
13565 	case LPFC_SLI_REV3:
13566 		/* Read chip Host Attention (HA) register */
13567 		ha_copy = lpfc_sli_eratt_read(phba);
13568 		break;
13569 	case LPFC_SLI_REV4:
13570 		/* Read device Uncoverable Error (UERR) registers */
13571 		ha_copy = lpfc_sli4_eratt_read(phba);
13572 		break;
13573 	default:
13574 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13575 				"0299 Invalid SLI revision (%d)\n",
13576 				phba->sli_rev);
13577 		ha_copy = 0;
13578 		break;
13579 	}
13580 	spin_unlock_irq(&phba->hbalock);
13581 
13582 	return ha_copy;
13583 }
13584 
13585 /**
13586  * lpfc_intr_state_check - Check device state for interrupt handling
13587  * @phba: Pointer to HBA context.
13588  *
13589  * This inline routine checks whether a device or its PCI slot is in a state
13590  * that the interrupt should be handled.
13591  *
13592  * This function returns 0 if the device or the PCI slot is in a state that
13593  * interrupt should be handled, otherwise -EIO.
13594  */
13595 static inline int
lpfc_intr_state_check(struct lpfc_hba * phba)13596 lpfc_intr_state_check(struct lpfc_hba *phba)
13597 {
13598 	/* If the pci channel is offline, ignore all the interrupts */
13599 	if (unlikely(pci_channel_offline(phba->pcidev)))
13600 		return -EIO;
13601 
13602 	/* Update device level interrupt statistics */
13603 	phba->sli.slistat.sli_intr++;
13604 
13605 	/* Ignore all interrupts during initialization. */
13606 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13607 		return -EIO;
13608 
13609 	return 0;
13610 }
13611 
13612 /**
13613  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
13614  * @irq: Interrupt number.
13615  * @dev_id: The device context pointer.
13616  *
13617  * This function is directly called from the PCI layer as an interrupt
13618  * service routine when device with SLI-3 interface spec is enabled with
13619  * MSI-X multi-message interrupt mode and there are slow-path events in
13620  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13621  * interrupt mode, this function is called as part of the device-level
13622  * interrupt handler. When the PCI slot is in error recovery or the HBA
13623  * is undergoing initialization, the interrupt handler will not process
13624  * the interrupt. The link attention and ELS ring attention events are
13625  * handled by the worker thread. The interrupt handler signals the worker
13626  * thread and returns for these events. This function is called without
13627  * any lock held. It gets the hbalock to access and update SLI data
13628  * structures.
13629  *
13630  * This function returns IRQ_HANDLED when interrupt is handled else it
13631  * returns IRQ_NONE.
13632  **/
13633 irqreturn_t
lpfc_sli_sp_intr_handler(int irq,void * dev_id)13634 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
13635 {
13636 	struct lpfc_hba  *phba;
13637 	uint32_t ha_copy, hc_copy;
13638 	uint32_t work_ha_copy;
13639 	unsigned long status;
13640 	unsigned long iflag;
13641 	uint32_t control;
13642 
13643 	MAILBOX_t *mbox, *pmbox;
13644 	struct lpfc_vport *vport;
13645 	struct lpfc_nodelist *ndlp;
13646 	struct lpfc_dmabuf *mp;
13647 	LPFC_MBOXQ_t *pmb;
13648 	int rc;
13649 
13650 	/*
13651 	 * Get the driver's phba structure from the dev_id and
13652 	 * assume the HBA is not interrupting.
13653 	 */
13654 	phba = (struct lpfc_hba *)dev_id;
13655 
13656 	if (unlikely(!phba))
13657 		return IRQ_NONE;
13658 
13659 	/*
13660 	 * Stuff needs to be attented to when this function is invoked as an
13661 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13662 	 */
13663 	if (phba->intr_type == MSIX) {
13664 		/* Check device state for handling interrupt */
13665 		if (lpfc_intr_state_check(phba))
13666 			return IRQ_NONE;
13667 		/* Need to read HA REG for slow-path events */
13668 		spin_lock_irqsave(&phba->hbalock, iflag);
13669 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13670 			goto unplug_error;
13671 		/* If somebody is waiting to handle an eratt don't process it
13672 		 * here. The brdkill function will do this.
13673 		 */
13674 		if (phba->link_flag & LS_IGNORE_ERATT)
13675 			ha_copy &= ~HA_ERATT;
13676 		/* Check the need for handling ERATT in interrupt handler */
13677 		if (ha_copy & HA_ERATT) {
13678 			if (phba->hba_flag & HBA_ERATT_HANDLED)
13679 				/* ERATT polling has handled ERATT */
13680 				ha_copy &= ~HA_ERATT;
13681 			else
13682 				/* Indicate interrupt handler handles ERATT */
13683 				phba->hba_flag |= HBA_ERATT_HANDLED;
13684 		}
13685 
13686 		/*
13687 		 * If there is deferred error attention, do not check for any
13688 		 * interrupt.
13689 		 */
13690 		if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13691 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13692 			return IRQ_NONE;
13693 		}
13694 
13695 		/* Clear up only attention source related to slow-path */
13696 		if (lpfc_readl(phba->HCregaddr, &hc_copy))
13697 			goto unplug_error;
13698 
13699 		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13700 			HC_LAINT_ENA | HC_ERINT_ENA),
13701 			phba->HCregaddr);
13702 		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13703 			phba->HAregaddr);
13704 		writel(hc_copy, phba->HCregaddr);
13705 		readl(phba->HAregaddr); /* flush */
13706 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13707 	} else
13708 		ha_copy = phba->ha_copy;
13709 
13710 	work_ha_copy = ha_copy & phba->work_ha_mask;
13711 
13712 	if (work_ha_copy) {
13713 		if (work_ha_copy & HA_LATT) {
13714 			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13715 				/*
13716 				 * Turn off Link Attention interrupts
13717 				 * until CLEAR_LA done
13718 				 */
13719 				spin_lock_irqsave(&phba->hbalock, iflag);
13720 				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
13721 				if (lpfc_readl(phba->HCregaddr, &control))
13722 					goto unplug_error;
13723 				control &= ~HC_LAINT_ENA;
13724 				writel(control, phba->HCregaddr);
13725 				readl(phba->HCregaddr); /* flush */
13726 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13727 			}
13728 			else
13729 				work_ha_copy &= ~HA_LATT;
13730 		}
13731 
13732 		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
13733 			/*
13734 			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13735 			 * the only slow ring.
13736 			 */
13737 			status = (work_ha_copy &
13738 				(HA_RXMASK  << (4*LPFC_ELS_RING)));
13739 			status >>= (4*LPFC_ELS_RING);
13740 			if (status & HA_RXMASK) {
13741 				spin_lock_irqsave(&phba->hbalock, iflag);
13742 				if (lpfc_readl(phba->HCregaddr, &control))
13743 					goto unplug_error;
13744 
13745 				lpfc_debugfs_slow_ring_trc(phba,
13746 				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
13747 				control, status,
13748 				(uint32_t)phba->sli.slistat.sli_intr);
13749 
13750 				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
13751 					lpfc_debugfs_slow_ring_trc(phba,
13752 						"ISR Disable ring:"
13753 						"pwork:x%x hawork:x%x wait:x%x",
13754 						phba->work_ha, work_ha_copy,
13755 						(uint32_t)((unsigned long)
13756 						&phba->work_waitq));
13757 
13758 					control &=
13759 					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
13760 					writel(control, phba->HCregaddr);
13761 					readl(phba->HCregaddr); /* flush */
13762 				}
13763 				else {
13764 					lpfc_debugfs_slow_ring_trc(phba,
13765 						"ISR slow ring:   pwork:"
13766 						"x%x hawork:x%x wait:x%x",
13767 						phba->work_ha, work_ha_copy,
13768 						(uint32_t)((unsigned long)
13769 						&phba->work_waitq));
13770 				}
13771 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13772 			}
13773 		}
13774 		spin_lock_irqsave(&phba->hbalock, iflag);
13775 		if (work_ha_copy & HA_ERATT) {
13776 			if (lpfc_sli_read_hs(phba))
13777 				goto unplug_error;
13778 			/*
13779 			 * Check if there is a deferred error condition
13780 			 * is active
13781 			 */
13782 			if ((HS_FFER1 & phba->work_hs) &&
13783 				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13784 				  HS_FFER6 | HS_FFER7 | HS_FFER8) &
13785 				  phba->work_hs)) {
13786 				phba->hba_flag |= DEFER_ERATT;
13787 				/* Clear all interrupt enable conditions */
13788 				writel(0, phba->HCregaddr);
13789 				readl(phba->HCregaddr);
13790 			}
13791 		}
13792 
13793 		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
13794 			pmb = phba->sli.mbox_active;
13795 			pmbox = &pmb->u.mb;
13796 			mbox = phba->mbox;
13797 			vport = pmb->vport;
13798 
13799 			/* First check out the status word */
13800 			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13801 			if (pmbox->mbxOwner != OWN_HOST) {
13802 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13803 				/*
13804 				 * Stray Mailbox Interrupt, mbxCommand <cmd>
13805 				 * mbxStatus <status>
13806 				 */
13807 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13808 						"(%d):0304 Stray Mailbox "
13809 						"Interrupt mbxCommand x%x "
13810 						"mbxStatus x%x\n",
13811 						(vport ? vport->vpi : 0),
13812 						pmbox->mbxCommand,
13813 						pmbox->mbxStatus);
13814 				/* clear mailbox attention bit */
13815 				work_ha_copy &= ~HA_MBATT;
13816 			} else {
13817 				phba->sli.mbox_active = NULL;
13818 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13819 				phba->last_completion_time = jiffies;
13820 				del_timer(&phba->sli.mbox_tmo);
13821 				if (pmb->mbox_cmpl) {
13822 					lpfc_sli_pcimem_bcopy(mbox, pmbox,
13823 							MAILBOX_CMD_SIZE);
13824 					if (pmb->out_ext_byte_len &&
13825 						pmb->ctx_buf)
13826 						lpfc_sli_pcimem_bcopy(
13827 						phba->mbox_ext,
13828 						pmb->ctx_buf,
13829 						pmb->out_ext_byte_len);
13830 				}
13831 				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13832 					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13833 
13834 					lpfc_debugfs_disc_trc(vport,
13835 						LPFC_DISC_TRC_MBOX_VPORT,
13836 						"MBOX dflt rpi: : "
13837 						"status:x%x rpi:x%x",
13838 						(uint32_t)pmbox->mbxStatus,
13839 						pmbox->un.varWords[0], 0);
13840 
13841 					if (!pmbox->mbxStatus) {
13842 						mp = (struct lpfc_dmabuf *)
13843 							(pmb->ctx_buf);
13844 						ndlp = (struct lpfc_nodelist *)
13845 							pmb->ctx_ndlp;
13846 
13847 						/* Reg_LOGIN of dflt RPI was
13848 						 * successful. new lets get
13849 						 * rid of the RPI using the
13850 						 * same mbox buffer.
13851 						 */
13852 						lpfc_unreg_login(phba,
13853 							vport->vpi,
13854 							pmbox->un.varWords[0],
13855 							pmb);
13856 						pmb->mbox_cmpl =
13857 							lpfc_mbx_cmpl_dflt_rpi;
13858 						pmb->ctx_buf = mp;
13859 						pmb->ctx_ndlp = ndlp;
13860 						pmb->vport = vport;
13861 						rc = lpfc_sli_issue_mbox(phba,
13862 								pmb,
13863 								MBX_NOWAIT);
13864 						if (rc != MBX_BUSY)
13865 							lpfc_printf_log(phba,
13866 							KERN_ERR,
13867 							LOG_TRACE_EVENT,
13868 							"0350 rc should have"
13869 							"been MBX_BUSY\n");
13870 						if (rc != MBX_NOT_FINISHED)
13871 							goto send_current_mbox;
13872 					}
13873 				}
13874 				spin_lock_irqsave(
13875 						&phba->pport->work_port_lock,
13876 						iflag);
13877 				phba->pport->work_port_events &=
13878 					~WORKER_MBOX_TMO;
13879 				spin_unlock_irqrestore(
13880 						&phba->pport->work_port_lock,
13881 						iflag);
13882 
13883 				/* Do NOT queue MBX_HEARTBEAT to the worker
13884 				 * thread for processing.
13885 				 */
13886 				if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13887 					/* Process mbox now */
13888 					phba->sli.mbox_active = NULL;
13889 					phba->sli.sli_flag &=
13890 						~LPFC_SLI_MBOX_ACTIVE;
13891 					if (pmb->mbox_cmpl)
13892 						pmb->mbox_cmpl(phba, pmb);
13893 				} else {
13894 					/* Queue to worker thread to process */
13895 					lpfc_mbox_cmpl_put(phba, pmb);
13896 				}
13897 			}
13898 		} else
13899 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13900 
13901 		if ((work_ha_copy & HA_MBATT) &&
13902 		    (phba->sli.mbox_active == NULL)) {
13903 send_current_mbox:
13904 			/* Process next mailbox command if there is one */
13905 			do {
13906 				rc = lpfc_sli_issue_mbox(phba, NULL,
13907 							 MBX_NOWAIT);
13908 			} while (rc == MBX_NOT_FINISHED);
13909 			if (rc != MBX_SUCCESS)
13910 				lpfc_printf_log(phba, KERN_ERR,
13911 						LOG_TRACE_EVENT,
13912 						"0349 rc should be "
13913 						"MBX_SUCCESS\n");
13914 		}
13915 
13916 		spin_lock_irqsave(&phba->hbalock, iflag);
13917 		phba->work_ha |= work_ha_copy;
13918 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13919 		lpfc_worker_wake_up(phba);
13920 	}
13921 	return IRQ_HANDLED;
13922 unplug_error:
13923 	spin_unlock_irqrestore(&phba->hbalock, iflag);
13924 	return IRQ_HANDLED;
13925 
13926 } /* lpfc_sli_sp_intr_handler */
13927 
13928 /**
13929  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13930  * @irq: Interrupt number.
13931  * @dev_id: The device context pointer.
13932  *
13933  * This function is directly called from the PCI layer as an interrupt
13934  * service routine when device with SLI-3 interface spec is enabled with
13935  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13936  * ring event in the HBA. However, when the device is enabled with either
13937  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13938  * device-level interrupt handler. When the PCI slot is in error recovery
13939  * or the HBA is undergoing initialization, the interrupt handler will not
13940  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13941  * the intrrupt context. This function is called without any lock held.
13942  * It gets the hbalock to access and update SLI data structures.
13943  *
13944  * This function returns IRQ_HANDLED when interrupt is handled else it
13945  * returns IRQ_NONE.
13946  **/
13947 irqreturn_t
lpfc_sli_fp_intr_handler(int irq,void * dev_id)13948 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13949 {
13950 	struct lpfc_hba  *phba;
13951 	uint32_t ha_copy;
13952 	unsigned long status;
13953 	unsigned long iflag;
13954 	struct lpfc_sli_ring *pring;
13955 
13956 	/* Get the driver's phba structure from the dev_id and
13957 	 * assume the HBA is not interrupting.
13958 	 */
13959 	phba = (struct lpfc_hba *) dev_id;
13960 
13961 	if (unlikely(!phba))
13962 		return IRQ_NONE;
13963 
13964 	/*
13965 	 * Stuff needs to be attented to when this function is invoked as an
13966 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13967 	 */
13968 	if (phba->intr_type == MSIX) {
13969 		/* Check device state for handling interrupt */
13970 		if (lpfc_intr_state_check(phba))
13971 			return IRQ_NONE;
13972 		/* Need to read HA REG for FCP ring and other ring events */
13973 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13974 			return IRQ_HANDLED;
13975 		/* Clear up only attention source related to fast-path */
13976 		spin_lock_irqsave(&phba->hbalock, iflag);
13977 		/*
13978 		 * If there is deferred error attention, do not check for
13979 		 * any interrupt.
13980 		 */
13981 		if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13982 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13983 			return IRQ_NONE;
13984 		}
13985 		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13986 			phba->HAregaddr);
13987 		readl(phba->HAregaddr); /* flush */
13988 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13989 	} else
13990 		ha_copy = phba->ha_copy;
13991 
13992 	/*
13993 	 * Process all events on FCP ring. Take the optimized path for FCP IO.
13994 	 */
13995 	ha_copy &= ~(phba->work_ha_mask);
13996 
13997 	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13998 	status >>= (4*LPFC_FCP_RING);
13999 	pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
14000 	if (status & HA_RXMASK)
14001 		lpfc_sli_handle_fast_ring_event(phba, pring, status);
14002 
14003 	if (phba->cfg_multi_ring_support == 2) {
14004 		/*
14005 		 * Process all events on extra ring. Take the optimized path
14006 		 * for extra ring IO.
14007 		 */
14008 		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14009 		status >>= (4*LPFC_EXTRA_RING);
14010 		if (status & HA_RXMASK) {
14011 			lpfc_sli_handle_fast_ring_event(phba,
14012 					&phba->sli.sli3_ring[LPFC_EXTRA_RING],
14013 					status);
14014 		}
14015 	}
14016 	return IRQ_HANDLED;
14017 }  /* lpfc_sli_fp_intr_handler */
14018 
14019 /**
14020  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
14021  * @irq: Interrupt number.
14022  * @dev_id: The device context pointer.
14023  *
14024  * This function is the HBA device-level interrupt handler to device with
14025  * SLI-3 interface spec, called from the PCI layer when either MSI or
14026  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
14027  * requires driver attention. This function invokes the slow-path interrupt
14028  * attention handling function and fast-path interrupt attention handling
14029  * function in turn to process the relevant HBA attention events. This
14030  * function is called without any lock held. It gets the hbalock to access
14031  * and update SLI data structures.
14032  *
14033  * This function returns IRQ_HANDLED when interrupt is handled, else it
14034  * returns IRQ_NONE.
14035  **/
14036 irqreturn_t
lpfc_sli_intr_handler(int irq,void * dev_id)14037 lpfc_sli_intr_handler(int irq, void *dev_id)
14038 {
14039 	struct lpfc_hba  *phba;
14040 	irqreturn_t sp_irq_rc, fp_irq_rc;
14041 	unsigned long status1, status2;
14042 	uint32_t hc_copy;
14043 
14044 	/*
14045 	 * Get the driver's phba structure from the dev_id and
14046 	 * assume the HBA is not interrupting.
14047 	 */
14048 	phba = (struct lpfc_hba *) dev_id;
14049 
14050 	if (unlikely(!phba))
14051 		return IRQ_NONE;
14052 
14053 	/* Check device state for handling interrupt */
14054 	if (lpfc_intr_state_check(phba))
14055 		return IRQ_NONE;
14056 
14057 	spin_lock(&phba->hbalock);
14058 	if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
14059 		spin_unlock(&phba->hbalock);
14060 		return IRQ_HANDLED;
14061 	}
14062 
14063 	if (unlikely(!phba->ha_copy)) {
14064 		spin_unlock(&phba->hbalock);
14065 		return IRQ_NONE;
14066 	} else if (phba->ha_copy & HA_ERATT) {
14067 		if (phba->hba_flag & HBA_ERATT_HANDLED)
14068 			/* ERATT polling has handled ERATT */
14069 			phba->ha_copy &= ~HA_ERATT;
14070 		else
14071 			/* Indicate interrupt handler handles ERATT */
14072 			phba->hba_flag |= HBA_ERATT_HANDLED;
14073 	}
14074 
14075 	/*
14076 	 * If there is deferred error attention, do not check for any interrupt.
14077 	 */
14078 	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
14079 		spin_unlock(&phba->hbalock);
14080 		return IRQ_NONE;
14081 	}
14082 
14083 	/* Clear attention sources except link and error attentions */
14084 	if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
14085 		spin_unlock(&phba->hbalock);
14086 		return IRQ_HANDLED;
14087 	}
14088 	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
14089 		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
14090 		phba->HCregaddr);
14091 	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
14092 	writel(hc_copy, phba->HCregaddr);
14093 	readl(phba->HAregaddr); /* flush */
14094 	spin_unlock(&phba->hbalock);
14095 
14096 	/*
14097 	 * Invokes slow-path host attention interrupt handling as appropriate.
14098 	 */
14099 
14100 	/* status of events with mailbox and link attention */
14101 	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
14102 
14103 	/* status of events with ELS ring */
14104 	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
14105 	status2 >>= (4*LPFC_ELS_RING);
14106 
14107 	if (status1 || (status2 & HA_RXMASK))
14108 		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
14109 	else
14110 		sp_irq_rc = IRQ_NONE;
14111 
14112 	/*
14113 	 * Invoke fast-path host attention interrupt handling as appropriate.
14114 	 */
14115 
14116 	/* status of events with FCP ring */
14117 	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14118 	status1 >>= (4*LPFC_FCP_RING);
14119 
14120 	/* status of events with extra ring */
14121 	if (phba->cfg_multi_ring_support == 2) {
14122 		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14123 		status2 >>= (4*LPFC_EXTRA_RING);
14124 	} else
14125 		status2 = 0;
14126 
14127 	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
14128 		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
14129 	else
14130 		fp_irq_rc = IRQ_NONE;
14131 
14132 	/* Return device-level interrupt handling status */
14133 	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
14134 }  /* lpfc_sli_intr_handler */
14135 
14136 /**
14137  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14138  * @phba: pointer to lpfc hba data structure.
14139  *
14140  * This routine is invoked by the worker thread to process all the pending
14141  * SLI4 els abort xri events.
14142  **/
lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba * phba)14143 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14144 {
14145 	struct lpfc_cq_event *cq_event;
14146 	unsigned long iflags;
14147 
14148 	/* First, declare the els xri abort event has been handled */
14149 	spin_lock_irqsave(&phba->hbalock, iflags);
14150 	phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
14151 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14152 
14153 	/* Now, handle all the els xri abort events */
14154 	spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14155 	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14156 		/* Get the first event from the head of the event queue */
14157 		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14158 				 cq_event, struct lpfc_cq_event, list);
14159 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14160 				       iflags);
14161 		/* Notify aborted XRI for ELS work queue */
14162 		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
14163 
14164 		/* Free the event processed back to the free pool */
14165 		lpfc_sli4_cq_event_release(phba, cq_event);
14166 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14167 				  iflags);
14168 	}
14169 	spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14170 }
14171 
14172 /**
14173  * lpfc_sli4_els_preprocess_rspiocbq - Get response iocbq from els wcqe
14174  * @phba: Pointer to HBA context object.
14175  * @irspiocbq: Pointer to work-queue completion queue entry.
14176  *
14177  * This routine handles an ELS work-queue completion event and construct
14178  * a pseudo response ELS IOCBQ from the SLI4 ELS WCQE for the common
14179  * discovery engine to handle.
14180  *
14181  * Return: Pointer to the receive IOCBQ, NULL otherwise.
14182  **/
14183 static struct lpfc_iocbq *
lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba * phba,struct lpfc_iocbq * irspiocbq)14184 lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
14185 				  struct lpfc_iocbq *irspiocbq)
14186 {
14187 	struct lpfc_sli_ring *pring;
14188 	struct lpfc_iocbq *cmdiocbq;
14189 	struct lpfc_wcqe_complete *wcqe;
14190 	unsigned long iflags;
14191 
14192 	pring = lpfc_phba_elsring(phba);
14193 	if (unlikely(!pring))
14194 		return NULL;
14195 
14196 	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
14197 	spin_lock_irqsave(&pring->ring_lock, iflags);
14198 	pring->stats.iocb_event++;
14199 	/* Look up the ELS command IOCB and create pseudo response IOCB */
14200 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14201 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
14202 	if (unlikely(!cmdiocbq)) {
14203 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
14204 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14205 				"0386 ELS complete with no corresponding "
14206 				"cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14207 				wcqe->word0, wcqe->total_data_placed,
14208 				wcqe->parameter, wcqe->word3);
14209 		lpfc_sli_release_iocbq(phba, irspiocbq);
14210 		return NULL;
14211 	}
14212 
14213 	memcpy(&irspiocbq->wqe, &cmdiocbq->wqe, sizeof(union lpfc_wqe128));
14214 	memcpy(&irspiocbq->wcqe_cmpl, wcqe, sizeof(*wcqe));
14215 
14216 	/* Put the iocb back on the txcmplq */
14217 	lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14218 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
14219 
14220 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14221 		spin_lock_irqsave(&phba->hbalock, iflags);
14222 		irspiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
14223 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14224 	}
14225 
14226 	return irspiocbq;
14227 }
14228 
14229 inline struct lpfc_cq_event *
lpfc_cq_event_setup(struct lpfc_hba * phba,void * entry,int size)14230 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14231 {
14232 	struct lpfc_cq_event *cq_event;
14233 
14234 	/* Allocate a new internal CQ_EVENT entry */
14235 	cq_event = lpfc_sli4_cq_event_alloc(phba);
14236 	if (!cq_event) {
14237 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14238 				"0602 Failed to alloc CQ_EVENT entry\n");
14239 		return NULL;
14240 	}
14241 
14242 	/* Move the CQE into the event */
14243 	memcpy(&cq_event->cqe, entry, size);
14244 	return cq_event;
14245 }
14246 
14247 /**
14248  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
14249  * @phba: Pointer to HBA context object.
14250  * @mcqe: Pointer to mailbox completion queue entry.
14251  *
14252  * This routine process a mailbox completion queue entry with asynchronous
14253  * event.
14254  *
14255  * Return: true if work posted to worker thread, otherwise false.
14256  **/
14257 static bool
lpfc_sli4_sp_handle_async_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14258 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14259 {
14260 	struct lpfc_cq_event *cq_event;
14261 	unsigned long iflags;
14262 
14263 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14264 			"0392 Async Event: word0:x%x, word1:x%x, "
14265 			"word2:x%x, word3:x%x\n", mcqe->word0,
14266 			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14267 
14268 	cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14269 	if (!cq_event)
14270 		return false;
14271 
14272 	spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
14273 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
14274 	spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14275 
14276 	/* Set the async event flag */
14277 	spin_lock_irqsave(&phba->hbalock, iflags);
14278 	phba->hba_flag |= ASYNC_EVENT;
14279 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14280 
14281 	return true;
14282 }
14283 
14284 /**
14285  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14286  * @phba: Pointer to HBA context object.
14287  * @mcqe: Pointer to mailbox completion queue entry.
14288  *
14289  * This routine process a mailbox completion queue entry with mailbox
14290  * completion event.
14291  *
14292  * Return: true if work posted to worker thread, otherwise false.
14293  **/
14294 static bool
lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14295 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14296 {
14297 	uint32_t mcqe_status;
14298 	MAILBOX_t *mbox, *pmbox;
14299 	struct lpfc_mqe *mqe;
14300 	struct lpfc_vport *vport;
14301 	struct lpfc_nodelist *ndlp;
14302 	struct lpfc_dmabuf *mp;
14303 	unsigned long iflags;
14304 	LPFC_MBOXQ_t *pmb;
14305 	bool workposted = false;
14306 	int rc;
14307 
14308 	/* If not a mailbox complete MCQE, out by checking mailbox consume */
14309 	if (!bf_get(lpfc_trailer_completed, mcqe))
14310 		goto out_no_mqe_complete;
14311 
14312 	/* Get the reference to the active mbox command */
14313 	spin_lock_irqsave(&phba->hbalock, iflags);
14314 	pmb = phba->sli.mbox_active;
14315 	if (unlikely(!pmb)) {
14316 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14317 				"1832 No pending MBOX command to handle\n");
14318 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14319 		goto out_no_mqe_complete;
14320 	}
14321 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14322 	mqe = &pmb->u.mqe;
14323 	pmbox = (MAILBOX_t *)&pmb->u.mqe;
14324 	mbox = phba->mbox;
14325 	vport = pmb->vport;
14326 
14327 	/* Reset heartbeat timer */
14328 	phba->last_completion_time = jiffies;
14329 	del_timer(&phba->sli.mbox_tmo);
14330 
14331 	/* Move mbox data to caller's mailbox region, do endian swapping */
14332 	if (pmb->mbox_cmpl && mbox)
14333 		lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
14334 
14335 	/*
14336 	 * For mcqe errors, conditionally move a modified error code to
14337 	 * the mbox so that the error will not be missed.
14338 	 */
14339 	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14340 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14341 		if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14342 			bf_set(lpfc_mqe_status, mqe,
14343 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
14344 	}
14345 	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14346 		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14347 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14348 				      "MBOX dflt rpi: status:x%x rpi:x%x",
14349 				      mcqe_status,
14350 				      pmbox->un.varWords[0], 0);
14351 		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
14352 			mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
14353 			ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
14354 
14355 			/* Reg_LOGIN of dflt RPI was successful. Mark the
14356 			 * node as having an UNREG_LOGIN in progress to stop
14357 			 * an unsolicited PLOGI from the same NPortId from
14358 			 * starting another mailbox transaction.
14359 			 */
14360 			spin_lock_irqsave(&ndlp->lock, iflags);
14361 			ndlp->nlp_flag |= NLP_UNREG_INP;
14362 			spin_unlock_irqrestore(&ndlp->lock, iflags);
14363 			lpfc_unreg_login(phba, vport->vpi,
14364 					 pmbox->un.varWords[0], pmb);
14365 			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
14366 			pmb->ctx_buf = mp;
14367 
14368 			/* No reference taken here.  This is a default
14369 			 * RPI reg/immediate unreg cycle. The reference was
14370 			 * taken in the reg rpi path and is released when
14371 			 * this mailbox completes.
14372 			 */
14373 			pmb->ctx_ndlp = ndlp;
14374 			pmb->vport = vport;
14375 			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14376 			if (rc != MBX_BUSY)
14377 				lpfc_printf_log(phba, KERN_ERR,
14378 						LOG_TRACE_EVENT,
14379 						"0385 rc should "
14380 						"have been MBX_BUSY\n");
14381 			if (rc != MBX_NOT_FINISHED)
14382 				goto send_current_mbox;
14383 		}
14384 	}
14385 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14386 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14387 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14388 
14389 	/* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14390 	if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14391 		spin_lock_irqsave(&phba->hbalock, iflags);
14392 		/* Release the mailbox command posting token */
14393 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14394 		phba->sli.mbox_active = NULL;
14395 		if (bf_get(lpfc_trailer_consumed, mcqe))
14396 			lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14397 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14398 
14399 		/* Post the next mbox command, if there is one */
14400 		lpfc_sli4_post_async_mbox(phba);
14401 
14402 		/* Process cmpl now */
14403 		if (pmb->mbox_cmpl)
14404 			pmb->mbox_cmpl(phba, pmb);
14405 		return false;
14406 	}
14407 
14408 	/* There is mailbox completion work to queue to the worker thread */
14409 	spin_lock_irqsave(&phba->hbalock, iflags);
14410 	__lpfc_mbox_cmpl_put(phba, pmb);
14411 	phba->work_ha |= HA_MBATT;
14412 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14413 	workposted = true;
14414 
14415 send_current_mbox:
14416 	spin_lock_irqsave(&phba->hbalock, iflags);
14417 	/* Release the mailbox command posting token */
14418 	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14419 	/* Setting active mailbox pointer need to be in sync to flag clear */
14420 	phba->sli.mbox_active = NULL;
14421 	if (bf_get(lpfc_trailer_consumed, mcqe))
14422 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14423 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14424 	/* Wake up worker thread to post the next pending mailbox command */
14425 	lpfc_worker_wake_up(phba);
14426 	return workposted;
14427 
14428 out_no_mqe_complete:
14429 	spin_lock_irqsave(&phba->hbalock, iflags);
14430 	if (bf_get(lpfc_trailer_consumed, mcqe))
14431 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14432 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14433 	return false;
14434 }
14435 
14436 /**
14437  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14438  * @phba: Pointer to HBA context object.
14439  * @cq: Pointer to associated CQ
14440  * @cqe: Pointer to mailbox completion queue entry.
14441  *
14442  * This routine process a mailbox completion queue entry, it invokes the
14443  * proper mailbox complete handling or asynchronous event handling routine
14444  * according to the MCQE's async bit.
14445  *
14446  * Return: true if work posted to worker thread, otherwise false.
14447  **/
14448 static bool
lpfc_sli4_sp_handle_mcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14449 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14450 			 struct lpfc_cqe *cqe)
14451 {
14452 	struct lpfc_mcqe mcqe;
14453 	bool workposted;
14454 
14455 	cq->CQ_mbox++;
14456 
14457 	/* Copy the mailbox MCQE and convert endian order as needed */
14458 	lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
14459 
14460 	/* Invoke the proper event handling routine */
14461 	if (!bf_get(lpfc_trailer_async, &mcqe))
14462 		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14463 	else
14464 		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14465 	return workposted;
14466 }
14467 
14468 /**
14469  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14470  * @phba: Pointer to HBA context object.
14471  * @cq: Pointer to associated CQ
14472  * @wcqe: Pointer to work-queue completion queue entry.
14473  *
14474  * This routine handles an ELS work-queue completion event.
14475  *
14476  * Return: true if work posted to worker thread, otherwise false.
14477  **/
14478 static bool
lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)14479 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14480 			     struct lpfc_wcqe_complete *wcqe)
14481 {
14482 	struct lpfc_iocbq *irspiocbq;
14483 	unsigned long iflags;
14484 	struct lpfc_sli_ring *pring = cq->pring;
14485 	int txq_cnt = 0;
14486 	int txcmplq_cnt = 0;
14487 
14488 	/* Check for response status */
14489 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14490 		/* Log the error status */
14491 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14492 				"0357 ELS CQE error: status=x%x: "
14493 				"CQE: %08x %08x %08x %08x\n",
14494 				bf_get(lpfc_wcqe_c_status, wcqe),
14495 				wcqe->word0, wcqe->total_data_placed,
14496 				wcqe->parameter, wcqe->word3);
14497 	}
14498 
14499 	/* Get an irspiocbq for later ELS response processing use */
14500 	irspiocbq = lpfc_sli_get_iocbq(phba);
14501 	if (!irspiocbq) {
14502 		if (!list_empty(&pring->txq))
14503 			txq_cnt++;
14504 		if (!list_empty(&pring->txcmplq))
14505 			txcmplq_cnt++;
14506 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14507 			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
14508 			"els_txcmplq_cnt=%d\n",
14509 			txq_cnt, phba->iocb_cnt,
14510 			txcmplq_cnt);
14511 		return false;
14512 	}
14513 
14514 	/* Save off the slow-path queue event for work thread to process */
14515 	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
14516 	spin_lock_irqsave(&phba->hbalock, iflags);
14517 	list_add_tail(&irspiocbq->cq_event.list,
14518 		      &phba->sli4_hba.sp_queue_event);
14519 	phba->hba_flag |= HBA_SP_QUEUE_EVT;
14520 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14521 
14522 	return true;
14523 }
14524 
14525 /**
14526  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14527  * @phba: Pointer to HBA context object.
14528  * @wcqe: Pointer to work-queue completion queue entry.
14529  *
14530  * This routine handles slow-path WQ entry consumed event by invoking the
14531  * proper WQ release routine to the slow-path WQ.
14532  **/
14533 static void
lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_wcqe_release * wcqe)14534 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14535 			     struct lpfc_wcqe_release *wcqe)
14536 {
14537 	/* sanity check on queue memory */
14538 	if (unlikely(!phba->sli4_hba.els_wq))
14539 		return;
14540 	/* Check for the slow-path ELS work queue */
14541 	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14542 		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14543 				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14544 	else
14545 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14546 				"2579 Slow-path wqe consume event carries "
14547 				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14548 				bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14549 				phba->sli4_hba.els_wq->queue_id);
14550 }
14551 
14552 /**
14553  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14554  * @phba: Pointer to HBA context object.
14555  * @cq: Pointer to a WQ completion queue.
14556  * @wcqe: Pointer to work-queue completion queue entry.
14557  *
14558  * This routine handles an XRI abort event.
14559  *
14560  * Return: true if work posted to worker thread, otherwise false.
14561  **/
14562 static bool
lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct sli4_wcqe_xri_aborted * wcqe)14563 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14564 				   struct lpfc_queue *cq,
14565 				   struct sli4_wcqe_xri_aborted *wcqe)
14566 {
14567 	bool workposted = false;
14568 	struct lpfc_cq_event *cq_event;
14569 	unsigned long iflags;
14570 
14571 	switch (cq->subtype) {
14572 	case LPFC_IO:
14573 		lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14574 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14575 			/* Notify aborted XRI for NVME work queue */
14576 			if (phba->nvmet_support)
14577 				lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14578 		}
14579 		workposted = false;
14580 		break;
14581 	case LPFC_NVME_LS: /* NVME LS uses ELS resources */
14582 	case LPFC_ELS:
14583 		cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14584 		if (!cq_event) {
14585 			workposted = false;
14586 			break;
14587 		}
14588 		cq_event->hdwq = cq->hdwq;
14589 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14590 				  iflags);
14591 		list_add_tail(&cq_event->list,
14592 			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14593 		/* Set the els xri abort event flag */
14594 		phba->hba_flag |= ELS_XRI_ABORT_EVENT;
14595 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14596 				       iflags);
14597 		workposted = true;
14598 		break;
14599 	default:
14600 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14601 				"0603 Invalid CQ subtype %d: "
14602 				"%08x %08x %08x %08x\n",
14603 				cq->subtype, wcqe->word0, wcqe->parameter,
14604 				wcqe->word2, wcqe->word3);
14605 		workposted = false;
14606 		break;
14607 	}
14608 	return workposted;
14609 }
14610 
14611 #define FC_RCTL_MDS_DIAGS	0xF4
14612 
14613 /**
14614  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
14615  * @phba: Pointer to HBA context object.
14616  * @rcqe: Pointer to receive-queue completion queue entry.
14617  *
14618  * This routine process a receive-queue completion queue entry.
14619  *
14620  * Return: true if work posted to worker thread, otherwise false.
14621  **/
14622 static bool
lpfc_sli4_sp_handle_rcqe(struct lpfc_hba * phba,struct lpfc_rcqe * rcqe)14623 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14624 {
14625 	bool workposted = false;
14626 	struct fc_frame_header *fc_hdr;
14627 	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14628 	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
14629 	struct lpfc_nvmet_tgtport *tgtp;
14630 	struct hbq_dmabuf *dma_buf;
14631 	uint32_t status, rq_id;
14632 	unsigned long iflags;
14633 
14634 	/* sanity check on queue memory */
14635 	if (unlikely(!hrq) || unlikely(!drq))
14636 		return workposted;
14637 
14638 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14639 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14640 	else
14641 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14642 	if (rq_id != hrq->queue_id)
14643 		goto out;
14644 
14645 	status = bf_get(lpfc_rcqe_status, rcqe);
14646 	switch (status) {
14647 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14648 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14649 				"2537 Receive Frame Truncated!!\n");
14650 		fallthrough;
14651 	case FC_STATUS_RQ_SUCCESS:
14652 		spin_lock_irqsave(&phba->hbalock, iflags);
14653 		lpfc_sli4_rq_release(hrq, drq);
14654 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14655 		if (!dma_buf) {
14656 			hrq->RQ_no_buf_found++;
14657 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14658 			goto out;
14659 		}
14660 		hrq->RQ_rcv_buf++;
14661 		hrq->RQ_buf_posted--;
14662 		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
14663 
14664 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14665 
14666 		if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14667 		    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14668 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14669 			/* Handle MDS Loopback frames */
14670 			if  (!(phba->pport->load_flag & FC_UNLOADING))
14671 				lpfc_sli4_handle_mds_loopback(phba->pport,
14672 							      dma_buf);
14673 			else
14674 				lpfc_in_buf_free(phba, &dma_buf->dbuf);
14675 			break;
14676 		}
14677 
14678 		/* save off the frame for the work thread to process */
14679 		list_add_tail(&dma_buf->cq_event.list,
14680 			      &phba->sli4_hba.sp_queue_event);
14681 		/* Frame received */
14682 		phba->hba_flag |= HBA_SP_QUEUE_EVT;
14683 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14684 		workposted = true;
14685 		break;
14686 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
14687 		if (phba->nvmet_support) {
14688 			tgtp = phba->targetport->private;
14689 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14690 					"6402 RQE Error x%x, posted %d err_cnt "
14691 					"%d: %x %x %x\n",
14692 					status, hrq->RQ_buf_posted,
14693 					hrq->RQ_no_posted_buf,
14694 					atomic_read(&tgtp->rcv_fcp_cmd_in),
14695 					atomic_read(&tgtp->rcv_fcp_cmd_out),
14696 					atomic_read(&tgtp->xmt_fcp_release));
14697 		}
14698 		fallthrough;
14699 
14700 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
14701 		hrq->RQ_no_posted_buf++;
14702 		/* Post more buffers if possible */
14703 		spin_lock_irqsave(&phba->hbalock, iflags);
14704 		phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
14705 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14706 		workposted = true;
14707 		break;
14708 	case FC_STATUS_RQ_DMA_FAILURE:
14709 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14710 				"2564 RQE DMA Error x%x, x%08x x%08x x%08x "
14711 				"x%08x\n",
14712 				status, rcqe->word0, rcqe->word1,
14713 				rcqe->word2, rcqe->word3);
14714 
14715 		/* If IV set, no further recovery */
14716 		if (bf_get(lpfc_rcqe_iv, rcqe))
14717 			break;
14718 
14719 		/* recycle consumed resource */
14720 		spin_lock_irqsave(&phba->hbalock, iflags);
14721 		lpfc_sli4_rq_release(hrq, drq);
14722 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14723 		if (!dma_buf) {
14724 			hrq->RQ_no_buf_found++;
14725 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14726 			break;
14727 		}
14728 		hrq->RQ_rcv_buf++;
14729 		hrq->RQ_buf_posted--;
14730 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14731 		lpfc_in_buf_free(phba, &dma_buf->dbuf);
14732 		break;
14733 	default:
14734 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14735 				"2565 Unexpected RQE Status x%x, w0-3 x%08x "
14736 				"x%08x x%08x x%08x\n",
14737 				status, rcqe->word0, rcqe->word1,
14738 				rcqe->word2, rcqe->word3);
14739 		break;
14740 	}
14741 out:
14742 	return workposted;
14743 }
14744 
14745 /**
14746  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14747  * @phba: Pointer to HBA context object.
14748  * @cq: Pointer to the completion queue.
14749  * @cqe: Pointer to a completion queue entry.
14750  *
14751  * This routine process a slow-path work-queue or receive queue completion queue
14752  * entry.
14753  *
14754  * Return: true if work posted to worker thread, otherwise false.
14755  **/
14756 static bool
lpfc_sli4_sp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14757 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14758 			 struct lpfc_cqe *cqe)
14759 {
14760 	struct lpfc_cqe cqevt;
14761 	bool workposted = false;
14762 
14763 	/* Copy the work queue CQE and convert endian order if needed */
14764 	lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
14765 
14766 	/* Check and process for different type of WCQE and dispatch */
14767 	switch (bf_get(lpfc_cqe_code, &cqevt)) {
14768 	case CQE_CODE_COMPL_WQE:
14769 		/* Process the WQ/RQ complete event */
14770 		phba->last_completion_time = jiffies;
14771 		workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
14772 				(struct lpfc_wcqe_complete *)&cqevt);
14773 		break;
14774 	case CQE_CODE_RELEASE_WQE:
14775 		/* Process the WQ release event */
14776 		lpfc_sli4_sp_handle_rel_wcqe(phba,
14777 				(struct lpfc_wcqe_release *)&cqevt);
14778 		break;
14779 	case CQE_CODE_XRI_ABORTED:
14780 		/* Process the WQ XRI abort event */
14781 		phba->last_completion_time = jiffies;
14782 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14783 				(struct sli4_wcqe_xri_aborted *)&cqevt);
14784 		break;
14785 	case CQE_CODE_RECEIVE:
14786 	case CQE_CODE_RECEIVE_V1:
14787 		/* Process the RQ event */
14788 		phba->last_completion_time = jiffies;
14789 		workposted = lpfc_sli4_sp_handle_rcqe(phba,
14790 				(struct lpfc_rcqe *)&cqevt);
14791 		break;
14792 	default:
14793 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14794 				"0388 Not a valid WCQE code: x%x\n",
14795 				bf_get(lpfc_cqe_code, &cqevt));
14796 		break;
14797 	}
14798 	return workposted;
14799 }
14800 
14801 /**
14802  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14803  * @phba: Pointer to HBA context object.
14804  * @eqe: Pointer to fast-path event queue entry.
14805  * @speq: Pointer to slow-path event queue.
14806  *
14807  * This routine process a event queue entry from the slow-path event queue.
14808  * It will check the MajorCode and MinorCode to determine this is for a
14809  * completion event on a completion queue, if not, an error shall be logged
14810  * and just return. Otherwise, it will get to the corresponding completion
14811  * queue and process all the entries on that completion queue, rearm the
14812  * completion queue, and then return.
14813  *
14814  **/
14815 static void
lpfc_sli4_sp_handle_eqe(struct lpfc_hba * phba,struct lpfc_eqe * eqe,struct lpfc_queue * speq)14816 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14817 	struct lpfc_queue *speq)
14818 {
14819 	struct lpfc_queue *cq = NULL, *childq;
14820 	uint16_t cqid;
14821 	int ret = 0;
14822 
14823 	/* Get the reference to the corresponding CQ */
14824 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14825 
14826 	list_for_each_entry(childq, &speq->child_list, list) {
14827 		if (childq->queue_id == cqid) {
14828 			cq = childq;
14829 			break;
14830 		}
14831 	}
14832 	if (unlikely(!cq)) {
14833 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14834 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14835 					"0365 Slow-path CQ identifier "
14836 					"(%d) does not exist\n", cqid);
14837 		return;
14838 	}
14839 
14840 	/* Save EQ associated with this CQ */
14841 	cq->assoc_qp = speq;
14842 
14843 	if (is_kdump_kernel())
14844 		ret = queue_work(phba->wq, &cq->spwork);
14845 	else
14846 		ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14847 
14848 	if (!ret)
14849 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14850 				"0390 Cannot schedule queue work "
14851 				"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14852 				cqid, cq->queue_id, raw_smp_processor_id());
14853 }
14854 
14855 /**
14856  * __lpfc_sli4_process_cq - Process elements of a CQ
14857  * @phba: Pointer to HBA context object.
14858  * @cq: Pointer to CQ to be processed
14859  * @handler: Routine to process each cqe
14860  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14861  *
14862  * This routine processes completion queue entries in a CQ. While a valid
14863  * queue element is found, the handler is called. During processing checks
14864  * are made for periodic doorbell writes to let the hardware know of
14865  * element consumption.
14866  *
14867  * If the max limit on cqes to process is hit, or there are no more valid
14868  * entries, the loop stops. If we processed a sufficient number of elements,
14869  * meaning there is sufficient load, rather than rearming and generating
14870  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14871  * indicates no rescheduling.
14872  *
14873  * Returns True if work scheduled, False otherwise.
14874  **/
14875 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)14876 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14877 	bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14878 			struct lpfc_cqe *), unsigned long *delay)
14879 {
14880 	struct lpfc_cqe *cqe;
14881 	bool workposted = false;
14882 	int count = 0, consumed = 0;
14883 	bool arm = true;
14884 
14885 	/* default - no reschedule */
14886 	*delay = 0;
14887 
14888 	if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14889 		goto rearm_and_exit;
14890 
14891 	/* Process all the entries to the CQ */
14892 	cq->q_flag = 0;
14893 	cqe = lpfc_sli4_cq_get(cq);
14894 	while (cqe) {
14895 		workposted |= handler(phba, cq, cqe);
14896 		__lpfc_sli4_consume_cqe(phba, cq, cqe);
14897 
14898 		consumed++;
14899 		if (!(++count % cq->max_proc_limit))
14900 			break;
14901 
14902 		if (!(count % cq->notify_interval)) {
14903 			phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14904 						LPFC_QUEUE_NOARM);
14905 			consumed = 0;
14906 			cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14907 		}
14908 
14909 		if (count == LPFC_NVMET_CQ_NOTIFY)
14910 			cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14911 
14912 		cqe = lpfc_sli4_cq_get(cq);
14913 	}
14914 	if (count >= phba->cfg_cq_poll_threshold) {
14915 		*delay = 1;
14916 		arm = false;
14917 	}
14918 
14919 	/* Track the max number of CQEs processed in 1 EQ */
14920 	if (count > cq->CQ_max_cqe)
14921 		cq->CQ_max_cqe = count;
14922 
14923 	cq->assoc_qp->EQ_cqe_cnt += count;
14924 
14925 	/* Catch the no cq entry condition */
14926 	if (unlikely(count == 0))
14927 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14928 				"0369 No entry from completion queue "
14929 				"qid=%d\n", cq->queue_id);
14930 
14931 	xchg(&cq->queue_claimed, 0);
14932 
14933 rearm_and_exit:
14934 	phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14935 			arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14936 
14937 	return workposted;
14938 }
14939 
14940 /**
14941  * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14942  * @cq: pointer to CQ to process
14943  *
14944  * This routine calls the cq processing routine with a handler specific
14945  * to the type of queue bound to it.
14946  *
14947  * The CQ routine returns two values: the first is the calling status,
14948  * which indicates whether work was queued to the  background discovery
14949  * thread. If true, the routine should wakeup the discovery thread;
14950  * the second is the delay parameter. If non-zero, rather than rearming
14951  * the CQ and yet another interrupt, the CQ handler should be queued so
14952  * that it is processed in a subsequent polling action. The value of
14953  * the delay indicates when to reschedule it.
14954  **/
14955 static void
__lpfc_sli4_sp_process_cq(struct lpfc_queue * cq)14956 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14957 {
14958 	struct lpfc_hba *phba = cq->phba;
14959 	unsigned long delay;
14960 	bool workposted = false;
14961 	int ret = 0;
14962 
14963 	/* Process and rearm the CQ */
14964 	switch (cq->type) {
14965 	case LPFC_MCQ:
14966 		workposted |= __lpfc_sli4_process_cq(phba, cq,
14967 						lpfc_sli4_sp_handle_mcqe,
14968 						&delay);
14969 		break;
14970 	case LPFC_WCQ:
14971 		if (cq->subtype == LPFC_IO)
14972 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14973 						lpfc_sli4_fp_handle_cqe,
14974 						&delay);
14975 		else
14976 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14977 						lpfc_sli4_sp_handle_cqe,
14978 						&delay);
14979 		break;
14980 	default:
14981 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14982 				"0370 Invalid completion queue type (%d)\n",
14983 				cq->type);
14984 		return;
14985 	}
14986 
14987 	if (delay) {
14988 		if (is_kdump_kernel())
14989 			ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14990 						delay);
14991 		else
14992 			ret = queue_delayed_work_on(cq->chann, phba->wq,
14993 						&cq->sched_spwork, delay);
14994 		if (!ret)
14995 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14996 				"0394 Cannot schedule queue work "
14997 				"for cqid=%d on CPU %d\n",
14998 				cq->queue_id, cq->chann);
14999 	}
15000 
15001 	/* wake up worker thread if there are works to be done */
15002 	if (workposted)
15003 		lpfc_worker_wake_up(phba);
15004 }
15005 
15006 /**
15007  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
15008  *   interrupt
15009  * @work: pointer to work element
15010  *
15011  * translates from the work handler and calls the slow-path handler.
15012  **/
15013 static void
lpfc_sli4_sp_process_cq(struct work_struct * work)15014 lpfc_sli4_sp_process_cq(struct work_struct *work)
15015 {
15016 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
15017 
15018 	__lpfc_sli4_sp_process_cq(cq);
15019 }
15020 
15021 /**
15022  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
15023  * @work: pointer to work element
15024  *
15025  * translates from the work handler and calls the slow-path handler.
15026  **/
15027 static void
lpfc_sli4_dly_sp_process_cq(struct work_struct * work)15028 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
15029 {
15030 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15031 					struct lpfc_queue, sched_spwork);
15032 
15033 	__lpfc_sli4_sp_process_cq(cq);
15034 }
15035 
15036 /**
15037  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
15038  * @phba: Pointer to HBA context object.
15039  * @cq: Pointer to associated CQ
15040  * @wcqe: Pointer to work-queue completion queue entry.
15041  *
15042  * This routine process a fast-path work queue completion entry from fast-path
15043  * event queue for FCP command response completion.
15044  **/
15045 static void
lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)15046 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15047 			     struct lpfc_wcqe_complete *wcqe)
15048 {
15049 	struct lpfc_sli_ring *pring = cq->pring;
15050 	struct lpfc_iocbq *cmdiocbq;
15051 	unsigned long iflags;
15052 
15053 	/* Check for response status */
15054 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15055 		/* If resource errors reported from HBA, reduce queue
15056 		 * depth of the SCSI device.
15057 		 */
15058 		if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15059 		     IOSTAT_LOCAL_REJECT)) &&
15060 		    ((wcqe->parameter & IOERR_PARAM_MASK) ==
15061 		     IOERR_NO_RESOURCES))
15062 			phba->lpfc_rampdown_queue_depth(phba);
15063 
15064 		/* Log the cmpl status */
15065 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15066 				"0373 FCP CQE cmpl: status=x%x: "
15067 				"CQE: %08x %08x %08x %08x\n",
15068 				bf_get(lpfc_wcqe_c_status, wcqe),
15069 				wcqe->word0, wcqe->total_data_placed,
15070 				wcqe->parameter, wcqe->word3);
15071 	}
15072 
15073 	/* Look up the FCP command IOCB and create pseudo response IOCB */
15074 	spin_lock_irqsave(&pring->ring_lock, iflags);
15075 	pring->stats.iocb_event++;
15076 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15077 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15078 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
15079 	if (unlikely(!cmdiocbq)) {
15080 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15081 				"0374 FCP complete with no corresponding "
15082 				"cmdiocb: iotag (%d)\n",
15083 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15084 		return;
15085 	}
15086 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15087 	cmdiocbq->isr_timestamp = cq->isr_timestamp;
15088 #endif
15089 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
15090 		spin_lock_irqsave(&phba->hbalock, iflags);
15091 		cmdiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
15092 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15093 	}
15094 
15095 	if (cmdiocbq->cmd_cmpl) {
15096 		/* For FCP the flag is cleared in cmd_cmpl */
15097 		if (!(cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
15098 		    cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED) {
15099 			spin_lock_irqsave(&phba->hbalock, iflags);
15100 			cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
15101 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15102 		}
15103 
15104 		/* Pass the cmd_iocb and the wcqe to the upper layer */
15105 		memcpy(&cmdiocbq->wcqe_cmpl, wcqe,
15106 		       sizeof(struct lpfc_wcqe_complete));
15107 		cmdiocbq->cmd_cmpl(phba, cmdiocbq, cmdiocbq);
15108 	} else {
15109 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15110 				"0375 FCP cmdiocb not callback function "
15111 				"iotag: (%d)\n",
15112 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15113 	}
15114 }
15115 
15116 /**
15117  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15118  * @phba: Pointer to HBA context object.
15119  * @cq: Pointer to completion queue.
15120  * @wcqe: Pointer to work-queue completion queue entry.
15121  *
15122  * This routine handles an fast-path WQ entry consumed event by invoking the
15123  * proper WQ release routine to the slow-path WQ.
15124  **/
15125 static void
lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_release * wcqe)15126 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15127 			     struct lpfc_wcqe_release *wcqe)
15128 {
15129 	struct lpfc_queue *childwq;
15130 	bool wqid_matched = false;
15131 	uint16_t hba_wqid;
15132 
15133 	/* Check for fast-path FCP work queue release */
15134 	hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
15135 	list_for_each_entry(childwq, &cq->child_list, list) {
15136 		if (childwq->queue_id == hba_wqid) {
15137 			lpfc_sli4_wq_release(childwq,
15138 					bf_get(lpfc_wcqe_r_wqe_index, wcqe));
15139 			if (childwq->q_flag & HBA_NVMET_WQFULL)
15140 				lpfc_nvmet_wqfull_process(phba, childwq);
15141 			wqid_matched = true;
15142 			break;
15143 		}
15144 	}
15145 	/* Report warning log message if no match found */
15146 	if (wqid_matched != true)
15147 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15148 				"2580 Fast-path wqe consume event carries "
15149 				"miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
15150 }
15151 
15152 /**
15153  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15154  * @phba: Pointer to HBA context object.
15155  * @cq: Pointer to completion queue.
15156  * @rcqe: Pointer to receive-queue completion queue entry.
15157  *
15158  * This routine process a receive-queue completion queue entry.
15159  *
15160  * Return: true if work posted to worker thread, otherwise false.
15161  **/
15162 static bool
lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_rcqe * rcqe)15163 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15164 			    struct lpfc_rcqe *rcqe)
15165 {
15166 	bool workposted = false;
15167 	struct lpfc_queue *hrq;
15168 	struct lpfc_queue *drq;
15169 	struct rqb_dmabuf *dma_buf;
15170 	struct fc_frame_header *fc_hdr;
15171 	struct lpfc_nvmet_tgtport *tgtp;
15172 	uint32_t status, rq_id;
15173 	unsigned long iflags;
15174 	uint32_t fctl, idx;
15175 
15176 	if ((phba->nvmet_support == 0) ||
15177 	    (phba->sli4_hba.nvmet_cqset == NULL))
15178 		return workposted;
15179 
15180 	idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15181 	hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15182 	drq = phba->sli4_hba.nvmet_mrq_data[idx];
15183 
15184 	/* sanity check on queue memory */
15185 	if (unlikely(!hrq) || unlikely(!drq))
15186 		return workposted;
15187 
15188 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15189 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15190 	else
15191 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15192 
15193 	if ((phba->nvmet_support == 0) ||
15194 	    (rq_id != hrq->queue_id))
15195 		return workposted;
15196 
15197 	status = bf_get(lpfc_rcqe_status, rcqe);
15198 	switch (status) {
15199 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
15200 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15201 				"6126 Receive Frame Truncated!!\n");
15202 		fallthrough;
15203 	case FC_STATUS_RQ_SUCCESS:
15204 		spin_lock_irqsave(&phba->hbalock, iflags);
15205 		lpfc_sli4_rq_release(hrq, drq);
15206 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15207 		if (!dma_buf) {
15208 			hrq->RQ_no_buf_found++;
15209 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15210 			goto out;
15211 		}
15212 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15213 		hrq->RQ_rcv_buf++;
15214 		hrq->RQ_buf_posted--;
15215 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15216 
15217 		/* Just some basic sanity checks on FCP Command frame */
15218 		fctl = (fc_hdr->fh_f_ctl[0] << 16 |
15219 			fc_hdr->fh_f_ctl[1] << 8 |
15220 			fc_hdr->fh_f_ctl[2]);
15221 		if (((fctl &
15222 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15223 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15224 		    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15225 			goto drop;
15226 
15227 		if (fc_hdr->fh_type == FC_TYPE_FCP) {
15228 			dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
15229 			lpfc_nvmet_unsol_fcp_event(
15230 				phba, idx, dma_buf, cq->isr_timestamp,
15231 				cq->q_flag & HBA_NVMET_CQ_NOTIFY);
15232 			return false;
15233 		}
15234 drop:
15235 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15236 		break;
15237 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
15238 		if (phba->nvmet_support) {
15239 			tgtp = phba->targetport->private;
15240 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15241 					"6401 RQE Error x%x, posted %d err_cnt "
15242 					"%d: %x %x %x\n",
15243 					status, hrq->RQ_buf_posted,
15244 					hrq->RQ_no_posted_buf,
15245 					atomic_read(&tgtp->rcv_fcp_cmd_in),
15246 					atomic_read(&tgtp->rcv_fcp_cmd_out),
15247 					atomic_read(&tgtp->xmt_fcp_release));
15248 		}
15249 		fallthrough;
15250 
15251 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
15252 		hrq->RQ_no_posted_buf++;
15253 		/* Post more buffers if possible */
15254 		break;
15255 	case FC_STATUS_RQ_DMA_FAILURE:
15256 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15257 				"2575 RQE DMA Error x%x, x%08x x%08x x%08x "
15258 				"x%08x\n",
15259 				status, rcqe->word0, rcqe->word1,
15260 				rcqe->word2, rcqe->word3);
15261 
15262 		/* If IV set, no further recovery */
15263 		if (bf_get(lpfc_rcqe_iv, rcqe))
15264 			break;
15265 
15266 		/* recycle consumed resource */
15267 		spin_lock_irqsave(&phba->hbalock, iflags);
15268 		lpfc_sli4_rq_release(hrq, drq);
15269 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15270 		if (!dma_buf) {
15271 			hrq->RQ_no_buf_found++;
15272 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15273 			break;
15274 		}
15275 		hrq->RQ_rcv_buf++;
15276 		hrq->RQ_buf_posted--;
15277 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15278 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15279 		break;
15280 	default:
15281 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15282 				"2576 Unexpected RQE Status x%x, w0-3 x%08x "
15283 				"x%08x x%08x x%08x\n",
15284 				status, rcqe->word0, rcqe->word1,
15285 				rcqe->word2, rcqe->word3);
15286 		break;
15287 	}
15288 out:
15289 	return workposted;
15290 }
15291 
15292 /**
15293  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
15294  * @phba: adapter with cq
15295  * @cq: Pointer to the completion queue.
15296  * @cqe: Pointer to fast-path completion queue entry.
15297  *
15298  * This routine process a fast-path work queue completion entry from fast-path
15299  * event queue for FCP command response completion.
15300  *
15301  * Return: true if work posted to worker thread, otherwise false.
15302  **/
15303 static bool
lpfc_sli4_fp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)15304 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15305 			 struct lpfc_cqe *cqe)
15306 {
15307 	struct lpfc_wcqe_release wcqe;
15308 	bool workposted = false;
15309 
15310 	/* Copy the work queue CQE and convert endian order if needed */
15311 	lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
15312 
15313 	/* Check and process for different type of WCQE and dispatch */
15314 	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15315 	case CQE_CODE_COMPL_WQE:
15316 	case CQE_CODE_NVME_ERSP:
15317 		cq->CQ_wq++;
15318 		/* Process the WQ complete event */
15319 		phba->last_completion_time = jiffies;
15320 		if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
15321 			lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
15322 				(struct lpfc_wcqe_complete *)&wcqe);
15323 		break;
15324 	case CQE_CODE_RELEASE_WQE:
15325 		cq->CQ_release_wqe++;
15326 		/* Process the WQ release event */
15327 		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15328 				(struct lpfc_wcqe_release *)&wcqe);
15329 		break;
15330 	case CQE_CODE_XRI_ABORTED:
15331 		cq->CQ_xri_aborted++;
15332 		/* Process the WQ XRI abort event */
15333 		phba->last_completion_time = jiffies;
15334 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15335 				(struct sli4_wcqe_xri_aborted *)&wcqe);
15336 		break;
15337 	case CQE_CODE_RECEIVE_V1:
15338 	case CQE_CODE_RECEIVE:
15339 		phba->last_completion_time = jiffies;
15340 		if (cq->subtype == LPFC_NVMET) {
15341 			workposted = lpfc_sli4_nvmet_handle_rcqe(
15342 				phba, cq, (struct lpfc_rcqe *)&wcqe);
15343 		}
15344 		break;
15345 	default:
15346 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15347 				"0144 Not a valid CQE code: x%x\n",
15348 				bf_get(lpfc_wcqe_c_code, &wcqe));
15349 		break;
15350 	}
15351 	return workposted;
15352 }
15353 
15354 /**
15355  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15356  * @cq: Pointer to CQ to be processed
15357  *
15358  * This routine calls the cq processing routine with the handler for
15359  * fast path CQEs.
15360  *
15361  * The CQ routine returns two values: the first is the calling status,
15362  * which indicates whether work was queued to the  background discovery
15363  * thread. If true, the routine should wakeup the discovery thread;
15364  * the second is the delay parameter. If non-zero, rather than rearming
15365  * the CQ and yet another interrupt, the CQ handler should be queued so
15366  * that it is processed in a subsequent polling action. The value of
15367  * the delay indicates when to reschedule it.
15368  **/
15369 static void
__lpfc_sli4_hba_process_cq(struct lpfc_queue * cq)15370 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
15371 {
15372 	struct lpfc_hba *phba = cq->phba;
15373 	unsigned long delay;
15374 	bool workposted = false;
15375 	int ret;
15376 
15377 	/* process and rearm the CQ */
15378 	workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
15379 					     &delay);
15380 
15381 	if (delay) {
15382 		if (is_kdump_kernel())
15383 			ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15384 						delay);
15385 		else
15386 			ret = queue_delayed_work_on(cq->chann, phba->wq,
15387 						&cq->sched_irqwork, delay);
15388 		if (!ret)
15389 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15390 					"0367 Cannot schedule queue work "
15391 					"for cqid=%d on CPU %d\n",
15392 					cq->queue_id, cq->chann);
15393 	}
15394 
15395 	/* wake up worker thread if there are works to be done */
15396 	if (workposted)
15397 		lpfc_worker_wake_up(phba);
15398 }
15399 
15400 /**
15401  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15402  *   interrupt
15403  * @work: pointer to work element
15404  *
15405  * translates from the work handler and calls the fast-path handler.
15406  **/
15407 static void
lpfc_sli4_hba_process_cq(struct work_struct * work)15408 lpfc_sli4_hba_process_cq(struct work_struct *work)
15409 {
15410 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
15411 
15412 	__lpfc_sli4_hba_process_cq(cq);
15413 }
15414 
15415 /**
15416  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
15417  * @phba: Pointer to HBA context object.
15418  * @eq: Pointer to the queue structure.
15419  * @eqe: Pointer to fast-path event queue entry.
15420  * @poll_mode: poll_mode to execute processing the cq.
15421  *
15422  * This routine process a event queue entry from the fast-path event queue.
15423  * It will check the MajorCode and MinorCode to determine this is for a
15424  * completion event on a completion queue, if not, an error shall be logged
15425  * and just return. Otherwise, it will get to the corresponding completion
15426  * queue and process all the entries on the completion queue, rearm the
15427  * completion queue, and then return.
15428  **/
15429 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)15430 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15431 			 struct lpfc_eqe *eqe, enum lpfc_poll_mode poll_mode)
15432 {
15433 	struct lpfc_queue *cq = NULL;
15434 	uint32_t qidx = eq->hdwq;
15435 	uint16_t cqid, id;
15436 	int ret;
15437 
15438 	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
15439 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15440 				"0366 Not a valid completion "
15441 				"event: majorcode=x%x, minorcode=x%x\n",
15442 				bf_get_le32(lpfc_eqe_major_code, eqe),
15443 				bf_get_le32(lpfc_eqe_minor_code, eqe));
15444 		return;
15445 	}
15446 
15447 	/* Get the reference to the corresponding CQ */
15448 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15449 
15450 	/* Use the fast lookup method first */
15451 	if (cqid <= phba->sli4_hba.cq_max) {
15452 		cq = phba->sli4_hba.cq_lookup[cqid];
15453 		if (cq)
15454 			goto  work_cq;
15455 	}
15456 
15457 	/* Next check for NVMET completion */
15458 	if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15459 		id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15460 		if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15461 			/* Process NVMET unsol rcv */
15462 			cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15463 			goto  process_cq;
15464 		}
15465 	}
15466 
15467 	if (phba->sli4_hba.nvmels_cq &&
15468 	    (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15469 		/* Process NVME unsol rcv */
15470 		cq = phba->sli4_hba.nvmels_cq;
15471 	}
15472 
15473 	/* Otherwise this is a Slow path event */
15474 	if (cq == NULL) {
15475 		lpfc_sli4_sp_handle_eqe(phba, eqe,
15476 					phba->sli4_hba.hdwq[qidx].hba_eq);
15477 		return;
15478 	}
15479 
15480 process_cq:
15481 	if (unlikely(cqid != cq->queue_id)) {
15482 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15483 				"0368 Miss-matched fast-path completion "
15484 				"queue identifier: eqcqid=%d, fcpcqid=%d\n",
15485 				cqid, cq->queue_id);
15486 		return;
15487 	}
15488 
15489 work_cq:
15490 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15491 	if (phba->ktime_on)
15492 		cq->isr_timestamp = ktime_get_ns();
15493 	else
15494 		cq->isr_timestamp = 0;
15495 #endif
15496 
15497 	switch (poll_mode) {
15498 	case LPFC_THREADED_IRQ:
15499 		__lpfc_sli4_hba_process_cq(cq);
15500 		break;
15501 	case LPFC_QUEUE_WORK:
15502 	default:
15503 		if (is_kdump_kernel())
15504 			ret = queue_work(phba->wq, &cq->irqwork);
15505 		else
15506 			ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15507 		if (!ret)
15508 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15509 					"0383 Cannot schedule queue work "
15510 					"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15511 					cqid, cq->queue_id,
15512 					raw_smp_processor_id());
15513 		break;
15514 	}
15515 }
15516 
15517 /**
15518  * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
15519  * @work: pointer to work element
15520  *
15521  * translates from the work handler and calls the fast-path handler.
15522  **/
15523 static void
lpfc_sli4_dly_hba_process_cq(struct work_struct * work)15524 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
15525 {
15526 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15527 					struct lpfc_queue, sched_irqwork);
15528 
15529 	__lpfc_sli4_hba_process_cq(cq);
15530 }
15531 
15532 /**
15533  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
15534  * @irq: Interrupt number.
15535  * @dev_id: The device context pointer.
15536  *
15537  * This function is directly called from the PCI layer as an interrupt
15538  * service routine when device with SLI-4 interface spec is enabled with
15539  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15540  * ring event in the HBA. However, when the device is enabled with either
15541  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15542  * device-level interrupt handler. When the PCI slot is in error recovery
15543  * or the HBA is undergoing initialization, the interrupt handler will not
15544  * process the interrupt. The SCSI FCP fast-path ring event are handled in
15545  * the intrrupt context. This function is called without any lock held.
15546  * It gets the hbalock to access and update SLI data structures. Note that,
15547  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15548  * equal to that of FCP CQ index.
15549  *
15550  * The link attention and ELS ring attention events are handled
15551  * by the worker thread. The interrupt handler signals the worker thread
15552  * and returns for these events. This function is called without any lock
15553  * held. It gets the hbalock to access and update SLI data structures.
15554  *
15555  * This function returns IRQ_HANDLED when interrupt is handled, IRQ_WAKE_THREAD
15556  * when interrupt is scheduled to be handled from a threaded irq context, or
15557  * else returns IRQ_NONE.
15558  **/
15559 irqreturn_t
lpfc_sli4_hba_intr_handler(int irq,void * dev_id)15560 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
15561 {
15562 	struct lpfc_hba *phba;
15563 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
15564 	struct lpfc_queue *fpeq;
15565 	unsigned long iflag;
15566 	int hba_eqidx;
15567 	int ecount = 0;
15568 	struct lpfc_eq_intr_info *eqi;
15569 
15570 	/* Get the driver's phba structure from the dev_id */
15571 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15572 	phba = hba_eq_hdl->phba;
15573 	hba_eqidx = hba_eq_hdl->idx;
15574 
15575 	if (unlikely(!phba))
15576 		return IRQ_NONE;
15577 	if (unlikely(!phba->sli4_hba.hdwq))
15578 		return IRQ_NONE;
15579 
15580 	/* Get to the EQ struct associated with this vector */
15581 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
15582 	if (unlikely(!fpeq))
15583 		return IRQ_NONE;
15584 
15585 	/* Check device state for handling interrupt */
15586 	if (unlikely(lpfc_intr_state_check(phba))) {
15587 		/* Check again for link_state with lock held */
15588 		spin_lock_irqsave(&phba->hbalock, iflag);
15589 		if (phba->link_state < LPFC_LINK_DOWN)
15590 			/* Flush, clear interrupt, and rearm the EQ */
15591 			lpfc_sli4_eqcq_flush(phba, fpeq);
15592 		spin_unlock_irqrestore(&phba->hbalock, iflag);
15593 		return IRQ_NONE;
15594 	}
15595 
15596 	switch (fpeq->poll_mode) {
15597 	case LPFC_THREADED_IRQ:
15598 		/* CGN mgmt is mutually exclusive from irq processing */
15599 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
15600 			return IRQ_WAKE_THREAD;
15601 		fallthrough;
15602 	case LPFC_QUEUE_WORK:
15603 	default:
15604 		eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15605 		eqi->icnt++;
15606 
15607 		fpeq->last_cpu = raw_smp_processor_id();
15608 
15609 		if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
15610 		    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
15611 		    phba->cfg_auto_imax &&
15612 		    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15613 		    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15614 			lpfc_sli4_mod_hba_eq_delay(phba, fpeq,
15615 						   LPFC_MAX_AUTO_EQ_DELAY);
15616 
15617 		/* process and rearm the EQ */
15618 		ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
15619 					      LPFC_QUEUE_WORK);
15620 
15621 		if (unlikely(ecount == 0)) {
15622 			fpeq->EQ_no_entry++;
15623 			if (phba->intr_type == MSIX)
15624 				/* MSI-X treated interrupt served as no EQ share INT */
15625 				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15626 						"0358 MSI-X interrupt with no EQE\n");
15627 			else
15628 				/* Non MSI-X treated on interrupt as EQ share INT */
15629 				return IRQ_NONE;
15630 		}
15631 	}
15632 
15633 	return IRQ_HANDLED;
15634 } /* lpfc_sli4_hba_intr_handler */
15635 
15636 /**
15637  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15638  * @irq: Interrupt number.
15639  * @dev_id: The device context pointer.
15640  *
15641  * This function is the device-level interrupt handler to device with SLI-4
15642  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15643  * interrupt mode is enabled and there is an event in the HBA which requires
15644  * driver attention. This function invokes the slow-path interrupt attention
15645  * handling function and fast-path interrupt attention handling function in
15646  * turn to process the relevant HBA attention events. This function is called
15647  * without any lock held. It gets the hbalock to access and update SLI data
15648  * structures.
15649  *
15650  * This function returns IRQ_HANDLED when interrupt is handled, else it
15651  * returns IRQ_NONE.
15652  **/
15653 irqreturn_t
lpfc_sli4_intr_handler(int irq,void * dev_id)15654 lpfc_sli4_intr_handler(int irq, void *dev_id)
15655 {
15656 	struct lpfc_hba  *phba;
15657 	irqreturn_t hba_irq_rc;
15658 	bool hba_handled = false;
15659 	int qidx;
15660 
15661 	/* Get the driver's phba structure from the dev_id */
15662 	phba = (struct lpfc_hba *)dev_id;
15663 
15664 	if (unlikely(!phba))
15665 		return IRQ_NONE;
15666 
15667 	/*
15668 	 * Invoke fast-path host attention interrupt handling as appropriate.
15669 	 */
15670 	for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
15671 		hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
15672 					&phba->sli4_hba.hba_eq_hdl[qidx]);
15673 		if (hba_irq_rc == IRQ_HANDLED)
15674 			hba_handled |= true;
15675 	}
15676 
15677 	return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
15678 } /* lpfc_sli4_intr_handler */
15679 
lpfc_sli4_poll_hbtimer(struct timer_list * t)15680 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15681 {
15682 	struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
15683 	struct lpfc_queue *eq;
15684 
15685 	rcu_read_lock();
15686 
15687 	list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15688 		lpfc_sli4_poll_eq(eq);
15689 	if (!list_empty(&phba->poll_list))
15690 		mod_timer(&phba->cpuhp_poll_timer,
15691 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15692 
15693 	rcu_read_unlock();
15694 }
15695 
lpfc_sli4_add_to_poll_list(struct lpfc_queue * eq)15696 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15697 {
15698 	struct lpfc_hba *phba = eq->phba;
15699 
15700 	/* kickstart slowpath processing if needed */
15701 	if (list_empty(&phba->poll_list))
15702 		mod_timer(&phba->cpuhp_poll_timer,
15703 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15704 
15705 	list_add_rcu(&eq->_poll_list, &phba->poll_list);
15706 	synchronize_rcu();
15707 }
15708 
lpfc_sli4_remove_from_poll_list(struct lpfc_queue * eq)15709 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15710 {
15711 	struct lpfc_hba *phba = eq->phba;
15712 
15713 	/* Disable slowpath processing for this eq.  Kick start the eq
15714 	 * by RE-ARMING the eq's ASAP
15715 	 */
15716 	list_del_rcu(&eq->_poll_list);
15717 	synchronize_rcu();
15718 
15719 	if (list_empty(&phba->poll_list))
15720 		del_timer_sync(&phba->cpuhp_poll_timer);
15721 }
15722 
lpfc_sli4_cleanup_poll_list(struct lpfc_hba * phba)15723 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
15724 {
15725 	struct lpfc_queue *eq, *next;
15726 
15727 	list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15728 		list_del(&eq->_poll_list);
15729 
15730 	INIT_LIST_HEAD(&phba->poll_list);
15731 	synchronize_rcu();
15732 }
15733 
15734 static inline void
__lpfc_sli4_switch_eqmode(struct lpfc_queue * eq,uint8_t mode)15735 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15736 {
15737 	if (mode == eq->mode)
15738 		return;
15739 	/*
15740 	 * currently this function is only called during a hotplug
15741 	 * event and the cpu on which this function is executing
15742 	 * is going offline.  By now the hotplug has instructed
15743 	 * the scheduler to remove this cpu from cpu active mask.
15744 	 * So we don't need to work about being put aside by the
15745 	 * scheduler for a high priority process.  Yes, the inte-
15746 	 * rrupts could come but they are known to retire ASAP.
15747 	 */
15748 
15749 	/* Disable polling in the fastpath */
15750 	WRITE_ONCE(eq->mode, mode);
15751 	/* flush out the store buffer */
15752 	smp_wmb();
15753 
15754 	/*
15755 	 * Add this eq to the polling list and start polling. For
15756 	 * a grace period both interrupt handler and poller will
15757 	 * try to process the eq _but_ that's fine.  We have a
15758 	 * synchronization mechanism in place (queue_claimed) to
15759 	 * deal with it.  This is just a draining phase for int-
15760 	 * errupt handler (not eq's) as we have guranteed through
15761 	 * barrier that all the CPUs have seen the new CQ_POLLED
15762 	 * state. which will effectively disable the REARMING of
15763 	 * the EQ.  The whole idea is eq's die off eventually as
15764 	 * we are not rearming EQ's anymore.
15765 	 */
15766 	mode ? lpfc_sli4_add_to_poll_list(eq) :
15767 	       lpfc_sli4_remove_from_poll_list(eq);
15768 }
15769 
lpfc_sli4_start_polling(struct lpfc_queue * eq)15770 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15771 {
15772 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15773 }
15774 
lpfc_sli4_stop_polling(struct lpfc_queue * eq)15775 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15776 {
15777 	struct lpfc_hba *phba = eq->phba;
15778 
15779 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15780 
15781 	/* Kick start for the pending io's in h/w.
15782 	 * Once we switch back to interrupt processing on a eq
15783 	 * the io path completion will only arm eq's when it
15784 	 * receives a completion.  But since eq's are in disa-
15785 	 * rmed state it doesn't receive a completion.  This
15786 	 * creates a deadlock scenaro.
15787 	 */
15788 	phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15789 }
15790 
15791 /**
15792  * lpfc_sli4_queue_free - free a queue structure and associated memory
15793  * @queue: The queue structure to free.
15794  *
15795  * This function frees a queue structure and the DMAable memory used for
15796  * the host resident queue. This function must be called after destroying the
15797  * queue on the HBA.
15798  **/
15799 void
lpfc_sli4_queue_free(struct lpfc_queue * queue)15800 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15801 {
15802 	struct lpfc_dmabuf *dmabuf;
15803 
15804 	if (!queue)
15805 		return;
15806 
15807 	if (!list_empty(&queue->wq_list))
15808 		list_del(&queue->wq_list);
15809 
15810 	while (!list_empty(&queue->page_list)) {
15811 		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15812 				 list);
15813 		dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15814 				  dmabuf->virt, dmabuf->phys);
15815 		kfree(dmabuf);
15816 	}
15817 	if (queue->rqbp) {
15818 		lpfc_free_rq_buffer(queue->phba, queue);
15819 		kfree(queue->rqbp);
15820 	}
15821 
15822 	if (!list_empty(&queue->cpu_list))
15823 		list_del(&queue->cpu_list);
15824 
15825 	kfree(queue);
15826 	return;
15827 }
15828 
15829 /**
15830  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15831  * @phba: The HBA that this queue is being created on.
15832  * @page_size: The size of a queue page
15833  * @entry_size: The size of each queue entry for this queue.
15834  * @entry_count: The number of entries that this queue will handle.
15835  * @cpu: The cpu that will primarily utilize this queue.
15836  *
15837  * This function allocates a queue structure and the DMAable memory used for
15838  * the host resident queue. This function must be called before creating the
15839  * queue on the HBA.
15840  **/
15841 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)15842 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15843 		      uint32_t entry_size, uint32_t entry_count, int cpu)
15844 {
15845 	struct lpfc_queue *queue;
15846 	struct lpfc_dmabuf *dmabuf;
15847 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15848 	uint16_t x, pgcnt;
15849 
15850 	if (!phba->sli4_hba.pc_sli4_params.supported)
15851 		hw_page_size = page_size;
15852 
15853 	pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15854 
15855 	/* If needed, Adjust page count to match the max the adapter supports */
15856 	if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15857 		pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15858 
15859 	queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15860 			     GFP_KERNEL, cpu_to_node(cpu));
15861 	if (!queue)
15862 		return NULL;
15863 
15864 	INIT_LIST_HEAD(&queue->list);
15865 	INIT_LIST_HEAD(&queue->_poll_list);
15866 	INIT_LIST_HEAD(&queue->wq_list);
15867 	INIT_LIST_HEAD(&queue->wqfull_list);
15868 	INIT_LIST_HEAD(&queue->page_list);
15869 	INIT_LIST_HEAD(&queue->child_list);
15870 	INIT_LIST_HEAD(&queue->cpu_list);
15871 
15872 	/* Set queue parameters now.  If the system cannot provide memory
15873 	 * resources, the free routine needs to know what was allocated.
15874 	 */
15875 	queue->page_count = pgcnt;
15876 	queue->q_pgs = (void **)&queue[1];
15877 	queue->entry_cnt_per_pg = hw_page_size / entry_size;
15878 	queue->entry_size = entry_size;
15879 	queue->entry_count = entry_count;
15880 	queue->page_size = hw_page_size;
15881 	queue->phba = phba;
15882 
15883 	for (x = 0; x < queue->page_count; x++) {
15884 		dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15885 				      dev_to_node(&phba->pcidev->dev));
15886 		if (!dmabuf)
15887 			goto out_fail;
15888 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15889 						  hw_page_size, &dmabuf->phys,
15890 						  GFP_KERNEL);
15891 		if (!dmabuf->virt) {
15892 			kfree(dmabuf);
15893 			goto out_fail;
15894 		}
15895 		dmabuf->buffer_tag = x;
15896 		list_add_tail(&dmabuf->list, &queue->page_list);
15897 		/* use lpfc_sli4_qe to index a paritcular entry in this page */
15898 		queue->q_pgs[x] = dmabuf->virt;
15899 	}
15900 	INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15901 	INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15902 	INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15903 	INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15904 
15905 	/* notify_interval will be set during q creation */
15906 
15907 	return queue;
15908 out_fail:
15909 	lpfc_sli4_queue_free(queue);
15910 	return NULL;
15911 }
15912 
15913 /**
15914  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15915  * @phba: HBA structure that indicates port to create a queue on.
15916  * @pci_barset: PCI BAR set flag.
15917  *
15918  * This function shall perform iomap of the specified PCI BAR address to host
15919  * memory address if not already done so and return it. The returned host
15920  * memory address can be NULL.
15921  */
15922 static void __iomem *
lpfc_dual_chute_pci_bar_map(struct lpfc_hba * phba,uint16_t pci_barset)15923 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15924 {
15925 	if (!phba->pcidev)
15926 		return NULL;
15927 
15928 	switch (pci_barset) {
15929 	case WQ_PCI_BAR_0_AND_1:
15930 		return phba->pci_bar0_memmap_p;
15931 	case WQ_PCI_BAR_2_AND_3:
15932 		return phba->pci_bar2_memmap_p;
15933 	case WQ_PCI_BAR_4_AND_5:
15934 		return phba->pci_bar4_memmap_p;
15935 	default:
15936 		break;
15937 	}
15938 	return NULL;
15939 }
15940 
15941 /**
15942  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15943  * @phba: HBA structure that EQs are on.
15944  * @startq: The starting EQ index to modify
15945  * @numq: The number of EQs (consecutive indexes) to modify
15946  * @usdelay: amount of delay
15947  *
15948  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15949  * is set either by writing to a register (if supported by the SLI Port)
15950  * or by mailbox command. The mailbox command allows several EQs to be
15951  * updated at once.
15952  *
15953  * The @phba struct is used to send a mailbox command to HBA. The @startq
15954  * is used to get the starting EQ index to change. The @numq value is
15955  * used to specify how many consecutive EQ indexes, starting at EQ index,
15956  * are to be changed. This function is asynchronous and will wait for any
15957  * mailbox commands to finish before returning.
15958  *
15959  * On success this function will return a zero. If unable to allocate
15960  * enough memory this function will return -ENOMEM. If a mailbox command
15961  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15962  * have had their delay multipler changed.
15963  **/
15964 void
lpfc_modify_hba_eq_delay(struct lpfc_hba * phba,uint32_t startq,uint32_t numq,uint32_t usdelay)15965 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
15966 			 uint32_t numq, uint32_t usdelay)
15967 {
15968 	struct lpfc_mbx_modify_eq_delay *eq_delay;
15969 	LPFC_MBOXQ_t *mbox;
15970 	struct lpfc_queue *eq;
15971 	int cnt = 0, rc, length;
15972 	uint32_t shdr_status, shdr_add_status;
15973 	uint32_t dmult;
15974 	int qidx;
15975 	union lpfc_sli4_cfg_shdr *shdr;
15976 
15977 	if (startq >= phba->cfg_irq_chann)
15978 		return;
15979 
15980 	if (usdelay > 0xFFFF) {
15981 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15982 				"6429 usdelay %d too large. Scaled down to "
15983 				"0xFFFF.\n", usdelay);
15984 		usdelay = 0xFFFF;
15985 	}
15986 
15987 	/* set values by EQ_DELAY register if supported */
15988 	if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15989 		for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15990 			eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15991 			if (!eq)
15992 				continue;
15993 
15994 			lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
15995 
15996 			if (++cnt >= numq)
15997 				break;
15998 		}
15999 		return;
16000 	}
16001 
16002 	/* Otherwise, set values by mailbox cmd */
16003 
16004 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16005 	if (!mbox) {
16006 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16007 				"6428 Failed allocating mailbox cmd buffer."
16008 				" EQ delay was not set.\n");
16009 		return;
16010 	}
16011 	length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
16012 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16013 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16014 			 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
16015 			 length, LPFC_SLI4_MBX_EMBED);
16016 	eq_delay = &mbox->u.mqe.un.eq_delay;
16017 
16018 	/* Calculate delay multiper from maximum interrupt per second */
16019 	dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
16020 	if (dmult)
16021 		dmult--;
16022 	if (dmult > LPFC_DMULT_MAX)
16023 		dmult = LPFC_DMULT_MAX;
16024 
16025 	for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16026 		eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16027 		if (!eq)
16028 			continue;
16029 		eq->q_mode = usdelay;
16030 		eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16031 		eq_delay->u.request.eq[cnt].phase = 0;
16032 		eq_delay->u.request.eq[cnt].delay_multi = dmult;
16033 
16034 		if (++cnt >= numq)
16035 			break;
16036 	}
16037 	eq_delay->u.request.num_eq = cnt;
16038 
16039 	mbox->vport = phba->pport;
16040 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16041 	mbox->ctx_ndlp = NULL;
16042 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16043 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16044 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16045 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16046 	if (shdr_status || shdr_add_status || rc) {
16047 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16048 				"2512 MODIFY_EQ_DELAY mailbox failed with "
16049 				"status x%x add_status x%x, mbx status x%x\n",
16050 				shdr_status, shdr_add_status, rc);
16051 	}
16052 	mempool_free(mbox, phba->mbox_mem_pool);
16053 	return;
16054 }
16055 
16056 /**
16057  * lpfc_eq_create - Create an Event Queue on the HBA
16058  * @phba: HBA structure that indicates port to create a queue on.
16059  * @eq: The queue structure to use to create the event queue.
16060  * @imax: The maximum interrupt per second limit.
16061  *
16062  * This function creates an event queue, as detailed in @eq, on a port,
16063  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16064  *
16065  * The @phba struct is used to send mailbox command to HBA. The @eq struct
16066  * is used to get the entry count and entry size that are necessary to
16067  * determine the number of pages to allocate and use for this queue. This
16068  * function will send the EQ_CREATE mailbox command to the HBA to setup the
16069  * event queue. This function is asynchronous and will wait for the mailbox
16070  * command to finish before continuing.
16071  *
16072  * On success this function will return a zero. If unable to allocate enough
16073  * memory this function will return -ENOMEM. If the queue create mailbox command
16074  * fails this function will return -ENXIO.
16075  **/
16076 int
lpfc_eq_create(struct lpfc_hba * phba,struct lpfc_queue * eq,uint32_t imax)16077 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
16078 {
16079 	struct lpfc_mbx_eq_create *eq_create;
16080 	LPFC_MBOXQ_t *mbox;
16081 	int rc, length, status = 0;
16082 	struct lpfc_dmabuf *dmabuf;
16083 	uint32_t shdr_status, shdr_add_status;
16084 	union lpfc_sli4_cfg_shdr *shdr;
16085 	uint16_t dmult;
16086 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16087 
16088 	/* sanity check on queue memory */
16089 	if (!eq)
16090 		return -ENODEV;
16091 	if (!phba->sli4_hba.pc_sli4_params.supported)
16092 		hw_page_size = SLI4_PAGE_SIZE;
16093 
16094 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16095 	if (!mbox)
16096 		return -ENOMEM;
16097 	length = (sizeof(struct lpfc_mbx_eq_create) -
16098 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16099 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16100 			 LPFC_MBOX_OPCODE_EQ_CREATE,
16101 			 length, LPFC_SLI4_MBX_EMBED);
16102 	eq_create = &mbox->u.mqe.un.eq_create;
16103 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
16104 	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16105 	       eq->page_count);
16106 	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16107 	       LPFC_EQE_SIZE);
16108 	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
16109 
16110 	/* Use version 2 of CREATE_EQ if eqav is set */
16111 	if (phba->sli4_hba.pc_sli4_params.eqav) {
16112 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16113 		       LPFC_Q_CREATE_VERSION_2);
16114 		bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16115 		       phba->sli4_hba.pc_sli4_params.eqav);
16116 	}
16117 
16118 	/* don't setup delay multiplier using EQ_CREATE */
16119 	dmult = 0;
16120 	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16121 	       dmult);
16122 	switch (eq->entry_count) {
16123 	default:
16124 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16125 				"0360 Unsupported EQ count. (%d)\n",
16126 				eq->entry_count);
16127 		if (eq->entry_count < 256) {
16128 			status = -EINVAL;
16129 			goto out;
16130 		}
16131 		fallthrough;	/* otherwise default to smallest count */
16132 	case 256:
16133 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16134 		       LPFC_EQ_CNT_256);
16135 		break;
16136 	case 512:
16137 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16138 		       LPFC_EQ_CNT_512);
16139 		break;
16140 	case 1024:
16141 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16142 		       LPFC_EQ_CNT_1024);
16143 		break;
16144 	case 2048:
16145 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16146 		       LPFC_EQ_CNT_2048);
16147 		break;
16148 	case 4096:
16149 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16150 		       LPFC_EQ_CNT_4096);
16151 		break;
16152 	}
16153 	list_for_each_entry(dmabuf, &eq->page_list, list) {
16154 		memset(dmabuf->virt, 0, hw_page_size);
16155 		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16156 					putPaddrLow(dmabuf->phys);
16157 		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16158 					putPaddrHigh(dmabuf->phys);
16159 	}
16160 	mbox->vport = phba->pport;
16161 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16162 	mbox->ctx_buf = NULL;
16163 	mbox->ctx_ndlp = NULL;
16164 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16165 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16166 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16167 	if (shdr_status || shdr_add_status || rc) {
16168 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16169 				"2500 EQ_CREATE mailbox failed with "
16170 				"status x%x add_status x%x, mbx status x%x\n",
16171 				shdr_status, shdr_add_status, rc);
16172 		status = -ENXIO;
16173 	}
16174 	eq->type = LPFC_EQ;
16175 	eq->subtype = LPFC_NONE;
16176 	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16177 	if (eq->queue_id == 0xFFFF)
16178 		status = -ENXIO;
16179 	eq->host_index = 0;
16180 	eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16181 	eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
16182 out:
16183 	mempool_free(mbox, phba->mbox_mem_pool);
16184 	return status;
16185 }
16186 
16187 /**
16188  * lpfc_sli4_hba_intr_handler_th - SLI4 HBA threaded interrupt handler
16189  * @irq: Interrupt number.
16190  * @dev_id: The device context pointer.
16191  *
16192  * This routine is a mirror of lpfc_sli4_hba_intr_handler, but executed within
16193  * threaded irq context.
16194  *
16195  * Returns
16196  * IRQ_HANDLED - interrupt is handled
16197  * IRQ_NONE - otherwise
16198  **/
lpfc_sli4_hba_intr_handler_th(int irq,void * dev_id)16199 irqreturn_t lpfc_sli4_hba_intr_handler_th(int irq, void *dev_id)
16200 {
16201 	struct lpfc_hba *phba;
16202 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
16203 	struct lpfc_queue *fpeq;
16204 	int ecount = 0;
16205 	int hba_eqidx;
16206 	struct lpfc_eq_intr_info *eqi;
16207 
16208 	/* Get the driver's phba structure from the dev_id */
16209 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
16210 	phba = hba_eq_hdl->phba;
16211 	hba_eqidx = hba_eq_hdl->idx;
16212 
16213 	if (unlikely(!phba))
16214 		return IRQ_NONE;
16215 	if (unlikely(!phba->sli4_hba.hdwq))
16216 		return IRQ_NONE;
16217 
16218 	/* Get to the EQ struct associated with this vector */
16219 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
16220 	if (unlikely(!fpeq))
16221 		return IRQ_NONE;
16222 
16223 	eqi = per_cpu_ptr(phba->sli4_hba.eq_info, raw_smp_processor_id());
16224 	eqi->icnt++;
16225 
16226 	fpeq->last_cpu = raw_smp_processor_id();
16227 
16228 	if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
16229 	    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
16230 	    phba->cfg_auto_imax &&
16231 	    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
16232 	    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
16233 		lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
16234 
16235 	/* process and rearm the EQ */
16236 	ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
16237 				      LPFC_THREADED_IRQ);
16238 
16239 	if (unlikely(ecount == 0)) {
16240 		fpeq->EQ_no_entry++;
16241 		if (phba->intr_type == MSIX)
16242 			/* MSI-X treated interrupt served as no EQ share INT */
16243 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16244 					"3358 MSI-X interrupt with no EQE\n");
16245 		else
16246 			/* Non MSI-X treated on interrupt as EQ share INT */
16247 			return IRQ_NONE;
16248 	}
16249 	return IRQ_HANDLED;
16250 }
16251 
16252 /**
16253  * lpfc_cq_create - Create a Completion Queue on the HBA
16254  * @phba: HBA structure that indicates port to create a queue on.
16255  * @cq: The queue structure to use to create the completion queue.
16256  * @eq: The event queue to bind this completion queue to.
16257  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16258  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16259  *
16260  * This function creates a completion queue, as detailed in @wq, on a port,
16261  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16262  *
16263  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16264  * is used to get the entry count and entry size that are necessary to
16265  * determine the number of pages to allocate and use for this queue. The @eq
16266  * is used to indicate which event queue to bind this completion queue to. This
16267  * function will send the CQ_CREATE mailbox command to the HBA to setup the
16268  * completion queue. This function is asynchronous and will wait for the mailbox
16269  * command to finish before continuing.
16270  *
16271  * On success this function will return a zero. If unable to allocate enough
16272  * memory this function will return -ENOMEM. If the queue create mailbox command
16273  * fails this function will return -ENXIO.
16274  **/
16275 int
lpfc_cq_create(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_queue * eq,uint32_t type,uint32_t subtype)16276 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16277 	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16278 {
16279 	struct lpfc_mbx_cq_create *cq_create;
16280 	struct lpfc_dmabuf *dmabuf;
16281 	LPFC_MBOXQ_t *mbox;
16282 	int rc, length, status = 0;
16283 	uint32_t shdr_status, shdr_add_status;
16284 	union lpfc_sli4_cfg_shdr *shdr;
16285 
16286 	/* sanity check on queue memory */
16287 	if (!cq || !eq)
16288 		return -ENODEV;
16289 
16290 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16291 	if (!mbox)
16292 		return -ENOMEM;
16293 	length = (sizeof(struct lpfc_mbx_cq_create) -
16294 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16295 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16296 			 LPFC_MBOX_OPCODE_CQ_CREATE,
16297 			 length, LPFC_SLI4_MBX_EMBED);
16298 	cq_create = &mbox->u.mqe.un.cq_create;
16299 	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
16300 	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16301 		    cq->page_count);
16302 	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16303 	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
16304 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16305 	       phba->sli4_hba.pc_sli4_params.cqv);
16306 	if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
16307 		bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16308 		       (cq->page_size / SLI4_PAGE_SIZE));
16309 		bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16310 		       eq->queue_id);
16311 		bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16312 		       phba->sli4_hba.pc_sli4_params.cqav);
16313 	} else {
16314 		bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16315 		       eq->queue_id);
16316 	}
16317 	switch (cq->entry_count) {
16318 	case 2048:
16319 	case 4096:
16320 		if (phba->sli4_hba.pc_sli4_params.cqv ==
16321 		    LPFC_Q_CREATE_VERSION_2) {
16322 			cq_create->u.request.context.lpfc_cq_context_count =
16323 				cq->entry_count;
16324 			bf_set(lpfc_cq_context_count,
16325 			       &cq_create->u.request.context,
16326 			       LPFC_CQ_CNT_WORD7);
16327 			break;
16328 		}
16329 		fallthrough;
16330 	default:
16331 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16332 				"0361 Unsupported CQ count: "
16333 				"entry cnt %d sz %d pg cnt %d\n",
16334 				cq->entry_count, cq->entry_size,
16335 				cq->page_count);
16336 		if (cq->entry_count < 256) {
16337 			status = -EINVAL;
16338 			goto out;
16339 		}
16340 		fallthrough;	/* otherwise default to smallest count */
16341 	case 256:
16342 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16343 		       LPFC_CQ_CNT_256);
16344 		break;
16345 	case 512:
16346 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16347 		       LPFC_CQ_CNT_512);
16348 		break;
16349 	case 1024:
16350 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16351 		       LPFC_CQ_CNT_1024);
16352 		break;
16353 	}
16354 	list_for_each_entry(dmabuf, &cq->page_list, list) {
16355 		memset(dmabuf->virt, 0, cq->page_size);
16356 		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16357 					putPaddrLow(dmabuf->phys);
16358 		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16359 					putPaddrHigh(dmabuf->phys);
16360 	}
16361 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16362 
16363 	/* The IOCTL status is embedded in the mailbox subheader. */
16364 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16365 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16366 	if (shdr_status || shdr_add_status || rc) {
16367 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16368 				"2501 CQ_CREATE mailbox failed with "
16369 				"status x%x add_status x%x, mbx status x%x\n",
16370 				shdr_status, shdr_add_status, rc);
16371 		status = -ENXIO;
16372 		goto out;
16373 	}
16374 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16375 	if (cq->queue_id == 0xFFFF) {
16376 		status = -ENXIO;
16377 		goto out;
16378 	}
16379 	/* link the cq onto the parent eq child list */
16380 	list_add_tail(&cq->list, &eq->child_list);
16381 	/* Set up completion queue's type and subtype */
16382 	cq->type = type;
16383 	cq->subtype = subtype;
16384 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16385 	cq->assoc_qid = eq->queue_id;
16386 	cq->assoc_qp = eq;
16387 	cq->host_index = 0;
16388 	cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16389 	cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
16390 
16391 	if (cq->queue_id > phba->sli4_hba.cq_max)
16392 		phba->sli4_hba.cq_max = cq->queue_id;
16393 out:
16394 	mempool_free(mbox, phba->mbox_mem_pool);
16395 	return status;
16396 }
16397 
16398 /**
16399  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16400  * @phba: HBA structure that indicates port to create a queue on.
16401  * @cqp: The queue structure array to use to create the completion queues.
16402  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
16403  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16404  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16405  *
16406  * This function creates a set of  completion queue, s to support MRQ
16407  * as detailed in @cqp, on a port,
16408  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16409  *
16410  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16411  * is used to get the entry count and entry size that are necessary to
16412  * determine the number of pages to allocate and use for this queue. The @eq
16413  * is used to indicate which event queue to bind this completion queue to. This
16414  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16415  * completion queue. This function is asynchronous and will wait for the mailbox
16416  * command to finish before continuing.
16417  *
16418  * On success this function will return a zero. If unable to allocate enough
16419  * memory this function will return -ENOMEM. If the queue create mailbox command
16420  * fails this function will return -ENXIO.
16421  **/
16422 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)16423 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
16424 		   struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16425 		   uint32_t subtype)
16426 {
16427 	struct lpfc_queue *cq;
16428 	struct lpfc_queue *eq;
16429 	struct lpfc_mbx_cq_create_set *cq_set;
16430 	struct lpfc_dmabuf *dmabuf;
16431 	LPFC_MBOXQ_t *mbox;
16432 	int rc, length, alloclen, status = 0;
16433 	int cnt, idx, numcq, page_idx = 0;
16434 	uint32_t shdr_status, shdr_add_status;
16435 	union lpfc_sli4_cfg_shdr *shdr;
16436 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16437 
16438 	/* sanity check on queue memory */
16439 	numcq = phba->cfg_nvmet_mrq;
16440 	if (!cqp || !hdwq || !numcq)
16441 		return -ENODEV;
16442 
16443 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16444 	if (!mbox)
16445 		return -ENOMEM;
16446 
16447 	length = sizeof(struct lpfc_mbx_cq_create_set);
16448 	length += ((numcq * cqp[0]->page_count) *
16449 		   sizeof(struct dma_address));
16450 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16451 			LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16452 			LPFC_SLI4_MBX_NEMBED);
16453 	if (alloclen < length) {
16454 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16455 				"3098 Allocated DMA memory size (%d) is "
16456 				"less than the requested DMA memory size "
16457 				"(%d)\n", alloclen, length);
16458 		status = -ENOMEM;
16459 		goto out;
16460 	}
16461 	cq_set = mbox->sge_array->addr[0];
16462 	shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16463 	bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16464 
16465 	for (idx = 0; idx < numcq; idx++) {
16466 		cq = cqp[idx];
16467 		eq = hdwq[idx].hba_eq;
16468 		if (!cq || !eq) {
16469 			status = -ENOMEM;
16470 			goto out;
16471 		}
16472 		if (!phba->sli4_hba.pc_sli4_params.supported)
16473 			hw_page_size = cq->page_size;
16474 
16475 		switch (idx) {
16476 		case 0:
16477 			bf_set(lpfc_mbx_cq_create_set_page_size,
16478 			       &cq_set->u.request,
16479 			       (hw_page_size / SLI4_PAGE_SIZE));
16480 			bf_set(lpfc_mbx_cq_create_set_num_pages,
16481 			       &cq_set->u.request, cq->page_count);
16482 			bf_set(lpfc_mbx_cq_create_set_evt,
16483 			       &cq_set->u.request, 1);
16484 			bf_set(lpfc_mbx_cq_create_set_valid,
16485 			       &cq_set->u.request, 1);
16486 			bf_set(lpfc_mbx_cq_create_set_cqe_size,
16487 			       &cq_set->u.request, 0);
16488 			bf_set(lpfc_mbx_cq_create_set_num_cq,
16489 			       &cq_set->u.request, numcq);
16490 			bf_set(lpfc_mbx_cq_create_set_autovalid,
16491 			       &cq_set->u.request,
16492 			       phba->sli4_hba.pc_sli4_params.cqav);
16493 			switch (cq->entry_count) {
16494 			case 2048:
16495 			case 4096:
16496 				if (phba->sli4_hba.pc_sli4_params.cqv ==
16497 				    LPFC_Q_CREATE_VERSION_2) {
16498 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16499 					       &cq_set->u.request,
16500 						cq->entry_count);
16501 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16502 					       &cq_set->u.request,
16503 					       LPFC_CQ_CNT_WORD7);
16504 					break;
16505 				}
16506 				fallthrough;
16507 			default:
16508 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16509 						"3118 Bad CQ count. (%d)\n",
16510 						cq->entry_count);
16511 				if (cq->entry_count < 256) {
16512 					status = -EINVAL;
16513 					goto out;
16514 				}
16515 				fallthrough;	/* otherwise default to smallest */
16516 			case 256:
16517 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16518 				       &cq_set->u.request, LPFC_CQ_CNT_256);
16519 				break;
16520 			case 512:
16521 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16522 				       &cq_set->u.request, LPFC_CQ_CNT_512);
16523 				break;
16524 			case 1024:
16525 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16526 				       &cq_set->u.request, LPFC_CQ_CNT_1024);
16527 				break;
16528 			}
16529 			bf_set(lpfc_mbx_cq_create_set_eq_id0,
16530 			       &cq_set->u.request, eq->queue_id);
16531 			break;
16532 		case 1:
16533 			bf_set(lpfc_mbx_cq_create_set_eq_id1,
16534 			       &cq_set->u.request, eq->queue_id);
16535 			break;
16536 		case 2:
16537 			bf_set(lpfc_mbx_cq_create_set_eq_id2,
16538 			       &cq_set->u.request, eq->queue_id);
16539 			break;
16540 		case 3:
16541 			bf_set(lpfc_mbx_cq_create_set_eq_id3,
16542 			       &cq_set->u.request, eq->queue_id);
16543 			break;
16544 		case 4:
16545 			bf_set(lpfc_mbx_cq_create_set_eq_id4,
16546 			       &cq_set->u.request, eq->queue_id);
16547 			break;
16548 		case 5:
16549 			bf_set(lpfc_mbx_cq_create_set_eq_id5,
16550 			       &cq_set->u.request, eq->queue_id);
16551 			break;
16552 		case 6:
16553 			bf_set(lpfc_mbx_cq_create_set_eq_id6,
16554 			       &cq_set->u.request, eq->queue_id);
16555 			break;
16556 		case 7:
16557 			bf_set(lpfc_mbx_cq_create_set_eq_id7,
16558 			       &cq_set->u.request, eq->queue_id);
16559 			break;
16560 		case 8:
16561 			bf_set(lpfc_mbx_cq_create_set_eq_id8,
16562 			       &cq_set->u.request, eq->queue_id);
16563 			break;
16564 		case 9:
16565 			bf_set(lpfc_mbx_cq_create_set_eq_id9,
16566 			       &cq_set->u.request, eq->queue_id);
16567 			break;
16568 		case 10:
16569 			bf_set(lpfc_mbx_cq_create_set_eq_id10,
16570 			       &cq_set->u.request, eq->queue_id);
16571 			break;
16572 		case 11:
16573 			bf_set(lpfc_mbx_cq_create_set_eq_id11,
16574 			       &cq_set->u.request, eq->queue_id);
16575 			break;
16576 		case 12:
16577 			bf_set(lpfc_mbx_cq_create_set_eq_id12,
16578 			       &cq_set->u.request, eq->queue_id);
16579 			break;
16580 		case 13:
16581 			bf_set(lpfc_mbx_cq_create_set_eq_id13,
16582 			       &cq_set->u.request, eq->queue_id);
16583 			break;
16584 		case 14:
16585 			bf_set(lpfc_mbx_cq_create_set_eq_id14,
16586 			       &cq_set->u.request, eq->queue_id);
16587 			break;
16588 		case 15:
16589 			bf_set(lpfc_mbx_cq_create_set_eq_id15,
16590 			       &cq_set->u.request, eq->queue_id);
16591 			break;
16592 		}
16593 
16594 		/* link the cq onto the parent eq child list */
16595 		list_add_tail(&cq->list, &eq->child_list);
16596 		/* Set up completion queue's type and subtype */
16597 		cq->type = type;
16598 		cq->subtype = subtype;
16599 		cq->assoc_qid = eq->queue_id;
16600 		cq->assoc_qp = eq;
16601 		cq->host_index = 0;
16602 		cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16603 		cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16604 					 cq->entry_count);
16605 		cq->chann = idx;
16606 
16607 		rc = 0;
16608 		list_for_each_entry(dmabuf, &cq->page_list, list) {
16609 			memset(dmabuf->virt, 0, hw_page_size);
16610 			cnt = page_idx + dmabuf->buffer_tag;
16611 			cq_set->u.request.page[cnt].addr_lo =
16612 					putPaddrLow(dmabuf->phys);
16613 			cq_set->u.request.page[cnt].addr_hi =
16614 					putPaddrHigh(dmabuf->phys);
16615 			rc++;
16616 		}
16617 		page_idx += rc;
16618 	}
16619 
16620 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16621 
16622 	/* The IOCTL status is embedded in the mailbox subheader. */
16623 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16624 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16625 	if (shdr_status || shdr_add_status || rc) {
16626 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16627 				"3119 CQ_CREATE_SET mailbox failed with "
16628 				"status x%x add_status x%x, mbx status x%x\n",
16629 				shdr_status, shdr_add_status, rc);
16630 		status = -ENXIO;
16631 		goto out;
16632 	}
16633 	rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16634 	if (rc == 0xFFFF) {
16635 		status = -ENXIO;
16636 		goto out;
16637 	}
16638 
16639 	for (idx = 0; idx < numcq; idx++) {
16640 		cq = cqp[idx];
16641 		cq->queue_id = rc + idx;
16642 		if (cq->queue_id > phba->sli4_hba.cq_max)
16643 			phba->sli4_hba.cq_max = cq->queue_id;
16644 	}
16645 
16646 out:
16647 	lpfc_sli4_mbox_cmd_free(phba, mbox);
16648 	return status;
16649 }
16650 
16651 /**
16652  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
16653  * @phba: HBA structure that indicates port to create a queue on.
16654  * @mq: The queue structure to use to create the mailbox queue.
16655  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16656  * @cq: The completion queue to associate with this cq.
16657  *
16658  * This function provides failback (fb) functionality when the
16659  * mq_create_ext fails on older FW generations.  It's purpose is identical
16660  * to mq_create_ext otherwise.
16661  *
16662  * This routine cannot fail as all attributes were previously accessed and
16663  * initialized in mq_create_ext.
16664  **/
16665 static void
lpfc_mq_create_fb_init(struct lpfc_hba * phba,struct lpfc_queue * mq,LPFC_MBOXQ_t * mbox,struct lpfc_queue * cq)16666 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16667 		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
16668 {
16669 	struct lpfc_mbx_mq_create *mq_create;
16670 	struct lpfc_dmabuf *dmabuf;
16671 	int length;
16672 
16673 	length = (sizeof(struct lpfc_mbx_mq_create) -
16674 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16675 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16676 			 LPFC_MBOX_OPCODE_MQ_CREATE,
16677 			 length, LPFC_SLI4_MBX_EMBED);
16678 	mq_create = &mbox->u.mqe.un.mq_create;
16679 	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
16680 	       mq->page_count);
16681 	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
16682 	       cq->queue_id);
16683 	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16684 	switch (mq->entry_count) {
16685 	case 16:
16686 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16687 		       LPFC_MQ_RING_SIZE_16);
16688 		break;
16689 	case 32:
16690 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16691 		       LPFC_MQ_RING_SIZE_32);
16692 		break;
16693 	case 64:
16694 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16695 		       LPFC_MQ_RING_SIZE_64);
16696 		break;
16697 	case 128:
16698 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16699 		       LPFC_MQ_RING_SIZE_128);
16700 		break;
16701 	}
16702 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16703 		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16704 			putPaddrLow(dmabuf->phys);
16705 		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16706 			putPaddrHigh(dmabuf->phys);
16707 	}
16708 }
16709 
16710 /**
16711  * lpfc_mq_create - Create a mailbox Queue on the HBA
16712  * @phba: HBA structure that indicates port to create a queue on.
16713  * @mq: The queue structure to use to create the mailbox queue.
16714  * @cq: The completion queue to associate with this cq.
16715  * @subtype: The queue's subtype.
16716  *
16717  * This function creates a mailbox queue, as detailed in @mq, on a port,
16718  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16719  *
16720  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16721  * is used to get the entry count and entry size that are necessary to
16722  * determine the number of pages to allocate and use for this queue. This
16723  * function will send the MQ_CREATE mailbox command to the HBA to setup the
16724  * mailbox queue. This function is asynchronous and will wait for the mailbox
16725  * command to finish before continuing.
16726  *
16727  * On success this function will return a zero. If unable to allocate enough
16728  * memory this function will return -ENOMEM. If the queue create mailbox command
16729  * fails this function will return -ENXIO.
16730  **/
16731 int32_t
lpfc_mq_create(struct lpfc_hba * phba,struct lpfc_queue * mq,struct lpfc_queue * cq,uint32_t subtype)16732 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16733 	       struct lpfc_queue *cq, uint32_t subtype)
16734 {
16735 	struct lpfc_mbx_mq_create *mq_create;
16736 	struct lpfc_mbx_mq_create_ext *mq_create_ext;
16737 	struct lpfc_dmabuf *dmabuf;
16738 	LPFC_MBOXQ_t *mbox;
16739 	int rc, length, status = 0;
16740 	uint32_t shdr_status, shdr_add_status;
16741 	union lpfc_sli4_cfg_shdr *shdr;
16742 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16743 
16744 	/* sanity check on queue memory */
16745 	if (!mq || !cq)
16746 		return -ENODEV;
16747 	if (!phba->sli4_hba.pc_sli4_params.supported)
16748 		hw_page_size = SLI4_PAGE_SIZE;
16749 
16750 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16751 	if (!mbox)
16752 		return -ENOMEM;
16753 	length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16754 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16755 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16756 			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16757 			 length, LPFC_SLI4_MBX_EMBED);
16758 
16759 	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
16760 	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
16761 	bf_set(lpfc_mbx_mq_create_ext_num_pages,
16762 	       &mq_create_ext->u.request, mq->page_count);
16763 	bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16764 	       &mq_create_ext->u.request, 1);
16765 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
16766 	       &mq_create_ext->u.request, 1);
16767 	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16768 	       &mq_create_ext->u.request, 1);
16769 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16770 	       &mq_create_ext->u.request, 1);
16771 	bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16772 	       &mq_create_ext->u.request, 1);
16773 	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
16774 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16775 	       phba->sli4_hba.pc_sli4_params.mqv);
16776 	if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16777 		bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16778 		       cq->queue_id);
16779 	else
16780 		bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16781 		       cq->queue_id);
16782 	switch (mq->entry_count) {
16783 	default:
16784 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16785 				"0362 Unsupported MQ count. (%d)\n",
16786 				mq->entry_count);
16787 		if (mq->entry_count < 16) {
16788 			status = -EINVAL;
16789 			goto out;
16790 		}
16791 		fallthrough;	/* otherwise default to smallest count */
16792 	case 16:
16793 		bf_set(lpfc_mq_context_ring_size,
16794 		       &mq_create_ext->u.request.context,
16795 		       LPFC_MQ_RING_SIZE_16);
16796 		break;
16797 	case 32:
16798 		bf_set(lpfc_mq_context_ring_size,
16799 		       &mq_create_ext->u.request.context,
16800 		       LPFC_MQ_RING_SIZE_32);
16801 		break;
16802 	case 64:
16803 		bf_set(lpfc_mq_context_ring_size,
16804 		       &mq_create_ext->u.request.context,
16805 		       LPFC_MQ_RING_SIZE_64);
16806 		break;
16807 	case 128:
16808 		bf_set(lpfc_mq_context_ring_size,
16809 		       &mq_create_ext->u.request.context,
16810 		       LPFC_MQ_RING_SIZE_128);
16811 		break;
16812 	}
16813 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16814 		memset(dmabuf->virt, 0, hw_page_size);
16815 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16816 					putPaddrLow(dmabuf->phys);
16817 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16818 					putPaddrHigh(dmabuf->phys);
16819 	}
16820 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16821 	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16822 			      &mq_create_ext->u.response);
16823 	if (rc != MBX_SUCCESS) {
16824 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16825 				"2795 MQ_CREATE_EXT failed with "
16826 				"status x%x. Failback to MQ_CREATE.\n",
16827 				rc);
16828 		lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16829 		mq_create = &mbox->u.mqe.un.mq_create;
16830 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16831 		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16832 		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16833 				      &mq_create->u.response);
16834 	}
16835 
16836 	/* The IOCTL status is embedded in the mailbox subheader. */
16837 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16838 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16839 	if (shdr_status || shdr_add_status || rc) {
16840 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16841 				"2502 MQ_CREATE mailbox failed with "
16842 				"status x%x add_status x%x, mbx status x%x\n",
16843 				shdr_status, shdr_add_status, rc);
16844 		status = -ENXIO;
16845 		goto out;
16846 	}
16847 	if (mq->queue_id == 0xFFFF) {
16848 		status = -ENXIO;
16849 		goto out;
16850 	}
16851 	mq->type = LPFC_MQ;
16852 	mq->assoc_qid = cq->queue_id;
16853 	mq->subtype = subtype;
16854 	mq->host_index = 0;
16855 	mq->hba_index = 0;
16856 
16857 	/* link the mq onto the parent cq child list */
16858 	list_add_tail(&mq->list, &cq->child_list);
16859 out:
16860 	mempool_free(mbox, phba->mbox_mem_pool);
16861 	return status;
16862 }
16863 
16864 /**
16865  * lpfc_wq_create - Create a Work Queue on the HBA
16866  * @phba: HBA structure that indicates port to create a queue on.
16867  * @wq: The queue structure to use to create the work queue.
16868  * @cq: The completion queue to bind this work queue to.
16869  * @subtype: The subtype of the work queue indicating its functionality.
16870  *
16871  * This function creates a work queue, as detailed in @wq, on a port, described
16872  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16873  *
16874  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16875  * is used to get the entry count and entry size that are necessary to
16876  * determine the number of pages to allocate and use for this queue. The @cq
16877  * is used to indicate which completion queue to bind this work queue to. This
16878  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16879  * work queue. This function is asynchronous and will wait for the mailbox
16880  * command to finish before continuing.
16881  *
16882  * On success this function will return a zero. If unable to allocate enough
16883  * memory this function will return -ENOMEM. If the queue create mailbox command
16884  * fails this function will return -ENXIO.
16885  **/
16886 int
lpfc_wq_create(struct lpfc_hba * phba,struct lpfc_queue * wq,struct lpfc_queue * cq,uint32_t subtype)16887 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16888 	       struct lpfc_queue *cq, uint32_t subtype)
16889 {
16890 	struct lpfc_mbx_wq_create *wq_create;
16891 	struct lpfc_dmabuf *dmabuf;
16892 	LPFC_MBOXQ_t *mbox;
16893 	int rc, length, status = 0;
16894 	uint32_t shdr_status, shdr_add_status;
16895 	union lpfc_sli4_cfg_shdr *shdr;
16896 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16897 	struct dma_address *page;
16898 	void __iomem *bar_memmap_p;
16899 	uint32_t db_offset;
16900 	uint16_t pci_barset;
16901 	uint8_t dpp_barset;
16902 	uint32_t dpp_offset;
16903 	uint8_t wq_create_version;
16904 #ifdef CONFIG_X86
16905 	unsigned long pg_addr;
16906 #endif
16907 
16908 	/* sanity check on queue memory */
16909 	if (!wq || !cq)
16910 		return -ENODEV;
16911 	if (!phba->sli4_hba.pc_sli4_params.supported)
16912 		hw_page_size = wq->page_size;
16913 
16914 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16915 	if (!mbox)
16916 		return -ENOMEM;
16917 	length = (sizeof(struct lpfc_mbx_wq_create) -
16918 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16919 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16920 			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16921 			 length, LPFC_SLI4_MBX_EMBED);
16922 	wq_create = &mbox->u.mqe.un.wq_create;
16923 	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16924 	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16925 		    wq->page_count);
16926 	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16927 		    cq->queue_id);
16928 
16929 	/* wqv is the earliest version supported, NOT the latest */
16930 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16931 	       phba->sli4_hba.pc_sli4_params.wqv);
16932 
16933 	if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16934 	    (wq->page_size > SLI4_PAGE_SIZE))
16935 		wq_create_version = LPFC_Q_CREATE_VERSION_1;
16936 	else
16937 		wq_create_version = LPFC_Q_CREATE_VERSION_0;
16938 
16939 	switch (wq_create_version) {
16940 	case LPFC_Q_CREATE_VERSION_1:
16941 		bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16942 		       wq->entry_count);
16943 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16944 		       LPFC_Q_CREATE_VERSION_1);
16945 
16946 		switch (wq->entry_size) {
16947 		default:
16948 		case 64:
16949 			bf_set(lpfc_mbx_wq_create_wqe_size,
16950 			       &wq_create->u.request_1,
16951 			       LPFC_WQ_WQE_SIZE_64);
16952 			break;
16953 		case 128:
16954 			bf_set(lpfc_mbx_wq_create_wqe_size,
16955 			       &wq_create->u.request_1,
16956 			       LPFC_WQ_WQE_SIZE_128);
16957 			break;
16958 		}
16959 		/* Request DPP by default */
16960 		bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
16961 		bf_set(lpfc_mbx_wq_create_page_size,
16962 		       &wq_create->u.request_1,
16963 		       (wq->page_size / SLI4_PAGE_SIZE));
16964 		page = wq_create->u.request_1.page;
16965 		break;
16966 	default:
16967 		page = wq_create->u.request.page;
16968 		break;
16969 	}
16970 
16971 	list_for_each_entry(dmabuf, &wq->page_list, list) {
16972 		memset(dmabuf->virt, 0, hw_page_size);
16973 		page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16974 		page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
16975 	}
16976 
16977 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16978 		bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16979 
16980 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16981 	/* The IOCTL status is embedded in the mailbox subheader. */
16982 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16983 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16984 	if (shdr_status || shdr_add_status || rc) {
16985 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16986 				"2503 WQ_CREATE mailbox failed with "
16987 				"status x%x add_status x%x, mbx status x%x\n",
16988 				shdr_status, shdr_add_status, rc);
16989 		status = -ENXIO;
16990 		goto out;
16991 	}
16992 
16993 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16994 		wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16995 					&wq_create->u.response);
16996 	else
16997 		wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16998 					&wq_create->u.response_1);
16999 
17000 	if (wq->queue_id == 0xFFFF) {
17001 		status = -ENXIO;
17002 		goto out;
17003 	}
17004 
17005 	wq->db_format = LPFC_DB_LIST_FORMAT;
17006 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
17007 		if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17008 			wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
17009 					       &wq_create->u.response);
17010 			if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
17011 			    (wq->db_format != LPFC_DB_RING_FORMAT)) {
17012 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17013 						"3265 WQ[%d] doorbell format "
17014 						"not supported: x%x\n",
17015 						wq->queue_id, wq->db_format);
17016 				status = -EINVAL;
17017 				goto out;
17018 			}
17019 			pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
17020 					    &wq_create->u.response);
17021 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17022 								   pci_barset);
17023 			if (!bar_memmap_p) {
17024 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17025 						"3263 WQ[%d] failed to memmap "
17026 						"pci barset:x%x\n",
17027 						wq->queue_id, pci_barset);
17028 				status = -ENOMEM;
17029 				goto out;
17030 			}
17031 			db_offset = wq_create->u.response.doorbell_offset;
17032 			if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
17033 			    (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
17034 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17035 						"3252 WQ[%d] doorbell offset "
17036 						"not supported: x%x\n",
17037 						wq->queue_id, db_offset);
17038 				status = -EINVAL;
17039 				goto out;
17040 			}
17041 			wq->db_regaddr = bar_memmap_p + db_offset;
17042 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17043 					"3264 WQ[%d]: barset:x%x, offset:x%x, "
17044 					"format:x%x\n", wq->queue_id,
17045 					pci_barset, db_offset, wq->db_format);
17046 		} else
17047 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17048 	} else {
17049 		/* Check if DPP was honored by the firmware */
17050 		wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
17051 				    &wq_create->u.response_1);
17052 		if (wq->dpp_enable) {
17053 			pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
17054 					    &wq_create->u.response_1);
17055 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17056 								   pci_barset);
17057 			if (!bar_memmap_p) {
17058 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17059 						"3267 WQ[%d] failed to memmap "
17060 						"pci barset:x%x\n",
17061 						wq->queue_id, pci_barset);
17062 				status = -ENOMEM;
17063 				goto out;
17064 			}
17065 			db_offset = wq_create->u.response_1.doorbell_offset;
17066 			wq->db_regaddr = bar_memmap_p + db_offset;
17067 			wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
17068 					    &wq_create->u.response_1);
17069 			dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
17070 					    &wq_create->u.response_1);
17071 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17072 								   dpp_barset);
17073 			if (!bar_memmap_p) {
17074 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17075 						"3268 WQ[%d] failed to memmap "
17076 						"pci barset:x%x\n",
17077 						wq->queue_id, dpp_barset);
17078 				status = -ENOMEM;
17079 				goto out;
17080 			}
17081 			dpp_offset = wq_create->u.response_1.dpp_offset;
17082 			wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17083 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17084 					"3271 WQ[%d]: barset:x%x, offset:x%x, "
17085 					"dpp_id:x%x dpp_barset:x%x "
17086 					"dpp_offset:x%x\n",
17087 					wq->queue_id, pci_barset, db_offset,
17088 					wq->dpp_id, dpp_barset, dpp_offset);
17089 
17090 #ifdef CONFIG_X86
17091 			/* Enable combined writes for DPP aperture */
17092 			pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
17093 			rc = set_memory_wc(pg_addr, 1);
17094 			if (rc) {
17095 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17096 					"3272 Cannot setup Combined "
17097 					"Write on WQ[%d] - disable DPP\n",
17098 					wq->queue_id);
17099 				phba->cfg_enable_dpp = 0;
17100 			}
17101 #else
17102 			phba->cfg_enable_dpp = 0;
17103 #endif
17104 		} else
17105 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17106 	}
17107 	wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17108 	if (wq->pring == NULL) {
17109 		status = -ENOMEM;
17110 		goto out;
17111 	}
17112 	wq->type = LPFC_WQ;
17113 	wq->assoc_qid = cq->queue_id;
17114 	wq->subtype = subtype;
17115 	wq->host_index = 0;
17116 	wq->hba_index = 0;
17117 	wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
17118 
17119 	/* link the wq onto the parent cq child list */
17120 	list_add_tail(&wq->list, &cq->child_list);
17121 out:
17122 	mempool_free(mbox, phba->mbox_mem_pool);
17123 	return status;
17124 }
17125 
17126 /**
17127  * lpfc_rq_create - Create a Receive Queue on the HBA
17128  * @phba: HBA structure that indicates port to create a queue on.
17129  * @hrq: The queue structure to use to create the header receive queue.
17130  * @drq: The queue structure to use to create the data receive queue.
17131  * @cq: The completion queue to bind this work queue to.
17132  * @subtype: The subtype of the work queue indicating its functionality.
17133  *
17134  * This function creates a receive buffer queue pair , as detailed in @hrq and
17135  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17136  * to the HBA.
17137  *
17138  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17139  * struct is used to get the entry count that is necessary to determine the
17140  * number of pages to use for this queue. The @cq is used to indicate which
17141  * completion queue to bind received buffers that are posted to these queues to.
17142  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17143  * receive queue pair. This function is asynchronous and will wait for the
17144  * mailbox command to finish before continuing.
17145  *
17146  * On success this function will return a zero. If unable to allocate enough
17147  * memory this function will return -ENOMEM. If the queue create mailbox command
17148  * fails this function will return -ENXIO.
17149  **/
17150 int
lpfc_rq_create(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,struct lpfc_queue * cq,uint32_t subtype)17151 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17152 	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17153 {
17154 	struct lpfc_mbx_rq_create *rq_create;
17155 	struct lpfc_dmabuf *dmabuf;
17156 	LPFC_MBOXQ_t *mbox;
17157 	int rc, length, status = 0;
17158 	uint32_t shdr_status, shdr_add_status;
17159 	union lpfc_sli4_cfg_shdr *shdr;
17160 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17161 	void __iomem *bar_memmap_p;
17162 	uint32_t db_offset;
17163 	uint16_t pci_barset;
17164 
17165 	/* sanity check on queue memory */
17166 	if (!hrq || !drq || !cq)
17167 		return -ENODEV;
17168 	if (!phba->sli4_hba.pc_sli4_params.supported)
17169 		hw_page_size = SLI4_PAGE_SIZE;
17170 
17171 	if (hrq->entry_count != drq->entry_count)
17172 		return -EINVAL;
17173 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17174 	if (!mbox)
17175 		return -ENOMEM;
17176 	length = (sizeof(struct lpfc_mbx_rq_create) -
17177 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17178 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17179 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17180 			 length, LPFC_SLI4_MBX_EMBED);
17181 	rq_create = &mbox->u.mqe.un.rq_create;
17182 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17183 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17184 	       phba->sli4_hba.pc_sli4_params.rqv);
17185 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17186 		bf_set(lpfc_rq_context_rqe_count_1,
17187 		       &rq_create->u.request.context,
17188 		       hrq->entry_count);
17189 		rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
17190 		bf_set(lpfc_rq_context_rqe_size,
17191 		       &rq_create->u.request.context,
17192 		       LPFC_RQE_SIZE_8);
17193 		bf_set(lpfc_rq_context_page_size,
17194 		       &rq_create->u.request.context,
17195 		       LPFC_RQ_PAGE_SIZE_4096);
17196 	} else {
17197 		switch (hrq->entry_count) {
17198 		default:
17199 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17200 					"2535 Unsupported RQ count. (%d)\n",
17201 					hrq->entry_count);
17202 			if (hrq->entry_count < 512) {
17203 				status = -EINVAL;
17204 				goto out;
17205 			}
17206 			fallthrough;	/* otherwise default to smallest count */
17207 		case 512:
17208 			bf_set(lpfc_rq_context_rqe_count,
17209 			       &rq_create->u.request.context,
17210 			       LPFC_RQ_RING_SIZE_512);
17211 			break;
17212 		case 1024:
17213 			bf_set(lpfc_rq_context_rqe_count,
17214 			       &rq_create->u.request.context,
17215 			       LPFC_RQ_RING_SIZE_1024);
17216 			break;
17217 		case 2048:
17218 			bf_set(lpfc_rq_context_rqe_count,
17219 			       &rq_create->u.request.context,
17220 			       LPFC_RQ_RING_SIZE_2048);
17221 			break;
17222 		case 4096:
17223 			bf_set(lpfc_rq_context_rqe_count,
17224 			       &rq_create->u.request.context,
17225 			       LPFC_RQ_RING_SIZE_4096);
17226 			break;
17227 		}
17228 		bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17229 		       LPFC_HDR_BUF_SIZE);
17230 	}
17231 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17232 	       cq->queue_id);
17233 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17234 	       hrq->page_count);
17235 	list_for_each_entry(dmabuf, &hrq->page_list, list) {
17236 		memset(dmabuf->virt, 0, hw_page_size);
17237 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17238 					putPaddrLow(dmabuf->phys);
17239 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17240 					putPaddrHigh(dmabuf->phys);
17241 	}
17242 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17243 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17244 
17245 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17246 	/* The IOCTL status is embedded in the mailbox subheader. */
17247 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17248 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17249 	if (shdr_status || shdr_add_status || rc) {
17250 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17251 				"2504 RQ_CREATE mailbox failed with "
17252 				"status x%x add_status x%x, mbx status x%x\n",
17253 				shdr_status, shdr_add_status, rc);
17254 		status = -ENXIO;
17255 		goto out;
17256 	}
17257 	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17258 	if (hrq->queue_id == 0xFFFF) {
17259 		status = -ENXIO;
17260 		goto out;
17261 	}
17262 
17263 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17264 		hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17265 					&rq_create->u.response);
17266 		if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17267 		    (hrq->db_format != LPFC_DB_RING_FORMAT)) {
17268 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17269 					"3262 RQ [%d] doorbell format not "
17270 					"supported: x%x\n", hrq->queue_id,
17271 					hrq->db_format);
17272 			status = -EINVAL;
17273 			goto out;
17274 		}
17275 
17276 		pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17277 				    &rq_create->u.response);
17278 		bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17279 		if (!bar_memmap_p) {
17280 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17281 					"3269 RQ[%d] failed to memmap pci "
17282 					"barset:x%x\n", hrq->queue_id,
17283 					pci_barset);
17284 			status = -ENOMEM;
17285 			goto out;
17286 		}
17287 
17288 		db_offset = rq_create->u.response.doorbell_offset;
17289 		if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17290 		    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
17291 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17292 					"3270 RQ[%d] doorbell offset not "
17293 					"supported: x%x\n", hrq->queue_id,
17294 					db_offset);
17295 			status = -EINVAL;
17296 			goto out;
17297 		}
17298 		hrq->db_regaddr = bar_memmap_p + db_offset;
17299 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17300 				"3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17301 				"format:x%x\n", hrq->queue_id, pci_barset,
17302 				db_offset, hrq->db_format);
17303 	} else {
17304 		hrq->db_format = LPFC_DB_RING_FORMAT;
17305 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17306 	}
17307 	hrq->type = LPFC_HRQ;
17308 	hrq->assoc_qid = cq->queue_id;
17309 	hrq->subtype = subtype;
17310 	hrq->host_index = 0;
17311 	hrq->hba_index = 0;
17312 	hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17313 
17314 	/* now create the data queue */
17315 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17316 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17317 			 length, LPFC_SLI4_MBX_EMBED);
17318 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17319 	       phba->sli4_hba.pc_sli4_params.rqv);
17320 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17321 		bf_set(lpfc_rq_context_rqe_count_1,
17322 		       &rq_create->u.request.context, hrq->entry_count);
17323 		if (subtype == LPFC_NVMET)
17324 			rq_create->u.request.context.buffer_size =
17325 				LPFC_NVMET_DATA_BUF_SIZE;
17326 		else
17327 			rq_create->u.request.context.buffer_size =
17328 				LPFC_DATA_BUF_SIZE;
17329 		bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17330 		       LPFC_RQE_SIZE_8);
17331 		bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17332 		       (PAGE_SIZE/SLI4_PAGE_SIZE));
17333 	} else {
17334 		switch (drq->entry_count) {
17335 		default:
17336 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17337 					"2536 Unsupported RQ count. (%d)\n",
17338 					drq->entry_count);
17339 			if (drq->entry_count < 512) {
17340 				status = -EINVAL;
17341 				goto out;
17342 			}
17343 			fallthrough;	/* otherwise default to smallest count */
17344 		case 512:
17345 			bf_set(lpfc_rq_context_rqe_count,
17346 			       &rq_create->u.request.context,
17347 			       LPFC_RQ_RING_SIZE_512);
17348 			break;
17349 		case 1024:
17350 			bf_set(lpfc_rq_context_rqe_count,
17351 			       &rq_create->u.request.context,
17352 			       LPFC_RQ_RING_SIZE_1024);
17353 			break;
17354 		case 2048:
17355 			bf_set(lpfc_rq_context_rqe_count,
17356 			       &rq_create->u.request.context,
17357 			       LPFC_RQ_RING_SIZE_2048);
17358 			break;
17359 		case 4096:
17360 			bf_set(lpfc_rq_context_rqe_count,
17361 			       &rq_create->u.request.context,
17362 			       LPFC_RQ_RING_SIZE_4096);
17363 			break;
17364 		}
17365 		if (subtype == LPFC_NVMET)
17366 			bf_set(lpfc_rq_context_buf_size,
17367 			       &rq_create->u.request.context,
17368 			       LPFC_NVMET_DATA_BUF_SIZE);
17369 		else
17370 			bf_set(lpfc_rq_context_buf_size,
17371 			       &rq_create->u.request.context,
17372 			       LPFC_DATA_BUF_SIZE);
17373 	}
17374 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17375 	       cq->queue_id);
17376 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17377 	       drq->page_count);
17378 	list_for_each_entry(dmabuf, &drq->page_list, list) {
17379 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17380 					putPaddrLow(dmabuf->phys);
17381 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17382 					putPaddrHigh(dmabuf->phys);
17383 	}
17384 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17385 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17386 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17387 	/* The IOCTL status is embedded in the mailbox subheader. */
17388 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17389 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17390 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17391 	if (shdr_status || shdr_add_status || rc) {
17392 		status = -ENXIO;
17393 		goto out;
17394 	}
17395 	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17396 	if (drq->queue_id == 0xFFFF) {
17397 		status = -ENXIO;
17398 		goto out;
17399 	}
17400 	drq->type = LPFC_DRQ;
17401 	drq->assoc_qid = cq->queue_id;
17402 	drq->subtype = subtype;
17403 	drq->host_index = 0;
17404 	drq->hba_index = 0;
17405 	drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17406 
17407 	/* link the header and data RQs onto the parent cq child list */
17408 	list_add_tail(&hrq->list, &cq->child_list);
17409 	list_add_tail(&drq->list, &cq->child_list);
17410 
17411 out:
17412 	mempool_free(mbox, phba->mbox_mem_pool);
17413 	return status;
17414 }
17415 
17416 /**
17417  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17418  * @phba: HBA structure that indicates port to create a queue on.
17419  * @hrqp: The queue structure array to use to create the header receive queues.
17420  * @drqp: The queue structure array to use to create the data receive queues.
17421  * @cqp: The completion queue array to bind these receive queues to.
17422  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
17423  *
17424  * This function creates a receive buffer queue pair , as detailed in @hrq and
17425  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17426  * to the HBA.
17427  *
17428  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17429  * struct is used to get the entry count that is necessary to determine the
17430  * number of pages to use for this queue. The @cq is used to indicate which
17431  * completion queue to bind received buffers that are posted to these queues to.
17432  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17433  * receive queue pair. This function is asynchronous and will wait for the
17434  * mailbox command to finish before continuing.
17435  *
17436  * On success this function will return a zero. If unable to allocate enough
17437  * memory this function will return -ENOMEM. If the queue create mailbox command
17438  * fails this function will return -ENXIO.
17439  **/
17440 int
lpfc_mrq_create(struct lpfc_hba * phba,struct lpfc_queue ** hrqp,struct lpfc_queue ** drqp,struct lpfc_queue ** cqp,uint32_t subtype)17441 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17442 		struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17443 		uint32_t subtype)
17444 {
17445 	struct lpfc_queue *hrq, *drq, *cq;
17446 	struct lpfc_mbx_rq_create_v2 *rq_create;
17447 	struct lpfc_dmabuf *dmabuf;
17448 	LPFC_MBOXQ_t *mbox;
17449 	int rc, length, alloclen, status = 0;
17450 	int cnt, idx, numrq, page_idx = 0;
17451 	uint32_t shdr_status, shdr_add_status;
17452 	union lpfc_sli4_cfg_shdr *shdr;
17453 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17454 
17455 	numrq = phba->cfg_nvmet_mrq;
17456 	/* sanity check on array memory */
17457 	if (!hrqp || !drqp || !cqp || !numrq)
17458 		return -ENODEV;
17459 	if (!phba->sli4_hba.pc_sli4_params.supported)
17460 		hw_page_size = SLI4_PAGE_SIZE;
17461 
17462 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17463 	if (!mbox)
17464 		return -ENOMEM;
17465 
17466 	length = sizeof(struct lpfc_mbx_rq_create_v2);
17467 	length += ((2 * numrq * hrqp[0]->page_count) *
17468 		   sizeof(struct dma_address));
17469 
17470 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17471 				    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17472 				    LPFC_SLI4_MBX_NEMBED);
17473 	if (alloclen < length) {
17474 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17475 				"3099 Allocated DMA memory size (%d) is "
17476 				"less than the requested DMA memory size "
17477 				"(%d)\n", alloclen, length);
17478 		status = -ENOMEM;
17479 		goto out;
17480 	}
17481 
17482 
17483 
17484 	rq_create = mbox->sge_array->addr[0];
17485 	shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17486 
17487 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17488 	cnt = 0;
17489 
17490 	for (idx = 0; idx < numrq; idx++) {
17491 		hrq = hrqp[idx];
17492 		drq = drqp[idx];
17493 		cq  = cqp[idx];
17494 
17495 		/* sanity check on queue memory */
17496 		if (!hrq || !drq || !cq) {
17497 			status = -ENODEV;
17498 			goto out;
17499 		}
17500 
17501 		if (hrq->entry_count != drq->entry_count) {
17502 			status = -EINVAL;
17503 			goto out;
17504 		}
17505 
17506 		if (idx == 0) {
17507 			bf_set(lpfc_mbx_rq_create_num_pages,
17508 			       &rq_create->u.request,
17509 			       hrq->page_count);
17510 			bf_set(lpfc_mbx_rq_create_rq_cnt,
17511 			       &rq_create->u.request, (numrq * 2));
17512 			bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17513 			       1);
17514 			bf_set(lpfc_rq_context_base_cq,
17515 			       &rq_create->u.request.context,
17516 			       cq->queue_id);
17517 			bf_set(lpfc_rq_context_data_size,
17518 			       &rq_create->u.request.context,
17519 			       LPFC_NVMET_DATA_BUF_SIZE);
17520 			bf_set(lpfc_rq_context_hdr_size,
17521 			       &rq_create->u.request.context,
17522 			       LPFC_HDR_BUF_SIZE);
17523 			bf_set(lpfc_rq_context_rqe_count_1,
17524 			       &rq_create->u.request.context,
17525 			       hrq->entry_count);
17526 			bf_set(lpfc_rq_context_rqe_size,
17527 			       &rq_create->u.request.context,
17528 			       LPFC_RQE_SIZE_8);
17529 			bf_set(lpfc_rq_context_page_size,
17530 			       &rq_create->u.request.context,
17531 			       (PAGE_SIZE/SLI4_PAGE_SIZE));
17532 		}
17533 		rc = 0;
17534 		list_for_each_entry(dmabuf, &hrq->page_list, list) {
17535 			memset(dmabuf->virt, 0, hw_page_size);
17536 			cnt = page_idx + dmabuf->buffer_tag;
17537 			rq_create->u.request.page[cnt].addr_lo =
17538 					putPaddrLow(dmabuf->phys);
17539 			rq_create->u.request.page[cnt].addr_hi =
17540 					putPaddrHigh(dmabuf->phys);
17541 			rc++;
17542 		}
17543 		page_idx += rc;
17544 
17545 		rc = 0;
17546 		list_for_each_entry(dmabuf, &drq->page_list, list) {
17547 			memset(dmabuf->virt, 0, hw_page_size);
17548 			cnt = page_idx + dmabuf->buffer_tag;
17549 			rq_create->u.request.page[cnt].addr_lo =
17550 					putPaddrLow(dmabuf->phys);
17551 			rq_create->u.request.page[cnt].addr_hi =
17552 					putPaddrHigh(dmabuf->phys);
17553 			rc++;
17554 		}
17555 		page_idx += rc;
17556 
17557 		hrq->db_format = LPFC_DB_RING_FORMAT;
17558 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17559 		hrq->type = LPFC_HRQ;
17560 		hrq->assoc_qid = cq->queue_id;
17561 		hrq->subtype = subtype;
17562 		hrq->host_index = 0;
17563 		hrq->hba_index = 0;
17564 		hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17565 
17566 		drq->db_format = LPFC_DB_RING_FORMAT;
17567 		drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17568 		drq->type = LPFC_DRQ;
17569 		drq->assoc_qid = cq->queue_id;
17570 		drq->subtype = subtype;
17571 		drq->host_index = 0;
17572 		drq->hba_index = 0;
17573 		drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17574 
17575 		list_add_tail(&hrq->list, &cq->child_list);
17576 		list_add_tail(&drq->list, &cq->child_list);
17577 	}
17578 
17579 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17580 	/* The IOCTL status is embedded in the mailbox subheader. */
17581 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17582 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17583 	if (shdr_status || shdr_add_status || rc) {
17584 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17585 				"3120 RQ_CREATE mailbox failed with "
17586 				"status x%x add_status x%x, mbx status x%x\n",
17587 				shdr_status, shdr_add_status, rc);
17588 		status = -ENXIO;
17589 		goto out;
17590 	}
17591 	rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17592 	if (rc == 0xFFFF) {
17593 		status = -ENXIO;
17594 		goto out;
17595 	}
17596 
17597 	/* Initialize all RQs with associated queue id */
17598 	for (idx = 0; idx < numrq; idx++) {
17599 		hrq = hrqp[idx];
17600 		hrq->queue_id = rc + (2 * idx);
17601 		drq = drqp[idx];
17602 		drq->queue_id = rc + (2 * idx) + 1;
17603 	}
17604 
17605 out:
17606 	lpfc_sli4_mbox_cmd_free(phba, mbox);
17607 	return status;
17608 }
17609 
17610 /**
17611  * lpfc_eq_destroy - Destroy an event Queue on the HBA
17612  * @phba: HBA structure that indicates port to destroy a queue on.
17613  * @eq: The queue structure associated with the queue to destroy.
17614  *
17615  * This function destroys a queue, as detailed in @eq by sending an mailbox
17616  * command, specific to the type of queue, to the HBA.
17617  *
17618  * The @eq struct is used to get the queue ID of the queue to destroy.
17619  *
17620  * On success this function will return a zero. If the queue destroy mailbox
17621  * command fails this function will return -ENXIO.
17622  **/
17623 int
lpfc_eq_destroy(struct lpfc_hba * phba,struct lpfc_queue * eq)17624 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17625 {
17626 	LPFC_MBOXQ_t *mbox;
17627 	int rc, length, status = 0;
17628 	uint32_t shdr_status, shdr_add_status;
17629 	union lpfc_sli4_cfg_shdr *shdr;
17630 
17631 	/* sanity check on queue memory */
17632 	if (!eq)
17633 		return -ENODEV;
17634 
17635 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17636 		goto list_remove;
17637 
17638 	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17639 	if (!mbox)
17640 		return -ENOMEM;
17641 	length = (sizeof(struct lpfc_mbx_eq_destroy) -
17642 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17643 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17644 			 LPFC_MBOX_OPCODE_EQ_DESTROY,
17645 			 length, LPFC_SLI4_MBX_EMBED);
17646 	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17647 	       eq->queue_id);
17648 	mbox->vport = eq->phba->pport;
17649 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17650 
17651 	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17652 	/* The IOCTL status is embedded in the mailbox subheader. */
17653 	shdr = (union lpfc_sli4_cfg_shdr *)
17654 		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17655 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17656 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17657 	if (shdr_status || shdr_add_status || rc) {
17658 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17659 				"2505 EQ_DESTROY mailbox failed with "
17660 				"status x%x add_status x%x, mbx status x%x\n",
17661 				shdr_status, shdr_add_status, rc);
17662 		status = -ENXIO;
17663 	}
17664 	mempool_free(mbox, eq->phba->mbox_mem_pool);
17665 
17666 list_remove:
17667 	/* Remove eq from any list */
17668 	list_del_init(&eq->list);
17669 
17670 	return status;
17671 }
17672 
17673 /**
17674  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
17675  * @phba: HBA structure that indicates port to destroy a queue on.
17676  * @cq: The queue structure associated with the queue to destroy.
17677  *
17678  * This function destroys a queue, as detailed in @cq by sending an mailbox
17679  * command, specific to the type of queue, to the HBA.
17680  *
17681  * The @cq struct is used to get the queue ID of the queue to destroy.
17682  *
17683  * On success this function will return a zero. If the queue destroy mailbox
17684  * command fails this function will return -ENXIO.
17685  **/
17686 int
lpfc_cq_destroy(struct lpfc_hba * phba,struct lpfc_queue * cq)17687 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17688 {
17689 	LPFC_MBOXQ_t *mbox;
17690 	int rc, length, status = 0;
17691 	uint32_t shdr_status, shdr_add_status;
17692 	union lpfc_sli4_cfg_shdr *shdr;
17693 
17694 	/* sanity check on queue memory */
17695 	if (!cq)
17696 		return -ENODEV;
17697 
17698 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17699 		goto list_remove;
17700 
17701 	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17702 	if (!mbox)
17703 		return -ENOMEM;
17704 	length = (sizeof(struct lpfc_mbx_cq_destroy) -
17705 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17706 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17707 			 LPFC_MBOX_OPCODE_CQ_DESTROY,
17708 			 length, LPFC_SLI4_MBX_EMBED);
17709 	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17710 	       cq->queue_id);
17711 	mbox->vport = cq->phba->pport;
17712 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17713 	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17714 	/* The IOCTL status is embedded in the mailbox subheader. */
17715 	shdr = (union lpfc_sli4_cfg_shdr *)
17716 		&mbox->u.mqe.un.wq_create.header.cfg_shdr;
17717 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17718 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17719 	if (shdr_status || shdr_add_status || rc) {
17720 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17721 				"2506 CQ_DESTROY mailbox failed with "
17722 				"status x%x add_status x%x, mbx status x%x\n",
17723 				shdr_status, shdr_add_status, rc);
17724 		status = -ENXIO;
17725 	}
17726 	mempool_free(mbox, cq->phba->mbox_mem_pool);
17727 
17728 list_remove:
17729 	/* Remove cq from any list */
17730 	list_del_init(&cq->list);
17731 	return status;
17732 }
17733 
17734 /**
17735  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
17736  * @phba: HBA structure that indicates port to destroy a queue on.
17737  * @mq: The queue structure associated with the queue to destroy.
17738  *
17739  * This function destroys a queue, as detailed in @mq by sending an mailbox
17740  * command, specific to the type of queue, to the HBA.
17741  *
17742  * The @mq struct is used to get the queue ID of the queue to destroy.
17743  *
17744  * On success this function will return a zero. If the queue destroy mailbox
17745  * command fails this function will return -ENXIO.
17746  **/
17747 int
lpfc_mq_destroy(struct lpfc_hba * phba,struct lpfc_queue * mq)17748 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17749 {
17750 	LPFC_MBOXQ_t *mbox;
17751 	int rc, length, status = 0;
17752 	uint32_t shdr_status, shdr_add_status;
17753 	union lpfc_sli4_cfg_shdr *shdr;
17754 
17755 	/* sanity check on queue memory */
17756 	if (!mq)
17757 		return -ENODEV;
17758 
17759 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17760 		goto list_remove;
17761 
17762 	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17763 	if (!mbox)
17764 		return -ENOMEM;
17765 	length = (sizeof(struct lpfc_mbx_mq_destroy) -
17766 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17767 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17768 			 LPFC_MBOX_OPCODE_MQ_DESTROY,
17769 			 length, LPFC_SLI4_MBX_EMBED);
17770 	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17771 	       mq->queue_id);
17772 	mbox->vport = mq->phba->pport;
17773 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17774 	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17775 	/* The IOCTL status is embedded in the mailbox subheader. */
17776 	shdr = (union lpfc_sli4_cfg_shdr *)
17777 		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17778 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17779 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17780 	if (shdr_status || shdr_add_status || rc) {
17781 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17782 				"2507 MQ_DESTROY mailbox failed with "
17783 				"status x%x add_status x%x, mbx status x%x\n",
17784 				shdr_status, shdr_add_status, rc);
17785 		status = -ENXIO;
17786 	}
17787 	mempool_free(mbox, mq->phba->mbox_mem_pool);
17788 
17789 list_remove:
17790 	/* Remove mq from any list */
17791 	list_del_init(&mq->list);
17792 	return status;
17793 }
17794 
17795 /**
17796  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
17797  * @phba: HBA structure that indicates port to destroy a queue on.
17798  * @wq: The queue structure associated with the queue to destroy.
17799  *
17800  * This function destroys a queue, as detailed in @wq by sending an mailbox
17801  * command, specific to the type of queue, to the HBA.
17802  *
17803  * The @wq struct is used to get the queue ID of the queue to destroy.
17804  *
17805  * On success this function will return a zero. If the queue destroy mailbox
17806  * command fails this function will return -ENXIO.
17807  **/
17808 int
lpfc_wq_destroy(struct lpfc_hba * phba,struct lpfc_queue * wq)17809 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17810 {
17811 	LPFC_MBOXQ_t *mbox;
17812 	int rc, length, status = 0;
17813 	uint32_t shdr_status, shdr_add_status;
17814 	union lpfc_sli4_cfg_shdr *shdr;
17815 
17816 	/* sanity check on queue memory */
17817 	if (!wq)
17818 		return -ENODEV;
17819 
17820 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17821 		goto list_remove;
17822 
17823 	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17824 	if (!mbox)
17825 		return -ENOMEM;
17826 	length = (sizeof(struct lpfc_mbx_wq_destroy) -
17827 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17828 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17829 			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17830 			 length, LPFC_SLI4_MBX_EMBED);
17831 	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17832 	       wq->queue_id);
17833 	mbox->vport = wq->phba->pport;
17834 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17835 	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17836 	shdr = (union lpfc_sli4_cfg_shdr *)
17837 		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17838 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17839 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17840 	if (shdr_status || shdr_add_status || rc) {
17841 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17842 				"2508 WQ_DESTROY mailbox failed with "
17843 				"status x%x add_status x%x, mbx status x%x\n",
17844 				shdr_status, shdr_add_status, rc);
17845 		status = -ENXIO;
17846 	}
17847 	mempool_free(mbox, wq->phba->mbox_mem_pool);
17848 
17849 list_remove:
17850 	/* Remove wq from any list */
17851 	list_del_init(&wq->list);
17852 	kfree(wq->pring);
17853 	wq->pring = NULL;
17854 	return status;
17855 }
17856 
17857 /**
17858  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17859  * @phba: HBA structure that indicates port to destroy a queue on.
17860  * @hrq: The queue structure associated with the queue to destroy.
17861  * @drq: The queue structure associated with the queue to destroy.
17862  *
17863  * This function destroys a queue, as detailed in @rq by sending an mailbox
17864  * command, specific to the type of queue, to the HBA.
17865  *
17866  * The @rq struct is used to get the queue ID of the queue to destroy.
17867  *
17868  * On success this function will return a zero. If the queue destroy mailbox
17869  * command fails this function will return -ENXIO.
17870  **/
17871 int
lpfc_rq_destroy(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq)17872 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17873 		struct lpfc_queue *drq)
17874 {
17875 	LPFC_MBOXQ_t *mbox;
17876 	int rc, length, status = 0;
17877 	uint32_t shdr_status, shdr_add_status;
17878 	union lpfc_sli4_cfg_shdr *shdr;
17879 
17880 	/* sanity check on queue memory */
17881 	if (!hrq || !drq)
17882 		return -ENODEV;
17883 
17884 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17885 		goto list_remove;
17886 
17887 	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17888 	if (!mbox)
17889 		return -ENOMEM;
17890 	length = (sizeof(struct lpfc_mbx_rq_destroy) -
17891 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17892 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17893 			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17894 			 length, LPFC_SLI4_MBX_EMBED);
17895 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17896 	       hrq->queue_id);
17897 	mbox->vport = hrq->phba->pport;
17898 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17899 	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17900 	/* The IOCTL status is embedded in the mailbox subheader. */
17901 	shdr = (union lpfc_sli4_cfg_shdr *)
17902 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17903 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17904 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17905 	if (shdr_status || shdr_add_status || rc) {
17906 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17907 				"2509 RQ_DESTROY mailbox failed with "
17908 				"status x%x add_status x%x, mbx status x%x\n",
17909 				shdr_status, shdr_add_status, rc);
17910 		mempool_free(mbox, hrq->phba->mbox_mem_pool);
17911 		return -ENXIO;
17912 	}
17913 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17914 	       drq->queue_id);
17915 	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17916 	shdr = (union lpfc_sli4_cfg_shdr *)
17917 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17918 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17919 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17920 	if (shdr_status || shdr_add_status || rc) {
17921 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17922 				"2510 RQ_DESTROY mailbox failed with "
17923 				"status x%x add_status x%x, mbx status x%x\n",
17924 				shdr_status, shdr_add_status, rc);
17925 		status = -ENXIO;
17926 	}
17927 	mempool_free(mbox, hrq->phba->mbox_mem_pool);
17928 
17929 list_remove:
17930 	list_del_init(&hrq->list);
17931 	list_del_init(&drq->list);
17932 	return status;
17933 }
17934 
17935 /**
17936  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17937  * @phba: The virtual port for which this call being executed.
17938  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17939  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17940  * @xritag: the xritag that ties this io to the SGL pages.
17941  *
17942  * This routine will post the sgl pages for the IO that has the xritag
17943  * that is in the iocbq structure. The xritag is assigned during iocbq
17944  * creation and persists for as long as the driver is loaded.
17945  * if the caller has fewer than 256 scatter gather segments to map then
17946  * pdma_phys_addr1 should be 0.
17947  * If the caller needs to map more than 256 scatter gather segment then
17948  * pdma_phys_addr1 should be a valid physical address.
17949  * physical address for SGLs must be 64 byte aligned.
17950  * If you are going to map 2 SGL's then the first one must have 256 entries
17951  * the second sgl can have between 1 and 256 entries.
17952  *
17953  * Return codes:
17954  * 	0 - Success
17955  * 	-ENXIO, -ENOMEM - Failure
17956  **/
17957 int
lpfc_sli4_post_sgl(struct lpfc_hba * phba,dma_addr_t pdma_phys_addr0,dma_addr_t pdma_phys_addr1,uint16_t xritag)17958 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17959 		dma_addr_t pdma_phys_addr0,
17960 		dma_addr_t pdma_phys_addr1,
17961 		uint16_t xritag)
17962 {
17963 	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17964 	LPFC_MBOXQ_t *mbox;
17965 	int rc;
17966 	uint32_t shdr_status, shdr_add_status;
17967 	uint32_t mbox_tmo;
17968 	union lpfc_sli4_cfg_shdr *shdr;
17969 
17970 	if (xritag == NO_XRI) {
17971 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17972 				"0364 Invalid param:\n");
17973 		return -EINVAL;
17974 	}
17975 
17976 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17977 	if (!mbox)
17978 		return -ENOMEM;
17979 
17980 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17981 			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17982 			sizeof(struct lpfc_mbx_post_sgl_pages) -
17983 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17984 
17985 	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17986 				&mbox->u.mqe.un.post_sgl_pages;
17987 	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17988 	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17989 
17990 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=
17991 				cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17992 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17993 				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17994 
17995 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=
17996 				cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17997 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17998 				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17999 	if (!phba->sli4_hba.intr_enable)
18000 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18001 	else {
18002 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18003 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18004 	}
18005 	/* The IOCTL status is embedded in the mailbox subheader. */
18006 	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
18007 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18008 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18009 	if (!phba->sli4_hba.intr_enable)
18010 		mempool_free(mbox, phba->mbox_mem_pool);
18011 	else if (rc != MBX_TIMEOUT)
18012 		mempool_free(mbox, phba->mbox_mem_pool);
18013 	if (shdr_status || shdr_add_status || rc) {
18014 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18015 				"2511 POST_SGL mailbox failed with "
18016 				"status x%x add_status x%x, mbx status x%x\n",
18017 				shdr_status, shdr_add_status, rc);
18018 	}
18019 	return 0;
18020 }
18021 
18022 /**
18023  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
18024  * @phba: pointer to lpfc hba data structure.
18025  *
18026  * This routine is invoked to post rpi header templates to the
18027  * HBA consistent with the SLI-4 interface spec.  This routine
18028  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18029  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18030  *
18031  * Returns
18032  *	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18033  *	LPFC_RPI_ALLOC_ERROR if no rpis are available.
18034  **/
18035 static uint16_t
lpfc_sli4_alloc_xri(struct lpfc_hba * phba)18036 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
18037 {
18038 	unsigned long xri;
18039 
18040 	/*
18041 	 * Fetch the next logical xri.  Because this index is logical,
18042 	 * the driver starts at 0 each time.
18043 	 */
18044 	spin_lock_irq(&phba->hbalock);
18045 	xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,
18046 				 phba->sli4_hba.max_cfg_param.max_xri);
18047 	if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
18048 		spin_unlock_irq(&phba->hbalock);
18049 		return NO_XRI;
18050 	} else {
18051 		set_bit(xri, phba->sli4_hba.xri_bmask);
18052 		phba->sli4_hba.max_cfg_param.xri_used++;
18053 	}
18054 	spin_unlock_irq(&phba->hbalock);
18055 	return xri;
18056 }
18057 
18058 /**
18059  * __lpfc_sli4_free_xri - Release an xri for reuse.
18060  * @phba: pointer to lpfc hba data structure.
18061  * @xri: xri to release.
18062  *
18063  * This routine is invoked to release an xri to the pool of
18064  * available rpis maintained by the driver.
18065  **/
18066 static void
__lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18067 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18068 {
18069 	if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
18070 		phba->sli4_hba.max_cfg_param.xri_used--;
18071 	}
18072 }
18073 
18074 /**
18075  * lpfc_sli4_free_xri - Release an xri for reuse.
18076  * @phba: pointer to lpfc hba data structure.
18077  * @xri: xri to release.
18078  *
18079  * This routine is invoked to release an xri to the pool of
18080  * available rpis maintained by the driver.
18081  **/
18082 void
lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18083 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18084 {
18085 	spin_lock_irq(&phba->hbalock);
18086 	__lpfc_sli4_free_xri(phba, xri);
18087 	spin_unlock_irq(&phba->hbalock);
18088 }
18089 
18090 /**
18091  * lpfc_sli4_next_xritag - Get an xritag for the io
18092  * @phba: Pointer to HBA context object.
18093  *
18094  * This function gets an xritag for the iocb. If there is no unused xritag
18095  * it will return 0xffff.
18096  * The function returns the allocated xritag if successful, else returns zero.
18097  * Zero is not a valid xritag.
18098  * The caller is not required to hold any lock.
18099  **/
18100 uint16_t
lpfc_sli4_next_xritag(struct lpfc_hba * phba)18101 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
18102 {
18103 	uint16_t xri_index;
18104 
18105 	xri_index = lpfc_sli4_alloc_xri(phba);
18106 	if (xri_index == NO_XRI)
18107 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18108 				"2004 Failed to allocate XRI.last XRITAG is %d"
18109 				" Max XRI is %d, Used XRI is %d\n",
18110 				xri_index,
18111 				phba->sli4_hba.max_cfg_param.max_xri,
18112 				phba->sli4_hba.max_cfg_param.xri_used);
18113 	return xri_index;
18114 }
18115 
18116 /**
18117  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
18118  * @phba: pointer to lpfc hba data structure.
18119  * @post_sgl_list: pointer to els sgl entry list.
18120  * @post_cnt: number of els sgl entries on the list.
18121  *
18122  * This routine is invoked to post a block of driver's sgl pages to the
18123  * HBA using non-embedded mailbox command. No Lock is held. This routine
18124  * is only called when the driver is loading and after all IO has been
18125  * stopped.
18126  **/
18127 static int
lpfc_sli4_post_sgl_list(struct lpfc_hba * phba,struct list_head * post_sgl_list,int post_cnt)18128 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
18129 			    struct list_head *post_sgl_list,
18130 			    int post_cnt)
18131 {
18132 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
18133 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18134 	struct sgl_page_pairs *sgl_pg_pairs;
18135 	void *viraddr;
18136 	LPFC_MBOXQ_t *mbox;
18137 	uint32_t reqlen, alloclen, pg_pairs;
18138 	uint32_t mbox_tmo;
18139 	uint16_t xritag_start = 0;
18140 	int rc = 0;
18141 	uint32_t shdr_status, shdr_add_status;
18142 	union lpfc_sli4_cfg_shdr *shdr;
18143 
18144 	reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
18145 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18146 	if (reqlen > SLI4_PAGE_SIZE) {
18147 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18148 				"2559 Block sgl registration required DMA "
18149 				"size (%d) great than a page\n", reqlen);
18150 		return -ENOMEM;
18151 	}
18152 
18153 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18154 	if (!mbox)
18155 		return -ENOMEM;
18156 
18157 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18158 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18159 			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18160 			 LPFC_SLI4_MBX_NEMBED);
18161 
18162 	if (alloclen < reqlen) {
18163 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18164 				"0285 Allocated DMA memory size (%d) is "
18165 				"less than the requested DMA memory "
18166 				"size (%d)\n", alloclen, reqlen);
18167 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18168 		return -ENOMEM;
18169 	}
18170 	/* Set up the SGL pages in the non-embedded DMA pages */
18171 	viraddr = mbox->sge_array->addr[0];
18172 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18173 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18174 
18175 	pg_pairs = 0;
18176 	list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
18177 		/* Set up the sge entry */
18178 		sgl_pg_pairs->sgl_pg0_addr_lo =
18179 				cpu_to_le32(putPaddrLow(sglq_entry->phys));
18180 		sgl_pg_pairs->sgl_pg0_addr_hi =
18181 				cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18182 		sgl_pg_pairs->sgl_pg1_addr_lo =
18183 				cpu_to_le32(putPaddrLow(0));
18184 		sgl_pg_pairs->sgl_pg1_addr_hi =
18185 				cpu_to_le32(putPaddrHigh(0));
18186 
18187 		/* Keep the first xritag on the list */
18188 		if (pg_pairs == 0)
18189 			xritag_start = sglq_entry->sli4_xritag;
18190 		sgl_pg_pairs++;
18191 		pg_pairs++;
18192 	}
18193 
18194 	/* Complete initialization and perform endian conversion. */
18195 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18196 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
18197 	sgl->word0 = cpu_to_le32(sgl->word0);
18198 
18199 	if (!phba->sli4_hba.intr_enable)
18200 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18201 	else {
18202 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18203 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18204 	}
18205 	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18206 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18207 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18208 	if (!phba->sli4_hba.intr_enable)
18209 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18210 	else if (rc != MBX_TIMEOUT)
18211 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18212 	if (shdr_status || shdr_add_status || rc) {
18213 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18214 				"2513 POST_SGL_BLOCK mailbox command failed "
18215 				"status x%x add_status x%x mbx status x%x\n",
18216 				shdr_status, shdr_add_status, rc);
18217 		rc = -ENXIO;
18218 	}
18219 	return rc;
18220 }
18221 
18222 /**
18223  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
18224  * @phba: pointer to lpfc hba data structure.
18225  * @nblist: pointer to nvme buffer list.
18226  * @count: number of scsi buffers on the list.
18227  *
18228  * This routine is invoked to post a block of @count scsi sgl pages from a
18229  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
18230  * No Lock is held.
18231  *
18232  **/
18233 static int
lpfc_sli4_post_io_sgl_block(struct lpfc_hba * phba,struct list_head * nblist,int count)18234 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18235 			    int count)
18236 {
18237 	struct lpfc_io_buf *lpfc_ncmd;
18238 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18239 	struct sgl_page_pairs *sgl_pg_pairs;
18240 	void *viraddr;
18241 	LPFC_MBOXQ_t *mbox;
18242 	uint32_t reqlen, alloclen, pg_pairs;
18243 	uint32_t mbox_tmo;
18244 	uint16_t xritag_start = 0;
18245 	int rc = 0;
18246 	uint32_t shdr_status, shdr_add_status;
18247 	dma_addr_t pdma_phys_bpl1;
18248 	union lpfc_sli4_cfg_shdr *shdr;
18249 
18250 	/* Calculate the requested length of the dma memory */
18251 	reqlen = count * sizeof(struct sgl_page_pairs) +
18252 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18253 	if (reqlen > SLI4_PAGE_SIZE) {
18254 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
18255 				"6118 Block sgl registration required DMA "
18256 				"size (%d) great than a page\n", reqlen);
18257 		return -ENOMEM;
18258 	}
18259 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18260 	if (!mbox) {
18261 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18262 				"6119 Failed to allocate mbox cmd memory\n");
18263 		return -ENOMEM;
18264 	}
18265 
18266 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18267 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18268 				    LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18269 				    reqlen, LPFC_SLI4_MBX_NEMBED);
18270 
18271 	if (alloclen < reqlen) {
18272 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18273 				"6120 Allocated DMA memory size (%d) is "
18274 				"less than the requested DMA memory "
18275 				"size (%d)\n", alloclen, reqlen);
18276 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18277 		return -ENOMEM;
18278 	}
18279 
18280 	/* Get the first SGE entry from the non-embedded DMA memory */
18281 	viraddr = mbox->sge_array->addr[0];
18282 
18283 	/* Set up the SGL pages in the non-embedded DMA pages */
18284 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18285 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18286 
18287 	pg_pairs = 0;
18288 	list_for_each_entry(lpfc_ncmd, nblist, list) {
18289 		/* Set up the sge entry */
18290 		sgl_pg_pairs->sgl_pg0_addr_lo =
18291 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
18292 		sgl_pg_pairs->sgl_pg0_addr_hi =
18293 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
18294 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
18295 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18296 						SGL_PAGE_SIZE;
18297 		else
18298 			pdma_phys_bpl1 = 0;
18299 		sgl_pg_pairs->sgl_pg1_addr_lo =
18300 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18301 		sgl_pg_pairs->sgl_pg1_addr_hi =
18302 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18303 		/* Keep the first xritag on the list */
18304 		if (pg_pairs == 0)
18305 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
18306 		sgl_pg_pairs++;
18307 		pg_pairs++;
18308 	}
18309 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18310 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18311 	/* Perform endian conversion if necessary */
18312 	sgl->word0 = cpu_to_le32(sgl->word0);
18313 
18314 	if (!phba->sli4_hba.intr_enable) {
18315 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18316 	} else {
18317 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18318 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18319 	}
18320 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
18321 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18322 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18323 	if (!phba->sli4_hba.intr_enable)
18324 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18325 	else if (rc != MBX_TIMEOUT)
18326 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18327 	if (shdr_status || shdr_add_status || rc) {
18328 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18329 				"6125 POST_SGL_BLOCK mailbox command failed "
18330 				"status x%x add_status x%x mbx status x%x\n",
18331 				shdr_status, shdr_add_status, rc);
18332 		rc = -ENXIO;
18333 	}
18334 	return rc;
18335 }
18336 
18337 /**
18338  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
18339  * @phba: pointer to lpfc hba data structure.
18340  * @post_nblist: pointer to the nvme buffer list.
18341  * @sb_count: number of nvme buffers.
18342  *
18343  * This routine walks a list of nvme buffers that was passed in. It attempts
18344  * to construct blocks of nvme buffer sgls which contains contiguous xris and
18345  * uses the non-embedded SGL block post mailbox commands to post to the port.
18346  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18347  * embedded SGL post mailbox command for posting. The @post_nblist passed in
18348  * must be local list, thus no lock is needed when manipulate the list.
18349  *
18350  * Returns: 0 = failure, non-zero number of successfully posted buffers.
18351  **/
18352 int
lpfc_sli4_post_io_sgl_list(struct lpfc_hba * phba,struct list_head * post_nblist,int sb_count)18353 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18354 			   struct list_head *post_nblist, int sb_count)
18355 {
18356 	struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
18357 	int status, sgl_size;
18358 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18359 	dma_addr_t pdma_phys_sgl1;
18360 	int last_xritag = NO_XRI;
18361 	int cur_xritag;
18362 	LIST_HEAD(prep_nblist);
18363 	LIST_HEAD(blck_nblist);
18364 	LIST_HEAD(nvme_nblist);
18365 
18366 	/* sanity check */
18367 	if (sb_count <= 0)
18368 		return -EINVAL;
18369 
18370 	sgl_size = phba->cfg_sg_dma_buf_size;
18371 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18372 		list_del_init(&lpfc_ncmd->list);
18373 		block_cnt++;
18374 		if ((last_xritag != NO_XRI) &&
18375 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18376 			/* a hole in xri block, form a sgl posting block */
18377 			list_splice_init(&prep_nblist, &blck_nblist);
18378 			post_cnt = block_cnt - 1;
18379 			/* prepare list for next posting block */
18380 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18381 			block_cnt = 1;
18382 		} else {
18383 			/* prepare list for next posting block */
18384 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18385 			/* enough sgls for non-embed sgl mbox command */
18386 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18387 				list_splice_init(&prep_nblist, &blck_nblist);
18388 				post_cnt = block_cnt;
18389 				block_cnt = 0;
18390 			}
18391 		}
18392 		num_posting++;
18393 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18394 
18395 		/* end of repost sgl list condition for NVME buffers */
18396 		if (num_posting == sb_count) {
18397 			if (post_cnt == 0) {
18398 				/* last sgl posting block */
18399 				list_splice_init(&prep_nblist, &blck_nblist);
18400 				post_cnt = block_cnt;
18401 			} else if (block_cnt == 1) {
18402 				/* last single sgl with non-contiguous xri */
18403 				if (sgl_size > SGL_PAGE_SIZE)
18404 					pdma_phys_sgl1 =
18405 						lpfc_ncmd->dma_phys_sgl +
18406 						SGL_PAGE_SIZE;
18407 				else
18408 					pdma_phys_sgl1 = 0;
18409 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18410 				status = lpfc_sli4_post_sgl(
18411 						phba, lpfc_ncmd->dma_phys_sgl,
18412 						pdma_phys_sgl1, cur_xritag);
18413 				if (status) {
18414 					/* Post error.  Buffer unavailable. */
18415 					lpfc_ncmd->flags |=
18416 						LPFC_SBUF_NOT_POSTED;
18417 				} else {
18418 					/* Post success. Bffer available. */
18419 					lpfc_ncmd->flags &=
18420 						~LPFC_SBUF_NOT_POSTED;
18421 					lpfc_ncmd->status = IOSTAT_SUCCESS;
18422 					num_posted++;
18423 				}
18424 				/* success, put on NVME buffer sgl list */
18425 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18426 			}
18427 		}
18428 
18429 		/* continue until a nembed page worth of sgls */
18430 		if (post_cnt == 0)
18431 			continue;
18432 
18433 		/* post block of NVME buffer list sgls */
18434 		status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18435 						     post_cnt);
18436 
18437 		/* don't reset xirtag due to hole in xri block */
18438 		if (block_cnt == 0)
18439 			last_xritag = NO_XRI;
18440 
18441 		/* reset NVME buffer post count for next round of posting */
18442 		post_cnt = 0;
18443 
18444 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18445 		while (!list_empty(&blck_nblist)) {
18446 			list_remove_head(&blck_nblist, lpfc_ncmd,
18447 					 struct lpfc_io_buf, list);
18448 			if (status) {
18449 				/* Post error.  Mark buffer unavailable. */
18450 				lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
18451 			} else {
18452 				/* Post success, Mark buffer available. */
18453 				lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
18454 				lpfc_ncmd->status = IOSTAT_SUCCESS;
18455 				num_posted++;
18456 			}
18457 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18458 		}
18459 	}
18460 	/* Push NVME buffers with sgl posted to the available list */
18461 	lpfc_io_buf_replenish(phba, &nvme_nblist);
18462 
18463 	return num_posted;
18464 }
18465 
18466 /**
18467  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18468  * @phba: pointer to lpfc_hba struct that the frame was received on
18469  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18470  *
18471  * This function checks the fields in the @fc_hdr to see if the FC frame is a
18472  * valid type of frame that the LPFC driver will handle. This function will
18473  * return a zero if the frame is a valid frame or a non zero value when the
18474  * frame does not pass the check.
18475  **/
18476 static int
lpfc_fc_frame_check(struct lpfc_hba * phba,struct fc_frame_header * fc_hdr)18477 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18478 {
18479 	/*  make rctl_names static to save stack space */
18480 	struct fc_vft_header *fc_vft_hdr;
18481 	uint32_t *header = (uint32_t *) fc_hdr;
18482 
18483 #define FC_RCTL_MDS_DIAGS	0xF4
18484 
18485 	switch (fc_hdr->fh_r_ctl) {
18486 	case FC_RCTL_DD_UNCAT:		/* uncategorized information */
18487 	case FC_RCTL_DD_SOL_DATA:	/* solicited data */
18488 	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */
18489 	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */
18490 	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */
18491 	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */
18492 	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */
18493 	case FC_RCTL_DD_CMD_STATUS:	/* command status */
18494 	case FC_RCTL_ELS_REQ:	/* extended link services request */
18495 	case FC_RCTL_ELS_REP:	/* extended link services reply */
18496 	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */
18497 	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */
18498 	case FC_RCTL_BA_ABTS: 	/* basic link service abort */
18499 	case FC_RCTL_BA_RMC: 	/* remove connection */
18500 	case FC_RCTL_BA_ACC:	/* basic accept */
18501 	case FC_RCTL_BA_RJT:	/* basic reject */
18502 	case FC_RCTL_BA_PRMT:
18503 	case FC_RCTL_ACK_1:	/* acknowledge_1 */
18504 	case FC_RCTL_ACK_0:	/* acknowledge_0 */
18505 	case FC_RCTL_P_RJT:	/* port reject */
18506 	case FC_RCTL_F_RJT:	/* fabric reject */
18507 	case FC_RCTL_P_BSY:	/* port busy */
18508 	case FC_RCTL_F_BSY:	/* fabric busy to data frame */
18509 	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */
18510 	case FC_RCTL_LCR:	/* link credit reset */
18511 	case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
18512 	case FC_RCTL_END:	/* end */
18513 		break;
18514 	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */
18515 		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18516 		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18517 		return lpfc_fc_frame_check(phba, fc_hdr);
18518 	case FC_RCTL_BA_NOP:	/* basic link service NOP */
18519 	default:
18520 		goto drop;
18521 	}
18522 
18523 	switch (fc_hdr->fh_type) {
18524 	case FC_TYPE_BLS:
18525 	case FC_TYPE_ELS:
18526 	case FC_TYPE_FCP:
18527 	case FC_TYPE_CT:
18528 	case FC_TYPE_NVME:
18529 		break;
18530 	case FC_TYPE_IP:
18531 	case FC_TYPE_ILS:
18532 	default:
18533 		goto drop;
18534 	}
18535 
18536 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
18537 			"2538 Received frame rctl:x%x, type:x%x, "
18538 			"frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
18539 			fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18540 			be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18541 			be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18542 			be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18543 			be32_to_cpu(header[6]));
18544 	return 0;
18545 drop:
18546 	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
18547 			"2539 Dropped frame rctl:x%x type:x%x\n",
18548 			fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18549 	return 1;
18550 }
18551 
18552 /**
18553  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18554  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18555  *
18556  * This function processes the FC header to retrieve the VFI from the VF
18557  * header, if one exists. This function will return the VFI if one exists
18558  * or 0 if no VSAN Header exists.
18559  **/
18560 static uint32_t
lpfc_fc_hdr_get_vfi(struct fc_frame_header * fc_hdr)18561 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18562 {
18563 	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18564 
18565 	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18566 		return 0;
18567 	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18568 }
18569 
18570 /**
18571  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18572  * @phba: Pointer to the HBA structure to search for the vport on
18573  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18574  * @fcfi: The FC Fabric ID that the frame came from
18575  * @did: Destination ID to match against
18576  *
18577  * This function searches the @phba for a vport that matches the content of the
18578  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18579  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18580  * returns the matching vport pointer or NULL if unable to match frame to a
18581  * vport.
18582  **/
18583 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)18584 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
18585 		       uint16_t fcfi, uint32_t did)
18586 {
18587 	struct lpfc_vport **vports;
18588 	struct lpfc_vport *vport = NULL;
18589 	int i;
18590 
18591 	if (did == Fabric_DID)
18592 		return phba->pport;
18593 	if ((phba->pport->fc_flag & FC_PT2PT) &&
18594 		!(phba->link_state == LPFC_HBA_READY))
18595 		return phba->pport;
18596 
18597 	vports = lpfc_create_vport_work_array(phba);
18598 	if (vports != NULL) {
18599 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18600 			if (phba->fcf.fcfi == fcfi &&
18601 			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18602 			    vports[i]->fc_myDID == did) {
18603 				vport = vports[i];
18604 				break;
18605 			}
18606 		}
18607 	}
18608 	lpfc_destroy_vport_work_array(phba, vports);
18609 	return vport;
18610 }
18611 
18612 /**
18613  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18614  * @vport: The vport to work on.
18615  *
18616  * This function updates the receive sequence time stamp for this vport. The
18617  * receive sequence time stamp indicates the time that the last frame of the
18618  * the sequence that has been idle for the longest amount of time was received.
18619  * the driver uses this time stamp to indicate if any received sequences have
18620  * timed out.
18621  **/
18622 static void
lpfc_update_rcv_time_stamp(struct lpfc_vport * vport)18623 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18624 {
18625 	struct lpfc_dmabuf *h_buf;
18626 	struct hbq_dmabuf *dmabuf = NULL;
18627 
18628 	/* get the oldest sequence on the rcv list */
18629 	h_buf = list_get_first(&vport->rcv_buffer_list,
18630 			       struct lpfc_dmabuf, list);
18631 	if (!h_buf)
18632 		return;
18633 	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18634 	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18635 }
18636 
18637 /**
18638  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18639  * @vport: The vport that the received sequences were sent to.
18640  *
18641  * This function cleans up all outstanding received sequences. This is called
18642  * by the driver when a link event or user action invalidates all the received
18643  * sequences.
18644  **/
18645 void
lpfc_cleanup_rcv_buffers(struct lpfc_vport * vport)18646 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18647 {
18648 	struct lpfc_dmabuf *h_buf, *hnext;
18649 	struct lpfc_dmabuf *d_buf, *dnext;
18650 	struct hbq_dmabuf *dmabuf = NULL;
18651 
18652 	/* start with the oldest sequence on the rcv list */
18653 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18654 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18655 		list_del_init(&dmabuf->hbuf.list);
18656 		list_for_each_entry_safe(d_buf, dnext,
18657 					 &dmabuf->dbuf.list, list) {
18658 			list_del_init(&d_buf->list);
18659 			lpfc_in_buf_free(vport->phba, d_buf);
18660 		}
18661 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18662 	}
18663 }
18664 
18665 /**
18666  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18667  * @vport: The vport that the received sequences were sent to.
18668  *
18669  * This function determines whether any received sequences have timed out by
18670  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18671  * indicates that there is at least one timed out sequence this routine will
18672  * go through the received sequences one at a time from most inactive to most
18673  * active to determine which ones need to be cleaned up. Once it has determined
18674  * that a sequence needs to be cleaned up it will simply free up the resources
18675  * without sending an abort.
18676  **/
18677 void
lpfc_rcv_seq_check_edtov(struct lpfc_vport * vport)18678 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18679 {
18680 	struct lpfc_dmabuf *h_buf, *hnext;
18681 	struct lpfc_dmabuf *d_buf, *dnext;
18682 	struct hbq_dmabuf *dmabuf = NULL;
18683 	unsigned long timeout;
18684 	int abort_count = 0;
18685 
18686 	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18687 		   vport->rcv_buffer_time_stamp);
18688 	if (list_empty(&vport->rcv_buffer_list) ||
18689 	    time_before(jiffies, timeout))
18690 		return;
18691 	/* start with the oldest sequence on the rcv list */
18692 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18693 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18694 		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18695 			   dmabuf->time_stamp);
18696 		if (time_before(jiffies, timeout))
18697 			break;
18698 		abort_count++;
18699 		list_del_init(&dmabuf->hbuf.list);
18700 		list_for_each_entry_safe(d_buf, dnext,
18701 					 &dmabuf->dbuf.list, list) {
18702 			list_del_init(&d_buf->list);
18703 			lpfc_in_buf_free(vport->phba, d_buf);
18704 		}
18705 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18706 	}
18707 	if (abort_count)
18708 		lpfc_update_rcv_time_stamp(vport);
18709 }
18710 
18711 /**
18712  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
18713  * @vport: pointer to a vitural port
18714  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18715  *
18716  * This function searches through the existing incomplete sequences that have
18717  * been sent to this @vport. If the frame matches one of the incomplete
18718  * sequences then the dbuf in the @dmabuf is added to the list of frames that
18719  * make up that sequence. If no sequence is found that matches this frame then
18720  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18721  * This function returns a pointer to the first dmabuf in the sequence list that
18722  * the frame was linked to.
18723  **/
18724 static struct hbq_dmabuf *
lpfc_fc_frame_add(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18725 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18726 {
18727 	struct fc_frame_header *new_hdr;
18728 	struct fc_frame_header *temp_hdr;
18729 	struct lpfc_dmabuf *d_buf;
18730 	struct lpfc_dmabuf *h_buf;
18731 	struct hbq_dmabuf *seq_dmabuf = NULL;
18732 	struct hbq_dmabuf *temp_dmabuf = NULL;
18733 	uint8_t	found = 0;
18734 
18735 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18736 	dmabuf->time_stamp = jiffies;
18737 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18738 
18739 	/* Use the hdr_buf to find the sequence that this frame belongs to */
18740 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18741 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18742 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18743 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18744 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18745 			continue;
18746 		/* found a pending sequence that matches this frame */
18747 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18748 		break;
18749 	}
18750 	if (!seq_dmabuf) {
18751 		/*
18752 		 * This indicates first frame received for this sequence.
18753 		 * Queue the buffer on the vport's rcv_buffer_list.
18754 		 */
18755 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18756 		lpfc_update_rcv_time_stamp(vport);
18757 		return dmabuf;
18758 	}
18759 	temp_hdr = seq_dmabuf->hbuf.virt;
18760 	if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18761 		be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18762 		list_del_init(&seq_dmabuf->hbuf.list);
18763 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18764 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18765 		lpfc_update_rcv_time_stamp(vport);
18766 		return dmabuf;
18767 	}
18768 	/* move this sequence to the tail to indicate a young sequence */
18769 	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18770 	seq_dmabuf->time_stamp = jiffies;
18771 	lpfc_update_rcv_time_stamp(vport);
18772 	if (list_empty(&seq_dmabuf->dbuf.list)) {
18773 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18774 		return seq_dmabuf;
18775 	}
18776 	/* find the correct place in the sequence to insert this frame */
18777 	d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18778 	while (!found) {
18779 		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18780 		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18781 		/*
18782 		 * If the frame's sequence count is greater than the frame on
18783 		 * the list then insert the frame right after this frame
18784 		 */
18785 		if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18786 			be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18787 			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
18788 			found = 1;
18789 			break;
18790 		}
18791 
18792 		if (&d_buf->list == &seq_dmabuf->dbuf.list)
18793 			break;
18794 		d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
18795 	}
18796 
18797 	if (found)
18798 		return seq_dmabuf;
18799 	return NULL;
18800 }
18801 
18802 /**
18803  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18804  * @vport: pointer to a vitural port
18805  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18806  *
18807  * This function tries to abort from the partially assembed sequence, described
18808  * by the information from basic abbort @dmabuf. It checks to see whether such
18809  * partially assembled sequence held by the driver. If so, it shall free up all
18810  * the frames from the partially assembled sequence.
18811  *
18812  * Return
18813  * true  -- if there is matching partially assembled sequence present and all
18814  *          the frames freed with the sequence;
18815  * false -- if there is no matching partially assembled sequence present so
18816  *          nothing got aborted in the lower layer driver
18817  **/
18818 static bool
lpfc_sli4_abort_partial_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18819 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18820 			    struct hbq_dmabuf *dmabuf)
18821 {
18822 	struct fc_frame_header *new_hdr;
18823 	struct fc_frame_header *temp_hdr;
18824 	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18825 	struct hbq_dmabuf *seq_dmabuf = NULL;
18826 
18827 	/* Use the hdr_buf to find the sequence that matches this frame */
18828 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18829 	INIT_LIST_HEAD(&dmabuf->hbuf.list);
18830 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18831 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18832 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18833 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18834 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18835 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18836 			continue;
18837 		/* found a pending sequence that matches this frame */
18838 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18839 		break;
18840 	}
18841 
18842 	/* Free up all the frames from the partially assembled sequence */
18843 	if (seq_dmabuf) {
18844 		list_for_each_entry_safe(d_buf, n_buf,
18845 					 &seq_dmabuf->dbuf.list, list) {
18846 			list_del_init(&d_buf->list);
18847 			lpfc_in_buf_free(vport->phba, d_buf);
18848 		}
18849 		return true;
18850 	}
18851 	return false;
18852 }
18853 
18854 /**
18855  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18856  * @vport: pointer to a vitural port
18857  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18858  *
18859  * This function tries to abort from the assembed sequence from upper level
18860  * protocol, described by the information from basic abbort @dmabuf. It
18861  * checks to see whether such pending context exists at upper level protocol.
18862  * If so, it shall clean up the pending context.
18863  *
18864  * Return
18865  * true  -- if there is matching pending context of the sequence cleaned
18866  *          at ulp;
18867  * false -- if there is no matching pending context of the sequence present
18868  *          at ulp.
18869  **/
18870 static bool
lpfc_sli4_abort_ulp_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18871 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18872 {
18873 	struct lpfc_hba *phba = vport->phba;
18874 	int handled;
18875 
18876 	/* Accepting abort at ulp with SLI4 only */
18877 	if (phba->sli_rev < LPFC_SLI_REV4)
18878 		return false;
18879 
18880 	/* Register all caring upper level protocols to attend abort */
18881 	handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18882 	if (handled)
18883 		return true;
18884 
18885 	return false;
18886 }
18887 
18888 /**
18889  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18890  * @phba: Pointer to HBA context object.
18891  * @cmd_iocbq: pointer to the command iocbq structure.
18892  * @rsp_iocbq: pointer to the response iocbq structure.
18893  *
18894  * This function handles the sequence abort response iocb command complete
18895  * event. It properly releases the memory allocated to the sequence abort
18896  * accept iocb.
18897  **/
18898 static void
lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmd_iocbq,struct lpfc_iocbq * rsp_iocbq)18899 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18900 			     struct lpfc_iocbq *cmd_iocbq,
18901 			     struct lpfc_iocbq *rsp_iocbq)
18902 {
18903 	if (cmd_iocbq) {
18904 		lpfc_nlp_put(cmd_iocbq->ndlp);
18905 		lpfc_sli_release_iocbq(phba, cmd_iocbq);
18906 	}
18907 
18908 	/* Failure means BLS ABORT RSP did not get delivered to remote node*/
18909 	if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18910 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18911 			"3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18912 			get_job_ulpstatus(phba, rsp_iocbq),
18913 			get_job_word4(phba, rsp_iocbq));
18914 }
18915 
18916 /**
18917  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18918  * @phba: Pointer to HBA context object.
18919  * @xri: xri id in transaction.
18920  *
18921  * This function validates the xri maps to the known range of XRIs allocated an
18922  * used by the driver.
18923  **/
18924 uint16_t
lpfc_sli4_xri_inrange(struct lpfc_hba * phba,uint16_t xri)18925 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18926 		      uint16_t xri)
18927 {
18928 	uint16_t i;
18929 
18930 	for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18931 		if (xri == phba->sli4_hba.xri_ids[i])
18932 			return i;
18933 	}
18934 	return NO_XRI;
18935 }
18936 
18937 /**
18938  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
18939  * @vport: pointer to a virtual port.
18940  * @fc_hdr: pointer to a FC frame header.
18941  * @aborted: was the partially assembled receive sequence successfully aborted
18942  *
18943  * This function sends a basic response to a previous unsol sequence abort
18944  * event after aborting the sequence handling.
18945  **/
18946 void
lpfc_sli4_seq_abort_rsp(struct lpfc_vport * vport,struct fc_frame_header * fc_hdr,bool aborted)18947 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18948 			struct fc_frame_header *fc_hdr, bool aborted)
18949 {
18950 	struct lpfc_hba *phba = vport->phba;
18951 	struct lpfc_iocbq *ctiocb = NULL;
18952 	struct lpfc_nodelist *ndlp;
18953 	uint16_t oxid, rxid, xri, lxri;
18954 	uint32_t sid, fctl;
18955 	union lpfc_wqe128 *icmd;
18956 	int rc;
18957 
18958 	if (!lpfc_is_link_up(phba))
18959 		return;
18960 
18961 	sid = sli4_sid_from_fc_hdr(fc_hdr);
18962 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
18963 	rxid = be16_to_cpu(fc_hdr->fh_rx_id);
18964 
18965 	ndlp = lpfc_findnode_did(vport, sid);
18966 	if (!ndlp) {
18967 		ndlp = lpfc_nlp_init(vport, sid);
18968 		if (!ndlp) {
18969 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18970 					 "1268 Failed to allocate ndlp for "
18971 					 "oxid:x%x SID:x%x\n", oxid, sid);
18972 			return;
18973 		}
18974 		/* Put ndlp onto pport node list */
18975 		lpfc_enqueue_node(vport, ndlp);
18976 	}
18977 
18978 	/* Allocate buffer for rsp iocb */
18979 	ctiocb = lpfc_sli_get_iocbq(phba);
18980 	if (!ctiocb)
18981 		return;
18982 
18983 	icmd = &ctiocb->wqe;
18984 
18985 	/* Extract the F_CTL field from FC_HDR */
18986 	fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18987 
18988 	ctiocb->ndlp = lpfc_nlp_get(ndlp);
18989 	if (!ctiocb->ndlp) {
18990 		lpfc_sli_release_iocbq(phba, ctiocb);
18991 		return;
18992 	}
18993 
18994 	ctiocb->vport = phba->pport;
18995 	ctiocb->cmd_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
18996 	ctiocb->sli4_lxritag = NO_XRI;
18997 	ctiocb->sli4_xritag = NO_XRI;
18998 	ctiocb->abort_rctl = FC_RCTL_BA_ACC;
18999 
19000 	if (fctl & FC_FC_EX_CTX)
19001 		/* Exchange responder sent the abort so we
19002 		 * own the oxid.
19003 		 */
19004 		xri = oxid;
19005 	else
19006 		xri = rxid;
19007 	lxri = lpfc_sli4_xri_inrange(phba, xri);
19008 	if (lxri != NO_XRI)
19009 		lpfc_set_rrq_active(phba, ndlp, lxri,
19010 			(xri == oxid) ? rxid : oxid, 0);
19011 	/* For BA_ABTS from exchange responder, if the logical xri with
19012 	 * the oxid maps to the FCP XRI range, the port no longer has
19013 	 * that exchange context, send a BLS_RJT. Override the IOCB for
19014 	 * a BA_RJT.
19015 	 */
19016 	if ((fctl & FC_FC_EX_CTX) &&
19017 	    (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
19018 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19019 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19020 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19021 		       FC_BA_RJT_INV_XID);
19022 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19023 		       FC_BA_RJT_UNABLE);
19024 	}
19025 
19026 	/* If BA_ABTS failed to abort a partially assembled receive sequence,
19027 	 * the driver no longer has that exchange, send a BLS_RJT. Override
19028 	 * the IOCB for a BA_RJT.
19029 	 */
19030 	if (aborted == false) {
19031 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19032 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19033 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19034 		       FC_BA_RJT_INV_XID);
19035 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19036 		       FC_BA_RJT_UNABLE);
19037 	}
19038 
19039 	if (fctl & FC_FC_EX_CTX) {
19040 		/* ABTS sent by responder to CT exchange, construction
19041 		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
19042 		 * field and RX_ID from ABTS for RX_ID field.
19043 		 */
19044 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_RSP;
19045 		bf_set(xmit_bls_rsp64_rxid, &icmd->xmit_bls_rsp, rxid);
19046 	} else {
19047 		/* ABTS sent by initiator to CT exchange, construction
19048 		 * of BA_ACC will need to allocate a new XRI as for the
19049 		 * XRI_TAG field.
19050 		 */
19051 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_INT;
19052 	}
19053 
19054 	/* OX_ID is invariable to who sent ABTS to CT exchange */
19055 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, oxid);
19056 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, rxid);
19057 
19058 	/* Use CT=VPI */
19059 	bf_set(wqe_els_did, &icmd->xmit_bls_rsp.wqe_dest,
19060 	       ndlp->nlp_DID);
19061 	bf_set(xmit_bls_rsp64_temprpi, &icmd->xmit_bls_rsp,
19062 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
19063 	bf_set(wqe_cmnd, &icmd->generic.wqe_com, CMD_XMIT_BLS_RSP64_CX);
19064 
19065 	/* Xmit CT abts response on exchange <xid> */
19066 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
19067 			 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
19068 			 ctiocb->abort_rctl, oxid, phba->link_state);
19069 
19070 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
19071 	if (rc == IOCB_ERROR) {
19072 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19073 				 "2925 Failed to issue CT ABTS RSP x%x on "
19074 				 "xri x%x, Data x%x\n",
19075 				 ctiocb->abort_rctl, oxid,
19076 				 phba->link_state);
19077 		lpfc_nlp_put(ndlp);
19078 		ctiocb->ndlp = NULL;
19079 		lpfc_sli_release_iocbq(phba, ctiocb);
19080 	}
19081 }
19082 
19083 /**
19084  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
19085  * @vport: Pointer to the vport on which this sequence was received
19086  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19087  *
19088  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
19089  * receive sequence is only partially assembed by the driver, it shall abort
19090  * the partially assembled frames for the sequence. Otherwise, if the
19091  * unsolicited receive sequence has been completely assembled and passed to
19092  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
19093  * unsolicited sequence has been aborted. After that, it will issue a basic
19094  * accept to accept the abort.
19095  **/
19096 static void
lpfc_sli4_handle_unsol_abort(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19097 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
19098 			     struct hbq_dmabuf *dmabuf)
19099 {
19100 	struct lpfc_hba *phba = vport->phba;
19101 	struct fc_frame_header fc_hdr;
19102 	uint32_t fctl;
19103 	bool aborted;
19104 
19105 	/* Make a copy of fc_hdr before the dmabuf being released */
19106 	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
19107 	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
19108 
19109 	if (fctl & FC_FC_EX_CTX) {
19110 		/* ABTS by responder to exchange, no cleanup needed */
19111 		aborted = true;
19112 	} else {
19113 		/* ABTS by initiator to exchange, need to do cleanup */
19114 		aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19115 		if (aborted == false)
19116 			aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
19117 	}
19118 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19119 
19120 	if (phba->nvmet_support) {
19121 		lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19122 		return;
19123 	}
19124 
19125 	/* Respond with BA_ACC or BA_RJT accordingly */
19126 	lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
19127 }
19128 
19129 /**
19130  * lpfc_seq_complete - Indicates if a sequence is complete
19131  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19132  *
19133  * This function checks the sequence, starting with the frame described by
19134  * @dmabuf, to see if all the frames associated with this sequence are present.
19135  * the frames associated with this sequence are linked to the @dmabuf using the
19136  * dbuf list. This function looks for two major things. 1) That the first frame
19137  * has a sequence count of zero. 2) There is a frame with last frame of sequence
19138  * set. 3) That there are no holes in the sequence count. The function will
19139  * return 1 when the sequence is complete, otherwise it will return 0.
19140  **/
19141 static int
lpfc_seq_complete(struct hbq_dmabuf * dmabuf)19142 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19143 {
19144 	struct fc_frame_header *hdr;
19145 	struct lpfc_dmabuf *d_buf;
19146 	struct hbq_dmabuf *seq_dmabuf;
19147 	uint32_t fctl;
19148 	int seq_count = 0;
19149 
19150 	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19151 	/* make sure first fame of sequence has a sequence count of zero */
19152 	if (hdr->fh_seq_cnt != seq_count)
19153 		return 0;
19154 	fctl = (hdr->fh_f_ctl[0] << 16 |
19155 		hdr->fh_f_ctl[1] << 8 |
19156 		hdr->fh_f_ctl[2]);
19157 	/* If last frame of sequence we can return success. */
19158 	if (fctl & FC_FC_END_SEQ)
19159 		return 1;
19160 	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19161 		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19162 		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19163 		/* If there is a hole in the sequence count then fail. */
19164 		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
19165 			return 0;
19166 		fctl = (hdr->fh_f_ctl[0] << 16 |
19167 			hdr->fh_f_ctl[1] << 8 |
19168 			hdr->fh_f_ctl[2]);
19169 		/* If last frame of sequence we can return success. */
19170 		if (fctl & FC_FC_END_SEQ)
19171 			return 1;
19172 	}
19173 	return 0;
19174 }
19175 
19176 /**
19177  * lpfc_prep_seq - Prep sequence for ULP processing
19178  * @vport: Pointer to the vport on which this sequence was received
19179  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
19180  *
19181  * This function takes a sequence, described by a list of frames, and creates
19182  * a list of iocbq structures to describe the sequence. This iocbq list will be
19183  * used to issue to the generic unsolicited sequence handler. This routine
19184  * returns a pointer to the first iocbq in the list. If the function is unable
19185  * to allocate an iocbq then it throw out the received frames that were not
19186  * able to be described and return a pointer to the first iocbq. If unable to
19187  * allocate any iocbqs (including the first) this function will return NULL.
19188  **/
19189 static struct lpfc_iocbq *
lpfc_prep_seq(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19190 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19191 {
19192 	struct hbq_dmabuf *hbq_buf;
19193 	struct lpfc_dmabuf *d_buf, *n_buf;
19194 	struct lpfc_iocbq *first_iocbq, *iocbq;
19195 	struct fc_frame_header *fc_hdr;
19196 	uint32_t sid;
19197 	uint32_t len, tot_len;
19198 
19199 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19200 	/* remove from receive buffer list */
19201 	list_del_init(&seq_dmabuf->hbuf.list);
19202 	lpfc_update_rcv_time_stamp(vport);
19203 	/* get the Remote Port's SID */
19204 	sid = sli4_sid_from_fc_hdr(fc_hdr);
19205 	tot_len = 0;
19206 	/* Get an iocbq struct to fill in. */
19207 	first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19208 	if (first_iocbq) {
19209 		/* Initialize the first IOCB. */
19210 		first_iocbq->wcqe_cmpl.total_data_placed = 0;
19211 		bf_set(lpfc_wcqe_c_status, &first_iocbq->wcqe_cmpl,
19212 		       IOSTAT_SUCCESS);
19213 		first_iocbq->vport = vport;
19214 
19215 		/* Check FC Header to see what TYPE of frame we are rcv'ing */
19216 		if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19217 			bf_set(els_rsp64_sid, &first_iocbq->wqe.xmit_els_rsp,
19218 			       sli4_did_from_fc_hdr(fc_hdr));
19219 		}
19220 
19221 		bf_set(wqe_ctxt_tag, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19222 		       NO_XRI);
19223 		bf_set(wqe_rcvoxid, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19224 		       be16_to_cpu(fc_hdr->fh_ox_id));
19225 
19226 		/* put the first buffer into the first iocb */
19227 		tot_len = bf_get(lpfc_rcqe_length,
19228 				 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19229 
19230 		first_iocbq->cmd_dmabuf = &seq_dmabuf->dbuf;
19231 		first_iocbq->bpl_dmabuf = NULL;
19232 		/* Keep track of the BDE count */
19233 		first_iocbq->wcqe_cmpl.word3 = 1;
19234 
19235 		if (tot_len > LPFC_DATA_BUF_SIZE)
19236 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize =
19237 				LPFC_DATA_BUF_SIZE;
19238 		else
19239 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize = tot_len;
19240 
19241 		first_iocbq->wcqe_cmpl.total_data_placed = tot_len;
19242 		bf_set(wqe_els_did, &first_iocbq->wqe.xmit_els_rsp.wqe_dest,
19243 		       sid);
19244 	}
19245 	iocbq = first_iocbq;
19246 	/*
19247 	 * Each IOCBq can have two Buffers assigned, so go through the list
19248 	 * of buffers for this sequence and save two buffers in each IOCBq
19249 	 */
19250 	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19251 		if (!iocbq) {
19252 			lpfc_in_buf_free(vport->phba, d_buf);
19253 			continue;
19254 		}
19255 		if (!iocbq->bpl_dmabuf) {
19256 			iocbq->bpl_dmabuf = d_buf;
19257 			iocbq->wcqe_cmpl.word3++;
19258 			/* We need to get the size out of the right CQE */
19259 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19260 			len = bf_get(lpfc_rcqe_length,
19261 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19262 			iocbq->unsol_rcv_len = len;
19263 			iocbq->wcqe_cmpl.total_data_placed += len;
19264 			tot_len += len;
19265 		} else {
19266 			iocbq = lpfc_sli_get_iocbq(vport->phba);
19267 			if (!iocbq) {
19268 				if (first_iocbq) {
19269 					bf_set(lpfc_wcqe_c_status,
19270 					       &first_iocbq->wcqe_cmpl,
19271 					       IOSTAT_SUCCESS);
19272 					first_iocbq->wcqe_cmpl.parameter =
19273 						IOERR_NO_RESOURCES;
19274 				}
19275 				lpfc_in_buf_free(vport->phba, d_buf);
19276 				continue;
19277 			}
19278 			/* We need to get the size out of the right CQE */
19279 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19280 			len = bf_get(lpfc_rcqe_length,
19281 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19282 			iocbq->cmd_dmabuf = d_buf;
19283 			iocbq->bpl_dmabuf = NULL;
19284 			iocbq->wcqe_cmpl.word3 = 1;
19285 
19286 			if (len > LPFC_DATA_BUF_SIZE)
19287 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19288 					LPFC_DATA_BUF_SIZE;
19289 			else
19290 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19291 					len;
19292 
19293 			tot_len += len;
19294 			iocbq->wcqe_cmpl.total_data_placed = tot_len;
19295 			bf_set(wqe_els_did, &iocbq->wqe.xmit_els_rsp.wqe_dest,
19296 			       sid);
19297 			list_add_tail(&iocbq->list, &first_iocbq->list);
19298 		}
19299 	}
19300 	/* Free the sequence's header buffer */
19301 	if (!first_iocbq)
19302 		lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19303 
19304 	return first_iocbq;
19305 }
19306 
19307 static void
lpfc_sli4_send_seq_to_ulp(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19308 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19309 			  struct hbq_dmabuf *seq_dmabuf)
19310 {
19311 	struct fc_frame_header *fc_hdr;
19312 	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19313 	struct lpfc_hba *phba = vport->phba;
19314 
19315 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19316 	iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19317 	if (!iocbq) {
19318 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19319 				"2707 Ring %d handler: Failed to allocate "
19320 				"iocb Rctl x%x Type x%x received\n",
19321 				LPFC_ELS_RING,
19322 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19323 		return;
19324 	}
19325 	if (!lpfc_complete_unsol_iocb(phba,
19326 				      phba->sli4_hba.els_wq->pring,
19327 				      iocbq, fc_hdr->fh_r_ctl,
19328 				      fc_hdr->fh_type)) {
19329 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19330 				"2540 Ring %d handler: unexpected Rctl "
19331 				"x%x Type x%x received\n",
19332 				LPFC_ELS_RING,
19333 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19334 		lpfc_in_buf_free(phba, &seq_dmabuf->dbuf);
19335 	}
19336 
19337 	/* Free iocb created in lpfc_prep_seq */
19338 	list_for_each_entry_safe(curr_iocb, next_iocb,
19339 				 &iocbq->list, list) {
19340 		list_del_init(&curr_iocb->list);
19341 		lpfc_sli_release_iocbq(phba, curr_iocb);
19342 	}
19343 	lpfc_sli_release_iocbq(phba, iocbq);
19344 }
19345 
19346 static void
lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)19347 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19348 			    struct lpfc_iocbq *rspiocb)
19349 {
19350 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
19351 
19352 	if (pcmd && pcmd->virt)
19353 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19354 	kfree(pcmd);
19355 	lpfc_sli_release_iocbq(phba, cmdiocb);
19356 	lpfc_drain_txq(phba);
19357 }
19358 
19359 static void
lpfc_sli4_handle_mds_loopback(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19360 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19361 			      struct hbq_dmabuf *dmabuf)
19362 {
19363 	struct fc_frame_header *fc_hdr;
19364 	struct lpfc_hba *phba = vport->phba;
19365 	struct lpfc_iocbq *iocbq = NULL;
19366 	union  lpfc_wqe128 *pwqe;
19367 	struct lpfc_dmabuf *pcmd = NULL;
19368 	uint32_t frame_len;
19369 	int rc;
19370 	unsigned long iflags;
19371 
19372 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19373 	frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19374 
19375 	/* Send the received frame back */
19376 	iocbq = lpfc_sli_get_iocbq(phba);
19377 	if (!iocbq) {
19378 		/* Queue cq event and wakeup worker thread to process it */
19379 		spin_lock_irqsave(&phba->hbalock, iflags);
19380 		list_add_tail(&dmabuf->cq_event.list,
19381 			      &phba->sli4_hba.sp_queue_event);
19382 		phba->hba_flag |= HBA_SP_QUEUE_EVT;
19383 		spin_unlock_irqrestore(&phba->hbalock, iflags);
19384 		lpfc_worker_wake_up(phba);
19385 		return;
19386 	}
19387 
19388 	/* Allocate buffer for command payload */
19389 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19390 	if (pcmd)
19391 		pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
19392 					    &pcmd->phys);
19393 	if (!pcmd || !pcmd->virt)
19394 		goto exit;
19395 
19396 	INIT_LIST_HEAD(&pcmd->list);
19397 
19398 	/* copyin the payload */
19399 	memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19400 
19401 	iocbq->cmd_dmabuf = pcmd;
19402 	iocbq->vport = vport;
19403 	iocbq->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
19404 	iocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
19405 	iocbq->num_bdes = 0;
19406 
19407 	pwqe = &iocbq->wqe;
19408 	/* fill in BDE's for command */
19409 	pwqe->gen_req.bde.addrHigh = putPaddrHigh(pcmd->phys);
19410 	pwqe->gen_req.bde.addrLow = putPaddrLow(pcmd->phys);
19411 	pwqe->gen_req.bde.tus.f.bdeSize = frame_len;
19412 	pwqe->gen_req.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
19413 
19414 	pwqe->send_frame.frame_len = frame_len;
19415 	pwqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((__be32 *)fc_hdr));
19416 	pwqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((__be32 *)fc_hdr + 1));
19417 	pwqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((__be32 *)fc_hdr + 2));
19418 	pwqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((__be32 *)fc_hdr + 3));
19419 	pwqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((__be32 *)fc_hdr + 4));
19420 	pwqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((__be32 *)fc_hdr + 5));
19421 
19422 	pwqe->generic.wqe_com.word7 = 0;
19423 	pwqe->generic.wqe_com.word10 = 0;
19424 
19425 	bf_set(wqe_cmnd, &pwqe->generic.wqe_com, CMD_SEND_FRAME);
19426 	bf_set(wqe_sof, &pwqe->generic.wqe_com, 0x2E); /* SOF byte */
19427 	bf_set(wqe_eof, &pwqe->generic.wqe_com, 0x41); /* EOF byte */
19428 	bf_set(wqe_lenloc, &pwqe->generic.wqe_com, 1);
19429 	bf_set(wqe_xbl, &pwqe->generic.wqe_com, 1);
19430 	bf_set(wqe_dbde, &pwqe->generic.wqe_com, 1);
19431 	bf_set(wqe_xc, &pwqe->generic.wqe_com, 1);
19432 	bf_set(wqe_cmd_type, &pwqe->generic.wqe_com, 0xA);
19433 	bf_set(wqe_cqid, &pwqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
19434 	bf_set(wqe_xri_tag, &pwqe->generic.wqe_com, iocbq->sli4_xritag);
19435 	bf_set(wqe_reqtag, &pwqe->generic.wqe_com, iocbq->iotag);
19436 	bf_set(wqe_class, &pwqe->generic.wqe_com, CLASS3);
19437 	pwqe->generic.wqe_com.abort_tag = iocbq->iotag;
19438 
19439 	iocbq->cmd_cmpl = lpfc_sli4_mds_loopback_cmpl;
19440 
19441 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19442 	if (rc == IOCB_ERROR)
19443 		goto exit;
19444 
19445 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19446 	return;
19447 
19448 exit:
19449 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19450 			"2023 Unable to process MDS loopback frame\n");
19451 	if (pcmd && pcmd->virt)
19452 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19453 	kfree(pcmd);
19454 	if (iocbq)
19455 		lpfc_sli_release_iocbq(phba, iocbq);
19456 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19457 }
19458 
19459 /**
19460  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19461  * @phba: Pointer to HBA context object.
19462  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
19463  *
19464  * This function is called with no lock held. This function processes all
19465  * the received buffers and gives it to upper layers when a received buffer
19466  * indicates that it is the final frame in the sequence. The interrupt
19467  * service routine processes received buffers at interrupt contexts.
19468  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19469  * appropriate receive function when the final frame in a sequence is received.
19470  **/
19471 void
lpfc_sli4_handle_received_buffer(struct lpfc_hba * phba,struct hbq_dmabuf * dmabuf)19472 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19473 				 struct hbq_dmabuf *dmabuf)
19474 {
19475 	struct hbq_dmabuf *seq_dmabuf;
19476 	struct fc_frame_header *fc_hdr;
19477 	struct lpfc_vport *vport;
19478 	uint32_t fcfi;
19479 	uint32_t did;
19480 
19481 	/* Process each received buffer */
19482 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19483 
19484 	if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19485 	    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19486 		vport = phba->pport;
19487 		/* Handle MDS Loopback frames */
19488 		if  (!(phba->pport->load_flag & FC_UNLOADING))
19489 			lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19490 		else
19491 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19492 		return;
19493 	}
19494 
19495 	/* check to see if this a valid type of frame */
19496 	if (lpfc_fc_frame_check(phba, fc_hdr)) {
19497 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19498 		return;
19499 	}
19500 
19501 	if ((bf_get(lpfc_cqe_code,
19502 		    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19503 		fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19504 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19505 	else
19506 		fcfi = bf_get(lpfc_rcqe_fcf_id,
19507 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19508 
19509 	if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19510 		vport = phba->pport;
19511 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19512 				"2023 MDS Loopback %d bytes\n",
19513 				bf_get(lpfc_rcqe_length,
19514 				       &dmabuf->cq_event.cqe.rcqe_cmpl));
19515 		/* Handle MDS Loopback frames */
19516 		lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19517 		return;
19518 	}
19519 
19520 	/* d_id this frame is directed to */
19521 	did = sli4_did_from_fc_hdr(fc_hdr);
19522 
19523 	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
19524 	if (!vport) {
19525 		/* throw out the frame */
19526 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19527 		return;
19528 	}
19529 
19530 	/* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19531 	if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19532 		(did != Fabric_DID)) {
19533 		/*
19534 		 * Throw out the frame if we are not pt2pt.
19535 		 * The pt2pt protocol allows for discovery frames
19536 		 * to be received without a registered VPI.
19537 		 */
19538 		if (!(vport->fc_flag & FC_PT2PT) ||
19539 			(phba->link_state == LPFC_HBA_READY)) {
19540 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19541 			return;
19542 		}
19543 	}
19544 
19545 	/* Handle the basic abort sequence (BA_ABTS) event */
19546 	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19547 		lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19548 		return;
19549 	}
19550 
19551 	/* Link this frame */
19552 	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19553 	if (!seq_dmabuf) {
19554 		/* unable to add frame to vport - throw it out */
19555 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19556 		return;
19557 	}
19558 	/* If not last frame in sequence continue processing frames. */
19559 	if (!lpfc_seq_complete(seq_dmabuf))
19560 		return;
19561 
19562 	/* Send the complete sequence to the upper layer protocol */
19563 	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
19564 }
19565 
19566 /**
19567  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19568  * @phba: pointer to lpfc hba data structure.
19569  *
19570  * This routine is invoked to post rpi header templates to the
19571  * HBA consistent with the SLI-4 interface spec.  This routine
19572  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19573  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19574  *
19575  * This routine does not require any locks.  It's usage is expected
19576  * to be driver load or reset recovery when the driver is
19577  * sequential.
19578  *
19579  * Return codes
19580  * 	0 - successful
19581  *      -EIO - The mailbox failed to complete successfully.
19582  * 	When this error occurs, the driver is not guaranteed
19583  *	to have any rpi regions posted to the device and
19584  *	must either attempt to repost the regions or take a
19585  *	fatal error.
19586  **/
19587 int
lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba * phba)19588 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19589 {
19590 	struct lpfc_rpi_hdr *rpi_page;
19591 	uint32_t rc = 0;
19592 	uint16_t lrpi = 0;
19593 
19594 	/* SLI4 ports that support extents do not require RPI headers. */
19595 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19596 		goto exit;
19597 	if (phba->sli4_hba.extents_in_use)
19598 		return -EIO;
19599 
19600 	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
19601 		/*
19602 		 * Assign the rpi headers a physical rpi only if the driver
19603 		 * has not initialized those resources.  A port reset only
19604 		 * needs the headers posted.
19605 		 */
19606 		if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19607 		    LPFC_RPI_RSRC_RDY)
19608 			rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19609 
19610 		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19611 		if (rc != MBX_SUCCESS) {
19612 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19613 					"2008 Error %d posting all rpi "
19614 					"headers\n", rc);
19615 			rc = -EIO;
19616 			break;
19617 		}
19618 	}
19619 
19620  exit:
19621 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19622 	       LPFC_RPI_RSRC_RDY);
19623 	return rc;
19624 }
19625 
19626 /**
19627  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19628  * @phba: pointer to lpfc hba data structure.
19629  * @rpi_page:  pointer to the rpi memory region.
19630  *
19631  * This routine is invoked to post a single rpi header to the
19632  * HBA consistent with the SLI-4 interface spec.  This memory region
19633  * maps up to 64 rpi context regions.
19634  *
19635  * Return codes
19636  * 	0 - successful
19637  * 	-ENOMEM - No available memory
19638  *      -EIO - The mailbox failed to complete successfully.
19639  **/
19640 int
lpfc_sli4_post_rpi_hdr(struct lpfc_hba * phba,struct lpfc_rpi_hdr * rpi_page)19641 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19642 {
19643 	LPFC_MBOXQ_t *mboxq;
19644 	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19645 	uint32_t rc = 0;
19646 	uint32_t shdr_status, shdr_add_status;
19647 	union lpfc_sli4_cfg_shdr *shdr;
19648 
19649 	/* SLI4 ports that support extents do not require RPI headers. */
19650 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19651 		return rc;
19652 	if (phba->sli4_hba.extents_in_use)
19653 		return -EIO;
19654 
19655 	/* The port is notified of the header region via a mailbox command. */
19656 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19657 	if (!mboxq) {
19658 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19659 				"2001 Unable to allocate memory for issuing "
19660 				"SLI_CONFIG_SPECIAL mailbox command\n");
19661 		return -ENOMEM;
19662 	}
19663 
19664 	/* Post all rpi memory regions to the port. */
19665 	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
19666 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19667 			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19668 			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
19669 			 sizeof(struct lpfc_sli4_cfg_mhdr),
19670 			 LPFC_SLI4_MBX_EMBED);
19671 
19672 
19673 	/* Post the physical rpi to the port for this rpi header. */
19674 	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19675 	       rpi_page->start_rpi);
19676 	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19677 	       hdr_tmpl, rpi_page->page_count);
19678 
19679 	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19680 	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
19681 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19682 	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19683 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19684 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19685 	mempool_free(mboxq, phba->mbox_mem_pool);
19686 	if (shdr_status || shdr_add_status || rc) {
19687 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19688 				"2514 POST_RPI_HDR mailbox failed with "
19689 				"status x%x add_status x%x, mbx status x%x\n",
19690 				shdr_status, shdr_add_status, rc);
19691 		rc = -ENXIO;
19692 	} else {
19693 		/*
19694 		 * The next_rpi stores the next logical module-64 rpi value used
19695 		 * to post physical rpis in subsequent rpi postings.
19696 		 */
19697 		spin_lock_irq(&phba->hbalock);
19698 		phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19699 		spin_unlock_irq(&phba->hbalock);
19700 	}
19701 	return rc;
19702 }
19703 
19704 /**
19705  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19706  * @phba: pointer to lpfc hba data structure.
19707  *
19708  * This routine is invoked to post rpi header templates to the
19709  * HBA consistent with the SLI-4 interface spec.  This routine
19710  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19711  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19712  *
19713  * Returns
19714  * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
19715  * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.
19716  **/
19717 int
lpfc_sli4_alloc_rpi(struct lpfc_hba * phba)19718 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19719 {
19720 	unsigned long rpi;
19721 	uint16_t max_rpi, rpi_limit;
19722 	uint16_t rpi_remaining, lrpi = 0;
19723 	struct lpfc_rpi_hdr *rpi_hdr;
19724 	unsigned long iflag;
19725 
19726 	/*
19727 	 * Fetch the next logical rpi.  Because this index is logical,
19728 	 * the  driver starts at 0 each time.
19729 	 */
19730 	spin_lock_irqsave(&phba->hbalock, iflag);
19731 	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19732 	rpi_limit = phba->sli4_hba.next_rpi;
19733 
19734 	rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);
19735 	if (rpi >= rpi_limit)
19736 		rpi = LPFC_RPI_ALLOC_ERROR;
19737 	else {
19738 		set_bit(rpi, phba->sli4_hba.rpi_bmask);
19739 		phba->sli4_hba.max_cfg_param.rpi_used++;
19740 		phba->sli4_hba.rpi_count++;
19741 	}
19742 	lpfc_printf_log(phba, KERN_INFO,
19743 			LOG_NODE | LOG_DISCOVERY,
19744 			"0001 Allocated rpi:x%x max:x%x lim:x%x\n",
19745 			(int) rpi, max_rpi, rpi_limit);
19746 
19747 	/*
19748 	 * Don't try to allocate more rpi header regions if the device limit
19749 	 * has been exhausted.
19750 	 */
19751 	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19752 	    (phba->sli4_hba.rpi_count >= max_rpi)) {
19753 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19754 		return rpi;
19755 	}
19756 
19757 	/*
19758 	 * RPI header postings are not required for SLI4 ports capable of
19759 	 * extents.
19760 	 */
19761 	if (!phba->sli4_hba.rpi_hdrs_in_use) {
19762 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19763 		return rpi;
19764 	}
19765 
19766 	/*
19767 	 * If the driver is running low on rpi resources, allocate another
19768 	 * page now.  Note that the next_rpi value is used because
19769 	 * it represents how many are actually in use whereas max_rpi notes
19770 	 * how many are supported max by the device.
19771 	 */
19772 	rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
19773 	spin_unlock_irqrestore(&phba->hbalock, iflag);
19774 	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19775 		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19776 		if (!rpi_hdr) {
19777 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19778 					"2002 Error Could not grow rpi "
19779 					"count\n");
19780 		} else {
19781 			lrpi = rpi_hdr->start_rpi;
19782 			rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19783 			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19784 		}
19785 	}
19786 
19787 	return rpi;
19788 }
19789 
19790 /**
19791  * __lpfc_sli4_free_rpi - Release an rpi for reuse.
19792  * @phba: pointer to lpfc hba data structure.
19793  * @rpi: rpi to free
19794  *
19795  * This routine is invoked to release an rpi to the pool of
19796  * available rpis maintained by the driver.
19797  **/
19798 static void
__lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19799 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19800 {
19801 	/*
19802 	 * if the rpi value indicates a prior unreg has already
19803 	 * been done, skip the unreg.
19804 	 */
19805 	if (rpi == LPFC_RPI_ALLOC_ERROR)
19806 		return;
19807 
19808 	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19809 		phba->sli4_hba.rpi_count--;
19810 		phba->sli4_hba.max_cfg_param.rpi_used--;
19811 	} else {
19812 		lpfc_printf_log(phba, KERN_INFO,
19813 				LOG_NODE | LOG_DISCOVERY,
19814 				"2016 rpi %x not inuse\n",
19815 				rpi);
19816 	}
19817 }
19818 
19819 /**
19820  * lpfc_sli4_free_rpi - Release an rpi for reuse.
19821  * @phba: pointer to lpfc hba data structure.
19822  * @rpi: rpi to free
19823  *
19824  * This routine is invoked to release an rpi to the pool of
19825  * available rpis maintained by the driver.
19826  **/
19827 void
lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19828 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19829 {
19830 	spin_lock_irq(&phba->hbalock);
19831 	__lpfc_sli4_free_rpi(phba, rpi);
19832 	spin_unlock_irq(&phba->hbalock);
19833 }
19834 
19835 /**
19836  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19837  * @phba: pointer to lpfc hba data structure.
19838  *
19839  * This routine is invoked to remove the memory region that
19840  * provided rpi via a bitmask.
19841  **/
19842 void
lpfc_sli4_remove_rpis(struct lpfc_hba * phba)19843 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19844 {
19845 	kfree(phba->sli4_hba.rpi_bmask);
19846 	kfree(phba->sli4_hba.rpi_ids);
19847 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
19848 }
19849 
19850 /**
19851  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
19852  * @ndlp: pointer to lpfc nodelist data structure.
19853  * @cmpl: completion call-back.
19854  * @arg: data to load as MBox 'caller buffer information'
19855  *
19856  * This routine is invoked to remove the memory region that
19857  * provided rpi via a bitmask.
19858  **/
19859 int
lpfc_sli4_resume_rpi(struct lpfc_nodelist * ndlp,void (* cmpl)(struct lpfc_hba *,LPFC_MBOXQ_t *),void * arg)19860 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19861 	void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
19862 {
19863 	LPFC_MBOXQ_t *mboxq;
19864 	struct lpfc_hba *phba = ndlp->phba;
19865 	int rc;
19866 
19867 	/* The port is notified of the header region via a mailbox command. */
19868 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19869 	if (!mboxq)
19870 		return -ENOMEM;
19871 
19872 	/* If cmpl assigned, then this nlp_get pairs with
19873 	 * lpfc_mbx_cmpl_resume_rpi.
19874 	 *
19875 	 * Else cmpl is NULL, then this nlp_get pairs with
19876 	 * lpfc_sli_def_mbox_cmpl.
19877 	 */
19878 	if (!lpfc_nlp_get(ndlp)) {
19879 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19880 				"2122 %s: Failed to get nlp ref\n",
19881 				__func__);
19882 		mempool_free(mboxq, phba->mbox_mem_pool);
19883 		return -EIO;
19884 	}
19885 
19886 	/* Post all rpi memory regions to the port. */
19887 	lpfc_resume_rpi(mboxq, ndlp);
19888 	if (cmpl) {
19889 		mboxq->mbox_cmpl = cmpl;
19890 		mboxq->ctx_buf = arg;
19891 	} else
19892 		mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19893 	mboxq->ctx_ndlp = ndlp;
19894 	mboxq->vport = ndlp->vport;
19895 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19896 	if (rc == MBX_NOT_FINISHED) {
19897 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19898 				"2010 Resume RPI Mailbox failed "
19899 				"status %d, mbxStatus x%x\n", rc,
19900 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19901 		lpfc_nlp_put(ndlp);
19902 		mempool_free(mboxq, phba->mbox_mem_pool);
19903 		return -EIO;
19904 	}
19905 	return 0;
19906 }
19907 
19908 /**
19909  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19910  * @vport: Pointer to the vport for which the vpi is being initialized
19911  *
19912  * This routine is invoked to activate a vpi with the port.
19913  *
19914  * Returns:
19915  *    0 success
19916  *    -Evalue otherwise
19917  **/
19918 int
lpfc_sli4_init_vpi(struct lpfc_vport * vport)19919 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
19920 {
19921 	LPFC_MBOXQ_t *mboxq;
19922 	int rc = 0;
19923 	int retval = MBX_SUCCESS;
19924 	uint32_t mbox_tmo;
19925 	struct lpfc_hba *phba = vport->phba;
19926 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19927 	if (!mboxq)
19928 		return -ENOMEM;
19929 	lpfc_init_vpi(phba, mboxq, vport->vpi);
19930 	mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
19931 	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
19932 	if (rc != MBX_SUCCESS) {
19933 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19934 				"2022 INIT VPI Mailbox failed "
19935 				"status %d, mbxStatus x%x\n", rc,
19936 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19937 		retval = -EIO;
19938 	}
19939 	if (rc != MBX_TIMEOUT)
19940 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
19941 
19942 	return retval;
19943 }
19944 
19945 /**
19946  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19947  * @phba: pointer to lpfc hba data structure.
19948  * @mboxq: Pointer to mailbox object.
19949  *
19950  * This routine is invoked to manually add a single FCF record. The caller
19951  * must pass a completely initialized FCF_Record.  This routine takes
19952  * care of the nonembedded mailbox operations.
19953  **/
19954 static void
lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)19955 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19956 {
19957 	void *virt_addr;
19958 	union lpfc_sli4_cfg_shdr *shdr;
19959 	uint32_t shdr_status, shdr_add_status;
19960 
19961 	virt_addr = mboxq->sge_array->addr[0];
19962 	/* The IOCTL status is embedded in the mailbox subheader. */
19963 	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19964 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19965 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19966 
19967 	if ((shdr_status || shdr_add_status) &&
19968 		(shdr_status != STATUS_FCF_IN_USE))
19969 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19970 			"2558 ADD_FCF_RECORD mailbox failed with "
19971 			"status x%x add_status x%x\n",
19972 			shdr_status, shdr_add_status);
19973 
19974 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
19975 }
19976 
19977 /**
19978  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19979  * @phba: pointer to lpfc hba data structure.
19980  * @fcf_record:  pointer to the initialized fcf record to add.
19981  *
19982  * This routine is invoked to manually add a single FCF record. The caller
19983  * must pass a completely initialized FCF_Record.  This routine takes
19984  * care of the nonembedded mailbox operations.
19985  **/
19986 int
lpfc_sli4_add_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record)19987 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19988 {
19989 	int rc = 0;
19990 	LPFC_MBOXQ_t *mboxq;
19991 	uint8_t *bytep;
19992 	void *virt_addr;
19993 	struct lpfc_mbx_sge sge;
19994 	uint32_t alloc_len, req_len;
19995 	uint32_t fcfindex;
19996 
19997 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19998 	if (!mboxq) {
19999 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20000 			"2009 Failed to allocate mbox for ADD_FCF cmd\n");
20001 		return -ENOMEM;
20002 	}
20003 
20004 	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
20005 		  sizeof(uint32_t);
20006 
20007 	/* Allocate DMA memory and set up the non-embedded mailbox command */
20008 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
20009 				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
20010 				     req_len, LPFC_SLI4_MBX_NEMBED);
20011 	if (alloc_len < req_len) {
20012 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20013 			"2523 Allocated DMA memory size (x%x) is "
20014 			"less than the requested DMA memory "
20015 			"size (x%x)\n", alloc_len, req_len);
20016 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20017 		return -ENOMEM;
20018 	}
20019 
20020 	/*
20021 	 * Get the first SGE entry from the non-embedded DMA memory.  This
20022 	 * routine only uses a single SGE.
20023 	 */
20024 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
20025 	virt_addr = mboxq->sge_array->addr[0];
20026 	/*
20027 	 * Configure the FCF record for FCFI 0.  This is the driver's
20028 	 * hardcoded default and gets used in nonFIP mode.
20029 	 */
20030 	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
20031 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
20032 	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
20033 
20034 	/*
20035 	 * Copy the fcf_index and the FCF Record Data. The data starts after
20036 	 * the FCoE header plus word10. The data copy needs to be endian
20037 	 * correct.
20038 	 */
20039 	bytep += sizeof(uint32_t);
20040 	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
20041 	mboxq->vport = phba->pport;
20042 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
20043 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20044 	if (rc == MBX_NOT_FINISHED) {
20045 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20046 			"2515 ADD_FCF_RECORD mailbox failed with "
20047 			"status 0x%x\n", rc);
20048 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20049 		rc = -EIO;
20050 	} else
20051 		rc = 0;
20052 
20053 	return rc;
20054 }
20055 
20056 /**
20057  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
20058  * @phba: pointer to lpfc hba data structure.
20059  * @fcf_record:  pointer to the fcf record to write the default data.
20060  * @fcf_index: FCF table entry index.
20061  *
20062  * This routine is invoked to build the driver's default FCF record.  The
20063  * values used are hardcoded.  This routine handles memory initialization.
20064  *
20065  **/
20066 void
lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record,uint16_t fcf_index)20067 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
20068 				struct fcf_record *fcf_record,
20069 				uint16_t fcf_index)
20070 {
20071 	memset(fcf_record, 0, sizeof(struct fcf_record));
20072 	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
20073 	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
20074 	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
20075 	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
20076 	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
20077 	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
20078 	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
20079 	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
20080 	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
20081 	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
20082 	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
20083 	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
20084 	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
20085 	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
20086 	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
20087 	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
20088 		LPFC_FCF_FPMA | LPFC_FCF_SPMA);
20089 	/* Set the VLAN bit map */
20090 	if (phba->valid_vlan) {
20091 		fcf_record->vlan_bitmap[phba->vlan_id / 8]
20092 			= 1 << (phba->vlan_id % 8);
20093 	}
20094 }
20095 
20096 /**
20097  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
20098  * @phba: pointer to lpfc hba data structure.
20099  * @fcf_index: FCF table entry offset.
20100  *
20101  * This routine is invoked to scan the entire FCF table by reading FCF
20102  * record and processing it one at a time starting from the @fcf_index
20103  * for initial FCF discovery or fast FCF failover rediscovery.
20104  *
20105  * Return 0 if the mailbox command is submitted successfully, none 0
20106  * otherwise.
20107  **/
20108 int
lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20109 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20110 {
20111 	int rc = 0, error;
20112 	LPFC_MBOXQ_t *mboxq;
20113 
20114 	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
20115 	phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
20116 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20117 	if (!mboxq) {
20118 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20119 				"2000 Failed to allocate mbox for "
20120 				"READ_FCF cmd\n");
20121 		error = -ENOMEM;
20122 		goto fail_fcf_scan;
20123 	}
20124 	/* Construct the read FCF record mailbox command */
20125 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20126 	if (rc) {
20127 		error = -EINVAL;
20128 		goto fail_fcf_scan;
20129 	}
20130 	/* Issue the mailbox command asynchronously */
20131 	mboxq->vport = phba->pport;
20132 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
20133 
20134 	spin_lock_irq(&phba->hbalock);
20135 	phba->hba_flag |= FCF_TS_INPROG;
20136 	spin_unlock_irq(&phba->hbalock);
20137 
20138 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20139 	if (rc == MBX_NOT_FINISHED)
20140 		error = -EIO;
20141 	else {
20142 		/* Reset eligible FCF count for new scan */
20143 		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
20144 			phba->fcf.eligible_fcf_cnt = 0;
20145 		error = 0;
20146 	}
20147 fail_fcf_scan:
20148 	if (error) {
20149 		if (mboxq)
20150 			lpfc_sli4_mbox_cmd_free(phba, mboxq);
20151 		/* FCF scan failed, clear FCF_TS_INPROG flag */
20152 		spin_lock_irq(&phba->hbalock);
20153 		phba->hba_flag &= ~FCF_TS_INPROG;
20154 		spin_unlock_irq(&phba->hbalock);
20155 	}
20156 	return error;
20157 }
20158 
20159 /**
20160  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
20161  * @phba: pointer to lpfc hba data structure.
20162  * @fcf_index: FCF table entry offset.
20163  *
20164  * This routine is invoked to read an FCF record indicated by @fcf_index
20165  * and to use it for FLOGI roundrobin FCF failover.
20166  *
20167  * Return 0 if the mailbox command is submitted successfully, none 0
20168  * otherwise.
20169  **/
20170 int
lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20171 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20172 {
20173 	int rc = 0, error;
20174 	LPFC_MBOXQ_t *mboxq;
20175 
20176 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20177 	if (!mboxq) {
20178 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20179 				"2763 Failed to allocate mbox for "
20180 				"READ_FCF cmd\n");
20181 		error = -ENOMEM;
20182 		goto fail_fcf_read;
20183 	}
20184 	/* Construct the read FCF record mailbox command */
20185 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20186 	if (rc) {
20187 		error = -EINVAL;
20188 		goto fail_fcf_read;
20189 	}
20190 	/* Issue the mailbox command asynchronously */
20191 	mboxq->vport = phba->pport;
20192 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20193 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20194 	if (rc == MBX_NOT_FINISHED)
20195 		error = -EIO;
20196 	else
20197 		error = 0;
20198 
20199 fail_fcf_read:
20200 	if (error && mboxq)
20201 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20202 	return error;
20203 }
20204 
20205 /**
20206  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20207  * @phba: pointer to lpfc hba data structure.
20208  * @fcf_index: FCF table entry offset.
20209  *
20210  * This routine is invoked to read an FCF record indicated by @fcf_index to
20211  * determine whether it's eligible for FLOGI roundrobin failover list.
20212  *
20213  * Return 0 if the mailbox command is submitted successfully, none 0
20214  * otherwise.
20215  **/
20216 int
lpfc_sli4_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20217 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20218 {
20219 	int rc = 0, error;
20220 	LPFC_MBOXQ_t *mboxq;
20221 
20222 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20223 	if (!mboxq) {
20224 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20225 				"2758 Failed to allocate mbox for "
20226 				"READ_FCF cmd\n");
20227 				error = -ENOMEM;
20228 				goto fail_fcf_read;
20229 	}
20230 	/* Construct the read FCF record mailbox command */
20231 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20232 	if (rc) {
20233 		error = -EINVAL;
20234 		goto fail_fcf_read;
20235 	}
20236 	/* Issue the mailbox command asynchronously */
20237 	mboxq->vport = phba->pport;
20238 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20239 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20240 	if (rc == MBX_NOT_FINISHED)
20241 		error = -EIO;
20242 	else
20243 		error = 0;
20244 
20245 fail_fcf_read:
20246 	if (error && mboxq)
20247 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20248 	return error;
20249 }
20250 
20251 /**
20252  * lpfc_check_next_fcf_pri_level
20253  * @phba: pointer to the lpfc_hba struct for this port.
20254  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20255  * routine when the rr_bmask is empty. The FCF indecies are put into the
20256  * rr_bmask based on their priority level. Starting from the highest priority
20257  * to the lowest. The most likely FCF candidate will be in the highest
20258  * priority group. When this routine is called it searches the fcf_pri list for
20259  * next lowest priority group and repopulates the rr_bmask with only those
20260  * fcf_indexes.
20261  * returns:
20262  * 1=success 0=failure
20263  **/
20264 static int
lpfc_check_next_fcf_pri_level(struct lpfc_hba * phba)20265 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20266 {
20267 	uint16_t next_fcf_pri;
20268 	uint16_t last_index;
20269 	struct lpfc_fcf_pri *fcf_pri;
20270 	int rc;
20271 	int ret = 0;
20272 
20273 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20274 			LPFC_SLI4_FCF_TBL_INDX_MAX);
20275 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20276 			"3060 Last IDX %d\n", last_index);
20277 
20278 	/* Verify the priority list has 2 or more entries */
20279 	spin_lock_irq(&phba->hbalock);
20280 	if (list_empty(&phba->fcf.fcf_pri_list) ||
20281 	    list_is_singular(&phba->fcf.fcf_pri_list)) {
20282 		spin_unlock_irq(&phba->hbalock);
20283 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20284 			"3061 Last IDX %d\n", last_index);
20285 		return 0; /* Empty rr list */
20286 	}
20287 	spin_unlock_irq(&phba->hbalock);
20288 
20289 	next_fcf_pri = 0;
20290 	/*
20291 	 * Clear the rr_bmask and set all of the bits that are at this
20292 	 * priority.
20293 	 */
20294 	memset(phba->fcf.fcf_rr_bmask, 0,
20295 			sizeof(*phba->fcf.fcf_rr_bmask));
20296 	spin_lock_irq(&phba->hbalock);
20297 	list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20298 		if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20299 			continue;
20300 		/*
20301 		 * the 1st priority that has not FLOGI failed
20302 		 * will be the highest.
20303 		 */
20304 		if (!next_fcf_pri)
20305 			next_fcf_pri = fcf_pri->fcf_rec.priority;
20306 		spin_unlock_irq(&phba->hbalock);
20307 		if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20308 			rc = lpfc_sli4_fcf_rr_index_set(phba,
20309 						fcf_pri->fcf_rec.fcf_index);
20310 			if (rc)
20311 				return 0;
20312 		}
20313 		spin_lock_irq(&phba->hbalock);
20314 	}
20315 	/*
20316 	 * if next_fcf_pri was not set above and the list is not empty then
20317 	 * we have failed flogis on all of them. So reset flogi failed
20318 	 * and start at the beginning.
20319 	 */
20320 	if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20321 		list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20322 			fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20323 			/*
20324 			 * the 1st priority that has not FLOGI failed
20325 			 * will be the highest.
20326 			 */
20327 			if (!next_fcf_pri)
20328 				next_fcf_pri = fcf_pri->fcf_rec.priority;
20329 			spin_unlock_irq(&phba->hbalock);
20330 			if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20331 				rc = lpfc_sli4_fcf_rr_index_set(phba,
20332 						fcf_pri->fcf_rec.fcf_index);
20333 				if (rc)
20334 					return 0;
20335 			}
20336 			spin_lock_irq(&phba->hbalock);
20337 		}
20338 	} else
20339 		ret = 1;
20340 	spin_unlock_irq(&phba->hbalock);
20341 
20342 	return ret;
20343 }
20344 /**
20345  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20346  * @phba: pointer to lpfc hba data structure.
20347  *
20348  * This routine is to get the next eligible FCF record index in a round
20349  * robin fashion. If the next eligible FCF record index equals to the
20350  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
20351  * shall be returned, otherwise, the next eligible FCF record's index
20352  * shall be returned.
20353  **/
20354 uint16_t
lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba * phba)20355 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20356 {
20357 	uint16_t next_fcf_index;
20358 
20359 initial_priority:
20360 	/* Search start from next bit of currently registered FCF index */
20361 	next_fcf_index = phba->fcf.current_rec.fcf_indx;
20362 
20363 next_priority:
20364 	/* Determine the next fcf index to check */
20365 	next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
20366 	next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20367 				       LPFC_SLI4_FCF_TBL_INDX_MAX,
20368 				       next_fcf_index);
20369 
20370 	/* Wrap around condition on phba->fcf.fcf_rr_bmask */
20371 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20372 		/*
20373 		 * If we have wrapped then we need to clear the bits that
20374 		 * have been tested so that we can detect when we should
20375 		 * change the priority level.
20376 		 */
20377 		next_fcf_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20378 					       LPFC_SLI4_FCF_TBL_INDX_MAX);
20379 	}
20380 
20381 
20382 	/* Check roundrobin failover list empty condition */
20383 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20384 		next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20385 		/*
20386 		 * If next fcf index is not found check if there are lower
20387 		 * Priority level fcf's in the fcf_priority list.
20388 		 * Set up the rr_bmask with all of the avaiable fcf bits
20389 		 * at that level and continue the selection process.
20390 		 */
20391 		if (lpfc_check_next_fcf_pri_level(phba))
20392 			goto initial_priority;
20393 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20394 				"2844 No roundrobin failover FCF available\n");
20395 
20396 		return LPFC_FCOE_FCF_NEXT_NONE;
20397 	}
20398 
20399 	if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20400 		phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
20401 		LPFC_FCF_FLOGI_FAILED) {
20402 		if (list_is_singular(&phba->fcf.fcf_pri_list))
20403 			return LPFC_FCOE_FCF_NEXT_NONE;
20404 
20405 		goto next_priority;
20406 	}
20407 
20408 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20409 			"2845 Get next roundrobin failover FCF (x%x)\n",
20410 			next_fcf_index);
20411 
20412 	return next_fcf_index;
20413 }
20414 
20415 /**
20416  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20417  * @phba: pointer to lpfc hba data structure.
20418  * @fcf_index: index into the FCF table to 'set'
20419  *
20420  * This routine sets the FCF record index in to the eligible bmask for
20421  * roundrobin failover search. It checks to make sure that the index
20422  * does not go beyond the range of the driver allocated bmask dimension
20423  * before setting the bit.
20424  *
20425  * Returns 0 if the index bit successfully set, otherwise, it returns
20426  * -EINVAL.
20427  **/
20428 int
lpfc_sli4_fcf_rr_index_set(struct lpfc_hba * phba,uint16_t fcf_index)20429 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20430 {
20431 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20432 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20433 				"2610 FCF (x%x) reached driver's book "
20434 				"keeping dimension:x%x\n",
20435 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20436 		return -EINVAL;
20437 	}
20438 	/* Set the eligible FCF record index bmask */
20439 	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20440 
20441 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20442 			"2790 Set FCF (x%x) to roundrobin FCF failover "
20443 			"bmask\n", fcf_index);
20444 
20445 	return 0;
20446 }
20447 
20448 /**
20449  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
20450  * @phba: pointer to lpfc hba data structure.
20451  * @fcf_index: index into the FCF table to 'clear'
20452  *
20453  * This routine clears the FCF record index from the eligible bmask for
20454  * roundrobin failover search. It checks to make sure that the index
20455  * does not go beyond the range of the driver allocated bmask dimension
20456  * before clearing the bit.
20457  **/
20458 void
lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba * phba,uint16_t fcf_index)20459 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20460 {
20461 	struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
20462 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20463 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20464 				"2762 FCF (x%x) reached driver's book "
20465 				"keeping dimension:x%x\n",
20466 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20467 		return;
20468 	}
20469 	/* Clear the eligible FCF record index bmask */
20470 	spin_lock_irq(&phba->hbalock);
20471 	list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20472 				 list) {
20473 		if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20474 			list_del_init(&fcf_pri->list);
20475 			break;
20476 		}
20477 	}
20478 	spin_unlock_irq(&phba->hbalock);
20479 	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20480 
20481 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20482 			"2791 Clear FCF (x%x) from roundrobin failover "
20483 			"bmask\n", fcf_index);
20484 }
20485 
20486 /**
20487  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20488  * @phba: pointer to lpfc hba data structure.
20489  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
20490  *
20491  * This routine is the completion routine for the rediscover FCF table mailbox
20492  * command. If the mailbox command returned failure, it will try to stop the
20493  * FCF rediscover wait timer.
20494  **/
20495 static void
lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)20496 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20497 {
20498 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20499 	uint32_t shdr_status, shdr_add_status;
20500 
20501 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20502 
20503 	shdr_status = bf_get(lpfc_mbox_hdr_status,
20504 			     &redisc_fcf->header.cfg_shdr.response);
20505 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20506 			     &redisc_fcf->header.cfg_shdr.response);
20507 	if (shdr_status || shdr_add_status) {
20508 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20509 				"2746 Requesting for FCF rediscovery failed "
20510 				"status x%x add_status x%x\n",
20511 				shdr_status, shdr_add_status);
20512 		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
20513 			spin_lock_irq(&phba->hbalock);
20514 			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
20515 			spin_unlock_irq(&phba->hbalock);
20516 			/*
20517 			 * CVL event triggered FCF rediscover request failed,
20518 			 * last resort to re-try current registered FCF entry.
20519 			 */
20520 			lpfc_retry_pport_discovery(phba);
20521 		} else {
20522 			spin_lock_irq(&phba->hbalock);
20523 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
20524 			spin_unlock_irq(&phba->hbalock);
20525 			/*
20526 			 * DEAD FCF event triggered FCF rediscover request
20527 			 * failed, last resort to fail over as a link down
20528 			 * to FCF registration.
20529 			 */
20530 			lpfc_sli4_fcf_dead_failthrough(phba);
20531 		}
20532 	} else {
20533 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20534 				"2775 Start FCF rediscover quiescent timer\n");
20535 		/*
20536 		 * Start FCF rediscovery wait timer for pending FCF
20537 		 * before rescan FCF record table.
20538 		 */
20539 		lpfc_fcf_redisc_wait_start_timer(phba);
20540 	}
20541 
20542 	mempool_free(mbox, phba->mbox_mem_pool);
20543 }
20544 
20545 /**
20546  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
20547  * @phba: pointer to lpfc hba data structure.
20548  *
20549  * This routine is invoked to request for rediscovery of the entire FCF table
20550  * by the port.
20551  **/
20552 int
lpfc_sli4_redisc_fcf_table(struct lpfc_hba * phba)20553 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20554 {
20555 	LPFC_MBOXQ_t *mbox;
20556 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20557 	int rc, length;
20558 
20559 	/* Cancel retry delay timers to all vports before FCF rediscover */
20560 	lpfc_cancel_all_vport_retry_delay_timer(phba);
20561 
20562 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20563 	if (!mbox) {
20564 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20565 				"2745 Failed to allocate mbox for "
20566 				"requesting FCF rediscover.\n");
20567 		return -ENOMEM;
20568 	}
20569 
20570 	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20571 		  sizeof(struct lpfc_sli4_cfg_mhdr));
20572 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20573 			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20574 			 length, LPFC_SLI4_MBX_EMBED);
20575 
20576 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20577 	/* Set count to 0 for invalidating the entire FCF database */
20578 	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20579 
20580 	/* Issue the mailbox command asynchronously */
20581 	mbox->vport = phba->pport;
20582 	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20583 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20584 
20585 	if (rc == MBX_NOT_FINISHED) {
20586 		mempool_free(mbox, phba->mbox_mem_pool);
20587 		return -EIO;
20588 	}
20589 	return 0;
20590 }
20591 
20592 /**
20593  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20594  * @phba: pointer to lpfc hba data structure.
20595  *
20596  * This function is the failover routine as a last resort to the FCF DEAD
20597  * event when driver failed to perform fast FCF failover.
20598  **/
20599 void
lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba * phba)20600 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20601 {
20602 	uint32_t link_state;
20603 
20604 	/*
20605 	 * Last resort as FCF DEAD event failover will treat this as
20606 	 * a link down, but save the link state because we don't want
20607 	 * it to be changed to Link Down unless it is already down.
20608 	 */
20609 	link_state = phba->link_state;
20610 	lpfc_linkdown(phba);
20611 	phba->link_state = link_state;
20612 
20613 	/* Unregister FCF if no devices connected to it */
20614 	lpfc_unregister_unused_fcf(phba);
20615 }
20616 
20617 /**
20618  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
20619  * @phba: pointer to lpfc hba data structure.
20620  * @rgn23_data: pointer to configure region 23 data.
20621  *
20622  * This function gets SLI3 port configure region 23 data through memory dump
20623  * mailbox command. When it successfully retrieves data, the size of the data
20624  * will be returned, otherwise, 0 will be returned.
20625  **/
20626 static uint32_t
lpfc_sli_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20627 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20628 {
20629 	LPFC_MBOXQ_t *pmb = NULL;
20630 	MAILBOX_t *mb;
20631 	uint32_t offset = 0;
20632 	int rc;
20633 
20634 	if (!rgn23_data)
20635 		return 0;
20636 
20637 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20638 	if (!pmb) {
20639 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20640 				"2600 failed to allocate mailbox memory\n");
20641 		return 0;
20642 	}
20643 	mb = &pmb->u.mb;
20644 
20645 	do {
20646 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20647 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20648 
20649 		if (rc != MBX_SUCCESS) {
20650 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20651 					"2601 failed to read config "
20652 					"region 23, rc 0x%x Status 0x%x\n",
20653 					rc, mb->mbxStatus);
20654 			mb->un.varDmp.word_cnt = 0;
20655 		}
20656 		/*
20657 		 * dump mem may return a zero when finished or we got a
20658 		 * mailbox error, either way we are done.
20659 		 */
20660 		if (mb->un.varDmp.word_cnt == 0)
20661 			break;
20662 
20663 		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20664 			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20665 
20666 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
20667 				       rgn23_data + offset,
20668 				       mb->un.varDmp.word_cnt);
20669 		offset += mb->un.varDmp.word_cnt;
20670 	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
20671 
20672 	mempool_free(pmb, phba->mbox_mem_pool);
20673 	return offset;
20674 }
20675 
20676 /**
20677  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20678  * @phba: pointer to lpfc hba data structure.
20679  * @rgn23_data: pointer to configure region 23 data.
20680  *
20681  * This function gets SLI4 port configure region 23 data through memory dump
20682  * mailbox command. When it successfully retrieves data, the size of the data
20683  * will be returned, otherwise, 0 will be returned.
20684  **/
20685 static uint32_t
lpfc_sli4_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20686 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20687 {
20688 	LPFC_MBOXQ_t *mboxq = NULL;
20689 	struct lpfc_dmabuf *mp = NULL;
20690 	struct lpfc_mqe *mqe;
20691 	uint32_t data_length = 0;
20692 	int rc;
20693 
20694 	if (!rgn23_data)
20695 		return 0;
20696 
20697 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20698 	if (!mboxq) {
20699 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20700 				"3105 failed to allocate mailbox memory\n");
20701 		return 0;
20702 	}
20703 
20704 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20705 		goto out;
20706 	mqe = &mboxq->u.mqe;
20707 	mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
20708 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20709 	if (rc)
20710 		goto out;
20711 	data_length = mqe->un.mb_words[5];
20712 	if (data_length == 0)
20713 		goto out;
20714 	if (data_length > DMP_RGN23_SIZE) {
20715 		data_length = 0;
20716 		goto out;
20717 	}
20718 	lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20719 out:
20720 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
20721 	return data_length;
20722 }
20723 
20724 /**
20725  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20726  * @phba: pointer to lpfc hba data structure.
20727  *
20728  * This function read region 23 and parse TLV for port status to
20729  * decide if the user disaled the port. If the TLV indicates the
20730  * port is disabled, the hba_flag is set accordingly.
20731  **/
20732 void
lpfc_sli_read_link_ste(struct lpfc_hba * phba)20733 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20734 {
20735 	uint8_t *rgn23_data = NULL;
20736 	uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20737 	uint32_t offset = 0;
20738 
20739 	/* Get adapter Region 23 data */
20740 	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20741 	if (!rgn23_data)
20742 		goto out;
20743 
20744 	if (phba->sli_rev < LPFC_SLI_REV4)
20745 		data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20746 	else {
20747 		if_type = bf_get(lpfc_sli_intf_if_type,
20748 				 &phba->sli4_hba.sli_intf);
20749 		if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20750 			goto out;
20751 		data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20752 	}
20753 
20754 	if (!data_size)
20755 		goto out;
20756 
20757 	/* Check the region signature first */
20758 	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
20759 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20760 			"2619 Config region 23 has bad signature\n");
20761 			goto out;
20762 	}
20763 	offset += 4;
20764 
20765 	/* Check the data structure version */
20766 	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
20767 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20768 			"2620 Config region 23 has bad version\n");
20769 		goto out;
20770 	}
20771 	offset += 4;
20772 
20773 	/* Parse TLV entries in the region */
20774 	while (offset < data_size) {
20775 		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20776 			break;
20777 		/*
20778 		 * If the TLV is not driver specific TLV or driver id is
20779 		 * not linux driver id, skip the record.
20780 		 */
20781 		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20782 		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20783 		    (rgn23_data[offset + 3] != 0)) {
20784 			offset += rgn23_data[offset + 1] * 4 + 4;
20785 			continue;
20786 		}
20787 
20788 		/* Driver found a driver specific TLV in the config region */
20789 		sub_tlv_len = rgn23_data[offset + 1] * 4;
20790 		offset += 4;
20791 		tlv_offset = 0;
20792 
20793 		/*
20794 		 * Search for configured port state sub-TLV.
20795 		 */
20796 		while ((offset < data_size) &&
20797 			(tlv_offset < sub_tlv_len)) {
20798 			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20799 				offset += 4;
20800 				tlv_offset += 4;
20801 				break;
20802 			}
20803 			if (rgn23_data[offset] != PORT_STE_TYPE) {
20804 				offset += rgn23_data[offset + 1] * 4 + 4;
20805 				tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20806 				continue;
20807 			}
20808 
20809 			/* This HBA contains PORT_STE configured */
20810 			if (!rgn23_data[offset + 2])
20811 				phba->hba_flag |= LINK_DISABLED;
20812 
20813 			goto out;
20814 		}
20815 	}
20816 
20817 out:
20818 	kfree(rgn23_data);
20819 	return;
20820 }
20821 
20822 /**
20823  * lpfc_log_fw_write_cmpl - logs firmware write completion status
20824  * @phba: pointer to lpfc hba data structure
20825  * @shdr_status: wr_object rsp's status field
20826  * @shdr_add_status: wr_object rsp's add_status field
20827  * @shdr_add_status_2: wr_object rsp's add_status_2 field
20828  * @shdr_change_status: wr_object rsp's change_status field
20829  * @shdr_csf: wr_object rsp's csf bit
20830  *
20831  * This routine is intended to be called after a firmware write completes.
20832  * It will log next action items to be performed by the user to instantiate
20833  * the newly downloaded firmware or reason for incompatibility.
20834  **/
20835 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)20836 lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20837 		       u32 shdr_add_status, u32 shdr_add_status_2,
20838 		       u32 shdr_change_status, u32 shdr_csf)
20839 {
20840 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20841 			"4198 %s: flash_id x%02x, asic_rev x%02x, "
20842 			"status x%02x, add_status x%02x, add_status_2 x%02x, "
20843 			"change_status x%02x, csf %01x\n", __func__,
20844 			phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20845 			shdr_status, shdr_add_status, shdr_add_status_2,
20846 			shdr_change_status, shdr_csf);
20847 
20848 	if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20849 		switch (shdr_add_status_2) {
20850 		case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20851 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20852 				     "4199 Firmware write failed: "
20853 				     "image incompatible with flash x%02x\n",
20854 				     phba->sli4_hba.flash_id);
20855 			break;
20856 		case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20857 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20858 				     "4200 Firmware write failed: "
20859 				     "image incompatible with ASIC "
20860 				     "architecture x%02x\n",
20861 				     phba->sli4_hba.asic_rev);
20862 			break;
20863 		default:
20864 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20865 				     "4210 Firmware write failed: "
20866 				     "add_status_2 x%02x\n",
20867 				     shdr_add_status_2);
20868 			break;
20869 		}
20870 	} else if (!shdr_status && !shdr_add_status) {
20871 		if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20872 		    shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20873 			if (shdr_csf)
20874 				shdr_change_status =
20875 						   LPFC_CHANGE_STATUS_PCI_RESET;
20876 		}
20877 
20878 		switch (shdr_change_status) {
20879 		case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20880 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20881 				     "3198 Firmware write complete: System "
20882 				     "reboot required to instantiate\n");
20883 			break;
20884 		case (LPFC_CHANGE_STATUS_FW_RESET):
20885 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20886 				     "3199 Firmware write complete: "
20887 				     "Firmware reset required to "
20888 				     "instantiate\n");
20889 			break;
20890 		case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20891 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20892 				     "3200 Firmware write complete: Port "
20893 				     "Migration or PCI Reset required to "
20894 				     "instantiate\n");
20895 			break;
20896 		case (LPFC_CHANGE_STATUS_PCI_RESET):
20897 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20898 				     "3201 Firmware write complete: PCI "
20899 				     "Reset required to instantiate\n");
20900 			break;
20901 		default:
20902 			break;
20903 		}
20904 	}
20905 }
20906 
20907 /**
20908  * lpfc_wr_object - write an object to the firmware
20909  * @phba: HBA structure that indicates port to create a queue on.
20910  * @dmabuf_list: list of dmabufs to write to the port.
20911  * @size: the total byte value of the objects to write to the port.
20912  * @offset: the current offset to be used to start the transfer.
20913  *
20914  * This routine will create a wr_object mailbox command to send to the port.
20915  * the mailbox command will be constructed using the dma buffers described in
20916  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20917  * BDEs that the imbedded mailbox can support. The @offset variable will be
20918  * used to indicate the starting offset of the transfer and will also return
20919  * the offset after the write object mailbox has completed. @size is used to
20920  * determine the end of the object and whether the eof bit should be set.
20921  *
20922  * Return 0 is successful and offset will contain the new offset to use
20923  * for the next write.
20924  * Return negative value for error cases.
20925  **/
20926 int
lpfc_wr_object(struct lpfc_hba * phba,struct list_head * dmabuf_list,uint32_t size,uint32_t * offset)20927 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20928 	       uint32_t size, uint32_t *offset)
20929 {
20930 	struct lpfc_mbx_wr_object *wr_object;
20931 	LPFC_MBOXQ_t *mbox;
20932 	int rc = 0, i = 0;
20933 	int mbox_status = 0;
20934 	uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20935 	uint32_t shdr_change_status = 0, shdr_csf = 0;
20936 	uint32_t mbox_tmo;
20937 	struct lpfc_dmabuf *dmabuf;
20938 	uint32_t written = 0;
20939 	bool check_change_status = false;
20940 
20941 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20942 	if (!mbox)
20943 		return -ENOMEM;
20944 
20945 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20946 			LPFC_MBOX_OPCODE_WRITE_OBJECT,
20947 			sizeof(struct lpfc_mbx_wr_object) -
20948 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20949 
20950 	wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20951 	wr_object->u.request.write_offset = *offset;
20952 	sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20953 	wr_object->u.request.object_name[0] =
20954 		cpu_to_le32(wr_object->u.request.object_name[0]);
20955 	bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20956 	list_for_each_entry(dmabuf, dmabuf_list, list) {
20957 		if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20958 			break;
20959 		wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20960 		wr_object->u.request.bde[i].addrHigh =
20961 			putPaddrHigh(dmabuf->phys);
20962 		if (written + SLI4_PAGE_SIZE >= size) {
20963 			wr_object->u.request.bde[i].tus.f.bdeSize =
20964 				(size - written);
20965 			written += (size - written);
20966 			bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
20967 			bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20968 			check_change_status = true;
20969 		} else {
20970 			wr_object->u.request.bde[i].tus.f.bdeSize =
20971 				SLI4_PAGE_SIZE;
20972 			written += SLI4_PAGE_SIZE;
20973 		}
20974 		i++;
20975 	}
20976 	wr_object->u.request.bde_count = i;
20977 	bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20978 	if (!phba->sli4_hba.intr_enable)
20979 		mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20980 	else {
20981 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
20982 		mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20983 	}
20984 
20985 	/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */
20986 	rc = mbox_status;
20987 
20988 	/* The IOCTL status is embedded in the mailbox subheader. */
20989 	shdr_status = bf_get(lpfc_mbox_hdr_status,
20990 			     &wr_object->header.cfg_shdr.response);
20991 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20992 				 &wr_object->header.cfg_shdr.response);
20993 	shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
20994 				   &wr_object->header.cfg_shdr.response);
20995 	if (check_change_status) {
20996 		shdr_change_status = bf_get(lpfc_wr_object_change_status,
20997 					    &wr_object->u.response);
20998 		shdr_csf = bf_get(lpfc_wr_object_csf,
20999 				  &wr_object->u.response);
21000 	}
21001 
21002 	if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
21003 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21004 				"3025 Write Object mailbox failed with "
21005 				"status x%x add_status x%x, add_status_2 x%x, "
21006 				"mbx status x%x\n",
21007 				shdr_status, shdr_add_status, shdr_add_status_2,
21008 				rc);
21009 		rc = -ENXIO;
21010 		*offset = shdr_add_status;
21011 	} else {
21012 		*offset += wr_object->u.response.actual_write_length;
21013 	}
21014 
21015 	if (rc || check_change_status)
21016 		lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
21017 				       shdr_add_status_2, shdr_change_status,
21018 				       shdr_csf);
21019 
21020 	if (!phba->sli4_hba.intr_enable)
21021 		mempool_free(mbox, phba->mbox_mem_pool);
21022 	else if (mbox_status != MBX_TIMEOUT)
21023 		mempool_free(mbox, phba->mbox_mem_pool);
21024 
21025 	return rc;
21026 }
21027 
21028 /**
21029  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
21030  * @vport: pointer to vport data structure.
21031  *
21032  * This function iterate through the mailboxq and clean up all REG_LOGIN
21033  * and REG_VPI mailbox commands associated with the vport. This function
21034  * is called when driver want to restart discovery of the vport due to
21035  * a Clear Virtual Link event.
21036  **/
21037 void
lpfc_cleanup_pending_mbox(struct lpfc_vport * vport)21038 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
21039 {
21040 	struct lpfc_hba *phba = vport->phba;
21041 	LPFC_MBOXQ_t *mb, *nextmb;
21042 	struct lpfc_nodelist *ndlp;
21043 	struct lpfc_nodelist *act_mbx_ndlp = NULL;
21044 	LIST_HEAD(mbox_cmd_list);
21045 	uint8_t restart_loop;
21046 
21047 	/* Clean up internally queued mailbox commands with the vport */
21048 	spin_lock_irq(&phba->hbalock);
21049 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
21050 		if (mb->vport != vport)
21051 			continue;
21052 
21053 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21054 			(mb->u.mb.mbxCommand != MBX_REG_VPI))
21055 			continue;
21056 
21057 		list_move_tail(&mb->list, &mbox_cmd_list);
21058 	}
21059 	/* Clean up active mailbox command with the vport */
21060 	mb = phba->sli.mbox_active;
21061 	if (mb && (mb->vport == vport)) {
21062 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
21063 			(mb->u.mb.mbxCommand == MBX_REG_VPI))
21064 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21065 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21066 			act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
21067 
21068 			/* This reference is local to this routine.  The
21069 			 * reference is removed at routine exit.
21070 			 */
21071 			act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
21072 
21073 			/* Unregister the RPI when mailbox complete */
21074 			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21075 		}
21076 	}
21077 	/* Cleanup any mailbox completions which are not yet processed */
21078 	do {
21079 		restart_loop = 0;
21080 		list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
21081 			/*
21082 			 * If this mailox is already processed or it is
21083 			 * for another vport ignore it.
21084 			 */
21085 			if ((mb->vport != vport) ||
21086 				(mb->mbox_flag & LPFC_MBX_IMED_UNREG))
21087 				continue;
21088 
21089 			if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21090 				(mb->u.mb.mbxCommand != MBX_REG_VPI))
21091 				continue;
21092 
21093 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21094 			if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21095 				ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
21096 				/* Unregister the RPI when mailbox complete */
21097 				mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21098 				restart_loop = 1;
21099 				spin_unlock_irq(&phba->hbalock);
21100 				spin_lock(&ndlp->lock);
21101 				ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
21102 				spin_unlock(&ndlp->lock);
21103 				spin_lock_irq(&phba->hbalock);
21104 				break;
21105 			}
21106 		}
21107 	} while (restart_loop);
21108 
21109 	spin_unlock_irq(&phba->hbalock);
21110 
21111 	/* Release the cleaned-up mailbox commands */
21112 	while (!list_empty(&mbox_cmd_list)) {
21113 		list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
21114 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21115 			ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
21116 			mb->ctx_ndlp = NULL;
21117 			if (ndlp) {
21118 				spin_lock(&ndlp->lock);
21119 				ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
21120 				spin_unlock(&ndlp->lock);
21121 				lpfc_nlp_put(ndlp);
21122 			}
21123 		}
21124 		lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_UNLOCKED);
21125 	}
21126 
21127 	/* Release the ndlp with the cleaned-up active mailbox command */
21128 	if (act_mbx_ndlp) {
21129 		spin_lock(&act_mbx_ndlp->lock);
21130 		act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
21131 		spin_unlock(&act_mbx_ndlp->lock);
21132 		lpfc_nlp_put(act_mbx_ndlp);
21133 	}
21134 }
21135 
21136 /**
21137  * lpfc_drain_txq - Drain the txq
21138  * @phba: Pointer to HBA context object.
21139  *
21140  * This function attempt to submit IOCBs on the txq
21141  * to the adapter.  For SLI4 adapters, the txq contains
21142  * ELS IOCBs that have been deferred because the there
21143  * are no SGLs.  This congestion can occur with large
21144  * vport counts during node discovery.
21145  **/
21146 
21147 uint32_t
lpfc_drain_txq(struct lpfc_hba * phba)21148 lpfc_drain_txq(struct lpfc_hba *phba)
21149 {
21150 	LIST_HEAD(completions);
21151 	struct lpfc_sli_ring *pring;
21152 	struct lpfc_iocbq *piocbq = NULL;
21153 	unsigned long iflags = 0;
21154 	char *fail_msg = NULL;
21155 	uint32_t txq_cnt = 0;
21156 	struct lpfc_queue *wq;
21157 	int ret = 0;
21158 
21159 	if (phba->link_flag & LS_MDS_LOOPBACK) {
21160 		/* MDS WQE are posted only to first WQ*/
21161 		wq = phba->sli4_hba.hdwq[0].io_wq;
21162 		if (unlikely(!wq))
21163 			return 0;
21164 		pring = wq->pring;
21165 	} else {
21166 		wq = phba->sli4_hba.els_wq;
21167 		if (unlikely(!wq))
21168 			return 0;
21169 		pring = lpfc_phba_elsring(phba);
21170 	}
21171 
21172 	if (unlikely(!pring) || list_empty(&pring->txq))
21173 		return 0;
21174 
21175 	spin_lock_irqsave(&pring->ring_lock, iflags);
21176 	list_for_each_entry(piocbq, &pring->txq, list) {
21177 		txq_cnt++;
21178 	}
21179 
21180 	if (txq_cnt > pring->txq_max)
21181 		pring->txq_max = txq_cnt;
21182 
21183 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
21184 
21185 	while (!list_empty(&pring->txq)) {
21186 		spin_lock_irqsave(&pring->ring_lock, iflags);
21187 
21188 		piocbq = lpfc_sli_ringtx_get(phba, pring);
21189 		if (!piocbq) {
21190 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21191 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21192 				"2823 txq empty and txq_cnt is %d\n ",
21193 				txq_cnt);
21194 			break;
21195 		}
21196 		txq_cnt--;
21197 
21198 		ret = __lpfc_sli_issue_iocb(phba, pring->ringno, piocbq, 0);
21199 
21200 		if (ret && ret != IOCB_BUSY) {
21201 			fail_msg = " - Cannot send IO ";
21202 			piocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21203 		}
21204 		if (fail_msg) {
21205 			piocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
21206 			/* Failed means we can't issue and need to cancel */
21207 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21208 					"2822 IOCB failed %s iotag 0x%x "
21209 					"xri 0x%x %d flg x%x\n",
21210 					fail_msg, piocbq->iotag,
21211 					piocbq->sli4_xritag, ret,
21212 					piocbq->cmd_flag);
21213 			list_add_tail(&piocbq->list, &completions);
21214 			fail_msg = NULL;
21215 		}
21216 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21217 		if (txq_cnt == 0 || ret == IOCB_BUSY)
21218 			break;
21219 	}
21220 	/* Cancel all the IOCBs that cannot be issued */
21221 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21222 			      IOERR_SLI_ABORTED);
21223 
21224 	return txq_cnt;
21225 }
21226 
21227 /**
21228  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21229  * @phba: Pointer to HBA context object.
21230  * @pwqeq: Pointer to command WQE.
21231  * @sglq: Pointer to the scatter gather queue object.
21232  *
21233  * This routine converts the bpl or bde that is in the WQE
21234  * to a sgl list for the sli4 hardware. The physical address
21235  * of the bpl/bde is converted back to a virtual address.
21236  * If the WQE contains a BPL then the list of BDE's is
21237  * converted to sli4_sge's. If the WQE contains a single
21238  * BDE then it is converted to a single sli_sge.
21239  * The WQE is still in cpu endianness so the contents of
21240  * the bpl can be used without byte swapping.
21241  *
21242  * Returns valid XRI = Success, NO_XRI = Failure.
21243  */
21244 static uint16_t
lpfc_wqe_bpl2sgl(struct lpfc_hba * phba,struct lpfc_iocbq * pwqeq,struct lpfc_sglq * sglq)21245 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21246 		 struct lpfc_sglq *sglq)
21247 {
21248 	uint16_t xritag = NO_XRI;
21249 	struct ulp_bde64 *bpl = NULL;
21250 	struct ulp_bde64 bde;
21251 	struct sli4_sge *sgl  = NULL;
21252 	struct lpfc_dmabuf *dmabuf;
21253 	union lpfc_wqe128 *wqe;
21254 	int numBdes = 0;
21255 	int i = 0;
21256 	uint32_t offset = 0; /* accumulated offset in the sg request list */
21257 	int inbound = 0; /* number of sg reply entries inbound from firmware */
21258 	uint32_t cmd;
21259 
21260 	if (!pwqeq || !sglq)
21261 		return xritag;
21262 
21263 	sgl  = (struct sli4_sge *)sglq->sgl;
21264 	wqe = &pwqeq->wqe;
21265 	pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21266 
21267 	cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21268 	if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21269 		return sglq->sli4_xritag;
21270 	numBdes = pwqeq->num_bdes;
21271 	if (numBdes) {
21272 		/* The addrHigh and addrLow fields within the WQE
21273 		 * have not been byteswapped yet so there is no
21274 		 * need to swap them back.
21275 		 */
21276 		if (pwqeq->bpl_dmabuf)
21277 			dmabuf = pwqeq->bpl_dmabuf;
21278 		else
21279 			return xritag;
21280 
21281 		bpl  = (struct ulp_bde64 *)dmabuf->virt;
21282 		if (!bpl)
21283 			return xritag;
21284 
21285 		for (i = 0; i < numBdes; i++) {
21286 			/* Should already be byte swapped. */
21287 			sgl->addr_hi = bpl->addrHigh;
21288 			sgl->addr_lo = bpl->addrLow;
21289 
21290 			sgl->word2 = le32_to_cpu(sgl->word2);
21291 			if ((i+1) == numBdes)
21292 				bf_set(lpfc_sli4_sge_last, sgl, 1);
21293 			else
21294 				bf_set(lpfc_sli4_sge_last, sgl, 0);
21295 			/* swap the size field back to the cpu so we
21296 			 * can assign it to the sgl.
21297 			 */
21298 			bde.tus.w = le32_to_cpu(bpl->tus.w);
21299 			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21300 			/* The offsets in the sgl need to be accumulated
21301 			 * separately for the request and reply lists.
21302 			 * The request is always first, the reply follows.
21303 			 */
21304 			switch (cmd) {
21305 			case CMD_GEN_REQUEST64_WQE:
21306 				/* add up the reply sg entries */
21307 				if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21308 					inbound++;
21309 				/* first inbound? reset the offset */
21310 				if (inbound == 1)
21311 					offset = 0;
21312 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21313 				bf_set(lpfc_sli4_sge_type, sgl,
21314 					LPFC_SGE_TYPE_DATA);
21315 				offset += bde.tus.f.bdeSize;
21316 				break;
21317 			case CMD_FCP_TRSP64_WQE:
21318 				bf_set(lpfc_sli4_sge_offset, sgl, 0);
21319 				bf_set(lpfc_sli4_sge_type, sgl,
21320 					LPFC_SGE_TYPE_DATA);
21321 				break;
21322 			case CMD_FCP_TSEND64_WQE:
21323 			case CMD_FCP_TRECEIVE64_WQE:
21324 				bf_set(lpfc_sli4_sge_type, sgl,
21325 					bpl->tus.f.bdeFlags);
21326 				if (i < 3)
21327 					offset = 0;
21328 				else
21329 					offset += bde.tus.f.bdeSize;
21330 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21331 				break;
21332 			}
21333 			sgl->word2 = cpu_to_le32(sgl->word2);
21334 			bpl++;
21335 			sgl++;
21336 		}
21337 	} else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21338 		/* The addrHigh and addrLow fields of the BDE have not
21339 		 * been byteswapped yet so they need to be swapped
21340 		 * before putting them in the sgl.
21341 		 */
21342 		sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21343 		sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21344 		sgl->word2 = le32_to_cpu(sgl->word2);
21345 		bf_set(lpfc_sli4_sge_last, sgl, 1);
21346 		sgl->word2 = cpu_to_le32(sgl->word2);
21347 		sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21348 	}
21349 	return sglq->sli4_xritag;
21350 }
21351 
21352 /**
21353  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21354  * @phba: Pointer to HBA context object.
21355  * @qp: Pointer to HDW queue.
21356  * @pwqe: Pointer to command WQE.
21357  **/
21358 int
lpfc_sli4_issue_wqe(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * qp,struct lpfc_iocbq * pwqe)21359 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21360 		    struct lpfc_iocbq *pwqe)
21361 {
21362 	union lpfc_wqe128 *wqe = &pwqe->wqe;
21363 	struct lpfc_async_xchg_ctx *ctxp;
21364 	struct lpfc_queue *wq;
21365 	struct lpfc_sglq *sglq;
21366 	struct lpfc_sli_ring *pring;
21367 	unsigned long iflags;
21368 	uint32_t ret = 0;
21369 
21370 	/* NVME_LS and NVME_LS ABTS requests. */
21371 	if (pwqe->cmd_flag & LPFC_IO_NVME_LS) {
21372 		pring =  phba->sli4_hba.nvmels_wq->pring;
21373 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21374 					  qp, wq_access);
21375 		sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21376 		if (!sglq) {
21377 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21378 			return WQE_BUSY;
21379 		}
21380 		pwqe->sli4_lxritag = sglq->sli4_lxritag;
21381 		pwqe->sli4_xritag = sglq->sli4_xritag;
21382 		if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21383 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21384 			return WQE_ERROR;
21385 		}
21386 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21387 		       pwqe->sli4_xritag);
21388 		ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21389 		if (ret) {
21390 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21391 			return ret;
21392 		}
21393 
21394 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21395 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21396 
21397 		lpfc_sli4_poll_eq(qp->hba_eq);
21398 		return 0;
21399 	}
21400 
21401 	/* NVME_FCREQ and NVME_ABTS requests */
21402 	if (pwqe->cmd_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
21403 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21404 		wq = qp->io_wq;
21405 		pring = wq->pring;
21406 
21407 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21408 
21409 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21410 					  qp, wq_access);
21411 		ret = lpfc_sli4_wq_put(wq, wqe);
21412 		if (ret) {
21413 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21414 			return ret;
21415 		}
21416 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21417 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21418 
21419 		lpfc_sli4_poll_eq(qp->hba_eq);
21420 		return 0;
21421 	}
21422 
21423 	/* NVMET requests */
21424 	if (pwqe->cmd_flag & LPFC_IO_NVMET) {
21425 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21426 		wq = qp->io_wq;
21427 		pring = wq->pring;
21428 
21429 		ctxp = pwqe->context_un.axchg;
21430 		sglq = ctxp->ctxbuf->sglq;
21431 		if (pwqe->sli4_xritag ==  NO_XRI) {
21432 			pwqe->sli4_lxritag = sglq->sli4_lxritag;
21433 			pwqe->sli4_xritag = sglq->sli4_xritag;
21434 		}
21435 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21436 		       pwqe->sli4_xritag);
21437 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21438 
21439 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21440 					  qp, wq_access);
21441 		ret = lpfc_sli4_wq_put(wq, wqe);
21442 		if (ret) {
21443 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21444 			return ret;
21445 		}
21446 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21447 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21448 
21449 		lpfc_sli4_poll_eq(qp->hba_eq);
21450 		return 0;
21451 	}
21452 	return WQE_ERROR;
21453 }
21454 
21455 /**
21456  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21457  * @phba: Pointer to HBA context object.
21458  * @cmdiocb: Pointer to driver command iocb object.
21459  * @cmpl: completion function.
21460  *
21461  * Fill the appropriate fields for the abort WQE and call
21462  * internal routine lpfc_sli4_issue_wqe to send the WQE
21463  * This function is called with hbalock held and no ring_lock held.
21464  *
21465  * RETURNS 0 - SUCCESS
21466  **/
21467 
21468 int
lpfc_sli4_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,void * cmpl)21469 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21470 			    void *cmpl)
21471 {
21472 	struct lpfc_vport *vport = cmdiocb->vport;
21473 	struct lpfc_iocbq *abtsiocb = NULL;
21474 	union lpfc_wqe128 *abtswqe;
21475 	struct lpfc_io_buf *lpfc_cmd;
21476 	int retval = IOCB_ERROR;
21477 	u16 xritag = cmdiocb->sli4_xritag;
21478 
21479 	/*
21480 	 * The scsi command can not be in txq and it is in flight because the
21481 	 * pCmd is still pointing at the SCSI command we have to abort. There
21482 	 * is no need to search the txcmplq. Just send an abort to the FW.
21483 	 */
21484 
21485 	abtsiocb = __lpfc_sli_get_iocbq(phba);
21486 	if (!abtsiocb)
21487 		return WQE_NORESOURCE;
21488 
21489 	/* Indicate the IO is being aborted by the driver. */
21490 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
21491 
21492 	abtswqe = &abtsiocb->wqe;
21493 	memset(abtswqe, 0, sizeof(*abtswqe));
21494 
21495 	if (!lpfc_is_link_up(phba) || (phba->link_flag & LS_EXTERNAL_LOOPBACK))
21496 		bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
21497 	bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21498 	abtswqe->abort_cmd.rsrvd5 = 0;
21499 	abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21500 	bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21501 	bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21502 	bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21503 	bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21504 	bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21505 	bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21506 
21507 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
21508 	abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21509 	abtsiocb->cmd_flag |= LPFC_USE_FCPWQIDX;
21510 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
21511 		abtsiocb->cmd_flag |= LPFC_IO_FCP;
21512 	if (cmdiocb->cmd_flag & LPFC_IO_NVME)
21513 		abtsiocb->cmd_flag |= LPFC_IO_NVME;
21514 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
21515 		abtsiocb->cmd_flag |= LPFC_IO_FOF;
21516 	abtsiocb->vport = vport;
21517 	abtsiocb->cmd_cmpl = cmpl;
21518 
21519 	lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21520 	retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21521 
21522 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21523 			 "0359 Abort xri x%x, original iotag x%x, "
21524 			 "abort cmd iotag x%x retval x%x\n",
21525 			 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21526 
21527 	if (retval) {
21528 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21529 		__lpfc_sli_release_iocbq(phba, abtsiocb);
21530 	}
21531 
21532 	return retval;
21533 }
21534 
21535 #ifdef LPFC_MXP_STAT
21536 /**
21537  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21538  * @phba: pointer to lpfc hba data structure.
21539  * @hwqid: belong to which HWQ.
21540  *
21541  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21542  * 15 seconds after a test case is running.
21543  *
21544  * The user should call lpfc_debugfs_multixripools_write before running a test
21545  * case to clear stat_snapshot_taken. Then the user starts a test case. During
21546  * test case is running, stat_snapshot_taken is incremented by 1 every time when
21547  * this routine is called from heartbeat timer. When stat_snapshot_taken is
21548  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21549  **/
lpfc_snapshot_mxp(struct lpfc_hba * phba,u32 hwqid)21550 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21551 {
21552 	struct lpfc_sli4_hdw_queue *qp;
21553 	struct lpfc_multixri_pool *multixri_pool;
21554 	struct lpfc_pvt_pool *pvt_pool;
21555 	struct lpfc_pbl_pool *pbl_pool;
21556 	u32 txcmplq_cnt;
21557 
21558 	qp = &phba->sli4_hba.hdwq[hwqid];
21559 	multixri_pool = qp->p_multixri_pool;
21560 	if (!multixri_pool)
21561 		return;
21562 
21563 	if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21564 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21565 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21566 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21567 
21568 		multixri_pool->stat_pbl_count = pbl_pool->count;
21569 		multixri_pool->stat_pvt_count = pvt_pool->count;
21570 		multixri_pool->stat_busy_count = txcmplq_cnt;
21571 	}
21572 
21573 	multixri_pool->stat_snapshot_taken++;
21574 }
21575 #endif
21576 
21577 /**
21578  * lpfc_adjust_pvt_pool_count - Adjust private pool count
21579  * @phba: pointer to lpfc hba data structure.
21580  * @hwqid: belong to which HWQ.
21581  *
21582  * This routine moves some XRIs from private to public pool when private pool
21583  * is not busy.
21584  **/
lpfc_adjust_pvt_pool_count(struct lpfc_hba * phba,u32 hwqid)21585 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21586 {
21587 	struct lpfc_multixri_pool *multixri_pool;
21588 	u32 io_req_count;
21589 	u32 prev_io_req_count;
21590 
21591 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21592 	if (!multixri_pool)
21593 		return;
21594 	io_req_count = multixri_pool->io_req_count;
21595 	prev_io_req_count = multixri_pool->prev_io_req_count;
21596 
21597 	if (prev_io_req_count != io_req_count) {
21598 		/* Private pool is busy */
21599 		multixri_pool->prev_io_req_count = io_req_count;
21600 	} else {
21601 		/* Private pool is not busy.
21602 		 * Move XRIs from private to public pool.
21603 		 */
21604 		lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21605 	}
21606 }
21607 
21608 /**
21609  * lpfc_adjust_high_watermark - Adjust high watermark
21610  * @phba: pointer to lpfc hba data structure.
21611  * @hwqid: belong to which HWQ.
21612  *
21613  * This routine sets high watermark as number of outstanding XRIs,
21614  * but make sure the new value is between xri_limit/2 and xri_limit.
21615  **/
lpfc_adjust_high_watermark(struct lpfc_hba * phba,u32 hwqid)21616 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21617 {
21618 	u32 new_watermark;
21619 	u32 watermark_max;
21620 	u32 watermark_min;
21621 	u32 xri_limit;
21622 	u32 txcmplq_cnt;
21623 	u32 abts_io_bufs;
21624 	struct lpfc_multixri_pool *multixri_pool;
21625 	struct lpfc_sli4_hdw_queue *qp;
21626 
21627 	qp = &phba->sli4_hba.hdwq[hwqid];
21628 	multixri_pool = qp->p_multixri_pool;
21629 	if (!multixri_pool)
21630 		return;
21631 	xri_limit = multixri_pool->xri_limit;
21632 
21633 	watermark_max = xri_limit;
21634 	watermark_min = xri_limit / 2;
21635 
21636 	txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21637 	abts_io_bufs = qp->abts_scsi_io_bufs;
21638 	abts_io_bufs += qp->abts_nvme_io_bufs;
21639 
21640 	new_watermark = txcmplq_cnt + abts_io_bufs;
21641 	new_watermark = min(watermark_max, new_watermark);
21642 	new_watermark = max(watermark_min, new_watermark);
21643 	multixri_pool->pvt_pool.high_watermark = new_watermark;
21644 
21645 #ifdef LPFC_MXP_STAT
21646 	multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21647 					  new_watermark);
21648 #endif
21649 }
21650 
21651 /**
21652  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21653  * @phba: pointer to lpfc hba data structure.
21654  * @hwqid: belong to which HWQ.
21655  *
21656  * This routine is called from hearbeat timer when pvt_pool is idle.
21657  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21658  * The first step moves (all - low_watermark) amount of XRIs.
21659  * The second step moves the rest of XRIs.
21660  **/
lpfc_move_xri_pvt_to_pbl(struct lpfc_hba * phba,u32 hwqid)21661 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21662 {
21663 	struct lpfc_pbl_pool *pbl_pool;
21664 	struct lpfc_pvt_pool *pvt_pool;
21665 	struct lpfc_sli4_hdw_queue *qp;
21666 	struct lpfc_io_buf *lpfc_ncmd;
21667 	struct lpfc_io_buf *lpfc_ncmd_next;
21668 	unsigned long iflag;
21669 	struct list_head tmp_list;
21670 	u32 tmp_count;
21671 
21672 	qp = &phba->sli4_hba.hdwq[hwqid];
21673 	pbl_pool = &qp->p_multixri_pool->pbl_pool;
21674 	pvt_pool = &qp->p_multixri_pool->pvt_pool;
21675 	tmp_count = 0;
21676 
21677 	lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21678 	lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
21679 
21680 	if (pvt_pool->count > pvt_pool->low_watermark) {
21681 		/* Step 1: move (all - low_watermark) from pvt_pool
21682 		 * to pbl_pool
21683 		 */
21684 
21685 		/* Move low watermark of bufs from pvt_pool to tmp_list */
21686 		INIT_LIST_HEAD(&tmp_list);
21687 		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21688 					 &pvt_pool->list, list) {
21689 			list_move_tail(&lpfc_ncmd->list, &tmp_list);
21690 			tmp_count++;
21691 			if (tmp_count >= pvt_pool->low_watermark)
21692 				break;
21693 		}
21694 
21695 		/* Move all bufs from pvt_pool to pbl_pool */
21696 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21697 
21698 		/* Move all bufs from tmp_list to pvt_pool */
21699 		list_splice(&tmp_list, &pvt_pool->list);
21700 
21701 		pbl_pool->count += (pvt_pool->count - tmp_count);
21702 		pvt_pool->count = tmp_count;
21703 	} else {
21704 		/* Step 2: move the rest from pvt_pool to pbl_pool */
21705 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21706 		pbl_pool->count += pvt_pool->count;
21707 		pvt_pool->count = 0;
21708 	}
21709 
21710 	spin_unlock(&pvt_pool->lock);
21711 	spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21712 }
21713 
21714 /**
21715  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21716  * @phba: pointer to lpfc hba data structure
21717  * @qp: pointer to HDW queue
21718  * @pbl_pool: specified public free XRI pool
21719  * @pvt_pool: specified private free XRI pool
21720  * @count: number of XRIs to move
21721  *
21722  * This routine tries to move some free common bufs from the specified pbl_pool
21723  * to the specified pvt_pool. It might move less than count XRIs if there's not
21724  * enough in public pool.
21725  *
21726  * Return:
21727  *   true - if XRIs are successfully moved from the specified pbl_pool to the
21728  *          specified pvt_pool
21729  *   false - if the specified pbl_pool is empty or locked by someone else
21730  **/
21731 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)21732 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21733 			  struct lpfc_pbl_pool *pbl_pool,
21734 			  struct lpfc_pvt_pool *pvt_pool, u32 count)
21735 {
21736 	struct lpfc_io_buf *lpfc_ncmd;
21737 	struct lpfc_io_buf *lpfc_ncmd_next;
21738 	unsigned long iflag;
21739 	int ret;
21740 
21741 	ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21742 	if (ret) {
21743 		if (pbl_pool->count) {
21744 			/* Move a batch of XRIs from public to private pool */
21745 			lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
21746 			list_for_each_entry_safe(lpfc_ncmd,
21747 						 lpfc_ncmd_next,
21748 						 &pbl_pool->list,
21749 						 list) {
21750 				list_move_tail(&lpfc_ncmd->list,
21751 					       &pvt_pool->list);
21752 				pvt_pool->count++;
21753 				pbl_pool->count--;
21754 				count--;
21755 				if (count == 0)
21756 					break;
21757 			}
21758 
21759 			spin_unlock(&pvt_pool->lock);
21760 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21761 			return true;
21762 		}
21763 		spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21764 	}
21765 
21766 	return false;
21767 }
21768 
21769 /**
21770  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21771  * @phba: pointer to lpfc hba data structure.
21772  * @hwqid: belong to which HWQ.
21773  * @count: number of XRIs to move
21774  *
21775  * This routine tries to find some free common bufs in one of public pools with
21776  * Round Robin method. The search always starts from local hwqid, then the next
21777  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21778  * a batch of free common bufs are moved to private pool on hwqid.
21779  * It might move less than count XRIs if there's not enough in public pool.
21780  **/
lpfc_move_xri_pbl_to_pvt(struct lpfc_hba * phba,u32 hwqid,u32 count)21781 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21782 {
21783 	struct lpfc_multixri_pool *multixri_pool;
21784 	struct lpfc_multixri_pool *next_multixri_pool;
21785 	struct lpfc_pvt_pool *pvt_pool;
21786 	struct lpfc_pbl_pool *pbl_pool;
21787 	struct lpfc_sli4_hdw_queue *qp;
21788 	u32 next_hwqid;
21789 	u32 hwq_count;
21790 	int ret;
21791 
21792 	qp = &phba->sli4_hba.hdwq[hwqid];
21793 	multixri_pool = qp->p_multixri_pool;
21794 	pvt_pool = &multixri_pool->pvt_pool;
21795 	pbl_pool = &multixri_pool->pbl_pool;
21796 
21797 	/* Check if local pbl_pool is available */
21798 	ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
21799 	if (ret) {
21800 #ifdef LPFC_MXP_STAT
21801 		multixri_pool->local_pbl_hit_count++;
21802 #endif
21803 		return;
21804 	}
21805 
21806 	hwq_count = phba->cfg_hdw_queue;
21807 
21808 	/* Get the next hwqid which was found last time */
21809 	next_hwqid = multixri_pool->rrb_next_hwqid;
21810 
21811 	do {
21812 		/* Go to next hwq */
21813 		next_hwqid = (next_hwqid + 1) % hwq_count;
21814 
21815 		next_multixri_pool =
21816 			phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21817 		pbl_pool = &next_multixri_pool->pbl_pool;
21818 
21819 		/* Check if the public free xri pool is available */
21820 		ret = _lpfc_move_xri_pbl_to_pvt(
21821 			phba, qp, pbl_pool, pvt_pool, count);
21822 
21823 		/* Exit while-loop if success or all hwqid are checked */
21824 	} while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21825 
21826 	/* Starting point for the next time */
21827 	multixri_pool->rrb_next_hwqid = next_hwqid;
21828 
21829 	if (!ret) {
21830 		/* stats: all public pools are empty*/
21831 		multixri_pool->pbl_empty_count++;
21832 	}
21833 
21834 #ifdef LPFC_MXP_STAT
21835 	if (ret) {
21836 		if (next_hwqid == hwqid)
21837 			multixri_pool->local_pbl_hit_count++;
21838 		else
21839 			multixri_pool->other_pbl_hit_count++;
21840 	}
21841 #endif
21842 }
21843 
21844 /**
21845  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21846  * @phba: pointer to lpfc hba data structure.
21847  * @hwqid: belong to which HWQ.
21848  *
21849  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21850  * low watermark.
21851  **/
lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba * phba,u32 hwqid)21852 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21853 {
21854 	struct lpfc_multixri_pool *multixri_pool;
21855 	struct lpfc_pvt_pool *pvt_pool;
21856 
21857 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21858 	pvt_pool = &multixri_pool->pvt_pool;
21859 
21860 	if (pvt_pool->count < pvt_pool->low_watermark)
21861 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21862 }
21863 
21864 /**
21865  * lpfc_release_io_buf - Return one IO buf back to free pool
21866  * @phba: pointer to lpfc hba data structure.
21867  * @lpfc_ncmd: IO buf to be returned.
21868  * @qp: belong to which HWQ.
21869  *
21870  * This routine returns one IO buf back to free pool. If this is an urgent IO,
21871  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21872  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21873  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
21874  * lpfc_io_buf_list_put.
21875  **/
lpfc_release_io_buf(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_ncmd,struct lpfc_sli4_hdw_queue * qp)21876 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21877 			 struct lpfc_sli4_hdw_queue *qp)
21878 {
21879 	unsigned long iflag;
21880 	struct lpfc_pbl_pool *pbl_pool;
21881 	struct lpfc_pvt_pool *pvt_pool;
21882 	struct lpfc_epd_pool *epd_pool;
21883 	u32 txcmplq_cnt;
21884 	u32 xri_owned;
21885 	u32 xri_limit;
21886 	u32 abts_io_bufs;
21887 
21888 	/* MUST zero fields if buffer is reused by another protocol */
21889 	lpfc_ncmd->nvmeCmd = NULL;
21890 	lpfc_ncmd->cur_iocbq.cmd_cmpl = NULL;
21891 
21892 	if (phba->cfg_xpsgl && !phba->nvmet_support &&
21893 	    !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21894 		lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21895 
21896 	if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21897 		lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21898 
21899 	if (phba->cfg_xri_rebalancing) {
21900 		if (lpfc_ncmd->expedite) {
21901 			/* Return to expedite pool */
21902 			epd_pool = &phba->epd_pool;
21903 			spin_lock_irqsave(&epd_pool->lock, iflag);
21904 			list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21905 			epd_pool->count++;
21906 			spin_unlock_irqrestore(&epd_pool->lock, iflag);
21907 			return;
21908 		}
21909 
21910 		/* Avoid invalid access if an IO sneaks in and is being rejected
21911 		 * just _after_ xri pools are destroyed in lpfc_offline.
21912 		 * Nothing much can be done at this point.
21913 		 */
21914 		if (!qp->p_multixri_pool)
21915 			return;
21916 
21917 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21918 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21919 
21920 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21921 		abts_io_bufs = qp->abts_scsi_io_bufs;
21922 		abts_io_bufs += qp->abts_nvme_io_bufs;
21923 
21924 		xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21925 		xri_limit = qp->p_multixri_pool->xri_limit;
21926 
21927 #ifdef LPFC_MXP_STAT
21928 		if (xri_owned <= xri_limit)
21929 			qp->p_multixri_pool->below_limit_count++;
21930 		else
21931 			qp->p_multixri_pool->above_limit_count++;
21932 #endif
21933 
21934 		/* XRI goes to either public or private free xri pool
21935 		 *     based on watermark and xri_limit
21936 		 */
21937 		if ((pvt_pool->count < pvt_pool->low_watermark) ||
21938 		    (xri_owned < xri_limit &&
21939 		     pvt_pool->count < pvt_pool->high_watermark)) {
21940 			lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21941 						  qp, free_pvt_pool);
21942 			list_add_tail(&lpfc_ncmd->list,
21943 				      &pvt_pool->list);
21944 			pvt_pool->count++;
21945 			spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21946 		} else {
21947 			lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21948 						  qp, free_pub_pool);
21949 			list_add_tail(&lpfc_ncmd->list,
21950 				      &pbl_pool->list);
21951 			pbl_pool->count++;
21952 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21953 		}
21954 	} else {
21955 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21956 					  qp, free_xri);
21957 		list_add_tail(&lpfc_ncmd->list,
21958 			      &qp->lpfc_io_buf_list_put);
21959 		qp->put_io_bufs++;
21960 		spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21961 				       iflag);
21962 	}
21963 }
21964 
21965 /**
21966  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21967  * @phba: pointer to lpfc hba data structure.
21968  * @qp: pointer to HDW queue
21969  * @pvt_pool: pointer to private pool data structure.
21970  * @ndlp: pointer to lpfc nodelist data structure.
21971  *
21972  * This routine tries to get one free IO buf from private pool.
21973  *
21974  * Return:
21975  *   pointer to one free IO buf - if private pool is not empty
21976  *   NULL - if private pool is empty
21977  **/
21978 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)21979 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
21980 				  struct lpfc_sli4_hdw_queue *qp,
21981 				  struct lpfc_pvt_pool *pvt_pool,
21982 				  struct lpfc_nodelist *ndlp)
21983 {
21984 	struct lpfc_io_buf *lpfc_ncmd;
21985 	struct lpfc_io_buf *lpfc_ncmd_next;
21986 	unsigned long iflag;
21987 
21988 	lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
21989 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21990 				 &pvt_pool->list, list) {
21991 		if (lpfc_test_rrq_active(
21992 			phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21993 			continue;
21994 		list_del(&lpfc_ncmd->list);
21995 		pvt_pool->count--;
21996 		spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21997 		return lpfc_ncmd;
21998 	}
21999 	spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22000 
22001 	return NULL;
22002 }
22003 
22004 /**
22005  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
22006  * @phba: pointer to lpfc hba data structure.
22007  *
22008  * This routine tries to get one free IO buf from expedite pool.
22009  *
22010  * Return:
22011  *   pointer to one free IO buf - if expedite pool is not empty
22012  *   NULL - if expedite pool is empty
22013  **/
22014 static struct lpfc_io_buf *
lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba * phba)22015 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
22016 {
22017 	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
22018 	struct lpfc_io_buf *lpfc_ncmd_next;
22019 	unsigned long iflag;
22020 	struct lpfc_epd_pool *epd_pool;
22021 
22022 	epd_pool = &phba->epd_pool;
22023 
22024 	spin_lock_irqsave(&epd_pool->lock, iflag);
22025 	if (epd_pool->count > 0) {
22026 		list_for_each_entry_safe(iter, lpfc_ncmd_next,
22027 					 &epd_pool->list, list) {
22028 			list_del(&iter->list);
22029 			epd_pool->count--;
22030 			lpfc_ncmd = iter;
22031 			break;
22032 		}
22033 	}
22034 	spin_unlock_irqrestore(&epd_pool->lock, iflag);
22035 
22036 	return lpfc_ncmd;
22037 }
22038 
22039 /**
22040  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
22041  * @phba: pointer to lpfc hba data structure.
22042  * @ndlp: pointer to lpfc nodelist data structure.
22043  * @hwqid: belong to which HWQ
22044  * @expedite: 1 means this request is urgent.
22045  *
22046  * This routine will do the following actions and then return a pointer to
22047  * one free IO buf.
22048  *
22049  * 1. If private free xri count is empty, move some XRIs from public to
22050  *    private pool.
22051  * 2. Get one XRI from private free xri pool.
22052  * 3. If we fail to get one from pvt_pool and this is an expedite request,
22053  *    get one free xri from expedite pool.
22054  *
22055  * Note: ndlp is only used on SCSI side for RRQ testing.
22056  *       The caller should pass NULL for ndlp on NVME side.
22057  *
22058  * Return:
22059  *   pointer to one free IO buf - if private pool is not empty
22060  *   NULL - if private pool is empty
22061  **/
22062 static struct lpfc_io_buf *
lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int hwqid,int expedite)22063 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
22064 				    struct lpfc_nodelist *ndlp,
22065 				    int hwqid, int expedite)
22066 {
22067 	struct lpfc_sli4_hdw_queue *qp;
22068 	struct lpfc_multixri_pool *multixri_pool;
22069 	struct lpfc_pvt_pool *pvt_pool;
22070 	struct lpfc_io_buf *lpfc_ncmd;
22071 
22072 	qp = &phba->sli4_hba.hdwq[hwqid];
22073 	lpfc_ncmd = NULL;
22074 	if (!qp) {
22075 		lpfc_printf_log(phba, KERN_INFO,
22076 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22077 				"5556 NULL qp for hwqid  x%x\n", hwqid);
22078 		return lpfc_ncmd;
22079 	}
22080 	multixri_pool = qp->p_multixri_pool;
22081 	if (!multixri_pool) {
22082 		lpfc_printf_log(phba, KERN_INFO,
22083 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22084 				"5557 NULL multixri for hwqid  x%x\n", hwqid);
22085 		return lpfc_ncmd;
22086 	}
22087 	pvt_pool = &multixri_pool->pvt_pool;
22088 	if (!pvt_pool) {
22089 		lpfc_printf_log(phba, KERN_INFO,
22090 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22091 				"5558 NULL pvt_pool for hwqid  x%x\n", hwqid);
22092 		return lpfc_ncmd;
22093 	}
22094 	multixri_pool->io_req_count++;
22095 
22096 	/* If pvt_pool is empty, move some XRIs from public to private pool */
22097 	if (pvt_pool->count == 0)
22098 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
22099 
22100 	/* Get one XRI from private free xri pool */
22101 	lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
22102 
22103 	if (lpfc_ncmd) {
22104 		lpfc_ncmd->hdwq = qp;
22105 		lpfc_ncmd->hdwq_no = hwqid;
22106 	} else if (expedite) {
22107 		/* If we fail to get one from pvt_pool and this is an expedite
22108 		 * request, get one free xri from expedite pool.
22109 		 */
22110 		lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
22111 	}
22112 
22113 	return lpfc_ncmd;
22114 }
22115 
22116 static inline struct lpfc_io_buf *
lpfc_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int idx)22117 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
22118 {
22119 	struct lpfc_sli4_hdw_queue *qp;
22120 	struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
22121 
22122 	qp = &phba->sli4_hba.hdwq[idx];
22123 	list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22124 				 &qp->lpfc_io_buf_list_get, list) {
22125 		if (lpfc_test_rrq_active(phba, ndlp,
22126 					 lpfc_cmd->cur_iocbq.sli4_lxritag))
22127 			continue;
22128 
22129 		if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22130 			continue;
22131 
22132 		list_del_init(&lpfc_cmd->list);
22133 		qp->get_io_bufs--;
22134 		lpfc_cmd->hdwq = qp;
22135 		lpfc_cmd->hdwq_no = idx;
22136 		return lpfc_cmd;
22137 	}
22138 	return NULL;
22139 }
22140 
22141 /**
22142  * lpfc_get_io_buf - Get one IO buffer from free pool
22143  * @phba: The HBA for which this call is being executed.
22144  * @ndlp: pointer to lpfc nodelist data structure.
22145  * @hwqid: belong to which HWQ
22146  * @expedite: 1 means this request is urgent.
22147  *
22148  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22149  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22150  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22151  *
22152  * Note: ndlp is only used on SCSI side for RRQ testing.
22153  *       The caller should pass NULL for ndlp on NVME side.
22154  *
22155  * Return codes:
22156  *   NULL - Error
22157  *   Pointer to lpfc_io_buf - Success
22158  **/
lpfc_get_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,u32 hwqid,int expedite)22159 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22160 				    struct lpfc_nodelist *ndlp,
22161 				    u32 hwqid, int expedite)
22162 {
22163 	struct lpfc_sli4_hdw_queue *qp;
22164 	unsigned long iflag;
22165 	struct lpfc_io_buf *lpfc_cmd;
22166 
22167 	qp = &phba->sli4_hba.hdwq[hwqid];
22168 	lpfc_cmd = NULL;
22169 	if (!qp) {
22170 		lpfc_printf_log(phba, KERN_WARNING,
22171 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22172 				"5555 NULL qp for hwqid  x%x\n", hwqid);
22173 		return lpfc_cmd;
22174 	}
22175 
22176 	if (phba->cfg_xri_rebalancing)
22177 		lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22178 			phba, ndlp, hwqid, expedite);
22179 	else {
22180 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22181 					  qp, alloc_xri_get);
22182 		if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22183 			lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22184 		if (!lpfc_cmd) {
22185 			lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22186 					  qp, alloc_xri_put);
22187 			list_splice(&qp->lpfc_io_buf_list_put,
22188 				    &qp->lpfc_io_buf_list_get);
22189 			qp->get_io_bufs += qp->put_io_bufs;
22190 			INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22191 			qp->put_io_bufs = 0;
22192 			spin_unlock(&qp->io_buf_list_put_lock);
22193 			if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22194 			    expedite)
22195 				lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22196 		}
22197 		spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22198 	}
22199 
22200 	return lpfc_cmd;
22201 }
22202 
22203 /**
22204  * lpfc_read_object - Retrieve object data from HBA
22205  * @phba: The HBA for which this call is being executed.
22206  * @rdobject: Pathname of object data we want to read.
22207  * @datap: Pointer to where data will be copied to.
22208  * @datasz: size of data area
22209  *
22210  * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22211  * The data will be truncated if datasz is not large enough.
22212  * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22213  * Returns the actual bytes read from the object.
22214  */
22215 int
lpfc_read_object(struct lpfc_hba * phba,char * rdobject,uint32_t * datap,uint32_t datasz)22216 lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22217 		 uint32_t datasz)
22218 {
22219 	struct lpfc_mbx_read_object *read_object;
22220 	LPFC_MBOXQ_t *mbox;
22221 	int rc, length, eof, j, byte_cnt = 0;
22222 	uint32_t shdr_status, shdr_add_status;
22223 	union lpfc_sli4_cfg_shdr *shdr;
22224 	struct lpfc_dmabuf *pcmd;
22225 	u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
22226 
22227 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22228 	if (!mbox)
22229 		return -ENOMEM;
22230 	length = (sizeof(struct lpfc_mbx_read_object) -
22231 		  sizeof(struct lpfc_sli4_cfg_mhdr));
22232 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22233 			 LPFC_MBOX_OPCODE_READ_OBJECT,
22234 			 length, LPFC_SLI4_MBX_EMBED);
22235 	read_object = &mbox->u.mqe.un.read_object;
22236 	shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22237 
22238 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22239 	bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22240 	read_object->u.request.rd_object_offset = 0;
22241 	read_object->u.request.rd_object_cnt = 1;
22242 
22243 	memset((void *)read_object->u.request.rd_object_name, 0,
22244 	       LPFC_OBJ_NAME_SZ);
22245 	scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
22246 	for (j = 0; j < strlen(rdobject); j++)
22247 		read_object->u.request.rd_object_name[j] =
22248 			cpu_to_le32(rd_object_name[j]);
22249 
22250 	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22251 	if (pcmd)
22252 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22253 	if (!pcmd || !pcmd->virt) {
22254 		kfree(pcmd);
22255 		mempool_free(mbox, phba->mbox_mem_pool);
22256 		return -ENOMEM;
22257 	}
22258 	memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22259 	read_object->u.request.rd_object_hbuf[0].pa_lo =
22260 		putPaddrLow(pcmd->phys);
22261 	read_object->u.request.rd_object_hbuf[0].pa_hi =
22262 		putPaddrHigh(pcmd->phys);
22263 	read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22264 
22265 	mbox->vport = phba->pport;
22266 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22267 	mbox->ctx_ndlp = NULL;
22268 
22269 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22270 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22271 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22272 
22273 	if (shdr_status == STATUS_FAILED &&
22274 	    shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22275 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22276 				"4674 No port cfg file in FW.\n");
22277 		byte_cnt = -ENOENT;
22278 	} else if (shdr_status || shdr_add_status || rc) {
22279 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22280 				"2625 READ_OBJECT mailbox failed with "
22281 				"status x%x add_status x%x, mbx status x%x\n",
22282 				shdr_status, shdr_add_status, rc);
22283 		byte_cnt = -ENXIO;
22284 	} else {
22285 		/* Success */
22286 		length = read_object->u.response.rd_object_actual_rlen;
22287 		eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22288 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22289 				"2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22290 				length, datasz, eof);
22291 
22292 		/* Detect the port config file exists but is empty */
22293 		if (!length && eof) {
22294 			byte_cnt = 0;
22295 			goto exit;
22296 		}
22297 
22298 		byte_cnt = length;
22299 		lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22300 	}
22301 
22302  exit:
22303 	/* This is an embedded SLI4 mailbox with an external buffer allocated.
22304 	 * Free the pcmd and then cleanup with the correct routine.
22305 	 */
22306 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22307 	kfree(pcmd);
22308 	lpfc_sli4_mbox_cmd_free(phba, mbox);
22309 	return byte_cnt;
22310 }
22311 
22312 /**
22313  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22314  * @phba: The HBA for which this call is being executed.
22315  * @lpfc_buf: IO buf structure to append the SGL chunk
22316  *
22317  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22318  * and will allocate an SGL chunk if the pool is empty.
22319  *
22320  * Return codes:
22321  *   NULL - Error
22322  *   Pointer to sli4_hybrid_sgl - Success
22323  **/
22324 struct sli4_hybrid_sgl *
lpfc_get_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22325 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22326 {
22327 	struct sli4_hybrid_sgl *list_entry = NULL;
22328 	struct sli4_hybrid_sgl *tmp = NULL;
22329 	struct sli4_hybrid_sgl *allocated_sgl = NULL;
22330 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22331 	struct list_head *buf_list = &hdwq->sgl_list;
22332 	unsigned long iflags;
22333 
22334 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22335 
22336 	if (likely(!list_empty(buf_list))) {
22337 		/* break off 1 chunk from the sgl_list */
22338 		list_for_each_entry_safe(list_entry, tmp,
22339 					 buf_list, list_node) {
22340 			list_move_tail(&list_entry->list_node,
22341 				       &lpfc_buf->dma_sgl_xtra_list);
22342 			break;
22343 		}
22344 	} else {
22345 		/* allocate more */
22346 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22347 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22348 				   cpu_to_node(hdwq->io_wq->chann));
22349 		if (!tmp) {
22350 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22351 					"8353 error kmalloc memory for HDWQ "
22352 					"%d %s\n",
22353 					lpfc_buf->hdwq_no, __func__);
22354 			return NULL;
22355 		}
22356 
22357 		tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22358 					      GFP_ATOMIC, &tmp->dma_phys_sgl);
22359 		if (!tmp->dma_sgl) {
22360 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22361 					"8354 error pool_alloc memory for HDWQ "
22362 					"%d %s\n",
22363 					lpfc_buf->hdwq_no, __func__);
22364 			kfree(tmp);
22365 			return NULL;
22366 		}
22367 
22368 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22369 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22370 	}
22371 
22372 	allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22373 					struct sli4_hybrid_sgl,
22374 					list_node);
22375 
22376 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22377 
22378 	return allocated_sgl;
22379 }
22380 
22381 /**
22382  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22383  * @phba: The HBA for which this call is being executed.
22384  * @lpfc_buf: IO buf structure with the SGL chunk
22385  *
22386  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22387  *
22388  * Return codes:
22389  *   0 - Success
22390  *   -EINVAL - Error
22391  **/
22392 int
lpfc_put_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22393 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22394 {
22395 	int rc = 0;
22396 	struct sli4_hybrid_sgl *list_entry = NULL;
22397 	struct sli4_hybrid_sgl *tmp = NULL;
22398 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22399 	struct list_head *buf_list = &hdwq->sgl_list;
22400 	unsigned long iflags;
22401 
22402 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22403 
22404 	if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22405 		list_for_each_entry_safe(list_entry, tmp,
22406 					 &lpfc_buf->dma_sgl_xtra_list,
22407 					 list_node) {
22408 			list_move_tail(&list_entry->list_node,
22409 				       buf_list);
22410 		}
22411 	} else {
22412 		rc = -EINVAL;
22413 	}
22414 
22415 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22416 	return rc;
22417 }
22418 
22419 /**
22420  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22421  * @phba: phba object
22422  * @hdwq: hdwq to cleanup sgl buff resources on
22423  *
22424  * This routine frees all SGL chunks of hdwq SGL chunk pool.
22425  *
22426  * Return codes:
22427  *   None
22428  **/
22429 void
lpfc_free_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22430 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22431 		       struct lpfc_sli4_hdw_queue *hdwq)
22432 {
22433 	struct list_head *buf_list = &hdwq->sgl_list;
22434 	struct sli4_hybrid_sgl *list_entry = NULL;
22435 	struct sli4_hybrid_sgl *tmp = NULL;
22436 	unsigned long iflags;
22437 
22438 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22439 
22440 	/* Free sgl pool */
22441 	list_for_each_entry_safe(list_entry, tmp,
22442 				 buf_list, list_node) {
22443 		list_del(&list_entry->list_node);
22444 		dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22445 			      list_entry->dma_sgl,
22446 			      list_entry->dma_phys_sgl);
22447 		kfree(list_entry);
22448 	}
22449 
22450 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22451 }
22452 
22453 /**
22454  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22455  * @phba: The HBA for which this call is being executed.
22456  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22457  *
22458  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22459  * and will allocate an CMD/RSP buffer if the pool is empty.
22460  *
22461  * Return codes:
22462  *   NULL - Error
22463  *   Pointer to fcp_cmd_rsp_buf - Success
22464  **/
22465 struct fcp_cmd_rsp_buf *
lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22466 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22467 			      struct lpfc_io_buf *lpfc_buf)
22468 {
22469 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22470 	struct fcp_cmd_rsp_buf *tmp = NULL;
22471 	struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22472 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22473 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22474 	unsigned long iflags;
22475 
22476 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22477 
22478 	if (likely(!list_empty(buf_list))) {
22479 		/* break off 1 chunk from the list */
22480 		list_for_each_entry_safe(list_entry, tmp,
22481 					 buf_list,
22482 					 list_node) {
22483 			list_move_tail(&list_entry->list_node,
22484 				       &lpfc_buf->dma_cmd_rsp_list);
22485 			break;
22486 		}
22487 	} else {
22488 		/* allocate more */
22489 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22490 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22491 				   cpu_to_node(hdwq->io_wq->chann));
22492 		if (!tmp) {
22493 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22494 					"8355 error kmalloc memory for HDWQ "
22495 					"%d %s\n",
22496 					lpfc_buf->hdwq_no, __func__);
22497 			return NULL;
22498 		}
22499 
22500 		tmp->fcp_cmnd = dma_pool_zalloc(phba->lpfc_cmd_rsp_buf_pool,
22501 						GFP_ATOMIC,
22502 						&tmp->fcp_cmd_rsp_dma_handle);
22503 
22504 		if (!tmp->fcp_cmnd) {
22505 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22506 					"8356 error pool_alloc memory for HDWQ "
22507 					"%d %s\n",
22508 					lpfc_buf->hdwq_no, __func__);
22509 			kfree(tmp);
22510 			return NULL;
22511 		}
22512 
22513 		tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22514 				sizeof(struct fcp_cmnd));
22515 
22516 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22517 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22518 	}
22519 
22520 	allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22521 					struct fcp_cmd_rsp_buf,
22522 					list_node);
22523 
22524 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22525 
22526 	return allocated_buf;
22527 }
22528 
22529 /**
22530  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22531  * @phba: The HBA for which this call is being executed.
22532  * @lpfc_buf: IO buf structure with the CMD/RSP buf
22533  *
22534  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22535  *
22536  * Return codes:
22537  *   0 - Success
22538  *   -EINVAL - Error
22539  **/
22540 int
lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22541 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22542 			      struct lpfc_io_buf *lpfc_buf)
22543 {
22544 	int rc = 0;
22545 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22546 	struct fcp_cmd_rsp_buf *tmp = NULL;
22547 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22548 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22549 	unsigned long iflags;
22550 
22551 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22552 
22553 	if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22554 		list_for_each_entry_safe(list_entry, tmp,
22555 					 &lpfc_buf->dma_cmd_rsp_list,
22556 					 list_node) {
22557 			list_move_tail(&list_entry->list_node,
22558 				       buf_list);
22559 		}
22560 	} else {
22561 		rc = -EINVAL;
22562 	}
22563 
22564 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22565 	return rc;
22566 }
22567 
22568 /**
22569  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22570  * @phba: phba object
22571  * @hdwq: hdwq to cleanup cmd rsp buff resources on
22572  *
22573  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22574  *
22575  * Return codes:
22576  *   None
22577  **/
22578 void
lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22579 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22580 			       struct lpfc_sli4_hdw_queue *hdwq)
22581 {
22582 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22583 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22584 	struct fcp_cmd_rsp_buf *tmp = NULL;
22585 	unsigned long iflags;
22586 
22587 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22588 
22589 	/* Free cmd_rsp buf pool */
22590 	list_for_each_entry_safe(list_entry, tmp,
22591 				 buf_list,
22592 				 list_node) {
22593 		list_del(&list_entry->list_node);
22594 		dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22595 			      list_entry->fcp_cmnd,
22596 			      list_entry->fcp_cmd_rsp_dma_handle);
22597 		kfree(list_entry);
22598 	}
22599 
22600 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22601 }
22602 
22603 /**
22604  * lpfc_sli_prep_wqe - Prepare WQE for the command to be posted
22605  * @phba: phba object
22606  * @job: job entry of the command to be posted.
22607  *
22608  * Fill the common fields of the wqe for each of the command.
22609  *
22610  * Return codes:
22611  *	None
22612  **/
22613 void
lpfc_sli_prep_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * job)22614 lpfc_sli_prep_wqe(struct lpfc_hba *phba, struct lpfc_iocbq *job)
22615 {
22616 	u8 cmnd;
22617 	u32 *pcmd;
22618 	u32 if_type = 0;
22619 	u32 fip, abort_tag;
22620 	struct lpfc_nodelist *ndlp = NULL;
22621 	union lpfc_wqe128 *wqe = &job->wqe;
22622 	u8 command_type = ELS_COMMAND_NON_FIP;
22623 
22624 	fip = phba->hba_flag & HBA_FIP_SUPPORT;
22625 	/* The fcp commands will set command type */
22626 	if (job->cmd_flag &  LPFC_IO_FCP)
22627 		command_type = FCP_COMMAND;
22628 	else if (fip && (job->cmd_flag & LPFC_FIP_ELS_ID_MASK))
22629 		command_type = ELS_COMMAND_FIP;
22630 	else
22631 		command_type = ELS_COMMAND_NON_FIP;
22632 
22633 	abort_tag = job->iotag;
22634 	cmnd = bf_get(wqe_cmnd, &wqe->els_req.wqe_com);
22635 
22636 	switch (cmnd) {
22637 	case CMD_ELS_REQUEST64_WQE:
22638 		ndlp = job->ndlp;
22639 
22640 		if_type = bf_get(lpfc_sli_intf_if_type,
22641 				 &phba->sli4_hba.sli_intf);
22642 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22643 			pcmd = (u32 *)job->cmd_dmabuf->virt;
22644 			if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
22645 				     *pcmd == ELS_CMD_SCR ||
22646 				     *pcmd == ELS_CMD_RDF ||
22647 				     *pcmd == ELS_CMD_EDC ||
22648 				     *pcmd == ELS_CMD_RSCN_XMT ||
22649 				     *pcmd == ELS_CMD_FDISC ||
22650 				     *pcmd == ELS_CMD_LOGO ||
22651 				     *pcmd == ELS_CMD_QFPA ||
22652 				     *pcmd == ELS_CMD_UVEM ||
22653 				     *pcmd == ELS_CMD_PLOGI)) {
22654 				bf_set(els_req64_sp, &wqe->els_req, 1);
22655 				bf_set(els_req64_sid, &wqe->els_req,
22656 				       job->vport->fc_myDID);
22657 
22658 				if ((*pcmd == ELS_CMD_FLOGI) &&
22659 				    !(phba->fc_topology ==
22660 				      LPFC_TOPOLOGY_LOOP))
22661 					bf_set(els_req64_sid, &wqe->els_req, 0);
22662 
22663 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
22664 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22665 				       phba->vpi_ids[job->vport->vpi]);
22666 			} else if (pcmd) {
22667 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
22668 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22669 				       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22670 			}
22671 		}
22672 
22673 		bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
22674 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22675 
22676 		bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
22677 		bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
22678 		bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
22679 		bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22680 		bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
22681 		break;
22682 	case CMD_XMIT_ELS_RSP64_WQE:
22683 		ndlp = job->ndlp;
22684 
22685 		/* word4 */
22686 		wqe->xmit_els_rsp.word4 = 0;
22687 
22688 		if_type = bf_get(lpfc_sli_intf_if_type,
22689 				 &phba->sli4_hba.sli_intf);
22690 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22691 			if (job->vport->fc_flag & FC_PT2PT) {
22692 				bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22693 				bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22694 				       job->vport->fc_myDID);
22695 				if (job->vport->fc_myDID == Fabric_DID) {
22696 					bf_set(wqe_els_did,
22697 					       &wqe->xmit_els_rsp.wqe_dest, 0);
22698 				}
22699 			}
22700 		}
22701 
22702 		bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
22703 		bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
22704 		bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
22705 		bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
22706 		       LPFC_WQE_LENLOC_WORD3);
22707 		bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
22708 
22709 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
22710 			bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22711 			bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22712 			       job->vport->fc_myDID);
22713 			bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
22714 		}
22715 
22716 		if (phba->sli_rev == LPFC_SLI_REV4) {
22717 			bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
22718 			       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22719 
22720 			if (bf_get(wqe_ct, &wqe->xmit_els_rsp.wqe_com))
22721 				bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
22722 				       phba->vpi_ids[job->vport->vpi]);
22723 		}
22724 		command_type = OTHER_COMMAND;
22725 		break;
22726 	case CMD_GEN_REQUEST64_WQE:
22727 		/* Word 10 */
22728 		bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
22729 		bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
22730 		bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
22731 		bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22732 		bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
22733 		command_type = OTHER_COMMAND;
22734 		break;
22735 	case CMD_XMIT_SEQUENCE64_WQE:
22736 		if (phba->link_flag & LS_LOOPBACK_MODE)
22737 			bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
22738 
22739 		wqe->xmit_sequence.rsvd3 = 0;
22740 		bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
22741 		bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
22742 		bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
22743 		       LPFC_WQE_IOD_WRITE);
22744 		bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
22745 		       LPFC_WQE_LENLOC_WORD12);
22746 		bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
22747 		command_type = OTHER_COMMAND;
22748 		break;
22749 	case CMD_XMIT_BLS_RSP64_WQE:
22750 		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
22751 		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
22752 		bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
22753 		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
22754 		       phba->vpi_ids[phba->pport->vpi]);
22755 		bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
22756 		bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
22757 		       LPFC_WQE_LENLOC_NONE);
22758 		/* Overwrite the pre-set comnd type with OTHER_COMMAND */
22759 		command_type = OTHER_COMMAND;
22760 		break;
22761 	case CMD_FCP_ICMND64_WQE:	/* task mgmt commands */
22762 	case CMD_ABORT_XRI_WQE:		/* abort iotag */
22763 	case CMD_SEND_FRAME:		/* mds loopback */
22764 		/* cases already formatted for sli4 wqe - no chgs necessary */
22765 		return;
22766 	default:
22767 		dump_stack();
22768 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
22769 				"6207 Invalid command 0x%x\n",
22770 				cmnd);
22771 		break;
22772 	}
22773 
22774 	wqe->generic.wqe_com.abort_tag = abort_tag;
22775 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, job->iotag);
22776 	bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
22777 	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
22778 }
22779