xref: /linux/drivers/scsi/lpfc/lpfc_sli.c (revision be54f8c558027a218423134dd9b8c7c46d92204a)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2025 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 	clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
1028 	next_time = jiffies + secs_to_jiffies(phba->fc_ratov + 1);
1029 	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
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->rrq_list_lock, iflags);
1038 	if ((!list_empty(&phba->active_rrq_list)) &&
1039 	    (!test_bit(FC_UNLOADING, &phba->pport->load_flag)))
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->rrq_list_lock, 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->rrq_list_lock, iflags);
1081 			return rrq;
1082 		}
1083 	}
1084 	spin_unlock_irqrestore(&phba->rrq_list_lock, 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->rrq_list_lock, 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->rrq_list_lock, 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 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
1183 		clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
1184 		goto outnl;
1185 	}
1186 
1187 	spin_lock_irqsave(&phba->hbalock, iflags);
1188 	if (ndlp->vport && test_bit(FC_UNLOADING, &ndlp->vport->load_flag))
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 + secs_to_jiffies(phba->fc_ratov + 1);
1212 	rrq->nlp_DID = ndlp->nlp_DID;
1213 	rrq->vport = ndlp->vport;
1214 	rrq->rxid = rxid;
1215 
1216 	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
1217 	empty = list_empty(&phba->active_rrq_list);
1218 	list_add_tail(&rrq->list, &phba->active_rrq_list);
1219 	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
1220 	set_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
1221 	if (empty)
1222 		lpfc_worker_wake_up(phba);
1223 	return 0;
1224 out:
1225 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1226 outnl:
1227 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1228 			"2921 Can't set rrq active xri:0x%x rxid:0x%x"
1229 			" DID:0x%x Send:%d\n",
1230 			xritag, rxid, ndlp->nlp_DID, send_rrq);
1231 	return -EINVAL;
1232 }
1233 
1234 /**
1235  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1236  * @phba: Pointer to HBA context object.
1237  * @piocbq: Pointer to the iocbq.
1238  *
1239  * The driver calls this function with either the nvme ls ring lock
1240  * or the fc els ring lock held depending on the iocb usage.  This function
1241  * gets a new driver sglq object from the sglq list. If the list is not empty
1242  * then it is successful, it returns pointer to the newly allocated sglq
1243  * object else it returns NULL.
1244  **/
1245 static struct lpfc_sglq *
__lpfc_sli_get_els_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1246 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1247 {
1248 	struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1249 	struct lpfc_sglq *sglq = NULL;
1250 	struct lpfc_sglq *start_sglq = NULL;
1251 	struct lpfc_io_buf *lpfc_cmd;
1252 	struct lpfc_nodelist *ndlp;
1253 	int found = 0;
1254 	u8 cmnd;
1255 
1256 	cmnd = get_job_cmnd(phba, piocbq);
1257 
1258 	if (piocbq->cmd_flag & LPFC_IO_FCP) {
1259 		lpfc_cmd = piocbq->io_buf;
1260 		ndlp = lpfc_cmd->rdata->pnode;
1261 	} else  if ((cmnd == CMD_GEN_REQUEST64_CR) &&
1262 			!(piocbq->cmd_flag & LPFC_IO_LIBDFC)) {
1263 		ndlp = piocbq->ndlp;
1264 	} else  if (piocbq->cmd_flag & LPFC_IO_LIBDFC) {
1265 		if (piocbq->cmd_flag & LPFC_IO_LOOPBACK)
1266 			ndlp = NULL;
1267 		else
1268 			ndlp = piocbq->ndlp;
1269 	} else {
1270 		ndlp = piocbq->ndlp;
1271 	}
1272 
1273 	spin_lock(&phba->sli4_hba.sgl_list_lock);
1274 	list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1275 	start_sglq = sglq;
1276 	while (!found) {
1277 		if (!sglq)
1278 			break;
1279 		if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1280 		    test_bit(sglq->sli4_lxritag,
1281 		    ndlp->active_rrqs_xri_bitmap)) {
1282 			/* This xri has an rrq outstanding for this DID.
1283 			 * put it back in the list and get another xri.
1284 			 */
1285 			list_add_tail(&sglq->list, lpfc_els_sgl_list);
1286 			sglq = NULL;
1287 			list_remove_head(lpfc_els_sgl_list, sglq,
1288 						struct lpfc_sglq, list);
1289 			if (sglq == start_sglq) {
1290 				list_add_tail(&sglq->list, lpfc_els_sgl_list);
1291 				sglq = NULL;
1292 				break;
1293 			} else
1294 				continue;
1295 		}
1296 		sglq->ndlp = ndlp;
1297 		found = 1;
1298 		phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1299 		sglq->state = SGL_ALLOCATED;
1300 	}
1301 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
1302 	return sglq;
1303 }
1304 
1305 /**
1306  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1307  * @phba: Pointer to HBA context object.
1308  * @piocbq: Pointer to the iocbq.
1309  *
1310  * This function is called with the sgl_list lock held. This function
1311  * gets a new driver sglq object from the sglq list. If the
1312  * list is not empty then it is successful, it returns pointer to the newly
1313  * allocated sglq object else it returns NULL.
1314  **/
1315 struct lpfc_sglq *
__lpfc_sli_get_nvmet_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1316 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1317 {
1318 	struct list_head *lpfc_nvmet_sgl_list;
1319 	struct lpfc_sglq *sglq = NULL;
1320 
1321 	lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1322 
1323 	lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1324 
1325 	list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1326 	if (!sglq)
1327 		return NULL;
1328 	phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1329 	sglq->state = SGL_ALLOCATED;
1330 	return sglq;
1331 }
1332 
1333 /**
1334  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1335  * @phba: Pointer to HBA context object.
1336  *
1337  * This function is called with no lock held. This function
1338  * allocates a new driver iocb object from the iocb pool. If the
1339  * allocation is successful, it returns pointer to the newly
1340  * allocated iocb object else it returns NULL.
1341  **/
1342 struct lpfc_iocbq *
lpfc_sli_get_iocbq(struct lpfc_hba * phba)1343 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1344 {
1345 	struct lpfc_iocbq * iocbq = NULL;
1346 	unsigned long iflags;
1347 
1348 	spin_lock_irqsave(&phba->hbalock, iflags);
1349 	iocbq = __lpfc_sli_get_iocbq(phba);
1350 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1351 	return iocbq;
1352 }
1353 
1354 /**
1355  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1356  * @phba: Pointer to HBA context object.
1357  * @iocbq: Pointer to driver iocb object.
1358  *
1359  * This function is called to release the driver iocb object
1360  * to the iocb pool. The iotag in the iocb object
1361  * does not change for each use of the iocb object. This function
1362  * clears all other fields of the iocb object when it is freed.
1363  * The sqlq structure that holds the xritag and phys and virtual
1364  * mappings for the scatter gather list is retrieved from the
1365  * active array of sglq. The get of the sglq pointer also clears
1366  * the entry in the array. If the status of the IO indiactes that
1367  * this IO was aborted then the sglq entry it put on the
1368  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1369  * IO has good status or fails for any other reason then the sglq
1370  * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1371  *  asserted held in the code path calling this routine.
1372  **/
1373 static void
__lpfc_sli_release_iocbq_s4(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1374 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1375 {
1376 	struct lpfc_sglq *sglq;
1377 	unsigned long iflag = 0;
1378 	struct lpfc_sli_ring *pring;
1379 
1380 	if (iocbq->sli4_xritag == NO_XRI)
1381 		sglq = NULL;
1382 	else
1383 		sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1384 
1385 
1386 	if (sglq)  {
1387 		if (iocbq->cmd_flag & LPFC_IO_NVMET) {
1388 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1389 					  iflag);
1390 			sglq->state = SGL_FREED;
1391 			sglq->ndlp = NULL;
1392 			list_add_tail(&sglq->list,
1393 				      &phba->sli4_hba.lpfc_nvmet_sgl_list);
1394 			spin_unlock_irqrestore(
1395 				&phba->sli4_hba.sgl_list_lock, iflag);
1396 			goto out;
1397 		}
1398 
1399 		if ((iocbq->cmd_flag & LPFC_EXCHANGE_BUSY) &&
1400 		    (!(unlikely(pci_channel_offline(phba->pcidev)))) &&
1401 		    sglq->state != SGL_XRI_ABORTED) {
1402 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1403 					  iflag);
1404 
1405 			/* Check if we can get a reference on ndlp */
1406 			if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1407 				sglq->ndlp = NULL;
1408 
1409 			list_add(&sglq->list,
1410 				 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1411 			spin_unlock_irqrestore(
1412 				&phba->sli4_hba.sgl_list_lock, iflag);
1413 		} else {
1414 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1415 					  iflag);
1416 			sglq->state = SGL_FREED;
1417 			sglq->ndlp = NULL;
1418 			list_add_tail(&sglq->list,
1419 				      &phba->sli4_hba.lpfc_els_sgl_list);
1420 			spin_unlock_irqrestore(
1421 				&phba->sli4_hba.sgl_list_lock, iflag);
1422 			pring = lpfc_phba_elsring(phba);
1423 			/* Check if TXQ queue needs to be serviced */
1424 			if (pring && (!list_empty(&pring->txq)))
1425 				lpfc_worker_wake_up(phba);
1426 		}
1427 	}
1428 
1429 out:
1430 	/*
1431 	 * Clean all volatile data fields, preserve iotag and node struct.
1432 	 */
1433 	memset_startat(iocbq, 0, wqe);
1434 	iocbq->sli4_lxritag = NO_XRI;
1435 	iocbq->sli4_xritag = NO_XRI;
1436 	iocbq->cmd_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |
1437 			      LPFC_IO_NVME_LS);
1438 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1439 }
1440 
1441 
1442 /**
1443  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1444  * @phba: Pointer to HBA context object.
1445  * @iocbq: Pointer to driver iocb object.
1446  *
1447  * This function is called to release the driver iocb object to the
1448  * iocb pool. The iotag in the iocb object does not change for each
1449  * use of the iocb object. This function clears all other fields of
1450  * the iocb object when it is freed. The hbalock is asserted held in
1451  * the code path calling this routine.
1452  **/
1453 static void
__lpfc_sli_release_iocbq_s3(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1454 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1455 {
1456 
1457 	/*
1458 	 * Clean all volatile data fields, preserve iotag and node struct.
1459 	 */
1460 	memset_startat(iocbq, 0, iocb);
1461 	iocbq->sli4_xritag = NO_XRI;
1462 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1463 }
1464 
1465 /**
1466  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1467  * @phba: Pointer to HBA context object.
1468  * @iocbq: Pointer to driver iocb object.
1469  *
1470  * This function is called with hbalock held to release driver
1471  * iocb object to the iocb pool. The iotag in the iocb object
1472  * does not change for each use of the iocb object. This function
1473  * clears all other fields of the iocb object when it is freed.
1474  **/
1475 static void
__lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1476 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1477 {
1478 	lockdep_assert_held(&phba->hbalock);
1479 
1480 	phba->__lpfc_sli_release_iocbq(phba, iocbq);
1481 	phba->iocb_cnt--;
1482 }
1483 
1484 /**
1485  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1486  * @phba: Pointer to HBA context object.
1487  * @iocbq: Pointer to driver iocb object.
1488  *
1489  * This function is called with no lock held to release the iocb to
1490  * iocb pool.
1491  **/
1492 void
lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1493 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1494 {
1495 	unsigned long iflags;
1496 
1497 	/*
1498 	 * Clean all volatile data fields, preserve iotag and node struct.
1499 	 */
1500 	spin_lock_irqsave(&phba->hbalock, iflags);
1501 	__lpfc_sli_release_iocbq(phba, iocbq);
1502 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1503 }
1504 
1505 /**
1506  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1507  * @phba: Pointer to HBA context object.
1508  * @iocblist: List of IOCBs.
1509  * @ulpstatus: ULP status in IOCB command field.
1510  * @ulpWord4: ULP word-4 in IOCB command field.
1511  *
1512  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1513  * on the list by invoking the complete callback function associated with the
1514  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1515  * fields.
1516  **/
1517 void
lpfc_sli_cancel_iocbs(struct lpfc_hba * phba,struct list_head * iocblist,uint32_t ulpstatus,uint32_t ulpWord4)1518 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1519 		      uint32_t ulpstatus, uint32_t ulpWord4)
1520 {
1521 	struct lpfc_iocbq *piocb;
1522 
1523 	while (!list_empty(iocblist)) {
1524 		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1525 		if (piocb->cmd_cmpl) {
1526 			if (piocb->cmd_flag & LPFC_IO_NVME) {
1527 				lpfc_nvme_cancel_iocb(phba, piocb,
1528 						      ulpstatus, ulpWord4);
1529 			} else {
1530 				if (phba->sli_rev == LPFC_SLI_REV4) {
1531 					bf_set(lpfc_wcqe_c_status,
1532 					       &piocb->wcqe_cmpl, ulpstatus);
1533 					piocb->wcqe_cmpl.parameter = ulpWord4;
1534 				} else {
1535 					piocb->iocb.ulpStatus = ulpstatus;
1536 					piocb->iocb.un.ulpWord[4] = ulpWord4;
1537 				}
1538 				(piocb->cmd_cmpl) (phba, piocb, piocb);
1539 			}
1540 		} else {
1541 			lpfc_sli_release_iocbq(phba, piocb);
1542 		}
1543 	}
1544 	return;
1545 }
1546 
1547 /**
1548  * lpfc_sli_iocb_cmd_type - Get the iocb type
1549  * @iocb_cmnd: iocb command code.
1550  *
1551  * This function is called by ring event handler function to get the iocb type.
1552  * This function translates the iocb command to an iocb command type used to
1553  * decide the final disposition of each completed IOCB.
1554  * The function returns
1555  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1556  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1557  * LPFC_ABORT_IOCB   if it is an abort iocb
1558  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1559  *
1560  * The caller is not required to hold any lock.
1561  **/
1562 static lpfc_iocb_type
lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)1563 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1564 {
1565 	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1566 
1567 	if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1568 		return 0;
1569 
1570 	switch (iocb_cmnd) {
1571 	case CMD_XMIT_SEQUENCE_CR:
1572 	case CMD_XMIT_SEQUENCE_CX:
1573 	case CMD_XMIT_BCAST_CN:
1574 	case CMD_XMIT_BCAST_CX:
1575 	case CMD_ELS_REQUEST_CR:
1576 	case CMD_ELS_REQUEST_CX:
1577 	case CMD_CREATE_XRI_CR:
1578 	case CMD_CREATE_XRI_CX:
1579 	case CMD_GET_RPI_CN:
1580 	case CMD_XMIT_ELS_RSP_CX:
1581 	case CMD_GET_RPI_CR:
1582 	case CMD_FCP_IWRITE_CR:
1583 	case CMD_FCP_IWRITE_CX:
1584 	case CMD_FCP_IREAD_CR:
1585 	case CMD_FCP_IREAD_CX:
1586 	case CMD_FCP_ICMND_CR:
1587 	case CMD_FCP_ICMND_CX:
1588 	case CMD_FCP_TSEND_CX:
1589 	case CMD_FCP_TRSP_CX:
1590 	case CMD_FCP_TRECEIVE_CX:
1591 	case CMD_FCP_AUTO_TRSP_CX:
1592 	case CMD_ADAPTER_MSG:
1593 	case CMD_ADAPTER_DUMP:
1594 	case CMD_XMIT_SEQUENCE64_CR:
1595 	case CMD_XMIT_SEQUENCE64_CX:
1596 	case CMD_XMIT_BCAST64_CN:
1597 	case CMD_XMIT_BCAST64_CX:
1598 	case CMD_ELS_REQUEST64_CR:
1599 	case CMD_ELS_REQUEST64_CX:
1600 	case CMD_FCP_IWRITE64_CR:
1601 	case CMD_FCP_IWRITE64_CX:
1602 	case CMD_FCP_IREAD64_CR:
1603 	case CMD_FCP_IREAD64_CX:
1604 	case CMD_FCP_ICMND64_CR:
1605 	case CMD_FCP_ICMND64_CX:
1606 	case CMD_FCP_TSEND64_CX:
1607 	case CMD_FCP_TRSP64_CX:
1608 	case CMD_FCP_TRECEIVE64_CX:
1609 	case CMD_GEN_REQUEST64_CR:
1610 	case CMD_GEN_REQUEST64_CX:
1611 	case CMD_XMIT_ELS_RSP64_CX:
1612 	case DSSCMD_IWRITE64_CR:
1613 	case DSSCMD_IWRITE64_CX:
1614 	case DSSCMD_IREAD64_CR:
1615 	case DSSCMD_IREAD64_CX:
1616 	case CMD_SEND_FRAME:
1617 		type = LPFC_SOL_IOCB;
1618 		break;
1619 	case CMD_ABORT_XRI_CN:
1620 	case CMD_ABORT_XRI_CX:
1621 	case CMD_CLOSE_XRI_CN:
1622 	case CMD_CLOSE_XRI_CX:
1623 	case CMD_XRI_ABORTED_CX:
1624 	case CMD_ABORT_MXRI64_CN:
1625 	case CMD_XMIT_BLS_RSP64_CX:
1626 		type = LPFC_ABORT_IOCB;
1627 		break;
1628 	case CMD_RCV_SEQUENCE_CX:
1629 	case CMD_RCV_ELS_REQ_CX:
1630 	case CMD_RCV_SEQUENCE64_CX:
1631 	case CMD_RCV_ELS_REQ64_CX:
1632 	case CMD_ASYNC_STATUS:
1633 	case CMD_IOCB_RCV_SEQ64_CX:
1634 	case CMD_IOCB_RCV_ELS64_CX:
1635 	case CMD_IOCB_RCV_CONT64_CX:
1636 	case CMD_IOCB_RET_XRI64_CX:
1637 		type = LPFC_UNSOL_IOCB;
1638 		break;
1639 	case CMD_IOCB_XMIT_MSEQ64_CR:
1640 	case CMD_IOCB_XMIT_MSEQ64_CX:
1641 	case CMD_IOCB_RCV_SEQ_LIST64_CX:
1642 	case CMD_IOCB_RCV_ELS_LIST64_CX:
1643 	case CMD_IOCB_CLOSE_EXTENDED_CN:
1644 	case CMD_IOCB_ABORT_EXTENDED_CN:
1645 	case CMD_IOCB_RET_HBQE64_CN:
1646 	case CMD_IOCB_FCP_IBIDIR64_CR:
1647 	case CMD_IOCB_FCP_IBIDIR64_CX:
1648 	case CMD_IOCB_FCP_ITASKMGT64_CX:
1649 	case CMD_IOCB_LOGENTRY_CN:
1650 	case CMD_IOCB_LOGENTRY_ASYNC_CN:
1651 		printk("%s - Unhandled SLI-3 Command x%x\n",
1652 				__func__, iocb_cmnd);
1653 		type = LPFC_UNKNOWN_IOCB;
1654 		break;
1655 	default:
1656 		type = LPFC_UNKNOWN_IOCB;
1657 		break;
1658 	}
1659 
1660 	return type;
1661 }
1662 
1663 /**
1664  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1665  * @phba: Pointer to HBA context object.
1666  *
1667  * This function is called from SLI initialization code
1668  * to configure every ring of the HBA's SLI interface. The
1669  * caller is not required to hold any lock. This function issues
1670  * a config_ring mailbox command for each ring.
1671  * This function returns zero if successful else returns a negative
1672  * error code.
1673  **/
1674 static int
lpfc_sli_ring_map(struct lpfc_hba * phba)1675 lpfc_sli_ring_map(struct lpfc_hba *phba)
1676 {
1677 	struct lpfc_sli *psli = &phba->sli;
1678 	LPFC_MBOXQ_t *pmb;
1679 	MAILBOX_t *pmbox;
1680 	int i, rc, ret = 0;
1681 
1682 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1683 	if (!pmb)
1684 		return -ENOMEM;
1685 	pmbox = &pmb->u.mb;
1686 	phba->link_state = LPFC_INIT_MBX_CMDS;
1687 	for (i = 0; i < psli->num_rings; i++) {
1688 		lpfc_config_ring(phba, i, pmb);
1689 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1690 		if (rc != MBX_SUCCESS) {
1691 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1692 					"0446 Adapter failed to init (%d), "
1693 					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
1694 					"ring %d\n",
1695 					rc, pmbox->mbxCommand,
1696 					pmbox->mbxStatus, i);
1697 			phba->link_state = LPFC_HBA_ERROR;
1698 			ret = -ENXIO;
1699 			break;
1700 		}
1701 	}
1702 	mempool_free(pmb, phba->mbox_mem_pool);
1703 	return ret;
1704 }
1705 
1706 /**
1707  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1708  * @phba: Pointer to HBA context object.
1709  * @pring: Pointer to driver SLI ring object.
1710  * @piocb: Pointer to the driver iocb object.
1711  *
1712  * The driver calls this function with the hbalock held for SLI3 ports or
1713  * the ring lock held for SLI4 ports. The function adds the
1714  * new iocb to txcmplq of the given ring. This function always returns
1715  * 0. If this function is called for ELS ring, this function checks if
1716  * there is a vport associated with the ELS command. This function also
1717  * starts els_tmofunc timer if this is an ELS command.
1718  **/
1719 static int
lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)1720 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1721 			struct lpfc_iocbq *piocb)
1722 {
1723 	u32 ulp_command = 0;
1724 
1725 	BUG_ON(!piocb);
1726 	ulp_command = get_job_cmnd(phba, piocb);
1727 
1728 	list_add_tail(&piocb->list, &pring->txcmplq);
1729 	piocb->cmd_flag |= LPFC_IO_ON_TXCMPLQ;
1730 	pring->txcmplq_cnt++;
1731 	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1732 	   (ulp_command != CMD_ABORT_XRI_WQE) &&
1733 	   (ulp_command != CMD_ABORT_XRI_CN) &&
1734 	   (ulp_command != CMD_CLOSE_XRI_CN)) {
1735 		BUG_ON(!piocb->vport);
1736 		if (!test_bit(FC_UNLOADING, &piocb->vport->load_flag))
1737 			mod_timer(&piocb->vport->els_tmofunc,
1738 				  jiffies + secs_to_jiffies(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, cgn_sig_freq;
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 	spin_lock_irqsave(&phba->hbalock, iflags);
1942 
1943 	/* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
1944 	if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
1945 	    phba->link_state < LPFC_LINK_UP) {
1946 		ret_val = 0;
1947 		goto out_unlock;
1948 	}
1949 
1950 	sync_buf = __lpfc_sli_get_iocbq(phba);
1951 	if (!sync_buf) {
1952 		lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
1953 				"6244 No available WQEs for CMF_SYNC_WQE\n");
1954 		ret_val = ENOMEM;
1955 		goto out_unlock;
1956 	}
1957 
1958 	wqe = &sync_buf->wqe;
1959 
1960 	/* WQEs are reused.  Clear stale data and set key fields to zero */
1961 	memset(wqe, 0, sizeof(*wqe));
1962 
1963 	/* If this is the very first CMF_SYNC_WQE, issue an init operation */
1964 	if (!ms) {
1965 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1966 				"6441 CMF Init %d - CMF_SYNC_WQE\n",
1967 				phba->fc_eventTag);
1968 		bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
1969 		bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
1970 		goto initpath;
1971 	}
1972 
1973 	bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
1974 	bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
1975 
1976 	/* Check for alarms / warnings */
1977 	if (atot) {
1978 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1979 			/* We hit an Signal alarm condition */
1980 			bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
1981 		} else {
1982 			/* We hit a FPIN alarm condition */
1983 			bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
1984 		}
1985 	} else if (wtot) {
1986 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
1987 		    phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1988 			cgn_sig_freq = phba->cgn_sig_freq ? phba->cgn_sig_freq :
1989 					lpfc_fabric_cgn_frequency;
1990 			/* We hit an Signal warning condition */
1991 			max = LPFC_SEC_TO_MSEC / cgn_sig_freq *
1992 				lpfc_acqe_cgn_frequency;
1993 			bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
1994 			bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
1995 			warn_sync_period = lpfc_acqe_cgn_frequency;
1996 		} else {
1997 			/* We hit a FPIN warning condition */
1998 			bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
1999 			bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
2000 			if (phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ)
2001 				warn_sync_period =
2002 				LPFC_MSECS_TO_SECS(phba->cgn_fpin_frequency);
2003 		}
2004 	}
2005 
2006 	/* Update total read blocks during previous timer interval */
2007 	wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
2008 
2009 initpath:
2010 	bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
2011 	wqe->cmf_sync.event_tag = phba->fc_eventTag;
2012 	bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
2013 
2014 	/* Setup reqtag to match the wqe completion. */
2015 	bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
2016 
2017 	bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
2018 	bf_set(cmf_sync_period, &wqe->cmf_sync, warn_sync_period);
2019 
2020 	bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
2021 	bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
2022 	bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
2023 
2024 	sync_buf->vport = phba->pport;
2025 	sync_buf->cmd_cmpl = lpfc_cmf_sync_cmpl;
2026 	sync_buf->cmd_dmabuf = NULL;
2027 	sync_buf->rsp_dmabuf = NULL;
2028 	sync_buf->bpl_dmabuf = NULL;
2029 	sync_buf->sli4_xritag = NO_XRI;
2030 
2031 	sync_buf->cmd_flag |= LPFC_IO_CMF;
2032 	ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
2033 	if (ret_val) {
2034 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2035 				"6214 Cannot issue CMF_SYNC_WQE: x%x\n",
2036 				ret_val);
2037 		__lpfc_sli_release_iocbq(phba, sync_buf);
2038 	}
2039 out_unlock:
2040 	spin_unlock_irqrestore(&phba->hbalock, iflags);
2041 	return ret_val;
2042 }
2043 
2044 /**
2045  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
2046  * @phba: Pointer to HBA context object.
2047  * @pring: Pointer to driver SLI ring object.
2048  *
2049  * This function is called with hbalock held and the caller must post the
2050  * iocb without releasing the lock. If the caller releases the lock,
2051  * iocb slot returned by the function is not guaranteed to be available.
2052  * The function returns pointer to the next available iocb slot if there
2053  * is available slot in the ring, else it returns NULL.
2054  * If the get index of the ring is ahead of the put index, the function
2055  * will post an error attention event to the worker thread to take the
2056  * HBA to offline state.
2057  **/
2058 static IOCB_t *
lpfc_sli_next_iocb_slot(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2059 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2060 {
2061 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2062 	uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
2063 
2064 	lockdep_assert_held(&phba->hbalock);
2065 
2066 	if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
2067 	   (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
2068 		pring->sli.sli3.next_cmdidx = 0;
2069 
2070 	if (unlikely(pring->sli.sli3.local_getidx ==
2071 		pring->sli.sli3.next_cmdidx)) {
2072 
2073 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
2074 
2075 		if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
2076 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2077 					"0315 Ring %d issue: portCmdGet %d "
2078 					"is bigger than cmd ring %d\n",
2079 					pring->ringno,
2080 					pring->sli.sli3.local_getidx,
2081 					max_cmd_idx);
2082 
2083 			phba->link_state = LPFC_HBA_ERROR;
2084 			/*
2085 			 * All error attention handlers are posted to
2086 			 * worker thread
2087 			 */
2088 			phba->work_ha |= HA_ERATT;
2089 			phba->work_hs = HS_FFER3;
2090 
2091 			lpfc_worker_wake_up(phba);
2092 
2093 			return NULL;
2094 		}
2095 
2096 		if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
2097 			return NULL;
2098 	}
2099 
2100 	return lpfc_cmd_iocb(phba, pring);
2101 }
2102 
2103 /**
2104  * lpfc_sli_next_iotag - Get an iotag for the iocb
2105  * @phba: Pointer to HBA context object.
2106  * @iocbq: Pointer to driver iocb object.
2107  *
2108  * This function gets an iotag for the iocb. If there is no unused iotag and
2109  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
2110  * array and assigns a new iotag.
2111  * The function returns the allocated iotag if successful, else returns zero.
2112  * Zero is not a valid iotag.
2113  * The caller is not required to hold any lock.
2114  **/
2115 uint16_t
lpfc_sli_next_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)2116 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
2117 {
2118 	struct lpfc_iocbq **new_arr;
2119 	struct lpfc_iocbq **old_arr;
2120 	size_t new_len;
2121 	struct lpfc_sli *psli = &phba->sli;
2122 	uint16_t iotag;
2123 
2124 	spin_lock_irq(&phba->hbalock);
2125 	iotag = psli->last_iotag;
2126 	if(++iotag < psli->iocbq_lookup_len) {
2127 		psli->last_iotag = iotag;
2128 		psli->iocbq_lookup[iotag] = iocbq;
2129 		spin_unlock_irq(&phba->hbalock);
2130 		iocbq->iotag = iotag;
2131 		return iotag;
2132 	} else if (psli->iocbq_lookup_len < (0xffff
2133 					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
2134 		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2135 		spin_unlock_irq(&phba->hbalock);
2136 		new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
2137 				  GFP_KERNEL);
2138 		if (new_arr) {
2139 			spin_lock_irq(&phba->hbalock);
2140 			old_arr = psli->iocbq_lookup;
2141 			if (new_len <= psli->iocbq_lookup_len) {
2142 				/* highly unprobable case */
2143 				kfree(new_arr);
2144 				iotag = psli->last_iotag;
2145 				if(++iotag < psli->iocbq_lookup_len) {
2146 					psli->last_iotag = iotag;
2147 					psli->iocbq_lookup[iotag] = iocbq;
2148 					spin_unlock_irq(&phba->hbalock);
2149 					iocbq->iotag = iotag;
2150 					return iotag;
2151 				}
2152 				spin_unlock_irq(&phba->hbalock);
2153 				return 0;
2154 			}
2155 			if (psli->iocbq_lookup)
2156 				memcpy(new_arr, old_arr,
2157 				       ((psli->last_iotag  + 1) *
2158 					sizeof (struct lpfc_iocbq *)));
2159 			psli->iocbq_lookup = new_arr;
2160 			psli->iocbq_lookup_len = new_len;
2161 			psli->last_iotag = iotag;
2162 			psli->iocbq_lookup[iotag] = iocbq;
2163 			spin_unlock_irq(&phba->hbalock);
2164 			iocbq->iotag = iotag;
2165 			kfree(old_arr);
2166 			return iotag;
2167 		}
2168 	} else
2169 		spin_unlock_irq(&phba->hbalock);
2170 
2171 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2172 			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",
2173 			psli->last_iotag);
2174 
2175 	return 0;
2176 }
2177 
2178 /**
2179  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
2180  * @phba: Pointer to HBA context object.
2181  * @pring: Pointer to driver SLI ring object.
2182  * @iocb: Pointer to iocb slot in the ring.
2183  * @nextiocb: Pointer to driver iocb object which need to be
2184  *            posted to firmware.
2185  *
2186  * This function is called to post a new iocb to the firmware. This
2187  * function copies the new iocb to ring iocb slot and updates the
2188  * ring pointers. It adds the new iocb to txcmplq if there is
2189  * a completion call back for this iocb else the function will free the
2190  * iocb object.  The hbalock is asserted held in the code path calling
2191  * this routine.
2192  **/
2193 static void
lpfc_sli_submit_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,IOCB_t * iocb,struct lpfc_iocbq * nextiocb)2194 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2195 		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
2196 {
2197 	/*
2198 	 * Set up an iotag
2199 	 */
2200 	nextiocb->iocb.ulpIoTag = (nextiocb->cmd_cmpl) ? nextiocb->iotag : 0;
2201 
2202 
2203 	if (pring->ringno == LPFC_ELS_RING) {
2204 		lpfc_debugfs_slow_ring_trc(phba,
2205 			"IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2206 			*(((uint32_t *) &nextiocb->iocb) + 4),
2207 			*(((uint32_t *) &nextiocb->iocb) + 6),
2208 			*(((uint32_t *) &nextiocb->iocb) + 7));
2209 	}
2210 
2211 	/*
2212 	 * Issue iocb command to adapter
2213 	 */
2214 	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
2215 	wmb();
2216 	pring->stats.iocb_cmd++;
2217 
2218 	/*
2219 	 * If there is no completion routine to call, we can release the
2220 	 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
2221 	 * that have no rsp ring completion, cmd_cmpl MUST be NULL.
2222 	 */
2223 	if (nextiocb->cmd_cmpl)
2224 		lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
2225 	else
2226 		__lpfc_sli_release_iocbq(phba, nextiocb);
2227 
2228 	/*
2229 	 * Let the HBA know what IOCB slot will be the next one the
2230 	 * driver will put a command into.
2231 	 */
2232 	pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
2233 	writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
2234 }
2235 
2236 /**
2237  * lpfc_sli_update_full_ring - Update the chip attention register
2238  * @phba: Pointer to HBA context object.
2239  * @pring: Pointer to driver SLI ring object.
2240  *
2241  * The caller is not required to hold any lock for calling this function.
2242  * This function updates the chip attention bits for the ring to inform firmware
2243  * that there are pending work to be done for this ring and requests an
2244  * interrupt when there is space available in the ring. This function is
2245  * called when the driver is unable to post more iocbs to the ring due
2246  * to unavailability of space in the ring.
2247  **/
2248 static void
lpfc_sli_update_full_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2249 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2250 {
2251 	int ringno = pring->ringno;
2252 
2253 	pring->flag |= LPFC_CALL_RING_AVAILABLE;
2254 
2255 	wmb();
2256 
2257 	/*
2258 	 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
2259 	 * The HBA will tell us when an IOCB entry is available.
2260 	 */
2261 	writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
2262 	readl(phba->CAregaddr); /* flush */
2263 
2264 	pring->stats.iocb_cmd_full++;
2265 }
2266 
2267 /**
2268  * lpfc_sli_update_ring - Update chip attention register
2269  * @phba: Pointer to HBA context object.
2270  * @pring: Pointer to driver SLI ring object.
2271  *
2272  * This function updates the chip attention register bit for the
2273  * given ring to inform HBA that there is more work to be done
2274  * in this ring. The caller is not required to hold any lock.
2275  **/
2276 static void
lpfc_sli_update_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2277 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2278 {
2279 	int ringno = pring->ringno;
2280 
2281 	/*
2282 	 * Tell the HBA that there is work to do in this ring.
2283 	 */
2284 	if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2285 		wmb();
2286 		writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2287 		readl(phba->CAregaddr); /* flush */
2288 	}
2289 }
2290 
2291 /**
2292  * lpfc_sli_resume_iocb - Process iocbs in the txq
2293  * @phba: Pointer to HBA context object.
2294  * @pring: Pointer to driver SLI ring object.
2295  *
2296  * This function is called with hbalock held to post pending iocbs
2297  * in the txq to the firmware. This function is called when driver
2298  * detects space available in the ring.
2299  **/
2300 static void
lpfc_sli_resume_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2301 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2302 {
2303 	IOCB_t *iocb;
2304 	struct lpfc_iocbq *nextiocb;
2305 
2306 	lockdep_assert_held(&phba->hbalock);
2307 
2308 	/*
2309 	 * Check to see if:
2310 	 *  (a) there is anything on the txq to send
2311 	 *  (b) link is up
2312 	 *  (c) link attention events can be processed (fcp ring only)
2313 	 *  (d) IOCB processing is not blocked by the outstanding mbox command.
2314 	 */
2315 
2316 	if (lpfc_is_link_up(phba) &&
2317 	    (!list_empty(&pring->txq)) &&
2318 	    (pring->ringno != LPFC_FCP_RING ||
2319 	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {
2320 
2321 		while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2322 		       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2323 			lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2324 
2325 		if (iocb)
2326 			lpfc_sli_update_ring(phba, pring);
2327 		else
2328 			lpfc_sli_update_full_ring(phba, pring);
2329 	}
2330 
2331 	return;
2332 }
2333 
2334 /**
2335  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
2336  * @phba: Pointer to HBA context object.
2337  * @hbqno: HBQ number.
2338  *
2339  * This function is called with hbalock held to get the next
2340  * available slot for the given HBQ. If there is free slot
2341  * available for the HBQ it will return pointer to the next available
2342  * HBQ entry else it will return NULL.
2343  **/
2344 static struct lpfc_hbq_entry *
lpfc_sli_next_hbq_slot(struct lpfc_hba * phba,uint32_t hbqno)2345 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2346 {
2347 	struct hbq_s *hbqp = &phba->hbqs[hbqno];
2348 
2349 	lockdep_assert_held(&phba->hbalock);
2350 
2351 	if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2352 	    ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2353 		hbqp->next_hbqPutIdx = 0;
2354 
2355 	if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
2356 		uint32_t raw_index = phba->hbq_get[hbqno];
2357 		uint32_t getidx = le32_to_cpu(raw_index);
2358 
2359 		hbqp->local_hbqGetIdx = getidx;
2360 
2361 		if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
2362 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2363 					"1802 HBQ %d: local_hbqGetIdx "
2364 					"%u is > than hbqp->entry_count %u\n",
2365 					hbqno, hbqp->local_hbqGetIdx,
2366 					hbqp->entry_count);
2367 
2368 			phba->link_state = LPFC_HBA_ERROR;
2369 			return NULL;
2370 		}
2371 
2372 		if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2373 			return NULL;
2374 	}
2375 
2376 	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2377 			hbqp->hbqPutIdx;
2378 }
2379 
2380 /**
2381  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
2382  * @phba: Pointer to HBA context object.
2383  *
2384  * This function is called with no lock held to free all the
2385  * hbq buffers while uninitializing the SLI interface. It also
2386  * frees the HBQ buffers returned by the firmware but not yet
2387  * processed by the upper layers.
2388  **/
2389 void
lpfc_sli_hbqbuf_free_all(struct lpfc_hba * phba)2390 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2391 {
2392 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2393 	struct hbq_dmabuf *hbq_buf;
2394 	unsigned long flags;
2395 	int i, hbq_count;
2396 
2397 	hbq_count = lpfc_sli_hbq_count();
2398 	/* Return all memory used by all HBQs */
2399 	spin_lock_irqsave(&phba->hbalock, flags);
2400 	for (i = 0; i < hbq_count; ++i) {
2401 		list_for_each_entry_safe(dmabuf, next_dmabuf,
2402 				&phba->hbqs[i].hbq_buffer_list, list) {
2403 			hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2404 			list_del(&hbq_buf->dbuf.list);
2405 			(phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2406 		}
2407 		phba->hbqs[i].buffer_count = 0;
2408 	}
2409 
2410 	/* Mark the HBQs not in use */
2411 	phba->hbq_in_use = 0;
2412 	spin_unlock_irqrestore(&phba->hbalock, flags);
2413 }
2414 
2415 /**
2416  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2417  * @phba: Pointer to HBA context object.
2418  * @hbqno: HBQ number.
2419  * @hbq_buf: Pointer to HBQ buffer.
2420  *
2421  * This function is called with the hbalock held to post a
2422  * hbq buffer to the firmware. If the function finds an empty
2423  * slot in the HBQ, it will post the buffer. The function will return
2424  * pointer to the hbq entry if it successfully post the buffer
2425  * else it will return NULL.
2426  **/
2427 static int
lpfc_sli_hbq_to_firmware(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2428 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2429 			 struct hbq_dmabuf *hbq_buf)
2430 {
2431 	lockdep_assert_held(&phba->hbalock);
2432 	return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2433 }
2434 
2435 /**
2436  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2437  * @phba: Pointer to HBA context object.
2438  * @hbqno: HBQ number.
2439  * @hbq_buf: Pointer to HBQ buffer.
2440  *
2441  * This function is called with the hbalock held to post a hbq buffer to the
2442  * firmware. If the function finds an empty slot in the HBQ, it will post the
2443  * buffer and place it on the hbq_buffer_list. The function will return zero if
2444  * it successfully post the buffer else it will return an error.
2445  **/
2446 static int
lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2447 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2448 			    struct hbq_dmabuf *hbq_buf)
2449 {
2450 	struct lpfc_hbq_entry *hbqe;
2451 	dma_addr_t physaddr = hbq_buf->dbuf.phys;
2452 
2453 	lockdep_assert_held(&phba->hbalock);
2454 	/* Get next HBQ entry slot to use */
2455 	hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2456 	if (hbqe) {
2457 		struct hbq_s *hbqp = &phba->hbqs[hbqno];
2458 
2459 		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2460 		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2461 		hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2462 		hbqe->bde.tus.f.bdeFlags = 0;
2463 		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2464 		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2465 				/* Sync SLIM */
2466 		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2467 		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2468 				/* flush */
2469 		readl(phba->hbq_put + hbqno);
2470 		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2471 		return 0;
2472 	} else
2473 		return -ENOMEM;
2474 }
2475 
2476 /**
2477  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2478  * @phba: Pointer to HBA context object.
2479  * @hbqno: HBQ number.
2480  * @hbq_buf: Pointer to HBQ buffer.
2481  *
2482  * This function is called with the hbalock held to post an RQE to the SLI4
2483  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2484  * the hbq_buffer_list and return zero, otherwise it will return an error.
2485  **/
2486 static int
lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2487 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2488 			    struct hbq_dmabuf *hbq_buf)
2489 {
2490 	int rc;
2491 	struct lpfc_rqe hrqe;
2492 	struct lpfc_rqe drqe;
2493 	struct lpfc_queue *hrq;
2494 	struct lpfc_queue *drq;
2495 
2496 	if (hbqno != LPFC_ELS_HBQ)
2497 		return 1;
2498 	hrq = phba->sli4_hba.hdr_rq;
2499 	drq = phba->sli4_hba.dat_rq;
2500 
2501 	lockdep_assert_held(&phba->hbalock);
2502 	hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2503 	hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2504 	drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2505 	drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2506 	rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2507 	if (rc < 0)
2508 		return rc;
2509 	hbq_buf->tag = (rc | (hbqno << 16));
2510 	list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2511 	return 0;
2512 }
2513 
2514 /* HBQ for ELS and CT traffic. */
2515 static struct lpfc_hbq_init lpfc_els_hbq = {
2516 	.rn = 1,
2517 	.entry_count = 256,
2518 	.mask_count = 0,
2519 	.profile = 0,
2520 	.ring_mask = (1 << LPFC_ELS_RING),
2521 	.buffer_count = 0,
2522 	.init_count = 40,
2523 	.add_count = 40,
2524 };
2525 
2526 /* Array of HBQs */
2527 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2528 	&lpfc_els_hbq,
2529 };
2530 
2531 /**
2532  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2533  * @phba: Pointer to HBA context object.
2534  * @hbqno: HBQ number.
2535  * @count: Number of HBQ buffers to be posted.
2536  *
2537  * This function is called with no lock held to post more hbq buffers to the
2538  * given HBQ. The function returns the number of HBQ buffers successfully
2539  * posted.
2540  **/
2541 static int
lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba * phba,uint32_t hbqno,uint32_t count)2542 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2543 {
2544 	uint32_t i, posted = 0;
2545 	unsigned long flags;
2546 	struct hbq_dmabuf *hbq_buffer;
2547 	LIST_HEAD(hbq_buf_list);
2548 	if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2549 		return 0;
2550 
2551 	if ((phba->hbqs[hbqno].buffer_count + count) >
2552 	    lpfc_hbq_defs[hbqno]->entry_count)
2553 		count = lpfc_hbq_defs[hbqno]->entry_count -
2554 					phba->hbqs[hbqno].buffer_count;
2555 	if (!count)
2556 		return 0;
2557 	/* Allocate HBQ entries */
2558 	for (i = 0; i < count; i++) {
2559 		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2560 		if (!hbq_buffer)
2561 			break;
2562 		list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2563 	}
2564 	/* Check whether HBQ is still in use */
2565 	spin_lock_irqsave(&phba->hbalock, flags);
2566 	if (!phba->hbq_in_use)
2567 		goto err;
2568 	while (!list_empty(&hbq_buf_list)) {
2569 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2570 				 dbuf.list);
2571 		hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2572 				      (hbqno << 16));
2573 		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2574 			phba->hbqs[hbqno].buffer_count++;
2575 			posted++;
2576 		} else
2577 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2578 	}
2579 	spin_unlock_irqrestore(&phba->hbalock, flags);
2580 	return posted;
2581 err:
2582 	spin_unlock_irqrestore(&phba->hbalock, flags);
2583 	while (!list_empty(&hbq_buf_list)) {
2584 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2585 				 dbuf.list);
2586 		(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2587 	}
2588 	return 0;
2589 }
2590 
2591 /**
2592  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2593  * @phba: Pointer to HBA context object.
2594  * @qno: HBQ number.
2595  *
2596  * This function posts more buffers to the HBQ. This function
2597  * is called with no lock held. The function returns the number of HBQ entries
2598  * successfully allocated.
2599  **/
2600 int
lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba * phba,uint32_t qno)2601 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2602 {
2603 	if (phba->sli_rev == LPFC_SLI_REV4)
2604 		return 0;
2605 	else
2606 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2607 					 lpfc_hbq_defs[qno]->add_count);
2608 }
2609 
2610 /**
2611  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2612  * @phba: Pointer to HBA context object.
2613  * @qno:  HBQ queue number.
2614  *
2615  * This function is called from SLI initialization code path with
2616  * no lock held to post initial HBQ buffers to firmware. The
2617  * function returns the number of HBQ entries successfully allocated.
2618  **/
2619 static int
lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba * phba,uint32_t qno)2620 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2621 {
2622 	if (phba->sli_rev == LPFC_SLI_REV4)
2623 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2624 					lpfc_hbq_defs[qno]->entry_count);
2625 	else
2626 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2627 					 lpfc_hbq_defs[qno]->init_count);
2628 }
2629 
2630 /*
2631  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2632  *
2633  * This function removes the first hbq buffer on an hbq list and returns a
2634  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2635  **/
2636 static struct hbq_dmabuf *
lpfc_sli_hbqbuf_get(struct list_head * rb_list)2637 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2638 {
2639 	struct lpfc_dmabuf *d_buf;
2640 
2641 	list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2642 	if (!d_buf)
2643 		return NULL;
2644 	return container_of(d_buf, struct hbq_dmabuf, dbuf);
2645 }
2646 
2647 /**
2648  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2649  * @phba: Pointer to HBA context object.
2650  * @hrq: HBQ number.
2651  *
2652  * This function removes the first RQ buffer on an RQ buffer list and returns a
2653  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2654  **/
2655 static struct rqb_dmabuf *
lpfc_sli_rqbuf_get(struct lpfc_hba * phba,struct lpfc_queue * hrq)2656 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2657 {
2658 	struct lpfc_dmabuf *h_buf;
2659 	struct lpfc_rqb *rqbp;
2660 
2661 	rqbp = hrq->rqbp;
2662 	list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2663 			 struct lpfc_dmabuf, list);
2664 	if (!h_buf)
2665 		return NULL;
2666 	rqbp->buffer_count--;
2667 	return container_of(h_buf, struct rqb_dmabuf, hbuf);
2668 }
2669 
2670 /**
2671  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2672  * @phba: Pointer to HBA context object.
2673  * @tag: Tag of the hbq buffer.
2674  *
2675  * This function searches for the hbq buffer associated with the given tag in
2676  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2677  * otherwise it returns NULL.
2678  **/
2679 static struct hbq_dmabuf *
lpfc_sli_hbqbuf_find(struct lpfc_hba * phba,uint32_t tag)2680 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2681 {
2682 	struct lpfc_dmabuf *d_buf;
2683 	struct hbq_dmabuf *hbq_buf;
2684 	uint32_t hbqno;
2685 
2686 	hbqno = tag >> 16;
2687 	if (hbqno >= LPFC_MAX_HBQS)
2688 		return NULL;
2689 
2690 	spin_lock_irq(&phba->hbalock);
2691 	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2692 		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2693 		if (hbq_buf->tag == tag) {
2694 			spin_unlock_irq(&phba->hbalock);
2695 			return hbq_buf;
2696 		}
2697 	}
2698 	spin_unlock_irq(&phba->hbalock);
2699 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2700 			"1803 Bad hbq tag. Data: x%x x%x\n",
2701 			tag, phba->hbqs[tag >> 16].buffer_count);
2702 	return NULL;
2703 }
2704 
2705 /**
2706  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2707  * @phba: Pointer to HBA context object.
2708  * @hbq_buffer: Pointer to HBQ buffer.
2709  *
2710  * This function is called with hbalock. This function gives back
2711  * the hbq buffer to firmware. If the HBQ does not have space to
2712  * post the buffer, it will free the buffer.
2713  **/
2714 void
lpfc_sli_free_hbq(struct lpfc_hba * phba,struct hbq_dmabuf * hbq_buffer)2715 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2716 {
2717 	uint32_t hbqno;
2718 
2719 	if (hbq_buffer) {
2720 		hbqno = hbq_buffer->tag >> 16;
2721 		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2722 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2723 	}
2724 }
2725 
2726 /**
2727  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2728  * @mbxCommand: mailbox command code.
2729  *
2730  * This function is called by the mailbox event handler function to verify
2731  * that the completed mailbox command is a legitimate mailbox command. If the
2732  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2733  * and the mailbox event handler will take the HBA offline.
2734  **/
2735 static int
lpfc_sli_chk_mbx_command(uint8_t mbxCommand)2736 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2737 {
2738 	uint8_t ret;
2739 
2740 	switch (mbxCommand) {
2741 	case MBX_LOAD_SM:
2742 	case MBX_READ_NV:
2743 	case MBX_WRITE_NV:
2744 	case MBX_WRITE_VPARMS:
2745 	case MBX_RUN_BIU_DIAG:
2746 	case MBX_INIT_LINK:
2747 	case MBX_DOWN_LINK:
2748 	case MBX_CONFIG_LINK:
2749 	case MBX_CONFIG_RING:
2750 	case MBX_RESET_RING:
2751 	case MBX_READ_CONFIG:
2752 	case MBX_READ_RCONFIG:
2753 	case MBX_READ_SPARM:
2754 	case MBX_READ_STATUS:
2755 	case MBX_READ_RPI:
2756 	case MBX_READ_XRI:
2757 	case MBX_READ_REV:
2758 	case MBX_READ_LNK_STAT:
2759 	case MBX_REG_LOGIN:
2760 	case MBX_UNREG_LOGIN:
2761 	case MBX_CLEAR_LA:
2762 	case MBX_DUMP_MEMORY:
2763 	case MBX_DUMP_CONTEXT:
2764 	case MBX_RUN_DIAGS:
2765 	case MBX_RESTART:
2766 	case MBX_UPDATE_CFG:
2767 	case MBX_DOWN_LOAD:
2768 	case MBX_DEL_LD_ENTRY:
2769 	case MBX_RUN_PROGRAM:
2770 	case MBX_SET_MASK:
2771 	case MBX_SET_VARIABLE:
2772 	case MBX_UNREG_D_ID:
2773 	case MBX_KILL_BOARD:
2774 	case MBX_CONFIG_FARP:
2775 	case MBX_BEACON:
2776 	case MBX_LOAD_AREA:
2777 	case MBX_RUN_BIU_DIAG64:
2778 	case MBX_CONFIG_PORT:
2779 	case MBX_READ_SPARM64:
2780 	case MBX_READ_RPI64:
2781 	case MBX_REG_LOGIN64:
2782 	case MBX_READ_TOPOLOGY:
2783 	case MBX_WRITE_WWN:
2784 	case MBX_SET_DEBUG:
2785 	case MBX_LOAD_EXP_ROM:
2786 	case MBX_ASYNCEVT_ENABLE:
2787 	case MBX_REG_VPI:
2788 	case MBX_UNREG_VPI:
2789 	case MBX_HEARTBEAT:
2790 	case MBX_PORT_CAPABILITIES:
2791 	case MBX_PORT_IOV_CONTROL:
2792 	case MBX_SLI4_CONFIG:
2793 	case MBX_SLI4_REQ_FTRS:
2794 	case MBX_REG_FCFI:
2795 	case MBX_UNREG_FCFI:
2796 	case MBX_REG_VFI:
2797 	case MBX_UNREG_VFI:
2798 	case MBX_INIT_VPI:
2799 	case MBX_INIT_VFI:
2800 	case MBX_RESUME_RPI:
2801 	case MBX_READ_EVENT_LOG_STATUS:
2802 	case MBX_READ_EVENT_LOG:
2803 	case MBX_SECURITY_MGMT:
2804 	case MBX_AUTH_PORT:
2805 	case MBX_ACCESS_VDATA:
2806 		ret = mbxCommand;
2807 		break;
2808 	default:
2809 		ret = MBX_SHUTDOWN;
2810 		break;
2811 	}
2812 	return ret;
2813 }
2814 
2815 /**
2816  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2817  * @phba: Pointer to HBA context object.
2818  * @pmboxq: Pointer to mailbox command.
2819  *
2820  * This is completion handler function for mailbox commands issued from
2821  * lpfc_sli_issue_mbox_wait function. This function is called by the
2822  * mailbox event handler function with no lock held. This function
2823  * will wake up thread waiting on the wait queue pointed by context1
2824  * of the mailbox.
2825  **/
2826 void
lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)2827 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2828 {
2829 	unsigned long drvr_flag;
2830 	struct completion *pmbox_done;
2831 
2832 	/*
2833 	 * If pmbox_done is empty, the driver thread gave up waiting and
2834 	 * continued running.
2835 	 */
2836 	pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2837 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
2838 	pmbox_done = pmboxq->ctx_u.mbox_wait;
2839 	if (pmbox_done)
2840 		complete(pmbox_done);
2841 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2842 	return;
2843 }
2844 
2845 /**
2846  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2847  * @phba: Pointer to HBA context object.
2848  * @pmb: Pointer to mailbox object.
2849  *
2850  * This function is the default mailbox completion handler. It
2851  * frees the memory resources associated with the completed mailbox
2852  * command. If the completed command is a REG_LOGIN mailbox command,
2853  * this function will issue a UREG_LOGIN to re-claim the RPI.
2854  **/
2855 void
lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2856 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2857 {
2858 	struct lpfc_vport  *vport = pmb->vport;
2859 	struct lpfc_dmabuf *mp;
2860 	struct lpfc_nodelist *ndlp;
2861 	struct Scsi_Host *shost;
2862 	uint16_t rpi, vpi;
2863 	int rc;
2864 
2865 	/*
2866 	 * If a REG_LOGIN succeeded  after node is destroyed or node
2867 	 * is in re-discovery driver need to cleanup the RPI.
2868 	 */
2869 	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
2870 	    pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2871 	    !pmb->u.mb.mbxStatus) {
2872 		mp = pmb->ctx_buf;
2873 		if (mp) {
2874 			pmb->ctx_buf = NULL;
2875 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
2876 			kfree(mp);
2877 		}
2878 		rpi = pmb->u.mb.un.varWords[0];
2879 		vpi = pmb->u.mb.un.varRegLogin.vpi;
2880 		if (phba->sli_rev == LPFC_SLI_REV4)
2881 			vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2882 		lpfc_unreg_login(phba, vpi, rpi, pmb);
2883 		pmb->vport = vport;
2884 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2885 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2886 		if (rc != MBX_NOT_FINISHED)
2887 			return;
2888 	}
2889 
2890 	if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2891 		!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
2892 		!pmb->u.mb.mbxStatus) {
2893 		shost = lpfc_shost_from_vport(vport);
2894 		spin_lock_irq(shost->host_lock);
2895 		vport->vpi_state |= LPFC_VPI_REGISTERED;
2896 		spin_unlock_irq(shost->host_lock);
2897 		clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
2898 	}
2899 
2900 	if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2901 		ndlp = pmb->ctx_ndlp;
2902 		lpfc_nlp_put(ndlp);
2903 	}
2904 
2905 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2906 		ndlp = pmb->ctx_ndlp;
2907 
2908 		/* Check to see if there are any deferred events to process */
2909 		if (ndlp) {
2910 			lpfc_printf_vlog(
2911 				vport,
2912 				KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2913 				"1438 UNREG cmpl deferred mbox x%x "
2914 				"on NPort x%x Data: x%lx x%x x%px x%lx x%x\n",
2915 				ndlp->nlp_rpi, ndlp->nlp_DID,
2916 				ndlp->nlp_flag, ndlp->nlp_defer_did,
2917 				ndlp, vport->load_flag, kref_read(&ndlp->kref));
2918 
2919 			if (test_bit(NLP_UNREG_INP, &ndlp->nlp_flag) &&
2920 			    ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING) {
2921 				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
2922 				ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2923 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2924 			} else {
2925 				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
2926 			}
2927 
2928 			/* The unreg_login mailbox is complete and had a
2929 			 * reference that has to be released.  The PLOGI
2930 			 * got its own ref.
2931 			 */
2932 			lpfc_nlp_put(ndlp);
2933 			pmb->ctx_ndlp = NULL;
2934 		}
2935 	}
2936 
2937 	/* This nlp_put pairs with lpfc_sli4_resume_rpi */
2938 	if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
2939 		ndlp = pmb->ctx_ndlp;
2940 		lpfc_nlp_put(ndlp);
2941 	}
2942 
2943 	/* Check security permission status on INIT_LINK mailbox command */
2944 	if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2945 	    (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2946 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2947 				"2860 SLI authentication is required "
2948 				"for INIT_LINK but has not done yet\n");
2949 
2950 	if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2951 		lpfc_sli4_mbox_cmd_free(phba, pmb);
2952 	else
2953 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
2954 }
2955  /**
2956  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2957  * @phba: Pointer to HBA context object.
2958  * @pmb: Pointer to mailbox object.
2959  *
2960  * This function is the unreg rpi mailbox completion handler. It
2961  * frees the memory resources associated with the completed mailbox
2962  * command. An additional reference is put on the ndlp to prevent
2963  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2964  * the unreg mailbox command completes, this routine puts the
2965  * reference back.
2966  *
2967  **/
2968 void
lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2969 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2970 {
2971 	struct lpfc_vport  *vport = pmb->vport;
2972 	struct lpfc_nodelist *ndlp;
2973 	bool unreg_inp;
2974 
2975 	ndlp = pmb->ctx_ndlp;
2976 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2977 		if (phba->sli_rev == LPFC_SLI_REV4 &&
2978 		    (bf_get(lpfc_sli_intf_if_type,
2979 		     &phba->sli4_hba.sli_intf) >=
2980 		     LPFC_SLI_INTF_IF_TYPE_2)) {
2981 			if (ndlp) {
2982 				lpfc_printf_vlog(
2983 					 vport, KERN_INFO,
2984 					 LOG_MBOX | LOG_SLI | LOG_NODE,
2985 					 "0010 UNREG_LOGIN vpi:x%x "
2986 					 "rpi:%x DID:%x defer x%x flg x%lx "
2987 					 "x%px\n",
2988 					 vport->vpi, ndlp->nlp_rpi,
2989 					 ndlp->nlp_DID, ndlp->nlp_defer_did,
2990 					 ndlp->nlp_flag,
2991 					 ndlp);
2992 
2993 				/* Cleanup the nlp_flag now that the UNREG RPI
2994 				 * has completed.
2995 				 */
2996 				unreg_inp = test_and_clear_bit(NLP_UNREG_INP,
2997 							       &ndlp->nlp_flag);
2998 				clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
2999 
3000 				/* Check to see if there are any deferred
3001 				 * events to process
3002 				 */
3003 				if (unreg_inp &&
3004 				    ndlp->nlp_defer_did !=
3005 				    NLP_EVT_NOTHING_PENDING) {
3006 					lpfc_printf_vlog(
3007 						vport, KERN_INFO,
3008 						LOG_MBOX | LOG_SLI | LOG_NODE,
3009 						"4111 UNREG cmpl deferred "
3010 						"clr x%x on "
3011 						"NPort x%x Data: x%x x%px\n",
3012 						ndlp->nlp_rpi, ndlp->nlp_DID,
3013 						ndlp->nlp_defer_did, ndlp);
3014 					ndlp->nlp_defer_did =
3015 						NLP_EVT_NOTHING_PENDING;
3016 					lpfc_issue_els_plogi(
3017 						vport, ndlp->nlp_DID, 0);
3018 				}
3019 
3020 				lpfc_nlp_put(ndlp);
3021 			}
3022 		}
3023 	}
3024 
3025 	mempool_free(pmb, phba->mbox_mem_pool);
3026 }
3027 
3028 /**
3029  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
3030  * @phba: Pointer to HBA context object.
3031  *
3032  * This function is called with no lock held. This function processes all
3033  * the completed mailbox commands and gives it to upper layers. The interrupt
3034  * service routine processes mailbox completion interrupt and adds completed
3035  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
3036  * Worker thread call lpfc_sli_handle_mb_event, which will return the
3037  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
3038  * function returns the mailbox commands to the upper layer by calling the
3039  * completion handler function of each mailbox.
3040  **/
3041 int
lpfc_sli_handle_mb_event(struct lpfc_hba * phba)3042 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
3043 {
3044 	MAILBOX_t *pmbox;
3045 	LPFC_MBOXQ_t *pmb;
3046 	int rc;
3047 	LIST_HEAD(cmplq);
3048 
3049 	phba->sli.slistat.mbox_event++;
3050 
3051 	/* Get all completed mailboxe buffers into the cmplq */
3052 	spin_lock_irq(&phba->hbalock);
3053 	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
3054 	spin_unlock_irq(&phba->hbalock);
3055 
3056 	/* Get a Mailbox buffer to setup mailbox commands for callback */
3057 	do {
3058 		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
3059 		if (pmb == NULL)
3060 			break;
3061 
3062 		pmbox = &pmb->u.mb;
3063 
3064 		if (pmbox->mbxCommand != MBX_HEARTBEAT) {
3065 			if (pmb->vport) {
3066 				lpfc_debugfs_disc_trc(pmb->vport,
3067 					LPFC_DISC_TRC_MBOX_VPORT,
3068 					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",
3069 					(uint32_t)pmbox->mbxCommand,
3070 					pmbox->un.varWords[0],
3071 					pmbox->un.varWords[1]);
3072 			}
3073 			else {
3074 				lpfc_debugfs_disc_trc(phba->pport,
3075 					LPFC_DISC_TRC_MBOX,
3076 					"MBOX cmpl:       cmd:x%x mb:x%x x%x",
3077 					(uint32_t)pmbox->mbxCommand,
3078 					pmbox->un.varWords[0],
3079 					pmbox->un.varWords[1]);
3080 			}
3081 		}
3082 
3083 		/*
3084 		 * It is a fatal error if unknown mbox command completion.
3085 		 */
3086 		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
3087 		    MBX_SHUTDOWN) {
3088 			/* Unknown mailbox command compl */
3089 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3090 					"(%d):0323 Unknown Mailbox command "
3091 					"x%x (x%x/x%x) Cmpl\n",
3092 					pmb->vport ? pmb->vport->vpi :
3093 					LPFC_VPORT_UNKNOWN,
3094 					pmbox->mbxCommand,
3095 					lpfc_sli_config_mbox_subsys_get(phba,
3096 									pmb),
3097 					lpfc_sli_config_mbox_opcode_get(phba,
3098 									pmb));
3099 			phba->link_state = LPFC_HBA_ERROR;
3100 			phba->work_hs = HS_FFER3;
3101 			lpfc_handle_eratt(phba);
3102 			continue;
3103 		}
3104 
3105 		if (pmbox->mbxStatus) {
3106 			phba->sli.slistat.mbox_stat_err++;
3107 			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
3108 				/* Mbox cmd cmpl error - RETRYing */
3109 				lpfc_printf_log(phba, KERN_INFO,
3110 					LOG_MBOX | LOG_SLI,
3111 					"(%d):0305 Mbox cmd cmpl "
3112 					"error - RETRYing Data: x%x "
3113 					"(x%x/x%x) x%x x%x x%x\n",
3114 					pmb->vport ? pmb->vport->vpi :
3115 					LPFC_VPORT_UNKNOWN,
3116 					pmbox->mbxCommand,
3117 					lpfc_sli_config_mbox_subsys_get(phba,
3118 									pmb),
3119 					lpfc_sli_config_mbox_opcode_get(phba,
3120 									pmb),
3121 					pmbox->mbxStatus,
3122 					pmbox->un.varWords[0],
3123 					pmb->vport ? pmb->vport->port_state :
3124 					LPFC_VPORT_UNKNOWN);
3125 				pmbox->mbxStatus = 0;
3126 				pmbox->mbxOwner = OWN_HOST;
3127 				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3128 				if (rc != MBX_NOT_FINISHED)
3129 					continue;
3130 			}
3131 		}
3132 
3133 		/* Mailbox cmd <cmd> Cmpl <cmpl> */
3134 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3135 				"(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
3136 				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3137 				"x%x x%x x%x\n",
3138 				pmb->vport ? pmb->vport->vpi : 0,
3139 				pmbox->mbxCommand,
3140 				lpfc_sli_config_mbox_subsys_get(phba, pmb),
3141 				lpfc_sli_config_mbox_opcode_get(phba, pmb),
3142 				pmb->mbox_cmpl,
3143 				*((uint32_t *) pmbox),
3144 				pmbox->un.varWords[0],
3145 				pmbox->un.varWords[1],
3146 				pmbox->un.varWords[2],
3147 				pmbox->un.varWords[3],
3148 				pmbox->un.varWords[4],
3149 				pmbox->un.varWords[5],
3150 				pmbox->un.varWords[6],
3151 				pmbox->un.varWords[7],
3152 				pmbox->un.varWords[8],
3153 				pmbox->un.varWords[9],
3154 				pmbox->un.varWords[10]);
3155 
3156 		if (pmb->mbox_cmpl)
3157 			pmb->mbox_cmpl(phba,pmb);
3158 	} while (1);
3159 	return 0;
3160 }
3161 
3162 /**
3163  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
3164  * @phba: Pointer to HBA context object.
3165  * @pring: Pointer to driver SLI ring object.
3166  * @tag: buffer tag.
3167  *
3168  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
3169  * is set in the tag the buffer is posted for a particular exchange,
3170  * the function will return the buffer without replacing the buffer.
3171  * If the buffer is for unsolicited ELS or CT traffic, this function
3172  * returns the buffer and also posts another buffer to the firmware.
3173  **/
3174 static struct lpfc_dmabuf *
lpfc_sli_get_buff(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)3175 lpfc_sli_get_buff(struct lpfc_hba *phba,
3176 		  struct lpfc_sli_ring *pring,
3177 		  uint32_t tag)
3178 {
3179 	struct hbq_dmabuf *hbq_entry;
3180 
3181 	if (tag & QUE_BUFTAG_BIT)
3182 		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
3183 	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
3184 	if (!hbq_entry)
3185 		return NULL;
3186 	return &hbq_entry->dbuf;
3187 }
3188 
3189 /**
3190  * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
3191  *                              containing a NVME LS request.
3192  * @phba: pointer to lpfc hba data structure.
3193  * @piocb: pointer to the iocbq struct representing the sequence starting
3194  *        frame.
3195  *
3196  * This routine initially validates the NVME LS, validates there is a login
3197  * with the port that sent the LS, and then calls the appropriate nvme host
3198  * or target LS request handler.
3199  **/
3200 static void
lpfc_nvme_unsol_ls_handler(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)3201 lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
3202 {
3203 	struct lpfc_nodelist *ndlp;
3204 	struct lpfc_dmabuf *d_buf;
3205 	struct hbq_dmabuf *nvmebuf;
3206 	struct fc_frame_header *fc_hdr;
3207 	struct lpfc_async_xchg_ctx *axchg = NULL;
3208 	char *failwhy = NULL;
3209 	uint32_t oxid, sid, did, fctl, size;
3210 	int ret = 1;
3211 
3212 	d_buf = piocb->cmd_dmabuf;
3213 
3214 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
3215 	fc_hdr = nvmebuf->hbuf.virt;
3216 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
3217 	sid = sli4_sid_from_fc_hdr(fc_hdr);
3218 	did = sli4_did_from_fc_hdr(fc_hdr);
3219 	fctl = (fc_hdr->fh_f_ctl[0] << 16 |
3220 		fc_hdr->fh_f_ctl[1] << 8 |
3221 		fc_hdr->fh_f_ctl[2]);
3222 	size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
3223 
3224 	lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
3225 			 oxid, size, sid);
3226 
3227 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
3228 		failwhy = "Driver Unloading";
3229 	} else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
3230 		failwhy = "NVME FC4 Disabled";
3231 	} else if (!phba->nvmet_support && !phba->pport->localport) {
3232 		failwhy = "No Localport";
3233 	} else if (phba->nvmet_support && !phba->targetport) {
3234 		failwhy = "No Targetport";
3235 	} else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
3236 		failwhy = "Bad NVME LS R_CTL";
3237 	} else if (unlikely((fctl & 0x00FF0000) !=
3238 			(FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
3239 		failwhy = "Bad NVME LS F_CTL";
3240 	} else {
3241 		axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
3242 		if (!axchg)
3243 			failwhy = "No CTX memory";
3244 	}
3245 
3246 	if (unlikely(failwhy)) {
3247 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3248 				"6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
3249 				sid, oxid, failwhy);
3250 		goto out_fail;
3251 	}
3252 
3253 	/* validate the source of the LS is logged in */
3254 	ndlp = lpfc_findnode_did(phba->pport, sid);
3255 	if (!ndlp ||
3256 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3257 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3258 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
3259 				"6216 NVME Unsol rcv: No ndlp: "
3260 				"NPort_ID x%x oxid x%x\n",
3261 				sid, oxid);
3262 		goto out_fail;
3263 	}
3264 
3265 	axchg->phba = phba;
3266 	axchg->ndlp = ndlp;
3267 	axchg->size = size;
3268 	axchg->oxid = oxid;
3269 	axchg->sid = sid;
3270 	axchg->wqeq = NULL;
3271 	axchg->state = LPFC_NVME_STE_LS_RCV;
3272 	axchg->entry_cnt = 1;
3273 	axchg->rqb_buffer = (void *)nvmebuf;
3274 	axchg->hdwq = &phba->sli4_hba.hdwq[0];
3275 	axchg->payload = nvmebuf->dbuf.virt;
3276 	INIT_LIST_HEAD(&axchg->list);
3277 
3278 	if (phba->nvmet_support) {
3279 		ret = lpfc_nvmet_handle_lsreq(phba, axchg);
3280 		spin_lock_irq(&ndlp->lock);
3281 		if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3282 			ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3283 			spin_unlock_irq(&ndlp->lock);
3284 
3285 			/* This reference is a single occurrence to hold the
3286 			 * node valid until the nvmet transport calls
3287 			 * host_release.
3288 			 */
3289 			if (!lpfc_nlp_get(ndlp))
3290 				goto out_fail;
3291 
3292 			lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
3293 					"6206 NVMET unsol ls_req ndlp x%px "
3294 					"DID x%x xflags x%x refcnt %d\n",
3295 					ndlp, ndlp->nlp_DID,
3296 					ndlp->fc4_xpt_flags,
3297 					kref_read(&ndlp->kref));
3298 		} else {
3299 			spin_unlock_irq(&ndlp->lock);
3300 		}
3301 	} else {
3302 		ret = lpfc_nvme_handle_lsreq(phba, axchg);
3303 	}
3304 
3305 	/* if zero, LS was successfully handled. If non-zero, LS not handled */
3306 	if (!ret)
3307 		return;
3308 
3309 out_fail:
3310 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3311 			"6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3312 			"NVMe%s handler failed %d\n",
3313 			did, sid, oxid,
3314 			(phba->nvmet_support) ? "T" : "I", ret);
3315 
3316 	/* recycle receive buffer */
3317 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3318 
3319 	/* If start of new exchange, abort it */
3320 	if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3321 		ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3322 
3323 	if (ret)
3324 		kfree(axchg);
3325 }
3326 
3327 /**
3328  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3329  * @phba: Pointer to HBA context object.
3330  * @pring: Pointer to driver SLI ring object.
3331  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3332  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3333  * @fch_type: the type for the first frame of the sequence.
3334  *
3335  * This function is called with no lock held. This function uses the r_ctl and
3336  * type of the received sequence to find the correct callback function to call
3337  * to process the sequence.
3338  **/
3339 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)3340 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3341 			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3342 			 uint32_t fch_type)
3343 {
3344 	int i;
3345 
3346 	switch (fch_type) {
3347 	case FC_TYPE_NVME:
3348 		lpfc_nvme_unsol_ls_handler(phba, saveq);
3349 		return 1;
3350 	default:
3351 		break;
3352 	}
3353 
3354 	/* unSolicited Responses */
3355 	if (pring->prt[0].profile) {
3356 		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3357 			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3358 									saveq);
3359 		return 1;
3360 	}
3361 	/* We must search, based on rctl / type
3362 	   for the right routine */
3363 	for (i = 0; i < pring->num_mask; i++) {
3364 		if ((pring->prt[i].rctl == fch_r_ctl) &&
3365 		    (pring->prt[i].type == fch_type)) {
3366 			if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3367 				(pring->prt[i].lpfc_sli_rcv_unsol_event)
3368 						(phba, pring, saveq);
3369 			return 1;
3370 		}
3371 	}
3372 	return 0;
3373 }
3374 
3375 static void
lpfc_sli_prep_unsol_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * saveq)3376 lpfc_sli_prep_unsol_wqe(struct lpfc_hba *phba,
3377 			struct lpfc_iocbq *saveq)
3378 {
3379 	IOCB_t *irsp;
3380 	union lpfc_wqe128 *wqe;
3381 	u16 i = 0;
3382 
3383 	irsp = &saveq->iocb;
3384 	wqe = &saveq->wqe;
3385 
3386 	/* Fill wcqe with the IOCB status fields */
3387 	bf_set(lpfc_wcqe_c_status, &saveq->wcqe_cmpl, irsp->ulpStatus);
3388 	saveq->wcqe_cmpl.word3 = irsp->ulpBdeCount;
3389 	saveq->wcqe_cmpl.parameter = irsp->un.ulpWord[4];
3390 	saveq->wcqe_cmpl.total_data_placed = irsp->unsli3.rcvsli3.acc_len;
3391 
3392 	/* Source ID */
3393 	bf_set(els_rsp64_sid, &wqe->xmit_els_rsp, irsp->un.rcvels.parmRo);
3394 
3395 	/* rx-id of the response frame */
3396 	bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com, irsp->ulpContext);
3397 
3398 	/* ox-id of the frame */
3399 	bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
3400 	       irsp->unsli3.rcvsli3.ox_id);
3401 
3402 	/* DID */
3403 	bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
3404 	       irsp->un.rcvels.remoteID);
3405 
3406 	/* unsol data len */
3407 	for (i = 0; i < irsp->ulpBdeCount; i++) {
3408 		struct lpfc_hbq_entry *hbqe = NULL;
3409 
3410 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3411 			if (i == 0) {
3412 				hbqe = (struct lpfc_hbq_entry *)
3413 					&irsp->un.ulpWord[0];
3414 				saveq->wqe.gen_req.bde.tus.f.bdeSize =
3415 					hbqe->bde.tus.f.bdeSize;
3416 			} else if (i == 1) {
3417 				hbqe = (struct lpfc_hbq_entry *)
3418 					&irsp->unsli3.sli3Words[4];
3419 				saveq->unsol_rcv_len = hbqe->bde.tus.f.bdeSize;
3420 			}
3421 		}
3422 	}
3423 }
3424 
3425 /**
3426  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
3427  * @phba: Pointer to HBA context object.
3428  * @pring: Pointer to driver SLI ring object.
3429  * @saveq: Pointer to the unsolicited iocb.
3430  *
3431  * This function is called with no lock held by the ring event handler
3432  * when there is an unsolicited iocb posted to the response ring by the
3433  * firmware. This function gets the buffer associated with the iocbs
3434  * and calls the event handler for the ring. This function handles both
3435  * qring buffers and hbq buffers.
3436  * When the function returns 1 the caller can free the iocb object otherwise
3437  * upper layer functions will free the iocb objects.
3438  **/
3439 static int
lpfc_sli_process_unsol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3440 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3441 			    struct lpfc_iocbq *saveq)
3442 {
3443 	IOCB_t           * irsp;
3444 	WORD5            * w5p;
3445 	dma_addr_t	 paddr;
3446 	uint32_t           Rctl, Type;
3447 	struct lpfc_iocbq *iocbq;
3448 	struct lpfc_dmabuf *dmzbuf;
3449 
3450 	irsp = &saveq->iocb;
3451 	saveq->vport = phba->pport;
3452 
3453 	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3454 		if (pring->lpfc_sli_rcv_async_status)
3455 			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3456 		else
3457 			lpfc_printf_log(phba,
3458 					KERN_WARNING,
3459 					LOG_SLI,
3460 					"0316 Ring %d handler: unexpected "
3461 					"ASYNC_STATUS iocb received evt_code "
3462 					"0x%x\n",
3463 					pring->ringno,
3464 					irsp->un.asyncstat.evt_code);
3465 		return 1;
3466 	}
3467 
3468 	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3469 	    (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3470 		if (irsp->ulpBdeCount > 0) {
3471 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3472 						   irsp->un.ulpWord[3]);
3473 			lpfc_in_buf_free(phba, dmzbuf);
3474 		}
3475 
3476 		if (irsp->ulpBdeCount > 1) {
3477 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3478 						   irsp->unsli3.sli3Words[3]);
3479 			lpfc_in_buf_free(phba, dmzbuf);
3480 		}
3481 
3482 		if (irsp->ulpBdeCount > 2) {
3483 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3484 						   irsp->unsli3.sli3Words[7]);
3485 			lpfc_in_buf_free(phba, dmzbuf);
3486 		}
3487 
3488 		return 1;
3489 	}
3490 
3491 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3492 		if (irsp->ulpBdeCount != 0) {
3493 			saveq->cmd_dmabuf = lpfc_sli_get_buff(phba, pring,
3494 						irsp->un.ulpWord[3]);
3495 			if (!saveq->cmd_dmabuf)
3496 				lpfc_printf_log(phba,
3497 					KERN_ERR,
3498 					LOG_SLI,
3499 					"0341 Ring %d Cannot find buffer for "
3500 					"an unsolicited iocb. tag 0x%x\n",
3501 					pring->ringno,
3502 					irsp->un.ulpWord[3]);
3503 		}
3504 		if (irsp->ulpBdeCount == 2) {
3505 			saveq->bpl_dmabuf = lpfc_sli_get_buff(phba, pring,
3506 						irsp->unsli3.sli3Words[7]);
3507 			if (!saveq->bpl_dmabuf)
3508 				lpfc_printf_log(phba,
3509 					KERN_ERR,
3510 					LOG_SLI,
3511 					"0342 Ring %d Cannot find buffer for an"
3512 					" unsolicited iocb. tag 0x%x\n",
3513 					pring->ringno,
3514 					irsp->unsli3.sli3Words[7]);
3515 		}
3516 		list_for_each_entry(iocbq, &saveq->list, list) {
3517 			irsp = &iocbq->iocb;
3518 			if (irsp->ulpBdeCount != 0) {
3519 				iocbq->cmd_dmabuf = lpfc_sli_get_buff(phba,
3520 							pring,
3521 							irsp->un.ulpWord[3]);
3522 				if (!iocbq->cmd_dmabuf)
3523 					lpfc_printf_log(phba,
3524 						KERN_ERR,
3525 						LOG_SLI,
3526 						"0343 Ring %d Cannot find "
3527 						"buffer for an unsolicited iocb"
3528 						". tag 0x%x\n", pring->ringno,
3529 						irsp->un.ulpWord[3]);
3530 			}
3531 			if (irsp->ulpBdeCount == 2) {
3532 				iocbq->bpl_dmabuf = lpfc_sli_get_buff(phba,
3533 						pring,
3534 						irsp->unsli3.sli3Words[7]);
3535 				if (!iocbq->bpl_dmabuf)
3536 					lpfc_printf_log(phba,
3537 						KERN_ERR,
3538 						LOG_SLI,
3539 						"0344 Ring %d Cannot find "
3540 						"buffer for an unsolicited "
3541 						"iocb. tag 0x%x\n",
3542 						pring->ringno,
3543 						irsp->unsli3.sli3Words[7]);
3544 			}
3545 		}
3546 	} else {
3547 		paddr = getPaddr(irsp->un.cont64[0].addrHigh,
3548 				 irsp->un.cont64[0].addrLow);
3549 		saveq->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
3550 							     paddr);
3551 		if (irsp->ulpBdeCount == 2) {
3552 			paddr = getPaddr(irsp->un.cont64[1].addrHigh,
3553 					 irsp->un.cont64[1].addrLow);
3554 			saveq->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
3555 								   pring,
3556 								   paddr);
3557 		}
3558 	}
3559 
3560 	if (irsp->ulpBdeCount != 0 &&
3561 	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3562 	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3563 		int found = 0;
3564 
3565 		/* search continue save q for same XRI */
3566 		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
3567 			if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3568 				saveq->iocb.unsli3.rcvsli3.ox_id) {
3569 				list_add_tail(&saveq->list, &iocbq->list);
3570 				found = 1;
3571 				break;
3572 			}
3573 		}
3574 		if (!found)
3575 			list_add_tail(&saveq->clist,
3576 				      &pring->iocb_continue_saveq);
3577 
3578 		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3579 			list_del_init(&iocbq->clist);
3580 			saveq = iocbq;
3581 			irsp = &saveq->iocb;
3582 		} else {
3583 			return 0;
3584 		}
3585 	}
3586 	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3587 	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3588 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
3589 		Rctl = FC_RCTL_ELS_REQ;
3590 		Type = FC_TYPE_ELS;
3591 	} else {
3592 		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3593 		Rctl = w5p->hcsw.Rctl;
3594 		Type = w5p->hcsw.Type;
3595 
3596 		/* Firmware Workaround */
3597 		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3598 			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3599 			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3600 			Rctl = FC_RCTL_ELS_REQ;
3601 			Type = FC_TYPE_ELS;
3602 			w5p->hcsw.Rctl = Rctl;
3603 			w5p->hcsw.Type = Type;
3604 		}
3605 	}
3606 
3607 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3608 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
3609 	    irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3610 		if (irsp->unsli3.rcvsli3.vpi == 0xffff)
3611 			saveq->vport = phba->pport;
3612 		else
3613 			saveq->vport = lpfc_find_vport_by_vpid(phba,
3614 					       irsp->unsli3.rcvsli3.vpi);
3615 	}
3616 
3617 	/* Prepare WQE with Unsol frame */
3618 	lpfc_sli_prep_unsol_wqe(phba, saveq);
3619 
3620 	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
3621 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3622 				"0313 Ring %d handler: unexpected Rctl x%x "
3623 				"Type x%x received\n",
3624 				pring->ringno, Rctl, Type);
3625 
3626 	return 1;
3627 }
3628 
3629 /**
3630  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
3631  * @phba: Pointer to HBA context object.
3632  * @pring: Pointer to driver SLI ring object.
3633  * @prspiocb: Pointer to response iocb object.
3634  *
3635  * This function looks up the iocb_lookup table to get the command iocb
3636  * corresponding to the given response iocb using the iotag of the
3637  * response iocb. The driver calls this function with the hbalock held
3638  * for SLI3 ports or the ring lock held for SLI4 ports.
3639  * This function returns the command iocb object if it finds the command
3640  * iocb else returns NULL.
3641  **/
3642 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * prspiocb)3643 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3644 		      struct lpfc_sli_ring *pring,
3645 		      struct lpfc_iocbq *prspiocb)
3646 {
3647 	struct lpfc_iocbq *cmd_iocb = NULL;
3648 	u16 iotag;
3649 
3650 	if (phba->sli_rev == LPFC_SLI_REV4)
3651 		iotag = get_wqe_reqtag(prspiocb);
3652 	else
3653 		iotag = prspiocb->iocb.ulpIoTag;
3654 
3655 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3656 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3657 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3658 			/* remove from txcmpl queue list */
3659 			list_del_init(&cmd_iocb->list);
3660 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3661 			pring->txcmplq_cnt--;
3662 			return cmd_iocb;
3663 		}
3664 	}
3665 
3666 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3667 			"0317 iotag x%x is out of "
3668 			"range: max iotag x%x\n",
3669 			iotag, phba->sli.last_iotag);
3670 	return NULL;
3671 }
3672 
3673 /**
3674  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3675  * @phba: Pointer to HBA context object.
3676  * @pring: Pointer to driver SLI ring object.
3677  * @iotag: IOCB tag.
3678  *
3679  * This function looks up the iocb_lookup table to get the command iocb
3680  * corresponding to the given iotag. The driver calls this function with
3681  * the ring lock held because this function is an SLI4 port only helper.
3682  * This function returns the command iocb object if it finds the command
3683  * iocb else returns NULL.
3684  **/
3685 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint16_t iotag)3686 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3687 			     struct lpfc_sli_ring *pring, uint16_t iotag)
3688 {
3689 	struct lpfc_iocbq *cmd_iocb = NULL;
3690 
3691 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3692 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3693 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3694 			/* remove from txcmpl queue list */
3695 			list_del_init(&cmd_iocb->list);
3696 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3697 			pring->txcmplq_cnt--;
3698 			return cmd_iocb;
3699 		}
3700 	}
3701 
3702 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3703 			"0372 iotag x%x lookup error: max iotag (x%x) "
3704 			"cmd_flag x%x\n",
3705 			iotag, phba->sli.last_iotag,
3706 			cmd_iocb ? cmd_iocb->cmd_flag : 0xffff);
3707 	return NULL;
3708 }
3709 
3710 /**
3711  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3712  * @phba: Pointer to HBA context object.
3713  * @pring: Pointer to driver SLI ring object.
3714  * @saveq: Pointer to the response iocb to be processed.
3715  *
3716  * This function is called by the ring event handler for non-fcp
3717  * rings when there is a new response iocb in the response ring.
3718  * The caller is not required to hold any locks. This function
3719  * gets the command iocb associated with the response iocb and
3720  * calls the completion handler for the command iocb. If there
3721  * is no completion handler, the function will free the resources
3722  * associated with command iocb. If the response iocb is for
3723  * an already aborted command iocb, the status of the completion
3724  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3725  * This function always returns 1.
3726  **/
3727 static int
lpfc_sli_process_sol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3728 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3729 			  struct lpfc_iocbq *saveq)
3730 {
3731 	struct lpfc_iocbq *cmdiocbp;
3732 	unsigned long iflag;
3733 	u32 ulp_command, ulp_status, ulp_word4, ulp_context, iotag;
3734 
3735 	if (phba->sli_rev == LPFC_SLI_REV4)
3736 		spin_lock_irqsave(&pring->ring_lock, iflag);
3737 	else
3738 		spin_lock_irqsave(&phba->hbalock, iflag);
3739 	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3740 	if (phba->sli_rev == LPFC_SLI_REV4)
3741 		spin_unlock_irqrestore(&pring->ring_lock, iflag);
3742 	else
3743 		spin_unlock_irqrestore(&phba->hbalock, iflag);
3744 
3745 	ulp_command = get_job_cmnd(phba, saveq);
3746 	ulp_status = get_job_ulpstatus(phba, saveq);
3747 	ulp_word4 = get_job_word4(phba, saveq);
3748 	ulp_context = get_job_ulpcontext(phba, saveq);
3749 	if (phba->sli_rev == LPFC_SLI_REV4)
3750 		iotag = get_wqe_reqtag(saveq);
3751 	else
3752 		iotag = saveq->iocb.ulpIoTag;
3753 
3754 	if (cmdiocbp) {
3755 		ulp_command = get_job_cmnd(phba, cmdiocbp);
3756 		if (cmdiocbp->cmd_cmpl) {
3757 			/*
3758 			 * If an ELS command failed send an event to mgmt
3759 			 * application.
3760 			 */
3761 			if (ulp_status &&
3762 			     (pring->ringno == LPFC_ELS_RING) &&
3763 			     (ulp_command == CMD_ELS_REQUEST64_CR))
3764 				lpfc_send_els_failure_event(phba,
3765 					cmdiocbp, saveq);
3766 
3767 			/*
3768 			 * Post all ELS completions to the worker thread.
3769 			 * All other are passed to the completion callback.
3770 			 */
3771 			if (pring->ringno == LPFC_ELS_RING) {
3772 				if ((phba->sli_rev < LPFC_SLI_REV4) &&
3773 				    (cmdiocbp->cmd_flag &
3774 							LPFC_DRIVER_ABORTED)) {
3775 					spin_lock_irqsave(&phba->hbalock,
3776 							  iflag);
3777 					cmdiocbp->cmd_flag &=
3778 						~LPFC_DRIVER_ABORTED;
3779 					spin_unlock_irqrestore(&phba->hbalock,
3780 							       iflag);
3781 					saveq->iocb.ulpStatus =
3782 						IOSTAT_LOCAL_REJECT;
3783 					saveq->iocb.un.ulpWord[4] =
3784 						IOERR_SLI_ABORTED;
3785 
3786 					/* Firmware could still be in progress
3787 					 * of DMAing payload, so don't free data
3788 					 * buffer till after a hbeat.
3789 					 */
3790 					spin_lock_irqsave(&phba->hbalock,
3791 							  iflag);
3792 					saveq->cmd_flag |= LPFC_DELAY_MEM_FREE;
3793 					spin_unlock_irqrestore(&phba->hbalock,
3794 							       iflag);
3795 				}
3796 				if (phba->sli_rev == LPFC_SLI_REV4) {
3797 					if (saveq->cmd_flag &
3798 					    LPFC_EXCHANGE_BUSY) {
3799 						/* Set cmdiocb flag for the
3800 						 * exchange busy so sgl (xri)
3801 						 * will not be released until
3802 						 * the abort xri is received
3803 						 * from hba.
3804 						 */
3805 						spin_lock_irqsave(
3806 							&phba->hbalock, iflag);
3807 						cmdiocbp->cmd_flag |=
3808 							LPFC_EXCHANGE_BUSY;
3809 						spin_unlock_irqrestore(
3810 							&phba->hbalock, iflag);
3811 					}
3812 					if (cmdiocbp->cmd_flag &
3813 					    LPFC_DRIVER_ABORTED) {
3814 						/*
3815 						 * Clear LPFC_DRIVER_ABORTED
3816 						 * bit in case it was driver
3817 						 * initiated abort.
3818 						 */
3819 						spin_lock_irqsave(
3820 							&phba->hbalock, iflag);
3821 						cmdiocbp->cmd_flag &=
3822 							~LPFC_DRIVER_ABORTED;
3823 						spin_unlock_irqrestore(
3824 							&phba->hbalock, iflag);
3825 						set_job_ulpstatus(cmdiocbp,
3826 								  IOSTAT_LOCAL_REJECT);
3827 						set_job_ulpword4(cmdiocbp,
3828 								 IOERR_ABORT_REQUESTED);
3829 						/*
3830 						 * For SLI4, irspiocb contains
3831 						 * NO_XRI in sli_xritag, it
3832 						 * shall not affect releasing
3833 						 * sgl (xri) process.
3834 						 */
3835 						set_job_ulpstatus(saveq,
3836 								  IOSTAT_LOCAL_REJECT);
3837 						set_job_ulpword4(saveq,
3838 								 IOERR_SLI_ABORTED);
3839 						spin_lock_irqsave(
3840 							&phba->hbalock, iflag);
3841 						saveq->cmd_flag |=
3842 							LPFC_DELAY_MEM_FREE;
3843 						spin_unlock_irqrestore(
3844 							&phba->hbalock, iflag);
3845 					}
3846 				}
3847 			}
3848 			cmdiocbp->cmd_cmpl(phba, cmdiocbp, saveq);
3849 		} else
3850 			lpfc_sli_release_iocbq(phba, cmdiocbp);
3851 	} else {
3852 		/*
3853 		 * Unknown initiating command based on the response iotag.
3854 		 * This could be the case on the ELS ring because of
3855 		 * lpfc_els_abort().
3856 		 */
3857 		if (pring->ringno != LPFC_ELS_RING) {
3858 			/*
3859 			 * Ring <ringno> handler: unexpected completion IoTag
3860 			 * <IoTag>
3861 			 */
3862 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3863 					 "0322 Ring %d handler: "
3864 					 "unexpected completion IoTag x%x "
3865 					 "Data: x%x x%x x%x x%x\n",
3866 					 pring->ringno, iotag, ulp_status,
3867 					 ulp_word4, ulp_command, ulp_context);
3868 		}
3869 	}
3870 
3871 	return 1;
3872 }
3873 
3874 /**
3875  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3876  * @phba: Pointer to HBA context object.
3877  * @pring: Pointer to driver SLI ring object.
3878  *
3879  * This function is called from the iocb ring event handlers when
3880  * put pointer is ahead of the get pointer for a ring. This function signal
3881  * an error attention condition to the worker thread and the worker
3882  * thread will transition the HBA to offline state.
3883  **/
3884 static void
lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)3885 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3886 {
3887 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3888 	/*
3889 	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3890 	 * rsp ring <portRspMax>
3891 	 */
3892 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3893 			"0312 Ring %d handler: portRspPut %d "
3894 			"is bigger than rsp ring %d\n",
3895 			pring->ringno, le32_to_cpu(pgp->rspPutInx),
3896 			pring->sli.sli3.numRiocb);
3897 
3898 	phba->link_state = LPFC_HBA_ERROR;
3899 
3900 	/*
3901 	 * All error attention handlers are posted to
3902 	 * worker thread
3903 	 */
3904 	phba->work_ha |= HA_ERATT;
3905 	phba->work_hs = HS_FFER3;
3906 
3907 	lpfc_worker_wake_up(phba);
3908 
3909 	return;
3910 }
3911 
3912 /**
3913  * lpfc_poll_eratt - Error attention polling timer timeout handler
3914  * @t: Context to fetch pointer to address of HBA context object from.
3915  *
3916  * This function is invoked by the Error Attention polling timer when the
3917  * timer times out. It will check the SLI Error Attention register for
3918  * possible attention events. If so, it will post an Error Attention event
3919  * and wake up worker thread to process it. Otherwise, it will set up the
3920  * Error Attention polling timer for the next poll.
3921  **/
lpfc_poll_eratt(struct timer_list * t)3922 void lpfc_poll_eratt(struct timer_list *t)
3923 {
3924 	struct lpfc_hba *phba;
3925 	uint32_t eratt = 0;
3926 	uint64_t sli_intr, cnt;
3927 
3928 	phba = timer_container_of(phba, t, eratt_poll);
3929 
3930 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
3931 		return;
3932 
3933 	if (phba->sli_rev == LPFC_SLI_REV4 &&
3934 	    !test_bit(HBA_SETUP, &phba->hba_flag)) {
3935 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3936 				"0663 HBA still initializing 0x%lx, restart "
3937 				"timer\n",
3938 				phba->hba_flag);
3939 		goto restart_timer;
3940 	}
3941 
3942 	/* Here we will also keep track of interrupts per sec of the hba */
3943 	sli_intr = phba->sli.slistat.sli_intr;
3944 
3945 	if (phba->sli.slistat.sli_prev_intr > sli_intr)
3946 		cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3947 			sli_intr);
3948 	else
3949 		cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3950 
3951 	/* 64-bit integer division not supported on 32-bit x86 - use do_div */
3952 	do_div(cnt, phba->eratt_poll_interval);
3953 	phba->sli.slistat.sli_ips = cnt;
3954 
3955 	phba->sli.slistat.sli_prev_intr = sli_intr;
3956 
3957 	/* Check chip HA register for error event */
3958 	eratt = lpfc_sli_check_eratt(phba);
3959 
3960 	if (eratt) {
3961 		/* Tell the worker thread there is work to do */
3962 		lpfc_worker_wake_up(phba);
3963 		return;
3964 	}
3965 
3966 restart_timer:
3967 	/* Restart the timer for next eratt poll */
3968 	mod_timer(&phba->eratt_poll,
3969 		  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
3970 	return;
3971 }
3972 
3973 
3974 /**
3975  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3976  * @phba: Pointer to HBA context object.
3977  * @pring: Pointer to driver SLI ring object.
3978  * @mask: Host attention register mask for this ring.
3979  *
3980  * This function is called from the interrupt context when there is a ring
3981  * event for the fcp ring. The caller does not hold any lock.
3982  * The function processes each response iocb in the response ring until it
3983  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3984  * LE bit set. The function will call the completion handler of the command iocb
3985  * if the response iocb indicates a completion for a command iocb or it is
3986  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3987  * function if this is an unsolicited iocb.
3988  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3989  * to check it explicitly.
3990  */
3991 int
lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)3992 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3993 				struct lpfc_sli_ring *pring, uint32_t mask)
3994 {
3995 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3996 	IOCB_t *irsp = NULL;
3997 	IOCB_t *entry = NULL;
3998 	struct lpfc_iocbq *cmdiocbq = NULL;
3999 	struct lpfc_iocbq rspiocbq;
4000 	uint32_t status;
4001 	uint32_t portRspPut, portRspMax;
4002 	int rc = 1;
4003 	lpfc_iocb_type type;
4004 	unsigned long iflag;
4005 	uint32_t rsp_cmpl = 0;
4006 
4007 	spin_lock_irqsave(&phba->hbalock, iflag);
4008 	pring->stats.iocb_event++;
4009 
4010 	/*
4011 	 * The next available response entry should never exceed the maximum
4012 	 * entries.  If it does, treat it as an adapter hardware error.
4013 	 */
4014 	portRspMax = pring->sli.sli3.numRiocb;
4015 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4016 	if (unlikely(portRspPut >= portRspMax)) {
4017 		lpfc_sli_rsp_pointers_error(phba, pring);
4018 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4019 		return 1;
4020 	}
4021 	if (phba->fcp_ring_in_use) {
4022 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4023 		return 1;
4024 	} else
4025 		phba->fcp_ring_in_use = 1;
4026 
4027 	rmb();
4028 	while (pring->sli.sli3.rspidx != portRspPut) {
4029 		/*
4030 		 * Fetch an entry off the ring and copy it into a local data
4031 		 * structure.  The copy involves a byte-swap since the
4032 		 * network byte order and pci byte orders are different.
4033 		 */
4034 		entry = lpfc_resp_iocb(phba, pring);
4035 		phba->last_completion_time = jiffies;
4036 
4037 		if (++pring->sli.sli3.rspidx >= portRspMax)
4038 			pring->sli.sli3.rspidx = 0;
4039 
4040 		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
4041 				      (uint32_t *) &rspiocbq.iocb,
4042 				      phba->iocb_rsp_size);
4043 		INIT_LIST_HEAD(&(rspiocbq.list));
4044 		irsp = &rspiocbq.iocb;
4045 
4046 		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
4047 		pring->stats.iocb_rsp++;
4048 		rsp_cmpl++;
4049 
4050 		if (unlikely(irsp->ulpStatus)) {
4051 			/*
4052 			 * If resource errors reported from HBA, reduce
4053 			 * queuedepths of the SCSI device.
4054 			 */
4055 			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
4056 			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4057 			     IOERR_NO_RESOURCES)) {
4058 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4059 				phba->lpfc_rampdown_queue_depth(phba);
4060 				spin_lock_irqsave(&phba->hbalock, iflag);
4061 			}
4062 
4063 			/* Rsp ring <ringno> error: IOCB */
4064 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4065 					"0336 Rsp Ring %d error: IOCB Data: "
4066 					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
4067 					pring->ringno,
4068 					irsp->un.ulpWord[0],
4069 					irsp->un.ulpWord[1],
4070 					irsp->un.ulpWord[2],
4071 					irsp->un.ulpWord[3],
4072 					irsp->un.ulpWord[4],
4073 					irsp->un.ulpWord[5],
4074 					*(uint32_t *)&irsp->un1,
4075 					*((uint32_t *)&irsp->un1 + 1));
4076 		}
4077 
4078 		switch (type) {
4079 		case LPFC_ABORT_IOCB:
4080 		case LPFC_SOL_IOCB:
4081 			/*
4082 			 * Idle exchange closed via ABTS from port.  No iocb
4083 			 * resources need to be recovered.
4084 			 */
4085 			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
4086 				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4087 						"0333 IOCB cmd 0x%x"
4088 						" processed. Skipping"
4089 						" completion\n",
4090 						irsp->ulpCommand);
4091 				break;
4092 			}
4093 
4094 			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
4095 							 &rspiocbq);
4096 			if (unlikely(!cmdiocbq))
4097 				break;
4098 			if (cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED)
4099 				cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
4100 			if (cmdiocbq->cmd_cmpl) {
4101 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4102 				cmdiocbq->cmd_cmpl(phba, cmdiocbq, &rspiocbq);
4103 				spin_lock_irqsave(&phba->hbalock, iflag);
4104 			}
4105 			break;
4106 		case LPFC_UNSOL_IOCB:
4107 			spin_unlock_irqrestore(&phba->hbalock, iflag);
4108 			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
4109 			spin_lock_irqsave(&phba->hbalock, iflag);
4110 			break;
4111 		default:
4112 			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4113 				char adaptermsg[LPFC_MAX_ADPTMSG];
4114 				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4115 				memcpy(&adaptermsg[0], (uint8_t *) irsp,
4116 				       MAX_MSG_DATA);
4117 				dev_warn(&((phba->pcidev)->dev),
4118 					 "lpfc%d: %s\n",
4119 					 phba->brd_no, adaptermsg);
4120 			} else {
4121 				/* Unknown IOCB command */
4122 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4123 						"0334 Unknown IOCB command "
4124 						"Data: x%x, x%x x%x x%x x%x\n",
4125 						type, irsp->ulpCommand,
4126 						irsp->ulpStatus,
4127 						irsp->ulpIoTag,
4128 						irsp->ulpContext);
4129 			}
4130 			break;
4131 		}
4132 
4133 		/*
4134 		 * The response IOCB has been processed.  Update the ring
4135 		 * pointer in SLIM.  If the port response put pointer has not
4136 		 * been updated, sync the pgp->rspPutInx and fetch the new port
4137 		 * response put pointer.
4138 		 */
4139 		writel(pring->sli.sli3.rspidx,
4140 			&phba->host_gp[pring->ringno].rspGetInx);
4141 
4142 		if (pring->sli.sli3.rspidx == portRspPut)
4143 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4144 	}
4145 
4146 	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4147 		pring->stats.iocb_rsp_full++;
4148 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4149 		writel(status, phba->CAregaddr);
4150 		readl(phba->CAregaddr);
4151 	}
4152 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4153 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4154 		pring->stats.iocb_cmd_empty++;
4155 
4156 		/* Force update of the local copy of cmdGetInx */
4157 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4158 		lpfc_sli_resume_iocb(phba, pring);
4159 
4160 		if ((pring->lpfc_sli_cmd_available))
4161 			(pring->lpfc_sli_cmd_available) (phba, pring);
4162 
4163 	}
4164 
4165 	phba->fcp_ring_in_use = 0;
4166 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4167 	return rc;
4168 }
4169 
4170 /**
4171  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4172  * @phba: Pointer to HBA context object.
4173  * @pring: Pointer to driver SLI ring object.
4174  * @rspiocbp: Pointer to driver response IOCB object.
4175  *
4176  * This function is called from the worker thread when there is a slow-path
4177  * response IOCB to process. This function chains all the response iocbs until
4178  * seeing the iocb with the LE bit set. The function will call
4179  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4180  * completion of a command iocb. The function will call the
4181  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4182  * The function frees the resources or calls the completion handler if this
4183  * iocb is an abort completion. The function returns NULL when the response
4184  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4185  * this function shall chain the iocb on to the iocb_continueq and return the
4186  * response iocb passed in.
4187  **/
4188 static struct lpfc_iocbq *
lpfc_sli_sp_handle_rspiocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * rspiocbp)4189 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4190 			struct lpfc_iocbq *rspiocbp)
4191 {
4192 	struct lpfc_iocbq *saveq;
4193 	struct lpfc_iocbq *cmdiocb;
4194 	struct lpfc_iocbq *next_iocb;
4195 	IOCB_t *irsp;
4196 	uint32_t free_saveq;
4197 	u8 cmd_type;
4198 	lpfc_iocb_type type;
4199 	unsigned long iflag;
4200 	u32 ulp_status = get_job_ulpstatus(phba, rspiocbp);
4201 	u32 ulp_word4 = get_job_word4(phba, rspiocbp);
4202 	u32 ulp_command = get_job_cmnd(phba, rspiocbp);
4203 	int rc;
4204 
4205 	spin_lock_irqsave(&phba->hbalock, iflag);
4206 	/* First add the response iocb to the countinueq list */
4207 	list_add_tail(&rspiocbp->list, &pring->iocb_continueq);
4208 	pring->iocb_continueq_cnt++;
4209 
4210 	/*
4211 	 * By default, the driver expects to free all resources
4212 	 * associated with this iocb completion.
4213 	 */
4214 	free_saveq = 1;
4215 	saveq = list_get_first(&pring->iocb_continueq,
4216 			       struct lpfc_iocbq, list);
4217 	list_del_init(&pring->iocb_continueq);
4218 	pring->iocb_continueq_cnt = 0;
4219 
4220 	pring->stats.iocb_rsp++;
4221 
4222 	/*
4223 	 * If resource errors reported from HBA, reduce
4224 	 * queuedepths of the SCSI device.
4225 	 */
4226 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
4227 	    ((ulp_word4 & IOERR_PARAM_MASK) ==
4228 	     IOERR_NO_RESOURCES)) {
4229 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4230 		phba->lpfc_rampdown_queue_depth(phba);
4231 		spin_lock_irqsave(&phba->hbalock, iflag);
4232 	}
4233 
4234 	if (ulp_status) {
4235 		/* Rsp ring <ringno> error: IOCB */
4236 		if (phba->sli_rev < LPFC_SLI_REV4) {
4237 			irsp = &rspiocbp->iocb;
4238 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4239 					"0328 Rsp Ring %d error: ulp_status x%x "
4240 					"IOCB Data: "
4241 					"x%08x x%08x x%08x x%08x "
4242 					"x%08x x%08x x%08x x%08x "
4243 					"x%08x x%08x x%08x x%08x "
4244 					"x%08x x%08x x%08x x%08x\n",
4245 					pring->ringno, ulp_status,
4246 					get_job_ulpword(rspiocbp, 0),
4247 					get_job_ulpword(rspiocbp, 1),
4248 					get_job_ulpword(rspiocbp, 2),
4249 					get_job_ulpword(rspiocbp, 3),
4250 					get_job_ulpword(rspiocbp, 4),
4251 					get_job_ulpword(rspiocbp, 5),
4252 					*(((uint32_t *)irsp) + 6),
4253 					*(((uint32_t *)irsp) + 7),
4254 					*(((uint32_t *)irsp) + 8),
4255 					*(((uint32_t *)irsp) + 9),
4256 					*(((uint32_t *)irsp) + 10),
4257 					*(((uint32_t *)irsp) + 11),
4258 					*(((uint32_t *)irsp) + 12),
4259 					*(((uint32_t *)irsp) + 13),
4260 					*(((uint32_t *)irsp) + 14),
4261 					*(((uint32_t *)irsp) + 15));
4262 		} else {
4263 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4264 					"0321 Rsp Ring %d error: "
4265 					"IOCB Data: "
4266 					"x%x x%x x%x x%x\n",
4267 					pring->ringno,
4268 					rspiocbp->wcqe_cmpl.word0,
4269 					rspiocbp->wcqe_cmpl.total_data_placed,
4270 					rspiocbp->wcqe_cmpl.parameter,
4271 					rspiocbp->wcqe_cmpl.word3);
4272 		}
4273 	}
4274 
4275 
4276 	/*
4277 	 * Fetch the iocb command type and call the correct completion
4278 	 * routine. Solicited and Unsolicited IOCBs on the ELS ring
4279 	 * get freed back to the lpfc_iocb_list by the discovery
4280 	 * kernel thread.
4281 	 */
4282 	cmd_type = ulp_command & CMD_IOCB_MASK;
4283 	type = lpfc_sli_iocb_cmd_type(cmd_type);
4284 	switch (type) {
4285 	case LPFC_SOL_IOCB:
4286 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4287 		rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4288 		spin_lock_irqsave(&phba->hbalock, iflag);
4289 		break;
4290 	case LPFC_UNSOL_IOCB:
4291 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4292 		rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4293 		spin_lock_irqsave(&phba->hbalock, iflag);
4294 		if (!rc)
4295 			free_saveq = 0;
4296 		break;
4297 	case LPFC_ABORT_IOCB:
4298 		cmdiocb = NULL;
4299 		if (ulp_command != CMD_XRI_ABORTED_CX)
4300 			cmdiocb = lpfc_sli_iocbq_lookup(phba, pring,
4301 							saveq);
4302 		if (cmdiocb) {
4303 			/* Call the specified completion routine */
4304 			if (cmdiocb->cmd_cmpl) {
4305 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4306 				cmdiocb->cmd_cmpl(phba, cmdiocb, saveq);
4307 				spin_lock_irqsave(&phba->hbalock, iflag);
4308 			} else {
4309 				__lpfc_sli_release_iocbq(phba, cmdiocb);
4310 			}
4311 		}
4312 		break;
4313 	case LPFC_UNKNOWN_IOCB:
4314 		if (ulp_command == CMD_ADAPTER_MSG) {
4315 			char adaptermsg[LPFC_MAX_ADPTMSG];
4316 
4317 			memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4318 			memcpy(&adaptermsg[0], (uint8_t *)&rspiocbp->wqe,
4319 			       MAX_MSG_DATA);
4320 			dev_warn(&((phba->pcidev)->dev),
4321 				 "lpfc%d: %s\n",
4322 				 phba->brd_no, adaptermsg);
4323 		} else {
4324 			/* Unknown command */
4325 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4326 					"0335 Unknown IOCB "
4327 					"command Data: x%x "
4328 					"x%x x%x x%x\n",
4329 					ulp_command,
4330 					ulp_status,
4331 					get_wqe_reqtag(rspiocbp),
4332 					get_job_ulpcontext(phba, rspiocbp));
4333 		}
4334 		break;
4335 	}
4336 
4337 	if (free_saveq) {
4338 		list_for_each_entry_safe(rspiocbp, next_iocb,
4339 					 &saveq->list, list) {
4340 			list_del_init(&rspiocbp->list);
4341 			__lpfc_sli_release_iocbq(phba, rspiocbp);
4342 		}
4343 		__lpfc_sli_release_iocbq(phba, saveq);
4344 	}
4345 	rspiocbp = NULL;
4346 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4347 	return rspiocbp;
4348 }
4349 
4350 /**
4351  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
4352  * @phba: Pointer to HBA context object.
4353  * @pring: Pointer to driver SLI ring object.
4354  * @mask: Host attention register mask for this ring.
4355  *
4356  * This routine wraps the actual slow_ring event process routine from the
4357  * API jump table function pointer from the lpfc_hba struct.
4358  **/
4359 void
lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4360 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4361 				struct lpfc_sli_ring *pring, uint32_t mask)
4362 {
4363 	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4364 }
4365 
4366 /**
4367  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4368  * @phba: Pointer to HBA context object.
4369  * @pring: Pointer to driver SLI ring object.
4370  * @mask: Host attention register mask for this ring.
4371  *
4372  * This function is called from the worker thread when there is a ring event
4373  * for non-fcp rings. The caller does not hold any lock. The function will
4374  * remove each response iocb in the response ring and calls the handle
4375  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4376  **/
4377 static void
lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4378 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4379 				   struct lpfc_sli_ring *pring, uint32_t mask)
4380 {
4381 	struct lpfc_pgp *pgp;
4382 	IOCB_t *entry;
4383 	IOCB_t *irsp = NULL;
4384 	struct lpfc_iocbq *rspiocbp = NULL;
4385 	uint32_t portRspPut, portRspMax;
4386 	unsigned long iflag;
4387 	uint32_t status;
4388 
4389 	pgp = &phba->port_gp[pring->ringno];
4390 	spin_lock_irqsave(&phba->hbalock, iflag);
4391 	pring->stats.iocb_event++;
4392 
4393 	/*
4394 	 * The next available response entry should never exceed the maximum
4395 	 * entries.  If it does, treat it as an adapter hardware error.
4396 	 */
4397 	portRspMax = pring->sli.sli3.numRiocb;
4398 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4399 	if (portRspPut >= portRspMax) {
4400 		/*
4401 		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4402 		 * rsp ring <portRspMax>
4403 		 */
4404 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4405 				"0303 Ring %d handler: portRspPut %d "
4406 				"is bigger than rsp ring %d\n",
4407 				pring->ringno, portRspPut, portRspMax);
4408 
4409 		phba->link_state = LPFC_HBA_ERROR;
4410 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4411 
4412 		phba->work_hs = HS_FFER3;
4413 		lpfc_handle_eratt(phba);
4414 
4415 		return;
4416 	}
4417 
4418 	rmb();
4419 	while (pring->sli.sli3.rspidx != portRspPut) {
4420 		/*
4421 		 * Build a completion list and call the appropriate handler.
4422 		 * The process is to get the next available response iocb, get
4423 		 * a free iocb from the list, copy the response data into the
4424 		 * free iocb, insert to the continuation list, and update the
4425 		 * next response index to slim.  This process makes response
4426 		 * iocb's in the ring available to DMA as fast as possible but
4427 		 * pays a penalty for a copy operation.  Since the iocb is
4428 		 * only 32 bytes, this penalty is considered small relative to
4429 		 * the PCI reads for register values and a slim write.  When
4430 		 * the ulpLe field is set, the entire Command has been
4431 		 * received.
4432 		 */
4433 		entry = lpfc_resp_iocb(phba, pring);
4434 
4435 		phba->last_completion_time = jiffies;
4436 		rspiocbp = __lpfc_sli_get_iocbq(phba);
4437 		if (rspiocbp == NULL) {
4438 			printk(KERN_ERR "%s: out of buffers! Failing "
4439 			       "completion.\n", __func__);
4440 			break;
4441 		}
4442 
4443 		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4444 				      phba->iocb_rsp_size);
4445 		irsp = &rspiocbp->iocb;
4446 
4447 		if (++pring->sli.sli3.rspidx >= portRspMax)
4448 			pring->sli.sli3.rspidx = 0;
4449 
4450 		if (pring->ringno == LPFC_ELS_RING) {
4451 			lpfc_debugfs_slow_ring_trc(phba,
4452 			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4453 				*(((uint32_t *) irsp) + 4),
4454 				*(((uint32_t *) irsp) + 6),
4455 				*(((uint32_t *) irsp) + 7));
4456 		}
4457 
4458 		writel(pring->sli.sli3.rspidx,
4459 			&phba->host_gp[pring->ringno].rspGetInx);
4460 
4461 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4462 		/* Handle the response IOCB */
4463 		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4464 		spin_lock_irqsave(&phba->hbalock, iflag);
4465 
4466 		/*
4467 		 * If the port response put pointer has not been updated, sync
4468 		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4469 		 * response put pointer.
4470 		 */
4471 		if (pring->sli.sli3.rspidx == portRspPut) {
4472 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4473 		}
4474 	} /* while (pring->sli.sli3.rspidx != portRspPut) */
4475 
4476 	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4477 		/* At least one response entry has been freed */
4478 		pring->stats.iocb_rsp_full++;
4479 		/* SET RxRE_RSP in Chip Att register */
4480 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4481 		writel(status, phba->CAregaddr);
4482 		readl(phba->CAregaddr); /* flush */
4483 	}
4484 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4485 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4486 		pring->stats.iocb_cmd_empty++;
4487 
4488 		/* Force update of the local copy of cmdGetInx */
4489 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4490 		lpfc_sli_resume_iocb(phba, pring);
4491 
4492 		if ((pring->lpfc_sli_cmd_available))
4493 			(pring->lpfc_sli_cmd_available) (phba, pring);
4494 
4495 	}
4496 
4497 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4498 	return;
4499 }
4500 
4501 /**
4502  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4503  * @phba: Pointer to HBA context object.
4504  * @pring: Pointer to driver SLI ring object.
4505  * @mask: Host attention register mask for this ring.
4506  *
4507  * This function is called from the worker thread when there is a pending
4508  * ELS response iocb on the driver internal slow-path response iocb worker
4509  * queue. The caller does not hold any lock. The function will remove each
4510  * response iocb from the response worker queue and calls the handle
4511  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4512  **/
4513 static void
lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4514 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4515 				   struct lpfc_sli_ring *pring, uint32_t mask)
4516 {
4517 	struct lpfc_iocbq *irspiocbq;
4518 	struct hbq_dmabuf *dmabuf;
4519 	struct lpfc_cq_event *cq_event;
4520 	unsigned long iflag;
4521 	int count = 0;
4522 
4523 	clear_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
4524 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4525 		/* Get the response iocb from the head of work queue */
4526 		spin_lock_irqsave(&phba->hbalock, iflag);
4527 		list_remove_head(&phba->sli4_hba.sp_queue_event,
4528 				 cq_event, struct lpfc_cq_event, list);
4529 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4530 
4531 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4532 		case CQE_CODE_COMPL_WQE:
4533 			irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4534 						 cq_event);
4535 			/* Translate ELS WCQE to response IOCBQ */
4536 			irspiocbq = lpfc_sli4_els_preprocess_rspiocbq(phba,
4537 								      irspiocbq);
4538 			if (irspiocbq)
4539 				lpfc_sli_sp_handle_rspiocb(phba, pring,
4540 							   irspiocbq);
4541 			count++;
4542 			break;
4543 		case CQE_CODE_RECEIVE:
4544 		case CQE_CODE_RECEIVE_V1:
4545 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
4546 					      cq_event);
4547 			lpfc_sli4_handle_received_buffer(phba, dmabuf);
4548 			count++;
4549 			break;
4550 		default:
4551 			break;
4552 		}
4553 
4554 		/* Limit the number of events to 64 to avoid soft lockups */
4555 		if (count == 64)
4556 			break;
4557 	}
4558 }
4559 
4560 /**
4561  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4562  * @phba: Pointer to HBA context object.
4563  * @pring: Pointer to driver SLI ring object.
4564  *
4565  * This function aborts all iocbs in the given ring and frees all the iocb
4566  * objects in txq. This function issues an abort iocb for all the iocb commands
4567  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4568  * the return of this function. The caller is not required to hold any locks.
4569  **/
4570 void
lpfc_sli_abort_iocb_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)4571 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4572 {
4573 	LIST_HEAD(tx_completions);
4574 	LIST_HEAD(txcmplq_completions);
4575 	struct lpfc_iocbq *iocb, *next_iocb;
4576 	int offline;
4577 
4578 	if (pring->ringno == LPFC_ELS_RING) {
4579 		lpfc_fabric_abort_hba(phba);
4580 	}
4581 	offline = pci_channel_offline(phba->pcidev);
4582 
4583 	/* Error everything on txq and txcmplq
4584 	 * First do the txq.
4585 	 */
4586 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4587 		spin_lock_irq(&pring->ring_lock);
4588 		list_splice_init(&pring->txq, &tx_completions);
4589 		pring->txq_cnt = 0;
4590 
4591 		if (offline) {
4592 			list_splice_init(&pring->txcmplq,
4593 					 &txcmplq_completions);
4594 		} else {
4595 			/* Next issue ABTS for everything on the txcmplq */
4596 			list_for_each_entry_safe(iocb, next_iocb,
4597 						 &pring->txcmplq, list)
4598 				lpfc_sli_issue_abort_iotag(phba, pring,
4599 							   iocb, NULL);
4600 		}
4601 		spin_unlock_irq(&pring->ring_lock);
4602 	} else {
4603 		spin_lock_irq(&phba->hbalock);
4604 		list_splice_init(&pring->txq, &tx_completions);
4605 		pring->txq_cnt = 0;
4606 
4607 		if (offline) {
4608 			list_splice_init(&pring->txcmplq, &txcmplq_completions);
4609 		} else {
4610 			/* Next issue ABTS for everything on the txcmplq */
4611 			list_for_each_entry_safe(iocb, next_iocb,
4612 						 &pring->txcmplq, list)
4613 				lpfc_sli_issue_abort_iotag(phba, pring,
4614 							   iocb, NULL);
4615 		}
4616 		spin_unlock_irq(&phba->hbalock);
4617 	}
4618 
4619 	if (offline) {
4620 		/* Cancel all the IOCBs from the completions list */
4621 		lpfc_sli_cancel_iocbs(phba, &txcmplq_completions,
4622 				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
4623 	} else {
4624 		/* Make sure HBA is alive */
4625 		lpfc_issue_hb_tmo(phba);
4626 	}
4627 	/* Cancel all the IOCBs from the completions list */
4628 	lpfc_sli_cancel_iocbs(phba, &tx_completions, IOSTAT_LOCAL_REJECT,
4629 			      IOERR_SLI_ABORTED);
4630 }
4631 
4632 /**
4633  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4634  * @phba: Pointer to HBA context object.
4635  *
4636  * This function aborts all iocbs in FCP rings and frees all the iocb
4637  * objects in txq. This function issues an abort iocb for all the iocb commands
4638  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4639  * the return of this function. The caller is not required to hold any locks.
4640  **/
4641 void
lpfc_sli_abort_fcp_rings(struct lpfc_hba * phba)4642 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4643 {
4644 	struct lpfc_sli *psli = &phba->sli;
4645 	struct lpfc_sli_ring  *pring;
4646 	uint32_t i;
4647 
4648 	/* Look on all the FCP Rings for the iotag */
4649 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4650 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4651 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4652 			lpfc_sli_abort_iocb_ring(phba, pring);
4653 		}
4654 	} else {
4655 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4656 		lpfc_sli_abort_iocb_ring(phba, pring);
4657 	}
4658 }
4659 
4660 /**
4661  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4662  * @phba: Pointer to HBA context object.
4663  *
4664  * This function flushes all iocbs in the IO ring and frees all the iocb
4665  * objects in txq and txcmplq. This function will not issue abort iocbs
4666  * for all the iocb commands in txcmplq, they will just be returned with
4667  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4668  * slot has been permanently disabled.
4669  **/
4670 void
lpfc_sli_flush_io_rings(struct lpfc_hba * phba)4671 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4672 {
4673 	LIST_HEAD(txq);
4674 	LIST_HEAD(txcmplq);
4675 	struct lpfc_sli *psli = &phba->sli;
4676 	struct lpfc_sli_ring  *pring;
4677 	uint32_t i;
4678 	struct lpfc_iocbq *piocb, *next_iocb;
4679 
4680 	/* Indicate the I/O queues are flushed */
4681 	set_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
4682 
4683 	/* Look on all the FCP Rings for the iotag */
4684 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4685 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4686 			if (!phba->sli4_hba.hdwq ||
4687 			    !phba->sli4_hba.hdwq[i].io_wq) {
4688 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4689 						"7777 hdwq's deleted %lx "
4690 						"%lx %x %x\n",
4691 						phba->pport->load_flag,
4692 						phba->hba_flag,
4693 						phba->link_state,
4694 						phba->sli.sli_flag);
4695 				return;
4696 			}
4697 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4698 
4699 			spin_lock_irq(&pring->ring_lock);
4700 			/* Retrieve everything on txq */
4701 			list_splice_init(&pring->txq, &txq);
4702 			list_for_each_entry_safe(piocb, next_iocb,
4703 						 &pring->txcmplq, list)
4704 				piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4705 			/* Retrieve everything on the txcmplq */
4706 			list_splice_init(&pring->txcmplq, &txcmplq);
4707 			pring->txq_cnt = 0;
4708 			pring->txcmplq_cnt = 0;
4709 			spin_unlock_irq(&pring->ring_lock);
4710 
4711 			/* Flush the txq */
4712 			lpfc_sli_cancel_iocbs(phba, &txq,
4713 					      IOSTAT_LOCAL_REJECT,
4714 					      IOERR_SLI_DOWN);
4715 			/* Flush the txcmplq */
4716 			lpfc_sli_cancel_iocbs(phba, &txcmplq,
4717 					      IOSTAT_LOCAL_REJECT,
4718 					      IOERR_SLI_DOWN);
4719 			if (unlikely(pci_channel_offline(phba->pcidev)))
4720 				lpfc_sli4_io_xri_aborted(phba, NULL, 0);
4721 		}
4722 	} else {
4723 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4724 
4725 		spin_lock_irq(&phba->hbalock);
4726 		/* Retrieve everything on txq */
4727 		list_splice_init(&pring->txq, &txq);
4728 		list_for_each_entry_safe(piocb, next_iocb,
4729 					 &pring->txcmplq, list)
4730 			piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4731 		/* Retrieve everything on the txcmplq */
4732 		list_splice_init(&pring->txcmplq, &txcmplq);
4733 		pring->txq_cnt = 0;
4734 		pring->txcmplq_cnt = 0;
4735 		spin_unlock_irq(&phba->hbalock);
4736 
4737 		/* Flush the txq */
4738 		lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4739 				      IOERR_SLI_DOWN);
4740 		/* Flush the txcmpq */
4741 		lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4742 				      IOERR_SLI_DOWN);
4743 	}
4744 }
4745 
4746 /**
4747  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4748  * @phba: Pointer to HBA context object.
4749  * @mask: Bit mask to be checked.
4750  *
4751  * This function reads the host status register and compares
4752  * with the provided bit mask to check if HBA completed
4753  * the restart. This function will wait in a loop for the
4754  * HBA to complete restart. If the HBA does not restart within
4755  * 15 iterations, the function will reset the HBA again. The
4756  * function returns 1 when HBA fail to restart otherwise returns
4757  * zero.
4758  **/
4759 static int
lpfc_sli_brdready_s3(struct lpfc_hba * phba,uint32_t mask)4760 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4761 {
4762 	uint32_t status;
4763 	int i = 0;
4764 	int retval = 0;
4765 
4766 	/* Read the HBA Host Status Register */
4767 	if (lpfc_readl(phba->HSregaddr, &status))
4768 		return 1;
4769 
4770 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
4771 
4772 	/*
4773 	 * Check status register every 100ms for 5 retries, then every
4774 	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4775 	 * every 2.5 sec for 4.
4776 	 * Break our of the loop if errors occurred during init.
4777 	 */
4778 	while (((status & mask) != mask) &&
4779 	       !(status & HS_FFERM) &&
4780 	       i++ < 20) {
4781 
4782 		if (i <= 5)
4783 			msleep(10);
4784 		else if (i <= 10)
4785 			msleep(500);
4786 		else
4787 			msleep(2500);
4788 
4789 		if (i == 15) {
4790 				/* Do post */
4791 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4792 			lpfc_sli_brdrestart(phba);
4793 		}
4794 		/* Read the HBA Host Status Register */
4795 		if (lpfc_readl(phba->HSregaddr, &status)) {
4796 			retval = 1;
4797 			break;
4798 		}
4799 	}
4800 
4801 	/* Check to see if any errors occurred during init */
4802 	if ((status & HS_FFERM) || (i >= 20)) {
4803 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4804 				"2751 Adapter failed to restart, "
4805 				"status reg x%x, FW Data: A8 x%x AC x%x\n",
4806 				status,
4807 				readl(phba->MBslimaddr + 0xa8),
4808 				readl(phba->MBslimaddr + 0xac));
4809 		phba->link_state = LPFC_HBA_ERROR;
4810 		retval = 1;
4811 	}
4812 
4813 	return retval;
4814 }
4815 
4816 /**
4817  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4818  * @phba: Pointer to HBA context object.
4819  * @mask: Bit mask to be checked.
4820  *
4821  * This function checks the host status register to check if HBA is
4822  * ready. This function will wait in a loop for the HBA to be ready
4823  * If the HBA is not ready , the function will will reset the HBA PCI
4824  * function again. The function returns 1 when HBA fail to be ready
4825  * otherwise returns zero.
4826  **/
4827 static int
lpfc_sli_brdready_s4(struct lpfc_hba * phba,uint32_t mask)4828 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4829 {
4830 	uint32_t status;
4831 	int retval = 0;
4832 
4833 	/* Read the HBA Host Status Register */
4834 	status = lpfc_sli4_post_status_check(phba);
4835 
4836 	if (status) {
4837 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4838 		lpfc_sli_brdrestart(phba);
4839 		status = lpfc_sli4_post_status_check(phba);
4840 	}
4841 
4842 	/* Check to see if any errors occurred during init */
4843 	if (status) {
4844 		phba->link_state = LPFC_HBA_ERROR;
4845 		retval = 1;
4846 	} else
4847 		phba->sli4_hba.intr_enable = 0;
4848 
4849 	clear_bit(HBA_SETUP, &phba->hba_flag);
4850 	return retval;
4851 }
4852 
4853 /**
4854  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4855  * @phba: Pointer to HBA context object.
4856  * @mask: Bit mask to be checked.
4857  *
4858  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4859  * from the API jump table function pointer from the lpfc_hba struct.
4860  **/
4861 int
lpfc_sli_brdready(struct lpfc_hba * phba,uint32_t mask)4862 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4863 {
4864 	return phba->lpfc_sli_brdready(phba, mask);
4865 }
4866 
4867 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4868 
4869 /**
4870  * lpfc_reset_barrier - Make HBA ready for HBA reset
4871  * @phba: Pointer to HBA context object.
4872  *
4873  * This function is called before resetting an HBA. This function is called
4874  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4875  **/
lpfc_reset_barrier(struct lpfc_hba * phba)4876 void lpfc_reset_barrier(struct lpfc_hba *phba)
4877 {
4878 	uint32_t __iomem *resp_buf;
4879 	uint32_t __iomem *mbox_buf;
4880 	volatile struct MAILBOX_word0 mbox;
4881 	uint32_t hc_copy, ha_copy, resp_data;
4882 	int  i;
4883 	uint8_t hdrtype;
4884 
4885 	lockdep_assert_held(&phba->hbalock);
4886 
4887 	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4888 	if (hdrtype != PCI_HEADER_TYPE_MFD ||
4889 	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4890 	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4891 		return;
4892 
4893 	/*
4894 	 * Tell the other part of the chip to suspend temporarily all
4895 	 * its DMA activity.
4896 	 */
4897 	resp_buf = phba->MBslimaddr;
4898 
4899 	/* Disable the error attention */
4900 	if (lpfc_readl(phba->HCregaddr, &hc_copy))
4901 		return;
4902 	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4903 	readl(phba->HCregaddr); /* flush */
4904 	phba->link_flag |= LS_IGNORE_ERATT;
4905 
4906 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
4907 		return;
4908 	if (ha_copy & HA_ERATT) {
4909 		/* Clear Chip error bit */
4910 		writel(HA_ERATT, phba->HAregaddr);
4911 		phba->pport->stopped = 1;
4912 	}
4913 
4914 	mbox.word0 = 0;
4915 	mbox.mbxCommand = MBX_KILL_BOARD;
4916 	mbox.mbxOwner = OWN_CHIP;
4917 
4918 	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4919 	mbox_buf = phba->MBslimaddr;
4920 	writel(mbox.word0, mbox_buf);
4921 
4922 	for (i = 0; i < 50; i++) {
4923 		if (lpfc_readl((resp_buf + 1), &resp_data))
4924 			return;
4925 		if (resp_data != ~(BARRIER_TEST_PATTERN))
4926 			mdelay(1);
4927 		else
4928 			break;
4929 	}
4930 	resp_data = 0;
4931 	if (lpfc_readl((resp_buf + 1), &resp_data))
4932 		return;
4933 	if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4934 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4935 		    phba->pport->stopped)
4936 			goto restore_hc;
4937 		else
4938 			goto clear_errat;
4939 	}
4940 
4941 	mbox.mbxOwner = OWN_HOST;
4942 	resp_data = 0;
4943 	for (i = 0; i < 500; i++) {
4944 		if (lpfc_readl(resp_buf, &resp_data))
4945 			return;
4946 		if (resp_data != mbox.word0)
4947 			mdelay(1);
4948 		else
4949 			break;
4950 	}
4951 
4952 clear_errat:
4953 
4954 	while (++i < 500) {
4955 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
4956 			return;
4957 		if (!(ha_copy & HA_ERATT))
4958 			mdelay(1);
4959 		else
4960 			break;
4961 	}
4962 
4963 	if (readl(phba->HAregaddr) & HA_ERATT) {
4964 		writel(HA_ERATT, phba->HAregaddr);
4965 		phba->pport->stopped = 1;
4966 	}
4967 
4968 restore_hc:
4969 	phba->link_flag &= ~LS_IGNORE_ERATT;
4970 	writel(hc_copy, phba->HCregaddr);
4971 	readl(phba->HCregaddr); /* flush */
4972 }
4973 
4974 /**
4975  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4976  * @phba: Pointer to HBA context object.
4977  *
4978  * This function issues a kill_board mailbox command and waits for
4979  * the error attention interrupt. This function is called for stopping
4980  * the firmware processing. The caller is not required to hold any
4981  * locks. This function calls lpfc_hba_down_post function to free
4982  * any pending commands after the kill. The function will return 1 when it
4983  * fails to kill the board else will return 0.
4984  **/
4985 int
lpfc_sli_brdkill(struct lpfc_hba * phba)4986 lpfc_sli_brdkill(struct lpfc_hba *phba)
4987 {
4988 	struct lpfc_sli *psli;
4989 	LPFC_MBOXQ_t *pmb;
4990 	uint32_t status;
4991 	uint32_t ha_copy;
4992 	int retval;
4993 	int i = 0;
4994 
4995 	psli = &phba->sli;
4996 
4997 	/* Kill HBA */
4998 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4999 			"0329 Kill HBA Data: x%x x%x\n",
5000 			phba->pport->port_state, psli->sli_flag);
5001 
5002 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5003 	if (!pmb)
5004 		return 1;
5005 
5006 	/* Disable the error attention */
5007 	spin_lock_irq(&phba->hbalock);
5008 	if (lpfc_readl(phba->HCregaddr, &status)) {
5009 		spin_unlock_irq(&phba->hbalock);
5010 		mempool_free(pmb, phba->mbox_mem_pool);
5011 		return 1;
5012 	}
5013 	status &= ~HC_ERINT_ENA;
5014 	writel(status, phba->HCregaddr);
5015 	readl(phba->HCregaddr); /* flush */
5016 	phba->link_flag |= LS_IGNORE_ERATT;
5017 	spin_unlock_irq(&phba->hbalock);
5018 
5019 	lpfc_kill_board(phba, pmb);
5020 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5021 	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5022 
5023 	if (retval != MBX_SUCCESS) {
5024 		if (retval != MBX_BUSY)
5025 			mempool_free(pmb, phba->mbox_mem_pool);
5026 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5027 				"2752 KILL_BOARD command failed retval %d\n",
5028 				retval);
5029 		spin_lock_irq(&phba->hbalock);
5030 		phba->link_flag &= ~LS_IGNORE_ERATT;
5031 		spin_unlock_irq(&phba->hbalock);
5032 		return 1;
5033 	}
5034 
5035 	spin_lock_irq(&phba->hbalock);
5036 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5037 	spin_unlock_irq(&phba->hbalock);
5038 
5039 	mempool_free(pmb, phba->mbox_mem_pool);
5040 
5041 	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
5042 	 * attention every 100ms for 3 seconds. If we don't get ERATT after
5043 	 * 3 seconds we still set HBA_ERROR state because the status of the
5044 	 * board is now undefined.
5045 	 */
5046 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
5047 		return 1;
5048 	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
5049 		mdelay(100);
5050 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
5051 			return 1;
5052 	}
5053 
5054 	timer_delete_sync(&psli->mbox_tmo);
5055 	if (ha_copy & HA_ERATT) {
5056 		writel(HA_ERATT, phba->HAregaddr);
5057 		phba->pport->stopped = 1;
5058 	}
5059 	spin_lock_irq(&phba->hbalock);
5060 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5061 	psli->mbox_active = NULL;
5062 	phba->link_flag &= ~LS_IGNORE_ERATT;
5063 	spin_unlock_irq(&phba->hbalock);
5064 
5065 	lpfc_hba_down_post(phba);
5066 	phba->link_state = LPFC_HBA_ERROR;
5067 
5068 	return ha_copy & HA_ERATT ? 0 : 1;
5069 }
5070 
5071 /**
5072  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
5073  * @phba: Pointer to HBA context object.
5074  *
5075  * This function resets the HBA by writing HC_INITFF to the control
5076  * register. After the HBA resets, this function resets all the iocb ring
5077  * indices. This function disables PCI layer parity checking during
5078  * the reset.
5079  * This function returns 0 always.
5080  * The caller is not required to hold any locks.
5081  **/
5082 int
lpfc_sli_brdreset(struct lpfc_hba * phba)5083 lpfc_sli_brdreset(struct lpfc_hba *phba)
5084 {
5085 	struct lpfc_sli *psli;
5086 	struct lpfc_sli_ring *pring;
5087 	uint16_t cfg_value;
5088 	int i;
5089 
5090 	psli = &phba->sli;
5091 
5092 	/* Reset HBA */
5093 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5094 			"0325 Reset HBA Data: x%x x%x\n",
5095 			(phba->pport) ? phba->pport->port_state : 0,
5096 			psli->sli_flag);
5097 
5098 	/* perform board reset */
5099 	phba->fc_eventTag = 0;
5100 	phba->link_events = 0;
5101 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5102 	if (phba->pport) {
5103 		phba->pport->fc_myDID = 0;
5104 		phba->pport->fc_prevDID = 0;
5105 	}
5106 
5107 	/* Turn off parity checking and serr during the physical reset */
5108 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
5109 		return -EIO;
5110 
5111 	pci_write_config_word(phba->pcidev, PCI_COMMAND,
5112 			      (cfg_value &
5113 			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5114 
5115 	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
5116 
5117 	/* Now toggle INITFF bit in the Host Control Register */
5118 	writel(HC_INITFF, phba->HCregaddr);
5119 	mdelay(1);
5120 	readl(phba->HCregaddr); /* flush */
5121 	writel(0, phba->HCregaddr);
5122 	readl(phba->HCregaddr); /* flush */
5123 
5124 	/* Restore PCI cmd register */
5125 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5126 
5127 	/* Initialize relevant SLI info */
5128 	for (i = 0; i < psli->num_rings; i++) {
5129 		pring = &psli->sli3_ring[i];
5130 		pring->flag = 0;
5131 		pring->sli.sli3.rspidx = 0;
5132 		pring->sli.sli3.next_cmdidx  = 0;
5133 		pring->sli.sli3.local_getidx = 0;
5134 		pring->sli.sli3.cmdidx = 0;
5135 		pring->missbufcnt = 0;
5136 	}
5137 
5138 	phba->link_state = LPFC_WARM_START;
5139 	return 0;
5140 }
5141 
5142 /**
5143  * lpfc_sli4_brdreset - Reset a sli-4 HBA
5144  * @phba: Pointer to HBA context object.
5145  *
5146  * This function resets a SLI4 HBA. This function disables PCI layer parity
5147  * checking during resets the device. The caller is not required to hold
5148  * any locks.
5149  *
5150  * This function returns 0 on success else returns negative error code.
5151  **/
5152 int
lpfc_sli4_brdreset(struct lpfc_hba * phba)5153 lpfc_sli4_brdreset(struct lpfc_hba *phba)
5154 {
5155 	struct lpfc_sli *psli = &phba->sli;
5156 	uint16_t cfg_value;
5157 	int rc = 0;
5158 
5159 	/* Reset HBA */
5160 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5161 			"0295 Reset HBA Data: x%x x%x x%lx\n",
5162 			phba->pport->port_state, psli->sli_flag,
5163 			phba->hba_flag);
5164 
5165 	/* perform board reset */
5166 	phba->fc_eventTag = 0;
5167 	phba->link_events = 0;
5168 	phba->pport->fc_myDID = 0;
5169 	phba->pport->fc_prevDID = 0;
5170 	clear_bit(HBA_SETUP, &phba->hba_flag);
5171 
5172 	spin_lock_irq(&phba->hbalock);
5173 	psli->sli_flag &= ~(LPFC_PROCESS_LA);
5174 	phba->fcf.fcf_flag = 0;
5175 	spin_unlock_irq(&phba->hbalock);
5176 
5177 	/* Now physically reset the device */
5178 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5179 			"0389 Performing PCI function reset!\n");
5180 
5181 	/* Turn off parity checking and serr during the physical reset */
5182 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5183 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5184 				"3205 PCI read Config failed\n");
5185 		return -EIO;
5186 	}
5187 
5188 	pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5189 			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5190 
5191 	/* Perform FCoE PCI function reset before freeing queue memory */
5192 	rc = lpfc_pci_function_reset(phba);
5193 
5194 	/* Restore PCI cmd register */
5195 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5196 
5197 	return rc;
5198 }
5199 
5200 /**
5201  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
5202  * @phba: Pointer to HBA context object.
5203  *
5204  * This function is called in the SLI initialization code path to
5205  * restart the HBA. The caller is not required to hold any lock.
5206  * This function writes MBX_RESTART mailbox command to the SLIM and
5207  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5208  * function to free any pending commands. The function enables
5209  * POST only during the first initialization. The function returns zero.
5210  * The function does not guarantee completion of MBX_RESTART mailbox
5211  * command before the return of this function.
5212  **/
5213 static int
lpfc_sli_brdrestart_s3(struct lpfc_hba * phba)5214 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
5215 {
5216 	volatile struct MAILBOX_word0 mb;
5217 	struct lpfc_sli *psli;
5218 	void __iomem *to_slim;
5219 
5220 	spin_lock_irq(&phba->hbalock);
5221 
5222 	psli = &phba->sli;
5223 
5224 	/* Restart HBA */
5225 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5226 			"0337 Restart HBA Data: x%x x%x\n",
5227 			(phba->pport) ? phba->pport->port_state : 0,
5228 			psli->sli_flag);
5229 
5230 	mb.word0 = 0;
5231 	mb.mbxCommand = MBX_RESTART;
5232 	mb.mbxHc = 1;
5233 
5234 	lpfc_reset_barrier(phba);
5235 
5236 	to_slim = phba->MBslimaddr;
5237 	writel(mb.word0, to_slim);
5238 	readl(to_slim); /* flush */
5239 
5240 	/* Only skip post after fc_ffinit is completed */
5241 	if (phba->pport && phba->pport->port_state)
5242 		mb.word0 = 1;	/* This is really setting up word1 */
5243 	else
5244 		mb.word0 = 0;	/* This is really setting up word1 */
5245 	to_slim = phba->MBslimaddr + sizeof (uint32_t);
5246 	writel(mb.word0, to_slim);
5247 	readl(to_slim); /* flush */
5248 
5249 	lpfc_sli_brdreset(phba);
5250 	if (phba->pport)
5251 		phba->pport->stopped = 0;
5252 	phba->link_state = LPFC_INIT_START;
5253 	phba->hba_flag = 0;
5254 	spin_unlock_irq(&phba->hbalock);
5255 
5256 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5257 	psli->stats_start = ktime_get_seconds();
5258 
5259 	/* Give the INITFF and Post time to settle. */
5260 	mdelay(100);
5261 
5262 	lpfc_hba_down_post(phba);
5263 
5264 	return 0;
5265 }
5266 
5267 /**
5268  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5269  * @phba: Pointer to HBA context object.
5270  *
5271  * This function is called in the SLI initialization code path to restart
5272  * a SLI4 HBA. The caller is not required to hold any lock.
5273  * At the end of the function, it calls lpfc_hba_down_post function to
5274  * free any pending commands.
5275  **/
5276 static int
lpfc_sli_brdrestart_s4(struct lpfc_hba * phba)5277 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5278 {
5279 	struct lpfc_sli *psli = &phba->sli;
5280 	int rc;
5281 
5282 	/* Restart HBA */
5283 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5284 			"0296 Restart HBA Data: x%x x%x\n",
5285 			phba->pport->port_state, psli->sli_flag);
5286 
5287 	lpfc_sli4_queue_unset(phba);
5288 
5289 	rc = lpfc_sli4_brdreset(phba);
5290 	if (rc) {
5291 		phba->link_state = LPFC_HBA_ERROR;
5292 		goto hba_down_queue;
5293 	}
5294 
5295 	spin_lock_irq(&phba->hbalock);
5296 	phba->pport->stopped = 0;
5297 	phba->link_state = LPFC_INIT_START;
5298 	phba->hba_flag = 0;
5299 	/* Preserve FA-PWWN expectation */
5300 	phba->sli4_hba.fawwpn_flag &= LPFC_FAWWPN_FABRIC;
5301 	spin_unlock_irq(&phba->hbalock);
5302 
5303 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5304 	psli->stats_start = ktime_get_seconds();
5305 
5306 hba_down_queue:
5307 	lpfc_hba_down_post(phba);
5308 	lpfc_sli4_queue_destroy(phba);
5309 
5310 	return rc;
5311 }
5312 
5313 /**
5314  * lpfc_sli_brdrestart - Wrapper func for restarting hba
5315  * @phba: Pointer to HBA context object.
5316  *
5317  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5318  * API jump table function pointer from the lpfc_hba struct.
5319 **/
5320 int
lpfc_sli_brdrestart(struct lpfc_hba * phba)5321 lpfc_sli_brdrestart(struct lpfc_hba *phba)
5322 {
5323 	return phba->lpfc_sli_brdrestart(phba);
5324 }
5325 
5326 /**
5327  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
5328  * @phba: Pointer to HBA context object.
5329  *
5330  * This function is called after a HBA restart to wait for successful
5331  * restart of the HBA. Successful restart of the HBA is indicated by
5332  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5333  * iteration, the function will restart the HBA again. The function returns
5334  * zero if HBA successfully restarted else returns negative error code.
5335  **/
5336 int
lpfc_sli_chipset_init(struct lpfc_hba * phba)5337 lpfc_sli_chipset_init(struct lpfc_hba *phba)
5338 {
5339 	uint32_t status, i = 0;
5340 
5341 	/* Read the HBA Host Status Register */
5342 	if (lpfc_readl(phba->HSregaddr, &status))
5343 		return -EIO;
5344 
5345 	/* Check status register to see what current state is */
5346 	i = 0;
5347 	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5348 
5349 		/* Check every 10ms for 10 retries, then every 100ms for 90
5350 		 * retries, then every 1 sec for 50 retires for a total of
5351 		 * ~60 seconds before reset the board again and check every
5352 		 * 1 sec for 50 retries. The up to 60 seconds before the
5353 		 * board ready is required by the Falcon FIPS zeroization
5354 		 * complete, and any reset the board in between shall cause
5355 		 * restart of zeroization, further delay the board ready.
5356 		 */
5357 		if (i++ >= 200) {
5358 			/* Adapter failed to init, timeout, status reg
5359 			   <status> */
5360 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5361 					"0436 Adapter failed to init, "
5362 					"timeout, status reg x%x, "
5363 					"FW Data: A8 x%x AC x%x\n", status,
5364 					readl(phba->MBslimaddr + 0xa8),
5365 					readl(phba->MBslimaddr + 0xac));
5366 			phba->link_state = LPFC_HBA_ERROR;
5367 			return -ETIMEDOUT;
5368 		}
5369 
5370 		/* Check to see if any errors occurred during init */
5371 		if (status & HS_FFERM) {
5372 			/* ERROR: During chipset initialization */
5373 			/* Adapter failed to init, chipset, status reg
5374 			   <status> */
5375 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5376 					"0437 Adapter failed to init, "
5377 					"chipset, status reg x%x, "
5378 					"FW Data: A8 x%x AC x%x\n", status,
5379 					readl(phba->MBslimaddr + 0xa8),
5380 					readl(phba->MBslimaddr + 0xac));
5381 			phba->link_state = LPFC_HBA_ERROR;
5382 			return -EIO;
5383 		}
5384 
5385 		if (i <= 10)
5386 			msleep(10);
5387 		else if (i <= 100)
5388 			msleep(100);
5389 		else
5390 			msleep(1000);
5391 
5392 		if (i == 150) {
5393 			/* Do post */
5394 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5395 			lpfc_sli_brdrestart(phba);
5396 		}
5397 		/* Read the HBA Host Status Register */
5398 		if (lpfc_readl(phba->HSregaddr, &status))
5399 			return -EIO;
5400 	}
5401 
5402 	/* Check to see if any errors occurred during init */
5403 	if (status & HS_FFERM) {
5404 		/* ERROR: During chipset initialization */
5405 		/* Adapter failed to init, chipset, status reg <status> */
5406 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5407 				"0438 Adapter failed to init, chipset, "
5408 				"status reg x%x, "
5409 				"FW Data: A8 x%x AC x%x\n", status,
5410 				readl(phba->MBslimaddr + 0xa8),
5411 				readl(phba->MBslimaddr + 0xac));
5412 		phba->link_state = LPFC_HBA_ERROR;
5413 		return -EIO;
5414 	}
5415 
5416 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5417 
5418 	/* Clear all interrupt enable conditions */
5419 	writel(0, phba->HCregaddr);
5420 	readl(phba->HCregaddr); /* flush */
5421 
5422 	/* setup host attn register */
5423 	writel(0xffffffff, phba->HAregaddr);
5424 	readl(phba->HAregaddr); /* flush */
5425 	return 0;
5426 }
5427 
5428 /**
5429  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5430  *
5431  * This function calculates and returns the number of HBQs required to be
5432  * configured.
5433  **/
5434 int
lpfc_sli_hbq_count(void)5435 lpfc_sli_hbq_count(void)
5436 {
5437 	return ARRAY_SIZE(lpfc_hbq_defs);
5438 }
5439 
5440 /**
5441  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5442  *
5443  * This function adds the number of hbq entries in every HBQ to get
5444  * the total number of hbq entries required for the HBA and returns
5445  * the total count.
5446  **/
5447 static int
lpfc_sli_hbq_entry_count(void)5448 lpfc_sli_hbq_entry_count(void)
5449 {
5450 	int  hbq_count = lpfc_sli_hbq_count();
5451 	int  count = 0;
5452 	int  i;
5453 
5454 	for (i = 0; i < hbq_count; ++i)
5455 		count += lpfc_hbq_defs[i]->entry_count;
5456 	return count;
5457 }
5458 
5459 /**
5460  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5461  *
5462  * This function calculates amount of memory required for all hbq entries
5463  * to be configured and returns the total memory required.
5464  **/
5465 int
lpfc_sli_hbq_size(void)5466 lpfc_sli_hbq_size(void)
5467 {
5468 	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5469 }
5470 
5471 /**
5472  * lpfc_sli_hbq_setup - configure and initialize HBQs
5473  * @phba: Pointer to HBA context object.
5474  *
5475  * This function is called during the SLI initialization to configure
5476  * all the HBQs and post buffers to the HBQ. The caller is not
5477  * required to hold any locks. This function will return zero if successful
5478  * else it will return negative error code.
5479  **/
5480 static int
lpfc_sli_hbq_setup(struct lpfc_hba * phba)5481 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5482 {
5483 	int  hbq_count = lpfc_sli_hbq_count();
5484 	LPFC_MBOXQ_t *pmb;
5485 	MAILBOX_t *pmbox;
5486 	uint32_t hbqno;
5487 	uint32_t hbq_entry_index;
5488 
5489 				/* Get a Mailbox buffer to setup mailbox
5490 				 * commands for HBA initialization
5491 				 */
5492 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5493 
5494 	if (!pmb)
5495 		return -ENOMEM;
5496 
5497 	pmbox = &pmb->u.mb;
5498 
5499 	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
5500 	phba->link_state = LPFC_INIT_MBX_CMDS;
5501 	phba->hbq_in_use = 1;
5502 
5503 	hbq_entry_index = 0;
5504 	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5505 		phba->hbqs[hbqno].next_hbqPutIdx = 0;
5506 		phba->hbqs[hbqno].hbqPutIdx      = 0;
5507 		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5508 		phba->hbqs[hbqno].entry_count =
5509 			lpfc_hbq_defs[hbqno]->entry_count;
5510 		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5511 			hbq_entry_index, pmb);
5512 		hbq_entry_index += phba->hbqs[hbqno].entry_count;
5513 
5514 		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5515 			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5516 			   mbxStatus <status>, ring <num> */
5517 
5518 			lpfc_printf_log(phba, KERN_ERR,
5519 					LOG_SLI | LOG_VPORT,
5520 					"1805 Adapter failed to init. "
5521 					"Data: x%x x%x x%x\n",
5522 					pmbox->mbxCommand,
5523 					pmbox->mbxStatus, hbqno);
5524 
5525 			phba->link_state = LPFC_HBA_ERROR;
5526 			mempool_free(pmb, phba->mbox_mem_pool);
5527 			return -ENXIO;
5528 		}
5529 	}
5530 	phba->hbq_count = hbq_count;
5531 
5532 	mempool_free(pmb, phba->mbox_mem_pool);
5533 
5534 	/* Initially populate or replenish the HBQs */
5535 	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5536 		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5537 	return 0;
5538 }
5539 
5540 /**
5541  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5542  * @phba: Pointer to HBA context object.
5543  *
5544  * This function is called during the SLI initialization to configure
5545  * all the HBQs and post buffers to the HBQ. The caller is not
5546  * required to hold any locks. This function will return zero if successful
5547  * else it will return negative error code.
5548  **/
5549 static int
lpfc_sli4_rb_setup(struct lpfc_hba * phba)5550 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5551 {
5552 	phba->hbq_in_use = 1;
5553 	/**
5554 	 * Specific case when the MDS diagnostics is enabled and supported.
5555 	 * The receive buffer count is truncated to manage the incoming
5556 	 * traffic.
5557 	 **/
5558 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5559 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5560 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5561 	else
5562 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5563 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5564 	phba->hbq_count = 1;
5565 	lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5566 	/* Initially populate or replenish the HBQs */
5567 	return 0;
5568 }
5569 
5570 /**
5571  * lpfc_sli_config_port - Issue config port mailbox command
5572  * @phba: Pointer to HBA context object.
5573  * @sli_mode: sli mode - 2/3
5574  *
5575  * This function is called by the sli initialization code path
5576  * to issue config_port mailbox command. This function restarts the
5577  * HBA firmware and issues a config_port mailbox command to configure
5578  * the SLI interface in the sli mode specified by sli_mode
5579  * variable. The caller is not required to hold any locks.
5580  * The function returns 0 if successful, else returns negative error
5581  * code.
5582  **/
5583 int
lpfc_sli_config_port(struct lpfc_hba * phba,int sli_mode)5584 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5585 {
5586 	LPFC_MBOXQ_t *pmb;
5587 	uint32_t resetcount = 0, rc = 0, done = 0;
5588 
5589 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5590 	if (!pmb) {
5591 		phba->link_state = LPFC_HBA_ERROR;
5592 		return -ENOMEM;
5593 	}
5594 
5595 	phba->sli_rev = sli_mode;
5596 	while (resetcount < 2 && !done) {
5597 		spin_lock_irq(&phba->hbalock);
5598 		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5599 		spin_unlock_irq(&phba->hbalock);
5600 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5601 		lpfc_sli_brdrestart(phba);
5602 		rc = lpfc_sli_chipset_init(phba);
5603 		if (rc)
5604 			break;
5605 
5606 		spin_lock_irq(&phba->hbalock);
5607 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5608 		spin_unlock_irq(&phba->hbalock);
5609 		resetcount++;
5610 
5611 		/* Call pre CONFIG_PORT mailbox command initialization.  A
5612 		 * value of 0 means the call was successful.  Any other
5613 		 * nonzero value is a failure, but if ERESTART is returned,
5614 		 * the driver may reset the HBA and try again.
5615 		 */
5616 		rc = lpfc_config_port_prep(phba);
5617 		if (rc == -ERESTART) {
5618 			phba->link_state = LPFC_LINK_UNKNOWN;
5619 			continue;
5620 		} else if (rc)
5621 			break;
5622 
5623 		phba->link_state = LPFC_INIT_MBX_CMDS;
5624 		lpfc_config_port(phba, pmb);
5625 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5626 		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5627 					LPFC_SLI3_HBQ_ENABLED |
5628 					LPFC_SLI3_CRP_ENABLED |
5629 					LPFC_SLI3_DSS_ENABLED);
5630 		if (rc != MBX_SUCCESS) {
5631 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5632 				"0442 Adapter failed to init, mbxCmd x%x "
5633 				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5634 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5635 			spin_lock_irq(&phba->hbalock);
5636 			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5637 			spin_unlock_irq(&phba->hbalock);
5638 			rc = -ENXIO;
5639 		} else {
5640 			/* Allow asynchronous mailbox command to go through */
5641 			spin_lock_irq(&phba->hbalock);
5642 			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5643 			spin_unlock_irq(&phba->hbalock);
5644 			done = 1;
5645 
5646 			if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5647 			    (pmb->u.mb.un.varCfgPort.gasabt == 0))
5648 				lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5649 					"3110 Port did not grant ASABT\n");
5650 		}
5651 	}
5652 	if (!done) {
5653 		rc = -EINVAL;
5654 		goto do_prep_failed;
5655 	}
5656 	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5657 		if (!pmb->u.mb.un.varCfgPort.cMA) {
5658 			rc = -ENXIO;
5659 			goto do_prep_failed;
5660 		}
5661 		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5662 			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5663 			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5664 			phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5665 				phba->max_vpi : phba->max_vports;
5666 
5667 		} else
5668 			phba->max_vpi = 0;
5669 		if (pmb->u.mb.un.varCfgPort.gerbm)
5670 			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5671 		if (pmb->u.mb.un.varCfgPort.gcrp)
5672 			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5673 
5674 		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5675 		phba->port_gp = phba->mbox->us.s3_pgp.port;
5676 
5677 		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5678 			if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5679 				phba->cfg_enable_bg = 0;
5680 				phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5681 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5682 						"0443 Adapter did not grant "
5683 						"BlockGuard\n");
5684 			}
5685 		}
5686 	} else {
5687 		phba->hbq_get = NULL;
5688 		phba->port_gp = phba->mbox->us.s2.port;
5689 		phba->max_vpi = 0;
5690 	}
5691 do_prep_failed:
5692 	mempool_free(pmb, phba->mbox_mem_pool);
5693 	return rc;
5694 }
5695 
5696 
5697 /**
5698  * lpfc_sli_hba_setup - SLI initialization function
5699  * @phba: Pointer to HBA context object.
5700  *
5701  * This function is the main SLI initialization function. This function
5702  * is called by the HBA initialization code, HBA reset code and HBA
5703  * error attention handler code. Caller is not required to hold any
5704  * locks. This function issues config_port mailbox command to configure
5705  * the SLI, setup iocb rings and HBQ rings. In the end the function
5706  * calls the config_port_post function to issue init_link mailbox
5707  * command and to start the discovery. The function will return zero
5708  * if successful, else it will return negative error code.
5709  **/
5710 int
lpfc_sli_hba_setup(struct lpfc_hba * phba)5711 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5712 {
5713 	uint32_t rc;
5714 	int  i;
5715 	int longs;
5716 
5717 	/* Enable ISR already does config_port because of config_msi mbx */
5718 	if (test_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag)) {
5719 		rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5720 		if (rc)
5721 			return -EIO;
5722 		clear_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5723 	}
5724 	phba->fcp_embed_io = 0;	/* SLI4 FC support only */
5725 
5726 	if (phba->sli_rev == 3) {
5727 		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5728 		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5729 	} else {
5730 		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5731 		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5732 		phba->sli3_options = 0;
5733 	}
5734 
5735 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5736 			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
5737 			phba->sli_rev, phba->max_vpi);
5738 	rc = lpfc_sli_ring_map(phba);
5739 
5740 	if (rc)
5741 		goto lpfc_sli_hba_setup_error;
5742 
5743 	/* Initialize VPIs. */
5744 	if (phba->sli_rev == LPFC_SLI_REV3) {
5745 		/*
5746 		 * The VPI bitmask and physical ID array are allocated
5747 		 * and initialized once only - at driver load.  A port
5748 		 * reset doesn't need to reinitialize this memory.
5749 		 */
5750 		if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5751 			longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5752 			phba->vpi_bmask = kcalloc(longs,
5753 						  sizeof(unsigned long),
5754 						  GFP_KERNEL);
5755 			if (!phba->vpi_bmask) {
5756 				rc = -ENOMEM;
5757 				goto lpfc_sli_hba_setup_error;
5758 			}
5759 
5760 			phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5761 						sizeof(uint16_t),
5762 						GFP_KERNEL);
5763 			if (!phba->vpi_ids) {
5764 				kfree(phba->vpi_bmask);
5765 				rc = -ENOMEM;
5766 				goto lpfc_sli_hba_setup_error;
5767 			}
5768 			for (i = 0; i < phba->max_vpi; i++)
5769 				phba->vpi_ids[i] = i;
5770 		}
5771 	}
5772 
5773 	/* Init HBQs */
5774 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5775 		rc = lpfc_sli_hbq_setup(phba);
5776 		if (rc)
5777 			goto lpfc_sli_hba_setup_error;
5778 	}
5779 	spin_lock_irq(&phba->hbalock);
5780 	phba->sli.sli_flag |= LPFC_PROCESS_LA;
5781 	spin_unlock_irq(&phba->hbalock);
5782 
5783 	rc = lpfc_config_port_post(phba);
5784 	if (rc)
5785 		goto lpfc_sli_hba_setup_error;
5786 
5787 	return rc;
5788 
5789 lpfc_sli_hba_setup_error:
5790 	phba->link_state = LPFC_HBA_ERROR;
5791 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5792 			"0445 Firmware initialization failed\n");
5793 	return rc;
5794 }
5795 
5796 /**
5797  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5798  * @phba: Pointer to HBA context object.
5799  *
5800  * This function issue a dump mailbox command to read config region
5801  * 23 and parse the records in the region and populate driver
5802  * data structure.
5803  **/
5804 static int
lpfc_sli4_read_fcoe_params(struct lpfc_hba * phba)5805 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5806 {
5807 	LPFC_MBOXQ_t *mboxq;
5808 	struct lpfc_dmabuf *mp;
5809 	struct lpfc_mqe *mqe;
5810 	uint32_t data_length;
5811 	int rc;
5812 
5813 	/* Program the default value of vlan_id and fc_map */
5814 	phba->valid_vlan = 0;
5815 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5816 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5817 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5818 
5819 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5820 	if (!mboxq)
5821 		return -ENOMEM;
5822 
5823 	mqe = &mboxq->u.mqe;
5824 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5825 		rc = -ENOMEM;
5826 		goto out_free_mboxq;
5827 	}
5828 
5829 	mp = mboxq->ctx_buf;
5830 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5831 
5832 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5833 			"(%d):2571 Mailbox cmd x%x Status x%x "
5834 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5835 			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5836 			"CQ: x%x x%x x%x x%x\n",
5837 			mboxq->vport ? mboxq->vport->vpi : 0,
5838 			bf_get(lpfc_mqe_command, mqe),
5839 			bf_get(lpfc_mqe_status, mqe),
5840 			mqe->un.mb_words[0], mqe->un.mb_words[1],
5841 			mqe->un.mb_words[2], mqe->un.mb_words[3],
5842 			mqe->un.mb_words[4], mqe->un.mb_words[5],
5843 			mqe->un.mb_words[6], mqe->un.mb_words[7],
5844 			mqe->un.mb_words[8], mqe->un.mb_words[9],
5845 			mqe->un.mb_words[10], mqe->un.mb_words[11],
5846 			mqe->un.mb_words[12], mqe->un.mb_words[13],
5847 			mqe->un.mb_words[14], mqe->un.mb_words[15],
5848 			mqe->un.mb_words[16], mqe->un.mb_words[50],
5849 			mboxq->mcqe.word0,
5850 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
5851 			mboxq->mcqe.trailer);
5852 
5853 	if (rc) {
5854 		rc = -EIO;
5855 		goto out_free_mboxq;
5856 	}
5857 	data_length = mqe->un.mb_words[5];
5858 	if (data_length > DMP_RGN23_SIZE) {
5859 		rc = -EIO;
5860 		goto out_free_mboxq;
5861 	}
5862 
5863 	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5864 	rc = 0;
5865 
5866 out_free_mboxq:
5867 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
5868 	return rc;
5869 }
5870 
5871 /**
5872  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5873  * @phba: pointer to lpfc hba data structure.
5874  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5875  * @vpd: pointer to the memory to hold resulting port vpd data.
5876  * @vpd_size: On input, the number of bytes allocated to @vpd.
5877  *	      On output, the number of data bytes in @vpd.
5878  *
5879  * This routine executes a READ_REV SLI4 mailbox command.  In
5880  * addition, this routine gets the port vpd data.
5881  *
5882  * Return codes
5883  * 	0 - successful
5884  * 	-ENOMEM - could not allocated memory.
5885  **/
5886 static int
lpfc_sli4_read_rev(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint8_t * vpd,uint32_t * vpd_size)5887 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5888 		    uint8_t *vpd, uint32_t *vpd_size)
5889 {
5890 	int rc = 0;
5891 	uint32_t dma_size;
5892 	struct lpfc_dmabuf *dmabuf;
5893 	struct lpfc_mqe *mqe;
5894 
5895 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5896 	if (!dmabuf)
5897 		return -ENOMEM;
5898 
5899 	/*
5900 	 * Get a DMA buffer for the vpd data resulting from the READ_REV
5901 	 * mailbox command.
5902 	 */
5903 	dma_size = *vpd_size;
5904 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5905 					  &dmabuf->phys, GFP_KERNEL);
5906 	if (!dmabuf->virt) {
5907 		kfree(dmabuf);
5908 		return -ENOMEM;
5909 	}
5910 
5911 	/*
5912 	 * The SLI4 implementation of READ_REV conflicts at word1,
5913 	 * bits 31:16 and SLI4 adds vpd functionality not present
5914 	 * in SLI3.  This code corrects the conflicts.
5915 	 */
5916 	lpfc_read_rev(phba, mboxq);
5917 	mqe = &mboxq->u.mqe;
5918 	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5919 	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5920 	mqe->un.read_rev.word1 &= 0x0000FFFF;
5921 	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5922 	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5923 
5924 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5925 	if (rc) {
5926 		dma_free_coherent(&phba->pcidev->dev, dma_size,
5927 				  dmabuf->virt, dmabuf->phys);
5928 		kfree(dmabuf);
5929 		return -EIO;
5930 	}
5931 
5932 	/*
5933 	 * The available vpd length cannot be bigger than the
5934 	 * DMA buffer passed to the port.  Catch the less than
5935 	 * case and update the caller's size.
5936 	 */
5937 	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5938 		*vpd_size = mqe->un.read_rev.avail_vpd_len;
5939 
5940 	memcpy(vpd, dmabuf->virt, *vpd_size);
5941 
5942 	dma_free_coherent(&phba->pcidev->dev, dma_size,
5943 			  dmabuf->virt, dmabuf->phys);
5944 	kfree(dmabuf);
5945 	return 0;
5946 }
5947 
5948 /**
5949  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5950  * @phba: pointer to lpfc hba data structure.
5951  *
5952  * This routine retrieves SLI4 device physical port name this PCI function
5953  * is attached to.
5954  *
5955  * Return codes
5956  *      0 - successful
5957  *      otherwise - failed to retrieve controller attributes
5958  **/
5959 static int
lpfc_sli4_get_ctl_attr(struct lpfc_hba * phba)5960 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5961 {
5962 	LPFC_MBOXQ_t *mboxq;
5963 	struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5964 	struct lpfc_controller_attribute *cntl_attr;
5965 	void *virtaddr = NULL;
5966 	uint32_t alloclen, reqlen;
5967 	uint32_t shdr_status, shdr_add_status;
5968 	union lpfc_sli4_cfg_shdr *shdr;
5969 	int rc;
5970 
5971 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5972 	if (!mboxq)
5973 		return -ENOMEM;
5974 
5975 	/* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5976 	reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5977 	alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5978 			LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5979 			LPFC_SLI4_MBX_NEMBED);
5980 
5981 	if (alloclen < reqlen) {
5982 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5983 				"3084 Allocated DMA memory size (%d) is "
5984 				"less than the requested DMA memory size "
5985 				"(%d)\n", alloclen, reqlen);
5986 		rc = -ENOMEM;
5987 		goto out_free_mboxq;
5988 	}
5989 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5990 	virtaddr = mboxq->sge_array->addr[0];
5991 	mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5992 	shdr = &mbx_cntl_attr->cfg_shdr;
5993 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5994 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5995 	if (shdr_status || shdr_add_status || rc) {
5996 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5997 				"3085 Mailbox x%x (x%x/x%x) failed, "
5998 				"rc:x%x, status:x%x, add_status:x%x\n",
5999 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6000 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6001 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6002 				rc, shdr_status, shdr_add_status);
6003 		rc = -ENXIO;
6004 		goto out_free_mboxq;
6005 	}
6006 
6007 	cntl_attr = &mbx_cntl_attr->cntl_attr;
6008 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6009 	phba->sli4_hba.lnk_info.lnk_tp =
6010 		bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
6011 	phba->sli4_hba.lnk_info.lnk_no =
6012 		bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
6013 	phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
6014 	phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
6015 
6016 	memcpy(phba->BIOSVersion, cntl_attr->bios_ver_str,
6017 		sizeof(phba->BIOSVersion));
6018 	phba->BIOSVersion[sizeof(phba->BIOSVersion) - 1] = '\0';
6019 
6020 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6021 			"3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
6022 			"flash_id: x%02x, asic_rev: x%02x\n",
6023 			phba->sli4_hba.lnk_info.lnk_tp,
6024 			phba->sli4_hba.lnk_info.lnk_no,
6025 			phba->BIOSVersion, phba->sli4_hba.flash_id,
6026 			phba->sli4_hba.asic_rev);
6027 out_free_mboxq:
6028 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6029 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6030 	else
6031 		mempool_free(mboxq, phba->mbox_mem_pool);
6032 	return rc;
6033 }
6034 
6035 /**
6036  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
6037  * @phba: pointer to lpfc hba data structure.
6038  *
6039  * This routine retrieves SLI4 device physical port name this PCI function
6040  * is attached to.
6041  *
6042  * Return codes
6043  *      0 - successful
6044  *      otherwise - failed to retrieve physical port name
6045  **/
6046 static int
lpfc_sli4_retrieve_pport_name(struct lpfc_hba * phba)6047 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
6048 {
6049 	LPFC_MBOXQ_t *mboxq;
6050 	struct lpfc_mbx_get_port_name *get_port_name;
6051 	uint32_t shdr_status, shdr_add_status;
6052 	union lpfc_sli4_cfg_shdr *shdr;
6053 	char cport_name = 0;
6054 	int rc;
6055 
6056 	/* We assume nothing at this point */
6057 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6058 	phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
6059 
6060 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6061 	if (!mboxq)
6062 		return -ENOMEM;
6063 	/* obtain link type and link number via READ_CONFIG */
6064 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6065 	lpfc_sli4_read_config(phba);
6066 
6067 	if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG)
6068 		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
6069 
6070 	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
6071 		goto retrieve_ppname;
6072 
6073 	/* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
6074 	rc = lpfc_sli4_get_ctl_attr(phba);
6075 	if (rc)
6076 		goto out_free_mboxq;
6077 
6078 retrieve_ppname:
6079 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6080 		LPFC_MBOX_OPCODE_GET_PORT_NAME,
6081 		sizeof(struct lpfc_mbx_get_port_name) -
6082 		sizeof(struct lpfc_sli4_cfg_mhdr),
6083 		LPFC_SLI4_MBX_EMBED);
6084 	get_port_name = &mboxq->u.mqe.un.get_port_name;
6085 	shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
6086 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
6087 	bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
6088 		phba->sli4_hba.lnk_info.lnk_tp);
6089 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6090 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6091 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6092 	if (shdr_status || shdr_add_status || rc) {
6093 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6094 				"3087 Mailbox x%x (x%x/x%x) failed: "
6095 				"rc:x%x, status:x%x, add_status:x%x\n",
6096 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6097 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6098 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6099 				rc, shdr_status, shdr_add_status);
6100 		rc = -ENXIO;
6101 		goto out_free_mboxq;
6102 	}
6103 	switch (phba->sli4_hba.lnk_info.lnk_no) {
6104 	case LPFC_LINK_NUMBER_0:
6105 		cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6106 				&get_port_name->u.response);
6107 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6108 		break;
6109 	case LPFC_LINK_NUMBER_1:
6110 		cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6111 				&get_port_name->u.response);
6112 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6113 		break;
6114 	case LPFC_LINK_NUMBER_2:
6115 		cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6116 				&get_port_name->u.response);
6117 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6118 		break;
6119 	case LPFC_LINK_NUMBER_3:
6120 		cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6121 				&get_port_name->u.response);
6122 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6123 		break;
6124 	default:
6125 		break;
6126 	}
6127 
6128 	if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6129 		phba->Port[0] = cport_name;
6130 		phba->Port[1] = '\0';
6131 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6132 				"3091 SLI get port name: %s\n", phba->Port);
6133 	}
6134 
6135 out_free_mboxq:
6136 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6137 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6138 	else
6139 		mempool_free(mboxq, phba->mbox_mem_pool);
6140 	return rc;
6141 }
6142 
6143 /**
6144  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6145  * @phba: pointer to lpfc hba data structure.
6146  *
6147  * This routine is called to explicitly arm the SLI4 device's completion and
6148  * event queues
6149  **/
6150 static void
lpfc_sli4_arm_cqeq_intr(struct lpfc_hba * phba)6151 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6152 {
6153 	int qidx;
6154 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
6155 	struct lpfc_sli4_hdw_queue *qp;
6156 	struct lpfc_queue *eq;
6157 
6158 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6159 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
6160 	if (sli4_hba->nvmels_cq)
6161 		sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6162 					   LPFC_QUEUE_REARM);
6163 
6164 	if (sli4_hba->hdwq) {
6165 		/* Loop thru all Hardware Queues */
6166 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
6167 			qp = &sli4_hba->hdwq[qidx];
6168 			/* ARM the corresponding CQ */
6169 			sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
6170 						LPFC_QUEUE_REARM);
6171 		}
6172 
6173 		/* Loop thru all IRQ vectors */
6174 		for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6175 			eq = sli4_hba->hba_eq_hdl[qidx].eq;
6176 			/* ARM the corresponding EQ */
6177 			sli4_hba->sli4_write_eq_db(phba, eq,
6178 						   0, LPFC_QUEUE_REARM);
6179 		}
6180 	}
6181 
6182 	if (phba->nvmet_support) {
6183 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
6184 			sli4_hba->sli4_write_cq_db(phba,
6185 				sli4_hba->nvmet_cqset[qidx], 0,
6186 				LPFC_QUEUE_REARM);
6187 		}
6188 	}
6189 }
6190 
6191 /**
6192  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6193  * @phba: Pointer to HBA context object.
6194  * @type: The resource extent type.
6195  * @extnt_count: buffer to hold port available extent count.
6196  * @extnt_size: buffer to hold element count per extent.
6197  *
6198  * This function calls the port and retrievs the number of available
6199  * extents and their size for a particular extent type.
6200  *
6201  * Returns: 0 if successful.  Nonzero otherwise.
6202  **/
6203 int
lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_count,uint16_t * extnt_size)6204 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6205 			       uint16_t *extnt_count, uint16_t *extnt_size)
6206 {
6207 	int rc = 0;
6208 	uint32_t length;
6209 	uint32_t mbox_tmo;
6210 	struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6211 	LPFC_MBOXQ_t *mbox;
6212 
6213 	*extnt_count = 0;
6214 	*extnt_size = 0;
6215 
6216 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6217 	if (!mbox)
6218 		return -ENOMEM;
6219 
6220 	/* Find out how many extents are available for this resource type */
6221 	length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6222 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6223 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6224 			 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6225 			 length, LPFC_SLI4_MBX_EMBED);
6226 
6227 	/* Send an extents count of 0 - the GET doesn't use it. */
6228 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6229 					LPFC_SLI4_MBX_EMBED);
6230 	if (unlikely(rc)) {
6231 		rc = -EIO;
6232 		goto err_exit;
6233 	}
6234 
6235 	if (!phba->sli4_hba.intr_enable)
6236 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6237 	else {
6238 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6239 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6240 	}
6241 	if (unlikely(rc)) {
6242 		rc = -EIO;
6243 		goto err_exit;
6244 	}
6245 
6246 	rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6247 	if (bf_get(lpfc_mbox_hdr_status,
6248 		   &rsrc_info->header.cfg_shdr.response)) {
6249 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6250 				"2930 Failed to get resource extents "
6251 				"Status 0x%x Add'l Status 0x%x\n",
6252 				bf_get(lpfc_mbox_hdr_status,
6253 				       &rsrc_info->header.cfg_shdr.response),
6254 				bf_get(lpfc_mbox_hdr_add_status,
6255 				       &rsrc_info->header.cfg_shdr.response));
6256 		rc = -EIO;
6257 		goto err_exit;
6258 	}
6259 
6260 	*extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6261 			      &rsrc_info->u.rsp);
6262 	*extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6263 			     &rsrc_info->u.rsp);
6264 
6265 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6266 			"3162 Retrieved extents type-%d from port: count:%d, "
6267 			"size:%d\n", type, *extnt_count, *extnt_size);
6268 
6269 err_exit:
6270 	mempool_free(mbox, phba->mbox_mem_pool);
6271 	return rc;
6272 }
6273 
6274 /**
6275  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6276  * @phba: Pointer to HBA context object.
6277  * @type: The extent type to check.
6278  *
6279  * This function reads the current available extents from the port and checks
6280  * if the extent count or extent size has changed since the last access.
6281  * Callers use this routine post port reset to understand if there is a
6282  * extent reprovisioning requirement.
6283  *
6284  * Returns:
6285  *   -Error: error indicates problem.
6286  *   1: Extent count or size has changed.
6287  *   0: No changes.
6288  **/
6289 static int
lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type)6290 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6291 {
6292 	uint16_t curr_ext_cnt, rsrc_ext_cnt;
6293 	uint16_t size_diff, rsrc_ext_size;
6294 	int rc = 0;
6295 	struct lpfc_rsrc_blks *rsrc_entry;
6296 	struct list_head *rsrc_blk_list = NULL;
6297 
6298 	size_diff = 0;
6299 	curr_ext_cnt = 0;
6300 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6301 					    &rsrc_ext_cnt,
6302 					    &rsrc_ext_size);
6303 	if (unlikely(rc))
6304 		return -EIO;
6305 
6306 	switch (type) {
6307 	case LPFC_RSC_TYPE_FCOE_RPI:
6308 		rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6309 		break;
6310 	case LPFC_RSC_TYPE_FCOE_VPI:
6311 		rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6312 		break;
6313 	case LPFC_RSC_TYPE_FCOE_XRI:
6314 		rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6315 		break;
6316 	case LPFC_RSC_TYPE_FCOE_VFI:
6317 		rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6318 		break;
6319 	default:
6320 		break;
6321 	}
6322 
6323 	list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6324 		curr_ext_cnt++;
6325 		if (rsrc_entry->rsrc_size != rsrc_ext_size)
6326 			size_diff++;
6327 	}
6328 
6329 	if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6330 		rc = 1;
6331 
6332 	return rc;
6333 }
6334 
6335 /**
6336  * lpfc_sli4_cfg_post_extnts -
6337  * @phba: Pointer to HBA context object.
6338  * @extnt_cnt: number of available extents.
6339  * @type: the extent type (rpi, xri, vfi, vpi).
6340  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6341  * @mbox: pointer to the caller's allocated mailbox structure.
6342  *
6343  * This function executes the extents allocation request.  It also
6344  * takes care of the amount of memory needed to allocate or get the
6345  * allocated extents. It is the caller's responsibility to evaluate
6346  * the response.
6347  *
6348  * Returns:
6349  *   -Error:  Error value describes the condition found.
6350  *   0: if successful
6351  **/
6352 static int
lpfc_sli4_cfg_post_extnts(struct lpfc_hba * phba,uint16_t extnt_cnt,uint16_t type,bool * emb,LPFC_MBOXQ_t * mbox)6353 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6354 			  uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6355 {
6356 	int rc = 0;
6357 	uint32_t req_len;
6358 	uint32_t emb_len;
6359 	uint32_t alloc_len, mbox_tmo;
6360 
6361 	/* Calculate the total requested length of the dma memory */
6362 	req_len = extnt_cnt * sizeof(uint16_t);
6363 
6364 	/*
6365 	 * Calculate the size of an embedded mailbox.  The uint32_t
6366 	 * accounts for extents-specific word.
6367 	 */
6368 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6369 		sizeof(uint32_t);
6370 
6371 	/*
6372 	 * Presume the allocation and response will fit into an embedded
6373 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6374 	 */
6375 	*emb = LPFC_SLI4_MBX_EMBED;
6376 	if (req_len > emb_len) {
6377 		req_len = extnt_cnt * sizeof(uint16_t) +
6378 			sizeof(union lpfc_sli4_cfg_shdr) +
6379 			sizeof(uint32_t);
6380 		*emb = LPFC_SLI4_MBX_NEMBED;
6381 	}
6382 
6383 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6384 				     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6385 				     req_len, *emb);
6386 	if (alloc_len < req_len) {
6387 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6388 			"2982 Allocated DMA memory size (x%x) is "
6389 			"less than the requested DMA memory "
6390 			"size (x%x)\n", alloc_len, req_len);
6391 		return -ENOMEM;
6392 	}
6393 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6394 	if (unlikely(rc))
6395 		return -EIO;
6396 
6397 	if (!phba->sli4_hba.intr_enable)
6398 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6399 	else {
6400 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6401 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6402 	}
6403 
6404 	if (unlikely(rc))
6405 		rc = -EIO;
6406 	return rc;
6407 }
6408 
6409 /**
6410  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6411  * @phba: Pointer to HBA context object.
6412  * @type:  The resource extent type to allocate.
6413  *
6414  * This function allocates the number of elements for the specified
6415  * resource type.
6416  **/
6417 static int
lpfc_sli4_alloc_extent(struct lpfc_hba * phba,uint16_t type)6418 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6419 {
6420 	bool emb = false;
6421 	uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6422 	uint16_t rsrc_id, rsrc_start, j, k;
6423 	uint16_t *ids;
6424 	int i, rc;
6425 	unsigned long longs;
6426 	unsigned long *bmask;
6427 	struct lpfc_rsrc_blks *rsrc_blks;
6428 	LPFC_MBOXQ_t *mbox;
6429 	uint32_t length;
6430 	struct lpfc_id_range *id_array = NULL;
6431 	void *virtaddr = NULL;
6432 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6433 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6434 	struct list_head *ext_blk_list;
6435 
6436 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6437 					    &rsrc_cnt,
6438 					    &rsrc_size);
6439 	if (unlikely(rc))
6440 		return -EIO;
6441 
6442 	if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6443 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6444 			"3009 No available Resource Extents "
6445 			"for resource type 0x%x: Count: 0x%x, "
6446 			"Size 0x%x\n", type, rsrc_cnt,
6447 			rsrc_size);
6448 		return -ENOMEM;
6449 	}
6450 
6451 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6452 			"2903 Post resource extents type-0x%x: "
6453 			"count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6454 
6455 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6456 	if (!mbox)
6457 		return -ENOMEM;
6458 
6459 	rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6460 	if (unlikely(rc)) {
6461 		rc = -EIO;
6462 		goto err_exit;
6463 	}
6464 
6465 	/*
6466 	 * Figure out where the response is located.  Then get local pointers
6467 	 * to the response data.  The port does not guarantee to respond to
6468 	 * all extents counts request so update the local variable with the
6469 	 * allocated count from the port.
6470 	 */
6471 	if (emb == LPFC_SLI4_MBX_EMBED) {
6472 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6473 		id_array = &rsrc_ext->u.rsp.id[0];
6474 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6475 	} else {
6476 		virtaddr = mbox->sge_array->addr[0];
6477 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6478 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6479 		id_array = &n_rsrc->id;
6480 	}
6481 
6482 	longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6483 	rsrc_id_cnt = rsrc_cnt * rsrc_size;
6484 
6485 	/*
6486 	 * Based on the resource size and count, correct the base and max
6487 	 * resource values.
6488 	 */
6489 	length = sizeof(struct lpfc_rsrc_blks);
6490 	switch (type) {
6491 	case LPFC_RSC_TYPE_FCOE_RPI:
6492 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
6493 						   sizeof(unsigned long),
6494 						   GFP_KERNEL);
6495 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6496 			rc = -ENOMEM;
6497 			goto err_exit;
6498 		}
6499 		phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6500 						 sizeof(uint16_t),
6501 						 GFP_KERNEL);
6502 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
6503 			kfree(phba->sli4_hba.rpi_bmask);
6504 			rc = -ENOMEM;
6505 			goto err_exit;
6506 		}
6507 
6508 		/*
6509 		 * The next_rpi was initialized with the maximum available
6510 		 * count but the port may allocate a smaller number.  Catch
6511 		 * that case and update the next_rpi.
6512 		 */
6513 		phba->sli4_hba.next_rpi = rsrc_id_cnt;
6514 
6515 		/* Initialize local ptrs for common extent processing later. */
6516 		bmask = phba->sli4_hba.rpi_bmask;
6517 		ids = phba->sli4_hba.rpi_ids;
6518 		ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6519 		break;
6520 	case LPFC_RSC_TYPE_FCOE_VPI:
6521 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6522 					  GFP_KERNEL);
6523 		if (unlikely(!phba->vpi_bmask)) {
6524 			rc = -ENOMEM;
6525 			goto err_exit;
6526 		}
6527 		phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6528 					 GFP_KERNEL);
6529 		if (unlikely(!phba->vpi_ids)) {
6530 			kfree(phba->vpi_bmask);
6531 			rc = -ENOMEM;
6532 			goto err_exit;
6533 		}
6534 
6535 		/* Initialize local ptrs for common extent processing later. */
6536 		bmask = phba->vpi_bmask;
6537 		ids = phba->vpi_ids;
6538 		ext_blk_list = &phba->lpfc_vpi_blk_list;
6539 		break;
6540 	case LPFC_RSC_TYPE_FCOE_XRI:
6541 		phba->sli4_hba.xri_bmask = kcalloc(longs,
6542 						   sizeof(unsigned long),
6543 						   GFP_KERNEL);
6544 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
6545 			rc = -ENOMEM;
6546 			goto err_exit;
6547 		}
6548 		phba->sli4_hba.max_cfg_param.xri_used = 0;
6549 		phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6550 						 sizeof(uint16_t),
6551 						 GFP_KERNEL);
6552 		if (unlikely(!phba->sli4_hba.xri_ids)) {
6553 			kfree(phba->sli4_hba.xri_bmask);
6554 			rc = -ENOMEM;
6555 			goto err_exit;
6556 		}
6557 
6558 		/* Initialize local ptrs for common extent processing later. */
6559 		bmask = phba->sli4_hba.xri_bmask;
6560 		ids = phba->sli4_hba.xri_ids;
6561 		ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6562 		break;
6563 	case LPFC_RSC_TYPE_FCOE_VFI:
6564 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
6565 						   sizeof(unsigned long),
6566 						   GFP_KERNEL);
6567 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6568 			rc = -ENOMEM;
6569 			goto err_exit;
6570 		}
6571 		phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6572 						 sizeof(uint16_t),
6573 						 GFP_KERNEL);
6574 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
6575 			kfree(phba->sli4_hba.vfi_bmask);
6576 			rc = -ENOMEM;
6577 			goto err_exit;
6578 		}
6579 
6580 		/* Initialize local ptrs for common extent processing later. */
6581 		bmask = phba->sli4_hba.vfi_bmask;
6582 		ids = phba->sli4_hba.vfi_ids;
6583 		ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6584 		break;
6585 	default:
6586 		/* Unsupported Opcode.  Fail call. */
6587 		id_array = NULL;
6588 		bmask = NULL;
6589 		ids = NULL;
6590 		ext_blk_list = NULL;
6591 		goto err_exit;
6592 	}
6593 
6594 	/*
6595 	 * Complete initializing the extent configuration with the
6596 	 * allocated ids assigned to this function.  The bitmask serves
6597 	 * as an index into the array and manages the available ids.  The
6598 	 * array just stores the ids communicated to the port via the wqes.
6599 	 */
6600 	for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6601 		if ((i % 2) == 0)
6602 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6603 					 &id_array[k]);
6604 		else
6605 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6606 					 &id_array[k]);
6607 
6608 		rsrc_blks = kzalloc(length, GFP_KERNEL);
6609 		if (unlikely(!rsrc_blks)) {
6610 			rc = -ENOMEM;
6611 			kfree(bmask);
6612 			kfree(ids);
6613 			goto err_exit;
6614 		}
6615 		rsrc_blks->rsrc_start = rsrc_id;
6616 		rsrc_blks->rsrc_size = rsrc_size;
6617 		list_add_tail(&rsrc_blks->list, ext_blk_list);
6618 		rsrc_start = rsrc_id;
6619 		if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6620 			phba->sli4_hba.io_xri_start = rsrc_start +
6621 				lpfc_sli4_get_iocb_cnt(phba);
6622 		}
6623 
6624 		while (rsrc_id < (rsrc_start + rsrc_size)) {
6625 			ids[j] = rsrc_id;
6626 			rsrc_id++;
6627 			j++;
6628 		}
6629 		/* Entire word processed.  Get next word.*/
6630 		if ((i % 2) == 1)
6631 			k++;
6632 	}
6633  err_exit:
6634 	lpfc_sli4_mbox_cmd_free(phba, mbox);
6635 	return rc;
6636 }
6637 
6638 
6639 
6640 /**
6641  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6642  * @phba: Pointer to HBA context object.
6643  * @type: the extent's type.
6644  *
6645  * This function deallocates all extents of a particular resource type.
6646  * SLI4 does not allow for deallocating a particular extent range.  It
6647  * is the caller's responsibility to release all kernel memory resources.
6648  **/
6649 static int
lpfc_sli4_dealloc_extent(struct lpfc_hba * phba,uint16_t type)6650 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6651 {
6652 	int rc;
6653 	uint32_t length, mbox_tmo = 0;
6654 	LPFC_MBOXQ_t *mbox;
6655 	struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6656 	struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6657 
6658 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6659 	if (!mbox)
6660 		return -ENOMEM;
6661 
6662 	/*
6663 	 * This function sends an embedded mailbox because it only sends the
6664 	 * the resource type.  All extents of this type are released by the
6665 	 * port.
6666 	 */
6667 	length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6668 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6669 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6670 			 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6671 			 length, LPFC_SLI4_MBX_EMBED);
6672 
6673 	/* Send an extents count of 0 - the dealloc doesn't use it. */
6674 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6675 					LPFC_SLI4_MBX_EMBED);
6676 	if (unlikely(rc)) {
6677 		rc = -EIO;
6678 		goto out_free_mbox;
6679 	}
6680 	if (!phba->sli4_hba.intr_enable)
6681 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6682 	else {
6683 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6684 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6685 	}
6686 	if (unlikely(rc)) {
6687 		rc = -EIO;
6688 		goto out_free_mbox;
6689 	}
6690 
6691 	dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6692 	if (bf_get(lpfc_mbox_hdr_status,
6693 		   &dealloc_rsrc->header.cfg_shdr.response)) {
6694 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6695 				"2919 Failed to release resource extents "
6696 				"for type %d - Status 0x%x Add'l Status 0x%x. "
6697 				"Resource memory not released.\n",
6698 				type,
6699 				bf_get(lpfc_mbox_hdr_status,
6700 				    &dealloc_rsrc->header.cfg_shdr.response),
6701 				bf_get(lpfc_mbox_hdr_add_status,
6702 				    &dealloc_rsrc->header.cfg_shdr.response));
6703 		rc = -EIO;
6704 		goto out_free_mbox;
6705 	}
6706 
6707 	/* Release kernel memory resources for the specific type. */
6708 	switch (type) {
6709 	case LPFC_RSC_TYPE_FCOE_VPI:
6710 		kfree(phba->vpi_bmask);
6711 		kfree(phba->vpi_ids);
6712 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6713 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6714 				    &phba->lpfc_vpi_blk_list, list) {
6715 			list_del_init(&rsrc_blk->list);
6716 			kfree(rsrc_blk);
6717 		}
6718 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
6719 		break;
6720 	case LPFC_RSC_TYPE_FCOE_XRI:
6721 		kfree(phba->sli4_hba.xri_bmask);
6722 		kfree(phba->sli4_hba.xri_ids);
6723 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6724 				    &phba->sli4_hba.lpfc_xri_blk_list, list) {
6725 			list_del_init(&rsrc_blk->list);
6726 			kfree(rsrc_blk);
6727 		}
6728 		break;
6729 	case LPFC_RSC_TYPE_FCOE_VFI:
6730 		kfree(phba->sli4_hba.vfi_bmask);
6731 		kfree(phba->sli4_hba.vfi_ids);
6732 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6733 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6734 				    &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6735 			list_del_init(&rsrc_blk->list);
6736 			kfree(rsrc_blk);
6737 		}
6738 		break;
6739 	case LPFC_RSC_TYPE_FCOE_RPI:
6740 		/* RPI bitmask and physical id array are cleaned up earlier. */
6741 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6742 				    &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6743 			list_del_init(&rsrc_blk->list);
6744 			kfree(rsrc_blk);
6745 		}
6746 		break;
6747 	default:
6748 		break;
6749 	}
6750 
6751 	bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6752 
6753  out_free_mbox:
6754 	mempool_free(mbox, phba->mbox_mem_pool);
6755 	return rc;
6756 }
6757 
6758 static void
lpfc_set_features(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox,uint32_t feature)6759 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6760 		  uint32_t feature)
6761 {
6762 	uint32_t len;
6763 	u32 sig_freq = 0;
6764 
6765 	len = sizeof(struct lpfc_mbx_set_feature) -
6766 		sizeof(struct lpfc_sli4_cfg_mhdr);
6767 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6768 			 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6769 			 LPFC_SLI4_MBX_EMBED);
6770 
6771 	switch (feature) {
6772 	case LPFC_SET_UE_RECOVERY:
6773 		bf_set(lpfc_mbx_set_feature_UER,
6774 		       &mbox->u.mqe.un.set_feature, 1);
6775 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6776 		mbox->u.mqe.un.set_feature.param_len = 8;
6777 		break;
6778 	case LPFC_SET_MDS_DIAGS:
6779 		bf_set(lpfc_mbx_set_feature_mds,
6780 		       &mbox->u.mqe.un.set_feature, 1);
6781 		bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6782 		       &mbox->u.mqe.un.set_feature, 1);
6783 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6784 		mbox->u.mqe.un.set_feature.param_len = 8;
6785 		break;
6786 	case LPFC_SET_CGN_SIGNAL:
6787 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
6788 			sig_freq = 0;
6789 		else
6790 			sig_freq = phba->cgn_sig_freq;
6791 
6792 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6793 			bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6794 			       &mbox->u.mqe.un.set_feature, sig_freq);
6795 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6796 			       &mbox->u.mqe.un.set_feature, sig_freq);
6797 		}
6798 
6799 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6800 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6801 			       &mbox->u.mqe.un.set_feature, sig_freq);
6802 
6803 		if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6804 		    phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6805 			sig_freq = 0;
6806 		else
6807 			sig_freq = lpfc_acqe_cgn_frequency;
6808 
6809 		bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6810 		       &mbox->u.mqe.un.set_feature, sig_freq);
6811 
6812 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6813 		mbox->u.mqe.un.set_feature.param_len = 12;
6814 		break;
6815 	case LPFC_SET_DUAL_DUMP:
6816 		bf_set(lpfc_mbx_set_feature_dd,
6817 		       &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6818 		bf_set(lpfc_mbx_set_feature_ddquery,
6819 		       &mbox->u.mqe.un.set_feature, 0);
6820 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6821 		mbox->u.mqe.un.set_feature.param_len = 4;
6822 		break;
6823 	case LPFC_SET_ENABLE_MI:
6824 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6825 		mbox->u.mqe.un.set_feature.param_len = 4;
6826 		bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6827 		       phba->pport->cfg_lun_queue_depth);
6828 		bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6829 		       phba->sli4_hba.pc_sli4_params.mi_ver);
6830 		break;
6831 	case LPFC_SET_LD_SIGNAL:
6832 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_LD_SIGNAL;
6833 		mbox->u.mqe.un.set_feature.param_len = 16;
6834 		bf_set(lpfc_mbx_set_feature_lds_qry,
6835 		       &mbox->u.mqe.un.set_feature, LPFC_QUERY_LDS_OP);
6836 		break;
6837 	case LPFC_SET_ENABLE_CMF:
6838 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6839 		mbox->u.mqe.un.set_feature.param_len = 4;
6840 		bf_set(lpfc_mbx_set_feature_cmf,
6841 		       &mbox->u.mqe.un.set_feature, 1);
6842 		break;
6843 	}
6844 	return;
6845 }
6846 
6847 /**
6848  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6849  * @phba: Pointer to HBA context object.
6850  *
6851  * Disable FW logging into host memory on the adapter. To
6852  * be done before reading logs from the host memory.
6853  **/
6854 void
lpfc_ras_stop_fwlog(struct lpfc_hba * phba)6855 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6856 {
6857 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6858 
6859 	spin_lock_irq(&phba->ras_fwlog_lock);
6860 	ras_fwlog->state = INACTIVE;
6861 	spin_unlock_irq(&phba->ras_fwlog_lock);
6862 
6863 	/* Disable FW logging to host memory */
6864 	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6865 	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6866 
6867 	/* Wait 10ms for firmware to stop using DMA buffer */
6868 	usleep_range(10 * 1000, 20 * 1000);
6869 }
6870 
6871 /**
6872  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6873  * @phba: Pointer to HBA context object.
6874  *
6875  * This function is called to free memory allocated for RAS FW logging
6876  * support in the driver.
6877  **/
6878 void
lpfc_sli4_ras_dma_free(struct lpfc_hba * phba)6879 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6880 {
6881 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6882 	struct lpfc_dmabuf *dmabuf, *next;
6883 
6884 	if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6885 		list_for_each_entry_safe(dmabuf, next,
6886 				    &ras_fwlog->fwlog_buff_list,
6887 				    list) {
6888 			list_del(&dmabuf->list);
6889 			dma_free_coherent(&phba->pcidev->dev,
6890 					  LPFC_RAS_MAX_ENTRY_SIZE,
6891 					  dmabuf->virt, dmabuf->phys);
6892 			kfree(dmabuf);
6893 		}
6894 	}
6895 
6896 	if (ras_fwlog->lwpd.virt) {
6897 		dma_free_coherent(&phba->pcidev->dev,
6898 				  sizeof(uint32_t) * 2,
6899 				  ras_fwlog->lwpd.virt,
6900 				  ras_fwlog->lwpd.phys);
6901 		ras_fwlog->lwpd.virt = NULL;
6902 	}
6903 
6904 	spin_lock_irq(&phba->ras_fwlog_lock);
6905 	ras_fwlog->state = INACTIVE;
6906 	spin_unlock_irq(&phba->ras_fwlog_lock);
6907 }
6908 
6909 /**
6910  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6911  * @phba: Pointer to HBA context object.
6912  * @fwlog_buff_count: Count of buffers to be created.
6913  *
6914  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6915  * to update FW log is posted to the adapter.
6916  * Buffer count is calculated based on module param ras_fwlog_buffsize
6917  * Size of each buffer posted to FW is 64K.
6918  **/
6919 
6920 static int
lpfc_sli4_ras_dma_alloc(struct lpfc_hba * phba,uint32_t fwlog_buff_count)6921 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6922 			uint32_t fwlog_buff_count)
6923 {
6924 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6925 	struct lpfc_dmabuf *dmabuf;
6926 	int rc = 0, i = 0;
6927 
6928 	/* Initialize List */
6929 	INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6930 
6931 	/* Allocate memory for the LWPD */
6932 	ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6933 					    sizeof(uint32_t) * 2,
6934 					    &ras_fwlog->lwpd.phys,
6935 					    GFP_KERNEL);
6936 	if (!ras_fwlog->lwpd.virt) {
6937 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6938 				"6185 LWPD Memory Alloc Failed\n");
6939 
6940 		return -ENOMEM;
6941 	}
6942 
6943 	ras_fwlog->fw_buffcount = fwlog_buff_count;
6944 	for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6945 		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6946 				 GFP_KERNEL);
6947 		if (!dmabuf) {
6948 			rc = -ENOMEM;
6949 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6950 					"6186 Memory Alloc failed FW logging");
6951 			goto free_mem;
6952 		}
6953 
6954 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6955 						  LPFC_RAS_MAX_ENTRY_SIZE,
6956 						  &dmabuf->phys, GFP_KERNEL);
6957 		if (!dmabuf->virt) {
6958 			kfree(dmabuf);
6959 			rc = -ENOMEM;
6960 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6961 					"6187 DMA Alloc Failed FW logging");
6962 			goto free_mem;
6963 		}
6964 		dmabuf->buffer_tag = i;
6965 		list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6966 	}
6967 
6968 free_mem:
6969 	if (rc)
6970 		lpfc_sli4_ras_dma_free(phba);
6971 
6972 	return rc;
6973 }
6974 
6975 /**
6976  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6977  * @phba: pointer to lpfc hba data structure.
6978  * @pmb: pointer to the driver internal queue element for mailbox command.
6979  *
6980  * Completion handler for driver's RAS MBX command to the device.
6981  **/
6982 static void
lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)6983 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6984 {
6985 	MAILBOX_t *mb;
6986 	union lpfc_sli4_cfg_shdr *shdr;
6987 	uint32_t shdr_status, shdr_add_status;
6988 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6989 
6990 	mb = &pmb->u.mb;
6991 
6992 	shdr = (union lpfc_sli4_cfg_shdr *)
6993 		&pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6994 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6995 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6996 
6997 	if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6998 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6999 				"6188 FW LOG mailbox "
7000 				"completed with status x%x add_status x%x,"
7001 				" mbx status x%x\n",
7002 				shdr_status, shdr_add_status, mb->mbxStatus);
7003 
7004 		ras_fwlog->ras_hwsupport = false;
7005 		goto disable_ras;
7006 	}
7007 
7008 	spin_lock_irq(&phba->ras_fwlog_lock);
7009 	ras_fwlog->state = ACTIVE;
7010 	spin_unlock_irq(&phba->ras_fwlog_lock);
7011 	mempool_free(pmb, phba->mbox_mem_pool);
7012 
7013 	return;
7014 
7015 disable_ras:
7016 	/* Free RAS DMA memory */
7017 	lpfc_sli4_ras_dma_free(phba);
7018 	mempool_free(pmb, phba->mbox_mem_pool);
7019 }
7020 
7021 /**
7022  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
7023  * @phba: pointer to lpfc hba data structure.
7024  * @fwlog_level: Logging verbosity level.
7025  * @fwlog_enable: Enable/Disable logging.
7026  *
7027  * Initialize memory and post mailbox command to enable FW logging in host
7028  * memory.
7029  **/
7030 int
lpfc_sli4_ras_fwlog_init(struct lpfc_hba * phba,uint32_t fwlog_level,uint32_t fwlog_enable)7031 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
7032 			 uint32_t fwlog_level,
7033 			 uint32_t fwlog_enable)
7034 {
7035 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
7036 	struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
7037 	struct lpfc_dmabuf *dmabuf;
7038 	LPFC_MBOXQ_t *mbox;
7039 	uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
7040 	int rc = 0;
7041 
7042 	spin_lock_irq(&phba->ras_fwlog_lock);
7043 	ras_fwlog->state = INACTIVE;
7044 	spin_unlock_irq(&phba->ras_fwlog_lock);
7045 
7046 	fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
7047 			  phba->cfg_ras_fwlog_buffsize);
7048 	fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
7049 
7050 	/*
7051 	 * If re-enabling FW logging support use earlier allocated
7052 	 * DMA buffers while posting MBX command.
7053 	 **/
7054 	if (!ras_fwlog->lwpd.virt) {
7055 		rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
7056 		if (rc) {
7057 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7058 					"6189 FW Log Memory Allocation Failed");
7059 			return rc;
7060 		}
7061 	}
7062 
7063 	/* Setup Mailbox command */
7064 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7065 	if (!mbox) {
7066 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7067 				"6190 RAS MBX Alloc Failed");
7068 		rc = -ENOMEM;
7069 		goto mem_free;
7070 	}
7071 
7072 	ras_fwlog->fw_loglevel = fwlog_level;
7073 	len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
7074 		sizeof(struct lpfc_sli4_cfg_mhdr));
7075 
7076 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
7077 			 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
7078 			 len, LPFC_SLI4_MBX_EMBED);
7079 
7080 	mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
7081 	bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
7082 	       fwlog_enable);
7083 	bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
7084 	       ras_fwlog->fw_loglevel);
7085 	bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
7086 	       ras_fwlog->fw_buffcount);
7087 	bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
7088 	       LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
7089 
7090 	/* Update DMA buffer address */
7091 	list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
7092 		memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
7093 
7094 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
7095 			putPaddrLow(dmabuf->phys);
7096 
7097 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
7098 			putPaddrHigh(dmabuf->phys);
7099 	}
7100 
7101 	/* Update LPWD address */
7102 	mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7103 	mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7104 
7105 	spin_lock_irq(&phba->ras_fwlog_lock);
7106 	ras_fwlog->state = REG_INPROGRESS;
7107 	spin_unlock_irq(&phba->ras_fwlog_lock);
7108 	mbox->vport = phba->pport;
7109 	mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7110 
7111 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7112 
7113 	if (rc == MBX_NOT_FINISHED) {
7114 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7115 				"6191 FW-Log Mailbox failed. "
7116 				"status %d mbxStatus : x%x", rc,
7117 				bf_get(lpfc_mqe_status, &mbox->u.mqe));
7118 		mempool_free(mbox, phba->mbox_mem_pool);
7119 		rc = -EIO;
7120 		goto mem_free;
7121 	} else
7122 		rc = 0;
7123 mem_free:
7124 	if (rc)
7125 		lpfc_sli4_ras_dma_free(phba);
7126 
7127 	return rc;
7128 }
7129 
7130 /**
7131  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7132  * @phba: Pointer to HBA context object.
7133  *
7134  * Check if RAS is supported on the adapter and initialize it.
7135  **/
7136 void
lpfc_sli4_ras_setup(struct lpfc_hba * phba)7137 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7138 {
7139 	/* Check RAS FW Log needs to be enabled or not */
7140 	if (lpfc_check_fwlog_support(phba))
7141 		return;
7142 
7143 	lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7144 				 LPFC_RAS_ENABLE_LOGGING);
7145 }
7146 
7147 /**
7148  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7149  * @phba: Pointer to HBA context object.
7150  *
7151  * This function allocates all SLI4 resource identifiers.
7152  **/
7153 int
lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba * phba)7154 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7155 {
7156 	int i, rc, error = 0;
7157 	uint16_t count, base;
7158 	unsigned long longs;
7159 
7160 	if (!phba->sli4_hba.rpi_hdrs_in_use)
7161 		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
7162 	if (phba->sli4_hba.extents_in_use) {
7163 		/*
7164 		 * The port supports resource extents. The XRI, VPI, VFI, RPI
7165 		 * resource extent count must be read and allocated before
7166 		 * provisioning the resource id arrays.
7167 		 */
7168 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7169 		    LPFC_IDX_RSRC_RDY) {
7170 			/*
7171 			 * Extent-based resources are set - the driver could
7172 			 * be in a port reset. Figure out if any corrective
7173 			 * actions need to be taken.
7174 			 */
7175 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7176 						 LPFC_RSC_TYPE_FCOE_VFI);
7177 			if (rc != 0)
7178 				error++;
7179 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7180 						 LPFC_RSC_TYPE_FCOE_VPI);
7181 			if (rc != 0)
7182 				error++;
7183 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7184 						 LPFC_RSC_TYPE_FCOE_XRI);
7185 			if (rc != 0)
7186 				error++;
7187 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7188 						 LPFC_RSC_TYPE_FCOE_RPI);
7189 			if (rc != 0)
7190 				error++;
7191 
7192 			/*
7193 			 * It's possible that the number of resources
7194 			 * provided to this port instance changed between
7195 			 * resets.  Detect this condition and reallocate
7196 			 * resources.  Otherwise, there is no action.
7197 			 */
7198 			if (error) {
7199 				lpfc_printf_log(phba, KERN_INFO,
7200 						LOG_MBOX | LOG_INIT,
7201 						"2931 Detected extent resource "
7202 						"change.  Reallocating all "
7203 						"extents.\n");
7204 				rc = lpfc_sli4_dealloc_extent(phba,
7205 						 LPFC_RSC_TYPE_FCOE_VFI);
7206 				rc = lpfc_sli4_dealloc_extent(phba,
7207 						 LPFC_RSC_TYPE_FCOE_VPI);
7208 				rc = lpfc_sli4_dealloc_extent(phba,
7209 						 LPFC_RSC_TYPE_FCOE_XRI);
7210 				rc = lpfc_sli4_dealloc_extent(phba,
7211 						 LPFC_RSC_TYPE_FCOE_RPI);
7212 			} else
7213 				return 0;
7214 		}
7215 
7216 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7217 		if (unlikely(rc))
7218 			goto err_exit;
7219 
7220 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7221 		if (unlikely(rc))
7222 			goto err_exit;
7223 
7224 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7225 		if (unlikely(rc))
7226 			goto err_exit;
7227 
7228 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7229 		if (unlikely(rc))
7230 			goto err_exit;
7231 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7232 		       LPFC_IDX_RSRC_RDY);
7233 		return rc;
7234 	} else {
7235 		/*
7236 		 * The port does not support resource extents.  The XRI, VPI,
7237 		 * VFI, RPI resource ids were determined from READ_CONFIG.
7238 		 * Just allocate the bitmasks and provision the resource id
7239 		 * arrays.  If a port reset is active, the resources don't
7240 		 * need any action - just exit.
7241 		 */
7242 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7243 		    LPFC_IDX_RSRC_RDY) {
7244 			lpfc_sli4_dealloc_resource_identifiers(phba);
7245 			lpfc_sli4_remove_rpis(phba);
7246 		}
7247 		/* RPIs. */
7248 		count = phba->sli4_hba.max_cfg_param.max_rpi;
7249 		if (count <= 0) {
7250 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7251 					"3279 Invalid provisioning of "
7252 					"rpi:%d\n", count);
7253 			rc = -EINVAL;
7254 			goto err_exit;
7255 		}
7256 		base = phba->sli4_hba.max_cfg_param.rpi_base;
7257 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7258 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
7259 						   sizeof(unsigned long),
7260 						   GFP_KERNEL);
7261 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7262 			rc = -ENOMEM;
7263 			goto err_exit;
7264 		}
7265 		phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
7266 						 GFP_KERNEL);
7267 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
7268 			rc = -ENOMEM;
7269 			goto free_rpi_bmask;
7270 		}
7271 
7272 		for (i = 0; i < count; i++)
7273 			phba->sli4_hba.rpi_ids[i] = base + i;
7274 
7275 		/* VPIs. */
7276 		count = phba->sli4_hba.max_cfg_param.max_vpi;
7277 		if (count <= 0) {
7278 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7279 					"3280 Invalid provisioning of "
7280 					"vpi:%d\n", count);
7281 			rc = -EINVAL;
7282 			goto free_rpi_ids;
7283 		}
7284 		base = phba->sli4_hba.max_cfg_param.vpi_base;
7285 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7286 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
7287 					  GFP_KERNEL);
7288 		if (unlikely(!phba->vpi_bmask)) {
7289 			rc = -ENOMEM;
7290 			goto free_rpi_ids;
7291 		}
7292 		phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
7293 					GFP_KERNEL);
7294 		if (unlikely(!phba->vpi_ids)) {
7295 			rc = -ENOMEM;
7296 			goto free_vpi_bmask;
7297 		}
7298 
7299 		for (i = 0; i < count; i++)
7300 			phba->vpi_ids[i] = base + i;
7301 
7302 		/* XRIs. */
7303 		count = phba->sli4_hba.max_cfg_param.max_xri;
7304 		if (count <= 0) {
7305 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7306 					"3281 Invalid provisioning of "
7307 					"xri:%d\n", count);
7308 			rc = -EINVAL;
7309 			goto free_vpi_ids;
7310 		}
7311 		base = phba->sli4_hba.max_cfg_param.xri_base;
7312 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7313 		phba->sli4_hba.xri_bmask = kcalloc(longs,
7314 						   sizeof(unsigned long),
7315 						   GFP_KERNEL);
7316 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
7317 			rc = -ENOMEM;
7318 			goto free_vpi_ids;
7319 		}
7320 		phba->sli4_hba.max_cfg_param.xri_used = 0;
7321 		phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
7322 						 GFP_KERNEL);
7323 		if (unlikely(!phba->sli4_hba.xri_ids)) {
7324 			rc = -ENOMEM;
7325 			goto free_xri_bmask;
7326 		}
7327 
7328 		for (i = 0; i < count; i++)
7329 			phba->sli4_hba.xri_ids[i] = base + i;
7330 
7331 		/* VFIs. */
7332 		count = phba->sli4_hba.max_cfg_param.max_vfi;
7333 		if (count <= 0) {
7334 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7335 					"3282 Invalid provisioning of "
7336 					"vfi:%d\n", count);
7337 			rc = -EINVAL;
7338 			goto free_xri_ids;
7339 		}
7340 		base = phba->sli4_hba.max_cfg_param.vfi_base;
7341 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7342 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
7343 						   sizeof(unsigned long),
7344 						   GFP_KERNEL);
7345 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7346 			rc = -ENOMEM;
7347 			goto free_xri_ids;
7348 		}
7349 		phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
7350 						 GFP_KERNEL);
7351 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
7352 			rc = -ENOMEM;
7353 			goto free_vfi_bmask;
7354 		}
7355 
7356 		for (i = 0; i < count; i++)
7357 			phba->sli4_hba.vfi_ids[i] = base + i;
7358 
7359 		/*
7360 		 * Mark all resources ready.  An HBA reset doesn't need
7361 		 * to reset the initialization.
7362 		 */
7363 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7364 		       LPFC_IDX_RSRC_RDY);
7365 		return 0;
7366 	}
7367 
7368  free_vfi_bmask:
7369 	kfree(phba->sli4_hba.vfi_bmask);
7370 	phba->sli4_hba.vfi_bmask = NULL;
7371  free_xri_ids:
7372 	kfree(phba->sli4_hba.xri_ids);
7373 	phba->sli4_hba.xri_ids = NULL;
7374  free_xri_bmask:
7375 	kfree(phba->sli4_hba.xri_bmask);
7376 	phba->sli4_hba.xri_bmask = NULL;
7377  free_vpi_ids:
7378 	kfree(phba->vpi_ids);
7379 	phba->vpi_ids = NULL;
7380  free_vpi_bmask:
7381 	kfree(phba->vpi_bmask);
7382 	phba->vpi_bmask = NULL;
7383  free_rpi_ids:
7384 	kfree(phba->sli4_hba.rpi_ids);
7385 	phba->sli4_hba.rpi_ids = NULL;
7386  free_rpi_bmask:
7387 	kfree(phba->sli4_hba.rpi_bmask);
7388 	phba->sli4_hba.rpi_bmask = NULL;
7389  err_exit:
7390 	return rc;
7391 }
7392 
7393 /**
7394  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7395  * @phba: Pointer to HBA context object.
7396  *
7397  * This function allocates the number of elements for the specified
7398  * resource type.
7399  **/
7400 int
lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba * phba)7401 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7402 {
7403 	if (phba->sli4_hba.extents_in_use) {
7404 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7405 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7406 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7407 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7408 	} else {
7409 		kfree(phba->vpi_bmask);
7410 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
7411 		kfree(phba->vpi_ids);
7412 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7413 		kfree(phba->sli4_hba.xri_bmask);
7414 		kfree(phba->sli4_hba.xri_ids);
7415 		kfree(phba->sli4_hba.vfi_bmask);
7416 		kfree(phba->sli4_hba.vfi_ids);
7417 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7418 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7419 	}
7420 
7421 	return 0;
7422 }
7423 
7424 /**
7425  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7426  * @phba: Pointer to HBA context object.
7427  * @type: The resource extent type.
7428  * @extnt_cnt: buffer to hold port extent count response
7429  * @extnt_size: buffer to hold port extent size response.
7430  *
7431  * This function calls the port to read the host allocated extents
7432  * for a particular type.
7433  **/
7434 int
lpfc_sli4_get_allocated_extnts(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_cnt,uint16_t * extnt_size)7435 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7436 			       uint16_t *extnt_cnt, uint16_t *extnt_size)
7437 {
7438 	bool emb;
7439 	int rc = 0;
7440 	uint16_t curr_blks = 0;
7441 	uint32_t req_len, emb_len;
7442 	uint32_t alloc_len, mbox_tmo;
7443 	struct list_head *blk_list_head;
7444 	struct lpfc_rsrc_blks *rsrc_blk;
7445 	LPFC_MBOXQ_t *mbox;
7446 	void *virtaddr = NULL;
7447 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7448 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7449 	union  lpfc_sli4_cfg_shdr *shdr;
7450 
7451 	switch (type) {
7452 	case LPFC_RSC_TYPE_FCOE_VPI:
7453 		blk_list_head = &phba->lpfc_vpi_blk_list;
7454 		break;
7455 	case LPFC_RSC_TYPE_FCOE_XRI:
7456 		blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7457 		break;
7458 	case LPFC_RSC_TYPE_FCOE_VFI:
7459 		blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7460 		break;
7461 	case LPFC_RSC_TYPE_FCOE_RPI:
7462 		blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7463 		break;
7464 	default:
7465 		return -EIO;
7466 	}
7467 
7468 	/* Count the number of extents currently allocatd for this type. */
7469 	list_for_each_entry(rsrc_blk, blk_list_head, list) {
7470 		if (curr_blks == 0) {
7471 			/*
7472 			 * The GET_ALLOCATED mailbox does not return the size,
7473 			 * just the count.  The size should be just the size
7474 			 * stored in the current allocated block and all sizes
7475 			 * for an extent type are the same so set the return
7476 			 * value now.
7477 			 */
7478 			*extnt_size = rsrc_blk->rsrc_size;
7479 		}
7480 		curr_blks++;
7481 	}
7482 
7483 	/*
7484 	 * Calculate the size of an embedded mailbox.  The uint32_t
7485 	 * accounts for extents-specific word.
7486 	 */
7487 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7488 		sizeof(uint32_t);
7489 
7490 	/*
7491 	 * Presume the allocation and response will fit into an embedded
7492 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7493 	 */
7494 	emb = LPFC_SLI4_MBX_EMBED;
7495 	req_len = emb_len;
7496 	if (req_len > emb_len) {
7497 		req_len = curr_blks * sizeof(uint16_t) +
7498 			sizeof(union lpfc_sli4_cfg_shdr) +
7499 			sizeof(uint32_t);
7500 		emb = LPFC_SLI4_MBX_NEMBED;
7501 	}
7502 
7503 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7504 	if (!mbox)
7505 		return -ENOMEM;
7506 	memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7507 
7508 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7509 				     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7510 				     req_len, emb);
7511 	if (alloc_len < req_len) {
7512 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7513 			"2983 Allocated DMA memory size (x%x) is "
7514 			"less than the requested DMA memory "
7515 			"size (x%x)\n", alloc_len, req_len);
7516 		rc = -ENOMEM;
7517 		goto err_exit;
7518 	}
7519 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7520 	if (unlikely(rc)) {
7521 		rc = -EIO;
7522 		goto err_exit;
7523 	}
7524 
7525 	if (!phba->sli4_hba.intr_enable)
7526 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7527 	else {
7528 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7529 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7530 	}
7531 
7532 	if (unlikely(rc)) {
7533 		rc = -EIO;
7534 		goto err_exit;
7535 	}
7536 
7537 	/*
7538 	 * Figure out where the response is located.  Then get local pointers
7539 	 * to the response data.  The port does not guarantee to respond to
7540 	 * all extents counts request so update the local variable with the
7541 	 * allocated count from the port.
7542 	 */
7543 	if (emb == LPFC_SLI4_MBX_EMBED) {
7544 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7545 		shdr = &rsrc_ext->header.cfg_shdr;
7546 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7547 	} else {
7548 		virtaddr = mbox->sge_array->addr[0];
7549 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7550 		shdr = &n_rsrc->cfg_shdr;
7551 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7552 	}
7553 
7554 	if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7555 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7556 			"2984 Failed to read allocated resources "
7557 			"for type %d - Status 0x%x Add'l Status 0x%x.\n",
7558 			type,
7559 			bf_get(lpfc_mbox_hdr_status, &shdr->response),
7560 			bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7561 		rc = -EIO;
7562 		goto err_exit;
7563 	}
7564  err_exit:
7565 	lpfc_sli4_mbox_cmd_free(phba, mbox);
7566 	return rc;
7567 }
7568 
7569 /**
7570  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7571  * @phba: pointer to lpfc hba data structure.
7572  * @sgl_list: linked link of sgl buffers to post
7573  * @cnt: number of linked list buffers
7574  *
7575  * This routine walks the list of buffers that have been allocated and
7576  * repost them to the port by using SGL block post. This is needed after a
7577  * pci_function_reset/warm_start or start. It attempts to construct blocks
7578  * of buffer sgls which contains contiguous xris and uses the non-embedded
7579  * SGL block post mailbox commands to post them to the port. For single
7580  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7581  * mailbox command for posting.
7582  *
7583  * Returns: 0 = success, non-zero failure.
7584  **/
7585 static int
lpfc_sli4_repost_sgl_list(struct lpfc_hba * phba,struct list_head * sgl_list,int cnt)7586 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7587 			  struct list_head *sgl_list, int cnt)
7588 {
7589 	struct lpfc_sglq *sglq_entry = NULL;
7590 	struct lpfc_sglq *sglq_entry_next = NULL;
7591 	struct lpfc_sglq *sglq_entry_first = NULL;
7592 	int status = 0, total_cnt;
7593 	int post_cnt = 0, num_posted = 0, block_cnt = 0;
7594 	int last_xritag = NO_XRI;
7595 	LIST_HEAD(prep_sgl_list);
7596 	LIST_HEAD(blck_sgl_list);
7597 	LIST_HEAD(allc_sgl_list);
7598 	LIST_HEAD(post_sgl_list);
7599 	LIST_HEAD(free_sgl_list);
7600 
7601 	spin_lock_irq(&phba->hbalock);
7602 	spin_lock(&phba->sli4_hba.sgl_list_lock);
7603 	list_splice_init(sgl_list, &allc_sgl_list);
7604 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
7605 	spin_unlock_irq(&phba->hbalock);
7606 
7607 	total_cnt = cnt;
7608 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7609 				 &allc_sgl_list, list) {
7610 		list_del_init(&sglq_entry->list);
7611 		block_cnt++;
7612 		if ((last_xritag != NO_XRI) &&
7613 		    (sglq_entry->sli4_xritag != last_xritag + 1)) {
7614 			/* a hole in xri block, form a sgl posting block */
7615 			list_splice_init(&prep_sgl_list, &blck_sgl_list);
7616 			post_cnt = block_cnt - 1;
7617 			/* prepare list for next posting block */
7618 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7619 			block_cnt = 1;
7620 		} else {
7621 			/* prepare list for next posting block */
7622 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7623 			/* enough sgls for non-embed sgl mbox command */
7624 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7625 				list_splice_init(&prep_sgl_list,
7626 						 &blck_sgl_list);
7627 				post_cnt = block_cnt;
7628 				block_cnt = 0;
7629 			}
7630 		}
7631 		num_posted++;
7632 
7633 		/* keep track of last sgl's xritag */
7634 		last_xritag = sglq_entry->sli4_xritag;
7635 
7636 		/* end of repost sgl list condition for buffers */
7637 		if (num_posted == total_cnt) {
7638 			if (post_cnt == 0) {
7639 				list_splice_init(&prep_sgl_list,
7640 						 &blck_sgl_list);
7641 				post_cnt = block_cnt;
7642 			} else if (block_cnt == 1) {
7643 				status = lpfc_sli4_post_sgl(phba,
7644 						sglq_entry->phys, 0,
7645 						sglq_entry->sli4_xritag);
7646 				if (!status) {
7647 					/* successful, put sgl to posted list */
7648 					list_add_tail(&sglq_entry->list,
7649 						      &post_sgl_list);
7650 				} else {
7651 					/* Failure, put sgl to free list */
7652 					lpfc_printf_log(phba, KERN_WARNING,
7653 						LOG_SLI,
7654 						"3159 Failed to post "
7655 						"sgl, xritag:x%x\n",
7656 						sglq_entry->sli4_xritag);
7657 					list_add_tail(&sglq_entry->list,
7658 						      &free_sgl_list);
7659 					total_cnt--;
7660 				}
7661 			}
7662 		}
7663 
7664 		/* continue until a nembed page worth of sgls */
7665 		if (post_cnt == 0)
7666 			continue;
7667 
7668 		/* post the buffer list sgls as a block */
7669 		status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7670 						 post_cnt);
7671 
7672 		if (!status) {
7673 			/* success, put sgl list to posted sgl list */
7674 			list_splice_init(&blck_sgl_list, &post_sgl_list);
7675 		} else {
7676 			/* Failure, put sgl list to free sgl list */
7677 			sglq_entry_first = list_first_entry(&blck_sgl_list,
7678 							    struct lpfc_sglq,
7679 							    list);
7680 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7681 					"3160 Failed to post sgl-list, "
7682 					"xritag:x%x-x%x\n",
7683 					sglq_entry_first->sli4_xritag,
7684 					(sglq_entry_first->sli4_xritag +
7685 					 post_cnt - 1));
7686 			list_splice_init(&blck_sgl_list, &free_sgl_list);
7687 			total_cnt -= post_cnt;
7688 		}
7689 
7690 		/* don't reset xirtag due to hole in xri block */
7691 		if (block_cnt == 0)
7692 			last_xritag = NO_XRI;
7693 
7694 		/* reset sgl post count for next round of posting */
7695 		post_cnt = 0;
7696 	}
7697 
7698 	/* free the sgls failed to post */
7699 	lpfc_free_sgl_list(phba, &free_sgl_list);
7700 
7701 	/* push sgls posted to the available list */
7702 	if (!list_empty(&post_sgl_list)) {
7703 		spin_lock_irq(&phba->hbalock);
7704 		spin_lock(&phba->sli4_hba.sgl_list_lock);
7705 		list_splice_init(&post_sgl_list, sgl_list);
7706 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
7707 		spin_unlock_irq(&phba->hbalock);
7708 	} else {
7709 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7710 				"3161 Failure to post sgl to port,status %x "
7711 				"blkcnt %d totalcnt %d postcnt %d\n",
7712 				status, block_cnt, total_cnt, post_cnt);
7713 		return -EIO;
7714 	}
7715 
7716 	/* return the number of XRIs actually posted */
7717 	return total_cnt;
7718 }
7719 
7720 /**
7721  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7722  * @phba: pointer to lpfc hba data structure.
7723  *
7724  * This routine walks the list of nvme buffers that have been allocated and
7725  * repost them to the port by using SGL block post. This is needed after a
7726  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7727  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7728  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7729  *
7730  * Returns: 0 = success, non-zero failure.
7731  **/
7732 static int
lpfc_sli4_repost_io_sgl_list(struct lpfc_hba * phba)7733 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7734 {
7735 	LIST_HEAD(post_nblist);
7736 	int num_posted, rc = 0;
7737 
7738 	/* get all NVME buffers need to repost to a local list */
7739 	lpfc_io_buf_flush(phba, &post_nblist);
7740 
7741 	/* post the list of nvme buffer sgls to port if available */
7742 	if (!list_empty(&post_nblist)) {
7743 		num_posted = lpfc_sli4_post_io_sgl_list(
7744 			phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7745 		/* failed to post any nvme buffer, return error */
7746 		if (num_posted == 0)
7747 			rc = -EIO;
7748 	}
7749 	return rc;
7750 }
7751 
7752 static void
lpfc_set_host_data(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)7753 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7754 {
7755 	uint32_t len;
7756 
7757 	len = sizeof(struct lpfc_mbx_set_host_data) -
7758 		sizeof(struct lpfc_sli4_cfg_mhdr);
7759 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7760 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7761 			 LPFC_SLI4_MBX_EMBED);
7762 
7763 	mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7764 	mbox->u.mqe.un.set_host_data.param_len =
7765 					LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7766 	snprintf(mbox->u.mqe.un.set_host_data.un.data,
7767 		 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7768 		 "Linux %s v"LPFC_DRIVER_VERSION,
7769 		 test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? "FCoE" : "FC");
7770 }
7771 
7772 int
lpfc_post_rq_buffer(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,int count,int idx)7773 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7774 		    struct lpfc_queue *drq, int count, int idx)
7775 {
7776 	int rc, i;
7777 	struct lpfc_rqe hrqe;
7778 	struct lpfc_rqe drqe;
7779 	struct lpfc_rqb *rqbp;
7780 	unsigned long flags;
7781 	struct rqb_dmabuf *rqb_buffer;
7782 	LIST_HEAD(rqb_buf_list);
7783 
7784 	rqbp = hrq->rqbp;
7785 	for (i = 0; i < count; i++) {
7786 		spin_lock_irqsave(&phba->hbalock, flags);
7787 		/* IF RQ is already full, don't bother */
7788 		if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7789 			spin_unlock_irqrestore(&phba->hbalock, flags);
7790 			break;
7791 		}
7792 		spin_unlock_irqrestore(&phba->hbalock, flags);
7793 
7794 		rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7795 		if (!rqb_buffer)
7796 			break;
7797 		rqb_buffer->hrq = hrq;
7798 		rqb_buffer->drq = drq;
7799 		rqb_buffer->idx = idx;
7800 		list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7801 	}
7802 
7803 	spin_lock_irqsave(&phba->hbalock, flags);
7804 	while (!list_empty(&rqb_buf_list)) {
7805 		list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7806 				 hbuf.list);
7807 
7808 		hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7809 		hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7810 		drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7811 		drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7812 		rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7813 		if (rc < 0) {
7814 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7815 					"6421 Cannot post to HRQ %d: %x %x %x "
7816 					"DRQ %x %x\n",
7817 					hrq->queue_id,
7818 					hrq->host_index,
7819 					hrq->hba_index,
7820 					hrq->entry_count,
7821 					drq->host_index,
7822 					drq->hba_index);
7823 			rqbp->rqb_free_buffer(phba, rqb_buffer);
7824 		} else {
7825 			list_add_tail(&rqb_buffer->hbuf.list,
7826 				      &rqbp->rqb_buffer_list);
7827 			rqbp->buffer_count++;
7828 		}
7829 	}
7830 	spin_unlock_irqrestore(&phba->hbalock, flags);
7831 	return 1;
7832 }
7833 
7834 static void
lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7835 lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7836 {
7837 	union lpfc_sli4_cfg_shdr *shdr;
7838 	u32 shdr_status, shdr_add_status;
7839 
7840 	shdr = (union lpfc_sli4_cfg_shdr *)
7841 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7842 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7843 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7844 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7845 		lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT | LOG_MBOX,
7846 				"4622 SET_FEATURE (x%x) mbox failed, "
7847 				"status x%x add_status x%x, mbx status x%x\n",
7848 				LPFC_SET_LD_SIGNAL, shdr_status,
7849 				shdr_add_status, pmb->u.mb.mbxStatus);
7850 		phba->degrade_activate_threshold = 0;
7851 		phba->degrade_deactivate_threshold = 0;
7852 		phba->fec_degrade_interval = 0;
7853 		goto out;
7854 	}
7855 
7856 	phba->degrade_activate_threshold = pmb->u.mqe.un.set_feature.word7;
7857 	phba->degrade_deactivate_threshold = pmb->u.mqe.un.set_feature.word8;
7858 	phba->fec_degrade_interval = pmb->u.mqe.un.set_feature.word10;
7859 
7860 	lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT,
7861 			"4624 Success: da x%x dd x%x interval x%x\n",
7862 			phba->degrade_activate_threshold,
7863 			phba->degrade_deactivate_threshold,
7864 			phba->fec_degrade_interval);
7865 out:
7866 	mempool_free(pmb, phba->mbox_mem_pool);
7867 }
7868 
7869 int
lpfc_read_lds_params(struct lpfc_hba * phba)7870 lpfc_read_lds_params(struct lpfc_hba *phba)
7871 {
7872 	LPFC_MBOXQ_t *mboxq;
7873 	int rc;
7874 
7875 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7876 	if (!mboxq)
7877 		return -ENOMEM;
7878 
7879 	lpfc_set_features(phba, mboxq, LPFC_SET_LD_SIGNAL);
7880 	mboxq->vport = phba->pport;
7881 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_lds_params;
7882 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7883 	if (rc == MBX_NOT_FINISHED) {
7884 		mempool_free(mboxq, phba->mbox_mem_pool);
7885 		return -EIO;
7886 	}
7887 	return 0;
7888 }
7889 
7890 static void
lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7891 lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7892 {
7893 	struct lpfc_vport *vport = pmb->vport;
7894 	union lpfc_sli4_cfg_shdr *shdr;
7895 	u32 shdr_status, shdr_add_status;
7896 	u32 sig, acqe;
7897 
7898 	/* Two outcomes. (1) Set featurs was successul and EDC negotiation
7899 	 * is done. (2) Mailbox failed and send FPIN support only.
7900 	 */
7901 	shdr = (union lpfc_sli4_cfg_shdr *)
7902 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7903 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7904 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7905 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7906 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7907 				"2516 CGN SET_FEATURE mbox failed with "
7908 				"status x%x add_status x%x, mbx status x%x "
7909 				"Reset Congestion to FPINs only\n",
7910 				shdr_status, shdr_add_status,
7911 				pmb->u.mb.mbxStatus);
7912 		/* If there is a mbox error, move on to RDF */
7913 		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7914 		phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7915 		goto out;
7916 	}
7917 
7918 	/* Zero out Congestion Signal ACQE counter */
7919 	phba->cgn_acqe_cnt = 0;
7920 
7921 	acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7922 		      &pmb->u.mqe.un.set_feature);
7923 	sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7924 		     &pmb->u.mqe.un.set_feature);
7925 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7926 			"4620 SET_FEATURES Success: Freq: %ds %dms "
7927 			" Reg: x%x x%x\n", acqe, sig,
7928 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7929 out:
7930 	mempool_free(pmb, phba->mbox_mem_pool);
7931 
7932 	/* Register for FPIN events from the fabric now that the
7933 	 * EDC common_set_features has completed.
7934 	 */
7935 	lpfc_issue_els_rdf(vport, 0);
7936 }
7937 
7938 int
lpfc_config_cgn_signal(struct lpfc_hba * phba)7939 lpfc_config_cgn_signal(struct lpfc_hba *phba)
7940 {
7941 	LPFC_MBOXQ_t *mboxq;
7942 	u32 rc;
7943 
7944 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7945 	if (!mboxq)
7946 		goto out_rdf;
7947 
7948 	lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7949 	mboxq->vport = phba->pport;
7950 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7951 
7952 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7953 			"4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7954 			"Reg: x%x x%x\n",
7955 			phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7956 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7957 
7958 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7959 	if (rc == MBX_NOT_FINISHED)
7960 		goto out;
7961 	return 0;
7962 
7963 out:
7964 	mempool_free(mboxq, phba->mbox_mem_pool);
7965 out_rdf:
7966 	/* If there is a mbox error, move on to RDF */
7967 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7968 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7969 	lpfc_issue_els_rdf(phba->pport, 0);
7970 	return -EIO;
7971 }
7972 
7973 /**
7974  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7975  * @phba: pointer to lpfc hba data structure.
7976  *
7977  * This routine initializes the per-eq idle_stat to dynamically dictate
7978  * polling decisions.
7979  *
7980  * Return codes:
7981  *   None
7982  **/
lpfc_init_idle_stat_hb(struct lpfc_hba * phba)7983 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7984 {
7985 	int i;
7986 	struct lpfc_sli4_hdw_queue *hdwq;
7987 	struct lpfc_queue *eq;
7988 	struct lpfc_idle_stat *idle_stat;
7989 	u64 wall;
7990 
7991 	for_each_present_cpu(i) {
7992 		hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7993 		eq = hdwq->hba_eq;
7994 
7995 		/* Skip if we've already handled this eq's primary CPU */
7996 		if (eq->chann != i)
7997 			continue;
7998 
7999 		idle_stat = &phba->sli4_hba.idle_stat[i];
8000 
8001 		idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
8002 		idle_stat->prev_wall = wall;
8003 
8004 		if (phba->nvmet_support ||
8005 		    phba->cmf_active_mode != LPFC_CFG_OFF ||
8006 		    phba->intr_type != MSIX)
8007 			eq->poll_mode = LPFC_QUEUE_WORK;
8008 		else
8009 			eq->poll_mode = LPFC_THREADED_IRQ;
8010 	}
8011 
8012 	if (!phba->nvmet_support && phba->intr_type == MSIX)
8013 		schedule_delayed_work(&phba->idle_stat_delay_work,
8014 				      msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
8015 }
8016 
lpfc_sli4_dip(struct lpfc_hba * phba)8017 static void lpfc_sli4_dip(struct lpfc_hba *phba)
8018 {
8019 	uint32_t if_type;
8020 
8021 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8022 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
8023 	    if_type == LPFC_SLI_INTF_IF_TYPE_6) {
8024 		struct lpfc_register reg_data;
8025 
8026 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8027 			       &reg_data.word0))
8028 			return;
8029 
8030 		if (bf_get(lpfc_sliport_status_dip, &reg_data))
8031 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8032 					"2904 Firmware Dump Image Present"
8033 					" on Adapter");
8034 	}
8035 }
8036 
8037 /**
8038  * lpfc_rx_monitor_create_ring - Initialize ring buffer for rx_monitor
8039  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8040  * @entries: Number of rx_info_entry objects to allocate in ring
8041  *
8042  * Return:
8043  * 0 - Success
8044  * ENOMEM - Failure to kmalloc
8045  **/
lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor * rx_monitor,u32 entries)8046 int lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor *rx_monitor,
8047 				u32 entries)
8048 {
8049 	rx_monitor->ring = kmalloc_array(entries, sizeof(struct rx_info_entry),
8050 					 GFP_KERNEL);
8051 	if (!rx_monitor->ring)
8052 		return -ENOMEM;
8053 
8054 	rx_monitor->head_idx = 0;
8055 	rx_monitor->tail_idx = 0;
8056 	spin_lock_init(&rx_monitor->lock);
8057 	rx_monitor->entries = entries;
8058 
8059 	return 0;
8060 }
8061 
8062 /**
8063  * lpfc_rx_monitor_destroy_ring - Free ring buffer for rx_monitor
8064  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8065  *
8066  * Called after cancellation of cmf_timer.
8067  **/
lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor * rx_monitor)8068 void lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor *rx_monitor)
8069 {
8070 	kfree(rx_monitor->ring);
8071 	rx_monitor->ring = NULL;
8072 	rx_monitor->entries = 0;
8073 	rx_monitor->head_idx = 0;
8074 	rx_monitor->tail_idx = 0;
8075 }
8076 
8077 /**
8078  * lpfc_rx_monitor_record - Insert an entry into rx_monitor's ring
8079  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8080  * @entry: Pointer to rx_info_entry
8081  *
8082  * Used to insert an rx_info_entry into rx_monitor's ring.  Note that this is a
8083  * deep copy of rx_info_entry not a shallow copy of the rx_info_entry ptr.
8084  *
8085  * This is called from lpfc_cmf_timer, which is in timer/softirq context.
8086  *
8087  * In cases of old data overflow, we do a best effort of FIFO order.
8088  **/
lpfc_rx_monitor_record(struct lpfc_rx_info_monitor * rx_monitor,struct rx_info_entry * entry)8089 void lpfc_rx_monitor_record(struct lpfc_rx_info_monitor *rx_monitor,
8090 			    struct rx_info_entry *entry)
8091 {
8092 	struct rx_info_entry *ring = rx_monitor->ring;
8093 	u32 *head_idx = &rx_monitor->head_idx;
8094 	u32 *tail_idx = &rx_monitor->tail_idx;
8095 	spinlock_t *ring_lock = &rx_monitor->lock;
8096 	u32 ring_size = rx_monitor->entries;
8097 
8098 	spin_lock(ring_lock);
8099 	memcpy(&ring[*tail_idx], entry, sizeof(*entry));
8100 	*tail_idx = (*tail_idx + 1) % ring_size;
8101 
8102 	/* Best effort of FIFO saved data */
8103 	if (*tail_idx == *head_idx)
8104 		*head_idx = (*head_idx + 1) % ring_size;
8105 
8106 	spin_unlock(ring_lock);
8107 }
8108 
8109 /**
8110  * lpfc_rx_monitor_report - Read out rx_monitor's ring
8111  * @phba: Pointer to lpfc_hba object
8112  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8113  * @buf: Pointer to char buffer that will contain rx monitor info data
8114  * @buf_len: Length buf including null char
8115  * @max_read_entries: Maximum number of entries to read out of ring
8116  *
8117  * Used to dump/read what's in rx_monitor's ring buffer.
8118  *
8119  * If buf is NULL || buf_len == 0, then it is implied that we want to log the
8120  * information to kmsg instead of filling out buf.
8121  *
8122  * Return:
8123  * Number of entries read out of the ring
8124  **/
lpfc_rx_monitor_report(struct lpfc_hba * phba,struct lpfc_rx_info_monitor * rx_monitor,char * buf,u32 buf_len,u32 max_read_entries)8125 u32 lpfc_rx_monitor_report(struct lpfc_hba *phba,
8126 			   struct lpfc_rx_info_monitor *rx_monitor, char *buf,
8127 			   u32 buf_len, u32 max_read_entries)
8128 {
8129 	struct rx_info_entry *ring = rx_monitor->ring;
8130 	struct rx_info_entry *entry;
8131 	u32 *head_idx = &rx_monitor->head_idx;
8132 	u32 *tail_idx = &rx_monitor->tail_idx;
8133 	spinlock_t *ring_lock = &rx_monitor->lock;
8134 	u32 ring_size = rx_monitor->entries;
8135 	u32 cnt = 0;
8136 	char tmp[DBG_LOG_STR_SZ] = {0};
8137 	bool log_to_kmsg = (!buf || !buf_len) ? true : false;
8138 
8139 	if (!log_to_kmsg) {
8140 		/* clear the buffer to be sure */
8141 		memset(buf, 0, buf_len);
8142 
8143 		scnprintf(buf, buf_len, "\t%-16s%-16s%-16s%-16s%-8s%-8s%-8s"
8144 					"%-8s%-8s%-8s%-16s\n",
8145 					"MaxBPI", "Tot_Data_CMF",
8146 					"Tot_Data_Cmd", "Tot_Data_Cmpl",
8147 					"Lat(us)", "Avg_IO", "Max_IO", "Bsy",
8148 					"IO_cnt", "Info", "BWutil(ms)");
8149 	}
8150 
8151 	/* Needs to be _irq because record is called from timer interrupt
8152 	 * context
8153 	 */
8154 	spin_lock_irq(ring_lock);
8155 	while (*head_idx != *tail_idx) {
8156 		entry = &ring[*head_idx];
8157 
8158 		/* Read out this entry's data. */
8159 		if (!log_to_kmsg) {
8160 			/* If !log_to_kmsg, then store to buf. */
8161 			scnprintf(tmp, sizeof(tmp),
8162 				  "%03d:\t%-16llu%-16llu%-16llu%-16llu%-8llu"
8163 				  "%-8llu%-8llu%-8u%-8u%-8u%u(%u)\n",
8164 				  *head_idx, entry->max_bytes_per_interval,
8165 				  entry->cmf_bytes, entry->total_bytes,
8166 				  entry->rcv_bytes, entry->avg_io_latency,
8167 				  entry->avg_io_size, entry->max_read_cnt,
8168 				  entry->cmf_busy, entry->io_cnt,
8169 				  entry->cmf_info, entry->timer_utilization,
8170 				  entry->timer_interval);
8171 
8172 			/* Check for buffer overflow */
8173 			if ((strlen(buf) + strlen(tmp)) >= buf_len)
8174 				break;
8175 
8176 			/* Append entry's data to buffer */
8177 			strlcat(buf, tmp, buf_len);
8178 		} else {
8179 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
8180 					"4410 %02u: MBPI %llu Xmit %llu "
8181 					"Cmpl %llu Lat %llu ASz %llu Info %02u "
8182 					"BWUtil %u Int %u slot %u\n",
8183 					cnt, entry->max_bytes_per_interval,
8184 					entry->total_bytes, entry->rcv_bytes,
8185 					entry->avg_io_latency,
8186 					entry->avg_io_size, entry->cmf_info,
8187 					entry->timer_utilization,
8188 					entry->timer_interval, *head_idx);
8189 		}
8190 
8191 		*head_idx = (*head_idx + 1) % ring_size;
8192 
8193 		/* Don't feed more than max_read_entries */
8194 		cnt++;
8195 		if (cnt >= max_read_entries)
8196 			break;
8197 	}
8198 	spin_unlock_irq(ring_lock);
8199 
8200 	return cnt;
8201 }
8202 
8203 /**
8204  * lpfc_cmf_setup - Initialize idle_stat tracking
8205  * @phba: Pointer to HBA context object.
8206  *
8207  * This is called from HBA setup during driver load or when the HBA
8208  * comes online. this does all the initialization to support CMF and MI.
8209  **/
8210 static int
lpfc_cmf_setup(struct lpfc_hba * phba)8211 lpfc_cmf_setup(struct lpfc_hba *phba)
8212 {
8213 	LPFC_MBOXQ_t *mboxq;
8214 	struct lpfc_dmabuf *mp;
8215 	struct lpfc_pc_sli4_params *sli4_params;
8216 	int rc, cmf, mi_ver;
8217 
8218 	rc = lpfc_sli4_refresh_params(phba);
8219 	if (unlikely(rc))
8220 		return rc;
8221 
8222 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8223 	if (!mboxq)
8224 		return -ENOMEM;
8225 
8226 	sli4_params = &phba->sli4_hba.pc_sli4_params;
8227 
8228 	/* Always try to enable MI feature if we can */
8229 	if (sli4_params->mi_ver) {
8230 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
8231 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8232 		mi_ver = bf_get(lpfc_mbx_set_feature_mi,
8233 				 &mboxq->u.mqe.un.set_feature);
8234 
8235 		if (rc == MBX_SUCCESS) {
8236 			if (mi_ver) {
8237 				lpfc_printf_log(phba,
8238 						KERN_WARNING, LOG_CGN_MGMT,
8239 						"6215 MI is enabled\n");
8240 				sli4_params->mi_ver = mi_ver;
8241 			} else {
8242 				lpfc_printf_log(phba,
8243 						KERN_WARNING, LOG_CGN_MGMT,
8244 						"6338 MI is disabled\n");
8245 				sli4_params->mi_ver = 0;
8246 			}
8247 		} else {
8248 			/* mi_ver is already set from GET_SLI4_PARAMETERS */
8249 			lpfc_printf_log(phba, KERN_INFO,
8250 					LOG_CGN_MGMT | LOG_INIT,
8251 					"6245 Enable MI Mailbox x%x (x%x/x%x) "
8252 					"failed, rc:x%x mi:x%x\n",
8253 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8254 					lpfc_sli_config_mbox_subsys_get
8255 						(phba, mboxq),
8256 					lpfc_sli_config_mbox_opcode_get
8257 						(phba, mboxq),
8258 					rc, sli4_params->mi_ver);
8259 		}
8260 	} else {
8261 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8262 				"6217 MI is disabled\n");
8263 	}
8264 
8265 	/* Ensure FDMI is enabled for MI if enable_mi is set */
8266 	if (sli4_params->mi_ver)
8267 		phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
8268 
8269 	/* Always try to enable CMF feature if we can */
8270 	if (sli4_params->cmf) {
8271 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
8272 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8273 		cmf = bf_get(lpfc_mbx_set_feature_cmf,
8274 			     &mboxq->u.mqe.un.set_feature);
8275 		if (rc == MBX_SUCCESS && cmf) {
8276 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8277 					"6218 CMF is enabled: mode %d\n",
8278 					phba->cmf_active_mode);
8279 		} else {
8280 			lpfc_printf_log(phba, KERN_WARNING,
8281 					LOG_CGN_MGMT | LOG_INIT,
8282 					"6219 Enable CMF Mailbox x%x (x%x/x%x) "
8283 					"failed, rc:x%x dd:x%x\n",
8284 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8285 					lpfc_sli_config_mbox_subsys_get
8286 						(phba, mboxq),
8287 					lpfc_sli_config_mbox_opcode_get
8288 						(phba, mboxq),
8289 					rc, cmf);
8290 			sli4_params->cmf = 0;
8291 			phba->cmf_active_mode = LPFC_CFG_OFF;
8292 			goto no_cmf;
8293 		}
8294 
8295 		/* Allocate Congestion Information Buffer */
8296 		if (!phba->cgn_i) {
8297 			mp = kmalloc(sizeof(*mp), GFP_KERNEL);
8298 			if (mp)
8299 				mp->virt = dma_alloc_coherent
8300 						(&phba->pcidev->dev,
8301 						sizeof(struct lpfc_cgn_info),
8302 						&mp->phys, GFP_KERNEL);
8303 			if (!mp || !mp->virt) {
8304 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8305 						"2640 Failed to alloc memory "
8306 						"for Congestion Info\n");
8307 				kfree(mp);
8308 				sli4_params->cmf = 0;
8309 				phba->cmf_active_mode = LPFC_CFG_OFF;
8310 				goto no_cmf;
8311 			}
8312 			phba->cgn_i = mp;
8313 
8314 			/* initialize congestion buffer info */
8315 			lpfc_init_congestion_buf(phba);
8316 			lpfc_init_congestion_stat(phba);
8317 
8318 			/* Zero out Congestion Signal counters */
8319 			atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
8320 			atomic64_set(&phba->cgn_acqe_stat.warn, 0);
8321 		}
8322 
8323 		rc = lpfc_sli4_cgn_params_read(phba);
8324 		if (rc < 0) {
8325 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8326 					"6242 Error reading Cgn Params (%d)\n",
8327 					rc);
8328 			/* Ensure CGN Mode is off */
8329 			sli4_params->cmf = 0;
8330 		} else if (!rc) {
8331 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8332 					"6243 CGN Event empty object.\n");
8333 			/* Ensure CGN Mode is off */
8334 			sli4_params->cmf = 0;
8335 		}
8336 	} else {
8337 no_cmf:
8338 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8339 				"6220 CMF is disabled\n");
8340 	}
8341 
8342 	/* Only register congestion buffer with firmware if BOTH
8343 	 * CMF and E2E are enabled.
8344 	 */
8345 	if (sli4_params->cmf && sli4_params->mi_ver) {
8346 		rc = lpfc_reg_congestion_buf(phba);
8347 		if (rc) {
8348 			dma_free_coherent(&phba->pcidev->dev,
8349 					  sizeof(struct lpfc_cgn_info),
8350 					  phba->cgn_i->virt, phba->cgn_i->phys);
8351 			kfree(phba->cgn_i);
8352 			phba->cgn_i = NULL;
8353 			/* Ensure CGN Mode is off */
8354 			phba->cmf_active_mode = LPFC_CFG_OFF;
8355 			sli4_params->cmf = 0;
8356 			return 0;
8357 		}
8358 	}
8359 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8360 			"6470 Setup MI version %d CMF %d mode %d\n",
8361 			sli4_params->mi_ver, sli4_params->cmf,
8362 			phba->cmf_active_mode);
8363 
8364 	mempool_free(mboxq, phba->mbox_mem_pool);
8365 
8366 	/* Initialize atomic counters */
8367 	atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8368 	atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8369 	atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8370 	atomic_set(&phba->cgn_sync_warn_cnt, 0);
8371 	atomic_set(&phba->cgn_driver_evt_cnt, 0);
8372 	atomic_set(&phba->cgn_latency_evt_cnt, 0);
8373 	atomic64_set(&phba->cgn_latency_evt, 0);
8374 
8375 	phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
8376 
8377 	/* Allocate RX Monitor Buffer */
8378 	if (!phba->rx_monitor) {
8379 		phba->rx_monitor = kzalloc(sizeof(*phba->rx_monitor),
8380 					   GFP_KERNEL);
8381 
8382 		if (!phba->rx_monitor) {
8383 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8384 					"2644 Failed to alloc memory "
8385 					"for RX Monitor Buffer\n");
8386 			return -ENOMEM;
8387 		}
8388 
8389 		/* Instruct the rx_monitor object to instantiate its ring */
8390 		if (lpfc_rx_monitor_create_ring(phba->rx_monitor,
8391 						LPFC_MAX_RXMONITOR_ENTRY)) {
8392 			kfree(phba->rx_monitor);
8393 			phba->rx_monitor = NULL;
8394 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8395 					"2645 Failed to alloc memory "
8396 					"for RX Monitor's Ring\n");
8397 			return -ENOMEM;
8398 		}
8399 	}
8400 
8401 	return 0;
8402 }
8403 
8404 static int
lpfc_set_host_tm(struct lpfc_hba * phba)8405 lpfc_set_host_tm(struct lpfc_hba *phba)
8406 {
8407 	LPFC_MBOXQ_t *mboxq;
8408 	uint32_t len, rc;
8409 	struct timespec64 cur_time;
8410 	struct tm broken;
8411 	uint32_t month, day, year;
8412 	uint32_t hour, minute, second;
8413 	struct lpfc_mbx_set_host_date_time *tm;
8414 
8415 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8416 	if (!mboxq)
8417 		return -ENOMEM;
8418 
8419 	len = sizeof(struct lpfc_mbx_set_host_data) -
8420 		sizeof(struct lpfc_sli4_cfg_mhdr);
8421 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8422 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8423 			 LPFC_SLI4_MBX_EMBED);
8424 
8425 	mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8426 	mboxq->u.mqe.un.set_host_data.param_len =
8427 			sizeof(struct lpfc_mbx_set_host_date_time);
8428 	tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8429 	ktime_get_real_ts64(&cur_time);
8430 	time64_to_tm(cur_time.tv_sec, 0, &broken);
8431 	month = broken.tm_mon + 1;
8432 	day = broken.tm_mday;
8433 	year = broken.tm_year - 100;
8434 	hour = broken.tm_hour;
8435 	minute = broken.tm_min;
8436 	second = broken.tm_sec;
8437 	bf_set(lpfc_mbx_set_host_month, tm, month);
8438 	bf_set(lpfc_mbx_set_host_day, tm, day);
8439 	bf_set(lpfc_mbx_set_host_year, tm, year);
8440 	bf_set(lpfc_mbx_set_host_hour, tm, hour);
8441 	bf_set(lpfc_mbx_set_host_min, tm, minute);
8442 	bf_set(lpfc_mbx_set_host_sec, tm, second);
8443 
8444 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8445 	mempool_free(mboxq, phba->mbox_mem_pool);
8446 	return rc;
8447 }
8448 
8449 /**
8450  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
8451  * @phba: Pointer to HBA context object.
8452  *
8453  * This function is the main SLI4 device initialization PCI function. This
8454  * function is called by the HBA initialization code, HBA reset code and
8455  * HBA error attention handler code. Caller is not required to hold any
8456  * locks.
8457  **/
8458 int
lpfc_sli4_hba_setup(struct lpfc_hba * phba)8459 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8460 {
8461 	int rc, i, cnt, len, dd;
8462 	LPFC_MBOXQ_t *mboxq;
8463 	struct lpfc_mqe *mqe;
8464 	uint8_t *vpd;
8465 	uint32_t vpd_size;
8466 	uint32_t ftr_rsp = 0;
8467 	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8468 	struct lpfc_vport *vport = phba->pport;
8469 	struct lpfc_dmabuf *mp;
8470 	struct lpfc_rqb *rqbp;
8471 	u32 flg;
8472 
8473 	/* Perform a PCI function reset to start from clean */
8474 	rc = lpfc_pci_function_reset(phba);
8475 	if (unlikely(rc))
8476 		return -ENODEV;
8477 
8478 	/* Check the HBA Host Status Register for readyness */
8479 	rc = lpfc_sli4_post_status_check(phba);
8480 	if (unlikely(rc))
8481 		return -ENODEV;
8482 	else {
8483 		spin_lock_irq(&phba->hbalock);
8484 		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
8485 		flg = phba->sli.sli_flag;
8486 		spin_unlock_irq(&phba->hbalock);
8487 		/* Allow a little time after setting SLI_ACTIVE for any polled
8488 		 * MBX commands to complete via BSG.
8489 		 */
8490 		for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {
8491 			msleep(20);
8492 			spin_lock_irq(&phba->hbalock);
8493 			flg = phba->sli.sli_flag;
8494 			spin_unlock_irq(&phba->hbalock);
8495 		}
8496 	}
8497 	clear_bit(HBA_SETUP, &phba->hba_flag);
8498 
8499 	lpfc_sli4_dip(phba);
8500 
8501 	/*
8502 	 * Allocate a single mailbox container for initializing the
8503 	 * port.
8504 	 */
8505 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8506 	if (!mboxq)
8507 		return -ENOMEM;
8508 
8509 	/* Issue READ_REV to collect vpd and FW information. */
8510 	vpd_size = SLI4_PAGE_SIZE;
8511 	vpd = kzalloc(vpd_size, GFP_KERNEL);
8512 	if (!vpd) {
8513 		rc = -ENOMEM;
8514 		goto out_free_mbox;
8515 	}
8516 
8517 	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
8518 	if (unlikely(rc)) {
8519 		kfree(vpd);
8520 		goto out_free_mbox;
8521 	}
8522 
8523 	mqe = &mboxq->u.mqe;
8524 	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
8525 	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
8526 		set_bit(HBA_FCOE_MODE, &phba->hba_flag);
8527 		phba->fcp_embed_io = 0;	/* SLI4 FC support only */
8528 	} else {
8529 		clear_bit(HBA_FCOE_MODE, &phba->hba_flag);
8530 	}
8531 
8532 	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8533 		LPFC_DCBX_CEE_MODE)
8534 		set_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8535 	else
8536 		clear_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8537 
8538 	clear_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
8539 
8540 	if (phba->sli_rev != LPFC_SLI_REV4) {
8541 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8542 			"0376 READ_REV Error. SLI Level %d "
8543 			"FCoE enabled %d\n",
8544 			phba->sli_rev,
8545 			test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? 1 : 0);
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 (test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
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 	kfree(vpd);
8592 
8593 	/* Save information as VPD data */
8594 	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8595 	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
8596 
8597 	/*
8598 	 * This is because first G7 ASIC doesn't support the standard
8599 	 * 0x5a NVME cmd descriptor type/subtype
8600 	 */
8601 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8602 			LPFC_SLI_INTF_IF_TYPE_6) &&
8603 	    (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8604 	    (phba->vpd.rev.smRev == 0) &&
8605 	    (phba->cfg_nvme_embed_cmd == 1))
8606 		phba->cfg_nvme_embed_cmd = 0;
8607 
8608 	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8609 	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8610 					 &mqe->un.read_rev);
8611 	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8612 				       &mqe->un.read_rev);
8613 	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8614 					    &mqe->un.read_rev);
8615 	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8616 					   &mqe->un.read_rev);
8617 	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8618 	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8619 	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8620 	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8621 	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8622 	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8623 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8624 			"(%d):0380 READ_REV Status x%x "
8625 			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8626 			mboxq->vport ? mboxq->vport->vpi : 0,
8627 			bf_get(lpfc_mqe_status, mqe),
8628 			phba->vpd.rev.opFwName,
8629 			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8630 			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
8631 
8632 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8633 	    LPFC_SLI_INTF_IF_TYPE_0) {
8634 		lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8635 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8636 		if (rc == MBX_SUCCESS) {
8637 			set_bit(HBA_RECOVERABLE_UE, &phba->hba_flag);
8638 			/* Set 1Sec interval to detect UE */
8639 			phba->eratt_poll_interval = 1;
8640 			phba->sli4_hba.ue_to_sr = bf_get(
8641 					lpfc_mbx_set_feature_UESR,
8642 					&mboxq->u.mqe.un.set_feature);
8643 			phba->sli4_hba.ue_to_rp = bf_get(
8644 					lpfc_mbx_set_feature_UERP,
8645 					&mboxq->u.mqe.un.set_feature);
8646 		}
8647 	}
8648 
8649 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8650 		/* Enable MDS Diagnostics only if the SLI Port supports it */
8651 		lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8652 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8653 		if (rc != MBX_SUCCESS)
8654 			phba->mds_diags_support = 0;
8655 	}
8656 
8657 	/*
8658 	 * Discover the port's supported feature set and match it against the
8659 	 * hosts requests.
8660 	 */
8661 	lpfc_request_features(phba, mboxq);
8662 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8663 	if (unlikely(rc)) {
8664 		rc = -EIO;
8665 		goto out_free_mbox;
8666 	}
8667 
8668 	/* Disable VMID if app header is not supported */
8669 	if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8670 						  &mqe->un.req_ftrs))) {
8671 		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8672 		phba->cfg_vmid_app_header = 0;
8673 		lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8674 				"1242 vmid feature not supported\n");
8675 	}
8676 
8677 	/*
8678 	 * The port must support FCP initiator mode as this is the
8679 	 * only mode running in the host.
8680 	 */
8681 	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8682 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8683 				"0378 No support for fcpi mode.\n");
8684 		ftr_rsp++;
8685 	}
8686 
8687 	/* Performance Hints are ONLY for FCoE */
8688 	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
8689 		if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
8690 			phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
8691 		else
8692 			phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
8693 	}
8694 
8695 	/*
8696 	 * If the port cannot support the host's requested features
8697 	 * then turn off the global config parameters to disable the
8698 	 * feature in the driver.  This is not a fatal error.
8699 	 */
8700 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8701 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8702 			phba->cfg_enable_bg = 0;
8703 			phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
8704 			ftr_rsp++;
8705 		}
8706 	}
8707 
8708 	if (phba->max_vpi && phba->cfg_enable_npiv &&
8709 	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8710 		ftr_rsp++;
8711 
8712 	if (ftr_rsp) {
8713 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8714 				"0379 Feature Mismatch Data: x%08x %08x "
8715 				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8716 				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8717 				phba->cfg_enable_npiv, phba->max_vpi);
8718 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8719 			phba->cfg_enable_bg = 0;
8720 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8721 			phba->cfg_enable_npiv = 0;
8722 	}
8723 
8724 	/* These SLI3 features are assumed in SLI4 */
8725 	spin_lock_irq(&phba->hbalock);
8726 	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8727 	spin_unlock_irq(&phba->hbalock);
8728 
8729 	/* Always try to enable dual dump feature if we can */
8730 	lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8731 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8732 	dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8733 	if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
8734 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8735 				"6448 Dual Dump is enabled\n");
8736 	else
8737 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8738 				"6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8739 				"rc:x%x dd:x%x\n",
8740 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8741 				lpfc_sli_config_mbox_subsys_get(
8742 					phba, mboxq),
8743 				lpfc_sli_config_mbox_opcode_get(
8744 					phba, mboxq),
8745 				rc, dd);
8746 
8747 	/*
8748 	 * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
8749 	 * calls depends on these resources to complete port setup.
8750 	 */
8751 	rc = lpfc_sli4_alloc_resource_identifiers(phba);
8752 	if (rc) {
8753 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8754 				"2920 Failed to alloc Resource IDs "
8755 				"rc = x%x\n", rc);
8756 		goto out_free_mbox;
8757 	}
8758 
8759 	lpfc_sli4_node_rpi_restore(phba);
8760 
8761 	lpfc_set_host_data(phba, mboxq);
8762 
8763 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8764 	if (rc) {
8765 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8766 				"2134 Failed to set host os driver version %x",
8767 				rc);
8768 	}
8769 
8770 	/* Read the port's service parameters. */
8771 	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8772 	if (rc) {
8773 		phba->link_state = LPFC_HBA_ERROR;
8774 		rc = -ENOMEM;
8775 		goto out_free_mbox;
8776 	}
8777 
8778 	mboxq->vport = vport;
8779 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8780 	mp = mboxq->ctx_buf;
8781 	if (rc == MBX_SUCCESS) {
8782 		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8783 		rc = 0;
8784 	}
8785 
8786 	/*
8787 	 * This memory was allocated by the lpfc_read_sparam routine but is
8788 	 * no longer needed.  It is released and ctx_buf NULLed to prevent
8789 	 * unintended pointer access as the mbox is reused.
8790 	 */
8791 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
8792 	kfree(mp);
8793 	mboxq->ctx_buf = NULL;
8794 	if (unlikely(rc)) {
8795 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8796 				"0382 READ_SPARAM command failed "
8797 				"status %d, mbxStatus x%x\n",
8798 				rc, bf_get(lpfc_mqe_status, mqe));
8799 		phba->link_state = LPFC_HBA_ERROR;
8800 		rc = -EIO;
8801 		goto out_free_mbox;
8802 	}
8803 
8804 	lpfc_update_vport_wwn(vport);
8805 
8806 	/* Update the fc_host data structures with new wwn. */
8807 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8808 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8809 
8810 	/* Create all the SLI4 queues */
8811 	rc = lpfc_sli4_queue_create(phba);
8812 	if (rc) {
8813 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8814 				"3089 Failed to allocate queues\n");
8815 		rc = -ENODEV;
8816 		goto out_free_mbox;
8817 	}
8818 	/* Set up all the queues to the device */
8819 	rc = lpfc_sli4_queue_setup(phba);
8820 	if (unlikely(rc)) {
8821 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8822 				"0381 Error %d during queue setup.\n", rc);
8823 		goto out_stop_timers;
8824 	}
8825 	/* Initialize the driver internal SLI layer lists. */
8826 	lpfc_sli4_setup(phba);
8827 	lpfc_sli4_queue_init(phba);
8828 
8829 	/* update host els xri-sgl sizes and mappings */
8830 	rc = lpfc_sli4_els_sgl_update(phba);
8831 	if (unlikely(rc)) {
8832 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8833 				"1400 Failed to update xri-sgl size and "
8834 				"mapping: %d\n", rc);
8835 		goto out_destroy_queue;
8836 	}
8837 
8838 	/* register the els sgl pool to the port */
8839 	rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8840 				       phba->sli4_hba.els_xri_cnt);
8841 	if (unlikely(rc < 0)) {
8842 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8843 				"0582 Error %d during els sgl post "
8844 				"operation\n", rc);
8845 		rc = -ENODEV;
8846 		goto out_destroy_queue;
8847 	}
8848 	phba->sli4_hba.els_xri_cnt = rc;
8849 
8850 	if (phba->nvmet_support) {
8851 		/* update host nvmet xri-sgl sizes and mappings */
8852 		rc = lpfc_sli4_nvmet_sgl_update(phba);
8853 		if (unlikely(rc)) {
8854 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8855 					"6308 Failed to update nvmet-sgl size "
8856 					"and mapping: %d\n", rc);
8857 			goto out_destroy_queue;
8858 		}
8859 
8860 		/* register the nvmet sgl pool to the port */
8861 		rc = lpfc_sli4_repost_sgl_list(
8862 			phba,
8863 			&phba->sli4_hba.lpfc_nvmet_sgl_list,
8864 			phba->sli4_hba.nvmet_xri_cnt);
8865 		if (unlikely(rc < 0)) {
8866 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8867 					"3117 Error %d during nvmet "
8868 					"sgl post\n", rc);
8869 			rc = -ENODEV;
8870 			goto out_destroy_queue;
8871 		}
8872 		phba->sli4_hba.nvmet_xri_cnt = rc;
8873 
8874 		/* We allocate an iocbq for every receive context SGL.
8875 		 * The additional allocation is for abort and ls handling.
8876 		 */
8877 		cnt = phba->sli4_hba.nvmet_xri_cnt +
8878 			phba->sli4_hba.max_cfg_param.max_xri;
8879 	} else {
8880 		/* update host common xri-sgl sizes and mappings */
8881 		rc = lpfc_sli4_io_sgl_update(phba);
8882 		if (unlikely(rc)) {
8883 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8884 					"6082 Failed to update nvme-sgl size "
8885 					"and mapping: %d\n", rc);
8886 			goto out_destroy_queue;
8887 		}
8888 
8889 		/* register the allocated common sgl pool to the port */
8890 		rc = lpfc_sli4_repost_io_sgl_list(phba);
8891 		if (unlikely(rc)) {
8892 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8893 					"6116 Error %d during nvme sgl post "
8894 					"operation\n", rc);
8895 			/* Some NVME buffers were moved to abort nvme list */
8896 			/* A pci function reset will repost them */
8897 			rc = -ENODEV;
8898 			goto out_destroy_queue;
8899 		}
8900 		/* Each lpfc_io_buf job structure has an iocbq element.
8901 		 * This cnt provides for abort, els, ct and ls requests.
8902 		 */
8903 		cnt = phba->sli4_hba.max_cfg_param.max_xri;
8904 	}
8905 
8906 	if (!phba->sli.iocbq_lookup) {
8907 		/* Initialize and populate the iocb list per host */
8908 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8909 				"2821 initialize iocb list with %d entries\n",
8910 				cnt);
8911 		rc = lpfc_init_iocb_list(phba, cnt);
8912 		if (rc) {
8913 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8914 					"1413 Failed to init iocb list.\n");
8915 			goto out_destroy_queue;
8916 		}
8917 	}
8918 
8919 	if (phba->nvmet_support)
8920 		lpfc_nvmet_create_targetport(phba);
8921 
8922 	if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
8923 		/* Post initial buffers to all RQs created */
8924 		for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8925 			rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8926 			INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8927 			rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8928 			rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
8929 			rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
8930 			rqbp->buffer_count = 0;
8931 
8932 			lpfc_post_rq_buffer(
8933 				phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8934 				phba->sli4_hba.nvmet_mrq_data[i],
8935 				phba->cfg_nvmet_mrq_post, i);
8936 		}
8937 	}
8938 
8939 	/* Post the rpi header region to the device. */
8940 	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8941 	if (unlikely(rc)) {
8942 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8943 				"0393 Error %d during rpi post operation\n",
8944 				rc);
8945 		rc = -ENODEV;
8946 		goto out_free_iocblist;
8947 	}
8948 
8949 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
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 + secs_to_jiffies(phba->fc_ratov * 2));
9021 
9022 	/* Start heart beat timer */
9023 	mod_timer(&phba->hb_tmofunc,
9024 		  jiffies + secs_to_jiffies(LPFC_HB_MBOX_INTERVAL));
9025 	clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
9026 	clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
9027 	phba->last_completion_time = jiffies;
9028 
9029 	/* start eq_delay heartbeat */
9030 	if (phba->cfg_auto_imax)
9031 		queue_delayed_work(phba->wq, &phba->eq_delay_work,
9032 				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
9033 
9034 	/* start per phba idle_stat_delay heartbeat */
9035 	lpfc_init_idle_stat_hb(phba);
9036 
9037 	/* Start error attention (ERATT) polling timer */
9038 	mod_timer(&phba->eratt_poll,
9039 		  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
9040 
9041 	/*
9042 	 * The port is ready, set the host's link state to LINK_DOWN
9043 	 * in preparation for link interrupts.
9044 	 */
9045 	spin_lock_irq(&phba->hbalock);
9046 	phba->link_state = LPFC_LINK_DOWN;
9047 
9048 	/* Check if physical ports are trunked */
9049 	if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
9050 		phba->trunk_link.link0.state = LPFC_LINK_DOWN;
9051 	if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
9052 		phba->trunk_link.link1.state = LPFC_LINK_DOWN;
9053 	if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
9054 		phba->trunk_link.link2.state = LPFC_LINK_DOWN;
9055 	if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
9056 		phba->trunk_link.link3.state = LPFC_LINK_DOWN;
9057 	spin_unlock_irq(&phba->hbalock);
9058 
9059 	/* Arm the CQs and then EQs on device */
9060 	lpfc_sli4_arm_cqeq_intr(phba);
9061 
9062 	/* Indicate device interrupt mode */
9063 	phba->sli4_hba.intr_enable = 1;
9064 
9065 	/* Setup CMF after HBA is initialized */
9066 	lpfc_cmf_setup(phba);
9067 
9068 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
9069 	    test_bit(LINK_DISABLED, &phba->hba_flag)) {
9070 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9071 				"3103 Adapter Link is disabled.\n");
9072 		lpfc_down_link(phba, mboxq);
9073 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9074 		if (rc != MBX_SUCCESS) {
9075 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9076 					"3104 Adapter failed to issue "
9077 					"DOWN_LINK mbox cmd, rc:x%x\n", rc);
9078 			goto out_io_buff_free;
9079 		}
9080 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
9081 		/* don't perform init_link on SLI4 FC port loopback test */
9082 		if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
9083 			rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
9084 			if (rc)
9085 				goto out_io_buff_free;
9086 		}
9087 	}
9088 	mempool_free(mboxq, phba->mbox_mem_pool);
9089 
9090 	/* Enable RAS FW log support */
9091 	lpfc_sli4_ras_setup(phba);
9092 
9093 	set_bit(HBA_SETUP, &phba->hba_flag);
9094 	return rc;
9095 
9096 out_io_buff_free:
9097 	/* Free allocated IO Buffers */
9098 	lpfc_io_free(phba);
9099 out_unset_queue:
9100 	/* Unset all the queues set up in this routine when error out */
9101 	lpfc_sli4_queue_unset(phba);
9102 out_free_iocblist:
9103 	lpfc_free_iocb_list(phba);
9104 out_destroy_queue:
9105 	lpfc_sli4_queue_destroy(phba);
9106 out_stop_timers:
9107 	lpfc_stop_hba_timers(phba);
9108 out_free_mbox:
9109 	mempool_free(mboxq, phba->mbox_mem_pool);
9110 	return rc;
9111 }
9112 
9113 /**
9114  * lpfc_mbox_timeout - Timeout call back function for mbox timer
9115  * @t: Context to fetch pointer to hba structure from.
9116  *
9117  * This is the callback function for mailbox timer. The mailbox
9118  * timer is armed when a new mailbox command is issued and the timer
9119  * is deleted when the mailbox complete. The function is called by
9120  * the kernel timer code when a mailbox does not complete within
9121  * expected time. This function wakes up the worker thread to
9122  * process the mailbox timeout and returns. All the processing is
9123  * done by the worker thread function lpfc_mbox_timeout_handler.
9124  **/
9125 void
lpfc_mbox_timeout(struct timer_list * t)9126 lpfc_mbox_timeout(struct timer_list *t)
9127 {
9128 	struct lpfc_hba  *phba = timer_container_of(phba, t, sli.mbox_tmo);
9129 	unsigned long iflag;
9130 	uint32_t tmo_posted;
9131 
9132 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
9133 	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
9134 	if (!tmo_posted)
9135 		phba->pport->work_port_events |= WORKER_MBOX_TMO;
9136 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
9137 
9138 	if (!tmo_posted)
9139 		lpfc_worker_wake_up(phba);
9140 	return;
9141 }
9142 
9143 /**
9144  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
9145  *                                    are pending
9146  * @phba: Pointer to HBA context object.
9147  *
9148  * This function checks if any mailbox completions are present on the mailbox
9149  * completion queue.
9150  **/
9151 static bool
lpfc_sli4_mbox_completions_pending(struct lpfc_hba * phba)9152 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
9153 {
9154 
9155 	uint32_t idx;
9156 	struct lpfc_queue *mcq;
9157 	struct lpfc_mcqe *mcqe;
9158 	bool pending_completions = false;
9159 	uint8_t	qe_valid;
9160 
9161 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9162 		return false;
9163 
9164 	/* Check for completions on mailbox completion queue */
9165 
9166 	mcq = phba->sli4_hba.mbx_cq;
9167 	idx = mcq->hba_index;
9168 	qe_valid = mcq->qe_valid;
9169 	while (bf_get_le32(lpfc_cqe_valid,
9170 	       (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
9171 		mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
9172 		if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
9173 		    (!bf_get_le32(lpfc_trailer_async, mcqe))) {
9174 			pending_completions = true;
9175 			break;
9176 		}
9177 		idx = (idx + 1) % mcq->entry_count;
9178 		if (mcq->hba_index == idx)
9179 			break;
9180 
9181 		/* if the index wrapped around, toggle the valid bit */
9182 		if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
9183 			qe_valid = (qe_valid) ? 0 : 1;
9184 	}
9185 	return pending_completions;
9186 
9187 }
9188 
9189 /**
9190  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
9191  *					      that were missed.
9192  * @phba: Pointer to HBA context object.
9193  *
9194  * For sli4, it is possible to miss an interrupt. As such mbox completions
9195  * maybe missed causing erroneous mailbox timeouts to occur. This function
9196  * checks to see if mbox completions are on the mailbox completion queue
9197  * and will process all the completions associated with the eq for the
9198  * mailbox completion queue.
9199  **/
9200 static bool
lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba * phba)9201 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
9202 {
9203 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
9204 	uint32_t eqidx;
9205 	struct lpfc_queue *fpeq = NULL;
9206 	struct lpfc_queue *eq;
9207 	bool mbox_pending;
9208 
9209 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9210 		return false;
9211 
9212 	/* Find the EQ associated with the mbox CQ */
9213 	if (sli4_hba->hdwq) {
9214 		for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
9215 			eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
9216 			if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
9217 				fpeq = eq;
9218 				break;
9219 			}
9220 		}
9221 	}
9222 	if (!fpeq)
9223 		return false;
9224 
9225 	/* Turn off interrupts from this EQ */
9226 
9227 	sli4_hba->sli4_eq_clr_intr(fpeq);
9228 
9229 	/* Check to see if a mbox completion is pending */
9230 
9231 	mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
9232 
9233 	/*
9234 	 * If a mbox completion is pending, process all the events on EQ
9235 	 * associated with the mbox completion queue (this could include
9236 	 * mailbox commands, async events, els commands, receive queue data
9237 	 * and fcp commands)
9238 	 */
9239 
9240 	if (mbox_pending)
9241 		/* process and rearm the EQ */
9242 		lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
9243 				     LPFC_QUEUE_WORK);
9244 	else
9245 		/* Always clear and re-arm the EQ */
9246 		sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
9247 
9248 	return mbox_pending;
9249 
9250 }
9251 
9252 /**
9253  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
9254  * @phba: Pointer to HBA context object.
9255  *
9256  * This function is called from worker thread when a mailbox command times out.
9257  * The caller is not required to hold any locks. This function will reset the
9258  * HBA and recover all the pending commands.
9259  **/
9260 void
lpfc_mbox_timeout_handler(struct lpfc_hba * phba)9261 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
9262 {
9263 	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
9264 	MAILBOX_t *mb = NULL;
9265 
9266 	struct lpfc_sli *psli = &phba->sli;
9267 
9268 	/* If the mailbox completed, process the completion */
9269 	lpfc_sli4_process_missed_mbox_completions(phba);
9270 
9271 	if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
9272 		return;
9273 
9274 	if (pmbox != NULL)
9275 		mb = &pmbox->u.mb;
9276 	/* Check the pmbox pointer first.  There is a race condition
9277 	 * between the mbox timeout handler getting executed in the
9278 	 * worklist and the mailbox actually completing. When this
9279 	 * race condition occurs, the mbox_active will be NULL.
9280 	 */
9281 	spin_lock_irq(&phba->hbalock);
9282 	if (pmbox == NULL) {
9283 		lpfc_printf_log(phba, KERN_WARNING,
9284 				LOG_MBOX | LOG_SLI,
9285 				"0353 Active Mailbox cleared - mailbox timeout "
9286 				"exiting\n");
9287 		spin_unlock_irq(&phba->hbalock);
9288 		return;
9289 	}
9290 
9291 	/* Mbox cmd <mbxCommand> timeout */
9292 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9293 			"0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
9294 			mb->mbxCommand,
9295 			phba->pport->port_state,
9296 			phba->sli.sli_flag,
9297 			phba->sli.mbox_active);
9298 	spin_unlock_irq(&phba->hbalock);
9299 
9300 	/* Setting state unknown so lpfc_sli_abort_iocb_ring
9301 	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
9302 	 * it to fail all outstanding SCSI IO.
9303 	 */
9304 	set_bit(MBX_TMO_ERR, &phba->bit_flags);
9305 	spin_lock_irq(&phba->pport->work_port_lock);
9306 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9307 	spin_unlock_irq(&phba->pport->work_port_lock);
9308 	spin_lock_irq(&phba->hbalock);
9309 	phba->link_state = LPFC_LINK_UNKNOWN;
9310 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9311 	spin_unlock_irq(&phba->hbalock);
9312 
9313 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9314 			"0345 Resetting board due to mailbox timeout\n");
9315 
9316 	/* Reset the HBA device */
9317 	lpfc_reset_hba(phba);
9318 }
9319 
9320 /**
9321  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
9322  * @phba: Pointer to HBA context object.
9323  * @pmbox: Pointer to mailbox object.
9324  * @flag: Flag indicating how the mailbox need to be processed.
9325  *
9326  * This function is called by discovery code and HBA management code
9327  * to submit a mailbox command to firmware with SLI-3 interface spec. This
9328  * function gets the hbalock to protect the data structures.
9329  * The mailbox command can be submitted in polling mode, in which case
9330  * this function will wait in a polling loop for the completion of the
9331  * mailbox.
9332  * If the mailbox is submitted in no_wait mode (not polling) the
9333  * function will submit the command and returns immediately without waiting
9334  * for the mailbox completion. The no_wait is supported only when HBA
9335  * is in SLI2/SLI3 mode - interrupts are enabled.
9336  * The SLI interface allows only one mailbox pending at a time. If the
9337  * mailbox is issued in polling mode and there is already a mailbox
9338  * pending, then the function will return an error. If the mailbox is issued
9339  * in NO_WAIT mode and there is a mailbox pending already, the function
9340  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9341  * The sli layer owns the mailbox object until the completion of mailbox
9342  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9343  * return codes the caller owns the mailbox command after the return of
9344  * the function.
9345  **/
9346 static int
lpfc_sli_issue_mbox_s3(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)9347 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9348 		       uint32_t flag)
9349 {
9350 	MAILBOX_t *mbx;
9351 	struct lpfc_sli *psli = &phba->sli;
9352 	uint32_t status, evtctr;
9353 	uint32_t ha_copy, hc_copy;
9354 	int i;
9355 	unsigned long timeout;
9356 	unsigned long drvr_flag = 0;
9357 	uint32_t word0, ldata;
9358 	void __iomem *to_slim;
9359 	int processing_queue = 0;
9360 
9361 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
9362 	if (!pmbox) {
9363 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9364 		/* processing mbox queue from intr_handler */
9365 		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9366 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9367 			return MBX_SUCCESS;
9368 		}
9369 		processing_queue = 1;
9370 		pmbox = lpfc_mbox_get(phba);
9371 		if (!pmbox) {
9372 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9373 			return MBX_SUCCESS;
9374 		}
9375 	}
9376 
9377 	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
9378 		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
9379 		if(!pmbox->vport) {
9380 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9381 			lpfc_printf_log(phba, KERN_ERR,
9382 					LOG_MBOX | LOG_VPORT,
9383 					"1806 Mbox x%x failed. No vport\n",
9384 					pmbox->u.mb.mbxCommand);
9385 			dump_stack();
9386 			goto out_not_finished;
9387 		}
9388 	}
9389 
9390 	/* If the PCI channel is in offline state, do not post mbox. */
9391 	if (unlikely(pci_channel_offline(phba->pcidev))) {
9392 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9393 		goto out_not_finished;
9394 	}
9395 
9396 	/* If HBA has a deferred error attention, fail the iocb. */
9397 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
9398 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9399 		goto out_not_finished;
9400 	}
9401 
9402 	psli = &phba->sli;
9403 
9404 	mbx = &pmbox->u.mb;
9405 	status = MBX_SUCCESS;
9406 
9407 	if (phba->link_state == LPFC_HBA_ERROR) {
9408 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9409 
9410 		/* Mbox command <mbxCommand> cannot issue */
9411 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9412 				"(%d):0311 Mailbox command x%x cannot "
9413 				"issue Data: x%x x%x\n",
9414 				pmbox->vport ? pmbox->vport->vpi : 0,
9415 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9416 		goto out_not_finished;
9417 	}
9418 
9419 	if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9420 		if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9421 			!(hc_copy & HC_MBINT_ENA)) {
9422 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9423 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9424 				"(%d):2528 Mailbox command x%x cannot "
9425 				"issue Data: x%x x%x\n",
9426 				pmbox->vport ? pmbox->vport->vpi : 0,
9427 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9428 			goto out_not_finished;
9429 		}
9430 	}
9431 
9432 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9433 		/* Polling for a mbox command when another one is already active
9434 		 * is not allowed in SLI. Also, the driver must have established
9435 		 * SLI2 mode to queue and process multiple mbox commands.
9436 		 */
9437 
9438 		if (flag & MBX_POLL) {
9439 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9440 
9441 			/* Mbox command <mbxCommand> cannot issue */
9442 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9443 					"(%d):2529 Mailbox command x%x "
9444 					"cannot issue Data: x%x x%x\n",
9445 					pmbox->vport ? pmbox->vport->vpi : 0,
9446 					pmbox->u.mb.mbxCommand,
9447 					psli->sli_flag, flag);
9448 			goto out_not_finished;
9449 		}
9450 
9451 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
9452 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9453 			/* Mbox command <mbxCommand> cannot issue */
9454 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9455 					"(%d):2530 Mailbox command x%x "
9456 					"cannot issue Data: x%x x%x\n",
9457 					pmbox->vport ? pmbox->vport->vpi : 0,
9458 					pmbox->u.mb.mbxCommand,
9459 					psli->sli_flag, flag);
9460 			goto out_not_finished;
9461 		}
9462 
9463 		/* Another mailbox command is still being processed, queue this
9464 		 * command to be processed later.
9465 		 */
9466 		lpfc_mbox_put(phba, pmbox);
9467 
9468 		/* Mbox cmd issue - BUSY */
9469 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9470 				"(%d):0308 Mbox cmd issue - BUSY Data: "
9471 				"x%x x%x x%x x%x\n",
9472 				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
9473 				mbx->mbxCommand,
9474 				phba->pport ? phba->pport->port_state : 0xff,
9475 				psli->sli_flag, flag);
9476 
9477 		psli->slistat.mbox_busy++;
9478 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9479 
9480 		if (pmbox->vport) {
9481 			lpfc_debugfs_disc_trc(pmbox->vport,
9482 				LPFC_DISC_TRC_MBOX_VPORT,
9483 				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
9484 				(uint32_t)mbx->mbxCommand,
9485 				mbx->un.varWords[0], mbx->un.varWords[1]);
9486 		}
9487 		else {
9488 			lpfc_debugfs_disc_trc(phba->pport,
9489 				LPFC_DISC_TRC_MBOX,
9490 				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
9491 				(uint32_t)mbx->mbxCommand,
9492 				mbx->un.varWords[0], mbx->un.varWords[1]);
9493 		}
9494 
9495 		return MBX_BUSY;
9496 	}
9497 
9498 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9499 
9500 	/* If we are not polling, we MUST be in SLI2 mode */
9501 	if (flag != MBX_POLL) {
9502 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
9503 		    (mbx->mbxCommand != MBX_KILL_BOARD)) {
9504 			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9505 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9506 			/* Mbox command <mbxCommand> cannot issue */
9507 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9508 					"(%d):2531 Mailbox command x%x "
9509 					"cannot issue Data: x%x x%x\n",
9510 					pmbox->vport ? pmbox->vport->vpi : 0,
9511 					pmbox->u.mb.mbxCommand,
9512 					psli->sli_flag, flag);
9513 			goto out_not_finished;
9514 		}
9515 		/* timeout active mbox command */
9516 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox));
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->ext_buf) {
9562 			lpfc_sli_pcimem_bcopy(pmbox->ext_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->ext_buf)
9576 			lpfc_memcpy_to_slim(phba->MBslimaddr +
9577 				MAILBOX_HBA_EXT_OFFSET,
9578 				pmbox->ext_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 = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox)) + jiffies;
9641 		i = 0;
9642 		/* Wait for command to complete */
9643 		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9644 		       (!(ha_copy & HA_MBATT) &&
9645 			(phba->link_state > LPFC_WARM_START))) {
9646 			if (time_after(jiffies, timeout)) {
9647 				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9648 				spin_unlock_irqrestore(&phba->hbalock,
9649 						       drvr_flag);
9650 				goto out_not_finished;
9651 			}
9652 
9653 			/* Check if we took a mbox interrupt while we were
9654 			   polling */
9655 			if (((word0 & OWN_CHIP) != OWN_CHIP)
9656 			    && (evtctr != psli->slistat.mbox_event))
9657 				break;
9658 
9659 			if (i++ > 10) {
9660 				spin_unlock_irqrestore(&phba->hbalock,
9661 						       drvr_flag);
9662 				msleep(1);
9663 				spin_lock_irqsave(&phba->hbalock, drvr_flag);
9664 			}
9665 
9666 			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9667 				/* First copy command data */
9668 				word0 = *((uint32_t *)phba->mbox);
9669 				word0 = le32_to_cpu(word0);
9670 				if (mbx->mbxCommand == MBX_CONFIG_PORT) {
9671 					MAILBOX_t *slimmb;
9672 					uint32_t slimword0;
9673 					/* Check real SLIM for any errors */
9674 					slimword0 = readl(phba->MBslimaddr);
9675 					slimmb = (MAILBOX_t *) & slimword0;
9676 					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9677 					    && slimmb->mbxStatus) {
9678 						psli->sli_flag &=
9679 						    ~LPFC_SLI_ACTIVE;
9680 						word0 = slimword0;
9681 					}
9682 				}
9683 			} else {
9684 				/* First copy command data */
9685 				word0 = readl(phba->MBslimaddr);
9686 			}
9687 			/* Read the HBA Host Attention Register */
9688 			if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9689 				spin_unlock_irqrestore(&phba->hbalock,
9690 						       drvr_flag);
9691 				goto out_not_finished;
9692 			}
9693 		}
9694 
9695 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9696 			/* copy results back to user */
9697 			lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9698 						MAILBOX_CMD_SIZE);
9699 			/* Copy the mailbox extension data */
9700 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9701 				lpfc_sli_pcimem_bcopy(phba->mbox_ext,
9702 						      pmbox->ext_buf,
9703 						      pmbox->out_ext_byte_len);
9704 			}
9705 		} else {
9706 			/* First copy command data */
9707 			lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
9708 						MAILBOX_CMD_SIZE);
9709 			/* Copy the mailbox extension data */
9710 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9711 				lpfc_memcpy_from_slim(
9712 					pmbox->ext_buf,
9713 					phba->MBslimaddr +
9714 					MAILBOX_HBA_EXT_OFFSET,
9715 					pmbox->out_ext_byte_len);
9716 			}
9717 		}
9718 
9719 		writel(HA_MBATT, phba->HAregaddr);
9720 		readl(phba->HAregaddr); /* flush */
9721 
9722 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9723 		status = mbx->mbxStatus;
9724 	}
9725 
9726 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9727 	return status;
9728 
9729 out_not_finished:
9730 	if (processing_queue) {
9731 		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
9732 		lpfc_mbox_cmpl_put(phba, pmbox);
9733 	}
9734 	return MBX_NOT_FINISHED;
9735 }
9736 
9737 /**
9738  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9739  * @phba: Pointer to HBA context object.
9740  *
9741  * The function blocks the posting of SLI4 asynchronous mailbox commands from
9742  * the driver internal pending mailbox queue. It will then try to wait out the
9743  * possible outstanding mailbox command before return.
9744  *
9745  * Returns:
9746  * 	0 - the outstanding mailbox command completed; otherwise, the wait for
9747  * 	the outstanding mailbox command timed out.
9748  **/
9749 static int
lpfc_sli4_async_mbox_block(struct lpfc_hba * phba)9750 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9751 {
9752 	struct lpfc_sli *psli = &phba->sli;
9753 	LPFC_MBOXQ_t *mboxq;
9754 	int rc = 0;
9755 	unsigned long timeout = 0;
9756 	u32 sli_flag;
9757 	u8 cmd, subsys, opcode;
9758 
9759 	/* Mark the asynchronous mailbox command posting as blocked */
9760 	spin_lock_irq(&phba->hbalock);
9761 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9762 	/* Determine how long we might wait for the active mailbox
9763 	 * command to be gracefully completed by firmware.
9764 	 */
9765 	if (phba->sli.mbox_active)
9766 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
9767 						phba->sli.mbox_active)) + jiffies;
9768 	spin_unlock_irq(&phba->hbalock);
9769 
9770 	/* Make sure the mailbox is really active */
9771 	if (timeout)
9772 		lpfc_sli4_process_missed_mbox_completions(phba);
9773 
9774 	/* Wait for the outstanding mailbox command to complete */
9775 	while (phba->sli.mbox_active) {
9776 		/* Check active mailbox complete status every 2ms */
9777 		msleep(2);
9778 		if (time_after(jiffies, timeout)) {
9779 			/* Timeout, mark the outstanding cmd not complete */
9780 
9781 			/* Sanity check sli.mbox_active has not completed or
9782 			 * cancelled from another context during last 2ms sleep,
9783 			 * so take hbalock to be sure before logging.
9784 			 */
9785 			spin_lock_irq(&phba->hbalock);
9786 			if (phba->sli.mbox_active) {
9787 				mboxq = phba->sli.mbox_active;
9788 				cmd = mboxq->u.mb.mbxCommand;
9789 				subsys = lpfc_sli_config_mbox_subsys_get(phba,
9790 									 mboxq);
9791 				opcode = lpfc_sli_config_mbox_opcode_get(phba,
9792 									 mboxq);
9793 				sli_flag = psli->sli_flag;
9794 				spin_unlock_irq(&phba->hbalock);
9795 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9796 						"2352 Mailbox command x%x "
9797 						"(x%x/x%x) sli_flag x%x could "
9798 						"not complete\n",
9799 						cmd, subsys, opcode,
9800 						sli_flag);
9801 			} else {
9802 				spin_unlock_irq(&phba->hbalock);
9803 			}
9804 
9805 			rc = 1;
9806 			break;
9807 		}
9808 	}
9809 
9810 	/* Can not cleanly block async mailbox command, fails it */
9811 	if (rc) {
9812 		spin_lock_irq(&phba->hbalock);
9813 		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9814 		spin_unlock_irq(&phba->hbalock);
9815 	}
9816 	return rc;
9817 }
9818 
9819 /**
9820  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9821  * @phba: Pointer to HBA context object.
9822  *
9823  * The function unblocks and resume posting of SLI4 asynchronous mailbox
9824  * commands from the driver internal pending mailbox queue. It makes sure
9825  * that there is no outstanding mailbox command before resuming posting
9826  * asynchronous mailbox commands. If, for any reason, there is outstanding
9827  * mailbox command, it will try to wait it out before resuming asynchronous
9828  * mailbox command posting.
9829  **/
9830 static void
lpfc_sli4_async_mbox_unblock(struct lpfc_hba * phba)9831 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9832 {
9833 	struct lpfc_sli *psli = &phba->sli;
9834 
9835 	spin_lock_irq(&phba->hbalock);
9836 	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9837 		/* Asynchronous mailbox posting is not blocked, do nothing */
9838 		spin_unlock_irq(&phba->hbalock);
9839 		return;
9840 	}
9841 
9842 	/* Outstanding synchronous mailbox command is guaranteed to be done,
9843 	 * successful or timeout, after timing-out the outstanding mailbox
9844 	 * command shall always be removed, so just unblock posting async
9845 	 * mailbox command and resume
9846 	 */
9847 	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9848 	spin_unlock_irq(&phba->hbalock);
9849 
9850 	/* wake up worker thread to post asynchronous mailbox command */
9851 	lpfc_worker_wake_up(phba);
9852 }
9853 
9854 /**
9855  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9856  * @phba: Pointer to HBA context object.
9857  * @mboxq: Pointer to mailbox object.
9858  *
9859  * The function waits for the bootstrap mailbox register ready bit from
9860  * port for twice the regular mailbox command timeout value.
9861  *
9862  *      0 - no timeout on waiting for bootstrap mailbox register ready.
9863  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out or port
9864  *                     is in an unrecoverable state.
9865  **/
9866 static int
lpfc_sli4_wait_bmbx_ready(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9867 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9868 {
9869 	uint32_t db_ready;
9870 	unsigned long timeout;
9871 	struct lpfc_register bmbx_reg;
9872 	struct lpfc_register portstat_reg = {-1};
9873 
9874 	/* Sanity check - there is no point to wait if the port is in an
9875 	 * unrecoverable state.
9876 	 */
9877 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
9878 	    LPFC_SLI_INTF_IF_TYPE_2) {
9879 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9880 			       &portstat_reg.word0) ||
9881 		    lpfc_sli4_unrecoverable_port(&portstat_reg)) {
9882 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9883 					"3858 Skipping bmbx ready because "
9884 					"Port Status x%x\n",
9885 					portstat_reg.word0);
9886 			return MBXERR_ERROR;
9887 		}
9888 	}
9889 
9890 	timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)) + jiffies;
9891 
9892 	do {
9893 		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9894 		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9895 		if (!db_ready)
9896 			mdelay(2);
9897 
9898 		if (time_after(jiffies, timeout))
9899 			return MBXERR_ERROR;
9900 	} while (!db_ready);
9901 
9902 	return 0;
9903 }
9904 
9905 /**
9906  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9907  * @phba: Pointer to HBA context object.
9908  * @mboxq: Pointer to mailbox object.
9909  *
9910  * The function posts a mailbox to the port.  The mailbox is expected
9911  * to be comletely filled in and ready for the port to operate on it.
9912  * This routine executes a synchronous completion operation on the
9913  * mailbox by polling for its completion.
9914  *
9915  * The caller must not be holding any locks when calling this routine.
9916  *
9917  * Returns:
9918  *	MBX_SUCCESS - mailbox posted successfully
9919  *	Any of the MBX error values.
9920  **/
9921 static int
lpfc_sli4_post_sync_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9922 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9923 {
9924 	int rc = MBX_SUCCESS;
9925 	unsigned long iflag;
9926 	uint32_t mcqe_status;
9927 	uint32_t mbx_cmnd;
9928 	struct lpfc_sli *psli = &phba->sli;
9929 	struct lpfc_mqe *mb = &mboxq->u.mqe;
9930 	struct lpfc_bmbx_create *mbox_rgn;
9931 	struct dma_address *dma_address;
9932 
9933 	/*
9934 	 * Only one mailbox can be active to the bootstrap mailbox region
9935 	 * at a time and there is no queueing provided.
9936 	 */
9937 	spin_lock_irqsave(&phba->hbalock, iflag);
9938 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9939 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9940 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9941 				"(%d):2532 Mailbox command x%x (x%x/x%x) "
9942 				"cannot issue Data: x%x x%x\n",
9943 				mboxq->vport ? mboxq->vport->vpi : 0,
9944 				mboxq->u.mb.mbxCommand,
9945 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9946 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9947 				psli->sli_flag, MBX_POLL);
9948 		return MBXERR_ERROR;
9949 	}
9950 	/* The server grabs the token and owns it until release */
9951 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9952 	phba->sli.mbox_active = mboxq;
9953 	spin_unlock_irqrestore(&phba->hbalock, iflag);
9954 
9955 	/* wait for bootstrap mbox register for readyness */
9956 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9957 	if (rc)
9958 		goto exit;
9959 	/*
9960 	 * Initialize the bootstrap memory region to avoid stale data areas
9961 	 * in the mailbox post.  Then copy the caller's mailbox contents to
9962 	 * the bmbx mailbox region.
9963 	 */
9964 	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
9965 	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
9966 	lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
9967 			       sizeof(struct lpfc_mqe));
9968 
9969 	/* Post the high mailbox dma address to the port and wait for ready. */
9970 	dma_address = &phba->sli4_hba.bmbx.dma_address;
9971 	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
9972 
9973 	/* wait for bootstrap mbox register for hi-address write done */
9974 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9975 	if (rc)
9976 		goto exit;
9977 
9978 	/* Post the low mailbox dma address to the port. */
9979 	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
9980 
9981 	/* wait for bootstrap mbox register for low address write done */
9982 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9983 	if (rc)
9984 		goto exit;
9985 
9986 	/*
9987 	 * Read the CQ to ensure the mailbox has completed.
9988 	 * If so, update the mailbox status so that the upper layers
9989 	 * can complete the request normally.
9990 	 */
9991 	lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
9992 			       sizeof(struct lpfc_mqe));
9993 	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
9994 	lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
9995 			       sizeof(struct lpfc_mcqe));
9996 	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
9997 	/*
9998 	 * When the CQE status indicates a failure and the mailbox status
9999 	 * indicates success then copy the CQE status into the mailbox status
10000 	 * (and prefix it with x4000).
10001 	 */
10002 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10003 		if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
10004 			bf_set(lpfc_mqe_status, mb,
10005 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
10006 		rc = MBXERR_ERROR;
10007 	} else
10008 		lpfc_sli4_swap_str(phba, mboxq);
10009 
10010 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10011 			"(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
10012 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
10013 			" x%x x%x CQ: x%x x%x x%x x%x\n",
10014 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10015 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10016 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10017 			bf_get(lpfc_mqe_status, mb),
10018 			mb->un.mb_words[0], mb->un.mb_words[1],
10019 			mb->un.mb_words[2], mb->un.mb_words[3],
10020 			mb->un.mb_words[4], mb->un.mb_words[5],
10021 			mb->un.mb_words[6], mb->un.mb_words[7],
10022 			mb->un.mb_words[8], mb->un.mb_words[9],
10023 			mb->un.mb_words[10], mb->un.mb_words[11],
10024 			mb->un.mb_words[12], mboxq->mcqe.word0,
10025 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
10026 			mboxq->mcqe.trailer);
10027 exit:
10028 	/* We are holding the token, no needed for lock when release */
10029 	spin_lock_irqsave(&phba->hbalock, iflag);
10030 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10031 	phba->sli.mbox_active = NULL;
10032 	spin_unlock_irqrestore(&phba->hbalock, iflag);
10033 	return rc;
10034 }
10035 
10036 /**
10037  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
10038  * @phba: Pointer to HBA context object.
10039  * @mboxq: Pointer to mailbox object.
10040  * @flag: Flag indicating how the mailbox need to be processed.
10041  *
10042  * This function is called by discovery code and HBA management code to submit
10043  * a mailbox command to firmware with SLI-4 interface spec.
10044  *
10045  * Return codes the caller owns the mailbox command after the return of the
10046  * function.
10047  **/
10048 static int
lpfc_sli_issue_mbox_s4(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint32_t flag)10049 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
10050 		       uint32_t flag)
10051 {
10052 	struct lpfc_sli *psli = &phba->sli;
10053 	unsigned long iflags;
10054 	int rc;
10055 
10056 	/* dump from issue mailbox command if setup */
10057 	lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
10058 
10059 	rc = lpfc_mbox_dev_check(phba);
10060 	if (unlikely(rc)) {
10061 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10062 				"(%d):2544 Mailbox command x%x (x%x/x%x) "
10063 				"cannot issue Data: x%x x%x\n",
10064 				mboxq->vport ? mboxq->vport->vpi : 0,
10065 				mboxq->u.mb.mbxCommand,
10066 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10067 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10068 				psli->sli_flag, flag);
10069 		goto out_not_finished;
10070 	}
10071 
10072 	/* Detect polling mode and jump to a handler */
10073 	if (!phba->sli4_hba.intr_enable) {
10074 		if (flag == MBX_POLL)
10075 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10076 		else
10077 			rc = -EIO;
10078 		if (rc != MBX_SUCCESS)
10079 			lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10080 					"(%d):2541 Mailbox command x%x "
10081 					"(x%x/x%x) failure: "
10082 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10083 					"Data: x%x x%x\n",
10084 					mboxq->vport ? mboxq->vport->vpi : 0,
10085 					mboxq->u.mb.mbxCommand,
10086 					lpfc_sli_config_mbox_subsys_get(phba,
10087 									mboxq),
10088 					lpfc_sli_config_mbox_opcode_get(phba,
10089 									mboxq),
10090 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10091 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10092 					bf_get(lpfc_mcqe_ext_status,
10093 					       &mboxq->mcqe),
10094 					psli->sli_flag, flag);
10095 		return rc;
10096 	} else if (flag == MBX_POLL) {
10097 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10098 				"(%d):2542 Try to issue mailbox command "
10099 				"x%x (x%x/x%x) synchronously ahead of async "
10100 				"mailbox command queue: x%x x%x\n",
10101 				mboxq->vport ? mboxq->vport->vpi : 0,
10102 				mboxq->u.mb.mbxCommand,
10103 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10104 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10105 				psli->sli_flag, flag);
10106 		/* Try to block the asynchronous mailbox posting */
10107 		rc = lpfc_sli4_async_mbox_block(phba);
10108 		if (!rc) {
10109 			/* Successfully blocked, now issue sync mbox cmd */
10110 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10111 			if (rc != MBX_SUCCESS)
10112 				lpfc_printf_log(phba, KERN_WARNING,
10113 					LOG_MBOX | LOG_SLI,
10114 					"(%d):2597 Sync Mailbox command "
10115 					"x%x (x%x/x%x) failure: "
10116 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10117 					"Data: x%x x%x\n",
10118 					mboxq->vport ? mboxq->vport->vpi : 0,
10119 					mboxq->u.mb.mbxCommand,
10120 					lpfc_sli_config_mbox_subsys_get(phba,
10121 									mboxq),
10122 					lpfc_sli_config_mbox_opcode_get(phba,
10123 									mboxq),
10124 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10125 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10126 					bf_get(lpfc_mcqe_ext_status,
10127 					       &mboxq->mcqe),
10128 					psli->sli_flag, flag);
10129 			/* Unblock the async mailbox posting afterward */
10130 			lpfc_sli4_async_mbox_unblock(phba);
10131 		}
10132 		return rc;
10133 	}
10134 
10135 	/* Now, interrupt mode asynchronous mailbox command */
10136 	rc = lpfc_mbox_cmd_check(phba, mboxq);
10137 	if (rc) {
10138 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10139 				"(%d):2543 Mailbox command x%x (x%x/x%x) "
10140 				"cannot issue Data: x%x x%x\n",
10141 				mboxq->vport ? mboxq->vport->vpi : 0,
10142 				mboxq->u.mb.mbxCommand,
10143 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10144 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10145 				psli->sli_flag, flag);
10146 		goto out_not_finished;
10147 	}
10148 
10149 	/* Put the mailbox command to the driver internal FIFO */
10150 	psli->slistat.mbox_busy++;
10151 	spin_lock_irqsave(&phba->hbalock, iflags);
10152 	lpfc_mbox_put(phba, mboxq);
10153 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10154 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10155 			"(%d):0354 Mbox cmd issue - Enqueue Data: "
10156 			"x%x (x%x/x%x) x%x x%x x%x x%x\n",
10157 			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
10158 			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
10159 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10160 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10161 			mboxq->u.mb.un.varUnregLogin.rpi,
10162 			phba->pport->port_state,
10163 			psli->sli_flag, MBX_NOWAIT);
10164 	/* Wake up worker thread to transport mailbox command from head */
10165 	lpfc_worker_wake_up(phba);
10166 
10167 	return MBX_BUSY;
10168 
10169 out_not_finished:
10170 	return MBX_NOT_FINISHED;
10171 }
10172 
10173 /**
10174  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
10175  * @phba: Pointer to HBA context object.
10176  *
10177  * This function is called by worker thread to send a mailbox command to
10178  * SLI4 HBA firmware.
10179  *
10180  **/
10181 int
lpfc_sli4_post_async_mbox(struct lpfc_hba * phba)10182 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
10183 {
10184 	struct lpfc_sli *psli = &phba->sli;
10185 	LPFC_MBOXQ_t *mboxq;
10186 	int rc = MBX_SUCCESS;
10187 	unsigned long iflags;
10188 	struct lpfc_mqe *mqe;
10189 	uint32_t mbx_cmnd;
10190 
10191 	/* Check interrupt mode before post async mailbox command */
10192 	if (unlikely(!phba->sli4_hba.intr_enable))
10193 		return MBX_NOT_FINISHED;
10194 
10195 	/* Check for mailbox command service token */
10196 	spin_lock_irqsave(&phba->hbalock, iflags);
10197 	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
10198 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10199 		return MBX_NOT_FINISHED;
10200 	}
10201 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10202 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10203 		return MBX_NOT_FINISHED;
10204 	}
10205 	if (unlikely(phba->sli.mbox_active)) {
10206 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10207 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10208 				"0384 There is pending active mailbox cmd\n");
10209 		return MBX_NOT_FINISHED;
10210 	}
10211 	/* Take the mailbox command service token */
10212 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
10213 
10214 	/* Get the next mailbox command from head of queue */
10215 	mboxq = lpfc_mbox_get(phba);
10216 
10217 	/* If no more mailbox command waiting for post, we're done */
10218 	if (!mboxq) {
10219 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10220 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10221 		return MBX_SUCCESS;
10222 	}
10223 	phba->sli.mbox_active = mboxq;
10224 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10225 
10226 	/* Check device readiness for posting mailbox command */
10227 	rc = lpfc_mbox_dev_check(phba);
10228 	if (unlikely(rc))
10229 		/* Driver clean routine will clean up pending mailbox */
10230 		goto out_not_finished;
10231 
10232 	/* Prepare the mbox command to be posted */
10233 	mqe = &mboxq->u.mqe;
10234 	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
10235 
10236 	/* Start timer for the mbox_tmo and log some mailbox post messages */
10237 	mod_timer(&psli->mbox_tmo, (jiffies +
10238 		  secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq))));
10239 
10240 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10241 			"(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
10242 			"x%x x%x\n",
10243 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10244 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10245 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10246 			phba->pport->port_state, psli->sli_flag);
10247 
10248 	if (mbx_cmnd != MBX_HEARTBEAT) {
10249 		if (mboxq->vport) {
10250 			lpfc_debugfs_disc_trc(mboxq->vport,
10251 				LPFC_DISC_TRC_MBOX_VPORT,
10252 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
10253 				mbx_cmnd, mqe->un.mb_words[0],
10254 				mqe->un.mb_words[1]);
10255 		} else {
10256 			lpfc_debugfs_disc_trc(phba->pport,
10257 				LPFC_DISC_TRC_MBOX,
10258 				"MBOX Send: cmd:x%x mb:x%x x%x",
10259 				mbx_cmnd, mqe->un.mb_words[0],
10260 				mqe->un.mb_words[1]);
10261 		}
10262 	}
10263 	psli->slistat.mbox_cmd++;
10264 
10265 	/* Post the mailbox command to the port */
10266 	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
10267 	if (rc != MBX_SUCCESS) {
10268 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10269 				"(%d):2533 Mailbox command x%x (x%x/x%x) "
10270 				"cannot issue Data: x%x x%x\n",
10271 				mboxq->vport ? mboxq->vport->vpi : 0,
10272 				mboxq->u.mb.mbxCommand,
10273 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10274 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10275 				psli->sli_flag, MBX_NOWAIT);
10276 		goto out_not_finished;
10277 	}
10278 
10279 	return rc;
10280 
10281 out_not_finished:
10282 	spin_lock_irqsave(&phba->hbalock, iflags);
10283 	if (phba->sli.mbox_active) {
10284 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10285 		__lpfc_mbox_cmpl_put(phba, mboxq);
10286 		/* Release the token */
10287 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10288 		phba->sli.mbox_active = NULL;
10289 	}
10290 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10291 
10292 	return MBX_NOT_FINISHED;
10293 }
10294 
10295 /**
10296  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
10297  * @phba: Pointer to HBA context object.
10298  * @pmbox: Pointer to mailbox object.
10299  * @flag: Flag indicating how the mailbox need to be processed.
10300  *
10301  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
10302  * the API jump table function pointer from the lpfc_hba struct.
10303  *
10304  * Return codes the caller owns the mailbox command after the return of the
10305  * function.
10306  **/
10307 int
lpfc_sli_issue_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)10308 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
10309 {
10310 	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
10311 }
10312 
10313 /**
10314  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
10315  * @phba: The hba struct for which this call is being executed.
10316  * @dev_grp: The HBA PCI-Device group number.
10317  *
10318  * This routine sets up the mbox interface API function jump table in @phba
10319  * struct.
10320  * Returns: 0 - success, -ENODEV - failure.
10321  **/
10322 int
lpfc_mbox_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)10323 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10324 {
10325 
10326 	switch (dev_grp) {
10327 	case LPFC_PCI_DEV_LP:
10328 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
10329 		phba->lpfc_sli_handle_slow_ring_event =
10330 				lpfc_sli_handle_slow_ring_event_s3;
10331 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
10332 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
10333 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
10334 		break;
10335 	case LPFC_PCI_DEV_OC:
10336 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
10337 		phba->lpfc_sli_handle_slow_ring_event =
10338 				lpfc_sli_handle_slow_ring_event_s4;
10339 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10340 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10341 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10342 		break;
10343 	default:
10344 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10345 				"1420 Invalid HBA PCI-device group: 0x%x\n",
10346 				dev_grp);
10347 		return -ENODEV;
10348 	}
10349 	return 0;
10350 }
10351 
10352 /**
10353  * __lpfc_sli_ringtx_put - Add an iocb to the txq
10354  * @phba: Pointer to HBA context object.
10355  * @pring: Pointer to driver SLI ring object.
10356  * @piocb: Pointer to address of newly added command iocb.
10357  *
10358  * This function is called with hbalock held for SLI3 ports or
10359  * the ring lock held for SLI4 ports to add a command
10360  * iocb to the txq when SLI layer cannot submit the command iocb
10361  * to the ring.
10362  **/
10363 void
__lpfc_sli_ringtx_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)10364 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10365 		    struct lpfc_iocbq *piocb)
10366 {
10367 	if (phba->sli_rev == LPFC_SLI_REV4)
10368 		lockdep_assert_held(&pring->ring_lock);
10369 	else
10370 		lockdep_assert_held(&phba->hbalock);
10371 	/* Insert the caller's iocb in the txq tail for later processing. */
10372 	list_add_tail(&piocb->list, &pring->txq);
10373 }
10374 
10375 /**
10376  * lpfc_sli_next_iocb - Get the next iocb in the txq
10377  * @phba: Pointer to HBA context object.
10378  * @pring: Pointer to driver SLI ring object.
10379  * @piocb: Pointer to address of newly added command iocb.
10380  *
10381  * This function is called with hbalock held before a new
10382  * iocb is submitted to the firmware. This function checks
10383  * txq to flush the iocbs in txq to Firmware before
10384  * submitting new iocbs to the Firmware.
10385  * If there are iocbs in the txq which need to be submitted
10386  * to firmware, lpfc_sli_next_iocb returns the first element
10387  * of the txq after dequeuing it from txq.
10388  * If there is no iocb in the txq then the function will return
10389  * *piocb and *piocb is set to NULL. Caller needs to check
10390  * *piocb to find if there are more commands in the txq.
10391  **/
10392 static struct lpfc_iocbq *
lpfc_sli_next_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq ** piocb)10393 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10394 		   struct lpfc_iocbq **piocb)
10395 {
10396 	struct lpfc_iocbq * nextiocb;
10397 
10398 	lockdep_assert_held(&phba->hbalock);
10399 
10400 	nextiocb = lpfc_sli_ringtx_get(phba, pring);
10401 	if (!nextiocb) {
10402 		nextiocb = *piocb;
10403 		*piocb = NULL;
10404 	}
10405 
10406 	return nextiocb;
10407 }
10408 
10409 /**
10410  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
10411  * @phba: Pointer to HBA context object.
10412  * @ring_number: SLI ring number to issue iocb on.
10413  * @piocb: Pointer to command iocb.
10414  * @flag: Flag indicating if this command can be put into txq.
10415  *
10416  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10417  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10418  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10419  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10420  * this function allows only iocbs for posting buffers. This function finds
10421  * next available slot in the command ring and posts the command to the
10422  * available slot and writes the port attention register to request HBA start
10423  * processing new iocb. If there is no slot available in the ring and
10424  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10425  * the function returns IOCB_BUSY.
10426  *
10427  * This function is called with hbalock held. The function will return success
10428  * after it successfully submit the iocb to firmware or after adding to the
10429  * txq.
10430  **/
10431 static int
__lpfc_sli_issue_iocb_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10432 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
10433 		    struct lpfc_iocbq *piocb, uint32_t flag)
10434 {
10435 	struct lpfc_iocbq *nextiocb;
10436 	IOCB_t *iocb;
10437 	struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
10438 
10439 	lockdep_assert_held(&phba->hbalock);
10440 
10441 	if (piocb->cmd_cmpl && (!piocb->vport) &&
10442 	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10443 	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
10444 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10445 				"1807 IOCB x%x failed. No vport\n",
10446 				piocb->iocb.ulpCommand);
10447 		dump_stack();
10448 		return IOCB_ERROR;
10449 	}
10450 
10451 
10452 	/* If the PCI channel is in offline state, do not post iocbs. */
10453 	if (unlikely(pci_channel_offline(phba->pcidev)))
10454 		return IOCB_ERROR;
10455 
10456 	/* If HBA has a deferred error attention, fail the iocb. */
10457 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
10458 		return IOCB_ERROR;
10459 
10460 	/*
10461 	 * We should never get an IOCB if we are in a < LINK_DOWN state
10462 	 */
10463 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10464 		return IOCB_ERROR;
10465 
10466 	/*
10467 	 * Check to see if we are blocking IOCB processing because of a
10468 	 * outstanding event.
10469 	 */
10470 	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
10471 		goto iocb_busy;
10472 
10473 	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
10474 		/*
10475 		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
10476 		 * can be issued if the link is not up.
10477 		 */
10478 		switch (piocb->iocb.ulpCommand) {
10479 		case CMD_QUE_RING_BUF_CN:
10480 		case CMD_QUE_RING_BUF64_CN:
10481 			/*
10482 			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10483 			 * completion, cmd_cmpl MUST be 0.
10484 			 */
10485 			if (piocb->cmd_cmpl)
10486 				piocb->cmd_cmpl = NULL;
10487 			fallthrough;
10488 		case CMD_CREATE_XRI_CR:
10489 		case CMD_CLOSE_XRI_CN:
10490 		case CMD_CLOSE_XRI_CX:
10491 			break;
10492 		default:
10493 			goto iocb_busy;
10494 		}
10495 
10496 	/*
10497 	 * For FCP commands, we must be in a state where we can process link
10498 	 * attention events.
10499 	 */
10500 	} else if (unlikely(pring->ringno == LPFC_FCP_RING &&
10501 			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
10502 		goto iocb_busy;
10503 	}
10504 
10505 	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10506 	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10507 		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10508 
10509 	if (iocb)
10510 		lpfc_sli_update_ring(phba, pring);
10511 	else
10512 		lpfc_sli_update_full_ring(phba, pring);
10513 
10514 	if (!piocb)
10515 		return IOCB_SUCCESS;
10516 
10517 	goto out_busy;
10518 
10519  iocb_busy:
10520 	pring->stats.iocb_cmd_delay++;
10521 
10522  out_busy:
10523 
10524 	if (!(flag & SLI_IOCB_RET_IOCB)) {
10525 		__lpfc_sli_ringtx_put(phba, pring, piocb);
10526 		return IOCB_SUCCESS;
10527 	}
10528 
10529 	return IOCB_BUSY;
10530 }
10531 
10532 /**
10533  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10534  * @phba: Pointer to HBA context object.
10535  * @ring_number: SLI ring number to issue wqe on.
10536  * @piocb: Pointer to command iocb.
10537  * @flag: Flag indicating if this command can be put into txq.
10538  *
10539  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10540  * send  an iocb command to an HBA with SLI-3 interface spec.
10541  *
10542  * This function takes the hbalock before invoking the lockless version.
10543  * The function will return success after it successfully submit the wqe to
10544  * firmware or after adding to the txq.
10545  **/
10546 static int
__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10547 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10548 			   struct lpfc_iocbq *piocb, uint32_t flag)
10549 {
10550 	unsigned long iflags;
10551 	int rc;
10552 
10553 	spin_lock_irqsave(&phba->hbalock, iflags);
10554 	rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10555 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10556 
10557 	return rc;
10558 }
10559 
10560 /**
10561  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10562  * @phba: Pointer to HBA context object.
10563  * @ring_number: SLI ring number to issue wqe on.
10564  * @piocb: Pointer to command iocb.
10565  * @flag: Flag indicating if this command can be put into txq.
10566  *
10567  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10568  * an wqe command to an HBA with SLI-4 interface spec.
10569  *
10570  * This function is a lockless version. The function will return success
10571  * after it successfully submit the wqe to firmware or after adding to the
10572  * txq.
10573  **/
10574 static int
__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10575 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10576 			   struct lpfc_iocbq *piocb, uint32_t flag)
10577 {
10578 	struct lpfc_io_buf *lpfc_cmd = piocb->io_buf;
10579 
10580 	lpfc_prep_embed_io(phba, lpfc_cmd);
10581 	return lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10582 }
10583 
10584 void
lpfc_prep_embed_io(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_cmd)10585 lpfc_prep_embed_io(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
10586 {
10587 	struct lpfc_iocbq *piocb = &lpfc_cmd->cur_iocbq;
10588 	union lpfc_wqe128 *wqe = &lpfc_cmd->cur_iocbq.wqe;
10589 	struct sli4_sge_le *sgl;
10590 	u32 type_size;
10591 
10592 	/* 128 byte wqe support here */
10593 	sgl = (struct sli4_sge_le *)lpfc_cmd->dma_sgl;
10594 
10595 	if (phba->fcp_embed_io) {
10596 		struct fcp_cmnd *fcp_cmnd;
10597 		u32 *ptr;
10598 
10599 		fcp_cmnd = lpfc_cmd->fcp_cmnd;
10600 
10601 		/* Word 0-2 - FCP_CMND */
10602 		type_size = le32_to_cpu(sgl->sge_len);
10603 		type_size |= ULP_BDE64_TYPE_BDE_IMMED;
10604 		wqe->generic.bde.tus.w = type_size;
10605 		wqe->generic.bde.addrHigh = 0;
10606 		wqe->generic.bde.addrLow =  72;  /* Word 18 */
10607 
10608 		bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10609 		bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10610 
10611 		/* Word 18-29  FCP CMND Payload */
10612 		ptr = &wqe->words[18];
10613 		lpfc_sli_pcimem_bcopy(fcp_cmnd, ptr, le32_to_cpu(sgl->sge_len));
10614 	} else {
10615 		/* Word 0-2 - Inline BDE */
10616 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10617 		wqe->generic.bde.tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
10618 		wqe->generic.bde.addrHigh = le32_to_cpu(sgl->addr_hi);
10619 		wqe->generic.bde.addrLow = le32_to_cpu(sgl->addr_lo);
10620 
10621 		/* Word 10 */
10622 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10623 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10624 	}
10625 
10626 	/* add the VMID tags as per switch response */
10627 	if (unlikely(piocb->cmd_flag & LPFC_IO_VMID)) {
10628 		if (phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
10629 			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10630 			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10631 					(piocb->vmid_tag.cs_ctl_vmid));
10632 		} else if (phba->cfg_vmid_app_header) {
10633 			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
10634 			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10635 			wqe->words[31] = piocb->vmid_tag.app_id;
10636 		}
10637 	}
10638 }
10639 
10640 /**
10641  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10642  * @phba: Pointer to HBA context object.
10643  * @ring_number: SLI ring number to issue iocb on.
10644  * @piocb: Pointer to command iocb.
10645  * @flag: Flag indicating if this command can be put into txq.
10646  *
10647  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10648  * an iocb command to an HBA with SLI-4 interface spec.
10649  *
10650  * This function is called with ringlock held. The function will return success
10651  * after it successfully submit the iocb to firmware or after adding to the
10652  * txq.
10653  **/
10654 static int
__lpfc_sli_issue_iocb_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10655 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10656 			 struct lpfc_iocbq *piocb, uint32_t flag)
10657 {
10658 	struct lpfc_sglq *sglq;
10659 	union lpfc_wqe128 *wqe;
10660 	struct lpfc_queue *wq;
10661 	struct lpfc_sli_ring *pring;
10662 	u32 ulp_command = get_job_cmnd(phba, piocb);
10663 
10664 	/* Get the WQ */
10665 	if ((piocb->cmd_flag & LPFC_IO_FCP) ||
10666 	    (piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
10667 		wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10668 	} else {
10669 		wq = phba->sli4_hba.els_wq;
10670 	}
10671 
10672 	/* Get corresponding ring */
10673 	pring = wq->pring;
10674 
10675 	/*
10676 	 * The WQE can be either 64 or 128 bytes,
10677 	 */
10678 
10679 	lockdep_assert_held(&pring->ring_lock);
10680 	wqe = &piocb->wqe;
10681 	if (piocb->sli4_xritag == NO_XRI) {
10682 		if (ulp_command == CMD_ABORT_XRI_CX)
10683 			sglq = NULL;
10684 		else {
10685 			sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10686 			if (!sglq) {
10687 				if (!(flag & SLI_IOCB_RET_IOCB)) {
10688 					__lpfc_sli_ringtx_put(phba,
10689 							pring,
10690 							piocb);
10691 					return IOCB_SUCCESS;
10692 				} else {
10693 					return IOCB_BUSY;
10694 				}
10695 			}
10696 		}
10697 	} else if (piocb->cmd_flag &  LPFC_IO_FCP) {
10698 		/* These IO's already have an XRI and a mapped sgl. */
10699 		sglq = NULL;
10700 	}
10701 	else {
10702 		/*
10703 		 * This is a continuation of a commandi,(CX) so this
10704 		 * sglq is on the active list
10705 		 */
10706 		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10707 		if (!sglq)
10708 			return IOCB_ERROR;
10709 	}
10710 
10711 	if (sglq) {
10712 		piocb->sli4_lxritag = sglq->sli4_lxritag;
10713 		piocb->sli4_xritag = sglq->sli4_xritag;
10714 
10715 		/* ABTS sent by initiator to CT exchange, the
10716 		 * RX_ID field will be filled with the newly
10717 		 * allocated responder XRI.
10718 		 */
10719 		if (ulp_command == CMD_XMIT_BLS_RSP64_CX &&
10720 		    piocb->abort_bls == LPFC_ABTS_UNSOL_INT)
10721 			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10722 			       piocb->sli4_xritag);
10723 
10724 		bf_set(wqe_xri_tag, &wqe->generic.wqe_com,
10725 		       piocb->sli4_xritag);
10726 
10727 		if (lpfc_wqe_bpl2sgl(phba, piocb, sglq) == NO_XRI)
10728 			return IOCB_ERROR;
10729 	}
10730 
10731 	if (lpfc_sli4_wq_put(wq, wqe))
10732 		return IOCB_ERROR;
10733 
10734 	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10735 
10736 	return 0;
10737 }
10738 
10739 /*
10740  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10741  *
10742  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10743  * or IOCB for sli-3  function.
10744  * pointer from the lpfc_hba struct.
10745  *
10746  * Return codes:
10747  * IOCB_ERROR - Error
10748  * IOCB_SUCCESS - Success
10749  * IOCB_BUSY - Busy
10750  **/
10751 int
lpfc_sli_issue_fcp_io(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10752 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10753 		      struct lpfc_iocbq *piocb, uint32_t flag)
10754 {
10755 	return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10756 }
10757 
10758 /*
10759  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10760  *
10761  * This routine wraps the actual lockless version for issusing IOCB function
10762  * pointer from the lpfc_hba struct.
10763  *
10764  * Return codes:
10765  * IOCB_ERROR - Error
10766  * IOCB_SUCCESS - Success
10767  * IOCB_BUSY - Busy
10768  **/
10769 int
__lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10770 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10771 		struct lpfc_iocbq *piocb, uint32_t flag)
10772 {
10773 	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10774 }
10775 
10776 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)10777 __lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq *cmdiocbq,
10778 			       struct lpfc_vport *vport,
10779 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10780 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10781 {
10782 	struct lpfc_hba *phba = vport->phba;
10783 	IOCB_t *cmd;
10784 
10785 	cmd = &cmdiocbq->iocb;
10786 	memset(cmd, 0, sizeof(*cmd));
10787 
10788 	cmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10789 	cmd->un.elsreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10790 	cmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10791 
10792 	if (expect_rsp) {
10793 		cmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
10794 		cmd->un.elsreq64.remoteID = did; /* DID */
10795 		cmd->ulpCommand = CMD_ELS_REQUEST64_CR;
10796 		cmd->ulpTimeout = tmo;
10797 	} else {
10798 		cmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
10799 		cmd->un.genreq64.xmit_els_remoteID = did; /* DID */
10800 		cmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
10801 		cmd->ulpPU = PARM_NPIV_DID;
10802 	}
10803 	cmd->ulpBdeCount = 1;
10804 	cmd->ulpLe = 1;
10805 	cmd->ulpClass = CLASS3;
10806 
10807 	/* If we have NPIV enabled, we want to send ELS traffic by VPI. */
10808 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
10809 		if (expect_rsp) {
10810 			cmd->un.elsreq64.myID = vport->fc_myDID;
10811 
10812 			/* For ELS_REQUEST64_CR, use the VPI by default */
10813 			cmd->ulpContext = phba->vpi_ids[vport->vpi];
10814 		}
10815 
10816 		cmd->ulpCt_h = 0;
10817 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10818 		if (elscmd == ELS_CMD_ECHO)
10819 			cmd->ulpCt_l = 0; /* context = invalid RPI */
10820 		else
10821 			cmd->ulpCt_l = 1; /* context = VPI */
10822 	}
10823 }
10824 
10825 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)10826 __lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq *cmdiocbq,
10827 			       struct lpfc_vport *vport,
10828 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10829 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10830 {
10831 	struct lpfc_hba  *phba = vport->phba;
10832 	union lpfc_wqe128 *wqe;
10833 	struct ulp_bde64_le *bde;
10834 	u8 els_id;
10835 
10836 	wqe = &cmdiocbq->wqe;
10837 	memset(wqe, 0, sizeof(*wqe));
10838 
10839 	/* Word 0 - 2 BDE */
10840 	bde = (struct ulp_bde64_le *)&wqe->generic.bde;
10841 	bde->addr_low = cpu_to_le32(putPaddrLow(bmp->phys));
10842 	bde->addr_high = cpu_to_le32(putPaddrHigh(bmp->phys));
10843 	bde->type_size = cpu_to_le32(cmd_size);
10844 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10845 
10846 	if (expect_rsp) {
10847 		bf_set(wqe_cmnd, &wqe->els_req.wqe_com, CMD_ELS_REQUEST64_WQE);
10848 
10849 		/* Transfer length */
10850 		wqe->els_req.payload_len = cmd_size;
10851 		wqe->els_req.max_response_payload_len = FCELSSIZE;
10852 
10853 		/* DID */
10854 		bf_set(wqe_els_did, &wqe->els_req.wqe_dest, did);
10855 
10856 		/* Word 11 - ELS_ID */
10857 		switch (elscmd) {
10858 		case ELS_CMD_PLOGI:
10859 			els_id = LPFC_ELS_ID_PLOGI;
10860 			break;
10861 		case ELS_CMD_FLOGI:
10862 			els_id = LPFC_ELS_ID_FLOGI;
10863 			break;
10864 		case ELS_CMD_LOGO:
10865 			els_id = LPFC_ELS_ID_LOGO;
10866 			break;
10867 		case ELS_CMD_FDISC:
10868 			if (!vport->fc_myDID) {
10869 				els_id = LPFC_ELS_ID_FDISC;
10870 				break;
10871 			}
10872 			fallthrough;
10873 		default:
10874 			els_id = LPFC_ELS_ID_DEFAULT;
10875 			break;
10876 		}
10877 
10878 		bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10879 	} else {
10880 		/* DID */
10881 		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest, did);
10882 
10883 		/* Transfer length */
10884 		wqe->xmit_els_rsp.response_payload_len = cmd_size;
10885 
10886 		bf_set(wqe_cmnd, &wqe->xmit_els_rsp.wqe_com,
10887 		       CMD_XMIT_ELS_RSP64_WQE);
10888 	}
10889 
10890 	bf_set(wqe_tmo, &wqe->generic.wqe_com, tmo);
10891 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, cmdiocbq->iotag);
10892 	bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
10893 
10894 	/* If we have NPIV enabled, we want to send ELS traffic by VPI.
10895 	 * For SLI4, since the driver controls VPIs we also want to include
10896 	 * all ELS pt2pt protocol traffic as well.
10897 	 */
10898 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
10899 	    test_bit(FC_PT2PT, &vport->fc_flag)) {
10900 		if (expect_rsp) {
10901 			bf_set(els_req64_sid, &wqe->els_req, vport->fc_myDID);
10902 
10903 			/* For ELS_REQUEST64_WQE, use the VPI by default */
10904 			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10905 			       phba->vpi_ids[vport->vpi]);
10906 		}
10907 
10908 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10909 		if (elscmd == ELS_CMD_ECHO)
10910 			bf_set(wqe_ct, &wqe->generic.wqe_com, 0);
10911 		else
10912 			bf_set(wqe_ct, &wqe->generic.wqe_com, 1);
10913 	}
10914 }
10915 
10916 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)10917 lpfc_sli_prep_els_req_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
10918 			  struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
10919 			  u16 cmd_size, u32 did, u32 elscmd, u8 tmo,
10920 			  u8 expect_rsp)
10921 {
10922 	phba->__lpfc_sli_prep_els_req_rsp(cmdiocbq, vport, bmp, cmd_size, did,
10923 					  elscmd, tmo, expect_rsp);
10924 }
10925 
10926 static void
__lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10927 __lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10928 			   u16 rpi, u32 num_entry, u8 tmo)
10929 {
10930 	IOCB_t *cmd;
10931 
10932 	cmd = &cmdiocbq->iocb;
10933 	memset(cmd, 0, sizeof(*cmd));
10934 
10935 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10936 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10937 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10938 	cmd->un.genreq64.bdl.bdeSize = num_entry * sizeof(struct ulp_bde64);
10939 
10940 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
10941 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
10942 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
10943 
10944 	cmd->ulpContext = rpi;
10945 	cmd->ulpClass = CLASS3;
10946 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
10947 	cmd->ulpBdeCount = 1;
10948 	cmd->ulpLe = 1;
10949 	cmd->ulpOwner = OWN_CHIP;
10950 	cmd->ulpTimeout = tmo;
10951 }
10952 
10953 static void
__lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10954 __lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10955 			   u16 rpi, u32 num_entry, u8 tmo)
10956 {
10957 	union lpfc_wqe128 *cmdwqe;
10958 	struct ulp_bde64_le *bde, *bpl;
10959 	u32 xmit_len = 0, total_len = 0, size, type, i;
10960 
10961 	cmdwqe = &cmdiocbq->wqe;
10962 	memset(cmdwqe, 0, sizeof(*cmdwqe));
10963 
10964 	/* Calculate total_len and xmit_len */
10965 	bpl = (struct ulp_bde64_le *)bmp->virt;
10966 	for (i = 0; i < num_entry; i++) {
10967 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10968 		total_len += size;
10969 	}
10970 	for (i = 0; i < num_entry; i++) {
10971 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10972 		type = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_TYPE_MASK;
10973 		if (type != ULP_BDE64_TYPE_BDE_64)
10974 			break;
10975 		xmit_len += size;
10976 	}
10977 
10978 	/* Words 0 - 2 */
10979 	bde = (struct ulp_bde64_le *)&cmdwqe->generic.bde;
10980 	bde->addr_low = bpl->addr_low;
10981 	bde->addr_high = bpl->addr_high;
10982 	bde->type_size = cpu_to_le32(xmit_len);
10983 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10984 
10985 	/* Word 3 */
10986 	cmdwqe->gen_req.request_payload_len = xmit_len;
10987 
10988 	/* Word 5 */
10989 	bf_set(wqe_type, &cmdwqe->gen_req.wge_ctl, FC_TYPE_CT);
10990 	bf_set(wqe_rctl, &cmdwqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);
10991 	bf_set(wqe_si, &cmdwqe->gen_req.wge_ctl, 1);
10992 	bf_set(wqe_la, &cmdwqe->gen_req.wge_ctl, 1);
10993 
10994 	/* Word 6 */
10995 	bf_set(wqe_ctxt_tag, &cmdwqe->gen_req.wqe_com, rpi);
10996 
10997 	/* Word 7 */
10998 	bf_set(wqe_tmo, &cmdwqe->gen_req.wqe_com, tmo);
10999 	bf_set(wqe_class, &cmdwqe->gen_req.wqe_com, CLASS3);
11000 	bf_set(wqe_cmnd, &cmdwqe->gen_req.wqe_com, CMD_GEN_REQUEST64_CR);
11001 	bf_set(wqe_ct, &cmdwqe->gen_req.wqe_com, SLI4_CT_RPI);
11002 
11003 	/* Word 12 */
11004 	cmdwqe->gen_req.max_response_payload_len = total_len - xmit_len;
11005 }
11006 
11007 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)11008 lpfc_sli_prep_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11009 		      struct lpfc_dmabuf *bmp, u16 rpi, u32 num_entry, u8 tmo)
11010 {
11011 	phba->__lpfc_sli_prep_gen_req(cmdiocbq, bmp, rpi, num_entry, tmo);
11012 }
11013 
11014 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)11015 __lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq *cmdiocbq,
11016 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11017 			      u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11018 {
11019 	IOCB_t *icmd;
11020 
11021 	icmd = &cmdiocbq->iocb;
11022 	memset(icmd, 0, sizeof(*icmd));
11023 
11024 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
11025 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
11026 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
11027 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
11028 	icmd->un.xseq64.w5.hcsw.Fctl = LA;
11029 	if (last_seq)
11030 		icmd->un.xseq64.w5.hcsw.Fctl |= LS;
11031 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11032 	icmd->un.xseq64.w5.hcsw.Rctl = rctl;
11033 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
11034 
11035 	icmd->ulpBdeCount = 1;
11036 	icmd->ulpLe = 1;
11037 	icmd->ulpClass = CLASS3;
11038 
11039 	switch (cr_cx_cmd) {
11040 	case CMD_XMIT_SEQUENCE64_CR:
11041 		icmd->ulpContext = rpi;
11042 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
11043 		break;
11044 	case CMD_XMIT_SEQUENCE64_CX:
11045 		icmd->ulpContext = ox_id;
11046 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
11047 		break;
11048 	default:
11049 		break;
11050 	}
11051 }
11052 
11053 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)11054 __lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq *cmdiocbq,
11055 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11056 			      u32 full_size, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11057 {
11058 	union lpfc_wqe128 *wqe;
11059 	struct ulp_bde64 *bpl;
11060 
11061 	wqe = &cmdiocbq->wqe;
11062 	memset(wqe, 0, sizeof(*wqe));
11063 
11064 	/* Words 0 - 2 */
11065 	bpl = (struct ulp_bde64 *)bmp->virt;
11066 	wqe->xmit_sequence.bde.addrHigh = bpl->addrHigh;
11067 	wqe->xmit_sequence.bde.addrLow = bpl->addrLow;
11068 	wqe->xmit_sequence.bde.tus.w = bpl->tus.w;
11069 
11070 	/* Word 5 */
11071 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, last_seq);
11072 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 1);
11073 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
11074 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, rctl);
11075 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_CT);
11076 
11077 	/* Word 6 */
11078 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com, rpi);
11079 
11080 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
11081 	       CMD_XMIT_SEQUENCE64_WQE);
11082 
11083 	/* Word 7 */
11084 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
11085 
11086 	/* Word 9 */
11087 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ox_id);
11088 
11089 	if (cmdiocbq->cmd_flag & (LPFC_IO_LIBDFC | LPFC_IO_LOOPBACK)) {
11090 		/* Word 10 */
11091 		if (cmdiocbq->cmd_flag & LPFC_IO_VMID) {
11092 			bf_set(wqe_appid, &wqe->xmit_sequence.wqe_com, 1);
11093 			bf_set(wqe_wqes, &wqe->xmit_sequence.wqe_com, 1);
11094 			wqe->words[31] = LOOPBACK_SRC_APPID;
11095 		}
11096 
11097 		/* Word 12 */
11098 		wqe->xmit_sequence.xmit_len = full_size;
11099 	}
11100 	else
11101 		wqe->xmit_sequence.xmit_len =
11102 			wqe->xmit_sequence.bde.tus.f.bdeSize;
11103 }
11104 
11105 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)11106 lpfc_sli_prep_xmit_seq64(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11107 			 struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11108 			 u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11109 {
11110 	phba->__lpfc_sli_prep_xmit_seq64(cmdiocbq, bmp, rpi, ox_id, num_entry,
11111 					 rctl, last_seq, cr_cx_cmd);
11112 }
11113 
11114 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)11115 __lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11116 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11117 			     bool wqec)
11118 {
11119 	IOCB_t *icmd = NULL;
11120 
11121 	icmd = &cmdiocbq->iocb;
11122 	memset(icmd, 0, sizeof(*icmd));
11123 
11124 	/* Word 5 */
11125 	icmd->un.acxri.abortContextTag = ulp_context;
11126 	icmd->un.acxri.abortIoTag = iotag;
11127 
11128 	if (ia) {
11129 		/* Word 7 */
11130 		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
11131 	} else {
11132 		/* Word 3 */
11133 		icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
11134 
11135 		/* Word 7 */
11136 		icmd->ulpClass = ulp_class;
11137 		icmd->ulpCommand = CMD_ABORT_XRI_CN;
11138 	}
11139 
11140 	/* Word 7 */
11141 	icmd->ulpLe = 1;
11142 }
11143 
11144 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)11145 __lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11146 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11147 			     bool wqec)
11148 {
11149 	union lpfc_wqe128 *wqe;
11150 
11151 	wqe = &cmdiocbq->wqe;
11152 	memset(wqe, 0, sizeof(*wqe));
11153 
11154 	/* Word 3 */
11155 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
11156 	if (ia)
11157 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
11158 	else
11159 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
11160 
11161 	/* Word 7 */
11162 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_WQE);
11163 
11164 	/* Word 8 */
11165 	wqe->abort_cmd.wqe_com.abort_tag = ulp_context;
11166 
11167 	/* Word 9 */
11168 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, iotag);
11169 
11170 	/* Word 10 */
11171 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
11172 
11173 	/* Word 11 */
11174 	if (wqec)
11175 		bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
11176 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, cqid);
11177 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11178 }
11179 
11180 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)11181 lpfc_sli_prep_abort_xri(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11182 			u16 ulp_context, u16 iotag, u8 ulp_class, u16 cqid,
11183 			bool ia, bool wqec)
11184 {
11185 	phba->__lpfc_sli_prep_abort_xri(cmdiocbq, ulp_context, iotag, ulp_class,
11186 					cqid, ia, wqec);
11187 }
11188 
11189 /**
11190  * lpfc_sli_api_table_setup - Set up sli api function jump table
11191  * @phba: The hba struct for which this call is being executed.
11192  * @dev_grp: The HBA PCI-Device group number.
11193  *
11194  * This routine sets up the SLI interface API function jump table in @phba
11195  * struct.
11196  * Returns: 0 - success, -ENODEV - failure.
11197  **/
11198 int
lpfc_sli_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)11199 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11200 {
11201 
11202 	switch (dev_grp) {
11203 	case LPFC_PCI_DEV_LP:
11204 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11205 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
11206 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
11207 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s3;
11208 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s3;
11209 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s3;
11210 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s3;
11211 		break;
11212 	case LPFC_PCI_DEV_OC:
11213 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11214 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
11215 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
11216 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s4;
11217 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s4;
11218 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s4;
11219 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s4;
11220 		break;
11221 	default:
11222 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11223 				"1419 Invalid HBA PCI-device group: 0x%x\n",
11224 				dev_grp);
11225 		return -ENODEV;
11226 	}
11227 	return 0;
11228 }
11229 
11230 /**
11231  * lpfc_sli4_calc_ring - Calculates which ring to use
11232  * @phba: Pointer to HBA context object.
11233  * @piocb: Pointer to command iocb.
11234  *
11235  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11236  * hba_wqidx, thus we need to calculate the corresponding ring.
11237  * Since ABORTS must go on the same WQ of the command they are
11238  * aborting, we use command's hba_wqidx.
11239  */
11240 struct lpfc_sli_ring *
lpfc_sli4_calc_ring(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)11241 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
11242 {
11243 	struct lpfc_io_buf *lpfc_cmd;
11244 
11245 	if (piocb->cmd_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
11246 		if (unlikely(!phba->sli4_hba.hdwq))
11247 			return NULL;
11248 		/*
11249 		 * for abort iocb hba_wqidx should already
11250 		 * be setup based on what work queue we used.
11251 		 */
11252 		if (!(piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
11253 			lpfc_cmd = piocb->io_buf;
11254 			piocb->hba_wqidx = lpfc_cmd->hdwq_no;
11255 		}
11256 		return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
11257 	} else {
11258 		if (unlikely(!phba->sli4_hba.els_wq))
11259 			return NULL;
11260 		piocb->hba_wqidx = 0;
11261 		return phba->sli4_hba.els_wq->pring;
11262 	}
11263 }
11264 
lpfc_sli4_poll_eq(struct lpfc_queue * eq)11265 inline void lpfc_sli4_poll_eq(struct lpfc_queue *eq)
11266 {
11267 	struct lpfc_hba *phba = eq->phba;
11268 
11269 	/*
11270 	 * Unlocking an irq is one of the entry point to check
11271 	 * for re-schedule, but we are good for io submission
11272 	 * path as midlayer does a get_cpu to glue us in. Flush
11273 	 * out the invalidate queue so we can see the updated
11274 	 * value for flag.
11275 	 */
11276 	smp_rmb();
11277 
11278 	if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
11279 		/* We will not likely get the completion for the caller
11280 		 * during this iteration but i guess that's fine.
11281 		 * Future io's coming on this eq should be able to
11282 		 * pick it up.  As for the case of single io's, they
11283 		 * will be handled through a sched from polling timer
11284 		 * function which is currently triggered every 1msec.
11285 		 */
11286 		lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM,
11287 				     LPFC_QUEUE_WORK);
11288 }
11289 
11290 /**
11291  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
11292  * @phba: Pointer to HBA context object.
11293  * @ring_number: Ring number
11294  * @piocb: Pointer to command iocb.
11295  * @flag: Flag indicating if this command can be put into txq.
11296  *
11297  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11298  * function. This function gets the hbalock and calls
11299  * __lpfc_sli_issue_iocb function and will return the error returned
11300  * by __lpfc_sli_issue_iocb function. This wrapper is used by
11301  * functions which do not hold hbalock.
11302  **/
11303 int
lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)11304 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11305 		    struct lpfc_iocbq *piocb, uint32_t flag)
11306 {
11307 	struct lpfc_sli_ring *pring;
11308 	struct lpfc_queue *eq;
11309 	unsigned long iflags;
11310 	int rc;
11311 
11312 	/* If the PCI channel is in offline state, do not post iocbs. */
11313 	if (unlikely(pci_channel_offline(phba->pcidev)))
11314 		return IOCB_ERROR;
11315 
11316 	if (phba->sli_rev == LPFC_SLI_REV4) {
11317 		lpfc_sli_prep_wqe(phba, piocb);
11318 
11319 		eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11320 
11321 		pring = lpfc_sli4_calc_ring(phba, piocb);
11322 		if (unlikely(pring == NULL))
11323 			return IOCB_ERROR;
11324 
11325 		spin_lock_irqsave(&pring->ring_lock, iflags);
11326 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11327 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
11328 
11329 		lpfc_sli4_poll_eq(eq);
11330 	} else {
11331 		/* For now, SLI2/3 will still use hbalock */
11332 		spin_lock_irqsave(&phba->hbalock, iflags);
11333 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11334 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11335 	}
11336 	return rc;
11337 }
11338 
11339 /**
11340  * lpfc_extra_ring_setup - Extra ring setup function
11341  * @phba: Pointer to HBA context object.
11342  *
11343  * This function is called while driver attaches with the
11344  * HBA to setup the extra ring. The extra ring is used
11345  * only when driver needs to support target mode functionality
11346  * or IP over FC functionalities.
11347  *
11348  * This function is called with no lock held. SLI3 only.
11349  **/
11350 static int
lpfc_extra_ring_setup(struct lpfc_hba * phba)11351 lpfc_extra_ring_setup( struct lpfc_hba *phba)
11352 {
11353 	struct lpfc_sli *psli;
11354 	struct lpfc_sli_ring *pring;
11355 
11356 	psli = &phba->sli;
11357 
11358 	/* Adjust cmd/rsp ring iocb entries more evenly */
11359 
11360 	/* Take some away from the FCP ring */
11361 	pring = &psli->sli3_ring[LPFC_FCP_RING];
11362 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11363 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11364 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11365 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11366 
11367 	/* and give them to the extra ring */
11368 	pring = &psli->sli3_ring[LPFC_EXTRA_RING];
11369 
11370 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11371 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11372 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11373 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11374 
11375 	/* Setup default profile for this ring */
11376 	pring->iotag_max = 4096;
11377 	pring->num_mask = 1;
11378 	pring->prt[0].profile = 0;      /* Mask 0 */
11379 	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11380 	pring->prt[0].type = phba->cfg_multi_ring_type;
11381 	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11382 	return 0;
11383 }
11384 
11385 static void
lpfc_sli_post_recovery_event(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)11386 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11387 			     struct lpfc_nodelist *ndlp)
11388 {
11389 	unsigned long iflags;
11390 	struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
11391 
11392 	/* Hold a node reference for outstanding queued work */
11393 	if (!lpfc_nlp_get(ndlp))
11394 		return;
11395 
11396 	spin_lock_irqsave(&phba->hbalock, iflags);
11397 	if (!list_empty(&evtp->evt_listp)) {
11398 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11399 		lpfc_nlp_put(ndlp);
11400 		return;
11401 	}
11402 
11403 	evtp->evt_arg1 = ndlp;
11404 	evtp->evt = LPFC_EVT_RECOVER_PORT;
11405 	list_add_tail(&evtp->evt_listp, &phba->work_list);
11406 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11407 
11408 	lpfc_worker_wake_up(phba);
11409 }
11410 
11411 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11412  * @phba: Pointer to HBA context object.
11413  * @iocbq: Pointer to iocb object.
11414  *
11415  * The async_event handler calls this routine when it receives
11416  * an ASYNC_STATUS_CN event from the port.  The port generates
11417  * this event when an Abort Sequence request to an rport fails
11418  * twice in succession.  The abort could be originated by the
11419  * driver or by the port.  The ABTS could have been for an ELS
11420  * or FCP IO.  The port only generates this event when an ABTS
11421  * fails to complete after one retry.
11422  */
11423 static void
lpfc_sli_abts_err_handler(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)11424 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11425 			  struct lpfc_iocbq *iocbq)
11426 {
11427 	struct lpfc_nodelist *ndlp = NULL;
11428 	uint16_t rpi = 0, vpi = 0;
11429 	struct lpfc_vport *vport = NULL;
11430 
11431 	/* The rpi in the ulpContext is vport-sensitive. */
11432 	vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11433 	rpi = iocbq->iocb.ulpContext;
11434 
11435 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11436 			"3092 Port generated ABTS async event "
11437 			"on vpi %d rpi %d status 0x%x\n",
11438 			vpi, rpi, iocbq->iocb.ulpStatus);
11439 
11440 	vport = lpfc_find_vport_by_vpid(phba, vpi);
11441 	if (!vport)
11442 		goto err_exit;
11443 	ndlp = lpfc_findnode_rpi(vport, rpi);
11444 	if (!ndlp)
11445 		goto err_exit;
11446 
11447 	if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11448 		lpfc_sli_abts_recover_port(vport, ndlp);
11449 	return;
11450 
11451  err_exit:
11452 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11453 			"3095 Event Context not found, no "
11454 			"action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11455 			vpi, rpi, iocbq->iocb.ulpStatus,
11456 			iocbq->iocb.ulpContext);
11457 }
11458 
11459 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11460  * @phba: pointer to HBA context object.
11461  * @ndlp: nodelist pointer for the impacted rport.
11462  * @axri: pointer to the wcqe containing the failed exchange.
11463  *
11464  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11465  * port.  The port generates this event when an abort exchange request to an
11466  * rport fails twice in succession with no reply.  The abort could be originated
11467  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
11468  */
11469 void
lpfc_sli4_abts_err_handler(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct sli4_wcqe_xri_aborted * axri)11470 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11471 			   struct lpfc_nodelist *ndlp,
11472 			   struct sli4_wcqe_xri_aborted *axri)
11473 {
11474 	uint32_t ext_status = 0;
11475 
11476 	if (!ndlp) {
11477 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11478 				"3115 Node Context not found, driver "
11479 				"ignoring abts err event\n");
11480 		return;
11481 	}
11482 
11483 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11484 			"3116 Port generated FCP XRI ABORT event on "
11485 			"vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
11486 			ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
11487 			bf_get(lpfc_wcqe_xa_xri, axri),
11488 			bf_get(lpfc_wcqe_xa_status, axri),
11489 			axri->parameter);
11490 
11491 	/*
11492 	 * Catch the ABTS protocol failure case.  Older OCe FW releases returned
11493 	 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11494 	 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11495 	 */
11496 	ext_status = axri->parameter & IOERR_PARAM_MASK;
11497 	if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11498 	    ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
11499 		lpfc_sli_post_recovery_event(phba, ndlp);
11500 }
11501 
11502 /**
11503  * lpfc_sli_async_event_handler - ASYNC iocb handler function
11504  * @phba: Pointer to HBA context object.
11505  * @pring: Pointer to driver SLI ring object.
11506  * @iocbq: Pointer to iocb object.
11507  *
11508  * This function is called by the slow ring event handler
11509  * function when there is an ASYNC event iocb in the ring.
11510  * This function is called with no lock held.
11511  * Currently this function handles only temperature related
11512  * ASYNC events. The function decodes the temperature sensor
11513  * event message and posts events for the management applications.
11514  **/
11515 static void
lpfc_sli_async_event_handler(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * iocbq)11516 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11517 	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11518 {
11519 	IOCB_t *icmd;
11520 	uint16_t evt_code;
11521 	struct temp_event temp_event_data;
11522 	struct Scsi_Host *shost;
11523 	uint32_t *iocb_w;
11524 
11525 	icmd = &iocbq->iocb;
11526 	evt_code = icmd->un.asyncstat.evt_code;
11527 
11528 	switch (evt_code) {
11529 	case ASYNC_TEMP_WARN:
11530 	case ASYNC_TEMP_SAFE:
11531 		temp_event_data.data = (uint32_t) icmd->ulpContext;
11532 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11533 		if (evt_code == ASYNC_TEMP_WARN) {
11534 			temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
11535 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11536 				"0347 Adapter is very hot, please take "
11537 				"corrective action. temperature : %d Celsius\n",
11538 				(uint32_t) icmd->ulpContext);
11539 		} else {
11540 			temp_event_data.event_code = LPFC_NORMAL_TEMP;
11541 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11542 				"0340 Adapter temperature is OK now. "
11543 				"temperature : %d Celsius\n",
11544 				(uint32_t) icmd->ulpContext);
11545 		}
11546 
11547 		/* Send temperature change event to applications */
11548 		shost = lpfc_shost_from_vport(phba->pport);
11549 		fc_host_post_vendor_event(shost, fc_get_event_number(),
11550 			sizeof(temp_event_data), (char *) &temp_event_data,
11551 			LPFC_NL_VENDOR_ID);
11552 		break;
11553 	case ASYNC_STATUS_CN:
11554 		lpfc_sli_abts_err_handler(phba, iocbq);
11555 		break;
11556 	default:
11557 		iocb_w = (uint32_t *) icmd;
11558 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11559 			"0346 Ring %d handler: unexpected ASYNC_STATUS"
11560 			" evt_code 0x%x\n"
11561 			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
11562 			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
11563 			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
11564 			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
11565 			pring->ringno, icmd->un.asyncstat.evt_code,
11566 			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11567 			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11568 			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11569 			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11570 
11571 		break;
11572 	}
11573 }
11574 
11575 
11576 /**
11577  * lpfc_sli4_setup - SLI ring setup function
11578  * @phba: Pointer to HBA context object.
11579  *
11580  * lpfc_sli_setup sets up rings of the SLI interface with
11581  * number of iocbs per ring and iotags. This function is
11582  * called while driver attach to the HBA and before the
11583  * interrupts are enabled. So there is no need for locking.
11584  *
11585  * This function always returns 0.
11586  **/
11587 int
lpfc_sli4_setup(struct lpfc_hba * phba)11588 lpfc_sli4_setup(struct lpfc_hba *phba)
11589 {
11590 	struct lpfc_sli_ring *pring;
11591 
11592 	pring = phba->sli4_hba.els_wq->pring;
11593 	pring->num_mask = LPFC_MAX_RING_MASK;
11594 	pring->prt[0].profile = 0;	/* Mask 0 */
11595 	pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11596 	pring->prt[0].type = FC_TYPE_ELS;
11597 	pring->prt[0].lpfc_sli_rcv_unsol_event =
11598 	    lpfc_els_unsol_event;
11599 	pring->prt[1].profile = 0;	/* Mask 1 */
11600 	pring->prt[1].rctl = FC_RCTL_ELS_REP;
11601 	pring->prt[1].type = FC_TYPE_ELS;
11602 	pring->prt[1].lpfc_sli_rcv_unsol_event =
11603 	    lpfc_els_unsol_event;
11604 	pring->prt[2].profile = 0;	/* Mask 2 */
11605 	/* NameServer Inquiry */
11606 	pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11607 	/* NameServer */
11608 	pring->prt[2].type = FC_TYPE_CT;
11609 	pring->prt[2].lpfc_sli_rcv_unsol_event =
11610 	    lpfc_ct_unsol_event;
11611 	pring->prt[3].profile = 0;	/* Mask 3 */
11612 	/* NameServer response */
11613 	pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11614 	/* NameServer */
11615 	pring->prt[3].type = FC_TYPE_CT;
11616 	pring->prt[3].lpfc_sli_rcv_unsol_event =
11617 	    lpfc_ct_unsol_event;
11618 	return 0;
11619 }
11620 
11621 /**
11622  * lpfc_sli_setup - SLI ring setup function
11623  * @phba: Pointer to HBA context object.
11624  *
11625  * lpfc_sli_setup sets up rings of the SLI interface with
11626  * number of iocbs per ring and iotags. This function is
11627  * called while driver attach to the HBA and before the
11628  * interrupts are enabled. So there is no need for locking.
11629  *
11630  * This function always returns 0. SLI3 only.
11631  **/
11632 int
lpfc_sli_setup(struct lpfc_hba * phba)11633 lpfc_sli_setup(struct lpfc_hba *phba)
11634 {
11635 	int i, totiocbsize = 0;
11636 	struct lpfc_sli *psli = &phba->sli;
11637 	struct lpfc_sli_ring *pring;
11638 
11639 	psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
11640 	psli->sli_flag = 0;
11641 
11642 	psli->iocbq_lookup = NULL;
11643 	psli->iocbq_lookup_len = 0;
11644 	psli->last_iotag = 0;
11645 
11646 	for (i = 0; i < psli->num_rings; i++) {
11647 		pring = &psli->sli3_ring[i];
11648 		switch (i) {
11649 		case LPFC_FCP_RING:	/* ring 0 - FCP */
11650 			/* numCiocb and numRiocb are used in config_port */
11651 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11652 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11653 			pring->sli.sli3.numCiocb +=
11654 				SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11655 			pring->sli.sli3.numRiocb +=
11656 				SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11657 			pring->sli.sli3.numCiocb +=
11658 				SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11659 			pring->sli.sli3.numRiocb +=
11660 				SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11661 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11662 							SLI3_IOCB_CMD_SIZE :
11663 							SLI2_IOCB_CMD_SIZE;
11664 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11665 							SLI3_IOCB_RSP_SIZE :
11666 							SLI2_IOCB_RSP_SIZE;
11667 			pring->iotag_ctr = 0;
11668 			pring->iotag_max =
11669 			    (phba->cfg_hba_queue_depth * 2);
11670 			pring->fast_iotag = pring->iotag_max;
11671 			pring->num_mask = 0;
11672 			break;
11673 		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
11674 			/* numCiocb and numRiocb are used in config_port */
11675 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11676 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11677 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11678 							SLI3_IOCB_CMD_SIZE :
11679 							SLI2_IOCB_CMD_SIZE;
11680 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11681 							SLI3_IOCB_RSP_SIZE :
11682 							SLI2_IOCB_RSP_SIZE;
11683 			pring->iotag_max = phba->cfg_hba_queue_depth;
11684 			pring->num_mask = 0;
11685 			break;
11686 		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
11687 			/* numCiocb and numRiocb are used in config_port */
11688 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11689 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11690 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11691 							SLI3_IOCB_CMD_SIZE :
11692 							SLI2_IOCB_CMD_SIZE;
11693 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11694 							SLI3_IOCB_RSP_SIZE :
11695 							SLI2_IOCB_RSP_SIZE;
11696 			pring->fast_iotag = 0;
11697 			pring->iotag_ctr = 0;
11698 			pring->iotag_max = 4096;
11699 			pring->lpfc_sli_rcv_async_status =
11700 				lpfc_sli_async_event_handler;
11701 			pring->num_mask = LPFC_MAX_RING_MASK;
11702 			pring->prt[0].profile = 0;	/* Mask 0 */
11703 			pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11704 			pring->prt[0].type = FC_TYPE_ELS;
11705 			pring->prt[0].lpfc_sli_rcv_unsol_event =
11706 			    lpfc_els_unsol_event;
11707 			pring->prt[1].profile = 0;	/* Mask 1 */
11708 			pring->prt[1].rctl = FC_RCTL_ELS_REP;
11709 			pring->prt[1].type = FC_TYPE_ELS;
11710 			pring->prt[1].lpfc_sli_rcv_unsol_event =
11711 			    lpfc_els_unsol_event;
11712 			pring->prt[2].profile = 0;	/* Mask 2 */
11713 			/* NameServer Inquiry */
11714 			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11715 			/* NameServer */
11716 			pring->prt[2].type = FC_TYPE_CT;
11717 			pring->prt[2].lpfc_sli_rcv_unsol_event =
11718 			    lpfc_ct_unsol_event;
11719 			pring->prt[3].profile = 0;	/* Mask 3 */
11720 			/* NameServer response */
11721 			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11722 			/* NameServer */
11723 			pring->prt[3].type = FC_TYPE_CT;
11724 			pring->prt[3].lpfc_sli_rcv_unsol_event =
11725 			    lpfc_ct_unsol_event;
11726 			break;
11727 		}
11728 		totiocbsize += (pring->sli.sli3.numCiocb *
11729 			pring->sli.sli3.sizeCiocb) +
11730 			(pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
11731 	}
11732 	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
11733 		/* Too many cmd / rsp ring entries in SLI2 SLIM */
11734 		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11735 		       "SLI2 SLIM Data: x%x x%lx\n",
11736 		       phba->brd_no, totiocbsize,
11737 		       (unsigned long) MAX_SLIM_IOCB_SIZE);
11738 	}
11739 	if (phba->cfg_multi_ring_support == 2)
11740 		lpfc_extra_ring_setup(phba);
11741 
11742 	return 0;
11743 }
11744 
11745 /**
11746  * lpfc_sli4_queue_init - Queue initialization function
11747  * @phba: Pointer to HBA context object.
11748  *
11749  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
11750  * ring. This function also initializes ring indices of each ring.
11751  * This function is called during the initialization of the SLI
11752  * interface of an HBA.
11753  * This function is called with no lock held and always returns
11754  * 1.
11755  **/
11756 void
lpfc_sli4_queue_init(struct lpfc_hba * phba)11757 lpfc_sli4_queue_init(struct lpfc_hba *phba)
11758 {
11759 	struct lpfc_sli *psli;
11760 	struct lpfc_sli_ring *pring;
11761 	int i;
11762 
11763 	psli = &phba->sli;
11764 	spin_lock_irq(&phba->hbalock);
11765 	INIT_LIST_HEAD(&psli->mboxq);
11766 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11767 	/* Initialize list headers for txq and txcmplq as double linked lists */
11768 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
11769 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11770 		pring->flag = 0;
11771 		pring->ringno = LPFC_FCP_RING;
11772 		pring->txcmplq_cnt = 0;
11773 		INIT_LIST_HEAD(&pring->txq);
11774 		INIT_LIST_HEAD(&pring->txcmplq);
11775 		INIT_LIST_HEAD(&pring->iocb_continueq);
11776 		spin_lock_init(&pring->ring_lock);
11777 	}
11778 	pring = phba->sli4_hba.els_wq->pring;
11779 	pring->flag = 0;
11780 	pring->ringno = LPFC_ELS_RING;
11781 	pring->txcmplq_cnt = 0;
11782 	INIT_LIST_HEAD(&pring->txq);
11783 	INIT_LIST_HEAD(&pring->txcmplq);
11784 	INIT_LIST_HEAD(&pring->iocb_continueq);
11785 	spin_lock_init(&pring->ring_lock);
11786 
11787 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11788 		pring = phba->sli4_hba.nvmels_wq->pring;
11789 		pring->flag = 0;
11790 		pring->ringno = LPFC_ELS_RING;
11791 		pring->txcmplq_cnt = 0;
11792 		INIT_LIST_HEAD(&pring->txq);
11793 		INIT_LIST_HEAD(&pring->txcmplq);
11794 		INIT_LIST_HEAD(&pring->iocb_continueq);
11795 		spin_lock_init(&pring->ring_lock);
11796 	}
11797 
11798 	spin_unlock_irq(&phba->hbalock);
11799 }
11800 
11801 /**
11802  * lpfc_sli_queue_init - Queue initialization function
11803  * @phba: Pointer to HBA context object.
11804  *
11805  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11806  * ring. This function also initializes ring indices of each ring.
11807  * This function is called during the initialization of the SLI
11808  * interface of an HBA.
11809  * This function is called with no lock held and always returns
11810  * 1.
11811  **/
11812 void
lpfc_sli_queue_init(struct lpfc_hba * phba)11813 lpfc_sli_queue_init(struct lpfc_hba *phba)
11814 {
11815 	struct lpfc_sli *psli;
11816 	struct lpfc_sli_ring *pring;
11817 	int i;
11818 
11819 	psli = &phba->sli;
11820 	spin_lock_irq(&phba->hbalock);
11821 	INIT_LIST_HEAD(&psli->mboxq);
11822 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11823 	/* Initialize list headers for txq and txcmplq as double linked lists */
11824 	for (i = 0; i < psli->num_rings; i++) {
11825 		pring = &psli->sli3_ring[i];
11826 		pring->ringno = i;
11827 		pring->sli.sli3.next_cmdidx  = 0;
11828 		pring->sli.sli3.local_getidx = 0;
11829 		pring->sli.sli3.cmdidx = 0;
11830 		INIT_LIST_HEAD(&pring->iocb_continueq);
11831 		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11832 		INIT_LIST_HEAD(&pring->postbufq);
11833 		pring->flag = 0;
11834 		INIT_LIST_HEAD(&pring->txq);
11835 		INIT_LIST_HEAD(&pring->txcmplq);
11836 		spin_lock_init(&pring->ring_lock);
11837 	}
11838 	spin_unlock_irq(&phba->hbalock);
11839 }
11840 
11841 /**
11842  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11843  * @phba: Pointer to HBA context object.
11844  *
11845  * This routine flushes the mailbox command subsystem. It will unconditionally
11846  * flush all the mailbox commands in the three possible stages in the mailbox
11847  * command sub-system: pending mailbox command queue; the outstanding mailbox
11848  * command; and completed mailbox command queue. It is caller's responsibility
11849  * to make sure that the driver is in the proper state to flush the mailbox
11850  * command sub-system. Namely, the posting of mailbox commands into the
11851  * pending mailbox command queue from the various clients must be stopped;
11852  * either the HBA is in a state that it will never works on the outstanding
11853  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11854  * mailbox command has been completed.
11855  **/
11856 static void
lpfc_sli_mbox_sys_flush(struct lpfc_hba * phba)11857 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11858 {
11859 	LIST_HEAD(completions);
11860 	struct lpfc_sli *psli = &phba->sli;
11861 	LPFC_MBOXQ_t *pmb;
11862 	unsigned long iflag;
11863 
11864 	/* Disable softirqs, including timers from obtaining phba->hbalock */
11865 	local_bh_disable();
11866 
11867 	/* Flush all the mailbox commands in the mbox system */
11868 	spin_lock_irqsave(&phba->hbalock, iflag);
11869 
11870 	/* The pending mailbox command queue */
11871 	list_splice_init(&phba->sli.mboxq, &completions);
11872 	/* The outstanding active mailbox command */
11873 	if (psli->mbox_active) {
11874 		list_add_tail(&psli->mbox_active->list, &completions);
11875 		psli->mbox_active = NULL;
11876 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11877 	}
11878 	/* The completed mailbox command queue */
11879 	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11880 	spin_unlock_irqrestore(&phba->hbalock, iflag);
11881 
11882 	/* Enable softirqs again, done with phba->hbalock */
11883 	local_bh_enable();
11884 
11885 	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11886 	while (!list_empty(&completions)) {
11887 		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11888 		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11889 		if (pmb->mbox_cmpl)
11890 			pmb->mbox_cmpl(phba, pmb);
11891 	}
11892 }
11893 
11894 /**
11895  * lpfc_sli_host_down - Vport cleanup function
11896  * @vport: Pointer to virtual port object.
11897  *
11898  * lpfc_sli_host_down is called to clean up the resources
11899  * associated with a vport before destroying virtual
11900  * port data structures.
11901  * This function does following operations:
11902  * - Free discovery resources associated with this virtual
11903  *   port.
11904  * - Free iocbs associated with this virtual port in
11905  *   the txq.
11906  * - Send abort for all iocb commands associated with this
11907  *   vport in txcmplq.
11908  *
11909  * This function is called with no lock held and always returns 1.
11910  **/
11911 int
lpfc_sli_host_down(struct lpfc_vport * vport)11912 lpfc_sli_host_down(struct lpfc_vport *vport)
11913 {
11914 	LIST_HEAD(completions);
11915 	struct lpfc_hba *phba = vport->phba;
11916 	struct lpfc_sli *psli = &phba->sli;
11917 	struct lpfc_queue *qp = NULL;
11918 	struct lpfc_sli_ring *pring;
11919 	struct lpfc_iocbq *iocb, *next_iocb;
11920 	int i;
11921 	unsigned long flags = 0;
11922 	uint16_t prev_pring_flag;
11923 
11924 	lpfc_cleanup_discovery_resources(vport);
11925 
11926 	spin_lock_irqsave(&phba->hbalock, flags);
11927 
11928 	/*
11929 	 * Error everything on the txq since these iocbs
11930 	 * have not been given to the FW yet.
11931 	 * Also issue ABTS for everything on the txcmplq
11932 	 */
11933 	if (phba->sli_rev != LPFC_SLI_REV4) {
11934 		for (i = 0; i < psli->num_rings; i++) {
11935 			pring = &psli->sli3_ring[i];
11936 			prev_pring_flag = pring->flag;
11937 			/* Only slow rings */
11938 			if (pring->ringno == LPFC_ELS_RING) {
11939 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11940 				/* Set the lpfc data pending flag */
11941 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11942 			}
11943 			list_for_each_entry_safe(iocb, next_iocb,
11944 						 &pring->txq, list) {
11945 				if (iocb->vport != vport)
11946 					continue;
11947 				list_move_tail(&iocb->list, &completions);
11948 			}
11949 			list_for_each_entry_safe(iocb, next_iocb,
11950 						 &pring->txcmplq, list) {
11951 				if (iocb->vport != vport)
11952 					continue;
11953 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11954 							   NULL);
11955 			}
11956 			pring->flag = prev_pring_flag;
11957 		}
11958 	} else {
11959 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11960 			pring = qp->pring;
11961 			if (!pring)
11962 				continue;
11963 			if (pring == phba->sli4_hba.els_wq->pring) {
11964 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11965 				/* Set the lpfc data pending flag */
11966 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11967 			}
11968 			prev_pring_flag = pring->flag;
11969 			spin_lock(&pring->ring_lock);
11970 			list_for_each_entry_safe(iocb, next_iocb,
11971 						 &pring->txq, list) {
11972 				if (iocb->vport != vport)
11973 					continue;
11974 				list_move_tail(&iocb->list, &completions);
11975 			}
11976 			spin_unlock(&pring->ring_lock);
11977 			list_for_each_entry_safe(iocb, next_iocb,
11978 						 &pring->txcmplq, list) {
11979 				if (iocb->vport != vport)
11980 					continue;
11981 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11982 							   NULL);
11983 			}
11984 			pring->flag = prev_pring_flag;
11985 		}
11986 	}
11987 	spin_unlock_irqrestore(&phba->hbalock, flags);
11988 
11989 	/* Make sure HBA is alive */
11990 	lpfc_issue_hb_tmo(phba);
11991 
11992 	/* Cancel all the IOCBs from the completions list */
11993 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11994 			      IOERR_SLI_DOWN);
11995 	return 1;
11996 }
11997 
11998 /**
11999  * lpfc_sli_hba_down - Resource cleanup function for the HBA
12000  * @phba: Pointer to HBA context object.
12001  *
12002  * This function cleans up all iocb, buffers, mailbox commands
12003  * while shutting down the HBA. This function is called with no
12004  * lock held and always returns 1.
12005  * This function does the following to cleanup driver resources:
12006  * - Free discovery resources for each virtual port
12007  * - Cleanup any pending fabric iocbs
12008  * - Iterate through the iocb txq and free each entry
12009  *   in the list.
12010  * - Free up any buffer posted to the HBA
12011  * - Free mailbox commands in the mailbox queue.
12012  **/
12013 int
lpfc_sli_hba_down(struct lpfc_hba * phba)12014 lpfc_sli_hba_down(struct lpfc_hba *phba)
12015 {
12016 	LIST_HEAD(completions);
12017 	struct lpfc_sli *psli = &phba->sli;
12018 	struct lpfc_queue *qp = NULL;
12019 	struct lpfc_sli_ring *pring;
12020 	struct lpfc_dmabuf *buf_ptr;
12021 	unsigned long flags = 0;
12022 	int i;
12023 
12024 	/* Shutdown the mailbox command sub-system */
12025 	lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
12026 
12027 	lpfc_hba_down_prep(phba);
12028 
12029 	/* Disable softirqs, including timers from obtaining phba->hbalock */
12030 	local_bh_disable();
12031 
12032 	lpfc_fabric_abort_hba(phba);
12033 
12034 	spin_lock_irqsave(&phba->hbalock, flags);
12035 
12036 	/*
12037 	 * Error everything on the txq since these iocbs
12038 	 * have not been given to the FW yet.
12039 	 */
12040 	if (phba->sli_rev != LPFC_SLI_REV4) {
12041 		for (i = 0; i < psli->num_rings; i++) {
12042 			pring = &psli->sli3_ring[i];
12043 			/* Only slow rings */
12044 			if (pring->ringno == LPFC_ELS_RING) {
12045 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12046 				/* Set the lpfc data pending flag */
12047 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12048 			}
12049 			list_splice_init(&pring->txq, &completions);
12050 		}
12051 	} else {
12052 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12053 			pring = qp->pring;
12054 			if (!pring)
12055 				continue;
12056 			spin_lock(&pring->ring_lock);
12057 			list_splice_init(&pring->txq, &completions);
12058 			spin_unlock(&pring->ring_lock);
12059 			if (pring == phba->sli4_hba.els_wq->pring) {
12060 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12061 				/* Set the lpfc data pending flag */
12062 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12063 			}
12064 		}
12065 	}
12066 	spin_unlock_irqrestore(&phba->hbalock, flags);
12067 
12068 	/* Cancel all the IOCBs from the completions list */
12069 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12070 			      IOERR_SLI_DOWN);
12071 
12072 	spin_lock_irqsave(&phba->hbalock, flags);
12073 	list_splice_init(&phba->elsbuf, &completions);
12074 	phba->elsbuf_cnt = 0;
12075 	phba->elsbuf_prev_cnt = 0;
12076 	spin_unlock_irqrestore(&phba->hbalock, flags);
12077 
12078 	while (!list_empty(&completions)) {
12079 		list_remove_head(&completions, buf_ptr,
12080 			struct lpfc_dmabuf, list);
12081 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12082 		kfree(buf_ptr);
12083 	}
12084 
12085 	/* Enable softirqs again, done with phba->hbalock */
12086 	local_bh_enable();
12087 
12088 	/* Return any active mbox cmds */
12089 	timer_delete_sync(&psli->mbox_tmo);
12090 
12091 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
12092 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12093 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
12094 
12095 	return 1;
12096 }
12097 
12098 /**
12099  * lpfc_sli_pcimem_bcopy - SLI memory copy function
12100  * @srcp: Source memory pointer.
12101  * @destp: Destination memory pointer.
12102  * @cnt: Number of words required to be copied.
12103  *
12104  * This function is used for copying data between driver memory
12105  * and the SLI memory. This function also changes the endianness
12106  * of each word if native endianness is different from SLI
12107  * endianness. This function can be called with or without
12108  * lock.
12109  **/
12110 void
lpfc_sli_pcimem_bcopy(void * srcp,void * destp,uint32_t cnt)12111 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12112 {
12113 	uint32_t *src = srcp;
12114 	uint32_t *dest = destp;
12115 	uint32_t ldata;
12116 	int i;
12117 
12118 	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12119 		ldata = *src;
12120 		ldata = le32_to_cpu(ldata);
12121 		*dest = ldata;
12122 		src++;
12123 		dest++;
12124 	}
12125 }
12126 
12127 
12128 /**
12129  * lpfc_sli_bemem_bcopy - SLI memory copy function
12130  * @srcp: Source memory pointer.
12131  * @destp: Destination memory pointer.
12132  * @cnt: Number of words required to be copied.
12133  *
12134  * This function is used for copying data between a data structure
12135  * with big endian representation to local endianness.
12136  * This function can be called with or without lock.
12137  **/
12138 void
lpfc_sli_bemem_bcopy(void * srcp,void * destp,uint32_t cnt)12139 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12140 {
12141 	uint32_t *src = srcp;
12142 	uint32_t *dest = destp;
12143 	uint32_t ldata;
12144 	int i;
12145 
12146 	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12147 		ldata = *src;
12148 		ldata = be32_to_cpu(ldata);
12149 		*dest = ldata;
12150 		src++;
12151 		dest++;
12152 	}
12153 }
12154 
12155 /**
12156  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
12157  * @phba: Pointer to HBA context object.
12158  * @pring: Pointer to driver SLI ring object.
12159  * @mp: Pointer to driver buffer object.
12160  *
12161  * This function is called with no lock held.
12162  * It always return zero after adding the buffer to the postbufq
12163  * buffer list.
12164  **/
12165 int
lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_dmabuf * mp)12166 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12167 			 struct lpfc_dmabuf *mp)
12168 {
12169 	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12170 	   later */
12171 	spin_lock_irq(&phba->hbalock);
12172 	list_add_tail(&mp->list, &pring->postbufq);
12173 	pring->postbufq_cnt++;
12174 	spin_unlock_irq(&phba->hbalock);
12175 	return 0;
12176 }
12177 
12178 /**
12179  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
12180  * @phba: Pointer to HBA context object.
12181  *
12182  * When HBQ is enabled, buffers are searched based on tags. This function
12183  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12184  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12185  * does not conflict with tags of buffer posted for unsolicited events.
12186  * The function returns the allocated tag. The function is called with
12187  * no locks held.
12188  **/
12189 uint32_t
lpfc_sli_get_buffer_tag(struct lpfc_hba * phba)12190 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12191 {
12192 	spin_lock_irq(&phba->hbalock);
12193 	phba->buffer_tag_count++;
12194 	/*
12195 	 * Always set the QUE_BUFTAG_BIT to distiguish between
12196 	 * a tag assigned by HBQ.
12197 	 */
12198 	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12199 	spin_unlock_irq(&phba->hbalock);
12200 	return phba->buffer_tag_count;
12201 }
12202 
12203 /**
12204  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
12205  * @phba: Pointer to HBA context object.
12206  * @pring: Pointer to driver SLI ring object.
12207  * @tag: Buffer tag.
12208  *
12209  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12210  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12211  * iocb is posted to the response ring with the tag of the buffer.
12212  * This function searches the pring->postbufq list using the tag
12213  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12214  * iocb. If the buffer is found then lpfc_dmabuf object of the
12215  * buffer is returned to the caller else NULL is returned.
12216  * This function is called with no lock held.
12217  **/
12218 struct lpfc_dmabuf *
lpfc_sli_ring_taggedbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)12219 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12220 			uint32_t tag)
12221 {
12222 	struct lpfc_dmabuf *mp, *next_mp;
12223 	struct list_head *slp = &pring->postbufq;
12224 
12225 	/* Search postbufq, from the beginning, looking for a match on tag */
12226 	spin_lock_irq(&phba->hbalock);
12227 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12228 		if (mp->buffer_tag == tag) {
12229 			list_del_init(&mp->list);
12230 			pring->postbufq_cnt--;
12231 			spin_unlock_irq(&phba->hbalock);
12232 			return mp;
12233 		}
12234 	}
12235 
12236 	spin_unlock_irq(&phba->hbalock);
12237 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12238 			"0402 Cannot find virtual addr for buffer tag on "
12239 			"ring %d Data x%lx x%px x%px x%x\n",
12240 			pring->ringno, (unsigned long) tag,
12241 			slp->next, slp->prev, pring->postbufq_cnt);
12242 
12243 	return NULL;
12244 }
12245 
12246 /**
12247  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
12248  * @phba: Pointer to HBA context object.
12249  * @pring: Pointer to driver SLI ring object.
12250  * @phys: DMA address of the buffer.
12251  *
12252  * This function searches the buffer list using the dma_address
12253  * of unsolicited event to find the driver's lpfc_dmabuf object
12254  * corresponding to the dma_address. The function returns the
12255  * lpfc_dmabuf object if a buffer is found else it returns NULL.
12256  * This function is called by the ct and els unsolicited event
12257  * handlers to get the buffer associated with the unsolicited
12258  * event.
12259  *
12260  * This function is called with no lock held.
12261  **/
12262 struct lpfc_dmabuf *
lpfc_sli_ringpostbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,dma_addr_t phys)12263 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12264 			 dma_addr_t phys)
12265 {
12266 	struct lpfc_dmabuf *mp, *next_mp;
12267 	struct list_head *slp = &pring->postbufq;
12268 
12269 	/* Search postbufq, from the beginning, looking for a match on phys */
12270 	spin_lock_irq(&phba->hbalock);
12271 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12272 		if (mp->phys == phys) {
12273 			list_del_init(&mp->list);
12274 			pring->postbufq_cnt--;
12275 			spin_unlock_irq(&phba->hbalock);
12276 			return mp;
12277 		}
12278 	}
12279 
12280 	spin_unlock_irq(&phba->hbalock);
12281 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12282 			"0410 Cannot find virtual addr for mapped buf on "
12283 			"ring %d Data x%llx x%px x%px x%x\n",
12284 			pring->ringno, (unsigned long long)phys,
12285 			slp->next, slp->prev, pring->postbufq_cnt);
12286 	return NULL;
12287 }
12288 
12289 /**
12290  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
12291  * @phba: Pointer to HBA context object.
12292  * @cmdiocb: Pointer to driver command iocb object.
12293  * @rspiocb: Pointer to driver response iocb object.
12294  *
12295  * This function is the completion handler for the abort iocbs for
12296  * ELS commands. This function is called from the ELS ring event
12297  * handler with no lock held. This function frees memory resources
12298  * associated with the abort iocb.
12299  **/
12300 static void
lpfc_sli_abort_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12301 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12302 			struct lpfc_iocbq *rspiocb)
12303 {
12304 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12305 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12306 	u8 cmnd = get_job_cmnd(phba, cmdiocb);
12307 
12308 	if (ulp_status) {
12309 		/*
12310 		 * Assume that the port already completed and returned, or
12311 		 * will return the iocb. Just Log the message.
12312 		 */
12313 		if (phba->sli_rev < LPFC_SLI_REV4) {
12314 			if (cmnd == CMD_ABORT_XRI_CX &&
12315 			    ulp_status == IOSTAT_LOCAL_REJECT &&
12316 			    ulp_word4 == IOERR_ABORT_REQUESTED) {
12317 				goto release_iocb;
12318 			}
12319 		}
12320 	}
12321 
12322 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
12323 			"0327 Abort els iocb complete x%px with io cmd xri %x "
12324 			"abort tag x%x abort status %x abort code %x\n",
12325 			cmdiocb, get_job_abtsiotag(phba, cmdiocb),
12326 			(phba->sli_rev == LPFC_SLI_REV4) ?
12327 			get_wqe_reqtag(cmdiocb) :
12328 			cmdiocb->iocb.ulpIoTag,
12329 			ulp_status, ulp_word4);
12330 release_iocb:
12331 	lpfc_sli_release_iocbq(phba, cmdiocb);
12332 	return;
12333 }
12334 
12335 /**
12336  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
12337  * @phba: Pointer to HBA context object.
12338  * @cmdiocb: Pointer to driver command iocb object.
12339  * @rspiocb: Pointer to driver response iocb object.
12340  *
12341  * The function is called from SLI ring event handler with no
12342  * lock held. This function is the completion handler for ELS commands
12343  * which are aborted. The function frees memory resources used for
12344  * the aborted ELS commands.
12345  **/
12346 void
lpfc_ignore_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12347 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12348 		     struct lpfc_iocbq *rspiocb)
12349 {
12350 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12351 	IOCB_t *irsp;
12352 	LPFC_MBOXQ_t *mbox;
12353 	u32 ulp_command, ulp_status, ulp_word4, iotag;
12354 
12355 	ulp_command = get_job_cmnd(phba, cmdiocb);
12356 	ulp_status = get_job_ulpstatus(phba, rspiocb);
12357 	ulp_word4 = get_job_word4(phba, rspiocb);
12358 
12359 	if (phba->sli_rev == LPFC_SLI_REV4) {
12360 		iotag = get_wqe_reqtag(cmdiocb);
12361 	} else {
12362 		irsp = &rspiocb->iocb;
12363 		iotag = irsp->ulpIoTag;
12364 
12365 		/* It is possible a PLOGI_RJT for NPIV ports to get aborted.
12366 		 * The MBX_REG_LOGIN64 mbox command is freed back to the
12367 		 * mbox_mem_pool here.
12368 		 */
12369 		if (cmdiocb->context_un.mbox) {
12370 			mbox = cmdiocb->context_un.mbox;
12371 			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
12372 			cmdiocb->context_un.mbox = NULL;
12373 		}
12374 	}
12375 
12376 	/* ELS cmd tag <ulpIoTag> completes */
12377 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12378 			"0139 Ignoring ELS cmd code x%x ref cnt x%x Data: "
12379 			"x%x x%x x%x x%px\n",
12380 			ulp_command, kref_read(&cmdiocb->ndlp->kref),
12381 			ulp_status, ulp_word4, iotag, cmdiocb->ndlp);
12382 	/*
12383 	 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12384 	 * if exchange is busy.
12385 	 */
12386 	if (ulp_command == CMD_GEN_REQUEST64_CR)
12387 		lpfc_ct_free_iocb(phba, cmdiocb);
12388 	else
12389 		lpfc_els_free_iocb(phba, cmdiocb);
12390 
12391 	lpfc_nlp_put(ndlp);
12392 }
12393 
12394 /**
12395  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
12396  * @phba: Pointer to HBA context object.
12397  * @pring: Pointer to driver SLI ring object.
12398  * @cmdiocb: Pointer to driver command iocb object.
12399  * @cmpl: completion function.
12400  *
12401  * This function issues an abort iocb for the provided command iocb. In case
12402  * of unloading, the abort iocb will not be issued to commands on the ELS
12403  * ring. Instead, the callback function shall be changed to those commands
12404  * so that nothing happens when them finishes. This function is called with
12405  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12406  * when the command iocb is an abort request.
12407  *
12408  **/
12409 int
lpfc_sli_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * cmdiocb,void * cmpl)12410 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12411 			   struct lpfc_iocbq *cmdiocb, void *cmpl)
12412 {
12413 	struct lpfc_vport *vport = cmdiocb->vport;
12414 	struct lpfc_iocbq *abtsiocbp;
12415 	int retval = IOCB_ERROR;
12416 	unsigned long iflags;
12417 	struct lpfc_nodelist *ndlp = NULL;
12418 	u32 ulp_command = get_job_cmnd(phba, cmdiocb);
12419 	u16 ulp_context, iotag;
12420 	bool ia;
12421 
12422 	/*
12423 	 * There are certain command types we don't want to abort.  And we
12424 	 * don't want to abort commands that are already in the process of
12425 	 * being aborted.
12426 	 */
12427 	if (ulp_command == CMD_ABORT_XRI_WQE ||
12428 	    ulp_command == CMD_ABORT_XRI_CN ||
12429 	    ulp_command == CMD_CLOSE_XRI_CN ||
12430 	    cmdiocb->cmd_flag & LPFC_DRIVER_ABORTED)
12431 		return IOCB_ABORTING;
12432 
12433 	if (!pring) {
12434 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12435 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12436 		else
12437 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12438 		return retval;
12439 	}
12440 
12441 	/*
12442 	 * If we're unloading, don't abort iocb on the ELS ring, but change
12443 	 * the callback so that nothing happens when it finishes.
12444 	 */
12445 	if (test_bit(FC_UNLOADING, &vport->load_flag) &&
12446 	    pring->ringno == LPFC_ELS_RING) {
12447 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12448 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12449 		else
12450 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12451 		return retval;
12452 	}
12453 
12454 	/* issue ABTS for this IOCB based on iotag */
12455 	abtsiocbp = __lpfc_sli_get_iocbq(phba);
12456 	if (abtsiocbp == NULL)
12457 		return IOCB_NORESOURCE;
12458 
12459 	/* This signals the response to set the correct status
12460 	 * before calling the completion handler
12461 	 */
12462 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
12463 
12464 	if (phba->sli_rev == LPFC_SLI_REV4) {
12465 		ulp_context = cmdiocb->sli4_xritag;
12466 		iotag = abtsiocbp->iotag;
12467 	} else {
12468 		iotag = cmdiocb->iocb.ulpIoTag;
12469 		if (pring->ringno == LPFC_ELS_RING) {
12470 			ndlp = cmdiocb->ndlp;
12471 			ulp_context = ndlp->nlp_rpi;
12472 		} else {
12473 			ulp_context = cmdiocb->iocb.ulpContext;
12474 		}
12475 	}
12476 
12477 	/* Just close the exchange under certain conditions. */
12478 	if (test_bit(FC_UNLOADING, &vport->load_flag) ||
12479 	    phba->link_state < LPFC_LINK_UP ||
12480 	    (phba->sli_rev == LPFC_SLI_REV4 &&
12481 	     phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN) ||
12482 	    (phba->link_flag & LS_EXTERNAL_LOOPBACK))
12483 		ia = true;
12484 	else
12485 		ia = false;
12486 
12487 	lpfc_sli_prep_abort_xri(phba, abtsiocbp, ulp_context, iotag,
12488 				cmdiocb->iocb.ulpClass,
12489 				LPFC_WQE_CQ_ID_DEFAULT, ia, false);
12490 
12491 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12492 	abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
12493 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
12494 		abtsiocbp->cmd_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);
12495 
12496 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
12497 		abtsiocbp->cmd_flag |= LPFC_IO_FOF;
12498 
12499 	if (cmpl)
12500 		abtsiocbp->cmd_cmpl = cmpl;
12501 	else
12502 		abtsiocbp->cmd_cmpl = lpfc_sli_abort_els_cmpl;
12503 	abtsiocbp->vport = vport;
12504 
12505 	if (phba->sli_rev == LPFC_SLI_REV4) {
12506 		pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12507 		if (unlikely(pring == NULL))
12508 			goto abort_iotag_exit;
12509 		/* Note: both hbalock and ring_lock need to be set here */
12510 		spin_lock_irqsave(&pring->ring_lock, iflags);
12511 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12512 			abtsiocbp, 0);
12513 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
12514 	} else {
12515 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12516 			abtsiocbp, 0);
12517 	}
12518 
12519 abort_iotag_exit:
12520 
12521 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12522 			 "0339 Abort IO XRI x%x, Original iotag x%x, "
12523 			 "abort tag x%x Cmdjob : x%px Abortjob : x%px "
12524 			 "retval x%x : IA %d cmd_cmpl %ps\n",
12525 			 ulp_context, (phba->sli_rev == LPFC_SLI_REV4) ?
12526 			 cmdiocb->iotag : iotag, iotag, cmdiocb, abtsiocbp,
12527 			 retval, ia, abtsiocbp->cmd_cmpl);
12528 	if (retval) {
12529 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
12530 		__lpfc_sli_release_iocbq(phba, abtsiocbp);
12531 	}
12532 
12533 	/*
12534 	 * Caller to this routine should check for IOCB_ERROR
12535 	 * and handle it properly.  This routine no longer removes
12536 	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
12537 	 */
12538 	return retval;
12539 }
12540 
12541 /**
12542  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12543  * @phba: pointer to lpfc HBA data structure.
12544  *
12545  * This routine will abort all pending and outstanding iocbs to an HBA.
12546  **/
12547 void
lpfc_sli_hba_iocb_abort(struct lpfc_hba * phba)12548 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12549 {
12550 	struct lpfc_sli *psli = &phba->sli;
12551 	struct lpfc_sli_ring *pring;
12552 	struct lpfc_queue *qp = NULL;
12553 	int i;
12554 
12555 	if (phba->sli_rev != LPFC_SLI_REV4) {
12556 		for (i = 0; i < psli->num_rings; i++) {
12557 			pring = &psli->sli3_ring[i];
12558 			lpfc_sli_abort_iocb_ring(phba, pring);
12559 		}
12560 		return;
12561 	}
12562 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12563 		pring = qp->pring;
12564 		if (!pring)
12565 			continue;
12566 		lpfc_sli_abort_iocb_ring(phba, pring);
12567 	}
12568 }
12569 
12570 /**
12571  * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts
12572  * @iocbq: Pointer to iocb object.
12573  * @vport: Pointer to driver virtual port object.
12574  *
12575  * This function acts as an iocb filter for functions which abort FCP iocbs.
12576  *
12577  * Return values
12578  * -ENODEV, if a null iocb or vport ptr is encountered
12579  * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as
12580  *          driver already started the abort process, or is an abort iocb itself
12581  * 0, passes criteria for aborting the FCP I/O iocb
12582  **/
12583 static int
lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq * iocbq,struct lpfc_vport * vport)12584 lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,
12585 				     struct lpfc_vport *vport)
12586 {
12587 	u8 ulp_command;
12588 
12589 	/* No null ptr vports */
12590 	if (!iocbq || iocbq->vport != vport)
12591 		return -ENODEV;
12592 
12593 	/* iocb must be for FCP IO, already exists on the TX cmpl queue,
12594 	 * can't be premarked as driver aborted, nor be an ABORT iocb itself
12595 	 */
12596 	ulp_command = get_job_cmnd(vport->phba, iocbq);
12597 	if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12598 	    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ) ||
12599 	    (iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12600 	    (ulp_command == CMD_ABORT_XRI_CN ||
12601 	     ulp_command == CMD_CLOSE_XRI_CN ||
12602 	     ulp_command == CMD_ABORT_XRI_WQE))
12603 		return -EINVAL;
12604 
12605 	return 0;
12606 }
12607 
12608 /**
12609  * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target
12610  * @iocbq: Pointer to driver iocb object.
12611  * @vport: Pointer to driver virtual port object.
12612  * @tgt_id: SCSI ID of the target.
12613  * @lun_id: LUN ID of the scsi device.
12614  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12615  *
12616  * This function acts as an iocb filter for validating a lun/SCSI target/SCSI
12617  * host.
12618  *
12619  * It will return
12620  * 0 if the filtering criteria is met for the given iocb and will return
12621  * 1 if the filtering criteria is not met.
12622  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12623  * given iocb is for the SCSI device specified by vport, tgt_id and
12624  * lun_id parameter.
12625  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
12626  * given iocb is for the SCSI target specified by vport and tgt_id
12627  * parameters.
12628  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12629  * given iocb is for the SCSI host associated with the given vport.
12630  * This function is called with no locks held.
12631  **/
12632 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)12633 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12634 			   uint16_t tgt_id, uint64_t lun_id,
12635 			   lpfc_ctx_cmd ctx_cmd)
12636 {
12637 	struct lpfc_io_buf *lpfc_cmd;
12638 	int rc = 1;
12639 
12640 	lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12641 
12642 	if (lpfc_cmd->pCmd == NULL)
12643 		return rc;
12644 
12645 	switch (ctx_cmd) {
12646 	case LPFC_CTX_LUN:
12647 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12648 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12649 		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
12650 			rc = 0;
12651 		break;
12652 	case LPFC_CTX_TGT:
12653 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12654 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
12655 			rc = 0;
12656 		break;
12657 	case LPFC_CTX_HOST:
12658 		rc = 0;
12659 		break;
12660 	default:
12661 		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
12662 			__func__, ctx_cmd);
12663 		break;
12664 	}
12665 
12666 	return rc;
12667 }
12668 
12669 /**
12670  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
12671  * @vport: Pointer to virtual port.
12672  * @tgt_id: SCSI ID of the target.
12673  * @lun_id: LUN ID of the scsi device.
12674  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12675  *
12676  * This function returns number of FCP commands pending for the vport.
12677  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12678  * commands pending on the vport associated with SCSI device specified
12679  * by tgt_id and lun_id parameters.
12680  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12681  * commands pending on the vport associated with SCSI target specified
12682  * by tgt_id parameter.
12683  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12684  * commands pending on the vport.
12685  * This function returns the number of iocbs which satisfy the filter.
12686  * This function is called without any lock held.
12687  **/
12688 int
lpfc_sli_sum_iocb(struct lpfc_vport * vport,uint16_t tgt_id,uint64_t lun_id,lpfc_ctx_cmd ctx_cmd)12689 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12690 		  lpfc_ctx_cmd ctx_cmd)
12691 {
12692 	struct lpfc_hba *phba = vport->phba;
12693 	struct lpfc_iocbq *iocbq;
12694 	int sum, i;
12695 	unsigned long iflags;
12696 	u8 ulp_command;
12697 
12698 	spin_lock_irqsave(&phba->hbalock, iflags);
12699 	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12700 		iocbq = phba->sli.iocbq_lookup[i];
12701 
12702 		if (!iocbq || iocbq->vport != vport)
12703 			continue;
12704 		if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12705 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ))
12706 			continue;
12707 
12708 		/* Include counting outstanding aborts */
12709 		ulp_command = get_job_cmnd(phba, iocbq);
12710 		if (ulp_command == CMD_ABORT_XRI_CN ||
12711 		    ulp_command == CMD_CLOSE_XRI_CN ||
12712 		    ulp_command == CMD_ABORT_XRI_WQE) {
12713 			sum++;
12714 			continue;
12715 		}
12716 
12717 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12718 					       ctx_cmd) == 0)
12719 			sum++;
12720 	}
12721 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12722 
12723 	return sum;
12724 }
12725 
12726 /**
12727  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12728  * @phba: Pointer to HBA context object
12729  * @cmdiocb: Pointer to command iocb object.
12730  * @rspiocb: Pointer to response iocb object.
12731  *
12732  * This function is called when an aborted FCP iocb completes. This
12733  * function is called by the ring event handler with no lock held.
12734  * This function frees the iocb.
12735  **/
12736 void
lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12737 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12738 			struct lpfc_iocbq *rspiocb)
12739 {
12740 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12741 			"3096 ABORT_XRI_CX completing on rpi x%x "
12742 			"original iotag x%x, abort cmd iotag x%x "
12743 			"status 0x%x, reason 0x%x\n",
12744 			(phba->sli_rev == LPFC_SLI_REV4) ?
12745 			cmdiocb->sli4_xritag :
12746 			cmdiocb->iocb.un.acxri.abortContextTag,
12747 			get_job_abtsiotag(phba, cmdiocb),
12748 			cmdiocb->iotag, get_job_ulpstatus(phba, rspiocb),
12749 			get_job_word4(phba, rspiocb));
12750 	lpfc_sli_release_iocbq(phba, cmdiocb);
12751 	return;
12752 }
12753 
12754 /**
12755  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
12756  * @vport: Pointer to virtual port.
12757  * @tgt_id: SCSI ID of the target.
12758  * @lun_id: LUN ID of the scsi device.
12759  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12760  *
12761  * This function sends an abort command for every SCSI command
12762  * associated with the given virtual port pending on the ring
12763  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12764  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12765  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12766  * followed by lpfc_sli_validate_fcp_iocb.
12767  *
12768  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12769  * FCP iocbs associated with lun specified by tgt_id and lun_id
12770  * parameters
12771  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12772  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12773  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12774  * FCP iocbs associated with virtual port.
12775  * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12776  * lpfc_sli4_calc_ring is used.
12777  * This function returns number of iocbs it failed to abort.
12778  * This function is called with no locks held.
12779  **/
12780 int
lpfc_sli_abort_iocb(struct lpfc_vport * vport,u16 tgt_id,u64 lun_id,lpfc_ctx_cmd abort_cmd)12781 lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12782 		    lpfc_ctx_cmd abort_cmd)
12783 {
12784 	struct lpfc_hba *phba = vport->phba;
12785 	struct lpfc_sli_ring *pring = NULL;
12786 	struct lpfc_iocbq *iocbq;
12787 	int errcnt = 0, ret_val = 0;
12788 	unsigned long iflags;
12789 	int i;
12790 
12791 	/* all I/Os are in process of being flushed */
12792 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12793 		return errcnt;
12794 
12795 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12796 		iocbq = phba->sli.iocbq_lookup[i];
12797 
12798 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12799 			continue;
12800 
12801 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12802 					       abort_cmd) != 0)
12803 			continue;
12804 
12805 		spin_lock_irqsave(&phba->hbalock, iflags);
12806 		if (phba->sli_rev == LPFC_SLI_REV3) {
12807 			pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12808 		} else if (phba->sli_rev == LPFC_SLI_REV4) {
12809 			pring = lpfc_sli4_calc_ring(phba, iocbq);
12810 		}
12811 		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
12812 						     lpfc_sli_abort_fcp_cmpl);
12813 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12814 		if (ret_val != IOCB_SUCCESS)
12815 			errcnt++;
12816 	}
12817 
12818 	return errcnt;
12819 }
12820 
12821 /**
12822  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12823  * @vport: Pointer to virtual port.
12824  * @pring: Pointer to driver SLI ring object.
12825  * @tgt_id: SCSI ID of the target.
12826  * @lun_id: LUN ID of the scsi device.
12827  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12828  *
12829  * This function sends an abort command for every SCSI command
12830  * associated with the given virtual port pending on the ring
12831  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12832  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12833  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12834  * followed by lpfc_sli_validate_fcp_iocb.
12835  *
12836  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12837  * FCP iocbs associated with lun specified by tgt_id and lun_id
12838  * parameters
12839  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12840  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12841  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12842  * FCP iocbs associated with virtual port.
12843  * This function returns number of iocbs it aborted .
12844  * This function is called with no locks held right after a taskmgmt
12845  * command is sent.
12846  **/
12847 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)12848 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12849 			uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12850 {
12851 	struct lpfc_hba *phba = vport->phba;
12852 	struct lpfc_io_buf *lpfc_cmd;
12853 	struct lpfc_iocbq *abtsiocbq;
12854 	struct lpfc_nodelist *ndlp = NULL;
12855 	struct lpfc_iocbq *iocbq;
12856 	int sum, i, ret_val;
12857 	unsigned long iflags;
12858 	struct lpfc_sli_ring *pring_s4 = NULL;
12859 	u16 ulp_context, iotag, cqid = LPFC_WQE_CQ_ID_DEFAULT;
12860 	bool ia;
12861 
12862 	/* all I/Os are in process of being flushed */
12863 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12864 		return 0;
12865 
12866 	sum = 0;
12867 
12868 	spin_lock_irqsave(&phba->hbalock, iflags);
12869 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12870 		iocbq = phba->sli.iocbq_lookup[i];
12871 
12872 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12873 			continue;
12874 
12875 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12876 					       cmd) != 0)
12877 			continue;
12878 
12879 		/* Guard against IO completion being called at same time */
12880 		lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12881 		spin_lock(&lpfc_cmd->buf_lock);
12882 
12883 		if (!lpfc_cmd->pCmd) {
12884 			spin_unlock(&lpfc_cmd->buf_lock);
12885 			continue;
12886 		}
12887 
12888 		if (phba->sli_rev == LPFC_SLI_REV4) {
12889 			pring_s4 =
12890 			    phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12891 			if (!pring_s4) {
12892 				spin_unlock(&lpfc_cmd->buf_lock);
12893 				continue;
12894 			}
12895 			/* Note: both hbalock and ring_lock must be set here */
12896 			spin_lock(&pring_s4->ring_lock);
12897 		}
12898 
12899 		/*
12900 		 * If the iocbq is already being aborted, don't take a second
12901 		 * action, but do count it.
12902 		 */
12903 		if ((iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12904 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
12905 			if (phba->sli_rev == LPFC_SLI_REV4)
12906 				spin_unlock(&pring_s4->ring_lock);
12907 			spin_unlock(&lpfc_cmd->buf_lock);
12908 			continue;
12909 		}
12910 
12911 		/* issue ABTS for this IOCB based on iotag */
12912 		abtsiocbq = __lpfc_sli_get_iocbq(phba);
12913 		if (!abtsiocbq) {
12914 			if (phba->sli_rev == LPFC_SLI_REV4)
12915 				spin_unlock(&pring_s4->ring_lock);
12916 			spin_unlock(&lpfc_cmd->buf_lock);
12917 			continue;
12918 		}
12919 
12920 		if (phba->sli_rev == LPFC_SLI_REV4) {
12921 			iotag = abtsiocbq->iotag;
12922 			ulp_context = iocbq->sli4_xritag;
12923 			cqid = lpfc_cmd->hdwq->io_cq_map;
12924 		} else {
12925 			iotag = iocbq->iocb.ulpIoTag;
12926 			if (pring->ringno == LPFC_ELS_RING) {
12927 				ndlp = iocbq->ndlp;
12928 				ulp_context = ndlp->nlp_rpi;
12929 			} else {
12930 				ulp_context = iocbq->iocb.ulpContext;
12931 			}
12932 		}
12933 
12934 		ndlp = lpfc_cmd->rdata->pnode;
12935 
12936 		if (lpfc_is_link_up(phba) &&
12937 		    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE) &&
12938 		    !(phba->link_flag & LS_EXTERNAL_LOOPBACK))
12939 			ia = false;
12940 		else
12941 			ia = true;
12942 
12943 		lpfc_sli_prep_abort_xri(phba, abtsiocbq, ulp_context, iotag,
12944 					iocbq->iocb.ulpClass, cqid,
12945 					ia, false);
12946 
12947 		abtsiocbq->vport = vport;
12948 
12949 		/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12950 		abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12951 		if (iocbq->cmd_flag & LPFC_IO_FCP)
12952 			abtsiocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
12953 		if (iocbq->cmd_flag & LPFC_IO_FOF)
12954 			abtsiocbq->cmd_flag |= LPFC_IO_FOF;
12955 
12956 		/* Setup callback routine and issue the command. */
12957 		abtsiocbq->cmd_cmpl = lpfc_sli_abort_fcp_cmpl;
12958 
12959 		/*
12960 		 * Indicate the IO is being aborted by the driver and set
12961 		 * the caller's flag into the aborted IO.
12962 		 */
12963 		iocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
12964 
12965 		if (phba->sli_rev == LPFC_SLI_REV4) {
12966 			ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12967 							abtsiocbq, 0);
12968 			spin_unlock(&pring_s4->ring_lock);
12969 		} else {
12970 			ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12971 							abtsiocbq, 0);
12972 		}
12973 
12974 		spin_unlock(&lpfc_cmd->buf_lock);
12975 
12976 		if (ret_val == IOCB_ERROR)
12977 			__lpfc_sli_release_iocbq(phba, abtsiocbq);
12978 		else
12979 			sum++;
12980 	}
12981 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12982 	return sum;
12983 }
12984 
12985 /**
12986  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
12987  * @phba: Pointer to HBA context object.
12988  * @cmdiocbq: Pointer to command iocb.
12989  * @rspiocbq: Pointer to response iocb.
12990  *
12991  * This function is the completion handler for iocbs issued using
12992  * lpfc_sli_issue_iocb_wait function. This function is called by the
12993  * ring event handler function without any lock held. This function
12994  * can be called from both worker thread context and interrupt
12995  * context. This function also can be called from other thread which
12996  * cleans up the SLI layer objects.
12997  * This function copy the contents of the response iocb to the
12998  * response iocb memory object provided by the caller of
12999  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
13000  * sleeps for the iocb completion.
13001  **/
13002 static void
lpfc_sli_wake_iocb_wait(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_iocbq * rspiocbq)13003 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
13004 			struct lpfc_iocbq *cmdiocbq,
13005 			struct lpfc_iocbq *rspiocbq)
13006 {
13007 	wait_queue_head_t *pdone_q;
13008 	unsigned long iflags;
13009 	struct lpfc_io_buf *lpfc_cmd;
13010 	size_t offset = offsetof(struct lpfc_iocbq, wqe);
13011 
13012 	spin_lock_irqsave(&phba->hbalock, iflags);
13013 	if (cmdiocbq->cmd_flag & LPFC_IO_WAKE_TMO) {
13014 
13015 		/*
13016 		 * A time out has occurred for the iocb.  If a time out
13017 		 * completion handler has been supplied, call it.  Otherwise,
13018 		 * just free the iocbq.
13019 		 */
13020 
13021 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13022 		cmdiocbq->cmd_cmpl = cmdiocbq->wait_cmd_cmpl;
13023 		cmdiocbq->wait_cmd_cmpl = NULL;
13024 		if (cmdiocbq->cmd_cmpl)
13025 			cmdiocbq->cmd_cmpl(phba, cmdiocbq, NULL);
13026 		else
13027 			lpfc_sli_release_iocbq(phba, cmdiocbq);
13028 		return;
13029 	}
13030 
13031 	/* Copy the contents of the local rspiocb into the caller's buffer. */
13032 	cmdiocbq->cmd_flag |= LPFC_IO_WAKE;
13033 	if (cmdiocbq->rsp_iocb && rspiocbq)
13034 		memcpy((char *)cmdiocbq->rsp_iocb + offset,
13035 		       (char *)rspiocbq + offset, sizeof(*rspiocbq) - offset);
13036 
13037 	/* Set the exchange busy flag for task management commands */
13038 	if ((cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
13039 	    !(cmdiocbq->cmd_flag & LPFC_IO_LIBDFC)) {
13040 		lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
13041 					cur_iocbq);
13042 		if (rspiocbq && (rspiocbq->cmd_flag & LPFC_EXCHANGE_BUSY))
13043 			lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
13044 		else
13045 			lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
13046 	}
13047 
13048 	pdone_q = cmdiocbq->context_un.wait_queue;
13049 	if (pdone_q)
13050 		wake_up(pdone_q);
13051 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13052 	return;
13053 }
13054 
13055 /**
13056  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
13057  * @phba: Pointer to HBA context object..
13058  * @piocbq: Pointer to command iocb.
13059  * @flag: Flag to test.
13060  *
13061  * This routine grabs the hbalock and then test the cmd_flag to
13062  * see if the passed in flag is set.
13063  * Returns:
13064  * 1 if flag is set.
13065  * 0 if flag is not set.
13066  **/
13067 static int
lpfc_chk_iocb_flg(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq,uint32_t flag)13068 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
13069 		 struct lpfc_iocbq *piocbq, uint32_t flag)
13070 {
13071 	unsigned long iflags;
13072 	int ret;
13073 
13074 	spin_lock_irqsave(&phba->hbalock, iflags);
13075 	ret = piocbq->cmd_flag & flag;
13076 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13077 	return ret;
13078 
13079 }
13080 
13081 /**
13082  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
13083  * @phba: Pointer to HBA context object..
13084  * @ring_number: Ring number
13085  * @piocb: Pointer to command iocb.
13086  * @prspiocbq: Pointer to response iocb.
13087  * @timeout: Timeout in number of seconds.
13088  *
13089  * This function issues the iocb to firmware and waits for the
13090  * iocb to complete. The cmd_cmpl field of the shall be used
13091  * to handle iocbs which time out. If the field is NULL, the
13092  * function shall free the iocbq structure.  If more clean up is
13093  * needed, the caller is expected to provide a completion function
13094  * that will provide the needed clean up.  If the iocb command is
13095  * not completed within timeout seconds, the function will either
13096  * free the iocbq structure (if cmd_cmpl == NULL) or execute the
13097  * completion function set in the cmd_cmpl field and then return
13098  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
13099  * resources if this function returns IOCB_TIMEDOUT.
13100  * The function waits for the iocb completion using an
13101  * non-interruptible wait.
13102  * This function will sleep while waiting for iocb completion.
13103  * So, this function should not be called from any context which
13104  * does not allow sleeping. Due to the same reason, this function
13105  * cannot be called with interrupt disabled.
13106  * This function assumes that the iocb completions occur while
13107  * this function sleep. So, this function cannot be called from
13108  * the thread which process iocb completion for this ring.
13109  * This function clears the cmd_flag of the iocb object before
13110  * issuing the iocb and the iocb completion handler sets this
13111  * flag and wakes this thread when the iocb completes.
13112  * The contents of the response iocb will be copied to prspiocbq
13113  * by the completion handler when the command completes.
13114  * This function returns IOCB_SUCCESS when success.
13115  * This function is called with no lock held.
13116  **/
13117 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)13118 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
13119 			 uint32_t ring_number,
13120 			 struct lpfc_iocbq *piocb,
13121 			 struct lpfc_iocbq *prspiocbq,
13122 			 uint32_t timeout)
13123 {
13124 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
13125 	long timeleft, timeout_req = 0;
13126 	int retval = IOCB_SUCCESS;
13127 	uint32_t creg_val;
13128 	struct lpfc_iocbq *iocb;
13129 	int txq_cnt = 0;
13130 	int txcmplq_cnt = 0;
13131 	struct lpfc_sli_ring *pring;
13132 	unsigned long iflags;
13133 	bool iocb_completed = true;
13134 
13135 	if (phba->sli_rev >= LPFC_SLI_REV4) {
13136 		lpfc_sli_prep_wqe(phba, piocb);
13137 
13138 		pring = lpfc_sli4_calc_ring(phba, piocb);
13139 	} else
13140 		pring = &phba->sli.sli3_ring[ring_number];
13141 	/*
13142 	 * If the caller has provided a response iocbq buffer, then rsp_iocb
13143 	 * is NULL or its an error.
13144 	 */
13145 	if (prspiocbq) {
13146 		if (piocb->rsp_iocb)
13147 			return IOCB_ERROR;
13148 		piocb->rsp_iocb = prspiocbq;
13149 	}
13150 
13151 	piocb->wait_cmd_cmpl = piocb->cmd_cmpl;
13152 	piocb->cmd_cmpl = lpfc_sli_wake_iocb_wait;
13153 	piocb->context_un.wait_queue = &done_q;
13154 	piocb->cmd_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
13155 
13156 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13157 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13158 			return IOCB_ERROR;
13159 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13160 		writel(creg_val, phba->HCregaddr);
13161 		readl(phba->HCregaddr); /* flush */
13162 	}
13163 
13164 	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13165 				     SLI_IOCB_RET_IOCB);
13166 	if (retval == IOCB_SUCCESS) {
13167 		timeout_req = secs_to_jiffies(timeout);
13168 		timeleft = wait_event_timeout(done_q,
13169 				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
13170 				timeout_req);
13171 		spin_lock_irqsave(&phba->hbalock, iflags);
13172 		if (!(piocb->cmd_flag & LPFC_IO_WAKE)) {
13173 
13174 			/*
13175 			 * IOCB timed out.  Inform the wake iocb wait
13176 			 * completion function and set local status
13177 			 */
13178 
13179 			iocb_completed = false;
13180 			piocb->cmd_flag |= LPFC_IO_WAKE_TMO;
13181 		}
13182 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13183 		if (iocb_completed) {
13184 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13185 					"0331 IOCB wake signaled\n");
13186 			/* Note: we are not indicating if the IOCB has a success
13187 			 * status or not - that's for the caller to check.
13188 			 * IOCB_SUCCESS means just that the command was sent and
13189 			 * completed. Not that it completed successfully.
13190 			 * */
13191 		} else if (timeleft == 0) {
13192 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13193 					"0338 IOCB wait timeout error - no "
13194 					"wake response Data x%x\n", timeout);
13195 			retval = IOCB_TIMEDOUT;
13196 		} else {
13197 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13198 					"0330 IOCB wake NOT set, "
13199 					"Data x%x x%lx\n",
13200 					timeout, (timeleft / jiffies));
13201 			retval = IOCB_TIMEDOUT;
13202 		}
13203 	} else if (retval == IOCB_BUSY) {
13204 		if (phba->cfg_log_verbose & LOG_SLI) {
13205 			list_for_each_entry(iocb, &pring->txq, list) {
13206 				txq_cnt++;
13207 			}
13208 			list_for_each_entry(iocb, &pring->txcmplq, list) {
13209 				txcmplq_cnt++;
13210 			}
13211 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13212 				"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13213 				phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13214 		}
13215 		return retval;
13216 	} else {
13217 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13218 				"0332 IOCB wait issue failed, Data x%x\n",
13219 				retval);
13220 		retval = IOCB_ERROR;
13221 	}
13222 
13223 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13224 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13225 			return IOCB_ERROR;
13226 		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13227 		writel(creg_val, phba->HCregaddr);
13228 		readl(phba->HCregaddr); /* flush */
13229 	}
13230 
13231 	if (prspiocbq)
13232 		piocb->rsp_iocb = NULL;
13233 
13234 	piocb->context_un.wait_queue = NULL;
13235 	piocb->cmd_cmpl = NULL;
13236 	return retval;
13237 }
13238 
13239 /**
13240  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
13241  * @phba: Pointer to HBA context object.
13242  * @pmboxq: Pointer to driver mailbox object.
13243  * @timeout: Timeout in number of seconds.
13244  *
13245  * This function issues the mailbox to firmware and waits for the
13246  * mailbox command to complete. If the mailbox command is not
13247  * completed within timeout seconds, it returns MBX_TIMEOUT.
13248  * The function waits for the mailbox completion using an
13249  * interruptible wait. If the thread is woken up due to a
13250  * signal, MBX_TIMEOUT error is returned to the caller. Caller
13251  * should not free the mailbox resources, if this function returns
13252  * MBX_TIMEOUT.
13253  * This function will sleep while waiting for mailbox completion.
13254  * So, this function should not be called from any context which
13255  * does not allow sleeping. Due to the same reason, this function
13256  * cannot be called with interrupt disabled.
13257  * This function assumes that the mailbox completion occurs while
13258  * this function sleep. So, this function cannot be called from
13259  * the worker thread which processes mailbox completion.
13260  * This function is called in the context of HBA management
13261  * applications.
13262  * This function returns MBX_SUCCESS when successful.
13263  * This function is called with no lock held.
13264  **/
13265 int
lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq,uint32_t timeout)13266 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
13267 			 uint32_t timeout)
13268 {
13269 	struct completion mbox_done;
13270 	int retval;
13271 	unsigned long flag;
13272 
13273 	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
13274 	/* setup wake call as IOCB callback */
13275 	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
13276 
13277 	/* setup ctx_u field to pass wait_queue pointer to wake function  */
13278 	init_completion(&mbox_done);
13279 	pmboxq->ctx_u.mbox_wait = &mbox_done;
13280 	/* now issue the command */
13281 	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
13282 	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
13283 		wait_for_completion_timeout(&mbox_done, secs_to_jiffies(timeout));
13284 
13285 		spin_lock_irqsave(&phba->hbalock, flag);
13286 		pmboxq->ctx_u.mbox_wait = NULL;
13287 		/*
13288 		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13289 		 * else do not free the resources.
13290 		 */
13291 		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
13292 			retval = MBX_SUCCESS;
13293 		} else {
13294 			retval = MBX_TIMEOUT;
13295 			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13296 		}
13297 		spin_unlock_irqrestore(&phba->hbalock, flag);
13298 	}
13299 	return retval;
13300 }
13301 
13302 /**
13303  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
13304  * @phba: Pointer to HBA context.
13305  * @mbx_action: Mailbox shutdown options.
13306  *
13307  * This function is called to shutdown the driver's mailbox sub-system.
13308  * It first marks the mailbox sub-system is in a block state to prevent
13309  * the asynchronous mailbox command from issued off the pending mailbox
13310  * command queue. If the mailbox command sub-system shutdown is due to
13311  * HBA error conditions such as EEH or ERATT, this routine shall invoke
13312  * the mailbox sub-system flush routine to forcefully bring down the
13313  * mailbox sub-system. Otherwise, if it is due to normal condition (such
13314  * as with offline or HBA function reset), this routine will wait for the
13315  * outstanding mailbox command to complete before invoking the mailbox
13316  * sub-system flush routine to gracefully bring down mailbox sub-system.
13317  **/
13318 void
lpfc_sli_mbox_sys_shutdown(struct lpfc_hba * phba,int mbx_action)13319 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
13320 {
13321 	struct lpfc_sli *psli = &phba->sli;
13322 	unsigned long timeout;
13323 
13324 	if (mbx_action == LPFC_MBX_NO_WAIT) {
13325 		/* delay 100ms for port state */
13326 		msleep(100);
13327 		lpfc_sli_mbox_sys_flush(phba);
13328 		return;
13329 	}
13330 	timeout = secs_to_jiffies(LPFC_MBOX_TMO) + jiffies;
13331 
13332 	/* Disable softirqs, including timers from obtaining phba->hbalock */
13333 	local_bh_disable();
13334 
13335 	spin_lock_irq(&phba->hbalock);
13336 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13337 
13338 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
13339 		/* Determine how long we might wait for the active mailbox
13340 		 * command to be gracefully completed by firmware.
13341 		 */
13342 		if (phba->sli.mbox_active)
13343 			timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
13344 						phba->sli.mbox_active)) + jiffies;
13345 		spin_unlock_irq(&phba->hbalock);
13346 
13347 		/* Enable softirqs again, done with phba->hbalock */
13348 		local_bh_enable();
13349 
13350 		while (phba->sli.mbox_active) {
13351 			/* Check active mailbox complete status every 2ms */
13352 			msleep(2);
13353 			if (time_after(jiffies, timeout))
13354 				/* Timeout, let the mailbox flush routine to
13355 				 * forcefully release active mailbox command
13356 				 */
13357 				break;
13358 		}
13359 	} else {
13360 		spin_unlock_irq(&phba->hbalock);
13361 
13362 		/* Enable softirqs again, done with phba->hbalock */
13363 		local_bh_enable();
13364 	}
13365 
13366 	lpfc_sli_mbox_sys_flush(phba);
13367 }
13368 
13369 /**
13370  * lpfc_sli_eratt_read - read sli-3 error attention events
13371  * @phba: Pointer to HBA context.
13372  *
13373  * This function is called to read the SLI3 device error attention registers
13374  * for possible error attention events. The caller must hold the hostlock
13375  * with spin_lock_irq().
13376  *
13377  * This function returns 1 when there is Error Attention in the Host Attention
13378  * Register and returns 0 otherwise.
13379  **/
13380 static int
lpfc_sli_eratt_read(struct lpfc_hba * phba)13381 lpfc_sli_eratt_read(struct lpfc_hba *phba)
13382 {
13383 	uint32_t ha_copy;
13384 
13385 	/* Read chip Host Attention (HA) register */
13386 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
13387 		goto unplug_err;
13388 
13389 	if (ha_copy & HA_ERATT) {
13390 		/* Read host status register to retrieve error event */
13391 		if (lpfc_sli_read_hs(phba))
13392 			goto unplug_err;
13393 
13394 		/* Check if there is a deferred error condition is active */
13395 		if ((HS_FFER1 & phba->work_hs) &&
13396 		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13397 		      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
13398 			set_bit(DEFER_ERATT, &phba->hba_flag);
13399 			/* Clear all interrupt enable conditions */
13400 			writel(0, phba->HCregaddr);
13401 			readl(phba->HCregaddr);
13402 		}
13403 
13404 		/* Set the driver HA work bitmap */
13405 		phba->work_ha |= HA_ERATT;
13406 		/* Indicate polling handles this ERATT */
13407 		set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13408 		return 1;
13409 	}
13410 	return 0;
13411 
13412 unplug_err:
13413 	/* Set the driver HS work bitmap */
13414 	phba->work_hs |= UNPLUG_ERR;
13415 	/* Set the driver HA work bitmap */
13416 	phba->work_ha |= HA_ERATT;
13417 	/* Indicate polling handles this ERATT */
13418 	set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13419 	return 1;
13420 }
13421 
13422 /**
13423  * lpfc_sli4_eratt_read - read sli-4 error attention events
13424  * @phba: Pointer to HBA context.
13425  *
13426  * This function is called to read the SLI4 device error attention registers
13427  * for possible error attention events. The caller must hold the hostlock
13428  * with spin_lock_irq().
13429  *
13430  * This function returns 1 when there is Error Attention in the Host Attention
13431  * Register and returns 0 otherwise.
13432  **/
13433 static int
lpfc_sli4_eratt_read(struct lpfc_hba * phba)13434 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13435 {
13436 	uint32_t uerr_sta_hi, uerr_sta_lo;
13437 	uint32_t if_type, portsmphr;
13438 	struct lpfc_register portstat_reg;
13439 	u32 logmask;
13440 
13441 	/*
13442 	 * For now, use the SLI4 device internal unrecoverable error
13443 	 * registers for error attention. This can be changed later.
13444 	 */
13445 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13446 	switch (if_type) {
13447 	case LPFC_SLI_INTF_IF_TYPE_0:
13448 		if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13449 			&uerr_sta_lo) ||
13450 			lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13451 			&uerr_sta_hi)) {
13452 			phba->work_hs |= UNPLUG_ERR;
13453 			phba->work_ha |= HA_ERATT;
13454 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13455 			return 1;
13456 		}
13457 		if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13458 		    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
13459 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13460 					"1423 HBA Unrecoverable error: "
13461 					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13462 					"ue_mask_lo_reg=0x%x, "
13463 					"ue_mask_hi_reg=0x%x\n",
13464 					uerr_sta_lo, uerr_sta_hi,
13465 					phba->sli4_hba.ue_mask_lo,
13466 					phba->sli4_hba.ue_mask_hi);
13467 			phba->work_status[0] = uerr_sta_lo;
13468 			phba->work_status[1] = uerr_sta_hi;
13469 			phba->work_ha |= HA_ERATT;
13470 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13471 			return 1;
13472 		}
13473 		break;
13474 	case LPFC_SLI_INTF_IF_TYPE_2:
13475 	case LPFC_SLI_INTF_IF_TYPE_6:
13476 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13477 			&portstat_reg.word0) ||
13478 			lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13479 			&portsmphr)){
13480 			phba->work_hs |= UNPLUG_ERR;
13481 			phba->work_ha |= HA_ERATT;
13482 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13483 			return 1;
13484 		}
13485 		if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13486 			phba->work_status[0] =
13487 				readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13488 			phba->work_status[1] =
13489 				readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
13490 			logmask = LOG_TRACE_EVENT;
13491 			if (phba->work_status[0] ==
13492 				SLIPORT_ERR1_REG_ERR_CODE_2 &&
13493 			    phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)
13494 				logmask = LOG_SLI;
13495 			lpfc_printf_log(phba, KERN_ERR, logmask,
13496 					"2885 Port Status Event: "
13497 					"port status reg 0x%x, "
13498 					"port smphr reg 0x%x, "
13499 					"error 1=0x%x, error 2=0x%x\n",
13500 					portstat_reg.word0,
13501 					portsmphr,
13502 					phba->work_status[0],
13503 					phba->work_status[1]);
13504 			phba->work_ha |= HA_ERATT;
13505 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13506 			return 1;
13507 		}
13508 		break;
13509 	case LPFC_SLI_INTF_IF_TYPE_1:
13510 	default:
13511 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13512 				"2886 HBA Error Attention on unsupported "
13513 				"if type %d.", if_type);
13514 		return 1;
13515 	}
13516 
13517 	return 0;
13518 }
13519 
13520 /**
13521  * lpfc_sli_check_eratt - check error attention events
13522  * @phba: Pointer to HBA context.
13523  *
13524  * This function is called from timer soft interrupt context to check HBA's
13525  * error attention register bit for error attention events.
13526  *
13527  * This function returns 1 when there is Error Attention in the Host Attention
13528  * Register and returns 0 otherwise.
13529  **/
13530 int
lpfc_sli_check_eratt(struct lpfc_hba * phba)13531 lpfc_sli_check_eratt(struct lpfc_hba *phba)
13532 {
13533 	uint32_t ha_copy;
13534 
13535 	/* If somebody is waiting to handle an eratt, don't process it
13536 	 * here. The brdkill function will do this.
13537 	 */
13538 	if (phba->link_flag & LS_IGNORE_ERATT)
13539 		return 0;
13540 
13541 	/* Check if interrupt handler handles this ERATT */
13542 	if (test_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
13543 		/* Interrupt handler has handled ERATT */
13544 		return 0;
13545 
13546 	/*
13547 	 * If there is deferred error attention, do not check for error
13548 	 * attention
13549 	 */
13550 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13551 		return 0;
13552 
13553 	spin_lock_irq(&phba->hbalock);
13554 	/* If PCI channel is offline, don't process it */
13555 	if (unlikely(pci_channel_offline(phba->pcidev))) {
13556 		spin_unlock_irq(&phba->hbalock);
13557 		return 0;
13558 	}
13559 
13560 	switch (phba->sli_rev) {
13561 	case LPFC_SLI_REV2:
13562 	case LPFC_SLI_REV3:
13563 		/* Read chip Host Attention (HA) register */
13564 		ha_copy = lpfc_sli_eratt_read(phba);
13565 		break;
13566 	case LPFC_SLI_REV4:
13567 		/* Read device Uncoverable Error (UERR) registers */
13568 		ha_copy = lpfc_sli4_eratt_read(phba);
13569 		break;
13570 	default:
13571 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13572 				"0299 Invalid SLI revision (%d)\n",
13573 				phba->sli_rev);
13574 		ha_copy = 0;
13575 		break;
13576 	}
13577 	spin_unlock_irq(&phba->hbalock);
13578 
13579 	return ha_copy;
13580 }
13581 
13582 /**
13583  * lpfc_intr_state_check - Check device state for interrupt handling
13584  * @phba: Pointer to HBA context.
13585  *
13586  * This inline routine checks whether a device or its PCI slot is in a state
13587  * that the interrupt should be handled.
13588  *
13589  * This function returns 0 if the device or the PCI slot is in a state that
13590  * interrupt should be handled, otherwise -EIO.
13591  */
13592 static inline int
lpfc_intr_state_check(struct lpfc_hba * phba)13593 lpfc_intr_state_check(struct lpfc_hba *phba)
13594 {
13595 	/* If the pci channel is offline, ignore all the interrupts */
13596 	if (unlikely(pci_channel_offline(phba->pcidev)))
13597 		return -EIO;
13598 
13599 	/* Update device level interrupt statistics */
13600 	phba->sli.slistat.sli_intr++;
13601 
13602 	/* Ignore all interrupts during initialization. */
13603 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13604 		return -EIO;
13605 
13606 	return 0;
13607 }
13608 
13609 /**
13610  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
13611  * @irq: Interrupt number.
13612  * @dev_id: The device context pointer.
13613  *
13614  * This function is directly called from the PCI layer as an interrupt
13615  * service routine when device with SLI-3 interface spec is enabled with
13616  * MSI-X multi-message interrupt mode and there are slow-path events in
13617  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13618  * interrupt mode, this function is called as part of the device-level
13619  * interrupt handler. When the PCI slot is in error recovery or the HBA
13620  * is undergoing initialization, the interrupt handler will not process
13621  * the interrupt. The link attention and ELS ring attention events are
13622  * handled by the worker thread. The interrupt handler signals the worker
13623  * thread and returns for these events. This function is called without
13624  * any lock held. It gets the hbalock to access and update SLI data
13625  * structures.
13626  *
13627  * This function returns IRQ_HANDLED when interrupt is handled else it
13628  * returns IRQ_NONE.
13629  **/
13630 irqreturn_t
lpfc_sli_sp_intr_handler(int irq,void * dev_id)13631 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
13632 {
13633 	struct lpfc_hba  *phba;
13634 	uint32_t ha_copy, hc_copy;
13635 	uint32_t work_ha_copy;
13636 	unsigned long status;
13637 	unsigned long iflag;
13638 	uint32_t control;
13639 
13640 	MAILBOX_t *mbox, *pmbox;
13641 	struct lpfc_vport *vport;
13642 	struct lpfc_nodelist *ndlp;
13643 	struct lpfc_dmabuf *mp;
13644 	LPFC_MBOXQ_t *pmb;
13645 	int rc;
13646 
13647 	/*
13648 	 * Get the driver's phba structure from the dev_id and
13649 	 * assume the HBA is not interrupting.
13650 	 */
13651 	phba = (struct lpfc_hba *)dev_id;
13652 
13653 	if (unlikely(!phba))
13654 		return IRQ_NONE;
13655 
13656 	/*
13657 	 * Stuff needs to be attented to when this function is invoked as an
13658 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13659 	 */
13660 	if (phba->intr_type == MSIX) {
13661 		/* Check device state for handling interrupt */
13662 		if (lpfc_intr_state_check(phba))
13663 			return IRQ_NONE;
13664 		/* Need to read HA REG for slow-path events */
13665 		spin_lock_irqsave(&phba->hbalock, iflag);
13666 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13667 			goto unplug_error;
13668 		/* If somebody is waiting to handle an eratt don't process it
13669 		 * here. The brdkill function will do this.
13670 		 */
13671 		if (phba->link_flag & LS_IGNORE_ERATT)
13672 			ha_copy &= ~HA_ERATT;
13673 		/* Check the need for handling ERATT in interrupt handler */
13674 		if (ha_copy & HA_ERATT) {
13675 			if (test_and_set_bit(HBA_ERATT_HANDLED,
13676 					     &phba->hba_flag))
13677 				/* ERATT polling has handled ERATT */
13678 				ha_copy &= ~HA_ERATT;
13679 		}
13680 
13681 		/*
13682 		 * If there is deferred error attention, do not check for any
13683 		 * interrupt.
13684 		 */
13685 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
13686 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13687 			return IRQ_NONE;
13688 		}
13689 
13690 		/* Clear up only attention source related to slow-path */
13691 		if (lpfc_readl(phba->HCregaddr, &hc_copy))
13692 			goto unplug_error;
13693 
13694 		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13695 			HC_LAINT_ENA | HC_ERINT_ENA),
13696 			phba->HCregaddr);
13697 		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13698 			phba->HAregaddr);
13699 		writel(hc_copy, phba->HCregaddr);
13700 		readl(phba->HAregaddr); /* flush */
13701 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13702 	} else
13703 		ha_copy = phba->ha_copy;
13704 
13705 	work_ha_copy = ha_copy & phba->work_ha_mask;
13706 
13707 	if (work_ha_copy) {
13708 		if (work_ha_copy & HA_LATT) {
13709 			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13710 				/*
13711 				 * Turn off Link Attention interrupts
13712 				 * until CLEAR_LA done
13713 				 */
13714 				spin_lock_irqsave(&phba->hbalock, iflag);
13715 				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
13716 				if (lpfc_readl(phba->HCregaddr, &control))
13717 					goto unplug_error;
13718 				control &= ~HC_LAINT_ENA;
13719 				writel(control, phba->HCregaddr);
13720 				readl(phba->HCregaddr); /* flush */
13721 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13722 			}
13723 			else
13724 				work_ha_copy &= ~HA_LATT;
13725 		}
13726 
13727 		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
13728 			/*
13729 			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13730 			 * the only slow ring.
13731 			 */
13732 			status = (work_ha_copy &
13733 				(HA_RXMASK  << (4*LPFC_ELS_RING)));
13734 			status >>= (4*LPFC_ELS_RING);
13735 			if (status & HA_RXMASK) {
13736 				spin_lock_irqsave(&phba->hbalock, iflag);
13737 				if (lpfc_readl(phba->HCregaddr, &control))
13738 					goto unplug_error;
13739 
13740 				lpfc_debugfs_slow_ring_trc(phba,
13741 				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
13742 				control, status,
13743 				(uint32_t)phba->sli.slistat.sli_intr);
13744 
13745 				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
13746 					lpfc_debugfs_slow_ring_trc(phba,
13747 						"ISR Disable ring:"
13748 						"pwork:x%x hawork:x%x wait:x%x",
13749 						phba->work_ha, work_ha_copy,
13750 						(uint32_t)((unsigned long)
13751 						&phba->work_waitq));
13752 
13753 					control &=
13754 					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
13755 					writel(control, phba->HCregaddr);
13756 					readl(phba->HCregaddr); /* flush */
13757 				}
13758 				else {
13759 					lpfc_debugfs_slow_ring_trc(phba,
13760 						"ISR slow ring:   pwork:"
13761 						"x%x hawork:x%x wait:x%x",
13762 						phba->work_ha, work_ha_copy,
13763 						(uint32_t)((unsigned long)
13764 						&phba->work_waitq));
13765 				}
13766 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13767 			}
13768 		}
13769 		spin_lock_irqsave(&phba->hbalock, iflag);
13770 		if (work_ha_copy & HA_ERATT) {
13771 			if (lpfc_sli_read_hs(phba))
13772 				goto unplug_error;
13773 			/*
13774 			 * Check if there is a deferred error condition
13775 			 * is active
13776 			 */
13777 			if ((HS_FFER1 & phba->work_hs) &&
13778 				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13779 				  HS_FFER6 | HS_FFER7 | HS_FFER8) &
13780 				  phba->work_hs)) {
13781 				set_bit(DEFER_ERATT, &phba->hba_flag);
13782 				/* Clear all interrupt enable conditions */
13783 				writel(0, phba->HCregaddr);
13784 				readl(phba->HCregaddr);
13785 			}
13786 		}
13787 
13788 		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
13789 			pmb = phba->sli.mbox_active;
13790 			pmbox = &pmb->u.mb;
13791 			mbox = phba->mbox;
13792 			vport = pmb->vport;
13793 
13794 			/* First check out the status word */
13795 			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13796 			if (pmbox->mbxOwner != OWN_HOST) {
13797 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13798 				/*
13799 				 * Stray Mailbox Interrupt, mbxCommand <cmd>
13800 				 * mbxStatus <status>
13801 				 */
13802 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13803 						"(%d):0304 Stray Mailbox "
13804 						"Interrupt mbxCommand x%x "
13805 						"mbxStatus x%x\n",
13806 						(vport ? vport->vpi : 0),
13807 						pmbox->mbxCommand,
13808 						pmbox->mbxStatus);
13809 				/* clear mailbox attention bit */
13810 				work_ha_copy &= ~HA_MBATT;
13811 			} else {
13812 				phba->sli.mbox_active = NULL;
13813 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13814 				phba->last_completion_time = jiffies;
13815 				timer_delete(&phba->sli.mbox_tmo);
13816 				if (pmb->mbox_cmpl) {
13817 					lpfc_sli_pcimem_bcopy(mbox, pmbox,
13818 							MAILBOX_CMD_SIZE);
13819 					if (pmb->out_ext_byte_len &&
13820 						pmb->ext_buf)
13821 						lpfc_sli_pcimem_bcopy(
13822 						phba->mbox_ext,
13823 						pmb->ext_buf,
13824 						pmb->out_ext_byte_len);
13825 				}
13826 				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13827 					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13828 
13829 					lpfc_debugfs_disc_trc(vport,
13830 						LPFC_DISC_TRC_MBOX_VPORT,
13831 						"MBOX dflt rpi: : "
13832 						"status:x%x rpi:x%x",
13833 						(uint32_t)pmbox->mbxStatus,
13834 						pmbox->un.varWords[0], 0);
13835 
13836 					if (!pmbox->mbxStatus) {
13837 						mp = pmb->ctx_buf;
13838 						ndlp = pmb->ctx_ndlp;
13839 
13840 						/* Reg_LOGIN of dflt RPI was
13841 						 * successful. new lets get
13842 						 * rid of the RPI using the
13843 						 * same mbox buffer.
13844 						 */
13845 						lpfc_unreg_login(phba,
13846 							vport->vpi,
13847 							pmbox->un.varWords[0],
13848 							pmb);
13849 						pmb->mbox_cmpl =
13850 							lpfc_mbx_cmpl_dflt_rpi;
13851 						pmb->ctx_buf = mp;
13852 						pmb->ctx_ndlp = ndlp;
13853 						pmb->vport = vport;
13854 						rc = lpfc_sli_issue_mbox(phba,
13855 								pmb,
13856 								MBX_NOWAIT);
13857 						if (rc != MBX_BUSY)
13858 							lpfc_printf_log(phba,
13859 							KERN_ERR,
13860 							LOG_TRACE_EVENT,
13861 							"0350 rc should have"
13862 							"been MBX_BUSY\n");
13863 						if (rc != MBX_NOT_FINISHED)
13864 							goto send_current_mbox;
13865 					}
13866 				}
13867 				spin_lock_irqsave(
13868 						&phba->pport->work_port_lock,
13869 						iflag);
13870 				phba->pport->work_port_events &=
13871 					~WORKER_MBOX_TMO;
13872 				spin_unlock_irqrestore(
13873 						&phba->pport->work_port_lock,
13874 						iflag);
13875 
13876 				/* Do NOT queue MBX_HEARTBEAT to the worker
13877 				 * thread for processing.
13878 				 */
13879 				if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13880 					/* Process mbox now */
13881 					phba->sli.mbox_active = NULL;
13882 					phba->sli.sli_flag &=
13883 						~LPFC_SLI_MBOX_ACTIVE;
13884 					if (pmb->mbox_cmpl)
13885 						pmb->mbox_cmpl(phba, pmb);
13886 				} else {
13887 					/* Queue to worker thread to process */
13888 					lpfc_mbox_cmpl_put(phba, pmb);
13889 				}
13890 			}
13891 		} else
13892 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13893 
13894 		if ((work_ha_copy & HA_MBATT) &&
13895 		    (phba->sli.mbox_active == NULL)) {
13896 send_current_mbox:
13897 			/* Process next mailbox command if there is one */
13898 			do {
13899 				rc = lpfc_sli_issue_mbox(phba, NULL,
13900 							 MBX_NOWAIT);
13901 			} while (rc == MBX_NOT_FINISHED);
13902 			if (rc != MBX_SUCCESS)
13903 				lpfc_printf_log(phba, KERN_ERR,
13904 						LOG_TRACE_EVENT,
13905 						"0349 rc should be "
13906 						"MBX_SUCCESS\n");
13907 		}
13908 
13909 		spin_lock_irqsave(&phba->hbalock, iflag);
13910 		phba->work_ha |= work_ha_copy;
13911 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13912 		lpfc_worker_wake_up(phba);
13913 	}
13914 	return IRQ_HANDLED;
13915 unplug_error:
13916 	spin_unlock_irqrestore(&phba->hbalock, iflag);
13917 	return IRQ_HANDLED;
13918 
13919 } /* lpfc_sli_sp_intr_handler */
13920 
13921 /**
13922  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13923  * @irq: Interrupt number.
13924  * @dev_id: The device context pointer.
13925  *
13926  * This function is directly called from the PCI layer as an interrupt
13927  * service routine when device with SLI-3 interface spec is enabled with
13928  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13929  * ring event in the HBA. However, when the device is enabled with either
13930  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13931  * device-level interrupt handler. When the PCI slot is in error recovery
13932  * or the HBA is undergoing initialization, the interrupt handler will not
13933  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13934  * the intrrupt context. This function is called without any lock held.
13935  * It gets the hbalock to access and update SLI data structures.
13936  *
13937  * This function returns IRQ_HANDLED when interrupt is handled else it
13938  * returns IRQ_NONE.
13939  **/
13940 irqreturn_t
lpfc_sli_fp_intr_handler(int irq,void * dev_id)13941 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13942 {
13943 	struct lpfc_hba  *phba;
13944 	uint32_t ha_copy;
13945 	unsigned long status;
13946 	unsigned long iflag;
13947 	struct lpfc_sli_ring *pring;
13948 
13949 	/* Get the driver's phba structure from the dev_id and
13950 	 * assume the HBA is not interrupting.
13951 	 */
13952 	phba = (struct lpfc_hba *) dev_id;
13953 
13954 	if (unlikely(!phba))
13955 		return IRQ_NONE;
13956 
13957 	/*
13958 	 * Stuff needs to be attented to when this function is invoked as an
13959 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13960 	 */
13961 	if (phba->intr_type == MSIX) {
13962 		/* Check device state for handling interrupt */
13963 		if (lpfc_intr_state_check(phba))
13964 			return IRQ_NONE;
13965 		/* Need to read HA REG for FCP ring and other ring events */
13966 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13967 			return IRQ_HANDLED;
13968 
13969 		/*
13970 		 * If there is deferred error attention, do not check for
13971 		 * any interrupt.
13972 		 */
13973 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13974 			return IRQ_NONE;
13975 
13976 		/* Clear up only attention source related to fast-path */
13977 		spin_lock_irqsave(&phba->hbalock, iflag);
13978 		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13979 			phba->HAregaddr);
13980 		readl(phba->HAregaddr); /* flush */
13981 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13982 	} else
13983 		ha_copy = phba->ha_copy;
13984 
13985 	/*
13986 	 * Process all events on FCP ring. Take the optimized path for FCP IO.
13987 	 */
13988 	ha_copy &= ~(phba->work_ha_mask);
13989 
13990 	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13991 	status >>= (4*LPFC_FCP_RING);
13992 	pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
13993 	if (status & HA_RXMASK)
13994 		lpfc_sli_handle_fast_ring_event(phba, pring, status);
13995 
13996 	if (phba->cfg_multi_ring_support == 2) {
13997 		/*
13998 		 * Process all events on extra ring. Take the optimized path
13999 		 * for extra ring IO.
14000 		 */
14001 		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14002 		status >>= (4*LPFC_EXTRA_RING);
14003 		if (status & HA_RXMASK) {
14004 			lpfc_sli_handle_fast_ring_event(phba,
14005 					&phba->sli.sli3_ring[LPFC_EXTRA_RING],
14006 					status);
14007 		}
14008 	}
14009 	return IRQ_HANDLED;
14010 }  /* lpfc_sli_fp_intr_handler */
14011 
14012 /**
14013  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
14014  * @irq: Interrupt number.
14015  * @dev_id: The device context pointer.
14016  *
14017  * This function is the HBA device-level interrupt handler to device with
14018  * SLI-3 interface spec, called from the PCI layer when either MSI or
14019  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
14020  * requires driver attention. This function invokes the slow-path interrupt
14021  * attention handling function and fast-path interrupt attention handling
14022  * function in turn to process the relevant HBA attention events. This
14023  * function is called without any lock held. It gets the hbalock to access
14024  * and update SLI data structures.
14025  *
14026  * This function returns IRQ_HANDLED when interrupt is handled, else it
14027  * returns IRQ_NONE.
14028  **/
14029 irqreturn_t
lpfc_sli_intr_handler(int irq,void * dev_id)14030 lpfc_sli_intr_handler(int irq, void *dev_id)
14031 {
14032 	struct lpfc_hba  *phba;
14033 	irqreturn_t sp_irq_rc, fp_irq_rc;
14034 	unsigned long status1, status2;
14035 	uint32_t hc_copy;
14036 
14037 	/*
14038 	 * Get the driver's phba structure from the dev_id and
14039 	 * assume the HBA is not interrupting.
14040 	 */
14041 	phba = (struct lpfc_hba *) dev_id;
14042 
14043 	if (unlikely(!phba))
14044 		return IRQ_NONE;
14045 
14046 	/* Check device state for handling interrupt */
14047 	if (lpfc_intr_state_check(phba))
14048 		return IRQ_NONE;
14049 
14050 	spin_lock(&phba->hbalock);
14051 	if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
14052 		spin_unlock(&phba->hbalock);
14053 		return IRQ_HANDLED;
14054 	}
14055 
14056 	if (unlikely(!phba->ha_copy)) {
14057 		spin_unlock(&phba->hbalock);
14058 		return IRQ_NONE;
14059 	} else if (phba->ha_copy & HA_ERATT) {
14060 		if (test_and_set_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
14061 			/* ERATT polling has handled ERATT */
14062 			phba->ha_copy &= ~HA_ERATT;
14063 	}
14064 
14065 	/*
14066 	 * If there is deferred error attention, do not check for any interrupt.
14067 	 */
14068 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
14069 		spin_unlock(&phba->hbalock);
14070 		return IRQ_NONE;
14071 	}
14072 
14073 	/* Clear attention sources except link and error attentions */
14074 	if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
14075 		spin_unlock(&phba->hbalock);
14076 		return IRQ_HANDLED;
14077 	}
14078 	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
14079 		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
14080 		phba->HCregaddr);
14081 	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
14082 	writel(hc_copy, phba->HCregaddr);
14083 	readl(phba->HAregaddr); /* flush */
14084 	spin_unlock(&phba->hbalock);
14085 
14086 	/*
14087 	 * Invokes slow-path host attention interrupt handling as appropriate.
14088 	 */
14089 
14090 	/* status of events with mailbox and link attention */
14091 	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
14092 
14093 	/* status of events with ELS ring */
14094 	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
14095 	status2 >>= (4*LPFC_ELS_RING);
14096 
14097 	if (status1 || (status2 & HA_RXMASK))
14098 		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
14099 	else
14100 		sp_irq_rc = IRQ_NONE;
14101 
14102 	/*
14103 	 * Invoke fast-path host attention interrupt handling as appropriate.
14104 	 */
14105 
14106 	/* status of events with FCP ring */
14107 	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14108 	status1 >>= (4*LPFC_FCP_RING);
14109 
14110 	/* status of events with extra ring */
14111 	if (phba->cfg_multi_ring_support == 2) {
14112 		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14113 		status2 >>= (4*LPFC_EXTRA_RING);
14114 	} else
14115 		status2 = 0;
14116 
14117 	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
14118 		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
14119 	else
14120 		fp_irq_rc = IRQ_NONE;
14121 
14122 	/* Return device-level interrupt handling status */
14123 	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
14124 }  /* lpfc_sli_intr_handler */
14125 
14126 /**
14127  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14128  * @phba: pointer to lpfc hba data structure.
14129  *
14130  * This routine is invoked by the worker thread to process all the pending
14131  * SLI4 els abort xri events.
14132  **/
lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba * phba)14133 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14134 {
14135 	struct lpfc_cq_event *cq_event;
14136 	unsigned long iflags;
14137 
14138 	/* First, declare the els xri abort event has been handled */
14139 	clear_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14140 
14141 	/* Now, handle all the els xri abort events */
14142 	spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14143 	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14144 		/* Get the first event from the head of the event queue */
14145 		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14146 				 cq_event, struct lpfc_cq_event, list);
14147 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14148 				       iflags);
14149 		/* Notify aborted XRI for ELS work queue */
14150 		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
14151 
14152 		/* Free the event processed back to the free pool */
14153 		lpfc_sli4_cq_event_release(phba, cq_event);
14154 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14155 				  iflags);
14156 	}
14157 	spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14158 }
14159 
14160 /**
14161  * lpfc_sli4_els_preprocess_rspiocbq - Get response iocbq from els wcqe
14162  * @phba: Pointer to HBA context object.
14163  * @irspiocbq: Pointer to work-queue completion queue entry.
14164  *
14165  * This routine handles an ELS work-queue completion event and construct
14166  * a pseudo response ELS IOCBQ from the SLI4 ELS WCQE for the common
14167  * discovery engine to handle.
14168  *
14169  * Return: Pointer to the receive IOCBQ, NULL otherwise.
14170  **/
14171 static struct lpfc_iocbq *
lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba * phba,struct lpfc_iocbq * irspiocbq)14172 lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
14173 				  struct lpfc_iocbq *irspiocbq)
14174 {
14175 	struct lpfc_sli_ring *pring;
14176 	struct lpfc_iocbq *cmdiocbq;
14177 	struct lpfc_wcqe_complete *wcqe;
14178 	unsigned long iflags;
14179 
14180 	pring = lpfc_phba_elsring(phba);
14181 	if (unlikely(!pring))
14182 		return NULL;
14183 
14184 	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
14185 	spin_lock_irqsave(&pring->ring_lock, iflags);
14186 	pring->stats.iocb_event++;
14187 	/* Look up the ELS command IOCB and create pseudo response IOCB */
14188 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14189 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
14190 	if (unlikely(!cmdiocbq)) {
14191 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
14192 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14193 				"0386 ELS complete with no corresponding "
14194 				"cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14195 				wcqe->word0, wcqe->total_data_placed,
14196 				wcqe->parameter, wcqe->word3);
14197 		lpfc_sli_release_iocbq(phba, irspiocbq);
14198 		return NULL;
14199 	}
14200 
14201 	memcpy(&irspiocbq->wqe, &cmdiocbq->wqe, sizeof(union lpfc_wqe128));
14202 	memcpy(&irspiocbq->wcqe_cmpl, wcqe, sizeof(*wcqe));
14203 
14204 	/* Put the iocb back on the txcmplq */
14205 	lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14206 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
14207 
14208 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14209 		spin_lock_irqsave(&phba->hbalock, iflags);
14210 		irspiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
14211 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14212 	}
14213 
14214 	return irspiocbq;
14215 }
14216 
14217 inline struct lpfc_cq_event *
lpfc_cq_event_setup(struct lpfc_hba * phba,void * entry,int size)14218 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14219 {
14220 	struct lpfc_cq_event *cq_event;
14221 
14222 	/* Allocate a new internal CQ_EVENT entry */
14223 	cq_event = lpfc_sli4_cq_event_alloc(phba);
14224 	if (!cq_event) {
14225 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14226 				"0602 Failed to alloc CQ_EVENT entry\n");
14227 		return NULL;
14228 	}
14229 
14230 	/* Move the CQE into the event */
14231 	memcpy(&cq_event->cqe, entry, size);
14232 	return cq_event;
14233 }
14234 
14235 /**
14236  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
14237  * @phba: Pointer to HBA context object.
14238  * @mcqe: Pointer to mailbox completion queue entry.
14239  *
14240  * This routine process a mailbox completion queue entry with asynchronous
14241  * event.
14242  *
14243  * Return: true if work posted to worker thread, otherwise false.
14244  **/
14245 static bool
lpfc_sli4_sp_handle_async_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14246 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14247 {
14248 	struct lpfc_cq_event *cq_event;
14249 	unsigned long iflags;
14250 
14251 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14252 			"0392 Async Event: word0:x%x, word1:x%x, "
14253 			"word2:x%x, word3:x%x\n", mcqe->word0,
14254 			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14255 
14256 	cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14257 	if (!cq_event)
14258 		return false;
14259 
14260 	spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
14261 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
14262 	spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14263 
14264 	/* Set the async event flag */
14265 	set_bit(ASYNC_EVENT, &phba->hba_flag);
14266 
14267 	return true;
14268 }
14269 
14270 /**
14271  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14272  * @phba: Pointer to HBA context object.
14273  * @mcqe: Pointer to mailbox completion queue entry.
14274  *
14275  * This routine process a mailbox completion queue entry with mailbox
14276  * completion event.
14277  *
14278  * Return: true if work posted to worker thread, otherwise false.
14279  **/
14280 static bool
lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14281 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14282 {
14283 	uint32_t mcqe_status;
14284 	MAILBOX_t *mbox, *pmbox;
14285 	struct lpfc_mqe *mqe;
14286 	struct lpfc_vport *vport;
14287 	struct lpfc_nodelist *ndlp;
14288 	struct lpfc_dmabuf *mp;
14289 	unsigned long iflags;
14290 	LPFC_MBOXQ_t *pmb;
14291 	bool workposted = false;
14292 	int rc;
14293 
14294 	/* If not a mailbox complete MCQE, out by checking mailbox consume */
14295 	if (!bf_get(lpfc_trailer_completed, mcqe))
14296 		goto out_no_mqe_complete;
14297 
14298 	/* Get the reference to the active mbox command */
14299 	spin_lock_irqsave(&phba->hbalock, iflags);
14300 	pmb = phba->sli.mbox_active;
14301 	if (unlikely(!pmb)) {
14302 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14303 				"1832 No pending MBOX command to handle\n");
14304 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14305 		goto out_no_mqe_complete;
14306 	}
14307 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14308 	mqe = &pmb->u.mqe;
14309 	pmbox = (MAILBOX_t *)&pmb->u.mqe;
14310 	mbox = phba->mbox;
14311 	vport = pmb->vport;
14312 
14313 	/* Reset heartbeat timer */
14314 	phba->last_completion_time = jiffies;
14315 	timer_delete(&phba->sli.mbox_tmo);
14316 
14317 	/* Move mbox data to caller's mailbox region, do endian swapping */
14318 	if (pmb->mbox_cmpl && mbox)
14319 		lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
14320 
14321 	/*
14322 	 * For mcqe errors, conditionally move a modified error code to
14323 	 * the mbox so that the error will not be missed.
14324 	 */
14325 	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14326 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14327 		if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14328 			bf_set(lpfc_mqe_status, mqe,
14329 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
14330 	}
14331 	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14332 		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14333 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14334 				      "MBOX dflt rpi: status:x%x rpi:x%x",
14335 				      mcqe_status,
14336 				      pmbox->un.varWords[0], 0);
14337 		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
14338 			mp = pmb->ctx_buf;
14339 			ndlp = pmb->ctx_ndlp;
14340 
14341 			/* Reg_LOGIN of dflt RPI was successful. Mark the
14342 			 * node as having an UNREG_LOGIN in progress to stop
14343 			 * an unsolicited PLOGI from the same NPortId from
14344 			 * starting another mailbox transaction.
14345 			 */
14346 			set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
14347 			lpfc_unreg_login(phba, vport->vpi,
14348 					 pmbox->un.varWords[0], pmb);
14349 			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
14350 			pmb->ctx_buf = mp;
14351 
14352 			/* No reference taken here.  This is a default
14353 			 * RPI reg/immediate unreg cycle. The reference was
14354 			 * taken in the reg rpi path and is released when
14355 			 * this mailbox completes.
14356 			 */
14357 			pmb->ctx_ndlp = ndlp;
14358 			pmb->vport = vport;
14359 			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14360 			if (rc != MBX_BUSY)
14361 				lpfc_printf_log(phba, KERN_ERR,
14362 						LOG_TRACE_EVENT,
14363 						"0385 rc should "
14364 						"have been MBX_BUSY\n");
14365 			if (rc != MBX_NOT_FINISHED)
14366 				goto send_current_mbox;
14367 		}
14368 	}
14369 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14370 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14371 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14372 
14373 	/* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14374 	if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14375 		spin_lock_irqsave(&phba->hbalock, iflags);
14376 		/* Release the mailbox command posting token */
14377 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14378 		phba->sli.mbox_active = NULL;
14379 		if (bf_get(lpfc_trailer_consumed, mcqe))
14380 			lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14381 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14382 
14383 		/* Post the next mbox command, if there is one */
14384 		lpfc_sli4_post_async_mbox(phba);
14385 
14386 		/* Process cmpl now */
14387 		if (pmb->mbox_cmpl)
14388 			pmb->mbox_cmpl(phba, pmb);
14389 		return false;
14390 	}
14391 
14392 	/* There is mailbox completion work to queue to the worker thread */
14393 	spin_lock_irqsave(&phba->hbalock, iflags);
14394 	__lpfc_mbox_cmpl_put(phba, pmb);
14395 	phba->work_ha |= HA_MBATT;
14396 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14397 	workposted = true;
14398 
14399 send_current_mbox:
14400 	spin_lock_irqsave(&phba->hbalock, iflags);
14401 	/* Release the mailbox command posting token */
14402 	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14403 	/* Setting active mailbox pointer need to be in sync to flag clear */
14404 	phba->sli.mbox_active = NULL;
14405 	if (bf_get(lpfc_trailer_consumed, mcqe))
14406 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14407 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14408 	/* Wake up worker thread to post the next pending mailbox command */
14409 	lpfc_worker_wake_up(phba);
14410 	return workposted;
14411 
14412 out_no_mqe_complete:
14413 	spin_lock_irqsave(&phba->hbalock, iflags);
14414 	if (bf_get(lpfc_trailer_consumed, mcqe))
14415 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14416 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14417 	return false;
14418 }
14419 
14420 /**
14421  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14422  * @phba: Pointer to HBA context object.
14423  * @cq: Pointer to associated CQ
14424  * @cqe: Pointer to mailbox completion queue entry.
14425  *
14426  * This routine process a mailbox completion queue entry, it invokes the
14427  * proper mailbox complete handling or asynchronous event handling routine
14428  * according to the MCQE's async bit.
14429  *
14430  * Return: true if work posted to worker thread, otherwise false.
14431  **/
14432 static bool
lpfc_sli4_sp_handle_mcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14433 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14434 			 struct lpfc_cqe *cqe)
14435 {
14436 	struct lpfc_mcqe mcqe;
14437 	bool workposted;
14438 
14439 	cq->CQ_mbox++;
14440 
14441 	/* Copy the mailbox MCQE and convert endian order as needed */
14442 	lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
14443 
14444 	/* Invoke the proper event handling routine */
14445 	if (!bf_get(lpfc_trailer_async, &mcqe))
14446 		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14447 	else
14448 		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14449 	return workposted;
14450 }
14451 
14452 /**
14453  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14454  * @phba: Pointer to HBA context object.
14455  * @cq: Pointer to associated CQ
14456  * @wcqe: Pointer to work-queue completion queue entry.
14457  *
14458  * This routine handles an ELS work-queue completion event.
14459  *
14460  * Return: true if work posted to worker thread, otherwise false.
14461  **/
14462 static bool
lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)14463 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14464 			     struct lpfc_wcqe_complete *wcqe)
14465 {
14466 	struct lpfc_iocbq *irspiocbq;
14467 	unsigned long iflags;
14468 	struct lpfc_sli_ring *pring = cq->pring;
14469 	int txq_cnt = 0;
14470 	int txcmplq_cnt = 0;
14471 
14472 	/* Check for response status */
14473 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14474 		/* Log the error status */
14475 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14476 				"0357 ELS CQE error: status=x%x: "
14477 				"CQE: %08x %08x %08x %08x\n",
14478 				bf_get(lpfc_wcqe_c_status, wcqe),
14479 				wcqe->word0, wcqe->total_data_placed,
14480 				wcqe->parameter, wcqe->word3);
14481 	}
14482 
14483 	/* Get an irspiocbq for later ELS response processing use */
14484 	irspiocbq = lpfc_sli_get_iocbq(phba);
14485 	if (!irspiocbq) {
14486 		if (!list_empty(&pring->txq))
14487 			txq_cnt++;
14488 		if (!list_empty(&pring->txcmplq))
14489 			txcmplq_cnt++;
14490 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14491 			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
14492 			"els_txcmplq_cnt=%d\n",
14493 			txq_cnt, phba->iocb_cnt,
14494 			txcmplq_cnt);
14495 		return false;
14496 	}
14497 
14498 	/* Save off the slow-path queue event for work thread to process */
14499 	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
14500 	spin_lock_irqsave(&phba->hbalock, iflags);
14501 	list_add_tail(&irspiocbq->cq_event.list,
14502 		      &phba->sli4_hba.sp_queue_event);
14503 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14504 	set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14505 
14506 	return true;
14507 }
14508 
14509 /**
14510  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14511  * @phba: Pointer to HBA context object.
14512  * @wcqe: Pointer to work-queue completion queue entry.
14513  *
14514  * This routine handles slow-path WQ entry consumed event by invoking the
14515  * proper WQ release routine to the slow-path WQ.
14516  **/
14517 static void
lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_wcqe_release * wcqe)14518 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14519 			     struct lpfc_wcqe_release *wcqe)
14520 {
14521 	/* sanity check on queue memory */
14522 	if (unlikely(!phba->sli4_hba.els_wq))
14523 		return;
14524 	/* Check for the slow-path ELS work queue */
14525 	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14526 		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14527 				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14528 	else
14529 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14530 				"2579 Slow-path wqe consume event carries "
14531 				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14532 				bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14533 				phba->sli4_hba.els_wq->queue_id);
14534 }
14535 
14536 /**
14537  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14538  * @phba: Pointer to HBA context object.
14539  * @cq: Pointer to a WQ completion queue.
14540  * @wcqe: Pointer to work-queue completion queue entry.
14541  *
14542  * This routine handles an XRI abort event.
14543  *
14544  * Return: true if work posted to worker thread, otherwise false.
14545  **/
14546 static bool
lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct sli4_wcqe_xri_aborted * wcqe)14547 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14548 				   struct lpfc_queue *cq,
14549 				   struct sli4_wcqe_xri_aborted *wcqe)
14550 {
14551 	bool workposted = false;
14552 	struct lpfc_cq_event *cq_event;
14553 	unsigned long iflags;
14554 
14555 	switch (cq->subtype) {
14556 	case LPFC_IO:
14557 		lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14558 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14559 			/* Notify aborted XRI for NVME work queue */
14560 			if (phba->nvmet_support)
14561 				lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14562 		}
14563 		workposted = false;
14564 		break;
14565 	case LPFC_NVME_LS: /* NVME LS uses ELS resources */
14566 	case LPFC_ELS:
14567 		cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14568 		if (!cq_event) {
14569 			workposted = false;
14570 			break;
14571 		}
14572 		cq_event->hdwq = cq->hdwq;
14573 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14574 				  iflags);
14575 		list_add_tail(&cq_event->list,
14576 			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14577 		/* Set the els xri abort event flag */
14578 		set_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14579 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14580 				       iflags);
14581 		workposted = true;
14582 		break;
14583 	default:
14584 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14585 				"0603 Invalid CQ subtype %d: "
14586 				"%08x %08x %08x %08x\n",
14587 				cq->subtype, wcqe->word0, wcqe->parameter,
14588 				wcqe->word2, wcqe->word3);
14589 		workposted = false;
14590 		break;
14591 	}
14592 	return workposted;
14593 }
14594 
14595 #define FC_RCTL_MDS_DIAGS	0xF4
14596 
14597 /**
14598  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
14599  * @phba: Pointer to HBA context object.
14600  * @rcqe: Pointer to receive-queue completion queue entry.
14601  *
14602  * This routine process a receive-queue completion queue entry.
14603  *
14604  * Return: true if work posted to worker thread, otherwise false.
14605  **/
14606 static bool
lpfc_sli4_sp_handle_rcqe(struct lpfc_hba * phba,struct lpfc_rcqe * rcqe)14607 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14608 {
14609 	bool workposted = false;
14610 	struct fc_frame_header *fc_hdr;
14611 	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14612 	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
14613 	struct lpfc_nvmet_tgtport *tgtp;
14614 	struct hbq_dmabuf *dma_buf;
14615 	uint32_t status, rq_id;
14616 	unsigned long iflags;
14617 
14618 	/* sanity check on queue memory */
14619 	if (unlikely(!hrq) || unlikely(!drq))
14620 		return workposted;
14621 
14622 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14623 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14624 	else
14625 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14626 	if (rq_id != hrq->queue_id)
14627 		goto out;
14628 
14629 	status = bf_get(lpfc_rcqe_status, rcqe);
14630 	switch (status) {
14631 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14632 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14633 				"2537 Receive Frame Truncated!!\n");
14634 		fallthrough;
14635 	case FC_STATUS_RQ_SUCCESS:
14636 		spin_lock_irqsave(&phba->hbalock, iflags);
14637 		lpfc_sli4_rq_release(hrq, drq);
14638 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14639 		if (!dma_buf) {
14640 			hrq->RQ_no_buf_found++;
14641 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14642 			goto out;
14643 		}
14644 		hrq->RQ_rcv_buf++;
14645 		hrq->RQ_buf_posted--;
14646 		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
14647 
14648 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14649 
14650 		if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14651 		    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14652 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14653 			/* Handle MDS Loopback frames */
14654 			if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
14655 				lpfc_sli4_handle_mds_loopback(phba->pport,
14656 							      dma_buf);
14657 			else
14658 				lpfc_in_buf_free(phba, &dma_buf->dbuf);
14659 			break;
14660 		}
14661 
14662 		/* save off the frame for the work thread to process */
14663 		list_add_tail(&dma_buf->cq_event.list,
14664 			      &phba->sli4_hba.sp_queue_event);
14665 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14666 		/* Frame received */
14667 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14668 		workposted = true;
14669 		break;
14670 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
14671 		if (phba->nvmet_support) {
14672 			tgtp = phba->targetport->private;
14673 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14674 					"6402 RQE Error x%x, posted %d err_cnt "
14675 					"%d: %x %x %x\n",
14676 					status, hrq->RQ_buf_posted,
14677 					hrq->RQ_no_posted_buf,
14678 					atomic_read(&tgtp->rcv_fcp_cmd_in),
14679 					atomic_read(&tgtp->rcv_fcp_cmd_out),
14680 					atomic_read(&tgtp->xmt_fcp_release));
14681 		}
14682 		fallthrough;
14683 
14684 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
14685 		hrq->RQ_no_posted_buf++;
14686 		/* Post more buffers if possible */
14687 		set_bit(HBA_POST_RECEIVE_BUFFER, &phba->hba_flag);
14688 		workposted = true;
14689 		break;
14690 	case FC_STATUS_RQ_DMA_FAILURE:
14691 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14692 				"2564 RQE DMA Error x%x, x%08x x%08x x%08x "
14693 				"x%08x\n",
14694 				status, rcqe->word0, rcqe->word1,
14695 				rcqe->word2, rcqe->word3);
14696 
14697 		/* If IV set, no further recovery */
14698 		if (bf_get(lpfc_rcqe_iv, rcqe))
14699 			break;
14700 
14701 		/* recycle consumed resource */
14702 		spin_lock_irqsave(&phba->hbalock, iflags);
14703 		lpfc_sli4_rq_release(hrq, drq);
14704 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14705 		if (!dma_buf) {
14706 			hrq->RQ_no_buf_found++;
14707 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14708 			break;
14709 		}
14710 		hrq->RQ_rcv_buf++;
14711 		hrq->RQ_buf_posted--;
14712 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14713 		lpfc_in_buf_free(phba, &dma_buf->dbuf);
14714 		break;
14715 	default:
14716 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14717 				"2565 Unexpected RQE Status x%x, w0-3 x%08x "
14718 				"x%08x x%08x x%08x\n",
14719 				status, rcqe->word0, rcqe->word1,
14720 				rcqe->word2, rcqe->word3);
14721 		break;
14722 	}
14723 out:
14724 	return workposted;
14725 }
14726 
14727 /**
14728  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14729  * @phba: Pointer to HBA context object.
14730  * @cq: Pointer to the completion queue.
14731  * @cqe: Pointer to a completion queue entry.
14732  *
14733  * This routine process a slow-path work-queue or receive queue completion queue
14734  * entry.
14735  *
14736  * Return: true if work posted to worker thread, otherwise false.
14737  **/
14738 static bool
lpfc_sli4_sp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14739 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14740 			 struct lpfc_cqe *cqe)
14741 {
14742 	struct lpfc_cqe cqevt;
14743 	bool workposted = false;
14744 
14745 	/* Copy the work queue CQE and convert endian order if needed */
14746 	lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
14747 
14748 	/* Check and process for different type of WCQE and dispatch */
14749 	switch (bf_get(lpfc_cqe_code, &cqevt)) {
14750 	case CQE_CODE_COMPL_WQE:
14751 		/* Process the WQ/RQ complete event */
14752 		phba->last_completion_time = jiffies;
14753 		workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
14754 				(struct lpfc_wcqe_complete *)&cqevt);
14755 		break;
14756 	case CQE_CODE_RELEASE_WQE:
14757 		/* Process the WQ release event */
14758 		lpfc_sli4_sp_handle_rel_wcqe(phba,
14759 				(struct lpfc_wcqe_release *)&cqevt);
14760 		break;
14761 	case CQE_CODE_XRI_ABORTED:
14762 		/* Process the WQ XRI abort event */
14763 		phba->last_completion_time = jiffies;
14764 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14765 				(struct sli4_wcqe_xri_aborted *)&cqevt);
14766 		break;
14767 	case CQE_CODE_RECEIVE:
14768 	case CQE_CODE_RECEIVE_V1:
14769 		/* Process the RQ event */
14770 		phba->last_completion_time = jiffies;
14771 		workposted = lpfc_sli4_sp_handle_rcqe(phba,
14772 				(struct lpfc_rcqe *)&cqevt);
14773 		break;
14774 	default:
14775 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14776 				"0388 Not a valid WCQE code: x%x\n",
14777 				bf_get(lpfc_cqe_code, &cqevt));
14778 		break;
14779 	}
14780 	return workposted;
14781 }
14782 
14783 /**
14784  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14785  * @phba: Pointer to HBA context object.
14786  * @eqe: Pointer to fast-path event queue entry.
14787  * @speq: Pointer to slow-path event queue.
14788  *
14789  * This routine process a event queue entry from the slow-path event queue.
14790  * It will check the MajorCode and MinorCode to determine this is for a
14791  * completion event on a completion queue, if not, an error shall be logged
14792  * and just return. Otherwise, it will get to the corresponding completion
14793  * queue and process all the entries on that completion queue, rearm the
14794  * completion queue, and then return.
14795  *
14796  **/
14797 static void
lpfc_sli4_sp_handle_eqe(struct lpfc_hba * phba,struct lpfc_eqe * eqe,struct lpfc_queue * speq)14798 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14799 	struct lpfc_queue *speq)
14800 {
14801 	struct lpfc_queue *cq = NULL, *childq;
14802 	uint16_t cqid;
14803 	int ret = 0;
14804 
14805 	/* Get the reference to the corresponding CQ */
14806 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14807 
14808 	list_for_each_entry(childq, &speq->child_list, list) {
14809 		if (childq->queue_id == cqid) {
14810 			cq = childq;
14811 			break;
14812 		}
14813 	}
14814 	if (unlikely(!cq)) {
14815 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14816 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14817 					"0365 Slow-path CQ identifier "
14818 					"(%d) does not exist\n", cqid);
14819 		return;
14820 	}
14821 
14822 	/* Save EQ associated with this CQ */
14823 	cq->assoc_qp = speq;
14824 
14825 	if (is_kdump_kernel())
14826 		ret = queue_work(phba->wq, &cq->spwork);
14827 	else
14828 		ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14829 
14830 	if (!ret)
14831 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14832 				"0390 Cannot schedule queue work "
14833 				"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14834 				cqid, cq->queue_id, raw_smp_processor_id());
14835 }
14836 
14837 /**
14838  * __lpfc_sli4_process_cq - Process elements of a CQ
14839  * @phba: Pointer to HBA context object.
14840  * @cq: Pointer to CQ to be processed
14841  * @handler: Routine to process each cqe
14842  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14843  *
14844  * This routine processes completion queue entries in a CQ. While a valid
14845  * queue element is found, the handler is called. During processing checks
14846  * are made for periodic doorbell writes to let the hardware know of
14847  * element consumption.
14848  *
14849  * If the max limit on cqes to process is hit, or there are no more valid
14850  * entries, the loop stops. If we processed a sufficient number of elements,
14851  * meaning there is sufficient load, rather than rearming and generating
14852  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14853  * indicates no rescheduling.
14854  *
14855  * Returns True if work scheduled, False otherwise.
14856  **/
14857 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)14858 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14859 	bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14860 			struct lpfc_cqe *), unsigned long *delay)
14861 {
14862 	struct lpfc_cqe *cqe;
14863 	bool workposted = false;
14864 	int count = 0, consumed = 0;
14865 	bool arm = true;
14866 
14867 	/* default - no reschedule */
14868 	*delay = 0;
14869 
14870 	if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14871 		goto rearm_and_exit;
14872 
14873 	/* Process all the entries to the CQ */
14874 	cq->q_flag = 0;
14875 	cqe = lpfc_sli4_cq_get(cq);
14876 	while (cqe) {
14877 		workposted |= handler(phba, cq, cqe);
14878 		__lpfc_sli4_consume_cqe(phba, cq, cqe);
14879 
14880 		consumed++;
14881 		if (!(++count % cq->max_proc_limit))
14882 			break;
14883 
14884 		if (!(count % cq->notify_interval)) {
14885 			phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14886 						LPFC_QUEUE_NOARM);
14887 			consumed = 0;
14888 			cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14889 		}
14890 
14891 		if (count == LPFC_NVMET_CQ_NOTIFY)
14892 			cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14893 
14894 		cqe = lpfc_sli4_cq_get(cq);
14895 	}
14896 	if (count >= phba->cfg_cq_poll_threshold) {
14897 		*delay = 1;
14898 		arm = false;
14899 	}
14900 
14901 	/* Track the max number of CQEs processed in 1 EQ */
14902 	if (count > cq->CQ_max_cqe)
14903 		cq->CQ_max_cqe = count;
14904 
14905 	cq->assoc_qp->EQ_cqe_cnt += count;
14906 
14907 	/* Catch the no cq entry condition */
14908 	if (unlikely(count == 0))
14909 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14910 				"0369 No entry from completion queue "
14911 				"qid=%d\n", cq->queue_id);
14912 
14913 	xchg(&cq->queue_claimed, 0);
14914 
14915 rearm_and_exit:
14916 	phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14917 			arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14918 
14919 	return workposted;
14920 }
14921 
14922 /**
14923  * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14924  * @cq: pointer to CQ to process
14925  *
14926  * This routine calls the cq processing routine with a handler specific
14927  * to the type of queue bound to it.
14928  *
14929  * The CQ routine returns two values: the first is the calling status,
14930  * which indicates whether work was queued to the  background discovery
14931  * thread. If true, the routine should wakeup the discovery thread;
14932  * the second is the delay parameter. If non-zero, rather than rearming
14933  * the CQ and yet another interrupt, the CQ handler should be queued so
14934  * that it is processed in a subsequent polling action. The value of
14935  * the delay indicates when to reschedule it.
14936  **/
14937 static void
__lpfc_sli4_sp_process_cq(struct lpfc_queue * cq)14938 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14939 {
14940 	struct lpfc_hba *phba = cq->phba;
14941 	unsigned long delay;
14942 	bool workposted = false;
14943 	int ret = 0;
14944 
14945 	/* Process and rearm the CQ */
14946 	switch (cq->type) {
14947 	case LPFC_MCQ:
14948 		workposted |= __lpfc_sli4_process_cq(phba, cq,
14949 						lpfc_sli4_sp_handle_mcqe,
14950 						&delay);
14951 		break;
14952 	case LPFC_WCQ:
14953 		if (cq->subtype == LPFC_IO)
14954 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14955 						lpfc_sli4_fp_handle_cqe,
14956 						&delay);
14957 		else
14958 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14959 						lpfc_sli4_sp_handle_cqe,
14960 						&delay);
14961 		break;
14962 	default:
14963 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14964 				"0370 Invalid completion queue type (%d)\n",
14965 				cq->type);
14966 		return;
14967 	}
14968 
14969 	if (delay) {
14970 		if (is_kdump_kernel())
14971 			ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14972 						delay);
14973 		else
14974 			ret = queue_delayed_work_on(cq->chann, phba->wq,
14975 						&cq->sched_spwork, delay);
14976 		if (!ret)
14977 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14978 				"0394 Cannot schedule queue work "
14979 				"for cqid=%d on CPU %d\n",
14980 				cq->queue_id, cq->chann);
14981 	}
14982 
14983 	/* wake up worker thread if there are works to be done */
14984 	if (workposted)
14985 		lpfc_worker_wake_up(phba);
14986 }
14987 
14988 /**
14989  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14990  *   interrupt
14991  * @work: pointer to work element
14992  *
14993  * translates from the work handler and calls the slow-path handler.
14994  **/
14995 static void
lpfc_sli4_sp_process_cq(struct work_struct * work)14996 lpfc_sli4_sp_process_cq(struct work_struct *work)
14997 {
14998 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14999 
15000 	__lpfc_sli4_sp_process_cq(cq);
15001 }
15002 
15003 /**
15004  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
15005  * @work: pointer to work element
15006  *
15007  * translates from the work handler and calls the slow-path handler.
15008  **/
15009 static void
lpfc_sli4_dly_sp_process_cq(struct work_struct * work)15010 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
15011 {
15012 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15013 					struct lpfc_queue, sched_spwork);
15014 
15015 	__lpfc_sli4_sp_process_cq(cq);
15016 }
15017 
15018 /**
15019  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
15020  * @phba: Pointer to HBA context object.
15021  * @cq: Pointer to associated CQ
15022  * @wcqe: Pointer to work-queue completion queue entry.
15023  *
15024  * This routine process a fast-path work queue completion entry from fast-path
15025  * event queue for FCP command response completion.
15026  **/
15027 static void
lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)15028 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15029 			     struct lpfc_wcqe_complete *wcqe)
15030 {
15031 	struct lpfc_sli_ring *pring = cq->pring;
15032 	struct lpfc_iocbq *cmdiocbq;
15033 	unsigned long iflags;
15034 
15035 	/* Check for response status */
15036 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15037 		/* If resource errors reported from HBA, reduce queue
15038 		 * depth of the SCSI device.
15039 		 */
15040 		if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15041 		     IOSTAT_LOCAL_REJECT)) &&
15042 		    ((wcqe->parameter & IOERR_PARAM_MASK) ==
15043 		     IOERR_NO_RESOURCES))
15044 			phba->lpfc_rampdown_queue_depth(phba);
15045 
15046 		/* Log the cmpl status */
15047 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15048 				"0373 FCP CQE cmpl: status=x%x: "
15049 				"CQE: %08x %08x %08x %08x\n",
15050 				bf_get(lpfc_wcqe_c_status, wcqe),
15051 				wcqe->word0, wcqe->total_data_placed,
15052 				wcqe->parameter, wcqe->word3);
15053 	}
15054 
15055 	/* Look up the FCP command IOCB and create pseudo response IOCB */
15056 	spin_lock_irqsave(&pring->ring_lock, iflags);
15057 	pring->stats.iocb_event++;
15058 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15059 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15060 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
15061 	if (unlikely(!cmdiocbq)) {
15062 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15063 				"0374 FCP complete with no corresponding "
15064 				"cmdiocb: iotag (%d)\n",
15065 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15066 		return;
15067 	}
15068 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15069 	cmdiocbq->isr_timestamp = cq->isr_timestamp;
15070 #endif
15071 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
15072 		spin_lock_irqsave(&phba->hbalock, iflags);
15073 		cmdiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
15074 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15075 	}
15076 
15077 	if (cmdiocbq->cmd_cmpl) {
15078 		/* For FCP the flag is cleared in cmd_cmpl */
15079 		if (!(cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
15080 		    cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED) {
15081 			spin_lock_irqsave(&phba->hbalock, iflags);
15082 			cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
15083 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15084 		}
15085 
15086 		/* Pass the cmd_iocb and the wcqe to the upper layer */
15087 		memcpy(&cmdiocbq->wcqe_cmpl, wcqe,
15088 		       sizeof(struct lpfc_wcqe_complete));
15089 		cmdiocbq->cmd_cmpl(phba, cmdiocbq, cmdiocbq);
15090 	} else {
15091 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15092 				"0375 FCP cmdiocb not callback function "
15093 				"iotag: (%d)\n",
15094 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15095 	}
15096 }
15097 
15098 /**
15099  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15100  * @phba: Pointer to HBA context object.
15101  * @cq: Pointer to completion queue.
15102  * @wcqe: Pointer to work-queue completion queue entry.
15103  *
15104  * This routine handles an fast-path WQ entry consumed event by invoking the
15105  * proper WQ release routine to the slow-path WQ.
15106  **/
15107 static void
lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_release * wcqe)15108 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15109 			     struct lpfc_wcqe_release *wcqe)
15110 {
15111 	struct lpfc_queue *childwq;
15112 	bool wqid_matched = false;
15113 	uint16_t hba_wqid;
15114 
15115 	/* Check for fast-path FCP work queue release */
15116 	hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
15117 	list_for_each_entry(childwq, &cq->child_list, list) {
15118 		if (childwq->queue_id == hba_wqid) {
15119 			lpfc_sli4_wq_release(childwq,
15120 					bf_get(lpfc_wcqe_r_wqe_index, wcqe));
15121 			if (childwq->q_flag & HBA_NVMET_WQFULL)
15122 				lpfc_nvmet_wqfull_process(phba, childwq);
15123 			wqid_matched = true;
15124 			break;
15125 		}
15126 	}
15127 	/* Report warning log message if no match found */
15128 	if (wqid_matched != true)
15129 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15130 				"2580 Fast-path wqe consume event carries "
15131 				"miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
15132 }
15133 
15134 /**
15135  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15136  * @phba: Pointer to HBA context object.
15137  * @cq: Pointer to completion queue.
15138  * @rcqe: Pointer to receive-queue completion queue entry.
15139  *
15140  * This routine process a receive-queue completion queue entry.
15141  *
15142  * Return: true if work posted to worker thread, otherwise false.
15143  **/
15144 static bool
lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_rcqe * rcqe)15145 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15146 			    struct lpfc_rcqe *rcqe)
15147 {
15148 	bool workposted = false;
15149 	struct lpfc_queue *hrq;
15150 	struct lpfc_queue *drq;
15151 	struct rqb_dmabuf *dma_buf;
15152 	struct fc_frame_header *fc_hdr;
15153 	struct lpfc_nvmet_tgtport *tgtp;
15154 	uint32_t status, rq_id;
15155 	unsigned long iflags;
15156 	uint32_t fctl, idx;
15157 
15158 	if ((phba->nvmet_support == 0) ||
15159 	    (phba->sli4_hba.nvmet_cqset == NULL))
15160 		return workposted;
15161 
15162 	idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15163 	hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15164 	drq = phba->sli4_hba.nvmet_mrq_data[idx];
15165 
15166 	/* sanity check on queue memory */
15167 	if (unlikely(!hrq) || unlikely(!drq))
15168 		return workposted;
15169 
15170 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15171 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15172 	else
15173 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15174 
15175 	if ((phba->nvmet_support == 0) ||
15176 	    (rq_id != hrq->queue_id))
15177 		return workposted;
15178 
15179 	status = bf_get(lpfc_rcqe_status, rcqe);
15180 	switch (status) {
15181 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
15182 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15183 				"6126 Receive Frame Truncated!!\n");
15184 		fallthrough;
15185 	case FC_STATUS_RQ_SUCCESS:
15186 		spin_lock_irqsave(&phba->hbalock, iflags);
15187 		lpfc_sli4_rq_release(hrq, drq);
15188 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15189 		if (!dma_buf) {
15190 			hrq->RQ_no_buf_found++;
15191 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15192 			goto out;
15193 		}
15194 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15195 		hrq->RQ_rcv_buf++;
15196 		hrq->RQ_buf_posted--;
15197 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15198 
15199 		/* Just some basic sanity checks on FCP Command frame */
15200 		fctl = (fc_hdr->fh_f_ctl[0] << 16 |
15201 			fc_hdr->fh_f_ctl[1] << 8 |
15202 			fc_hdr->fh_f_ctl[2]);
15203 		if (((fctl &
15204 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15205 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15206 		    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15207 			goto drop;
15208 
15209 		if (fc_hdr->fh_type == FC_TYPE_FCP) {
15210 			dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
15211 			lpfc_nvmet_unsol_fcp_event(
15212 				phba, idx, dma_buf, cq->isr_timestamp,
15213 				cq->q_flag & HBA_NVMET_CQ_NOTIFY);
15214 			return false;
15215 		}
15216 drop:
15217 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15218 		break;
15219 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
15220 		if (phba->nvmet_support) {
15221 			tgtp = phba->targetport->private;
15222 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15223 					"6401 RQE Error x%x, posted %d err_cnt "
15224 					"%d: %x %x %x\n",
15225 					status, hrq->RQ_buf_posted,
15226 					hrq->RQ_no_posted_buf,
15227 					atomic_read(&tgtp->rcv_fcp_cmd_in),
15228 					atomic_read(&tgtp->rcv_fcp_cmd_out),
15229 					atomic_read(&tgtp->xmt_fcp_release));
15230 		}
15231 		fallthrough;
15232 
15233 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
15234 		hrq->RQ_no_posted_buf++;
15235 		/* Post more buffers if possible */
15236 		break;
15237 	case FC_STATUS_RQ_DMA_FAILURE:
15238 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15239 				"2575 RQE DMA Error x%x, x%08x x%08x x%08x "
15240 				"x%08x\n",
15241 				status, rcqe->word0, rcqe->word1,
15242 				rcqe->word2, rcqe->word3);
15243 
15244 		/* If IV set, no further recovery */
15245 		if (bf_get(lpfc_rcqe_iv, rcqe))
15246 			break;
15247 
15248 		/* recycle consumed resource */
15249 		spin_lock_irqsave(&phba->hbalock, iflags);
15250 		lpfc_sli4_rq_release(hrq, drq);
15251 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15252 		if (!dma_buf) {
15253 			hrq->RQ_no_buf_found++;
15254 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15255 			break;
15256 		}
15257 		hrq->RQ_rcv_buf++;
15258 		hrq->RQ_buf_posted--;
15259 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15260 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15261 		break;
15262 	default:
15263 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15264 				"2576 Unexpected RQE Status x%x, w0-3 x%08x "
15265 				"x%08x x%08x x%08x\n",
15266 				status, rcqe->word0, rcqe->word1,
15267 				rcqe->word2, rcqe->word3);
15268 		break;
15269 	}
15270 out:
15271 	return workposted;
15272 }
15273 
15274 /**
15275  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
15276  * @phba: adapter with cq
15277  * @cq: Pointer to the completion queue.
15278  * @cqe: Pointer to fast-path completion queue entry.
15279  *
15280  * This routine process a fast-path work queue completion entry from fast-path
15281  * event queue for FCP command response completion.
15282  *
15283  * Return: true if work posted to worker thread, otherwise false.
15284  **/
15285 static bool
lpfc_sli4_fp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)15286 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15287 			 struct lpfc_cqe *cqe)
15288 {
15289 	struct lpfc_wcqe_release wcqe;
15290 	bool workposted = false;
15291 
15292 	/* Copy the work queue CQE and convert endian order if needed */
15293 	lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
15294 
15295 	/* Check and process for different type of WCQE and dispatch */
15296 	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15297 	case CQE_CODE_COMPL_WQE:
15298 	case CQE_CODE_NVME_ERSP:
15299 		cq->CQ_wq++;
15300 		/* Process the WQ complete event */
15301 		phba->last_completion_time = jiffies;
15302 		if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
15303 			lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
15304 				(struct lpfc_wcqe_complete *)&wcqe);
15305 		break;
15306 	case CQE_CODE_RELEASE_WQE:
15307 		cq->CQ_release_wqe++;
15308 		/* Process the WQ release event */
15309 		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15310 				(struct lpfc_wcqe_release *)&wcqe);
15311 		break;
15312 	case CQE_CODE_XRI_ABORTED:
15313 		cq->CQ_xri_aborted++;
15314 		/* Process the WQ XRI abort event */
15315 		phba->last_completion_time = jiffies;
15316 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15317 				(struct sli4_wcqe_xri_aborted *)&wcqe);
15318 		break;
15319 	case CQE_CODE_RECEIVE_V1:
15320 	case CQE_CODE_RECEIVE:
15321 		phba->last_completion_time = jiffies;
15322 		if (cq->subtype == LPFC_NVMET) {
15323 			workposted = lpfc_sli4_nvmet_handle_rcqe(
15324 				phba, cq, (struct lpfc_rcqe *)&wcqe);
15325 		}
15326 		break;
15327 	default:
15328 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15329 				"0144 Not a valid CQE code: x%x\n",
15330 				bf_get(lpfc_wcqe_c_code, &wcqe));
15331 		break;
15332 	}
15333 	return workposted;
15334 }
15335 
15336 /**
15337  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15338  * @cq: Pointer to CQ to be processed
15339  *
15340  * This routine calls the cq processing routine with the handler for
15341  * fast path CQEs.
15342  *
15343  * The CQ routine returns two values: the first is the calling status,
15344  * which indicates whether work was queued to the  background discovery
15345  * thread. If true, the routine should wakeup the discovery thread;
15346  * the second is the delay parameter. If non-zero, rather than rearming
15347  * the CQ and yet another interrupt, the CQ handler should be queued so
15348  * that it is processed in a subsequent polling action. The value of
15349  * the delay indicates when to reschedule it.
15350  **/
15351 static void
__lpfc_sli4_hba_process_cq(struct lpfc_queue * cq)15352 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
15353 {
15354 	struct lpfc_hba *phba = cq->phba;
15355 	unsigned long delay;
15356 	bool workposted = false;
15357 	int ret;
15358 
15359 	/* process and rearm the CQ */
15360 	workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
15361 					     &delay);
15362 
15363 	if (delay) {
15364 		if (is_kdump_kernel())
15365 			ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15366 						delay);
15367 		else
15368 			ret = queue_delayed_work_on(cq->chann, phba->wq,
15369 						&cq->sched_irqwork, delay);
15370 		if (!ret)
15371 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15372 					"0367 Cannot schedule queue work "
15373 					"for cqid=%d on CPU %d\n",
15374 					cq->queue_id, cq->chann);
15375 	}
15376 
15377 	/* wake up worker thread if there are works to be done */
15378 	if (workposted)
15379 		lpfc_worker_wake_up(phba);
15380 }
15381 
15382 /**
15383  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15384  *   interrupt
15385  * @work: pointer to work element
15386  *
15387  * translates from the work handler and calls the fast-path handler.
15388  **/
15389 static void
lpfc_sli4_hba_process_cq(struct work_struct * work)15390 lpfc_sli4_hba_process_cq(struct work_struct *work)
15391 {
15392 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
15393 
15394 	__lpfc_sli4_hba_process_cq(cq);
15395 }
15396 
15397 /**
15398  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
15399  * @phba: Pointer to HBA context object.
15400  * @eq: Pointer to the queue structure.
15401  * @eqe: Pointer to fast-path event queue entry.
15402  * @poll_mode: poll_mode to execute processing the cq.
15403  *
15404  * This routine process a event queue entry from the fast-path event queue.
15405  * It will check the MajorCode and MinorCode to determine this is for a
15406  * completion event on a completion queue, if not, an error shall be logged
15407  * and just return. Otherwise, it will get to the corresponding completion
15408  * queue and process all the entries on the completion queue, rearm the
15409  * completion queue, and then return.
15410  **/
15411 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)15412 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15413 			 struct lpfc_eqe *eqe, enum lpfc_poll_mode poll_mode)
15414 {
15415 	struct lpfc_queue *cq = NULL;
15416 	uint32_t qidx = eq->hdwq;
15417 	uint16_t cqid, id;
15418 	int ret;
15419 
15420 	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
15421 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15422 				"0366 Not a valid completion "
15423 				"event: majorcode=x%x, minorcode=x%x\n",
15424 				bf_get_le32(lpfc_eqe_major_code, eqe),
15425 				bf_get_le32(lpfc_eqe_minor_code, eqe));
15426 		return;
15427 	}
15428 
15429 	/* Get the reference to the corresponding CQ */
15430 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15431 
15432 	/* Use the fast lookup method first */
15433 	if (cqid <= phba->sli4_hba.cq_max) {
15434 		cq = phba->sli4_hba.cq_lookup[cqid];
15435 		if (cq)
15436 			goto  work_cq;
15437 	}
15438 
15439 	/* Next check for NVMET completion */
15440 	if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15441 		id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15442 		if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15443 			/* Process NVMET unsol rcv */
15444 			cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15445 			goto  process_cq;
15446 		}
15447 	}
15448 
15449 	if (phba->sli4_hba.nvmels_cq &&
15450 	    (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15451 		/* Process NVME unsol rcv */
15452 		cq = phba->sli4_hba.nvmels_cq;
15453 	}
15454 
15455 	/* Otherwise this is a Slow path event */
15456 	if (cq == NULL) {
15457 		lpfc_sli4_sp_handle_eqe(phba, eqe,
15458 					phba->sli4_hba.hdwq[qidx].hba_eq);
15459 		return;
15460 	}
15461 
15462 process_cq:
15463 	if (unlikely(cqid != cq->queue_id)) {
15464 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15465 				"0368 Miss-matched fast-path completion "
15466 				"queue identifier: eqcqid=%d, fcpcqid=%d\n",
15467 				cqid, cq->queue_id);
15468 		return;
15469 	}
15470 
15471 work_cq:
15472 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15473 	if (phba->ktime_on)
15474 		cq->isr_timestamp = ktime_get_ns();
15475 	else
15476 		cq->isr_timestamp = 0;
15477 #endif
15478 
15479 	switch (poll_mode) {
15480 	case LPFC_THREADED_IRQ:
15481 		__lpfc_sli4_hba_process_cq(cq);
15482 		break;
15483 	case LPFC_QUEUE_WORK:
15484 	default:
15485 		if (is_kdump_kernel())
15486 			ret = queue_work(phba->wq, &cq->irqwork);
15487 		else
15488 			ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15489 		if (!ret)
15490 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15491 					"0383 Cannot schedule queue work "
15492 					"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15493 					cqid, cq->queue_id,
15494 					raw_smp_processor_id());
15495 		break;
15496 	}
15497 }
15498 
15499 /**
15500  * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
15501  * @work: pointer to work element
15502  *
15503  * translates from the work handler and calls the fast-path handler.
15504  **/
15505 static void
lpfc_sli4_dly_hba_process_cq(struct work_struct * work)15506 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
15507 {
15508 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15509 					struct lpfc_queue, sched_irqwork);
15510 
15511 	__lpfc_sli4_hba_process_cq(cq);
15512 }
15513 
15514 /**
15515  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
15516  * @irq: Interrupt number.
15517  * @dev_id: The device context pointer.
15518  *
15519  * This function is directly called from the PCI layer as an interrupt
15520  * service routine when device with SLI-4 interface spec is enabled with
15521  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15522  * ring event in the HBA. However, when the device is enabled with either
15523  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15524  * device-level interrupt handler. When the PCI slot is in error recovery
15525  * or the HBA is undergoing initialization, the interrupt handler will not
15526  * process the interrupt. The SCSI FCP fast-path ring event are handled in
15527  * the intrrupt context. This function is called without any lock held.
15528  * It gets the hbalock to access and update SLI data structures. Note that,
15529  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15530  * equal to that of FCP CQ index.
15531  *
15532  * The link attention and ELS ring attention events are handled
15533  * by the worker thread. The interrupt handler signals the worker thread
15534  * and returns for these events. This function is called without any lock
15535  * held. It gets the hbalock to access and update SLI data structures.
15536  *
15537  * This function returns IRQ_HANDLED when interrupt is handled, IRQ_WAKE_THREAD
15538  * when interrupt is scheduled to be handled from a threaded irq context, or
15539  * else returns IRQ_NONE.
15540  **/
15541 irqreturn_t
lpfc_sli4_hba_intr_handler(int irq,void * dev_id)15542 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
15543 {
15544 	struct lpfc_hba *phba;
15545 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
15546 	struct lpfc_queue *fpeq;
15547 	unsigned long iflag;
15548 	int hba_eqidx;
15549 	int ecount = 0;
15550 	struct lpfc_eq_intr_info *eqi;
15551 
15552 	/* Get the driver's phba structure from the dev_id */
15553 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15554 	phba = hba_eq_hdl->phba;
15555 	hba_eqidx = hba_eq_hdl->idx;
15556 
15557 	if (unlikely(!phba))
15558 		return IRQ_NONE;
15559 	if (unlikely(!phba->sli4_hba.hdwq))
15560 		return IRQ_NONE;
15561 
15562 	/* Get to the EQ struct associated with this vector */
15563 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
15564 	if (unlikely(!fpeq))
15565 		return IRQ_NONE;
15566 
15567 	/* Check device state for handling interrupt */
15568 	if (unlikely(lpfc_intr_state_check(phba))) {
15569 		/* Check again for link_state with lock held */
15570 		spin_lock_irqsave(&phba->hbalock, iflag);
15571 		if (phba->link_state < LPFC_LINK_DOWN)
15572 			/* Flush, clear interrupt, and rearm the EQ */
15573 			lpfc_sli4_eqcq_flush(phba, fpeq);
15574 		spin_unlock_irqrestore(&phba->hbalock, iflag);
15575 		return IRQ_NONE;
15576 	}
15577 
15578 	switch (fpeq->poll_mode) {
15579 	case LPFC_THREADED_IRQ:
15580 		/* CGN mgmt is mutually exclusive from irq processing */
15581 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
15582 			return IRQ_WAKE_THREAD;
15583 		fallthrough;
15584 	case LPFC_QUEUE_WORK:
15585 	default:
15586 		eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15587 		eqi->icnt++;
15588 
15589 		fpeq->last_cpu = raw_smp_processor_id();
15590 
15591 		if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
15592 		    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
15593 		    phba->cfg_auto_imax &&
15594 		    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15595 		    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15596 			lpfc_sli4_mod_hba_eq_delay(phba, fpeq,
15597 						   LPFC_MAX_AUTO_EQ_DELAY);
15598 
15599 		/* process and rearm the EQ */
15600 		ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
15601 					      LPFC_QUEUE_WORK);
15602 
15603 		if (unlikely(ecount == 0)) {
15604 			fpeq->EQ_no_entry++;
15605 			if (phba->intr_type == MSIX)
15606 				/* MSI-X treated interrupt served as no EQ share INT */
15607 				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15608 						"0358 MSI-X interrupt with no EQE\n");
15609 			else
15610 				/* Non MSI-X treated on interrupt as EQ share INT */
15611 				return IRQ_NONE;
15612 		}
15613 	}
15614 
15615 	return IRQ_HANDLED;
15616 } /* lpfc_sli4_hba_intr_handler */
15617 
15618 /**
15619  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15620  * @irq: Interrupt number.
15621  * @dev_id: The device context pointer.
15622  *
15623  * This function is the device-level interrupt handler to device with SLI-4
15624  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15625  * interrupt mode is enabled and there is an event in the HBA which requires
15626  * driver attention. This function invokes the slow-path interrupt attention
15627  * handling function and fast-path interrupt attention handling function in
15628  * turn to process the relevant HBA attention events. This function is called
15629  * without any lock held. It gets the hbalock to access and update SLI data
15630  * structures.
15631  *
15632  * This function returns IRQ_HANDLED when interrupt is handled, else it
15633  * returns IRQ_NONE.
15634  **/
15635 irqreturn_t
lpfc_sli4_intr_handler(int irq,void * dev_id)15636 lpfc_sli4_intr_handler(int irq, void *dev_id)
15637 {
15638 	struct lpfc_hba  *phba;
15639 	irqreturn_t hba_irq_rc;
15640 	bool hba_handled = false;
15641 	int qidx;
15642 
15643 	/* Get the driver's phba structure from the dev_id */
15644 	phba = (struct lpfc_hba *)dev_id;
15645 
15646 	if (unlikely(!phba))
15647 		return IRQ_NONE;
15648 
15649 	/*
15650 	 * Invoke fast-path host attention interrupt handling as appropriate.
15651 	 */
15652 	for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
15653 		hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
15654 					&phba->sli4_hba.hba_eq_hdl[qidx]);
15655 		if (hba_irq_rc == IRQ_HANDLED)
15656 			hba_handled |= true;
15657 	}
15658 
15659 	return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
15660 } /* lpfc_sli4_intr_handler */
15661 
lpfc_sli4_poll_hbtimer(struct timer_list * t)15662 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15663 {
15664 	struct lpfc_hba *phba = timer_container_of(phba, t, cpuhp_poll_timer);
15665 	struct lpfc_queue *eq;
15666 
15667 	rcu_read_lock();
15668 
15669 	list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15670 		lpfc_sli4_poll_eq(eq);
15671 	if (!list_empty(&phba->poll_list))
15672 		mod_timer(&phba->cpuhp_poll_timer,
15673 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15674 
15675 	rcu_read_unlock();
15676 }
15677 
lpfc_sli4_add_to_poll_list(struct lpfc_queue * eq)15678 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15679 {
15680 	struct lpfc_hba *phba = eq->phba;
15681 
15682 	/* kickstart slowpath processing if needed */
15683 	if (list_empty(&phba->poll_list))
15684 		mod_timer(&phba->cpuhp_poll_timer,
15685 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15686 
15687 	list_add_rcu(&eq->_poll_list, &phba->poll_list);
15688 	synchronize_rcu();
15689 }
15690 
lpfc_sli4_remove_from_poll_list(struct lpfc_queue * eq)15691 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15692 {
15693 	struct lpfc_hba *phba = eq->phba;
15694 
15695 	/* Disable slowpath processing for this eq.  Kick start the eq
15696 	 * by RE-ARMING the eq's ASAP
15697 	 */
15698 	list_del_rcu(&eq->_poll_list);
15699 	synchronize_rcu();
15700 
15701 	if (list_empty(&phba->poll_list))
15702 		timer_delete_sync(&phba->cpuhp_poll_timer);
15703 }
15704 
lpfc_sli4_cleanup_poll_list(struct lpfc_hba * phba)15705 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
15706 {
15707 	struct lpfc_queue *eq, *next;
15708 
15709 	list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15710 		list_del(&eq->_poll_list);
15711 
15712 	INIT_LIST_HEAD(&phba->poll_list);
15713 	synchronize_rcu();
15714 }
15715 
15716 static inline void
__lpfc_sli4_switch_eqmode(struct lpfc_queue * eq,uint8_t mode)15717 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15718 {
15719 	if (mode == eq->mode)
15720 		return;
15721 	/*
15722 	 * currently this function is only called during a hotplug
15723 	 * event and the cpu on which this function is executing
15724 	 * is going offline.  By now the hotplug has instructed
15725 	 * the scheduler to remove this cpu from cpu active mask.
15726 	 * So we don't need to work about being put aside by the
15727 	 * scheduler for a high priority process.  Yes, the inte-
15728 	 * rrupts could come but they are known to retire ASAP.
15729 	 */
15730 
15731 	/* Disable polling in the fastpath */
15732 	WRITE_ONCE(eq->mode, mode);
15733 	/* flush out the store buffer */
15734 	smp_wmb();
15735 
15736 	/*
15737 	 * Add this eq to the polling list and start polling. For
15738 	 * a grace period both interrupt handler and poller will
15739 	 * try to process the eq _but_ that's fine.  We have a
15740 	 * synchronization mechanism in place (queue_claimed) to
15741 	 * deal with it.  This is just a draining phase for int-
15742 	 * errupt handler (not eq's) as we have guranteed through
15743 	 * barrier that all the CPUs have seen the new CQ_POLLED
15744 	 * state. which will effectively disable the REARMING of
15745 	 * the EQ.  The whole idea is eq's die off eventually as
15746 	 * we are not rearming EQ's anymore.
15747 	 */
15748 	mode ? lpfc_sli4_add_to_poll_list(eq) :
15749 	       lpfc_sli4_remove_from_poll_list(eq);
15750 }
15751 
lpfc_sli4_start_polling(struct lpfc_queue * eq)15752 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15753 {
15754 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15755 }
15756 
lpfc_sli4_stop_polling(struct lpfc_queue * eq)15757 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15758 {
15759 	struct lpfc_hba *phba = eq->phba;
15760 
15761 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15762 
15763 	/* Kick start for the pending io's in h/w.
15764 	 * Once we switch back to interrupt processing on a eq
15765 	 * the io path completion will only arm eq's when it
15766 	 * receives a completion.  But since eq's are in disa-
15767 	 * rmed state it doesn't receive a completion.  This
15768 	 * creates a deadlock scenaro.
15769 	 */
15770 	phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15771 }
15772 
15773 /**
15774  * lpfc_sli4_queue_free - free a queue structure and associated memory
15775  * @queue: The queue structure to free.
15776  *
15777  * This function frees a queue structure and the DMAable memory used for
15778  * the host resident queue. This function must be called after destroying the
15779  * queue on the HBA.
15780  **/
15781 void
lpfc_sli4_queue_free(struct lpfc_queue * queue)15782 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15783 {
15784 	struct lpfc_dmabuf *dmabuf;
15785 
15786 	if (!queue)
15787 		return;
15788 
15789 	if (!list_empty(&queue->wq_list))
15790 		list_del(&queue->wq_list);
15791 
15792 	while (!list_empty(&queue->page_list)) {
15793 		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15794 				 list);
15795 		dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15796 				  dmabuf->virt, dmabuf->phys);
15797 		kfree(dmabuf);
15798 	}
15799 	if (queue->rqbp) {
15800 		lpfc_free_rq_buffer(queue->phba, queue);
15801 		kfree(queue->rqbp);
15802 	}
15803 
15804 	if (!list_empty(&queue->cpu_list))
15805 		list_del(&queue->cpu_list);
15806 
15807 	kfree(queue);
15808 	return;
15809 }
15810 
15811 /**
15812  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15813  * @phba: The HBA that this queue is being created on.
15814  * @page_size: The size of a queue page
15815  * @entry_size: The size of each queue entry for this queue.
15816  * @entry_count: The number of entries that this queue will handle.
15817  * @cpu: The cpu that will primarily utilize this queue.
15818  *
15819  * This function allocates a queue structure and the DMAable memory used for
15820  * the host resident queue. This function must be called before creating the
15821  * queue on the HBA.
15822  **/
15823 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)15824 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15825 		      uint32_t entry_size, uint32_t entry_count, int cpu)
15826 {
15827 	struct lpfc_queue *queue;
15828 	struct lpfc_dmabuf *dmabuf;
15829 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15830 	uint16_t x, pgcnt;
15831 
15832 	if (!phba->sli4_hba.pc_sli4_params.supported)
15833 		hw_page_size = page_size;
15834 
15835 	pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15836 
15837 	/* If needed, Adjust page count to match the max the adapter supports */
15838 	if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15839 		pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15840 
15841 	queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15842 			     GFP_KERNEL, cpu_to_node(cpu));
15843 	if (!queue)
15844 		return NULL;
15845 
15846 	INIT_LIST_HEAD(&queue->list);
15847 	INIT_LIST_HEAD(&queue->_poll_list);
15848 	INIT_LIST_HEAD(&queue->wq_list);
15849 	INIT_LIST_HEAD(&queue->wqfull_list);
15850 	INIT_LIST_HEAD(&queue->page_list);
15851 	INIT_LIST_HEAD(&queue->child_list);
15852 	INIT_LIST_HEAD(&queue->cpu_list);
15853 
15854 	/* Set queue parameters now.  If the system cannot provide memory
15855 	 * resources, the free routine needs to know what was allocated.
15856 	 */
15857 	queue->page_count = pgcnt;
15858 	queue->q_pgs = (void **)&queue[1];
15859 	queue->entry_cnt_per_pg = hw_page_size / entry_size;
15860 	queue->entry_size = entry_size;
15861 	queue->entry_count = entry_count;
15862 	queue->page_size = hw_page_size;
15863 	queue->phba = phba;
15864 
15865 	for (x = 0; x < queue->page_count; x++) {
15866 		dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15867 				      dev_to_node(&phba->pcidev->dev));
15868 		if (!dmabuf)
15869 			goto out_fail;
15870 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15871 						  hw_page_size, &dmabuf->phys,
15872 						  GFP_KERNEL);
15873 		if (!dmabuf->virt) {
15874 			kfree(dmabuf);
15875 			goto out_fail;
15876 		}
15877 		dmabuf->buffer_tag = x;
15878 		list_add_tail(&dmabuf->list, &queue->page_list);
15879 		/* use lpfc_sli4_qe to index a paritcular entry in this page */
15880 		queue->q_pgs[x] = dmabuf->virt;
15881 	}
15882 	INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15883 	INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15884 	INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15885 	INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15886 
15887 	/* notify_interval will be set during q creation */
15888 
15889 	return queue;
15890 out_fail:
15891 	lpfc_sli4_queue_free(queue);
15892 	return NULL;
15893 }
15894 
15895 /**
15896  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15897  * @phba: HBA structure that indicates port to create a queue on.
15898  * @pci_barset: PCI BAR set flag.
15899  *
15900  * This function shall perform iomap of the specified PCI BAR address to host
15901  * memory address if not already done so and return it. The returned host
15902  * memory address can be NULL.
15903  */
15904 static void __iomem *
lpfc_dual_chute_pci_bar_map(struct lpfc_hba * phba,uint16_t pci_barset)15905 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15906 {
15907 	if (!phba->pcidev)
15908 		return NULL;
15909 
15910 	switch (pci_barset) {
15911 	case WQ_PCI_BAR_0_AND_1:
15912 		return phba->pci_bar0_memmap_p;
15913 	case WQ_PCI_BAR_2_AND_3:
15914 		return phba->pci_bar2_memmap_p;
15915 	case WQ_PCI_BAR_4_AND_5:
15916 		return phba->pci_bar4_memmap_p;
15917 	default:
15918 		break;
15919 	}
15920 	return NULL;
15921 }
15922 
15923 /**
15924  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15925  * @phba: HBA structure that EQs are on.
15926  * @startq: The starting EQ index to modify
15927  * @numq: The number of EQs (consecutive indexes) to modify
15928  * @usdelay: amount of delay
15929  *
15930  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15931  * is set either by writing to a register (if supported by the SLI Port)
15932  * or by mailbox command. The mailbox command allows several EQs to be
15933  * updated at once.
15934  *
15935  * The @phba struct is used to send a mailbox command to HBA. The @startq
15936  * is used to get the starting EQ index to change. The @numq value is
15937  * used to specify how many consecutive EQ indexes, starting at EQ index,
15938  * are to be changed. This function is asynchronous and will wait for any
15939  * mailbox commands to finish before returning.
15940  *
15941  * On success this function will return a zero. If unable to allocate
15942  * enough memory this function will return -ENOMEM. If a mailbox command
15943  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15944  * have had their delay multipler changed.
15945  **/
15946 void
lpfc_modify_hba_eq_delay(struct lpfc_hba * phba,uint32_t startq,uint32_t numq,uint32_t usdelay)15947 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
15948 			 uint32_t numq, uint32_t usdelay)
15949 {
15950 	struct lpfc_mbx_modify_eq_delay *eq_delay;
15951 	LPFC_MBOXQ_t *mbox;
15952 	struct lpfc_queue *eq;
15953 	int cnt = 0, rc, length;
15954 	uint32_t shdr_status, shdr_add_status;
15955 	uint32_t dmult;
15956 	int qidx;
15957 	union lpfc_sli4_cfg_shdr *shdr;
15958 
15959 	if (startq >= phba->cfg_irq_chann)
15960 		return;
15961 
15962 	if (usdelay > 0xFFFF) {
15963 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15964 				"6429 usdelay %d too large. Scaled down to "
15965 				"0xFFFF.\n", usdelay);
15966 		usdelay = 0xFFFF;
15967 	}
15968 
15969 	/* set values by EQ_DELAY register if supported */
15970 	if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15971 		for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15972 			eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15973 			if (!eq)
15974 				continue;
15975 
15976 			lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
15977 
15978 			if (++cnt >= numq)
15979 				break;
15980 		}
15981 		return;
15982 	}
15983 
15984 	/* Otherwise, set values by mailbox cmd */
15985 
15986 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15987 	if (!mbox) {
15988 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15989 				"6428 Failed allocating mailbox cmd buffer."
15990 				" EQ delay was not set.\n");
15991 		return;
15992 	}
15993 	length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15994 		  sizeof(struct lpfc_sli4_cfg_mhdr));
15995 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15996 			 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15997 			 length, LPFC_SLI4_MBX_EMBED);
15998 	eq_delay = &mbox->u.mqe.un.eq_delay;
15999 
16000 	/* Calculate delay multiper from maximum interrupt per second */
16001 	dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
16002 	if (dmult)
16003 		dmult--;
16004 	if (dmult > LPFC_DMULT_MAX)
16005 		dmult = LPFC_DMULT_MAX;
16006 
16007 	for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16008 		eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16009 		if (!eq)
16010 			continue;
16011 		eq->q_mode = usdelay;
16012 		eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16013 		eq_delay->u.request.eq[cnt].phase = 0;
16014 		eq_delay->u.request.eq[cnt].delay_multi = dmult;
16015 
16016 		if (++cnt >= numq)
16017 			break;
16018 	}
16019 	eq_delay->u.request.num_eq = cnt;
16020 
16021 	mbox->vport = phba->pport;
16022 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16023 	mbox->ctx_ndlp = NULL;
16024 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16025 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16026 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16027 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16028 	if (shdr_status || shdr_add_status || rc) {
16029 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16030 				"2512 MODIFY_EQ_DELAY mailbox failed with "
16031 				"status x%x add_status x%x, mbx status x%x\n",
16032 				shdr_status, shdr_add_status, rc);
16033 	}
16034 	mempool_free(mbox, phba->mbox_mem_pool);
16035 	return;
16036 }
16037 
16038 /**
16039  * lpfc_eq_create - Create an Event Queue on the HBA
16040  * @phba: HBA structure that indicates port to create a queue on.
16041  * @eq: The queue structure to use to create the event queue.
16042  * @imax: The maximum interrupt per second limit.
16043  *
16044  * This function creates an event queue, as detailed in @eq, on a port,
16045  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16046  *
16047  * The @phba struct is used to send mailbox command to HBA. The @eq struct
16048  * is used to get the entry count and entry size that are necessary to
16049  * determine the number of pages to allocate and use for this queue. This
16050  * function will send the EQ_CREATE mailbox command to the HBA to setup the
16051  * event queue. This function is asynchronous and will wait for the mailbox
16052  * command to finish before continuing.
16053  *
16054  * On success this function will return a zero. If unable to allocate enough
16055  * memory this function will return -ENOMEM. If the queue create mailbox command
16056  * fails this function will return -ENXIO.
16057  **/
16058 int
lpfc_eq_create(struct lpfc_hba * phba,struct lpfc_queue * eq,uint32_t imax)16059 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
16060 {
16061 	struct lpfc_mbx_eq_create *eq_create;
16062 	LPFC_MBOXQ_t *mbox;
16063 	int rc, length, status = 0;
16064 	struct lpfc_dmabuf *dmabuf;
16065 	uint32_t shdr_status, shdr_add_status;
16066 	union lpfc_sli4_cfg_shdr *shdr;
16067 	uint16_t dmult;
16068 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16069 
16070 	/* sanity check on queue memory */
16071 	if (!eq)
16072 		return -ENODEV;
16073 	if (!phba->sli4_hba.pc_sli4_params.supported)
16074 		hw_page_size = SLI4_PAGE_SIZE;
16075 
16076 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16077 	if (!mbox)
16078 		return -ENOMEM;
16079 	length = (sizeof(struct lpfc_mbx_eq_create) -
16080 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16081 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16082 			 LPFC_MBOX_OPCODE_EQ_CREATE,
16083 			 length, LPFC_SLI4_MBX_EMBED);
16084 	eq_create = &mbox->u.mqe.un.eq_create;
16085 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
16086 	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16087 	       eq->page_count);
16088 	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16089 	       LPFC_EQE_SIZE);
16090 	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
16091 
16092 	/* Use version 2 of CREATE_EQ if eqav is set */
16093 	if (phba->sli4_hba.pc_sli4_params.eqav) {
16094 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16095 		       LPFC_Q_CREATE_VERSION_2);
16096 		bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16097 		       phba->sli4_hba.pc_sli4_params.eqav);
16098 	}
16099 
16100 	/* don't setup delay multiplier using EQ_CREATE */
16101 	dmult = 0;
16102 	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16103 	       dmult);
16104 	switch (eq->entry_count) {
16105 	default:
16106 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16107 				"0360 Unsupported EQ count. (%d)\n",
16108 				eq->entry_count);
16109 		if (eq->entry_count < 256) {
16110 			status = -EINVAL;
16111 			goto out;
16112 		}
16113 		fallthrough;	/* otherwise default to smallest count */
16114 	case 256:
16115 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16116 		       LPFC_EQ_CNT_256);
16117 		break;
16118 	case 512:
16119 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16120 		       LPFC_EQ_CNT_512);
16121 		break;
16122 	case 1024:
16123 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16124 		       LPFC_EQ_CNT_1024);
16125 		break;
16126 	case 2048:
16127 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16128 		       LPFC_EQ_CNT_2048);
16129 		break;
16130 	case 4096:
16131 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16132 		       LPFC_EQ_CNT_4096);
16133 		break;
16134 	}
16135 	list_for_each_entry(dmabuf, &eq->page_list, list) {
16136 		memset(dmabuf->virt, 0, hw_page_size);
16137 		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16138 					putPaddrLow(dmabuf->phys);
16139 		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16140 					putPaddrHigh(dmabuf->phys);
16141 	}
16142 	mbox->vport = phba->pport;
16143 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16144 	mbox->ctx_buf = NULL;
16145 	mbox->ctx_ndlp = NULL;
16146 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16147 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16148 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16149 	if (shdr_status || shdr_add_status || rc) {
16150 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16151 				"2500 EQ_CREATE mailbox failed with "
16152 				"status x%x add_status x%x, mbx status x%x\n",
16153 				shdr_status, shdr_add_status, rc);
16154 		status = -ENXIO;
16155 	}
16156 	eq->type = LPFC_EQ;
16157 	eq->subtype = LPFC_NONE;
16158 	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16159 	if (eq->queue_id == 0xFFFF)
16160 		status = -ENXIO;
16161 	eq->host_index = 0;
16162 	eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16163 	eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
16164 out:
16165 	mempool_free(mbox, phba->mbox_mem_pool);
16166 	return status;
16167 }
16168 
16169 /**
16170  * lpfc_sli4_hba_intr_handler_th - SLI4 HBA threaded interrupt handler
16171  * @irq: Interrupt number.
16172  * @dev_id: The device context pointer.
16173  *
16174  * This routine is a mirror of lpfc_sli4_hba_intr_handler, but executed within
16175  * threaded irq context.
16176  *
16177  * Returns
16178  * IRQ_HANDLED - interrupt is handled
16179  * IRQ_NONE - otherwise
16180  **/
lpfc_sli4_hba_intr_handler_th(int irq,void * dev_id)16181 irqreturn_t lpfc_sli4_hba_intr_handler_th(int irq, void *dev_id)
16182 {
16183 	struct lpfc_hba *phba;
16184 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
16185 	struct lpfc_queue *fpeq;
16186 	int ecount = 0;
16187 	int hba_eqidx;
16188 	struct lpfc_eq_intr_info *eqi;
16189 
16190 	/* Get the driver's phba structure from the dev_id */
16191 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
16192 	phba = hba_eq_hdl->phba;
16193 	hba_eqidx = hba_eq_hdl->idx;
16194 
16195 	if (unlikely(!phba))
16196 		return IRQ_NONE;
16197 	if (unlikely(!phba->sli4_hba.hdwq))
16198 		return IRQ_NONE;
16199 
16200 	/* Get to the EQ struct associated with this vector */
16201 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
16202 	if (unlikely(!fpeq))
16203 		return IRQ_NONE;
16204 
16205 	eqi = per_cpu_ptr(phba->sli4_hba.eq_info, raw_smp_processor_id());
16206 	eqi->icnt++;
16207 
16208 	fpeq->last_cpu = raw_smp_processor_id();
16209 
16210 	if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
16211 	    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
16212 	    phba->cfg_auto_imax &&
16213 	    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
16214 	    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
16215 		lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
16216 
16217 	/* process and rearm the EQ */
16218 	ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
16219 				      LPFC_THREADED_IRQ);
16220 
16221 	if (unlikely(ecount == 0)) {
16222 		fpeq->EQ_no_entry++;
16223 		if (phba->intr_type == MSIX)
16224 			/* MSI-X treated interrupt served as no EQ share INT */
16225 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16226 					"3358 MSI-X interrupt with no EQE\n");
16227 		else
16228 			/* Non MSI-X treated on interrupt as EQ share INT */
16229 			return IRQ_NONE;
16230 	}
16231 	return IRQ_HANDLED;
16232 }
16233 
16234 /**
16235  * lpfc_cq_create - Create a Completion Queue on the HBA
16236  * @phba: HBA structure that indicates port to create a queue on.
16237  * @cq: The queue structure to use to create the completion queue.
16238  * @eq: The event queue to bind this completion queue to.
16239  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16240  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16241  *
16242  * This function creates a completion queue, as detailed in @wq, on a port,
16243  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16244  *
16245  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16246  * is used to get the entry count and entry size that are necessary to
16247  * determine the number of pages to allocate and use for this queue. The @eq
16248  * is used to indicate which event queue to bind this completion queue to. This
16249  * function will send the CQ_CREATE mailbox command to the HBA to setup the
16250  * completion queue. This function is asynchronous and will wait for the mailbox
16251  * command to finish before continuing.
16252  *
16253  * On success this function will return a zero. If unable to allocate enough
16254  * memory this function will return -ENOMEM. If the queue create mailbox command
16255  * fails this function will return -ENXIO.
16256  **/
16257 int
lpfc_cq_create(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_queue * eq,uint32_t type,uint32_t subtype)16258 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16259 	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16260 {
16261 	struct lpfc_mbx_cq_create *cq_create;
16262 	struct lpfc_dmabuf *dmabuf;
16263 	LPFC_MBOXQ_t *mbox;
16264 	int rc, length, status = 0;
16265 	uint32_t shdr_status, shdr_add_status;
16266 	union lpfc_sli4_cfg_shdr *shdr;
16267 
16268 	/* sanity check on queue memory */
16269 	if (!cq || !eq)
16270 		return -ENODEV;
16271 
16272 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16273 	if (!mbox)
16274 		return -ENOMEM;
16275 	length = (sizeof(struct lpfc_mbx_cq_create) -
16276 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16277 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16278 			 LPFC_MBOX_OPCODE_CQ_CREATE,
16279 			 length, LPFC_SLI4_MBX_EMBED);
16280 	cq_create = &mbox->u.mqe.un.cq_create;
16281 	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
16282 	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16283 		    cq->page_count);
16284 	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16285 	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
16286 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16287 	       phba->sli4_hba.pc_sli4_params.cqv);
16288 	if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
16289 		bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16290 		       (cq->page_size / SLI4_PAGE_SIZE));
16291 		bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16292 		       eq->queue_id);
16293 		bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16294 		       phba->sli4_hba.pc_sli4_params.cqav);
16295 	} else {
16296 		bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16297 		       eq->queue_id);
16298 	}
16299 	switch (cq->entry_count) {
16300 	case 2048:
16301 	case 4096:
16302 		if (phba->sli4_hba.pc_sli4_params.cqv ==
16303 		    LPFC_Q_CREATE_VERSION_2) {
16304 			cq_create->u.request.context.lpfc_cq_context_count =
16305 				cq->entry_count;
16306 			bf_set(lpfc_cq_context_count,
16307 			       &cq_create->u.request.context,
16308 			       LPFC_CQ_CNT_WORD7);
16309 			break;
16310 		}
16311 		fallthrough;
16312 	default:
16313 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16314 				"0361 Unsupported CQ count: "
16315 				"entry cnt %d sz %d pg cnt %d\n",
16316 				cq->entry_count, cq->entry_size,
16317 				cq->page_count);
16318 		if (cq->entry_count < 256) {
16319 			status = -EINVAL;
16320 			goto out;
16321 		}
16322 		fallthrough;	/* otherwise default to smallest count */
16323 	case 256:
16324 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16325 		       LPFC_CQ_CNT_256);
16326 		break;
16327 	case 512:
16328 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16329 		       LPFC_CQ_CNT_512);
16330 		break;
16331 	case 1024:
16332 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16333 		       LPFC_CQ_CNT_1024);
16334 		break;
16335 	}
16336 	list_for_each_entry(dmabuf, &cq->page_list, list) {
16337 		memset(dmabuf->virt, 0, cq->page_size);
16338 		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16339 					putPaddrLow(dmabuf->phys);
16340 		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16341 					putPaddrHigh(dmabuf->phys);
16342 	}
16343 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16344 
16345 	/* The IOCTL status is embedded in the mailbox subheader. */
16346 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16347 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16348 	if (shdr_status || shdr_add_status || rc) {
16349 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16350 				"2501 CQ_CREATE mailbox failed with "
16351 				"status x%x add_status x%x, mbx status x%x\n",
16352 				shdr_status, shdr_add_status, rc);
16353 		status = -ENXIO;
16354 		goto out;
16355 	}
16356 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16357 	if (cq->queue_id == 0xFFFF) {
16358 		status = -ENXIO;
16359 		goto out;
16360 	}
16361 	/* link the cq onto the parent eq child list */
16362 	list_add_tail(&cq->list, &eq->child_list);
16363 	/* Set up completion queue's type and subtype */
16364 	cq->type = type;
16365 	cq->subtype = subtype;
16366 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16367 	cq->assoc_qid = eq->queue_id;
16368 	cq->assoc_qp = eq;
16369 	cq->host_index = 0;
16370 	cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16371 	cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
16372 
16373 	if (cq->queue_id > phba->sli4_hba.cq_max)
16374 		phba->sli4_hba.cq_max = cq->queue_id;
16375 out:
16376 	mempool_free(mbox, phba->mbox_mem_pool);
16377 	return status;
16378 }
16379 
16380 /**
16381  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16382  * @phba: HBA structure that indicates port to create a queue on.
16383  * @cqp: The queue structure array to use to create the completion queues.
16384  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
16385  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16386  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16387  *
16388  * This function creates a set of  completion queue, s to support MRQ
16389  * as detailed in @cqp, on a port,
16390  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16391  *
16392  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16393  * is used to get the entry count and entry size that are necessary to
16394  * determine the number of pages to allocate and use for this queue. The @eq
16395  * is used to indicate which event queue to bind this completion queue to. This
16396  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16397  * completion queue. This function is asynchronous and will wait for the mailbox
16398  * command to finish before continuing.
16399  *
16400  * On success this function will return a zero. If unable to allocate enough
16401  * memory this function will return -ENOMEM. If the queue create mailbox command
16402  * fails this function will return -ENXIO.
16403  **/
16404 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)16405 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
16406 		   struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16407 		   uint32_t subtype)
16408 {
16409 	struct lpfc_queue *cq;
16410 	struct lpfc_queue *eq;
16411 	struct lpfc_mbx_cq_create_set *cq_set;
16412 	struct lpfc_dmabuf *dmabuf;
16413 	LPFC_MBOXQ_t *mbox;
16414 	int rc, length, alloclen, status = 0;
16415 	int cnt, idx, numcq, page_idx = 0;
16416 	uint32_t shdr_status, shdr_add_status;
16417 	union lpfc_sli4_cfg_shdr *shdr;
16418 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16419 
16420 	/* sanity check on queue memory */
16421 	numcq = phba->cfg_nvmet_mrq;
16422 	if (!cqp || !hdwq || !numcq)
16423 		return -ENODEV;
16424 
16425 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16426 	if (!mbox)
16427 		return -ENOMEM;
16428 
16429 	length = sizeof(struct lpfc_mbx_cq_create_set);
16430 	length += ((numcq * cqp[0]->page_count) *
16431 		   sizeof(struct dma_address));
16432 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16433 			LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16434 			LPFC_SLI4_MBX_NEMBED);
16435 	if (alloclen < length) {
16436 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16437 				"3098 Allocated DMA memory size (%d) is "
16438 				"less than the requested DMA memory size "
16439 				"(%d)\n", alloclen, length);
16440 		status = -ENOMEM;
16441 		goto out;
16442 	}
16443 	cq_set = mbox->sge_array->addr[0];
16444 	shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16445 	bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16446 
16447 	for (idx = 0; idx < numcq; idx++) {
16448 		cq = cqp[idx];
16449 		eq = hdwq[idx].hba_eq;
16450 		if (!cq || !eq) {
16451 			status = -ENOMEM;
16452 			goto out;
16453 		}
16454 		if (!phba->sli4_hba.pc_sli4_params.supported)
16455 			hw_page_size = cq->page_size;
16456 
16457 		switch (idx) {
16458 		case 0:
16459 			bf_set(lpfc_mbx_cq_create_set_page_size,
16460 			       &cq_set->u.request,
16461 			       (hw_page_size / SLI4_PAGE_SIZE));
16462 			bf_set(lpfc_mbx_cq_create_set_num_pages,
16463 			       &cq_set->u.request, cq->page_count);
16464 			bf_set(lpfc_mbx_cq_create_set_evt,
16465 			       &cq_set->u.request, 1);
16466 			bf_set(lpfc_mbx_cq_create_set_valid,
16467 			       &cq_set->u.request, 1);
16468 			bf_set(lpfc_mbx_cq_create_set_cqe_size,
16469 			       &cq_set->u.request, 0);
16470 			bf_set(lpfc_mbx_cq_create_set_num_cq,
16471 			       &cq_set->u.request, numcq);
16472 			bf_set(lpfc_mbx_cq_create_set_autovalid,
16473 			       &cq_set->u.request,
16474 			       phba->sli4_hba.pc_sli4_params.cqav);
16475 			switch (cq->entry_count) {
16476 			case 2048:
16477 			case 4096:
16478 				if (phba->sli4_hba.pc_sli4_params.cqv ==
16479 				    LPFC_Q_CREATE_VERSION_2) {
16480 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16481 					       &cq_set->u.request,
16482 						cq->entry_count);
16483 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16484 					       &cq_set->u.request,
16485 					       LPFC_CQ_CNT_WORD7);
16486 					break;
16487 				}
16488 				fallthrough;
16489 			default:
16490 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16491 						"3118 Bad CQ count. (%d)\n",
16492 						cq->entry_count);
16493 				if (cq->entry_count < 256) {
16494 					status = -EINVAL;
16495 					goto out;
16496 				}
16497 				fallthrough;	/* otherwise default to smallest */
16498 			case 256:
16499 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16500 				       &cq_set->u.request, LPFC_CQ_CNT_256);
16501 				break;
16502 			case 512:
16503 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16504 				       &cq_set->u.request, LPFC_CQ_CNT_512);
16505 				break;
16506 			case 1024:
16507 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16508 				       &cq_set->u.request, LPFC_CQ_CNT_1024);
16509 				break;
16510 			}
16511 			bf_set(lpfc_mbx_cq_create_set_eq_id0,
16512 			       &cq_set->u.request, eq->queue_id);
16513 			break;
16514 		case 1:
16515 			bf_set(lpfc_mbx_cq_create_set_eq_id1,
16516 			       &cq_set->u.request, eq->queue_id);
16517 			break;
16518 		case 2:
16519 			bf_set(lpfc_mbx_cq_create_set_eq_id2,
16520 			       &cq_set->u.request, eq->queue_id);
16521 			break;
16522 		case 3:
16523 			bf_set(lpfc_mbx_cq_create_set_eq_id3,
16524 			       &cq_set->u.request, eq->queue_id);
16525 			break;
16526 		case 4:
16527 			bf_set(lpfc_mbx_cq_create_set_eq_id4,
16528 			       &cq_set->u.request, eq->queue_id);
16529 			break;
16530 		case 5:
16531 			bf_set(lpfc_mbx_cq_create_set_eq_id5,
16532 			       &cq_set->u.request, eq->queue_id);
16533 			break;
16534 		case 6:
16535 			bf_set(lpfc_mbx_cq_create_set_eq_id6,
16536 			       &cq_set->u.request, eq->queue_id);
16537 			break;
16538 		case 7:
16539 			bf_set(lpfc_mbx_cq_create_set_eq_id7,
16540 			       &cq_set->u.request, eq->queue_id);
16541 			break;
16542 		case 8:
16543 			bf_set(lpfc_mbx_cq_create_set_eq_id8,
16544 			       &cq_set->u.request, eq->queue_id);
16545 			break;
16546 		case 9:
16547 			bf_set(lpfc_mbx_cq_create_set_eq_id9,
16548 			       &cq_set->u.request, eq->queue_id);
16549 			break;
16550 		case 10:
16551 			bf_set(lpfc_mbx_cq_create_set_eq_id10,
16552 			       &cq_set->u.request, eq->queue_id);
16553 			break;
16554 		case 11:
16555 			bf_set(lpfc_mbx_cq_create_set_eq_id11,
16556 			       &cq_set->u.request, eq->queue_id);
16557 			break;
16558 		case 12:
16559 			bf_set(lpfc_mbx_cq_create_set_eq_id12,
16560 			       &cq_set->u.request, eq->queue_id);
16561 			break;
16562 		case 13:
16563 			bf_set(lpfc_mbx_cq_create_set_eq_id13,
16564 			       &cq_set->u.request, eq->queue_id);
16565 			break;
16566 		case 14:
16567 			bf_set(lpfc_mbx_cq_create_set_eq_id14,
16568 			       &cq_set->u.request, eq->queue_id);
16569 			break;
16570 		case 15:
16571 			bf_set(lpfc_mbx_cq_create_set_eq_id15,
16572 			       &cq_set->u.request, eq->queue_id);
16573 			break;
16574 		}
16575 
16576 		/* link the cq onto the parent eq child list */
16577 		list_add_tail(&cq->list, &eq->child_list);
16578 		/* Set up completion queue's type and subtype */
16579 		cq->type = type;
16580 		cq->subtype = subtype;
16581 		cq->assoc_qid = eq->queue_id;
16582 		cq->assoc_qp = eq;
16583 		cq->host_index = 0;
16584 		cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16585 		cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16586 					 cq->entry_count);
16587 		cq->chann = idx;
16588 
16589 		rc = 0;
16590 		list_for_each_entry(dmabuf, &cq->page_list, list) {
16591 			memset(dmabuf->virt, 0, hw_page_size);
16592 			cnt = page_idx + dmabuf->buffer_tag;
16593 			cq_set->u.request.page[cnt].addr_lo =
16594 					putPaddrLow(dmabuf->phys);
16595 			cq_set->u.request.page[cnt].addr_hi =
16596 					putPaddrHigh(dmabuf->phys);
16597 			rc++;
16598 		}
16599 		page_idx += rc;
16600 	}
16601 
16602 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16603 
16604 	/* The IOCTL status is embedded in the mailbox subheader. */
16605 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16606 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16607 	if (shdr_status || shdr_add_status || rc) {
16608 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16609 				"3119 CQ_CREATE_SET mailbox failed with "
16610 				"status x%x add_status x%x, mbx status x%x\n",
16611 				shdr_status, shdr_add_status, rc);
16612 		status = -ENXIO;
16613 		goto out;
16614 	}
16615 	rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16616 	if (rc == 0xFFFF) {
16617 		status = -ENXIO;
16618 		goto out;
16619 	}
16620 
16621 	for (idx = 0; idx < numcq; idx++) {
16622 		cq = cqp[idx];
16623 		cq->queue_id = rc + idx;
16624 		if (cq->queue_id > phba->sli4_hba.cq_max)
16625 			phba->sli4_hba.cq_max = cq->queue_id;
16626 	}
16627 
16628 out:
16629 	lpfc_sli4_mbox_cmd_free(phba, mbox);
16630 	return status;
16631 }
16632 
16633 /**
16634  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
16635  * @phba: HBA structure that indicates port to create a queue on.
16636  * @mq: The queue structure to use to create the mailbox queue.
16637  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16638  * @cq: The completion queue to associate with this cq.
16639  *
16640  * This function provides failback (fb) functionality when the
16641  * mq_create_ext fails on older FW generations.  It's purpose is identical
16642  * to mq_create_ext otherwise.
16643  *
16644  * This routine cannot fail as all attributes were previously accessed and
16645  * initialized in mq_create_ext.
16646  **/
16647 static void
lpfc_mq_create_fb_init(struct lpfc_hba * phba,struct lpfc_queue * mq,LPFC_MBOXQ_t * mbox,struct lpfc_queue * cq)16648 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16649 		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
16650 {
16651 	struct lpfc_mbx_mq_create *mq_create;
16652 	struct lpfc_dmabuf *dmabuf;
16653 	int length;
16654 
16655 	length = (sizeof(struct lpfc_mbx_mq_create) -
16656 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16657 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16658 			 LPFC_MBOX_OPCODE_MQ_CREATE,
16659 			 length, LPFC_SLI4_MBX_EMBED);
16660 	mq_create = &mbox->u.mqe.un.mq_create;
16661 	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
16662 	       mq->page_count);
16663 	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
16664 	       cq->queue_id);
16665 	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16666 	switch (mq->entry_count) {
16667 	case 16:
16668 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16669 		       LPFC_MQ_RING_SIZE_16);
16670 		break;
16671 	case 32:
16672 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16673 		       LPFC_MQ_RING_SIZE_32);
16674 		break;
16675 	case 64:
16676 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16677 		       LPFC_MQ_RING_SIZE_64);
16678 		break;
16679 	case 128:
16680 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16681 		       LPFC_MQ_RING_SIZE_128);
16682 		break;
16683 	}
16684 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16685 		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16686 			putPaddrLow(dmabuf->phys);
16687 		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16688 			putPaddrHigh(dmabuf->phys);
16689 	}
16690 }
16691 
16692 /**
16693  * lpfc_mq_create - Create a mailbox Queue on the HBA
16694  * @phba: HBA structure that indicates port to create a queue on.
16695  * @mq: The queue structure to use to create the mailbox queue.
16696  * @cq: The completion queue to associate with this cq.
16697  * @subtype: The queue's subtype.
16698  *
16699  * This function creates a mailbox queue, as detailed in @mq, on a port,
16700  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16701  *
16702  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16703  * is used to get the entry count and entry size that are necessary to
16704  * determine the number of pages to allocate and use for this queue. This
16705  * function will send the MQ_CREATE mailbox command to the HBA to setup the
16706  * mailbox queue. This function is asynchronous and will wait for the mailbox
16707  * command to finish before continuing.
16708  *
16709  * On success this function will return a zero. If unable to allocate enough
16710  * memory this function will return -ENOMEM. If the queue create mailbox command
16711  * fails this function will return -ENXIO.
16712  **/
16713 int32_t
lpfc_mq_create(struct lpfc_hba * phba,struct lpfc_queue * mq,struct lpfc_queue * cq,uint32_t subtype)16714 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16715 	       struct lpfc_queue *cq, uint32_t subtype)
16716 {
16717 	struct lpfc_mbx_mq_create *mq_create;
16718 	struct lpfc_mbx_mq_create_ext *mq_create_ext;
16719 	struct lpfc_dmabuf *dmabuf;
16720 	LPFC_MBOXQ_t *mbox;
16721 	int rc, length, status = 0;
16722 	uint32_t shdr_status, shdr_add_status;
16723 	union lpfc_sli4_cfg_shdr *shdr;
16724 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16725 
16726 	/* sanity check on queue memory */
16727 	if (!mq || !cq)
16728 		return -ENODEV;
16729 	if (!phba->sli4_hba.pc_sli4_params.supported)
16730 		hw_page_size = SLI4_PAGE_SIZE;
16731 
16732 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16733 	if (!mbox)
16734 		return -ENOMEM;
16735 	length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16736 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16737 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16738 			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16739 			 length, LPFC_SLI4_MBX_EMBED);
16740 
16741 	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
16742 	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
16743 	bf_set(lpfc_mbx_mq_create_ext_num_pages,
16744 	       &mq_create_ext->u.request, mq->page_count);
16745 	bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16746 	       &mq_create_ext->u.request, 1);
16747 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
16748 	       &mq_create_ext->u.request, 1);
16749 	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16750 	       &mq_create_ext->u.request, 1);
16751 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16752 	       &mq_create_ext->u.request, 1);
16753 	bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16754 	       &mq_create_ext->u.request, 1);
16755 	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
16756 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16757 	       phba->sli4_hba.pc_sli4_params.mqv);
16758 	if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16759 		bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16760 		       cq->queue_id);
16761 	else
16762 		bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16763 		       cq->queue_id);
16764 	switch (mq->entry_count) {
16765 	default:
16766 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16767 				"0362 Unsupported MQ count. (%d)\n",
16768 				mq->entry_count);
16769 		if (mq->entry_count < 16) {
16770 			status = -EINVAL;
16771 			goto out;
16772 		}
16773 		fallthrough;	/* otherwise default to smallest count */
16774 	case 16:
16775 		bf_set(lpfc_mq_context_ring_size,
16776 		       &mq_create_ext->u.request.context,
16777 		       LPFC_MQ_RING_SIZE_16);
16778 		break;
16779 	case 32:
16780 		bf_set(lpfc_mq_context_ring_size,
16781 		       &mq_create_ext->u.request.context,
16782 		       LPFC_MQ_RING_SIZE_32);
16783 		break;
16784 	case 64:
16785 		bf_set(lpfc_mq_context_ring_size,
16786 		       &mq_create_ext->u.request.context,
16787 		       LPFC_MQ_RING_SIZE_64);
16788 		break;
16789 	case 128:
16790 		bf_set(lpfc_mq_context_ring_size,
16791 		       &mq_create_ext->u.request.context,
16792 		       LPFC_MQ_RING_SIZE_128);
16793 		break;
16794 	}
16795 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16796 		memset(dmabuf->virt, 0, hw_page_size);
16797 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16798 					putPaddrLow(dmabuf->phys);
16799 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16800 					putPaddrHigh(dmabuf->phys);
16801 	}
16802 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16803 	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16804 			      &mq_create_ext->u.response);
16805 	if (rc != MBX_SUCCESS) {
16806 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16807 				"2795 MQ_CREATE_EXT failed with "
16808 				"status x%x. Failback to MQ_CREATE.\n",
16809 				rc);
16810 		lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16811 		mq_create = &mbox->u.mqe.un.mq_create;
16812 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16813 		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16814 		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16815 				      &mq_create->u.response);
16816 	}
16817 
16818 	/* The IOCTL status is embedded in the mailbox subheader. */
16819 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16820 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16821 	if (shdr_status || shdr_add_status || rc) {
16822 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16823 				"2502 MQ_CREATE mailbox failed with "
16824 				"status x%x add_status x%x, mbx status x%x\n",
16825 				shdr_status, shdr_add_status, rc);
16826 		status = -ENXIO;
16827 		goto out;
16828 	}
16829 	if (mq->queue_id == 0xFFFF) {
16830 		status = -ENXIO;
16831 		goto out;
16832 	}
16833 	mq->type = LPFC_MQ;
16834 	mq->assoc_qid = cq->queue_id;
16835 	mq->subtype = subtype;
16836 	mq->host_index = 0;
16837 	mq->hba_index = 0;
16838 
16839 	/* link the mq onto the parent cq child list */
16840 	list_add_tail(&mq->list, &cq->child_list);
16841 out:
16842 	mempool_free(mbox, phba->mbox_mem_pool);
16843 	return status;
16844 }
16845 
16846 /**
16847  * lpfc_wq_create - Create a Work Queue on the HBA
16848  * @phba: HBA structure that indicates port to create a queue on.
16849  * @wq: The queue structure to use to create the work queue.
16850  * @cq: The completion queue to bind this work queue to.
16851  * @subtype: The subtype of the work queue indicating its functionality.
16852  *
16853  * This function creates a work queue, as detailed in @wq, on a port, described
16854  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16855  *
16856  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16857  * is used to get the entry count and entry size that are necessary to
16858  * determine the number of pages to allocate and use for this queue. The @cq
16859  * is used to indicate which completion queue to bind this work queue to. This
16860  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16861  * work queue. This function is asynchronous and will wait for the mailbox
16862  * command to finish before continuing.
16863  *
16864  * On success this function will return a zero. If unable to allocate enough
16865  * memory this function will return -ENOMEM. If the queue create mailbox command
16866  * fails this function will return -ENXIO.
16867  **/
16868 int
lpfc_wq_create(struct lpfc_hba * phba,struct lpfc_queue * wq,struct lpfc_queue * cq,uint32_t subtype)16869 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16870 	       struct lpfc_queue *cq, uint32_t subtype)
16871 {
16872 	struct lpfc_mbx_wq_create *wq_create;
16873 	struct lpfc_dmabuf *dmabuf;
16874 	LPFC_MBOXQ_t *mbox;
16875 	int rc, length, status = 0;
16876 	uint32_t shdr_status, shdr_add_status;
16877 	union lpfc_sli4_cfg_shdr *shdr;
16878 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16879 	struct dma_address *page;
16880 	void __iomem *bar_memmap_p;
16881 	uint32_t db_offset;
16882 	uint16_t pci_barset;
16883 	uint8_t dpp_barset;
16884 	uint32_t dpp_offset;
16885 	uint8_t wq_create_version;
16886 #ifdef CONFIG_X86
16887 	unsigned long pg_addr;
16888 #endif
16889 
16890 	/* sanity check on queue memory */
16891 	if (!wq || !cq)
16892 		return -ENODEV;
16893 	if (!phba->sli4_hba.pc_sli4_params.supported)
16894 		hw_page_size = wq->page_size;
16895 
16896 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16897 	if (!mbox)
16898 		return -ENOMEM;
16899 	length = (sizeof(struct lpfc_mbx_wq_create) -
16900 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16901 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16902 			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16903 			 length, LPFC_SLI4_MBX_EMBED);
16904 	wq_create = &mbox->u.mqe.un.wq_create;
16905 	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16906 	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16907 		    wq->page_count);
16908 	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16909 		    cq->queue_id);
16910 
16911 	/* wqv is the earliest version supported, NOT the latest */
16912 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16913 	       phba->sli4_hba.pc_sli4_params.wqv);
16914 
16915 	if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16916 	    (wq->page_size > SLI4_PAGE_SIZE))
16917 		wq_create_version = LPFC_Q_CREATE_VERSION_1;
16918 	else
16919 		wq_create_version = LPFC_Q_CREATE_VERSION_0;
16920 
16921 	switch (wq_create_version) {
16922 	case LPFC_Q_CREATE_VERSION_1:
16923 		bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16924 		       wq->entry_count);
16925 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16926 		       LPFC_Q_CREATE_VERSION_1);
16927 
16928 		switch (wq->entry_size) {
16929 		default:
16930 		case 64:
16931 			bf_set(lpfc_mbx_wq_create_wqe_size,
16932 			       &wq_create->u.request_1,
16933 			       LPFC_WQ_WQE_SIZE_64);
16934 			break;
16935 		case 128:
16936 			bf_set(lpfc_mbx_wq_create_wqe_size,
16937 			       &wq_create->u.request_1,
16938 			       LPFC_WQ_WQE_SIZE_128);
16939 			break;
16940 		}
16941 		/* Request DPP by default */
16942 		bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
16943 		bf_set(lpfc_mbx_wq_create_page_size,
16944 		       &wq_create->u.request_1,
16945 		       (wq->page_size / SLI4_PAGE_SIZE));
16946 		page = wq_create->u.request_1.page;
16947 		break;
16948 	default:
16949 		page = wq_create->u.request.page;
16950 		break;
16951 	}
16952 
16953 	list_for_each_entry(dmabuf, &wq->page_list, list) {
16954 		memset(dmabuf->virt, 0, hw_page_size);
16955 		page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16956 		page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
16957 	}
16958 
16959 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16960 		bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16961 
16962 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16963 	/* The IOCTL status is embedded in the mailbox subheader. */
16964 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16965 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16966 	if (shdr_status || shdr_add_status || rc) {
16967 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16968 				"2503 WQ_CREATE mailbox failed with "
16969 				"status x%x add_status x%x, mbx status x%x\n",
16970 				shdr_status, shdr_add_status, rc);
16971 		status = -ENXIO;
16972 		goto out;
16973 	}
16974 
16975 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16976 		wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16977 					&wq_create->u.response);
16978 	else
16979 		wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16980 					&wq_create->u.response_1);
16981 
16982 	if (wq->queue_id == 0xFFFF) {
16983 		status = -ENXIO;
16984 		goto out;
16985 	}
16986 
16987 	wq->db_format = LPFC_DB_LIST_FORMAT;
16988 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16989 		if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16990 			wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16991 					       &wq_create->u.response);
16992 			if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16993 			    (wq->db_format != LPFC_DB_RING_FORMAT)) {
16994 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16995 						"3265 WQ[%d] doorbell format "
16996 						"not supported: x%x\n",
16997 						wq->queue_id, wq->db_format);
16998 				status = -EINVAL;
16999 				goto out;
17000 			}
17001 			pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
17002 					    &wq_create->u.response);
17003 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17004 								   pci_barset);
17005 			if (!bar_memmap_p) {
17006 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17007 						"3263 WQ[%d] failed to memmap "
17008 						"pci barset:x%x\n",
17009 						wq->queue_id, pci_barset);
17010 				status = -ENOMEM;
17011 				goto out;
17012 			}
17013 			db_offset = wq_create->u.response.doorbell_offset;
17014 			if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
17015 			    (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
17016 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17017 						"3252 WQ[%d] doorbell offset "
17018 						"not supported: x%x\n",
17019 						wq->queue_id, db_offset);
17020 				status = -EINVAL;
17021 				goto out;
17022 			}
17023 			wq->db_regaddr = bar_memmap_p + db_offset;
17024 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17025 					"3264 WQ[%d]: barset:x%x, offset:x%x, "
17026 					"format:x%x\n", wq->queue_id,
17027 					pci_barset, db_offset, wq->db_format);
17028 		} else
17029 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17030 	} else {
17031 		/* Check if DPP was honored by the firmware */
17032 		wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
17033 				    &wq_create->u.response_1);
17034 		if (wq->dpp_enable) {
17035 			pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
17036 					    &wq_create->u.response_1);
17037 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17038 								   pci_barset);
17039 			if (!bar_memmap_p) {
17040 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17041 						"3267 WQ[%d] failed to memmap "
17042 						"pci barset:x%x\n",
17043 						wq->queue_id, pci_barset);
17044 				status = -ENOMEM;
17045 				goto out;
17046 			}
17047 			db_offset = wq_create->u.response_1.doorbell_offset;
17048 			wq->db_regaddr = bar_memmap_p + db_offset;
17049 			wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
17050 					    &wq_create->u.response_1);
17051 			dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
17052 					    &wq_create->u.response_1);
17053 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17054 								   dpp_barset);
17055 			if (!bar_memmap_p) {
17056 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17057 						"3268 WQ[%d] failed to memmap "
17058 						"pci barset:x%x\n",
17059 						wq->queue_id, dpp_barset);
17060 				status = -ENOMEM;
17061 				goto out;
17062 			}
17063 			dpp_offset = wq_create->u.response_1.dpp_offset;
17064 			wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17065 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17066 					"3271 WQ[%d]: barset:x%x, offset:x%x, "
17067 					"dpp_id:x%x dpp_barset:x%x "
17068 					"dpp_offset:x%x\n",
17069 					wq->queue_id, pci_barset, db_offset,
17070 					wq->dpp_id, dpp_barset, dpp_offset);
17071 
17072 #ifdef CONFIG_X86
17073 			/* Enable combined writes for DPP aperture */
17074 			pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
17075 			rc = set_memory_wc(pg_addr, 1);
17076 			if (rc) {
17077 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17078 					"3272 Cannot setup Combined "
17079 					"Write on WQ[%d] - disable DPP\n",
17080 					wq->queue_id);
17081 				phba->cfg_enable_dpp = 0;
17082 			}
17083 #else
17084 			phba->cfg_enable_dpp = 0;
17085 #endif
17086 		} else
17087 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17088 	}
17089 	wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17090 	if (wq->pring == NULL) {
17091 		status = -ENOMEM;
17092 		goto out;
17093 	}
17094 	wq->type = LPFC_WQ;
17095 	wq->assoc_qid = cq->queue_id;
17096 	wq->subtype = subtype;
17097 	wq->host_index = 0;
17098 	wq->hba_index = 0;
17099 	wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
17100 
17101 	/* link the wq onto the parent cq child list */
17102 	list_add_tail(&wq->list, &cq->child_list);
17103 out:
17104 	mempool_free(mbox, phba->mbox_mem_pool);
17105 	return status;
17106 }
17107 
17108 /**
17109  * lpfc_rq_create - Create a Receive Queue on the HBA
17110  * @phba: HBA structure that indicates port to create a queue on.
17111  * @hrq: The queue structure to use to create the header receive queue.
17112  * @drq: The queue structure to use to create the data receive queue.
17113  * @cq: The completion queue to bind this work queue to.
17114  * @subtype: The subtype of the work queue indicating its functionality.
17115  *
17116  * This function creates a receive buffer queue pair , as detailed in @hrq and
17117  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17118  * to the HBA.
17119  *
17120  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17121  * struct is used to get the entry count that is necessary to determine the
17122  * number of pages to use for this queue. The @cq is used to indicate which
17123  * completion queue to bind received buffers that are posted to these queues to.
17124  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17125  * receive queue pair. This function is asynchronous and will wait for the
17126  * mailbox command to finish before continuing.
17127  *
17128  * On success this function will return a zero. If unable to allocate enough
17129  * memory this function will return -ENOMEM. If the queue create mailbox command
17130  * fails this function will return -ENXIO.
17131  **/
17132 int
lpfc_rq_create(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,struct lpfc_queue * cq,uint32_t subtype)17133 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17134 	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17135 {
17136 	struct lpfc_mbx_rq_create *rq_create;
17137 	struct lpfc_dmabuf *dmabuf;
17138 	LPFC_MBOXQ_t *mbox;
17139 	int rc, length, status = 0;
17140 	uint32_t shdr_status, shdr_add_status;
17141 	union lpfc_sli4_cfg_shdr *shdr;
17142 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17143 	void __iomem *bar_memmap_p;
17144 	uint32_t db_offset;
17145 	uint16_t pci_barset;
17146 
17147 	/* sanity check on queue memory */
17148 	if (!hrq || !drq || !cq)
17149 		return -ENODEV;
17150 	if (!phba->sli4_hba.pc_sli4_params.supported)
17151 		hw_page_size = SLI4_PAGE_SIZE;
17152 
17153 	if (hrq->entry_count != drq->entry_count)
17154 		return -EINVAL;
17155 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17156 	if (!mbox)
17157 		return -ENOMEM;
17158 	length = (sizeof(struct lpfc_mbx_rq_create) -
17159 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17160 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17161 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17162 			 length, LPFC_SLI4_MBX_EMBED);
17163 	rq_create = &mbox->u.mqe.un.rq_create;
17164 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17165 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17166 	       phba->sli4_hba.pc_sli4_params.rqv);
17167 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17168 		bf_set(lpfc_rq_context_rqe_count_1,
17169 		       &rq_create->u.request.context,
17170 		       hrq->entry_count);
17171 		rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
17172 		bf_set(lpfc_rq_context_rqe_size,
17173 		       &rq_create->u.request.context,
17174 		       LPFC_RQE_SIZE_8);
17175 		bf_set(lpfc_rq_context_page_size,
17176 		       &rq_create->u.request.context,
17177 		       LPFC_RQ_PAGE_SIZE_4096);
17178 	} else {
17179 		switch (hrq->entry_count) {
17180 		default:
17181 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17182 					"2535 Unsupported RQ count. (%d)\n",
17183 					hrq->entry_count);
17184 			if (hrq->entry_count < 512) {
17185 				status = -EINVAL;
17186 				goto out;
17187 			}
17188 			fallthrough;	/* otherwise default to smallest count */
17189 		case 512:
17190 			bf_set(lpfc_rq_context_rqe_count,
17191 			       &rq_create->u.request.context,
17192 			       LPFC_RQ_RING_SIZE_512);
17193 			break;
17194 		case 1024:
17195 			bf_set(lpfc_rq_context_rqe_count,
17196 			       &rq_create->u.request.context,
17197 			       LPFC_RQ_RING_SIZE_1024);
17198 			break;
17199 		case 2048:
17200 			bf_set(lpfc_rq_context_rqe_count,
17201 			       &rq_create->u.request.context,
17202 			       LPFC_RQ_RING_SIZE_2048);
17203 			break;
17204 		case 4096:
17205 			bf_set(lpfc_rq_context_rqe_count,
17206 			       &rq_create->u.request.context,
17207 			       LPFC_RQ_RING_SIZE_4096);
17208 			break;
17209 		}
17210 		bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17211 		       LPFC_HDR_BUF_SIZE);
17212 	}
17213 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17214 	       cq->queue_id);
17215 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17216 	       hrq->page_count);
17217 	list_for_each_entry(dmabuf, &hrq->page_list, list) {
17218 		memset(dmabuf->virt, 0, hw_page_size);
17219 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17220 					putPaddrLow(dmabuf->phys);
17221 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17222 					putPaddrHigh(dmabuf->phys);
17223 	}
17224 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17225 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17226 
17227 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17228 	/* The IOCTL status is embedded in the mailbox subheader. */
17229 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17230 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17231 	if (shdr_status || shdr_add_status || rc) {
17232 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17233 				"2504 RQ_CREATE mailbox failed with "
17234 				"status x%x add_status x%x, mbx status x%x\n",
17235 				shdr_status, shdr_add_status, rc);
17236 		status = -ENXIO;
17237 		goto out;
17238 	}
17239 	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17240 	if (hrq->queue_id == 0xFFFF) {
17241 		status = -ENXIO;
17242 		goto out;
17243 	}
17244 
17245 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17246 		hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17247 					&rq_create->u.response);
17248 		if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17249 		    (hrq->db_format != LPFC_DB_RING_FORMAT)) {
17250 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17251 					"3262 RQ [%d] doorbell format not "
17252 					"supported: x%x\n", hrq->queue_id,
17253 					hrq->db_format);
17254 			status = -EINVAL;
17255 			goto out;
17256 		}
17257 
17258 		pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17259 				    &rq_create->u.response);
17260 		bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17261 		if (!bar_memmap_p) {
17262 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17263 					"3269 RQ[%d] failed to memmap pci "
17264 					"barset:x%x\n", hrq->queue_id,
17265 					pci_barset);
17266 			status = -ENOMEM;
17267 			goto out;
17268 		}
17269 
17270 		db_offset = rq_create->u.response.doorbell_offset;
17271 		if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17272 		    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
17273 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17274 					"3270 RQ[%d] doorbell offset not "
17275 					"supported: x%x\n", hrq->queue_id,
17276 					db_offset);
17277 			status = -EINVAL;
17278 			goto out;
17279 		}
17280 		hrq->db_regaddr = bar_memmap_p + db_offset;
17281 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17282 				"3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17283 				"format:x%x\n", hrq->queue_id, pci_barset,
17284 				db_offset, hrq->db_format);
17285 	} else {
17286 		hrq->db_format = LPFC_DB_RING_FORMAT;
17287 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17288 	}
17289 	hrq->type = LPFC_HRQ;
17290 	hrq->assoc_qid = cq->queue_id;
17291 	hrq->subtype = subtype;
17292 	hrq->host_index = 0;
17293 	hrq->hba_index = 0;
17294 	hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17295 
17296 	/* now create the data queue */
17297 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17298 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17299 			 length, LPFC_SLI4_MBX_EMBED);
17300 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17301 	       phba->sli4_hba.pc_sli4_params.rqv);
17302 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17303 		bf_set(lpfc_rq_context_rqe_count_1,
17304 		       &rq_create->u.request.context, hrq->entry_count);
17305 		if (subtype == LPFC_NVMET)
17306 			rq_create->u.request.context.buffer_size =
17307 				LPFC_NVMET_DATA_BUF_SIZE;
17308 		else
17309 			rq_create->u.request.context.buffer_size =
17310 				LPFC_DATA_BUF_SIZE;
17311 		bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17312 		       LPFC_RQE_SIZE_8);
17313 		bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17314 		       (PAGE_SIZE/SLI4_PAGE_SIZE));
17315 	} else {
17316 		switch (drq->entry_count) {
17317 		default:
17318 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17319 					"2536 Unsupported RQ count. (%d)\n",
17320 					drq->entry_count);
17321 			if (drq->entry_count < 512) {
17322 				status = -EINVAL;
17323 				goto out;
17324 			}
17325 			fallthrough;	/* otherwise default to smallest count */
17326 		case 512:
17327 			bf_set(lpfc_rq_context_rqe_count,
17328 			       &rq_create->u.request.context,
17329 			       LPFC_RQ_RING_SIZE_512);
17330 			break;
17331 		case 1024:
17332 			bf_set(lpfc_rq_context_rqe_count,
17333 			       &rq_create->u.request.context,
17334 			       LPFC_RQ_RING_SIZE_1024);
17335 			break;
17336 		case 2048:
17337 			bf_set(lpfc_rq_context_rqe_count,
17338 			       &rq_create->u.request.context,
17339 			       LPFC_RQ_RING_SIZE_2048);
17340 			break;
17341 		case 4096:
17342 			bf_set(lpfc_rq_context_rqe_count,
17343 			       &rq_create->u.request.context,
17344 			       LPFC_RQ_RING_SIZE_4096);
17345 			break;
17346 		}
17347 		if (subtype == LPFC_NVMET)
17348 			bf_set(lpfc_rq_context_buf_size,
17349 			       &rq_create->u.request.context,
17350 			       LPFC_NVMET_DATA_BUF_SIZE);
17351 		else
17352 			bf_set(lpfc_rq_context_buf_size,
17353 			       &rq_create->u.request.context,
17354 			       LPFC_DATA_BUF_SIZE);
17355 	}
17356 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17357 	       cq->queue_id);
17358 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17359 	       drq->page_count);
17360 	list_for_each_entry(dmabuf, &drq->page_list, list) {
17361 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17362 					putPaddrLow(dmabuf->phys);
17363 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17364 					putPaddrHigh(dmabuf->phys);
17365 	}
17366 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17367 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17368 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17369 	/* The IOCTL status is embedded in the mailbox subheader. */
17370 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17371 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17372 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17373 	if (shdr_status || shdr_add_status || rc) {
17374 		status = -ENXIO;
17375 		goto out;
17376 	}
17377 	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17378 	if (drq->queue_id == 0xFFFF) {
17379 		status = -ENXIO;
17380 		goto out;
17381 	}
17382 	drq->type = LPFC_DRQ;
17383 	drq->assoc_qid = cq->queue_id;
17384 	drq->subtype = subtype;
17385 	drq->host_index = 0;
17386 	drq->hba_index = 0;
17387 	drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17388 
17389 	/* link the header and data RQs onto the parent cq child list */
17390 	list_add_tail(&hrq->list, &cq->child_list);
17391 	list_add_tail(&drq->list, &cq->child_list);
17392 
17393 out:
17394 	mempool_free(mbox, phba->mbox_mem_pool);
17395 	return status;
17396 }
17397 
17398 /**
17399  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17400  * @phba: HBA structure that indicates port to create a queue on.
17401  * @hrqp: The queue structure array to use to create the header receive queues.
17402  * @drqp: The queue structure array to use to create the data receive queues.
17403  * @cqp: The completion queue array to bind these receive queues to.
17404  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
17405  *
17406  * This function creates a receive buffer queue pair , as detailed in @hrq and
17407  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17408  * to the HBA.
17409  *
17410  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17411  * struct is used to get the entry count that is necessary to determine the
17412  * number of pages to use for this queue. The @cq is used to indicate which
17413  * completion queue to bind received buffers that are posted to these queues to.
17414  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17415  * receive queue pair. This function is asynchronous and will wait for the
17416  * mailbox command to finish before continuing.
17417  *
17418  * On success this function will return a zero. If unable to allocate enough
17419  * memory this function will return -ENOMEM. If the queue create mailbox command
17420  * fails this function will return -ENXIO.
17421  **/
17422 int
lpfc_mrq_create(struct lpfc_hba * phba,struct lpfc_queue ** hrqp,struct lpfc_queue ** drqp,struct lpfc_queue ** cqp,uint32_t subtype)17423 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17424 		struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17425 		uint32_t subtype)
17426 {
17427 	struct lpfc_queue *hrq, *drq, *cq;
17428 	struct lpfc_mbx_rq_create_v2 *rq_create;
17429 	struct lpfc_dmabuf *dmabuf;
17430 	LPFC_MBOXQ_t *mbox;
17431 	int rc, length, alloclen, status = 0;
17432 	int cnt, idx, numrq, page_idx = 0;
17433 	uint32_t shdr_status, shdr_add_status;
17434 	union lpfc_sli4_cfg_shdr *shdr;
17435 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17436 
17437 	numrq = phba->cfg_nvmet_mrq;
17438 	/* sanity check on array memory */
17439 	if (!hrqp || !drqp || !cqp || !numrq)
17440 		return -ENODEV;
17441 	if (!phba->sli4_hba.pc_sli4_params.supported)
17442 		hw_page_size = SLI4_PAGE_SIZE;
17443 
17444 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17445 	if (!mbox)
17446 		return -ENOMEM;
17447 
17448 	length = sizeof(struct lpfc_mbx_rq_create_v2);
17449 	length += ((2 * numrq * hrqp[0]->page_count) *
17450 		   sizeof(struct dma_address));
17451 
17452 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17453 				    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17454 				    LPFC_SLI4_MBX_NEMBED);
17455 	if (alloclen < length) {
17456 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17457 				"3099 Allocated DMA memory size (%d) is "
17458 				"less than the requested DMA memory size "
17459 				"(%d)\n", alloclen, length);
17460 		status = -ENOMEM;
17461 		goto out;
17462 	}
17463 
17464 
17465 
17466 	rq_create = mbox->sge_array->addr[0];
17467 	shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17468 
17469 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17470 	cnt = 0;
17471 
17472 	for (idx = 0; idx < numrq; idx++) {
17473 		hrq = hrqp[idx];
17474 		drq = drqp[idx];
17475 		cq  = cqp[idx];
17476 
17477 		/* sanity check on queue memory */
17478 		if (!hrq || !drq || !cq) {
17479 			status = -ENODEV;
17480 			goto out;
17481 		}
17482 
17483 		if (hrq->entry_count != drq->entry_count) {
17484 			status = -EINVAL;
17485 			goto out;
17486 		}
17487 
17488 		if (idx == 0) {
17489 			bf_set(lpfc_mbx_rq_create_num_pages,
17490 			       &rq_create->u.request,
17491 			       hrq->page_count);
17492 			bf_set(lpfc_mbx_rq_create_rq_cnt,
17493 			       &rq_create->u.request, (numrq * 2));
17494 			bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17495 			       1);
17496 			bf_set(lpfc_rq_context_base_cq,
17497 			       &rq_create->u.request.context,
17498 			       cq->queue_id);
17499 			bf_set(lpfc_rq_context_data_size,
17500 			       &rq_create->u.request.context,
17501 			       LPFC_NVMET_DATA_BUF_SIZE);
17502 			bf_set(lpfc_rq_context_hdr_size,
17503 			       &rq_create->u.request.context,
17504 			       LPFC_HDR_BUF_SIZE);
17505 			bf_set(lpfc_rq_context_rqe_count_1,
17506 			       &rq_create->u.request.context,
17507 			       hrq->entry_count);
17508 			bf_set(lpfc_rq_context_rqe_size,
17509 			       &rq_create->u.request.context,
17510 			       LPFC_RQE_SIZE_8);
17511 			bf_set(lpfc_rq_context_page_size,
17512 			       &rq_create->u.request.context,
17513 			       (PAGE_SIZE/SLI4_PAGE_SIZE));
17514 		}
17515 		rc = 0;
17516 		list_for_each_entry(dmabuf, &hrq->page_list, list) {
17517 			memset(dmabuf->virt, 0, hw_page_size);
17518 			cnt = page_idx + dmabuf->buffer_tag;
17519 			rq_create->u.request.page[cnt].addr_lo =
17520 					putPaddrLow(dmabuf->phys);
17521 			rq_create->u.request.page[cnt].addr_hi =
17522 					putPaddrHigh(dmabuf->phys);
17523 			rc++;
17524 		}
17525 		page_idx += rc;
17526 
17527 		rc = 0;
17528 		list_for_each_entry(dmabuf, &drq->page_list, list) {
17529 			memset(dmabuf->virt, 0, hw_page_size);
17530 			cnt = page_idx + dmabuf->buffer_tag;
17531 			rq_create->u.request.page[cnt].addr_lo =
17532 					putPaddrLow(dmabuf->phys);
17533 			rq_create->u.request.page[cnt].addr_hi =
17534 					putPaddrHigh(dmabuf->phys);
17535 			rc++;
17536 		}
17537 		page_idx += rc;
17538 
17539 		hrq->db_format = LPFC_DB_RING_FORMAT;
17540 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17541 		hrq->type = LPFC_HRQ;
17542 		hrq->assoc_qid = cq->queue_id;
17543 		hrq->subtype = subtype;
17544 		hrq->host_index = 0;
17545 		hrq->hba_index = 0;
17546 		hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17547 
17548 		drq->db_format = LPFC_DB_RING_FORMAT;
17549 		drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17550 		drq->type = LPFC_DRQ;
17551 		drq->assoc_qid = cq->queue_id;
17552 		drq->subtype = subtype;
17553 		drq->host_index = 0;
17554 		drq->hba_index = 0;
17555 		drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17556 
17557 		list_add_tail(&hrq->list, &cq->child_list);
17558 		list_add_tail(&drq->list, &cq->child_list);
17559 	}
17560 
17561 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17562 	/* The IOCTL status is embedded in the mailbox subheader. */
17563 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17564 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17565 	if (shdr_status || shdr_add_status || rc) {
17566 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17567 				"3120 RQ_CREATE mailbox failed with "
17568 				"status x%x add_status x%x, mbx status x%x\n",
17569 				shdr_status, shdr_add_status, rc);
17570 		status = -ENXIO;
17571 		goto out;
17572 	}
17573 	rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17574 	if (rc == 0xFFFF) {
17575 		status = -ENXIO;
17576 		goto out;
17577 	}
17578 
17579 	/* Initialize all RQs with associated queue id */
17580 	for (idx = 0; idx < numrq; idx++) {
17581 		hrq = hrqp[idx];
17582 		hrq->queue_id = rc + (2 * idx);
17583 		drq = drqp[idx];
17584 		drq->queue_id = rc + (2 * idx) + 1;
17585 	}
17586 
17587 out:
17588 	lpfc_sli4_mbox_cmd_free(phba, mbox);
17589 	return status;
17590 }
17591 
17592 /**
17593  * lpfc_eq_destroy - Destroy an event Queue on the HBA
17594  * @phba: HBA structure that indicates port to destroy a queue on.
17595  * @eq: The queue structure associated with the queue to destroy.
17596  *
17597  * This function destroys a queue, as detailed in @eq by sending an mailbox
17598  * command, specific to the type of queue, to the HBA.
17599  *
17600  * The @eq struct is used to get the queue ID of the queue to destroy.
17601  *
17602  * On success this function will return a zero. If the queue destroy mailbox
17603  * command fails this function will return -ENXIO.
17604  **/
17605 int
lpfc_eq_destroy(struct lpfc_hba * phba,struct lpfc_queue * eq)17606 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17607 {
17608 	LPFC_MBOXQ_t *mbox;
17609 	int rc, length, status = 0;
17610 	uint32_t shdr_status, shdr_add_status;
17611 	union lpfc_sli4_cfg_shdr *shdr;
17612 
17613 	/* sanity check on queue memory */
17614 	if (!eq)
17615 		return -ENODEV;
17616 
17617 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17618 		goto list_remove;
17619 
17620 	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17621 	if (!mbox)
17622 		return -ENOMEM;
17623 	length = (sizeof(struct lpfc_mbx_eq_destroy) -
17624 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17625 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17626 			 LPFC_MBOX_OPCODE_EQ_DESTROY,
17627 			 length, LPFC_SLI4_MBX_EMBED);
17628 	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17629 	       eq->queue_id);
17630 	mbox->vport = eq->phba->pport;
17631 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17632 
17633 	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17634 	/* The IOCTL status is embedded in the mailbox subheader. */
17635 	shdr = (union lpfc_sli4_cfg_shdr *)
17636 		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17637 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17638 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17639 	if (shdr_status || shdr_add_status || rc) {
17640 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17641 				"2505 EQ_DESTROY mailbox failed with "
17642 				"status x%x add_status x%x, mbx status x%x\n",
17643 				shdr_status, shdr_add_status, rc);
17644 		status = -ENXIO;
17645 	}
17646 	mempool_free(mbox, eq->phba->mbox_mem_pool);
17647 
17648 list_remove:
17649 	/* Remove eq from any list */
17650 	list_del_init(&eq->list);
17651 
17652 	return status;
17653 }
17654 
17655 /**
17656  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
17657  * @phba: HBA structure that indicates port to destroy a queue on.
17658  * @cq: The queue structure associated with the queue to destroy.
17659  *
17660  * This function destroys a queue, as detailed in @cq by sending an mailbox
17661  * command, specific to the type of queue, to the HBA.
17662  *
17663  * The @cq struct is used to get the queue ID of the queue to destroy.
17664  *
17665  * On success this function will return a zero. If the queue destroy mailbox
17666  * command fails this function will return -ENXIO.
17667  **/
17668 int
lpfc_cq_destroy(struct lpfc_hba * phba,struct lpfc_queue * cq)17669 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17670 {
17671 	LPFC_MBOXQ_t *mbox;
17672 	int rc, length, status = 0;
17673 	uint32_t shdr_status, shdr_add_status;
17674 	union lpfc_sli4_cfg_shdr *shdr;
17675 
17676 	/* sanity check on queue memory */
17677 	if (!cq)
17678 		return -ENODEV;
17679 
17680 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17681 		goto list_remove;
17682 
17683 	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17684 	if (!mbox)
17685 		return -ENOMEM;
17686 	length = (sizeof(struct lpfc_mbx_cq_destroy) -
17687 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17688 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17689 			 LPFC_MBOX_OPCODE_CQ_DESTROY,
17690 			 length, LPFC_SLI4_MBX_EMBED);
17691 	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17692 	       cq->queue_id);
17693 	mbox->vport = cq->phba->pport;
17694 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17695 	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17696 	/* The IOCTL status is embedded in the mailbox subheader. */
17697 	shdr = (union lpfc_sli4_cfg_shdr *)
17698 		&mbox->u.mqe.un.wq_create.header.cfg_shdr;
17699 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17700 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17701 	if (shdr_status || shdr_add_status || rc) {
17702 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17703 				"2506 CQ_DESTROY mailbox failed with "
17704 				"status x%x add_status x%x, mbx status x%x\n",
17705 				shdr_status, shdr_add_status, rc);
17706 		status = -ENXIO;
17707 	}
17708 	mempool_free(mbox, cq->phba->mbox_mem_pool);
17709 
17710 list_remove:
17711 	/* Remove cq from any list */
17712 	list_del_init(&cq->list);
17713 	return status;
17714 }
17715 
17716 /**
17717  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
17718  * @phba: HBA structure that indicates port to destroy a queue on.
17719  * @mq: The queue structure associated with the queue to destroy.
17720  *
17721  * This function destroys a queue, as detailed in @mq by sending an mailbox
17722  * command, specific to the type of queue, to the HBA.
17723  *
17724  * The @mq struct is used to get the queue ID of the queue to destroy.
17725  *
17726  * On success this function will return a zero. If the queue destroy mailbox
17727  * command fails this function will return -ENXIO.
17728  **/
17729 int
lpfc_mq_destroy(struct lpfc_hba * phba,struct lpfc_queue * mq)17730 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17731 {
17732 	LPFC_MBOXQ_t *mbox;
17733 	int rc, length, status = 0;
17734 	uint32_t shdr_status, shdr_add_status;
17735 	union lpfc_sli4_cfg_shdr *shdr;
17736 
17737 	/* sanity check on queue memory */
17738 	if (!mq)
17739 		return -ENODEV;
17740 
17741 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17742 		goto list_remove;
17743 
17744 	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17745 	if (!mbox)
17746 		return -ENOMEM;
17747 	length = (sizeof(struct lpfc_mbx_mq_destroy) -
17748 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17749 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17750 			 LPFC_MBOX_OPCODE_MQ_DESTROY,
17751 			 length, LPFC_SLI4_MBX_EMBED);
17752 	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17753 	       mq->queue_id);
17754 	mbox->vport = mq->phba->pport;
17755 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17756 	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17757 	/* The IOCTL status is embedded in the mailbox subheader. */
17758 	shdr = (union lpfc_sli4_cfg_shdr *)
17759 		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17760 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17761 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17762 	if (shdr_status || shdr_add_status || rc) {
17763 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17764 				"2507 MQ_DESTROY mailbox failed with "
17765 				"status x%x add_status x%x, mbx status x%x\n",
17766 				shdr_status, shdr_add_status, rc);
17767 		status = -ENXIO;
17768 	}
17769 	mempool_free(mbox, mq->phba->mbox_mem_pool);
17770 
17771 list_remove:
17772 	/* Remove mq from any list */
17773 	list_del_init(&mq->list);
17774 	return status;
17775 }
17776 
17777 /**
17778  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
17779  * @phba: HBA structure that indicates port to destroy a queue on.
17780  * @wq: The queue structure associated with the queue to destroy.
17781  *
17782  * This function destroys a queue, as detailed in @wq by sending an mailbox
17783  * command, specific to the type of queue, to the HBA.
17784  *
17785  * The @wq struct is used to get the queue ID of the queue to destroy.
17786  *
17787  * On success this function will return a zero. If the queue destroy mailbox
17788  * command fails this function will return -ENXIO.
17789  **/
17790 int
lpfc_wq_destroy(struct lpfc_hba * phba,struct lpfc_queue * wq)17791 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17792 {
17793 	LPFC_MBOXQ_t *mbox;
17794 	int rc, length, status = 0;
17795 	uint32_t shdr_status, shdr_add_status;
17796 	union lpfc_sli4_cfg_shdr *shdr;
17797 
17798 	/* sanity check on queue memory */
17799 	if (!wq)
17800 		return -ENODEV;
17801 
17802 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17803 		goto list_remove;
17804 
17805 	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17806 	if (!mbox)
17807 		return -ENOMEM;
17808 	length = (sizeof(struct lpfc_mbx_wq_destroy) -
17809 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17810 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17811 			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17812 			 length, LPFC_SLI4_MBX_EMBED);
17813 	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17814 	       wq->queue_id);
17815 	mbox->vport = wq->phba->pport;
17816 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17817 	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17818 	shdr = (union lpfc_sli4_cfg_shdr *)
17819 		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17820 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17821 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17822 	if (shdr_status || shdr_add_status || rc) {
17823 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17824 				"2508 WQ_DESTROY mailbox failed with "
17825 				"status x%x add_status x%x, mbx status x%x\n",
17826 				shdr_status, shdr_add_status, rc);
17827 		status = -ENXIO;
17828 	}
17829 	mempool_free(mbox, wq->phba->mbox_mem_pool);
17830 
17831 list_remove:
17832 	/* Remove wq from any list */
17833 	list_del_init(&wq->list);
17834 	kfree(wq->pring);
17835 	wq->pring = NULL;
17836 	return status;
17837 }
17838 
17839 /**
17840  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17841  * @phba: HBA structure that indicates port to destroy a queue on.
17842  * @hrq: The queue structure associated with the queue to destroy.
17843  * @drq: The queue structure associated with the queue to destroy.
17844  *
17845  * This function destroys a queue, as detailed in @rq by sending an mailbox
17846  * command, specific to the type of queue, to the HBA.
17847  *
17848  * The @rq struct is used to get the queue ID of the queue to destroy.
17849  *
17850  * On success this function will return a zero. If the queue destroy mailbox
17851  * command fails this function will return -ENXIO.
17852  **/
17853 int
lpfc_rq_destroy(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq)17854 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17855 		struct lpfc_queue *drq)
17856 {
17857 	LPFC_MBOXQ_t *mbox;
17858 	int rc, length, status = 0;
17859 	uint32_t shdr_status, shdr_add_status;
17860 	union lpfc_sli4_cfg_shdr *shdr;
17861 
17862 	/* sanity check on queue memory */
17863 	if (!hrq || !drq)
17864 		return -ENODEV;
17865 
17866 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17867 		goto list_remove;
17868 
17869 	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17870 	if (!mbox)
17871 		return -ENOMEM;
17872 	length = (sizeof(struct lpfc_mbx_rq_destroy) -
17873 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17874 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17875 			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17876 			 length, LPFC_SLI4_MBX_EMBED);
17877 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17878 	       hrq->queue_id);
17879 	mbox->vport = hrq->phba->pport;
17880 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17881 	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17882 	/* The IOCTL status is embedded in the mailbox subheader. */
17883 	shdr = (union lpfc_sli4_cfg_shdr *)
17884 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17885 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17886 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17887 	if (shdr_status || shdr_add_status || rc) {
17888 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17889 				"2509 RQ_DESTROY mailbox failed with "
17890 				"status x%x add_status x%x, mbx status x%x\n",
17891 				shdr_status, shdr_add_status, rc);
17892 		mempool_free(mbox, hrq->phba->mbox_mem_pool);
17893 		return -ENXIO;
17894 	}
17895 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17896 	       drq->queue_id);
17897 	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17898 	shdr = (union lpfc_sli4_cfg_shdr *)
17899 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17900 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17901 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17902 	if (shdr_status || shdr_add_status || rc) {
17903 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17904 				"2510 RQ_DESTROY mailbox failed with "
17905 				"status x%x add_status x%x, mbx status x%x\n",
17906 				shdr_status, shdr_add_status, rc);
17907 		status = -ENXIO;
17908 	}
17909 	mempool_free(mbox, hrq->phba->mbox_mem_pool);
17910 
17911 list_remove:
17912 	list_del_init(&hrq->list);
17913 	list_del_init(&drq->list);
17914 	return status;
17915 }
17916 
17917 /**
17918  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17919  * @phba: The virtual port for which this call being executed.
17920  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17921  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17922  * @xritag: the xritag that ties this io to the SGL pages.
17923  *
17924  * This routine will post the sgl pages for the IO that has the xritag
17925  * that is in the iocbq structure. The xritag is assigned during iocbq
17926  * creation and persists for as long as the driver is loaded.
17927  * if the caller has fewer than 256 scatter gather segments to map then
17928  * pdma_phys_addr1 should be 0.
17929  * If the caller needs to map more than 256 scatter gather segment then
17930  * pdma_phys_addr1 should be a valid physical address.
17931  * physical address for SGLs must be 64 byte aligned.
17932  * If you are going to map 2 SGL's then the first one must have 256 entries
17933  * the second sgl can have between 1 and 256 entries.
17934  *
17935  * Return codes:
17936  * 	0 - Success
17937  * 	-ENXIO, -ENOMEM - Failure
17938  **/
17939 int
lpfc_sli4_post_sgl(struct lpfc_hba * phba,dma_addr_t pdma_phys_addr0,dma_addr_t pdma_phys_addr1,uint16_t xritag)17940 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17941 		dma_addr_t pdma_phys_addr0,
17942 		dma_addr_t pdma_phys_addr1,
17943 		uint16_t xritag)
17944 {
17945 	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17946 	LPFC_MBOXQ_t *mbox;
17947 	int rc;
17948 	uint32_t shdr_status, shdr_add_status;
17949 	uint32_t mbox_tmo;
17950 	union lpfc_sli4_cfg_shdr *shdr;
17951 
17952 	if (xritag == NO_XRI) {
17953 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17954 				"0364 Invalid param:\n");
17955 		return -EINVAL;
17956 	}
17957 
17958 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17959 	if (!mbox)
17960 		return -ENOMEM;
17961 
17962 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17963 			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17964 			sizeof(struct lpfc_mbx_post_sgl_pages) -
17965 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17966 
17967 	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17968 				&mbox->u.mqe.un.post_sgl_pages;
17969 	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17970 	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17971 
17972 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=
17973 				cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17974 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17975 				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17976 
17977 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=
17978 				cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17979 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17980 				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17981 	if (!phba->sli4_hba.intr_enable)
17982 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17983 	else {
17984 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17985 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17986 	}
17987 	/* The IOCTL status is embedded in the mailbox subheader. */
17988 	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17989 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17990 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17991 	if (!phba->sli4_hba.intr_enable)
17992 		mempool_free(mbox, phba->mbox_mem_pool);
17993 	else if (rc != MBX_TIMEOUT)
17994 		mempool_free(mbox, phba->mbox_mem_pool);
17995 	if (shdr_status || shdr_add_status || rc) {
17996 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17997 				"2511 POST_SGL mailbox failed with "
17998 				"status x%x add_status x%x, mbx status x%x\n",
17999 				shdr_status, shdr_add_status, rc);
18000 	}
18001 	return 0;
18002 }
18003 
18004 /**
18005  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
18006  * @phba: pointer to lpfc hba data structure.
18007  *
18008  * This routine is invoked to post rpi header templates to the
18009  * HBA consistent with the SLI-4 interface spec.  This routine
18010  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18011  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18012  *
18013  * Returns
18014  *	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18015  *	LPFC_RPI_ALLOC_ERROR if no rpis are available.
18016  **/
18017 static uint16_t
lpfc_sli4_alloc_xri(struct lpfc_hba * phba)18018 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
18019 {
18020 	unsigned long xri;
18021 
18022 	/*
18023 	 * Fetch the next logical xri.  Because this index is logical,
18024 	 * the driver starts at 0 each time.
18025 	 */
18026 	spin_lock_irq(&phba->hbalock);
18027 	xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,
18028 				 phba->sli4_hba.max_cfg_param.max_xri);
18029 	if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
18030 		spin_unlock_irq(&phba->hbalock);
18031 		return NO_XRI;
18032 	} else {
18033 		set_bit(xri, phba->sli4_hba.xri_bmask);
18034 		phba->sli4_hba.max_cfg_param.xri_used++;
18035 	}
18036 	spin_unlock_irq(&phba->hbalock);
18037 	return xri;
18038 }
18039 
18040 /**
18041  * __lpfc_sli4_free_xri - Release an xri for reuse.
18042  * @phba: pointer to lpfc hba data structure.
18043  * @xri: xri to release.
18044  *
18045  * This routine is invoked to release an xri to the pool of
18046  * available rpis maintained by the driver.
18047  **/
18048 static void
__lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18049 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18050 {
18051 	if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
18052 		phba->sli4_hba.max_cfg_param.xri_used--;
18053 	}
18054 }
18055 
18056 /**
18057  * lpfc_sli4_free_xri - Release an xri for reuse.
18058  * @phba: pointer to lpfc hba data structure.
18059  * @xri: xri to release.
18060  *
18061  * This routine is invoked to release an xri to the pool of
18062  * available rpis maintained by the driver.
18063  **/
18064 void
lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18065 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18066 {
18067 	spin_lock_irq(&phba->hbalock);
18068 	__lpfc_sli4_free_xri(phba, xri);
18069 	spin_unlock_irq(&phba->hbalock);
18070 }
18071 
18072 /**
18073  * lpfc_sli4_next_xritag - Get an xritag for the io
18074  * @phba: Pointer to HBA context object.
18075  *
18076  * This function gets an xritag for the iocb. If there is no unused xritag
18077  * it will return 0xffff.
18078  * The function returns the allocated xritag if successful, else returns zero.
18079  * Zero is not a valid xritag.
18080  * The caller is not required to hold any lock.
18081  **/
18082 uint16_t
lpfc_sli4_next_xritag(struct lpfc_hba * phba)18083 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
18084 {
18085 	uint16_t xri_index;
18086 
18087 	xri_index = lpfc_sli4_alloc_xri(phba);
18088 	if (xri_index == NO_XRI)
18089 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18090 				"2004 Failed to allocate XRI.last XRITAG is %d"
18091 				" Max XRI is %d, Used XRI is %d\n",
18092 				xri_index,
18093 				phba->sli4_hba.max_cfg_param.max_xri,
18094 				phba->sli4_hba.max_cfg_param.xri_used);
18095 	return xri_index;
18096 }
18097 
18098 /**
18099  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
18100  * @phba: pointer to lpfc hba data structure.
18101  * @post_sgl_list: pointer to els sgl entry list.
18102  * @post_cnt: number of els sgl entries on the list.
18103  *
18104  * This routine is invoked to post a block of driver's sgl pages to the
18105  * HBA using non-embedded mailbox command. No Lock is held. This routine
18106  * is only called when the driver is loading and after all IO has been
18107  * stopped.
18108  **/
18109 static int
lpfc_sli4_post_sgl_list(struct lpfc_hba * phba,struct list_head * post_sgl_list,int post_cnt)18110 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
18111 			    struct list_head *post_sgl_list,
18112 			    int post_cnt)
18113 {
18114 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
18115 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18116 	struct sgl_page_pairs *sgl_pg_pairs;
18117 	void *viraddr;
18118 	LPFC_MBOXQ_t *mbox;
18119 	uint32_t reqlen, alloclen, pg_pairs;
18120 	uint32_t mbox_tmo;
18121 	uint16_t xritag_start = 0;
18122 	int rc = 0;
18123 	uint32_t shdr_status, shdr_add_status;
18124 	union lpfc_sli4_cfg_shdr *shdr;
18125 
18126 	reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
18127 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18128 	if (reqlen > SLI4_PAGE_SIZE) {
18129 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18130 				"2559 Block sgl registration required DMA "
18131 				"size (%d) great than a page\n", reqlen);
18132 		return -ENOMEM;
18133 	}
18134 
18135 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18136 	if (!mbox)
18137 		return -ENOMEM;
18138 
18139 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18140 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18141 			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18142 			 LPFC_SLI4_MBX_NEMBED);
18143 
18144 	if (alloclen < reqlen) {
18145 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18146 				"0285 Allocated DMA memory size (%d) is "
18147 				"less than the requested DMA memory "
18148 				"size (%d)\n", alloclen, reqlen);
18149 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18150 		return -ENOMEM;
18151 	}
18152 	/* Set up the SGL pages in the non-embedded DMA pages */
18153 	viraddr = mbox->sge_array->addr[0];
18154 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18155 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18156 
18157 	pg_pairs = 0;
18158 	list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
18159 		/* Set up the sge entry */
18160 		sgl_pg_pairs->sgl_pg0_addr_lo =
18161 				cpu_to_le32(putPaddrLow(sglq_entry->phys));
18162 		sgl_pg_pairs->sgl_pg0_addr_hi =
18163 				cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18164 		sgl_pg_pairs->sgl_pg1_addr_lo =
18165 				cpu_to_le32(putPaddrLow(0));
18166 		sgl_pg_pairs->sgl_pg1_addr_hi =
18167 				cpu_to_le32(putPaddrHigh(0));
18168 
18169 		/* Keep the first xritag on the list */
18170 		if (pg_pairs == 0)
18171 			xritag_start = sglq_entry->sli4_xritag;
18172 		sgl_pg_pairs++;
18173 		pg_pairs++;
18174 	}
18175 
18176 	/* Complete initialization and perform endian conversion. */
18177 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18178 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
18179 	sgl->word0 = cpu_to_le32(sgl->word0);
18180 
18181 	if (!phba->sli4_hba.intr_enable)
18182 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18183 	else {
18184 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18185 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18186 	}
18187 	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18188 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18189 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18190 	if (!phba->sli4_hba.intr_enable)
18191 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18192 	else if (rc != MBX_TIMEOUT)
18193 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18194 	if (shdr_status || shdr_add_status || rc) {
18195 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18196 				"2513 POST_SGL_BLOCK mailbox command failed "
18197 				"status x%x add_status x%x mbx status x%x\n",
18198 				shdr_status, shdr_add_status, rc);
18199 		rc = -ENXIO;
18200 	}
18201 	return rc;
18202 }
18203 
18204 /**
18205  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
18206  * @phba: pointer to lpfc hba data structure.
18207  * @nblist: pointer to nvme buffer list.
18208  * @count: number of scsi buffers on the list.
18209  *
18210  * This routine is invoked to post a block of @count scsi sgl pages from a
18211  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
18212  * No Lock is held.
18213  *
18214  **/
18215 static int
lpfc_sli4_post_io_sgl_block(struct lpfc_hba * phba,struct list_head * nblist,int count)18216 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18217 			    int count)
18218 {
18219 	struct lpfc_io_buf *lpfc_ncmd;
18220 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18221 	struct sgl_page_pairs *sgl_pg_pairs;
18222 	void *viraddr;
18223 	LPFC_MBOXQ_t *mbox;
18224 	uint32_t reqlen, alloclen, pg_pairs;
18225 	uint32_t mbox_tmo;
18226 	uint16_t xritag_start = 0;
18227 	int rc = 0;
18228 	uint32_t shdr_status, shdr_add_status;
18229 	dma_addr_t pdma_phys_bpl1;
18230 	union lpfc_sli4_cfg_shdr *shdr;
18231 
18232 	/* Calculate the requested length of the dma memory */
18233 	reqlen = count * sizeof(struct sgl_page_pairs) +
18234 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18235 	if (reqlen > SLI4_PAGE_SIZE) {
18236 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
18237 				"6118 Block sgl registration required DMA "
18238 				"size (%d) great than a page\n", reqlen);
18239 		return -ENOMEM;
18240 	}
18241 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18242 	if (!mbox) {
18243 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18244 				"6119 Failed to allocate mbox cmd memory\n");
18245 		return -ENOMEM;
18246 	}
18247 
18248 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18249 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18250 				    LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18251 				    reqlen, LPFC_SLI4_MBX_NEMBED);
18252 
18253 	if (alloclen < reqlen) {
18254 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18255 				"6120 Allocated DMA memory size (%d) is "
18256 				"less than the requested DMA memory "
18257 				"size (%d)\n", alloclen, reqlen);
18258 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18259 		return -ENOMEM;
18260 	}
18261 
18262 	/* Get the first SGE entry from the non-embedded DMA memory */
18263 	viraddr = mbox->sge_array->addr[0];
18264 
18265 	/* Set up the SGL pages in the non-embedded DMA pages */
18266 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18267 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18268 
18269 	pg_pairs = 0;
18270 	list_for_each_entry(lpfc_ncmd, nblist, list) {
18271 		/* Set up the sge entry */
18272 		sgl_pg_pairs->sgl_pg0_addr_lo =
18273 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
18274 		sgl_pg_pairs->sgl_pg0_addr_hi =
18275 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
18276 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
18277 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18278 						SGL_PAGE_SIZE;
18279 		else
18280 			pdma_phys_bpl1 = 0;
18281 		sgl_pg_pairs->sgl_pg1_addr_lo =
18282 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18283 		sgl_pg_pairs->sgl_pg1_addr_hi =
18284 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18285 		/* Keep the first xritag on the list */
18286 		if (pg_pairs == 0)
18287 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
18288 		sgl_pg_pairs++;
18289 		pg_pairs++;
18290 	}
18291 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18292 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18293 	/* Perform endian conversion if necessary */
18294 	sgl->word0 = cpu_to_le32(sgl->word0);
18295 
18296 	if (!phba->sli4_hba.intr_enable) {
18297 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18298 	} else {
18299 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18300 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18301 	}
18302 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
18303 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18304 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18305 	if (!phba->sli4_hba.intr_enable)
18306 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18307 	else if (rc != MBX_TIMEOUT)
18308 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18309 	if (shdr_status || shdr_add_status || rc) {
18310 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18311 				"6125 POST_SGL_BLOCK mailbox command failed "
18312 				"status x%x add_status x%x mbx status x%x\n",
18313 				shdr_status, shdr_add_status, rc);
18314 		rc = -ENXIO;
18315 	}
18316 	return rc;
18317 }
18318 
18319 /**
18320  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
18321  * @phba: pointer to lpfc hba data structure.
18322  * @post_nblist: pointer to the nvme buffer list.
18323  * @sb_count: number of nvme buffers.
18324  *
18325  * This routine walks a list of nvme buffers that was passed in. It attempts
18326  * to construct blocks of nvme buffer sgls which contains contiguous xris and
18327  * uses the non-embedded SGL block post mailbox commands to post to the port.
18328  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18329  * embedded SGL post mailbox command for posting. The @post_nblist passed in
18330  * must be local list, thus no lock is needed when manipulate the list.
18331  *
18332  * Returns: 0 = failure, non-zero number of successfully posted buffers.
18333  **/
18334 int
lpfc_sli4_post_io_sgl_list(struct lpfc_hba * phba,struct list_head * post_nblist,int sb_count)18335 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18336 			   struct list_head *post_nblist, int sb_count)
18337 {
18338 	struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
18339 	int status, sgl_size;
18340 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18341 	dma_addr_t pdma_phys_sgl1;
18342 	int last_xritag = NO_XRI;
18343 	int cur_xritag;
18344 	LIST_HEAD(prep_nblist);
18345 	LIST_HEAD(blck_nblist);
18346 	LIST_HEAD(nvme_nblist);
18347 
18348 	/* sanity check */
18349 	if (sb_count <= 0)
18350 		return -EINVAL;
18351 
18352 	sgl_size = phba->cfg_sg_dma_buf_size;
18353 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18354 		list_del_init(&lpfc_ncmd->list);
18355 		block_cnt++;
18356 		if ((last_xritag != NO_XRI) &&
18357 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18358 			/* a hole in xri block, form a sgl posting block */
18359 			list_splice_init(&prep_nblist, &blck_nblist);
18360 			post_cnt = block_cnt - 1;
18361 			/* prepare list for next posting block */
18362 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18363 			block_cnt = 1;
18364 		} else {
18365 			/* prepare list for next posting block */
18366 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18367 			/* enough sgls for non-embed sgl mbox command */
18368 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18369 				list_splice_init(&prep_nblist, &blck_nblist);
18370 				post_cnt = block_cnt;
18371 				block_cnt = 0;
18372 			}
18373 		}
18374 		num_posting++;
18375 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18376 
18377 		/* end of repost sgl list condition for NVME buffers */
18378 		if (num_posting == sb_count) {
18379 			if (post_cnt == 0) {
18380 				/* last sgl posting block */
18381 				list_splice_init(&prep_nblist, &blck_nblist);
18382 				post_cnt = block_cnt;
18383 			} else if (block_cnt == 1) {
18384 				/* last single sgl with non-contiguous xri */
18385 				if (sgl_size > SGL_PAGE_SIZE)
18386 					pdma_phys_sgl1 =
18387 						lpfc_ncmd->dma_phys_sgl +
18388 						SGL_PAGE_SIZE;
18389 				else
18390 					pdma_phys_sgl1 = 0;
18391 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18392 				status = lpfc_sli4_post_sgl(
18393 						phba, lpfc_ncmd->dma_phys_sgl,
18394 						pdma_phys_sgl1, cur_xritag);
18395 				if (status) {
18396 					/* Post error.  Buffer unavailable. */
18397 					lpfc_ncmd->flags |=
18398 						LPFC_SBUF_NOT_POSTED;
18399 				} else {
18400 					/* Post success. Bffer available. */
18401 					lpfc_ncmd->flags &=
18402 						~LPFC_SBUF_NOT_POSTED;
18403 					lpfc_ncmd->status = IOSTAT_SUCCESS;
18404 					num_posted++;
18405 				}
18406 				/* success, put on NVME buffer sgl list */
18407 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18408 			}
18409 		}
18410 
18411 		/* continue until a nembed page worth of sgls */
18412 		if (post_cnt == 0)
18413 			continue;
18414 
18415 		/* post block of NVME buffer list sgls */
18416 		status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18417 						     post_cnt);
18418 
18419 		/* don't reset xirtag due to hole in xri block */
18420 		if (block_cnt == 0)
18421 			last_xritag = NO_XRI;
18422 
18423 		/* reset NVME buffer post count for next round of posting */
18424 		post_cnt = 0;
18425 
18426 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18427 		while (!list_empty(&blck_nblist)) {
18428 			list_remove_head(&blck_nblist, lpfc_ncmd,
18429 					 struct lpfc_io_buf, list);
18430 			if (status) {
18431 				/* Post error.  Mark buffer unavailable. */
18432 				lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
18433 			} else {
18434 				/* Post success, Mark buffer available. */
18435 				lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
18436 				lpfc_ncmd->status = IOSTAT_SUCCESS;
18437 				num_posted++;
18438 			}
18439 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18440 		}
18441 	}
18442 	/* Push NVME buffers with sgl posted to the available list */
18443 	lpfc_io_buf_replenish(phba, &nvme_nblist);
18444 
18445 	return num_posted;
18446 }
18447 
18448 /**
18449  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18450  * @phba: pointer to lpfc_hba struct that the frame was received on
18451  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18452  *
18453  * This function checks the fields in the @fc_hdr to see if the FC frame is a
18454  * valid type of frame that the LPFC driver will handle. This function will
18455  * return a zero if the frame is a valid frame or a non zero value when the
18456  * frame does not pass the check.
18457  **/
18458 static int
lpfc_fc_frame_check(struct lpfc_hba * phba,struct fc_frame_header * fc_hdr)18459 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18460 {
18461 	/*  make rctl_names static to save stack space */
18462 	struct fc_vft_header *fc_vft_hdr;
18463 	struct fc_app_header *fc_app_hdr;
18464 	uint32_t *header = (uint32_t *) fc_hdr;
18465 
18466 #define FC_RCTL_MDS_DIAGS	0xF4
18467 
18468 	switch (fc_hdr->fh_r_ctl) {
18469 	case FC_RCTL_DD_UNCAT:		/* uncategorized information */
18470 	case FC_RCTL_DD_SOL_DATA:	/* solicited data */
18471 	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */
18472 	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */
18473 	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */
18474 	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */
18475 	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */
18476 	case FC_RCTL_DD_CMD_STATUS:	/* command status */
18477 	case FC_RCTL_ELS_REQ:	/* extended link services request */
18478 	case FC_RCTL_ELS_REP:	/* extended link services reply */
18479 	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */
18480 	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */
18481 	case FC_RCTL_BA_ABTS: 	/* basic link service abort */
18482 	case FC_RCTL_BA_RMC: 	/* remove connection */
18483 	case FC_RCTL_BA_ACC:	/* basic accept */
18484 	case FC_RCTL_BA_RJT:	/* basic reject */
18485 	case FC_RCTL_BA_PRMT:
18486 	case FC_RCTL_ACK_1:	/* acknowledge_1 */
18487 	case FC_RCTL_ACK_0:	/* acknowledge_0 */
18488 	case FC_RCTL_P_RJT:	/* port reject */
18489 	case FC_RCTL_F_RJT:	/* fabric reject */
18490 	case FC_RCTL_P_BSY:	/* port busy */
18491 	case FC_RCTL_F_BSY:	/* fabric busy to data frame */
18492 	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */
18493 	case FC_RCTL_LCR:	/* link credit reset */
18494 	case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
18495 	case FC_RCTL_END:	/* end */
18496 		break;
18497 	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */
18498 		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18499 		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18500 		return lpfc_fc_frame_check(phba, fc_hdr);
18501 	case FC_RCTL_BA_NOP:	/* basic link service NOP */
18502 	default:
18503 		goto drop;
18504 	}
18505 
18506 	switch (fc_hdr->fh_type) {
18507 	case FC_TYPE_BLS:
18508 	case FC_TYPE_ELS:
18509 	case FC_TYPE_FCP:
18510 	case FC_TYPE_CT:
18511 	case FC_TYPE_NVME:
18512 		break;
18513 	case FC_TYPE_IP:
18514 	case FC_TYPE_ILS:
18515 	default:
18516 		goto drop;
18517 	}
18518 
18519 	if (unlikely(phba->link_flag == LS_LOOPBACK_MODE &&
18520 				phba->cfg_vmid_app_header)) {
18521 		/* Application header is 16B device header */
18522 		if (fc_hdr->fh_df_ctl & LPFC_FC_16B_DEVICE_HEADER) {
18523 			fc_app_hdr = (struct fc_app_header *) (fc_hdr + 1);
18524 			if (be32_to_cpu(fc_app_hdr->src_app_id) !=
18525 					LOOPBACK_SRC_APPID) {
18526 				lpfc_printf_log(phba, KERN_WARNING,
18527 						LOG_ELS | LOG_LIBDFC,
18528 						"1932 Loopback src app id "
18529 						"not matched, app_id:x%x\n",
18530 						be32_to_cpu(fc_app_hdr->src_app_id));
18531 
18532 				goto drop;
18533 			}
18534 		} else {
18535 			lpfc_printf_log(phba, KERN_WARNING,
18536 					LOG_ELS | LOG_LIBDFC,
18537 					"1933 Loopback df_ctl bit not set, "
18538 					"df_ctl:x%x\n",
18539 					fc_hdr->fh_df_ctl);
18540 
18541 			goto drop;
18542 		}
18543 	}
18544 
18545 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
18546 			"2538 Received frame rctl:x%x, type:x%x, "
18547 			"frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
18548 			fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18549 			be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18550 			be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18551 			be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18552 			be32_to_cpu(header[6]));
18553 	return 0;
18554 drop:
18555 	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
18556 			"2539 Dropped frame rctl:x%x type:x%x\n",
18557 			fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18558 	return 1;
18559 }
18560 
18561 /**
18562  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18563  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18564  *
18565  * This function processes the FC header to retrieve the VFI from the VF
18566  * header, if one exists. This function will return the VFI if one exists
18567  * or 0 if no VSAN Header exists.
18568  **/
18569 static uint32_t
lpfc_fc_hdr_get_vfi(struct fc_frame_header * fc_hdr)18570 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18571 {
18572 	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18573 
18574 	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18575 		return 0;
18576 	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18577 }
18578 
18579 /**
18580  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18581  * @phba: Pointer to the HBA structure to search for the vport on
18582  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18583  * @fcfi: The FC Fabric ID that the frame came from
18584  * @did: Destination ID to match against
18585  *
18586  * This function searches the @phba for a vport that matches the content of the
18587  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18588  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18589  * returns the matching vport pointer or NULL if unable to match frame to a
18590  * vport.
18591  **/
18592 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)18593 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
18594 		       uint16_t fcfi, uint32_t did)
18595 {
18596 	struct lpfc_vport **vports;
18597 	struct lpfc_vport *vport = NULL;
18598 	int i;
18599 
18600 	if (did == Fabric_DID)
18601 		return phba->pport;
18602 	if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
18603 	    phba->link_state != LPFC_HBA_READY)
18604 		return phba->pport;
18605 
18606 	vports = lpfc_create_vport_work_array(phba);
18607 	if (vports != NULL) {
18608 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18609 			if (phba->fcf.fcfi == fcfi &&
18610 			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18611 			    vports[i]->fc_myDID == did) {
18612 				vport = vports[i];
18613 				break;
18614 			}
18615 		}
18616 	}
18617 	lpfc_destroy_vport_work_array(phba, vports);
18618 	return vport;
18619 }
18620 
18621 /**
18622  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18623  * @vport: The vport to work on.
18624  *
18625  * This function updates the receive sequence time stamp for this vport. The
18626  * receive sequence time stamp indicates the time that the last frame of the
18627  * the sequence that has been idle for the longest amount of time was received.
18628  * the driver uses this time stamp to indicate if any received sequences have
18629  * timed out.
18630  **/
18631 static void
lpfc_update_rcv_time_stamp(struct lpfc_vport * vport)18632 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18633 {
18634 	struct lpfc_dmabuf *h_buf;
18635 	struct hbq_dmabuf *dmabuf = NULL;
18636 
18637 	/* get the oldest sequence on the rcv list */
18638 	h_buf = list_get_first(&vport->rcv_buffer_list,
18639 			       struct lpfc_dmabuf, list);
18640 	if (!h_buf)
18641 		return;
18642 	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18643 	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18644 }
18645 
18646 /**
18647  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18648  * @vport: The vport that the received sequences were sent to.
18649  *
18650  * This function cleans up all outstanding received sequences. This is called
18651  * by the driver when a link event or user action invalidates all the received
18652  * sequences.
18653  **/
18654 void
lpfc_cleanup_rcv_buffers(struct lpfc_vport * vport)18655 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18656 {
18657 	struct lpfc_dmabuf *h_buf, *hnext;
18658 	struct lpfc_dmabuf *d_buf, *dnext;
18659 	struct hbq_dmabuf *dmabuf = NULL;
18660 
18661 	/* start with the oldest sequence on the rcv list */
18662 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18663 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18664 		list_del_init(&dmabuf->hbuf.list);
18665 		list_for_each_entry_safe(d_buf, dnext,
18666 					 &dmabuf->dbuf.list, list) {
18667 			list_del_init(&d_buf->list);
18668 			lpfc_in_buf_free(vport->phba, d_buf);
18669 		}
18670 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18671 	}
18672 }
18673 
18674 /**
18675  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18676  * @vport: The vport that the received sequences were sent to.
18677  *
18678  * This function determines whether any received sequences have timed out by
18679  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18680  * indicates that there is at least one timed out sequence this routine will
18681  * go through the received sequences one at a time from most inactive to most
18682  * active to determine which ones need to be cleaned up. Once it has determined
18683  * that a sequence needs to be cleaned up it will simply free up the resources
18684  * without sending an abort.
18685  **/
18686 void
lpfc_rcv_seq_check_edtov(struct lpfc_vport * vport)18687 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18688 {
18689 	struct lpfc_dmabuf *h_buf, *hnext;
18690 	struct lpfc_dmabuf *d_buf, *dnext;
18691 	struct hbq_dmabuf *dmabuf = NULL;
18692 	unsigned long timeout;
18693 	int abort_count = 0;
18694 
18695 	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18696 		   vport->rcv_buffer_time_stamp);
18697 	if (list_empty(&vport->rcv_buffer_list) ||
18698 	    time_before(jiffies, timeout))
18699 		return;
18700 	/* start with the oldest sequence on the rcv list */
18701 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18702 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18703 		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18704 			   dmabuf->time_stamp);
18705 		if (time_before(jiffies, timeout))
18706 			break;
18707 		abort_count++;
18708 		list_del_init(&dmabuf->hbuf.list);
18709 		list_for_each_entry_safe(d_buf, dnext,
18710 					 &dmabuf->dbuf.list, list) {
18711 			list_del_init(&d_buf->list);
18712 			lpfc_in_buf_free(vport->phba, d_buf);
18713 		}
18714 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18715 	}
18716 	if (abort_count)
18717 		lpfc_update_rcv_time_stamp(vport);
18718 }
18719 
18720 /**
18721  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
18722  * @vport: pointer to a vitural port
18723  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18724  *
18725  * This function searches through the existing incomplete sequences that have
18726  * been sent to this @vport. If the frame matches one of the incomplete
18727  * sequences then the dbuf in the @dmabuf is added to the list of frames that
18728  * make up that sequence. If no sequence is found that matches this frame then
18729  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18730  * This function returns a pointer to the first dmabuf in the sequence list that
18731  * the frame was linked to.
18732  **/
18733 static struct hbq_dmabuf *
lpfc_fc_frame_add(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18734 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18735 {
18736 	struct fc_frame_header *new_hdr;
18737 	struct fc_frame_header *temp_hdr;
18738 	struct lpfc_dmabuf *d_buf;
18739 	struct lpfc_dmabuf *h_buf;
18740 	struct hbq_dmabuf *seq_dmabuf = NULL;
18741 	struct hbq_dmabuf *temp_dmabuf = NULL;
18742 	uint8_t	found = 0;
18743 
18744 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18745 	dmabuf->time_stamp = jiffies;
18746 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18747 
18748 	/* Use the hdr_buf to find the sequence that this frame belongs to */
18749 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18750 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18751 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18752 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18753 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18754 			continue;
18755 		/* found a pending sequence that matches this frame */
18756 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18757 		break;
18758 	}
18759 	if (!seq_dmabuf) {
18760 		/*
18761 		 * This indicates first frame received for this sequence.
18762 		 * Queue the buffer on the vport's rcv_buffer_list.
18763 		 */
18764 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18765 		lpfc_update_rcv_time_stamp(vport);
18766 		return dmabuf;
18767 	}
18768 	temp_hdr = seq_dmabuf->hbuf.virt;
18769 	if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18770 		be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18771 		list_del_init(&seq_dmabuf->hbuf.list);
18772 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18773 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18774 		lpfc_update_rcv_time_stamp(vport);
18775 		return dmabuf;
18776 	}
18777 	/* move this sequence to the tail to indicate a young sequence */
18778 	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18779 	seq_dmabuf->time_stamp = jiffies;
18780 	lpfc_update_rcv_time_stamp(vport);
18781 	if (list_empty(&seq_dmabuf->dbuf.list)) {
18782 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18783 		return seq_dmabuf;
18784 	}
18785 	/* find the correct place in the sequence to insert this frame */
18786 	d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18787 	while (!found) {
18788 		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18789 		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18790 		/*
18791 		 * If the frame's sequence count is greater than the frame on
18792 		 * the list then insert the frame right after this frame
18793 		 */
18794 		if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18795 			be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18796 			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
18797 			found = 1;
18798 			break;
18799 		}
18800 
18801 		if (&d_buf->list == &seq_dmabuf->dbuf.list)
18802 			break;
18803 		d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
18804 	}
18805 
18806 	if (found)
18807 		return seq_dmabuf;
18808 	return NULL;
18809 }
18810 
18811 /**
18812  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18813  * @vport: pointer to a vitural port
18814  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18815  *
18816  * This function tries to abort from the partially assembed sequence, described
18817  * by the information from basic abbort @dmabuf. It checks to see whether such
18818  * partially assembled sequence held by the driver. If so, it shall free up all
18819  * the frames from the partially assembled sequence.
18820  *
18821  * Return
18822  * true  -- if there is matching partially assembled sequence present and all
18823  *          the frames freed with the sequence;
18824  * false -- if there is no matching partially assembled sequence present so
18825  *          nothing got aborted in the lower layer driver
18826  **/
18827 static bool
lpfc_sli4_abort_partial_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18828 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18829 			    struct hbq_dmabuf *dmabuf)
18830 {
18831 	struct fc_frame_header *new_hdr;
18832 	struct fc_frame_header *temp_hdr;
18833 	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18834 	struct hbq_dmabuf *seq_dmabuf = NULL;
18835 
18836 	/* Use the hdr_buf to find the sequence that matches this frame */
18837 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18838 	INIT_LIST_HEAD(&dmabuf->hbuf.list);
18839 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18840 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18841 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18842 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18843 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18844 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18845 			continue;
18846 		/* found a pending sequence that matches this frame */
18847 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18848 		break;
18849 	}
18850 
18851 	/* Free up all the frames from the partially assembled sequence */
18852 	if (seq_dmabuf) {
18853 		list_for_each_entry_safe(d_buf, n_buf,
18854 					 &seq_dmabuf->dbuf.list, list) {
18855 			list_del_init(&d_buf->list);
18856 			lpfc_in_buf_free(vport->phba, d_buf);
18857 		}
18858 		return true;
18859 	}
18860 	return false;
18861 }
18862 
18863 /**
18864  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18865  * @vport: pointer to a vitural port
18866  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18867  *
18868  * This function tries to abort from the assembed sequence from upper level
18869  * protocol, described by the information from basic abbort @dmabuf. It
18870  * checks to see whether such pending context exists at upper level protocol.
18871  * If so, it shall clean up the pending context.
18872  *
18873  * Return
18874  * true  -- if there is matching pending context of the sequence cleaned
18875  *          at ulp;
18876  * false -- if there is no matching pending context of the sequence present
18877  *          at ulp.
18878  **/
18879 static bool
lpfc_sli4_abort_ulp_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18880 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18881 {
18882 	struct lpfc_hba *phba = vport->phba;
18883 	int handled;
18884 
18885 	/* Accepting abort at ulp with SLI4 only */
18886 	if (phba->sli_rev < LPFC_SLI_REV4)
18887 		return false;
18888 
18889 	/* Register all caring upper level protocols to attend abort */
18890 	handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18891 	if (handled)
18892 		return true;
18893 
18894 	return false;
18895 }
18896 
18897 /**
18898  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18899  * @phba: Pointer to HBA context object.
18900  * @cmd_iocbq: pointer to the command iocbq structure.
18901  * @rsp_iocbq: pointer to the response iocbq structure.
18902  *
18903  * This function handles the sequence abort response iocb command complete
18904  * event. It properly releases the memory allocated to the sequence abort
18905  * accept iocb.
18906  **/
18907 static void
lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmd_iocbq,struct lpfc_iocbq * rsp_iocbq)18908 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18909 			     struct lpfc_iocbq *cmd_iocbq,
18910 			     struct lpfc_iocbq *rsp_iocbq)
18911 {
18912 	if (cmd_iocbq) {
18913 		lpfc_nlp_put(cmd_iocbq->ndlp);
18914 		lpfc_sli_release_iocbq(phba, cmd_iocbq);
18915 	}
18916 
18917 	/* Failure means BLS ABORT RSP did not get delivered to remote node*/
18918 	if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18919 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18920 			"3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18921 			get_job_ulpstatus(phba, rsp_iocbq),
18922 			get_job_word4(phba, rsp_iocbq));
18923 }
18924 
18925 /**
18926  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18927  * @phba: Pointer to HBA context object.
18928  * @xri: xri id in transaction.
18929  *
18930  * This function validates the xri maps to the known range of XRIs allocated an
18931  * used by the driver.
18932  **/
18933 uint16_t
lpfc_sli4_xri_inrange(struct lpfc_hba * phba,uint16_t xri)18934 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18935 		      uint16_t xri)
18936 {
18937 	uint16_t i;
18938 
18939 	for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18940 		if (xri == phba->sli4_hba.xri_ids[i])
18941 			return i;
18942 	}
18943 	return NO_XRI;
18944 }
18945 
18946 /**
18947  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
18948  * @vport: pointer to a virtual port.
18949  * @fc_hdr: pointer to a FC frame header.
18950  * @aborted: was the partially assembled receive sequence successfully aborted
18951  *
18952  * This function sends a basic response to a previous unsol sequence abort
18953  * event after aborting the sequence handling.
18954  **/
18955 void
lpfc_sli4_seq_abort_rsp(struct lpfc_vport * vport,struct fc_frame_header * fc_hdr,bool aborted)18956 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18957 			struct fc_frame_header *fc_hdr, bool aborted)
18958 {
18959 	struct lpfc_hba *phba = vport->phba;
18960 	struct lpfc_iocbq *ctiocb = NULL;
18961 	struct lpfc_nodelist *ndlp;
18962 	uint16_t oxid, rxid, xri, lxri;
18963 	uint32_t sid, fctl;
18964 	union lpfc_wqe128 *icmd;
18965 	int rc;
18966 
18967 	if (!lpfc_is_link_up(phba))
18968 		return;
18969 
18970 	sid = sli4_sid_from_fc_hdr(fc_hdr);
18971 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
18972 	rxid = be16_to_cpu(fc_hdr->fh_rx_id);
18973 
18974 	ndlp = lpfc_findnode_did(vport, sid);
18975 	if (!ndlp) {
18976 		ndlp = lpfc_nlp_init(vport, sid);
18977 		if (!ndlp) {
18978 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18979 					 "1268 Failed to allocate ndlp for "
18980 					 "oxid:x%x SID:x%x\n", oxid, sid);
18981 			return;
18982 		}
18983 		/* Put ndlp onto vport node list */
18984 		lpfc_enqueue_node(vport, ndlp);
18985 	}
18986 
18987 	/* Allocate buffer for rsp iocb */
18988 	ctiocb = lpfc_sli_get_iocbq(phba);
18989 	if (!ctiocb)
18990 		return;
18991 
18992 	icmd = &ctiocb->wqe;
18993 
18994 	/* Extract the F_CTL field from FC_HDR */
18995 	fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18996 
18997 	ctiocb->ndlp = lpfc_nlp_get(ndlp);
18998 	if (!ctiocb->ndlp) {
18999 		lpfc_sli_release_iocbq(phba, ctiocb);
19000 		return;
19001 	}
19002 
19003 	ctiocb->vport = vport;
19004 	ctiocb->cmd_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
19005 	ctiocb->sli4_lxritag = NO_XRI;
19006 	ctiocb->sli4_xritag = NO_XRI;
19007 	ctiocb->abort_rctl = FC_RCTL_BA_ACC;
19008 
19009 	if (fctl & FC_FC_EX_CTX)
19010 		/* Exchange responder sent the abort so we
19011 		 * own the oxid.
19012 		 */
19013 		xri = oxid;
19014 	else
19015 		xri = rxid;
19016 	lxri = lpfc_sli4_xri_inrange(phba, xri);
19017 	if (lxri != NO_XRI)
19018 		lpfc_set_rrq_active(phba, ndlp, lxri,
19019 			(xri == oxid) ? rxid : oxid, 0);
19020 	/* For BA_ABTS from exchange responder, if the logical xri with
19021 	 * the oxid maps to the FCP XRI range, the port no longer has
19022 	 * that exchange context, send a BLS_RJT. Override the IOCB for
19023 	 * a BA_RJT.
19024 	 */
19025 	if ((fctl & FC_FC_EX_CTX) &&
19026 	    (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
19027 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19028 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19029 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19030 		       FC_BA_RJT_INV_XID);
19031 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19032 		       FC_BA_RJT_UNABLE);
19033 	}
19034 
19035 	/* If BA_ABTS failed to abort a partially assembled receive sequence,
19036 	 * the driver no longer has that exchange, send a BLS_RJT. Override
19037 	 * the IOCB for a BA_RJT.
19038 	 */
19039 	if (aborted == false) {
19040 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19041 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19042 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19043 		       FC_BA_RJT_INV_XID);
19044 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19045 		       FC_BA_RJT_UNABLE);
19046 	}
19047 
19048 	if (fctl & FC_FC_EX_CTX) {
19049 		/* ABTS sent by responder to CT exchange, construction
19050 		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
19051 		 * field and RX_ID from ABTS for RX_ID field.
19052 		 */
19053 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_RSP;
19054 		bf_set(xmit_bls_rsp64_rxid, &icmd->xmit_bls_rsp, rxid);
19055 	} else {
19056 		/* ABTS sent by initiator to CT exchange, construction
19057 		 * of BA_ACC will need to allocate a new XRI as for the
19058 		 * XRI_TAG field.
19059 		 */
19060 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_INT;
19061 	}
19062 
19063 	/* OX_ID is invariable to who sent ABTS to CT exchange */
19064 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, oxid);
19065 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, rxid);
19066 
19067 	/* Use CT=VPI */
19068 	bf_set(wqe_els_did, &icmd->xmit_bls_rsp.wqe_dest,
19069 	       ndlp->nlp_DID);
19070 	bf_set(xmit_bls_rsp64_temprpi, &icmd->xmit_bls_rsp,
19071 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
19072 	bf_set(wqe_cmnd, &icmd->generic.wqe_com, CMD_XMIT_BLS_RSP64_CX);
19073 
19074 	/* Xmit CT abts response on exchange <xid> */
19075 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
19076 			 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
19077 			 ctiocb->abort_rctl, oxid, phba->link_state);
19078 
19079 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
19080 	if (rc == IOCB_ERROR) {
19081 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19082 				 "2925 Failed to issue CT ABTS RSP x%x on "
19083 				 "xri x%x, Data x%x\n",
19084 				 ctiocb->abort_rctl, oxid,
19085 				 phba->link_state);
19086 		lpfc_nlp_put(ndlp);
19087 		ctiocb->ndlp = NULL;
19088 		lpfc_sli_release_iocbq(phba, ctiocb);
19089 	}
19090 
19091 	/* if only usage of this nodelist is BLS response, release initial ref
19092 	 * to free ndlp when transmit completes
19093 	 */
19094 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE &&
19095 	    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
19096 	    !(ndlp->fc4_xpt_flags & (NVME_XPT_REGD | SCSI_XPT_REGD))) {
19097 		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
19098 		lpfc_nlp_put(ndlp);
19099 	}
19100 }
19101 
19102 /**
19103  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
19104  * @vport: Pointer to the vport on which this sequence was received
19105  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19106  *
19107  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
19108  * receive sequence is only partially assembed by the driver, it shall abort
19109  * the partially assembled frames for the sequence. Otherwise, if the
19110  * unsolicited receive sequence has been completely assembled and passed to
19111  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
19112  * unsolicited sequence has been aborted. After that, it will issue a basic
19113  * accept to accept the abort.
19114  **/
19115 static void
lpfc_sli4_handle_unsol_abort(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19116 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
19117 			     struct hbq_dmabuf *dmabuf)
19118 {
19119 	struct lpfc_hba *phba = vport->phba;
19120 	struct fc_frame_header fc_hdr;
19121 	uint32_t fctl;
19122 	bool aborted;
19123 
19124 	/* Make a copy of fc_hdr before the dmabuf being released */
19125 	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
19126 	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
19127 
19128 	if (fctl & FC_FC_EX_CTX) {
19129 		/* ABTS by responder to exchange, no cleanup needed */
19130 		aborted = true;
19131 	} else {
19132 		/* ABTS by initiator to exchange, need to do cleanup */
19133 		aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19134 		if (aborted == false)
19135 			aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
19136 	}
19137 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19138 
19139 	if (phba->nvmet_support) {
19140 		lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19141 		return;
19142 	}
19143 
19144 	/* Respond with BA_ACC or BA_RJT accordingly */
19145 	lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
19146 }
19147 
19148 /**
19149  * lpfc_seq_complete - Indicates if a sequence is complete
19150  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19151  *
19152  * This function checks the sequence, starting with the frame described by
19153  * @dmabuf, to see if all the frames associated with this sequence are present.
19154  * the frames associated with this sequence are linked to the @dmabuf using the
19155  * dbuf list. This function looks for two major things. 1) That the first frame
19156  * has a sequence count of zero. 2) There is a frame with last frame of sequence
19157  * set. 3) That there are no holes in the sequence count. The function will
19158  * return 1 when the sequence is complete, otherwise it will return 0.
19159  **/
19160 static int
lpfc_seq_complete(struct hbq_dmabuf * dmabuf)19161 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19162 {
19163 	struct fc_frame_header *hdr;
19164 	struct lpfc_dmabuf *d_buf;
19165 	struct hbq_dmabuf *seq_dmabuf;
19166 	uint32_t fctl;
19167 	int seq_count = 0;
19168 
19169 	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19170 	/* make sure first fame of sequence has a sequence count of zero */
19171 	if (hdr->fh_seq_cnt != seq_count)
19172 		return 0;
19173 	fctl = (hdr->fh_f_ctl[0] << 16 |
19174 		hdr->fh_f_ctl[1] << 8 |
19175 		hdr->fh_f_ctl[2]);
19176 	/* If last frame of sequence we can return success. */
19177 	if (fctl & FC_FC_END_SEQ)
19178 		return 1;
19179 	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19180 		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19181 		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19182 		/* If there is a hole in the sequence count then fail. */
19183 		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
19184 			return 0;
19185 		fctl = (hdr->fh_f_ctl[0] << 16 |
19186 			hdr->fh_f_ctl[1] << 8 |
19187 			hdr->fh_f_ctl[2]);
19188 		/* If last frame of sequence we can return success. */
19189 		if (fctl & FC_FC_END_SEQ)
19190 			return 1;
19191 	}
19192 	return 0;
19193 }
19194 
19195 /**
19196  * lpfc_prep_seq - Prep sequence for ULP processing
19197  * @vport: Pointer to the vport on which this sequence was received
19198  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
19199  *
19200  * This function takes a sequence, described by a list of frames, and creates
19201  * a list of iocbq structures to describe the sequence. This iocbq list will be
19202  * used to issue to the generic unsolicited sequence handler. This routine
19203  * returns a pointer to the first iocbq in the list. If the function is unable
19204  * to allocate an iocbq then it throw out the received frames that were not
19205  * able to be described and return a pointer to the first iocbq. If unable to
19206  * allocate any iocbqs (including the first) this function will return NULL.
19207  **/
19208 static struct lpfc_iocbq *
lpfc_prep_seq(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19209 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19210 {
19211 	struct hbq_dmabuf *hbq_buf;
19212 	struct lpfc_dmabuf *d_buf, *n_buf;
19213 	struct lpfc_iocbq *first_iocbq, *iocbq;
19214 	struct fc_frame_header *fc_hdr;
19215 	uint32_t sid;
19216 	uint32_t len, tot_len;
19217 
19218 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19219 	/* remove from receive buffer list */
19220 	list_del_init(&seq_dmabuf->hbuf.list);
19221 	lpfc_update_rcv_time_stamp(vport);
19222 	/* get the Remote Port's SID */
19223 	sid = sli4_sid_from_fc_hdr(fc_hdr);
19224 	tot_len = 0;
19225 	/* Get an iocbq struct to fill in. */
19226 	first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19227 	if (first_iocbq) {
19228 		/* Initialize the first IOCB. */
19229 		first_iocbq->wcqe_cmpl.total_data_placed = 0;
19230 		bf_set(lpfc_wcqe_c_status, &first_iocbq->wcqe_cmpl,
19231 		       IOSTAT_SUCCESS);
19232 		first_iocbq->vport = vport;
19233 
19234 		/* Check FC Header to see what TYPE of frame we are rcv'ing */
19235 		if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19236 			bf_set(els_rsp64_sid, &first_iocbq->wqe.xmit_els_rsp,
19237 			       sli4_did_from_fc_hdr(fc_hdr));
19238 		}
19239 
19240 		bf_set(wqe_ctxt_tag, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19241 		       NO_XRI);
19242 		bf_set(wqe_rcvoxid, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19243 		       be16_to_cpu(fc_hdr->fh_ox_id));
19244 
19245 		/* put the first buffer into the first iocb */
19246 		tot_len = bf_get(lpfc_rcqe_length,
19247 				 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19248 
19249 		first_iocbq->cmd_dmabuf = &seq_dmabuf->dbuf;
19250 		first_iocbq->bpl_dmabuf = NULL;
19251 		/* Keep track of the BDE count */
19252 		first_iocbq->wcqe_cmpl.word3 = 1;
19253 
19254 		if (tot_len > LPFC_DATA_BUF_SIZE)
19255 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize =
19256 				LPFC_DATA_BUF_SIZE;
19257 		else
19258 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize = tot_len;
19259 
19260 		first_iocbq->wcqe_cmpl.total_data_placed = tot_len;
19261 		bf_set(wqe_els_did, &first_iocbq->wqe.xmit_els_rsp.wqe_dest,
19262 		       sid);
19263 	}
19264 	iocbq = first_iocbq;
19265 	/*
19266 	 * Each IOCBq can have two Buffers assigned, so go through the list
19267 	 * of buffers for this sequence and save two buffers in each IOCBq
19268 	 */
19269 	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19270 		if (!iocbq) {
19271 			lpfc_in_buf_free(vport->phba, d_buf);
19272 			continue;
19273 		}
19274 		if (!iocbq->bpl_dmabuf) {
19275 			iocbq->bpl_dmabuf = d_buf;
19276 			iocbq->wcqe_cmpl.word3++;
19277 			/* We need to get the size out of the right CQE */
19278 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19279 			len = bf_get(lpfc_rcqe_length,
19280 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19281 			iocbq->unsol_rcv_len = len;
19282 			iocbq->wcqe_cmpl.total_data_placed += len;
19283 			tot_len += len;
19284 		} else {
19285 			iocbq = lpfc_sli_get_iocbq(vport->phba);
19286 			if (!iocbq) {
19287 				if (first_iocbq) {
19288 					bf_set(lpfc_wcqe_c_status,
19289 					       &first_iocbq->wcqe_cmpl,
19290 					       IOSTAT_SUCCESS);
19291 					first_iocbq->wcqe_cmpl.parameter =
19292 						IOERR_NO_RESOURCES;
19293 				}
19294 				lpfc_in_buf_free(vport->phba, d_buf);
19295 				continue;
19296 			}
19297 			/* We need to get the size out of the right CQE */
19298 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19299 			len = bf_get(lpfc_rcqe_length,
19300 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19301 			iocbq->cmd_dmabuf = d_buf;
19302 			iocbq->bpl_dmabuf = NULL;
19303 			iocbq->wcqe_cmpl.word3 = 1;
19304 
19305 			if (len > LPFC_DATA_BUF_SIZE)
19306 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19307 					LPFC_DATA_BUF_SIZE;
19308 			else
19309 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19310 					len;
19311 
19312 			tot_len += len;
19313 			iocbq->wcqe_cmpl.total_data_placed = tot_len;
19314 			bf_set(wqe_els_did, &iocbq->wqe.xmit_els_rsp.wqe_dest,
19315 			       sid);
19316 			list_add_tail(&iocbq->list, &first_iocbq->list);
19317 		}
19318 	}
19319 	/* Free the sequence's header buffer */
19320 	if (!first_iocbq)
19321 		lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19322 
19323 	return first_iocbq;
19324 }
19325 
19326 static void
lpfc_sli4_send_seq_to_ulp(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19327 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19328 			  struct hbq_dmabuf *seq_dmabuf)
19329 {
19330 	struct fc_frame_header *fc_hdr;
19331 	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19332 	struct lpfc_hba *phba = vport->phba;
19333 
19334 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19335 	iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19336 	if (!iocbq) {
19337 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19338 				"2707 Ring %d handler: Failed to allocate "
19339 				"iocb Rctl x%x Type x%x received\n",
19340 				LPFC_ELS_RING,
19341 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19342 		return;
19343 	}
19344 	if (!lpfc_complete_unsol_iocb(phba,
19345 				      phba->sli4_hba.els_wq->pring,
19346 				      iocbq, fc_hdr->fh_r_ctl,
19347 				      fc_hdr->fh_type)) {
19348 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19349 				"2540 Ring %d handler: unexpected Rctl "
19350 				"x%x Type x%x received\n",
19351 				LPFC_ELS_RING,
19352 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19353 		lpfc_in_buf_free(phba, &seq_dmabuf->dbuf);
19354 	}
19355 
19356 	/* Free iocb created in lpfc_prep_seq */
19357 	list_for_each_entry_safe(curr_iocb, next_iocb,
19358 				 &iocbq->list, list) {
19359 		list_del_init(&curr_iocb->list);
19360 		lpfc_sli_release_iocbq(phba, curr_iocb);
19361 	}
19362 	lpfc_sli_release_iocbq(phba, iocbq);
19363 }
19364 
19365 static void
lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)19366 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19367 			    struct lpfc_iocbq *rspiocb)
19368 {
19369 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
19370 
19371 	if (pcmd && pcmd->virt)
19372 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19373 	kfree(pcmd);
19374 	lpfc_sli_release_iocbq(phba, cmdiocb);
19375 	lpfc_drain_txq(phba);
19376 }
19377 
19378 static void
lpfc_sli4_handle_mds_loopback(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19379 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19380 			      struct hbq_dmabuf *dmabuf)
19381 {
19382 	struct fc_frame_header *fc_hdr;
19383 	struct lpfc_hba *phba = vport->phba;
19384 	struct lpfc_iocbq *iocbq = NULL;
19385 	union  lpfc_wqe128 *pwqe;
19386 	struct lpfc_dmabuf *pcmd = NULL;
19387 	uint32_t frame_len;
19388 	int rc;
19389 	unsigned long iflags;
19390 
19391 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19392 	frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19393 
19394 	/* Send the received frame back */
19395 	iocbq = lpfc_sli_get_iocbq(phba);
19396 	if (!iocbq) {
19397 		/* Queue cq event and wakeup worker thread to process it */
19398 		spin_lock_irqsave(&phba->hbalock, iflags);
19399 		list_add_tail(&dmabuf->cq_event.list,
19400 			      &phba->sli4_hba.sp_queue_event);
19401 		spin_unlock_irqrestore(&phba->hbalock, iflags);
19402 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
19403 		lpfc_worker_wake_up(phba);
19404 		return;
19405 	}
19406 
19407 	/* Allocate buffer for command payload */
19408 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19409 	if (pcmd)
19410 		pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
19411 					    &pcmd->phys);
19412 	if (!pcmd || !pcmd->virt)
19413 		goto exit;
19414 
19415 	INIT_LIST_HEAD(&pcmd->list);
19416 
19417 	/* copyin the payload */
19418 	memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19419 
19420 	iocbq->cmd_dmabuf = pcmd;
19421 	iocbq->vport = vport;
19422 	iocbq->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
19423 	iocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
19424 	iocbq->num_bdes = 0;
19425 
19426 	pwqe = &iocbq->wqe;
19427 	/* fill in BDE's for command */
19428 	pwqe->gen_req.bde.addrHigh = putPaddrHigh(pcmd->phys);
19429 	pwqe->gen_req.bde.addrLow = putPaddrLow(pcmd->phys);
19430 	pwqe->gen_req.bde.tus.f.bdeSize = frame_len;
19431 	pwqe->gen_req.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
19432 
19433 	pwqe->send_frame.frame_len = frame_len;
19434 	pwqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((__be32 *)fc_hdr));
19435 	pwqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((__be32 *)fc_hdr + 1));
19436 	pwqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((__be32 *)fc_hdr + 2));
19437 	pwqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((__be32 *)fc_hdr + 3));
19438 	pwqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((__be32 *)fc_hdr + 4));
19439 	pwqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((__be32 *)fc_hdr + 5));
19440 
19441 	pwqe->generic.wqe_com.word7 = 0;
19442 	pwqe->generic.wqe_com.word10 = 0;
19443 
19444 	bf_set(wqe_cmnd, &pwqe->generic.wqe_com, CMD_SEND_FRAME);
19445 	bf_set(wqe_sof, &pwqe->generic.wqe_com, 0x2E); /* SOF byte */
19446 	bf_set(wqe_eof, &pwqe->generic.wqe_com, 0x41); /* EOF byte */
19447 	bf_set(wqe_lenloc, &pwqe->generic.wqe_com, 1);
19448 	bf_set(wqe_xbl, &pwqe->generic.wqe_com, 1);
19449 	bf_set(wqe_dbde, &pwqe->generic.wqe_com, 1);
19450 	bf_set(wqe_xc, &pwqe->generic.wqe_com, 1);
19451 	bf_set(wqe_cmd_type, &pwqe->generic.wqe_com, 0xA);
19452 	bf_set(wqe_cqid, &pwqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
19453 	bf_set(wqe_xri_tag, &pwqe->generic.wqe_com, iocbq->sli4_xritag);
19454 	bf_set(wqe_reqtag, &pwqe->generic.wqe_com, iocbq->iotag);
19455 	bf_set(wqe_class, &pwqe->generic.wqe_com, CLASS3);
19456 	pwqe->generic.wqe_com.abort_tag = iocbq->iotag;
19457 
19458 	iocbq->cmd_cmpl = lpfc_sli4_mds_loopback_cmpl;
19459 
19460 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19461 	if (rc == IOCB_ERROR)
19462 		goto exit;
19463 
19464 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19465 	return;
19466 
19467 exit:
19468 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19469 			"2023 Unable to process MDS loopback frame\n");
19470 	if (pcmd && pcmd->virt)
19471 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19472 	kfree(pcmd);
19473 	if (iocbq)
19474 		lpfc_sli_release_iocbq(phba, iocbq);
19475 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19476 }
19477 
19478 /**
19479  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19480  * @phba: Pointer to HBA context object.
19481  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
19482  *
19483  * This function is called with no lock held. This function processes all
19484  * the received buffers and gives it to upper layers when a received buffer
19485  * indicates that it is the final frame in the sequence. The interrupt
19486  * service routine processes received buffers at interrupt contexts.
19487  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19488  * appropriate receive function when the final frame in a sequence is received.
19489  **/
19490 void
lpfc_sli4_handle_received_buffer(struct lpfc_hba * phba,struct hbq_dmabuf * dmabuf)19491 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19492 				 struct hbq_dmabuf *dmabuf)
19493 {
19494 	struct hbq_dmabuf *seq_dmabuf;
19495 	struct fc_frame_header *fc_hdr;
19496 	struct lpfc_vport *vport;
19497 	uint32_t fcfi;
19498 	uint32_t did;
19499 
19500 	/* Process each received buffer */
19501 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19502 
19503 	if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19504 	    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19505 		vport = phba->pport;
19506 		/* Handle MDS Loopback frames */
19507 		if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
19508 			lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19509 		else
19510 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19511 		return;
19512 	}
19513 
19514 	/* check to see if this a valid type of frame */
19515 	if (lpfc_fc_frame_check(phba, fc_hdr)) {
19516 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19517 		return;
19518 	}
19519 
19520 	if ((bf_get(lpfc_cqe_code,
19521 		    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19522 		fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19523 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19524 	else
19525 		fcfi = bf_get(lpfc_rcqe_fcf_id,
19526 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19527 
19528 	if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19529 		vport = phba->pport;
19530 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19531 				"2023 MDS Loopback %d bytes\n",
19532 				bf_get(lpfc_rcqe_length,
19533 				       &dmabuf->cq_event.cqe.rcqe_cmpl));
19534 		/* Handle MDS Loopback frames */
19535 		lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19536 		return;
19537 	}
19538 
19539 	/* d_id this frame is directed to */
19540 	did = sli4_did_from_fc_hdr(fc_hdr);
19541 
19542 	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
19543 	if (!vport) {
19544 		/* throw out the frame */
19545 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19546 		return;
19547 	}
19548 
19549 	/* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19550 	if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19551 		(did != Fabric_DID)) {
19552 		/*
19553 		 * Throw out the frame if we are not pt2pt.
19554 		 * The pt2pt protocol allows for discovery frames
19555 		 * to be received without a registered VPI.
19556 		 */
19557 		if (!test_bit(FC_PT2PT, &vport->fc_flag) ||
19558 		    phba->link_state == LPFC_HBA_READY) {
19559 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19560 			return;
19561 		}
19562 	}
19563 
19564 	/* Handle the basic abort sequence (BA_ABTS) event */
19565 	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19566 		lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19567 		return;
19568 	}
19569 
19570 	/* Link this frame */
19571 	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19572 	if (!seq_dmabuf) {
19573 		/* unable to add frame to vport - throw it out */
19574 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19575 		return;
19576 	}
19577 	/* If not last frame in sequence continue processing frames. */
19578 	if (!lpfc_seq_complete(seq_dmabuf))
19579 		return;
19580 
19581 	/* Send the complete sequence to the upper layer protocol */
19582 	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
19583 }
19584 
19585 /**
19586  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19587  * @phba: pointer to lpfc hba data structure.
19588  *
19589  * This routine is invoked to post rpi header templates to the
19590  * HBA consistent with the SLI-4 interface spec.  This routine
19591  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19592  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19593  *
19594  * This routine does not require any locks.  It's usage is expected
19595  * to be driver load or reset recovery when the driver is
19596  * sequential.
19597  *
19598  * Return codes
19599  * 	0 - successful
19600  *      -EIO - The mailbox failed to complete successfully.
19601  * 	When this error occurs, the driver is not guaranteed
19602  *	to have any rpi regions posted to the device and
19603  *	must either attempt to repost the regions or take a
19604  *	fatal error.
19605  **/
19606 int
lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba * phba)19607 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19608 {
19609 	struct lpfc_rpi_hdr *rpi_page;
19610 	uint32_t rc = 0;
19611 	uint16_t lrpi = 0;
19612 
19613 	/* SLI4 ports that support extents do not require RPI headers. */
19614 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19615 		goto exit;
19616 	if (phba->sli4_hba.extents_in_use)
19617 		return -EIO;
19618 
19619 	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
19620 		/*
19621 		 * Assign the rpi headers a physical rpi only if the driver
19622 		 * has not initialized those resources.  A port reset only
19623 		 * needs the headers posted.
19624 		 */
19625 		if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19626 		    LPFC_RPI_RSRC_RDY)
19627 			rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19628 
19629 		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19630 		if (rc != MBX_SUCCESS) {
19631 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19632 					"2008 Error %d posting all rpi "
19633 					"headers\n", rc);
19634 			rc = -EIO;
19635 			break;
19636 		}
19637 	}
19638 
19639  exit:
19640 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19641 	       LPFC_RPI_RSRC_RDY);
19642 	return rc;
19643 }
19644 
19645 /**
19646  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19647  * @phba: pointer to lpfc hba data structure.
19648  * @rpi_page:  pointer to the rpi memory region.
19649  *
19650  * This routine is invoked to post a single rpi header to the
19651  * HBA consistent with the SLI-4 interface spec.  This memory region
19652  * maps up to 64 rpi context regions.
19653  *
19654  * Return codes
19655  * 	0 - successful
19656  * 	-ENOMEM - No available memory
19657  *      -EIO - The mailbox failed to complete successfully.
19658  **/
19659 int
lpfc_sli4_post_rpi_hdr(struct lpfc_hba * phba,struct lpfc_rpi_hdr * rpi_page)19660 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19661 {
19662 	LPFC_MBOXQ_t *mboxq;
19663 	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19664 	uint32_t rc = 0;
19665 	uint32_t shdr_status, shdr_add_status;
19666 	union lpfc_sli4_cfg_shdr *shdr;
19667 
19668 	/* SLI4 ports that support extents do not require RPI headers. */
19669 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19670 		return rc;
19671 	if (phba->sli4_hba.extents_in_use)
19672 		return -EIO;
19673 
19674 	/* The port is notified of the header region via a mailbox command. */
19675 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19676 	if (!mboxq) {
19677 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19678 				"2001 Unable to allocate memory for issuing "
19679 				"SLI_CONFIG_SPECIAL mailbox command\n");
19680 		return -ENOMEM;
19681 	}
19682 
19683 	/* Post all rpi memory regions to the port. */
19684 	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
19685 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19686 			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19687 			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
19688 			 sizeof(struct lpfc_sli4_cfg_mhdr),
19689 			 LPFC_SLI4_MBX_EMBED);
19690 
19691 
19692 	/* Post the physical rpi to the port for this rpi header. */
19693 	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19694 	       rpi_page->start_rpi);
19695 	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19696 	       hdr_tmpl, rpi_page->page_count);
19697 
19698 	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19699 	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
19700 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19701 	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19702 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19703 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19704 	mempool_free(mboxq, phba->mbox_mem_pool);
19705 	if (shdr_status || shdr_add_status || rc) {
19706 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19707 				"2514 POST_RPI_HDR mailbox failed with "
19708 				"status x%x add_status x%x, mbx status x%x\n",
19709 				shdr_status, shdr_add_status, rc);
19710 		rc = -ENXIO;
19711 	} else {
19712 		/*
19713 		 * The next_rpi stores the next logical module-64 rpi value used
19714 		 * to post physical rpis in subsequent rpi postings.
19715 		 */
19716 		spin_lock_irq(&phba->hbalock);
19717 		phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19718 		spin_unlock_irq(&phba->hbalock);
19719 	}
19720 	return rc;
19721 }
19722 
19723 /**
19724  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19725  * @phba: pointer to lpfc hba data structure.
19726  *
19727  * This routine is invoked to post rpi header templates to the
19728  * HBA consistent with the SLI-4 interface spec.  This routine
19729  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19730  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19731  *
19732  * Returns
19733  * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
19734  * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.
19735  **/
19736 int
lpfc_sli4_alloc_rpi(struct lpfc_hba * phba)19737 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19738 {
19739 	unsigned long rpi;
19740 	uint16_t max_rpi, rpi_limit;
19741 	uint16_t rpi_remaining, lrpi = 0;
19742 	struct lpfc_rpi_hdr *rpi_hdr;
19743 	unsigned long iflag;
19744 
19745 	/*
19746 	 * Fetch the next logical rpi.  Because this index is logical,
19747 	 * the  driver starts at 0 each time.
19748 	 */
19749 	spin_lock_irqsave(&phba->hbalock, iflag);
19750 	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19751 	rpi_limit = phba->sli4_hba.next_rpi;
19752 
19753 	rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);
19754 	if (rpi >= rpi_limit)
19755 		rpi = LPFC_RPI_ALLOC_ERROR;
19756 	else {
19757 		set_bit(rpi, phba->sli4_hba.rpi_bmask);
19758 		phba->sli4_hba.max_cfg_param.rpi_used++;
19759 		phba->sli4_hba.rpi_count++;
19760 	}
19761 	lpfc_printf_log(phba, KERN_INFO,
19762 			LOG_NODE | LOG_DISCOVERY,
19763 			"0001 Allocated rpi:x%x max:x%x lim:x%x\n",
19764 			(int) rpi, max_rpi, rpi_limit);
19765 
19766 	/*
19767 	 * Don't try to allocate more rpi header regions if the device limit
19768 	 * has been exhausted.
19769 	 */
19770 	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19771 	    (phba->sli4_hba.rpi_count >= max_rpi)) {
19772 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19773 		return rpi;
19774 	}
19775 
19776 	/*
19777 	 * RPI header postings are not required for SLI4 ports capable of
19778 	 * extents.
19779 	 */
19780 	if (!phba->sli4_hba.rpi_hdrs_in_use) {
19781 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19782 		return rpi;
19783 	}
19784 
19785 	/*
19786 	 * If the driver is running low on rpi resources, allocate another
19787 	 * page now.  Note that the next_rpi value is used because
19788 	 * it represents how many are actually in use whereas max_rpi notes
19789 	 * how many are supported max by the device.
19790 	 */
19791 	rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
19792 	spin_unlock_irqrestore(&phba->hbalock, iflag);
19793 	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19794 		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19795 		if (!rpi_hdr) {
19796 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19797 					"2002 Error Could not grow rpi "
19798 					"count\n");
19799 		} else {
19800 			lrpi = rpi_hdr->start_rpi;
19801 			rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19802 			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19803 		}
19804 	}
19805 
19806 	return rpi;
19807 }
19808 
19809 /**
19810  * __lpfc_sli4_free_rpi - Release an rpi for reuse.
19811  * @phba: pointer to lpfc hba data structure.
19812  * @rpi: rpi to free
19813  *
19814  * This routine is invoked to release an rpi to the pool of
19815  * available rpis maintained by the driver.
19816  **/
19817 static void
__lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19818 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19819 {
19820 	/*
19821 	 * if the rpi value indicates a prior unreg has already
19822 	 * been done, skip the unreg.
19823 	 */
19824 	if (rpi == LPFC_RPI_ALLOC_ERROR)
19825 		return;
19826 
19827 	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19828 		phba->sli4_hba.rpi_count--;
19829 		phba->sli4_hba.max_cfg_param.rpi_used--;
19830 	} else {
19831 		lpfc_printf_log(phba, KERN_INFO,
19832 				LOG_NODE | LOG_DISCOVERY,
19833 				"2016 rpi %x not inuse\n",
19834 				rpi);
19835 	}
19836 }
19837 
19838 /**
19839  * lpfc_sli4_free_rpi - Release an rpi for reuse.
19840  * @phba: pointer to lpfc hba data structure.
19841  * @rpi: rpi to free
19842  *
19843  * This routine is invoked to release an rpi to the pool of
19844  * available rpis maintained by the driver.
19845  **/
19846 void
lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19847 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19848 {
19849 	spin_lock_irq(&phba->hbalock);
19850 	__lpfc_sli4_free_rpi(phba, rpi);
19851 	spin_unlock_irq(&phba->hbalock);
19852 }
19853 
19854 /**
19855  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19856  * @phba: pointer to lpfc hba data structure.
19857  *
19858  * This routine is invoked to remove the memory region that
19859  * provided rpi via a bitmask.
19860  **/
19861 void
lpfc_sli4_remove_rpis(struct lpfc_hba * phba)19862 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19863 {
19864 	kfree(phba->sli4_hba.rpi_bmask);
19865 	kfree(phba->sli4_hba.rpi_ids);
19866 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
19867 }
19868 
19869 /**
19870  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
19871  * @ndlp: pointer to lpfc nodelist data structure.
19872  * @cmpl: completion call-back.
19873  * @iocbq: data to load as mbox ctx_u information
19874  *
19875  * This routine is invoked to remove the memory region that
19876  * provided rpi via a bitmask.
19877  **/
19878 int
lpfc_sli4_resume_rpi(struct lpfc_nodelist * ndlp,void (* cmpl)(struct lpfc_hba *,LPFC_MBOXQ_t *),struct lpfc_iocbq * iocbq)19879 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19880 		     void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *),
19881 		     struct lpfc_iocbq *iocbq)
19882 {
19883 	LPFC_MBOXQ_t *mboxq;
19884 	struct lpfc_hba *phba = ndlp->phba;
19885 	int rc;
19886 
19887 	/* The port is notified of the header region via a mailbox command. */
19888 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19889 	if (!mboxq)
19890 		return -ENOMEM;
19891 
19892 	/* If cmpl assigned, then this nlp_get pairs with
19893 	 * lpfc_mbx_cmpl_resume_rpi.
19894 	 *
19895 	 * Else cmpl is NULL, then this nlp_get pairs with
19896 	 * lpfc_sli_def_mbox_cmpl.
19897 	 */
19898 	if (!lpfc_nlp_get(ndlp)) {
19899 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19900 				"2122 %s: Failed to get nlp ref\n",
19901 				__func__);
19902 		mempool_free(mboxq, phba->mbox_mem_pool);
19903 		return -EIO;
19904 	}
19905 
19906 	/* Post all rpi memory regions to the port. */
19907 	lpfc_resume_rpi(mboxq, ndlp);
19908 	if (cmpl) {
19909 		mboxq->mbox_cmpl = cmpl;
19910 		mboxq->ctx_u.save_iocb = iocbq;
19911 	} else
19912 		mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19913 	mboxq->ctx_ndlp = ndlp;
19914 	mboxq->vport = ndlp->vport;
19915 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19916 	if (rc == MBX_NOT_FINISHED) {
19917 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19918 				"2010 Resume RPI Mailbox failed "
19919 				"status %d, mbxStatus x%x\n", rc,
19920 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19921 		lpfc_nlp_put(ndlp);
19922 		mempool_free(mboxq, phba->mbox_mem_pool);
19923 		return -EIO;
19924 	}
19925 	return 0;
19926 }
19927 
19928 /**
19929  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19930  * @vport: Pointer to the vport for which the vpi is being initialized
19931  *
19932  * This routine is invoked to activate a vpi with the port.
19933  *
19934  * Returns:
19935  *    0 success
19936  *    -Evalue otherwise
19937  **/
19938 int
lpfc_sli4_init_vpi(struct lpfc_vport * vport)19939 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
19940 {
19941 	LPFC_MBOXQ_t *mboxq;
19942 	int rc = 0;
19943 	int retval = MBX_SUCCESS;
19944 	uint32_t mbox_tmo;
19945 	struct lpfc_hba *phba = vport->phba;
19946 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19947 	if (!mboxq)
19948 		return -ENOMEM;
19949 	lpfc_init_vpi(phba, mboxq, vport->vpi);
19950 	mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
19951 	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
19952 	if (rc != MBX_SUCCESS) {
19953 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19954 				"2022 INIT VPI Mailbox failed "
19955 				"status %d, mbxStatus x%x\n", rc,
19956 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19957 		retval = -EIO;
19958 	}
19959 	if (rc != MBX_TIMEOUT)
19960 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
19961 
19962 	return retval;
19963 }
19964 
19965 /**
19966  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19967  * @phba: pointer to lpfc hba data structure.
19968  * @mboxq: Pointer to mailbox object.
19969  *
19970  * This routine is invoked to manually add a single FCF record. The caller
19971  * must pass a completely initialized FCF_Record.  This routine takes
19972  * care of the nonembedded mailbox operations.
19973  **/
19974 static void
lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)19975 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19976 {
19977 	void *virt_addr;
19978 	union lpfc_sli4_cfg_shdr *shdr;
19979 	uint32_t shdr_status, shdr_add_status;
19980 
19981 	virt_addr = mboxq->sge_array->addr[0];
19982 	/* The IOCTL status is embedded in the mailbox subheader. */
19983 	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19984 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19985 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19986 
19987 	if ((shdr_status || shdr_add_status) &&
19988 		(shdr_status != STATUS_FCF_IN_USE))
19989 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19990 			"2558 ADD_FCF_RECORD mailbox failed with "
19991 			"status x%x add_status x%x\n",
19992 			shdr_status, shdr_add_status);
19993 
19994 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
19995 }
19996 
19997 /**
19998  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19999  * @phba: pointer to lpfc hba data structure.
20000  * @fcf_record:  pointer to the initialized fcf record to add.
20001  *
20002  * This routine is invoked to manually add a single FCF record. The caller
20003  * must pass a completely initialized FCF_Record.  This routine takes
20004  * care of the nonembedded mailbox operations.
20005  **/
20006 int
lpfc_sli4_add_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record)20007 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
20008 {
20009 	int rc = 0;
20010 	LPFC_MBOXQ_t *mboxq;
20011 	uint8_t *bytep;
20012 	void *virt_addr;
20013 	struct lpfc_mbx_sge sge;
20014 	uint32_t alloc_len, req_len;
20015 	uint32_t fcfindex;
20016 
20017 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20018 	if (!mboxq) {
20019 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20020 			"2009 Failed to allocate mbox for ADD_FCF cmd\n");
20021 		return -ENOMEM;
20022 	}
20023 
20024 	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
20025 		  sizeof(uint32_t);
20026 
20027 	/* Allocate DMA memory and set up the non-embedded mailbox command */
20028 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
20029 				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
20030 				     req_len, LPFC_SLI4_MBX_NEMBED);
20031 	if (alloc_len < req_len) {
20032 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20033 			"2523 Allocated DMA memory size (x%x) is "
20034 			"less than the requested DMA memory "
20035 			"size (x%x)\n", alloc_len, req_len);
20036 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20037 		return -ENOMEM;
20038 	}
20039 
20040 	/*
20041 	 * Get the first SGE entry from the non-embedded DMA memory.  This
20042 	 * routine only uses a single SGE.
20043 	 */
20044 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
20045 	virt_addr = mboxq->sge_array->addr[0];
20046 	/*
20047 	 * Configure the FCF record for FCFI 0.  This is the driver's
20048 	 * hardcoded default and gets used in nonFIP mode.
20049 	 */
20050 	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
20051 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
20052 	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
20053 
20054 	/*
20055 	 * Copy the fcf_index and the FCF Record Data. The data starts after
20056 	 * the FCoE header plus word10. The data copy needs to be endian
20057 	 * correct.
20058 	 */
20059 	bytep += sizeof(uint32_t);
20060 	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
20061 	mboxq->vport = phba->pport;
20062 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
20063 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20064 	if (rc == MBX_NOT_FINISHED) {
20065 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20066 			"2515 ADD_FCF_RECORD mailbox failed with "
20067 			"status 0x%x\n", rc);
20068 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20069 		rc = -EIO;
20070 	} else
20071 		rc = 0;
20072 
20073 	return rc;
20074 }
20075 
20076 /**
20077  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
20078  * @phba: pointer to lpfc hba data structure.
20079  * @fcf_record:  pointer to the fcf record to write the default data.
20080  * @fcf_index: FCF table entry index.
20081  *
20082  * This routine is invoked to build the driver's default FCF record.  The
20083  * values used are hardcoded.  This routine handles memory initialization.
20084  *
20085  **/
20086 void
lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record,uint16_t fcf_index)20087 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
20088 				struct fcf_record *fcf_record,
20089 				uint16_t fcf_index)
20090 {
20091 	memset(fcf_record, 0, sizeof(struct fcf_record));
20092 	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
20093 	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
20094 	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
20095 	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
20096 	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
20097 	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
20098 	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
20099 	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
20100 	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
20101 	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
20102 	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
20103 	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
20104 	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
20105 	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
20106 	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
20107 	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
20108 		LPFC_FCF_FPMA | LPFC_FCF_SPMA);
20109 	/* Set the VLAN bit map */
20110 	if (phba->valid_vlan) {
20111 		fcf_record->vlan_bitmap[phba->vlan_id / 8]
20112 			= 1 << (phba->vlan_id % 8);
20113 	}
20114 }
20115 
20116 /**
20117  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
20118  * @phba: pointer to lpfc hba data structure.
20119  * @fcf_index: FCF table entry offset.
20120  *
20121  * This routine is invoked to scan the entire FCF table by reading FCF
20122  * record and processing it one at a time starting from the @fcf_index
20123  * for initial FCF discovery or fast FCF failover rediscovery.
20124  *
20125  * Return 0 if the mailbox command is submitted successfully, none 0
20126  * otherwise.
20127  **/
20128 int
lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20129 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20130 {
20131 	int rc = 0, error;
20132 	LPFC_MBOXQ_t *mboxq;
20133 
20134 	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
20135 	phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
20136 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20137 	if (!mboxq) {
20138 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20139 				"2000 Failed to allocate mbox for "
20140 				"READ_FCF cmd\n");
20141 		error = -ENOMEM;
20142 		goto fail_fcf_scan;
20143 	}
20144 	/* Construct the read FCF record mailbox command */
20145 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20146 	if (rc) {
20147 		error = -EINVAL;
20148 		goto fail_fcf_scan;
20149 	}
20150 	/* Issue the mailbox command asynchronously */
20151 	mboxq->vport = phba->pport;
20152 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
20153 
20154 	set_bit(FCF_TS_INPROG, &phba->hba_flag);
20155 
20156 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20157 	if (rc == MBX_NOT_FINISHED)
20158 		error = -EIO;
20159 	else {
20160 		/* Reset eligible FCF count for new scan */
20161 		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
20162 			phba->fcf.eligible_fcf_cnt = 0;
20163 		error = 0;
20164 	}
20165 fail_fcf_scan:
20166 	if (error) {
20167 		if (mboxq)
20168 			lpfc_sli4_mbox_cmd_free(phba, mboxq);
20169 		/* FCF scan failed, clear FCF_TS_INPROG flag */
20170 		clear_bit(FCF_TS_INPROG, &phba->hba_flag);
20171 	}
20172 	return error;
20173 }
20174 
20175 /**
20176  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
20177  * @phba: pointer to lpfc hba data structure.
20178  * @fcf_index: FCF table entry offset.
20179  *
20180  * This routine is invoked to read an FCF record indicated by @fcf_index
20181  * and to use it for FLOGI roundrobin FCF failover.
20182  *
20183  * Return 0 if the mailbox command is submitted successfully, none 0
20184  * otherwise.
20185  **/
20186 int
lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20187 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20188 {
20189 	int rc = 0, error;
20190 	LPFC_MBOXQ_t *mboxq;
20191 
20192 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20193 	if (!mboxq) {
20194 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20195 				"2763 Failed to allocate mbox for "
20196 				"READ_FCF cmd\n");
20197 		error = -ENOMEM;
20198 		goto fail_fcf_read;
20199 	}
20200 	/* Construct the read FCF record mailbox command */
20201 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20202 	if (rc) {
20203 		error = -EINVAL;
20204 		goto fail_fcf_read;
20205 	}
20206 	/* Issue the mailbox command asynchronously */
20207 	mboxq->vport = phba->pport;
20208 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20209 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20210 	if (rc == MBX_NOT_FINISHED)
20211 		error = -EIO;
20212 	else
20213 		error = 0;
20214 
20215 fail_fcf_read:
20216 	if (error && mboxq)
20217 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20218 	return error;
20219 }
20220 
20221 /**
20222  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20223  * @phba: pointer to lpfc hba data structure.
20224  * @fcf_index: FCF table entry offset.
20225  *
20226  * This routine is invoked to read an FCF record indicated by @fcf_index to
20227  * determine whether it's eligible for FLOGI roundrobin failover list.
20228  *
20229  * Return 0 if the mailbox command is submitted successfully, none 0
20230  * otherwise.
20231  **/
20232 int
lpfc_sli4_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20233 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20234 {
20235 	int rc = 0, error;
20236 	LPFC_MBOXQ_t *mboxq;
20237 
20238 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20239 	if (!mboxq) {
20240 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20241 				"2758 Failed to allocate mbox for "
20242 				"READ_FCF cmd\n");
20243 				error = -ENOMEM;
20244 				goto fail_fcf_read;
20245 	}
20246 	/* Construct the read FCF record mailbox command */
20247 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20248 	if (rc) {
20249 		error = -EINVAL;
20250 		goto fail_fcf_read;
20251 	}
20252 	/* Issue the mailbox command asynchronously */
20253 	mboxq->vport = phba->pport;
20254 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20255 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20256 	if (rc == MBX_NOT_FINISHED)
20257 		error = -EIO;
20258 	else
20259 		error = 0;
20260 
20261 fail_fcf_read:
20262 	if (error && mboxq)
20263 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20264 	return error;
20265 }
20266 
20267 /**
20268  * lpfc_check_next_fcf_pri_level
20269  * @phba: pointer to the lpfc_hba struct for this port.
20270  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20271  * routine when the rr_bmask is empty. The FCF indecies are put into the
20272  * rr_bmask based on their priority level. Starting from the highest priority
20273  * to the lowest. The most likely FCF candidate will be in the highest
20274  * priority group. When this routine is called it searches the fcf_pri list for
20275  * next lowest priority group and repopulates the rr_bmask with only those
20276  * fcf_indexes.
20277  * returns:
20278  * 1=success 0=failure
20279  **/
20280 static int
lpfc_check_next_fcf_pri_level(struct lpfc_hba * phba)20281 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20282 {
20283 	uint16_t next_fcf_pri;
20284 	uint16_t last_index;
20285 	struct lpfc_fcf_pri *fcf_pri;
20286 	int rc;
20287 	int ret = 0;
20288 
20289 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20290 			LPFC_SLI4_FCF_TBL_INDX_MAX);
20291 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20292 			"3060 Last IDX %d\n", last_index);
20293 
20294 	/* Verify the priority list has 2 or more entries */
20295 	spin_lock_irq(&phba->hbalock);
20296 	if (list_empty(&phba->fcf.fcf_pri_list) ||
20297 	    list_is_singular(&phba->fcf.fcf_pri_list)) {
20298 		spin_unlock_irq(&phba->hbalock);
20299 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20300 			"3061 Last IDX %d\n", last_index);
20301 		return 0; /* Empty rr list */
20302 	}
20303 	spin_unlock_irq(&phba->hbalock);
20304 
20305 	next_fcf_pri = 0;
20306 	/*
20307 	 * Clear the rr_bmask and set all of the bits that are at this
20308 	 * priority.
20309 	 */
20310 	memset(phba->fcf.fcf_rr_bmask, 0,
20311 			sizeof(*phba->fcf.fcf_rr_bmask));
20312 	spin_lock_irq(&phba->hbalock);
20313 	list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20314 		if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20315 			continue;
20316 		/*
20317 		 * the 1st priority that has not FLOGI failed
20318 		 * will be the highest.
20319 		 */
20320 		if (!next_fcf_pri)
20321 			next_fcf_pri = fcf_pri->fcf_rec.priority;
20322 		spin_unlock_irq(&phba->hbalock);
20323 		if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20324 			rc = lpfc_sli4_fcf_rr_index_set(phba,
20325 						fcf_pri->fcf_rec.fcf_index);
20326 			if (rc)
20327 				return 0;
20328 		}
20329 		spin_lock_irq(&phba->hbalock);
20330 	}
20331 	/*
20332 	 * if next_fcf_pri was not set above and the list is not empty then
20333 	 * we have failed flogis on all of them. So reset flogi failed
20334 	 * and start at the beginning.
20335 	 */
20336 	if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20337 		list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20338 			fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20339 			/*
20340 			 * the 1st priority that has not FLOGI failed
20341 			 * will be the highest.
20342 			 */
20343 			if (!next_fcf_pri)
20344 				next_fcf_pri = fcf_pri->fcf_rec.priority;
20345 			spin_unlock_irq(&phba->hbalock);
20346 			if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20347 				rc = lpfc_sli4_fcf_rr_index_set(phba,
20348 						fcf_pri->fcf_rec.fcf_index);
20349 				if (rc)
20350 					return 0;
20351 			}
20352 			spin_lock_irq(&phba->hbalock);
20353 		}
20354 	} else
20355 		ret = 1;
20356 	spin_unlock_irq(&phba->hbalock);
20357 
20358 	return ret;
20359 }
20360 /**
20361  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20362  * @phba: pointer to lpfc hba data structure.
20363  *
20364  * This routine is to get the next eligible FCF record index in a round
20365  * robin fashion. If the next eligible FCF record index equals to the
20366  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
20367  * shall be returned, otherwise, the next eligible FCF record's index
20368  * shall be returned.
20369  **/
20370 uint16_t
lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba * phba)20371 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20372 {
20373 	uint16_t next_fcf_index;
20374 
20375 initial_priority:
20376 	/* Search start from next bit of currently registered FCF index */
20377 	next_fcf_index = phba->fcf.current_rec.fcf_indx;
20378 
20379 next_priority:
20380 	/* Determine the next fcf index to check */
20381 	next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
20382 	next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20383 				       LPFC_SLI4_FCF_TBL_INDX_MAX,
20384 				       next_fcf_index);
20385 
20386 	/* Wrap around condition on phba->fcf.fcf_rr_bmask */
20387 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20388 		/*
20389 		 * If we have wrapped then we need to clear the bits that
20390 		 * have been tested so that we can detect when we should
20391 		 * change the priority level.
20392 		 */
20393 		next_fcf_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20394 					       LPFC_SLI4_FCF_TBL_INDX_MAX);
20395 	}
20396 
20397 
20398 	/* Check roundrobin failover list empty condition */
20399 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20400 		next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20401 		/*
20402 		 * If next fcf index is not found check if there are lower
20403 		 * Priority level fcf's in the fcf_priority list.
20404 		 * Set up the rr_bmask with all of the avaiable fcf bits
20405 		 * at that level and continue the selection process.
20406 		 */
20407 		if (lpfc_check_next_fcf_pri_level(phba))
20408 			goto initial_priority;
20409 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20410 				"2844 No roundrobin failover FCF available\n");
20411 
20412 		return LPFC_FCOE_FCF_NEXT_NONE;
20413 	}
20414 
20415 	if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20416 		phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
20417 		LPFC_FCF_FLOGI_FAILED) {
20418 		if (list_is_singular(&phba->fcf.fcf_pri_list))
20419 			return LPFC_FCOE_FCF_NEXT_NONE;
20420 
20421 		goto next_priority;
20422 	}
20423 
20424 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20425 			"2845 Get next roundrobin failover FCF (x%x)\n",
20426 			next_fcf_index);
20427 
20428 	return next_fcf_index;
20429 }
20430 
20431 /**
20432  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20433  * @phba: pointer to lpfc hba data structure.
20434  * @fcf_index: index into the FCF table to 'set'
20435  *
20436  * This routine sets the FCF record index in to the eligible bmask for
20437  * roundrobin failover search. It checks to make sure that the index
20438  * does not go beyond the range of the driver allocated bmask dimension
20439  * before setting the bit.
20440  *
20441  * Returns 0 if the index bit successfully set, otherwise, it returns
20442  * -EINVAL.
20443  **/
20444 int
lpfc_sli4_fcf_rr_index_set(struct lpfc_hba * phba,uint16_t fcf_index)20445 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20446 {
20447 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20448 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20449 				"2610 FCF (x%x) reached driver's book "
20450 				"keeping dimension:x%x\n",
20451 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20452 		return -EINVAL;
20453 	}
20454 	/* Set the eligible FCF record index bmask */
20455 	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20456 
20457 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20458 			"2790 Set FCF (x%x) to roundrobin FCF failover "
20459 			"bmask\n", fcf_index);
20460 
20461 	return 0;
20462 }
20463 
20464 /**
20465  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
20466  * @phba: pointer to lpfc hba data structure.
20467  * @fcf_index: index into the FCF table to 'clear'
20468  *
20469  * This routine clears the FCF record index from the eligible bmask for
20470  * roundrobin failover search. It checks to make sure that the index
20471  * does not go beyond the range of the driver allocated bmask dimension
20472  * before clearing the bit.
20473  **/
20474 void
lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba * phba,uint16_t fcf_index)20475 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20476 {
20477 	struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
20478 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20479 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20480 				"2762 FCF (x%x) reached driver's book "
20481 				"keeping dimension:x%x\n",
20482 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20483 		return;
20484 	}
20485 	/* Clear the eligible FCF record index bmask */
20486 	spin_lock_irq(&phba->hbalock);
20487 	list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20488 				 list) {
20489 		if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20490 			list_del_init(&fcf_pri->list);
20491 			break;
20492 		}
20493 	}
20494 	spin_unlock_irq(&phba->hbalock);
20495 	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20496 
20497 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20498 			"2791 Clear FCF (x%x) from roundrobin failover "
20499 			"bmask\n", fcf_index);
20500 }
20501 
20502 /**
20503  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20504  * @phba: pointer to lpfc hba data structure.
20505  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
20506  *
20507  * This routine is the completion routine for the rediscover FCF table mailbox
20508  * command. If the mailbox command returned failure, it will try to stop the
20509  * FCF rediscover wait timer.
20510  **/
20511 static void
lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)20512 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20513 {
20514 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20515 	uint32_t shdr_status, shdr_add_status;
20516 
20517 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20518 
20519 	shdr_status = bf_get(lpfc_mbox_hdr_status,
20520 			     &redisc_fcf->header.cfg_shdr.response);
20521 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20522 			     &redisc_fcf->header.cfg_shdr.response);
20523 	if (shdr_status || shdr_add_status) {
20524 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20525 				"2746 Requesting for FCF rediscovery failed "
20526 				"status x%x add_status x%x\n",
20527 				shdr_status, shdr_add_status);
20528 		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
20529 			spin_lock_irq(&phba->hbalock);
20530 			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
20531 			spin_unlock_irq(&phba->hbalock);
20532 			/*
20533 			 * CVL event triggered FCF rediscover request failed,
20534 			 * last resort to re-try current registered FCF entry.
20535 			 */
20536 			lpfc_retry_pport_discovery(phba);
20537 		} else {
20538 			spin_lock_irq(&phba->hbalock);
20539 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
20540 			spin_unlock_irq(&phba->hbalock);
20541 			/*
20542 			 * DEAD FCF event triggered FCF rediscover request
20543 			 * failed, last resort to fail over as a link down
20544 			 * to FCF registration.
20545 			 */
20546 			lpfc_sli4_fcf_dead_failthrough(phba);
20547 		}
20548 	} else {
20549 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20550 				"2775 Start FCF rediscover quiescent timer\n");
20551 		/*
20552 		 * Start FCF rediscovery wait timer for pending FCF
20553 		 * before rescan FCF record table.
20554 		 */
20555 		lpfc_fcf_redisc_wait_start_timer(phba);
20556 	}
20557 
20558 	mempool_free(mbox, phba->mbox_mem_pool);
20559 }
20560 
20561 /**
20562  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
20563  * @phba: pointer to lpfc hba data structure.
20564  *
20565  * This routine is invoked to request for rediscovery of the entire FCF table
20566  * by the port.
20567  **/
20568 int
lpfc_sli4_redisc_fcf_table(struct lpfc_hba * phba)20569 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20570 {
20571 	LPFC_MBOXQ_t *mbox;
20572 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20573 	int rc, length;
20574 
20575 	/* Cancel retry delay timers to all vports before FCF rediscover */
20576 	lpfc_cancel_all_vport_retry_delay_timer(phba);
20577 
20578 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20579 	if (!mbox) {
20580 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20581 				"2745 Failed to allocate mbox for "
20582 				"requesting FCF rediscover.\n");
20583 		return -ENOMEM;
20584 	}
20585 
20586 	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20587 		  sizeof(struct lpfc_sli4_cfg_mhdr));
20588 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20589 			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20590 			 length, LPFC_SLI4_MBX_EMBED);
20591 
20592 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20593 	/* Set count to 0 for invalidating the entire FCF database */
20594 	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20595 
20596 	/* Issue the mailbox command asynchronously */
20597 	mbox->vport = phba->pport;
20598 	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20599 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20600 
20601 	if (rc == MBX_NOT_FINISHED) {
20602 		mempool_free(mbox, phba->mbox_mem_pool);
20603 		return -EIO;
20604 	}
20605 	return 0;
20606 }
20607 
20608 /**
20609  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20610  * @phba: pointer to lpfc hba data structure.
20611  *
20612  * This function is the failover routine as a last resort to the FCF DEAD
20613  * event when driver failed to perform fast FCF failover.
20614  **/
20615 void
lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba * phba)20616 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20617 {
20618 	uint32_t link_state;
20619 
20620 	/*
20621 	 * Last resort as FCF DEAD event failover will treat this as
20622 	 * a link down, but save the link state because we don't want
20623 	 * it to be changed to Link Down unless it is already down.
20624 	 */
20625 	link_state = phba->link_state;
20626 	lpfc_linkdown(phba);
20627 	phba->link_state = link_state;
20628 
20629 	/* Unregister FCF if no devices connected to it */
20630 	lpfc_unregister_unused_fcf(phba);
20631 }
20632 
20633 /**
20634  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
20635  * @phba: pointer to lpfc hba data structure.
20636  * @rgn23_data: pointer to configure region 23 data.
20637  *
20638  * This function gets SLI3 port configure region 23 data through memory dump
20639  * mailbox command. When it successfully retrieves data, the size of the data
20640  * will be returned, otherwise, 0 will be returned.
20641  **/
20642 static uint32_t
lpfc_sli_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20643 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20644 {
20645 	LPFC_MBOXQ_t *pmb = NULL;
20646 	MAILBOX_t *mb;
20647 	uint32_t offset = 0;
20648 	int rc;
20649 
20650 	if (!rgn23_data)
20651 		return 0;
20652 
20653 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20654 	if (!pmb) {
20655 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20656 				"2600 failed to allocate mailbox memory\n");
20657 		return 0;
20658 	}
20659 	mb = &pmb->u.mb;
20660 
20661 	do {
20662 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20663 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20664 
20665 		if (rc != MBX_SUCCESS) {
20666 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20667 					"2601 failed to read config "
20668 					"region 23, rc 0x%x Status 0x%x\n",
20669 					rc, mb->mbxStatus);
20670 			mb->un.varDmp.word_cnt = 0;
20671 		}
20672 		/*
20673 		 * dump mem may return a zero when finished or we got a
20674 		 * mailbox error, either way we are done.
20675 		 */
20676 		if (mb->un.varDmp.word_cnt == 0)
20677 			break;
20678 
20679 		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20680 			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20681 
20682 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
20683 				       rgn23_data + offset,
20684 				       mb->un.varDmp.word_cnt);
20685 		offset += mb->un.varDmp.word_cnt;
20686 	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
20687 
20688 	mempool_free(pmb, phba->mbox_mem_pool);
20689 	return offset;
20690 }
20691 
20692 /**
20693  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20694  * @phba: pointer to lpfc hba data structure.
20695  * @rgn23_data: pointer to configure region 23 data.
20696  *
20697  * This function gets SLI4 port configure region 23 data through memory dump
20698  * mailbox command. When it successfully retrieves data, the size of the data
20699  * will be returned, otherwise, 0 will be returned.
20700  **/
20701 static uint32_t
lpfc_sli4_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20702 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20703 {
20704 	LPFC_MBOXQ_t *mboxq = NULL;
20705 	struct lpfc_dmabuf *mp = NULL;
20706 	struct lpfc_mqe *mqe;
20707 	uint32_t data_length = 0;
20708 	int rc;
20709 
20710 	if (!rgn23_data)
20711 		return 0;
20712 
20713 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20714 	if (!mboxq) {
20715 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20716 				"3105 failed to allocate mailbox memory\n");
20717 		return 0;
20718 	}
20719 
20720 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20721 		goto out;
20722 	mqe = &mboxq->u.mqe;
20723 	mp = mboxq->ctx_buf;
20724 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20725 	if (rc)
20726 		goto out;
20727 	data_length = mqe->un.mb_words[5];
20728 	if (data_length == 0)
20729 		goto out;
20730 	if (data_length > DMP_RGN23_SIZE) {
20731 		data_length = 0;
20732 		goto out;
20733 	}
20734 	lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20735 out:
20736 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
20737 	return data_length;
20738 }
20739 
20740 /**
20741  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20742  * @phba: pointer to lpfc hba data structure.
20743  *
20744  * This function read region 23 and parse TLV for port status to
20745  * decide if the user disaled the port. If the TLV indicates the
20746  * port is disabled, the hba_flag is set accordingly.
20747  **/
20748 void
lpfc_sli_read_link_ste(struct lpfc_hba * phba)20749 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20750 {
20751 	uint8_t *rgn23_data = NULL;
20752 	uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20753 	uint32_t offset = 0;
20754 
20755 	/* Get adapter Region 23 data */
20756 	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20757 	if (!rgn23_data)
20758 		goto out;
20759 
20760 	if (phba->sli_rev < LPFC_SLI_REV4)
20761 		data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20762 	else {
20763 		if_type = bf_get(lpfc_sli_intf_if_type,
20764 				 &phba->sli4_hba.sli_intf);
20765 		if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20766 			goto out;
20767 		data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20768 	}
20769 
20770 	if (!data_size)
20771 		goto out;
20772 
20773 	/* Check the region signature first */
20774 	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
20775 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20776 			"2619 Config region 23 has bad signature\n");
20777 			goto out;
20778 	}
20779 	offset += 4;
20780 
20781 	/* Check the data structure version */
20782 	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
20783 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20784 			"2620 Config region 23 has bad version\n");
20785 		goto out;
20786 	}
20787 	offset += 4;
20788 
20789 	/* Parse TLV entries in the region */
20790 	while (offset < data_size) {
20791 		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20792 			break;
20793 		/*
20794 		 * If the TLV is not driver specific TLV or driver id is
20795 		 * not linux driver id, skip the record.
20796 		 */
20797 		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20798 		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20799 		    (rgn23_data[offset + 3] != 0)) {
20800 			offset += rgn23_data[offset + 1] * 4 + 4;
20801 			continue;
20802 		}
20803 
20804 		/* Driver found a driver specific TLV in the config region */
20805 		sub_tlv_len = rgn23_data[offset + 1] * 4;
20806 		offset += 4;
20807 		tlv_offset = 0;
20808 
20809 		/*
20810 		 * Search for configured port state sub-TLV.
20811 		 */
20812 		while ((offset < data_size) &&
20813 			(tlv_offset < sub_tlv_len)) {
20814 			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20815 				offset += 4;
20816 				tlv_offset += 4;
20817 				break;
20818 			}
20819 			if (rgn23_data[offset] != PORT_STE_TYPE) {
20820 				offset += rgn23_data[offset + 1] * 4 + 4;
20821 				tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20822 				continue;
20823 			}
20824 
20825 			/* This HBA contains PORT_STE configured */
20826 			if (!rgn23_data[offset + 2])
20827 				set_bit(LINK_DISABLED, &phba->hba_flag);
20828 
20829 			goto out;
20830 		}
20831 	}
20832 
20833 out:
20834 	kfree(rgn23_data);
20835 	return;
20836 }
20837 
20838 /**
20839  * lpfc_log_fw_write_cmpl - logs firmware write completion status
20840  * @phba: pointer to lpfc hba data structure
20841  * @shdr_status: wr_object rsp's status field
20842  * @shdr_add_status: wr_object rsp's add_status field
20843  * @shdr_add_status_2: wr_object rsp's add_status_2 field
20844  * @shdr_change_status: wr_object rsp's change_status field
20845  * @shdr_csf: wr_object rsp's csf bit
20846  *
20847  * This routine is intended to be called after a firmware write completes.
20848  * It will log next action items to be performed by the user to instantiate
20849  * the newly downloaded firmware or reason for incompatibility.
20850  **/
20851 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)20852 lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20853 		       u32 shdr_add_status, u32 shdr_add_status_2,
20854 		       u32 shdr_change_status, u32 shdr_csf)
20855 {
20856 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20857 			"4198 %s: flash_id x%02x, asic_rev x%02x, "
20858 			"status x%02x, add_status x%02x, add_status_2 x%02x, "
20859 			"change_status x%02x, csf %01x\n", __func__,
20860 			phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20861 			shdr_status, shdr_add_status, shdr_add_status_2,
20862 			shdr_change_status, shdr_csf);
20863 
20864 	if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20865 		switch (shdr_add_status_2) {
20866 		case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20867 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20868 				     "4199 Firmware write failed: "
20869 				     "image incompatible with flash x%02x\n",
20870 				     phba->sli4_hba.flash_id);
20871 			break;
20872 		case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20873 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20874 				     "4200 Firmware write failed: "
20875 				     "image incompatible with ASIC "
20876 				     "architecture x%02x\n",
20877 				     phba->sli4_hba.asic_rev);
20878 			break;
20879 		default:
20880 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20881 				     "4210 Firmware write failed: "
20882 				     "add_status_2 x%02x\n",
20883 				     shdr_add_status_2);
20884 			break;
20885 		}
20886 	} else if (!shdr_status && !shdr_add_status) {
20887 		if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20888 		    shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20889 			if (shdr_csf)
20890 				shdr_change_status =
20891 						   LPFC_CHANGE_STATUS_PCI_RESET;
20892 		}
20893 
20894 		switch (shdr_change_status) {
20895 		case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20896 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20897 				     "3198 Firmware write complete: System "
20898 				     "reboot required to instantiate\n");
20899 			break;
20900 		case (LPFC_CHANGE_STATUS_FW_RESET):
20901 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20902 				     "3199 Firmware write complete: "
20903 				     "Firmware reset required to "
20904 				     "instantiate\n");
20905 			break;
20906 		case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20907 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20908 				     "3200 Firmware write complete: Port "
20909 				     "Migration or PCI Reset required to "
20910 				     "instantiate\n");
20911 			break;
20912 		case (LPFC_CHANGE_STATUS_PCI_RESET):
20913 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20914 				     "3201 Firmware write complete: PCI "
20915 				     "Reset required to instantiate\n");
20916 			break;
20917 		default:
20918 			break;
20919 		}
20920 	}
20921 }
20922 
20923 /**
20924  * lpfc_wr_object - write an object to the firmware
20925  * @phba: HBA structure that indicates port to create a queue on.
20926  * @dmabuf_list: list of dmabufs to write to the port.
20927  * @size: the total byte value of the objects to write to the port.
20928  * @offset: the current offset to be used to start the transfer.
20929  *
20930  * This routine will create a wr_object mailbox command to send to the port.
20931  * the mailbox command will be constructed using the dma buffers described in
20932  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20933  * BDEs that the imbedded mailbox can support. The @offset variable will be
20934  * used to indicate the starting offset of the transfer and will also return
20935  * the offset after the write object mailbox has completed. @size is used to
20936  * determine the end of the object and whether the eof bit should be set.
20937  *
20938  * Return 0 is successful and offset will contain the new offset to use
20939  * for the next write.
20940  * Return negative value for error cases.
20941  **/
20942 int
lpfc_wr_object(struct lpfc_hba * phba,struct list_head * dmabuf_list,uint32_t size,uint32_t * offset)20943 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20944 	       uint32_t size, uint32_t *offset)
20945 {
20946 	struct lpfc_mbx_wr_object *wr_object;
20947 	LPFC_MBOXQ_t *mbox;
20948 	int rc = 0, i = 0;
20949 	int mbox_status = 0;
20950 	uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20951 	uint32_t shdr_change_status = 0, shdr_csf = 0;
20952 	uint32_t mbox_tmo;
20953 	struct lpfc_dmabuf *dmabuf;
20954 	uint32_t written = 0;
20955 	bool check_change_status = false;
20956 
20957 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20958 	if (!mbox)
20959 		return -ENOMEM;
20960 
20961 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20962 			LPFC_MBOX_OPCODE_WRITE_OBJECT,
20963 			sizeof(struct lpfc_mbx_wr_object) -
20964 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20965 
20966 	wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20967 	wr_object->u.request.write_offset = *offset;
20968 	sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20969 	wr_object->u.request.object_name[0] =
20970 		cpu_to_le32(wr_object->u.request.object_name[0]);
20971 	bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20972 	list_for_each_entry(dmabuf, dmabuf_list, list) {
20973 		if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20974 			break;
20975 		wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20976 		wr_object->u.request.bde[i].addrHigh =
20977 			putPaddrHigh(dmabuf->phys);
20978 		if (written + SLI4_PAGE_SIZE >= size) {
20979 			wr_object->u.request.bde[i].tus.f.bdeSize =
20980 				(size - written);
20981 			written += (size - written);
20982 			bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
20983 			bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20984 			check_change_status = true;
20985 		} else {
20986 			wr_object->u.request.bde[i].tus.f.bdeSize =
20987 				SLI4_PAGE_SIZE;
20988 			written += SLI4_PAGE_SIZE;
20989 		}
20990 		i++;
20991 	}
20992 	wr_object->u.request.bde_count = i;
20993 	bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20994 	if (!phba->sli4_hba.intr_enable)
20995 		mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20996 	else {
20997 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
20998 		mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20999 	}
21000 
21001 	/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */
21002 	rc = mbox_status;
21003 
21004 	/* The IOCTL status is embedded in the mailbox subheader. */
21005 	shdr_status = bf_get(lpfc_mbox_hdr_status,
21006 			     &wr_object->header.cfg_shdr.response);
21007 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
21008 				 &wr_object->header.cfg_shdr.response);
21009 	shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
21010 				   &wr_object->header.cfg_shdr.response);
21011 	if (check_change_status) {
21012 		shdr_change_status = bf_get(lpfc_wr_object_change_status,
21013 					    &wr_object->u.response);
21014 		shdr_csf = bf_get(lpfc_wr_object_csf,
21015 				  &wr_object->u.response);
21016 	}
21017 
21018 	if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
21019 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21020 				"3025 Write Object mailbox failed with "
21021 				"status x%x add_status x%x, add_status_2 x%x, "
21022 				"mbx status x%x\n",
21023 				shdr_status, shdr_add_status, shdr_add_status_2,
21024 				rc);
21025 		rc = -ENXIO;
21026 		*offset = shdr_add_status;
21027 	} else {
21028 		*offset += wr_object->u.response.actual_write_length;
21029 	}
21030 
21031 	if (rc || check_change_status)
21032 		lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
21033 				       shdr_add_status_2, shdr_change_status,
21034 				       shdr_csf);
21035 
21036 	if (!phba->sli4_hba.intr_enable)
21037 		mempool_free(mbox, phba->mbox_mem_pool);
21038 	else if (mbox_status != MBX_TIMEOUT)
21039 		mempool_free(mbox, phba->mbox_mem_pool);
21040 
21041 	return rc;
21042 }
21043 
21044 /**
21045  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
21046  * @vport: pointer to vport data structure.
21047  *
21048  * This function iterate through the mailboxq and clean up all REG_LOGIN
21049  * and REG_VPI mailbox commands associated with the vport. This function
21050  * is called when driver want to restart discovery of the vport due to
21051  * a Clear Virtual Link event.
21052  **/
21053 void
lpfc_cleanup_pending_mbox(struct lpfc_vport * vport)21054 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
21055 {
21056 	struct lpfc_hba *phba = vport->phba;
21057 	LPFC_MBOXQ_t *mb, *nextmb;
21058 	struct lpfc_nodelist *ndlp;
21059 	struct lpfc_nodelist *act_mbx_ndlp = NULL;
21060 	LIST_HEAD(mbox_cmd_list);
21061 	uint8_t restart_loop;
21062 
21063 	/* Clean up internally queued mailbox commands with the vport */
21064 	spin_lock_irq(&phba->hbalock);
21065 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
21066 		if (mb->vport != vport)
21067 			continue;
21068 
21069 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21070 			(mb->u.mb.mbxCommand != MBX_REG_VPI))
21071 			continue;
21072 
21073 		list_move_tail(&mb->list, &mbox_cmd_list);
21074 	}
21075 	/* Clean up active mailbox command with the vport */
21076 	mb = phba->sli.mbox_active;
21077 	if (mb && (mb->vport == vport)) {
21078 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
21079 			(mb->u.mb.mbxCommand == MBX_REG_VPI))
21080 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21081 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21082 			act_mbx_ndlp = mb->ctx_ndlp;
21083 
21084 			/* This reference is local to this routine.  The
21085 			 * reference is removed at routine exit.
21086 			 */
21087 			act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
21088 
21089 			/* Unregister the RPI when mailbox complete */
21090 			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21091 		}
21092 	}
21093 	/* Cleanup any mailbox completions which are not yet processed */
21094 	do {
21095 		restart_loop = 0;
21096 		list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
21097 			/*
21098 			 * If this mailox is already processed or it is
21099 			 * for another vport ignore it.
21100 			 */
21101 			if ((mb->vport != vport) ||
21102 				(mb->mbox_flag & LPFC_MBX_IMED_UNREG))
21103 				continue;
21104 
21105 			if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21106 				(mb->u.mb.mbxCommand != MBX_REG_VPI))
21107 				continue;
21108 
21109 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21110 			if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21111 				ndlp = mb->ctx_ndlp;
21112 				/* Unregister the RPI when mailbox complete */
21113 				mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21114 				restart_loop = 1;
21115 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21116 				break;
21117 			}
21118 		}
21119 	} while (restart_loop);
21120 
21121 	spin_unlock_irq(&phba->hbalock);
21122 
21123 	/* Release the cleaned-up mailbox commands */
21124 	while (!list_empty(&mbox_cmd_list)) {
21125 		list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
21126 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21127 			ndlp = mb->ctx_ndlp;
21128 			mb->ctx_ndlp = NULL;
21129 			if (ndlp) {
21130 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21131 				lpfc_nlp_put(ndlp);
21132 			}
21133 		}
21134 		lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_UNLOCKED);
21135 	}
21136 
21137 	/* Release the ndlp with the cleaned-up active mailbox command */
21138 	if (act_mbx_ndlp) {
21139 		clear_bit(NLP_IGNR_REG_CMPL, &act_mbx_ndlp->nlp_flag);
21140 		lpfc_nlp_put(act_mbx_ndlp);
21141 	}
21142 }
21143 
21144 /**
21145  * lpfc_drain_txq - Drain the txq
21146  * @phba: Pointer to HBA context object.
21147  *
21148  * This function attempt to submit IOCBs on the txq
21149  * to the adapter.  For SLI4 adapters, the txq contains
21150  * ELS IOCBs that have been deferred because the there
21151  * are no SGLs.  This congestion can occur with large
21152  * vport counts during node discovery.
21153  **/
21154 
21155 uint32_t
lpfc_drain_txq(struct lpfc_hba * phba)21156 lpfc_drain_txq(struct lpfc_hba *phba)
21157 {
21158 	LIST_HEAD(completions);
21159 	struct lpfc_sli_ring *pring;
21160 	struct lpfc_iocbq *piocbq = NULL;
21161 	unsigned long iflags = 0;
21162 	char *fail_msg = NULL;
21163 	uint32_t txq_cnt = 0;
21164 	struct lpfc_queue *wq;
21165 	int ret = 0;
21166 
21167 	if (phba->link_flag & LS_MDS_LOOPBACK) {
21168 		/* MDS WQE are posted only to first WQ*/
21169 		wq = phba->sli4_hba.hdwq[0].io_wq;
21170 		if (unlikely(!wq))
21171 			return 0;
21172 		pring = wq->pring;
21173 	} else {
21174 		wq = phba->sli4_hba.els_wq;
21175 		if (unlikely(!wq))
21176 			return 0;
21177 		pring = lpfc_phba_elsring(phba);
21178 	}
21179 
21180 	if (unlikely(!pring) || list_empty(&pring->txq))
21181 		return 0;
21182 
21183 	spin_lock_irqsave(&pring->ring_lock, iflags);
21184 	list_for_each_entry(piocbq, &pring->txq, list) {
21185 		txq_cnt++;
21186 	}
21187 
21188 	if (txq_cnt > pring->txq_max)
21189 		pring->txq_max = txq_cnt;
21190 
21191 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
21192 
21193 	while (!list_empty(&pring->txq)) {
21194 		spin_lock_irqsave(&pring->ring_lock, iflags);
21195 
21196 		piocbq = lpfc_sli_ringtx_get(phba, pring);
21197 		if (!piocbq) {
21198 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21199 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21200 				"2823 txq empty and txq_cnt is %d\n",
21201 				txq_cnt);
21202 			break;
21203 		}
21204 		txq_cnt--;
21205 
21206 		ret = __lpfc_sli_issue_iocb(phba, pring->ringno, piocbq, 0);
21207 
21208 		if (ret && ret != IOCB_BUSY) {
21209 			fail_msg = " - Cannot send IO ";
21210 			piocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21211 		}
21212 		if (fail_msg) {
21213 			piocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
21214 			/* Failed means we can't issue and need to cancel */
21215 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21216 					"2822 IOCB failed %s iotag 0x%x "
21217 					"xri 0x%x %d flg x%x\n",
21218 					fail_msg, piocbq->iotag,
21219 					piocbq->sli4_xritag, ret,
21220 					piocbq->cmd_flag);
21221 			list_add_tail(&piocbq->list, &completions);
21222 			fail_msg = NULL;
21223 		}
21224 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21225 		if (txq_cnt == 0 || ret == IOCB_BUSY)
21226 			break;
21227 	}
21228 	/* Cancel all the IOCBs that cannot be issued */
21229 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21230 			      IOERR_SLI_ABORTED);
21231 
21232 	return txq_cnt;
21233 }
21234 
21235 /**
21236  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21237  * @phba: Pointer to HBA context object.
21238  * @pwqeq: Pointer to command WQE.
21239  * @sglq: Pointer to the scatter gather queue object.
21240  *
21241  * This routine converts the bpl or bde that is in the WQE
21242  * to a sgl list for the sli4 hardware. The physical address
21243  * of the bpl/bde is converted back to a virtual address.
21244  * If the WQE contains a BPL then the list of BDE's is
21245  * converted to sli4_sge's. If the WQE contains a single
21246  * BDE then it is converted to a single sli_sge.
21247  * The WQE is still in cpu endianness so the contents of
21248  * the bpl can be used without byte swapping.
21249  *
21250  * Returns valid XRI = Success, NO_XRI = Failure.
21251  */
21252 static uint16_t
lpfc_wqe_bpl2sgl(struct lpfc_hba * phba,struct lpfc_iocbq * pwqeq,struct lpfc_sglq * sglq)21253 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21254 		 struct lpfc_sglq *sglq)
21255 {
21256 	uint16_t xritag = NO_XRI;
21257 	struct ulp_bde64 *bpl = NULL;
21258 	struct ulp_bde64 bde;
21259 	struct sli4_sge *sgl  = NULL;
21260 	struct lpfc_dmabuf *dmabuf;
21261 	union lpfc_wqe128 *wqe;
21262 	int numBdes = 0;
21263 	int i = 0;
21264 	uint32_t offset = 0; /* accumulated offset in the sg request list */
21265 	int inbound = 0; /* number of sg reply entries inbound from firmware */
21266 	uint32_t cmd;
21267 
21268 	if (!pwqeq || !sglq)
21269 		return xritag;
21270 
21271 	sgl  = (struct sli4_sge *)sglq->sgl;
21272 	wqe = &pwqeq->wqe;
21273 	pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21274 
21275 	cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21276 	if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21277 		return sglq->sli4_xritag;
21278 	numBdes = pwqeq->num_bdes;
21279 	if (numBdes) {
21280 		/* The addrHigh and addrLow fields within the WQE
21281 		 * have not been byteswapped yet so there is no
21282 		 * need to swap them back.
21283 		 */
21284 		if (pwqeq->bpl_dmabuf)
21285 			dmabuf = pwqeq->bpl_dmabuf;
21286 		else
21287 			return xritag;
21288 
21289 		bpl  = (struct ulp_bde64 *)dmabuf->virt;
21290 		if (!bpl)
21291 			return xritag;
21292 
21293 		for (i = 0; i < numBdes; i++) {
21294 			/* Should already be byte swapped. */
21295 			sgl->addr_hi = bpl->addrHigh;
21296 			sgl->addr_lo = bpl->addrLow;
21297 
21298 			sgl->word2 = le32_to_cpu(sgl->word2);
21299 			if ((i+1) == numBdes)
21300 				bf_set(lpfc_sli4_sge_last, sgl, 1);
21301 			else
21302 				bf_set(lpfc_sli4_sge_last, sgl, 0);
21303 			/* swap the size field back to the cpu so we
21304 			 * can assign it to the sgl.
21305 			 */
21306 			bde.tus.w = le32_to_cpu(bpl->tus.w);
21307 			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21308 			/* The offsets in the sgl need to be accumulated
21309 			 * separately for the request and reply lists.
21310 			 * The request is always first, the reply follows.
21311 			 */
21312 			switch (cmd) {
21313 			case CMD_GEN_REQUEST64_WQE:
21314 				/* add up the reply sg entries */
21315 				if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21316 					inbound++;
21317 				/* first inbound? reset the offset */
21318 				if (inbound == 1)
21319 					offset = 0;
21320 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21321 				bf_set(lpfc_sli4_sge_type, sgl,
21322 					LPFC_SGE_TYPE_DATA);
21323 				offset += bde.tus.f.bdeSize;
21324 				break;
21325 			case CMD_FCP_TRSP64_WQE:
21326 				bf_set(lpfc_sli4_sge_offset, sgl, 0);
21327 				bf_set(lpfc_sli4_sge_type, sgl,
21328 					LPFC_SGE_TYPE_DATA);
21329 				break;
21330 			case CMD_FCP_TSEND64_WQE:
21331 			case CMD_FCP_TRECEIVE64_WQE:
21332 				bf_set(lpfc_sli4_sge_type, sgl,
21333 					bpl->tus.f.bdeFlags);
21334 				if (i < 3)
21335 					offset = 0;
21336 				else
21337 					offset += bde.tus.f.bdeSize;
21338 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21339 				break;
21340 			}
21341 			sgl->word2 = cpu_to_le32(sgl->word2);
21342 			bpl++;
21343 			sgl++;
21344 		}
21345 	} else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21346 		/* The addrHigh and addrLow fields of the BDE have not
21347 		 * been byteswapped yet so they need to be swapped
21348 		 * before putting them in the sgl.
21349 		 */
21350 		sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21351 		sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21352 		sgl->word2 = le32_to_cpu(sgl->word2);
21353 		bf_set(lpfc_sli4_sge_last, sgl, 1);
21354 		sgl->word2 = cpu_to_le32(sgl->word2);
21355 		sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21356 	}
21357 	return sglq->sli4_xritag;
21358 }
21359 
21360 /**
21361  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21362  * @phba: Pointer to HBA context object.
21363  * @qp: Pointer to HDW queue.
21364  * @pwqe: Pointer to command WQE.
21365  **/
21366 int
lpfc_sli4_issue_wqe(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * qp,struct lpfc_iocbq * pwqe)21367 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21368 		    struct lpfc_iocbq *pwqe)
21369 {
21370 	union lpfc_wqe128 *wqe = &pwqe->wqe;
21371 	struct lpfc_async_xchg_ctx *ctxp;
21372 	struct lpfc_queue *wq;
21373 	struct lpfc_sglq *sglq;
21374 	struct lpfc_sli_ring *pring;
21375 	unsigned long iflags;
21376 	uint32_t ret = 0;
21377 
21378 	/* NVME_LS and NVME_LS ABTS requests. */
21379 	if (pwqe->cmd_flag & LPFC_IO_NVME_LS) {
21380 		pring =  phba->sli4_hba.nvmels_wq->pring;
21381 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21382 					  qp, wq_access);
21383 		sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21384 		if (!sglq) {
21385 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21386 			return WQE_BUSY;
21387 		}
21388 		pwqe->sli4_lxritag = sglq->sli4_lxritag;
21389 		pwqe->sli4_xritag = sglq->sli4_xritag;
21390 		if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21391 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21392 			return WQE_ERROR;
21393 		}
21394 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21395 		       pwqe->sli4_xritag);
21396 		ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21397 		if (ret) {
21398 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21399 			return ret;
21400 		}
21401 
21402 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21403 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21404 
21405 		lpfc_sli4_poll_eq(qp->hba_eq);
21406 		return 0;
21407 	}
21408 
21409 	/* NVME_FCREQ and NVME_ABTS requests */
21410 	if (pwqe->cmd_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
21411 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21412 		wq = qp->io_wq;
21413 		pring = wq->pring;
21414 
21415 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21416 
21417 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21418 					  qp, wq_access);
21419 		ret = lpfc_sli4_wq_put(wq, wqe);
21420 		if (ret) {
21421 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21422 			return ret;
21423 		}
21424 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21425 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21426 
21427 		lpfc_sli4_poll_eq(qp->hba_eq);
21428 		return 0;
21429 	}
21430 
21431 	/* NVMET requests */
21432 	if (pwqe->cmd_flag & LPFC_IO_NVMET) {
21433 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21434 		wq = qp->io_wq;
21435 		pring = wq->pring;
21436 
21437 		ctxp = pwqe->context_un.axchg;
21438 		sglq = ctxp->ctxbuf->sglq;
21439 		if (pwqe->sli4_xritag ==  NO_XRI) {
21440 			pwqe->sli4_lxritag = sglq->sli4_lxritag;
21441 			pwqe->sli4_xritag = sglq->sli4_xritag;
21442 		}
21443 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21444 		       pwqe->sli4_xritag);
21445 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21446 
21447 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21448 					  qp, wq_access);
21449 		ret = lpfc_sli4_wq_put(wq, wqe);
21450 		if (ret) {
21451 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21452 			return ret;
21453 		}
21454 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21455 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21456 
21457 		lpfc_sli4_poll_eq(qp->hba_eq);
21458 		return 0;
21459 	}
21460 	return WQE_ERROR;
21461 }
21462 
21463 /**
21464  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21465  * @phba: Pointer to HBA context object.
21466  * @cmdiocb: Pointer to driver command iocb object.
21467  * @cmpl: completion function.
21468  *
21469  * Fill the appropriate fields for the abort WQE and call
21470  * internal routine lpfc_sli4_issue_wqe to send the WQE
21471  * This function is called with hbalock held and no ring_lock held.
21472  *
21473  * RETURNS 0 - SUCCESS
21474  **/
21475 
21476 int
lpfc_sli4_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,void * cmpl)21477 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21478 			    void *cmpl)
21479 {
21480 	struct lpfc_vport *vport = cmdiocb->vport;
21481 	struct lpfc_iocbq *abtsiocb = NULL;
21482 	union lpfc_wqe128 *abtswqe;
21483 	struct lpfc_io_buf *lpfc_cmd;
21484 	int retval = IOCB_ERROR;
21485 	u16 xritag = cmdiocb->sli4_xritag;
21486 
21487 	/*
21488 	 * The scsi command can not be in txq and it is in flight because the
21489 	 * pCmd is still pointing at the SCSI command we have to abort. There
21490 	 * is no need to search the txcmplq. Just send an abort to the FW.
21491 	 */
21492 
21493 	abtsiocb = __lpfc_sli_get_iocbq(phba);
21494 	if (!abtsiocb)
21495 		return WQE_NORESOURCE;
21496 
21497 	/* Indicate the IO is being aborted by the driver. */
21498 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
21499 
21500 	abtswqe = &abtsiocb->wqe;
21501 	memset(abtswqe, 0, sizeof(*abtswqe));
21502 
21503 	if (!lpfc_is_link_up(phba) || (phba->link_flag & LS_EXTERNAL_LOOPBACK))
21504 		bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
21505 	bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21506 	abtswqe->abort_cmd.rsrvd5 = 0;
21507 	abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21508 	bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21509 	bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21510 	bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21511 	bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21512 	bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21513 	bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21514 
21515 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
21516 	abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21517 	abtsiocb->cmd_flag |= LPFC_USE_FCPWQIDX;
21518 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
21519 		abtsiocb->cmd_flag |= LPFC_IO_FCP;
21520 	if (cmdiocb->cmd_flag & LPFC_IO_NVME)
21521 		abtsiocb->cmd_flag |= LPFC_IO_NVME;
21522 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
21523 		abtsiocb->cmd_flag |= LPFC_IO_FOF;
21524 	abtsiocb->vport = vport;
21525 	abtsiocb->cmd_cmpl = cmpl;
21526 
21527 	lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21528 	retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21529 
21530 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21531 			 "0359 Abort xri x%x, original iotag x%x, "
21532 			 "abort cmd iotag x%x retval x%x\n",
21533 			 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21534 
21535 	if (retval) {
21536 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21537 		__lpfc_sli_release_iocbq(phba, abtsiocb);
21538 	}
21539 
21540 	return retval;
21541 }
21542 
21543 #ifdef LPFC_MXP_STAT
21544 /**
21545  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21546  * @phba: pointer to lpfc hba data structure.
21547  * @hwqid: belong to which HWQ.
21548  *
21549  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21550  * 15 seconds after a test case is running.
21551  *
21552  * The user should call lpfc_debugfs_multixripools_write before running a test
21553  * case to clear stat_snapshot_taken. Then the user starts a test case. During
21554  * test case is running, stat_snapshot_taken is incremented by 1 every time when
21555  * this routine is called from heartbeat timer. When stat_snapshot_taken is
21556  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21557  **/
lpfc_snapshot_mxp(struct lpfc_hba * phba,u32 hwqid)21558 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21559 {
21560 	struct lpfc_sli4_hdw_queue *qp;
21561 	struct lpfc_multixri_pool *multixri_pool;
21562 	struct lpfc_pvt_pool *pvt_pool;
21563 	struct lpfc_pbl_pool *pbl_pool;
21564 	u32 txcmplq_cnt;
21565 
21566 	qp = &phba->sli4_hba.hdwq[hwqid];
21567 	multixri_pool = qp->p_multixri_pool;
21568 	if (!multixri_pool)
21569 		return;
21570 
21571 	if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21572 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21573 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21574 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21575 
21576 		multixri_pool->stat_pbl_count = pbl_pool->count;
21577 		multixri_pool->stat_pvt_count = pvt_pool->count;
21578 		multixri_pool->stat_busy_count = txcmplq_cnt;
21579 	}
21580 
21581 	multixri_pool->stat_snapshot_taken++;
21582 }
21583 #endif
21584 
21585 /**
21586  * lpfc_adjust_pvt_pool_count - Adjust private pool count
21587  * @phba: pointer to lpfc hba data structure.
21588  * @hwqid: belong to which HWQ.
21589  *
21590  * This routine moves some XRIs from private to public pool when private pool
21591  * is not busy.
21592  **/
lpfc_adjust_pvt_pool_count(struct lpfc_hba * phba,u32 hwqid)21593 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21594 {
21595 	struct lpfc_multixri_pool *multixri_pool;
21596 	u32 io_req_count;
21597 	u32 prev_io_req_count;
21598 
21599 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21600 	if (!multixri_pool)
21601 		return;
21602 	io_req_count = multixri_pool->io_req_count;
21603 	prev_io_req_count = multixri_pool->prev_io_req_count;
21604 
21605 	if (prev_io_req_count != io_req_count) {
21606 		/* Private pool is busy */
21607 		multixri_pool->prev_io_req_count = io_req_count;
21608 	} else {
21609 		/* Private pool is not busy.
21610 		 * Move XRIs from private to public pool.
21611 		 */
21612 		lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21613 	}
21614 }
21615 
21616 /**
21617  * lpfc_adjust_high_watermark - Adjust high watermark
21618  * @phba: pointer to lpfc hba data structure.
21619  * @hwqid: belong to which HWQ.
21620  *
21621  * This routine sets high watermark as number of outstanding XRIs,
21622  * but make sure the new value is between xri_limit/2 and xri_limit.
21623  **/
lpfc_adjust_high_watermark(struct lpfc_hba * phba,u32 hwqid)21624 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21625 {
21626 	u32 new_watermark;
21627 	u32 watermark_max;
21628 	u32 watermark_min;
21629 	u32 xri_limit;
21630 	u32 txcmplq_cnt;
21631 	u32 abts_io_bufs;
21632 	struct lpfc_multixri_pool *multixri_pool;
21633 	struct lpfc_sli4_hdw_queue *qp;
21634 
21635 	qp = &phba->sli4_hba.hdwq[hwqid];
21636 	multixri_pool = qp->p_multixri_pool;
21637 	if (!multixri_pool)
21638 		return;
21639 	xri_limit = multixri_pool->xri_limit;
21640 
21641 	watermark_max = xri_limit;
21642 	watermark_min = xri_limit / 2;
21643 
21644 	txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21645 	abts_io_bufs = qp->abts_scsi_io_bufs;
21646 	abts_io_bufs += qp->abts_nvme_io_bufs;
21647 
21648 	new_watermark = txcmplq_cnt + abts_io_bufs;
21649 	new_watermark = min(watermark_max, new_watermark);
21650 	new_watermark = max(watermark_min, new_watermark);
21651 	multixri_pool->pvt_pool.high_watermark = new_watermark;
21652 
21653 #ifdef LPFC_MXP_STAT
21654 	multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21655 					  new_watermark);
21656 #endif
21657 }
21658 
21659 /**
21660  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21661  * @phba: pointer to lpfc hba data structure.
21662  * @hwqid: belong to which HWQ.
21663  *
21664  * This routine is called from hearbeat timer when pvt_pool is idle.
21665  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21666  * The first step moves (all - low_watermark) amount of XRIs.
21667  * The second step moves the rest of XRIs.
21668  **/
lpfc_move_xri_pvt_to_pbl(struct lpfc_hba * phba,u32 hwqid)21669 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21670 {
21671 	struct lpfc_pbl_pool *pbl_pool;
21672 	struct lpfc_pvt_pool *pvt_pool;
21673 	struct lpfc_sli4_hdw_queue *qp;
21674 	struct lpfc_io_buf *lpfc_ncmd;
21675 	struct lpfc_io_buf *lpfc_ncmd_next;
21676 	unsigned long iflag;
21677 	struct list_head tmp_list;
21678 	u32 tmp_count;
21679 
21680 	qp = &phba->sli4_hba.hdwq[hwqid];
21681 	pbl_pool = &qp->p_multixri_pool->pbl_pool;
21682 	pvt_pool = &qp->p_multixri_pool->pvt_pool;
21683 	tmp_count = 0;
21684 
21685 	lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21686 	lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
21687 
21688 	if (pvt_pool->count > pvt_pool->low_watermark) {
21689 		/* Step 1: move (all - low_watermark) from pvt_pool
21690 		 * to pbl_pool
21691 		 */
21692 
21693 		/* Move low watermark of bufs from pvt_pool to tmp_list */
21694 		INIT_LIST_HEAD(&tmp_list);
21695 		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21696 					 &pvt_pool->list, list) {
21697 			list_move_tail(&lpfc_ncmd->list, &tmp_list);
21698 			tmp_count++;
21699 			if (tmp_count >= pvt_pool->low_watermark)
21700 				break;
21701 		}
21702 
21703 		/* Move all bufs from pvt_pool to pbl_pool */
21704 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21705 
21706 		/* Move all bufs from tmp_list to pvt_pool */
21707 		list_splice(&tmp_list, &pvt_pool->list);
21708 
21709 		pbl_pool->count += (pvt_pool->count - tmp_count);
21710 		pvt_pool->count = tmp_count;
21711 	} else {
21712 		/* Step 2: move the rest from pvt_pool to pbl_pool */
21713 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21714 		pbl_pool->count += pvt_pool->count;
21715 		pvt_pool->count = 0;
21716 	}
21717 
21718 	spin_unlock(&pvt_pool->lock);
21719 	spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21720 }
21721 
21722 /**
21723  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21724  * @phba: pointer to lpfc hba data structure
21725  * @qp: pointer to HDW queue
21726  * @pbl_pool: specified public free XRI pool
21727  * @pvt_pool: specified private free XRI pool
21728  * @count: number of XRIs to move
21729  *
21730  * This routine tries to move some free common bufs from the specified pbl_pool
21731  * to the specified pvt_pool. It might move less than count XRIs if there's not
21732  * enough in public pool.
21733  *
21734  * Return:
21735  *   true - if XRIs are successfully moved from the specified pbl_pool to the
21736  *          specified pvt_pool
21737  *   false - if the specified pbl_pool is empty or locked by someone else
21738  **/
21739 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)21740 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21741 			  struct lpfc_pbl_pool *pbl_pool,
21742 			  struct lpfc_pvt_pool *pvt_pool, u32 count)
21743 {
21744 	struct lpfc_io_buf *lpfc_ncmd;
21745 	struct lpfc_io_buf *lpfc_ncmd_next;
21746 	unsigned long iflag;
21747 	int ret;
21748 
21749 	ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21750 	if (ret) {
21751 		if (pbl_pool->count) {
21752 			/* Move a batch of XRIs from public to private pool */
21753 			lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
21754 			list_for_each_entry_safe(lpfc_ncmd,
21755 						 lpfc_ncmd_next,
21756 						 &pbl_pool->list,
21757 						 list) {
21758 				list_move_tail(&lpfc_ncmd->list,
21759 					       &pvt_pool->list);
21760 				pvt_pool->count++;
21761 				pbl_pool->count--;
21762 				count--;
21763 				if (count == 0)
21764 					break;
21765 			}
21766 
21767 			spin_unlock(&pvt_pool->lock);
21768 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21769 			return true;
21770 		}
21771 		spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21772 	}
21773 
21774 	return false;
21775 }
21776 
21777 /**
21778  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21779  * @phba: pointer to lpfc hba data structure.
21780  * @hwqid: belong to which HWQ.
21781  * @count: number of XRIs to move
21782  *
21783  * This routine tries to find some free common bufs in one of public pools with
21784  * Round Robin method. The search always starts from local hwqid, then the next
21785  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21786  * a batch of free common bufs are moved to private pool on hwqid.
21787  * It might move less than count XRIs if there's not enough in public pool.
21788  **/
lpfc_move_xri_pbl_to_pvt(struct lpfc_hba * phba,u32 hwqid,u32 count)21789 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21790 {
21791 	struct lpfc_multixri_pool *multixri_pool;
21792 	struct lpfc_multixri_pool *next_multixri_pool;
21793 	struct lpfc_pvt_pool *pvt_pool;
21794 	struct lpfc_pbl_pool *pbl_pool;
21795 	struct lpfc_sli4_hdw_queue *qp;
21796 	u32 next_hwqid;
21797 	u32 hwq_count;
21798 	int ret;
21799 
21800 	qp = &phba->sli4_hba.hdwq[hwqid];
21801 	multixri_pool = qp->p_multixri_pool;
21802 	pvt_pool = &multixri_pool->pvt_pool;
21803 	pbl_pool = &multixri_pool->pbl_pool;
21804 
21805 	/* Check if local pbl_pool is available */
21806 	ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
21807 	if (ret) {
21808 #ifdef LPFC_MXP_STAT
21809 		multixri_pool->local_pbl_hit_count++;
21810 #endif
21811 		return;
21812 	}
21813 
21814 	hwq_count = phba->cfg_hdw_queue;
21815 
21816 	/* Get the next hwqid which was found last time */
21817 	next_hwqid = multixri_pool->rrb_next_hwqid;
21818 
21819 	do {
21820 		/* Go to next hwq */
21821 		next_hwqid = (next_hwqid + 1) % hwq_count;
21822 
21823 		next_multixri_pool =
21824 			phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21825 		pbl_pool = &next_multixri_pool->pbl_pool;
21826 
21827 		/* Check if the public free xri pool is available */
21828 		ret = _lpfc_move_xri_pbl_to_pvt(
21829 			phba, qp, pbl_pool, pvt_pool, count);
21830 
21831 		/* Exit while-loop if success or all hwqid are checked */
21832 	} while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21833 
21834 	/* Starting point for the next time */
21835 	multixri_pool->rrb_next_hwqid = next_hwqid;
21836 
21837 	if (!ret) {
21838 		/* stats: all public pools are empty*/
21839 		multixri_pool->pbl_empty_count++;
21840 	}
21841 
21842 #ifdef LPFC_MXP_STAT
21843 	if (ret) {
21844 		if (next_hwqid == hwqid)
21845 			multixri_pool->local_pbl_hit_count++;
21846 		else
21847 			multixri_pool->other_pbl_hit_count++;
21848 	}
21849 #endif
21850 }
21851 
21852 /**
21853  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21854  * @phba: pointer to lpfc hba data structure.
21855  * @hwqid: belong to which HWQ.
21856  *
21857  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21858  * low watermark.
21859  **/
lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba * phba,u32 hwqid)21860 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21861 {
21862 	struct lpfc_multixri_pool *multixri_pool;
21863 	struct lpfc_pvt_pool *pvt_pool;
21864 
21865 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21866 	pvt_pool = &multixri_pool->pvt_pool;
21867 
21868 	if (pvt_pool->count < pvt_pool->low_watermark)
21869 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21870 }
21871 
21872 /**
21873  * lpfc_release_io_buf - Return one IO buf back to free pool
21874  * @phba: pointer to lpfc hba data structure.
21875  * @lpfc_ncmd: IO buf to be returned.
21876  * @qp: belong to which HWQ.
21877  *
21878  * This routine returns one IO buf back to free pool. If this is an urgent IO,
21879  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21880  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21881  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
21882  * lpfc_io_buf_list_put.
21883  **/
lpfc_release_io_buf(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_ncmd,struct lpfc_sli4_hdw_queue * qp)21884 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21885 			 struct lpfc_sli4_hdw_queue *qp)
21886 {
21887 	unsigned long iflag;
21888 	struct lpfc_pbl_pool *pbl_pool;
21889 	struct lpfc_pvt_pool *pvt_pool;
21890 	struct lpfc_epd_pool *epd_pool;
21891 	u32 txcmplq_cnt;
21892 	u32 xri_owned;
21893 	u32 xri_limit;
21894 	u32 abts_io_bufs;
21895 
21896 	/* MUST zero fields if buffer is reused by another protocol */
21897 	lpfc_ncmd->nvmeCmd = NULL;
21898 	lpfc_ncmd->cur_iocbq.cmd_cmpl = NULL;
21899 
21900 	if (phba->cfg_xpsgl && !phba->nvmet_support &&
21901 	    !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21902 		lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21903 
21904 	if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21905 		lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21906 
21907 	if (phba->cfg_xri_rebalancing) {
21908 		if (lpfc_ncmd->expedite) {
21909 			/* Return to expedite pool */
21910 			epd_pool = &phba->epd_pool;
21911 			spin_lock_irqsave(&epd_pool->lock, iflag);
21912 			list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21913 			epd_pool->count++;
21914 			spin_unlock_irqrestore(&epd_pool->lock, iflag);
21915 			return;
21916 		}
21917 
21918 		/* Avoid invalid access if an IO sneaks in and is being rejected
21919 		 * just _after_ xri pools are destroyed in lpfc_offline.
21920 		 * Nothing much can be done at this point.
21921 		 */
21922 		if (!qp->p_multixri_pool)
21923 			return;
21924 
21925 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21926 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21927 
21928 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21929 		abts_io_bufs = qp->abts_scsi_io_bufs;
21930 		abts_io_bufs += qp->abts_nvme_io_bufs;
21931 
21932 		xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21933 		xri_limit = qp->p_multixri_pool->xri_limit;
21934 
21935 #ifdef LPFC_MXP_STAT
21936 		if (xri_owned <= xri_limit)
21937 			qp->p_multixri_pool->below_limit_count++;
21938 		else
21939 			qp->p_multixri_pool->above_limit_count++;
21940 #endif
21941 
21942 		/* XRI goes to either public or private free xri pool
21943 		 *     based on watermark and xri_limit
21944 		 */
21945 		if ((pvt_pool->count < pvt_pool->low_watermark) ||
21946 		    (xri_owned < xri_limit &&
21947 		     pvt_pool->count < pvt_pool->high_watermark)) {
21948 			lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21949 						  qp, free_pvt_pool);
21950 			list_add_tail(&lpfc_ncmd->list,
21951 				      &pvt_pool->list);
21952 			pvt_pool->count++;
21953 			spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21954 		} else {
21955 			lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21956 						  qp, free_pub_pool);
21957 			list_add_tail(&lpfc_ncmd->list,
21958 				      &pbl_pool->list);
21959 			pbl_pool->count++;
21960 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21961 		}
21962 	} else {
21963 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21964 					  qp, free_xri);
21965 		list_add_tail(&lpfc_ncmd->list,
21966 			      &qp->lpfc_io_buf_list_put);
21967 		qp->put_io_bufs++;
21968 		spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21969 				       iflag);
21970 	}
21971 }
21972 
21973 /**
21974  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21975  * @phba: pointer to lpfc hba data structure.
21976  * @qp: pointer to HDW queue
21977  * @pvt_pool: pointer to private pool data structure.
21978  * @ndlp: pointer to lpfc nodelist data structure.
21979  *
21980  * This routine tries to get one free IO buf from private pool.
21981  *
21982  * Return:
21983  *   pointer to one free IO buf - if private pool is not empty
21984  *   NULL - if private pool is empty
21985  **/
21986 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)21987 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
21988 				  struct lpfc_sli4_hdw_queue *qp,
21989 				  struct lpfc_pvt_pool *pvt_pool,
21990 				  struct lpfc_nodelist *ndlp)
21991 {
21992 	struct lpfc_io_buf *lpfc_ncmd;
21993 	struct lpfc_io_buf *lpfc_ncmd_next;
21994 	unsigned long iflag;
21995 
21996 	lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
21997 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21998 				 &pvt_pool->list, list) {
21999 		if (lpfc_test_rrq_active(
22000 			phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
22001 			continue;
22002 		list_del(&lpfc_ncmd->list);
22003 		pvt_pool->count--;
22004 		spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22005 		return lpfc_ncmd;
22006 	}
22007 	spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22008 
22009 	return NULL;
22010 }
22011 
22012 /**
22013  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
22014  * @phba: pointer to lpfc hba data structure.
22015  *
22016  * This routine tries to get one free IO buf from expedite pool.
22017  *
22018  * Return:
22019  *   pointer to one free IO buf - if expedite pool is not empty
22020  *   NULL - if expedite pool is empty
22021  **/
22022 static struct lpfc_io_buf *
lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba * phba)22023 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
22024 {
22025 	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
22026 	struct lpfc_io_buf *lpfc_ncmd_next;
22027 	unsigned long iflag;
22028 	struct lpfc_epd_pool *epd_pool;
22029 
22030 	epd_pool = &phba->epd_pool;
22031 
22032 	spin_lock_irqsave(&epd_pool->lock, iflag);
22033 	if (epd_pool->count > 0) {
22034 		list_for_each_entry_safe(iter, lpfc_ncmd_next,
22035 					 &epd_pool->list, list) {
22036 			list_del(&iter->list);
22037 			epd_pool->count--;
22038 			lpfc_ncmd = iter;
22039 			break;
22040 		}
22041 	}
22042 	spin_unlock_irqrestore(&epd_pool->lock, iflag);
22043 
22044 	return lpfc_ncmd;
22045 }
22046 
22047 /**
22048  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
22049  * @phba: pointer to lpfc hba data structure.
22050  * @ndlp: pointer to lpfc nodelist data structure.
22051  * @hwqid: belong to which HWQ
22052  * @expedite: 1 means this request is urgent.
22053  *
22054  * This routine will do the following actions and then return a pointer to
22055  * one free IO buf.
22056  *
22057  * 1. If private free xri count is empty, move some XRIs from public to
22058  *    private pool.
22059  * 2. Get one XRI from private free xri pool.
22060  * 3. If we fail to get one from pvt_pool and this is an expedite request,
22061  *    get one free xri from expedite pool.
22062  *
22063  * Note: ndlp is only used on SCSI side for RRQ testing.
22064  *       The caller should pass NULL for ndlp on NVME side.
22065  *
22066  * Return:
22067  *   pointer to one free IO buf - if private pool is not empty
22068  *   NULL - if private pool is empty
22069  **/
22070 static struct lpfc_io_buf *
lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int hwqid,int expedite)22071 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
22072 				    struct lpfc_nodelist *ndlp,
22073 				    int hwqid, int expedite)
22074 {
22075 	struct lpfc_sli4_hdw_queue *qp;
22076 	struct lpfc_multixri_pool *multixri_pool;
22077 	struct lpfc_pvt_pool *pvt_pool;
22078 	struct lpfc_io_buf *lpfc_ncmd;
22079 
22080 	qp = &phba->sli4_hba.hdwq[hwqid];
22081 	lpfc_ncmd = NULL;
22082 	if (!qp) {
22083 		lpfc_printf_log(phba, KERN_INFO,
22084 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22085 				"5556 NULL qp for hwqid  x%x\n", hwqid);
22086 		return lpfc_ncmd;
22087 	}
22088 	multixri_pool = qp->p_multixri_pool;
22089 	if (!multixri_pool) {
22090 		lpfc_printf_log(phba, KERN_INFO,
22091 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22092 				"5557 NULL multixri for hwqid  x%x\n", hwqid);
22093 		return lpfc_ncmd;
22094 	}
22095 	pvt_pool = &multixri_pool->pvt_pool;
22096 	if (!pvt_pool) {
22097 		lpfc_printf_log(phba, KERN_INFO,
22098 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22099 				"5558 NULL pvt_pool for hwqid  x%x\n", hwqid);
22100 		return lpfc_ncmd;
22101 	}
22102 	multixri_pool->io_req_count++;
22103 
22104 	/* If pvt_pool is empty, move some XRIs from public to private pool */
22105 	if (pvt_pool->count == 0)
22106 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
22107 
22108 	/* Get one XRI from private free xri pool */
22109 	lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
22110 
22111 	if (lpfc_ncmd) {
22112 		lpfc_ncmd->hdwq = qp;
22113 		lpfc_ncmd->hdwq_no = hwqid;
22114 	} else if (expedite) {
22115 		/* If we fail to get one from pvt_pool and this is an expedite
22116 		 * request, get one free xri from expedite pool.
22117 		 */
22118 		lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
22119 	}
22120 
22121 	return lpfc_ncmd;
22122 }
22123 
22124 static inline struct lpfc_io_buf *
lpfc_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int idx)22125 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
22126 {
22127 	struct lpfc_sli4_hdw_queue *qp;
22128 	struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
22129 
22130 	qp = &phba->sli4_hba.hdwq[idx];
22131 	list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22132 				 &qp->lpfc_io_buf_list_get, list) {
22133 		if (lpfc_test_rrq_active(phba, ndlp,
22134 					 lpfc_cmd->cur_iocbq.sli4_lxritag))
22135 			continue;
22136 
22137 		if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22138 			continue;
22139 
22140 		list_del_init(&lpfc_cmd->list);
22141 		qp->get_io_bufs--;
22142 		lpfc_cmd->hdwq = qp;
22143 		lpfc_cmd->hdwq_no = idx;
22144 		return lpfc_cmd;
22145 	}
22146 	return NULL;
22147 }
22148 
22149 /**
22150  * lpfc_get_io_buf - Get one IO buffer from free pool
22151  * @phba: The HBA for which this call is being executed.
22152  * @ndlp: pointer to lpfc nodelist data structure.
22153  * @hwqid: belong to which HWQ
22154  * @expedite: 1 means this request is urgent.
22155  *
22156  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22157  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22158  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22159  *
22160  * Note: ndlp is only used on SCSI side for RRQ testing.
22161  *       The caller should pass NULL for ndlp on NVME side.
22162  *
22163  * Return codes:
22164  *   NULL - Error
22165  *   Pointer to lpfc_io_buf - Success
22166  **/
lpfc_get_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,u32 hwqid,int expedite)22167 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22168 				    struct lpfc_nodelist *ndlp,
22169 				    u32 hwqid, int expedite)
22170 {
22171 	struct lpfc_sli4_hdw_queue *qp;
22172 	unsigned long iflag;
22173 	struct lpfc_io_buf *lpfc_cmd;
22174 
22175 	qp = &phba->sli4_hba.hdwq[hwqid];
22176 	lpfc_cmd = NULL;
22177 	if (!qp) {
22178 		lpfc_printf_log(phba, KERN_WARNING,
22179 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22180 				"5555 NULL qp for hwqid  x%x\n", hwqid);
22181 		return lpfc_cmd;
22182 	}
22183 
22184 	if (phba->cfg_xri_rebalancing)
22185 		lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22186 			phba, ndlp, hwqid, expedite);
22187 	else {
22188 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22189 					  qp, alloc_xri_get);
22190 		if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22191 			lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22192 		if (!lpfc_cmd) {
22193 			lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22194 					  qp, alloc_xri_put);
22195 			list_splice(&qp->lpfc_io_buf_list_put,
22196 				    &qp->lpfc_io_buf_list_get);
22197 			qp->get_io_bufs += qp->put_io_bufs;
22198 			INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22199 			qp->put_io_bufs = 0;
22200 			spin_unlock(&qp->io_buf_list_put_lock);
22201 			if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22202 			    expedite)
22203 				lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22204 		}
22205 		spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22206 	}
22207 
22208 	return lpfc_cmd;
22209 }
22210 
22211 /**
22212  * lpfc_read_object - Retrieve object data from HBA
22213  * @phba: The HBA for which this call is being executed.
22214  * @rdobject: Pathname of object data we want to read.
22215  * @datap: Pointer to where data will be copied to.
22216  * @datasz: size of data area
22217  *
22218  * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22219  * The data will be truncated if datasz is not large enough.
22220  * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22221  * Returns the actual bytes read from the object.
22222  *
22223  * This routine is hard coded to use a poll completion.  Unlike other
22224  * sli4_config mailboxes, it uses lpfc_mbuf memory which is not
22225  * cleaned up in lpfc_sli4_cmd_mbox_free.  If this routine is modified
22226  * to use interrupt-based completions, code is needed to fully cleanup
22227  * the memory.
22228  */
22229 int
lpfc_read_object(struct lpfc_hba * phba,char * rdobject,uint32_t * datap,uint32_t datasz)22230 lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22231 		 uint32_t datasz)
22232 {
22233 	struct lpfc_mbx_read_object *read_object;
22234 	LPFC_MBOXQ_t *mbox;
22235 	int rc, length, eof, j, byte_cnt = 0;
22236 	uint32_t shdr_status, shdr_add_status;
22237 	union lpfc_sli4_cfg_shdr *shdr;
22238 	struct lpfc_dmabuf *pcmd;
22239 	u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
22240 
22241 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22242 	if (!mbox)
22243 		return -ENOMEM;
22244 	length = (sizeof(struct lpfc_mbx_read_object) -
22245 		  sizeof(struct lpfc_sli4_cfg_mhdr));
22246 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22247 			 LPFC_MBOX_OPCODE_READ_OBJECT,
22248 			 length, LPFC_SLI4_MBX_EMBED);
22249 	read_object = &mbox->u.mqe.un.read_object;
22250 	shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22251 
22252 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22253 	bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22254 	read_object->u.request.rd_object_offset = 0;
22255 	read_object->u.request.rd_object_cnt = 1;
22256 
22257 	memset((void *)read_object->u.request.rd_object_name, 0,
22258 	       LPFC_OBJ_NAME_SZ);
22259 	scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
22260 	for (j = 0; j < strlen(rdobject); j++)
22261 		read_object->u.request.rd_object_name[j] =
22262 			cpu_to_le32(rd_object_name[j]);
22263 
22264 	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22265 	if (pcmd)
22266 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22267 	if (!pcmd || !pcmd->virt) {
22268 		kfree(pcmd);
22269 		mempool_free(mbox, phba->mbox_mem_pool);
22270 		return -ENOMEM;
22271 	}
22272 	memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22273 	read_object->u.request.rd_object_hbuf[0].pa_lo =
22274 		putPaddrLow(pcmd->phys);
22275 	read_object->u.request.rd_object_hbuf[0].pa_hi =
22276 		putPaddrHigh(pcmd->phys);
22277 	read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22278 
22279 	mbox->vport = phba->pport;
22280 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22281 	mbox->ctx_ndlp = NULL;
22282 
22283 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22284 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22285 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22286 
22287 	if (shdr_status == STATUS_FAILED &&
22288 	    shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22289 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22290 				"4674 No port cfg file in FW.\n");
22291 		byte_cnt = -ENOENT;
22292 	} else if (shdr_status || shdr_add_status || rc) {
22293 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22294 				"2625 READ_OBJECT mailbox failed with "
22295 				"status x%x add_status x%x, mbx status x%x\n",
22296 				shdr_status, shdr_add_status, rc);
22297 		byte_cnt = -ENXIO;
22298 	} else {
22299 		/* Success */
22300 		length = read_object->u.response.rd_object_actual_rlen;
22301 		eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22302 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22303 				"2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22304 				length, datasz, eof);
22305 
22306 		/* Detect the port config file exists but is empty */
22307 		if (!length && eof) {
22308 			byte_cnt = 0;
22309 			goto exit;
22310 		}
22311 
22312 		byte_cnt = length;
22313 		lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22314 	}
22315 
22316  exit:
22317 	/* This is an embedded SLI4 mailbox with an external buffer allocated.
22318 	 * Free the pcmd and then cleanup with the correct routine.
22319 	 */
22320 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22321 	kfree(pcmd);
22322 	lpfc_sli4_mbox_cmd_free(phba, mbox);
22323 	return byte_cnt;
22324 }
22325 
22326 /**
22327  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22328  * @phba: The HBA for which this call is being executed.
22329  * @lpfc_buf: IO buf structure to append the SGL chunk
22330  *
22331  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22332  * and will allocate an SGL chunk if the pool is empty.
22333  *
22334  * Return codes:
22335  *   NULL - Error
22336  *   Pointer to sli4_hybrid_sgl - Success
22337  **/
22338 struct sli4_hybrid_sgl *
lpfc_get_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22339 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22340 {
22341 	struct sli4_hybrid_sgl *list_entry = NULL;
22342 	struct sli4_hybrid_sgl *tmp = NULL;
22343 	struct sli4_hybrid_sgl *allocated_sgl = NULL;
22344 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22345 	struct list_head *buf_list = &hdwq->sgl_list;
22346 	unsigned long iflags;
22347 
22348 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22349 
22350 	if (likely(!list_empty(buf_list))) {
22351 		/* break off 1 chunk from the sgl_list */
22352 		list_for_each_entry_safe(list_entry, tmp,
22353 					 buf_list, list_node) {
22354 			list_move_tail(&list_entry->list_node,
22355 				       &lpfc_buf->dma_sgl_xtra_list);
22356 			break;
22357 		}
22358 	} else {
22359 		/* allocate more */
22360 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22361 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22362 				   cpu_to_node(hdwq->io_wq->chann));
22363 		if (!tmp) {
22364 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22365 					"8353 error kmalloc memory for HDWQ "
22366 					"%d %s\n",
22367 					lpfc_buf->hdwq_no, __func__);
22368 			return NULL;
22369 		}
22370 
22371 		tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22372 					      GFP_ATOMIC, &tmp->dma_phys_sgl);
22373 		if (!tmp->dma_sgl) {
22374 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22375 					"8354 error pool_alloc memory for HDWQ "
22376 					"%d %s\n",
22377 					lpfc_buf->hdwq_no, __func__);
22378 			kfree(tmp);
22379 			return NULL;
22380 		}
22381 
22382 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22383 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22384 	}
22385 
22386 	allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22387 					struct sli4_hybrid_sgl,
22388 					list_node);
22389 
22390 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22391 
22392 	return allocated_sgl;
22393 }
22394 
22395 /**
22396  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22397  * @phba: The HBA for which this call is being executed.
22398  * @lpfc_buf: IO buf structure with the SGL chunk
22399  *
22400  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22401  *
22402  * Return codes:
22403  *   0 - Success
22404  *   -EINVAL - Error
22405  **/
22406 int
lpfc_put_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22407 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22408 {
22409 	int rc = 0;
22410 	struct sli4_hybrid_sgl *list_entry = NULL;
22411 	struct sli4_hybrid_sgl *tmp = NULL;
22412 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22413 	struct list_head *buf_list = &hdwq->sgl_list;
22414 	unsigned long iflags;
22415 
22416 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22417 
22418 	if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22419 		list_for_each_entry_safe(list_entry, tmp,
22420 					 &lpfc_buf->dma_sgl_xtra_list,
22421 					 list_node) {
22422 			list_move_tail(&list_entry->list_node,
22423 				       buf_list);
22424 		}
22425 	} else {
22426 		rc = -EINVAL;
22427 	}
22428 
22429 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22430 	return rc;
22431 }
22432 
22433 /**
22434  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22435  * @phba: phba object
22436  * @hdwq: hdwq to cleanup sgl buff resources on
22437  *
22438  * This routine frees all SGL chunks of hdwq SGL chunk pool.
22439  *
22440  * Return codes:
22441  *   None
22442  **/
22443 void
lpfc_free_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22444 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22445 		       struct lpfc_sli4_hdw_queue *hdwq)
22446 {
22447 	struct list_head *buf_list = &hdwq->sgl_list;
22448 	struct sli4_hybrid_sgl *list_entry = NULL;
22449 	struct sli4_hybrid_sgl *tmp = NULL;
22450 	unsigned long iflags;
22451 
22452 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22453 
22454 	/* Free sgl pool */
22455 	list_for_each_entry_safe(list_entry, tmp,
22456 				 buf_list, list_node) {
22457 		list_del(&list_entry->list_node);
22458 		dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22459 			      list_entry->dma_sgl,
22460 			      list_entry->dma_phys_sgl);
22461 		kfree(list_entry);
22462 	}
22463 
22464 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22465 }
22466 
22467 /**
22468  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22469  * @phba: The HBA for which this call is being executed.
22470  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22471  *
22472  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22473  * and will allocate an CMD/RSP buffer if the pool is empty.
22474  *
22475  * Return codes:
22476  *   NULL - Error
22477  *   Pointer to fcp_cmd_rsp_buf - Success
22478  **/
22479 struct fcp_cmd_rsp_buf *
lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22480 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22481 			      struct lpfc_io_buf *lpfc_buf)
22482 {
22483 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22484 	struct fcp_cmd_rsp_buf *tmp = NULL;
22485 	struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22486 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22487 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22488 	unsigned long iflags;
22489 
22490 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22491 
22492 	if (likely(!list_empty(buf_list))) {
22493 		/* break off 1 chunk from the list */
22494 		list_for_each_entry_safe(list_entry, tmp,
22495 					 buf_list,
22496 					 list_node) {
22497 			list_move_tail(&list_entry->list_node,
22498 				       &lpfc_buf->dma_cmd_rsp_list);
22499 			break;
22500 		}
22501 	} else {
22502 		/* allocate more */
22503 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22504 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22505 				   cpu_to_node(hdwq->io_wq->chann));
22506 		if (!tmp) {
22507 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22508 					"8355 error kmalloc memory for HDWQ "
22509 					"%d %s\n",
22510 					lpfc_buf->hdwq_no, __func__);
22511 			return NULL;
22512 		}
22513 
22514 		tmp->fcp_cmnd = dma_pool_zalloc(phba->lpfc_cmd_rsp_buf_pool,
22515 						GFP_ATOMIC,
22516 						&tmp->fcp_cmd_rsp_dma_handle);
22517 
22518 		if (!tmp->fcp_cmnd) {
22519 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22520 					"8356 error pool_alloc memory for HDWQ "
22521 					"%d %s\n",
22522 					lpfc_buf->hdwq_no, __func__);
22523 			kfree(tmp);
22524 			return NULL;
22525 		}
22526 
22527 		tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22528 				sizeof(struct fcp_cmnd32));
22529 
22530 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22531 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22532 	}
22533 
22534 	allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22535 					struct fcp_cmd_rsp_buf,
22536 					list_node);
22537 
22538 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22539 
22540 	return allocated_buf;
22541 }
22542 
22543 /**
22544  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22545  * @phba: The HBA for which this call is being executed.
22546  * @lpfc_buf: IO buf structure with the CMD/RSP buf
22547  *
22548  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22549  *
22550  * Return codes:
22551  *   0 - Success
22552  *   -EINVAL - Error
22553  **/
22554 int
lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22555 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22556 			      struct lpfc_io_buf *lpfc_buf)
22557 {
22558 	int rc = 0;
22559 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22560 	struct fcp_cmd_rsp_buf *tmp = NULL;
22561 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22562 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22563 	unsigned long iflags;
22564 
22565 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22566 
22567 	if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22568 		list_for_each_entry_safe(list_entry, tmp,
22569 					 &lpfc_buf->dma_cmd_rsp_list,
22570 					 list_node) {
22571 			list_move_tail(&list_entry->list_node,
22572 				       buf_list);
22573 		}
22574 	} else {
22575 		rc = -EINVAL;
22576 	}
22577 
22578 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22579 	return rc;
22580 }
22581 
22582 /**
22583  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22584  * @phba: phba object
22585  * @hdwq: hdwq to cleanup cmd rsp buff resources on
22586  *
22587  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22588  *
22589  * Return codes:
22590  *   None
22591  **/
22592 void
lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22593 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22594 			       struct lpfc_sli4_hdw_queue *hdwq)
22595 {
22596 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22597 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22598 	struct fcp_cmd_rsp_buf *tmp = NULL;
22599 	unsigned long iflags;
22600 
22601 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22602 
22603 	/* Free cmd_rsp buf pool */
22604 	list_for_each_entry_safe(list_entry, tmp,
22605 				 buf_list,
22606 				 list_node) {
22607 		list_del(&list_entry->list_node);
22608 		dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22609 			      list_entry->fcp_cmnd,
22610 			      list_entry->fcp_cmd_rsp_dma_handle);
22611 		kfree(list_entry);
22612 	}
22613 
22614 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22615 }
22616 
22617 /**
22618  * lpfc_sli_prep_wqe - Prepare WQE for the command to be posted
22619  * @phba: phba object
22620  * @job: job entry of the command to be posted.
22621  *
22622  * Fill the common fields of the wqe for each of the command.
22623  *
22624  * Return codes:
22625  *	None
22626  **/
22627 void
lpfc_sli_prep_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * job)22628 lpfc_sli_prep_wqe(struct lpfc_hba *phba, struct lpfc_iocbq *job)
22629 {
22630 	u8 cmnd;
22631 	u32 *pcmd;
22632 	u32 if_type = 0;
22633 	u32 abort_tag;
22634 	bool fip;
22635 	struct lpfc_nodelist *ndlp = NULL;
22636 	union lpfc_wqe128 *wqe = &job->wqe;
22637 	u8 command_type = ELS_COMMAND_NON_FIP;
22638 
22639 	fip = test_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
22640 	/* The fcp commands will set command type */
22641 	if (job->cmd_flag &  LPFC_IO_FCP)
22642 		command_type = FCP_COMMAND;
22643 	else if (fip && (job->cmd_flag & LPFC_FIP_ELS_ID_MASK))
22644 		command_type = ELS_COMMAND_FIP;
22645 	else
22646 		command_type = ELS_COMMAND_NON_FIP;
22647 
22648 	abort_tag = job->iotag;
22649 	cmnd = bf_get(wqe_cmnd, &wqe->els_req.wqe_com);
22650 
22651 	switch (cmnd) {
22652 	case CMD_ELS_REQUEST64_WQE:
22653 		ndlp = job->ndlp;
22654 
22655 		if_type = bf_get(lpfc_sli_intf_if_type,
22656 				 &phba->sli4_hba.sli_intf);
22657 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22658 			pcmd = (u32 *)job->cmd_dmabuf->virt;
22659 			if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
22660 				     *pcmd == ELS_CMD_SCR ||
22661 				     *pcmd == ELS_CMD_RDF ||
22662 				     *pcmd == ELS_CMD_EDC ||
22663 				     *pcmd == ELS_CMD_RSCN_XMT ||
22664 				     *pcmd == ELS_CMD_FDISC ||
22665 				     *pcmd == ELS_CMD_LOGO ||
22666 				     *pcmd == ELS_CMD_QFPA ||
22667 				     *pcmd == ELS_CMD_UVEM ||
22668 				     *pcmd == ELS_CMD_PLOGI)) {
22669 				bf_set(els_req64_sp, &wqe->els_req, 1);
22670 				bf_set(els_req64_sid, &wqe->els_req,
22671 				       job->vport->fc_myDID);
22672 
22673 				if ((*pcmd == ELS_CMD_FLOGI) &&
22674 				    !(phba->fc_topology ==
22675 				      LPFC_TOPOLOGY_LOOP))
22676 					bf_set(els_req64_sid, &wqe->els_req, 0);
22677 
22678 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
22679 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22680 				       phba->vpi_ids[job->vport->vpi]);
22681 			} else if (pcmd) {
22682 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
22683 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22684 				       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22685 			}
22686 		}
22687 
22688 		bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
22689 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22690 
22691 		bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
22692 		bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
22693 		bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
22694 		bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22695 		bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
22696 		break;
22697 	case CMD_XMIT_ELS_RSP64_WQE:
22698 		ndlp = job->ndlp;
22699 
22700 		/* word4 */
22701 		wqe->xmit_els_rsp.word4 = 0;
22702 
22703 		if_type = bf_get(lpfc_sli_intf_if_type,
22704 				 &phba->sli4_hba.sli_intf);
22705 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22706 			if (test_bit(FC_PT2PT, &job->vport->fc_flag)) {
22707 				bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22708 				bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22709 				       job->vport->fc_myDID);
22710 				if (job->vport->fc_myDID == Fabric_DID) {
22711 					bf_set(wqe_els_did,
22712 					       &wqe->xmit_els_rsp.wqe_dest, 0);
22713 				}
22714 			}
22715 		}
22716 
22717 		bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
22718 		bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
22719 		bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
22720 		bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
22721 		       LPFC_WQE_LENLOC_WORD3);
22722 		bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
22723 
22724 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
22725 			bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22726 			bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22727 			       job->vport->fc_myDID);
22728 			bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
22729 		}
22730 
22731 		if (phba->sli_rev == LPFC_SLI_REV4) {
22732 			bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
22733 			       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22734 
22735 			if (bf_get(wqe_ct, &wqe->xmit_els_rsp.wqe_com))
22736 				bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
22737 				       phba->vpi_ids[job->vport->vpi]);
22738 		}
22739 		command_type = OTHER_COMMAND;
22740 		break;
22741 	case CMD_GEN_REQUEST64_WQE:
22742 		/* Word 10 */
22743 		bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
22744 		bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
22745 		bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
22746 		bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22747 		bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
22748 		command_type = OTHER_COMMAND;
22749 		break;
22750 	case CMD_XMIT_SEQUENCE64_WQE:
22751 		if (phba->link_flag & LS_LOOPBACK_MODE)
22752 			bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
22753 
22754 		wqe->xmit_sequence.rsvd3 = 0;
22755 		bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
22756 		bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
22757 		bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
22758 		       LPFC_WQE_IOD_WRITE);
22759 		bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
22760 		       LPFC_WQE_LENLOC_WORD12);
22761 		bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
22762 		command_type = OTHER_COMMAND;
22763 		break;
22764 	case CMD_XMIT_BLS_RSP64_WQE:
22765 		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
22766 		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
22767 		bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
22768 		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
22769 		       phba->vpi_ids[phba->pport->vpi]);
22770 		bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
22771 		bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
22772 		       LPFC_WQE_LENLOC_NONE);
22773 		/* Overwrite the pre-set comnd type with OTHER_COMMAND */
22774 		command_type = OTHER_COMMAND;
22775 		break;
22776 	case CMD_FCP_ICMND64_WQE:	/* task mgmt commands */
22777 	case CMD_ABORT_XRI_WQE:		/* abort iotag */
22778 	case CMD_SEND_FRAME:		/* mds loopback */
22779 		/* cases already formatted for sli4 wqe - no chgs necessary */
22780 		return;
22781 	default:
22782 		dump_stack();
22783 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
22784 				"6207 Invalid command 0x%x\n",
22785 				cmnd);
22786 		break;
22787 	}
22788 
22789 	wqe->generic.wqe_com.abort_tag = abort_tag;
22790 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, job->iotag);
22791 	bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
22792 	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
22793 }
22794