xref: /linux/drivers/scsi/lpfc/lpfc_sli.c (revision 16cd1c2657762c62a00ac78eecaa25868f7e601b)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2024 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 = from_timer(phba, t, eratt_poll);
3929 	if (!test_bit(HBA_SETUP, &phba->hba_flag))
3930 		return;
3931 
3932 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
3933 		return;
3934 
3935 	/* Here we will also keep track of interrupts per sec of the hba */
3936 	sli_intr = phba->sli.slistat.sli_intr;
3937 
3938 	if (phba->sli.slistat.sli_prev_intr > sli_intr)
3939 		cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3940 			sli_intr);
3941 	else
3942 		cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3943 
3944 	/* 64-bit integer division not supported on 32-bit x86 - use do_div */
3945 	do_div(cnt, phba->eratt_poll_interval);
3946 	phba->sli.slistat.sli_ips = cnt;
3947 
3948 	phba->sli.slistat.sli_prev_intr = sli_intr;
3949 
3950 	/* Check chip HA register for error event */
3951 	eratt = lpfc_sli_check_eratt(phba);
3952 
3953 	if (eratt)
3954 		/* Tell the worker thread there is work to do */
3955 		lpfc_worker_wake_up(phba);
3956 	else
3957 		/* Restart the timer for next eratt poll */
3958 		mod_timer(&phba->eratt_poll,
3959 			  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
3960 	return;
3961 }
3962 
3963 
3964 /**
3965  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3966  * @phba: Pointer to HBA context object.
3967  * @pring: Pointer to driver SLI ring object.
3968  * @mask: Host attention register mask for this ring.
3969  *
3970  * This function is called from the interrupt context when there is a ring
3971  * event for the fcp ring. The caller does not hold any lock.
3972  * The function processes each response iocb in the response ring until it
3973  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3974  * LE bit set. The function will call the completion handler of the command iocb
3975  * if the response iocb indicates a completion for a command iocb or it is
3976  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3977  * function if this is an unsolicited iocb.
3978  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3979  * to check it explicitly.
3980  */
3981 int
lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)3982 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3983 				struct lpfc_sli_ring *pring, uint32_t mask)
3984 {
3985 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3986 	IOCB_t *irsp = NULL;
3987 	IOCB_t *entry = NULL;
3988 	struct lpfc_iocbq *cmdiocbq = NULL;
3989 	struct lpfc_iocbq rspiocbq;
3990 	uint32_t status;
3991 	uint32_t portRspPut, portRspMax;
3992 	int rc = 1;
3993 	lpfc_iocb_type type;
3994 	unsigned long iflag;
3995 	uint32_t rsp_cmpl = 0;
3996 
3997 	spin_lock_irqsave(&phba->hbalock, iflag);
3998 	pring->stats.iocb_event++;
3999 
4000 	/*
4001 	 * The next available response entry should never exceed the maximum
4002 	 * entries.  If it does, treat it as an adapter hardware error.
4003 	 */
4004 	portRspMax = pring->sli.sli3.numRiocb;
4005 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4006 	if (unlikely(portRspPut >= portRspMax)) {
4007 		lpfc_sli_rsp_pointers_error(phba, pring);
4008 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4009 		return 1;
4010 	}
4011 	if (phba->fcp_ring_in_use) {
4012 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4013 		return 1;
4014 	} else
4015 		phba->fcp_ring_in_use = 1;
4016 
4017 	rmb();
4018 	while (pring->sli.sli3.rspidx != portRspPut) {
4019 		/*
4020 		 * Fetch an entry off the ring and copy it into a local data
4021 		 * structure.  The copy involves a byte-swap since the
4022 		 * network byte order and pci byte orders are different.
4023 		 */
4024 		entry = lpfc_resp_iocb(phba, pring);
4025 		phba->last_completion_time = jiffies;
4026 
4027 		if (++pring->sli.sli3.rspidx >= portRspMax)
4028 			pring->sli.sli3.rspidx = 0;
4029 
4030 		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
4031 				      (uint32_t *) &rspiocbq.iocb,
4032 				      phba->iocb_rsp_size);
4033 		INIT_LIST_HEAD(&(rspiocbq.list));
4034 		irsp = &rspiocbq.iocb;
4035 
4036 		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
4037 		pring->stats.iocb_rsp++;
4038 		rsp_cmpl++;
4039 
4040 		if (unlikely(irsp->ulpStatus)) {
4041 			/*
4042 			 * If resource errors reported from HBA, reduce
4043 			 * queuedepths of the SCSI device.
4044 			 */
4045 			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
4046 			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4047 			     IOERR_NO_RESOURCES)) {
4048 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4049 				phba->lpfc_rampdown_queue_depth(phba);
4050 				spin_lock_irqsave(&phba->hbalock, iflag);
4051 			}
4052 
4053 			/* Rsp ring <ringno> error: IOCB */
4054 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4055 					"0336 Rsp Ring %d error: IOCB Data: "
4056 					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
4057 					pring->ringno,
4058 					irsp->un.ulpWord[0],
4059 					irsp->un.ulpWord[1],
4060 					irsp->un.ulpWord[2],
4061 					irsp->un.ulpWord[3],
4062 					irsp->un.ulpWord[4],
4063 					irsp->un.ulpWord[5],
4064 					*(uint32_t *)&irsp->un1,
4065 					*((uint32_t *)&irsp->un1 + 1));
4066 		}
4067 
4068 		switch (type) {
4069 		case LPFC_ABORT_IOCB:
4070 		case LPFC_SOL_IOCB:
4071 			/*
4072 			 * Idle exchange closed via ABTS from port.  No iocb
4073 			 * resources need to be recovered.
4074 			 */
4075 			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
4076 				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4077 						"0333 IOCB cmd 0x%x"
4078 						" processed. Skipping"
4079 						" completion\n",
4080 						irsp->ulpCommand);
4081 				break;
4082 			}
4083 
4084 			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
4085 							 &rspiocbq);
4086 			if (unlikely(!cmdiocbq))
4087 				break;
4088 			if (cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED)
4089 				cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
4090 			if (cmdiocbq->cmd_cmpl) {
4091 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4092 				cmdiocbq->cmd_cmpl(phba, cmdiocbq, &rspiocbq);
4093 				spin_lock_irqsave(&phba->hbalock, iflag);
4094 			}
4095 			break;
4096 		case LPFC_UNSOL_IOCB:
4097 			spin_unlock_irqrestore(&phba->hbalock, iflag);
4098 			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
4099 			spin_lock_irqsave(&phba->hbalock, iflag);
4100 			break;
4101 		default:
4102 			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4103 				char adaptermsg[LPFC_MAX_ADPTMSG];
4104 				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4105 				memcpy(&adaptermsg[0], (uint8_t *) irsp,
4106 				       MAX_MSG_DATA);
4107 				dev_warn(&((phba->pcidev)->dev),
4108 					 "lpfc%d: %s\n",
4109 					 phba->brd_no, adaptermsg);
4110 			} else {
4111 				/* Unknown IOCB command */
4112 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4113 						"0334 Unknown IOCB command "
4114 						"Data: x%x, x%x x%x x%x x%x\n",
4115 						type, irsp->ulpCommand,
4116 						irsp->ulpStatus,
4117 						irsp->ulpIoTag,
4118 						irsp->ulpContext);
4119 			}
4120 			break;
4121 		}
4122 
4123 		/*
4124 		 * The response IOCB has been processed.  Update the ring
4125 		 * pointer in SLIM.  If the port response put pointer has not
4126 		 * been updated, sync the pgp->rspPutInx and fetch the new port
4127 		 * response put pointer.
4128 		 */
4129 		writel(pring->sli.sli3.rspidx,
4130 			&phba->host_gp[pring->ringno].rspGetInx);
4131 
4132 		if (pring->sli.sli3.rspidx == portRspPut)
4133 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4134 	}
4135 
4136 	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4137 		pring->stats.iocb_rsp_full++;
4138 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4139 		writel(status, phba->CAregaddr);
4140 		readl(phba->CAregaddr);
4141 	}
4142 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4143 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4144 		pring->stats.iocb_cmd_empty++;
4145 
4146 		/* Force update of the local copy of cmdGetInx */
4147 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4148 		lpfc_sli_resume_iocb(phba, pring);
4149 
4150 		if ((pring->lpfc_sli_cmd_available))
4151 			(pring->lpfc_sli_cmd_available) (phba, pring);
4152 
4153 	}
4154 
4155 	phba->fcp_ring_in_use = 0;
4156 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4157 	return rc;
4158 }
4159 
4160 /**
4161  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4162  * @phba: Pointer to HBA context object.
4163  * @pring: Pointer to driver SLI ring object.
4164  * @rspiocbp: Pointer to driver response IOCB object.
4165  *
4166  * This function is called from the worker thread when there is a slow-path
4167  * response IOCB to process. This function chains all the response iocbs until
4168  * seeing the iocb with the LE bit set. The function will call
4169  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4170  * completion of a command iocb. The function will call the
4171  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4172  * The function frees the resources or calls the completion handler if this
4173  * iocb is an abort completion. The function returns NULL when the response
4174  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4175  * this function shall chain the iocb on to the iocb_continueq and return the
4176  * response iocb passed in.
4177  **/
4178 static struct lpfc_iocbq *
lpfc_sli_sp_handle_rspiocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * rspiocbp)4179 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4180 			struct lpfc_iocbq *rspiocbp)
4181 {
4182 	struct lpfc_iocbq *saveq;
4183 	struct lpfc_iocbq *cmdiocb;
4184 	struct lpfc_iocbq *next_iocb;
4185 	IOCB_t *irsp;
4186 	uint32_t free_saveq;
4187 	u8 cmd_type;
4188 	lpfc_iocb_type type;
4189 	unsigned long iflag;
4190 	u32 ulp_status = get_job_ulpstatus(phba, rspiocbp);
4191 	u32 ulp_word4 = get_job_word4(phba, rspiocbp);
4192 	u32 ulp_command = get_job_cmnd(phba, rspiocbp);
4193 	int rc;
4194 
4195 	spin_lock_irqsave(&phba->hbalock, iflag);
4196 	/* First add the response iocb to the countinueq list */
4197 	list_add_tail(&rspiocbp->list, &pring->iocb_continueq);
4198 	pring->iocb_continueq_cnt++;
4199 
4200 	/*
4201 	 * By default, the driver expects to free all resources
4202 	 * associated with this iocb completion.
4203 	 */
4204 	free_saveq = 1;
4205 	saveq = list_get_first(&pring->iocb_continueq,
4206 			       struct lpfc_iocbq, list);
4207 	list_del_init(&pring->iocb_continueq);
4208 	pring->iocb_continueq_cnt = 0;
4209 
4210 	pring->stats.iocb_rsp++;
4211 
4212 	/*
4213 	 * If resource errors reported from HBA, reduce
4214 	 * queuedepths of the SCSI device.
4215 	 */
4216 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
4217 	    ((ulp_word4 & IOERR_PARAM_MASK) ==
4218 	     IOERR_NO_RESOURCES)) {
4219 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4220 		phba->lpfc_rampdown_queue_depth(phba);
4221 		spin_lock_irqsave(&phba->hbalock, iflag);
4222 	}
4223 
4224 	if (ulp_status) {
4225 		/* Rsp ring <ringno> error: IOCB */
4226 		if (phba->sli_rev < LPFC_SLI_REV4) {
4227 			irsp = &rspiocbp->iocb;
4228 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4229 					"0328 Rsp Ring %d error: ulp_status x%x "
4230 					"IOCB Data: "
4231 					"x%08x x%08x x%08x x%08x "
4232 					"x%08x x%08x x%08x x%08x "
4233 					"x%08x x%08x x%08x x%08x "
4234 					"x%08x x%08x x%08x x%08x\n",
4235 					pring->ringno, ulp_status,
4236 					get_job_ulpword(rspiocbp, 0),
4237 					get_job_ulpword(rspiocbp, 1),
4238 					get_job_ulpword(rspiocbp, 2),
4239 					get_job_ulpword(rspiocbp, 3),
4240 					get_job_ulpword(rspiocbp, 4),
4241 					get_job_ulpword(rspiocbp, 5),
4242 					*(((uint32_t *)irsp) + 6),
4243 					*(((uint32_t *)irsp) + 7),
4244 					*(((uint32_t *)irsp) + 8),
4245 					*(((uint32_t *)irsp) + 9),
4246 					*(((uint32_t *)irsp) + 10),
4247 					*(((uint32_t *)irsp) + 11),
4248 					*(((uint32_t *)irsp) + 12),
4249 					*(((uint32_t *)irsp) + 13),
4250 					*(((uint32_t *)irsp) + 14),
4251 					*(((uint32_t *)irsp) + 15));
4252 		} else {
4253 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4254 					"0321 Rsp Ring %d error: "
4255 					"IOCB Data: "
4256 					"x%x x%x x%x x%x\n",
4257 					pring->ringno,
4258 					rspiocbp->wcqe_cmpl.word0,
4259 					rspiocbp->wcqe_cmpl.total_data_placed,
4260 					rspiocbp->wcqe_cmpl.parameter,
4261 					rspiocbp->wcqe_cmpl.word3);
4262 		}
4263 	}
4264 
4265 
4266 	/*
4267 	 * Fetch the iocb command type and call the correct completion
4268 	 * routine. Solicited and Unsolicited IOCBs on the ELS ring
4269 	 * get freed back to the lpfc_iocb_list by the discovery
4270 	 * kernel thread.
4271 	 */
4272 	cmd_type = ulp_command & CMD_IOCB_MASK;
4273 	type = lpfc_sli_iocb_cmd_type(cmd_type);
4274 	switch (type) {
4275 	case LPFC_SOL_IOCB:
4276 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4277 		rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4278 		spin_lock_irqsave(&phba->hbalock, iflag);
4279 		break;
4280 	case LPFC_UNSOL_IOCB:
4281 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4282 		rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4283 		spin_lock_irqsave(&phba->hbalock, iflag);
4284 		if (!rc)
4285 			free_saveq = 0;
4286 		break;
4287 	case LPFC_ABORT_IOCB:
4288 		cmdiocb = NULL;
4289 		if (ulp_command != CMD_XRI_ABORTED_CX)
4290 			cmdiocb = lpfc_sli_iocbq_lookup(phba, pring,
4291 							saveq);
4292 		if (cmdiocb) {
4293 			/* Call the specified completion routine */
4294 			if (cmdiocb->cmd_cmpl) {
4295 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4296 				cmdiocb->cmd_cmpl(phba, cmdiocb, saveq);
4297 				spin_lock_irqsave(&phba->hbalock, iflag);
4298 			} else {
4299 				__lpfc_sli_release_iocbq(phba, cmdiocb);
4300 			}
4301 		}
4302 		break;
4303 	case LPFC_UNKNOWN_IOCB:
4304 		if (ulp_command == CMD_ADAPTER_MSG) {
4305 			char adaptermsg[LPFC_MAX_ADPTMSG];
4306 
4307 			memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4308 			memcpy(&adaptermsg[0], (uint8_t *)&rspiocbp->wqe,
4309 			       MAX_MSG_DATA);
4310 			dev_warn(&((phba->pcidev)->dev),
4311 				 "lpfc%d: %s\n",
4312 				 phba->brd_no, adaptermsg);
4313 		} else {
4314 			/* Unknown command */
4315 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4316 					"0335 Unknown IOCB "
4317 					"command Data: x%x "
4318 					"x%x x%x x%x\n",
4319 					ulp_command,
4320 					ulp_status,
4321 					get_wqe_reqtag(rspiocbp),
4322 					get_job_ulpcontext(phba, rspiocbp));
4323 		}
4324 		break;
4325 	}
4326 
4327 	if (free_saveq) {
4328 		list_for_each_entry_safe(rspiocbp, next_iocb,
4329 					 &saveq->list, list) {
4330 			list_del_init(&rspiocbp->list);
4331 			__lpfc_sli_release_iocbq(phba, rspiocbp);
4332 		}
4333 		__lpfc_sli_release_iocbq(phba, saveq);
4334 	}
4335 	rspiocbp = NULL;
4336 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4337 	return rspiocbp;
4338 }
4339 
4340 /**
4341  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
4342  * @phba: Pointer to HBA context object.
4343  * @pring: Pointer to driver SLI ring object.
4344  * @mask: Host attention register mask for this ring.
4345  *
4346  * This routine wraps the actual slow_ring event process routine from the
4347  * API jump table function pointer from the lpfc_hba struct.
4348  **/
4349 void
lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4350 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4351 				struct lpfc_sli_ring *pring, uint32_t mask)
4352 {
4353 	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4354 }
4355 
4356 /**
4357  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4358  * @phba: Pointer to HBA context object.
4359  * @pring: Pointer to driver SLI ring object.
4360  * @mask: Host attention register mask for this ring.
4361  *
4362  * This function is called from the worker thread when there is a ring event
4363  * for non-fcp rings. The caller does not hold any lock. The function will
4364  * remove each response iocb in the response ring and calls the handle
4365  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4366  **/
4367 static void
lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4368 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4369 				   struct lpfc_sli_ring *pring, uint32_t mask)
4370 {
4371 	struct lpfc_pgp *pgp;
4372 	IOCB_t *entry;
4373 	IOCB_t *irsp = NULL;
4374 	struct lpfc_iocbq *rspiocbp = NULL;
4375 	uint32_t portRspPut, portRspMax;
4376 	unsigned long iflag;
4377 	uint32_t status;
4378 
4379 	pgp = &phba->port_gp[pring->ringno];
4380 	spin_lock_irqsave(&phba->hbalock, iflag);
4381 	pring->stats.iocb_event++;
4382 
4383 	/*
4384 	 * The next available response entry should never exceed the maximum
4385 	 * entries.  If it does, treat it as an adapter hardware error.
4386 	 */
4387 	portRspMax = pring->sli.sli3.numRiocb;
4388 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4389 	if (portRspPut >= portRspMax) {
4390 		/*
4391 		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4392 		 * rsp ring <portRspMax>
4393 		 */
4394 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4395 				"0303 Ring %d handler: portRspPut %d "
4396 				"is bigger than rsp ring %d\n",
4397 				pring->ringno, portRspPut, portRspMax);
4398 
4399 		phba->link_state = LPFC_HBA_ERROR;
4400 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4401 
4402 		phba->work_hs = HS_FFER3;
4403 		lpfc_handle_eratt(phba);
4404 
4405 		return;
4406 	}
4407 
4408 	rmb();
4409 	while (pring->sli.sli3.rspidx != portRspPut) {
4410 		/*
4411 		 * Build a completion list and call the appropriate handler.
4412 		 * The process is to get the next available response iocb, get
4413 		 * a free iocb from the list, copy the response data into the
4414 		 * free iocb, insert to the continuation list, and update the
4415 		 * next response index to slim.  This process makes response
4416 		 * iocb's in the ring available to DMA as fast as possible but
4417 		 * pays a penalty for a copy operation.  Since the iocb is
4418 		 * only 32 bytes, this penalty is considered small relative to
4419 		 * the PCI reads for register values and a slim write.  When
4420 		 * the ulpLe field is set, the entire Command has been
4421 		 * received.
4422 		 */
4423 		entry = lpfc_resp_iocb(phba, pring);
4424 
4425 		phba->last_completion_time = jiffies;
4426 		rspiocbp = __lpfc_sli_get_iocbq(phba);
4427 		if (rspiocbp == NULL) {
4428 			printk(KERN_ERR "%s: out of buffers! Failing "
4429 			       "completion.\n", __func__);
4430 			break;
4431 		}
4432 
4433 		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4434 				      phba->iocb_rsp_size);
4435 		irsp = &rspiocbp->iocb;
4436 
4437 		if (++pring->sli.sli3.rspidx >= portRspMax)
4438 			pring->sli.sli3.rspidx = 0;
4439 
4440 		if (pring->ringno == LPFC_ELS_RING) {
4441 			lpfc_debugfs_slow_ring_trc(phba,
4442 			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4443 				*(((uint32_t *) irsp) + 4),
4444 				*(((uint32_t *) irsp) + 6),
4445 				*(((uint32_t *) irsp) + 7));
4446 		}
4447 
4448 		writel(pring->sli.sli3.rspidx,
4449 			&phba->host_gp[pring->ringno].rspGetInx);
4450 
4451 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4452 		/* Handle the response IOCB */
4453 		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4454 		spin_lock_irqsave(&phba->hbalock, iflag);
4455 
4456 		/*
4457 		 * If the port response put pointer has not been updated, sync
4458 		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4459 		 * response put pointer.
4460 		 */
4461 		if (pring->sli.sli3.rspidx == portRspPut) {
4462 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4463 		}
4464 	} /* while (pring->sli.sli3.rspidx != portRspPut) */
4465 
4466 	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4467 		/* At least one response entry has been freed */
4468 		pring->stats.iocb_rsp_full++;
4469 		/* SET RxRE_RSP in Chip Att register */
4470 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4471 		writel(status, phba->CAregaddr);
4472 		readl(phba->CAregaddr); /* flush */
4473 	}
4474 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4475 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4476 		pring->stats.iocb_cmd_empty++;
4477 
4478 		/* Force update of the local copy of cmdGetInx */
4479 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4480 		lpfc_sli_resume_iocb(phba, pring);
4481 
4482 		if ((pring->lpfc_sli_cmd_available))
4483 			(pring->lpfc_sli_cmd_available) (phba, pring);
4484 
4485 	}
4486 
4487 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4488 	return;
4489 }
4490 
4491 /**
4492  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4493  * @phba: Pointer to HBA context object.
4494  * @pring: Pointer to driver SLI ring object.
4495  * @mask: Host attention register mask for this ring.
4496  *
4497  * This function is called from the worker thread when there is a pending
4498  * ELS response iocb on the driver internal slow-path response iocb worker
4499  * queue. The caller does not hold any lock. The function will remove each
4500  * response iocb from the response worker queue and calls the handle
4501  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4502  **/
4503 static void
lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4504 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4505 				   struct lpfc_sli_ring *pring, uint32_t mask)
4506 {
4507 	struct lpfc_iocbq *irspiocbq;
4508 	struct hbq_dmabuf *dmabuf;
4509 	struct lpfc_cq_event *cq_event;
4510 	unsigned long iflag;
4511 	int count = 0;
4512 
4513 	clear_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
4514 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4515 		/* Get the response iocb from the head of work queue */
4516 		spin_lock_irqsave(&phba->hbalock, iflag);
4517 		list_remove_head(&phba->sli4_hba.sp_queue_event,
4518 				 cq_event, struct lpfc_cq_event, list);
4519 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4520 
4521 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4522 		case CQE_CODE_COMPL_WQE:
4523 			irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4524 						 cq_event);
4525 			/* Translate ELS WCQE to response IOCBQ */
4526 			irspiocbq = lpfc_sli4_els_preprocess_rspiocbq(phba,
4527 								      irspiocbq);
4528 			if (irspiocbq)
4529 				lpfc_sli_sp_handle_rspiocb(phba, pring,
4530 							   irspiocbq);
4531 			count++;
4532 			break;
4533 		case CQE_CODE_RECEIVE:
4534 		case CQE_CODE_RECEIVE_V1:
4535 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
4536 					      cq_event);
4537 			lpfc_sli4_handle_received_buffer(phba, dmabuf);
4538 			count++;
4539 			break;
4540 		default:
4541 			break;
4542 		}
4543 
4544 		/* Limit the number of events to 64 to avoid soft lockups */
4545 		if (count == 64)
4546 			break;
4547 	}
4548 }
4549 
4550 /**
4551  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4552  * @phba: Pointer to HBA context object.
4553  * @pring: Pointer to driver SLI ring object.
4554  *
4555  * This function aborts all iocbs in the given ring and frees all the iocb
4556  * objects in txq. This function issues an abort iocb for all the iocb commands
4557  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4558  * the return of this function. The caller is not required to hold any locks.
4559  **/
4560 void
lpfc_sli_abort_iocb_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)4561 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4562 {
4563 	LIST_HEAD(tx_completions);
4564 	LIST_HEAD(txcmplq_completions);
4565 	struct lpfc_iocbq *iocb, *next_iocb;
4566 	int offline;
4567 
4568 	if (pring->ringno == LPFC_ELS_RING) {
4569 		lpfc_fabric_abort_hba(phba);
4570 	}
4571 	offline = pci_channel_offline(phba->pcidev);
4572 
4573 	/* Error everything on txq and txcmplq
4574 	 * First do the txq.
4575 	 */
4576 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4577 		spin_lock_irq(&pring->ring_lock);
4578 		list_splice_init(&pring->txq, &tx_completions);
4579 		pring->txq_cnt = 0;
4580 
4581 		if (offline) {
4582 			list_splice_init(&pring->txcmplq,
4583 					 &txcmplq_completions);
4584 		} else {
4585 			/* Next issue ABTS for everything on the txcmplq */
4586 			list_for_each_entry_safe(iocb, next_iocb,
4587 						 &pring->txcmplq, list)
4588 				lpfc_sli_issue_abort_iotag(phba, pring,
4589 							   iocb, NULL);
4590 		}
4591 		spin_unlock_irq(&pring->ring_lock);
4592 	} else {
4593 		spin_lock_irq(&phba->hbalock);
4594 		list_splice_init(&pring->txq, &tx_completions);
4595 		pring->txq_cnt = 0;
4596 
4597 		if (offline) {
4598 			list_splice_init(&pring->txcmplq, &txcmplq_completions);
4599 		} else {
4600 			/* Next issue ABTS for everything on the txcmplq */
4601 			list_for_each_entry_safe(iocb, next_iocb,
4602 						 &pring->txcmplq, list)
4603 				lpfc_sli_issue_abort_iotag(phba, pring,
4604 							   iocb, NULL);
4605 		}
4606 		spin_unlock_irq(&phba->hbalock);
4607 	}
4608 
4609 	if (offline) {
4610 		/* Cancel all the IOCBs from the completions list */
4611 		lpfc_sli_cancel_iocbs(phba, &txcmplq_completions,
4612 				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
4613 	} else {
4614 		/* Make sure HBA is alive */
4615 		lpfc_issue_hb_tmo(phba);
4616 	}
4617 	/* Cancel all the IOCBs from the completions list */
4618 	lpfc_sli_cancel_iocbs(phba, &tx_completions, IOSTAT_LOCAL_REJECT,
4619 			      IOERR_SLI_ABORTED);
4620 }
4621 
4622 /**
4623  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4624  * @phba: Pointer to HBA context object.
4625  *
4626  * This function aborts all iocbs in FCP rings and frees all the iocb
4627  * objects in txq. This function issues an abort iocb for all the iocb commands
4628  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4629  * the return of this function. The caller is not required to hold any locks.
4630  **/
4631 void
lpfc_sli_abort_fcp_rings(struct lpfc_hba * phba)4632 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4633 {
4634 	struct lpfc_sli *psli = &phba->sli;
4635 	struct lpfc_sli_ring  *pring;
4636 	uint32_t i;
4637 
4638 	/* Look on all the FCP Rings for the iotag */
4639 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4640 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4641 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4642 			lpfc_sli_abort_iocb_ring(phba, pring);
4643 		}
4644 	} else {
4645 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4646 		lpfc_sli_abort_iocb_ring(phba, pring);
4647 	}
4648 }
4649 
4650 /**
4651  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4652  * @phba: Pointer to HBA context object.
4653  *
4654  * This function flushes all iocbs in the IO ring and frees all the iocb
4655  * objects in txq and txcmplq. This function will not issue abort iocbs
4656  * for all the iocb commands in txcmplq, they will just be returned with
4657  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4658  * slot has been permanently disabled.
4659  **/
4660 void
lpfc_sli_flush_io_rings(struct lpfc_hba * phba)4661 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4662 {
4663 	LIST_HEAD(txq);
4664 	LIST_HEAD(txcmplq);
4665 	struct lpfc_sli *psli = &phba->sli;
4666 	struct lpfc_sli_ring  *pring;
4667 	uint32_t i;
4668 	struct lpfc_iocbq *piocb, *next_iocb;
4669 
4670 	/* Indicate the I/O queues are flushed */
4671 	set_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
4672 
4673 	/* Look on all the FCP Rings for the iotag */
4674 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4675 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4676 			if (!phba->sli4_hba.hdwq ||
4677 			    !phba->sli4_hba.hdwq[i].io_wq) {
4678 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4679 						"7777 hdwq's deleted %lx "
4680 						"%lx %x %x\n",
4681 						phba->pport->load_flag,
4682 						phba->hba_flag,
4683 						phba->link_state,
4684 						phba->sli.sli_flag);
4685 				return;
4686 			}
4687 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4688 
4689 			spin_lock_irq(&pring->ring_lock);
4690 			/* Retrieve everything on txq */
4691 			list_splice_init(&pring->txq, &txq);
4692 			list_for_each_entry_safe(piocb, next_iocb,
4693 						 &pring->txcmplq, list)
4694 				piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4695 			/* Retrieve everything on the txcmplq */
4696 			list_splice_init(&pring->txcmplq, &txcmplq);
4697 			pring->txq_cnt = 0;
4698 			pring->txcmplq_cnt = 0;
4699 			spin_unlock_irq(&pring->ring_lock);
4700 
4701 			/* Flush the txq */
4702 			lpfc_sli_cancel_iocbs(phba, &txq,
4703 					      IOSTAT_LOCAL_REJECT,
4704 					      IOERR_SLI_DOWN);
4705 			/* Flush the txcmplq */
4706 			lpfc_sli_cancel_iocbs(phba, &txcmplq,
4707 					      IOSTAT_LOCAL_REJECT,
4708 					      IOERR_SLI_DOWN);
4709 			if (unlikely(pci_channel_offline(phba->pcidev)))
4710 				lpfc_sli4_io_xri_aborted(phba, NULL, 0);
4711 		}
4712 	} else {
4713 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4714 
4715 		spin_lock_irq(&phba->hbalock);
4716 		/* Retrieve everything on txq */
4717 		list_splice_init(&pring->txq, &txq);
4718 		list_for_each_entry_safe(piocb, next_iocb,
4719 					 &pring->txcmplq, list)
4720 			piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4721 		/* Retrieve everything on the txcmplq */
4722 		list_splice_init(&pring->txcmplq, &txcmplq);
4723 		pring->txq_cnt = 0;
4724 		pring->txcmplq_cnt = 0;
4725 		spin_unlock_irq(&phba->hbalock);
4726 
4727 		/* Flush the txq */
4728 		lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4729 				      IOERR_SLI_DOWN);
4730 		/* Flush the txcmpq */
4731 		lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4732 				      IOERR_SLI_DOWN);
4733 	}
4734 }
4735 
4736 /**
4737  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4738  * @phba: Pointer to HBA context object.
4739  * @mask: Bit mask to be checked.
4740  *
4741  * This function reads the host status register and compares
4742  * with the provided bit mask to check if HBA completed
4743  * the restart. This function will wait in a loop for the
4744  * HBA to complete restart. If the HBA does not restart within
4745  * 15 iterations, the function will reset the HBA again. The
4746  * function returns 1 when HBA fail to restart otherwise returns
4747  * zero.
4748  **/
4749 static int
lpfc_sli_brdready_s3(struct lpfc_hba * phba,uint32_t mask)4750 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4751 {
4752 	uint32_t status;
4753 	int i = 0;
4754 	int retval = 0;
4755 
4756 	/* Read the HBA Host Status Register */
4757 	if (lpfc_readl(phba->HSregaddr, &status))
4758 		return 1;
4759 
4760 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
4761 
4762 	/*
4763 	 * Check status register every 100ms for 5 retries, then every
4764 	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4765 	 * every 2.5 sec for 4.
4766 	 * Break our of the loop if errors occurred during init.
4767 	 */
4768 	while (((status & mask) != mask) &&
4769 	       !(status & HS_FFERM) &&
4770 	       i++ < 20) {
4771 
4772 		if (i <= 5)
4773 			msleep(10);
4774 		else if (i <= 10)
4775 			msleep(500);
4776 		else
4777 			msleep(2500);
4778 
4779 		if (i == 15) {
4780 				/* Do post */
4781 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4782 			lpfc_sli_brdrestart(phba);
4783 		}
4784 		/* Read the HBA Host Status Register */
4785 		if (lpfc_readl(phba->HSregaddr, &status)) {
4786 			retval = 1;
4787 			break;
4788 		}
4789 	}
4790 
4791 	/* Check to see if any errors occurred during init */
4792 	if ((status & HS_FFERM) || (i >= 20)) {
4793 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4794 				"2751 Adapter failed to restart, "
4795 				"status reg x%x, FW Data: A8 x%x AC x%x\n",
4796 				status,
4797 				readl(phba->MBslimaddr + 0xa8),
4798 				readl(phba->MBslimaddr + 0xac));
4799 		phba->link_state = LPFC_HBA_ERROR;
4800 		retval = 1;
4801 	}
4802 
4803 	return retval;
4804 }
4805 
4806 /**
4807  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4808  * @phba: Pointer to HBA context object.
4809  * @mask: Bit mask to be checked.
4810  *
4811  * This function checks the host status register to check if HBA is
4812  * ready. This function will wait in a loop for the HBA to be ready
4813  * If the HBA is not ready , the function will will reset the HBA PCI
4814  * function again. The function returns 1 when HBA fail to be ready
4815  * otherwise returns zero.
4816  **/
4817 static int
lpfc_sli_brdready_s4(struct lpfc_hba * phba,uint32_t mask)4818 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4819 {
4820 	uint32_t status;
4821 	int retval = 0;
4822 
4823 	/* Read the HBA Host Status Register */
4824 	status = lpfc_sli4_post_status_check(phba);
4825 
4826 	if (status) {
4827 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4828 		lpfc_sli_brdrestart(phba);
4829 		status = lpfc_sli4_post_status_check(phba);
4830 	}
4831 
4832 	/* Check to see if any errors occurred during init */
4833 	if (status) {
4834 		phba->link_state = LPFC_HBA_ERROR;
4835 		retval = 1;
4836 	} else
4837 		phba->sli4_hba.intr_enable = 0;
4838 
4839 	clear_bit(HBA_SETUP, &phba->hba_flag);
4840 	return retval;
4841 }
4842 
4843 /**
4844  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4845  * @phba: Pointer to HBA context object.
4846  * @mask: Bit mask to be checked.
4847  *
4848  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4849  * from the API jump table function pointer from the lpfc_hba struct.
4850  **/
4851 int
lpfc_sli_brdready(struct lpfc_hba * phba,uint32_t mask)4852 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4853 {
4854 	return phba->lpfc_sli_brdready(phba, mask);
4855 }
4856 
4857 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4858 
4859 /**
4860  * lpfc_reset_barrier - Make HBA ready for HBA reset
4861  * @phba: Pointer to HBA context object.
4862  *
4863  * This function is called before resetting an HBA. This function is called
4864  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4865  **/
lpfc_reset_barrier(struct lpfc_hba * phba)4866 void lpfc_reset_barrier(struct lpfc_hba *phba)
4867 {
4868 	uint32_t __iomem *resp_buf;
4869 	uint32_t __iomem *mbox_buf;
4870 	volatile struct MAILBOX_word0 mbox;
4871 	uint32_t hc_copy, ha_copy, resp_data;
4872 	int  i;
4873 	uint8_t hdrtype;
4874 
4875 	lockdep_assert_held(&phba->hbalock);
4876 
4877 	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4878 	if (hdrtype != PCI_HEADER_TYPE_MFD ||
4879 	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4880 	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4881 		return;
4882 
4883 	/*
4884 	 * Tell the other part of the chip to suspend temporarily all
4885 	 * its DMA activity.
4886 	 */
4887 	resp_buf = phba->MBslimaddr;
4888 
4889 	/* Disable the error attention */
4890 	if (lpfc_readl(phba->HCregaddr, &hc_copy))
4891 		return;
4892 	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4893 	readl(phba->HCregaddr); /* flush */
4894 	phba->link_flag |= LS_IGNORE_ERATT;
4895 
4896 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
4897 		return;
4898 	if (ha_copy & HA_ERATT) {
4899 		/* Clear Chip error bit */
4900 		writel(HA_ERATT, phba->HAregaddr);
4901 		phba->pport->stopped = 1;
4902 	}
4903 
4904 	mbox.word0 = 0;
4905 	mbox.mbxCommand = MBX_KILL_BOARD;
4906 	mbox.mbxOwner = OWN_CHIP;
4907 
4908 	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4909 	mbox_buf = phba->MBslimaddr;
4910 	writel(mbox.word0, mbox_buf);
4911 
4912 	for (i = 0; i < 50; i++) {
4913 		if (lpfc_readl((resp_buf + 1), &resp_data))
4914 			return;
4915 		if (resp_data != ~(BARRIER_TEST_PATTERN))
4916 			mdelay(1);
4917 		else
4918 			break;
4919 	}
4920 	resp_data = 0;
4921 	if (lpfc_readl((resp_buf + 1), &resp_data))
4922 		return;
4923 	if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4924 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4925 		    phba->pport->stopped)
4926 			goto restore_hc;
4927 		else
4928 			goto clear_errat;
4929 	}
4930 
4931 	mbox.mbxOwner = OWN_HOST;
4932 	resp_data = 0;
4933 	for (i = 0; i < 500; i++) {
4934 		if (lpfc_readl(resp_buf, &resp_data))
4935 			return;
4936 		if (resp_data != mbox.word0)
4937 			mdelay(1);
4938 		else
4939 			break;
4940 	}
4941 
4942 clear_errat:
4943 
4944 	while (++i < 500) {
4945 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
4946 			return;
4947 		if (!(ha_copy & HA_ERATT))
4948 			mdelay(1);
4949 		else
4950 			break;
4951 	}
4952 
4953 	if (readl(phba->HAregaddr) & HA_ERATT) {
4954 		writel(HA_ERATT, phba->HAregaddr);
4955 		phba->pport->stopped = 1;
4956 	}
4957 
4958 restore_hc:
4959 	phba->link_flag &= ~LS_IGNORE_ERATT;
4960 	writel(hc_copy, phba->HCregaddr);
4961 	readl(phba->HCregaddr); /* flush */
4962 }
4963 
4964 /**
4965  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4966  * @phba: Pointer to HBA context object.
4967  *
4968  * This function issues a kill_board mailbox command and waits for
4969  * the error attention interrupt. This function is called for stopping
4970  * the firmware processing. The caller is not required to hold any
4971  * locks. This function calls lpfc_hba_down_post function to free
4972  * any pending commands after the kill. The function will return 1 when it
4973  * fails to kill the board else will return 0.
4974  **/
4975 int
lpfc_sli_brdkill(struct lpfc_hba * phba)4976 lpfc_sli_brdkill(struct lpfc_hba *phba)
4977 {
4978 	struct lpfc_sli *psli;
4979 	LPFC_MBOXQ_t *pmb;
4980 	uint32_t status;
4981 	uint32_t ha_copy;
4982 	int retval;
4983 	int i = 0;
4984 
4985 	psli = &phba->sli;
4986 
4987 	/* Kill HBA */
4988 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4989 			"0329 Kill HBA Data: x%x x%x\n",
4990 			phba->pport->port_state, psli->sli_flag);
4991 
4992 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4993 	if (!pmb)
4994 		return 1;
4995 
4996 	/* Disable the error attention */
4997 	spin_lock_irq(&phba->hbalock);
4998 	if (lpfc_readl(phba->HCregaddr, &status)) {
4999 		spin_unlock_irq(&phba->hbalock);
5000 		mempool_free(pmb, phba->mbox_mem_pool);
5001 		return 1;
5002 	}
5003 	status &= ~HC_ERINT_ENA;
5004 	writel(status, phba->HCregaddr);
5005 	readl(phba->HCregaddr); /* flush */
5006 	phba->link_flag |= LS_IGNORE_ERATT;
5007 	spin_unlock_irq(&phba->hbalock);
5008 
5009 	lpfc_kill_board(phba, pmb);
5010 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5011 	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5012 
5013 	if (retval != MBX_SUCCESS) {
5014 		if (retval != MBX_BUSY)
5015 			mempool_free(pmb, phba->mbox_mem_pool);
5016 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5017 				"2752 KILL_BOARD command failed retval %d\n",
5018 				retval);
5019 		spin_lock_irq(&phba->hbalock);
5020 		phba->link_flag &= ~LS_IGNORE_ERATT;
5021 		spin_unlock_irq(&phba->hbalock);
5022 		return 1;
5023 	}
5024 
5025 	spin_lock_irq(&phba->hbalock);
5026 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5027 	spin_unlock_irq(&phba->hbalock);
5028 
5029 	mempool_free(pmb, phba->mbox_mem_pool);
5030 
5031 	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
5032 	 * attention every 100ms for 3 seconds. If we don't get ERATT after
5033 	 * 3 seconds we still set HBA_ERROR state because the status of the
5034 	 * board is now undefined.
5035 	 */
5036 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
5037 		return 1;
5038 	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
5039 		mdelay(100);
5040 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
5041 			return 1;
5042 	}
5043 
5044 	timer_delete_sync(&psli->mbox_tmo);
5045 	if (ha_copy & HA_ERATT) {
5046 		writel(HA_ERATT, phba->HAregaddr);
5047 		phba->pport->stopped = 1;
5048 	}
5049 	spin_lock_irq(&phba->hbalock);
5050 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5051 	psli->mbox_active = NULL;
5052 	phba->link_flag &= ~LS_IGNORE_ERATT;
5053 	spin_unlock_irq(&phba->hbalock);
5054 
5055 	lpfc_hba_down_post(phba);
5056 	phba->link_state = LPFC_HBA_ERROR;
5057 
5058 	return ha_copy & HA_ERATT ? 0 : 1;
5059 }
5060 
5061 /**
5062  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
5063  * @phba: Pointer to HBA context object.
5064  *
5065  * This function resets the HBA by writing HC_INITFF to the control
5066  * register. After the HBA resets, this function resets all the iocb ring
5067  * indices. This function disables PCI layer parity checking during
5068  * the reset.
5069  * This function returns 0 always.
5070  * The caller is not required to hold any locks.
5071  **/
5072 int
lpfc_sli_brdreset(struct lpfc_hba * phba)5073 lpfc_sli_brdreset(struct lpfc_hba *phba)
5074 {
5075 	struct lpfc_sli *psli;
5076 	struct lpfc_sli_ring *pring;
5077 	uint16_t cfg_value;
5078 	int i;
5079 
5080 	psli = &phba->sli;
5081 
5082 	/* Reset HBA */
5083 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5084 			"0325 Reset HBA Data: x%x x%x\n",
5085 			(phba->pport) ? phba->pport->port_state : 0,
5086 			psli->sli_flag);
5087 
5088 	/* perform board reset */
5089 	phba->fc_eventTag = 0;
5090 	phba->link_events = 0;
5091 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5092 	if (phba->pport) {
5093 		phba->pport->fc_myDID = 0;
5094 		phba->pport->fc_prevDID = 0;
5095 	}
5096 
5097 	/* Turn off parity checking and serr during the physical reset */
5098 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
5099 		return -EIO;
5100 
5101 	pci_write_config_word(phba->pcidev, PCI_COMMAND,
5102 			      (cfg_value &
5103 			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5104 
5105 	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
5106 
5107 	/* Now toggle INITFF bit in the Host Control Register */
5108 	writel(HC_INITFF, phba->HCregaddr);
5109 	mdelay(1);
5110 	readl(phba->HCregaddr); /* flush */
5111 	writel(0, phba->HCregaddr);
5112 	readl(phba->HCregaddr); /* flush */
5113 
5114 	/* Restore PCI cmd register */
5115 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5116 
5117 	/* Initialize relevant SLI info */
5118 	for (i = 0; i < psli->num_rings; i++) {
5119 		pring = &psli->sli3_ring[i];
5120 		pring->flag = 0;
5121 		pring->sli.sli3.rspidx = 0;
5122 		pring->sli.sli3.next_cmdidx  = 0;
5123 		pring->sli.sli3.local_getidx = 0;
5124 		pring->sli.sli3.cmdidx = 0;
5125 		pring->missbufcnt = 0;
5126 	}
5127 
5128 	phba->link_state = LPFC_WARM_START;
5129 	return 0;
5130 }
5131 
5132 /**
5133  * lpfc_sli4_brdreset - Reset a sli-4 HBA
5134  * @phba: Pointer to HBA context object.
5135  *
5136  * This function resets a SLI4 HBA. This function disables PCI layer parity
5137  * checking during resets the device. The caller is not required to hold
5138  * any locks.
5139  *
5140  * This function returns 0 on success else returns negative error code.
5141  **/
5142 int
lpfc_sli4_brdreset(struct lpfc_hba * phba)5143 lpfc_sli4_brdreset(struct lpfc_hba *phba)
5144 {
5145 	struct lpfc_sli *psli = &phba->sli;
5146 	uint16_t cfg_value;
5147 	int rc = 0;
5148 
5149 	/* Reset HBA */
5150 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5151 			"0295 Reset HBA Data: x%x x%x x%lx\n",
5152 			phba->pport->port_state, psli->sli_flag,
5153 			phba->hba_flag);
5154 
5155 	/* perform board reset */
5156 	phba->fc_eventTag = 0;
5157 	phba->link_events = 0;
5158 	phba->pport->fc_myDID = 0;
5159 	phba->pport->fc_prevDID = 0;
5160 	clear_bit(HBA_SETUP, &phba->hba_flag);
5161 
5162 	spin_lock_irq(&phba->hbalock);
5163 	psli->sli_flag &= ~(LPFC_PROCESS_LA);
5164 	phba->fcf.fcf_flag = 0;
5165 	spin_unlock_irq(&phba->hbalock);
5166 
5167 	/* Now physically reset the device */
5168 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5169 			"0389 Performing PCI function reset!\n");
5170 
5171 	/* Turn off parity checking and serr during the physical reset */
5172 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5173 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5174 				"3205 PCI read Config failed\n");
5175 		return -EIO;
5176 	}
5177 
5178 	pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5179 			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5180 
5181 	/* Perform FCoE PCI function reset before freeing queue memory */
5182 	rc = lpfc_pci_function_reset(phba);
5183 
5184 	/* Restore PCI cmd register */
5185 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5186 
5187 	return rc;
5188 }
5189 
5190 /**
5191  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
5192  * @phba: Pointer to HBA context object.
5193  *
5194  * This function is called in the SLI initialization code path to
5195  * restart the HBA. The caller is not required to hold any lock.
5196  * This function writes MBX_RESTART mailbox command to the SLIM and
5197  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5198  * function to free any pending commands. The function enables
5199  * POST only during the first initialization. The function returns zero.
5200  * The function does not guarantee completion of MBX_RESTART mailbox
5201  * command before the return of this function.
5202  **/
5203 static int
lpfc_sli_brdrestart_s3(struct lpfc_hba * phba)5204 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
5205 {
5206 	volatile struct MAILBOX_word0 mb;
5207 	struct lpfc_sli *psli;
5208 	void __iomem *to_slim;
5209 
5210 	spin_lock_irq(&phba->hbalock);
5211 
5212 	psli = &phba->sli;
5213 
5214 	/* Restart HBA */
5215 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5216 			"0337 Restart HBA Data: x%x x%x\n",
5217 			(phba->pport) ? phba->pport->port_state : 0,
5218 			psli->sli_flag);
5219 
5220 	mb.word0 = 0;
5221 	mb.mbxCommand = MBX_RESTART;
5222 	mb.mbxHc = 1;
5223 
5224 	lpfc_reset_barrier(phba);
5225 
5226 	to_slim = phba->MBslimaddr;
5227 	writel(mb.word0, to_slim);
5228 	readl(to_slim); /* flush */
5229 
5230 	/* Only skip post after fc_ffinit is completed */
5231 	if (phba->pport && phba->pport->port_state)
5232 		mb.word0 = 1;	/* This is really setting up word1 */
5233 	else
5234 		mb.word0 = 0;	/* This is really setting up word1 */
5235 	to_slim = phba->MBslimaddr + sizeof (uint32_t);
5236 	writel(mb.word0, to_slim);
5237 	readl(to_slim); /* flush */
5238 
5239 	lpfc_sli_brdreset(phba);
5240 	if (phba->pport)
5241 		phba->pport->stopped = 0;
5242 	phba->link_state = LPFC_INIT_START;
5243 	phba->hba_flag = 0;
5244 	spin_unlock_irq(&phba->hbalock);
5245 
5246 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5247 	psli->stats_start = ktime_get_seconds();
5248 
5249 	/* Give the INITFF and Post time to settle. */
5250 	mdelay(100);
5251 
5252 	lpfc_hba_down_post(phba);
5253 
5254 	return 0;
5255 }
5256 
5257 /**
5258  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5259  * @phba: Pointer to HBA context object.
5260  *
5261  * This function is called in the SLI initialization code path to restart
5262  * a SLI4 HBA. The caller is not required to hold any lock.
5263  * At the end of the function, it calls lpfc_hba_down_post function to
5264  * free any pending commands.
5265  **/
5266 static int
lpfc_sli_brdrestart_s4(struct lpfc_hba * phba)5267 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5268 {
5269 	struct lpfc_sli *psli = &phba->sli;
5270 	int rc;
5271 
5272 	/* Restart HBA */
5273 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5274 			"0296 Restart HBA Data: x%x x%x\n",
5275 			phba->pport->port_state, psli->sli_flag);
5276 
5277 	lpfc_sli4_queue_unset(phba);
5278 
5279 	rc = lpfc_sli4_brdreset(phba);
5280 	if (rc) {
5281 		phba->link_state = LPFC_HBA_ERROR;
5282 		goto hba_down_queue;
5283 	}
5284 
5285 	spin_lock_irq(&phba->hbalock);
5286 	phba->pport->stopped = 0;
5287 	phba->link_state = LPFC_INIT_START;
5288 	phba->hba_flag = 0;
5289 	/* Preserve FA-PWWN expectation */
5290 	phba->sli4_hba.fawwpn_flag &= LPFC_FAWWPN_FABRIC;
5291 	spin_unlock_irq(&phba->hbalock);
5292 
5293 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5294 	psli->stats_start = ktime_get_seconds();
5295 
5296 hba_down_queue:
5297 	lpfc_hba_down_post(phba);
5298 	lpfc_sli4_queue_destroy(phba);
5299 
5300 	return rc;
5301 }
5302 
5303 /**
5304  * lpfc_sli_brdrestart - Wrapper func for restarting hba
5305  * @phba: Pointer to HBA context object.
5306  *
5307  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5308  * API jump table function pointer from the lpfc_hba struct.
5309 **/
5310 int
lpfc_sli_brdrestart(struct lpfc_hba * phba)5311 lpfc_sli_brdrestart(struct lpfc_hba *phba)
5312 {
5313 	return phba->lpfc_sli_brdrestart(phba);
5314 }
5315 
5316 /**
5317  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
5318  * @phba: Pointer to HBA context object.
5319  *
5320  * This function is called after a HBA restart to wait for successful
5321  * restart of the HBA. Successful restart of the HBA is indicated by
5322  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5323  * iteration, the function will restart the HBA again. The function returns
5324  * zero if HBA successfully restarted else returns negative error code.
5325  **/
5326 int
lpfc_sli_chipset_init(struct lpfc_hba * phba)5327 lpfc_sli_chipset_init(struct lpfc_hba *phba)
5328 {
5329 	uint32_t status, i = 0;
5330 
5331 	/* Read the HBA Host Status Register */
5332 	if (lpfc_readl(phba->HSregaddr, &status))
5333 		return -EIO;
5334 
5335 	/* Check status register to see what current state is */
5336 	i = 0;
5337 	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5338 
5339 		/* Check every 10ms for 10 retries, then every 100ms for 90
5340 		 * retries, then every 1 sec for 50 retires for a total of
5341 		 * ~60 seconds before reset the board again and check every
5342 		 * 1 sec for 50 retries. The up to 60 seconds before the
5343 		 * board ready is required by the Falcon FIPS zeroization
5344 		 * complete, and any reset the board in between shall cause
5345 		 * restart of zeroization, further delay the board ready.
5346 		 */
5347 		if (i++ >= 200) {
5348 			/* Adapter failed to init, timeout, status reg
5349 			   <status> */
5350 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5351 					"0436 Adapter failed to init, "
5352 					"timeout, status reg x%x, "
5353 					"FW Data: A8 x%x AC x%x\n", status,
5354 					readl(phba->MBslimaddr + 0xa8),
5355 					readl(phba->MBslimaddr + 0xac));
5356 			phba->link_state = LPFC_HBA_ERROR;
5357 			return -ETIMEDOUT;
5358 		}
5359 
5360 		/* Check to see if any errors occurred during init */
5361 		if (status & HS_FFERM) {
5362 			/* ERROR: During chipset initialization */
5363 			/* Adapter failed to init, chipset, status reg
5364 			   <status> */
5365 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5366 					"0437 Adapter failed to init, "
5367 					"chipset, status reg x%x, "
5368 					"FW Data: A8 x%x AC x%x\n", status,
5369 					readl(phba->MBslimaddr + 0xa8),
5370 					readl(phba->MBslimaddr + 0xac));
5371 			phba->link_state = LPFC_HBA_ERROR;
5372 			return -EIO;
5373 		}
5374 
5375 		if (i <= 10)
5376 			msleep(10);
5377 		else if (i <= 100)
5378 			msleep(100);
5379 		else
5380 			msleep(1000);
5381 
5382 		if (i == 150) {
5383 			/* Do post */
5384 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5385 			lpfc_sli_brdrestart(phba);
5386 		}
5387 		/* Read the HBA Host Status Register */
5388 		if (lpfc_readl(phba->HSregaddr, &status))
5389 			return -EIO;
5390 	}
5391 
5392 	/* Check to see if any errors occurred during init */
5393 	if (status & HS_FFERM) {
5394 		/* ERROR: During chipset initialization */
5395 		/* Adapter failed to init, chipset, status reg <status> */
5396 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5397 				"0438 Adapter failed to init, chipset, "
5398 				"status reg x%x, "
5399 				"FW Data: A8 x%x AC x%x\n", status,
5400 				readl(phba->MBslimaddr + 0xa8),
5401 				readl(phba->MBslimaddr + 0xac));
5402 		phba->link_state = LPFC_HBA_ERROR;
5403 		return -EIO;
5404 	}
5405 
5406 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5407 
5408 	/* Clear all interrupt enable conditions */
5409 	writel(0, phba->HCregaddr);
5410 	readl(phba->HCregaddr); /* flush */
5411 
5412 	/* setup host attn register */
5413 	writel(0xffffffff, phba->HAregaddr);
5414 	readl(phba->HAregaddr); /* flush */
5415 	return 0;
5416 }
5417 
5418 /**
5419  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5420  *
5421  * This function calculates and returns the number of HBQs required to be
5422  * configured.
5423  **/
5424 int
lpfc_sli_hbq_count(void)5425 lpfc_sli_hbq_count(void)
5426 {
5427 	return ARRAY_SIZE(lpfc_hbq_defs);
5428 }
5429 
5430 /**
5431  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5432  *
5433  * This function adds the number of hbq entries in every HBQ to get
5434  * the total number of hbq entries required for the HBA and returns
5435  * the total count.
5436  **/
5437 static int
lpfc_sli_hbq_entry_count(void)5438 lpfc_sli_hbq_entry_count(void)
5439 {
5440 	int  hbq_count = lpfc_sli_hbq_count();
5441 	int  count = 0;
5442 	int  i;
5443 
5444 	for (i = 0; i < hbq_count; ++i)
5445 		count += lpfc_hbq_defs[i]->entry_count;
5446 	return count;
5447 }
5448 
5449 /**
5450  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5451  *
5452  * This function calculates amount of memory required for all hbq entries
5453  * to be configured and returns the total memory required.
5454  **/
5455 int
lpfc_sli_hbq_size(void)5456 lpfc_sli_hbq_size(void)
5457 {
5458 	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5459 }
5460 
5461 /**
5462  * lpfc_sli_hbq_setup - configure and initialize HBQs
5463  * @phba: Pointer to HBA context object.
5464  *
5465  * This function is called during the SLI initialization to configure
5466  * all the HBQs and post buffers to the HBQ. The caller is not
5467  * required to hold any locks. This function will return zero if successful
5468  * else it will return negative error code.
5469  **/
5470 static int
lpfc_sli_hbq_setup(struct lpfc_hba * phba)5471 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5472 {
5473 	int  hbq_count = lpfc_sli_hbq_count();
5474 	LPFC_MBOXQ_t *pmb;
5475 	MAILBOX_t *pmbox;
5476 	uint32_t hbqno;
5477 	uint32_t hbq_entry_index;
5478 
5479 				/* Get a Mailbox buffer to setup mailbox
5480 				 * commands for HBA initialization
5481 				 */
5482 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5483 
5484 	if (!pmb)
5485 		return -ENOMEM;
5486 
5487 	pmbox = &pmb->u.mb;
5488 
5489 	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
5490 	phba->link_state = LPFC_INIT_MBX_CMDS;
5491 	phba->hbq_in_use = 1;
5492 
5493 	hbq_entry_index = 0;
5494 	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5495 		phba->hbqs[hbqno].next_hbqPutIdx = 0;
5496 		phba->hbqs[hbqno].hbqPutIdx      = 0;
5497 		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5498 		phba->hbqs[hbqno].entry_count =
5499 			lpfc_hbq_defs[hbqno]->entry_count;
5500 		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5501 			hbq_entry_index, pmb);
5502 		hbq_entry_index += phba->hbqs[hbqno].entry_count;
5503 
5504 		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5505 			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5506 			   mbxStatus <status>, ring <num> */
5507 
5508 			lpfc_printf_log(phba, KERN_ERR,
5509 					LOG_SLI | LOG_VPORT,
5510 					"1805 Adapter failed to init. "
5511 					"Data: x%x x%x x%x\n",
5512 					pmbox->mbxCommand,
5513 					pmbox->mbxStatus, hbqno);
5514 
5515 			phba->link_state = LPFC_HBA_ERROR;
5516 			mempool_free(pmb, phba->mbox_mem_pool);
5517 			return -ENXIO;
5518 		}
5519 	}
5520 	phba->hbq_count = hbq_count;
5521 
5522 	mempool_free(pmb, phba->mbox_mem_pool);
5523 
5524 	/* Initially populate or replenish the HBQs */
5525 	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5526 		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5527 	return 0;
5528 }
5529 
5530 /**
5531  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5532  * @phba: Pointer to HBA context object.
5533  *
5534  * This function is called during the SLI initialization to configure
5535  * all the HBQs and post buffers to the HBQ. The caller is not
5536  * required to hold any locks. This function will return zero if successful
5537  * else it will return negative error code.
5538  **/
5539 static int
lpfc_sli4_rb_setup(struct lpfc_hba * phba)5540 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5541 {
5542 	phba->hbq_in_use = 1;
5543 	/**
5544 	 * Specific case when the MDS diagnostics is enabled and supported.
5545 	 * The receive buffer count is truncated to manage the incoming
5546 	 * traffic.
5547 	 **/
5548 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5549 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5550 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5551 	else
5552 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5553 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5554 	phba->hbq_count = 1;
5555 	lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5556 	/* Initially populate or replenish the HBQs */
5557 	return 0;
5558 }
5559 
5560 /**
5561  * lpfc_sli_config_port - Issue config port mailbox command
5562  * @phba: Pointer to HBA context object.
5563  * @sli_mode: sli mode - 2/3
5564  *
5565  * This function is called by the sli initialization code path
5566  * to issue config_port mailbox command. This function restarts the
5567  * HBA firmware and issues a config_port mailbox command to configure
5568  * the SLI interface in the sli mode specified by sli_mode
5569  * variable. The caller is not required to hold any locks.
5570  * The function returns 0 if successful, else returns negative error
5571  * code.
5572  **/
5573 int
lpfc_sli_config_port(struct lpfc_hba * phba,int sli_mode)5574 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5575 {
5576 	LPFC_MBOXQ_t *pmb;
5577 	uint32_t resetcount = 0, rc = 0, done = 0;
5578 
5579 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5580 	if (!pmb) {
5581 		phba->link_state = LPFC_HBA_ERROR;
5582 		return -ENOMEM;
5583 	}
5584 
5585 	phba->sli_rev = sli_mode;
5586 	while (resetcount < 2 && !done) {
5587 		spin_lock_irq(&phba->hbalock);
5588 		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5589 		spin_unlock_irq(&phba->hbalock);
5590 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5591 		lpfc_sli_brdrestart(phba);
5592 		rc = lpfc_sli_chipset_init(phba);
5593 		if (rc)
5594 			break;
5595 
5596 		spin_lock_irq(&phba->hbalock);
5597 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5598 		spin_unlock_irq(&phba->hbalock);
5599 		resetcount++;
5600 
5601 		/* Call pre CONFIG_PORT mailbox command initialization.  A
5602 		 * value of 0 means the call was successful.  Any other
5603 		 * nonzero value is a failure, but if ERESTART is returned,
5604 		 * the driver may reset the HBA and try again.
5605 		 */
5606 		rc = lpfc_config_port_prep(phba);
5607 		if (rc == -ERESTART) {
5608 			phba->link_state = LPFC_LINK_UNKNOWN;
5609 			continue;
5610 		} else if (rc)
5611 			break;
5612 
5613 		phba->link_state = LPFC_INIT_MBX_CMDS;
5614 		lpfc_config_port(phba, pmb);
5615 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5616 		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5617 					LPFC_SLI3_HBQ_ENABLED |
5618 					LPFC_SLI3_CRP_ENABLED |
5619 					LPFC_SLI3_DSS_ENABLED);
5620 		if (rc != MBX_SUCCESS) {
5621 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5622 				"0442 Adapter failed to init, mbxCmd x%x "
5623 				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5624 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5625 			spin_lock_irq(&phba->hbalock);
5626 			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5627 			spin_unlock_irq(&phba->hbalock);
5628 			rc = -ENXIO;
5629 		} else {
5630 			/* Allow asynchronous mailbox command to go through */
5631 			spin_lock_irq(&phba->hbalock);
5632 			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5633 			spin_unlock_irq(&phba->hbalock);
5634 			done = 1;
5635 
5636 			if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5637 			    (pmb->u.mb.un.varCfgPort.gasabt == 0))
5638 				lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5639 					"3110 Port did not grant ASABT\n");
5640 		}
5641 	}
5642 	if (!done) {
5643 		rc = -EINVAL;
5644 		goto do_prep_failed;
5645 	}
5646 	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5647 		if (!pmb->u.mb.un.varCfgPort.cMA) {
5648 			rc = -ENXIO;
5649 			goto do_prep_failed;
5650 		}
5651 		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5652 			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5653 			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5654 			phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5655 				phba->max_vpi : phba->max_vports;
5656 
5657 		} else
5658 			phba->max_vpi = 0;
5659 		if (pmb->u.mb.un.varCfgPort.gerbm)
5660 			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5661 		if (pmb->u.mb.un.varCfgPort.gcrp)
5662 			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5663 
5664 		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5665 		phba->port_gp = phba->mbox->us.s3_pgp.port;
5666 
5667 		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5668 			if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5669 				phba->cfg_enable_bg = 0;
5670 				phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5671 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5672 						"0443 Adapter did not grant "
5673 						"BlockGuard\n");
5674 			}
5675 		}
5676 	} else {
5677 		phba->hbq_get = NULL;
5678 		phba->port_gp = phba->mbox->us.s2.port;
5679 		phba->max_vpi = 0;
5680 	}
5681 do_prep_failed:
5682 	mempool_free(pmb, phba->mbox_mem_pool);
5683 	return rc;
5684 }
5685 
5686 
5687 /**
5688  * lpfc_sli_hba_setup - SLI initialization function
5689  * @phba: Pointer to HBA context object.
5690  *
5691  * This function is the main SLI initialization function. This function
5692  * is called by the HBA initialization code, HBA reset code and HBA
5693  * error attention handler code. Caller is not required to hold any
5694  * locks. This function issues config_port mailbox command to configure
5695  * the SLI, setup iocb rings and HBQ rings. In the end the function
5696  * calls the config_port_post function to issue init_link mailbox
5697  * command and to start the discovery. The function will return zero
5698  * if successful, else it will return negative error code.
5699  **/
5700 int
lpfc_sli_hba_setup(struct lpfc_hba * phba)5701 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5702 {
5703 	uint32_t rc;
5704 	int  i;
5705 	int longs;
5706 
5707 	/* Enable ISR already does config_port because of config_msi mbx */
5708 	if (test_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag)) {
5709 		rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5710 		if (rc)
5711 			return -EIO;
5712 		clear_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5713 	}
5714 	phba->fcp_embed_io = 0;	/* SLI4 FC support only */
5715 
5716 	if (phba->sli_rev == 3) {
5717 		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5718 		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5719 	} else {
5720 		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5721 		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5722 		phba->sli3_options = 0;
5723 	}
5724 
5725 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5726 			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
5727 			phba->sli_rev, phba->max_vpi);
5728 	rc = lpfc_sli_ring_map(phba);
5729 
5730 	if (rc)
5731 		goto lpfc_sli_hba_setup_error;
5732 
5733 	/* Initialize VPIs. */
5734 	if (phba->sli_rev == LPFC_SLI_REV3) {
5735 		/*
5736 		 * The VPI bitmask and physical ID array are allocated
5737 		 * and initialized once only - at driver load.  A port
5738 		 * reset doesn't need to reinitialize this memory.
5739 		 */
5740 		if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5741 			longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5742 			phba->vpi_bmask = kcalloc(longs,
5743 						  sizeof(unsigned long),
5744 						  GFP_KERNEL);
5745 			if (!phba->vpi_bmask) {
5746 				rc = -ENOMEM;
5747 				goto lpfc_sli_hba_setup_error;
5748 			}
5749 
5750 			phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5751 						sizeof(uint16_t),
5752 						GFP_KERNEL);
5753 			if (!phba->vpi_ids) {
5754 				kfree(phba->vpi_bmask);
5755 				rc = -ENOMEM;
5756 				goto lpfc_sli_hba_setup_error;
5757 			}
5758 			for (i = 0; i < phba->max_vpi; i++)
5759 				phba->vpi_ids[i] = i;
5760 		}
5761 	}
5762 
5763 	/* Init HBQs */
5764 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5765 		rc = lpfc_sli_hbq_setup(phba);
5766 		if (rc)
5767 			goto lpfc_sli_hba_setup_error;
5768 	}
5769 	spin_lock_irq(&phba->hbalock);
5770 	phba->sli.sli_flag |= LPFC_PROCESS_LA;
5771 	spin_unlock_irq(&phba->hbalock);
5772 
5773 	rc = lpfc_config_port_post(phba);
5774 	if (rc)
5775 		goto lpfc_sli_hba_setup_error;
5776 
5777 	return rc;
5778 
5779 lpfc_sli_hba_setup_error:
5780 	phba->link_state = LPFC_HBA_ERROR;
5781 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5782 			"0445 Firmware initialization failed\n");
5783 	return rc;
5784 }
5785 
5786 /**
5787  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5788  * @phba: Pointer to HBA context object.
5789  *
5790  * This function issue a dump mailbox command to read config region
5791  * 23 and parse the records in the region and populate driver
5792  * data structure.
5793  **/
5794 static int
lpfc_sli4_read_fcoe_params(struct lpfc_hba * phba)5795 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5796 {
5797 	LPFC_MBOXQ_t *mboxq;
5798 	struct lpfc_dmabuf *mp;
5799 	struct lpfc_mqe *mqe;
5800 	uint32_t data_length;
5801 	int rc;
5802 
5803 	/* Program the default value of vlan_id and fc_map */
5804 	phba->valid_vlan = 0;
5805 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5806 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5807 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5808 
5809 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5810 	if (!mboxq)
5811 		return -ENOMEM;
5812 
5813 	mqe = &mboxq->u.mqe;
5814 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5815 		rc = -ENOMEM;
5816 		goto out_free_mboxq;
5817 	}
5818 
5819 	mp = mboxq->ctx_buf;
5820 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5821 
5822 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5823 			"(%d):2571 Mailbox cmd x%x Status x%x "
5824 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5825 			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5826 			"CQ: x%x x%x x%x x%x\n",
5827 			mboxq->vport ? mboxq->vport->vpi : 0,
5828 			bf_get(lpfc_mqe_command, mqe),
5829 			bf_get(lpfc_mqe_status, mqe),
5830 			mqe->un.mb_words[0], mqe->un.mb_words[1],
5831 			mqe->un.mb_words[2], mqe->un.mb_words[3],
5832 			mqe->un.mb_words[4], mqe->un.mb_words[5],
5833 			mqe->un.mb_words[6], mqe->un.mb_words[7],
5834 			mqe->un.mb_words[8], mqe->un.mb_words[9],
5835 			mqe->un.mb_words[10], mqe->un.mb_words[11],
5836 			mqe->un.mb_words[12], mqe->un.mb_words[13],
5837 			mqe->un.mb_words[14], mqe->un.mb_words[15],
5838 			mqe->un.mb_words[16], mqe->un.mb_words[50],
5839 			mboxq->mcqe.word0,
5840 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
5841 			mboxq->mcqe.trailer);
5842 
5843 	if (rc) {
5844 		rc = -EIO;
5845 		goto out_free_mboxq;
5846 	}
5847 	data_length = mqe->un.mb_words[5];
5848 	if (data_length > DMP_RGN23_SIZE) {
5849 		rc = -EIO;
5850 		goto out_free_mboxq;
5851 	}
5852 
5853 	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5854 	rc = 0;
5855 
5856 out_free_mboxq:
5857 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
5858 	return rc;
5859 }
5860 
5861 /**
5862  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5863  * @phba: pointer to lpfc hba data structure.
5864  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5865  * @vpd: pointer to the memory to hold resulting port vpd data.
5866  * @vpd_size: On input, the number of bytes allocated to @vpd.
5867  *	      On output, the number of data bytes in @vpd.
5868  *
5869  * This routine executes a READ_REV SLI4 mailbox command.  In
5870  * addition, this routine gets the port vpd data.
5871  *
5872  * Return codes
5873  * 	0 - successful
5874  * 	-ENOMEM - could not allocated memory.
5875  **/
5876 static int
lpfc_sli4_read_rev(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint8_t * vpd,uint32_t * vpd_size)5877 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5878 		    uint8_t *vpd, uint32_t *vpd_size)
5879 {
5880 	int rc = 0;
5881 	uint32_t dma_size;
5882 	struct lpfc_dmabuf *dmabuf;
5883 	struct lpfc_mqe *mqe;
5884 
5885 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5886 	if (!dmabuf)
5887 		return -ENOMEM;
5888 
5889 	/*
5890 	 * Get a DMA buffer for the vpd data resulting from the READ_REV
5891 	 * mailbox command.
5892 	 */
5893 	dma_size = *vpd_size;
5894 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5895 					  &dmabuf->phys, GFP_KERNEL);
5896 	if (!dmabuf->virt) {
5897 		kfree(dmabuf);
5898 		return -ENOMEM;
5899 	}
5900 
5901 	/*
5902 	 * The SLI4 implementation of READ_REV conflicts at word1,
5903 	 * bits 31:16 and SLI4 adds vpd functionality not present
5904 	 * in SLI3.  This code corrects the conflicts.
5905 	 */
5906 	lpfc_read_rev(phba, mboxq);
5907 	mqe = &mboxq->u.mqe;
5908 	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5909 	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5910 	mqe->un.read_rev.word1 &= 0x0000FFFF;
5911 	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5912 	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5913 
5914 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5915 	if (rc) {
5916 		dma_free_coherent(&phba->pcidev->dev, dma_size,
5917 				  dmabuf->virt, dmabuf->phys);
5918 		kfree(dmabuf);
5919 		return -EIO;
5920 	}
5921 
5922 	/*
5923 	 * The available vpd length cannot be bigger than the
5924 	 * DMA buffer passed to the port.  Catch the less than
5925 	 * case and update the caller's size.
5926 	 */
5927 	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5928 		*vpd_size = mqe->un.read_rev.avail_vpd_len;
5929 
5930 	memcpy(vpd, dmabuf->virt, *vpd_size);
5931 
5932 	dma_free_coherent(&phba->pcidev->dev, dma_size,
5933 			  dmabuf->virt, dmabuf->phys);
5934 	kfree(dmabuf);
5935 	return 0;
5936 }
5937 
5938 /**
5939  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5940  * @phba: pointer to lpfc hba data structure.
5941  *
5942  * This routine retrieves SLI4 device physical port name this PCI function
5943  * is attached to.
5944  *
5945  * Return codes
5946  *      0 - successful
5947  *      otherwise - failed to retrieve controller attributes
5948  **/
5949 static int
lpfc_sli4_get_ctl_attr(struct lpfc_hba * phba)5950 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5951 {
5952 	LPFC_MBOXQ_t *mboxq;
5953 	struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5954 	struct lpfc_controller_attribute *cntl_attr;
5955 	void *virtaddr = NULL;
5956 	uint32_t alloclen, reqlen;
5957 	uint32_t shdr_status, shdr_add_status;
5958 	union lpfc_sli4_cfg_shdr *shdr;
5959 	int rc;
5960 
5961 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5962 	if (!mboxq)
5963 		return -ENOMEM;
5964 
5965 	/* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5966 	reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5967 	alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5968 			LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5969 			LPFC_SLI4_MBX_NEMBED);
5970 
5971 	if (alloclen < reqlen) {
5972 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5973 				"3084 Allocated DMA memory size (%d) is "
5974 				"less than the requested DMA memory size "
5975 				"(%d)\n", alloclen, reqlen);
5976 		rc = -ENOMEM;
5977 		goto out_free_mboxq;
5978 	}
5979 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5980 	virtaddr = mboxq->sge_array->addr[0];
5981 	mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5982 	shdr = &mbx_cntl_attr->cfg_shdr;
5983 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5984 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5985 	if (shdr_status || shdr_add_status || rc) {
5986 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5987 				"3085 Mailbox x%x (x%x/x%x) failed, "
5988 				"rc:x%x, status:x%x, add_status:x%x\n",
5989 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5990 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5991 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5992 				rc, shdr_status, shdr_add_status);
5993 		rc = -ENXIO;
5994 		goto out_free_mboxq;
5995 	}
5996 
5997 	cntl_attr = &mbx_cntl_attr->cntl_attr;
5998 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5999 	phba->sli4_hba.lnk_info.lnk_tp =
6000 		bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
6001 	phba->sli4_hba.lnk_info.lnk_no =
6002 		bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
6003 	phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
6004 	phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
6005 
6006 	memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
6007 	strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
6008 		sizeof(phba->BIOSVersion));
6009 
6010 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6011 			"3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
6012 			"flash_id: x%02x, asic_rev: x%02x\n",
6013 			phba->sli4_hba.lnk_info.lnk_tp,
6014 			phba->sli4_hba.lnk_info.lnk_no,
6015 			phba->BIOSVersion, phba->sli4_hba.flash_id,
6016 			phba->sli4_hba.asic_rev);
6017 out_free_mboxq:
6018 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6019 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6020 	else
6021 		mempool_free(mboxq, phba->mbox_mem_pool);
6022 	return rc;
6023 }
6024 
6025 /**
6026  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
6027  * @phba: pointer to lpfc hba data structure.
6028  *
6029  * This routine retrieves SLI4 device physical port name this PCI function
6030  * is attached to.
6031  *
6032  * Return codes
6033  *      0 - successful
6034  *      otherwise - failed to retrieve physical port name
6035  **/
6036 static int
lpfc_sli4_retrieve_pport_name(struct lpfc_hba * phba)6037 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
6038 {
6039 	LPFC_MBOXQ_t *mboxq;
6040 	struct lpfc_mbx_get_port_name *get_port_name;
6041 	uint32_t shdr_status, shdr_add_status;
6042 	union lpfc_sli4_cfg_shdr *shdr;
6043 	char cport_name = 0;
6044 	int rc;
6045 
6046 	/* We assume nothing at this point */
6047 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6048 	phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
6049 
6050 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6051 	if (!mboxq)
6052 		return -ENOMEM;
6053 	/* obtain link type and link number via READ_CONFIG */
6054 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6055 	lpfc_sli4_read_config(phba);
6056 
6057 	if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG)
6058 		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
6059 
6060 	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
6061 		goto retrieve_ppname;
6062 
6063 	/* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
6064 	rc = lpfc_sli4_get_ctl_attr(phba);
6065 	if (rc)
6066 		goto out_free_mboxq;
6067 
6068 retrieve_ppname:
6069 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6070 		LPFC_MBOX_OPCODE_GET_PORT_NAME,
6071 		sizeof(struct lpfc_mbx_get_port_name) -
6072 		sizeof(struct lpfc_sli4_cfg_mhdr),
6073 		LPFC_SLI4_MBX_EMBED);
6074 	get_port_name = &mboxq->u.mqe.un.get_port_name;
6075 	shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
6076 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
6077 	bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
6078 		phba->sli4_hba.lnk_info.lnk_tp);
6079 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6080 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6081 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6082 	if (shdr_status || shdr_add_status || rc) {
6083 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6084 				"3087 Mailbox x%x (x%x/x%x) failed: "
6085 				"rc:x%x, status:x%x, add_status:x%x\n",
6086 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6087 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6088 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6089 				rc, shdr_status, shdr_add_status);
6090 		rc = -ENXIO;
6091 		goto out_free_mboxq;
6092 	}
6093 	switch (phba->sli4_hba.lnk_info.lnk_no) {
6094 	case LPFC_LINK_NUMBER_0:
6095 		cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6096 				&get_port_name->u.response);
6097 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6098 		break;
6099 	case LPFC_LINK_NUMBER_1:
6100 		cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6101 				&get_port_name->u.response);
6102 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6103 		break;
6104 	case LPFC_LINK_NUMBER_2:
6105 		cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6106 				&get_port_name->u.response);
6107 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6108 		break;
6109 	case LPFC_LINK_NUMBER_3:
6110 		cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6111 				&get_port_name->u.response);
6112 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6113 		break;
6114 	default:
6115 		break;
6116 	}
6117 
6118 	if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6119 		phba->Port[0] = cport_name;
6120 		phba->Port[1] = '\0';
6121 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6122 				"3091 SLI get port name: %s\n", phba->Port);
6123 	}
6124 
6125 out_free_mboxq:
6126 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6127 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6128 	else
6129 		mempool_free(mboxq, phba->mbox_mem_pool);
6130 	return rc;
6131 }
6132 
6133 /**
6134  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6135  * @phba: pointer to lpfc hba data structure.
6136  *
6137  * This routine is called to explicitly arm the SLI4 device's completion and
6138  * event queues
6139  **/
6140 static void
lpfc_sli4_arm_cqeq_intr(struct lpfc_hba * phba)6141 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6142 {
6143 	int qidx;
6144 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
6145 	struct lpfc_sli4_hdw_queue *qp;
6146 	struct lpfc_queue *eq;
6147 
6148 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6149 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
6150 	if (sli4_hba->nvmels_cq)
6151 		sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6152 					   LPFC_QUEUE_REARM);
6153 
6154 	if (sli4_hba->hdwq) {
6155 		/* Loop thru all Hardware Queues */
6156 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
6157 			qp = &sli4_hba->hdwq[qidx];
6158 			/* ARM the corresponding CQ */
6159 			sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
6160 						LPFC_QUEUE_REARM);
6161 		}
6162 
6163 		/* Loop thru all IRQ vectors */
6164 		for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6165 			eq = sli4_hba->hba_eq_hdl[qidx].eq;
6166 			/* ARM the corresponding EQ */
6167 			sli4_hba->sli4_write_eq_db(phba, eq,
6168 						   0, LPFC_QUEUE_REARM);
6169 		}
6170 	}
6171 
6172 	if (phba->nvmet_support) {
6173 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
6174 			sli4_hba->sli4_write_cq_db(phba,
6175 				sli4_hba->nvmet_cqset[qidx], 0,
6176 				LPFC_QUEUE_REARM);
6177 		}
6178 	}
6179 }
6180 
6181 /**
6182  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6183  * @phba: Pointer to HBA context object.
6184  * @type: The resource extent type.
6185  * @extnt_count: buffer to hold port available extent count.
6186  * @extnt_size: buffer to hold element count per extent.
6187  *
6188  * This function calls the port and retrievs the number of available
6189  * extents and their size for a particular extent type.
6190  *
6191  * Returns: 0 if successful.  Nonzero otherwise.
6192  **/
6193 int
lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_count,uint16_t * extnt_size)6194 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6195 			       uint16_t *extnt_count, uint16_t *extnt_size)
6196 {
6197 	int rc = 0;
6198 	uint32_t length;
6199 	uint32_t mbox_tmo;
6200 	struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6201 	LPFC_MBOXQ_t *mbox;
6202 
6203 	*extnt_count = 0;
6204 	*extnt_size = 0;
6205 
6206 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6207 	if (!mbox)
6208 		return -ENOMEM;
6209 
6210 	/* Find out how many extents are available for this resource type */
6211 	length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6212 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6213 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6214 			 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6215 			 length, LPFC_SLI4_MBX_EMBED);
6216 
6217 	/* Send an extents count of 0 - the GET doesn't use it. */
6218 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6219 					LPFC_SLI4_MBX_EMBED);
6220 	if (unlikely(rc)) {
6221 		rc = -EIO;
6222 		goto err_exit;
6223 	}
6224 
6225 	if (!phba->sli4_hba.intr_enable)
6226 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6227 	else {
6228 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6229 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6230 	}
6231 	if (unlikely(rc)) {
6232 		rc = -EIO;
6233 		goto err_exit;
6234 	}
6235 
6236 	rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6237 	if (bf_get(lpfc_mbox_hdr_status,
6238 		   &rsrc_info->header.cfg_shdr.response)) {
6239 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6240 				"2930 Failed to get resource extents "
6241 				"Status 0x%x Add'l Status 0x%x\n",
6242 				bf_get(lpfc_mbox_hdr_status,
6243 				       &rsrc_info->header.cfg_shdr.response),
6244 				bf_get(lpfc_mbox_hdr_add_status,
6245 				       &rsrc_info->header.cfg_shdr.response));
6246 		rc = -EIO;
6247 		goto err_exit;
6248 	}
6249 
6250 	*extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6251 			      &rsrc_info->u.rsp);
6252 	*extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6253 			     &rsrc_info->u.rsp);
6254 
6255 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6256 			"3162 Retrieved extents type-%d from port: count:%d, "
6257 			"size:%d\n", type, *extnt_count, *extnt_size);
6258 
6259 err_exit:
6260 	mempool_free(mbox, phba->mbox_mem_pool);
6261 	return rc;
6262 }
6263 
6264 /**
6265  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6266  * @phba: Pointer to HBA context object.
6267  * @type: The extent type to check.
6268  *
6269  * This function reads the current available extents from the port and checks
6270  * if the extent count or extent size has changed since the last access.
6271  * Callers use this routine post port reset to understand if there is a
6272  * extent reprovisioning requirement.
6273  *
6274  * Returns:
6275  *   -Error: error indicates problem.
6276  *   1: Extent count or size has changed.
6277  *   0: No changes.
6278  **/
6279 static int
lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type)6280 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6281 {
6282 	uint16_t curr_ext_cnt, rsrc_ext_cnt;
6283 	uint16_t size_diff, rsrc_ext_size;
6284 	int rc = 0;
6285 	struct lpfc_rsrc_blks *rsrc_entry;
6286 	struct list_head *rsrc_blk_list = NULL;
6287 
6288 	size_diff = 0;
6289 	curr_ext_cnt = 0;
6290 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6291 					    &rsrc_ext_cnt,
6292 					    &rsrc_ext_size);
6293 	if (unlikely(rc))
6294 		return -EIO;
6295 
6296 	switch (type) {
6297 	case LPFC_RSC_TYPE_FCOE_RPI:
6298 		rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6299 		break;
6300 	case LPFC_RSC_TYPE_FCOE_VPI:
6301 		rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6302 		break;
6303 	case LPFC_RSC_TYPE_FCOE_XRI:
6304 		rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6305 		break;
6306 	case LPFC_RSC_TYPE_FCOE_VFI:
6307 		rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6308 		break;
6309 	default:
6310 		break;
6311 	}
6312 
6313 	list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6314 		curr_ext_cnt++;
6315 		if (rsrc_entry->rsrc_size != rsrc_ext_size)
6316 			size_diff++;
6317 	}
6318 
6319 	if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6320 		rc = 1;
6321 
6322 	return rc;
6323 }
6324 
6325 /**
6326  * lpfc_sli4_cfg_post_extnts -
6327  * @phba: Pointer to HBA context object.
6328  * @extnt_cnt: number of available extents.
6329  * @type: the extent type (rpi, xri, vfi, vpi).
6330  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6331  * @mbox: pointer to the caller's allocated mailbox structure.
6332  *
6333  * This function executes the extents allocation request.  It also
6334  * takes care of the amount of memory needed to allocate or get the
6335  * allocated extents. It is the caller's responsibility to evaluate
6336  * the response.
6337  *
6338  * Returns:
6339  *   -Error:  Error value describes the condition found.
6340  *   0: if successful
6341  **/
6342 static int
lpfc_sli4_cfg_post_extnts(struct lpfc_hba * phba,uint16_t extnt_cnt,uint16_t type,bool * emb,LPFC_MBOXQ_t * mbox)6343 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6344 			  uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6345 {
6346 	int rc = 0;
6347 	uint32_t req_len;
6348 	uint32_t emb_len;
6349 	uint32_t alloc_len, mbox_tmo;
6350 
6351 	/* Calculate the total requested length of the dma memory */
6352 	req_len = extnt_cnt * sizeof(uint16_t);
6353 
6354 	/*
6355 	 * Calculate the size of an embedded mailbox.  The uint32_t
6356 	 * accounts for extents-specific word.
6357 	 */
6358 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6359 		sizeof(uint32_t);
6360 
6361 	/*
6362 	 * Presume the allocation and response will fit into an embedded
6363 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6364 	 */
6365 	*emb = LPFC_SLI4_MBX_EMBED;
6366 	if (req_len > emb_len) {
6367 		req_len = extnt_cnt * sizeof(uint16_t) +
6368 			sizeof(union lpfc_sli4_cfg_shdr) +
6369 			sizeof(uint32_t);
6370 		*emb = LPFC_SLI4_MBX_NEMBED;
6371 	}
6372 
6373 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6374 				     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6375 				     req_len, *emb);
6376 	if (alloc_len < req_len) {
6377 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6378 			"2982 Allocated DMA memory size (x%x) is "
6379 			"less than the requested DMA memory "
6380 			"size (x%x)\n", alloc_len, req_len);
6381 		return -ENOMEM;
6382 	}
6383 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6384 	if (unlikely(rc))
6385 		return -EIO;
6386 
6387 	if (!phba->sli4_hba.intr_enable)
6388 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6389 	else {
6390 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6391 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6392 	}
6393 
6394 	if (unlikely(rc))
6395 		rc = -EIO;
6396 	return rc;
6397 }
6398 
6399 /**
6400  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6401  * @phba: Pointer to HBA context object.
6402  * @type:  The resource extent type to allocate.
6403  *
6404  * This function allocates the number of elements for the specified
6405  * resource type.
6406  **/
6407 static int
lpfc_sli4_alloc_extent(struct lpfc_hba * phba,uint16_t type)6408 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6409 {
6410 	bool emb = false;
6411 	uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6412 	uint16_t rsrc_id, rsrc_start, j, k;
6413 	uint16_t *ids;
6414 	int i, rc;
6415 	unsigned long longs;
6416 	unsigned long *bmask;
6417 	struct lpfc_rsrc_blks *rsrc_blks;
6418 	LPFC_MBOXQ_t *mbox;
6419 	uint32_t length;
6420 	struct lpfc_id_range *id_array = NULL;
6421 	void *virtaddr = NULL;
6422 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6423 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6424 	struct list_head *ext_blk_list;
6425 
6426 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6427 					    &rsrc_cnt,
6428 					    &rsrc_size);
6429 	if (unlikely(rc))
6430 		return -EIO;
6431 
6432 	if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6433 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6434 			"3009 No available Resource Extents "
6435 			"for resource type 0x%x: Count: 0x%x, "
6436 			"Size 0x%x\n", type, rsrc_cnt,
6437 			rsrc_size);
6438 		return -ENOMEM;
6439 	}
6440 
6441 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6442 			"2903 Post resource extents type-0x%x: "
6443 			"count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6444 
6445 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6446 	if (!mbox)
6447 		return -ENOMEM;
6448 
6449 	rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6450 	if (unlikely(rc)) {
6451 		rc = -EIO;
6452 		goto err_exit;
6453 	}
6454 
6455 	/*
6456 	 * Figure out where the response is located.  Then get local pointers
6457 	 * to the response data.  The port does not guarantee to respond to
6458 	 * all extents counts request so update the local variable with the
6459 	 * allocated count from the port.
6460 	 */
6461 	if (emb == LPFC_SLI4_MBX_EMBED) {
6462 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6463 		id_array = &rsrc_ext->u.rsp.id[0];
6464 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6465 	} else {
6466 		virtaddr = mbox->sge_array->addr[0];
6467 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6468 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6469 		id_array = &n_rsrc->id;
6470 	}
6471 
6472 	longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6473 	rsrc_id_cnt = rsrc_cnt * rsrc_size;
6474 
6475 	/*
6476 	 * Based on the resource size and count, correct the base and max
6477 	 * resource values.
6478 	 */
6479 	length = sizeof(struct lpfc_rsrc_blks);
6480 	switch (type) {
6481 	case LPFC_RSC_TYPE_FCOE_RPI:
6482 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
6483 						   sizeof(unsigned long),
6484 						   GFP_KERNEL);
6485 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6486 			rc = -ENOMEM;
6487 			goto err_exit;
6488 		}
6489 		phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6490 						 sizeof(uint16_t),
6491 						 GFP_KERNEL);
6492 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
6493 			kfree(phba->sli4_hba.rpi_bmask);
6494 			rc = -ENOMEM;
6495 			goto err_exit;
6496 		}
6497 
6498 		/*
6499 		 * The next_rpi was initialized with the maximum available
6500 		 * count but the port may allocate a smaller number.  Catch
6501 		 * that case and update the next_rpi.
6502 		 */
6503 		phba->sli4_hba.next_rpi = rsrc_id_cnt;
6504 
6505 		/* Initialize local ptrs for common extent processing later. */
6506 		bmask = phba->sli4_hba.rpi_bmask;
6507 		ids = phba->sli4_hba.rpi_ids;
6508 		ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6509 		break;
6510 	case LPFC_RSC_TYPE_FCOE_VPI:
6511 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6512 					  GFP_KERNEL);
6513 		if (unlikely(!phba->vpi_bmask)) {
6514 			rc = -ENOMEM;
6515 			goto err_exit;
6516 		}
6517 		phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6518 					 GFP_KERNEL);
6519 		if (unlikely(!phba->vpi_ids)) {
6520 			kfree(phba->vpi_bmask);
6521 			rc = -ENOMEM;
6522 			goto err_exit;
6523 		}
6524 
6525 		/* Initialize local ptrs for common extent processing later. */
6526 		bmask = phba->vpi_bmask;
6527 		ids = phba->vpi_ids;
6528 		ext_blk_list = &phba->lpfc_vpi_blk_list;
6529 		break;
6530 	case LPFC_RSC_TYPE_FCOE_XRI:
6531 		phba->sli4_hba.xri_bmask = kcalloc(longs,
6532 						   sizeof(unsigned long),
6533 						   GFP_KERNEL);
6534 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
6535 			rc = -ENOMEM;
6536 			goto err_exit;
6537 		}
6538 		phba->sli4_hba.max_cfg_param.xri_used = 0;
6539 		phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6540 						 sizeof(uint16_t),
6541 						 GFP_KERNEL);
6542 		if (unlikely(!phba->sli4_hba.xri_ids)) {
6543 			kfree(phba->sli4_hba.xri_bmask);
6544 			rc = -ENOMEM;
6545 			goto err_exit;
6546 		}
6547 
6548 		/* Initialize local ptrs for common extent processing later. */
6549 		bmask = phba->sli4_hba.xri_bmask;
6550 		ids = phba->sli4_hba.xri_ids;
6551 		ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6552 		break;
6553 	case LPFC_RSC_TYPE_FCOE_VFI:
6554 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
6555 						   sizeof(unsigned long),
6556 						   GFP_KERNEL);
6557 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6558 			rc = -ENOMEM;
6559 			goto err_exit;
6560 		}
6561 		phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6562 						 sizeof(uint16_t),
6563 						 GFP_KERNEL);
6564 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
6565 			kfree(phba->sli4_hba.vfi_bmask);
6566 			rc = -ENOMEM;
6567 			goto err_exit;
6568 		}
6569 
6570 		/* Initialize local ptrs for common extent processing later. */
6571 		bmask = phba->sli4_hba.vfi_bmask;
6572 		ids = phba->sli4_hba.vfi_ids;
6573 		ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6574 		break;
6575 	default:
6576 		/* Unsupported Opcode.  Fail call. */
6577 		id_array = NULL;
6578 		bmask = NULL;
6579 		ids = NULL;
6580 		ext_blk_list = NULL;
6581 		goto err_exit;
6582 	}
6583 
6584 	/*
6585 	 * Complete initializing the extent configuration with the
6586 	 * allocated ids assigned to this function.  The bitmask serves
6587 	 * as an index into the array and manages the available ids.  The
6588 	 * array just stores the ids communicated to the port via the wqes.
6589 	 */
6590 	for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6591 		if ((i % 2) == 0)
6592 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6593 					 &id_array[k]);
6594 		else
6595 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6596 					 &id_array[k]);
6597 
6598 		rsrc_blks = kzalloc(length, GFP_KERNEL);
6599 		if (unlikely(!rsrc_blks)) {
6600 			rc = -ENOMEM;
6601 			kfree(bmask);
6602 			kfree(ids);
6603 			goto err_exit;
6604 		}
6605 		rsrc_blks->rsrc_start = rsrc_id;
6606 		rsrc_blks->rsrc_size = rsrc_size;
6607 		list_add_tail(&rsrc_blks->list, ext_blk_list);
6608 		rsrc_start = rsrc_id;
6609 		if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6610 			phba->sli4_hba.io_xri_start = rsrc_start +
6611 				lpfc_sli4_get_iocb_cnt(phba);
6612 		}
6613 
6614 		while (rsrc_id < (rsrc_start + rsrc_size)) {
6615 			ids[j] = rsrc_id;
6616 			rsrc_id++;
6617 			j++;
6618 		}
6619 		/* Entire word processed.  Get next word.*/
6620 		if ((i % 2) == 1)
6621 			k++;
6622 	}
6623  err_exit:
6624 	lpfc_sli4_mbox_cmd_free(phba, mbox);
6625 	return rc;
6626 }
6627 
6628 
6629 
6630 /**
6631  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6632  * @phba: Pointer to HBA context object.
6633  * @type: the extent's type.
6634  *
6635  * This function deallocates all extents of a particular resource type.
6636  * SLI4 does not allow for deallocating a particular extent range.  It
6637  * is the caller's responsibility to release all kernel memory resources.
6638  **/
6639 static int
lpfc_sli4_dealloc_extent(struct lpfc_hba * phba,uint16_t type)6640 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6641 {
6642 	int rc;
6643 	uint32_t length, mbox_tmo = 0;
6644 	LPFC_MBOXQ_t *mbox;
6645 	struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6646 	struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6647 
6648 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6649 	if (!mbox)
6650 		return -ENOMEM;
6651 
6652 	/*
6653 	 * This function sends an embedded mailbox because it only sends the
6654 	 * the resource type.  All extents of this type are released by the
6655 	 * port.
6656 	 */
6657 	length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6658 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6659 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6660 			 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6661 			 length, LPFC_SLI4_MBX_EMBED);
6662 
6663 	/* Send an extents count of 0 - the dealloc doesn't use it. */
6664 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6665 					LPFC_SLI4_MBX_EMBED);
6666 	if (unlikely(rc)) {
6667 		rc = -EIO;
6668 		goto out_free_mbox;
6669 	}
6670 	if (!phba->sli4_hba.intr_enable)
6671 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6672 	else {
6673 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6674 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6675 	}
6676 	if (unlikely(rc)) {
6677 		rc = -EIO;
6678 		goto out_free_mbox;
6679 	}
6680 
6681 	dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6682 	if (bf_get(lpfc_mbox_hdr_status,
6683 		   &dealloc_rsrc->header.cfg_shdr.response)) {
6684 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6685 				"2919 Failed to release resource extents "
6686 				"for type %d - Status 0x%x Add'l Status 0x%x. "
6687 				"Resource memory not released.\n",
6688 				type,
6689 				bf_get(lpfc_mbox_hdr_status,
6690 				    &dealloc_rsrc->header.cfg_shdr.response),
6691 				bf_get(lpfc_mbox_hdr_add_status,
6692 				    &dealloc_rsrc->header.cfg_shdr.response));
6693 		rc = -EIO;
6694 		goto out_free_mbox;
6695 	}
6696 
6697 	/* Release kernel memory resources for the specific type. */
6698 	switch (type) {
6699 	case LPFC_RSC_TYPE_FCOE_VPI:
6700 		kfree(phba->vpi_bmask);
6701 		kfree(phba->vpi_ids);
6702 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6703 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6704 				    &phba->lpfc_vpi_blk_list, list) {
6705 			list_del_init(&rsrc_blk->list);
6706 			kfree(rsrc_blk);
6707 		}
6708 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
6709 		break;
6710 	case LPFC_RSC_TYPE_FCOE_XRI:
6711 		kfree(phba->sli4_hba.xri_bmask);
6712 		kfree(phba->sli4_hba.xri_ids);
6713 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6714 				    &phba->sli4_hba.lpfc_xri_blk_list, list) {
6715 			list_del_init(&rsrc_blk->list);
6716 			kfree(rsrc_blk);
6717 		}
6718 		break;
6719 	case LPFC_RSC_TYPE_FCOE_VFI:
6720 		kfree(phba->sli4_hba.vfi_bmask);
6721 		kfree(phba->sli4_hba.vfi_ids);
6722 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6723 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6724 				    &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6725 			list_del_init(&rsrc_blk->list);
6726 			kfree(rsrc_blk);
6727 		}
6728 		break;
6729 	case LPFC_RSC_TYPE_FCOE_RPI:
6730 		/* RPI bitmask and physical id array are cleaned up earlier. */
6731 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6732 				    &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6733 			list_del_init(&rsrc_blk->list);
6734 			kfree(rsrc_blk);
6735 		}
6736 		break;
6737 	default:
6738 		break;
6739 	}
6740 
6741 	bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6742 
6743  out_free_mbox:
6744 	mempool_free(mbox, phba->mbox_mem_pool);
6745 	return rc;
6746 }
6747 
6748 static void
lpfc_set_features(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox,uint32_t feature)6749 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6750 		  uint32_t feature)
6751 {
6752 	uint32_t len;
6753 	u32 sig_freq = 0;
6754 
6755 	len = sizeof(struct lpfc_mbx_set_feature) -
6756 		sizeof(struct lpfc_sli4_cfg_mhdr);
6757 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6758 			 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6759 			 LPFC_SLI4_MBX_EMBED);
6760 
6761 	switch (feature) {
6762 	case LPFC_SET_UE_RECOVERY:
6763 		bf_set(lpfc_mbx_set_feature_UER,
6764 		       &mbox->u.mqe.un.set_feature, 1);
6765 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6766 		mbox->u.mqe.un.set_feature.param_len = 8;
6767 		break;
6768 	case LPFC_SET_MDS_DIAGS:
6769 		bf_set(lpfc_mbx_set_feature_mds,
6770 		       &mbox->u.mqe.un.set_feature, 1);
6771 		bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6772 		       &mbox->u.mqe.un.set_feature, 1);
6773 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6774 		mbox->u.mqe.un.set_feature.param_len = 8;
6775 		break;
6776 	case LPFC_SET_CGN_SIGNAL:
6777 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
6778 			sig_freq = 0;
6779 		else
6780 			sig_freq = phba->cgn_sig_freq;
6781 
6782 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6783 			bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6784 			       &mbox->u.mqe.un.set_feature, sig_freq);
6785 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6786 			       &mbox->u.mqe.un.set_feature, sig_freq);
6787 		}
6788 
6789 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6790 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6791 			       &mbox->u.mqe.un.set_feature, sig_freq);
6792 
6793 		if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6794 		    phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6795 			sig_freq = 0;
6796 		else
6797 			sig_freq = lpfc_acqe_cgn_frequency;
6798 
6799 		bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6800 		       &mbox->u.mqe.un.set_feature, sig_freq);
6801 
6802 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6803 		mbox->u.mqe.un.set_feature.param_len = 12;
6804 		break;
6805 	case LPFC_SET_DUAL_DUMP:
6806 		bf_set(lpfc_mbx_set_feature_dd,
6807 		       &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6808 		bf_set(lpfc_mbx_set_feature_ddquery,
6809 		       &mbox->u.mqe.un.set_feature, 0);
6810 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6811 		mbox->u.mqe.un.set_feature.param_len = 4;
6812 		break;
6813 	case LPFC_SET_ENABLE_MI:
6814 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6815 		mbox->u.mqe.un.set_feature.param_len = 4;
6816 		bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6817 		       phba->pport->cfg_lun_queue_depth);
6818 		bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6819 		       phba->sli4_hba.pc_sli4_params.mi_ver);
6820 		break;
6821 	case LPFC_SET_LD_SIGNAL:
6822 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_LD_SIGNAL;
6823 		mbox->u.mqe.un.set_feature.param_len = 16;
6824 		bf_set(lpfc_mbx_set_feature_lds_qry,
6825 		       &mbox->u.mqe.un.set_feature, LPFC_QUERY_LDS_OP);
6826 		break;
6827 	case LPFC_SET_ENABLE_CMF:
6828 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6829 		mbox->u.mqe.un.set_feature.param_len = 4;
6830 		bf_set(lpfc_mbx_set_feature_cmf,
6831 		       &mbox->u.mqe.un.set_feature, 1);
6832 		break;
6833 	}
6834 	return;
6835 }
6836 
6837 /**
6838  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6839  * @phba: Pointer to HBA context object.
6840  *
6841  * Disable FW logging into host memory on the adapter. To
6842  * be done before reading logs from the host memory.
6843  **/
6844 void
lpfc_ras_stop_fwlog(struct lpfc_hba * phba)6845 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6846 {
6847 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6848 
6849 	spin_lock_irq(&phba->ras_fwlog_lock);
6850 	ras_fwlog->state = INACTIVE;
6851 	spin_unlock_irq(&phba->ras_fwlog_lock);
6852 
6853 	/* Disable FW logging to host memory */
6854 	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6855 	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6856 
6857 	/* Wait 10ms for firmware to stop using DMA buffer */
6858 	usleep_range(10 * 1000, 20 * 1000);
6859 }
6860 
6861 /**
6862  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6863  * @phba: Pointer to HBA context object.
6864  *
6865  * This function is called to free memory allocated for RAS FW logging
6866  * support in the driver.
6867  **/
6868 void
lpfc_sli4_ras_dma_free(struct lpfc_hba * phba)6869 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6870 {
6871 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6872 	struct lpfc_dmabuf *dmabuf, *next;
6873 
6874 	if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6875 		list_for_each_entry_safe(dmabuf, next,
6876 				    &ras_fwlog->fwlog_buff_list,
6877 				    list) {
6878 			list_del(&dmabuf->list);
6879 			dma_free_coherent(&phba->pcidev->dev,
6880 					  LPFC_RAS_MAX_ENTRY_SIZE,
6881 					  dmabuf->virt, dmabuf->phys);
6882 			kfree(dmabuf);
6883 		}
6884 	}
6885 
6886 	if (ras_fwlog->lwpd.virt) {
6887 		dma_free_coherent(&phba->pcidev->dev,
6888 				  sizeof(uint32_t) * 2,
6889 				  ras_fwlog->lwpd.virt,
6890 				  ras_fwlog->lwpd.phys);
6891 		ras_fwlog->lwpd.virt = NULL;
6892 	}
6893 
6894 	spin_lock_irq(&phba->ras_fwlog_lock);
6895 	ras_fwlog->state = INACTIVE;
6896 	spin_unlock_irq(&phba->ras_fwlog_lock);
6897 }
6898 
6899 /**
6900  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6901  * @phba: Pointer to HBA context object.
6902  * @fwlog_buff_count: Count of buffers to be created.
6903  *
6904  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6905  * to update FW log is posted to the adapter.
6906  * Buffer count is calculated based on module param ras_fwlog_buffsize
6907  * Size of each buffer posted to FW is 64K.
6908  **/
6909 
6910 static int
lpfc_sli4_ras_dma_alloc(struct lpfc_hba * phba,uint32_t fwlog_buff_count)6911 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6912 			uint32_t fwlog_buff_count)
6913 {
6914 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6915 	struct lpfc_dmabuf *dmabuf;
6916 	int rc = 0, i = 0;
6917 
6918 	/* Initialize List */
6919 	INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6920 
6921 	/* Allocate memory for the LWPD */
6922 	ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6923 					    sizeof(uint32_t) * 2,
6924 					    &ras_fwlog->lwpd.phys,
6925 					    GFP_KERNEL);
6926 	if (!ras_fwlog->lwpd.virt) {
6927 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6928 				"6185 LWPD Memory Alloc Failed\n");
6929 
6930 		return -ENOMEM;
6931 	}
6932 
6933 	ras_fwlog->fw_buffcount = fwlog_buff_count;
6934 	for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6935 		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6936 				 GFP_KERNEL);
6937 		if (!dmabuf) {
6938 			rc = -ENOMEM;
6939 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6940 					"6186 Memory Alloc failed FW logging");
6941 			goto free_mem;
6942 		}
6943 
6944 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6945 						  LPFC_RAS_MAX_ENTRY_SIZE,
6946 						  &dmabuf->phys, GFP_KERNEL);
6947 		if (!dmabuf->virt) {
6948 			kfree(dmabuf);
6949 			rc = -ENOMEM;
6950 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6951 					"6187 DMA Alloc Failed FW logging");
6952 			goto free_mem;
6953 		}
6954 		dmabuf->buffer_tag = i;
6955 		list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6956 	}
6957 
6958 free_mem:
6959 	if (rc)
6960 		lpfc_sli4_ras_dma_free(phba);
6961 
6962 	return rc;
6963 }
6964 
6965 /**
6966  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6967  * @phba: pointer to lpfc hba data structure.
6968  * @pmb: pointer to the driver internal queue element for mailbox command.
6969  *
6970  * Completion handler for driver's RAS MBX command to the device.
6971  **/
6972 static void
lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)6973 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6974 {
6975 	MAILBOX_t *mb;
6976 	union lpfc_sli4_cfg_shdr *shdr;
6977 	uint32_t shdr_status, shdr_add_status;
6978 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6979 
6980 	mb = &pmb->u.mb;
6981 
6982 	shdr = (union lpfc_sli4_cfg_shdr *)
6983 		&pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6984 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6985 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6986 
6987 	if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6988 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6989 				"6188 FW LOG mailbox "
6990 				"completed with status x%x add_status x%x,"
6991 				" mbx status x%x\n",
6992 				shdr_status, shdr_add_status, mb->mbxStatus);
6993 
6994 		ras_fwlog->ras_hwsupport = false;
6995 		goto disable_ras;
6996 	}
6997 
6998 	spin_lock_irq(&phba->ras_fwlog_lock);
6999 	ras_fwlog->state = ACTIVE;
7000 	spin_unlock_irq(&phba->ras_fwlog_lock);
7001 	mempool_free(pmb, phba->mbox_mem_pool);
7002 
7003 	return;
7004 
7005 disable_ras:
7006 	/* Free RAS DMA memory */
7007 	lpfc_sli4_ras_dma_free(phba);
7008 	mempool_free(pmb, phba->mbox_mem_pool);
7009 }
7010 
7011 /**
7012  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
7013  * @phba: pointer to lpfc hba data structure.
7014  * @fwlog_level: Logging verbosity level.
7015  * @fwlog_enable: Enable/Disable logging.
7016  *
7017  * Initialize memory and post mailbox command to enable FW logging in host
7018  * memory.
7019  **/
7020 int
lpfc_sli4_ras_fwlog_init(struct lpfc_hba * phba,uint32_t fwlog_level,uint32_t fwlog_enable)7021 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
7022 			 uint32_t fwlog_level,
7023 			 uint32_t fwlog_enable)
7024 {
7025 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
7026 	struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
7027 	struct lpfc_dmabuf *dmabuf;
7028 	LPFC_MBOXQ_t *mbox;
7029 	uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
7030 	int rc = 0;
7031 
7032 	spin_lock_irq(&phba->ras_fwlog_lock);
7033 	ras_fwlog->state = INACTIVE;
7034 	spin_unlock_irq(&phba->ras_fwlog_lock);
7035 
7036 	fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
7037 			  phba->cfg_ras_fwlog_buffsize);
7038 	fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
7039 
7040 	/*
7041 	 * If re-enabling FW logging support use earlier allocated
7042 	 * DMA buffers while posting MBX command.
7043 	 **/
7044 	if (!ras_fwlog->lwpd.virt) {
7045 		rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
7046 		if (rc) {
7047 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7048 					"6189 FW Log Memory Allocation Failed");
7049 			return rc;
7050 		}
7051 	}
7052 
7053 	/* Setup Mailbox command */
7054 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7055 	if (!mbox) {
7056 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7057 				"6190 RAS MBX Alloc Failed");
7058 		rc = -ENOMEM;
7059 		goto mem_free;
7060 	}
7061 
7062 	ras_fwlog->fw_loglevel = fwlog_level;
7063 	len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
7064 		sizeof(struct lpfc_sli4_cfg_mhdr));
7065 
7066 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
7067 			 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
7068 			 len, LPFC_SLI4_MBX_EMBED);
7069 
7070 	mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
7071 	bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
7072 	       fwlog_enable);
7073 	bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
7074 	       ras_fwlog->fw_loglevel);
7075 	bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
7076 	       ras_fwlog->fw_buffcount);
7077 	bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
7078 	       LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
7079 
7080 	/* Update DMA buffer address */
7081 	list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
7082 		memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
7083 
7084 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
7085 			putPaddrLow(dmabuf->phys);
7086 
7087 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
7088 			putPaddrHigh(dmabuf->phys);
7089 	}
7090 
7091 	/* Update LPWD address */
7092 	mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7093 	mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7094 
7095 	spin_lock_irq(&phba->ras_fwlog_lock);
7096 	ras_fwlog->state = REG_INPROGRESS;
7097 	spin_unlock_irq(&phba->ras_fwlog_lock);
7098 	mbox->vport = phba->pport;
7099 	mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7100 
7101 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7102 
7103 	if (rc == MBX_NOT_FINISHED) {
7104 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7105 				"6191 FW-Log Mailbox failed. "
7106 				"status %d mbxStatus : x%x", rc,
7107 				bf_get(lpfc_mqe_status, &mbox->u.mqe));
7108 		mempool_free(mbox, phba->mbox_mem_pool);
7109 		rc = -EIO;
7110 		goto mem_free;
7111 	} else
7112 		rc = 0;
7113 mem_free:
7114 	if (rc)
7115 		lpfc_sli4_ras_dma_free(phba);
7116 
7117 	return rc;
7118 }
7119 
7120 /**
7121  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7122  * @phba: Pointer to HBA context object.
7123  *
7124  * Check if RAS is supported on the adapter and initialize it.
7125  **/
7126 void
lpfc_sli4_ras_setup(struct lpfc_hba * phba)7127 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7128 {
7129 	/* Check RAS FW Log needs to be enabled or not */
7130 	if (lpfc_check_fwlog_support(phba))
7131 		return;
7132 
7133 	lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7134 				 LPFC_RAS_ENABLE_LOGGING);
7135 }
7136 
7137 /**
7138  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7139  * @phba: Pointer to HBA context object.
7140  *
7141  * This function allocates all SLI4 resource identifiers.
7142  **/
7143 int
lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba * phba)7144 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7145 {
7146 	int i, rc, error = 0;
7147 	uint16_t count, base;
7148 	unsigned long longs;
7149 
7150 	if (!phba->sli4_hba.rpi_hdrs_in_use)
7151 		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
7152 	if (phba->sli4_hba.extents_in_use) {
7153 		/*
7154 		 * The port supports resource extents. The XRI, VPI, VFI, RPI
7155 		 * resource extent count must be read and allocated before
7156 		 * provisioning the resource id arrays.
7157 		 */
7158 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7159 		    LPFC_IDX_RSRC_RDY) {
7160 			/*
7161 			 * Extent-based resources are set - the driver could
7162 			 * be in a port reset. Figure out if any corrective
7163 			 * actions need to be taken.
7164 			 */
7165 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7166 						 LPFC_RSC_TYPE_FCOE_VFI);
7167 			if (rc != 0)
7168 				error++;
7169 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7170 						 LPFC_RSC_TYPE_FCOE_VPI);
7171 			if (rc != 0)
7172 				error++;
7173 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7174 						 LPFC_RSC_TYPE_FCOE_XRI);
7175 			if (rc != 0)
7176 				error++;
7177 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7178 						 LPFC_RSC_TYPE_FCOE_RPI);
7179 			if (rc != 0)
7180 				error++;
7181 
7182 			/*
7183 			 * It's possible that the number of resources
7184 			 * provided to this port instance changed between
7185 			 * resets.  Detect this condition and reallocate
7186 			 * resources.  Otherwise, there is no action.
7187 			 */
7188 			if (error) {
7189 				lpfc_printf_log(phba, KERN_INFO,
7190 						LOG_MBOX | LOG_INIT,
7191 						"2931 Detected extent resource "
7192 						"change.  Reallocating all "
7193 						"extents.\n");
7194 				rc = lpfc_sli4_dealloc_extent(phba,
7195 						 LPFC_RSC_TYPE_FCOE_VFI);
7196 				rc = lpfc_sli4_dealloc_extent(phba,
7197 						 LPFC_RSC_TYPE_FCOE_VPI);
7198 				rc = lpfc_sli4_dealloc_extent(phba,
7199 						 LPFC_RSC_TYPE_FCOE_XRI);
7200 				rc = lpfc_sli4_dealloc_extent(phba,
7201 						 LPFC_RSC_TYPE_FCOE_RPI);
7202 			} else
7203 				return 0;
7204 		}
7205 
7206 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7207 		if (unlikely(rc))
7208 			goto err_exit;
7209 
7210 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7211 		if (unlikely(rc))
7212 			goto err_exit;
7213 
7214 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7215 		if (unlikely(rc))
7216 			goto err_exit;
7217 
7218 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7219 		if (unlikely(rc))
7220 			goto err_exit;
7221 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7222 		       LPFC_IDX_RSRC_RDY);
7223 		return rc;
7224 	} else {
7225 		/*
7226 		 * The port does not support resource extents.  The XRI, VPI,
7227 		 * VFI, RPI resource ids were determined from READ_CONFIG.
7228 		 * Just allocate the bitmasks and provision the resource id
7229 		 * arrays.  If a port reset is active, the resources don't
7230 		 * need any action - just exit.
7231 		 */
7232 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7233 		    LPFC_IDX_RSRC_RDY) {
7234 			lpfc_sli4_dealloc_resource_identifiers(phba);
7235 			lpfc_sli4_remove_rpis(phba);
7236 		}
7237 		/* RPIs. */
7238 		count = phba->sli4_hba.max_cfg_param.max_rpi;
7239 		if (count <= 0) {
7240 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7241 					"3279 Invalid provisioning of "
7242 					"rpi:%d\n", count);
7243 			rc = -EINVAL;
7244 			goto err_exit;
7245 		}
7246 		base = phba->sli4_hba.max_cfg_param.rpi_base;
7247 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7248 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
7249 						   sizeof(unsigned long),
7250 						   GFP_KERNEL);
7251 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7252 			rc = -ENOMEM;
7253 			goto err_exit;
7254 		}
7255 		phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
7256 						 GFP_KERNEL);
7257 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
7258 			rc = -ENOMEM;
7259 			goto free_rpi_bmask;
7260 		}
7261 
7262 		for (i = 0; i < count; i++)
7263 			phba->sli4_hba.rpi_ids[i] = base + i;
7264 
7265 		/* VPIs. */
7266 		count = phba->sli4_hba.max_cfg_param.max_vpi;
7267 		if (count <= 0) {
7268 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7269 					"3280 Invalid provisioning of "
7270 					"vpi:%d\n", count);
7271 			rc = -EINVAL;
7272 			goto free_rpi_ids;
7273 		}
7274 		base = phba->sli4_hba.max_cfg_param.vpi_base;
7275 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7276 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
7277 					  GFP_KERNEL);
7278 		if (unlikely(!phba->vpi_bmask)) {
7279 			rc = -ENOMEM;
7280 			goto free_rpi_ids;
7281 		}
7282 		phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
7283 					GFP_KERNEL);
7284 		if (unlikely(!phba->vpi_ids)) {
7285 			rc = -ENOMEM;
7286 			goto free_vpi_bmask;
7287 		}
7288 
7289 		for (i = 0; i < count; i++)
7290 			phba->vpi_ids[i] = base + i;
7291 
7292 		/* XRIs. */
7293 		count = phba->sli4_hba.max_cfg_param.max_xri;
7294 		if (count <= 0) {
7295 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7296 					"3281 Invalid provisioning of "
7297 					"xri:%d\n", count);
7298 			rc = -EINVAL;
7299 			goto free_vpi_ids;
7300 		}
7301 		base = phba->sli4_hba.max_cfg_param.xri_base;
7302 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7303 		phba->sli4_hba.xri_bmask = kcalloc(longs,
7304 						   sizeof(unsigned long),
7305 						   GFP_KERNEL);
7306 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
7307 			rc = -ENOMEM;
7308 			goto free_vpi_ids;
7309 		}
7310 		phba->sli4_hba.max_cfg_param.xri_used = 0;
7311 		phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
7312 						 GFP_KERNEL);
7313 		if (unlikely(!phba->sli4_hba.xri_ids)) {
7314 			rc = -ENOMEM;
7315 			goto free_xri_bmask;
7316 		}
7317 
7318 		for (i = 0; i < count; i++)
7319 			phba->sli4_hba.xri_ids[i] = base + i;
7320 
7321 		/* VFIs. */
7322 		count = phba->sli4_hba.max_cfg_param.max_vfi;
7323 		if (count <= 0) {
7324 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7325 					"3282 Invalid provisioning of "
7326 					"vfi:%d\n", count);
7327 			rc = -EINVAL;
7328 			goto free_xri_ids;
7329 		}
7330 		base = phba->sli4_hba.max_cfg_param.vfi_base;
7331 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7332 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
7333 						   sizeof(unsigned long),
7334 						   GFP_KERNEL);
7335 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7336 			rc = -ENOMEM;
7337 			goto free_xri_ids;
7338 		}
7339 		phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
7340 						 GFP_KERNEL);
7341 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
7342 			rc = -ENOMEM;
7343 			goto free_vfi_bmask;
7344 		}
7345 
7346 		for (i = 0; i < count; i++)
7347 			phba->sli4_hba.vfi_ids[i] = base + i;
7348 
7349 		/*
7350 		 * Mark all resources ready.  An HBA reset doesn't need
7351 		 * to reset the initialization.
7352 		 */
7353 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7354 		       LPFC_IDX_RSRC_RDY);
7355 		return 0;
7356 	}
7357 
7358  free_vfi_bmask:
7359 	kfree(phba->sli4_hba.vfi_bmask);
7360 	phba->sli4_hba.vfi_bmask = NULL;
7361  free_xri_ids:
7362 	kfree(phba->sli4_hba.xri_ids);
7363 	phba->sli4_hba.xri_ids = NULL;
7364  free_xri_bmask:
7365 	kfree(phba->sli4_hba.xri_bmask);
7366 	phba->sli4_hba.xri_bmask = NULL;
7367  free_vpi_ids:
7368 	kfree(phba->vpi_ids);
7369 	phba->vpi_ids = NULL;
7370  free_vpi_bmask:
7371 	kfree(phba->vpi_bmask);
7372 	phba->vpi_bmask = NULL;
7373  free_rpi_ids:
7374 	kfree(phba->sli4_hba.rpi_ids);
7375 	phba->sli4_hba.rpi_ids = NULL;
7376  free_rpi_bmask:
7377 	kfree(phba->sli4_hba.rpi_bmask);
7378 	phba->sli4_hba.rpi_bmask = NULL;
7379  err_exit:
7380 	return rc;
7381 }
7382 
7383 /**
7384  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7385  * @phba: Pointer to HBA context object.
7386  *
7387  * This function allocates the number of elements for the specified
7388  * resource type.
7389  **/
7390 int
lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba * phba)7391 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7392 {
7393 	if (phba->sli4_hba.extents_in_use) {
7394 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7395 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7396 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7397 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7398 	} else {
7399 		kfree(phba->vpi_bmask);
7400 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
7401 		kfree(phba->vpi_ids);
7402 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7403 		kfree(phba->sli4_hba.xri_bmask);
7404 		kfree(phba->sli4_hba.xri_ids);
7405 		kfree(phba->sli4_hba.vfi_bmask);
7406 		kfree(phba->sli4_hba.vfi_ids);
7407 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7408 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7409 	}
7410 
7411 	return 0;
7412 }
7413 
7414 /**
7415  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7416  * @phba: Pointer to HBA context object.
7417  * @type: The resource extent type.
7418  * @extnt_cnt: buffer to hold port extent count response
7419  * @extnt_size: buffer to hold port extent size response.
7420  *
7421  * This function calls the port to read the host allocated extents
7422  * for a particular type.
7423  **/
7424 int
lpfc_sli4_get_allocated_extnts(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_cnt,uint16_t * extnt_size)7425 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7426 			       uint16_t *extnt_cnt, uint16_t *extnt_size)
7427 {
7428 	bool emb;
7429 	int rc = 0;
7430 	uint16_t curr_blks = 0;
7431 	uint32_t req_len, emb_len;
7432 	uint32_t alloc_len, mbox_tmo;
7433 	struct list_head *blk_list_head;
7434 	struct lpfc_rsrc_blks *rsrc_blk;
7435 	LPFC_MBOXQ_t *mbox;
7436 	void *virtaddr = NULL;
7437 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7438 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7439 	union  lpfc_sli4_cfg_shdr *shdr;
7440 
7441 	switch (type) {
7442 	case LPFC_RSC_TYPE_FCOE_VPI:
7443 		blk_list_head = &phba->lpfc_vpi_blk_list;
7444 		break;
7445 	case LPFC_RSC_TYPE_FCOE_XRI:
7446 		blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7447 		break;
7448 	case LPFC_RSC_TYPE_FCOE_VFI:
7449 		blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7450 		break;
7451 	case LPFC_RSC_TYPE_FCOE_RPI:
7452 		blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7453 		break;
7454 	default:
7455 		return -EIO;
7456 	}
7457 
7458 	/* Count the number of extents currently allocatd for this type. */
7459 	list_for_each_entry(rsrc_blk, blk_list_head, list) {
7460 		if (curr_blks == 0) {
7461 			/*
7462 			 * The GET_ALLOCATED mailbox does not return the size,
7463 			 * just the count.  The size should be just the size
7464 			 * stored in the current allocated block and all sizes
7465 			 * for an extent type are the same so set the return
7466 			 * value now.
7467 			 */
7468 			*extnt_size = rsrc_blk->rsrc_size;
7469 		}
7470 		curr_blks++;
7471 	}
7472 
7473 	/*
7474 	 * Calculate the size of an embedded mailbox.  The uint32_t
7475 	 * accounts for extents-specific word.
7476 	 */
7477 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7478 		sizeof(uint32_t);
7479 
7480 	/*
7481 	 * Presume the allocation and response will fit into an embedded
7482 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7483 	 */
7484 	emb = LPFC_SLI4_MBX_EMBED;
7485 	req_len = emb_len;
7486 	if (req_len > emb_len) {
7487 		req_len = curr_blks * sizeof(uint16_t) +
7488 			sizeof(union lpfc_sli4_cfg_shdr) +
7489 			sizeof(uint32_t);
7490 		emb = LPFC_SLI4_MBX_NEMBED;
7491 	}
7492 
7493 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7494 	if (!mbox)
7495 		return -ENOMEM;
7496 	memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7497 
7498 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7499 				     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7500 				     req_len, emb);
7501 	if (alloc_len < req_len) {
7502 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7503 			"2983 Allocated DMA memory size (x%x) is "
7504 			"less than the requested DMA memory "
7505 			"size (x%x)\n", alloc_len, req_len);
7506 		rc = -ENOMEM;
7507 		goto err_exit;
7508 	}
7509 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7510 	if (unlikely(rc)) {
7511 		rc = -EIO;
7512 		goto err_exit;
7513 	}
7514 
7515 	if (!phba->sli4_hba.intr_enable)
7516 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7517 	else {
7518 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7519 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7520 	}
7521 
7522 	if (unlikely(rc)) {
7523 		rc = -EIO;
7524 		goto err_exit;
7525 	}
7526 
7527 	/*
7528 	 * Figure out where the response is located.  Then get local pointers
7529 	 * to the response data.  The port does not guarantee to respond to
7530 	 * all extents counts request so update the local variable with the
7531 	 * allocated count from the port.
7532 	 */
7533 	if (emb == LPFC_SLI4_MBX_EMBED) {
7534 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7535 		shdr = &rsrc_ext->header.cfg_shdr;
7536 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7537 	} else {
7538 		virtaddr = mbox->sge_array->addr[0];
7539 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7540 		shdr = &n_rsrc->cfg_shdr;
7541 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7542 	}
7543 
7544 	if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7545 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7546 			"2984 Failed to read allocated resources "
7547 			"for type %d - Status 0x%x Add'l Status 0x%x.\n",
7548 			type,
7549 			bf_get(lpfc_mbox_hdr_status, &shdr->response),
7550 			bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7551 		rc = -EIO;
7552 		goto err_exit;
7553 	}
7554  err_exit:
7555 	lpfc_sli4_mbox_cmd_free(phba, mbox);
7556 	return rc;
7557 }
7558 
7559 /**
7560  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7561  * @phba: pointer to lpfc hba data structure.
7562  * @sgl_list: linked link of sgl buffers to post
7563  * @cnt: number of linked list buffers
7564  *
7565  * This routine walks the list of buffers that have been allocated and
7566  * repost them to the port by using SGL block post. This is needed after a
7567  * pci_function_reset/warm_start or start. It attempts to construct blocks
7568  * of buffer sgls which contains contiguous xris and uses the non-embedded
7569  * SGL block post mailbox commands to post them to the port. For single
7570  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7571  * mailbox command for posting.
7572  *
7573  * Returns: 0 = success, non-zero failure.
7574  **/
7575 static int
lpfc_sli4_repost_sgl_list(struct lpfc_hba * phba,struct list_head * sgl_list,int cnt)7576 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7577 			  struct list_head *sgl_list, int cnt)
7578 {
7579 	struct lpfc_sglq *sglq_entry = NULL;
7580 	struct lpfc_sglq *sglq_entry_next = NULL;
7581 	struct lpfc_sglq *sglq_entry_first = NULL;
7582 	int status = 0, total_cnt;
7583 	int post_cnt = 0, num_posted = 0, block_cnt = 0;
7584 	int last_xritag = NO_XRI;
7585 	LIST_HEAD(prep_sgl_list);
7586 	LIST_HEAD(blck_sgl_list);
7587 	LIST_HEAD(allc_sgl_list);
7588 	LIST_HEAD(post_sgl_list);
7589 	LIST_HEAD(free_sgl_list);
7590 
7591 	spin_lock_irq(&phba->hbalock);
7592 	spin_lock(&phba->sli4_hba.sgl_list_lock);
7593 	list_splice_init(sgl_list, &allc_sgl_list);
7594 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
7595 	spin_unlock_irq(&phba->hbalock);
7596 
7597 	total_cnt = cnt;
7598 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7599 				 &allc_sgl_list, list) {
7600 		list_del_init(&sglq_entry->list);
7601 		block_cnt++;
7602 		if ((last_xritag != NO_XRI) &&
7603 		    (sglq_entry->sli4_xritag != last_xritag + 1)) {
7604 			/* a hole in xri block, form a sgl posting block */
7605 			list_splice_init(&prep_sgl_list, &blck_sgl_list);
7606 			post_cnt = block_cnt - 1;
7607 			/* prepare list for next posting block */
7608 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7609 			block_cnt = 1;
7610 		} else {
7611 			/* prepare list for next posting block */
7612 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7613 			/* enough sgls for non-embed sgl mbox command */
7614 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7615 				list_splice_init(&prep_sgl_list,
7616 						 &blck_sgl_list);
7617 				post_cnt = block_cnt;
7618 				block_cnt = 0;
7619 			}
7620 		}
7621 		num_posted++;
7622 
7623 		/* keep track of last sgl's xritag */
7624 		last_xritag = sglq_entry->sli4_xritag;
7625 
7626 		/* end of repost sgl list condition for buffers */
7627 		if (num_posted == total_cnt) {
7628 			if (post_cnt == 0) {
7629 				list_splice_init(&prep_sgl_list,
7630 						 &blck_sgl_list);
7631 				post_cnt = block_cnt;
7632 			} else if (block_cnt == 1) {
7633 				status = lpfc_sli4_post_sgl(phba,
7634 						sglq_entry->phys, 0,
7635 						sglq_entry->sli4_xritag);
7636 				if (!status) {
7637 					/* successful, put sgl to posted list */
7638 					list_add_tail(&sglq_entry->list,
7639 						      &post_sgl_list);
7640 				} else {
7641 					/* Failure, put sgl to free list */
7642 					lpfc_printf_log(phba, KERN_WARNING,
7643 						LOG_SLI,
7644 						"3159 Failed to post "
7645 						"sgl, xritag:x%x\n",
7646 						sglq_entry->sli4_xritag);
7647 					list_add_tail(&sglq_entry->list,
7648 						      &free_sgl_list);
7649 					total_cnt--;
7650 				}
7651 			}
7652 		}
7653 
7654 		/* continue until a nembed page worth of sgls */
7655 		if (post_cnt == 0)
7656 			continue;
7657 
7658 		/* post the buffer list sgls as a block */
7659 		status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7660 						 post_cnt);
7661 
7662 		if (!status) {
7663 			/* success, put sgl list to posted sgl list */
7664 			list_splice_init(&blck_sgl_list, &post_sgl_list);
7665 		} else {
7666 			/* Failure, put sgl list to free sgl list */
7667 			sglq_entry_first = list_first_entry(&blck_sgl_list,
7668 							    struct lpfc_sglq,
7669 							    list);
7670 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7671 					"3160 Failed to post sgl-list, "
7672 					"xritag:x%x-x%x\n",
7673 					sglq_entry_first->sli4_xritag,
7674 					(sglq_entry_first->sli4_xritag +
7675 					 post_cnt - 1));
7676 			list_splice_init(&blck_sgl_list, &free_sgl_list);
7677 			total_cnt -= post_cnt;
7678 		}
7679 
7680 		/* don't reset xirtag due to hole in xri block */
7681 		if (block_cnt == 0)
7682 			last_xritag = NO_XRI;
7683 
7684 		/* reset sgl post count for next round of posting */
7685 		post_cnt = 0;
7686 	}
7687 
7688 	/* free the sgls failed to post */
7689 	lpfc_free_sgl_list(phba, &free_sgl_list);
7690 
7691 	/* push sgls posted to the available list */
7692 	if (!list_empty(&post_sgl_list)) {
7693 		spin_lock_irq(&phba->hbalock);
7694 		spin_lock(&phba->sli4_hba.sgl_list_lock);
7695 		list_splice_init(&post_sgl_list, sgl_list);
7696 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
7697 		spin_unlock_irq(&phba->hbalock);
7698 	} else {
7699 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7700 				"3161 Failure to post sgl to port,status %x "
7701 				"blkcnt %d totalcnt %d postcnt %d\n",
7702 				status, block_cnt, total_cnt, post_cnt);
7703 		return -EIO;
7704 	}
7705 
7706 	/* return the number of XRIs actually posted */
7707 	return total_cnt;
7708 }
7709 
7710 /**
7711  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7712  * @phba: pointer to lpfc hba data structure.
7713  *
7714  * This routine walks the list of nvme buffers that have been allocated and
7715  * repost them to the port by using SGL block post. This is needed after a
7716  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7717  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7718  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7719  *
7720  * Returns: 0 = success, non-zero failure.
7721  **/
7722 static int
lpfc_sli4_repost_io_sgl_list(struct lpfc_hba * phba)7723 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7724 {
7725 	LIST_HEAD(post_nblist);
7726 	int num_posted, rc = 0;
7727 
7728 	/* get all NVME buffers need to repost to a local list */
7729 	lpfc_io_buf_flush(phba, &post_nblist);
7730 
7731 	/* post the list of nvme buffer sgls to port if available */
7732 	if (!list_empty(&post_nblist)) {
7733 		num_posted = lpfc_sli4_post_io_sgl_list(
7734 			phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7735 		/* failed to post any nvme buffer, return error */
7736 		if (num_posted == 0)
7737 			rc = -EIO;
7738 	}
7739 	return rc;
7740 }
7741 
7742 static void
lpfc_set_host_data(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)7743 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7744 {
7745 	uint32_t len;
7746 
7747 	len = sizeof(struct lpfc_mbx_set_host_data) -
7748 		sizeof(struct lpfc_sli4_cfg_mhdr);
7749 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7750 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7751 			 LPFC_SLI4_MBX_EMBED);
7752 
7753 	mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7754 	mbox->u.mqe.un.set_host_data.param_len =
7755 					LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7756 	snprintf(mbox->u.mqe.un.set_host_data.un.data,
7757 		 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7758 		 "Linux %s v"LPFC_DRIVER_VERSION,
7759 		 test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? "FCoE" : "FC");
7760 }
7761 
7762 int
lpfc_post_rq_buffer(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,int count,int idx)7763 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7764 		    struct lpfc_queue *drq, int count, int idx)
7765 {
7766 	int rc, i;
7767 	struct lpfc_rqe hrqe;
7768 	struct lpfc_rqe drqe;
7769 	struct lpfc_rqb *rqbp;
7770 	unsigned long flags;
7771 	struct rqb_dmabuf *rqb_buffer;
7772 	LIST_HEAD(rqb_buf_list);
7773 
7774 	rqbp = hrq->rqbp;
7775 	for (i = 0; i < count; i++) {
7776 		spin_lock_irqsave(&phba->hbalock, flags);
7777 		/* IF RQ is already full, don't bother */
7778 		if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7779 			spin_unlock_irqrestore(&phba->hbalock, flags);
7780 			break;
7781 		}
7782 		spin_unlock_irqrestore(&phba->hbalock, flags);
7783 
7784 		rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7785 		if (!rqb_buffer)
7786 			break;
7787 		rqb_buffer->hrq = hrq;
7788 		rqb_buffer->drq = drq;
7789 		rqb_buffer->idx = idx;
7790 		list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7791 	}
7792 
7793 	spin_lock_irqsave(&phba->hbalock, flags);
7794 	while (!list_empty(&rqb_buf_list)) {
7795 		list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7796 				 hbuf.list);
7797 
7798 		hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7799 		hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7800 		drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7801 		drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7802 		rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7803 		if (rc < 0) {
7804 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7805 					"6421 Cannot post to HRQ %d: %x %x %x "
7806 					"DRQ %x %x\n",
7807 					hrq->queue_id,
7808 					hrq->host_index,
7809 					hrq->hba_index,
7810 					hrq->entry_count,
7811 					drq->host_index,
7812 					drq->hba_index);
7813 			rqbp->rqb_free_buffer(phba, rqb_buffer);
7814 		} else {
7815 			list_add_tail(&rqb_buffer->hbuf.list,
7816 				      &rqbp->rqb_buffer_list);
7817 			rqbp->buffer_count++;
7818 		}
7819 	}
7820 	spin_unlock_irqrestore(&phba->hbalock, flags);
7821 	return 1;
7822 }
7823 
7824 static void
lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7825 lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7826 {
7827 	union lpfc_sli4_cfg_shdr *shdr;
7828 	u32 shdr_status, shdr_add_status;
7829 
7830 	shdr = (union lpfc_sli4_cfg_shdr *)
7831 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7832 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7833 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7834 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7835 		lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT | LOG_MBOX,
7836 				"4622 SET_FEATURE (x%x) mbox failed, "
7837 				"status x%x add_status x%x, mbx status x%x\n",
7838 				LPFC_SET_LD_SIGNAL, shdr_status,
7839 				shdr_add_status, pmb->u.mb.mbxStatus);
7840 		phba->degrade_activate_threshold = 0;
7841 		phba->degrade_deactivate_threshold = 0;
7842 		phba->fec_degrade_interval = 0;
7843 		goto out;
7844 	}
7845 
7846 	phba->degrade_activate_threshold = pmb->u.mqe.un.set_feature.word7;
7847 	phba->degrade_deactivate_threshold = pmb->u.mqe.un.set_feature.word8;
7848 	phba->fec_degrade_interval = pmb->u.mqe.un.set_feature.word10;
7849 
7850 	lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT,
7851 			"4624 Success: da x%x dd x%x interval x%x\n",
7852 			phba->degrade_activate_threshold,
7853 			phba->degrade_deactivate_threshold,
7854 			phba->fec_degrade_interval);
7855 out:
7856 	mempool_free(pmb, phba->mbox_mem_pool);
7857 }
7858 
7859 int
lpfc_read_lds_params(struct lpfc_hba * phba)7860 lpfc_read_lds_params(struct lpfc_hba *phba)
7861 {
7862 	LPFC_MBOXQ_t *mboxq;
7863 	int rc;
7864 
7865 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7866 	if (!mboxq)
7867 		return -ENOMEM;
7868 
7869 	lpfc_set_features(phba, mboxq, LPFC_SET_LD_SIGNAL);
7870 	mboxq->vport = phba->pport;
7871 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_lds_params;
7872 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7873 	if (rc == MBX_NOT_FINISHED) {
7874 		mempool_free(mboxq, phba->mbox_mem_pool);
7875 		return -EIO;
7876 	}
7877 	return 0;
7878 }
7879 
7880 static void
lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7881 lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7882 {
7883 	struct lpfc_vport *vport = pmb->vport;
7884 	union lpfc_sli4_cfg_shdr *shdr;
7885 	u32 shdr_status, shdr_add_status;
7886 	u32 sig, acqe;
7887 
7888 	/* Two outcomes. (1) Set featurs was successul and EDC negotiation
7889 	 * is done. (2) Mailbox failed and send FPIN support only.
7890 	 */
7891 	shdr = (union lpfc_sli4_cfg_shdr *)
7892 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7893 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7894 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7895 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7896 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7897 				"2516 CGN SET_FEATURE mbox failed with "
7898 				"status x%x add_status x%x, mbx status x%x "
7899 				"Reset Congestion to FPINs only\n",
7900 				shdr_status, shdr_add_status,
7901 				pmb->u.mb.mbxStatus);
7902 		/* If there is a mbox error, move on to RDF */
7903 		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7904 		phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7905 		goto out;
7906 	}
7907 
7908 	/* Zero out Congestion Signal ACQE counter */
7909 	phba->cgn_acqe_cnt = 0;
7910 
7911 	acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7912 		      &pmb->u.mqe.un.set_feature);
7913 	sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7914 		     &pmb->u.mqe.un.set_feature);
7915 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7916 			"4620 SET_FEATURES Success: Freq: %ds %dms "
7917 			" Reg: x%x x%x\n", acqe, sig,
7918 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7919 out:
7920 	mempool_free(pmb, phba->mbox_mem_pool);
7921 
7922 	/* Register for FPIN events from the fabric now that the
7923 	 * EDC common_set_features has completed.
7924 	 */
7925 	lpfc_issue_els_rdf(vport, 0);
7926 }
7927 
7928 int
lpfc_config_cgn_signal(struct lpfc_hba * phba)7929 lpfc_config_cgn_signal(struct lpfc_hba *phba)
7930 {
7931 	LPFC_MBOXQ_t *mboxq;
7932 	u32 rc;
7933 
7934 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7935 	if (!mboxq)
7936 		goto out_rdf;
7937 
7938 	lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7939 	mboxq->vport = phba->pport;
7940 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7941 
7942 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7943 			"4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7944 			"Reg: x%x x%x\n",
7945 			phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7946 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7947 
7948 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7949 	if (rc == MBX_NOT_FINISHED)
7950 		goto out;
7951 	return 0;
7952 
7953 out:
7954 	mempool_free(mboxq, phba->mbox_mem_pool);
7955 out_rdf:
7956 	/* If there is a mbox error, move on to RDF */
7957 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7958 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7959 	lpfc_issue_els_rdf(phba->pport, 0);
7960 	return -EIO;
7961 }
7962 
7963 /**
7964  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7965  * @phba: pointer to lpfc hba data structure.
7966  *
7967  * This routine initializes the per-eq idle_stat to dynamically dictate
7968  * polling decisions.
7969  *
7970  * Return codes:
7971  *   None
7972  **/
lpfc_init_idle_stat_hb(struct lpfc_hba * phba)7973 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7974 {
7975 	int i;
7976 	struct lpfc_sli4_hdw_queue *hdwq;
7977 	struct lpfc_queue *eq;
7978 	struct lpfc_idle_stat *idle_stat;
7979 	u64 wall;
7980 
7981 	for_each_present_cpu(i) {
7982 		hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7983 		eq = hdwq->hba_eq;
7984 
7985 		/* Skip if we've already handled this eq's primary CPU */
7986 		if (eq->chann != i)
7987 			continue;
7988 
7989 		idle_stat = &phba->sli4_hba.idle_stat[i];
7990 
7991 		idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7992 		idle_stat->prev_wall = wall;
7993 
7994 		if (phba->nvmet_support ||
7995 		    phba->cmf_active_mode != LPFC_CFG_OFF ||
7996 		    phba->intr_type != MSIX)
7997 			eq->poll_mode = LPFC_QUEUE_WORK;
7998 		else
7999 			eq->poll_mode = LPFC_THREADED_IRQ;
8000 	}
8001 
8002 	if (!phba->nvmet_support && phba->intr_type == MSIX)
8003 		schedule_delayed_work(&phba->idle_stat_delay_work,
8004 				      msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
8005 }
8006 
lpfc_sli4_dip(struct lpfc_hba * phba)8007 static void lpfc_sli4_dip(struct lpfc_hba *phba)
8008 {
8009 	uint32_t if_type;
8010 
8011 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8012 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
8013 	    if_type == LPFC_SLI_INTF_IF_TYPE_6) {
8014 		struct lpfc_register reg_data;
8015 
8016 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8017 			       &reg_data.word0))
8018 			return;
8019 
8020 		if (bf_get(lpfc_sliport_status_dip, &reg_data))
8021 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8022 					"2904 Firmware Dump Image Present"
8023 					" on Adapter");
8024 	}
8025 }
8026 
8027 /**
8028  * lpfc_rx_monitor_create_ring - Initialize ring buffer for rx_monitor
8029  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8030  * @entries: Number of rx_info_entry objects to allocate in ring
8031  *
8032  * Return:
8033  * 0 - Success
8034  * ENOMEM - Failure to kmalloc
8035  **/
lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor * rx_monitor,u32 entries)8036 int lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor *rx_monitor,
8037 				u32 entries)
8038 {
8039 	rx_monitor->ring = kmalloc_array(entries, sizeof(struct rx_info_entry),
8040 					 GFP_KERNEL);
8041 	if (!rx_monitor->ring)
8042 		return -ENOMEM;
8043 
8044 	rx_monitor->head_idx = 0;
8045 	rx_monitor->tail_idx = 0;
8046 	spin_lock_init(&rx_monitor->lock);
8047 	rx_monitor->entries = entries;
8048 
8049 	return 0;
8050 }
8051 
8052 /**
8053  * lpfc_rx_monitor_destroy_ring - Free ring buffer for rx_monitor
8054  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8055  *
8056  * Called after cancellation of cmf_timer.
8057  **/
lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor * rx_monitor)8058 void lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor *rx_monitor)
8059 {
8060 	kfree(rx_monitor->ring);
8061 	rx_monitor->ring = NULL;
8062 	rx_monitor->entries = 0;
8063 	rx_monitor->head_idx = 0;
8064 	rx_monitor->tail_idx = 0;
8065 }
8066 
8067 /**
8068  * lpfc_rx_monitor_record - Insert an entry into rx_monitor's ring
8069  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8070  * @entry: Pointer to rx_info_entry
8071  *
8072  * Used to insert an rx_info_entry into rx_monitor's ring.  Note that this is a
8073  * deep copy of rx_info_entry not a shallow copy of the rx_info_entry ptr.
8074  *
8075  * This is called from lpfc_cmf_timer, which is in timer/softirq context.
8076  *
8077  * In cases of old data overflow, we do a best effort of FIFO order.
8078  **/
lpfc_rx_monitor_record(struct lpfc_rx_info_monitor * rx_monitor,struct rx_info_entry * entry)8079 void lpfc_rx_monitor_record(struct lpfc_rx_info_monitor *rx_monitor,
8080 			    struct rx_info_entry *entry)
8081 {
8082 	struct rx_info_entry *ring = rx_monitor->ring;
8083 	u32 *head_idx = &rx_monitor->head_idx;
8084 	u32 *tail_idx = &rx_monitor->tail_idx;
8085 	spinlock_t *ring_lock = &rx_monitor->lock;
8086 	u32 ring_size = rx_monitor->entries;
8087 
8088 	spin_lock(ring_lock);
8089 	memcpy(&ring[*tail_idx], entry, sizeof(*entry));
8090 	*tail_idx = (*tail_idx + 1) % ring_size;
8091 
8092 	/* Best effort of FIFO saved data */
8093 	if (*tail_idx == *head_idx)
8094 		*head_idx = (*head_idx + 1) % ring_size;
8095 
8096 	spin_unlock(ring_lock);
8097 }
8098 
8099 /**
8100  * lpfc_rx_monitor_report - Read out rx_monitor's ring
8101  * @phba: Pointer to lpfc_hba object
8102  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8103  * @buf: Pointer to char buffer that will contain rx monitor info data
8104  * @buf_len: Length buf including null char
8105  * @max_read_entries: Maximum number of entries to read out of ring
8106  *
8107  * Used to dump/read what's in rx_monitor's ring buffer.
8108  *
8109  * If buf is NULL || buf_len == 0, then it is implied that we want to log the
8110  * information to kmsg instead of filling out buf.
8111  *
8112  * Return:
8113  * Number of entries read out of the ring
8114  **/
lpfc_rx_monitor_report(struct lpfc_hba * phba,struct lpfc_rx_info_monitor * rx_monitor,char * buf,u32 buf_len,u32 max_read_entries)8115 u32 lpfc_rx_monitor_report(struct lpfc_hba *phba,
8116 			   struct lpfc_rx_info_monitor *rx_monitor, char *buf,
8117 			   u32 buf_len, u32 max_read_entries)
8118 {
8119 	struct rx_info_entry *ring = rx_monitor->ring;
8120 	struct rx_info_entry *entry;
8121 	u32 *head_idx = &rx_monitor->head_idx;
8122 	u32 *tail_idx = &rx_monitor->tail_idx;
8123 	spinlock_t *ring_lock = &rx_monitor->lock;
8124 	u32 ring_size = rx_monitor->entries;
8125 	u32 cnt = 0;
8126 	char tmp[DBG_LOG_STR_SZ] = {0};
8127 	bool log_to_kmsg = (!buf || !buf_len) ? true : false;
8128 
8129 	if (!log_to_kmsg) {
8130 		/* clear the buffer to be sure */
8131 		memset(buf, 0, buf_len);
8132 
8133 		scnprintf(buf, buf_len, "\t%-16s%-16s%-16s%-16s%-8s%-8s%-8s"
8134 					"%-8s%-8s%-8s%-16s\n",
8135 					"MaxBPI", "Tot_Data_CMF",
8136 					"Tot_Data_Cmd", "Tot_Data_Cmpl",
8137 					"Lat(us)", "Avg_IO", "Max_IO", "Bsy",
8138 					"IO_cnt", "Info", "BWutil(ms)");
8139 	}
8140 
8141 	/* Needs to be _irq because record is called from timer interrupt
8142 	 * context
8143 	 */
8144 	spin_lock_irq(ring_lock);
8145 	while (*head_idx != *tail_idx) {
8146 		entry = &ring[*head_idx];
8147 
8148 		/* Read out this entry's data. */
8149 		if (!log_to_kmsg) {
8150 			/* If !log_to_kmsg, then store to buf. */
8151 			scnprintf(tmp, sizeof(tmp),
8152 				  "%03d:\t%-16llu%-16llu%-16llu%-16llu%-8llu"
8153 				  "%-8llu%-8llu%-8u%-8u%-8u%u(%u)\n",
8154 				  *head_idx, entry->max_bytes_per_interval,
8155 				  entry->cmf_bytes, entry->total_bytes,
8156 				  entry->rcv_bytes, entry->avg_io_latency,
8157 				  entry->avg_io_size, entry->max_read_cnt,
8158 				  entry->cmf_busy, entry->io_cnt,
8159 				  entry->cmf_info, entry->timer_utilization,
8160 				  entry->timer_interval);
8161 
8162 			/* Check for buffer overflow */
8163 			if ((strlen(buf) + strlen(tmp)) >= buf_len)
8164 				break;
8165 
8166 			/* Append entry's data to buffer */
8167 			strlcat(buf, tmp, buf_len);
8168 		} else {
8169 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
8170 					"4410 %02u: MBPI %llu Xmit %llu "
8171 					"Cmpl %llu Lat %llu ASz %llu Info %02u "
8172 					"BWUtil %u Int %u slot %u\n",
8173 					cnt, entry->max_bytes_per_interval,
8174 					entry->total_bytes, entry->rcv_bytes,
8175 					entry->avg_io_latency,
8176 					entry->avg_io_size, entry->cmf_info,
8177 					entry->timer_utilization,
8178 					entry->timer_interval, *head_idx);
8179 		}
8180 
8181 		*head_idx = (*head_idx + 1) % ring_size;
8182 
8183 		/* Don't feed more than max_read_entries */
8184 		cnt++;
8185 		if (cnt >= max_read_entries)
8186 			break;
8187 	}
8188 	spin_unlock_irq(ring_lock);
8189 
8190 	return cnt;
8191 }
8192 
8193 /**
8194  * lpfc_cmf_setup - Initialize idle_stat tracking
8195  * @phba: Pointer to HBA context object.
8196  *
8197  * This is called from HBA setup during driver load or when the HBA
8198  * comes online. this does all the initialization to support CMF and MI.
8199  **/
8200 static int
lpfc_cmf_setup(struct lpfc_hba * phba)8201 lpfc_cmf_setup(struct lpfc_hba *phba)
8202 {
8203 	LPFC_MBOXQ_t *mboxq;
8204 	struct lpfc_dmabuf *mp;
8205 	struct lpfc_pc_sli4_params *sli4_params;
8206 	int rc, cmf, mi_ver;
8207 
8208 	rc = lpfc_sli4_refresh_params(phba);
8209 	if (unlikely(rc))
8210 		return rc;
8211 
8212 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8213 	if (!mboxq)
8214 		return -ENOMEM;
8215 
8216 	sli4_params = &phba->sli4_hba.pc_sli4_params;
8217 
8218 	/* Always try to enable MI feature if we can */
8219 	if (sli4_params->mi_ver) {
8220 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
8221 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8222 		mi_ver = bf_get(lpfc_mbx_set_feature_mi,
8223 				 &mboxq->u.mqe.un.set_feature);
8224 
8225 		if (rc == MBX_SUCCESS) {
8226 			if (mi_ver) {
8227 				lpfc_printf_log(phba,
8228 						KERN_WARNING, LOG_CGN_MGMT,
8229 						"6215 MI is enabled\n");
8230 				sli4_params->mi_ver = mi_ver;
8231 			} else {
8232 				lpfc_printf_log(phba,
8233 						KERN_WARNING, LOG_CGN_MGMT,
8234 						"6338 MI is disabled\n");
8235 				sli4_params->mi_ver = 0;
8236 			}
8237 		} else {
8238 			/* mi_ver is already set from GET_SLI4_PARAMETERS */
8239 			lpfc_printf_log(phba, KERN_INFO,
8240 					LOG_CGN_MGMT | LOG_INIT,
8241 					"6245 Enable MI Mailbox x%x (x%x/x%x) "
8242 					"failed, rc:x%x mi:x%x\n",
8243 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8244 					lpfc_sli_config_mbox_subsys_get
8245 						(phba, mboxq),
8246 					lpfc_sli_config_mbox_opcode_get
8247 						(phba, mboxq),
8248 					rc, sli4_params->mi_ver);
8249 		}
8250 	} else {
8251 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8252 				"6217 MI is disabled\n");
8253 	}
8254 
8255 	/* Ensure FDMI is enabled for MI if enable_mi is set */
8256 	if (sli4_params->mi_ver)
8257 		phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
8258 
8259 	/* Always try to enable CMF feature if we can */
8260 	if (sli4_params->cmf) {
8261 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
8262 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8263 		cmf = bf_get(lpfc_mbx_set_feature_cmf,
8264 			     &mboxq->u.mqe.un.set_feature);
8265 		if (rc == MBX_SUCCESS && cmf) {
8266 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8267 					"6218 CMF is enabled: mode %d\n",
8268 					phba->cmf_active_mode);
8269 		} else {
8270 			lpfc_printf_log(phba, KERN_WARNING,
8271 					LOG_CGN_MGMT | LOG_INIT,
8272 					"6219 Enable CMF Mailbox x%x (x%x/x%x) "
8273 					"failed, rc:x%x dd:x%x\n",
8274 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8275 					lpfc_sli_config_mbox_subsys_get
8276 						(phba, mboxq),
8277 					lpfc_sli_config_mbox_opcode_get
8278 						(phba, mboxq),
8279 					rc, cmf);
8280 			sli4_params->cmf = 0;
8281 			phba->cmf_active_mode = LPFC_CFG_OFF;
8282 			goto no_cmf;
8283 		}
8284 
8285 		/* Allocate Congestion Information Buffer */
8286 		if (!phba->cgn_i) {
8287 			mp = kmalloc(sizeof(*mp), GFP_KERNEL);
8288 			if (mp)
8289 				mp->virt = dma_alloc_coherent
8290 						(&phba->pcidev->dev,
8291 						sizeof(struct lpfc_cgn_info),
8292 						&mp->phys, GFP_KERNEL);
8293 			if (!mp || !mp->virt) {
8294 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8295 						"2640 Failed to alloc memory "
8296 						"for Congestion Info\n");
8297 				kfree(mp);
8298 				sli4_params->cmf = 0;
8299 				phba->cmf_active_mode = LPFC_CFG_OFF;
8300 				goto no_cmf;
8301 			}
8302 			phba->cgn_i = mp;
8303 
8304 			/* initialize congestion buffer info */
8305 			lpfc_init_congestion_buf(phba);
8306 			lpfc_init_congestion_stat(phba);
8307 
8308 			/* Zero out Congestion Signal counters */
8309 			atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
8310 			atomic64_set(&phba->cgn_acqe_stat.warn, 0);
8311 		}
8312 
8313 		rc = lpfc_sli4_cgn_params_read(phba);
8314 		if (rc < 0) {
8315 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8316 					"6242 Error reading Cgn Params (%d)\n",
8317 					rc);
8318 			/* Ensure CGN Mode is off */
8319 			sli4_params->cmf = 0;
8320 		} else if (!rc) {
8321 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8322 					"6243 CGN Event empty object.\n");
8323 			/* Ensure CGN Mode is off */
8324 			sli4_params->cmf = 0;
8325 		}
8326 	} else {
8327 no_cmf:
8328 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8329 				"6220 CMF is disabled\n");
8330 	}
8331 
8332 	/* Only register congestion buffer with firmware if BOTH
8333 	 * CMF and E2E are enabled.
8334 	 */
8335 	if (sli4_params->cmf && sli4_params->mi_ver) {
8336 		rc = lpfc_reg_congestion_buf(phba);
8337 		if (rc) {
8338 			dma_free_coherent(&phba->pcidev->dev,
8339 					  sizeof(struct lpfc_cgn_info),
8340 					  phba->cgn_i->virt, phba->cgn_i->phys);
8341 			kfree(phba->cgn_i);
8342 			phba->cgn_i = NULL;
8343 			/* Ensure CGN Mode is off */
8344 			phba->cmf_active_mode = LPFC_CFG_OFF;
8345 			sli4_params->cmf = 0;
8346 			return 0;
8347 		}
8348 	}
8349 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8350 			"6470 Setup MI version %d CMF %d mode %d\n",
8351 			sli4_params->mi_ver, sli4_params->cmf,
8352 			phba->cmf_active_mode);
8353 
8354 	mempool_free(mboxq, phba->mbox_mem_pool);
8355 
8356 	/* Initialize atomic counters */
8357 	atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8358 	atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8359 	atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8360 	atomic_set(&phba->cgn_sync_warn_cnt, 0);
8361 	atomic_set(&phba->cgn_driver_evt_cnt, 0);
8362 	atomic_set(&phba->cgn_latency_evt_cnt, 0);
8363 	atomic64_set(&phba->cgn_latency_evt, 0);
8364 
8365 	phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
8366 
8367 	/* Allocate RX Monitor Buffer */
8368 	if (!phba->rx_monitor) {
8369 		phba->rx_monitor = kzalloc(sizeof(*phba->rx_monitor),
8370 					   GFP_KERNEL);
8371 
8372 		if (!phba->rx_monitor) {
8373 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8374 					"2644 Failed to alloc memory "
8375 					"for RX Monitor Buffer\n");
8376 			return -ENOMEM;
8377 		}
8378 
8379 		/* Instruct the rx_monitor object to instantiate its ring */
8380 		if (lpfc_rx_monitor_create_ring(phba->rx_monitor,
8381 						LPFC_MAX_RXMONITOR_ENTRY)) {
8382 			kfree(phba->rx_monitor);
8383 			phba->rx_monitor = NULL;
8384 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8385 					"2645 Failed to alloc memory "
8386 					"for RX Monitor's Ring\n");
8387 			return -ENOMEM;
8388 		}
8389 	}
8390 
8391 	return 0;
8392 }
8393 
8394 static int
lpfc_set_host_tm(struct lpfc_hba * phba)8395 lpfc_set_host_tm(struct lpfc_hba *phba)
8396 {
8397 	LPFC_MBOXQ_t *mboxq;
8398 	uint32_t len, rc;
8399 	struct timespec64 cur_time;
8400 	struct tm broken;
8401 	uint32_t month, day, year;
8402 	uint32_t hour, minute, second;
8403 	struct lpfc_mbx_set_host_date_time *tm;
8404 
8405 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8406 	if (!mboxq)
8407 		return -ENOMEM;
8408 
8409 	len = sizeof(struct lpfc_mbx_set_host_data) -
8410 		sizeof(struct lpfc_sli4_cfg_mhdr);
8411 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8412 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8413 			 LPFC_SLI4_MBX_EMBED);
8414 
8415 	mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8416 	mboxq->u.mqe.un.set_host_data.param_len =
8417 			sizeof(struct lpfc_mbx_set_host_date_time);
8418 	tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8419 	ktime_get_real_ts64(&cur_time);
8420 	time64_to_tm(cur_time.tv_sec, 0, &broken);
8421 	month = broken.tm_mon + 1;
8422 	day = broken.tm_mday;
8423 	year = broken.tm_year - 100;
8424 	hour = broken.tm_hour;
8425 	minute = broken.tm_min;
8426 	second = broken.tm_sec;
8427 	bf_set(lpfc_mbx_set_host_month, tm, month);
8428 	bf_set(lpfc_mbx_set_host_day, tm, day);
8429 	bf_set(lpfc_mbx_set_host_year, tm, year);
8430 	bf_set(lpfc_mbx_set_host_hour, tm, hour);
8431 	bf_set(lpfc_mbx_set_host_min, tm, minute);
8432 	bf_set(lpfc_mbx_set_host_sec, tm, second);
8433 
8434 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8435 	mempool_free(mboxq, phba->mbox_mem_pool);
8436 	return rc;
8437 }
8438 
8439 /**
8440  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
8441  * @phba: Pointer to HBA context object.
8442  *
8443  * This function is the main SLI4 device initialization PCI function. This
8444  * function is called by the HBA initialization code, HBA reset code and
8445  * HBA error attention handler code. Caller is not required to hold any
8446  * locks.
8447  **/
8448 int
lpfc_sli4_hba_setup(struct lpfc_hba * phba)8449 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8450 {
8451 	int rc, i, cnt, len, dd;
8452 	LPFC_MBOXQ_t *mboxq;
8453 	struct lpfc_mqe *mqe;
8454 	uint8_t *vpd;
8455 	uint32_t vpd_size;
8456 	uint32_t ftr_rsp = 0;
8457 	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8458 	struct lpfc_vport *vport = phba->pport;
8459 	struct lpfc_dmabuf *mp;
8460 	struct lpfc_rqb *rqbp;
8461 	u32 flg;
8462 
8463 	/* Perform a PCI function reset to start from clean */
8464 	rc = lpfc_pci_function_reset(phba);
8465 	if (unlikely(rc))
8466 		return -ENODEV;
8467 
8468 	/* Check the HBA Host Status Register for readyness */
8469 	rc = lpfc_sli4_post_status_check(phba);
8470 	if (unlikely(rc))
8471 		return -ENODEV;
8472 	else {
8473 		spin_lock_irq(&phba->hbalock);
8474 		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
8475 		flg = phba->sli.sli_flag;
8476 		spin_unlock_irq(&phba->hbalock);
8477 		/* Allow a little time after setting SLI_ACTIVE for any polled
8478 		 * MBX commands to complete via BSG.
8479 		 */
8480 		for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {
8481 			msleep(20);
8482 			spin_lock_irq(&phba->hbalock);
8483 			flg = phba->sli.sli_flag;
8484 			spin_unlock_irq(&phba->hbalock);
8485 		}
8486 	}
8487 	clear_bit(HBA_SETUP, &phba->hba_flag);
8488 
8489 	lpfc_sli4_dip(phba);
8490 
8491 	/*
8492 	 * Allocate a single mailbox container for initializing the
8493 	 * port.
8494 	 */
8495 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8496 	if (!mboxq)
8497 		return -ENOMEM;
8498 
8499 	/* Issue READ_REV to collect vpd and FW information. */
8500 	vpd_size = SLI4_PAGE_SIZE;
8501 	vpd = kzalloc(vpd_size, GFP_KERNEL);
8502 	if (!vpd) {
8503 		rc = -ENOMEM;
8504 		goto out_free_mbox;
8505 	}
8506 
8507 	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
8508 	if (unlikely(rc)) {
8509 		kfree(vpd);
8510 		goto out_free_mbox;
8511 	}
8512 
8513 	mqe = &mboxq->u.mqe;
8514 	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
8515 	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
8516 		set_bit(HBA_FCOE_MODE, &phba->hba_flag);
8517 		phba->fcp_embed_io = 0;	/* SLI4 FC support only */
8518 	} else {
8519 		clear_bit(HBA_FCOE_MODE, &phba->hba_flag);
8520 	}
8521 
8522 	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8523 		LPFC_DCBX_CEE_MODE)
8524 		set_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8525 	else
8526 		clear_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8527 
8528 	clear_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
8529 
8530 	if (phba->sli_rev != LPFC_SLI_REV4) {
8531 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8532 			"0376 READ_REV Error. SLI Level %d "
8533 			"FCoE enabled %d\n",
8534 			phba->sli_rev,
8535 			test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? 1 : 0);
8536 		rc = -EIO;
8537 		kfree(vpd);
8538 		goto out_free_mbox;
8539 	}
8540 
8541 	rc = lpfc_set_host_tm(phba);
8542 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
8543 			"6468 Set host date / time: Status x%x:\n", rc);
8544 
8545 	/*
8546 	 * Continue initialization with default values even if driver failed
8547 	 * to read FCoE param config regions, only read parameters if the
8548 	 * board is FCoE
8549 	 */
8550 	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
8551 	    lpfc_sli4_read_fcoe_params(phba))
8552 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
8553 			"2570 Failed to read FCoE parameters\n");
8554 
8555 	/*
8556 	 * Retrieve sli4 device physical port name, failure of doing it
8557 	 * is considered as non-fatal.
8558 	 */
8559 	rc = lpfc_sli4_retrieve_pport_name(phba);
8560 	if (!rc)
8561 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8562 				"3080 Successful retrieving SLI4 device "
8563 				"physical port name: %s.\n", phba->Port);
8564 
8565 	rc = lpfc_sli4_get_ctl_attr(phba);
8566 	if (!rc)
8567 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8568 				"8351 Successful retrieving SLI4 device "
8569 				"CTL ATTR\n");
8570 
8571 	/*
8572 	 * Evaluate the read rev and vpd data. Populate the driver
8573 	 * state with the results. If this routine fails, the failure
8574 	 * is not fatal as the driver will use generic values.
8575 	 */
8576 	rc = lpfc_parse_vpd(phba, vpd, vpd_size);
8577 	if (unlikely(!rc))
8578 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8579 				"0377 Error %d parsing vpd. "
8580 				"Using defaults.\n", rc);
8581 	kfree(vpd);
8582 
8583 	/* Save information as VPD data */
8584 	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8585 	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
8586 
8587 	/*
8588 	 * This is because first G7 ASIC doesn't support the standard
8589 	 * 0x5a NVME cmd descriptor type/subtype
8590 	 */
8591 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8592 			LPFC_SLI_INTF_IF_TYPE_6) &&
8593 	    (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8594 	    (phba->vpd.rev.smRev == 0) &&
8595 	    (phba->cfg_nvme_embed_cmd == 1))
8596 		phba->cfg_nvme_embed_cmd = 0;
8597 
8598 	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8599 	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8600 					 &mqe->un.read_rev);
8601 	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8602 				       &mqe->un.read_rev);
8603 	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8604 					    &mqe->un.read_rev);
8605 	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8606 					   &mqe->un.read_rev);
8607 	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8608 	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8609 	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8610 	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8611 	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8612 	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8613 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8614 			"(%d):0380 READ_REV Status x%x "
8615 			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8616 			mboxq->vport ? mboxq->vport->vpi : 0,
8617 			bf_get(lpfc_mqe_status, mqe),
8618 			phba->vpd.rev.opFwName,
8619 			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8620 			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
8621 
8622 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8623 	    LPFC_SLI_INTF_IF_TYPE_0) {
8624 		lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8625 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8626 		if (rc == MBX_SUCCESS) {
8627 			set_bit(HBA_RECOVERABLE_UE, &phba->hba_flag);
8628 			/* Set 1Sec interval to detect UE */
8629 			phba->eratt_poll_interval = 1;
8630 			phba->sli4_hba.ue_to_sr = bf_get(
8631 					lpfc_mbx_set_feature_UESR,
8632 					&mboxq->u.mqe.un.set_feature);
8633 			phba->sli4_hba.ue_to_rp = bf_get(
8634 					lpfc_mbx_set_feature_UERP,
8635 					&mboxq->u.mqe.un.set_feature);
8636 		}
8637 	}
8638 
8639 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8640 		/* Enable MDS Diagnostics only if the SLI Port supports it */
8641 		lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8642 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8643 		if (rc != MBX_SUCCESS)
8644 			phba->mds_diags_support = 0;
8645 	}
8646 
8647 	/*
8648 	 * Discover the port's supported feature set and match it against the
8649 	 * hosts requests.
8650 	 */
8651 	lpfc_request_features(phba, mboxq);
8652 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8653 	if (unlikely(rc)) {
8654 		rc = -EIO;
8655 		goto out_free_mbox;
8656 	}
8657 
8658 	/* Disable VMID if app header is not supported */
8659 	if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8660 						  &mqe->un.req_ftrs))) {
8661 		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8662 		phba->cfg_vmid_app_header = 0;
8663 		lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8664 				"1242 vmid feature not supported\n");
8665 	}
8666 
8667 	/*
8668 	 * The port must support FCP initiator mode as this is the
8669 	 * only mode running in the host.
8670 	 */
8671 	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8672 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8673 				"0378 No support for fcpi mode.\n");
8674 		ftr_rsp++;
8675 	}
8676 
8677 	/* Performance Hints are ONLY for FCoE */
8678 	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
8679 		if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
8680 			phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
8681 		else
8682 			phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
8683 	}
8684 
8685 	/*
8686 	 * If the port cannot support the host's requested features
8687 	 * then turn off the global config parameters to disable the
8688 	 * feature in the driver.  This is not a fatal error.
8689 	 */
8690 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8691 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8692 			phba->cfg_enable_bg = 0;
8693 			phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
8694 			ftr_rsp++;
8695 		}
8696 	}
8697 
8698 	if (phba->max_vpi && phba->cfg_enable_npiv &&
8699 	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8700 		ftr_rsp++;
8701 
8702 	if (ftr_rsp) {
8703 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8704 				"0379 Feature Mismatch Data: x%08x %08x "
8705 				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8706 				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8707 				phba->cfg_enable_npiv, phba->max_vpi);
8708 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8709 			phba->cfg_enable_bg = 0;
8710 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8711 			phba->cfg_enable_npiv = 0;
8712 	}
8713 
8714 	/* These SLI3 features are assumed in SLI4 */
8715 	spin_lock_irq(&phba->hbalock);
8716 	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8717 	spin_unlock_irq(&phba->hbalock);
8718 
8719 	/* Always try to enable dual dump feature if we can */
8720 	lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8721 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8722 	dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8723 	if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
8724 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8725 				"6448 Dual Dump is enabled\n");
8726 	else
8727 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8728 				"6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8729 				"rc:x%x dd:x%x\n",
8730 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8731 				lpfc_sli_config_mbox_subsys_get(
8732 					phba, mboxq),
8733 				lpfc_sli_config_mbox_opcode_get(
8734 					phba, mboxq),
8735 				rc, dd);
8736 
8737 	/*
8738 	 * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
8739 	 * calls depends on these resources to complete port setup.
8740 	 */
8741 	rc = lpfc_sli4_alloc_resource_identifiers(phba);
8742 	if (rc) {
8743 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8744 				"2920 Failed to alloc Resource IDs "
8745 				"rc = x%x\n", rc);
8746 		goto out_free_mbox;
8747 	}
8748 
8749 	lpfc_sli4_node_rpi_restore(phba);
8750 
8751 	lpfc_set_host_data(phba, mboxq);
8752 
8753 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8754 	if (rc) {
8755 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8756 				"2134 Failed to set host os driver version %x",
8757 				rc);
8758 	}
8759 
8760 	/* Read the port's service parameters. */
8761 	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8762 	if (rc) {
8763 		phba->link_state = LPFC_HBA_ERROR;
8764 		rc = -ENOMEM;
8765 		goto out_free_mbox;
8766 	}
8767 
8768 	mboxq->vport = vport;
8769 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8770 	mp = mboxq->ctx_buf;
8771 	if (rc == MBX_SUCCESS) {
8772 		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8773 		rc = 0;
8774 	}
8775 
8776 	/*
8777 	 * This memory was allocated by the lpfc_read_sparam routine but is
8778 	 * no longer needed.  It is released and ctx_buf NULLed to prevent
8779 	 * unintended pointer access as the mbox is reused.
8780 	 */
8781 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
8782 	kfree(mp);
8783 	mboxq->ctx_buf = NULL;
8784 	if (unlikely(rc)) {
8785 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8786 				"0382 READ_SPARAM command failed "
8787 				"status %d, mbxStatus x%x\n",
8788 				rc, bf_get(lpfc_mqe_status, mqe));
8789 		phba->link_state = LPFC_HBA_ERROR;
8790 		rc = -EIO;
8791 		goto out_free_mbox;
8792 	}
8793 
8794 	lpfc_update_vport_wwn(vport);
8795 
8796 	/* Update the fc_host data structures with new wwn. */
8797 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8798 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8799 
8800 	/* Create all the SLI4 queues */
8801 	rc = lpfc_sli4_queue_create(phba);
8802 	if (rc) {
8803 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8804 				"3089 Failed to allocate queues\n");
8805 		rc = -ENODEV;
8806 		goto out_free_mbox;
8807 	}
8808 	/* Set up all the queues to the device */
8809 	rc = lpfc_sli4_queue_setup(phba);
8810 	if (unlikely(rc)) {
8811 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8812 				"0381 Error %d during queue setup.\n", rc);
8813 		goto out_stop_timers;
8814 	}
8815 	/* Initialize the driver internal SLI layer lists. */
8816 	lpfc_sli4_setup(phba);
8817 	lpfc_sli4_queue_init(phba);
8818 
8819 	/* update host els xri-sgl sizes and mappings */
8820 	rc = lpfc_sli4_els_sgl_update(phba);
8821 	if (unlikely(rc)) {
8822 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8823 				"1400 Failed to update xri-sgl size and "
8824 				"mapping: %d\n", rc);
8825 		goto out_destroy_queue;
8826 	}
8827 
8828 	/* register the els sgl pool to the port */
8829 	rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8830 				       phba->sli4_hba.els_xri_cnt);
8831 	if (unlikely(rc < 0)) {
8832 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8833 				"0582 Error %d during els sgl post "
8834 				"operation\n", rc);
8835 		rc = -ENODEV;
8836 		goto out_destroy_queue;
8837 	}
8838 	phba->sli4_hba.els_xri_cnt = rc;
8839 
8840 	if (phba->nvmet_support) {
8841 		/* update host nvmet xri-sgl sizes and mappings */
8842 		rc = lpfc_sli4_nvmet_sgl_update(phba);
8843 		if (unlikely(rc)) {
8844 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8845 					"6308 Failed to update nvmet-sgl size "
8846 					"and mapping: %d\n", rc);
8847 			goto out_destroy_queue;
8848 		}
8849 
8850 		/* register the nvmet sgl pool to the port */
8851 		rc = lpfc_sli4_repost_sgl_list(
8852 			phba,
8853 			&phba->sli4_hba.lpfc_nvmet_sgl_list,
8854 			phba->sli4_hba.nvmet_xri_cnt);
8855 		if (unlikely(rc < 0)) {
8856 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8857 					"3117 Error %d during nvmet "
8858 					"sgl post\n", rc);
8859 			rc = -ENODEV;
8860 			goto out_destroy_queue;
8861 		}
8862 		phba->sli4_hba.nvmet_xri_cnt = rc;
8863 
8864 		/* We allocate an iocbq for every receive context SGL.
8865 		 * The additional allocation is for abort and ls handling.
8866 		 */
8867 		cnt = phba->sli4_hba.nvmet_xri_cnt +
8868 			phba->sli4_hba.max_cfg_param.max_xri;
8869 	} else {
8870 		/* update host common xri-sgl sizes and mappings */
8871 		rc = lpfc_sli4_io_sgl_update(phba);
8872 		if (unlikely(rc)) {
8873 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8874 					"6082 Failed to update nvme-sgl size "
8875 					"and mapping: %d\n", rc);
8876 			goto out_destroy_queue;
8877 		}
8878 
8879 		/* register the allocated common sgl pool to the port */
8880 		rc = lpfc_sli4_repost_io_sgl_list(phba);
8881 		if (unlikely(rc)) {
8882 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8883 					"6116 Error %d during nvme sgl post "
8884 					"operation\n", rc);
8885 			/* Some NVME buffers were moved to abort nvme list */
8886 			/* A pci function reset will repost them */
8887 			rc = -ENODEV;
8888 			goto out_destroy_queue;
8889 		}
8890 		/* Each lpfc_io_buf job structure has an iocbq element.
8891 		 * This cnt provides for abort, els, ct and ls requests.
8892 		 */
8893 		cnt = phba->sli4_hba.max_cfg_param.max_xri;
8894 	}
8895 
8896 	if (!phba->sli.iocbq_lookup) {
8897 		/* Initialize and populate the iocb list per host */
8898 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8899 				"2821 initialize iocb list with %d entries\n",
8900 				cnt);
8901 		rc = lpfc_init_iocb_list(phba, cnt);
8902 		if (rc) {
8903 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8904 					"1413 Failed to init iocb list.\n");
8905 			goto out_destroy_queue;
8906 		}
8907 	}
8908 
8909 	if (phba->nvmet_support)
8910 		lpfc_nvmet_create_targetport(phba);
8911 
8912 	if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
8913 		/* Post initial buffers to all RQs created */
8914 		for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8915 			rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8916 			INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8917 			rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8918 			rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
8919 			rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
8920 			rqbp->buffer_count = 0;
8921 
8922 			lpfc_post_rq_buffer(
8923 				phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8924 				phba->sli4_hba.nvmet_mrq_data[i],
8925 				phba->cfg_nvmet_mrq_post, i);
8926 		}
8927 	}
8928 
8929 	/* Post the rpi header region to the device. */
8930 	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8931 	if (unlikely(rc)) {
8932 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8933 				"0393 Error %d during rpi post operation\n",
8934 				rc);
8935 		rc = -ENODEV;
8936 		goto out_free_iocblist;
8937 	}
8938 
8939 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
8940 		if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
8941 			/*
8942 			 * The FC Port needs to register FCFI (index 0)
8943 			 */
8944 			lpfc_reg_fcfi(phba, mboxq);
8945 			mboxq->vport = phba->pport;
8946 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8947 			if (rc != MBX_SUCCESS)
8948 				goto out_unset_queue;
8949 			rc = 0;
8950 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
8951 						&mboxq->u.mqe.un.reg_fcfi);
8952 		} else {
8953 			/* We are a NVME Target mode with MRQ > 1 */
8954 
8955 			/* First register the FCFI */
8956 			lpfc_reg_fcfi_mrq(phba, mboxq, 0);
8957 			mboxq->vport = phba->pport;
8958 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8959 			if (rc != MBX_SUCCESS)
8960 				goto out_unset_queue;
8961 			rc = 0;
8962 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
8963 						&mboxq->u.mqe.un.reg_fcfi_mrq);
8964 
8965 			/* Next register the MRQs */
8966 			lpfc_reg_fcfi_mrq(phba, mboxq, 1);
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 		}
8973 		/* Check if the port is configured to be disabled */
8974 		lpfc_sli_read_link_ste(phba);
8975 	}
8976 
8977 	/* Don't post more new bufs if repost already recovered
8978 	 * the nvme sgls.
8979 	 */
8980 	if (phba->nvmet_support == 0) {
8981 		if (phba->sli4_hba.io_xri_cnt == 0) {
8982 			len = lpfc_new_io_buf(
8983 					      phba, phba->sli4_hba.io_xri_max);
8984 			if (len == 0) {
8985 				rc = -ENOMEM;
8986 				goto out_unset_queue;
8987 			}
8988 
8989 			if (phba->cfg_xri_rebalancing)
8990 				lpfc_create_multixri_pools(phba);
8991 		}
8992 	} else {
8993 		phba->cfg_xri_rebalancing = 0;
8994 	}
8995 
8996 	/* Allow asynchronous mailbox command to go through */
8997 	spin_lock_irq(&phba->hbalock);
8998 	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8999 	spin_unlock_irq(&phba->hbalock);
9000 
9001 	/* Post receive buffers to the device */
9002 	lpfc_sli4_rb_setup(phba);
9003 
9004 	/* Reset HBA FCF states after HBA reset */
9005 	phba->fcf.fcf_flag = 0;
9006 	phba->fcf.current_rec.flag = 0;
9007 
9008 	/* Start the ELS watchdog timer */
9009 	mod_timer(&vport->els_tmofunc,
9010 			jiffies + secs_to_jiffies(phba->fc_ratov * 2));
9011 
9012 	/* Start heart beat timer */
9013 	mod_timer(&phba->hb_tmofunc,
9014 		  jiffies + secs_to_jiffies(LPFC_HB_MBOX_INTERVAL));
9015 	clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
9016 	clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
9017 	phba->last_completion_time = jiffies;
9018 
9019 	/* start eq_delay heartbeat */
9020 	if (phba->cfg_auto_imax)
9021 		queue_delayed_work(phba->wq, &phba->eq_delay_work,
9022 				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
9023 
9024 	/* start per phba idle_stat_delay heartbeat */
9025 	lpfc_init_idle_stat_hb(phba);
9026 
9027 	/* Start error attention (ERATT) polling timer */
9028 	mod_timer(&phba->eratt_poll,
9029 		  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
9030 
9031 	/*
9032 	 * The port is ready, set the host's link state to LINK_DOWN
9033 	 * in preparation for link interrupts.
9034 	 */
9035 	spin_lock_irq(&phba->hbalock);
9036 	phba->link_state = LPFC_LINK_DOWN;
9037 
9038 	/* Check if physical ports are trunked */
9039 	if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
9040 		phba->trunk_link.link0.state = LPFC_LINK_DOWN;
9041 	if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
9042 		phba->trunk_link.link1.state = LPFC_LINK_DOWN;
9043 	if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
9044 		phba->trunk_link.link2.state = LPFC_LINK_DOWN;
9045 	if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
9046 		phba->trunk_link.link3.state = LPFC_LINK_DOWN;
9047 	spin_unlock_irq(&phba->hbalock);
9048 
9049 	/* Arm the CQs and then EQs on device */
9050 	lpfc_sli4_arm_cqeq_intr(phba);
9051 
9052 	/* Indicate device interrupt mode */
9053 	phba->sli4_hba.intr_enable = 1;
9054 
9055 	/* Setup CMF after HBA is initialized */
9056 	lpfc_cmf_setup(phba);
9057 
9058 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
9059 	    test_bit(LINK_DISABLED, &phba->hba_flag)) {
9060 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9061 				"3103 Adapter Link is disabled.\n");
9062 		lpfc_down_link(phba, mboxq);
9063 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9064 		if (rc != MBX_SUCCESS) {
9065 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9066 					"3104 Adapter failed to issue "
9067 					"DOWN_LINK mbox cmd, rc:x%x\n", rc);
9068 			goto out_io_buff_free;
9069 		}
9070 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
9071 		/* don't perform init_link on SLI4 FC port loopback test */
9072 		if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
9073 			rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
9074 			if (rc)
9075 				goto out_io_buff_free;
9076 		}
9077 	}
9078 	mempool_free(mboxq, phba->mbox_mem_pool);
9079 
9080 	/* Enable RAS FW log support */
9081 	lpfc_sli4_ras_setup(phba);
9082 
9083 	set_bit(HBA_SETUP, &phba->hba_flag);
9084 	return rc;
9085 
9086 out_io_buff_free:
9087 	/* Free allocated IO Buffers */
9088 	lpfc_io_free(phba);
9089 out_unset_queue:
9090 	/* Unset all the queues set up in this routine when error out */
9091 	lpfc_sli4_queue_unset(phba);
9092 out_free_iocblist:
9093 	lpfc_free_iocb_list(phba);
9094 out_destroy_queue:
9095 	lpfc_sli4_queue_destroy(phba);
9096 out_stop_timers:
9097 	lpfc_stop_hba_timers(phba);
9098 out_free_mbox:
9099 	mempool_free(mboxq, phba->mbox_mem_pool);
9100 	return rc;
9101 }
9102 
9103 /**
9104  * lpfc_mbox_timeout - Timeout call back function for mbox timer
9105  * @t: Context to fetch pointer to hba structure from.
9106  *
9107  * This is the callback function for mailbox timer. The mailbox
9108  * timer is armed when a new mailbox command is issued and the timer
9109  * is deleted when the mailbox complete. The function is called by
9110  * the kernel timer code when a mailbox does not complete within
9111  * expected time. This function wakes up the worker thread to
9112  * process the mailbox timeout and returns. All the processing is
9113  * done by the worker thread function lpfc_mbox_timeout_handler.
9114  **/
9115 void
lpfc_mbox_timeout(struct timer_list * t)9116 lpfc_mbox_timeout(struct timer_list *t)
9117 {
9118 	struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
9119 	unsigned long iflag;
9120 	uint32_t tmo_posted;
9121 
9122 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
9123 	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
9124 	if (!tmo_posted)
9125 		phba->pport->work_port_events |= WORKER_MBOX_TMO;
9126 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
9127 
9128 	if (!tmo_posted)
9129 		lpfc_worker_wake_up(phba);
9130 	return;
9131 }
9132 
9133 /**
9134  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
9135  *                                    are pending
9136  * @phba: Pointer to HBA context object.
9137  *
9138  * This function checks if any mailbox completions are present on the mailbox
9139  * completion queue.
9140  **/
9141 static bool
lpfc_sli4_mbox_completions_pending(struct lpfc_hba * phba)9142 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
9143 {
9144 
9145 	uint32_t idx;
9146 	struct lpfc_queue *mcq;
9147 	struct lpfc_mcqe *mcqe;
9148 	bool pending_completions = false;
9149 	uint8_t	qe_valid;
9150 
9151 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9152 		return false;
9153 
9154 	/* Check for completions on mailbox completion queue */
9155 
9156 	mcq = phba->sli4_hba.mbx_cq;
9157 	idx = mcq->hba_index;
9158 	qe_valid = mcq->qe_valid;
9159 	while (bf_get_le32(lpfc_cqe_valid,
9160 	       (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
9161 		mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
9162 		if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
9163 		    (!bf_get_le32(lpfc_trailer_async, mcqe))) {
9164 			pending_completions = true;
9165 			break;
9166 		}
9167 		idx = (idx + 1) % mcq->entry_count;
9168 		if (mcq->hba_index == idx)
9169 			break;
9170 
9171 		/* if the index wrapped around, toggle the valid bit */
9172 		if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
9173 			qe_valid = (qe_valid) ? 0 : 1;
9174 	}
9175 	return pending_completions;
9176 
9177 }
9178 
9179 /**
9180  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
9181  *					      that were missed.
9182  * @phba: Pointer to HBA context object.
9183  *
9184  * For sli4, it is possible to miss an interrupt. As such mbox completions
9185  * maybe missed causing erroneous mailbox timeouts to occur. This function
9186  * checks to see if mbox completions are on the mailbox completion queue
9187  * and will process all the completions associated with the eq for the
9188  * mailbox completion queue.
9189  **/
9190 static bool
lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba * phba)9191 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
9192 {
9193 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
9194 	uint32_t eqidx;
9195 	struct lpfc_queue *fpeq = NULL;
9196 	struct lpfc_queue *eq;
9197 	bool mbox_pending;
9198 
9199 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9200 		return false;
9201 
9202 	/* Find the EQ associated with the mbox CQ */
9203 	if (sli4_hba->hdwq) {
9204 		for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
9205 			eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
9206 			if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
9207 				fpeq = eq;
9208 				break;
9209 			}
9210 		}
9211 	}
9212 	if (!fpeq)
9213 		return false;
9214 
9215 	/* Turn off interrupts from this EQ */
9216 
9217 	sli4_hba->sli4_eq_clr_intr(fpeq);
9218 
9219 	/* Check to see if a mbox completion is pending */
9220 
9221 	mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
9222 
9223 	/*
9224 	 * If a mbox completion is pending, process all the events on EQ
9225 	 * associated with the mbox completion queue (this could include
9226 	 * mailbox commands, async events, els commands, receive queue data
9227 	 * and fcp commands)
9228 	 */
9229 
9230 	if (mbox_pending)
9231 		/* process and rearm the EQ */
9232 		lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
9233 				     LPFC_QUEUE_WORK);
9234 	else
9235 		/* Always clear and re-arm the EQ */
9236 		sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
9237 
9238 	return mbox_pending;
9239 
9240 }
9241 
9242 /**
9243  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
9244  * @phba: Pointer to HBA context object.
9245  *
9246  * This function is called from worker thread when a mailbox command times out.
9247  * The caller is not required to hold any locks. This function will reset the
9248  * HBA and recover all the pending commands.
9249  **/
9250 void
lpfc_mbox_timeout_handler(struct lpfc_hba * phba)9251 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
9252 {
9253 	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
9254 	MAILBOX_t *mb = NULL;
9255 
9256 	struct lpfc_sli *psli = &phba->sli;
9257 
9258 	/* If the mailbox completed, process the completion */
9259 	lpfc_sli4_process_missed_mbox_completions(phba);
9260 
9261 	if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
9262 		return;
9263 
9264 	if (pmbox != NULL)
9265 		mb = &pmbox->u.mb;
9266 	/* Check the pmbox pointer first.  There is a race condition
9267 	 * between the mbox timeout handler getting executed in the
9268 	 * worklist and the mailbox actually completing. When this
9269 	 * race condition occurs, the mbox_active will be NULL.
9270 	 */
9271 	spin_lock_irq(&phba->hbalock);
9272 	if (pmbox == NULL) {
9273 		lpfc_printf_log(phba, KERN_WARNING,
9274 				LOG_MBOX | LOG_SLI,
9275 				"0353 Active Mailbox cleared - mailbox timeout "
9276 				"exiting\n");
9277 		spin_unlock_irq(&phba->hbalock);
9278 		return;
9279 	}
9280 
9281 	/* Mbox cmd <mbxCommand> timeout */
9282 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9283 			"0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
9284 			mb->mbxCommand,
9285 			phba->pport->port_state,
9286 			phba->sli.sli_flag,
9287 			phba->sli.mbox_active);
9288 	spin_unlock_irq(&phba->hbalock);
9289 
9290 	/* Setting state unknown so lpfc_sli_abort_iocb_ring
9291 	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
9292 	 * it to fail all outstanding SCSI IO.
9293 	 */
9294 	set_bit(MBX_TMO_ERR, &phba->bit_flags);
9295 	spin_lock_irq(&phba->pport->work_port_lock);
9296 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9297 	spin_unlock_irq(&phba->pport->work_port_lock);
9298 	spin_lock_irq(&phba->hbalock);
9299 	phba->link_state = LPFC_LINK_UNKNOWN;
9300 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9301 	spin_unlock_irq(&phba->hbalock);
9302 
9303 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9304 			"0345 Resetting board due to mailbox timeout\n");
9305 
9306 	/* Reset the HBA device */
9307 	lpfc_reset_hba(phba);
9308 }
9309 
9310 /**
9311  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
9312  * @phba: Pointer to HBA context object.
9313  * @pmbox: Pointer to mailbox object.
9314  * @flag: Flag indicating how the mailbox need to be processed.
9315  *
9316  * This function is called by discovery code and HBA management code
9317  * to submit a mailbox command to firmware with SLI-3 interface spec. This
9318  * function gets the hbalock to protect the data structures.
9319  * The mailbox command can be submitted in polling mode, in which case
9320  * this function will wait in a polling loop for the completion of the
9321  * mailbox.
9322  * If the mailbox is submitted in no_wait mode (not polling) the
9323  * function will submit the command and returns immediately without waiting
9324  * for the mailbox completion. The no_wait is supported only when HBA
9325  * is in SLI2/SLI3 mode - interrupts are enabled.
9326  * The SLI interface allows only one mailbox pending at a time. If the
9327  * mailbox is issued in polling mode and there is already a mailbox
9328  * pending, then the function will return an error. If the mailbox is issued
9329  * in NO_WAIT mode and there is a mailbox pending already, the function
9330  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9331  * The sli layer owns the mailbox object until the completion of mailbox
9332  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9333  * return codes the caller owns the mailbox command after the return of
9334  * the function.
9335  **/
9336 static int
lpfc_sli_issue_mbox_s3(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)9337 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9338 		       uint32_t flag)
9339 {
9340 	MAILBOX_t *mbx;
9341 	struct lpfc_sli *psli = &phba->sli;
9342 	uint32_t status, evtctr;
9343 	uint32_t ha_copy, hc_copy;
9344 	int i;
9345 	unsigned long timeout;
9346 	unsigned long drvr_flag = 0;
9347 	uint32_t word0, ldata;
9348 	void __iomem *to_slim;
9349 	int processing_queue = 0;
9350 
9351 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
9352 	if (!pmbox) {
9353 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9354 		/* processing mbox queue from intr_handler */
9355 		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9356 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9357 			return MBX_SUCCESS;
9358 		}
9359 		processing_queue = 1;
9360 		pmbox = lpfc_mbox_get(phba);
9361 		if (!pmbox) {
9362 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9363 			return MBX_SUCCESS;
9364 		}
9365 	}
9366 
9367 	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
9368 		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
9369 		if(!pmbox->vport) {
9370 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9371 			lpfc_printf_log(phba, KERN_ERR,
9372 					LOG_MBOX | LOG_VPORT,
9373 					"1806 Mbox x%x failed. No vport\n",
9374 					pmbox->u.mb.mbxCommand);
9375 			dump_stack();
9376 			goto out_not_finished;
9377 		}
9378 	}
9379 
9380 	/* If the PCI channel is in offline state, do not post mbox. */
9381 	if (unlikely(pci_channel_offline(phba->pcidev))) {
9382 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9383 		goto out_not_finished;
9384 	}
9385 
9386 	/* If HBA has a deferred error attention, fail the iocb. */
9387 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
9388 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9389 		goto out_not_finished;
9390 	}
9391 
9392 	psli = &phba->sli;
9393 
9394 	mbx = &pmbox->u.mb;
9395 	status = MBX_SUCCESS;
9396 
9397 	if (phba->link_state == LPFC_HBA_ERROR) {
9398 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9399 
9400 		/* Mbox command <mbxCommand> cannot issue */
9401 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9402 				"(%d):0311 Mailbox command x%x cannot "
9403 				"issue Data: x%x x%x\n",
9404 				pmbox->vport ? pmbox->vport->vpi : 0,
9405 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9406 		goto out_not_finished;
9407 	}
9408 
9409 	if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9410 		if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9411 			!(hc_copy & HC_MBINT_ENA)) {
9412 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9413 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9414 				"(%d):2528 Mailbox command x%x cannot "
9415 				"issue Data: x%x x%x\n",
9416 				pmbox->vport ? pmbox->vport->vpi : 0,
9417 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9418 			goto out_not_finished;
9419 		}
9420 	}
9421 
9422 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9423 		/* Polling for a mbox command when another one is already active
9424 		 * is not allowed in SLI. Also, the driver must have established
9425 		 * SLI2 mode to queue and process multiple mbox commands.
9426 		 */
9427 
9428 		if (flag & MBX_POLL) {
9429 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9430 
9431 			/* Mbox command <mbxCommand> cannot issue */
9432 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9433 					"(%d):2529 Mailbox command x%x "
9434 					"cannot issue Data: x%x x%x\n",
9435 					pmbox->vport ? pmbox->vport->vpi : 0,
9436 					pmbox->u.mb.mbxCommand,
9437 					psli->sli_flag, flag);
9438 			goto out_not_finished;
9439 		}
9440 
9441 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
9442 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9443 			/* Mbox command <mbxCommand> cannot issue */
9444 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9445 					"(%d):2530 Mailbox command x%x "
9446 					"cannot issue Data: x%x x%x\n",
9447 					pmbox->vport ? pmbox->vport->vpi : 0,
9448 					pmbox->u.mb.mbxCommand,
9449 					psli->sli_flag, flag);
9450 			goto out_not_finished;
9451 		}
9452 
9453 		/* Another mailbox command is still being processed, queue this
9454 		 * command to be processed later.
9455 		 */
9456 		lpfc_mbox_put(phba, pmbox);
9457 
9458 		/* Mbox cmd issue - BUSY */
9459 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9460 				"(%d):0308 Mbox cmd issue - BUSY Data: "
9461 				"x%x x%x x%x x%x\n",
9462 				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
9463 				mbx->mbxCommand,
9464 				phba->pport ? phba->pport->port_state : 0xff,
9465 				psli->sli_flag, flag);
9466 
9467 		psli->slistat.mbox_busy++;
9468 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9469 
9470 		if (pmbox->vport) {
9471 			lpfc_debugfs_disc_trc(pmbox->vport,
9472 				LPFC_DISC_TRC_MBOX_VPORT,
9473 				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
9474 				(uint32_t)mbx->mbxCommand,
9475 				mbx->un.varWords[0], mbx->un.varWords[1]);
9476 		}
9477 		else {
9478 			lpfc_debugfs_disc_trc(phba->pport,
9479 				LPFC_DISC_TRC_MBOX,
9480 				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
9481 				(uint32_t)mbx->mbxCommand,
9482 				mbx->un.varWords[0], mbx->un.varWords[1]);
9483 		}
9484 
9485 		return MBX_BUSY;
9486 	}
9487 
9488 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9489 
9490 	/* If we are not polling, we MUST be in SLI2 mode */
9491 	if (flag != MBX_POLL) {
9492 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
9493 		    (mbx->mbxCommand != MBX_KILL_BOARD)) {
9494 			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9495 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9496 			/* Mbox command <mbxCommand> cannot issue */
9497 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9498 					"(%d):2531 Mailbox command x%x "
9499 					"cannot issue Data: x%x x%x\n",
9500 					pmbox->vport ? pmbox->vport->vpi : 0,
9501 					pmbox->u.mb.mbxCommand,
9502 					psli->sli_flag, flag);
9503 			goto out_not_finished;
9504 		}
9505 		/* timeout active mbox command */
9506 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox));
9507 		mod_timer(&psli->mbox_tmo, jiffies + timeout);
9508 	}
9509 
9510 	/* Mailbox cmd <cmd> issue */
9511 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9512 			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
9513 			"x%x\n",
9514 			pmbox->vport ? pmbox->vport->vpi : 0,
9515 			mbx->mbxCommand,
9516 			phba->pport ? phba->pport->port_state : 0xff,
9517 			psli->sli_flag, flag);
9518 
9519 	if (mbx->mbxCommand != MBX_HEARTBEAT) {
9520 		if (pmbox->vport) {
9521 			lpfc_debugfs_disc_trc(pmbox->vport,
9522 				LPFC_DISC_TRC_MBOX_VPORT,
9523 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
9524 				(uint32_t)mbx->mbxCommand,
9525 				mbx->un.varWords[0], mbx->un.varWords[1]);
9526 		}
9527 		else {
9528 			lpfc_debugfs_disc_trc(phba->pport,
9529 				LPFC_DISC_TRC_MBOX,
9530 				"MBOX Send:       cmd:x%x mb:x%x x%x",
9531 				(uint32_t)mbx->mbxCommand,
9532 				mbx->un.varWords[0], mbx->un.varWords[1]);
9533 		}
9534 	}
9535 
9536 	psli->slistat.mbox_cmd++;
9537 	evtctr = psli->slistat.mbox_event;
9538 
9539 	/* next set own bit for the adapter and copy over command word */
9540 	mbx->mbxOwner = OWN_CHIP;
9541 
9542 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9543 		/* Populate mbox extension offset word. */
9544 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
9545 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9546 				= (uint8_t *)phba->mbox_ext
9547 				  - (uint8_t *)phba->mbox;
9548 		}
9549 
9550 		/* Copy the mailbox extension data */
9551 		if (pmbox->in_ext_byte_len && pmbox->ext_buf) {
9552 			lpfc_sli_pcimem_bcopy(pmbox->ext_buf,
9553 					      (uint8_t *)phba->mbox_ext,
9554 					      pmbox->in_ext_byte_len);
9555 		}
9556 		/* Copy command data to host SLIM area */
9557 		lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
9558 	} else {
9559 		/* Populate mbox extension offset word. */
9560 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
9561 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9562 				= MAILBOX_HBA_EXT_OFFSET;
9563 
9564 		/* Copy the mailbox extension data */
9565 		if (pmbox->in_ext_byte_len && pmbox->ext_buf)
9566 			lpfc_memcpy_to_slim(phba->MBslimaddr +
9567 				MAILBOX_HBA_EXT_OFFSET,
9568 				pmbox->ext_buf, pmbox->in_ext_byte_len);
9569 
9570 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9571 			/* copy command data into host mbox for cmpl */
9572 			lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
9573 					      MAILBOX_CMD_SIZE);
9574 
9575 		/* First copy mbox command data to HBA SLIM, skip past first
9576 		   word */
9577 		to_slim = phba->MBslimaddr + sizeof (uint32_t);
9578 		lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
9579 			    MAILBOX_CMD_SIZE - sizeof (uint32_t));
9580 
9581 		/* Next copy over first word, with mbxOwner set */
9582 		ldata = *((uint32_t *)mbx);
9583 		to_slim = phba->MBslimaddr;
9584 		writel(ldata, to_slim);
9585 		readl(to_slim); /* flush */
9586 
9587 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9588 			/* switch over to host mailbox */
9589 			psli->sli_flag |= LPFC_SLI_ACTIVE;
9590 	}
9591 
9592 	wmb();
9593 
9594 	switch (flag) {
9595 	case MBX_NOWAIT:
9596 		/* Set up reference to mailbox command */
9597 		psli->mbox_active = pmbox;
9598 		/* Interrupt board to do it */
9599 		writel(CA_MBATT, phba->CAregaddr);
9600 		readl(phba->CAregaddr); /* flush */
9601 		/* Don't wait for it to finish, just return */
9602 		break;
9603 
9604 	case MBX_POLL:
9605 		/* Set up null reference to mailbox command */
9606 		psli->mbox_active = NULL;
9607 		/* Interrupt board to do it */
9608 		writel(CA_MBATT, phba->CAregaddr);
9609 		readl(phba->CAregaddr); /* flush */
9610 
9611 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9612 			/* First read mbox status word */
9613 			word0 = *((uint32_t *)phba->mbox);
9614 			word0 = le32_to_cpu(word0);
9615 		} else {
9616 			/* First read mbox status word */
9617 			if (lpfc_readl(phba->MBslimaddr, &word0)) {
9618 				spin_unlock_irqrestore(&phba->hbalock,
9619 						       drvr_flag);
9620 				goto out_not_finished;
9621 			}
9622 		}
9623 
9624 		/* Read the HBA Host Attention Register */
9625 		if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9626 			spin_unlock_irqrestore(&phba->hbalock,
9627 						       drvr_flag);
9628 			goto out_not_finished;
9629 		}
9630 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox)) + jiffies;
9631 		i = 0;
9632 		/* Wait for command to complete */
9633 		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9634 		       (!(ha_copy & HA_MBATT) &&
9635 			(phba->link_state > LPFC_WARM_START))) {
9636 			if (time_after(jiffies, timeout)) {
9637 				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9638 				spin_unlock_irqrestore(&phba->hbalock,
9639 						       drvr_flag);
9640 				goto out_not_finished;
9641 			}
9642 
9643 			/* Check if we took a mbox interrupt while we were
9644 			   polling */
9645 			if (((word0 & OWN_CHIP) != OWN_CHIP)
9646 			    && (evtctr != psli->slistat.mbox_event))
9647 				break;
9648 
9649 			if (i++ > 10) {
9650 				spin_unlock_irqrestore(&phba->hbalock,
9651 						       drvr_flag);
9652 				msleep(1);
9653 				spin_lock_irqsave(&phba->hbalock, drvr_flag);
9654 			}
9655 
9656 			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9657 				/* First copy command data */
9658 				word0 = *((uint32_t *)phba->mbox);
9659 				word0 = le32_to_cpu(word0);
9660 				if (mbx->mbxCommand == MBX_CONFIG_PORT) {
9661 					MAILBOX_t *slimmb;
9662 					uint32_t slimword0;
9663 					/* Check real SLIM for any errors */
9664 					slimword0 = readl(phba->MBslimaddr);
9665 					slimmb = (MAILBOX_t *) & slimword0;
9666 					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9667 					    && slimmb->mbxStatus) {
9668 						psli->sli_flag &=
9669 						    ~LPFC_SLI_ACTIVE;
9670 						word0 = slimword0;
9671 					}
9672 				}
9673 			} else {
9674 				/* First copy command data */
9675 				word0 = readl(phba->MBslimaddr);
9676 			}
9677 			/* Read the HBA Host Attention Register */
9678 			if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9679 				spin_unlock_irqrestore(&phba->hbalock,
9680 						       drvr_flag);
9681 				goto out_not_finished;
9682 			}
9683 		}
9684 
9685 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9686 			/* copy results back to user */
9687 			lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9688 						MAILBOX_CMD_SIZE);
9689 			/* Copy the mailbox extension data */
9690 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9691 				lpfc_sli_pcimem_bcopy(phba->mbox_ext,
9692 						      pmbox->ext_buf,
9693 						      pmbox->out_ext_byte_len);
9694 			}
9695 		} else {
9696 			/* First copy command data */
9697 			lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
9698 						MAILBOX_CMD_SIZE);
9699 			/* Copy the mailbox extension data */
9700 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9701 				lpfc_memcpy_from_slim(
9702 					pmbox->ext_buf,
9703 					phba->MBslimaddr +
9704 					MAILBOX_HBA_EXT_OFFSET,
9705 					pmbox->out_ext_byte_len);
9706 			}
9707 		}
9708 
9709 		writel(HA_MBATT, phba->HAregaddr);
9710 		readl(phba->HAregaddr); /* flush */
9711 
9712 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9713 		status = mbx->mbxStatus;
9714 	}
9715 
9716 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9717 	return status;
9718 
9719 out_not_finished:
9720 	if (processing_queue) {
9721 		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
9722 		lpfc_mbox_cmpl_put(phba, pmbox);
9723 	}
9724 	return MBX_NOT_FINISHED;
9725 }
9726 
9727 /**
9728  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9729  * @phba: Pointer to HBA context object.
9730  *
9731  * The function blocks the posting of SLI4 asynchronous mailbox commands from
9732  * the driver internal pending mailbox queue. It will then try to wait out the
9733  * possible outstanding mailbox command before return.
9734  *
9735  * Returns:
9736  * 	0 - the outstanding mailbox command completed; otherwise, the wait for
9737  * 	the outstanding mailbox command timed out.
9738  **/
9739 static int
lpfc_sli4_async_mbox_block(struct lpfc_hba * phba)9740 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9741 {
9742 	struct lpfc_sli *psli = &phba->sli;
9743 	LPFC_MBOXQ_t *mboxq;
9744 	int rc = 0;
9745 	unsigned long timeout = 0;
9746 	u32 sli_flag;
9747 	u8 cmd, subsys, opcode;
9748 
9749 	/* Mark the asynchronous mailbox command posting as blocked */
9750 	spin_lock_irq(&phba->hbalock);
9751 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9752 	/* Determine how long we might wait for the active mailbox
9753 	 * command to be gracefully completed by firmware.
9754 	 */
9755 	if (phba->sli.mbox_active)
9756 		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
9757 						phba->sli.mbox_active)) + jiffies;
9758 	spin_unlock_irq(&phba->hbalock);
9759 
9760 	/* Make sure the mailbox is really active */
9761 	if (timeout)
9762 		lpfc_sli4_process_missed_mbox_completions(phba);
9763 
9764 	/* Wait for the outstanding mailbox command to complete */
9765 	while (phba->sli.mbox_active) {
9766 		/* Check active mailbox complete status every 2ms */
9767 		msleep(2);
9768 		if (time_after(jiffies, timeout)) {
9769 			/* Timeout, mark the outstanding cmd not complete */
9770 
9771 			/* Sanity check sli.mbox_active has not completed or
9772 			 * cancelled from another context during last 2ms sleep,
9773 			 * so take hbalock to be sure before logging.
9774 			 */
9775 			spin_lock_irq(&phba->hbalock);
9776 			if (phba->sli.mbox_active) {
9777 				mboxq = phba->sli.mbox_active;
9778 				cmd = mboxq->u.mb.mbxCommand;
9779 				subsys = lpfc_sli_config_mbox_subsys_get(phba,
9780 									 mboxq);
9781 				opcode = lpfc_sli_config_mbox_opcode_get(phba,
9782 									 mboxq);
9783 				sli_flag = psli->sli_flag;
9784 				spin_unlock_irq(&phba->hbalock);
9785 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9786 						"2352 Mailbox command x%x "
9787 						"(x%x/x%x) sli_flag x%x could "
9788 						"not complete\n",
9789 						cmd, subsys, opcode,
9790 						sli_flag);
9791 			} else {
9792 				spin_unlock_irq(&phba->hbalock);
9793 			}
9794 
9795 			rc = 1;
9796 			break;
9797 		}
9798 	}
9799 
9800 	/* Can not cleanly block async mailbox command, fails it */
9801 	if (rc) {
9802 		spin_lock_irq(&phba->hbalock);
9803 		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9804 		spin_unlock_irq(&phba->hbalock);
9805 	}
9806 	return rc;
9807 }
9808 
9809 /**
9810  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9811  * @phba: Pointer to HBA context object.
9812  *
9813  * The function unblocks and resume posting of SLI4 asynchronous mailbox
9814  * commands from the driver internal pending mailbox queue. It makes sure
9815  * that there is no outstanding mailbox command before resuming posting
9816  * asynchronous mailbox commands. If, for any reason, there is outstanding
9817  * mailbox command, it will try to wait it out before resuming asynchronous
9818  * mailbox command posting.
9819  **/
9820 static void
lpfc_sli4_async_mbox_unblock(struct lpfc_hba * phba)9821 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9822 {
9823 	struct lpfc_sli *psli = &phba->sli;
9824 
9825 	spin_lock_irq(&phba->hbalock);
9826 	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9827 		/* Asynchronous mailbox posting is not blocked, do nothing */
9828 		spin_unlock_irq(&phba->hbalock);
9829 		return;
9830 	}
9831 
9832 	/* Outstanding synchronous mailbox command is guaranteed to be done,
9833 	 * successful or timeout, after timing-out the outstanding mailbox
9834 	 * command shall always be removed, so just unblock posting async
9835 	 * mailbox command and resume
9836 	 */
9837 	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9838 	spin_unlock_irq(&phba->hbalock);
9839 
9840 	/* wake up worker thread to post asynchronous mailbox command */
9841 	lpfc_worker_wake_up(phba);
9842 }
9843 
9844 /**
9845  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9846  * @phba: Pointer to HBA context object.
9847  * @mboxq: Pointer to mailbox object.
9848  *
9849  * The function waits for the bootstrap mailbox register ready bit from
9850  * port for twice the regular mailbox command timeout value.
9851  *
9852  *      0 - no timeout on waiting for bootstrap mailbox register ready.
9853  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out or port
9854  *                     is in an unrecoverable state.
9855  **/
9856 static int
lpfc_sli4_wait_bmbx_ready(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9857 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9858 {
9859 	uint32_t db_ready;
9860 	unsigned long timeout;
9861 	struct lpfc_register bmbx_reg;
9862 	struct lpfc_register portstat_reg = {-1};
9863 
9864 	/* Sanity check - there is no point to wait if the port is in an
9865 	 * unrecoverable state.
9866 	 */
9867 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
9868 	    LPFC_SLI_INTF_IF_TYPE_2) {
9869 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9870 			       &portstat_reg.word0) ||
9871 		    lpfc_sli4_unrecoverable_port(&portstat_reg)) {
9872 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9873 					"3858 Skipping bmbx ready because "
9874 					"Port Status x%x\n",
9875 					portstat_reg.word0);
9876 			return MBXERR_ERROR;
9877 		}
9878 	}
9879 
9880 	timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)) + jiffies;
9881 
9882 	do {
9883 		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9884 		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9885 		if (!db_ready)
9886 			mdelay(2);
9887 
9888 		if (time_after(jiffies, timeout))
9889 			return MBXERR_ERROR;
9890 	} while (!db_ready);
9891 
9892 	return 0;
9893 }
9894 
9895 /**
9896  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9897  * @phba: Pointer to HBA context object.
9898  * @mboxq: Pointer to mailbox object.
9899  *
9900  * The function posts a mailbox to the port.  The mailbox is expected
9901  * to be comletely filled in and ready for the port to operate on it.
9902  * This routine executes a synchronous completion operation on the
9903  * mailbox by polling for its completion.
9904  *
9905  * The caller must not be holding any locks when calling this routine.
9906  *
9907  * Returns:
9908  *	MBX_SUCCESS - mailbox posted successfully
9909  *	Any of the MBX error values.
9910  **/
9911 static int
lpfc_sli4_post_sync_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9912 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9913 {
9914 	int rc = MBX_SUCCESS;
9915 	unsigned long iflag;
9916 	uint32_t mcqe_status;
9917 	uint32_t mbx_cmnd;
9918 	struct lpfc_sli *psli = &phba->sli;
9919 	struct lpfc_mqe *mb = &mboxq->u.mqe;
9920 	struct lpfc_bmbx_create *mbox_rgn;
9921 	struct dma_address *dma_address;
9922 
9923 	/*
9924 	 * Only one mailbox can be active to the bootstrap mailbox region
9925 	 * at a time and there is no queueing provided.
9926 	 */
9927 	spin_lock_irqsave(&phba->hbalock, iflag);
9928 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9929 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9930 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9931 				"(%d):2532 Mailbox command x%x (x%x/x%x) "
9932 				"cannot issue Data: x%x x%x\n",
9933 				mboxq->vport ? mboxq->vport->vpi : 0,
9934 				mboxq->u.mb.mbxCommand,
9935 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9936 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9937 				psli->sli_flag, MBX_POLL);
9938 		return MBXERR_ERROR;
9939 	}
9940 	/* The server grabs the token and owns it until release */
9941 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9942 	phba->sli.mbox_active = mboxq;
9943 	spin_unlock_irqrestore(&phba->hbalock, iflag);
9944 
9945 	/* wait for bootstrap mbox register for readyness */
9946 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9947 	if (rc)
9948 		goto exit;
9949 	/*
9950 	 * Initialize the bootstrap memory region to avoid stale data areas
9951 	 * in the mailbox post.  Then copy the caller's mailbox contents to
9952 	 * the bmbx mailbox region.
9953 	 */
9954 	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
9955 	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
9956 	lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
9957 			       sizeof(struct lpfc_mqe));
9958 
9959 	/* Post the high mailbox dma address to the port and wait for ready. */
9960 	dma_address = &phba->sli4_hba.bmbx.dma_address;
9961 	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
9962 
9963 	/* wait for bootstrap mbox register for hi-address write done */
9964 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9965 	if (rc)
9966 		goto exit;
9967 
9968 	/* Post the low mailbox dma address to the port. */
9969 	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
9970 
9971 	/* wait for bootstrap mbox register for low address write done */
9972 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9973 	if (rc)
9974 		goto exit;
9975 
9976 	/*
9977 	 * Read the CQ to ensure the mailbox has completed.
9978 	 * If so, update the mailbox status so that the upper layers
9979 	 * can complete the request normally.
9980 	 */
9981 	lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
9982 			       sizeof(struct lpfc_mqe));
9983 	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
9984 	lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
9985 			       sizeof(struct lpfc_mcqe));
9986 	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
9987 	/*
9988 	 * When the CQE status indicates a failure and the mailbox status
9989 	 * indicates success then copy the CQE status into the mailbox status
9990 	 * (and prefix it with x4000).
9991 	 */
9992 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
9993 		if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
9994 			bf_set(lpfc_mqe_status, mb,
9995 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
9996 		rc = MBXERR_ERROR;
9997 	} else
9998 		lpfc_sli4_swap_str(phba, mboxq);
9999 
10000 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10001 			"(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
10002 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
10003 			" x%x x%x CQ: x%x x%x x%x x%x\n",
10004 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10005 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10006 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10007 			bf_get(lpfc_mqe_status, mb),
10008 			mb->un.mb_words[0], mb->un.mb_words[1],
10009 			mb->un.mb_words[2], mb->un.mb_words[3],
10010 			mb->un.mb_words[4], mb->un.mb_words[5],
10011 			mb->un.mb_words[6], mb->un.mb_words[7],
10012 			mb->un.mb_words[8], mb->un.mb_words[9],
10013 			mb->un.mb_words[10], mb->un.mb_words[11],
10014 			mb->un.mb_words[12], mboxq->mcqe.word0,
10015 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
10016 			mboxq->mcqe.trailer);
10017 exit:
10018 	/* We are holding the token, no needed for lock when release */
10019 	spin_lock_irqsave(&phba->hbalock, iflag);
10020 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10021 	phba->sli.mbox_active = NULL;
10022 	spin_unlock_irqrestore(&phba->hbalock, iflag);
10023 	return rc;
10024 }
10025 
10026 /**
10027  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
10028  * @phba: Pointer to HBA context object.
10029  * @mboxq: Pointer to mailbox object.
10030  * @flag: Flag indicating how the mailbox need to be processed.
10031  *
10032  * This function is called by discovery code and HBA management code to submit
10033  * a mailbox command to firmware with SLI-4 interface spec.
10034  *
10035  * Return codes the caller owns the mailbox command after the return of the
10036  * function.
10037  **/
10038 static int
lpfc_sli_issue_mbox_s4(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint32_t flag)10039 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
10040 		       uint32_t flag)
10041 {
10042 	struct lpfc_sli *psli = &phba->sli;
10043 	unsigned long iflags;
10044 	int rc;
10045 
10046 	/* dump from issue mailbox command if setup */
10047 	lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
10048 
10049 	rc = lpfc_mbox_dev_check(phba);
10050 	if (unlikely(rc)) {
10051 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10052 				"(%d):2544 Mailbox command x%x (x%x/x%x) "
10053 				"cannot issue Data: x%x x%x\n",
10054 				mboxq->vport ? mboxq->vport->vpi : 0,
10055 				mboxq->u.mb.mbxCommand,
10056 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10057 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10058 				psli->sli_flag, flag);
10059 		goto out_not_finished;
10060 	}
10061 
10062 	/* Detect polling mode and jump to a handler */
10063 	if (!phba->sli4_hba.intr_enable) {
10064 		if (flag == MBX_POLL)
10065 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10066 		else
10067 			rc = -EIO;
10068 		if (rc != MBX_SUCCESS)
10069 			lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10070 					"(%d):2541 Mailbox command x%x "
10071 					"(x%x/x%x) failure: "
10072 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10073 					"Data: x%x x%x\n",
10074 					mboxq->vport ? mboxq->vport->vpi : 0,
10075 					mboxq->u.mb.mbxCommand,
10076 					lpfc_sli_config_mbox_subsys_get(phba,
10077 									mboxq),
10078 					lpfc_sli_config_mbox_opcode_get(phba,
10079 									mboxq),
10080 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10081 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10082 					bf_get(lpfc_mcqe_ext_status,
10083 					       &mboxq->mcqe),
10084 					psli->sli_flag, flag);
10085 		return rc;
10086 	} else if (flag == MBX_POLL) {
10087 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10088 				"(%d):2542 Try to issue mailbox command "
10089 				"x%x (x%x/x%x) synchronously ahead of async "
10090 				"mailbox command queue: x%x x%x\n",
10091 				mboxq->vport ? mboxq->vport->vpi : 0,
10092 				mboxq->u.mb.mbxCommand,
10093 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10094 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10095 				psli->sli_flag, flag);
10096 		/* Try to block the asynchronous mailbox posting */
10097 		rc = lpfc_sli4_async_mbox_block(phba);
10098 		if (!rc) {
10099 			/* Successfully blocked, now issue sync mbox cmd */
10100 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10101 			if (rc != MBX_SUCCESS)
10102 				lpfc_printf_log(phba, KERN_WARNING,
10103 					LOG_MBOX | LOG_SLI,
10104 					"(%d):2597 Sync Mailbox command "
10105 					"x%x (x%x/x%x) failure: "
10106 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10107 					"Data: x%x x%x\n",
10108 					mboxq->vport ? mboxq->vport->vpi : 0,
10109 					mboxq->u.mb.mbxCommand,
10110 					lpfc_sli_config_mbox_subsys_get(phba,
10111 									mboxq),
10112 					lpfc_sli_config_mbox_opcode_get(phba,
10113 									mboxq),
10114 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10115 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10116 					bf_get(lpfc_mcqe_ext_status,
10117 					       &mboxq->mcqe),
10118 					psli->sli_flag, flag);
10119 			/* Unblock the async mailbox posting afterward */
10120 			lpfc_sli4_async_mbox_unblock(phba);
10121 		}
10122 		return rc;
10123 	}
10124 
10125 	/* Now, interrupt mode asynchronous mailbox command */
10126 	rc = lpfc_mbox_cmd_check(phba, mboxq);
10127 	if (rc) {
10128 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10129 				"(%d):2543 Mailbox command x%x (x%x/x%x) "
10130 				"cannot issue Data: x%x x%x\n",
10131 				mboxq->vport ? mboxq->vport->vpi : 0,
10132 				mboxq->u.mb.mbxCommand,
10133 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10134 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10135 				psli->sli_flag, flag);
10136 		goto out_not_finished;
10137 	}
10138 
10139 	/* Put the mailbox command to the driver internal FIFO */
10140 	psli->slistat.mbox_busy++;
10141 	spin_lock_irqsave(&phba->hbalock, iflags);
10142 	lpfc_mbox_put(phba, mboxq);
10143 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10144 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10145 			"(%d):0354 Mbox cmd issue - Enqueue Data: "
10146 			"x%x (x%x/x%x) x%x x%x x%x x%x\n",
10147 			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
10148 			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
10149 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10150 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10151 			mboxq->u.mb.un.varUnregLogin.rpi,
10152 			phba->pport->port_state,
10153 			psli->sli_flag, MBX_NOWAIT);
10154 	/* Wake up worker thread to transport mailbox command from head */
10155 	lpfc_worker_wake_up(phba);
10156 
10157 	return MBX_BUSY;
10158 
10159 out_not_finished:
10160 	return MBX_NOT_FINISHED;
10161 }
10162 
10163 /**
10164  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
10165  * @phba: Pointer to HBA context object.
10166  *
10167  * This function is called by worker thread to send a mailbox command to
10168  * SLI4 HBA firmware.
10169  *
10170  **/
10171 int
lpfc_sli4_post_async_mbox(struct lpfc_hba * phba)10172 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
10173 {
10174 	struct lpfc_sli *psli = &phba->sli;
10175 	LPFC_MBOXQ_t *mboxq;
10176 	int rc = MBX_SUCCESS;
10177 	unsigned long iflags;
10178 	struct lpfc_mqe *mqe;
10179 	uint32_t mbx_cmnd;
10180 
10181 	/* Check interrupt mode before post async mailbox command */
10182 	if (unlikely(!phba->sli4_hba.intr_enable))
10183 		return MBX_NOT_FINISHED;
10184 
10185 	/* Check for mailbox command service token */
10186 	spin_lock_irqsave(&phba->hbalock, iflags);
10187 	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
10188 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10189 		return MBX_NOT_FINISHED;
10190 	}
10191 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10192 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10193 		return MBX_NOT_FINISHED;
10194 	}
10195 	if (unlikely(phba->sli.mbox_active)) {
10196 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10197 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10198 				"0384 There is pending active mailbox cmd\n");
10199 		return MBX_NOT_FINISHED;
10200 	}
10201 	/* Take the mailbox command service token */
10202 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
10203 
10204 	/* Get the next mailbox command from head of queue */
10205 	mboxq = lpfc_mbox_get(phba);
10206 
10207 	/* If no more mailbox command waiting for post, we're done */
10208 	if (!mboxq) {
10209 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10210 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10211 		return MBX_SUCCESS;
10212 	}
10213 	phba->sli.mbox_active = mboxq;
10214 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10215 
10216 	/* Check device readiness for posting mailbox command */
10217 	rc = lpfc_mbox_dev_check(phba);
10218 	if (unlikely(rc))
10219 		/* Driver clean routine will clean up pending mailbox */
10220 		goto out_not_finished;
10221 
10222 	/* Prepare the mbox command to be posted */
10223 	mqe = &mboxq->u.mqe;
10224 	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
10225 
10226 	/* Start timer for the mbox_tmo and log some mailbox post messages */
10227 	mod_timer(&psli->mbox_tmo, (jiffies +
10228 		  secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq))));
10229 
10230 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10231 			"(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
10232 			"x%x x%x\n",
10233 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10234 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10235 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10236 			phba->pport->port_state, psli->sli_flag);
10237 
10238 	if (mbx_cmnd != MBX_HEARTBEAT) {
10239 		if (mboxq->vport) {
10240 			lpfc_debugfs_disc_trc(mboxq->vport,
10241 				LPFC_DISC_TRC_MBOX_VPORT,
10242 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
10243 				mbx_cmnd, mqe->un.mb_words[0],
10244 				mqe->un.mb_words[1]);
10245 		} else {
10246 			lpfc_debugfs_disc_trc(phba->pport,
10247 				LPFC_DISC_TRC_MBOX,
10248 				"MBOX Send: cmd:x%x mb:x%x x%x",
10249 				mbx_cmnd, mqe->un.mb_words[0],
10250 				mqe->un.mb_words[1]);
10251 		}
10252 	}
10253 	psli->slistat.mbox_cmd++;
10254 
10255 	/* Post the mailbox command to the port */
10256 	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
10257 	if (rc != MBX_SUCCESS) {
10258 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10259 				"(%d):2533 Mailbox command x%x (x%x/x%x) "
10260 				"cannot issue Data: x%x x%x\n",
10261 				mboxq->vport ? mboxq->vport->vpi : 0,
10262 				mboxq->u.mb.mbxCommand,
10263 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10264 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10265 				psli->sli_flag, MBX_NOWAIT);
10266 		goto out_not_finished;
10267 	}
10268 
10269 	return rc;
10270 
10271 out_not_finished:
10272 	spin_lock_irqsave(&phba->hbalock, iflags);
10273 	if (phba->sli.mbox_active) {
10274 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10275 		__lpfc_mbox_cmpl_put(phba, mboxq);
10276 		/* Release the token */
10277 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10278 		phba->sli.mbox_active = NULL;
10279 	}
10280 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10281 
10282 	return MBX_NOT_FINISHED;
10283 }
10284 
10285 /**
10286  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
10287  * @phba: Pointer to HBA context object.
10288  * @pmbox: Pointer to mailbox object.
10289  * @flag: Flag indicating how the mailbox need to be processed.
10290  *
10291  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
10292  * the API jump table function pointer from the lpfc_hba struct.
10293  *
10294  * Return codes the caller owns the mailbox command after the return of the
10295  * function.
10296  **/
10297 int
lpfc_sli_issue_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)10298 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
10299 {
10300 	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
10301 }
10302 
10303 /**
10304  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
10305  * @phba: The hba struct for which this call is being executed.
10306  * @dev_grp: The HBA PCI-Device group number.
10307  *
10308  * This routine sets up the mbox interface API function jump table in @phba
10309  * struct.
10310  * Returns: 0 - success, -ENODEV - failure.
10311  **/
10312 int
lpfc_mbox_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)10313 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10314 {
10315 
10316 	switch (dev_grp) {
10317 	case LPFC_PCI_DEV_LP:
10318 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
10319 		phba->lpfc_sli_handle_slow_ring_event =
10320 				lpfc_sli_handle_slow_ring_event_s3;
10321 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
10322 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
10323 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
10324 		break;
10325 	case LPFC_PCI_DEV_OC:
10326 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
10327 		phba->lpfc_sli_handle_slow_ring_event =
10328 				lpfc_sli_handle_slow_ring_event_s4;
10329 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10330 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10331 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10332 		break;
10333 	default:
10334 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10335 				"1420 Invalid HBA PCI-device group: 0x%x\n",
10336 				dev_grp);
10337 		return -ENODEV;
10338 	}
10339 	return 0;
10340 }
10341 
10342 /**
10343  * __lpfc_sli_ringtx_put - Add an iocb to the txq
10344  * @phba: Pointer to HBA context object.
10345  * @pring: Pointer to driver SLI ring object.
10346  * @piocb: Pointer to address of newly added command iocb.
10347  *
10348  * This function is called with hbalock held for SLI3 ports or
10349  * the ring lock held for SLI4 ports to add a command
10350  * iocb to the txq when SLI layer cannot submit the command iocb
10351  * to the ring.
10352  **/
10353 void
__lpfc_sli_ringtx_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)10354 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10355 		    struct lpfc_iocbq *piocb)
10356 {
10357 	if (phba->sli_rev == LPFC_SLI_REV4)
10358 		lockdep_assert_held(&pring->ring_lock);
10359 	else
10360 		lockdep_assert_held(&phba->hbalock);
10361 	/* Insert the caller's iocb in the txq tail for later processing. */
10362 	list_add_tail(&piocb->list, &pring->txq);
10363 }
10364 
10365 /**
10366  * lpfc_sli_next_iocb - Get the next iocb in the txq
10367  * @phba: Pointer to HBA context object.
10368  * @pring: Pointer to driver SLI ring object.
10369  * @piocb: Pointer to address of newly added command iocb.
10370  *
10371  * This function is called with hbalock held before a new
10372  * iocb is submitted to the firmware. This function checks
10373  * txq to flush the iocbs in txq to Firmware before
10374  * submitting new iocbs to the Firmware.
10375  * If there are iocbs in the txq which need to be submitted
10376  * to firmware, lpfc_sli_next_iocb returns the first element
10377  * of the txq after dequeuing it from txq.
10378  * If there is no iocb in the txq then the function will return
10379  * *piocb and *piocb is set to NULL. Caller needs to check
10380  * *piocb to find if there are more commands in the txq.
10381  **/
10382 static struct lpfc_iocbq *
lpfc_sli_next_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq ** piocb)10383 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10384 		   struct lpfc_iocbq **piocb)
10385 {
10386 	struct lpfc_iocbq * nextiocb;
10387 
10388 	lockdep_assert_held(&phba->hbalock);
10389 
10390 	nextiocb = lpfc_sli_ringtx_get(phba, pring);
10391 	if (!nextiocb) {
10392 		nextiocb = *piocb;
10393 		*piocb = NULL;
10394 	}
10395 
10396 	return nextiocb;
10397 }
10398 
10399 /**
10400  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
10401  * @phba: Pointer to HBA context object.
10402  * @ring_number: SLI ring number to issue iocb on.
10403  * @piocb: Pointer to command iocb.
10404  * @flag: Flag indicating if this command can be put into txq.
10405  *
10406  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10407  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10408  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10409  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10410  * this function allows only iocbs for posting buffers. This function finds
10411  * next available slot in the command ring and posts the command to the
10412  * available slot and writes the port attention register to request HBA start
10413  * processing new iocb. If there is no slot available in the ring and
10414  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10415  * the function returns IOCB_BUSY.
10416  *
10417  * This function is called with hbalock held. The function will return success
10418  * after it successfully submit the iocb to firmware or after adding to the
10419  * txq.
10420  **/
10421 static int
__lpfc_sli_issue_iocb_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10422 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
10423 		    struct lpfc_iocbq *piocb, uint32_t flag)
10424 {
10425 	struct lpfc_iocbq *nextiocb;
10426 	IOCB_t *iocb;
10427 	struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
10428 
10429 	lockdep_assert_held(&phba->hbalock);
10430 
10431 	if (piocb->cmd_cmpl && (!piocb->vport) &&
10432 	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10433 	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
10434 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10435 				"1807 IOCB x%x failed. No vport\n",
10436 				piocb->iocb.ulpCommand);
10437 		dump_stack();
10438 		return IOCB_ERROR;
10439 	}
10440 
10441 
10442 	/* If the PCI channel is in offline state, do not post iocbs. */
10443 	if (unlikely(pci_channel_offline(phba->pcidev)))
10444 		return IOCB_ERROR;
10445 
10446 	/* If HBA has a deferred error attention, fail the iocb. */
10447 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
10448 		return IOCB_ERROR;
10449 
10450 	/*
10451 	 * We should never get an IOCB if we are in a < LINK_DOWN state
10452 	 */
10453 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10454 		return IOCB_ERROR;
10455 
10456 	/*
10457 	 * Check to see if we are blocking IOCB processing because of a
10458 	 * outstanding event.
10459 	 */
10460 	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
10461 		goto iocb_busy;
10462 
10463 	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
10464 		/*
10465 		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
10466 		 * can be issued if the link is not up.
10467 		 */
10468 		switch (piocb->iocb.ulpCommand) {
10469 		case CMD_QUE_RING_BUF_CN:
10470 		case CMD_QUE_RING_BUF64_CN:
10471 			/*
10472 			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10473 			 * completion, cmd_cmpl MUST be 0.
10474 			 */
10475 			if (piocb->cmd_cmpl)
10476 				piocb->cmd_cmpl = NULL;
10477 			fallthrough;
10478 		case CMD_CREATE_XRI_CR:
10479 		case CMD_CLOSE_XRI_CN:
10480 		case CMD_CLOSE_XRI_CX:
10481 			break;
10482 		default:
10483 			goto iocb_busy;
10484 		}
10485 
10486 	/*
10487 	 * For FCP commands, we must be in a state where we can process link
10488 	 * attention events.
10489 	 */
10490 	} else if (unlikely(pring->ringno == LPFC_FCP_RING &&
10491 			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
10492 		goto iocb_busy;
10493 	}
10494 
10495 	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10496 	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10497 		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10498 
10499 	if (iocb)
10500 		lpfc_sli_update_ring(phba, pring);
10501 	else
10502 		lpfc_sli_update_full_ring(phba, pring);
10503 
10504 	if (!piocb)
10505 		return IOCB_SUCCESS;
10506 
10507 	goto out_busy;
10508 
10509  iocb_busy:
10510 	pring->stats.iocb_cmd_delay++;
10511 
10512  out_busy:
10513 
10514 	if (!(flag & SLI_IOCB_RET_IOCB)) {
10515 		__lpfc_sli_ringtx_put(phba, pring, piocb);
10516 		return IOCB_SUCCESS;
10517 	}
10518 
10519 	return IOCB_BUSY;
10520 }
10521 
10522 /**
10523  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10524  * @phba: Pointer to HBA context object.
10525  * @ring_number: SLI ring number to issue wqe on.
10526  * @piocb: Pointer to command iocb.
10527  * @flag: Flag indicating if this command can be put into txq.
10528  *
10529  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10530  * send  an iocb command to an HBA with SLI-3 interface spec.
10531  *
10532  * This function takes the hbalock before invoking the lockless version.
10533  * The function will return success after it successfully submit the wqe to
10534  * firmware or after adding to the txq.
10535  **/
10536 static int
__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10537 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10538 			   struct lpfc_iocbq *piocb, uint32_t flag)
10539 {
10540 	unsigned long iflags;
10541 	int rc;
10542 
10543 	spin_lock_irqsave(&phba->hbalock, iflags);
10544 	rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10545 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10546 
10547 	return rc;
10548 }
10549 
10550 /**
10551  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10552  * @phba: Pointer to HBA context object.
10553  * @ring_number: SLI ring number to issue wqe on.
10554  * @piocb: Pointer to command iocb.
10555  * @flag: Flag indicating if this command can be put into txq.
10556  *
10557  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10558  * an wqe command to an HBA with SLI-4 interface spec.
10559  *
10560  * This function is a lockless version. The function will return success
10561  * after it successfully submit the wqe to firmware or after adding to the
10562  * txq.
10563  **/
10564 static int
__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10565 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10566 			   struct lpfc_iocbq *piocb, uint32_t flag)
10567 {
10568 	struct lpfc_io_buf *lpfc_cmd = piocb->io_buf;
10569 
10570 	lpfc_prep_embed_io(phba, lpfc_cmd);
10571 	return lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10572 }
10573 
10574 void
lpfc_prep_embed_io(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_cmd)10575 lpfc_prep_embed_io(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
10576 {
10577 	struct lpfc_iocbq *piocb = &lpfc_cmd->cur_iocbq;
10578 	union lpfc_wqe128 *wqe = &lpfc_cmd->cur_iocbq.wqe;
10579 	struct sli4_sge_le *sgl;
10580 	u32 type_size;
10581 
10582 	/* 128 byte wqe support here */
10583 	sgl = (struct sli4_sge_le *)lpfc_cmd->dma_sgl;
10584 
10585 	if (phba->fcp_embed_io) {
10586 		struct fcp_cmnd *fcp_cmnd;
10587 		u32 *ptr;
10588 
10589 		fcp_cmnd = lpfc_cmd->fcp_cmnd;
10590 
10591 		/* Word 0-2 - FCP_CMND */
10592 		type_size = le32_to_cpu(sgl->sge_len);
10593 		type_size |= ULP_BDE64_TYPE_BDE_IMMED;
10594 		wqe->generic.bde.tus.w = type_size;
10595 		wqe->generic.bde.addrHigh = 0;
10596 		wqe->generic.bde.addrLow =  72;  /* Word 18 */
10597 
10598 		bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10599 		bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10600 
10601 		/* Word 18-29  FCP CMND Payload */
10602 		ptr = &wqe->words[18];
10603 		lpfc_sli_pcimem_bcopy(fcp_cmnd, ptr, le32_to_cpu(sgl->sge_len));
10604 	} else {
10605 		/* Word 0-2 - Inline BDE */
10606 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10607 		wqe->generic.bde.tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
10608 		wqe->generic.bde.addrHigh = le32_to_cpu(sgl->addr_hi);
10609 		wqe->generic.bde.addrLow = le32_to_cpu(sgl->addr_lo);
10610 
10611 		/* Word 10 */
10612 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10613 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10614 	}
10615 
10616 	/* add the VMID tags as per switch response */
10617 	if (unlikely(piocb->cmd_flag & LPFC_IO_VMID)) {
10618 		if (phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
10619 			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10620 			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10621 					(piocb->vmid_tag.cs_ctl_vmid));
10622 		} else if (phba->cfg_vmid_app_header) {
10623 			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
10624 			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10625 			wqe->words[31] = piocb->vmid_tag.app_id;
10626 		}
10627 	}
10628 }
10629 
10630 /**
10631  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10632  * @phba: Pointer to HBA context object.
10633  * @ring_number: SLI ring number to issue iocb on.
10634  * @piocb: Pointer to command iocb.
10635  * @flag: Flag indicating if this command can be put into txq.
10636  *
10637  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10638  * an iocb command to an HBA with SLI-4 interface spec.
10639  *
10640  * This function is called with ringlock held. The function will return success
10641  * after it successfully submit the iocb to firmware or after adding to the
10642  * txq.
10643  **/
10644 static int
__lpfc_sli_issue_iocb_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10645 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10646 			 struct lpfc_iocbq *piocb, uint32_t flag)
10647 {
10648 	struct lpfc_sglq *sglq;
10649 	union lpfc_wqe128 *wqe;
10650 	struct lpfc_queue *wq;
10651 	struct lpfc_sli_ring *pring;
10652 	u32 ulp_command = get_job_cmnd(phba, piocb);
10653 
10654 	/* Get the WQ */
10655 	if ((piocb->cmd_flag & LPFC_IO_FCP) ||
10656 	    (piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
10657 		wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10658 	} else {
10659 		wq = phba->sli4_hba.els_wq;
10660 	}
10661 
10662 	/* Get corresponding ring */
10663 	pring = wq->pring;
10664 
10665 	/*
10666 	 * The WQE can be either 64 or 128 bytes,
10667 	 */
10668 
10669 	lockdep_assert_held(&pring->ring_lock);
10670 	wqe = &piocb->wqe;
10671 	if (piocb->sli4_xritag == NO_XRI) {
10672 		if (ulp_command == CMD_ABORT_XRI_CX)
10673 			sglq = NULL;
10674 		else {
10675 			sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10676 			if (!sglq) {
10677 				if (!(flag & SLI_IOCB_RET_IOCB)) {
10678 					__lpfc_sli_ringtx_put(phba,
10679 							pring,
10680 							piocb);
10681 					return IOCB_SUCCESS;
10682 				} else {
10683 					return IOCB_BUSY;
10684 				}
10685 			}
10686 		}
10687 	} else if (piocb->cmd_flag &  LPFC_IO_FCP) {
10688 		/* These IO's already have an XRI and a mapped sgl. */
10689 		sglq = NULL;
10690 	}
10691 	else {
10692 		/*
10693 		 * This is a continuation of a commandi,(CX) so this
10694 		 * sglq is on the active list
10695 		 */
10696 		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10697 		if (!sglq)
10698 			return IOCB_ERROR;
10699 	}
10700 
10701 	if (sglq) {
10702 		piocb->sli4_lxritag = sglq->sli4_lxritag;
10703 		piocb->sli4_xritag = sglq->sli4_xritag;
10704 
10705 		/* ABTS sent by initiator to CT exchange, the
10706 		 * RX_ID field will be filled with the newly
10707 		 * allocated responder XRI.
10708 		 */
10709 		if (ulp_command == CMD_XMIT_BLS_RSP64_CX &&
10710 		    piocb->abort_bls == LPFC_ABTS_UNSOL_INT)
10711 			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10712 			       piocb->sli4_xritag);
10713 
10714 		bf_set(wqe_xri_tag, &wqe->generic.wqe_com,
10715 		       piocb->sli4_xritag);
10716 
10717 		if (lpfc_wqe_bpl2sgl(phba, piocb, sglq) == NO_XRI)
10718 			return IOCB_ERROR;
10719 	}
10720 
10721 	if (lpfc_sli4_wq_put(wq, wqe))
10722 		return IOCB_ERROR;
10723 
10724 	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10725 
10726 	return 0;
10727 }
10728 
10729 /*
10730  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10731  *
10732  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10733  * or IOCB for sli-3  function.
10734  * pointer from the lpfc_hba struct.
10735  *
10736  * Return codes:
10737  * IOCB_ERROR - Error
10738  * IOCB_SUCCESS - Success
10739  * IOCB_BUSY - Busy
10740  **/
10741 int
lpfc_sli_issue_fcp_io(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10742 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10743 		      struct lpfc_iocbq *piocb, uint32_t flag)
10744 {
10745 	return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10746 }
10747 
10748 /*
10749  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10750  *
10751  * This routine wraps the actual lockless version for issusing IOCB function
10752  * pointer from the lpfc_hba struct.
10753  *
10754  * Return codes:
10755  * IOCB_ERROR - Error
10756  * IOCB_SUCCESS - Success
10757  * IOCB_BUSY - Busy
10758  **/
10759 int
__lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10760 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10761 		struct lpfc_iocbq *piocb, uint32_t flag)
10762 {
10763 	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10764 }
10765 
10766 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)10767 __lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq *cmdiocbq,
10768 			       struct lpfc_vport *vport,
10769 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10770 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10771 {
10772 	struct lpfc_hba *phba = vport->phba;
10773 	IOCB_t *cmd;
10774 
10775 	cmd = &cmdiocbq->iocb;
10776 	memset(cmd, 0, sizeof(*cmd));
10777 
10778 	cmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10779 	cmd->un.elsreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10780 	cmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10781 
10782 	if (expect_rsp) {
10783 		cmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
10784 		cmd->un.elsreq64.remoteID = did; /* DID */
10785 		cmd->ulpCommand = CMD_ELS_REQUEST64_CR;
10786 		cmd->ulpTimeout = tmo;
10787 	} else {
10788 		cmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
10789 		cmd->un.genreq64.xmit_els_remoteID = did; /* DID */
10790 		cmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
10791 		cmd->ulpPU = PARM_NPIV_DID;
10792 	}
10793 	cmd->ulpBdeCount = 1;
10794 	cmd->ulpLe = 1;
10795 	cmd->ulpClass = CLASS3;
10796 
10797 	/* If we have NPIV enabled, we want to send ELS traffic by VPI. */
10798 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
10799 		if (expect_rsp) {
10800 			cmd->un.elsreq64.myID = vport->fc_myDID;
10801 
10802 			/* For ELS_REQUEST64_CR, use the VPI by default */
10803 			cmd->ulpContext = phba->vpi_ids[vport->vpi];
10804 		}
10805 
10806 		cmd->ulpCt_h = 0;
10807 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10808 		if (elscmd == ELS_CMD_ECHO)
10809 			cmd->ulpCt_l = 0; /* context = invalid RPI */
10810 		else
10811 			cmd->ulpCt_l = 1; /* context = VPI */
10812 	}
10813 }
10814 
10815 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)10816 __lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq *cmdiocbq,
10817 			       struct lpfc_vport *vport,
10818 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10819 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10820 {
10821 	struct lpfc_hba  *phba = vport->phba;
10822 	union lpfc_wqe128 *wqe;
10823 	struct ulp_bde64_le *bde;
10824 	u8 els_id;
10825 
10826 	wqe = &cmdiocbq->wqe;
10827 	memset(wqe, 0, sizeof(*wqe));
10828 
10829 	/* Word 0 - 2 BDE */
10830 	bde = (struct ulp_bde64_le *)&wqe->generic.bde;
10831 	bde->addr_low = cpu_to_le32(putPaddrLow(bmp->phys));
10832 	bde->addr_high = cpu_to_le32(putPaddrHigh(bmp->phys));
10833 	bde->type_size = cpu_to_le32(cmd_size);
10834 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10835 
10836 	if (expect_rsp) {
10837 		bf_set(wqe_cmnd, &wqe->els_req.wqe_com, CMD_ELS_REQUEST64_WQE);
10838 
10839 		/* Transfer length */
10840 		wqe->els_req.payload_len = cmd_size;
10841 		wqe->els_req.max_response_payload_len = FCELSSIZE;
10842 
10843 		/* DID */
10844 		bf_set(wqe_els_did, &wqe->els_req.wqe_dest, did);
10845 
10846 		/* Word 11 - ELS_ID */
10847 		switch (elscmd) {
10848 		case ELS_CMD_PLOGI:
10849 			els_id = LPFC_ELS_ID_PLOGI;
10850 			break;
10851 		case ELS_CMD_FLOGI:
10852 			els_id = LPFC_ELS_ID_FLOGI;
10853 			break;
10854 		case ELS_CMD_LOGO:
10855 			els_id = LPFC_ELS_ID_LOGO;
10856 			break;
10857 		case ELS_CMD_FDISC:
10858 			if (!vport->fc_myDID) {
10859 				els_id = LPFC_ELS_ID_FDISC;
10860 				break;
10861 			}
10862 			fallthrough;
10863 		default:
10864 			els_id = LPFC_ELS_ID_DEFAULT;
10865 			break;
10866 		}
10867 
10868 		bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10869 	} else {
10870 		/* DID */
10871 		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest, did);
10872 
10873 		/* Transfer length */
10874 		wqe->xmit_els_rsp.response_payload_len = cmd_size;
10875 
10876 		bf_set(wqe_cmnd, &wqe->xmit_els_rsp.wqe_com,
10877 		       CMD_XMIT_ELS_RSP64_WQE);
10878 	}
10879 
10880 	bf_set(wqe_tmo, &wqe->generic.wqe_com, tmo);
10881 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, cmdiocbq->iotag);
10882 	bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
10883 
10884 	/* If we have NPIV enabled, we want to send ELS traffic by VPI.
10885 	 * For SLI4, since the driver controls VPIs we also want to include
10886 	 * all ELS pt2pt protocol traffic as well.
10887 	 */
10888 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
10889 	    test_bit(FC_PT2PT, &vport->fc_flag)) {
10890 		if (expect_rsp) {
10891 			bf_set(els_req64_sid, &wqe->els_req, vport->fc_myDID);
10892 
10893 			/* For ELS_REQUEST64_WQE, use the VPI by default */
10894 			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10895 			       phba->vpi_ids[vport->vpi]);
10896 		}
10897 
10898 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10899 		if (elscmd == ELS_CMD_ECHO)
10900 			bf_set(wqe_ct, &wqe->generic.wqe_com, 0);
10901 		else
10902 			bf_set(wqe_ct, &wqe->generic.wqe_com, 1);
10903 	}
10904 }
10905 
10906 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)10907 lpfc_sli_prep_els_req_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
10908 			  struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
10909 			  u16 cmd_size, u32 did, u32 elscmd, u8 tmo,
10910 			  u8 expect_rsp)
10911 {
10912 	phba->__lpfc_sli_prep_els_req_rsp(cmdiocbq, vport, bmp, cmd_size, did,
10913 					  elscmd, tmo, expect_rsp);
10914 }
10915 
10916 static void
__lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10917 __lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10918 			   u16 rpi, u32 num_entry, u8 tmo)
10919 {
10920 	IOCB_t *cmd;
10921 
10922 	cmd = &cmdiocbq->iocb;
10923 	memset(cmd, 0, sizeof(*cmd));
10924 
10925 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10926 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10927 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10928 	cmd->un.genreq64.bdl.bdeSize = num_entry * sizeof(struct ulp_bde64);
10929 
10930 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
10931 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
10932 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
10933 
10934 	cmd->ulpContext = rpi;
10935 	cmd->ulpClass = CLASS3;
10936 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
10937 	cmd->ulpBdeCount = 1;
10938 	cmd->ulpLe = 1;
10939 	cmd->ulpOwner = OWN_CHIP;
10940 	cmd->ulpTimeout = tmo;
10941 }
10942 
10943 static void
__lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10944 __lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10945 			   u16 rpi, u32 num_entry, u8 tmo)
10946 {
10947 	union lpfc_wqe128 *cmdwqe;
10948 	struct ulp_bde64_le *bde, *bpl;
10949 	u32 xmit_len = 0, total_len = 0, size, type, i;
10950 
10951 	cmdwqe = &cmdiocbq->wqe;
10952 	memset(cmdwqe, 0, sizeof(*cmdwqe));
10953 
10954 	/* Calculate total_len and xmit_len */
10955 	bpl = (struct ulp_bde64_le *)bmp->virt;
10956 	for (i = 0; i < num_entry; i++) {
10957 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10958 		total_len += size;
10959 	}
10960 	for (i = 0; i < num_entry; i++) {
10961 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10962 		type = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_TYPE_MASK;
10963 		if (type != ULP_BDE64_TYPE_BDE_64)
10964 			break;
10965 		xmit_len += size;
10966 	}
10967 
10968 	/* Words 0 - 2 */
10969 	bde = (struct ulp_bde64_le *)&cmdwqe->generic.bde;
10970 	bde->addr_low = bpl->addr_low;
10971 	bde->addr_high = bpl->addr_high;
10972 	bde->type_size = cpu_to_le32(xmit_len);
10973 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10974 
10975 	/* Word 3 */
10976 	cmdwqe->gen_req.request_payload_len = xmit_len;
10977 
10978 	/* Word 5 */
10979 	bf_set(wqe_type, &cmdwqe->gen_req.wge_ctl, FC_TYPE_CT);
10980 	bf_set(wqe_rctl, &cmdwqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);
10981 	bf_set(wqe_si, &cmdwqe->gen_req.wge_ctl, 1);
10982 	bf_set(wqe_la, &cmdwqe->gen_req.wge_ctl, 1);
10983 
10984 	/* Word 6 */
10985 	bf_set(wqe_ctxt_tag, &cmdwqe->gen_req.wqe_com, rpi);
10986 
10987 	/* Word 7 */
10988 	bf_set(wqe_tmo, &cmdwqe->gen_req.wqe_com, tmo);
10989 	bf_set(wqe_class, &cmdwqe->gen_req.wqe_com, CLASS3);
10990 	bf_set(wqe_cmnd, &cmdwqe->gen_req.wqe_com, CMD_GEN_REQUEST64_CR);
10991 	bf_set(wqe_ct, &cmdwqe->gen_req.wqe_com, SLI4_CT_RPI);
10992 
10993 	/* Word 12 */
10994 	cmdwqe->gen_req.max_response_payload_len = total_len - xmit_len;
10995 }
10996 
10997 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)10998 lpfc_sli_prep_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
10999 		      struct lpfc_dmabuf *bmp, u16 rpi, u32 num_entry, u8 tmo)
11000 {
11001 	phba->__lpfc_sli_prep_gen_req(cmdiocbq, bmp, rpi, num_entry, tmo);
11002 }
11003 
11004 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)11005 __lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq *cmdiocbq,
11006 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11007 			      u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11008 {
11009 	IOCB_t *icmd;
11010 
11011 	icmd = &cmdiocbq->iocb;
11012 	memset(icmd, 0, sizeof(*icmd));
11013 
11014 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
11015 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
11016 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
11017 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
11018 	icmd->un.xseq64.w5.hcsw.Fctl = LA;
11019 	if (last_seq)
11020 		icmd->un.xseq64.w5.hcsw.Fctl |= LS;
11021 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11022 	icmd->un.xseq64.w5.hcsw.Rctl = rctl;
11023 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
11024 
11025 	icmd->ulpBdeCount = 1;
11026 	icmd->ulpLe = 1;
11027 	icmd->ulpClass = CLASS3;
11028 
11029 	switch (cr_cx_cmd) {
11030 	case CMD_XMIT_SEQUENCE64_CR:
11031 		icmd->ulpContext = rpi;
11032 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
11033 		break;
11034 	case CMD_XMIT_SEQUENCE64_CX:
11035 		icmd->ulpContext = ox_id;
11036 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
11037 		break;
11038 	default:
11039 		break;
11040 	}
11041 }
11042 
11043 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)11044 __lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq *cmdiocbq,
11045 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11046 			      u32 full_size, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11047 {
11048 	union lpfc_wqe128 *wqe;
11049 	struct ulp_bde64 *bpl;
11050 
11051 	wqe = &cmdiocbq->wqe;
11052 	memset(wqe, 0, sizeof(*wqe));
11053 
11054 	/* Words 0 - 2 */
11055 	bpl = (struct ulp_bde64 *)bmp->virt;
11056 	wqe->xmit_sequence.bde.addrHigh = bpl->addrHigh;
11057 	wqe->xmit_sequence.bde.addrLow = bpl->addrLow;
11058 	wqe->xmit_sequence.bde.tus.w = bpl->tus.w;
11059 
11060 	/* Word 5 */
11061 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, last_seq);
11062 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 1);
11063 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
11064 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, rctl);
11065 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_CT);
11066 
11067 	/* Word 6 */
11068 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com, rpi);
11069 
11070 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
11071 	       CMD_XMIT_SEQUENCE64_WQE);
11072 
11073 	/* Word 7 */
11074 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
11075 
11076 	/* Word 9 */
11077 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ox_id);
11078 
11079 	if (cmdiocbq->cmd_flag & (LPFC_IO_LIBDFC | LPFC_IO_LOOPBACK)) {
11080 		/* Word 10 */
11081 		if (cmdiocbq->cmd_flag & LPFC_IO_VMID) {
11082 			bf_set(wqe_appid, &wqe->xmit_sequence.wqe_com, 1);
11083 			bf_set(wqe_wqes, &wqe->xmit_sequence.wqe_com, 1);
11084 			wqe->words[31] = LOOPBACK_SRC_APPID;
11085 		}
11086 
11087 		/* Word 12 */
11088 		wqe->xmit_sequence.xmit_len = full_size;
11089 	}
11090 	else
11091 		wqe->xmit_sequence.xmit_len =
11092 			wqe->xmit_sequence.bde.tus.f.bdeSize;
11093 }
11094 
11095 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)11096 lpfc_sli_prep_xmit_seq64(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11097 			 struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11098 			 u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11099 {
11100 	phba->__lpfc_sli_prep_xmit_seq64(cmdiocbq, bmp, rpi, ox_id, num_entry,
11101 					 rctl, last_seq, cr_cx_cmd);
11102 }
11103 
11104 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)11105 __lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11106 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11107 			     bool wqec)
11108 {
11109 	IOCB_t *icmd = NULL;
11110 
11111 	icmd = &cmdiocbq->iocb;
11112 	memset(icmd, 0, sizeof(*icmd));
11113 
11114 	/* Word 5 */
11115 	icmd->un.acxri.abortContextTag = ulp_context;
11116 	icmd->un.acxri.abortIoTag = iotag;
11117 
11118 	if (ia) {
11119 		/* Word 7 */
11120 		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
11121 	} else {
11122 		/* Word 3 */
11123 		icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
11124 
11125 		/* Word 7 */
11126 		icmd->ulpClass = ulp_class;
11127 		icmd->ulpCommand = CMD_ABORT_XRI_CN;
11128 	}
11129 
11130 	/* Word 7 */
11131 	icmd->ulpLe = 1;
11132 }
11133 
11134 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)11135 __lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11136 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11137 			     bool wqec)
11138 {
11139 	union lpfc_wqe128 *wqe;
11140 
11141 	wqe = &cmdiocbq->wqe;
11142 	memset(wqe, 0, sizeof(*wqe));
11143 
11144 	/* Word 3 */
11145 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
11146 	if (ia)
11147 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
11148 	else
11149 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
11150 
11151 	/* Word 7 */
11152 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_WQE);
11153 
11154 	/* Word 8 */
11155 	wqe->abort_cmd.wqe_com.abort_tag = ulp_context;
11156 
11157 	/* Word 9 */
11158 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, iotag);
11159 
11160 	/* Word 10 */
11161 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
11162 
11163 	/* Word 11 */
11164 	if (wqec)
11165 		bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
11166 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, cqid);
11167 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11168 }
11169 
11170 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)11171 lpfc_sli_prep_abort_xri(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11172 			u16 ulp_context, u16 iotag, u8 ulp_class, u16 cqid,
11173 			bool ia, bool wqec)
11174 {
11175 	phba->__lpfc_sli_prep_abort_xri(cmdiocbq, ulp_context, iotag, ulp_class,
11176 					cqid, ia, wqec);
11177 }
11178 
11179 /**
11180  * lpfc_sli_api_table_setup - Set up sli api function jump table
11181  * @phba: The hba struct for which this call is being executed.
11182  * @dev_grp: The HBA PCI-Device group number.
11183  *
11184  * This routine sets up the SLI interface API function jump table in @phba
11185  * struct.
11186  * Returns: 0 - success, -ENODEV - failure.
11187  **/
11188 int
lpfc_sli_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)11189 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11190 {
11191 
11192 	switch (dev_grp) {
11193 	case LPFC_PCI_DEV_LP:
11194 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11195 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
11196 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
11197 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s3;
11198 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s3;
11199 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s3;
11200 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s3;
11201 		break;
11202 	case LPFC_PCI_DEV_OC:
11203 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11204 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
11205 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
11206 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s4;
11207 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s4;
11208 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s4;
11209 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s4;
11210 		break;
11211 	default:
11212 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11213 				"1419 Invalid HBA PCI-device group: 0x%x\n",
11214 				dev_grp);
11215 		return -ENODEV;
11216 	}
11217 	return 0;
11218 }
11219 
11220 /**
11221  * lpfc_sli4_calc_ring - Calculates which ring to use
11222  * @phba: Pointer to HBA context object.
11223  * @piocb: Pointer to command iocb.
11224  *
11225  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11226  * hba_wqidx, thus we need to calculate the corresponding ring.
11227  * Since ABORTS must go on the same WQ of the command they are
11228  * aborting, we use command's hba_wqidx.
11229  */
11230 struct lpfc_sli_ring *
lpfc_sli4_calc_ring(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)11231 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
11232 {
11233 	struct lpfc_io_buf *lpfc_cmd;
11234 
11235 	if (piocb->cmd_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
11236 		if (unlikely(!phba->sli4_hba.hdwq))
11237 			return NULL;
11238 		/*
11239 		 * for abort iocb hba_wqidx should already
11240 		 * be setup based on what work queue we used.
11241 		 */
11242 		if (!(piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
11243 			lpfc_cmd = piocb->io_buf;
11244 			piocb->hba_wqidx = lpfc_cmd->hdwq_no;
11245 		}
11246 		return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
11247 	} else {
11248 		if (unlikely(!phba->sli4_hba.els_wq))
11249 			return NULL;
11250 		piocb->hba_wqidx = 0;
11251 		return phba->sli4_hba.els_wq->pring;
11252 	}
11253 }
11254 
lpfc_sli4_poll_eq(struct lpfc_queue * eq)11255 inline void lpfc_sli4_poll_eq(struct lpfc_queue *eq)
11256 {
11257 	struct lpfc_hba *phba = eq->phba;
11258 
11259 	/*
11260 	 * Unlocking an irq is one of the entry point to check
11261 	 * for re-schedule, but we are good for io submission
11262 	 * path as midlayer does a get_cpu to glue us in. Flush
11263 	 * out the invalidate queue so we can see the updated
11264 	 * value for flag.
11265 	 */
11266 	smp_rmb();
11267 
11268 	if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
11269 		/* We will not likely get the completion for the caller
11270 		 * during this iteration but i guess that's fine.
11271 		 * Future io's coming on this eq should be able to
11272 		 * pick it up.  As for the case of single io's, they
11273 		 * will be handled through a sched from polling timer
11274 		 * function which is currently triggered every 1msec.
11275 		 */
11276 		lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM,
11277 				     LPFC_QUEUE_WORK);
11278 }
11279 
11280 /**
11281  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
11282  * @phba: Pointer to HBA context object.
11283  * @ring_number: Ring number
11284  * @piocb: Pointer to command iocb.
11285  * @flag: Flag indicating if this command can be put into txq.
11286  *
11287  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11288  * function. This function gets the hbalock and calls
11289  * __lpfc_sli_issue_iocb function and will return the error returned
11290  * by __lpfc_sli_issue_iocb function. This wrapper is used by
11291  * functions which do not hold hbalock.
11292  **/
11293 int
lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)11294 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11295 		    struct lpfc_iocbq *piocb, uint32_t flag)
11296 {
11297 	struct lpfc_sli_ring *pring;
11298 	struct lpfc_queue *eq;
11299 	unsigned long iflags;
11300 	int rc;
11301 
11302 	/* If the PCI channel is in offline state, do not post iocbs. */
11303 	if (unlikely(pci_channel_offline(phba->pcidev)))
11304 		return IOCB_ERROR;
11305 
11306 	if (phba->sli_rev == LPFC_SLI_REV4) {
11307 		lpfc_sli_prep_wqe(phba, piocb);
11308 
11309 		eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11310 
11311 		pring = lpfc_sli4_calc_ring(phba, piocb);
11312 		if (unlikely(pring == NULL))
11313 			return IOCB_ERROR;
11314 
11315 		spin_lock_irqsave(&pring->ring_lock, iflags);
11316 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11317 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
11318 
11319 		lpfc_sli4_poll_eq(eq);
11320 	} else {
11321 		/* For now, SLI2/3 will still use hbalock */
11322 		spin_lock_irqsave(&phba->hbalock, iflags);
11323 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11324 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11325 	}
11326 	return rc;
11327 }
11328 
11329 /**
11330  * lpfc_extra_ring_setup - Extra ring setup function
11331  * @phba: Pointer to HBA context object.
11332  *
11333  * This function is called while driver attaches with the
11334  * HBA to setup the extra ring. The extra ring is used
11335  * only when driver needs to support target mode functionality
11336  * or IP over FC functionalities.
11337  *
11338  * This function is called with no lock held. SLI3 only.
11339  **/
11340 static int
lpfc_extra_ring_setup(struct lpfc_hba * phba)11341 lpfc_extra_ring_setup( struct lpfc_hba *phba)
11342 {
11343 	struct lpfc_sli *psli;
11344 	struct lpfc_sli_ring *pring;
11345 
11346 	psli = &phba->sli;
11347 
11348 	/* Adjust cmd/rsp ring iocb entries more evenly */
11349 
11350 	/* Take some away from the FCP ring */
11351 	pring = &psli->sli3_ring[LPFC_FCP_RING];
11352 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11353 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11354 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11355 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11356 
11357 	/* and give them to the extra ring */
11358 	pring = &psli->sli3_ring[LPFC_EXTRA_RING];
11359 
11360 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11361 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11362 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11363 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11364 
11365 	/* Setup default profile for this ring */
11366 	pring->iotag_max = 4096;
11367 	pring->num_mask = 1;
11368 	pring->prt[0].profile = 0;      /* Mask 0 */
11369 	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11370 	pring->prt[0].type = phba->cfg_multi_ring_type;
11371 	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11372 	return 0;
11373 }
11374 
11375 static void
lpfc_sli_post_recovery_event(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)11376 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11377 			     struct lpfc_nodelist *ndlp)
11378 {
11379 	unsigned long iflags;
11380 	struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
11381 
11382 	/* Hold a node reference for outstanding queued work */
11383 	if (!lpfc_nlp_get(ndlp))
11384 		return;
11385 
11386 	spin_lock_irqsave(&phba->hbalock, iflags);
11387 	if (!list_empty(&evtp->evt_listp)) {
11388 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11389 		lpfc_nlp_put(ndlp);
11390 		return;
11391 	}
11392 
11393 	evtp->evt_arg1 = ndlp;
11394 	evtp->evt = LPFC_EVT_RECOVER_PORT;
11395 	list_add_tail(&evtp->evt_listp, &phba->work_list);
11396 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11397 
11398 	lpfc_worker_wake_up(phba);
11399 }
11400 
11401 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11402  * @phba: Pointer to HBA context object.
11403  * @iocbq: Pointer to iocb object.
11404  *
11405  * The async_event handler calls this routine when it receives
11406  * an ASYNC_STATUS_CN event from the port.  The port generates
11407  * this event when an Abort Sequence request to an rport fails
11408  * twice in succession.  The abort could be originated by the
11409  * driver or by the port.  The ABTS could have been for an ELS
11410  * or FCP IO.  The port only generates this event when an ABTS
11411  * fails to complete after one retry.
11412  */
11413 static void
lpfc_sli_abts_err_handler(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)11414 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11415 			  struct lpfc_iocbq *iocbq)
11416 {
11417 	struct lpfc_nodelist *ndlp = NULL;
11418 	uint16_t rpi = 0, vpi = 0;
11419 	struct lpfc_vport *vport = NULL;
11420 
11421 	/* The rpi in the ulpContext is vport-sensitive. */
11422 	vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11423 	rpi = iocbq->iocb.ulpContext;
11424 
11425 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11426 			"3092 Port generated ABTS async event "
11427 			"on vpi %d rpi %d status 0x%x\n",
11428 			vpi, rpi, iocbq->iocb.ulpStatus);
11429 
11430 	vport = lpfc_find_vport_by_vpid(phba, vpi);
11431 	if (!vport)
11432 		goto err_exit;
11433 	ndlp = lpfc_findnode_rpi(vport, rpi);
11434 	if (!ndlp)
11435 		goto err_exit;
11436 
11437 	if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11438 		lpfc_sli_abts_recover_port(vport, ndlp);
11439 	return;
11440 
11441  err_exit:
11442 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11443 			"3095 Event Context not found, no "
11444 			"action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11445 			vpi, rpi, iocbq->iocb.ulpStatus,
11446 			iocbq->iocb.ulpContext);
11447 }
11448 
11449 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11450  * @phba: pointer to HBA context object.
11451  * @ndlp: nodelist pointer for the impacted rport.
11452  * @axri: pointer to the wcqe containing the failed exchange.
11453  *
11454  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11455  * port.  The port generates this event when an abort exchange request to an
11456  * rport fails twice in succession with no reply.  The abort could be originated
11457  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
11458  */
11459 void
lpfc_sli4_abts_err_handler(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct sli4_wcqe_xri_aborted * axri)11460 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11461 			   struct lpfc_nodelist *ndlp,
11462 			   struct sli4_wcqe_xri_aborted *axri)
11463 {
11464 	uint32_t ext_status = 0;
11465 
11466 	if (!ndlp) {
11467 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11468 				"3115 Node Context not found, driver "
11469 				"ignoring abts err event\n");
11470 		return;
11471 	}
11472 
11473 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11474 			"3116 Port generated FCP XRI ABORT event on "
11475 			"vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
11476 			ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
11477 			bf_get(lpfc_wcqe_xa_xri, axri),
11478 			bf_get(lpfc_wcqe_xa_status, axri),
11479 			axri->parameter);
11480 
11481 	/*
11482 	 * Catch the ABTS protocol failure case.  Older OCe FW releases returned
11483 	 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11484 	 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11485 	 */
11486 	ext_status = axri->parameter & IOERR_PARAM_MASK;
11487 	if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11488 	    ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
11489 		lpfc_sli_post_recovery_event(phba, ndlp);
11490 }
11491 
11492 /**
11493  * lpfc_sli_async_event_handler - ASYNC iocb handler function
11494  * @phba: Pointer to HBA context object.
11495  * @pring: Pointer to driver SLI ring object.
11496  * @iocbq: Pointer to iocb object.
11497  *
11498  * This function is called by the slow ring event handler
11499  * function when there is an ASYNC event iocb in the ring.
11500  * This function is called with no lock held.
11501  * Currently this function handles only temperature related
11502  * ASYNC events. The function decodes the temperature sensor
11503  * event message and posts events for the management applications.
11504  **/
11505 static void
lpfc_sli_async_event_handler(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * iocbq)11506 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11507 	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11508 {
11509 	IOCB_t *icmd;
11510 	uint16_t evt_code;
11511 	struct temp_event temp_event_data;
11512 	struct Scsi_Host *shost;
11513 	uint32_t *iocb_w;
11514 
11515 	icmd = &iocbq->iocb;
11516 	evt_code = icmd->un.asyncstat.evt_code;
11517 
11518 	switch (evt_code) {
11519 	case ASYNC_TEMP_WARN:
11520 	case ASYNC_TEMP_SAFE:
11521 		temp_event_data.data = (uint32_t) icmd->ulpContext;
11522 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11523 		if (evt_code == ASYNC_TEMP_WARN) {
11524 			temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
11525 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11526 				"0347 Adapter is very hot, please take "
11527 				"corrective action. temperature : %d Celsius\n",
11528 				(uint32_t) icmd->ulpContext);
11529 		} else {
11530 			temp_event_data.event_code = LPFC_NORMAL_TEMP;
11531 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11532 				"0340 Adapter temperature is OK now. "
11533 				"temperature : %d Celsius\n",
11534 				(uint32_t) icmd->ulpContext);
11535 		}
11536 
11537 		/* Send temperature change event to applications */
11538 		shost = lpfc_shost_from_vport(phba->pport);
11539 		fc_host_post_vendor_event(shost, fc_get_event_number(),
11540 			sizeof(temp_event_data), (char *) &temp_event_data,
11541 			LPFC_NL_VENDOR_ID);
11542 		break;
11543 	case ASYNC_STATUS_CN:
11544 		lpfc_sli_abts_err_handler(phba, iocbq);
11545 		break;
11546 	default:
11547 		iocb_w = (uint32_t *) icmd;
11548 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11549 			"0346 Ring %d handler: unexpected ASYNC_STATUS"
11550 			" evt_code 0x%x\n"
11551 			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
11552 			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
11553 			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
11554 			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
11555 			pring->ringno, icmd->un.asyncstat.evt_code,
11556 			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11557 			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11558 			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11559 			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11560 
11561 		break;
11562 	}
11563 }
11564 
11565 
11566 /**
11567  * lpfc_sli4_setup - SLI ring setup function
11568  * @phba: Pointer to HBA context object.
11569  *
11570  * lpfc_sli_setup sets up rings of the SLI interface with
11571  * number of iocbs per ring and iotags. This function is
11572  * called while driver attach to the HBA and before the
11573  * interrupts are enabled. So there is no need for locking.
11574  *
11575  * This function always returns 0.
11576  **/
11577 int
lpfc_sli4_setup(struct lpfc_hba * phba)11578 lpfc_sli4_setup(struct lpfc_hba *phba)
11579 {
11580 	struct lpfc_sli_ring *pring;
11581 
11582 	pring = phba->sli4_hba.els_wq->pring;
11583 	pring->num_mask = LPFC_MAX_RING_MASK;
11584 	pring->prt[0].profile = 0;	/* Mask 0 */
11585 	pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11586 	pring->prt[0].type = FC_TYPE_ELS;
11587 	pring->prt[0].lpfc_sli_rcv_unsol_event =
11588 	    lpfc_els_unsol_event;
11589 	pring->prt[1].profile = 0;	/* Mask 1 */
11590 	pring->prt[1].rctl = FC_RCTL_ELS_REP;
11591 	pring->prt[1].type = FC_TYPE_ELS;
11592 	pring->prt[1].lpfc_sli_rcv_unsol_event =
11593 	    lpfc_els_unsol_event;
11594 	pring->prt[2].profile = 0;	/* Mask 2 */
11595 	/* NameServer Inquiry */
11596 	pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11597 	/* NameServer */
11598 	pring->prt[2].type = FC_TYPE_CT;
11599 	pring->prt[2].lpfc_sli_rcv_unsol_event =
11600 	    lpfc_ct_unsol_event;
11601 	pring->prt[3].profile = 0;	/* Mask 3 */
11602 	/* NameServer response */
11603 	pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11604 	/* NameServer */
11605 	pring->prt[3].type = FC_TYPE_CT;
11606 	pring->prt[3].lpfc_sli_rcv_unsol_event =
11607 	    lpfc_ct_unsol_event;
11608 	return 0;
11609 }
11610 
11611 /**
11612  * lpfc_sli_setup - SLI ring setup function
11613  * @phba: Pointer to HBA context object.
11614  *
11615  * lpfc_sli_setup sets up rings of the SLI interface with
11616  * number of iocbs per ring and iotags. This function is
11617  * called while driver attach to the HBA and before the
11618  * interrupts are enabled. So there is no need for locking.
11619  *
11620  * This function always returns 0. SLI3 only.
11621  **/
11622 int
lpfc_sli_setup(struct lpfc_hba * phba)11623 lpfc_sli_setup(struct lpfc_hba *phba)
11624 {
11625 	int i, totiocbsize = 0;
11626 	struct lpfc_sli *psli = &phba->sli;
11627 	struct lpfc_sli_ring *pring;
11628 
11629 	psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
11630 	psli->sli_flag = 0;
11631 
11632 	psli->iocbq_lookup = NULL;
11633 	psli->iocbq_lookup_len = 0;
11634 	psli->last_iotag = 0;
11635 
11636 	for (i = 0; i < psli->num_rings; i++) {
11637 		pring = &psli->sli3_ring[i];
11638 		switch (i) {
11639 		case LPFC_FCP_RING:	/* ring 0 - FCP */
11640 			/* numCiocb and numRiocb are used in config_port */
11641 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11642 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11643 			pring->sli.sli3.numCiocb +=
11644 				SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11645 			pring->sli.sli3.numRiocb +=
11646 				SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11647 			pring->sli.sli3.numCiocb +=
11648 				SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11649 			pring->sli.sli3.numRiocb +=
11650 				SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11651 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11652 							SLI3_IOCB_CMD_SIZE :
11653 							SLI2_IOCB_CMD_SIZE;
11654 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11655 							SLI3_IOCB_RSP_SIZE :
11656 							SLI2_IOCB_RSP_SIZE;
11657 			pring->iotag_ctr = 0;
11658 			pring->iotag_max =
11659 			    (phba->cfg_hba_queue_depth * 2);
11660 			pring->fast_iotag = pring->iotag_max;
11661 			pring->num_mask = 0;
11662 			break;
11663 		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
11664 			/* numCiocb and numRiocb are used in config_port */
11665 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11666 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11667 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11668 							SLI3_IOCB_CMD_SIZE :
11669 							SLI2_IOCB_CMD_SIZE;
11670 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11671 							SLI3_IOCB_RSP_SIZE :
11672 							SLI2_IOCB_RSP_SIZE;
11673 			pring->iotag_max = phba->cfg_hba_queue_depth;
11674 			pring->num_mask = 0;
11675 			break;
11676 		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
11677 			/* numCiocb and numRiocb are used in config_port */
11678 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11679 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11680 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11681 							SLI3_IOCB_CMD_SIZE :
11682 							SLI2_IOCB_CMD_SIZE;
11683 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11684 							SLI3_IOCB_RSP_SIZE :
11685 							SLI2_IOCB_RSP_SIZE;
11686 			pring->fast_iotag = 0;
11687 			pring->iotag_ctr = 0;
11688 			pring->iotag_max = 4096;
11689 			pring->lpfc_sli_rcv_async_status =
11690 				lpfc_sli_async_event_handler;
11691 			pring->num_mask = LPFC_MAX_RING_MASK;
11692 			pring->prt[0].profile = 0;	/* Mask 0 */
11693 			pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11694 			pring->prt[0].type = FC_TYPE_ELS;
11695 			pring->prt[0].lpfc_sli_rcv_unsol_event =
11696 			    lpfc_els_unsol_event;
11697 			pring->prt[1].profile = 0;	/* Mask 1 */
11698 			pring->prt[1].rctl = FC_RCTL_ELS_REP;
11699 			pring->prt[1].type = FC_TYPE_ELS;
11700 			pring->prt[1].lpfc_sli_rcv_unsol_event =
11701 			    lpfc_els_unsol_event;
11702 			pring->prt[2].profile = 0;	/* Mask 2 */
11703 			/* NameServer Inquiry */
11704 			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11705 			/* NameServer */
11706 			pring->prt[2].type = FC_TYPE_CT;
11707 			pring->prt[2].lpfc_sli_rcv_unsol_event =
11708 			    lpfc_ct_unsol_event;
11709 			pring->prt[3].profile = 0;	/* Mask 3 */
11710 			/* NameServer response */
11711 			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11712 			/* NameServer */
11713 			pring->prt[3].type = FC_TYPE_CT;
11714 			pring->prt[3].lpfc_sli_rcv_unsol_event =
11715 			    lpfc_ct_unsol_event;
11716 			break;
11717 		}
11718 		totiocbsize += (pring->sli.sli3.numCiocb *
11719 			pring->sli.sli3.sizeCiocb) +
11720 			(pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
11721 	}
11722 	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
11723 		/* Too many cmd / rsp ring entries in SLI2 SLIM */
11724 		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11725 		       "SLI2 SLIM Data: x%x x%lx\n",
11726 		       phba->brd_no, totiocbsize,
11727 		       (unsigned long) MAX_SLIM_IOCB_SIZE);
11728 	}
11729 	if (phba->cfg_multi_ring_support == 2)
11730 		lpfc_extra_ring_setup(phba);
11731 
11732 	return 0;
11733 }
11734 
11735 /**
11736  * lpfc_sli4_queue_init - Queue initialization function
11737  * @phba: Pointer to HBA context object.
11738  *
11739  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
11740  * ring. This function also initializes ring indices of each ring.
11741  * This function is called during the initialization of the SLI
11742  * interface of an HBA.
11743  * This function is called with no lock held and always returns
11744  * 1.
11745  **/
11746 void
lpfc_sli4_queue_init(struct lpfc_hba * phba)11747 lpfc_sli4_queue_init(struct lpfc_hba *phba)
11748 {
11749 	struct lpfc_sli *psli;
11750 	struct lpfc_sli_ring *pring;
11751 	int i;
11752 
11753 	psli = &phba->sli;
11754 	spin_lock_irq(&phba->hbalock);
11755 	INIT_LIST_HEAD(&psli->mboxq);
11756 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11757 	/* Initialize list headers for txq and txcmplq as double linked lists */
11758 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
11759 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11760 		pring->flag = 0;
11761 		pring->ringno = LPFC_FCP_RING;
11762 		pring->txcmplq_cnt = 0;
11763 		INIT_LIST_HEAD(&pring->txq);
11764 		INIT_LIST_HEAD(&pring->txcmplq);
11765 		INIT_LIST_HEAD(&pring->iocb_continueq);
11766 		spin_lock_init(&pring->ring_lock);
11767 	}
11768 	pring = phba->sli4_hba.els_wq->pring;
11769 	pring->flag = 0;
11770 	pring->ringno = LPFC_ELS_RING;
11771 	pring->txcmplq_cnt = 0;
11772 	INIT_LIST_HEAD(&pring->txq);
11773 	INIT_LIST_HEAD(&pring->txcmplq);
11774 	INIT_LIST_HEAD(&pring->iocb_continueq);
11775 	spin_lock_init(&pring->ring_lock);
11776 
11777 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11778 		pring = phba->sli4_hba.nvmels_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 
11788 	spin_unlock_irq(&phba->hbalock);
11789 }
11790 
11791 /**
11792  * lpfc_sli_queue_init - Queue initialization function
11793  * @phba: Pointer to HBA context object.
11794  *
11795  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11796  * ring. This function also initializes ring indices of each ring.
11797  * This function is called during the initialization of the SLI
11798  * interface of an HBA.
11799  * This function is called with no lock held and always returns
11800  * 1.
11801  **/
11802 void
lpfc_sli_queue_init(struct lpfc_hba * phba)11803 lpfc_sli_queue_init(struct lpfc_hba *phba)
11804 {
11805 	struct lpfc_sli *psli;
11806 	struct lpfc_sli_ring *pring;
11807 	int i;
11808 
11809 	psli = &phba->sli;
11810 	spin_lock_irq(&phba->hbalock);
11811 	INIT_LIST_HEAD(&psli->mboxq);
11812 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11813 	/* Initialize list headers for txq and txcmplq as double linked lists */
11814 	for (i = 0; i < psli->num_rings; i++) {
11815 		pring = &psli->sli3_ring[i];
11816 		pring->ringno = i;
11817 		pring->sli.sli3.next_cmdidx  = 0;
11818 		pring->sli.sli3.local_getidx = 0;
11819 		pring->sli.sli3.cmdidx = 0;
11820 		INIT_LIST_HEAD(&pring->iocb_continueq);
11821 		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11822 		INIT_LIST_HEAD(&pring->postbufq);
11823 		pring->flag = 0;
11824 		INIT_LIST_HEAD(&pring->txq);
11825 		INIT_LIST_HEAD(&pring->txcmplq);
11826 		spin_lock_init(&pring->ring_lock);
11827 	}
11828 	spin_unlock_irq(&phba->hbalock);
11829 }
11830 
11831 /**
11832  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11833  * @phba: Pointer to HBA context object.
11834  *
11835  * This routine flushes the mailbox command subsystem. It will unconditionally
11836  * flush all the mailbox commands in the three possible stages in the mailbox
11837  * command sub-system: pending mailbox command queue; the outstanding mailbox
11838  * command; and completed mailbox command queue. It is caller's responsibility
11839  * to make sure that the driver is in the proper state to flush the mailbox
11840  * command sub-system. Namely, the posting of mailbox commands into the
11841  * pending mailbox command queue from the various clients must be stopped;
11842  * either the HBA is in a state that it will never works on the outstanding
11843  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11844  * mailbox command has been completed.
11845  **/
11846 static void
lpfc_sli_mbox_sys_flush(struct lpfc_hba * phba)11847 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11848 {
11849 	LIST_HEAD(completions);
11850 	struct lpfc_sli *psli = &phba->sli;
11851 	LPFC_MBOXQ_t *pmb;
11852 	unsigned long iflag;
11853 
11854 	/* Disable softirqs, including timers from obtaining phba->hbalock */
11855 	local_bh_disable();
11856 
11857 	/* Flush all the mailbox commands in the mbox system */
11858 	spin_lock_irqsave(&phba->hbalock, iflag);
11859 
11860 	/* The pending mailbox command queue */
11861 	list_splice_init(&phba->sli.mboxq, &completions);
11862 	/* The outstanding active mailbox command */
11863 	if (psli->mbox_active) {
11864 		list_add_tail(&psli->mbox_active->list, &completions);
11865 		psli->mbox_active = NULL;
11866 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11867 	}
11868 	/* The completed mailbox command queue */
11869 	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11870 	spin_unlock_irqrestore(&phba->hbalock, iflag);
11871 
11872 	/* Enable softirqs again, done with phba->hbalock */
11873 	local_bh_enable();
11874 
11875 	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11876 	while (!list_empty(&completions)) {
11877 		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11878 		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11879 		if (pmb->mbox_cmpl)
11880 			pmb->mbox_cmpl(phba, pmb);
11881 	}
11882 }
11883 
11884 /**
11885  * lpfc_sli_host_down - Vport cleanup function
11886  * @vport: Pointer to virtual port object.
11887  *
11888  * lpfc_sli_host_down is called to clean up the resources
11889  * associated with a vport before destroying virtual
11890  * port data structures.
11891  * This function does following operations:
11892  * - Free discovery resources associated with this virtual
11893  *   port.
11894  * - Free iocbs associated with this virtual port in
11895  *   the txq.
11896  * - Send abort for all iocb commands associated with this
11897  *   vport in txcmplq.
11898  *
11899  * This function is called with no lock held and always returns 1.
11900  **/
11901 int
lpfc_sli_host_down(struct lpfc_vport * vport)11902 lpfc_sli_host_down(struct lpfc_vport *vport)
11903 {
11904 	LIST_HEAD(completions);
11905 	struct lpfc_hba *phba = vport->phba;
11906 	struct lpfc_sli *psli = &phba->sli;
11907 	struct lpfc_queue *qp = NULL;
11908 	struct lpfc_sli_ring *pring;
11909 	struct lpfc_iocbq *iocb, *next_iocb;
11910 	int i;
11911 	unsigned long flags = 0;
11912 	uint16_t prev_pring_flag;
11913 
11914 	lpfc_cleanup_discovery_resources(vport);
11915 
11916 	spin_lock_irqsave(&phba->hbalock, flags);
11917 
11918 	/*
11919 	 * Error everything on the txq since these iocbs
11920 	 * have not been given to the FW yet.
11921 	 * Also issue ABTS for everything on the txcmplq
11922 	 */
11923 	if (phba->sli_rev != LPFC_SLI_REV4) {
11924 		for (i = 0; i < psli->num_rings; i++) {
11925 			pring = &psli->sli3_ring[i];
11926 			prev_pring_flag = pring->flag;
11927 			/* Only slow rings */
11928 			if (pring->ringno == LPFC_ELS_RING) {
11929 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11930 				/* Set the lpfc data pending flag */
11931 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11932 			}
11933 			list_for_each_entry_safe(iocb, next_iocb,
11934 						 &pring->txq, list) {
11935 				if (iocb->vport != vport)
11936 					continue;
11937 				list_move_tail(&iocb->list, &completions);
11938 			}
11939 			list_for_each_entry_safe(iocb, next_iocb,
11940 						 &pring->txcmplq, list) {
11941 				if (iocb->vport != vport)
11942 					continue;
11943 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11944 							   NULL);
11945 			}
11946 			pring->flag = prev_pring_flag;
11947 		}
11948 	} else {
11949 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11950 			pring = qp->pring;
11951 			if (!pring)
11952 				continue;
11953 			if (pring == phba->sli4_hba.els_wq->pring) {
11954 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11955 				/* Set the lpfc data pending flag */
11956 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11957 			}
11958 			prev_pring_flag = pring->flag;
11959 			spin_lock(&pring->ring_lock);
11960 			list_for_each_entry_safe(iocb, next_iocb,
11961 						 &pring->txq, list) {
11962 				if (iocb->vport != vport)
11963 					continue;
11964 				list_move_tail(&iocb->list, &completions);
11965 			}
11966 			spin_unlock(&pring->ring_lock);
11967 			list_for_each_entry_safe(iocb, next_iocb,
11968 						 &pring->txcmplq, list) {
11969 				if (iocb->vport != vport)
11970 					continue;
11971 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11972 							   NULL);
11973 			}
11974 			pring->flag = prev_pring_flag;
11975 		}
11976 	}
11977 	spin_unlock_irqrestore(&phba->hbalock, flags);
11978 
11979 	/* Make sure HBA is alive */
11980 	lpfc_issue_hb_tmo(phba);
11981 
11982 	/* Cancel all the IOCBs from the completions list */
11983 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11984 			      IOERR_SLI_DOWN);
11985 	return 1;
11986 }
11987 
11988 /**
11989  * lpfc_sli_hba_down - Resource cleanup function for the HBA
11990  * @phba: Pointer to HBA context object.
11991  *
11992  * This function cleans up all iocb, buffers, mailbox commands
11993  * while shutting down the HBA. This function is called with no
11994  * lock held and always returns 1.
11995  * This function does the following to cleanup driver resources:
11996  * - Free discovery resources for each virtual port
11997  * - Cleanup any pending fabric iocbs
11998  * - Iterate through the iocb txq and free each entry
11999  *   in the list.
12000  * - Free up any buffer posted to the HBA
12001  * - Free mailbox commands in the mailbox queue.
12002  **/
12003 int
lpfc_sli_hba_down(struct lpfc_hba * phba)12004 lpfc_sli_hba_down(struct lpfc_hba *phba)
12005 {
12006 	LIST_HEAD(completions);
12007 	struct lpfc_sli *psli = &phba->sli;
12008 	struct lpfc_queue *qp = NULL;
12009 	struct lpfc_sli_ring *pring;
12010 	struct lpfc_dmabuf *buf_ptr;
12011 	unsigned long flags = 0;
12012 	int i;
12013 
12014 	/* Shutdown the mailbox command sub-system */
12015 	lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
12016 
12017 	lpfc_hba_down_prep(phba);
12018 
12019 	/* Disable softirqs, including timers from obtaining phba->hbalock */
12020 	local_bh_disable();
12021 
12022 	lpfc_fabric_abort_hba(phba);
12023 
12024 	spin_lock_irqsave(&phba->hbalock, flags);
12025 
12026 	/*
12027 	 * Error everything on the txq since these iocbs
12028 	 * have not been given to the FW yet.
12029 	 */
12030 	if (phba->sli_rev != LPFC_SLI_REV4) {
12031 		for (i = 0; i < psli->num_rings; i++) {
12032 			pring = &psli->sli3_ring[i];
12033 			/* Only slow rings */
12034 			if (pring->ringno == LPFC_ELS_RING) {
12035 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12036 				/* Set the lpfc data pending flag */
12037 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12038 			}
12039 			list_splice_init(&pring->txq, &completions);
12040 		}
12041 	} else {
12042 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12043 			pring = qp->pring;
12044 			if (!pring)
12045 				continue;
12046 			spin_lock(&pring->ring_lock);
12047 			list_splice_init(&pring->txq, &completions);
12048 			spin_unlock(&pring->ring_lock);
12049 			if (pring == phba->sli4_hba.els_wq->pring) {
12050 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12051 				/* Set the lpfc data pending flag */
12052 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12053 			}
12054 		}
12055 	}
12056 	spin_unlock_irqrestore(&phba->hbalock, flags);
12057 
12058 	/* Cancel all the IOCBs from the completions list */
12059 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12060 			      IOERR_SLI_DOWN);
12061 
12062 	spin_lock_irqsave(&phba->hbalock, flags);
12063 	list_splice_init(&phba->elsbuf, &completions);
12064 	phba->elsbuf_cnt = 0;
12065 	phba->elsbuf_prev_cnt = 0;
12066 	spin_unlock_irqrestore(&phba->hbalock, flags);
12067 
12068 	while (!list_empty(&completions)) {
12069 		list_remove_head(&completions, buf_ptr,
12070 			struct lpfc_dmabuf, list);
12071 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12072 		kfree(buf_ptr);
12073 	}
12074 
12075 	/* Enable softirqs again, done with phba->hbalock */
12076 	local_bh_enable();
12077 
12078 	/* Return any active mbox cmds */
12079 	timer_delete_sync(&psli->mbox_tmo);
12080 
12081 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
12082 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12083 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
12084 
12085 	return 1;
12086 }
12087 
12088 /**
12089  * lpfc_sli_pcimem_bcopy - SLI memory copy function
12090  * @srcp: Source memory pointer.
12091  * @destp: Destination memory pointer.
12092  * @cnt: Number of words required to be copied.
12093  *
12094  * This function is used for copying data between driver memory
12095  * and the SLI memory. This function also changes the endianness
12096  * of each word if native endianness is different from SLI
12097  * endianness. This function can be called with or without
12098  * lock.
12099  **/
12100 void
lpfc_sli_pcimem_bcopy(void * srcp,void * destp,uint32_t cnt)12101 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12102 {
12103 	uint32_t *src = srcp;
12104 	uint32_t *dest = destp;
12105 	uint32_t ldata;
12106 	int i;
12107 
12108 	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12109 		ldata = *src;
12110 		ldata = le32_to_cpu(ldata);
12111 		*dest = ldata;
12112 		src++;
12113 		dest++;
12114 	}
12115 }
12116 
12117 
12118 /**
12119  * lpfc_sli_bemem_bcopy - SLI memory copy function
12120  * @srcp: Source memory pointer.
12121  * @destp: Destination memory pointer.
12122  * @cnt: Number of words required to be copied.
12123  *
12124  * This function is used for copying data between a data structure
12125  * with big endian representation to local endianness.
12126  * This function can be called with or without lock.
12127  **/
12128 void
lpfc_sli_bemem_bcopy(void * srcp,void * destp,uint32_t cnt)12129 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12130 {
12131 	uint32_t *src = srcp;
12132 	uint32_t *dest = destp;
12133 	uint32_t ldata;
12134 	int i;
12135 
12136 	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12137 		ldata = *src;
12138 		ldata = be32_to_cpu(ldata);
12139 		*dest = ldata;
12140 		src++;
12141 		dest++;
12142 	}
12143 }
12144 
12145 /**
12146  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
12147  * @phba: Pointer to HBA context object.
12148  * @pring: Pointer to driver SLI ring object.
12149  * @mp: Pointer to driver buffer object.
12150  *
12151  * This function is called with no lock held.
12152  * It always return zero after adding the buffer to the postbufq
12153  * buffer list.
12154  **/
12155 int
lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_dmabuf * mp)12156 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12157 			 struct lpfc_dmabuf *mp)
12158 {
12159 	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12160 	   later */
12161 	spin_lock_irq(&phba->hbalock);
12162 	list_add_tail(&mp->list, &pring->postbufq);
12163 	pring->postbufq_cnt++;
12164 	spin_unlock_irq(&phba->hbalock);
12165 	return 0;
12166 }
12167 
12168 /**
12169  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
12170  * @phba: Pointer to HBA context object.
12171  *
12172  * When HBQ is enabled, buffers are searched based on tags. This function
12173  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12174  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12175  * does not conflict with tags of buffer posted for unsolicited events.
12176  * The function returns the allocated tag. The function is called with
12177  * no locks held.
12178  **/
12179 uint32_t
lpfc_sli_get_buffer_tag(struct lpfc_hba * phba)12180 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12181 {
12182 	spin_lock_irq(&phba->hbalock);
12183 	phba->buffer_tag_count++;
12184 	/*
12185 	 * Always set the QUE_BUFTAG_BIT to distiguish between
12186 	 * a tag assigned by HBQ.
12187 	 */
12188 	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12189 	spin_unlock_irq(&phba->hbalock);
12190 	return phba->buffer_tag_count;
12191 }
12192 
12193 /**
12194  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
12195  * @phba: Pointer to HBA context object.
12196  * @pring: Pointer to driver SLI ring object.
12197  * @tag: Buffer tag.
12198  *
12199  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12200  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12201  * iocb is posted to the response ring with the tag of the buffer.
12202  * This function searches the pring->postbufq list using the tag
12203  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12204  * iocb. If the buffer is found then lpfc_dmabuf object of the
12205  * buffer is returned to the caller else NULL is returned.
12206  * This function is called with no lock held.
12207  **/
12208 struct lpfc_dmabuf *
lpfc_sli_ring_taggedbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)12209 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12210 			uint32_t tag)
12211 {
12212 	struct lpfc_dmabuf *mp, *next_mp;
12213 	struct list_head *slp = &pring->postbufq;
12214 
12215 	/* Search postbufq, from the beginning, looking for a match on tag */
12216 	spin_lock_irq(&phba->hbalock);
12217 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12218 		if (mp->buffer_tag == tag) {
12219 			list_del_init(&mp->list);
12220 			pring->postbufq_cnt--;
12221 			spin_unlock_irq(&phba->hbalock);
12222 			return mp;
12223 		}
12224 	}
12225 
12226 	spin_unlock_irq(&phba->hbalock);
12227 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12228 			"0402 Cannot find virtual addr for buffer tag on "
12229 			"ring %d Data x%lx x%px x%px x%x\n",
12230 			pring->ringno, (unsigned long) tag,
12231 			slp->next, slp->prev, pring->postbufq_cnt);
12232 
12233 	return NULL;
12234 }
12235 
12236 /**
12237  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
12238  * @phba: Pointer to HBA context object.
12239  * @pring: Pointer to driver SLI ring object.
12240  * @phys: DMA address of the buffer.
12241  *
12242  * This function searches the buffer list using the dma_address
12243  * of unsolicited event to find the driver's lpfc_dmabuf object
12244  * corresponding to the dma_address. The function returns the
12245  * lpfc_dmabuf object if a buffer is found else it returns NULL.
12246  * This function is called by the ct and els unsolicited event
12247  * handlers to get the buffer associated with the unsolicited
12248  * event.
12249  *
12250  * This function is called with no lock held.
12251  **/
12252 struct lpfc_dmabuf *
lpfc_sli_ringpostbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,dma_addr_t phys)12253 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12254 			 dma_addr_t phys)
12255 {
12256 	struct lpfc_dmabuf *mp, *next_mp;
12257 	struct list_head *slp = &pring->postbufq;
12258 
12259 	/* Search postbufq, from the beginning, looking for a match on phys */
12260 	spin_lock_irq(&phba->hbalock);
12261 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12262 		if (mp->phys == phys) {
12263 			list_del_init(&mp->list);
12264 			pring->postbufq_cnt--;
12265 			spin_unlock_irq(&phba->hbalock);
12266 			return mp;
12267 		}
12268 	}
12269 
12270 	spin_unlock_irq(&phba->hbalock);
12271 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12272 			"0410 Cannot find virtual addr for mapped buf on "
12273 			"ring %d Data x%llx x%px x%px x%x\n",
12274 			pring->ringno, (unsigned long long)phys,
12275 			slp->next, slp->prev, pring->postbufq_cnt);
12276 	return NULL;
12277 }
12278 
12279 /**
12280  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
12281  * @phba: Pointer to HBA context object.
12282  * @cmdiocb: Pointer to driver command iocb object.
12283  * @rspiocb: Pointer to driver response iocb object.
12284  *
12285  * This function is the completion handler for the abort iocbs for
12286  * ELS commands. This function is called from the ELS ring event
12287  * handler with no lock held. This function frees memory resources
12288  * associated with the abort iocb.
12289  **/
12290 static void
lpfc_sli_abort_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12291 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12292 			struct lpfc_iocbq *rspiocb)
12293 {
12294 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12295 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12296 	u8 cmnd = get_job_cmnd(phba, cmdiocb);
12297 
12298 	if (ulp_status) {
12299 		/*
12300 		 * Assume that the port already completed and returned, or
12301 		 * will return the iocb. Just Log the message.
12302 		 */
12303 		if (phba->sli_rev < LPFC_SLI_REV4) {
12304 			if (cmnd == CMD_ABORT_XRI_CX &&
12305 			    ulp_status == IOSTAT_LOCAL_REJECT &&
12306 			    ulp_word4 == IOERR_ABORT_REQUESTED) {
12307 				goto release_iocb;
12308 			}
12309 		}
12310 	}
12311 
12312 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
12313 			"0327 Abort els iocb complete x%px with io cmd xri %x "
12314 			"abort tag x%x abort status %x abort code %x\n",
12315 			cmdiocb, get_job_abtsiotag(phba, cmdiocb),
12316 			(phba->sli_rev == LPFC_SLI_REV4) ?
12317 			get_wqe_reqtag(cmdiocb) :
12318 			cmdiocb->iocb.ulpIoTag,
12319 			ulp_status, ulp_word4);
12320 release_iocb:
12321 	lpfc_sli_release_iocbq(phba, cmdiocb);
12322 	return;
12323 }
12324 
12325 /**
12326  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
12327  * @phba: Pointer to HBA context object.
12328  * @cmdiocb: Pointer to driver command iocb object.
12329  * @rspiocb: Pointer to driver response iocb object.
12330  *
12331  * The function is called from SLI ring event handler with no
12332  * lock held. This function is the completion handler for ELS commands
12333  * which are aborted. The function frees memory resources used for
12334  * the aborted ELS commands.
12335  **/
12336 void
lpfc_ignore_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12337 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12338 		     struct lpfc_iocbq *rspiocb)
12339 {
12340 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12341 	IOCB_t *irsp;
12342 	LPFC_MBOXQ_t *mbox;
12343 	u32 ulp_command, ulp_status, ulp_word4, iotag;
12344 
12345 	ulp_command = get_job_cmnd(phba, cmdiocb);
12346 	ulp_status = get_job_ulpstatus(phba, rspiocb);
12347 	ulp_word4 = get_job_word4(phba, rspiocb);
12348 
12349 	if (phba->sli_rev == LPFC_SLI_REV4) {
12350 		iotag = get_wqe_reqtag(cmdiocb);
12351 	} else {
12352 		irsp = &rspiocb->iocb;
12353 		iotag = irsp->ulpIoTag;
12354 
12355 		/* It is possible a PLOGI_RJT for NPIV ports to get aborted.
12356 		 * The MBX_REG_LOGIN64 mbox command is freed back to the
12357 		 * mbox_mem_pool here.
12358 		 */
12359 		if (cmdiocb->context_un.mbox) {
12360 			mbox = cmdiocb->context_un.mbox;
12361 			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
12362 			cmdiocb->context_un.mbox = NULL;
12363 		}
12364 	}
12365 
12366 	/* ELS cmd tag <ulpIoTag> completes */
12367 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12368 			"0139 Ignoring ELS cmd code x%x ref cnt x%x Data: "
12369 			"x%x x%x x%x x%px\n",
12370 			ulp_command, kref_read(&cmdiocb->ndlp->kref),
12371 			ulp_status, ulp_word4, iotag, cmdiocb->ndlp);
12372 	/*
12373 	 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12374 	 * if exchange is busy.
12375 	 */
12376 	if (ulp_command == CMD_GEN_REQUEST64_CR)
12377 		lpfc_ct_free_iocb(phba, cmdiocb);
12378 	else
12379 		lpfc_els_free_iocb(phba, cmdiocb);
12380 
12381 	lpfc_nlp_put(ndlp);
12382 }
12383 
12384 /**
12385  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
12386  * @phba: Pointer to HBA context object.
12387  * @pring: Pointer to driver SLI ring object.
12388  * @cmdiocb: Pointer to driver command iocb object.
12389  * @cmpl: completion function.
12390  *
12391  * This function issues an abort iocb for the provided command iocb. In case
12392  * of unloading, the abort iocb will not be issued to commands on the ELS
12393  * ring. Instead, the callback function shall be changed to those commands
12394  * so that nothing happens when them finishes. This function is called with
12395  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12396  * when the command iocb is an abort request.
12397  *
12398  **/
12399 int
lpfc_sli_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * cmdiocb,void * cmpl)12400 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12401 			   struct lpfc_iocbq *cmdiocb, void *cmpl)
12402 {
12403 	struct lpfc_vport *vport = cmdiocb->vport;
12404 	struct lpfc_iocbq *abtsiocbp;
12405 	int retval = IOCB_ERROR;
12406 	unsigned long iflags;
12407 	struct lpfc_nodelist *ndlp = NULL;
12408 	u32 ulp_command = get_job_cmnd(phba, cmdiocb);
12409 	u16 ulp_context, iotag;
12410 	bool ia;
12411 
12412 	/*
12413 	 * There are certain command types we don't want to abort.  And we
12414 	 * don't want to abort commands that are already in the process of
12415 	 * being aborted.
12416 	 */
12417 	if (ulp_command == CMD_ABORT_XRI_WQE ||
12418 	    ulp_command == CMD_ABORT_XRI_CN ||
12419 	    ulp_command == CMD_CLOSE_XRI_CN ||
12420 	    cmdiocb->cmd_flag & LPFC_DRIVER_ABORTED)
12421 		return IOCB_ABORTING;
12422 
12423 	if (!pring) {
12424 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12425 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12426 		else
12427 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12428 		return retval;
12429 	}
12430 
12431 	/*
12432 	 * If we're unloading, don't abort iocb on the ELS ring, but change
12433 	 * the callback so that nothing happens when it finishes.
12434 	 */
12435 	if (test_bit(FC_UNLOADING, &vport->load_flag) &&
12436 	    pring->ringno == LPFC_ELS_RING) {
12437 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12438 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12439 		else
12440 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12441 		return retval;
12442 	}
12443 
12444 	/* issue ABTS for this IOCB based on iotag */
12445 	abtsiocbp = __lpfc_sli_get_iocbq(phba);
12446 	if (abtsiocbp == NULL)
12447 		return IOCB_NORESOURCE;
12448 
12449 	/* This signals the response to set the correct status
12450 	 * before calling the completion handler
12451 	 */
12452 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
12453 
12454 	if (phba->sli_rev == LPFC_SLI_REV4) {
12455 		ulp_context = cmdiocb->sli4_xritag;
12456 		iotag = abtsiocbp->iotag;
12457 	} else {
12458 		iotag = cmdiocb->iocb.ulpIoTag;
12459 		if (pring->ringno == LPFC_ELS_RING) {
12460 			ndlp = cmdiocb->ndlp;
12461 			ulp_context = ndlp->nlp_rpi;
12462 		} else {
12463 			ulp_context = cmdiocb->iocb.ulpContext;
12464 		}
12465 	}
12466 
12467 	/* Just close the exchange under certain conditions. */
12468 	if (test_bit(FC_UNLOADING, &vport->load_flag) ||
12469 	    phba->link_state < LPFC_LINK_UP ||
12470 	    (phba->sli_rev == LPFC_SLI_REV4 &&
12471 	     phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN) ||
12472 	    (phba->link_flag & LS_EXTERNAL_LOOPBACK))
12473 		ia = true;
12474 	else
12475 		ia = false;
12476 
12477 	lpfc_sli_prep_abort_xri(phba, abtsiocbp, ulp_context, iotag,
12478 				cmdiocb->iocb.ulpClass,
12479 				LPFC_WQE_CQ_ID_DEFAULT, ia, false);
12480 
12481 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12482 	abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
12483 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
12484 		abtsiocbp->cmd_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);
12485 
12486 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
12487 		abtsiocbp->cmd_flag |= LPFC_IO_FOF;
12488 
12489 	if (cmpl)
12490 		abtsiocbp->cmd_cmpl = cmpl;
12491 	else
12492 		abtsiocbp->cmd_cmpl = lpfc_sli_abort_els_cmpl;
12493 	abtsiocbp->vport = vport;
12494 
12495 	if (phba->sli_rev == LPFC_SLI_REV4) {
12496 		pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12497 		if (unlikely(pring == NULL))
12498 			goto abort_iotag_exit;
12499 		/* Note: both hbalock and ring_lock need to be set here */
12500 		spin_lock_irqsave(&pring->ring_lock, iflags);
12501 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12502 			abtsiocbp, 0);
12503 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
12504 	} else {
12505 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12506 			abtsiocbp, 0);
12507 	}
12508 
12509 abort_iotag_exit:
12510 
12511 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12512 			 "0339 Abort IO XRI x%x, Original iotag x%x, "
12513 			 "abort tag x%x Cmdjob : x%px Abortjob : x%px "
12514 			 "retval x%x : IA %d cmd_cmpl %ps\n",
12515 			 ulp_context, (phba->sli_rev == LPFC_SLI_REV4) ?
12516 			 cmdiocb->iotag : iotag, iotag, cmdiocb, abtsiocbp,
12517 			 retval, ia, abtsiocbp->cmd_cmpl);
12518 	if (retval) {
12519 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
12520 		__lpfc_sli_release_iocbq(phba, abtsiocbp);
12521 	}
12522 
12523 	/*
12524 	 * Caller to this routine should check for IOCB_ERROR
12525 	 * and handle it properly.  This routine no longer removes
12526 	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
12527 	 */
12528 	return retval;
12529 }
12530 
12531 /**
12532  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12533  * @phba: pointer to lpfc HBA data structure.
12534  *
12535  * This routine will abort all pending and outstanding iocbs to an HBA.
12536  **/
12537 void
lpfc_sli_hba_iocb_abort(struct lpfc_hba * phba)12538 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12539 {
12540 	struct lpfc_sli *psli = &phba->sli;
12541 	struct lpfc_sli_ring *pring;
12542 	struct lpfc_queue *qp = NULL;
12543 	int i;
12544 
12545 	if (phba->sli_rev != LPFC_SLI_REV4) {
12546 		for (i = 0; i < psli->num_rings; i++) {
12547 			pring = &psli->sli3_ring[i];
12548 			lpfc_sli_abort_iocb_ring(phba, pring);
12549 		}
12550 		return;
12551 	}
12552 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12553 		pring = qp->pring;
12554 		if (!pring)
12555 			continue;
12556 		lpfc_sli_abort_iocb_ring(phba, pring);
12557 	}
12558 }
12559 
12560 /**
12561  * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts
12562  * @iocbq: Pointer to iocb object.
12563  * @vport: Pointer to driver virtual port object.
12564  *
12565  * This function acts as an iocb filter for functions which abort FCP iocbs.
12566  *
12567  * Return values
12568  * -ENODEV, if a null iocb or vport ptr is encountered
12569  * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as
12570  *          driver already started the abort process, or is an abort iocb itself
12571  * 0, passes criteria for aborting the FCP I/O iocb
12572  **/
12573 static int
lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq * iocbq,struct lpfc_vport * vport)12574 lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,
12575 				     struct lpfc_vport *vport)
12576 {
12577 	u8 ulp_command;
12578 
12579 	/* No null ptr vports */
12580 	if (!iocbq || iocbq->vport != vport)
12581 		return -ENODEV;
12582 
12583 	/* iocb must be for FCP IO, already exists on the TX cmpl queue,
12584 	 * can't be premarked as driver aborted, nor be an ABORT iocb itself
12585 	 */
12586 	ulp_command = get_job_cmnd(vport->phba, iocbq);
12587 	if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12588 	    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ) ||
12589 	    (iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12590 	    (ulp_command == CMD_ABORT_XRI_CN ||
12591 	     ulp_command == CMD_CLOSE_XRI_CN ||
12592 	     ulp_command == CMD_ABORT_XRI_WQE))
12593 		return -EINVAL;
12594 
12595 	return 0;
12596 }
12597 
12598 /**
12599  * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target
12600  * @iocbq: Pointer to driver iocb object.
12601  * @vport: Pointer to driver virtual port object.
12602  * @tgt_id: SCSI ID of the target.
12603  * @lun_id: LUN ID of the scsi device.
12604  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12605  *
12606  * This function acts as an iocb filter for validating a lun/SCSI target/SCSI
12607  * host.
12608  *
12609  * It will return
12610  * 0 if the filtering criteria is met for the given iocb and will return
12611  * 1 if the filtering criteria is not met.
12612  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12613  * given iocb is for the SCSI device specified by vport, tgt_id and
12614  * lun_id parameter.
12615  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
12616  * given iocb is for the SCSI target specified by vport and tgt_id
12617  * parameters.
12618  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12619  * given iocb is for the SCSI host associated with the given vport.
12620  * This function is called with no locks held.
12621  **/
12622 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)12623 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12624 			   uint16_t tgt_id, uint64_t lun_id,
12625 			   lpfc_ctx_cmd ctx_cmd)
12626 {
12627 	struct lpfc_io_buf *lpfc_cmd;
12628 	int rc = 1;
12629 
12630 	lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12631 
12632 	if (lpfc_cmd->pCmd == NULL)
12633 		return rc;
12634 
12635 	switch (ctx_cmd) {
12636 	case LPFC_CTX_LUN:
12637 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12638 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12639 		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
12640 			rc = 0;
12641 		break;
12642 	case LPFC_CTX_TGT:
12643 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12644 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
12645 			rc = 0;
12646 		break;
12647 	case LPFC_CTX_HOST:
12648 		rc = 0;
12649 		break;
12650 	default:
12651 		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
12652 			__func__, ctx_cmd);
12653 		break;
12654 	}
12655 
12656 	return rc;
12657 }
12658 
12659 /**
12660  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
12661  * @vport: Pointer to virtual port.
12662  * @tgt_id: SCSI ID of the target.
12663  * @lun_id: LUN ID of the scsi device.
12664  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12665  *
12666  * This function returns number of FCP commands pending for the vport.
12667  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12668  * commands pending on the vport associated with SCSI device specified
12669  * by tgt_id and lun_id parameters.
12670  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12671  * commands pending on the vport associated with SCSI target specified
12672  * by tgt_id parameter.
12673  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12674  * commands pending on the vport.
12675  * This function returns the number of iocbs which satisfy the filter.
12676  * This function is called without any lock held.
12677  **/
12678 int
lpfc_sli_sum_iocb(struct lpfc_vport * vport,uint16_t tgt_id,uint64_t lun_id,lpfc_ctx_cmd ctx_cmd)12679 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12680 		  lpfc_ctx_cmd ctx_cmd)
12681 {
12682 	struct lpfc_hba *phba = vport->phba;
12683 	struct lpfc_iocbq *iocbq;
12684 	int sum, i;
12685 	unsigned long iflags;
12686 	u8 ulp_command;
12687 
12688 	spin_lock_irqsave(&phba->hbalock, iflags);
12689 	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12690 		iocbq = phba->sli.iocbq_lookup[i];
12691 
12692 		if (!iocbq || iocbq->vport != vport)
12693 			continue;
12694 		if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12695 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ))
12696 			continue;
12697 
12698 		/* Include counting outstanding aborts */
12699 		ulp_command = get_job_cmnd(phba, iocbq);
12700 		if (ulp_command == CMD_ABORT_XRI_CN ||
12701 		    ulp_command == CMD_CLOSE_XRI_CN ||
12702 		    ulp_command == CMD_ABORT_XRI_WQE) {
12703 			sum++;
12704 			continue;
12705 		}
12706 
12707 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12708 					       ctx_cmd) == 0)
12709 			sum++;
12710 	}
12711 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12712 
12713 	return sum;
12714 }
12715 
12716 /**
12717  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12718  * @phba: Pointer to HBA context object
12719  * @cmdiocb: Pointer to command iocb object.
12720  * @rspiocb: Pointer to response iocb object.
12721  *
12722  * This function is called when an aborted FCP iocb completes. This
12723  * function is called by the ring event handler with no lock held.
12724  * This function frees the iocb.
12725  **/
12726 void
lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12727 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12728 			struct lpfc_iocbq *rspiocb)
12729 {
12730 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12731 			"3096 ABORT_XRI_CX completing on rpi x%x "
12732 			"original iotag x%x, abort cmd iotag x%x "
12733 			"status 0x%x, reason 0x%x\n",
12734 			(phba->sli_rev == LPFC_SLI_REV4) ?
12735 			cmdiocb->sli4_xritag :
12736 			cmdiocb->iocb.un.acxri.abortContextTag,
12737 			get_job_abtsiotag(phba, cmdiocb),
12738 			cmdiocb->iotag, get_job_ulpstatus(phba, rspiocb),
12739 			get_job_word4(phba, rspiocb));
12740 	lpfc_sli_release_iocbq(phba, cmdiocb);
12741 	return;
12742 }
12743 
12744 /**
12745  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
12746  * @vport: Pointer to virtual port.
12747  * @tgt_id: SCSI ID of the target.
12748  * @lun_id: LUN ID of the scsi device.
12749  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12750  *
12751  * This function sends an abort command for every SCSI command
12752  * associated with the given virtual port pending on the ring
12753  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12754  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12755  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12756  * followed by lpfc_sli_validate_fcp_iocb.
12757  *
12758  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12759  * FCP iocbs associated with lun specified by tgt_id and lun_id
12760  * parameters
12761  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12762  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12763  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12764  * FCP iocbs associated with virtual port.
12765  * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12766  * lpfc_sli4_calc_ring is used.
12767  * This function returns number of iocbs it failed to abort.
12768  * This function is called with no locks held.
12769  **/
12770 int
lpfc_sli_abort_iocb(struct lpfc_vport * vport,u16 tgt_id,u64 lun_id,lpfc_ctx_cmd abort_cmd)12771 lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12772 		    lpfc_ctx_cmd abort_cmd)
12773 {
12774 	struct lpfc_hba *phba = vport->phba;
12775 	struct lpfc_sli_ring *pring = NULL;
12776 	struct lpfc_iocbq *iocbq;
12777 	int errcnt = 0, ret_val = 0;
12778 	unsigned long iflags;
12779 	int i;
12780 
12781 	/* all I/Os are in process of being flushed */
12782 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12783 		return errcnt;
12784 
12785 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12786 		iocbq = phba->sli.iocbq_lookup[i];
12787 
12788 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12789 			continue;
12790 
12791 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12792 					       abort_cmd) != 0)
12793 			continue;
12794 
12795 		spin_lock_irqsave(&phba->hbalock, iflags);
12796 		if (phba->sli_rev == LPFC_SLI_REV3) {
12797 			pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12798 		} else if (phba->sli_rev == LPFC_SLI_REV4) {
12799 			pring = lpfc_sli4_calc_ring(phba, iocbq);
12800 		}
12801 		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
12802 						     lpfc_sli_abort_fcp_cmpl);
12803 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12804 		if (ret_val != IOCB_SUCCESS)
12805 			errcnt++;
12806 	}
12807 
12808 	return errcnt;
12809 }
12810 
12811 /**
12812  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12813  * @vport: Pointer to virtual port.
12814  * @pring: Pointer to driver SLI ring object.
12815  * @tgt_id: SCSI ID of the target.
12816  * @lun_id: LUN ID of the scsi device.
12817  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12818  *
12819  * This function sends an abort command for every SCSI command
12820  * associated with the given virtual port pending on the ring
12821  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12822  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12823  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12824  * followed by lpfc_sli_validate_fcp_iocb.
12825  *
12826  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12827  * FCP iocbs associated with lun specified by tgt_id and lun_id
12828  * parameters
12829  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12830  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12831  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12832  * FCP iocbs associated with virtual port.
12833  * This function returns number of iocbs it aborted .
12834  * This function is called with no locks held right after a taskmgmt
12835  * command is sent.
12836  **/
12837 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)12838 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12839 			uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12840 {
12841 	struct lpfc_hba *phba = vport->phba;
12842 	struct lpfc_io_buf *lpfc_cmd;
12843 	struct lpfc_iocbq *abtsiocbq;
12844 	struct lpfc_nodelist *ndlp = NULL;
12845 	struct lpfc_iocbq *iocbq;
12846 	int sum, i, ret_val;
12847 	unsigned long iflags;
12848 	struct lpfc_sli_ring *pring_s4 = NULL;
12849 	u16 ulp_context, iotag, cqid = LPFC_WQE_CQ_ID_DEFAULT;
12850 	bool ia;
12851 
12852 	/* all I/Os are in process of being flushed */
12853 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12854 		return 0;
12855 
12856 	sum = 0;
12857 
12858 	spin_lock_irqsave(&phba->hbalock, iflags);
12859 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12860 		iocbq = phba->sli.iocbq_lookup[i];
12861 
12862 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12863 			continue;
12864 
12865 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12866 					       cmd) != 0)
12867 			continue;
12868 
12869 		/* Guard against IO completion being called at same time */
12870 		lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12871 		spin_lock(&lpfc_cmd->buf_lock);
12872 
12873 		if (!lpfc_cmd->pCmd) {
12874 			spin_unlock(&lpfc_cmd->buf_lock);
12875 			continue;
12876 		}
12877 
12878 		if (phba->sli_rev == LPFC_SLI_REV4) {
12879 			pring_s4 =
12880 			    phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12881 			if (!pring_s4) {
12882 				spin_unlock(&lpfc_cmd->buf_lock);
12883 				continue;
12884 			}
12885 			/* Note: both hbalock and ring_lock must be set here */
12886 			spin_lock(&pring_s4->ring_lock);
12887 		}
12888 
12889 		/*
12890 		 * If the iocbq is already being aborted, don't take a second
12891 		 * action, but do count it.
12892 		 */
12893 		if ((iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12894 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
12895 			if (phba->sli_rev == LPFC_SLI_REV4)
12896 				spin_unlock(&pring_s4->ring_lock);
12897 			spin_unlock(&lpfc_cmd->buf_lock);
12898 			continue;
12899 		}
12900 
12901 		/* issue ABTS for this IOCB based on iotag */
12902 		abtsiocbq = __lpfc_sli_get_iocbq(phba);
12903 		if (!abtsiocbq) {
12904 			if (phba->sli_rev == LPFC_SLI_REV4)
12905 				spin_unlock(&pring_s4->ring_lock);
12906 			spin_unlock(&lpfc_cmd->buf_lock);
12907 			continue;
12908 		}
12909 
12910 		if (phba->sli_rev == LPFC_SLI_REV4) {
12911 			iotag = abtsiocbq->iotag;
12912 			ulp_context = iocbq->sli4_xritag;
12913 			cqid = lpfc_cmd->hdwq->io_cq_map;
12914 		} else {
12915 			iotag = iocbq->iocb.ulpIoTag;
12916 			if (pring->ringno == LPFC_ELS_RING) {
12917 				ndlp = iocbq->ndlp;
12918 				ulp_context = ndlp->nlp_rpi;
12919 			} else {
12920 				ulp_context = iocbq->iocb.ulpContext;
12921 			}
12922 		}
12923 
12924 		ndlp = lpfc_cmd->rdata->pnode;
12925 
12926 		if (lpfc_is_link_up(phba) &&
12927 		    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE) &&
12928 		    !(phba->link_flag & LS_EXTERNAL_LOOPBACK))
12929 			ia = false;
12930 		else
12931 			ia = true;
12932 
12933 		lpfc_sli_prep_abort_xri(phba, abtsiocbq, ulp_context, iotag,
12934 					iocbq->iocb.ulpClass, cqid,
12935 					ia, false);
12936 
12937 		abtsiocbq->vport = vport;
12938 
12939 		/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12940 		abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12941 		if (iocbq->cmd_flag & LPFC_IO_FCP)
12942 			abtsiocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
12943 		if (iocbq->cmd_flag & LPFC_IO_FOF)
12944 			abtsiocbq->cmd_flag |= LPFC_IO_FOF;
12945 
12946 		/* Setup callback routine and issue the command. */
12947 		abtsiocbq->cmd_cmpl = lpfc_sli_abort_fcp_cmpl;
12948 
12949 		/*
12950 		 * Indicate the IO is being aborted by the driver and set
12951 		 * the caller's flag into the aborted IO.
12952 		 */
12953 		iocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
12954 
12955 		if (phba->sli_rev == LPFC_SLI_REV4) {
12956 			ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12957 							abtsiocbq, 0);
12958 			spin_unlock(&pring_s4->ring_lock);
12959 		} else {
12960 			ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12961 							abtsiocbq, 0);
12962 		}
12963 
12964 		spin_unlock(&lpfc_cmd->buf_lock);
12965 
12966 		if (ret_val == IOCB_ERROR)
12967 			__lpfc_sli_release_iocbq(phba, abtsiocbq);
12968 		else
12969 			sum++;
12970 	}
12971 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12972 	return sum;
12973 }
12974 
12975 /**
12976  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
12977  * @phba: Pointer to HBA context object.
12978  * @cmdiocbq: Pointer to command iocb.
12979  * @rspiocbq: Pointer to response iocb.
12980  *
12981  * This function is the completion handler for iocbs issued using
12982  * lpfc_sli_issue_iocb_wait function. This function is called by the
12983  * ring event handler function without any lock held. This function
12984  * can be called from both worker thread context and interrupt
12985  * context. This function also can be called from other thread which
12986  * cleans up the SLI layer objects.
12987  * This function copy the contents of the response iocb to the
12988  * response iocb memory object provided by the caller of
12989  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12990  * sleeps for the iocb completion.
12991  **/
12992 static void
lpfc_sli_wake_iocb_wait(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_iocbq * rspiocbq)12993 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
12994 			struct lpfc_iocbq *cmdiocbq,
12995 			struct lpfc_iocbq *rspiocbq)
12996 {
12997 	wait_queue_head_t *pdone_q;
12998 	unsigned long iflags;
12999 	struct lpfc_io_buf *lpfc_cmd;
13000 	size_t offset = offsetof(struct lpfc_iocbq, wqe);
13001 
13002 	spin_lock_irqsave(&phba->hbalock, iflags);
13003 	if (cmdiocbq->cmd_flag & LPFC_IO_WAKE_TMO) {
13004 
13005 		/*
13006 		 * A time out has occurred for the iocb.  If a time out
13007 		 * completion handler has been supplied, call it.  Otherwise,
13008 		 * just free the iocbq.
13009 		 */
13010 
13011 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13012 		cmdiocbq->cmd_cmpl = cmdiocbq->wait_cmd_cmpl;
13013 		cmdiocbq->wait_cmd_cmpl = NULL;
13014 		if (cmdiocbq->cmd_cmpl)
13015 			cmdiocbq->cmd_cmpl(phba, cmdiocbq, NULL);
13016 		else
13017 			lpfc_sli_release_iocbq(phba, cmdiocbq);
13018 		return;
13019 	}
13020 
13021 	/* Copy the contents of the local rspiocb into the caller's buffer. */
13022 	cmdiocbq->cmd_flag |= LPFC_IO_WAKE;
13023 	if (cmdiocbq->rsp_iocb && rspiocbq)
13024 		memcpy((char *)cmdiocbq->rsp_iocb + offset,
13025 		       (char *)rspiocbq + offset, sizeof(*rspiocbq) - offset);
13026 
13027 	/* Set the exchange busy flag for task management commands */
13028 	if ((cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
13029 	    !(cmdiocbq->cmd_flag & LPFC_IO_LIBDFC)) {
13030 		lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
13031 					cur_iocbq);
13032 		if (rspiocbq && (rspiocbq->cmd_flag & LPFC_EXCHANGE_BUSY))
13033 			lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
13034 		else
13035 			lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
13036 	}
13037 
13038 	pdone_q = cmdiocbq->context_un.wait_queue;
13039 	if (pdone_q)
13040 		wake_up(pdone_q);
13041 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13042 	return;
13043 }
13044 
13045 /**
13046  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
13047  * @phba: Pointer to HBA context object..
13048  * @piocbq: Pointer to command iocb.
13049  * @flag: Flag to test.
13050  *
13051  * This routine grabs the hbalock and then test the cmd_flag to
13052  * see if the passed in flag is set.
13053  * Returns:
13054  * 1 if flag is set.
13055  * 0 if flag is not set.
13056  **/
13057 static int
lpfc_chk_iocb_flg(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq,uint32_t flag)13058 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
13059 		 struct lpfc_iocbq *piocbq, uint32_t flag)
13060 {
13061 	unsigned long iflags;
13062 	int ret;
13063 
13064 	spin_lock_irqsave(&phba->hbalock, iflags);
13065 	ret = piocbq->cmd_flag & flag;
13066 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13067 	return ret;
13068 
13069 }
13070 
13071 /**
13072  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
13073  * @phba: Pointer to HBA context object..
13074  * @ring_number: Ring number
13075  * @piocb: Pointer to command iocb.
13076  * @prspiocbq: Pointer to response iocb.
13077  * @timeout: Timeout in number of seconds.
13078  *
13079  * This function issues the iocb to firmware and waits for the
13080  * iocb to complete. The cmd_cmpl field of the shall be used
13081  * to handle iocbs which time out. If the field is NULL, the
13082  * function shall free the iocbq structure.  If more clean up is
13083  * needed, the caller is expected to provide a completion function
13084  * that will provide the needed clean up.  If the iocb command is
13085  * not completed within timeout seconds, the function will either
13086  * free the iocbq structure (if cmd_cmpl == NULL) or execute the
13087  * completion function set in the cmd_cmpl field and then return
13088  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
13089  * resources if this function returns IOCB_TIMEDOUT.
13090  * The function waits for the iocb completion using an
13091  * non-interruptible wait.
13092  * This function will sleep while waiting for iocb completion.
13093  * So, this function should not be called from any context which
13094  * does not allow sleeping. Due to the same reason, this function
13095  * cannot be called with interrupt disabled.
13096  * This function assumes that the iocb completions occur while
13097  * this function sleep. So, this function cannot be called from
13098  * the thread which process iocb completion for this ring.
13099  * This function clears the cmd_flag of the iocb object before
13100  * issuing the iocb and the iocb completion handler sets this
13101  * flag and wakes this thread when the iocb completes.
13102  * The contents of the response iocb will be copied to prspiocbq
13103  * by the completion handler when the command completes.
13104  * This function returns IOCB_SUCCESS when success.
13105  * This function is called with no lock held.
13106  **/
13107 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)13108 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
13109 			 uint32_t ring_number,
13110 			 struct lpfc_iocbq *piocb,
13111 			 struct lpfc_iocbq *prspiocbq,
13112 			 uint32_t timeout)
13113 {
13114 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
13115 	long timeleft, timeout_req = 0;
13116 	int retval = IOCB_SUCCESS;
13117 	uint32_t creg_val;
13118 	struct lpfc_iocbq *iocb;
13119 	int txq_cnt = 0;
13120 	int txcmplq_cnt = 0;
13121 	struct lpfc_sli_ring *pring;
13122 	unsigned long iflags;
13123 	bool iocb_completed = true;
13124 
13125 	if (phba->sli_rev >= LPFC_SLI_REV4) {
13126 		lpfc_sli_prep_wqe(phba, piocb);
13127 
13128 		pring = lpfc_sli4_calc_ring(phba, piocb);
13129 	} else
13130 		pring = &phba->sli.sli3_ring[ring_number];
13131 	/*
13132 	 * If the caller has provided a response iocbq buffer, then rsp_iocb
13133 	 * is NULL or its an error.
13134 	 */
13135 	if (prspiocbq) {
13136 		if (piocb->rsp_iocb)
13137 			return IOCB_ERROR;
13138 		piocb->rsp_iocb = prspiocbq;
13139 	}
13140 
13141 	piocb->wait_cmd_cmpl = piocb->cmd_cmpl;
13142 	piocb->cmd_cmpl = lpfc_sli_wake_iocb_wait;
13143 	piocb->context_un.wait_queue = &done_q;
13144 	piocb->cmd_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
13145 
13146 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13147 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13148 			return IOCB_ERROR;
13149 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13150 		writel(creg_val, phba->HCregaddr);
13151 		readl(phba->HCregaddr); /* flush */
13152 	}
13153 
13154 	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13155 				     SLI_IOCB_RET_IOCB);
13156 	if (retval == IOCB_SUCCESS) {
13157 		timeout_req = secs_to_jiffies(timeout);
13158 		timeleft = wait_event_timeout(done_q,
13159 				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
13160 				timeout_req);
13161 		spin_lock_irqsave(&phba->hbalock, iflags);
13162 		if (!(piocb->cmd_flag & LPFC_IO_WAKE)) {
13163 
13164 			/*
13165 			 * IOCB timed out.  Inform the wake iocb wait
13166 			 * completion function and set local status
13167 			 */
13168 
13169 			iocb_completed = false;
13170 			piocb->cmd_flag |= LPFC_IO_WAKE_TMO;
13171 		}
13172 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13173 		if (iocb_completed) {
13174 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13175 					"0331 IOCB wake signaled\n");
13176 			/* Note: we are not indicating if the IOCB has a success
13177 			 * status or not - that's for the caller to check.
13178 			 * IOCB_SUCCESS means just that the command was sent and
13179 			 * completed. Not that it completed successfully.
13180 			 * */
13181 		} else if (timeleft == 0) {
13182 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13183 					"0338 IOCB wait timeout error - no "
13184 					"wake response Data x%x\n", timeout);
13185 			retval = IOCB_TIMEDOUT;
13186 		} else {
13187 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13188 					"0330 IOCB wake NOT set, "
13189 					"Data x%x x%lx\n",
13190 					timeout, (timeleft / jiffies));
13191 			retval = IOCB_TIMEDOUT;
13192 		}
13193 	} else if (retval == IOCB_BUSY) {
13194 		if (phba->cfg_log_verbose & LOG_SLI) {
13195 			list_for_each_entry(iocb, &pring->txq, list) {
13196 				txq_cnt++;
13197 			}
13198 			list_for_each_entry(iocb, &pring->txcmplq, list) {
13199 				txcmplq_cnt++;
13200 			}
13201 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13202 				"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13203 				phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13204 		}
13205 		return retval;
13206 	} else {
13207 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13208 				"0332 IOCB wait issue failed, Data x%x\n",
13209 				retval);
13210 		retval = IOCB_ERROR;
13211 	}
13212 
13213 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13214 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13215 			return IOCB_ERROR;
13216 		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13217 		writel(creg_val, phba->HCregaddr);
13218 		readl(phba->HCregaddr); /* flush */
13219 	}
13220 
13221 	if (prspiocbq)
13222 		piocb->rsp_iocb = NULL;
13223 
13224 	piocb->context_un.wait_queue = NULL;
13225 	piocb->cmd_cmpl = NULL;
13226 	return retval;
13227 }
13228 
13229 /**
13230  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
13231  * @phba: Pointer to HBA context object.
13232  * @pmboxq: Pointer to driver mailbox object.
13233  * @timeout: Timeout in number of seconds.
13234  *
13235  * This function issues the mailbox to firmware and waits for the
13236  * mailbox command to complete. If the mailbox command is not
13237  * completed within timeout seconds, it returns MBX_TIMEOUT.
13238  * The function waits for the mailbox completion using an
13239  * interruptible wait. If the thread is woken up due to a
13240  * signal, MBX_TIMEOUT error is returned to the caller. Caller
13241  * should not free the mailbox resources, if this function returns
13242  * MBX_TIMEOUT.
13243  * This function will sleep while waiting for mailbox completion.
13244  * So, this function should not be called from any context which
13245  * does not allow sleeping. Due to the same reason, this function
13246  * cannot be called with interrupt disabled.
13247  * This function assumes that the mailbox completion occurs while
13248  * this function sleep. So, this function cannot be called from
13249  * the worker thread which processes mailbox completion.
13250  * This function is called in the context of HBA management
13251  * applications.
13252  * This function returns MBX_SUCCESS when successful.
13253  * This function is called with no lock held.
13254  **/
13255 int
lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq,uint32_t timeout)13256 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
13257 			 uint32_t timeout)
13258 {
13259 	struct completion mbox_done;
13260 	int retval;
13261 	unsigned long flag;
13262 
13263 	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
13264 	/* setup wake call as IOCB callback */
13265 	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
13266 
13267 	/* setup ctx_u field to pass wait_queue pointer to wake function  */
13268 	init_completion(&mbox_done);
13269 	pmboxq->ctx_u.mbox_wait = &mbox_done;
13270 	/* now issue the command */
13271 	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
13272 	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
13273 		wait_for_completion_timeout(&mbox_done, secs_to_jiffies(timeout));
13274 
13275 		spin_lock_irqsave(&phba->hbalock, flag);
13276 		pmboxq->ctx_u.mbox_wait = NULL;
13277 		/*
13278 		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13279 		 * else do not free the resources.
13280 		 */
13281 		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
13282 			retval = MBX_SUCCESS;
13283 		} else {
13284 			retval = MBX_TIMEOUT;
13285 			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13286 		}
13287 		spin_unlock_irqrestore(&phba->hbalock, flag);
13288 	}
13289 	return retval;
13290 }
13291 
13292 /**
13293  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
13294  * @phba: Pointer to HBA context.
13295  * @mbx_action: Mailbox shutdown options.
13296  *
13297  * This function is called to shutdown the driver's mailbox sub-system.
13298  * It first marks the mailbox sub-system is in a block state to prevent
13299  * the asynchronous mailbox command from issued off the pending mailbox
13300  * command queue. If the mailbox command sub-system shutdown is due to
13301  * HBA error conditions such as EEH or ERATT, this routine shall invoke
13302  * the mailbox sub-system flush routine to forcefully bring down the
13303  * mailbox sub-system. Otherwise, if it is due to normal condition (such
13304  * as with offline or HBA function reset), this routine will wait for the
13305  * outstanding mailbox command to complete before invoking the mailbox
13306  * sub-system flush routine to gracefully bring down mailbox sub-system.
13307  **/
13308 void
lpfc_sli_mbox_sys_shutdown(struct lpfc_hba * phba,int mbx_action)13309 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
13310 {
13311 	struct lpfc_sli *psli = &phba->sli;
13312 	unsigned long timeout;
13313 
13314 	if (mbx_action == LPFC_MBX_NO_WAIT) {
13315 		/* delay 100ms for port state */
13316 		msleep(100);
13317 		lpfc_sli_mbox_sys_flush(phba);
13318 		return;
13319 	}
13320 	timeout = secs_to_jiffies(LPFC_MBOX_TMO) + jiffies;
13321 
13322 	/* Disable softirqs, including timers from obtaining phba->hbalock */
13323 	local_bh_disable();
13324 
13325 	spin_lock_irq(&phba->hbalock);
13326 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13327 
13328 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
13329 		/* Determine how long we might wait for the active mailbox
13330 		 * command to be gracefully completed by firmware.
13331 		 */
13332 		if (phba->sli.mbox_active)
13333 			timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
13334 						phba->sli.mbox_active)) + jiffies;
13335 		spin_unlock_irq(&phba->hbalock);
13336 
13337 		/* Enable softirqs again, done with phba->hbalock */
13338 		local_bh_enable();
13339 
13340 		while (phba->sli.mbox_active) {
13341 			/* Check active mailbox complete status every 2ms */
13342 			msleep(2);
13343 			if (time_after(jiffies, timeout))
13344 				/* Timeout, let the mailbox flush routine to
13345 				 * forcefully release active mailbox command
13346 				 */
13347 				break;
13348 		}
13349 	} else {
13350 		spin_unlock_irq(&phba->hbalock);
13351 
13352 		/* Enable softirqs again, done with phba->hbalock */
13353 		local_bh_enable();
13354 	}
13355 
13356 	lpfc_sli_mbox_sys_flush(phba);
13357 }
13358 
13359 /**
13360  * lpfc_sli_eratt_read - read sli-3 error attention events
13361  * @phba: Pointer to HBA context.
13362  *
13363  * This function is called to read the SLI3 device error attention registers
13364  * for possible error attention events. The caller must hold the hostlock
13365  * with spin_lock_irq().
13366  *
13367  * This function returns 1 when there is Error Attention in the Host Attention
13368  * Register and returns 0 otherwise.
13369  **/
13370 static int
lpfc_sli_eratt_read(struct lpfc_hba * phba)13371 lpfc_sli_eratt_read(struct lpfc_hba *phba)
13372 {
13373 	uint32_t ha_copy;
13374 
13375 	/* Read chip Host Attention (HA) register */
13376 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
13377 		goto unplug_err;
13378 
13379 	if (ha_copy & HA_ERATT) {
13380 		/* Read host status register to retrieve error event */
13381 		if (lpfc_sli_read_hs(phba))
13382 			goto unplug_err;
13383 
13384 		/* Check if there is a deferred error condition is active */
13385 		if ((HS_FFER1 & phba->work_hs) &&
13386 		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13387 		      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
13388 			set_bit(DEFER_ERATT, &phba->hba_flag);
13389 			/* Clear all interrupt enable conditions */
13390 			writel(0, phba->HCregaddr);
13391 			readl(phba->HCregaddr);
13392 		}
13393 
13394 		/* Set the driver HA work bitmap */
13395 		phba->work_ha |= HA_ERATT;
13396 		/* Indicate polling handles this ERATT */
13397 		set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13398 		return 1;
13399 	}
13400 	return 0;
13401 
13402 unplug_err:
13403 	/* Set the driver HS work bitmap */
13404 	phba->work_hs |= UNPLUG_ERR;
13405 	/* Set the driver HA work bitmap */
13406 	phba->work_ha |= HA_ERATT;
13407 	/* Indicate polling handles this ERATT */
13408 	set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13409 	return 1;
13410 }
13411 
13412 /**
13413  * lpfc_sli4_eratt_read - read sli-4 error attention events
13414  * @phba: Pointer to HBA context.
13415  *
13416  * This function is called to read the SLI4 device error attention registers
13417  * for possible error attention events. The caller must hold the hostlock
13418  * with spin_lock_irq().
13419  *
13420  * This function returns 1 when there is Error Attention in the Host Attention
13421  * Register and returns 0 otherwise.
13422  **/
13423 static int
lpfc_sli4_eratt_read(struct lpfc_hba * phba)13424 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13425 {
13426 	uint32_t uerr_sta_hi, uerr_sta_lo;
13427 	uint32_t if_type, portsmphr;
13428 	struct lpfc_register portstat_reg;
13429 	u32 logmask;
13430 
13431 	/*
13432 	 * For now, use the SLI4 device internal unrecoverable error
13433 	 * registers for error attention. This can be changed later.
13434 	 */
13435 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13436 	switch (if_type) {
13437 	case LPFC_SLI_INTF_IF_TYPE_0:
13438 		if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13439 			&uerr_sta_lo) ||
13440 			lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13441 			&uerr_sta_hi)) {
13442 			phba->work_hs |= UNPLUG_ERR;
13443 			phba->work_ha |= HA_ERATT;
13444 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13445 			return 1;
13446 		}
13447 		if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13448 		    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
13449 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13450 					"1423 HBA Unrecoverable error: "
13451 					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13452 					"ue_mask_lo_reg=0x%x, "
13453 					"ue_mask_hi_reg=0x%x\n",
13454 					uerr_sta_lo, uerr_sta_hi,
13455 					phba->sli4_hba.ue_mask_lo,
13456 					phba->sli4_hba.ue_mask_hi);
13457 			phba->work_status[0] = uerr_sta_lo;
13458 			phba->work_status[1] = uerr_sta_hi;
13459 			phba->work_ha |= HA_ERATT;
13460 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13461 			return 1;
13462 		}
13463 		break;
13464 	case LPFC_SLI_INTF_IF_TYPE_2:
13465 	case LPFC_SLI_INTF_IF_TYPE_6:
13466 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13467 			&portstat_reg.word0) ||
13468 			lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13469 			&portsmphr)){
13470 			phba->work_hs |= UNPLUG_ERR;
13471 			phba->work_ha |= HA_ERATT;
13472 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13473 			return 1;
13474 		}
13475 		if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13476 			phba->work_status[0] =
13477 				readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13478 			phba->work_status[1] =
13479 				readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
13480 			logmask = LOG_TRACE_EVENT;
13481 			if (phba->work_status[0] ==
13482 				SLIPORT_ERR1_REG_ERR_CODE_2 &&
13483 			    phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)
13484 				logmask = LOG_SLI;
13485 			lpfc_printf_log(phba, KERN_ERR, logmask,
13486 					"2885 Port Status Event: "
13487 					"port status reg 0x%x, "
13488 					"port smphr reg 0x%x, "
13489 					"error 1=0x%x, error 2=0x%x\n",
13490 					portstat_reg.word0,
13491 					portsmphr,
13492 					phba->work_status[0],
13493 					phba->work_status[1]);
13494 			phba->work_ha |= HA_ERATT;
13495 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13496 			return 1;
13497 		}
13498 		break;
13499 	case LPFC_SLI_INTF_IF_TYPE_1:
13500 	default:
13501 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13502 				"2886 HBA Error Attention on unsupported "
13503 				"if type %d.", if_type);
13504 		return 1;
13505 	}
13506 
13507 	return 0;
13508 }
13509 
13510 /**
13511  * lpfc_sli_check_eratt - check error attention events
13512  * @phba: Pointer to HBA context.
13513  *
13514  * This function is called from timer soft interrupt context to check HBA's
13515  * error attention register bit for error attention events.
13516  *
13517  * This function returns 1 when there is Error Attention in the Host Attention
13518  * Register and returns 0 otherwise.
13519  **/
13520 int
lpfc_sli_check_eratt(struct lpfc_hba * phba)13521 lpfc_sli_check_eratt(struct lpfc_hba *phba)
13522 {
13523 	uint32_t ha_copy;
13524 
13525 	/* If somebody is waiting to handle an eratt, don't process it
13526 	 * here. The brdkill function will do this.
13527 	 */
13528 	if (phba->link_flag & LS_IGNORE_ERATT)
13529 		return 0;
13530 
13531 	/* Check if interrupt handler handles this ERATT */
13532 	if (test_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
13533 		/* Interrupt handler has handled ERATT */
13534 		return 0;
13535 
13536 	/*
13537 	 * If there is deferred error attention, do not check for error
13538 	 * attention
13539 	 */
13540 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13541 		return 0;
13542 
13543 	spin_lock_irq(&phba->hbalock);
13544 	/* If PCI channel is offline, don't process it */
13545 	if (unlikely(pci_channel_offline(phba->pcidev))) {
13546 		spin_unlock_irq(&phba->hbalock);
13547 		return 0;
13548 	}
13549 
13550 	switch (phba->sli_rev) {
13551 	case LPFC_SLI_REV2:
13552 	case LPFC_SLI_REV3:
13553 		/* Read chip Host Attention (HA) register */
13554 		ha_copy = lpfc_sli_eratt_read(phba);
13555 		break;
13556 	case LPFC_SLI_REV4:
13557 		/* Read device Uncoverable Error (UERR) registers */
13558 		ha_copy = lpfc_sli4_eratt_read(phba);
13559 		break;
13560 	default:
13561 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13562 				"0299 Invalid SLI revision (%d)\n",
13563 				phba->sli_rev);
13564 		ha_copy = 0;
13565 		break;
13566 	}
13567 	spin_unlock_irq(&phba->hbalock);
13568 
13569 	return ha_copy;
13570 }
13571 
13572 /**
13573  * lpfc_intr_state_check - Check device state for interrupt handling
13574  * @phba: Pointer to HBA context.
13575  *
13576  * This inline routine checks whether a device or its PCI slot is in a state
13577  * that the interrupt should be handled.
13578  *
13579  * This function returns 0 if the device or the PCI slot is in a state that
13580  * interrupt should be handled, otherwise -EIO.
13581  */
13582 static inline int
lpfc_intr_state_check(struct lpfc_hba * phba)13583 lpfc_intr_state_check(struct lpfc_hba *phba)
13584 {
13585 	/* If the pci channel is offline, ignore all the interrupts */
13586 	if (unlikely(pci_channel_offline(phba->pcidev)))
13587 		return -EIO;
13588 
13589 	/* Update device level interrupt statistics */
13590 	phba->sli.slistat.sli_intr++;
13591 
13592 	/* Ignore all interrupts during initialization. */
13593 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13594 		return -EIO;
13595 
13596 	return 0;
13597 }
13598 
13599 /**
13600  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
13601  * @irq: Interrupt number.
13602  * @dev_id: The device context pointer.
13603  *
13604  * This function is directly called from the PCI layer as an interrupt
13605  * service routine when device with SLI-3 interface spec is enabled with
13606  * MSI-X multi-message interrupt mode and there are slow-path events in
13607  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13608  * interrupt mode, this function is called as part of the device-level
13609  * interrupt handler. When the PCI slot is in error recovery or the HBA
13610  * is undergoing initialization, the interrupt handler will not process
13611  * the interrupt. The link attention and ELS ring attention events are
13612  * handled by the worker thread. The interrupt handler signals the worker
13613  * thread and returns for these events. This function is called without
13614  * any lock held. It gets the hbalock to access and update SLI data
13615  * structures.
13616  *
13617  * This function returns IRQ_HANDLED when interrupt is handled else it
13618  * returns IRQ_NONE.
13619  **/
13620 irqreturn_t
lpfc_sli_sp_intr_handler(int irq,void * dev_id)13621 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
13622 {
13623 	struct lpfc_hba  *phba;
13624 	uint32_t ha_copy, hc_copy;
13625 	uint32_t work_ha_copy;
13626 	unsigned long status;
13627 	unsigned long iflag;
13628 	uint32_t control;
13629 
13630 	MAILBOX_t *mbox, *pmbox;
13631 	struct lpfc_vport *vport;
13632 	struct lpfc_nodelist *ndlp;
13633 	struct lpfc_dmabuf *mp;
13634 	LPFC_MBOXQ_t *pmb;
13635 	int rc;
13636 
13637 	/*
13638 	 * Get the driver's phba structure from the dev_id and
13639 	 * assume the HBA is not interrupting.
13640 	 */
13641 	phba = (struct lpfc_hba *)dev_id;
13642 
13643 	if (unlikely(!phba))
13644 		return IRQ_NONE;
13645 
13646 	/*
13647 	 * Stuff needs to be attented to when this function is invoked as an
13648 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13649 	 */
13650 	if (phba->intr_type == MSIX) {
13651 		/* Check device state for handling interrupt */
13652 		if (lpfc_intr_state_check(phba))
13653 			return IRQ_NONE;
13654 		/* Need to read HA REG for slow-path events */
13655 		spin_lock_irqsave(&phba->hbalock, iflag);
13656 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13657 			goto unplug_error;
13658 		/* If somebody is waiting to handle an eratt don't process it
13659 		 * here. The brdkill function will do this.
13660 		 */
13661 		if (phba->link_flag & LS_IGNORE_ERATT)
13662 			ha_copy &= ~HA_ERATT;
13663 		/* Check the need for handling ERATT in interrupt handler */
13664 		if (ha_copy & HA_ERATT) {
13665 			if (test_and_set_bit(HBA_ERATT_HANDLED,
13666 					     &phba->hba_flag))
13667 				/* ERATT polling has handled ERATT */
13668 				ha_copy &= ~HA_ERATT;
13669 		}
13670 
13671 		/*
13672 		 * If there is deferred error attention, do not check for any
13673 		 * interrupt.
13674 		 */
13675 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
13676 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13677 			return IRQ_NONE;
13678 		}
13679 
13680 		/* Clear up only attention source related to slow-path */
13681 		if (lpfc_readl(phba->HCregaddr, &hc_copy))
13682 			goto unplug_error;
13683 
13684 		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13685 			HC_LAINT_ENA | HC_ERINT_ENA),
13686 			phba->HCregaddr);
13687 		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13688 			phba->HAregaddr);
13689 		writel(hc_copy, phba->HCregaddr);
13690 		readl(phba->HAregaddr); /* flush */
13691 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13692 	} else
13693 		ha_copy = phba->ha_copy;
13694 
13695 	work_ha_copy = ha_copy & phba->work_ha_mask;
13696 
13697 	if (work_ha_copy) {
13698 		if (work_ha_copy & HA_LATT) {
13699 			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13700 				/*
13701 				 * Turn off Link Attention interrupts
13702 				 * until CLEAR_LA done
13703 				 */
13704 				spin_lock_irqsave(&phba->hbalock, iflag);
13705 				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
13706 				if (lpfc_readl(phba->HCregaddr, &control))
13707 					goto unplug_error;
13708 				control &= ~HC_LAINT_ENA;
13709 				writel(control, phba->HCregaddr);
13710 				readl(phba->HCregaddr); /* flush */
13711 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13712 			}
13713 			else
13714 				work_ha_copy &= ~HA_LATT;
13715 		}
13716 
13717 		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
13718 			/*
13719 			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13720 			 * the only slow ring.
13721 			 */
13722 			status = (work_ha_copy &
13723 				(HA_RXMASK  << (4*LPFC_ELS_RING)));
13724 			status >>= (4*LPFC_ELS_RING);
13725 			if (status & HA_RXMASK) {
13726 				spin_lock_irqsave(&phba->hbalock, iflag);
13727 				if (lpfc_readl(phba->HCregaddr, &control))
13728 					goto unplug_error;
13729 
13730 				lpfc_debugfs_slow_ring_trc(phba,
13731 				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
13732 				control, status,
13733 				(uint32_t)phba->sli.slistat.sli_intr);
13734 
13735 				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
13736 					lpfc_debugfs_slow_ring_trc(phba,
13737 						"ISR Disable ring:"
13738 						"pwork:x%x hawork:x%x wait:x%x",
13739 						phba->work_ha, work_ha_copy,
13740 						(uint32_t)((unsigned long)
13741 						&phba->work_waitq));
13742 
13743 					control &=
13744 					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
13745 					writel(control, phba->HCregaddr);
13746 					readl(phba->HCregaddr); /* flush */
13747 				}
13748 				else {
13749 					lpfc_debugfs_slow_ring_trc(phba,
13750 						"ISR slow ring:   pwork:"
13751 						"x%x hawork:x%x wait:x%x",
13752 						phba->work_ha, work_ha_copy,
13753 						(uint32_t)((unsigned long)
13754 						&phba->work_waitq));
13755 				}
13756 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13757 			}
13758 		}
13759 		spin_lock_irqsave(&phba->hbalock, iflag);
13760 		if (work_ha_copy & HA_ERATT) {
13761 			if (lpfc_sli_read_hs(phba))
13762 				goto unplug_error;
13763 			/*
13764 			 * Check if there is a deferred error condition
13765 			 * is active
13766 			 */
13767 			if ((HS_FFER1 & phba->work_hs) &&
13768 				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13769 				  HS_FFER6 | HS_FFER7 | HS_FFER8) &
13770 				  phba->work_hs)) {
13771 				set_bit(DEFER_ERATT, &phba->hba_flag);
13772 				/* Clear all interrupt enable conditions */
13773 				writel(0, phba->HCregaddr);
13774 				readl(phba->HCregaddr);
13775 			}
13776 		}
13777 
13778 		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
13779 			pmb = phba->sli.mbox_active;
13780 			pmbox = &pmb->u.mb;
13781 			mbox = phba->mbox;
13782 			vport = pmb->vport;
13783 
13784 			/* First check out the status word */
13785 			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13786 			if (pmbox->mbxOwner != OWN_HOST) {
13787 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13788 				/*
13789 				 * Stray Mailbox Interrupt, mbxCommand <cmd>
13790 				 * mbxStatus <status>
13791 				 */
13792 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13793 						"(%d):0304 Stray Mailbox "
13794 						"Interrupt mbxCommand x%x "
13795 						"mbxStatus x%x\n",
13796 						(vport ? vport->vpi : 0),
13797 						pmbox->mbxCommand,
13798 						pmbox->mbxStatus);
13799 				/* clear mailbox attention bit */
13800 				work_ha_copy &= ~HA_MBATT;
13801 			} else {
13802 				phba->sli.mbox_active = NULL;
13803 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13804 				phba->last_completion_time = jiffies;
13805 				timer_delete(&phba->sli.mbox_tmo);
13806 				if (pmb->mbox_cmpl) {
13807 					lpfc_sli_pcimem_bcopy(mbox, pmbox,
13808 							MAILBOX_CMD_SIZE);
13809 					if (pmb->out_ext_byte_len &&
13810 						pmb->ext_buf)
13811 						lpfc_sli_pcimem_bcopy(
13812 						phba->mbox_ext,
13813 						pmb->ext_buf,
13814 						pmb->out_ext_byte_len);
13815 				}
13816 				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13817 					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13818 
13819 					lpfc_debugfs_disc_trc(vport,
13820 						LPFC_DISC_TRC_MBOX_VPORT,
13821 						"MBOX dflt rpi: : "
13822 						"status:x%x rpi:x%x",
13823 						(uint32_t)pmbox->mbxStatus,
13824 						pmbox->un.varWords[0], 0);
13825 
13826 					if (!pmbox->mbxStatus) {
13827 						mp = pmb->ctx_buf;
13828 						ndlp = pmb->ctx_ndlp;
13829 
13830 						/* Reg_LOGIN of dflt RPI was
13831 						 * successful. new lets get
13832 						 * rid of the RPI using the
13833 						 * same mbox buffer.
13834 						 */
13835 						lpfc_unreg_login(phba,
13836 							vport->vpi,
13837 							pmbox->un.varWords[0],
13838 							pmb);
13839 						pmb->mbox_cmpl =
13840 							lpfc_mbx_cmpl_dflt_rpi;
13841 						pmb->ctx_buf = mp;
13842 						pmb->ctx_ndlp = ndlp;
13843 						pmb->vport = vport;
13844 						rc = lpfc_sli_issue_mbox(phba,
13845 								pmb,
13846 								MBX_NOWAIT);
13847 						if (rc != MBX_BUSY)
13848 							lpfc_printf_log(phba,
13849 							KERN_ERR,
13850 							LOG_TRACE_EVENT,
13851 							"0350 rc should have"
13852 							"been MBX_BUSY\n");
13853 						if (rc != MBX_NOT_FINISHED)
13854 							goto send_current_mbox;
13855 					}
13856 				}
13857 				spin_lock_irqsave(
13858 						&phba->pport->work_port_lock,
13859 						iflag);
13860 				phba->pport->work_port_events &=
13861 					~WORKER_MBOX_TMO;
13862 				spin_unlock_irqrestore(
13863 						&phba->pport->work_port_lock,
13864 						iflag);
13865 
13866 				/* Do NOT queue MBX_HEARTBEAT to the worker
13867 				 * thread for processing.
13868 				 */
13869 				if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13870 					/* Process mbox now */
13871 					phba->sli.mbox_active = NULL;
13872 					phba->sli.sli_flag &=
13873 						~LPFC_SLI_MBOX_ACTIVE;
13874 					if (pmb->mbox_cmpl)
13875 						pmb->mbox_cmpl(phba, pmb);
13876 				} else {
13877 					/* Queue to worker thread to process */
13878 					lpfc_mbox_cmpl_put(phba, pmb);
13879 				}
13880 			}
13881 		} else
13882 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13883 
13884 		if ((work_ha_copy & HA_MBATT) &&
13885 		    (phba->sli.mbox_active == NULL)) {
13886 send_current_mbox:
13887 			/* Process next mailbox command if there is one */
13888 			do {
13889 				rc = lpfc_sli_issue_mbox(phba, NULL,
13890 							 MBX_NOWAIT);
13891 			} while (rc == MBX_NOT_FINISHED);
13892 			if (rc != MBX_SUCCESS)
13893 				lpfc_printf_log(phba, KERN_ERR,
13894 						LOG_TRACE_EVENT,
13895 						"0349 rc should be "
13896 						"MBX_SUCCESS\n");
13897 		}
13898 
13899 		spin_lock_irqsave(&phba->hbalock, iflag);
13900 		phba->work_ha |= work_ha_copy;
13901 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13902 		lpfc_worker_wake_up(phba);
13903 	}
13904 	return IRQ_HANDLED;
13905 unplug_error:
13906 	spin_unlock_irqrestore(&phba->hbalock, iflag);
13907 	return IRQ_HANDLED;
13908 
13909 } /* lpfc_sli_sp_intr_handler */
13910 
13911 /**
13912  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13913  * @irq: Interrupt number.
13914  * @dev_id: The device context pointer.
13915  *
13916  * This function is directly called from the PCI layer as an interrupt
13917  * service routine when device with SLI-3 interface spec is enabled with
13918  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13919  * ring event in the HBA. However, when the device is enabled with either
13920  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13921  * device-level interrupt handler. When the PCI slot is in error recovery
13922  * or the HBA is undergoing initialization, the interrupt handler will not
13923  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13924  * the intrrupt context. This function is called without any lock held.
13925  * It gets the hbalock to access and update SLI data structures.
13926  *
13927  * This function returns IRQ_HANDLED when interrupt is handled else it
13928  * returns IRQ_NONE.
13929  **/
13930 irqreturn_t
lpfc_sli_fp_intr_handler(int irq,void * dev_id)13931 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13932 {
13933 	struct lpfc_hba  *phba;
13934 	uint32_t ha_copy;
13935 	unsigned long status;
13936 	unsigned long iflag;
13937 	struct lpfc_sli_ring *pring;
13938 
13939 	/* Get the driver's phba structure from the dev_id and
13940 	 * assume the HBA is not interrupting.
13941 	 */
13942 	phba = (struct lpfc_hba *) dev_id;
13943 
13944 	if (unlikely(!phba))
13945 		return IRQ_NONE;
13946 
13947 	/*
13948 	 * Stuff needs to be attented to when this function is invoked as an
13949 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13950 	 */
13951 	if (phba->intr_type == MSIX) {
13952 		/* Check device state for handling interrupt */
13953 		if (lpfc_intr_state_check(phba))
13954 			return IRQ_NONE;
13955 		/* Need to read HA REG for FCP ring and other ring events */
13956 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13957 			return IRQ_HANDLED;
13958 
13959 		/*
13960 		 * If there is deferred error attention, do not check for
13961 		 * any interrupt.
13962 		 */
13963 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13964 			return IRQ_NONE;
13965 
13966 		/* Clear up only attention source related to fast-path */
13967 		spin_lock_irqsave(&phba->hbalock, iflag);
13968 		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13969 			phba->HAregaddr);
13970 		readl(phba->HAregaddr); /* flush */
13971 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13972 	} else
13973 		ha_copy = phba->ha_copy;
13974 
13975 	/*
13976 	 * Process all events on FCP ring. Take the optimized path for FCP IO.
13977 	 */
13978 	ha_copy &= ~(phba->work_ha_mask);
13979 
13980 	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13981 	status >>= (4*LPFC_FCP_RING);
13982 	pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
13983 	if (status & HA_RXMASK)
13984 		lpfc_sli_handle_fast_ring_event(phba, pring, status);
13985 
13986 	if (phba->cfg_multi_ring_support == 2) {
13987 		/*
13988 		 * Process all events on extra ring. Take the optimized path
13989 		 * for extra ring IO.
13990 		 */
13991 		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13992 		status >>= (4*LPFC_EXTRA_RING);
13993 		if (status & HA_RXMASK) {
13994 			lpfc_sli_handle_fast_ring_event(phba,
13995 					&phba->sli.sli3_ring[LPFC_EXTRA_RING],
13996 					status);
13997 		}
13998 	}
13999 	return IRQ_HANDLED;
14000 }  /* lpfc_sli_fp_intr_handler */
14001 
14002 /**
14003  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
14004  * @irq: Interrupt number.
14005  * @dev_id: The device context pointer.
14006  *
14007  * This function is the HBA device-level interrupt handler to device with
14008  * SLI-3 interface spec, called from the PCI layer when either MSI or
14009  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
14010  * requires driver attention. This function invokes the slow-path interrupt
14011  * attention handling function and fast-path interrupt attention handling
14012  * function in turn to process the relevant HBA attention events. This
14013  * function is called without any lock held. It gets the hbalock to access
14014  * and update SLI data structures.
14015  *
14016  * This function returns IRQ_HANDLED when interrupt is handled, else it
14017  * returns IRQ_NONE.
14018  **/
14019 irqreturn_t
lpfc_sli_intr_handler(int irq,void * dev_id)14020 lpfc_sli_intr_handler(int irq, void *dev_id)
14021 {
14022 	struct lpfc_hba  *phba;
14023 	irqreturn_t sp_irq_rc, fp_irq_rc;
14024 	unsigned long status1, status2;
14025 	uint32_t hc_copy;
14026 
14027 	/*
14028 	 * Get the driver's phba structure from the dev_id and
14029 	 * assume the HBA is not interrupting.
14030 	 */
14031 	phba = (struct lpfc_hba *) dev_id;
14032 
14033 	if (unlikely(!phba))
14034 		return IRQ_NONE;
14035 
14036 	/* Check device state for handling interrupt */
14037 	if (lpfc_intr_state_check(phba))
14038 		return IRQ_NONE;
14039 
14040 	spin_lock(&phba->hbalock);
14041 	if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
14042 		spin_unlock(&phba->hbalock);
14043 		return IRQ_HANDLED;
14044 	}
14045 
14046 	if (unlikely(!phba->ha_copy)) {
14047 		spin_unlock(&phba->hbalock);
14048 		return IRQ_NONE;
14049 	} else if (phba->ha_copy & HA_ERATT) {
14050 		if (test_and_set_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
14051 			/* ERATT polling has handled ERATT */
14052 			phba->ha_copy &= ~HA_ERATT;
14053 	}
14054 
14055 	/*
14056 	 * If there is deferred error attention, do not check for any interrupt.
14057 	 */
14058 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
14059 		spin_unlock(&phba->hbalock);
14060 		return IRQ_NONE;
14061 	}
14062 
14063 	/* Clear attention sources except link and error attentions */
14064 	if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
14065 		spin_unlock(&phba->hbalock);
14066 		return IRQ_HANDLED;
14067 	}
14068 	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
14069 		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
14070 		phba->HCregaddr);
14071 	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
14072 	writel(hc_copy, phba->HCregaddr);
14073 	readl(phba->HAregaddr); /* flush */
14074 	spin_unlock(&phba->hbalock);
14075 
14076 	/*
14077 	 * Invokes slow-path host attention interrupt handling as appropriate.
14078 	 */
14079 
14080 	/* status of events with mailbox and link attention */
14081 	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
14082 
14083 	/* status of events with ELS ring */
14084 	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
14085 	status2 >>= (4*LPFC_ELS_RING);
14086 
14087 	if (status1 || (status2 & HA_RXMASK))
14088 		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
14089 	else
14090 		sp_irq_rc = IRQ_NONE;
14091 
14092 	/*
14093 	 * Invoke fast-path host attention interrupt handling as appropriate.
14094 	 */
14095 
14096 	/* status of events with FCP ring */
14097 	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14098 	status1 >>= (4*LPFC_FCP_RING);
14099 
14100 	/* status of events with extra ring */
14101 	if (phba->cfg_multi_ring_support == 2) {
14102 		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14103 		status2 >>= (4*LPFC_EXTRA_RING);
14104 	} else
14105 		status2 = 0;
14106 
14107 	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
14108 		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
14109 	else
14110 		fp_irq_rc = IRQ_NONE;
14111 
14112 	/* Return device-level interrupt handling status */
14113 	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
14114 }  /* lpfc_sli_intr_handler */
14115 
14116 /**
14117  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14118  * @phba: pointer to lpfc hba data structure.
14119  *
14120  * This routine is invoked by the worker thread to process all the pending
14121  * SLI4 els abort xri events.
14122  **/
lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba * phba)14123 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14124 {
14125 	struct lpfc_cq_event *cq_event;
14126 	unsigned long iflags;
14127 
14128 	/* First, declare the els xri abort event has been handled */
14129 	clear_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14130 
14131 	/* Now, handle all the els xri abort events */
14132 	spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14133 	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14134 		/* Get the first event from the head of the event queue */
14135 		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14136 				 cq_event, struct lpfc_cq_event, list);
14137 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14138 				       iflags);
14139 		/* Notify aborted XRI for ELS work queue */
14140 		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
14141 
14142 		/* Free the event processed back to the free pool */
14143 		lpfc_sli4_cq_event_release(phba, cq_event);
14144 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14145 				  iflags);
14146 	}
14147 	spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14148 }
14149 
14150 /**
14151  * lpfc_sli4_els_preprocess_rspiocbq - Get response iocbq from els wcqe
14152  * @phba: Pointer to HBA context object.
14153  * @irspiocbq: Pointer to work-queue completion queue entry.
14154  *
14155  * This routine handles an ELS work-queue completion event and construct
14156  * a pseudo response ELS IOCBQ from the SLI4 ELS WCQE for the common
14157  * discovery engine to handle.
14158  *
14159  * Return: Pointer to the receive IOCBQ, NULL otherwise.
14160  **/
14161 static struct lpfc_iocbq *
lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba * phba,struct lpfc_iocbq * irspiocbq)14162 lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
14163 				  struct lpfc_iocbq *irspiocbq)
14164 {
14165 	struct lpfc_sli_ring *pring;
14166 	struct lpfc_iocbq *cmdiocbq;
14167 	struct lpfc_wcqe_complete *wcqe;
14168 	unsigned long iflags;
14169 
14170 	pring = lpfc_phba_elsring(phba);
14171 	if (unlikely(!pring))
14172 		return NULL;
14173 
14174 	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
14175 	spin_lock_irqsave(&pring->ring_lock, iflags);
14176 	pring->stats.iocb_event++;
14177 	/* Look up the ELS command IOCB and create pseudo response IOCB */
14178 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14179 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
14180 	if (unlikely(!cmdiocbq)) {
14181 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
14182 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14183 				"0386 ELS complete with no corresponding "
14184 				"cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14185 				wcqe->word0, wcqe->total_data_placed,
14186 				wcqe->parameter, wcqe->word3);
14187 		lpfc_sli_release_iocbq(phba, irspiocbq);
14188 		return NULL;
14189 	}
14190 
14191 	memcpy(&irspiocbq->wqe, &cmdiocbq->wqe, sizeof(union lpfc_wqe128));
14192 	memcpy(&irspiocbq->wcqe_cmpl, wcqe, sizeof(*wcqe));
14193 
14194 	/* Put the iocb back on the txcmplq */
14195 	lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14196 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
14197 
14198 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14199 		spin_lock_irqsave(&phba->hbalock, iflags);
14200 		irspiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
14201 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14202 	}
14203 
14204 	return irspiocbq;
14205 }
14206 
14207 inline struct lpfc_cq_event *
lpfc_cq_event_setup(struct lpfc_hba * phba,void * entry,int size)14208 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14209 {
14210 	struct lpfc_cq_event *cq_event;
14211 
14212 	/* Allocate a new internal CQ_EVENT entry */
14213 	cq_event = lpfc_sli4_cq_event_alloc(phba);
14214 	if (!cq_event) {
14215 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14216 				"0602 Failed to alloc CQ_EVENT entry\n");
14217 		return NULL;
14218 	}
14219 
14220 	/* Move the CQE into the event */
14221 	memcpy(&cq_event->cqe, entry, size);
14222 	return cq_event;
14223 }
14224 
14225 /**
14226  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
14227  * @phba: Pointer to HBA context object.
14228  * @mcqe: Pointer to mailbox completion queue entry.
14229  *
14230  * This routine process a mailbox completion queue entry with asynchronous
14231  * event.
14232  *
14233  * Return: true if work posted to worker thread, otherwise false.
14234  **/
14235 static bool
lpfc_sli4_sp_handle_async_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14236 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14237 {
14238 	struct lpfc_cq_event *cq_event;
14239 	unsigned long iflags;
14240 
14241 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14242 			"0392 Async Event: word0:x%x, word1:x%x, "
14243 			"word2:x%x, word3:x%x\n", mcqe->word0,
14244 			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14245 
14246 	cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14247 	if (!cq_event)
14248 		return false;
14249 
14250 	spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
14251 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
14252 	spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14253 
14254 	/* Set the async event flag */
14255 	set_bit(ASYNC_EVENT, &phba->hba_flag);
14256 
14257 	return true;
14258 }
14259 
14260 /**
14261  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14262  * @phba: Pointer to HBA context object.
14263  * @mcqe: Pointer to mailbox completion queue entry.
14264  *
14265  * This routine process a mailbox completion queue entry with mailbox
14266  * completion event.
14267  *
14268  * Return: true if work posted to worker thread, otherwise false.
14269  **/
14270 static bool
lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14271 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14272 {
14273 	uint32_t mcqe_status;
14274 	MAILBOX_t *mbox, *pmbox;
14275 	struct lpfc_mqe *mqe;
14276 	struct lpfc_vport *vport;
14277 	struct lpfc_nodelist *ndlp;
14278 	struct lpfc_dmabuf *mp;
14279 	unsigned long iflags;
14280 	LPFC_MBOXQ_t *pmb;
14281 	bool workposted = false;
14282 	int rc;
14283 
14284 	/* If not a mailbox complete MCQE, out by checking mailbox consume */
14285 	if (!bf_get(lpfc_trailer_completed, mcqe))
14286 		goto out_no_mqe_complete;
14287 
14288 	/* Get the reference to the active mbox command */
14289 	spin_lock_irqsave(&phba->hbalock, iflags);
14290 	pmb = phba->sli.mbox_active;
14291 	if (unlikely(!pmb)) {
14292 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14293 				"1832 No pending MBOX command to handle\n");
14294 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14295 		goto out_no_mqe_complete;
14296 	}
14297 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14298 	mqe = &pmb->u.mqe;
14299 	pmbox = (MAILBOX_t *)&pmb->u.mqe;
14300 	mbox = phba->mbox;
14301 	vport = pmb->vport;
14302 
14303 	/* Reset heartbeat timer */
14304 	phba->last_completion_time = jiffies;
14305 	timer_delete(&phba->sli.mbox_tmo);
14306 
14307 	/* Move mbox data to caller's mailbox region, do endian swapping */
14308 	if (pmb->mbox_cmpl && mbox)
14309 		lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
14310 
14311 	/*
14312 	 * For mcqe errors, conditionally move a modified error code to
14313 	 * the mbox so that the error will not be missed.
14314 	 */
14315 	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14316 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14317 		if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14318 			bf_set(lpfc_mqe_status, mqe,
14319 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
14320 	}
14321 	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14322 		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14323 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14324 				      "MBOX dflt rpi: status:x%x rpi:x%x",
14325 				      mcqe_status,
14326 				      pmbox->un.varWords[0], 0);
14327 		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
14328 			mp = pmb->ctx_buf;
14329 			ndlp = pmb->ctx_ndlp;
14330 
14331 			/* Reg_LOGIN of dflt RPI was successful. Mark the
14332 			 * node as having an UNREG_LOGIN in progress to stop
14333 			 * an unsolicited PLOGI from the same NPortId from
14334 			 * starting another mailbox transaction.
14335 			 */
14336 			set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
14337 			lpfc_unreg_login(phba, vport->vpi,
14338 					 pmbox->un.varWords[0], pmb);
14339 			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
14340 			pmb->ctx_buf = mp;
14341 
14342 			/* No reference taken here.  This is a default
14343 			 * RPI reg/immediate unreg cycle. The reference was
14344 			 * taken in the reg rpi path and is released when
14345 			 * this mailbox completes.
14346 			 */
14347 			pmb->ctx_ndlp = ndlp;
14348 			pmb->vport = vport;
14349 			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14350 			if (rc != MBX_BUSY)
14351 				lpfc_printf_log(phba, KERN_ERR,
14352 						LOG_TRACE_EVENT,
14353 						"0385 rc should "
14354 						"have been MBX_BUSY\n");
14355 			if (rc != MBX_NOT_FINISHED)
14356 				goto send_current_mbox;
14357 		}
14358 	}
14359 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14360 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14361 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14362 
14363 	/* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14364 	if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14365 		spin_lock_irqsave(&phba->hbalock, iflags);
14366 		/* Release the mailbox command posting token */
14367 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14368 		phba->sli.mbox_active = NULL;
14369 		if (bf_get(lpfc_trailer_consumed, mcqe))
14370 			lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14371 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14372 
14373 		/* Post the next mbox command, if there is one */
14374 		lpfc_sli4_post_async_mbox(phba);
14375 
14376 		/* Process cmpl now */
14377 		if (pmb->mbox_cmpl)
14378 			pmb->mbox_cmpl(phba, pmb);
14379 		return false;
14380 	}
14381 
14382 	/* There is mailbox completion work to queue to the worker thread */
14383 	spin_lock_irqsave(&phba->hbalock, iflags);
14384 	__lpfc_mbox_cmpl_put(phba, pmb);
14385 	phba->work_ha |= HA_MBATT;
14386 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14387 	workposted = true;
14388 
14389 send_current_mbox:
14390 	spin_lock_irqsave(&phba->hbalock, iflags);
14391 	/* Release the mailbox command posting token */
14392 	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14393 	/* Setting active mailbox pointer need to be in sync to flag clear */
14394 	phba->sli.mbox_active = NULL;
14395 	if (bf_get(lpfc_trailer_consumed, mcqe))
14396 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14397 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14398 	/* Wake up worker thread to post the next pending mailbox command */
14399 	lpfc_worker_wake_up(phba);
14400 	return workposted;
14401 
14402 out_no_mqe_complete:
14403 	spin_lock_irqsave(&phba->hbalock, iflags);
14404 	if (bf_get(lpfc_trailer_consumed, mcqe))
14405 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14406 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14407 	return false;
14408 }
14409 
14410 /**
14411  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14412  * @phba: Pointer to HBA context object.
14413  * @cq: Pointer to associated CQ
14414  * @cqe: Pointer to mailbox completion queue entry.
14415  *
14416  * This routine process a mailbox completion queue entry, it invokes the
14417  * proper mailbox complete handling or asynchronous event handling routine
14418  * according to the MCQE's async bit.
14419  *
14420  * Return: true if work posted to worker thread, otherwise false.
14421  **/
14422 static bool
lpfc_sli4_sp_handle_mcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14423 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14424 			 struct lpfc_cqe *cqe)
14425 {
14426 	struct lpfc_mcqe mcqe;
14427 	bool workposted;
14428 
14429 	cq->CQ_mbox++;
14430 
14431 	/* Copy the mailbox MCQE and convert endian order as needed */
14432 	lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
14433 
14434 	/* Invoke the proper event handling routine */
14435 	if (!bf_get(lpfc_trailer_async, &mcqe))
14436 		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14437 	else
14438 		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14439 	return workposted;
14440 }
14441 
14442 /**
14443  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14444  * @phba: Pointer to HBA context object.
14445  * @cq: Pointer to associated CQ
14446  * @wcqe: Pointer to work-queue completion queue entry.
14447  *
14448  * This routine handles an ELS work-queue completion event.
14449  *
14450  * Return: true if work posted to worker thread, otherwise false.
14451  **/
14452 static bool
lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)14453 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14454 			     struct lpfc_wcqe_complete *wcqe)
14455 {
14456 	struct lpfc_iocbq *irspiocbq;
14457 	unsigned long iflags;
14458 	struct lpfc_sli_ring *pring = cq->pring;
14459 	int txq_cnt = 0;
14460 	int txcmplq_cnt = 0;
14461 
14462 	/* Check for response status */
14463 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14464 		/* Log the error status */
14465 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14466 				"0357 ELS CQE error: status=x%x: "
14467 				"CQE: %08x %08x %08x %08x\n",
14468 				bf_get(lpfc_wcqe_c_status, wcqe),
14469 				wcqe->word0, wcqe->total_data_placed,
14470 				wcqe->parameter, wcqe->word3);
14471 	}
14472 
14473 	/* Get an irspiocbq for later ELS response processing use */
14474 	irspiocbq = lpfc_sli_get_iocbq(phba);
14475 	if (!irspiocbq) {
14476 		if (!list_empty(&pring->txq))
14477 			txq_cnt++;
14478 		if (!list_empty(&pring->txcmplq))
14479 			txcmplq_cnt++;
14480 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14481 			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
14482 			"els_txcmplq_cnt=%d\n",
14483 			txq_cnt, phba->iocb_cnt,
14484 			txcmplq_cnt);
14485 		return false;
14486 	}
14487 
14488 	/* Save off the slow-path queue event for work thread to process */
14489 	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
14490 	spin_lock_irqsave(&phba->hbalock, iflags);
14491 	list_add_tail(&irspiocbq->cq_event.list,
14492 		      &phba->sli4_hba.sp_queue_event);
14493 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14494 	set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14495 
14496 	return true;
14497 }
14498 
14499 /**
14500  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14501  * @phba: Pointer to HBA context object.
14502  * @wcqe: Pointer to work-queue completion queue entry.
14503  *
14504  * This routine handles slow-path WQ entry consumed event by invoking the
14505  * proper WQ release routine to the slow-path WQ.
14506  **/
14507 static void
lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_wcqe_release * wcqe)14508 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14509 			     struct lpfc_wcqe_release *wcqe)
14510 {
14511 	/* sanity check on queue memory */
14512 	if (unlikely(!phba->sli4_hba.els_wq))
14513 		return;
14514 	/* Check for the slow-path ELS work queue */
14515 	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14516 		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14517 				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14518 	else
14519 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14520 				"2579 Slow-path wqe consume event carries "
14521 				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14522 				bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14523 				phba->sli4_hba.els_wq->queue_id);
14524 }
14525 
14526 /**
14527  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14528  * @phba: Pointer to HBA context object.
14529  * @cq: Pointer to a WQ completion queue.
14530  * @wcqe: Pointer to work-queue completion queue entry.
14531  *
14532  * This routine handles an XRI abort event.
14533  *
14534  * Return: true if work posted to worker thread, otherwise false.
14535  **/
14536 static bool
lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct sli4_wcqe_xri_aborted * wcqe)14537 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14538 				   struct lpfc_queue *cq,
14539 				   struct sli4_wcqe_xri_aborted *wcqe)
14540 {
14541 	bool workposted = false;
14542 	struct lpfc_cq_event *cq_event;
14543 	unsigned long iflags;
14544 
14545 	switch (cq->subtype) {
14546 	case LPFC_IO:
14547 		lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14548 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14549 			/* Notify aborted XRI for NVME work queue */
14550 			if (phba->nvmet_support)
14551 				lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14552 		}
14553 		workposted = false;
14554 		break;
14555 	case LPFC_NVME_LS: /* NVME LS uses ELS resources */
14556 	case LPFC_ELS:
14557 		cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14558 		if (!cq_event) {
14559 			workposted = false;
14560 			break;
14561 		}
14562 		cq_event->hdwq = cq->hdwq;
14563 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14564 				  iflags);
14565 		list_add_tail(&cq_event->list,
14566 			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14567 		/* Set the els xri abort event flag */
14568 		set_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14569 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14570 				       iflags);
14571 		workposted = true;
14572 		break;
14573 	default:
14574 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14575 				"0603 Invalid CQ subtype %d: "
14576 				"%08x %08x %08x %08x\n",
14577 				cq->subtype, wcqe->word0, wcqe->parameter,
14578 				wcqe->word2, wcqe->word3);
14579 		workposted = false;
14580 		break;
14581 	}
14582 	return workposted;
14583 }
14584 
14585 #define FC_RCTL_MDS_DIAGS	0xF4
14586 
14587 /**
14588  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
14589  * @phba: Pointer to HBA context object.
14590  * @rcqe: Pointer to receive-queue completion queue entry.
14591  *
14592  * This routine process a receive-queue completion queue entry.
14593  *
14594  * Return: true if work posted to worker thread, otherwise false.
14595  **/
14596 static bool
lpfc_sli4_sp_handle_rcqe(struct lpfc_hba * phba,struct lpfc_rcqe * rcqe)14597 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14598 {
14599 	bool workposted = false;
14600 	struct fc_frame_header *fc_hdr;
14601 	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14602 	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
14603 	struct lpfc_nvmet_tgtport *tgtp;
14604 	struct hbq_dmabuf *dma_buf;
14605 	uint32_t status, rq_id;
14606 	unsigned long iflags;
14607 
14608 	/* sanity check on queue memory */
14609 	if (unlikely(!hrq) || unlikely(!drq))
14610 		return workposted;
14611 
14612 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14613 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14614 	else
14615 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14616 	if (rq_id != hrq->queue_id)
14617 		goto out;
14618 
14619 	status = bf_get(lpfc_rcqe_status, rcqe);
14620 	switch (status) {
14621 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14622 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14623 				"2537 Receive Frame Truncated!!\n");
14624 		fallthrough;
14625 	case FC_STATUS_RQ_SUCCESS:
14626 		spin_lock_irqsave(&phba->hbalock, iflags);
14627 		lpfc_sli4_rq_release(hrq, drq);
14628 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14629 		if (!dma_buf) {
14630 			hrq->RQ_no_buf_found++;
14631 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14632 			goto out;
14633 		}
14634 		hrq->RQ_rcv_buf++;
14635 		hrq->RQ_buf_posted--;
14636 		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
14637 
14638 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14639 
14640 		if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14641 		    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14642 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14643 			/* Handle MDS Loopback frames */
14644 			if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
14645 				lpfc_sli4_handle_mds_loopback(phba->pport,
14646 							      dma_buf);
14647 			else
14648 				lpfc_in_buf_free(phba, &dma_buf->dbuf);
14649 			break;
14650 		}
14651 
14652 		/* save off the frame for the work thread to process */
14653 		list_add_tail(&dma_buf->cq_event.list,
14654 			      &phba->sli4_hba.sp_queue_event);
14655 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14656 		/* Frame received */
14657 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14658 		workposted = true;
14659 		break;
14660 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
14661 		if (phba->nvmet_support) {
14662 			tgtp = phba->targetport->private;
14663 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14664 					"6402 RQE Error x%x, posted %d err_cnt "
14665 					"%d: %x %x %x\n",
14666 					status, hrq->RQ_buf_posted,
14667 					hrq->RQ_no_posted_buf,
14668 					atomic_read(&tgtp->rcv_fcp_cmd_in),
14669 					atomic_read(&tgtp->rcv_fcp_cmd_out),
14670 					atomic_read(&tgtp->xmt_fcp_release));
14671 		}
14672 		fallthrough;
14673 
14674 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
14675 		hrq->RQ_no_posted_buf++;
14676 		/* Post more buffers if possible */
14677 		set_bit(HBA_POST_RECEIVE_BUFFER, &phba->hba_flag);
14678 		workposted = true;
14679 		break;
14680 	case FC_STATUS_RQ_DMA_FAILURE:
14681 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14682 				"2564 RQE DMA Error x%x, x%08x x%08x x%08x "
14683 				"x%08x\n",
14684 				status, rcqe->word0, rcqe->word1,
14685 				rcqe->word2, rcqe->word3);
14686 
14687 		/* If IV set, no further recovery */
14688 		if (bf_get(lpfc_rcqe_iv, rcqe))
14689 			break;
14690 
14691 		/* recycle consumed resource */
14692 		spin_lock_irqsave(&phba->hbalock, iflags);
14693 		lpfc_sli4_rq_release(hrq, drq);
14694 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14695 		if (!dma_buf) {
14696 			hrq->RQ_no_buf_found++;
14697 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14698 			break;
14699 		}
14700 		hrq->RQ_rcv_buf++;
14701 		hrq->RQ_buf_posted--;
14702 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14703 		lpfc_in_buf_free(phba, &dma_buf->dbuf);
14704 		break;
14705 	default:
14706 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14707 				"2565 Unexpected RQE Status x%x, w0-3 x%08x "
14708 				"x%08x x%08x x%08x\n",
14709 				status, rcqe->word0, rcqe->word1,
14710 				rcqe->word2, rcqe->word3);
14711 		break;
14712 	}
14713 out:
14714 	return workposted;
14715 }
14716 
14717 /**
14718  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14719  * @phba: Pointer to HBA context object.
14720  * @cq: Pointer to the completion queue.
14721  * @cqe: Pointer to a completion queue entry.
14722  *
14723  * This routine process a slow-path work-queue or receive queue completion queue
14724  * entry.
14725  *
14726  * Return: true if work posted to worker thread, otherwise false.
14727  **/
14728 static bool
lpfc_sli4_sp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14729 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14730 			 struct lpfc_cqe *cqe)
14731 {
14732 	struct lpfc_cqe cqevt;
14733 	bool workposted = false;
14734 
14735 	/* Copy the work queue CQE and convert endian order if needed */
14736 	lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
14737 
14738 	/* Check and process for different type of WCQE and dispatch */
14739 	switch (bf_get(lpfc_cqe_code, &cqevt)) {
14740 	case CQE_CODE_COMPL_WQE:
14741 		/* Process the WQ/RQ complete event */
14742 		phba->last_completion_time = jiffies;
14743 		workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
14744 				(struct lpfc_wcqe_complete *)&cqevt);
14745 		break;
14746 	case CQE_CODE_RELEASE_WQE:
14747 		/* Process the WQ release event */
14748 		lpfc_sli4_sp_handle_rel_wcqe(phba,
14749 				(struct lpfc_wcqe_release *)&cqevt);
14750 		break;
14751 	case CQE_CODE_XRI_ABORTED:
14752 		/* Process the WQ XRI abort event */
14753 		phba->last_completion_time = jiffies;
14754 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14755 				(struct sli4_wcqe_xri_aborted *)&cqevt);
14756 		break;
14757 	case CQE_CODE_RECEIVE:
14758 	case CQE_CODE_RECEIVE_V1:
14759 		/* Process the RQ event */
14760 		phba->last_completion_time = jiffies;
14761 		workposted = lpfc_sli4_sp_handle_rcqe(phba,
14762 				(struct lpfc_rcqe *)&cqevt);
14763 		break;
14764 	default:
14765 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14766 				"0388 Not a valid WCQE code: x%x\n",
14767 				bf_get(lpfc_cqe_code, &cqevt));
14768 		break;
14769 	}
14770 	return workposted;
14771 }
14772 
14773 /**
14774  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14775  * @phba: Pointer to HBA context object.
14776  * @eqe: Pointer to fast-path event queue entry.
14777  * @speq: Pointer to slow-path event queue.
14778  *
14779  * This routine process a event queue entry from the slow-path event queue.
14780  * It will check the MajorCode and MinorCode to determine this is for a
14781  * completion event on a completion queue, if not, an error shall be logged
14782  * and just return. Otherwise, it will get to the corresponding completion
14783  * queue and process all the entries on that completion queue, rearm the
14784  * completion queue, and then return.
14785  *
14786  **/
14787 static void
lpfc_sli4_sp_handle_eqe(struct lpfc_hba * phba,struct lpfc_eqe * eqe,struct lpfc_queue * speq)14788 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14789 	struct lpfc_queue *speq)
14790 {
14791 	struct lpfc_queue *cq = NULL, *childq;
14792 	uint16_t cqid;
14793 	int ret = 0;
14794 
14795 	/* Get the reference to the corresponding CQ */
14796 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14797 
14798 	list_for_each_entry(childq, &speq->child_list, list) {
14799 		if (childq->queue_id == cqid) {
14800 			cq = childq;
14801 			break;
14802 		}
14803 	}
14804 	if (unlikely(!cq)) {
14805 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14806 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14807 					"0365 Slow-path CQ identifier "
14808 					"(%d) does not exist\n", cqid);
14809 		return;
14810 	}
14811 
14812 	/* Save EQ associated with this CQ */
14813 	cq->assoc_qp = speq;
14814 
14815 	if (is_kdump_kernel())
14816 		ret = queue_work(phba->wq, &cq->spwork);
14817 	else
14818 		ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14819 
14820 	if (!ret)
14821 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14822 				"0390 Cannot schedule queue work "
14823 				"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14824 				cqid, cq->queue_id, raw_smp_processor_id());
14825 }
14826 
14827 /**
14828  * __lpfc_sli4_process_cq - Process elements of a CQ
14829  * @phba: Pointer to HBA context object.
14830  * @cq: Pointer to CQ to be processed
14831  * @handler: Routine to process each cqe
14832  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14833  *
14834  * This routine processes completion queue entries in a CQ. While a valid
14835  * queue element is found, the handler is called. During processing checks
14836  * are made for periodic doorbell writes to let the hardware know of
14837  * element consumption.
14838  *
14839  * If the max limit on cqes to process is hit, or there are no more valid
14840  * entries, the loop stops. If we processed a sufficient number of elements,
14841  * meaning there is sufficient load, rather than rearming and generating
14842  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14843  * indicates no rescheduling.
14844  *
14845  * Returns True if work scheduled, False otherwise.
14846  **/
14847 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)14848 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14849 	bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14850 			struct lpfc_cqe *), unsigned long *delay)
14851 {
14852 	struct lpfc_cqe *cqe;
14853 	bool workposted = false;
14854 	int count = 0, consumed = 0;
14855 	bool arm = true;
14856 
14857 	/* default - no reschedule */
14858 	*delay = 0;
14859 
14860 	if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14861 		goto rearm_and_exit;
14862 
14863 	/* Process all the entries to the CQ */
14864 	cq->q_flag = 0;
14865 	cqe = lpfc_sli4_cq_get(cq);
14866 	while (cqe) {
14867 		workposted |= handler(phba, cq, cqe);
14868 		__lpfc_sli4_consume_cqe(phba, cq, cqe);
14869 
14870 		consumed++;
14871 		if (!(++count % cq->max_proc_limit))
14872 			break;
14873 
14874 		if (!(count % cq->notify_interval)) {
14875 			phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14876 						LPFC_QUEUE_NOARM);
14877 			consumed = 0;
14878 			cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14879 		}
14880 
14881 		if (count == LPFC_NVMET_CQ_NOTIFY)
14882 			cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14883 
14884 		cqe = lpfc_sli4_cq_get(cq);
14885 	}
14886 	if (count >= phba->cfg_cq_poll_threshold) {
14887 		*delay = 1;
14888 		arm = false;
14889 	}
14890 
14891 	/* Track the max number of CQEs processed in 1 EQ */
14892 	if (count > cq->CQ_max_cqe)
14893 		cq->CQ_max_cqe = count;
14894 
14895 	cq->assoc_qp->EQ_cqe_cnt += count;
14896 
14897 	/* Catch the no cq entry condition */
14898 	if (unlikely(count == 0))
14899 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14900 				"0369 No entry from completion queue "
14901 				"qid=%d\n", cq->queue_id);
14902 
14903 	xchg(&cq->queue_claimed, 0);
14904 
14905 rearm_and_exit:
14906 	phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14907 			arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14908 
14909 	return workposted;
14910 }
14911 
14912 /**
14913  * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14914  * @cq: pointer to CQ to process
14915  *
14916  * This routine calls the cq processing routine with a handler specific
14917  * to the type of queue bound to it.
14918  *
14919  * The CQ routine returns two values: the first is the calling status,
14920  * which indicates whether work was queued to the  background discovery
14921  * thread. If true, the routine should wakeup the discovery thread;
14922  * the second is the delay parameter. If non-zero, rather than rearming
14923  * the CQ and yet another interrupt, the CQ handler should be queued so
14924  * that it is processed in a subsequent polling action. The value of
14925  * the delay indicates when to reschedule it.
14926  **/
14927 static void
__lpfc_sli4_sp_process_cq(struct lpfc_queue * cq)14928 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14929 {
14930 	struct lpfc_hba *phba = cq->phba;
14931 	unsigned long delay;
14932 	bool workposted = false;
14933 	int ret = 0;
14934 
14935 	/* Process and rearm the CQ */
14936 	switch (cq->type) {
14937 	case LPFC_MCQ:
14938 		workposted |= __lpfc_sli4_process_cq(phba, cq,
14939 						lpfc_sli4_sp_handle_mcqe,
14940 						&delay);
14941 		break;
14942 	case LPFC_WCQ:
14943 		if (cq->subtype == LPFC_IO)
14944 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14945 						lpfc_sli4_fp_handle_cqe,
14946 						&delay);
14947 		else
14948 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14949 						lpfc_sli4_sp_handle_cqe,
14950 						&delay);
14951 		break;
14952 	default:
14953 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14954 				"0370 Invalid completion queue type (%d)\n",
14955 				cq->type);
14956 		return;
14957 	}
14958 
14959 	if (delay) {
14960 		if (is_kdump_kernel())
14961 			ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14962 						delay);
14963 		else
14964 			ret = queue_delayed_work_on(cq->chann, phba->wq,
14965 						&cq->sched_spwork, delay);
14966 		if (!ret)
14967 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14968 				"0394 Cannot schedule queue work "
14969 				"for cqid=%d on CPU %d\n",
14970 				cq->queue_id, cq->chann);
14971 	}
14972 
14973 	/* wake up worker thread if there are works to be done */
14974 	if (workposted)
14975 		lpfc_worker_wake_up(phba);
14976 }
14977 
14978 /**
14979  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14980  *   interrupt
14981  * @work: pointer to work element
14982  *
14983  * translates from the work handler and calls the slow-path handler.
14984  **/
14985 static void
lpfc_sli4_sp_process_cq(struct work_struct * work)14986 lpfc_sli4_sp_process_cq(struct work_struct *work)
14987 {
14988 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14989 
14990 	__lpfc_sli4_sp_process_cq(cq);
14991 }
14992 
14993 /**
14994  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
14995  * @work: pointer to work element
14996  *
14997  * translates from the work handler and calls the slow-path handler.
14998  **/
14999 static void
lpfc_sli4_dly_sp_process_cq(struct work_struct * work)15000 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
15001 {
15002 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15003 					struct lpfc_queue, sched_spwork);
15004 
15005 	__lpfc_sli4_sp_process_cq(cq);
15006 }
15007 
15008 /**
15009  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
15010  * @phba: Pointer to HBA context object.
15011  * @cq: Pointer to associated CQ
15012  * @wcqe: Pointer to work-queue completion queue entry.
15013  *
15014  * This routine process a fast-path work queue completion entry from fast-path
15015  * event queue for FCP command response completion.
15016  **/
15017 static void
lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)15018 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15019 			     struct lpfc_wcqe_complete *wcqe)
15020 {
15021 	struct lpfc_sli_ring *pring = cq->pring;
15022 	struct lpfc_iocbq *cmdiocbq;
15023 	unsigned long iflags;
15024 
15025 	/* Check for response status */
15026 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15027 		/* If resource errors reported from HBA, reduce queue
15028 		 * depth of the SCSI device.
15029 		 */
15030 		if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15031 		     IOSTAT_LOCAL_REJECT)) &&
15032 		    ((wcqe->parameter & IOERR_PARAM_MASK) ==
15033 		     IOERR_NO_RESOURCES))
15034 			phba->lpfc_rampdown_queue_depth(phba);
15035 
15036 		/* Log the cmpl status */
15037 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15038 				"0373 FCP CQE cmpl: status=x%x: "
15039 				"CQE: %08x %08x %08x %08x\n",
15040 				bf_get(lpfc_wcqe_c_status, wcqe),
15041 				wcqe->word0, wcqe->total_data_placed,
15042 				wcqe->parameter, wcqe->word3);
15043 	}
15044 
15045 	/* Look up the FCP command IOCB and create pseudo response IOCB */
15046 	spin_lock_irqsave(&pring->ring_lock, iflags);
15047 	pring->stats.iocb_event++;
15048 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15049 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15050 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
15051 	if (unlikely(!cmdiocbq)) {
15052 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15053 				"0374 FCP complete with no corresponding "
15054 				"cmdiocb: iotag (%d)\n",
15055 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15056 		return;
15057 	}
15058 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15059 	cmdiocbq->isr_timestamp = cq->isr_timestamp;
15060 #endif
15061 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
15062 		spin_lock_irqsave(&phba->hbalock, iflags);
15063 		cmdiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
15064 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15065 	}
15066 
15067 	if (cmdiocbq->cmd_cmpl) {
15068 		/* For FCP the flag is cleared in cmd_cmpl */
15069 		if (!(cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
15070 		    cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED) {
15071 			spin_lock_irqsave(&phba->hbalock, iflags);
15072 			cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
15073 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15074 		}
15075 
15076 		/* Pass the cmd_iocb and the wcqe to the upper layer */
15077 		memcpy(&cmdiocbq->wcqe_cmpl, wcqe,
15078 		       sizeof(struct lpfc_wcqe_complete));
15079 		cmdiocbq->cmd_cmpl(phba, cmdiocbq, cmdiocbq);
15080 	} else {
15081 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15082 				"0375 FCP cmdiocb not callback function "
15083 				"iotag: (%d)\n",
15084 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15085 	}
15086 }
15087 
15088 /**
15089  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15090  * @phba: Pointer to HBA context object.
15091  * @cq: Pointer to completion queue.
15092  * @wcqe: Pointer to work-queue completion queue entry.
15093  *
15094  * This routine handles an fast-path WQ entry consumed event by invoking the
15095  * proper WQ release routine to the slow-path WQ.
15096  **/
15097 static void
lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_release * wcqe)15098 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15099 			     struct lpfc_wcqe_release *wcqe)
15100 {
15101 	struct lpfc_queue *childwq;
15102 	bool wqid_matched = false;
15103 	uint16_t hba_wqid;
15104 
15105 	/* Check for fast-path FCP work queue release */
15106 	hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
15107 	list_for_each_entry(childwq, &cq->child_list, list) {
15108 		if (childwq->queue_id == hba_wqid) {
15109 			lpfc_sli4_wq_release(childwq,
15110 					bf_get(lpfc_wcqe_r_wqe_index, wcqe));
15111 			if (childwq->q_flag & HBA_NVMET_WQFULL)
15112 				lpfc_nvmet_wqfull_process(phba, childwq);
15113 			wqid_matched = true;
15114 			break;
15115 		}
15116 	}
15117 	/* Report warning log message if no match found */
15118 	if (wqid_matched != true)
15119 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15120 				"2580 Fast-path wqe consume event carries "
15121 				"miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
15122 }
15123 
15124 /**
15125  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15126  * @phba: Pointer to HBA context object.
15127  * @cq: Pointer to completion queue.
15128  * @rcqe: Pointer to receive-queue completion queue entry.
15129  *
15130  * This routine process a receive-queue completion queue entry.
15131  *
15132  * Return: true if work posted to worker thread, otherwise false.
15133  **/
15134 static bool
lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_rcqe * rcqe)15135 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15136 			    struct lpfc_rcqe *rcqe)
15137 {
15138 	bool workposted = false;
15139 	struct lpfc_queue *hrq;
15140 	struct lpfc_queue *drq;
15141 	struct rqb_dmabuf *dma_buf;
15142 	struct fc_frame_header *fc_hdr;
15143 	struct lpfc_nvmet_tgtport *tgtp;
15144 	uint32_t status, rq_id;
15145 	unsigned long iflags;
15146 	uint32_t fctl, idx;
15147 
15148 	if ((phba->nvmet_support == 0) ||
15149 	    (phba->sli4_hba.nvmet_cqset == NULL))
15150 		return workposted;
15151 
15152 	idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15153 	hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15154 	drq = phba->sli4_hba.nvmet_mrq_data[idx];
15155 
15156 	/* sanity check on queue memory */
15157 	if (unlikely(!hrq) || unlikely(!drq))
15158 		return workposted;
15159 
15160 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15161 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15162 	else
15163 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15164 
15165 	if ((phba->nvmet_support == 0) ||
15166 	    (rq_id != hrq->queue_id))
15167 		return workposted;
15168 
15169 	status = bf_get(lpfc_rcqe_status, rcqe);
15170 	switch (status) {
15171 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
15172 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15173 				"6126 Receive Frame Truncated!!\n");
15174 		fallthrough;
15175 	case FC_STATUS_RQ_SUCCESS:
15176 		spin_lock_irqsave(&phba->hbalock, iflags);
15177 		lpfc_sli4_rq_release(hrq, drq);
15178 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15179 		if (!dma_buf) {
15180 			hrq->RQ_no_buf_found++;
15181 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15182 			goto out;
15183 		}
15184 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15185 		hrq->RQ_rcv_buf++;
15186 		hrq->RQ_buf_posted--;
15187 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15188 
15189 		/* Just some basic sanity checks on FCP Command frame */
15190 		fctl = (fc_hdr->fh_f_ctl[0] << 16 |
15191 			fc_hdr->fh_f_ctl[1] << 8 |
15192 			fc_hdr->fh_f_ctl[2]);
15193 		if (((fctl &
15194 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15195 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15196 		    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15197 			goto drop;
15198 
15199 		if (fc_hdr->fh_type == FC_TYPE_FCP) {
15200 			dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
15201 			lpfc_nvmet_unsol_fcp_event(
15202 				phba, idx, dma_buf, cq->isr_timestamp,
15203 				cq->q_flag & HBA_NVMET_CQ_NOTIFY);
15204 			return false;
15205 		}
15206 drop:
15207 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15208 		break;
15209 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
15210 		if (phba->nvmet_support) {
15211 			tgtp = phba->targetport->private;
15212 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15213 					"6401 RQE Error x%x, posted %d err_cnt "
15214 					"%d: %x %x %x\n",
15215 					status, hrq->RQ_buf_posted,
15216 					hrq->RQ_no_posted_buf,
15217 					atomic_read(&tgtp->rcv_fcp_cmd_in),
15218 					atomic_read(&tgtp->rcv_fcp_cmd_out),
15219 					atomic_read(&tgtp->xmt_fcp_release));
15220 		}
15221 		fallthrough;
15222 
15223 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
15224 		hrq->RQ_no_posted_buf++;
15225 		/* Post more buffers if possible */
15226 		break;
15227 	case FC_STATUS_RQ_DMA_FAILURE:
15228 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15229 				"2575 RQE DMA Error x%x, x%08x x%08x x%08x "
15230 				"x%08x\n",
15231 				status, rcqe->word0, rcqe->word1,
15232 				rcqe->word2, rcqe->word3);
15233 
15234 		/* If IV set, no further recovery */
15235 		if (bf_get(lpfc_rcqe_iv, rcqe))
15236 			break;
15237 
15238 		/* recycle consumed resource */
15239 		spin_lock_irqsave(&phba->hbalock, iflags);
15240 		lpfc_sli4_rq_release(hrq, drq);
15241 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15242 		if (!dma_buf) {
15243 			hrq->RQ_no_buf_found++;
15244 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15245 			break;
15246 		}
15247 		hrq->RQ_rcv_buf++;
15248 		hrq->RQ_buf_posted--;
15249 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15250 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15251 		break;
15252 	default:
15253 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15254 				"2576 Unexpected RQE Status x%x, w0-3 x%08x "
15255 				"x%08x x%08x x%08x\n",
15256 				status, rcqe->word0, rcqe->word1,
15257 				rcqe->word2, rcqe->word3);
15258 		break;
15259 	}
15260 out:
15261 	return workposted;
15262 }
15263 
15264 /**
15265  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
15266  * @phba: adapter with cq
15267  * @cq: Pointer to the completion queue.
15268  * @cqe: Pointer to fast-path completion queue entry.
15269  *
15270  * This routine process a fast-path work queue completion entry from fast-path
15271  * event queue for FCP command response completion.
15272  *
15273  * Return: true if work posted to worker thread, otherwise false.
15274  **/
15275 static bool
lpfc_sli4_fp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)15276 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15277 			 struct lpfc_cqe *cqe)
15278 {
15279 	struct lpfc_wcqe_release wcqe;
15280 	bool workposted = false;
15281 
15282 	/* Copy the work queue CQE and convert endian order if needed */
15283 	lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
15284 
15285 	/* Check and process for different type of WCQE and dispatch */
15286 	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15287 	case CQE_CODE_COMPL_WQE:
15288 	case CQE_CODE_NVME_ERSP:
15289 		cq->CQ_wq++;
15290 		/* Process the WQ complete event */
15291 		phba->last_completion_time = jiffies;
15292 		if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
15293 			lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
15294 				(struct lpfc_wcqe_complete *)&wcqe);
15295 		break;
15296 	case CQE_CODE_RELEASE_WQE:
15297 		cq->CQ_release_wqe++;
15298 		/* Process the WQ release event */
15299 		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15300 				(struct lpfc_wcqe_release *)&wcqe);
15301 		break;
15302 	case CQE_CODE_XRI_ABORTED:
15303 		cq->CQ_xri_aborted++;
15304 		/* Process the WQ XRI abort event */
15305 		phba->last_completion_time = jiffies;
15306 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15307 				(struct sli4_wcqe_xri_aborted *)&wcqe);
15308 		break;
15309 	case CQE_CODE_RECEIVE_V1:
15310 	case CQE_CODE_RECEIVE:
15311 		phba->last_completion_time = jiffies;
15312 		if (cq->subtype == LPFC_NVMET) {
15313 			workposted = lpfc_sli4_nvmet_handle_rcqe(
15314 				phba, cq, (struct lpfc_rcqe *)&wcqe);
15315 		}
15316 		break;
15317 	default:
15318 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15319 				"0144 Not a valid CQE code: x%x\n",
15320 				bf_get(lpfc_wcqe_c_code, &wcqe));
15321 		break;
15322 	}
15323 	return workposted;
15324 }
15325 
15326 /**
15327  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15328  * @cq: Pointer to CQ to be processed
15329  *
15330  * This routine calls the cq processing routine with the handler for
15331  * fast path CQEs.
15332  *
15333  * The CQ routine returns two values: the first is the calling status,
15334  * which indicates whether work was queued to the  background discovery
15335  * thread. If true, the routine should wakeup the discovery thread;
15336  * the second is the delay parameter. If non-zero, rather than rearming
15337  * the CQ and yet another interrupt, the CQ handler should be queued so
15338  * that it is processed in a subsequent polling action. The value of
15339  * the delay indicates when to reschedule it.
15340  **/
15341 static void
__lpfc_sli4_hba_process_cq(struct lpfc_queue * cq)15342 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
15343 {
15344 	struct lpfc_hba *phba = cq->phba;
15345 	unsigned long delay;
15346 	bool workposted = false;
15347 	int ret;
15348 
15349 	/* process and rearm the CQ */
15350 	workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
15351 					     &delay);
15352 
15353 	if (delay) {
15354 		if (is_kdump_kernel())
15355 			ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15356 						delay);
15357 		else
15358 			ret = queue_delayed_work_on(cq->chann, phba->wq,
15359 						&cq->sched_irqwork, delay);
15360 		if (!ret)
15361 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15362 					"0367 Cannot schedule queue work "
15363 					"for cqid=%d on CPU %d\n",
15364 					cq->queue_id, cq->chann);
15365 	}
15366 
15367 	/* wake up worker thread if there are works to be done */
15368 	if (workposted)
15369 		lpfc_worker_wake_up(phba);
15370 }
15371 
15372 /**
15373  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15374  *   interrupt
15375  * @work: pointer to work element
15376  *
15377  * translates from the work handler and calls the fast-path handler.
15378  **/
15379 static void
lpfc_sli4_hba_process_cq(struct work_struct * work)15380 lpfc_sli4_hba_process_cq(struct work_struct *work)
15381 {
15382 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
15383 
15384 	__lpfc_sli4_hba_process_cq(cq);
15385 }
15386 
15387 /**
15388  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
15389  * @phba: Pointer to HBA context object.
15390  * @eq: Pointer to the queue structure.
15391  * @eqe: Pointer to fast-path event queue entry.
15392  * @poll_mode: poll_mode to execute processing the cq.
15393  *
15394  * This routine process a event queue entry from the fast-path event queue.
15395  * It will check the MajorCode and MinorCode to determine this is for a
15396  * completion event on a completion queue, if not, an error shall be logged
15397  * and just return. Otherwise, it will get to the corresponding completion
15398  * queue and process all the entries on the completion queue, rearm the
15399  * completion queue, and then return.
15400  **/
15401 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)15402 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15403 			 struct lpfc_eqe *eqe, enum lpfc_poll_mode poll_mode)
15404 {
15405 	struct lpfc_queue *cq = NULL;
15406 	uint32_t qidx = eq->hdwq;
15407 	uint16_t cqid, id;
15408 	int ret;
15409 
15410 	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
15411 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15412 				"0366 Not a valid completion "
15413 				"event: majorcode=x%x, minorcode=x%x\n",
15414 				bf_get_le32(lpfc_eqe_major_code, eqe),
15415 				bf_get_le32(lpfc_eqe_minor_code, eqe));
15416 		return;
15417 	}
15418 
15419 	/* Get the reference to the corresponding CQ */
15420 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15421 
15422 	/* Use the fast lookup method first */
15423 	if (cqid <= phba->sli4_hba.cq_max) {
15424 		cq = phba->sli4_hba.cq_lookup[cqid];
15425 		if (cq)
15426 			goto  work_cq;
15427 	}
15428 
15429 	/* Next check for NVMET completion */
15430 	if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15431 		id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15432 		if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15433 			/* Process NVMET unsol rcv */
15434 			cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15435 			goto  process_cq;
15436 		}
15437 	}
15438 
15439 	if (phba->sli4_hba.nvmels_cq &&
15440 	    (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15441 		/* Process NVME unsol rcv */
15442 		cq = phba->sli4_hba.nvmels_cq;
15443 	}
15444 
15445 	/* Otherwise this is a Slow path event */
15446 	if (cq == NULL) {
15447 		lpfc_sli4_sp_handle_eqe(phba, eqe,
15448 					phba->sli4_hba.hdwq[qidx].hba_eq);
15449 		return;
15450 	}
15451 
15452 process_cq:
15453 	if (unlikely(cqid != cq->queue_id)) {
15454 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15455 				"0368 Miss-matched fast-path completion "
15456 				"queue identifier: eqcqid=%d, fcpcqid=%d\n",
15457 				cqid, cq->queue_id);
15458 		return;
15459 	}
15460 
15461 work_cq:
15462 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15463 	if (phba->ktime_on)
15464 		cq->isr_timestamp = ktime_get_ns();
15465 	else
15466 		cq->isr_timestamp = 0;
15467 #endif
15468 
15469 	switch (poll_mode) {
15470 	case LPFC_THREADED_IRQ:
15471 		__lpfc_sli4_hba_process_cq(cq);
15472 		break;
15473 	case LPFC_QUEUE_WORK:
15474 	default:
15475 		if (is_kdump_kernel())
15476 			ret = queue_work(phba->wq, &cq->irqwork);
15477 		else
15478 			ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15479 		if (!ret)
15480 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15481 					"0383 Cannot schedule queue work "
15482 					"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15483 					cqid, cq->queue_id,
15484 					raw_smp_processor_id());
15485 		break;
15486 	}
15487 }
15488 
15489 /**
15490  * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
15491  * @work: pointer to work element
15492  *
15493  * translates from the work handler and calls the fast-path handler.
15494  **/
15495 static void
lpfc_sli4_dly_hba_process_cq(struct work_struct * work)15496 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
15497 {
15498 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15499 					struct lpfc_queue, sched_irqwork);
15500 
15501 	__lpfc_sli4_hba_process_cq(cq);
15502 }
15503 
15504 /**
15505  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
15506  * @irq: Interrupt number.
15507  * @dev_id: The device context pointer.
15508  *
15509  * This function is directly called from the PCI layer as an interrupt
15510  * service routine when device with SLI-4 interface spec is enabled with
15511  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15512  * ring event in the HBA. However, when the device is enabled with either
15513  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15514  * device-level interrupt handler. When the PCI slot is in error recovery
15515  * or the HBA is undergoing initialization, the interrupt handler will not
15516  * process the interrupt. The SCSI FCP fast-path ring event are handled in
15517  * the intrrupt context. This function is called without any lock held.
15518  * It gets the hbalock to access and update SLI data structures. Note that,
15519  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15520  * equal to that of FCP CQ index.
15521  *
15522  * The link attention and ELS ring attention events are handled
15523  * by the worker thread. The interrupt handler signals the worker thread
15524  * and returns for these events. This function is called without any lock
15525  * held. It gets the hbalock to access and update SLI data structures.
15526  *
15527  * This function returns IRQ_HANDLED when interrupt is handled, IRQ_WAKE_THREAD
15528  * when interrupt is scheduled to be handled from a threaded irq context, or
15529  * else returns IRQ_NONE.
15530  **/
15531 irqreturn_t
lpfc_sli4_hba_intr_handler(int irq,void * dev_id)15532 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
15533 {
15534 	struct lpfc_hba *phba;
15535 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
15536 	struct lpfc_queue *fpeq;
15537 	unsigned long iflag;
15538 	int hba_eqidx;
15539 	int ecount = 0;
15540 	struct lpfc_eq_intr_info *eqi;
15541 
15542 	/* Get the driver's phba structure from the dev_id */
15543 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15544 	phba = hba_eq_hdl->phba;
15545 	hba_eqidx = hba_eq_hdl->idx;
15546 
15547 	if (unlikely(!phba))
15548 		return IRQ_NONE;
15549 	if (unlikely(!phba->sli4_hba.hdwq))
15550 		return IRQ_NONE;
15551 
15552 	/* Get to the EQ struct associated with this vector */
15553 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
15554 	if (unlikely(!fpeq))
15555 		return IRQ_NONE;
15556 
15557 	/* Check device state for handling interrupt */
15558 	if (unlikely(lpfc_intr_state_check(phba))) {
15559 		/* Check again for link_state with lock held */
15560 		spin_lock_irqsave(&phba->hbalock, iflag);
15561 		if (phba->link_state < LPFC_LINK_DOWN)
15562 			/* Flush, clear interrupt, and rearm the EQ */
15563 			lpfc_sli4_eqcq_flush(phba, fpeq);
15564 		spin_unlock_irqrestore(&phba->hbalock, iflag);
15565 		return IRQ_NONE;
15566 	}
15567 
15568 	switch (fpeq->poll_mode) {
15569 	case LPFC_THREADED_IRQ:
15570 		/* CGN mgmt is mutually exclusive from irq processing */
15571 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
15572 			return IRQ_WAKE_THREAD;
15573 		fallthrough;
15574 	case LPFC_QUEUE_WORK:
15575 	default:
15576 		eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15577 		eqi->icnt++;
15578 
15579 		fpeq->last_cpu = raw_smp_processor_id();
15580 
15581 		if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
15582 		    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
15583 		    phba->cfg_auto_imax &&
15584 		    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15585 		    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15586 			lpfc_sli4_mod_hba_eq_delay(phba, fpeq,
15587 						   LPFC_MAX_AUTO_EQ_DELAY);
15588 
15589 		/* process and rearm the EQ */
15590 		ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
15591 					      LPFC_QUEUE_WORK);
15592 
15593 		if (unlikely(ecount == 0)) {
15594 			fpeq->EQ_no_entry++;
15595 			if (phba->intr_type == MSIX)
15596 				/* MSI-X treated interrupt served as no EQ share INT */
15597 				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15598 						"0358 MSI-X interrupt with no EQE\n");
15599 			else
15600 				/* Non MSI-X treated on interrupt as EQ share INT */
15601 				return IRQ_NONE;
15602 		}
15603 	}
15604 
15605 	return IRQ_HANDLED;
15606 } /* lpfc_sli4_hba_intr_handler */
15607 
15608 /**
15609  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15610  * @irq: Interrupt number.
15611  * @dev_id: The device context pointer.
15612  *
15613  * This function is the device-level interrupt handler to device with SLI-4
15614  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15615  * interrupt mode is enabled and there is an event in the HBA which requires
15616  * driver attention. This function invokes the slow-path interrupt attention
15617  * handling function and fast-path interrupt attention handling function in
15618  * turn to process the relevant HBA attention events. This function is called
15619  * without any lock held. It gets the hbalock to access and update SLI data
15620  * structures.
15621  *
15622  * This function returns IRQ_HANDLED when interrupt is handled, else it
15623  * returns IRQ_NONE.
15624  **/
15625 irqreturn_t
lpfc_sli4_intr_handler(int irq,void * dev_id)15626 lpfc_sli4_intr_handler(int irq, void *dev_id)
15627 {
15628 	struct lpfc_hba  *phba;
15629 	irqreturn_t hba_irq_rc;
15630 	bool hba_handled = false;
15631 	int qidx;
15632 
15633 	/* Get the driver's phba structure from the dev_id */
15634 	phba = (struct lpfc_hba *)dev_id;
15635 
15636 	if (unlikely(!phba))
15637 		return IRQ_NONE;
15638 
15639 	/*
15640 	 * Invoke fast-path host attention interrupt handling as appropriate.
15641 	 */
15642 	for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
15643 		hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
15644 					&phba->sli4_hba.hba_eq_hdl[qidx]);
15645 		if (hba_irq_rc == IRQ_HANDLED)
15646 			hba_handled |= true;
15647 	}
15648 
15649 	return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
15650 } /* lpfc_sli4_intr_handler */
15651 
lpfc_sli4_poll_hbtimer(struct timer_list * t)15652 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15653 {
15654 	struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
15655 	struct lpfc_queue *eq;
15656 
15657 	rcu_read_lock();
15658 
15659 	list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15660 		lpfc_sli4_poll_eq(eq);
15661 	if (!list_empty(&phba->poll_list))
15662 		mod_timer(&phba->cpuhp_poll_timer,
15663 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15664 
15665 	rcu_read_unlock();
15666 }
15667 
lpfc_sli4_add_to_poll_list(struct lpfc_queue * eq)15668 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15669 {
15670 	struct lpfc_hba *phba = eq->phba;
15671 
15672 	/* kickstart slowpath processing if needed */
15673 	if (list_empty(&phba->poll_list))
15674 		mod_timer(&phba->cpuhp_poll_timer,
15675 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15676 
15677 	list_add_rcu(&eq->_poll_list, &phba->poll_list);
15678 	synchronize_rcu();
15679 }
15680 
lpfc_sli4_remove_from_poll_list(struct lpfc_queue * eq)15681 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15682 {
15683 	struct lpfc_hba *phba = eq->phba;
15684 
15685 	/* Disable slowpath processing for this eq.  Kick start the eq
15686 	 * by RE-ARMING the eq's ASAP
15687 	 */
15688 	list_del_rcu(&eq->_poll_list);
15689 	synchronize_rcu();
15690 
15691 	if (list_empty(&phba->poll_list))
15692 		timer_delete_sync(&phba->cpuhp_poll_timer);
15693 }
15694 
lpfc_sli4_cleanup_poll_list(struct lpfc_hba * phba)15695 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
15696 {
15697 	struct lpfc_queue *eq, *next;
15698 
15699 	list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15700 		list_del(&eq->_poll_list);
15701 
15702 	INIT_LIST_HEAD(&phba->poll_list);
15703 	synchronize_rcu();
15704 }
15705 
15706 static inline void
__lpfc_sli4_switch_eqmode(struct lpfc_queue * eq,uint8_t mode)15707 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15708 {
15709 	if (mode == eq->mode)
15710 		return;
15711 	/*
15712 	 * currently this function is only called during a hotplug
15713 	 * event and the cpu on which this function is executing
15714 	 * is going offline.  By now the hotplug has instructed
15715 	 * the scheduler to remove this cpu from cpu active mask.
15716 	 * So we don't need to work about being put aside by the
15717 	 * scheduler for a high priority process.  Yes, the inte-
15718 	 * rrupts could come but they are known to retire ASAP.
15719 	 */
15720 
15721 	/* Disable polling in the fastpath */
15722 	WRITE_ONCE(eq->mode, mode);
15723 	/* flush out the store buffer */
15724 	smp_wmb();
15725 
15726 	/*
15727 	 * Add this eq to the polling list and start polling. For
15728 	 * a grace period both interrupt handler and poller will
15729 	 * try to process the eq _but_ that's fine.  We have a
15730 	 * synchronization mechanism in place (queue_claimed) to
15731 	 * deal with it.  This is just a draining phase for int-
15732 	 * errupt handler (not eq's) as we have guranteed through
15733 	 * barrier that all the CPUs have seen the new CQ_POLLED
15734 	 * state. which will effectively disable the REARMING of
15735 	 * the EQ.  The whole idea is eq's die off eventually as
15736 	 * we are not rearming EQ's anymore.
15737 	 */
15738 	mode ? lpfc_sli4_add_to_poll_list(eq) :
15739 	       lpfc_sli4_remove_from_poll_list(eq);
15740 }
15741 
lpfc_sli4_start_polling(struct lpfc_queue * eq)15742 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15743 {
15744 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15745 }
15746 
lpfc_sli4_stop_polling(struct lpfc_queue * eq)15747 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15748 {
15749 	struct lpfc_hba *phba = eq->phba;
15750 
15751 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15752 
15753 	/* Kick start for the pending io's in h/w.
15754 	 * Once we switch back to interrupt processing on a eq
15755 	 * the io path completion will only arm eq's when it
15756 	 * receives a completion.  But since eq's are in disa-
15757 	 * rmed state it doesn't receive a completion.  This
15758 	 * creates a deadlock scenaro.
15759 	 */
15760 	phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15761 }
15762 
15763 /**
15764  * lpfc_sli4_queue_free - free a queue structure and associated memory
15765  * @queue: The queue structure to free.
15766  *
15767  * This function frees a queue structure and the DMAable memory used for
15768  * the host resident queue. This function must be called after destroying the
15769  * queue on the HBA.
15770  **/
15771 void
lpfc_sli4_queue_free(struct lpfc_queue * queue)15772 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15773 {
15774 	struct lpfc_dmabuf *dmabuf;
15775 
15776 	if (!queue)
15777 		return;
15778 
15779 	if (!list_empty(&queue->wq_list))
15780 		list_del(&queue->wq_list);
15781 
15782 	while (!list_empty(&queue->page_list)) {
15783 		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15784 				 list);
15785 		dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15786 				  dmabuf->virt, dmabuf->phys);
15787 		kfree(dmabuf);
15788 	}
15789 	if (queue->rqbp) {
15790 		lpfc_free_rq_buffer(queue->phba, queue);
15791 		kfree(queue->rqbp);
15792 	}
15793 
15794 	if (!list_empty(&queue->cpu_list))
15795 		list_del(&queue->cpu_list);
15796 
15797 	kfree(queue);
15798 	return;
15799 }
15800 
15801 /**
15802  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15803  * @phba: The HBA that this queue is being created on.
15804  * @page_size: The size of a queue page
15805  * @entry_size: The size of each queue entry for this queue.
15806  * @entry_count: The number of entries that this queue will handle.
15807  * @cpu: The cpu that will primarily utilize this queue.
15808  *
15809  * This function allocates a queue structure and the DMAable memory used for
15810  * the host resident queue. This function must be called before creating the
15811  * queue on the HBA.
15812  **/
15813 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)15814 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15815 		      uint32_t entry_size, uint32_t entry_count, int cpu)
15816 {
15817 	struct lpfc_queue *queue;
15818 	struct lpfc_dmabuf *dmabuf;
15819 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15820 	uint16_t x, pgcnt;
15821 
15822 	if (!phba->sli4_hba.pc_sli4_params.supported)
15823 		hw_page_size = page_size;
15824 
15825 	pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15826 
15827 	/* If needed, Adjust page count to match the max the adapter supports */
15828 	if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15829 		pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15830 
15831 	queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15832 			     GFP_KERNEL, cpu_to_node(cpu));
15833 	if (!queue)
15834 		return NULL;
15835 
15836 	INIT_LIST_HEAD(&queue->list);
15837 	INIT_LIST_HEAD(&queue->_poll_list);
15838 	INIT_LIST_HEAD(&queue->wq_list);
15839 	INIT_LIST_HEAD(&queue->wqfull_list);
15840 	INIT_LIST_HEAD(&queue->page_list);
15841 	INIT_LIST_HEAD(&queue->child_list);
15842 	INIT_LIST_HEAD(&queue->cpu_list);
15843 
15844 	/* Set queue parameters now.  If the system cannot provide memory
15845 	 * resources, the free routine needs to know what was allocated.
15846 	 */
15847 	queue->page_count = pgcnt;
15848 	queue->q_pgs = (void **)&queue[1];
15849 	queue->entry_cnt_per_pg = hw_page_size / entry_size;
15850 	queue->entry_size = entry_size;
15851 	queue->entry_count = entry_count;
15852 	queue->page_size = hw_page_size;
15853 	queue->phba = phba;
15854 
15855 	for (x = 0; x < queue->page_count; x++) {
15856 		dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15857 				      dev_to_node(&phba->pcidev->dev));
15858 		if (!dmabuf)
15859 			goto out_fail;
15860 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15861 						  hw_page_size, &dmabuf->phys,
15862 						  GFP_KERNEL);
15863 		if (!dmabuf->virt) {
15864 			kfree(dmabuf);
15865 			goto out_fail;
15866 		}
15867 		dmabuf->buffer_tag = x;
15868 		list_add_tail(&dmabuf->list, &queue->page_list);
15869 		/* use lpfc_sli4_qe to index a paritcular entry in this page */
15870 		queue->q_pgs[x] = dmabuf->virt;
15871 	}
15872 	INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15873 	INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15874 	INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15875 	INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15876 
15877 	/* notify_interval will be set during q creation */
15878 
15879 	return queue;
15880 out_fail:
15881 	lpfc_sli4_queue_free(queue);
15882 	return NULL;
15883 }
15884 
15885 /**
15886  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15887  * @phba: HBA structure that indicates port to create a queue on.
15888  * @pci_barset: PCI BAR set flag.
15889  *
15890  * This function shall perform iomap of the specified PCI BAR address to host
15891  * memory address if not already done so and return it. The returned host
15892  * memory address can be NULL.
15893  */
15894 static void __iomem *
lpfc_dual_chute_pci_bar_map(struct lpfc_hba * phba,uint16_t pci_barset)15895 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15896 {
15897 	if (!phba->pcidev)
15898 		return NULL;
15899 
15900 	switch (pci_barset) {
15901 	case WQ_PCI_BAR_0_AND_1:
15902 		return phba->pci_bar0_memmap_p;
15903 	case WQ_PCI_BAR_2_AND_3:
15904 		return phba->pci_bar2_memmap_p;
15905 	case WQ_PCI_BAR_4_AND_5:
15906 		return phba->pci_bar4_memmap_p;
15907 	default:
15908 		break;
15909 	}
15910 	return NULL;
15911 }
15912 
15913 /**
15914  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15915  * @phba: HBA structure that EQs are on.
15916  * @startq: The starting EQ index to modify
15917  * @numq: The number of EQs (consecutive indexes) to modify
15918  * @usdelay: amount of delay
15919  *
15920  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15921  * is set either by writing to a register (if supported by the SLI Port)
15922  * or by mailbox command. The mailbox command allows several EQs to be
15923  * updated at once.
15924  *
15925  * The @phba struct is used to send a mailbox command to HBA. The @startq
15926  * is used to get the starting EQ index to change. The @numq value is
15927  * used to specify how many consecutive EQ indexes, starting at EQ index,
15928  * are to be changed. This function is asynchronous and will wait for any
15929  * mailbox commands to finish before returning.
15930  *
15931  * On success this function will return a zero. If unable to allocate
15932  * enough memory this function will return -ENOMEM. If a mailbox command
15933  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15934  * have had their delay multipler changed.
15935  **/
15936 void
lpfc_modify_hba_eq_delay(struct lpfc_hba * phba,uint32_t startq,uint32_t numq,uint32_t usdelay)15937 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
15938 			 uint32_t numq, uint32_t usdelay)
15939 {
15940 	struct lpfc_mbx_modify_eq_delay *eq_delay;
15941 	LPFC_MBOXQ_t *mbox;
15942 	struct lpfc_queue *eq;
15943 	int cnt = 0, rc, length;
15944 	uint32_t shdr_status, shdr_add_status;
15945 	uint32_t dmult;
15946 	int qidx;
15947 	union lpfc_sli4_cfg_shdr *shdr;
15948 
15949 	if (startq >= phba->cfg_irq_chann)
15950 		return;
15951 
15952 	if (usdelay > 0xFFFF) {
15953 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15954 				"6429 usdelay %d too large. Scaled down to "
15955 				"0xFFFF.\n", usdelay);
15956 		usdelay = 0xFFFF;
15957 	}
15958 
15959 	/* set values by EQ_DELAY register if supported */
15960 	if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15961 		for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15962 			eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15963 			if (!eq)
15964 				continue;
15965 
15966 			lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
15967 
15968 			if (++cnt >= numq)
15969 				break;
15970 		}
15971 		return;
15972 	}
15973 
15974 	/* Otherwise, set values by mailbox cmd */
15975 
15976 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15977 	if (!mbox) {
15978 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15979 				"6428 Failed allocating mailbox cmd buffer."
15980 				" EQ delay was not set.\n");
15981 		return;
15982 	}
15983 	length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15984 		  sizeof(struct lpfc_sli4_cfg_mhdr));
15985 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15986 			 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15987 			 length, LPFC_SLI4_MBX_EMBED);
15988 	eq_delay = &mbox->u.mqe.un.eq_delay;
15989 
15990 	/* Calculate delay multiper from maximum interrupt per second */
15991 	dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
15992 	if (dmult)
15993 		dmult--;
15994 	if (dmult > LPFC_DMULT_MAX)
15995 		dmult = LPFC_DMULT_MAX;
15996 
15997 	for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15998 		eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15999 		if (!eq)
16000 			continue;
16001 		eq->q_mode = usdelay;
16002 		eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16003 		eq_delay->u.request.eq[cnt].phase = 0;
16004 		eq_delay->u.request.eq[cnt].delay_multi = dmult;
16005 
16006 		if (++cnt >= numq)
16007 			break;
16008 	}
16009 	eq_delay->u.request.num_eq = cnt;
16010 
16011 	mbox->vport = phba->pport;
16012 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16013 	mbox->ctx_ndlp = NULL;
16014 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16015 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16016 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16017 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16018 	if (shdr_status || shdr_add_status || rc) {
16019 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16020 				"2512 MODIFY_EQ_DELAY mailbox failed with "
16021 				"status x%x add_status x%x, mbx status x%x\n",
16022 				shdr_status, shdr_add_status, rc);
16023 	}
16024 	mempool_free(mbox, phba->mbox_mem_pool);
16025 	return;
16026 }
16027 
16028 /**
16029  * lpfc_eq_create - Create an Event Queue on the HBA
16030  * @phba: HBA structure that indicates port to create a queue on.
16031  * @eq: The queue structure to use to create the event queue.
16032  * @imax: The maximum interrupt per second limit.
16033  *
16034  * This function creates an event queue, as detailed in @eq, on a port,
16035  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16036  *
16037  * The @phba struct is used to send mailbox command to HBA. The @eq struct
16038  * is used to get the entry count and entry size that are necessary to
16039  * determine the number of pages to allocate and use for this queue. This
16040  * function will send the EQ_CREATE mailbox command to the HBA to setup the
16041  * event queue. This function is asynchronous and will wait for the mailbox
16042  * command to finish before continuing.
16043  *
16044  * On success this function will return a zero. If unable to allocate enough
16045  * memory this function will return -ENOMEM. If the queue create mailbox command
16046  * fails this function will return -ENXIO.
16047  **/
16048 int
lpfc_eq_create(struct lpfc_hba * phba,struct lpfc_queue * eq,uint32_t imax)16049 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
16050 {
16051 	struct lpfc_mbx_eq_create *eq_create;
16052 	LPFC_MBOXQ_t *mbox;
16053 	int rc, length, status = 0;
16054 	struct lpfc_dmabuf *dmabuf;
16055 	uint32_t shdr_status, shdr_add_status;
16056 	union lpfc_sli4_cfg_shdr *shdr;
16057 	uint16_t dmult;
16058 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16059 
16060 	/* sanity check on queue memory */
16061 	if (!eq)
16062 		return -ENODEV;
16063 	if (!phba->sli4_hba.pc_sli4_params.supported)
16064 		hw_page_size = SLI4_PAGE_SIZE;
16065 
16066 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16067 	if (!mbox)
16068 		return -ENOMEM;
16069 	length = (sizeof(struct lpfc_mbx_eq_create) -
16070 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16071 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16072 			 LPFC_MBOX_OPCODE_EQ_CREATE,
16073 			 length, LPFC_SLI4_MBX_EMBED);
16074 	eq_create = &mbox->u.mqe.un.eq_create;
16075 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
16076 	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16077 	       eq->page_count);
16078 	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16079 	       LPFC_EQE_SIZE);
16080 	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
16081 
16082 	/* Use version 2 of CREATE_EQ if eqav is set */
16083 	if (phba->sli4_hba.pc_sli4_params.eqav) {
16084 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16085 		       LPFC_Q_CREATE_VERSION_2);
16086 		bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16087 		       phba->sli4_hba.pc_sli4_params.eqav);
16088 	}
16089 
16090 	/* don't setup delay multiplier using EQ_CREATE */
16091 	dmult = 0;
16092 	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16093 	       dmult);
16094 	switch (eq->entry_count) {
16095 	default:
16096 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16097 				"0360 Unsupported EQ count. (%d)\n",
16098 				eq->entry_count);
16099 		if (eq->entry_count < 256) {
16100 			status = -EINVAL;
16101 			goto out;
16102 		}
16103 		fallthrough;	/* otherwise default to smallest count */
16104 	case 256:
16105 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16106 		       LPFC_EQ_CNT_256);
16107 		break;
16108 	case 512:
16109 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16110 		       LPFC_EQ_CNT_512);
16111 		break;
16112 	case 1024:
16113 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16114 		       LPFC_EQ_CNT_1024);
16115 		break;
16116 	case 2048:
16117 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16118 		       LPFC_EQ_CNT_2048);
16119 		break;
16120 	case 4096:
16121 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16122 		       LPFC_EQ_CNT_4096);
16123 		break;
16124 	}
16125 	list_for_each_entry(dmabuf, &eq->page_list, list) {
16126 		memset(dmabuf->virt, 0, hw_page_size);
16127 		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16128 					putPaddrLow(dmabuf->phys);
16129 		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16130 					putPaddrHigh(dmabuf->phys);
16131 	}
16132 	mbox->vport = phba->pport;
16133 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16134 	mbox->ctx_buf = NULL;
16135 	mbox->ctx_ndlp = NULL;
16136 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16137 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16138 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16139 	if (shdr_status || shdr_add_status || rc) {
16140 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16141 				"2500 EQ_CREATE mailbox failed with "
16142 				"status x%x add_status x%x, mbx status x%x\n",
16143 				shdr_status, shdr_add_status, rc);
16144 		status = -ENXIO;
16145 	}
16146 	eq->type = LPFC_EQ;
16147 	eq->subtype = LPFC_NONE;
16148 	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16149 	if (eq->queue_id == 0xFFFF)
16150 		status = -ENXIO;
16151 	eq->host_index = 0;
16152 	eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16153 	eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
16154 out:
16155 	mempool_free(mbox, phba->mbox_mem_pool);
16156 	return status;
16157 }
16158 
16159 /**
16160  * lpfc_sli4_hba_intr_handler_th - SLI4 HBA threaded interrupt handler
16161  * @irq: Interrupt number.
16162  * @dev_id: The device context pointer.
16163  *
16164  * This routine is a mirror of lpfc_sli4_hba_intr_handler, but executed within
16165  * threaded irq context.
16166  *
16167  * Returns
16168  * IRQ_HANDLED - interrupt is handled
16169  * IRQ_NONE - otherwise
16170  **/
lpfc_sli4_hba_intr_handler_th(int irq,void * dev_id)16171 irqreturn_t lpfc_sli4_hba_intr_handler_th(int irq, void *dev_id)
16172 {
16173 	struct lpfc_hba *phba;
16174 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
16175 	struct lpfc_queue *fpeq;
16176 	int ecount = 0;
16177 	int hba_eqidx;
16178 	struct lpfc_eq_intr_info *eqi;
16179 
16180 	/* Get the driver's phba structure from the dev_id */
16181 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
16182 	phba = hba_eq_hdl->phba;
16183 	hba_eqidx = hba_eq_hdl->idx;
16184 
16185 	if (unlikely(!phba))
16186 		return IRQ_NONE;
16187 	if (unlikely(!phba->sli4_hba.hdwq))
16188 		return IRQ_NONE;
16189 
16190 	/* Get to the EQ struct associated with this vector */
16191 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
16192 	if (unlikely(!fpeq))
16193 		return IRQ_NONE;
16194 
16195 	eqi = per_cpu_ptr(phba->sli4_hba.eq_info, raw_smp_processor_id());
16196 	eqi->icnt++;
16197 
16198 	fpeq->last_cpu = raw_smp_processor_id();
16199 
16200 	if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
16201 	    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
16202 	    phba->cfg_auto_imax &&
16203 	    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
16204 	    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
16205 		lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
16206 
16207 	/* process and rearm the EQ */
16208 	ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
16209 				      LPFC_THREADED_IRQ);
16210 
16211 	if (unlikely(ecount == 0)) {
16212 		fpeq->EQ_no_entry++;
16213 		if (phba->intr_type == MSIX)
16214 			/* MSI-X treated interrupt served as no EQ share INT */
16215 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16216 					"3358 MSI-X interrupt with no EQE\n");
16217 		else
16218 			/* Non MSI-X treated on interrupt as EQ share INT */
16219 			return IRQ_NONE;
16220 	}
16221 	return IRQ_HANDLED;
16222 }
16223 
16224 /**
16225  * lpfc_cq_create - Create a Completion Queue on the HBA
16226  * @phba: HBA structure that indicates port to create a queue on.
16227  * @cq: The queue structure to use to create the completion queue.
16228  * @eq: The event queue to bind this completion queue to.
16229  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16230  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16231  *
16232  * This function creates a completion queue, as detailed in @wq, on a port,
16233  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16234  *
16235  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16236  * is used to get the entry count and entry size that are necessary to
16237  * determine the number of pages to allocate and use for this queue. The @eq
16238  * is used to indicate which event queue to bind this completion queue to. This
16239  * function will send the CQ_CREATE mailbox command to the HBA to setup the
16240  * completion queue. This function is asynchronous and will wait for the mailbox
16241  * command to finish before continuing.
16242  *
16243  * On success this function will return a zero. If unable to allocate enough
16244  * memory this function will return -ENOMEM. If the queue create mailbox command
16245  * fails this function will return -ENXIO.
16246  **/
16247 int
lpfc_cq_create(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_queue * eq,uint32_t type,uint32_t subtype)16248 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16249 	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16250 {
16251 	struct lpfc_mbx_cq_create *cq_create;
16252 	struct lpfc_dmabuf *dmabuf;
16253 	LPFC_MBOXQ_t *mbox;
16254 	int rc, length, status = 0;
16255 	uint32_t shdr_status, shdr_add_status;
16256 	union lpfc_sli4_cfg_shdr *shdr;
16257 
16258 	/* sanity check on queue memory */
16259 	if (!cq || !eq)
16260 		return -ENODEV;
16261 
16262 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16263 	if (!mbox)
16264 		return -ENOMEM;
16265 	length = (sizeof(struct lpfc_mbx_cq_create) -
16266 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16267 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16268 			 LPFC_MBOX_OPCODE_CQ_CREATE,
16269 			 length, LPFC_SLI4_MBX_EMBED);
16270 	cq_create = &mbox->u.mqe.un.cq_create;
16271 	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
16272 	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16273 		    cq->page_count);
16274 	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16275 	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
16276 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16277 	       phba->sli4_hba.pc_sli4_params.cqv);
16278 	if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
16279 		bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16280 		       (cq->page_size / SLI4_PAGE_SIZE));
16281 		bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16282 		       eq->queue_id);
16283 		bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16284 		       phba->sli4_hba.pc_sli4_params.cqav);
16285 	} else {
16286 		bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16287 		       eq->queue_id);
16288 	}
16289 	switch (cq->entry_count) {
16290 	case 2048:
16291 	case 4096:
16292 		if (phba->sli4_hba.pc_sli4_params.cqv ==
16293 		    LPFC_Q_CREATE_VERSION_2) {
16294 			cq_create->u.request.context.lpfc_cq_context_count =
16295 				cq->entry_count;
16296 			bf_set(lpfc_cq_context_count,
16297 			       &cq_create->u.request.context,
16298 			       LPFC_CQ_CNT_WORD7);
16299 			break;
16300 		}
16301 		fallthrough;
16302 	default:
16303 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16304 				"0361 Unsupported CQ count: "
16305 				"entry cnt %d sz %d pg cnt %d\n",
16306 				cq->entry_count, cq->entry_size,
16307 				cq->page_count);
16308 		if (cq->entry_count < 256) {
16309 			status = -EINVAL;
16310 			goto out;
16311 		}
16312 		fallthrough;	/* otherwise default to smallest count */
16313 	case 256:
16314 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16315 		       LPFC_CQ_CNT_256);
16316 		break;
16317 	case 512:
16318 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16319 		       LPFC_CQ_CNT_512);
16320 		break;
16321 	case 1024:
16322 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16323 		       LPFC_CQ_CNT_1024);
16324 		break;
16325 	}
16326 	list_for_each_entry(dmabuf, &cq->page_list, list) {
16327 		memset(dmabuf->virt, 0, cq->page_size);
16328 		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16329 					putPaddrLow(dmabuf->phys);
16330 		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16331 					putPaddrHigh(dmabuf->phys);
16332 	}
16333 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16334 
16335 	/* The IOCTL status is embedded in the mailbox subheader. */
16336 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16337 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16338 	if (shdr_status || shdr_add_status || rc) {
16339 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16340 				"2501 CQ_CREATE mailbox failed with "
16341 				"status x%x add_status x%x, mbx status x%x\n",
16342 				shdr_status, shdr_add_status, rc);
16343 		status = -ENXIO;
16344 		goto out;
16345 	}
16346 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16347 	if (cq->queue_id == 0xFFFF) {
16348 		status = -ENXIO;
16349 		goto out;
16350 	}
16351 	/* link the cq onto the parent eq child list */
16352 	list_add_tail(&cq->list, &eq->child_list);
16353 	/* Set up completion queue's type and subtype */
16354 	cq->type = type;
16355 	cq->subtype = subtype;
16356 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16357 	cq->assoc_qid = eq->queue_id;
16358 	cq->assoc_qp = eq;
16359 	cq->host_index = 0;
16360 	cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16361 	cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
16362 
16363 	if (cq->queue_id > phba->sli4_hba.cq_max)
16364 		phba->sli4_hba.cq_max = cq->queue_id;
16365 out:
16366 	mempool_free(mbox, phba->mbox_mem_pool);
16367 	return status;
16368 }
16369 
16370 /**
16371  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16372  * @phba: HBA structure that indicates port to create a queue on.
16373  * @cqp: The queue structure array to use to create the completion queues.
16374  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
16375  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16376  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16377  *
16378  * This function creates a set of  completion queue, s to support MRQ
16379  * as detailed in @cqp, on a port,
16380  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16381  *
16382  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16383  * is used to get the entry count and entry size that are necessary to
16384  * determine the number of pages to allocate and use for this queue. The @eq
16385  * is used to indicate which event queue to bind this completion queue to. This
16386  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16387  * completion queue. This function is asynchronous and will wait for the mailbox
16388  * command to finish before continuing.
16389  *
16390  * On success this function will return a zero. If unable to allocate enough
16391  * memory this function will return -ENOMEM. If the queue create mailbox command
16392  * fails this function will return -ENXIO.
16393  **/
16394 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)16395 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
16396 		   struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16397 		   uint32_t subtype)
16398 {
16399 	struct lpfc_queue *cq;
16400 	struct lpfc_queue *eq;
16401 	struct lpfc_mbx_cq_create_set *cq_set;
16402 	struct lpfc_dmabuf *dmabuf;
16403 	LPFC_MBOXQ_t *mbox;
16404 	int rc, length, alloclen, status = 0;
16405 	int cnt, idx, numcq, page_idx = 0;
16406 	uint32_t shdr_status, shdr_add_status;
16407 	union lpfc_sli4_cfg_shdr *shdr;
16408 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16409 
16410 	/* sanity check on queue memory */
16411 	numcq = phba->cfg_nvmet_mrq;
16412 	if (!cqp || !hdwq || !numcq)
16413 		return -ENODEV;
16414 
16415 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16416 	if (!mbox)
16417 		return -ENOMEM;
16418 
16419 	length = sizeof(struct lpfc_mbx_cq_create_set);
16420 	length += ((numcq * cqp[0]->page_count) *
16421 		   sizeof(struct dma_address));
16422 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16423 			LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16424 			LPFC_SLI4_MBX_NEMBED);
16425 	if (alloclen < length) {
16426 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16427 				"3098 Allocated DMA memory size (%d) is "
16428 				"less than the requested DMA memory size "
16429 				"(%d)\n", alloclen, length);
16430 		status = -ENOMEM;
16431 		goto out;
16432 	}
16433 	cq_set = mbox->sge_array->addr[0];
16434 	shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16435 	bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16436 
16437 	for (idx = 0; idx < numcq; idx++) {
16438 		cq = cqp[idx];
16439 		eq = hdwq[idx].hba_eq;
16440 		if (!cq || !eq) {
16441 			status = -ENOMEM;
16442 			goto out;
16443 		}
16444 		if (!phba->sli4_hba.pc_sli4_params.supported)
16445 			hw_page_size = cq->page_size;
16446 
16447 		switch (idx) {
16448 		case 0:
16449 			bf_set(lpfc_mbx_cq_create_set_page_size,
16450 			       &cq_set->u.request,
16451 			       (hw_page_size / SLI4_PAGE_SIZE));
16452 			bf_set(lpfc_mbx_cq_create_set_num_pages,
16453 			       &cq_set->u.request, cq->page_count);
16454 			bf_set(lpfc_mbx_cq_create_set_evt,
16455 			       &cq_set->u.request, 1);
16456 			bf_set(lpfc_mbx_cq_create_set_valid,
16457 			       &cq_set->u.request, 1);
16458 			bf_set(lpfc_mbx_cq_create_set_cqe_size,
16459 			       &cq_set->u.request, 0);
16460 			bf_set(lpfc_mbx_cq_create_set_num_cq,
16461 			       &cq_set->u.request, numcq);
16462 			bf_set(lpfc_mbx_cq_create_set_autovalid,
16463 			       &cq_set->u.request,
16464 			       phba->sli4_hba.pc_sli4_params.cqav);
16465 			switch (cq->entry_count) {
16466 			case 2048:
16467 			case 4096:
16468 				if (phba->sli4_hba.pc_sli4_params.cqv ==
16469 				    LPFC_Q_CREATE_VERSION_2) {
16470 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16471 					       &cq_set->u.request,
16472 						cq->entry_count);
16473 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16474 					       &cq_set->u.request,
16475 					       LPFC_CQ_CNT_WORD7);
16476 					break;
16477 				}
16478 				fallthrough;
16479 			default:
16480 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16481 						"3118 Bad CQ count. (%d)\n",
16482 						cq->entry_count);
16483 				if (cq->entry_count < 256) {
16484 					status = -EINVAL;
16485 					goto out;
16486 				}
16487 				fallthrough;	/* otherwise default to smallest */
16488 			case 256:
16489 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16490 				       &cq_set->u.request, LPFC_CQ_CNT_256);
16491 				break;
16492 			case 512:
16493 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16494 				       &cq_set->u.request, LPFC_CQ_CNT_512);
16495 				break;
16496 			case 1024:
16497 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16498 				       &cq_set->u.request, LPFC_CQ_CNT_1024);
16499 				break;
16500 			}
16501 			bf_set(lpfc_mbx_cq_create_set_eq_id0,
16502 			       &cq_set->u.request, eq->queue_id);
16503 			break;
16504 		case 1:
16505 			bf_set(lpfc_mbx_cq_create_set_eq_id1,
16506 			       &cq_set->u.request, eq->queue_id);
16507 			break;
16508 		case 2:
16509 			bf_set(lpfc_mbx_cq_create_set_eq_id2,
16510 			       &cq_set->u.request, eq->queue_id);
16511 			break;
16512 		case 3:
16513 			bf_set(lpfc_mbx_cq_create_set_eq_id3,
16514 			       &cq_set->u.request, eq->queue_id);
16515 			break;
16516 		case 4:
16517 			bf_set(lpfc_mbx_cq_create_set_eq_id4,
16518 			       &cq_set->u.request, eq->queue_id);
16519 			break;
16520 		case 5:
16521 			bf_set(lpfc_mbx_cq_create_set_eq_id5,
16522 			       &cq_set->u.request, eq->queue_id);
16523 			break;
16524 		case 6:
16525 			bf_set(lpfc_mbx_cq_create_set_eq_id6,
16526 			       &cq_set->u.request, eq->queue_id);
16527 			break;
16528 		case 7:
16529 			bf_set(lpfc_mbx_cq_create_set_eq_id7,
16530 			       &cq_set->u.request, eq->queue_id);
16531 			break;
16532 		case 8:
16533 			bf_set(lpfc_mbx_cq_create_set_eq_id8,
16534 			       &cq_set->u.request, eq->queue_id);
16535 			break;
16536 		case 9:
16537 			bf_set(lpfc_mbx_cq_create_set_eq_id9,
16538 			       &cq_set->u.request, eq->queue_id);
16539 			break;
16540 		case 10:
16541 			bf_set(lpfc_mbx_cq_create_set_eq_id10,
16542 			       &cq_set->u.request, eq->queue_id);
16543 			break;
16544 		case 11:
16545 			bf_set(lpfc_mbx_cq_create_set_eq_id11,
16546 			       &cq_set->u.request, eq->queue_id);
16547 			break;
16548 		case 12:
16549 			bf_set(lpfc_mbx_cq_create_set_eq_id12,
16550 			       &cq_set->u.request, eq->queue_id);
16551 			break;
16552 		case 13:
16553 			bf_set(lpfc_mbx_cq_create_set_eq_id13,
16554 			       &cq_set->u.request, eq->queue_id);
16555 			break;
16556 		case 14:
16557 			bf_set(lpfc_mbx_cq_create_set_eq_id14,
16558 			       &cq_set->u.request, eq->queue_id);
16559 			break;
16560 		case 15:
16561 			bf_set(lpfc_mbx_cq_create_set_eq_id15,
16562 			       &cq_set->u.request, eq->queue_id);
16563 			break;
16564 		}
16565 
16566 		/* link the cq onto the parent eq child list */
16567 		list_add_tail(&cq->list, &eq->child_list);
16568 		/* Set up completion queue's type and subtype */
16569 		cq->type = type;
16570 		cq->subtype = subtype;
16571 		cq->assoc_qid = eq->queue_id;
16572 		cq->assoc_qp = eq;
16573 		cq->host_index = 0;
16574 		cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16575 		cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16576 					 cq->entry_count);
16577 		cq->chann = idx;
16578 
16579 		rc = 0;
16580 		list_for_each_entry(dmabuf, &cq->page_list, list) {
16581 			memset(dmabuf->virt, 0, hw_page_size);
16582 			cnt = page_idx + dmabuf->buffer_tag;
16583 			cq_set->u.request.page[cnt].addr_lo =
16584 					putPaddrLow(dmabuf->phys);
16585 			cq_set->u.request.page[cnt].addr_hi =
16586 					putPaddrHigh(dmabuf->phys);
16587 			rc++;
16588 		}
16589 		page_idx += rc;
16590 	}
16591 
16592 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16593 
16594 	/* The IOCTL status is embedded in the mailbox subheader. */
16595 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16596 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16597 	if (shdr_status || shdr_add_status || rc) {
16598 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16599 				"3119 CQ_CREATE_SET mailbox failed with "
16600 				"status x%x add_status x%x, mbx status x%x\n",
16601 				shdr_status, shdr_add_status, rc);
16602 		status = -ENXIO;
16603 		goto out;
16604 	}
16605 	rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16606 	if (rc == 0xFFFF) {
16607 		status = -ENXIO;
16608 		goto out;
16609 	}
16610 
16611 	for (idx = 0; idx < numcq; idx++) {
16612 		cq = cqp[idx];
16613 		cq->queue_id = rc + idx;
16614 		if (cq->queue_id > phba->sli4_hba.cq_max)
16615 			phba->sli4_hba.cq_max = cq->queue_id;
16616 	}
16617 
16618 out:
16619 	lpfc_sli4_mbox_cmd_free(phba, mbox);
16620 	return status;
16621 }
16622 
16623 /**
16624  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
16625  * @phba: HBA structure that indicates port to create a queue on.
16626  * @mq: The queue structure to use to create the mailbox queue.
16627  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16628  * @cq: The completion queue to associate with this cq.
16629  *
16630  * This function provides failback (fb) functionality when the
16631  * mq_create_ext fails on older FW generations.  It's purpose is identical
16632  * to mq_create_ext otherwise.
16633  *
16634  * This routine cannot fail as all attributes were previously accessed and
16635  * initialized in mq_create_ext.
16636  **/
16637 static void
lpfc_mq_create_fb_init(struct lpfc_hba * phba,struct lpfc_queue * mq,LPFC_MBOXQ_t * mbox,struct lpfc_queue * cq)16638 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16639 		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
16640 {
16641 	struct lpfc_mbx_mq_create *mq_create;
16642 	struct lpfc_dmabuf *dmabuf;
16643 	int length;
16644 
16645 	length = (sizeof(struct lpfc_mbx_mq_create) -
16646 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16647 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16648 			 LPFC_MBOX_OPCODE_MQ_CREATE,
16649 			 length, LPFC_SLI4_MBX_EMBED);
16650 	mq_create = &mbox->u.mqe.un.mq_create;
16651 	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
16652 	       mq->page_count);
16653 	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
16654 	       cq->queue_id);
16655 	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16656 	switch (mq->entry_count) {
16657 	case 16:
16658 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16659 		       LPFC_MQ_RING_SIZE_16);
16660 		break;
16661 	case 32:
16662 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16663 		       LPFC_MQ_RING_SIZE_32);
16664 		break;
16665 	case 64:
16666 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16667 		       LPFC_MQ_RING_SIZE_64);
16668 		break;
16669 	case 128:
16670 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16671 		       LPFC_MQ_RING_SIZE_128);
16672 		break;
16673 	}
16674 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16675 		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16676 			putPaddrLow(dmabuf->phys);
16677 		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16678 			putPaddrHigh(dmabuf->phys);
16679 	}
16680 }
16681 
16682 /**
16683  * lpfc_mq_create - Create a mailbox Queue on the HBA
16684  * @phba: HBA structure that indicates port to create a queue on.
16685  * @mq: The queue structure to use to create the mailbox queue.
16686  * @cq: The completion queue to associate with this cq.
16687  * @subtype: The queue's subtype.
16688  *
16689  * This function creates a mailbox queue, as detailed in @mq, on a port,
16690  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16691  *
16692  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16693  * is used to get the entry count and entry size that are necessary to
16694  * determine the number of pages to allocate and use for this queue. This
16695  * function will send the MQ_CREATE mailbox command to the HBA to setup the
16696  * mailbox queue. This function is asynchronous and will wait for the mailbox
16697  * command to finish before continuing.
16698  *
16699  * On success this function will return a zero. If unable to allocate enough
16700  * memory this function will return -ENOMEM. If the queue create mailbox command
16701  * fails this function will return -ENXIO.
16702  **/
16703 int32_t
lpfc_mq_create(struct lpfc_hba * phba,struct lpfc_queue * mq,struct lpfc_queue * cq,uint32_t subtype)16704 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16705 	       struct lpfc_queue *cq, uint32_t subtype)
16706 {
16707 	struct lpfc_mbx_mq_create *mq_create;
16708 	struct lpfc_mbx_mq_create_ext *mq_create_ext;
16709 	struct lpfc_dmabuf *dmabuf;
16710 	LPFC_MBOXQ_t *mbox;
16711 	int rc, length, status = 0;
16712 	uint32_t shdr_status, shdr_add_status;
16713 	union lpfc_sli4_cfg_shdr *shdr;
16714 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16715 
16716 	/* sanity check on queue memory */
16717 	if (!mq || !cq)
16718 		return -ENODEV;
16719 	if (!phba->sli4_hba.pc_sli4_params.supported)
16720 		hw_page_size = SLI4_PAGE_SIZE;
16721 
16722 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16723 	if (!mbox)
16724 		return -ENOMEM;
16725 	length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16726 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16727 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16728 			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16729 			 length, LPFC_SLI4_MBX_EMBED);
16730 
16731 	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
16732 	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
16733 	bf_set(lpfc_mbx_mq_create_ext_num_pages,
16734 	       &mq_create_ext->u.request, mq->page_count);
16735 	bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16736 	       &mq_create_ext->u.request, 1);
16737 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
16738 	       &mq_create_ext->u.request, 1);
16739 	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16740 	       &mq_create_ext->u.request, 1);
16741 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16742 	       &mq_create_ext->u.request, 1);
16743 	bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16744 	       &mq_create_ext->u.request, 1);
16745 	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
16746 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16747 	       phba->sli4_hba.pc_sli4_params.mqv);
16748 	if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16749 		bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16750 		       cq->queue_id);
16751 	else
16752 		bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16753 		       cq->queue_id);
16754 	switch (mq->entry_count) {
16755 	default:
16756 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16757 				"0362 Unsupported MQ count. (%d)\n",
16758 				mq->entry_count);
16759 		if (mq->entry_count < 16) {
16760 			status = -EINVAL;
16761 			goto out;
16762 		}
16763 		fallthrough;	/* otherwise default to smallest count */
16764 	case 16:
16765 		bf_set(lpfc_mq_context_ring_size,
16766 		       &mq_create_ext->u.request.context,
16767 		       LPFC_MQ_RING_SIZE_16);
16768 		break;
16769 	case 32:
16770 		bf_set(lpfc_mq_context_ring_size,
16771 		       &mq_create_ext->u.request.context,
16772 		       LPFC_MQ_RING_SIZE_32);
16773 		break;
16774 	case 64:
16775 		bf_set(lpfc_mq_context_ring_size,
16776 		       &mq_create_ext->u.request.context,
16777 		       LPFC_MQ_RING_SIZE_64);
16778 		break;
16779 	case 128:
16780 		bf_set(lpfc_mq_context_ring_size,
16781 		       &mq_create_ext->u.request.context,
16782 		       LPFC_MQ_RING_SIZE_128);
16783 		break;
16784 	}
16785 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16786 		memset(dmabuf->virt, 0, hw_page_size);
16787 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16788 					putPaddrLow(dmabuf->phys);
16789 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16790 					putPaddrHigh(dmabuf->phys);
16791 	}
16792 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16793 	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16794 			      &mq_create_ext->u.response);
16795 	if (rc != MBX_SUCCESS) {
16796 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16797 				"2795 MQ_CREATE_EXT failed with "
16798 				"status x%x. Failback to MQ_CREATE.\n",
16799 				rc);
16800 		lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16801 		mq_create = &mbox->u.mqe.un.mq_create;
16802 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16803 		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16804 		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16805 				      &mq_create->u.response);
16806 	}
16807 
16808 	/* The IOCTL status is embedded in the mailbox subheader. */
16809 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16810 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16811 	if (shdr_status || shdr_add_status || rc) {
16812 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16813 				"2502 MQ_CREATE mailbox failed with "
16814 				"status x%x add_status x%x, mbx status x%x\n",
16815 				shdr_status, shdr_add_status, rc);
16816 		status = -ENXIO;
16817 		goto out;
16818 	}
16819 	if (mq->queue_id == 0xFFFF) {
16820 		status = -ENXIO;
16821 		goto out;
16822 	}
16823 	mq->type = LPFC_MQ;
16824 	mq->assoc_qid = cq->queue_id;
16825 	mq->subtype = subtype;
16826 	mq->host_index = 0;
16827 	mq->hba_index = 0;
16828 
16829 	/* link the mq onto the parent cq child list */
16830 	list_add_tail(&mq->list, &cq->child_list);
16831 out:
16832 	mempool_free(mbox, phba->mbox_mem_pool);
16833 	return status;
16834 }
16835 
16836 /**
16837  * lpfc_wq_create - Create a Work Queue on the HBA
16838  * @phba: HBA structure that indicates port to create a queue on.
16839  * @wq: The queue structure to use to create the work queue.
16840  * @cq: The completion queue to bind this work queue to.
16841  * @subtype: The subtype of the work queue indicating its functionality.
16842  *
16843  * This function creates a work queue, as detailed in @wq, on a port, described
16844  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16845  *
16846  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16847  * is used to get the entry count and entry size that are necessary to
16848  * determine the number of pages to allocate and use for this queue. The @cq
16849  * is used to indicate which completion queue to bind this work queue to. This
16850  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16851  * work queue. This function is asynchronous and will wait for the mailbox
16852  * command to finish before continuing.
16853  *
16854  * On success this function will return a zero. If unable to allocate enough
16855  * memory this function will return -ENOMEM. If the queue create mailbox command
16856  * fails this function will return -ENXIO.
16857  **/
16858 int
lpfc_wq_create(struct lpfc_hba * phba,struct lpfc_queue * wq,struct lpfc_queue * cq,uint32_t subtype)16859 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16860 	       struct lpfc_queue *cq, uint32_t subtype)
16861 {
16862 	struct lpfc_mbx_wq_create *wq_create;
16863 	struct lpfc_dmabuf *dmabuf;
16864 	LPFC_MBOXQ_t *mbox;
16865 	int rc, length, status = 0;
16866 	uint32_t shdr_status, shdr_add_status;
16867 	union lpfc_sli4_cfg_shdr *shdr;
16868 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16869 	struct dma_address *page;
16870 	void __iomem *bar_memmap_p;
16871 	uint32_t db_offset;
16872 	uint16_t pci_barset;
16873 	uint8_t dpp_barset;
16874 	uint32_t dpp_offset;
16875 	uint8_t wq_create_version;
16876 #ifdef CONFIG_X86
16877 	unsigned long pg_addr;
16878 #endif
16879 
16880 	/* sanity check on queue memory */
16881 	if (!wq || !cq)
16882 		return -ENODEV;
16883 	if (!phba->sli4_hba.pc_sli4_params.supported)
16884 		hw_page_size = wq->page_size;
16885 
16886 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16887 	if (!mbox)
16888 		return -ENOMEM;
16889 	length = (sizeof(struct lpfc_mbx_wq_create) -
16890 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16891 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16892 			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16893 			 length, LPFC_SLI4_MBX_EMBED);
16894 	wq_create = &mbox->u.mqe.un.wq_create;
16895 	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16896 	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16897 		    wq->page_count);
16898 	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16899 		    cq->queue_id);
16900 
16901 	/* wqv is the earliest version supported, NOT the latest */
16902 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16903 	       phba->sli4_hba.pc_sli4_params.wqv);
16904 
16905 	if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16906 	    (wq->page_size > SLI4_PAGE_SIZE))
16907 		wq_create_version = LPFC_Q_CREATE_VERSION_1;
16908 	else
16909 		wq_create_version = LPFC_Q_CREATE_VERSION_0;
16910 
16911 	switch (wq_create_version) {
16912 	case LPFC_Q_CREATE_VERSION_1:
16913 		bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16914 		       wq->entry_count);
16915 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16916 		       LPFC_Q_CREATE_VERSION_1);
16917 
16918 		switch (wq->entry_size) {
16919 		default:
16920 		case 64:
16921 			bf_set(lpfc_mbx_wq_create_wqe_size,
16922 			       &wq_create->u.request_1,
16923 			       LPFC_WQ_WQE_SIZE_64);
16924 			break;
16925 		case 128:
16926 			bf_set(lpfc_mbx_wq_create_wqe_size,
16927 			       &wq_create->u.request_1,
16928 			       LPFC_WQ_WQE_SIZE_128);
16929 			break;
16930 		}
16931 		/* Request DPP by default */
16932 		bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
16933 		bf_set(lpfc_mbx_wq_create_page_size,
16934 		       &wq_create->u.request_1,
16935 		       (wq->page_size / SLI4_PAGE_SIZE));
16936 		page = wq_create->u.request_1.page;
16937 		break;
16938 	default:
16939 		page = wq_create->u.request.page;
16940 		break;
16941 	}
16942 
16943 	list_for_each_entry(dmabuf, &wq->page_list, list) {
16944 		memset(dmabuf->virt, 0, hw_page_size);
16945 		page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16946 		page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
16947 	}
16948 
16949 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16950 		bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16951 
16952 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16953 	/* The IOCTL status is embedded in the mailbox subheader. */
16954 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16955 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16956 	if (shdr_status || shdr_add_status || rc) {
16957 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16958 				"2503 WQ_CREATE mailbox failed with "
16959 				"status x%x add_status x%x, mbx status x%x\n",
16960 				shdr_status, shdr_add_status, rc);
16961 		status = -ENXIO;
16962 		goto out;
16963 	}
16964 
16965 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16966 		wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16967 					&wq_create->u.response);
16968 	else
16969 		wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16970 					&wq_create->u.response_1);
16971 
16972 	if (wq->queue_id == 0xFFFF) {
16973 		status = -ENXIO;
16974 		goto out;
16975 	}
16976 
16977 	wq->db_format = LPFC_DB_LIST_FORMAT;
16978 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16979 		if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16980 			wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16981 					       &wq_create->u.response);
16982 			if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16983 			    (wq->db_format != LPFC_DB_RING_FORMAT)) {
16984 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16985 						"3265 WQ[%d] doorbell format "
16986 						"not supported: x%x\n",
16987 						wq->queue_id, wq->db_format);
16988 				status = -EINVAL;
16989 				goto out;
16990 			}
16991 			pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
16992 					    &wq_create->u.response);
16993 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16994 								   pci_barset);
16995 			if (!bar_memmap_p) {
16996 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16997 						"3263 WQ[%d] failed to memmap "
16998 						"pci barset:x%x\n",
16999 						wq->queue_id, pci_barset);
17000 				status = -ENOMEM;
17001 				goto out;
17002 			}
17003 			db_offset = wq_create->u.response.doorbell_offset;
17004 			if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
17005 			    (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
17006 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17007 						"3252 WQ[%d] doorbell offset "
17008 						"not supported: x%x\n",
17009 						wq->queue_id, db_offset);
17010 				status = -EINVAL;
17011 				goto out;
17012 			}
17013 			wq->db_regaddr = bar_memmap_p + db_offset;
17014 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17015 					"3264 WQ[%d]: barset:x%x, offset:x%x, "
17016 					"format:x%x\n", wq->queue_id,
17017 					pci_barset, db_offset, wq->db_format);
17018 		} else
17019 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17020 	} else {
17021 		/* Check if DPP was honored by the firmware */
17022 		wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
17023 				    &wq_create->u.response_1);
17024 		if (wq->dpp_enable) {
17025 			pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
17026 					    &wq_create->u.response_1);
17027 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17028 								   pci_barset);
17029 			if (!bar_memmap_p) {
17030 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17031 						"3267 WQ[%d] failed to memmap "
17032 						"pci barset:x%x\n",
17033 						wq->queue_id, pci_barset);
17034 				status = -ENOMEM;
17035 				goto out;
17036 			}
17037 			db_offset = wq_create->u.response_1.doorbell_offset;
17038 			wq->db_regaddr = bar_memmap_p + db_offset;
17039 			wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
17040 					    &wq_create->u.response_1);
17041 			dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
17042 					    &wq_create->u.response_1);
17043 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17044 								   dpp_barset);
17045 			if (!bar_memmap_p) {
17046 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17047 						"3268 WQ[%d] failed to memmap "
17048 						"pci barset:x%x\n",
17049 						wq->queue_id, dpp_barset);
17050 				status = -ENOMEM;
17051 				goto out;
17052 			}
17053 			dpp_offset = wq_create->u.response_1.dpp_offset;
17054 			wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17055 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17056 					"3271 WQ[%d]: barset:x%x, offset:x%x, "
17057 					"dpp_id:x%x dpp_barset:x%x "
17058 					"dpp_offset:x%x\n",
17059 					wq->queue_id, pci_barset, db_offset,
17060 					wq->dpp_id, dpp_barset, dpp_offset);
17061 
17062 #ifdef CONFIG_X86
17063 			/* Enable combined writes for DPP aperture */
17064 			pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
17065 			rc = set_memory_wc(pg_addr, 1);
17066 			if (rc) {
17067 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17068 					"3272 Cannot setup Combined "
17069 					"Write on WQ[%d] - disable DPP\n",
17070 					wq->queue_id);
17071 				phba->cfg_enable_dpp = 0;
17072 			}
17073 #else
17074 			phba->cfg_enable_dpp = 0;
17075 #endif
17076 		} else
17077 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17078 	}
17079 	wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17080 	if (wq->pring == NULL) {
17081 		status = -ENOMEM;
17082 		goto out;
17083 	}
17084 	wq->type = LPFC_WQ;
17085 	wq->assoc_qid = cq->queue_id;
17086 	wq->subtype = subtype;
17087 	wq->host_index = 0;
17088 	wq->hba_index = 0;
17089 	wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
17090 
17091 	/* link the wq onto the parent cq child list */
17092 	list_add_tail(&wq->list, &cq->child_list);
17093 out:
17094 	mempool_free(mbox, phba->mbox_mem_pool);
17095 	return status;
17096 }
17097 
17098 /**
17099  * lpfc_rq_create - Create a Receive Queue on the HBA
17100  * @phba: HBA structure that indicates port to create a queue on.
17101  * @hrq: The queue structure to use to create the header receive queue.
17102  * @drq: The queue structure to use to create the data receive queue.
17103  * @cq: The completion queue to bind this work queue to.
17104  * @subtype: The subtype of the work queue indicating its functionality.
17105  *
17106  * This function creates a receive buffer queue pair , as detailed in @hrq and
17107  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17108  * to the HBA.
17109  *
17110  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17111  * struct is used to get the entry count that is necessary to determine the
17112  * number of pages to use for this queue. The @cq is used to indicate which
17113  * completion queue to bind received buffers that are posted to these queues to.
17114  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17115  * receive queue pair. This function is asynchronous and will wait for the
17116  * mailbox command to finish before continuing.
17117  *
17118  * On success this function will return a zero. If unable to allocate enough
17119  * memory this function will return -ENOMEM. If the queue create mailbox command
17120  * fails this function will return -ENXIO.
17121  **/
17122 int
lpfc_rq_create(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,struct lpfc_queue * cq,uint32_t subtype)17123 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17124 	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17125 {
17126 	struct lpfc_mbx_rq_create *rq_create;
17127 	struct lpfc_dmabuf *dmabuf;
17128 	LPFC_MBOXQ_t *mbox;
17129 	int rc, length, status = 0;
17130 	uint32_t shdr_status, shdr_add_status;
17131 	union lpfc_sli4_cfg_shdr *shdr;
17132 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17133 	void __iomem *bar_memmap_p;
17134 	uint32_t db_offset;
17135 	uint16_t pci_barset;
17136 
17137 	/* sanity check on queue memory */
17138 	if (!hrq || !drq || !cq)
17139 		return -ENODEV;
17140 	if (!phba->sli4_hba.pc_sli4_params.supported)
17141 		hw_page_size = SLI4_PAGE_SIZE;
17142 
17143 	if (hrq->entry_count != drq->entry_count)
17144 		return -EINVAL;
17145 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17146 	if (!mbox)
17147 		return -ENOMEM;
17148 	length = (sizeof(struct lpfc_mbx_rq_create) -
17149 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17150 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17151 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17152 			 length, LPFC_SLI4_MBX_EMBED);
17153 	rq_create = &mbox->u.mqe.un.rq_create;
17154 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17155 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17156 	       phba->sli4_hba.pc_sli4_params.rqv);
17157 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17158 		bf_set(lpfc_rq_context_rqe_count_1,
17159 		       &rq_create->u.request.context,
17160 		       hrq->entry_count);
17161 		rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
17162 		bf_set(lpfc_rq_context_rqe_size,
17163 		       &rq_create->u.request.context,
17164 		       LPFC_RQE_SIZE_8);
17165 		bf_set(lpfc_rq_context_page_size,
17166 		       &rq_create->u.request.context,
17167 		       LPFC_RQ_PAGE_SIZE_4096);
17168 	} else {
17169 		switch (hrq->entry_count) {
17170 		default:
17171 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17172 					"2535 Unsupported RQ count. (%d)\n",
17173 					hrq->entry_count);
17174 			if (hrq->entry_count < 512) {
17175 				status = -EINVAL;
17176 				goto out;
17177 			}
17178 			fallthrough;	/* otherwise default to smallest count */
17179 		case 512:
17180 			bf_set(lpfc_rq_context_rqe_count,
17181 			       &rq_create->u.request.context,
17182 			       LPFC_RQ_RING_SIZE_512);
17183 			break;
17184 		case 1024:
17185 			bf_set(lpfc_rq_context_rqe_count,
17186 			       &rq_create->u.request.context,
17187 			       LPFC_RQ_RING_SIZE_1024);
17188 			break;
17189 		case 2048:
17190 			bf_set(lpfc_rq_context_rqe_count,
17191 			       &rq_create->u.request.context,
17192 			       LPFC_RQ_RING_SIZE_2048);
17193 			break;
17194 		case 4096:
17195 			bf_set(lpfc_rq_context_rqe_count,
17196 			       &rq_create->u.request.context,
17197 			       LPFC_RQ_RING_SIZE_4096);
17198 			break;
17199 		}
17200 		bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17201 		       LPFC_HDR_BUF_SIZE);
17202 	}
17203 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17204 	       cq->queue_id);
17205 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17206 	       hrq->page_count);
17207 	list_for_each_entry(dmabuf, &hrq->page_list, list) {
17208 		memset(dmabuf->virt, 0, hw_page_size);
17209 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17210 					putPaddrLow(dmabuf->phys);
17211 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17212 					putPaddrHigh(dmabuf->phys);
17213 	}
17214 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17215 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17216 
17217 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17218 	/* The IOCTL status is embedded in the mailbox subheader. */
17219 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17220 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17221 	if (shdr_status || shdr_add_status || rc) {
17222 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17223 				"2504 RQ_CREATE mailbox failed with "
17224 				"status x%x add_status x%x, mbx status x%x\n",
17225 				shdr_status, shdr_add_status, rc);
17226 		status = -ENXIO;
17227 		goto out;
17228 	}
17229 	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17230 	if (hrq->queue_id == 0xFFFF) {
17231 		status = -ENXIO;
17232 		goto out;
17233 	}
17234 
17235 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17236 		hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17237 					&rq_create->u.response);
17238 		if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17239 		    (hrq->db_format != LPFC_DB_RING_FORMAT)) {
17240 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17241 					"3262 RQ [%d] doorbell format not "
17242 					"supported: x%x\n", hrq->queue_id,
17243 					hrq->db_format);
17244 			status = -EINVAL;
17245 			goto out;
17246 		}
17247 
17248 		pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17249 				    &rq_create->u.response);
17250 		bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17251 		if (!bar_memmap_p) {
17252 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17253 					"3269 RQ[%d] failed to memmap pci "
17254 					"barset:x%x\n", hrq->queue_id,
17255 					pci_barset);
17256 			status = -ENOMEM;
17257 			goto out;
17258 		}
17259 
17260 		db_offset = rq_create->u.response.doorbell_offset;
17261 		if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17262 		    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
17263 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17264 					"3270 RQ[%d] doorbell offset not "
17265 					"supported: x%x\n", hrq->queue_id,
17266 					db_offset);
17267 			status = -EINVAL;
17268 			goto out;
17269 		}
17270 		hrq->db_regaddr = bar_memmap_p + db_offset;
17271 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17272 				"3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17273 				"format:x%x\n", hrq->queue_id, pci_barset,
17274 				db_offset, hrq->db_format);
17275 	} else {
17276 		hrq->db_format = LPFC_DB_RING_FORMAT;
17277 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17278 	}
17279 	hrq->type = LPFC_HRQ;
17280 	hrq->assoc_qid = cq->queue_id;
17281 	hrq->subtype = subtype;
17282 	hrq->host_index = 0;
17283 	hrq->hba_index = 0;
17284 	hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17285 
17286 	/* now create the data queue */
17287 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17288 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17289 			 length, LPFC_SLI4_MBX_EMBED);
17290 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17291 	       phba->sli4_hba.pc_sli4_params.rqv);
17292 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17293 		bf_set(lpfc_rq_context_rqe_count_1,
17294 		       &rq_create->u.request.context, hrq->entry_count);
17295 		if (subtype == LPFC_NVMET)
17296 			rq_create->u.request.context.buffer_size =
17297 				LPFC_NVMET_DATA_BUF_SIZE;
17298 		else
17299 			rq_create->u.request.context.buffer_size =
17300 				LPFC_DATA_BUF_SIZE;
17301 		bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17302 		       LPFC_RQE_SIZE_8);
17303 		bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17304 		       (PAGE_SIZE/SLI4_PAGE_SIZE));
17305 	} else {
17306 		switch (drq->entry_count) {
17307 		default:
17308 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17309 					"2536 Unsupported RQ count. (%d)\n",
17310 					drq->entry_count);
17311 			if (drq->entry_count < 512) {
17312 				status = -EINVAL;
17313 				goto out;
17314 			}
17315 			fallthrough;	/* otherwise default to smallest count */
17316 		case 512:
17317 			bf_set(lpfc_rq_context_rqe_count,
17318 			       &rq_create->u.request.context,
17319 			       LPFC_RQ_RING_SIZE_512);
17320 			break;
17321 		case 1024:
17322 			bf_set(lpfc_rq_context_rqe_count,
17323 			       &rq_create->u.request.context,
17324 			       LPFC_RQ_RING_SIZE_1024);
17325 			break;
17326 		case 2048:
17327 			bf_set(lpfc_rq_context_rqe_count,
17328 			       &rq_create->u.request.context,
17329 			       LPFC_RQ_RING_SIZE_2048);
17330 			break;
17331 		case 4096:
17332 			bf_set(lpfc_rq_context_rqe_count,
17333 			       &rq_create->u.request.context,
17334 			       LPFC_RQ_RING_SIZE_4096);
17335 			break;
17336 		}
17337 		if (subtype == LPFC_NVMET)
17338 			bf_set(lpfc_rq_context_buf_size,
17339 			       &rq_create->u.request.context,
17340 			       LPFC_NVMET_DATA_BUF_SIZE);
17341 		else
17342 			bf_set(lpfc_rq_context_buf_size,
17343 			       &rq_create->u.request.context,
17344 			       LPFC_DATA_BUF_SIZE);
17345 	}
17346 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17347 	       cq->queue_id);
17348 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17349 	       drq->page_count);
17350 	list_for_each_entry(dmabuf, &drq->page_list, list) {
17351 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17352 					putPaddrLow(dmabuf->phys);
17353 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17354 					putPaddrHigh(dmabuf->phys);
17355 	}
17356 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17357 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17358 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17359 	/* The IOCTL status is embedded in the mailbox subheader. */
17360 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17361 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17362 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17363 	if (shdr_status || shdr_add_status || rc) {
17364 		status = -ENXIO;
17365 		goto out;
17366 	}
17367 	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17368 	if (drq->queue_id == 0xFFFF) {
17369 		status = -ENXIO;
17370 		goto out;
17371 	}
17372 	drq->type = LPFC_DRQ;
17373 	drq->assoc_qid = cq->queue_id;
17374 	drq->subtype = subtype;
17375 	drq->host_index = 0;
17376 	drq->hba_index = 0;
17377 	drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17378 
17379 	/* link the header and data RQs onto the parent cq child list */
17380 	list_add_tail(&hrq->list, &cq->child_list);
17381 	list_add_tail(&drq->list, &cq->child_list);
17382 
17383 out:
17384 	mempool_free(mbox, phba->mbox_mem_pool);
17385 	return status;
17386 }
17387 
17388 /**
17389  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17390  * @phba: HBA structure that indicates port to create a queue on.
17391  * @hrqp: The queue structure array to use to create the header receive queues.
17392  * @drqp: The queue structure array to use to create the data receive queues.
17393  * @cqp: The completion queue array to bind these receive queues to.
17394  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
17395  *
17396  * This function creates a receive buffer queue pair , as detailed in @hrq and
17397  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17398  * to the HBA.
17399  *
17400  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17401  * struct is used to get the entry count that is necessary to determine the
17402  * number of pages to use for this queue. The @cq is used to indicate which
17403  * completion queue to bind received buffers that are posted to these queues to.
17404  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17405  * receive queue pair. This function is asynchronous and will wait for the
17406  * mailbox command to finish before continuing.
17407  *
17408  * On success this function will return a zero. If unable to allocate enough
17409  * memory this function will return -ENOMEM. If the queue create mailbox command
17410  * fails this function will return -ENXIO.
17411  **/
17412 int
lpfc_mrq_create(struct lpfc_hba * phba,struct lpfc_queue ** hrqp,struct lpfc_queue ** drqp,struct lpfc_queue ** cqp,uint32_t subtype)17413 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17414 		struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17415 		uint32_t subtype)
17416 {
17417 	struct lpfc_queue *hrq, *drq, *cq;
17418 	struct lpfc_mbx_rq_create_v2 *rq_create;
17419 	struct lpfc_dmabuf *dmabuf;
17420 	LPFC_MBOXQ_t *mbox;
17421 	int rc, length, alloclen, status = 0;
17422 	int cnt, idx, numrq, page_idx = 0;
17423 	uint32_t shdr_status, shdr_add_status;
17424 	union lpfc_sli4_cfg_shdr *shdr;
17425 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17426 
17427 	numrq = phba->cfg_nvmet_mrq;
17428 	/* sanity check on array memory */
17429 	if (!hrqp || !drqp || !cqp || !numrq)
17430 		return -ENODEV;
17431 	if (!phba->sli4_hba.pc_sli4_params.supported)
17432 		hw_page_size = SLI4_PAGE_SIZE;
17433 
17434 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17435 	if (!mbox)
17436 		return -ENOMEM;
17437 
17438 	length = sizeof(struct lpfc_mbx_rq_create_v2);
17439 	length += ((2 * numrq * hrqp[0]->page_count) *
17440 		   sizeof(struct dma_address));
17441 
17442 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17443 				    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17444 				    LPFC_SLI4_MBX_NEMBED);
17445 	if (alloclen < length) {
17446 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17447 				"3099 Allocated DMA memory size (%d) is "
17448 				"less than the requested DMA memory size "
17449 				"(%d)\n", alloclen, length);
17450 		status = -ENOMEM;
17451 		goto out;
17452 	}
17453 
17454 
17455 
17456 	rq_create = mbox->sge_array->addr[0];
17457 	shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17458 
17459 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17460 	cnt = 0;
17461 
17462 	for (idx = 0; idx < numrq; idx++) {
17463 		hrq = hrqp[idx];
17464 		drq = drqp[idx];
17465 		cq  = cqp[idx];
17466 
17467 		/* sanity check on queue memory */
17468 		if (!hrq || !drq || !cq) {
17469 			status = -ENODEV;
17470 			goto out;
17471 		}
17472 
17473 		if (hrq->entry_count != drq->entry_count) {
17474 			status = -EINVAL;
17475 			goto out;
17476 		}
17477 
17478 		if (idx == 0) {
17479 			bf_set(lpfc_mbx_rq_create_num_pages,
17480 			       &rq_create->u.request,
17481 			       hrq->page_count);
17482 			bf_set(lpfc_mbx_rq_create_rq_cnt,
17483 			       &rq_create->u.request, (numrq * 2));
17484 			bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17485 			       1);
17486 			bf_set(lpfc_rq_context_base_cq,
17487 			       &rq_create->u.request.context,
17488 			       cq->queue_id);
17489 			bf_set(lpfc_rq_context_data_size,
17490 			       &rq_create->u.request.context,
17491 			       LPFC_NVMET_DATA_BUF_SIZE);
17492 			bf_set(lpfc_rq_context_hdr_size,
17493 			       &rq_create->u.request.context,
17494 			       LPFC_HDR_BUF_SIZE);
17495 			bf_set(lpfc_rq_context_rqe_count_1,
17496 			       &rq_create->u.request.context,
17497 			       hrq->entry_count);
17498 			bf_set(lpfc_rq_context_rqe_size,
17499 			       &rq_create->u.request.context,
17500 			       LPFC_RQE_SIZE_8);
17501 			bf_set(lpfc_rq_context_page_size,
17502 			       &rq_create->u.request.context,
17503 			       (PAGE_SIZE/SLI4_PAGE_SIZE));
17504 		}
17505 		rc = 0;
17506 		list_for_each_entry(dmabuf, &hrq->page_list, list) {
17507 			memset(dmabuf->virt, 0, hw_page_size);
17508 			cnt = page_idx + dmabuf->buffer_tag;
17509 			rq_create->u.request.page[cnt].addr_lo =
17510 					putPaddrLow(dmabuf->phys);
17511 			rq_create->u.request.page[cnt].addr_hi =
17512 					putPaddrHigh(dmabuf->phys);
17513 			rc++;
17514 		}
17515 		page_idx += rc;
17516 
17517 		rc = 0;
17518 		list_for_each_entry(dmabuf, &drq->page_list, list) {
17519 			memset(dmabuf->virt, 0, hw_page_size);
17520 			cnt = page_idx + dmabuf->buffer_tag;
17521 			rq_create->u.request.page[cnt].addr_lo =
17522 					putPaddrLow(dmabuf->phys);
17523 			rq_create->u.request.page[cnt].addr_hi =
17524 					putPaddrHigh(dmabuf->phys);
17525 			rc++;
17526 		}
17527 		page_idx += rc;
17528 
17529 		hrq->db_format = LPFC_DB_RING_FORMAT;
17530 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17531 		hrq->type = LPFC_HRQ;
17532 		hrq->assoc_qid = cq->queue_id;
17533 		hrq->subtype = subtype;
17534 		hrq->host_index = 0;
17535 		hrq->hba_index = 0;
17536 		hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17537 
17538 		drq->db_format = LPFC_DB_RING_FORMAT;
17539 		drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17540 		drq->type = LPFC_DRQ;
17541 		drq->assoc_qid = cq->queue_id;
17542 		drq->subtype = subtype;
17543 		drq->host_index = 0;
17544 		drq->hba_index = 0;
17545 		drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17546 
17547 		list_add_tail(&hrq->list, &cq->child_list);
17548 		list_add_tail(&drq->list, &cq->child_list);
17549 	}
17550 
17551 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17552 	/* The IOCTL status is embedded in the mailbox subheader. */
17553 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17554 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17555 	if (shdr_status || shdr_add_status || rc) {
17556 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17557 				"3120 RQ_CREATE mailbox failed with "
17558 				"status x%x add_status x%x, mbx status x%x\n",
17559 				shdr_status, shdr_add_status, rc);
17560 		status = -ENXIO;
17561 		goto out;
17562 	}
17563 	rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17564 	if (rc == 0xFFFF) {
17565 		status = -ENXIO;
17566 		goto out;
17567 	}
17568 
17569 	/* Initialize all RQs with associated queue id */
17570 	for (idx = 0; idx < numrq; idx++) {
17571 		hrq = hrqp[idx];
17572 		hrq->queue_id = rc + (2 * idx);
17573 		drq = drqp[idx];
17574 		drq->queue_id = rc + (2 * idx) + 1;
17575 	}
17576 
17577 out:
17578 	lpfc_sli4_mbox_cmd_free(phba, mbox);
17579 	return status;
17580 }
17581 
17582 /**
17583  * lpfc_eq_destroy - Destroy an event Queue on the HBA
17584  * @phba: HBA structure that indicates port to destroy a queue on.
17585  * @eq: The queue structure associated with the queue to destroy.
17586  *
17587  * This function destroys a queue, as detailed in @eq by sending an mailbox
17588  * command, specific to the type of queue, to the HBA.
17589  *
17590  * The @eq struct is used to get the queue ID of the queue to destroy.
17591  *
17592  * On success this function will return a zero. If the queue destroy mailbox
17593  * command fails this function will return -ENXIO.
17594  **/
17595 int
lpfc_eq_destroy(struct lpfc_hba * phba,struct lpfc_queue * eq)17596 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17597 {
17598 	LPFC_MBOXQ_t *mbox;
17599 	int rc, length, status = 0;
17600 	uint32_t shdr_status, shdr_add_status;
17601 	union lpfc_sli4_cfg_shdr *shdr;
17602 
17603 	/* sanity check on queue memory */
17604 	if (!eq)
17605 		return -ENODEV;
17606 
17607 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17608 		goto list_remove;
17609 
17610 	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17611 	if (!mbox)
17612 		return -ENOMEM;
17613 	length = (sizeof(struct lpfc_mbx_eq_destroy) -
17614 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17615 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17616 			 LPFC_MBOX_OPCODE_EQ_DESTROY,
17617 			 length, LPFC_SLI4_MBX_EMBED);
17618 	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17619 	       eq->queue_id);
17620 	mbox->vport = eq->phba->pport;
17621 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17622 
17623 	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17624 	/* The IOCTL status is embedded in the mailbox subheader. */
17625 	shdr = (union lpfc_sli4_cfg_shdr *)
17626 		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17627 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17628 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17629 	if (shdr_status || shdr_add_status || rc) {
17630 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17631 				"2505 EQ_DESTROY mailbox failed with "
17632 				"status x%x add_status x%x, mbx status x%x\n",
17633 				shdr_status, shdr_add_status, rc);
17634 		status = -ENXIO;
17635 	}
17636 	mempool_free(mbox, eq->phba->mbox_mem_pool);
17637 
17638 list_remove:
17639 	/* Remove eq from any list */
17640 	list_del_init(&eq->list);
17641 
17642 	return status;
17643 }
17644 
17645 /**
17646  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
17647  * @phba: HBA structure that indicates port to destroy a queue on.
17648  * @cq: The queue structure associated with the queue to destroy.
17649  *
17650  * This function destroys a queue, as detailed in @cq by sending an mailbox
17651  * command, specific to the type of queue, to the HBA.
17652  *
17653  * The @cq struct is used to get the queue ID of the queue to destroy.
17654  *
17655  * On success this function will return a zero. If the queue destroy mailbox
17656  * command fails this function will return -ENXIO.
17657  **/
17658 int
lpfc_cq_destroy(struct lpfc_hba * phba,struct lpfc_queue * cq)17659 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17660 {
17661 	LPFC_MBOXQ_t *mbox;
17662 	int rc, length, status = 0;
17663 	uint32_t shdr_status, shdr_add_status;
17664 	union lpfc_sli4_cfg_shdr *shdr;
17665 
17666 	/* sanity check on queue memory */
17667 	if (!cq)
17668 		return -ENODEV;
17669 
17670 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17671 		goto list_remove;
17672 
17673 	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17674 	if (!mbox)
17675 		return -ENOMEM;
17676 	length = (sizeof(struct lpfc_mbx_cq_destroy) -
17677 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17678 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17679 			 LPFC_MBOX_OPCODE_CQ_DESTROY,
17680 			 length, LPFC_SLI4_MBX_EMBED);
17681 	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17682 	       cq->queue_id);
17683 	mbox->vport = cq->phba->pport;
17684 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17685 	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17686 	/* The IOCTL status is embedded in the mailbox subheader. */
17687 	shdr = (union lpfc_sli4_cfg_shdr *)
17688 		&mbox->u.mqe.un.wq_create.header.cfg_shdr;
17689 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17690 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17691 	if (shdr_status || shdr_add_status || rc) {
17692 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17693 				"2506 CQ_DESTROY mailbox failed with "
17694 				"status x%x add_status x%x, mbx status x%x\n",
17695 				shdr_status, shdr_add_status, rc);
17696 		status = -ENXIO;
17697 	}
17698 	mempool_free(mbox, cq->phba->mbox_mem_pool);
17699 
17700 list_remove:
17701 	/* Remove cq from any list */
17702 	list_del_init(&cq->list);
17703 	return status;
17704 }
17705 
17706 /**
17707  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
17708  * @phba: HBA structure that indicates port to destroy a queue on.
17709  * @mq: The queue structure associated with the queue to destroy.
17710  *
17711  * This function destroys a queue, as detailed in @mq by sending an mailbox
17712  * command, specific to the type of queue, to the HBA.
17713  *
17714  * The @mq struct is used to get the queue ID of the queue to destroy.
17715  *
17716  * On success this function will return a zero. If the queue destroy mailbox
17717  * command fails this function will return -ENXIO.
17718  **/
17719 int
lpfc_mq_destroy(struct lpfc_hba * phba,struct lpfc_queue * mq)17720 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17721 {
17722 	LPFC_MBOXQ_t *mbox;
17723 	int rc, length, status = 0;
17724 	uint32_t shdr_status, shdr_add_status;
17725 	union lpfc_sli4_cfg_shdr *shdr;
17726 
17727 	/* sanity check on queue memory */
17728 	if (!mq)
17729 		return -ENODEV;
17730 
17731 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17732 		goto list_remove;
17733 
17734 	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17735 	if (!mbox)
17736 		return -ENOMEM;
17737 	length = (sizeof(struct lpfc_mbx_mq_destroy) -
17738 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17739 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17740 			 LPFC_MBOX_OPCODE_MQ_DESTROY,
17741 			 length, LPFC_SLI4_MBX_EMBED);
17742 	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17743 	       mq->queue_id);
17744 	mbox->vport = mq->phba->pport;
17745 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17746 	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17747 	/* The IOCTL status is embedded in the mailbox subheader. */
17748 	shdr = (union lpfc_sli4_cfg_shdr *)
17749 		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17750 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17751 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17752 	if (shdr_status || shdr_add_status || rc) {
17753 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17754 				"2507 MQ_DESTROY mailbox failed with "
17755 				"status x%x add_status x%x, mbx status x%x\n",
17756 				shdr_status, shdr_add_status, rc);
17757 		status = -ENXIO;
17758 	}
17759 	mempool_free(mbox, mq->phba->mbox_mem_pool);
17760 
17761 list_remove:
17762 	/* Remove mq from any list */
17763 	list_del_init(&mq->list);
17764 	return status;
17765 }
17766 
17767 /**
17768  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
17769  * @phba: HBA structure that indicates port to destroy a queue on.
17770  * @wq: The queue structure associated with the queue to destroy.
17771  *
17772  * This function destroys a queue, as detailed in @wq by sending an mailbox
17773  * command, specific to the type of queue, to the HBA.
17774  *
17775  * The @wq struct is used to get the queue ID of the queue to destroy.
17776  *
17777  * On success this function will return a zero. If the queue destroy mailbox
17778  * command fails this function will return -ENXIO.
17779  **/
17780 int
lpfc_wq_destroy(struct lpfc_hba * phba,struct lpfc_queue * wq)17781 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17782 {
17783 	LPFC_MBOXQ_t *mbox;
17784 	int rc, length, status = 0;
17785 	uint32_t shdr_status, shdr_add_status;
17786 	union lpfc_sli4_cfg_shdr *shdr;
17787 
17788 	/* sanity check on queue memory */
17789 	if (!wq)
17790 		return -ENODEV;
17791 
17792 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17793 		goto list_remove;
17794 
17795 	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17796 	if (!mbox)
17797 		return -ENOMEM;
17798 	length = (sizeof(struct lpfc_mbx_wq_destroy) -
17799 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17800 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17801 			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17802 			 length, LPFC_SLI4_MBX_EMBED);
17803 	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17804 	       wq->queue_id);
17805 	mbox->vport = wq->phba->pport;
17806 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17807 	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17808 	shdr = (union lpfc_sli4_cfg_shdr *)
17809 		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17810 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17811 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17812 	if (shdr_status || shdr_add_status || rc) {
17813 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17814 				"2508 WQ_DESTROY mailbox failed with "
17815 				"status x%x add_status x%x, mbx status x%x\n",
17816 				shdr_status, shdr_add_status, rc);
17817 		status = -ENXIO;
17818 	}
17819 	mempool_free(mbox, wq->phba->mbox_mem_pool);
17820 
17821 list_remove:
17822 	/* Remove wq from any list */
17823 	list_del_init(&wq->list);
17824 	kfree(wq->pring);
17825 	wq->pring = NULL;
17826 	return status;
17827 }
17828 
17829 /**
17830  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17831  * @phba: HBA structure that indicates port to destroy a queue on.
17832  * @hrq: The queue structure associated with the queue to destroy.
17833  * @drq: The queue structure associated with the queue to destroy.
17834  *
17835  * This function destroys a queue, as detailed in @rq by sending an mailbox
17836  * command, specific to the type of queue, to the HBA.
17837  *
17838  * The @rq struct is used to get the queue ID of the queue to destroy.
17839  *
17840  * On success this function will return a zero. If the queue destroy mailbox
17841  * command fails this function will return -ENXIO.
17842  **/
17843 int
lpfc_rq_destroy(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq)17844 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17845 		struct lpfc_queue *drq)
17846 {
17847 	LPFC_MBOXQ_t *mbox;
17848 	int rc, length, status = 0;
17849 	uint32_t shdr_status, shdr_add_status;
17850 	union lpfc_sli4_cfg_shdr *shdr;
17851 
17852 	/* sanity check on queue memory */
17853 	if (!hrq || !drq)
17854 		return -ENODEV;
17855 
17856 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17857 		goto list_remove;
17858 
17859 	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17860 	if (!mbox)
17861 		return -ENOMEM;
17862 	length = (sizeof(struct lpfc_mbx_rq_destroy) -
17863 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17864 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17865 			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17866 			 length, LPFC_SLI4_MBX_EMBED);
17867 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17868 	       hrq->queue_id);
17869 	mbox->vport = hrq->phba->pport;
17870 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17871 	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17872 	/* The IOCTL status is embedded in the mailbox subheader. */
17873 	shdr = (union lpfc_sli4_cfg_shdr *)
17874 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17875 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17876 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17877 	if (shdr_status || shdr_add_status || rc) {
17878 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17879 				"2509 RQ_DESTROY mailbox failed with "
17880 				"status x%x add_status x%x, mbx status x%x\n",
17881 				shdr_status, shdr_add_status, rc);
17882 		mempool_free(mbox, hrq->phba->mbox_mem_pool);
17883 		return -ENXIO;
17884 	}
17885 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17886 	       drq->queue_id);
17887 	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17888 	shdr = (union lpfc_sli4_cfg_shdr *)
17889 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17890 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17891 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17892 	if (shdr_status || shdr_add_status || rc) {
17893 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17894 				"2510 RQ_DESTROY mailbox failed with "
17895 				"status x%x add_status x%x, mbx status x%x\n",
17896 				shdr_status, shdr_add_status, rc);
17897 		status = -ENXIO;
17898 	}
17899 	mempool_free(mbox, hrq->phba->mbox_mem_pool);
17900 
17901 list_remove:
17902 	list_del_init(&hrq->list);
17903 	list_del_init(&drq->list);
17904 	return status;
17905 }
17906 
17907 /**
17908  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17909  * @phba: The virtual port for which this call being executed.
17910  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17911  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17912  * @xritag: the xritag that ties this io to the SGL pages.
17913  *
17914  * This routine will post the sgl pages for the IO that has the xritag
17915  * that is in the iocbq structure. The xritag is assigned during iocbq
17916  * creation and persists for as long as the driver is loaded.
17917  * if the caller has fewer than 256 scatter gather segments to map then
17918  * pdma_phys_addr1 should be 0.
17919  * If the caller needs to map more than 256 scatter gather segment then
17920  * pdma_phys_addr1 should be a valid physical address.
17921  * physical address for SGLs must be 64 byte aligned.
17922  * If you are going to map 2 SGL's then the first one must have 256 entries
17923  * the second sgl can have between 1 and 256 entries.
17924  *
17925  * Return codes:
17926  * 	0 - Success
17927  * 	-ENXIO, -ENOMEM - Failure
17928  **/
17929 int
lpfc_sli4_post_sgl(struct lpfc_hba * phba,dma_addr_t pdma_phys_addr0,dma_addr_t pdma_phys_addr1,uint16_t xritag)17930 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17931 		dma_addr_t pdma_phys_addr0,
17932 		dma_addr_t pdma_phys_addr1,
17933 		uint16_t xritag)
17934 {
17935 	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17936 	LPFC_MBOXQ_t *mbox;
17937 	int rc;
17938 	uint32_t shdr_status, shdr_add_status;
17939 	uint32_t mbox_tmo;
17940 	union lpfc_sli4_cfg_shdr *shdr;
17941 
17942 	if (xritag == NO_XRI) {
17943 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17944 				"0364 Invalid param:\n");
17945 		return -EINVAL;
17946 	}
17947 
17948 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17949 	if (!mbox)
17950 		return -ENOMEM;
17951 
17952 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17953 			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17954 			sizeof(struct lpfc_mbx_post_sgl_pages) -
17955 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17956 
17957 	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17958 				&mbox->u.mqe.un.post_sgl_pages;
17959 	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17960 	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17961 
17962 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=
17963 				cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17964 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17965 				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17966 
17967 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=
17968 				cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17969 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17970 				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17971 	if (!phba->sli4_hba.intr_enable)
17972 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17973 	else {
17974 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17975 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17976 	}
17977 	/* The IOCTL status is embedded in the mailbox subheader. */
17978 	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17979 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17980 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17981 	if (!phba->sli4_hba.intr_enable)
17982 		mempool_free(mbox, phba->mbox_mem_pool);
17983 	else if (rc != MBX_TIMEOUT)
17984 		mempool_free(mbox, phba->mbox_mem_pool);
17985 	if (shdr_status || shdr_add_status || rc) {
17986 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17987 				"2511 POST_SGL mailbox failed with "
17988 				"status x%x add_status x%x, mbx status x%x\n",
17989 				shdr_status, shdr_add_status, rc);
17990 	}
17991 	return 0;
17992 }
17993 
17994 /**
17995  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
17996  * @phba: pointer to lpfc hba data structure.
17997  *
17998  * This routine is invoked to post rpi header templates to the
17999  * HBA consistent with the SLI-4 interface spec.  This routine
18000  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18001  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18002  *
18003  * Returns
18004  *	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18005  *	LPFC_RPI_ALLOC_ERROR if no rpis are available.
18006  **/
18007 static uint16_t
lpfc_sli4_alloc_xri(struct lpfc_hba * phba)18008 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
18009 {
18010 	unsigned long xri;
18011 
18012 	/*
18013 	 * Fetch the next logical xri.  Because this index is logical,
18014 	 * the driver starts at 0 each time.
18015 	 */
18016 	spin_lock_irq(&phba->hbalock);
18017 	xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,
18018 				 phba->sli4_hba.max_cfg_param.max_xri);
18019 	if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
18020 		spin_unlock_irq(&phba->hbalock);
18021 		return NO_XRI;
18022 	} else {
18023 		set_bit(xri, phba->sli4_hba.xri_bmask);
18024 		phba->sli4_hba.max_cfg_param.xri_used++;
18025 	}
18026 	spin_unlock_irq(&phba->hbalock);
18027 	return xri;
18028 }
18029 
18030 /**
18031  * __lpfc_sli4_free_xri - Release an xri for reuse.
18032  * @phba: pointer to lpfc hba data structure.
18033  * @xri: xri to release.
18034  *
18035  * This routine is invoked to release an xri to the pool of
18036  * available rpis maintained by the driver.
18037  **/
18038 static void
__lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18039 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18040 {
18041 	if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
18042 		phba->sli4_hba.max_cfg_param.xri_used--;
18043 	}
18044 }
18045 
18046 /**
18047  * lpfc_sli4_free_xri - Release an xri for reuse.
18048  * @phba: pointer to lpfc hba data structure.
18049  * @xri: xri to release.
18050  *
18051  * This routine is invoked to release an xri to the pool of
18052  * available rpis maintained by the driver.
18053  **/
18054 void
lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18055 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18056 {
18057 	spin_lock_irq(&phba->hbalock);
18058 	__lpfc_sli4_free_xri(phba, xri);
18059 	spin_unlock_irq(&phba->hbalock);
18060 }
18061 
18062 /**
18063  * lpfc_sli4_next_xritag - Get an xritag for the io
18064  * @phba: Pointer to HBA context object.
18065  *
18066  * This function gets an xritag for the iocb. If there is no unused xritag
18067  * it will return 0xffff.
18068  * The function returns the allocated xritag if successful, else returns zero.
18069  * Zero is not a valid xritag.
18070  * The caller is not required to hold any lock.
18071  **/
18072 uint16_t
lpfc_sli4_next_xritag(struct lpfc_hba * phba)18073 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
18074 {
18075 	uint16_t xri_index;
18076 
18077 	xri_index = lpfc_sli4_alloc_xri(phba);
18078 	if (xri_index == NO_XRI)
18079 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18080 				"2004 Failed to allocate XRI.last XRITAG is %d"
18081 				" Max XRI is %d, Used XRI is %d\n",
18082 				xri_index,
18083 				phba->sli4_hba.max_cfg_param.max_xri,
18084 				phba->sli4_hba.max_cfg_param.xri_used);
18085 	return xri_index;
18086 }
18087 
18088 /**
18089  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
18090  * @phba: pointer to lpfc hba data structure.
18091  * @post_sgl_list: pointer to els sgl entry list.
18092  * @post_cnt: number of els sgl entries on the list.
18093  *
18094  * This routine is invoked to post a block of driver's sgl pages to the
18095  * HBA using non-embedded mailbox command. No Lock is held. This routine
18096  * is only called when the driver is loading and after all IO has been
18097  * stopped.
18098  **/
18099 static int
lpfc_sli4_post_sgl_list(struct lpfc_hba * phba,struct list_head * post_sgl_list,int post_cnt)18100 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
18101 			    struct list_head *post_sgl_list,
18102 			    int post_cnt)
18103 {
18104 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
18105 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18106 	struct sgl_page_pairs *sgl_pg_pairs;
18107 	void *viraddr;
18108 	LPFC_MBOXQ_t *mbox;
18109 	uint32_t reqlen, alloclen, pg_pairs;
18110 	uint32_t mbox_tmo;
18111 	uint16_t xritag_start = 0;
18112 	int rc = 0;
18113 	uint32_t shdr_status, shdr_add_status;
18114 	union lpfc_sli4_cfg_shdr *shdr;
18115 
18116 	reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
18117 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18118 	if (reqlen > SLI4_PAGE_SIZE) {
18119 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18120 				"2559 Block sgl registration required DMA "
18121 				"size (%d) great than a page\n", reqlen);
18122 		return -ENOMEM;
18123 	}
18124 
18125 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18126 	if (!mbox)
18127 		return -ENOMEM;
18128 
18129 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18130 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18131 			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18132 			 LPFC_SLI4_MBX_NEMBED);
18133 
18134 	if (alloclen < reqlen) {
18135 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18136 				"0285 Allocated DMA memory size (%d) is "
18137 				"less than the requested DMA memory "
18138 				"size (%d)\n", alloclen, reqlen);
18139 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18140 		return -ENOMEM;
18141 	}
18142 	/* Set up the SGL pages in the non-embedded DMA pages */
18143 	viraddr = mbox->sge_array->addr[0];
18144 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18145 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18146 
18147 	pg_pairs = 0;
18148 	list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
18149 		/* Set up the sge entry */
18150 		sgl_pg_pairs->sgl_pg0_addr_lo =
18151 				cpu_to_le32(putPaddrLow(sglq_entry->phys));
18152 		sgl_pg_pairs->sgl_pg0_addr_hi =
18153 				cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18154 		sgl_pg_pairs->sgl_pg1_addr_lo =
18155 				cpu_to_le32(putPaddrLow(0));
18156 		sgl_pg_pairs->sgl_pg1_addr_hi =
18157 				cpu_to_le32(putPaddrHigh(0));
18158 
18159 		/* Keep the first xritag on the list */
18160 		if (pg_pairs == 0)
18161 			xritag_start = sglq_entry->sli4_xritag;
18162 		sgl_pg_pairs++;
18163 		pg_pairs++;
18164 	}
18165 
18166 	/* Complete initialization and perform endian conversion. */
18167 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18168 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
18169 	sgl->word0 = cpu_to_le32(sgl->word0);
18170 
18171 	if (!phba->sli4_hba.intr_enable)
18172 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18173 	else {
18174 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18175 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18176 	}
18177 	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18178 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18179 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18180 	if (!phba->sli4_hba.intr_enable)
18181 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18182 	else if (rc != MBX_TIMEOUT)
18183 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18184 	if (shdr_status || shdr_add_status || rc) {
18185 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18186 				"2513 POST_SGL_BLOCK mailbox command failed "
18187 				"status x%x add_status x%x mbx status x%x\n",
18188 				shdr_status, shdr_add_status, rc);
18189 		rc = -ENXIO;
18190 	}
18191 	return rc;
18192 }
18193 
18194 /**
18195  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
18196  * @phba: pointer to lpfc hba data structure.
18197  * @nblist: pointer to nvme buffer list.
18198  * @count: number of scsi buffers on the list.
18199  *
18200  * This routine is invoked to post a block of @count scsi sgl pages from a
18201  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
18202  * No Lock is held.
18203  *
18204  **/
18205 static int
lpfc_sli4_post_io_sgl_block(struct lpfc_hba * phba,struct list_head * nblist,int count)18206 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18207 			    int count)
18208 {
18209 	struct lpfc_io_buf *lpfc_ncmd;
18210 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18211 	struct sgl_page_pairs *sgl_pg_pairs;
18212 	void *viraddr;
18213 	LPFC_MBOXQ_t *mbox;
18214 	uint32_t reqlen, alloclen, pg_pairs;
18215 	uint32_t mbox_tmo;
18216 	uint16_t xritag_start = 0;
18217 	int rc = 0;
18218 	uint32_t shdr_status, shdr_add_status;
18219 	dma_addr_t pdma_phys_bpl1;
18220 	union lpfc_sli4_cfg_shdr *shdr;
18221 
18222 	/* Calculate the requested length of the dma memory */
18223 	reqlen = count * sizeof(struct sgl_page_pairs) +
18224 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18225 	if (reqlen > SLI4_PAGE_SIZE) {
18226 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
18227 				"6118 Block sgl registration required DMA "
18228 				"size (%d) great than a page\n", reqlen);
18229 		return -ENOMEM;
18230 	}
18231 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18232 	if (!mbox) {
18233 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18234 				"6119 Failed to allocate mbox cmd memory\n");
18235 		return -ENOMEM;
18236 	}
18237 
18238 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18239 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18240 				    LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18241 				    reqlen, LPFC_SLI4_MBX_NEMBED);
18242 
18243 	if (alloclen < reqlen) {
18244 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18245 				"6120 Allocated DMA memory size (%d) is "
18246 				"less than the requested DMA memory "
18247 				"size (%d)\n", alloclen, reqlen);
18248 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18249 		return -ENOMEM;
18250 	}
18251 
18252 	/* Get the first SGE entry from the non-embedded DMA memory */
18253 	viraddr = mbox->sge_array->addr[0];
18254 
18255 	/* Set up the SGL pages in the non-embedded DMA pages */
18256 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18257 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18258 
18259 	pg_pairs = 0;
18260 	list_for_each_entry(lpfc_ncmd, nblist, list) {
18261 		/* Set up the sge entry */
18262 		sgl_pg_pairs->sgl_pg0_addr_lo =
18263 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
18264 		sgl_pg_pairs->sgl_pg0_addr_hi =
18265 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
18266 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
18267 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18268 						SGL_PAGE_SIZE;
18269 		else
18270 			pdma_phys_bpl1 = 0;
18271 		sgl_pg_pairs->sgl_pg1_addr_lo =
18272 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18273 		sgl_pg_pairs->sgl_pg1_addr_hi =
18274 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18275 		/* Keep the first xritag on the list */
18276 		if (pg_pairs == 0)
18277 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
18278 		sgl_pg_pairs++;
18279 		pg_pairs++;
18280 	}
18281 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18282 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18283 	/* Perform endian conversion if necessary */
18284 	sgl->word0 = cpu_to_le32(sgl->word0);
18285 
18286 	if (!phba->sli4_hba.intr_enable) {
18287 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18288 	} else {
18289 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18290 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18291 	}
18292 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
18293 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18294 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18295 	if (!phba->sli4_hba.intr_enable)
18296 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18297 	else if (rc != MBX_TIMEOUT)
18298 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18299 	if (shdr_status || shdr_add_status || rc) {
18300 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18301 				"6125 POST_SGL_BLOCK mailbox command failed "
18302 				"status x%x add_status x%x mbx status x%x\n",
18303 				shdr_status, shdr_add_status, rc);
18304 		rc = -ENXIO;
18305 	}
18306 	return rc;
18307 }
18308 
18309 /**
18310  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
18311  * @phba: pointer to lpfc hba data structure.
18312  * @post_nblist: pointer to the nvme buffer list.
18313  * @sb_count: number of nvme buffers.
18314  *
18315  * This routine walks a list of nvme buffers that was passed in. It attempts
18316  * to construct blocks of nvme buffer sgls which contains contiguous xris and
18317  * uses the non-embedded SGL block post mailbox commands to post to the port.
18318  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18319  * embedded SGL post mailbox command for posting. The @post_nblist passed in
18320  * must be local list, thus no lock is needed when manipulate the list.
18321  *
18322  * Returns: 0 = failure, non-zero number of successfully posted buffers.
18323  **/
18324 int
lpfc_sli4_post_io_sgl_list(struct lpfc_hba * phba,struct list_head * post_nblist,int sb_count)18325 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18326 			   struct list_head *post_nblist, int sb_count)
18327 {
18328 	struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
18329 	int status, sgl_size;
18330 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18331 	dma_addr_t pdma_phys_sgl1;
18332 	int last_xritag = NO_XRI;
18333 	int cur_xritag;
18334 	LIST_HEAD(prep_nblist);
18335 	LIST_HEAD(blck_nblist);
18336 	LIST_HEAD(nvme_nblist);
18337 
18338 	/* sanity check */
18339 	if (sb_count <= 0)
18340 		return -EINVAL;
18341 
18342 	sgl_size = phba->cfg_sg_dma_buf_size;
18343 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18344 		list_del_init(&lpfc_ncmd->list);
18345 		block_cnt++;
18346 		if ((last_xritag != NO_XRI) &&
18347 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18348 			/* a hole in xri block, form a sgl posting block */
18349 			list_splice_init(&prep_nblist, &blck_nblist);
18350 			post_cnt = block_cnt - 1;
18351 			/* prepare list for next posting block */
18352 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18353 			block_cnt = 1;
18354 		} else {
18355 			/* prepare list for next posting block */
18356 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18357 			/* enough sgls for non-embed sgl mbox command */
18358 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18359 				list_splice_init(&prep_nblist, &blck_nblist);
18360 				post_cnt = block_cnt;
18361 				block_cnt = 0;
18362 			}
18363 		}
18364 		num_posting++;
18365 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18366 
18367 		/* end of repost sgl list condition for NVME buffers */
18368 		if (num_posting == sb_count) {
18369 			if (post_cnt == 0) {
18370 				/* last sgl posting block */
18371 				list_splice_init(&prep_nblist, &blck_nblist);
18372 				post_cnt = block_cnt;
18373 			} else if (block_cnt == 1) {
18374 				/* last single sgl with non-contiguous xri */
18375 				if (sgl_size > SGL_PAGE_SIZE)
18376 					pdma_phys_sgl1 =
18377 						lpfc_ncmd->dma_phys_sgl +
18378 						SGL_PAGE_SIZE;
18379 				else
18380 					pdma_phys_sgl1 = 0;
18381 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18382 				status = lpfc_sli4_post_sgl(
18383 						phba, lpfc_ncmd->dma_phys_sgl,
18384 						pdma_phys_sgl1, cur_xritag);
18385 				if (status) {
18386 					/* Post error.  Buffer unavailable. */
18387 					lpfc_ncmd->flags |=
18388 						LPFC_SBUF_NOT_POSTED;
18389 				} else {
18390 					/* Post success. Bffer available. */
18391 					lpfc_ncmd->flags &=
18392 						~LPFC_SBUF_NOT_POSTED;
18393 					lpfc_ncmd->status = IOSTAT_SUCCESS;
18394 					num_posted++;
18395 				}
18396 				/* success, put on NVME buffer sgl list */
18397 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18398 			}
18399 		}
18400 
18401 		/* continue until a nembed page worth of sgls */
18402 		if (post_cnt == 0)
18403 			continue;
18404 
18405 		/* post block of NVME buffer list sgls */
18406 		status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18407 						     post_cnt);
18408 
18409 		/* don't reset xirtag due to hole in xri block */
18410 		if (block_cnt == 0)
18411 			last_xritag = NO_XRI;
18412 
18413 		/* reset NVME buffer post count for next round of posting */
18414 		post_cnt = 0;
18415 
18416 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18417 		while (!list_empty(&blck_nblist)) {
18418 			list_remove_head(&blck_nblist, lpfc_ncmd,
18419 					 struct lpfc_io_buf, list);
18420 			if (status) {
18421 				/* Post error.  Mark buffer unavailable. */
18422 				lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
18423 			} else {
18424 				/* Post success, Mark buffer available. */
18425 				lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
18426 				lpfc_ncmd->status = IOSTAT_SUCCESS;
18427 				num_posted++;
18428 			}
18429 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18430 		}
18431 	}
18432 	/* Push NVME buffers with sgl posted to the available list */
18433 	lpfc_io_buf_replenish(phba, &nvme_nblist);
18434 
18435 	return num_posted;
18436 }
18437 
18438 /**
18439  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18440  * @phba: pointer to lpfc_hba struct that the frame was received on
18441  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18442  *
18443  * This function checks the fields in the @fc_hdr to see if the FC frame is a
18444  * valid type of frame that the LPFC driver will handle. This function will
18445  * return a zero if the frame is a valid frame or a non zero value when the
18446  * frame does not pass the check.
18447  **/
18448 static int
lpfc_fc_frame_check(struct lpfc_hba * phba,struct fc_frame_header * fc_hdr)18449 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18450 {
18451 	/*  make rctl_names static to save stack space */
18452 	struct fc_vft_header *fc_vft_hdr;
18453 	struct fc_app_header *fc_app_hdr;
18454 	uint32_t *header = (uint32_t *) fc_hdr;
18455 
18456 #define FC_RCTL_MDS_DIAGS	0xF4
18457 
18458 	switch (fc_hdr->fh_r_ctl) {
18459 	case FC_RCTL_DD_UNCAT:		/* uncategorized information */
18460 	case FC_RCTL_DD_SOL_DATA:	/* solicited data */
18461 	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */
18462 	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */
18463 	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */
18464 	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */
18465 	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */
18466 	case FC_RCTL_DD_CMD_STATUS:	/* command status */
18467 	case FC_RCTL_ELS_REQ:	/* extended link services request */
18468 	case FC_RCTL_ELS_REP:	/* extended link services reply */
18469 	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */
18470 	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */
18471 	case FC_RCTL_BA_ABTS: 	/* basic link service abort */
18472 	case FC_RCTL_BA_RMC: 	/* remove connection */
18473 	case FC_RCTL_BA_ACC:	/* basic accept */
18474 	case FC_RCTL_BA_RJT:	/* basic reject */
18475 	case FC_RCTL_BA_PRMT:
18476 	case FC_RCTL_ACK_1:	/* acknowledge_1 */
18477 	case FC_RCTL_ACK_0:	/* acknowledge_0 */
18478 	case FC_RCTL_P_RJT:	/* port reject */
18479 	case FC_RCTL_F_RJT:	/* fabric reject */
18480 	case FC_RCTL_P_BSY:	/* port busy */
18481 	case FC_RCTL_F_BSY:	/* fabric busy to data frame */
18482 	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */
18483 	case FC_RCTL_LCR:	/* link credit reset */
18484 	case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
18485 	case FC_RCTL_END:	/* end */
18486 		break;
18487 	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */
18488 		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18489 		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18490 		return lpfc_fc_frame_check(phba, fc_hdr);
18491 	case FC_RCTL_BA_NOP:	/* basic link service NOP */
18492 	default:
18493 		goto drop;
18494 	}
18495 
18496 	switch (fc_hdr->fh_type) {
18497 	case FC_TYPE_BLS:
18498 	case FC_TYPE_ELS:
18499 	case FC_TYPE_FCP:
18500 	case FC_TYPE_CT:
18501 	case FC_TYPE_NVME:
18502 		break;
18503 	case FC_TYPE_IP:
18504 	case FC_TYPE_ILS:
18505 	default:
18506 		goto drop;
18507 	}
18508 
18509 	if (unlikely(phba->link_flag == LS_LOOPBACK_MODE &&
18510 				phba->cfg_vmid_app_header)) {
18511 		/* Application header is 16B device header */
18512 		if (fc_hdr->fh_df_ctl & LPFC_FC_16B_DEVICE_HEADER) {
18513 			fc_app_hdr = (struct fc_app_header *) (fc_hdr + 1);
18514 			if (be32_to_cpu(fc_app_hdr->src_app_id) !=
18515 					LOOPBACK_SRC_APPID) {
18516 				lpfc_printf_log(phba, KERN_WARNING,
18517 						LOG_ELS | LOG_LIBDFC,
18518 						"1932 Loopback src app id "
18519 						"not matched, app_id:x%x\n",
18520 						be32_to_cpu(fc_app_hdr->src_app_id));
18521 
18522 				goto drop;
18523 			}
18524 		} else {
18525 			lpfc_printf_log(phba, KERN_WARNING,
18526 					LOG_ELS | LOG_LIBDFC,
18527 					"1933 Loopback df_ctl bit not set, "
18528 					"df_ctl:x%x\n",
18529 					fc_hdr->fh_df_ctl);
18530 
18531 			goto drop;
18532 		}
18533 	}
18534 
18535 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
18536 			"2538 Received frame rctl:x%x, type:x%x, "
18537 			"frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
18538 			fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18539 			be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18540 			be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18541 			be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18542 			be32_to_cpu(header[6]));
18543 	return 0;
18544 drop:
18545 	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
18546 			"2539 Dropped frame rctl:x%x type:x%x\n",
18547 			fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18548 	return 1;
18549 }
18550 
18551 /**
18552  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18553  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18554  *
18555  * This function processes the FC header to retrieve the VFI from the VF
18556  * header, if one exists. This function will return the VFI if one exists
18557  * or 0 if no VSAN Header exists.
18558  **/
18559 static uint32_t
lpfc_fc_hdr_get_vfi(struct fc_frame_header * fc_hdr)18560 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18561 {
18562 	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18563 
18564 	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18565 		return 0;
18566 	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18567 }
18568 
18569 /**
18570  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18571  * @phba: Pointer to the HBA structure to search for the vport on
18572  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18573  * @fcfi: The FC Fabric ID that the frame came from
18574  * @did: Destination ID to match against
18575  *
18576  * This function searches the @phba for a vport that matches the content of the
18577  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18578  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18579  * returns the matching vport pointer or NULL if unable to match frame to a
18580  * vport.
18581  **/
18582 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)18583 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
18584 		       uint16_t fcfi, uint32_t did)
18585 {
18586 	struct lpfc_vport **vports;
18587 	struct lpfc_vport *vport = NULL;
18588 	int i;
18589 
18590 	if (did == Fabric_DID)
18591 		return phba->pport;
18592 	if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
18593 	    phba->link_state != LPFC_HBA_READY)
18594 		return phba->pport;
18595 
18596 	vports = lpfc_create_vport_work_array(phba);
18597 	if (vports != NULL) {
18598 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18599 			if (phba->fcf.fcfi == fcfi &&
18600 			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18601 			    vports[i]->fc_myDID == did) {
18602 				vport = vports[i];
18603 				break;
18604 			}
18605 		}
18606 	}
18607 	lpfc_destroy_vport_work_array(phba, vports);
18608 	return vport;
18609 }
18610 
18611 /**
18612  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18613  * @vport: The vport to work on.
18614  *
18615  * This function updates the receive sequence time stamp for this vport. The
18616  * receive sequence time stamp indicates the time that the last frame of the
18617  * the sequence that has been idle for the longest amount of time was received.
18618  * the driver uses this time stamp to indicate if any received sequences have
18619  * timed out.
18620  **/
18621 static void
lpfc_update_rcv_time_stamp(struct lpfc_vport * vport)18622 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18623 {
18624 	struct lpfc_dmabuf *h_buf;
18625 	struct hbq_dmabuf *dmabuf = NULL;
18626 
18627 	/* get the oldest sequence on the rcv list */
18628 	h_buf = list_get_first(&vport->rcv_buffer_list,
18629 			       struct lpfc_dmabuf, list);
18630 	if (!h_buf)
18631 		return;
18632 	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18633 	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18634 }
18635 
18636 /**
18637  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18638  * @vport: The vport that the received sequences were sent to.
18639  *
18640  * This function cleans up all outstanding received sequences. This is called
18641  * by the driver when a link event or user action invalidates all the received
18642  * sequences.
18643  **/
18644 void
lpfc_cleanup_rcv_buffers(struct lpfc_vport * vport)18645 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18646 {
18647 	struct lpfc_dmabuf *h_buf, *hnext;
18648 	struct lpfc_dmabuf *d_buf, *dnext;
18649 	struct hbq_dmabuf *dmabuf = NULL;
18650 
18651 	/* start with the oldest sequence on the rcv list */
18652 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18653 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18654 		list_del_init(&dmabuf->hbuf.list);
18655 		list_for_each_entry_safe(d_buf, dnext,
18656 					 &dmabuf->dbuf.list, list) {
18657 			list_del_init(&d_buf->list);
18658 			lpfc_in_buf_free(vport->phba, d_buf);
18659 		}
18660 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18661 	}
18662 }
18663 
18664 /**
18665  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18666  * @vport: The vport that the received sequences were sent to.
18667  *
18668  * This function determines whether any received sequences have timed out by
18669  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18670  * indicates that there is at least one timed out sequence this routine will
18671  * go through the received sequences one at a time from most inactive to most
18672  * active to determine which ones need to be cleaned up. Once it has determined
18673  * that a sequence needs to be cleaned up it will simply free up the resources
18674  * without sending an abort.
18675  **/
18676 void
lpfc_rcv_seq_check_edtov(struct lpfc_vport * vport)18677 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18678 {
18679 	struct lpfc_dmabuf *h_buf, *hnext;
18680 	struct lpfc_dmabuf *d_buf, *dnext;
18681 	struct hbq_dmabuf *dmabuf = NULL;
18682 	unsigned long timeout;
18683 	int abort_count = 0;
18684 
18685 	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18686 		   vport->rcv_buffer_time_stamp);
18687 	if (list_empty(&vport->rcv_buffer_list) ||
18688 	    time_before(jiffies, timeout))
18689 		return;
18690 	/* start with the oldest sequence on the rcv list */
18691 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18692 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18693 		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18694 			   dmabuf->time_stamp);
18695 		if (time_before(jiffies, timeout))
18696 			break;
18697 		abort_count++;
18698 		list_del_init(&dmabuf->hbuf.list);
18699 		list_for_each_entry_safe(d_buf, dnext,
18700 					 &dmabuf->dbuf.list, list) {
18701 			list_del_init(&d_buf->list);
18702 			lpfc_in_buf_free(vport->phba, d_buf);
18703 		}
18704 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18705 	}
18706 	if (abort_count)
18707 		lpfc_update_rcv_time_stamp(vport);
18708 }
18709 
18710 /**
18711  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
18712  * @vport: pointer to a vitural port
18713  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18714  *
18715  * This function searches through the existing incomplete sequences that have
18716  * been sent to this @vport. If the frame matches one of the incomplete
18717  * sequences then the dbuf in the @dmabuf is added to the list of frames that
18718  * make up that sequence. If no sequence is found that matches this frame then
18719  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18720  * This function returns a pointer to the first dmabuf in the sequence list that
18721  * the frame was linked to.
18722  **/
18723 static struct hbq_dmabuf *
lpfc_fc_frame_add(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18724 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18725 {
18726 	struct fc_frame_header *new_hdr;
18727 	struct fc_frame_header *temp_hdr;
18728 	struct lpfc_dmabuf *d_buf;
18729 	struct lpfc_dmabuf *h_buf;
18730 	struct hbq_dmabuf *seq_dmabuf = NULL;
18731 	struct hbq_dmabuf *temp_dmabuf = NULL;
18732 	uint8_t	found = 0;
18733 
18734 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18735 	dmabuf->time_stamp = jiffies;
18736 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18737 
18738 	/* Use the hdr_buf to find the sequence that this frame belongs to */
18739 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18740 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18741 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18742 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18743 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18744 			continue;
18745 		/* found a pending sequence that matches this frame */
18746 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18747 		break;
18748 	}
18749 	if (!seq_dmabuf) {
18750 		/*
18751 		 * This indicates first frame received for this sequence.
18752 		 * Queue the buffer on the vport's rcv_buffer_list.
18753 		 */
18754 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18755 		lpfc_update_rcv_time_stamp(vport);
18756 		return dmabuf;
18757 	}
18758 	temp_hdr = seq_dmabuf->hbuf.virt;
18759 	if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18760 		be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18761 		list_del_init(&seq_dmabuf->hbuf.list);
18762 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18763 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18764 		lpfc_update_rcv_time_stamp(vport);
18765 		return dmabuf;
18766 	}
18767 	/* move this sequence to the tail to indicate a young sequence */
18768 	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18769 	seq_dmabuf->time_stamp = jiffies;
18770 	lpfc_update_rcv_time_stamp(vport);
18771 	if (list_empty(&seq_dmabuf->dbuf.list)) {
18772 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18773 		return seq_dmabuf;
18774 	}
18775 	/* find the correct place in the sequence to insert this frame */
18776 	d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18777 	while (!found) {
18778 		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18779 		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18780 		/*
18781 		 * If the frame's sequence count is greater than the frame on
18782 		 * the list then insert the frame right after this frame
18783 		 */
18784 		if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18785 			be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18786 			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
18787 			found = 1;
18788 			break;
18789 		}
18790 
18791 		if (&d_buf->list == &seq_dmabuf->dbuf.list)
18792 			break;
18793 		d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
18794 	}
18795 
18796 	if (found)
18797 		return seq_dmabuf;
18798 	return NULL;
18799 }
18800 
18801 /**
18802  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18803  * @vport: pointer to a vitural port
18804  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18805  *
18806  * This function tries to abort from the partially assembed sequence, described
18807  * by the information from basic abbort @dmabuf. It checks to see whether such
18808  * partially assembled sequence held by the driver. If so, it shall free up all
18809  * the frames from the partially assembled sequence.
18810  *
18811  * Return
18812  * true  -- if there is matching partially assembled sequence present and all
18813  *          the frames freed with the sequence;
18814  * false -- if there is no matching partially assembled sequence present so
18815  *          nothing got aborted in the lower layer driver
18816  **/
18817 static bool
lpfc_sli4_abort_partial_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18818 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18819 			    struct hbq_dmabuf *dmabuf)
18820 {
18821 	struct fc_frame_header *new_hdr;
18822 	struct fc_frame_header *temp_hdr;
18823 	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18824 	struct hbq_dmabuf *seq_dmabuf = NULL;
18825 
18826 	/* Use the hdr_buf to find the sequence that matches this frame */
18827 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18828 	INIT_LIST_HEAD(&dmabuf->hbuf.list);
18829 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18830 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18831 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18832 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18833 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18834 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18835 			continue;
18836 		/* found a pending sequence that matches this frame */
18837 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18838 		break;
18839 	}
18840 
18841 	/* Free up all the frames from the partially assembled sequence */
18842 	if (seq_dmabuf) {
18843 		list_for_each_entry_safe(d_buf, n_buf,
18844 					 &seq_dmabuf->dbuf.list, list) {
18845 			list_del_init(&d_buf->list);
18846 			lpfc_in_buf_free(vport->phba, d_buf);
18847 		}
18848 		return true;
18849 	}
18850 	return false;
18851 }
18852 
18853 /**
18854  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18855  * @vport: pointer to a vitural port
18856  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18857  *
18858  * This function tries to abort from the assembed sequence from upper level
18859  * protocol, described by the information from basic abbort @dmabuf. It
18860  * checks to see whether such pending context exists at upper level protocol.
18861  * If so, it shall clean up the pending context.
18862  *
18863  * Return
18864  * true  -- if there is matching pending context of the sequence cleaned
18865  *          at ulp;
18866  * false -- if there is no matching pending context of the sequence present
18867  *          at ulp.
18868  **/
18869 static bool
lpfc_sli4_abort_ulp_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18870 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18871 {
18872 	struct lpfc_hba *phba = vport->phba;
18873 	int handled;
18874 
18875 	/* Accepting abort at ulp with SLI4 only */
18876 	if (phba->sli_rev < LPFC_SLI_REV4)
18877 		return false;
18878 
18879 	/* Register all caring upper level protocols to attend abort */
18880 	handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18881 	if (handled)
18882 		return true;
18883 
18884 	return false;
18885 }
18886 
18887 /**
18888  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18889  * @phba: Pointer to HBA context object.
18890  * @cmd_iocbq: pointer to the command iocbq structure.
18891  * @rsp_iocbq: pointer to the response iocbq structure.
18892  *
18893  * This function handles the sequence abort response iocb command complete
18894  * event. It properly releases the memory allocated to the sequence abort
18895  * accept iocb.
18896  **/
18897 static void
lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmd_iocbq,struct lpfc_iocbq * rsp_iocbq)18898 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18899 			     struct lpfc_iocbq *cmd_iocbq,
18900 			     struct lpfc_iocbq *rsp_iocbq)
18901 {
18902 	if (cmd_iocbq) {
18903 		lpfc_nlp_put(cmd_iocbq->ndlp);
18904 		lpfc_sli_release_iocbq(phba, cmd_iocbq);
18905 	}
18906 
18907 	/* Failure means BLS ABORT RSP did not get delivered to remote node*/
18908 	if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18909 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18910 			"3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18911 			get_job_ulpstatus(phba, rsp_iocbq),
18912 			get_job_word4(phba, rsp_iocbq));
18913 }
18914 
18915 /**
18916  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18917  * @phba: Pointer to HBA context object.
18918  * @xri: xri id in transaction.
18919  *
18920  * This function validates the xri maps to the known range of XRIs allocated an
18921  * used by the driver.
18922  **/
18923 uint16_t
lpfc_sli4_xri_inrange(struct lpfc_hba * phba,uint16_t xri)18924 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18925 		      uint16_t xri)
18926 {
18927 	uint16_t i;
18928 
18929 	for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18930 		if (xri == phba->sli4_hba.xri_ids[i])
18931 			return i;
18932 	}
18933 	return NO_XRI;
18934 }
18935 
18936 /**
18937  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
18938  * @vport: pointer to a virtual port.
18939  * @fc_hdr: pointer to a FC frame header.
18940  * @aborted: was the partially assembled receive sequence successfully aborted
18941  *
18942  * This function sends a basic response to a previous unsol sequence abort
18943  * event after aborting the sequence handling.
18944  **/
18945 void
lpfc_sli4_seq_abort_rsp(struct lpfc_vport * vport,struct fc_frame_header * fc_hdr,bool aborted)18946 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18947 			struct fc_frame_header *fc_hdr, bool aborted)
18948 {
18949 	struct lpfc_hba *phba = vport->phba;
18950 	struct lpfc_iocbq *ctiocb = NULL;
18951 	struct lpfc_nodelist *ndlp;
18952 	uint16_t oxid, rxid, xri, lxri;
18953 	uint32_t sid, fctl;
18954 	union lpfc_wqe128 *icmd;
18955 	int rc;
18956 
18957 	if (!lpfc_is_link_up(phba))
18958 		return;
18959 
18960 	sid = sli4_sid_from_fc_hdr(fc_hdr);
18961 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
18962 	rxid = be16_to_cpu(fc_hdr->fh_rx_id);
18963 
18964 	ndlp = lpfc_findnode_did(vport, sid);
18965 	if (!ndlp) {
18966 		ndlp = lpfc_nlp_init(vport, sid);
18967 		if (!ndlp) {
18968 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18969 					 "1268 Failed to allocate ndlp for "
18970 					 "oxid:x%x SID:x%x\n", oxid, sid);
18971 			return;
18972 		}
18973 		/* Put ndlp onto vport node list */
18974 		lpfc_enqueue_node(vport, ndlp);
18975 	}
18976 
18977 	/* Allocate buffer for rsp iocb */
18978 	ctiocb = lpfc_sli_get_iocbq(phba);
18979 	if (!ctiocb)
18980 		return;
18981 
18982 	icmd = &ctiocb->wqe;
18983 
18984 	/* Extract the F_CTL field from FC_HDR */
18985 	fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18986 
18987 	ctiocb->ndlp = lpfc_nlp_get(ndlp);
18988 	if (!ctiocb->ndlp) {
18989 		lpfc_sli_release_iocbq(phba, ctiocb);
18990 		return;
18991 	}
18992 
18993 	ctiocb->vport = vport;
18994 	ctiocb->cmd_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
18995 	ctiocb->sli4_lxritag = NO_XRI;
18996 	ctiocb->sli4_xritag = NO_XRI;
18997 	ctiocb->abort_rctl = FC_RCTL_BA_ACC;
18998 
18999 	if (fctl & FC_FC_EX_CTX)
19000 		/* Exchange responder sent the abort so we
19001 		 * own the oxid.
19002 		 */
19003 		xri = oxid;
19004 	else
19005 		xri = rxid;
19006 	lxri = lpfc_sli4_xri_inrange(phba, xri);
19007 	if (lxri != NO_XRI)
19008 		lpfc_set_rrq_active(phba, ndlp, lxri,
19009 			(xri == oxid) ? rxid : oxid, 0);
19010 	/* For BA_ABTS from exchange responder, if the logical xri with
19011 	 * the oxid maps to the FCP XRI range, the port no longer has
19012 	 * that exchange context, send a BLS_RJT. Override the IOCB for
19013 	 * a BA_RJT.
19014 	 */
19015 	if ((fctl & FC_FC_EX_CTX) &&
19016 	    (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
19017 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19018 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19019 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19020 		       FC_BA_RJT_INV_XID);
19021 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19022 		       FC_BA_RJT_UNABLE);
19023 	}
19024 
19025 	/* If BA_ABTS failed to abort a partially assembled receive sequence,
19026 	 * the driver no longer has that exchange, send a BLS_RJT. Override
19027 	 * the IOCB for a BA_RJT.
19028 	 */
19029 	if (aborted == false) {
19030 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19031 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19032 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19033 		       FC_BA_RJT_INV_XID);
19034 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19035 		       FC_BA_RJT_UNABLE);
19036 	}
19037 
19038 	if (fctl & FC_FC_EX_CTX) {
19039 		/* ABTS sent by responder to CT exchange, construction
19040 		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
19041 		 * field and RX_ID from ABTS for RX_ID field.
19042 		 */
19043 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_RSP;
19044 		bf_set(xmit_bls_rsp64_rxid, &icmd->xmit_bls_rsp, rxid);
19045 	} else {
19046 		/* ABTS sent by initiator to CT exchange, construction
19047 		 * of BA_ACC will need to allocate a new XRI as for the
19048 		 * XRI_TAG field.
19049 		 */
19050 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_INT;
19051 	}
19052 
19053 	/* OX_ID is invariable to who sent ABTS to CT exchange */
19054 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, oxid);
19055 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, rxid);
19056 
19057 	/* Use CT=VPI */
19058 	bf_set(wqe_els_did, &icmd->xmit_bls_rsp.wqe_dest,
19059 	       ndlp->nlp_DID);
19060 	bf_set(xmit_bls_rsp64_temprpi, &icmd->xmit_bls_rsp,
19061 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
19062 	bf_set(wqe_cmnd, &icmd->generic.wqe_com, CMD_XMIT_BLS_RSP64_CX);
19063 
19064 	/* Xmit CT abts response on exchange <xid> */
19065 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
19066 			 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
19067 			 ctiocb->abort_rctl, oxid, phba->link_state);
19068 
19069 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
19070 	if (rc == IOCB_ERROR) {
19071 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19072 				 "2925 Failed to issue CT ABTS RSP x%x on "
19073 				 "xri x%x, Data x%x\n",
19074 				 ctiocb->abort_rctl, oxid,
19075 				 phba->link_state);
19076 		lpfc_nlp_put(ndlp);
19077 		ctiocb->ndlp = NULL;
19078 		lpfc_sli_release_iocbq(phba, ctiocb);
19079 	}
19080 
19081 	/* if only usage of this nodelist is BLS response, release initial ref
19082 	 * to free ndlp when transmit completes
19083 	 */
19084 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE &&
19085 	    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
19086 	    !(ndlp->fc4_xpt_flags & (NVME_XPT_REGD | SCSI_XPT_REGD))) {
19087 		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
19088 		lpfc_nlp_put(ndlp);
19089 	}
19090 }
19091 
19092 /**
19093  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
19094  * @vport: Pointer to the vport on which this sequence was received
19095  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19096  *
19097  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
19098  * receive sequence is only partially assembed by the driver, it shall abort
19099  * the partially assembled frames for the sequence. Otherwise, if the
19100  * unsolicited receive sequence has been completely assembled and passed to
19101  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
19102  * unsolicited sequence has been aborted. After that, it will issue a basic
19103  * accept to accept the abort.
19104  **/
19105 static void
lpfc_sli4_handle_unsol_abort(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19106 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
19107 			     struct hbq_dmabuf *dmabuf)
19108 {
19109 	struct lpfc_hba *phba = vport->phba;
19110 	struct fc_frame_header fc_hdr;
19111 	uint32_t fctl;
19112 	bool aborted;
19113 
19114 	/* Make a copy of fc_hdr before the dmabuf being released */
19115 	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
19116 	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
19117 
19118 	if (fctl & FC_FC_EX_CTX) {
19119 		/* ABTS by responder to exchange, no cleanup needed */
19120 		aborted = true;
19121 	} else {
19122 		/* ABTS by initiator to exchange, need to do cleanup */
19123 		aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19124 		if (aborted == false)
19125 			aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
19126 	}
19127 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19128 
19129 	if (phba->nvmet_support) {
19130 		lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19131 		return;
19132 	}
19133 
19134 	/* Respond with BA_ACC or BA_RJT accordingly */
19135 	lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
19136 }
19137 
19138 /**
19139  * lpfc_seq_complete - Indicates if a sequence is complete
19140  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19141  *
19142  * This function checks the sequence, starting with the frame described by
19143  * @dmabuf, to see if all the frames associated with this sequence are present.
19144  * the frames associated with this sequence are linked to the @dmabuf using the
19145  * dbuf list. This function looks for two major things. 1) That the first frame
19146  * has a sequence count of zero. 2) There is a frame with last frame of sequence
19147  * set. 3) That there are no holes in the sequence count. The function will
19148  * return 1 when the sequence is complete, otherwise it will return 0.
19149  **/
19150 static int
lpfc_seq_complete(struct hbq_dmabuf * dmabuf)19151 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19152 {
19153 	struct fc_frame_header *hdr;
19154 	struct lpfc_dmabuf *d_buf;
19155 	struct hbq_dmabuf *seq_dmabuf;
19156 	uint32_t fctl;
19157 	int seq_count = 0;
19158 
19159 	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19160 	/* make sure first fame of sequence has a sequence count of zero */
19161 	if (hdr->fh_seq_cnt != seq_count)
19162 		return 0;
19163 	fctl = (hdr->fh_f_ctl[0] << 16 |
19164 		hdr->fh_f_ctl[1] << 8 |
19165 		hdr->fh_f_ctl[2]);
19166 	/* If last frame of sequence we can return success. */
19167 	if (fctl & FC_FC_END_SEQ)
19168 		return 1;
19169 	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19170 		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19171 		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19172 		/* If there is a hole in the sequence count then fail. */
19173 		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
19174 			return 0;
19175 		fctl = (hdr->fh_f_ctl[0] << 16 |
19176 			hdr->fh_f_ctl[1] << 8 |
19177 			hdr->fh_f_ctl[2]);
19178 		/* If last frame of sequence we can return success. */
19179 		if (fctl & FC_FC_END_SEQ)
19180 			return 1;
19181 	}
19182 	return 0;
19183 }
19184 
19185 /**
19186  * lpfc_prep_seq - Prep sequence for ULP processing
19187  * @vport: Pointer to the vport on which this sequence was received
19188  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
19189  *
19190  * This function takes a sequence, described by a list of frames, and creates
19191  * a list of iocbq structures to describe the sequence. This iocbq list will be
19192  * used to issue to the generic unsolicited sequence handler. This routine
19193  * returns a pointer to the first iocbq in the list. If the function is unable
19194  * to allocate an iocbq then it throw out the received frames that were not
19195  * able to be described and return a pointer to the first iocbq. If unable to
19196  * allocate any iocbqs (including the first) this function will return NULL.
19197  **/
19198 static struct lpfc_iocbq *
lpfc_prep_seq(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19199 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19200 {
19201 	struct hbq_dmabuf *hbq_buf;
19202 	struct lpfc_dmabuf *d_buf, *n_buf;
19203 	struct lpfc_iocbq *first_iocbq, *iocbq;
19204 	struct fc_frame_header *fc_hdr;
19205 	uint32_t sid;
19206 	uint32_t len, tot_len;
19207 
19208 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19209 	/* remove from receive buffer list */
19210 	list_del_init(&seq_dmabuf->hbuf.list);
19211 	lpfc_update_rcv_time_stamp(vport);
19212 	/* get the Remote Port's SID */
19213 	sid = sli4_sid_from_fc_hdr(fc_hdr);
19214 	tot_len = 0;
19215 	/* Get an iocbq struct to fill in. */
19216 	first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19217 	if (first_iocbq) {
19218 		/* Initialize the first IOCB. */
19219 		first_iocbq->wcqe_cmpl.total_data_placed = 0;
19220 		bf_set(lpfc_wcqe_c_status, &first_iocbq->wcqe_cmpl,
19221 		       IOSTAT_SUCCESS);
19222 		first_iocbq->vport = vport;
19223 
19224 		/* Check FC Header to see what TYPE of frame we are rcv'ing */
19225 		if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19226 			bf_set(els_rsp64_sid, &first_iocbq->wqe.xmit_els_rsp,
19227 			       sli4_did_from_fc_hdr(fc_hdr));
19228 		}
19229 
19230 		bf_set(wqe_ctxt_tag, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19231 		       NO_XRI);
19232 		bf_set(wqe_rcvoxid, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19233 		       be16_to_cpu(fc_hdr->fh_ox_id));
19234 
19235 		/* put the first buffer into the first iocb */
19236 		tot_len = bf_get(lpfc_rcqe_length,
19237 				 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19238 
19239 		first_iocbq->cmd_dmabuf = &seq_dmabuf->dbuf;
19240 		first_iocbq->bpl_dmabuf = NULL;
19241 		/* Keep track of the BDE count */
19242 		first_iocbq->wcqe_cmpl.word3 = 1;
19243 
19244 		if (tot_len > LPFC_DATA_BUF_SIZE)
19245 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize =
19246 				LPFC_DATA_BUF_SIZE;
19247 		else
19248 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize = tot_len;
19249 
19250 		first_iocbq->wcqe_cmpl.total_data_placed = tot_len;
19251 		bf_set(wqe_els_did, &first_iocbq->wqe.xmit_els_rsp.wqe_dest,
19252 		       sid);
19253 	}
19254 	iocbq = first_iocbq;
19255 	/*
19256 	 * Each IOCBq can have two Buffers assigned, so go through the list
19257 	 * of buffers for this sequence and save two buffers in each IOCBq
19258 	 */
19259 	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19260 		if (!iocbq) {
19261 			lpfc_in_buf_free(vport->phba, d_buf);
19262 			continue;
19263 		}
19264 		if (!iocbq->bpl_dmabuf) {
19265 			iocbq->bpl_dmabuf = d_buf;
19266 			iocbq->wcqe_cmpl.word3++;
19267 			/* We need to get the size out of the right CQE */
19268 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19269 			len = bf_get(lpfc_rcqe_length,
19270 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19271 			iocbq->unsol_rcv_len = len;
19272 			iocbq->wcqe_cmpl.total_data_placed += len;
19273 			tot_len += len;
19274 		} else {
19275 			iocbq = lpfc_sli_get_iocbq(vport->phba);
19276 			if (!iocbq) {
19277 				if (first_iocbq) {
19278 					bf_set(lpfc_wcqe_c_status,
19279 					       &first_iocbq->wcqe_cmpl,
19280 					       IOSTAT_SUCCESS);
19281 					first_iocbq->wcqe_cmpl.parameter =
19282 						IOERR_NO_RESOURCES;
19283 				}
19284 				lpfc_in_buf_free(vport->phba, d_buf);
19285 				continue;
19286 			}
19287 			/* We need to get the size out of the right CQE */
19288 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19289 			len = bf_get(lpfc_rcqe_length,
19290 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19291 			iocbq->cmd_dmabuf = d_buf;
19292 			iocbq->bpl_dmabuf = NULL;
19293 			iocbq->wcqe_cmpl.word3 = 1;
19294 
19295 			if (len > LPFC_DATA_BUF_SIZE)
19296 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19297 					LPFC_DATA_BUF_SIZE;
19298 			else
19299 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19300 					len;
19301 
19302 			tot_len += len;
19303 			iocbq->wcqe_cmpl.total_data_placed = tot_len;
19304 			bf_set(wqe_els_did, &iocbq->wqe.xmit_els_rsp.wqe_dest,
19305 			       sid);
19306 			list_add_tail(&iocbq->list, &first_iocbq->list);
19307 		}
19308 	}
19309 	/* Free the sequence's header buffer */
19310 	if (!first_iocbq)
19311 		lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19312 
19313 	return first_iocbq;
19314 }
19315 
19316 static void
lpfc_sli4_send_seq_to_ulp(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19317 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19318 			  struct hbq_dmabuf *seq_dmabuf)
19319 {
19320 	struct fc_frame_header *fc_hdr;
19321 	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19322 	struct lpfc_hba *phba = vport->phba;
19323 
19324 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19325 	iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19326 	if (!iocbq) {
19327 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19328 				"2707 Ring %d handler: Failed to allocate "
19329 				"iocb Rctl x%x Type x%x received\n",
19330 				LPFC_ELS_RING,
19331 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19332 		return;
19333 	}
19334 	if (!lpfc_complete_unsol_iocb(phba,
19335 				      phba->sli4_hba.els_wq->pring,
19336 				      iocbq, fc_hdr->fh_r_ctl,
19337 				      fc_hdr->fh_type)) {
19338 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19339 				"2540 Ring %d handler: unexpected Rctl "
19340 				"x%x Type x%x received\n",
19341 				LPFC_ELS_RING,
19342 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19343 		lpfc_in_buf_free(phba, &seq_dmabuf->dbuf);
19344 	}
19345 
19346 	/* Free iocb created in lpfc_prep_seq */
19347 	list_for_each_entry_safe(curr_iocb, next_iocb,
19348 				 &iocbq->list, list) {
19349 		list_del_init(&curr_iocb->list);
19350 		lpfc_sli_release_iocbq(phba, curr_iocb);
19351 	}
19352 	lpfc_sli_release_iocbq(phba, iocbq);
19353 }
19354 
19355 static void
lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)19356 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19357 			    struct lpfc_iocbq *rspiocb)
19358 {
19359 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
19360 
19361 	if (pcmd && pcmd->virt)
19362 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19363 	kfree(pcmd);
19364 	lpfc_sli_release_iocbq(phba, cmdiocb);
19365 	lpfc_drain_txq(phba);
19366 }
19367 
19368 static void
lpfc_sli4_handle_mds_loopback(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19369 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19370 			      struct hbq_dmabuf *dmabuf)
19371 {
19372 	struct fc_frame_header *fc_hdr;
19373 	struct lpfc_hba *phba = vport->phba;
19374 	struct lpfc_iocbq *iocbq = NULL;
19375 	union  lpfc_wqe128 *pwqe;
19376 	struct lpfc_dmabuf *pcmd = NULL;
19377 	uint32_t frame_len;
19378 	int rc;
19379 	unsigned long iflags;
19380 
19381 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19382 	frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19383 
19384 	/* Send the received frame back */
19385 	iocbq = lpfc_sli_get_iocbq(phba);
19386 	if (!iocbq) {
19387 		/* Queue cq event and wakeup worker thread to process it */
19388 		spin_lock_irqsave(&phba->hbalock, iflags);
19389 		list_add_tail(&dmabuf->cq_event.list,
19390 			      &phba->sli4_hba.sp_queue_event);
19391 		spin_unlock_irqrestore(&phba->hbalock, iflags);
19392 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
19393 		lpfc_worker_wake_up(phba);
19394 		return;
19395 	}
19396 
19397 	/* Allocate buffer for command payload */
19398 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19399 	if (pcmd)
19400 		pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
19401 					    &pcmd->phys);
19402 	if (!pcmd || !pcmd->virt)
19403 		goto exit;
19404 
19405 	INIT_LIST_HEAD(&pcmd->list);
19406 
19407 	/* copyin the payload */
19408 	memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19409 
19410 	iocbq->cmd_dmabuf = pcmd;
19411 	iocbq->vport = vport;
19412 	iocbq->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
19413 	iocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
19414 	iocbq->num_bdes = 0;
19415 
19416 	pwqe = &iocbq->wqe;
19417 	/* fill in BDE's for command */
19418 	pwqe->gen_req.bde.addrHigh = putPaddrHigh(pcmd->phys);
19419 	pwqe->gen_req.bde.addrLow = putPaddrLow(pcmd->phys);
19420 	pwqe->gen_req.bde.tus.f.bdeSize = frame_len;
19421 	pwqe->gen_req.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
19422 
19423 	pwqe->send_frame.frame_len = frame_len;
19424 	pwqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((__be32 *)fc_hdr));
19425 	pwqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((__be32 *)fc_hdr + 1));
19426 	pwqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((__be32 *)fc_hdr + 2));
19427 	pwqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((__be32 *)fc_hdr + 3));
19428 	pwqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((__be32 *)fc_hdr + 4));
19429 	pwqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((__be32 *)fc_hdr + 5));
19430 
19431 	pwqe->generic.wqe_com.word7 = 0;
19432 	pwqe->generic.wqe_com.word10 = 0;
19433 
19434 	bf_set(wqe_cmnd, &pwqe->generic.wqe_com, CMD_SEND_FRAME);
19435 	bf_set(wqe_sof, &pwqe->generic.wqe_com, 0x2E); /* SOF byte */
19436 	bf_set(wqe_eof, &pwqe->generic.wqe_com, 0x41); /* EOF byte */
19437 	bf_set(wqe_lenloc, &pwqe->generic.wqe_com, 1);
19438 	bf_set(wqe_xbl, &pwqe->generic.wqe_com, 1);
19439 	bf_set(wqe_dbde, &pwqe->generic.wqe_com, 1);
19440 	bf_set(wqe_xc, &pwqe->generic.wqe_com, 1);
19441 	bf_set(wqe_cmd_type, &pwqe->generic.wqe_com, 0xA);
19442 	bf_set(wqe_cqid, &pwqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
19443 	bf_set(wqe_xri_tag, &pwqe->generic.wqe_com, iocbq->sli4_xritag);
19444 	bf_set(wqe_reqtag, &pwqe->generic.wqe_com, iocbq->iotag);
19445 	bf_set(wqe_class, &pwqe->generic.wqe_com, CLASS3);
19446 	pwqe->generic.wqe_com.abort_tag = iocbq->iotag;
19447 
19448 	iocbq->cmd_cmpl = lpfc_sli4_mds_loopback_cmpl;
19449 
19450 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19451 	if (rc == IOCB_ERROR)
19452 		goto exit;
19453 
19454 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19455 	return;
19456 
19457 exit:
19458 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19459 			"2023 Unable to process MDS loopback frame\n");
19460 	if (pcmd && pcmd->virt)
19461 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19462 	kfree(pcmd);
19463 	if (iocbq)
19464 		lpfc_sli_release_iocbq(phba, iocbq);
19465 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19466 }
19467 
19468 /**
19469  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19470  * @phba: Pointer to HBA context object.
19471  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
19472  *
19473  * This function is called with no lock held. This function processes all
19474  * the received buffers and gives it to upper layers when a received buffer
19475  * indicates that it is the final frame in the sequence. The interrupt
19476  * service routine processes received buffers at interrupt contexts.
19477  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19478  * appropriate receive function when the final frame in a sequence is received.
19479  **/
19480 void
lpfc_sli4_handle_received_buffer(struct lpfc_hba * phba,struct hbq_dmabuf * dmabuf)19481 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19482 				 struct hbq_dmabuf *dmabuf)
19483 {
19484 	struct hbq_dmabuf *seq_dmabuf;
19485 	struct fc_frame_header *fc_hdr;
19486 	struct lpfc_vport *vport;
19487 	uint32_t fcfi;
19488 	uint32_t did;
19489 
19490 	/* Process each received buffer */
19491 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19492 
19493 	if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19494 	    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19495 		vport = phba->pport;
19496 		/* Handle MDS Loopback frames */
19497 		if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
19498 			lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19499 		else
19500 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19501 		return;
19502 	}
19503 
19504 	/* check to see if this a valid type of frame */
19505 	if (lpfc_fc_frame_check(phba, fc_hdr)) {
19506 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19507 		return;
19508 	}
19509 
19510 	if ((bf_get(lpfc_cqe_code,
19511 		    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19512 		fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19513 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19514 	else
19515 		fcfi = bf_get(lpfc_rcqe_fcf_id,
19516 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19517 
19518 	if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19519 		vport = phba->pport;
19520 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19521 				"2023 MDS Loopback %d bytes\n",
19522 				bf_get(lpfc_rcqe_length,
19523 				       &dmabuf->cq_event.cqe.rcqe_cmpl));
19524 		/* Handle MDS Loopback frames */
19525 		lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19526 		return;
19527 	}
19528 
19529 	/* d_id this frame is directed to */
19530 	did = sli4_did_from_fc_hdr(fc_hdr);
19531 
19532 	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
19533 	if (!vport) {
19534 		/* throw out the frame */
19535 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19536 		return;
19537 	}
19538 
19539 	/* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19540 	if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19541 		(did != Fabric_DID)) {
19542 		/*
19543 		 * Throw out the frame if we are not pt2pt.
19544 		 * The pt2pt protocol allows for discovery frames
19545 		 * to be received without a registered VPI.
19546 		 */
19547 		if (!test_bit(FC_PT2PT, &vport->fc_flag) ||
19548 		    phba->link_state == LPFC_HBA_READY) {
19549 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19550 			return;
19551 		}
19552 	}
19553 
19554 	/* Handle the basic abort sequence (BA_ABTS) event */
19555 	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19556 		lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19557 		return;
19558 	}
19559 
19560 	/* Link this frame */
19561 	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19562 	if (!seq_dmabuf) {
19563 		/* unable to add frame to vport - throw it out */
19564 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19565 		return;
19566 	}
19567 	/* If not last frame in sequence continue processing frames. */
19568 	if (!lpfc_seq_complete(seq_dmabuf))
19569 		return;
19570 
19571 	/* Send the complete sequence to the upper layer protocol */
19572 	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
19573 }
19574 
19575 /**
19576  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19577  * @phba: pointer to lpfc hba data structure.
19578  *
19579  * This routine is invoked to post rpi header templates to the
19580  * HBA consistent with the SLI-4 interface spec.  This routine
19581  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19582  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19583  *
19584  * This routine does not require any locks.  It's usage is expected
19585  * to be driver load or reset recovery when the driver is
19586  * sequential.
19587  *
19588  * Return codes
19589  * 	0 - successful
19590  *      -EIO - The mailbox failed to complete successfully.
19591  * 	When this error occurs, the driver is not guaranteed
19592  *	to have any rpi regions posted to the device and
19593  *	must either attempt to repost the regions or take a
19594  *	fatal error.
19595  **/
19596 int
lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba * phba)19597 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19598 {
19599 	struct lpfc_rpi_hdr *rpi_page;
19600 	uint32_t rc = 0;
19601 	uint16_t lrpi = 0;
19602 
19603 	/* SLI4 ports that support extents do not require RPI headers. */
19604 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19605 		goto exit;
19606 	if (phba->sli4_hba.extents_in_use)
19607 		return -EIO;
19608 
19609 	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
19610 		/*
19611 		 * Assign the rpi headers a physical rpi only if the driver
19612 		 * has not initialized those resources.  A port reset only
19613 		 * needs the headers posted.
19614 		 */
19615 		if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19616 		    LPFC_RPI_RSRC_RDY)
19617 			rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19618 
19619 		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19620 		if (rc != MBX_SUCCESS) {
19621 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19622 					"2008 Error %d posting all rpi "
19623 					"headers\n", rc);
19624 			rc = -EIO;
19625 			break;
19626 		}
19627 	}
19628 
19629  exit:
19630 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19631 	       LPFC_RPI_RSRC_RDY);
19632 	return rc;
19633 }
19634 
19635 /**
19636  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19637  * @phba: pointer to lpfc hba data structure.
19638  * @rpi_page:  pointer to the rpi memory region.
19639  *
19640  * This routine is invoked to post a single rpi header to the
19641  * HBA consistent with the SLI-4 interface spec.  This memory region
19642  * maps up to 64 rpi context regions.
19643  *
19644  * Return codes
19645  * 	0 - successful
19646  * 	-ENOMEM - No available memory
19647  *      -EIO - The mailbox failed to complete successfully.
19648  **/
19649 int
lpfc_sli4_post_rpi_hdr(struct lpfc_hba * phba,struct lpfc_rpi_hdr * rpi_page)19650 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19651 {
19652 	LPFC_MBOXQ_t *mboxq;
19653 	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19654 	uint32_t rc = 0;
19655 	uint32_t shdr_status, shdr_add_status;
19656 	union lpfc_sli4_cfg_shdr *shdr;
19657 
19658 	/* SLI4 ports that support extents do not require RPI headers. */
19659 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19660 		return rc;
19661 	if (phba->sli4_hba.extents_in_use)
19662 		return -EIO;
19663 
19664 	/* The port is notified of the header region via a mailbox command. */
19665 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19666 	if (!mboxq) {
19667 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19668 				"2001 Unable to allocate memory for issuing "
19669 				"SLI_CONFIG_SPECIAL mailbox command\n");
19670 		return -ENOMEM;
19671 	}
19672 
19673 	/* Post all rpi memory regions to the port. */
19674 	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
19675 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19676 			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19677 			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
19678 			 sizeof(struct lpfc_sli4_cfg_mhdr),
19679 			 LPFC_SLI4_MBX_EMBED);
19680 
19681 
19682 	/* Post the physical rpi to the port for this rpi header. */
19683 	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19684 	       rpi_page->start_rpi);
19685 	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19686 	       hdr_tmpl, rpi_page->page_count);
19687 
19688 	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19689 	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
19690 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19691 	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19692 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19693 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19694 	mempool_free(mboxq, phba->mbox_mem_pool);
19695 	if (shdr_status || shdr_add_status || rc) {
19696 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19697 				"2514 POST_RPI_HDR mailbox failed with "
19698 				"status x%x add_status x%x, mbx status x%x\n",
19699 				shdr_status, shdr_add_status, rc);
19700 		rc = -ENXIO;
19701 	} else {
19702 		/*
19703 		 * The next_rpi stores the next logical module-64 rpi value used
19704 		 * to post physical rpis in subsequent rpi postings.
19705 		 */
19706 		spin_lock_irq(&phba->hbalock);
19707 		phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19708 		spin_unlock_irq(&phba->hbalock);
19709 	}
19710 	return rc;
19711 }
19712 
19713 /**
19714  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19715  * @phba: pointer to lpfc hba data structure.
19716  *
19717  * This routine is invoked to post rpi header templates to the
19718  * HBA consistent with the SLI-4 interface spec.  This routine
19719  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19720  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19721  *
19722  * Returns
19723  * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
19724  * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.
19725  **/
19726 int
lpfc_sli4_alloc_rpi(struct lpfc_hba * phba)19727 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19728 {
19729 	unsigned long rpi;
19730 	uint16_t max_rpi, rpi_limit;
19731 	uint16_t rpi_remaining, lrpi = 0;
19732 	struct lpfc_rpi_hdr *rpi_hdr;
19733 	unsigned long iflag;
19734 
19735 	/*
19736 	 * Fetch the next logical rpi.  Because this index is logical,
19737 	 * the  driver starts at 0 each time.
19738 	 */
19739 	spin_lock_irqsave(&phba->hbalock, iflag);
19740 	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19741 	rpi_limit = phba->sli4_hba.next_rpi;
19742 
19743 	rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);
19744 	if (rpi >= rpi_limit)
19745 		rpi = LPFC_RPI_ALLOC_ERROR;
19746 	else {
19747 		set_bit(rpi, phba->sli4_hba.rpi_bmask);
19748 		phba->sli4_hba.max_cfg_param.rpi_used++;
19749 		phba->sli4_hba.rpi_count++;
19750 	}
19751 	lpfc_printf_log(phba, KERN_INFO,
19752 			LOG_NODE | LOG_DISCOVERY,
19753 			"0001 Allocated rpi:x%x max:x%x lim:x%x\n",
19754 			(int) rpi, max_rpi, rpi_limit);
19755 
19756 	/*
19757 	 * Don't try to allocate more rpi header regions if the device limit
19758 	 * has been exhausted.
19759 	 */
19760 	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19761 	    (phba->sli4_hba.rpi_count >= max_rpi)) {
19762 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19763 		return rpi;
19764 	}
19765 
19766 	/*
19767 	 * RPI header postings are not required for SLI4 ports capable of
19768 	 * extents.
19769 	 */
19770 	if (!phba->sli4_hba.rpi_hdrs_in_use) {
19771 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19772 		return rpi;
19773 	}
19774 
19775 	/*
19776 	 * If the driver is running low on rpi resources, allocate another
19777 	 * page now.  Note that the next_rpi value is used because
19778 	 * it represents how many are actually in use whereas max_rpi notes
19779 	 * how many are supported max by the device.
19780 	 */
19781 	rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
19782 	spin_unlock_irqrestore(&phba->hbalock, iflag);
19783 	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19784 		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19785 		if (!rpi_hdr) {
19786 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19787 					"2002 Error Could not grow rpi "
19788 					"count\n");
19789 		} else {
19790 			lrpi = rpi_hdr->start_rpi;
19791 			rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19792 			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19793 		}
19794 	}
19795 
19796 	return rpi;
19797 }
19798 
19799 /**
19800  * __lpfc_sli4_free_rpi - Release an rpi for reuse.
19801  * @phba: pointer to lpfc hba data structure.
19802  * @rpi: rpi to free
19803  *
19804  * This routine is invoked to release an rpi to the pool of
19805  * available rpis maintained by the driver.
19806  **/
19807 static void
__lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19808 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19809 {
19810 	/*
19811 	 * if the rpi value indicates a prior unreg has already
19812 	 * been done, skip the unreg.
19813 	 */
19814 	if (rpi == LPFC_RPI_ALLOC_ERROR)
19815 		return;
19816 
19817 	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19818 		phba->sli4_hba.rpi_count--;
19819 		phba->sli4_hba.max_cfg_param.rpi_used--;
19820 	} else {
19821 		lpfc_printf_log(phba, KERN_INFO,
19822 				LOG_NODE | LOG_DISCOVERY,
19823 				"2016 rpi %x not inuse\n",
19824 				rpi);
19825 	}
19826 }
19827 
19828 /**
19829  * lpfc_sli4_free_rpi - Release an rpi for reuse.
19830  * @phba: pointer to lpfc hba data structure.
19831  * @rpi: rpi to free
19832  *
19833  * This routine is invoked to release an rpi to the pool of
19834  * available rpis maintained by the driver.
19835  **/
19836 void
lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19837 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19838 {
19839 	spin_lock_irq(&phba->hbalock);
19840 	__lpfc_sli4_free_rpi(phba, rpi);
19841 	spin_unlock_irq(&phba->hbalock);
19842 }
19843 
19844 /**
19845  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19846  * @phba: pointer to lpfc hba data structure.
19847  *
19848  * This routine is invoked to remove the memory region that
19849  * provided rpi via a bitmask.
19850  **/
19851 void
lpfc_sli4_remove_rpis(struct lpfc_hba * phba)19852 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19853 {
19854 	kfree(phba->sli4_hba.rpi_bmask);
19855 	kfree(phba->sli4_hba.rpi_ids);
19856 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
19857 }
19858 
19859 /**
19860  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
19861  * @ndlp: pointer to lpfc nodelist data structure.
19862  * @cmpl: completion call-back.
19863  * @iocbq: data to load as mbox ctx_u information
19864  *
19865  * This routine is invoked to remove the memory region that
19866  * provided rpi via a bitmask.
19867  **/
19868 int
lpfc_sli4_resume_rpi(struct lpfc_nodelist * ndlp,void (* cmpl)(struct lpfc_hba *,LPFC_MBOXQ_t *),struct lpfc_iocbq * iocbq)19869 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19870 		     void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *),
19871 		     struct lpfc_iocbq *iocbq)
19872 {
19873 	LPFC_MBOXQ_t *mboxq;
19874 	struct lpfc_hba *phba = ndlp->phba;
19875 	int rc;
19876 
19877 	/* The port is notified of the header region via a mailbox command. */
19878 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19879 	if (!mboxq)
19880 		return -ENOMEM;
19881 
19882 	/* If cmpl assigned, then this nlp_get pairs with
19883 	 * lpfc_mbx_cmpl_resume_rpi.
19884 	 *
19885 	 * Else cmpl is NULL, then this nlp_get pairs with
19886 	 * lpfc_sli_def_mbox_cmpl.
19887 	 */
19888 	if (!lpfc_nlp_get(ndlp)) {
19889 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19890 				"2122 %s: Failed to get nlp ref\n",
19891 				__func__);
19892 		mempool_free(mboxq, phba->mbox_mem_pool);
19893 		return -EIO;
19894 	}
19895 
19896 	/* Post all rpi memory regions to the port. */
19897 	lpfc_resume_rpi(mboxq, ndlp);
19898 	if (cmpl) {
19899 		mboxq->mbox_cmpl = cmpl;
19900 		mboxq->ctx_u.save_iocb = iocbq;
19901 	} else
19902 		mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19903 	mboxq->ctx_ndlp = ndlp;
19904 	mboxq->vport = ndlp->vport;
19905 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19906 	if (rc == MBX_NOT_FINISHED) {
19907 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19908 				"2010 Resume RPI Mailbox failed "
19909 				"status %d, mbxStatus x%x\n", rc,
19910 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19911 		lpfc_nlp_put(ndlp);
19912 		mempool_free(mboxq, phba->mbox_mem_pool);
19913 		return -EIO;
19914 	}
19915 	return 0;
19916 }
19917 
19918 /**
19919  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19920  * @vport: Pointer to the vport for which the vpi is being initialized
19921  *
19922  * This routine is invoked to activate a vpi with the port.
19923  *
19924  * Returns:
19925  *    0 success
19926  *    -Evalue otherwise
19927  **/
19928 int
lpfc_sli4_init_vpi(struct lpfc_vport * vport)19929 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
19930 {
19931 	LPFC_MBOXQ_t *mboxq;
19932 	int rc = 0;
19933 	int retval = MBX_SUCCESS;
19934 	uint32_t mbox_tmo;
19935 	struct lpfc_hba *phba = vport->phba;
19936 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19937 	if (!mboxq)
19938 		return -ENOMEM;
19939 	lpfc_init_vpi(phba, mboxq, vport->vpi);
19940 	mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
19941 	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
19942 	if (rc != MBX_SUCCESS) {
19943 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19944 				"2022 INIT VPI Mailbox failed "
19945 				"status %d, mbxStatus x%x\n", rc,
19946 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19947 		retval = -EIO;
19948 	}
19949 	if (rc != MBX_TIMEOUT)
19950 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
19951 
19952 	return retval;
19953 }
19954 
19955 /**
19956  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19957  * @phba: pointer to lpfc hba data structure.
19958  * @mboxq: Pointer to mailbox object.
19959  *
19960  * This routine is invoked to manually add a single FCF record. The caller
19961  * must pass a completely initialized FCF_Record.  This routine takes
19962  * care of the nonembedded mailbox operations.
19963  **/
19964 static void
lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)19965 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19966 {
19967 	void *virt_addr;
19968 	union lpfc_sli4_cfg_shdr *shdr;
19969 	uint32_t shdr_status, shdr_add_status;
19970 
19971 	virt_addr = mboxq->sge_array->addr[0];
19972 	/* The IOCTL status is embedded in the mailbox subheader. */
19973 	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19974 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19975 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19976 
19977 	if ((shdr_status || shdr_add_status) &&
19978 		(shdr_status != STATUS_FCF_IN_USE))
19979 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19980 			"2558 ADD_FCF_RECORD mailbox failed with "
19981 			"status x%x add_status x%x\n",
19982 			shdr_status, shdr_add_status);
19983 
19984 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
19985 }
19986 
19987 /**
19988  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19989  * @phba: pointer to lpfc hba data structure.
19990  * @fcf_record:  pointer to the initialized fcf record to add.
19991  *
19992  * This routine is invoked to manually add a single FCF record. The caller
19993  * must pass a completely initialized FCF_Record.  This routine takes
19994  * care of the nonembedded mailbox operations.
19995  **/
19996 int
lpfc_sli4_add_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record)19997 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19998 {
19999 	int rc = 0;
20000 	LPFC_MBOXQ_t *mboxq;
20001 	uint8_t *bytep;
20002 	void *virt_addr;
20003 	struct lpfc_mbx_sge sge;
20004 	uint32_t alloc_len, req_len;
20005 	uint32_t fcfindex;
20006 
20007 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20008 	if (!mboxq) {
20009 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20010 			"2009 Failed to allocate mbox for ADD_FCF cmd\n");
20011 		return -ENOMEM;
20012 	}
20013 
20014 	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
20015 		  sizeof(uint32_t);
20016 
20017 	/* Allocate DMA memory and set up the non-embedded mailbox command */
20018 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
20019 				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
20020 				     req_len, LPFC_SLI4_MBX_NEMBED);
20021 	if (alloc_len < req_len) {
20022 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20023 			"2523 Allocated DMA memory size (x%x) is "
20024 			"less than the requested DMA memory "
20025 			"size (x%x)\n", alloc_len, req_len);
20026 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20027 		return -ENOMEM;
20028 	}
20029 
20030 	/*
20031 	 * Get the first SGE entry from the non-embedded DMA memory.  This
20032 	 * routine only uses a single SGE.
20033 	 */
20034 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
20035 	virt_addr = mboxq->sge_array->addr[0];
20036 	/*
20037 	 * Configure the FCF record for FCFI 0.  This is the driver's
20038 	 * hardcoded default and gets used in nonFIP mode.
20039 	 */
20040 	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
20041 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
20042 	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
20043 
20044 	/*
20045 	 * Copy the fcf_index and the FCF Record Data. The data starts after
20046 	 * the FCoE header plus word10. The data copy needs to be endian
20047 	 * correct.
20048 	 */
20049 	bytep += sizeof(uint32_t);
20050 	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
20051 	mboxq->vport = phba->pport;
20052 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
20053 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20054 	if (rc == MBX_NOT_FINISHED) {
20055 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20056 			"2515 ADD_FCF_RECORD mailbox failed with "
20057 			"status 0x%x\n", rc);
20058 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20059 		rc = -EIO;
20060 	} else
20061 		rc = 0;
20062 
20063 	return rc;
20064 }
20065 
20066 /**
20067  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
20068  * @phba: pointer to lpfc hba data structure.
20069  * @fcf_record:  pointer to the fcf record to write the default data.
20070  * @fcf_index: FCF table entry index.
20071  *
20072  * This routine is invoked to build the driver's default FCF record.  The
20073  * values used are hardcoded.  This routine handles memory initialization.
20074  *
20075  **/
20076 void
lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record,uint16_t fcf_index)20077 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
20078 				struct fcf_record *fcf_record,
20079 				uint16_t fcf_index)
20080 {
20081 	memset(fcf_record, 0, sizeof(struct fcf_record));
20082 	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
20083 	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
20084 	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
20085 	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
20086 	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
20087 	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
20088 	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
20089 	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
20090 	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
20091 	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
20092 	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
20093 	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
20094 	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
20095 	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
20096 	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
20097 	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
20098 		LPFC_FCF_FPMA | LPFC_FCF_SPMA);
20099 	/* Set the VLAN bit map */
20100 	if (phba->valid_vlan) {
20101 		fcf_record->vlan_bitmap[phba->vlan_id / 8]
20102 			= 1 << (phba->vlan_id % 8);
20103 	}
20104 }
20105 
20106 /**
20107  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
20108  * @phba: pointer to lpfc hba data structure.
20109  * @fcf_index: FCF table entry offset.
20110  *
20111  * This routine is invoked to scan the entire FCF table by reading FCF
20112  * record and processing it one at a time starting from the @fcf_index
20113  * for initial FCF discovery or fast FCF failover rediscovery.
20114  *
20115  * Return 0 if the mailbox command is submitted successfully, none 0
20116  * otherwise.
20117  **/
20118 int
lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20119 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20120 {
20121 	int rc = 0, error;
20122 	LPFC_MBOXQ_t *mboxq;
20123 
20124 	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
20125 	phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
20126 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20127 	if (!mboxq) {
20128 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20129 				"2000 Failed to allocate mbox for "
20130 				"READ_FCF cmd\n");
20131 		error = -ENOMEM;
20132 		goto fail_fcf_scan;
20133 	}
20134 	/* Construct the read FCF record mailbox command */
20135 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20136 	if (rc) {
20137 		error = -EINVAL;
20138 		goto fail_fcf_scan;
20139 	}
20140 	/* Issue the mailbox command asynchronously */
20141 	mboxq->vport = phba->pport;
20142 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
20143 
20144 	set_bit(FCF_TS_INPROG, &phba->hba_flag);
20145 
20146 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20147 	if (rc == MBX_NOT_FINISHED)
20148 		error = -EIO;
20149 	else {
20150 		/* Reset eligible FCF count for new scan */
20151 		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
20152 			phba->fcf.eligible_fcf_cnt = 0;
20153 		error = 0;
20154 	}
20155 fail_fcf_scan:
20156 	if (error) {
20157 		if (mboxq)
20158 			lpfc_sli4_mbox_cmd_free(phba, mboxq);
20159 		/* FCF scan failed, clear FCF_TS_INPROG flag */
20160 		clear_bit(FCF_TS_INPROG, &phba->hba_flag);
20161 	}
20162 	return error;
20163 }
20164 
20165 /**
20166  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
20167  * @phba: pointer to lpfc hba data structure.
20168  * @fcf_index: FCF table entry offset.
20169  *
20170  * This routine is invoked to read an FCF record indicated by @fcf_index
20171  * and to use it for FLOGI roundrobin FCF failover.
20172  *
20173  * Return 0 if the mailbox command is submitted successfully, none 0
20174  * otherwise.
20175  **/
20176 int
lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20177 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20178 {
20179 	int rc = 0, error;
20180 	LPFC_MBOXQ_t *mboxq;
20181 
20182 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20183 	if (!mboxq) {
20184 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20185 				"2763 Failed to allocate mbox for "
20186 				"READ_FCF cmd\n");
20187 		error = -ENOMEM;
20188 		goto fail_fcf_read;
20189 	}
20190 	/* Construct the read FCF record mailbox command */
20191 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20192 	if (rc) {
20193 		error = -EINVAL;
20194 		goto fail_fcf_read;
20195 	}
20196 	/* Issue the mailbox command asynchronously */
20197 	mboxq->vport = phba->pport;
20198 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20199 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20200 	if (rc == MBX_NOT_FINISHED)
20201 		error = -EIO;
20202 	else
20203 		error = 0;
20204 
20205 fail_fcf_read:
20206 	if (error && mboxq)
20207 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20208 	return error;
20209 }
20210 
20211 /**
20212  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20213  * @phba: pointer to lpfc hba data structure.
20214  * @fcf_index: FCF table entry offset.
20215  *
20216  * This routine is invoked to read an FCF record indicated by @fcf_index to
20217  * determine whether it's eligible for FLOGI roundrobin failover list.
20218  *
20219  * Return 0 if the mailbox command is submitted successfully, none 0
20220  * otherwise.
20221  **/
20222 int
lpfc_sli4_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20223 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20224 {
20225 	int rc = 0, error;
20226 	LPFC_MBOXQ_t *mboxq;
20227 
20228 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20229 	if (!mboxq) {
20230 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20231 				"2758 Failed to allocate mbox for "
20232 				"READ_FCF cmd\n");
20233 				error = -ENOMEM;
20234 				goto fail_fcf_read;
20235 	}
20236 	/* Construct the read FCF record mailbox command */
20237 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20238 	if (rc) {
20239 		error = -EINVAL;
20240 		goto fail_fcf_read;
20241 	}
20242 	/* Issue the mailbox command asynchronously */
20243 	mboxq->vport = phba->pport;
20244 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20245 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20246 	if (rc == MBX_NOT_FINISHED)
20247 		error = -EIO;
20248 	else
20249 		error = 0;
20250 
20251 fail_fcf_read:
20252 	if (error && mboxq)
20253 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20254 	return error;
20255 }
20256 
20257 /**
20258  * lpfc_check_next_fcf_pri_level
20259  * @phba: pointer to the lpfc_hba struct for this port.
20260  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20261  * routine when the rr_bmask is empty. The FCF indecies are put into the
20262  * rr_bmask based on their priority level. Starting from the highest priority
20263  * to the lowest. The most likely FCF candidate will be in the highest
20264  * priority group. When this routine is called it searches the fcf_pri list for
20265  * next lowest priority group and repopulates the rr_bmask with only those
20266  * fcf_indexes.
20267  * returns:
20268  * 1=success 0=failure
20269  **/
20270 static int
lpfc_check_next_fcf_pri_level(struct lpfc_hba * phba)20271 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20272 {
20273 	uint16_t next_fcf_pri;
20274 	uint16_t last_index;
20275 	struct lpfc_fcf_pri *fcf_pri;
20276 	int rc;
20277 	int ret = 0;
20278 
20279 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20280 			LPFC_SLI4_FCF_TBL_INDX_MAX);
20281 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20282 			"3060 Last IDX %d\n", last_index);
20283 
20284 	/* Verify the priority list has 2 or more entries */
20285 	spin_lock_irq(&phba->hbalock);
20286 	if (list_empty(&phba->fcf.fcf_pri_list) ||
20287 	    list_is_singular(&phba->fcf.fcf_pri_list)) {
20288 		spin_unlock_irq(&phba->hbalock);
20289 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20290 			"3061 Last IDX %d\n", last_index);
20291 		return 0; /* Empty rr list */
20292 	}
20293 	spin_unlock_irq(&phba->hbalock);
20294 
20295 	next_fcf_pri = 0;
20296 	/*
20297 	 * Clear the rr_bmask and set all of the bits that are at this
20298 	 * priority.
20299 	 */
20300 	memset(phba->fcf.fcf_rr_bmask, 0,
20301 			sizeof(*phba->fcf.fcf_rr_bmask));
20302 	spin_lock_irq(&phba->hbalock);
20303 	list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20304 		if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20305 			continue;
20306 		/*
20307 		 * the 1st priority that has not FLOGI failed
20308 		 * will be the highest.
20309 		 */
20310 		if (!next_fcf_pri)
20311 			next_fcf_pri = fcf_pri->fcf_rec.priority;
20312 		spin_unlock_irq(&phba->hbalock);
20313 		if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20314 			rc = lpfc_sli4_fcf_rr_index_set(phba,
20315 						fcf_pri->fcf_rec.fcf_index);
20316 			if (rc)
20317 				return 0;
20318 		}
20319 		spin_lock_irq(&phba->hbalock);
20320 	}
20321 	/*
20322 	 * if next_fcf_pri was not set above and the list is not empty then
20323 	 * we have failed flogis on all of them. So reset flogi failed
20324 	 * and start at the beginning.
20325 	 */
20326 	if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20327 		list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20328 			fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20329 			/*
20330 			 * the 1st priority that has not FLOGI failed
20331 			 * will be the highest.
20332 			 */
20333 			if (!next_fcf_pri)
20334 				next_fcf_pri = fcf_pri->fcf_rec.priority;
20335 			spin_unlock_irq(&phba->hbalock);
20336 			if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20337 				rc = lpfc_sli4_fcf_rr_index_set(phba,
20338 						fcf_pri->fcf_rec.fcf_index);
20339 				if (rc)
20340 					return 0;
20341 			}
20342 			spin_lock_irq(&phba->hbalock);
20343 		}
20344 	} else
20345 		ret = 1;
20346 	spin_unlock_irq(&phba->hbalock);
20347 
20348 	return ret;
20349 }
20350 /**
20351  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20352  * @phba: pointer to lpfc hba data structure.
20353  *
20354  * This routine is to get the next eligible FCF record index in a round
20355  * robin fashion. If the next eligible FCF record index equals to the
20356  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
20357  * shall be returned, otherwise, the next eligible FCF record's index
20358  * shall be returned.
20359  **/
20360 uint16_t
lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba * phba)20361 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20362 {
20363 	uint16_t next_fcf_index;
20364 
20365 initial_priority:
20366 	/* Search start from next bit of currently registered FCF index */
20367 	next_fcf_index = phba->fcf.current_rec.fcf_indx;
20368 
20369 next_priority:
20370 	/* Determine the next fcf index to check */
20371 	next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
20372 	next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20373 				       LPFC_SLI4_FCF_TBL_INDX_MAX,
20374 				       next_fcf_index);
20375 
20376 	/* Wrap around condition on phba->fcf.fcf_rr_bmask */
20377 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20378 		/*
20379 		 * If we have wrapped then we need to clear the bits that
20380 		 * have been tested so that we can detect when we should
20381 		 * change the priority level.
20382 		 */
20383 		next_fcf_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20384 					       LPFC_SLI4_FCF_TBL_INDX_MAX);
20385 	}
20386 
20387 
20388 	/* Check roundrobin failover list empty condition */
20389 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20390 		next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20391 		/*
20392 		 * If next fcf index is not found check if there are lower
20393 		 * Priority level fcf's in the fcf_priority list.
20394 		 * Set up the rr_bmask with all of the avaiable fcf bits
20395 		 * at that level and continue the selection process.
20396 		 */
20397 		if (lpfc_check_next_fcf_pri_level(phba))
20398 			goto initial_priority;
20399 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20400 				"2844 No roundrobin failover FCF available\n");
20401 
20402 		return LPFC_FCOE_FCF_NEXT_NONE;
20403 	}
20404 
20405 	if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20406 		phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
20407 		LPFC_FCF_FLOGI_FAILED) {
20408 		if (list_is_singular(&phba->fcf.fcf_pri_list))
20409 			return LPFC_FCOE_FCF_NEXT_NONE;
20410 
20411 		goto next_priority;
20412 	}
20413 
20414 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20415 			"2845 Get next roundrobin failover FCF (x%x)\n",
20416 			next_fcf_index);
20417 
20418 	return next_fcf_index;
20419 }
20420 
20421 /**
20422  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20423  * @phba: pointer to lpfc hba data structure.
20424  * @fcf_index: index into the FCF table to 'set'
20425  *
20426  * This routine sets the FCF record index in to the eligible bmask for
20427  * roundrobin failover search. It checks to make sure that the index
20428  * does not go beyond the range of the driver allocated bmask dimension
20429  * before setting the bit.
20430  *
20431  * Returns 0 if the index bit successfully set, otherwise, it returns
20432  * -EINVAL.
20433  **/
20434 int
lpfc_sli4_fcf_rr_index_set(struct lpfc_hba * phba,uint16_t fcf_index)20435 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20436 {
20437 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20438 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20439 				"2610 FCF (x%x) reached driver's book "
20440 				"keeping dimension:x%x\n",
20441 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20442 		return -EINVAL;
20443 	}
20444 	/* Set the eligible FCF record index bmask */
20445 	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20446 
20447 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20448 			"2790 Set FCF (x%x) to roundrobin FCF failover "
20449 			"bmask\n", fcf_index);
20450 
20451 	return 0;
20452 }
20453 
20454 /**
20455  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
20456  * @phba: pointer to lpfc hba data structure.
20457  * @fcf_index: index into the FCF table to 'clear'
20458  *
20459  * This routine clears the FCF record index from the eligible bmask for
20460  * roundrobin failover search. It checks to make sure that the index
20461  * does not go beyond the range of the driver allocated bmask dimension
20462  * before clearing the bit.
20463  **/
20464 void
lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba * phba,uint16_t fcf_index)20465 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20466 {
20467 	struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
20468 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20469 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20470 				"2762 FCF (x%x) reached driver's book "
20471 				"keeping dimension:x%x\n",
20472 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20473 		return;
20474 	}
20475 	/* Clear the eligible FCF record index bmask */
20476 	spin_lock_irq(&phba->hbalock);
20477 	list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20478 				 list) {
20479 		if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20480 			list_del_init(&fcf_pri->list);
20481 			break;
20482 		}
20483 	}
20484 	spin_unlock_irq(&phba->hbalock);
20485 	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20486 
20487 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20488 			"2791 Clear FCF (x%x) from roundrobin failover "
20489 			"bmask\n", fcf_index);
20490 }
20491 
20492 /**
20493  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20494  * @phba: pointer to lpfc hba data structure.
20495  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
20496  *
20497  * This routine is the completion routine for the rediscover FCF table mailbox
20498  * command. If the mailbox command returned failure, it will try to stop the
20499  * FCF rediscover wait timer.
20500  **/
20501 static void
lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)20502 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20503 {
20504 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20505 	uint32_t shdr_status, shdr_add_status;
20506 
20507 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20508 
20509 	shdr_status = bf_get(lpfc_mbox_hdr_status,
20510 			     &redisc_fcf->header.cfg_shdr.response);
20511 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20512 			     &redisc_fcf->header.cfg_shdr.response);
20513 	if (shdr_status || shdr_add_status) {
20514 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20515 				"2746 Requesting for FCF rediscovery failed "
20516 				"status x%x add_status x%x\n",
20517 				shdr_status, shdr_add_status);
20518 		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
20519 			spin_lock_irq(&phba->hbalock);
20520 			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
20521 			spin_unlock_irq(&phba->hbalock);
20522 			/*
20523 			 * CVL event triggered FCF rediscover request failed,
20524 			 * last resort to re-try current registered FCF entry.
20525 			 */
20526 			lpfc_retry_pport_discovery(phba);
20527 		} else {
20528 			spin_lock_irq(&phba->hbalock);
20529 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
20530 			spin_unlock_irq(&phba->hbalock);
20531 			/*
20532 			 * DEAD FCF event triggered FCF rediscover request
20533 			 * failed, last resort to fail over as a link down
20534 			 * to FCF registration.
20535 			 */
20536 			lpfc_sli4_fcf_dead_failthrough(phba);
20537 		}
20538 	} else {
20539 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20540 				"2775 Start FCF rediscover quiescent timer\n");
20541 		/*
20542 		 * Start FCF rediscovery wait timer for pending FCF
20543 		 * before rescan FCF record table.
20544 		 */
20545 		lpfc_fcf_redisc_wait_start_timer(phba);
20546 	}
20547 
20548 	mempool_free(mbox, phba->mbox_mem_pool);
20549 }
20550 
20551 /**
20552  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
20553  * @phba: pointer to lpfc hba data structure.
20554  *
20555  * This routine is invoked to request for rediscovery of the entire FCF table
20556  * by the port.
20557  **/
20558 int
lpfc_sli4_redisc_fcf_table(struct lpfc_hba * phba)20559 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20560 {
20561 	LPFC_MBOXQ_t *mbox;
20562 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20563 	int rc, length;
20564 
20565 	/* Cancel retry delay timers to all vports before FCF rediscover */
20566 	lpfc_cancel_all_vport_retry_delay_timer(phba);
20567 
20568 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20569 	if (!mbox) {
20570 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20571 				"2745 Failed to allocate mbox for "
20572 				"requesting FCF rediscover.\n");
20573 		return -ENOMEM;
20574 	}
20575 
20576 	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20577 		  sizeof(struct lpfc_sli4_cfg_mhdr));
20578 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20579 			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20580 			 length, LPFC_SLI4_MBX_EMBED);
20581 
20582 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20583 	/* Set count to 0 for invalidating the entire FCF database */
20584 	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20585 
20586 	/* Issue the mailbox command asynchronously */
20587 	mbox->vport = phba->pport;
20588 	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20589 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20590 
20591 	if (rc == MBX_NOT_FINISHED) {
20592 		mempool_free(mbox, phba->mbox_mem_pool);
20593 		return -EIO;
20594 	}
20595 	return 0;
20596 }
20597 
20598 /**
20599  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20600  * @phba: pointer to lpfc hba data structure.
20601  *
20602  * This function is the failover routine as a last resort to the FCF DEAD
20603  * event when driver failed to perform fast FCF failover.
20604  **/
20605 void
lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba * phba)20606 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20607 {
20608 	uint32_t link_state;
20609 
20610 	/*
20611 	 * Last resort as FCF DEAD event failover will treat this as
20612 	 * a link down, but save the link state because we don't want
20613 	 * it to be changed to Link Down unless it is already down.
20614 	 */
20615 	link_state = phba->link_state;
20616 	lpfc_linkdown(phba);
20617 	phba->link_state = link_state;
20618 
20619 	/* Unregister FCF if no devices connected to it */
20620 	lpfc_unregister_unused_fcf(phba);
20621 }
20622 
20623 /**
20624  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
20625  * @phba: pointer to lpfc hba data structure.
20626  * @rgn23_data: pointer to configure region 23 data.
20627  *
20628  * This function gets SLI3 port configure region 23 data through memory dump
20629  * mailbox command. When it successfully retrieves data, the size of the data
20630  * will be returned, otherwise, 0 will be returned.
20631  **/
20632 static uint32_t
lpfc_sli_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20633 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20634 {
20635 	LPFC_MBOXQ_t *pmb = NULL;
20636 	MAILBOX_t *mb;
20637 	uint32_t offset = 0;
20638 	int rc;
20639 
20640 	if (!rgn23_data)
20641 		return 0;
20642 
20643 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20644 	if (!pmb) {
20645 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20646 				"2600 failed to allocate mailbox memory\n");
20647 		return 0;
20648 	}
20649 	mb = &pmb->u.mb;
20650 
20651 	do {
20652 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20653 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20654 
20655 		if (rc != MBX_SUCCESS) {
20656 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20657 					"2601 failed to read config "
20658 					"region 23, rc 0x%x Status 0x%x\n",
20659 					rc, mb->mbxStatus);
20660 			mb->un.varDmp.word_cnt = 0;
20661 		}
20662 		/*
20663 		 * dump mem may return a zero when finished or we got a
20664 		 * mailbox error, either way we are done.
20665 		 */
20666 		if (mb->un.varDmp.word_cnt == 0)
20667 			break;
20668 
20669 		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20670 			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20671 
20672 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
20673 				       rgn23_data + offset,
20674 				       mb->un.varDmp.word_cnt);
20675 		offset += mb->un.varDmp.word_cnt;
20676 	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
20677 
20678 	mempool_free(pmb, phba->mbox_mem_pool);
20679 	return offset;
20680 }
20681 
20682 /**
20683  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20684  * @phba: pointer to lpfc hba data structure.
20685  * @rgn23_data: pointer to configure region 23 data.
20686  *
20687  * This function gets SLI4 port configure region 23 data through memory dump
20688  * mailbox command. When it successfully retrieves data, the size of the data
20689  * will be returned, otherwise, 0 will be returned.
20690  **/
20691 static uint32_t
lpfc_sli4_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20692 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20693 {
20694 	LPFC_MBOXQ_t *mboxq = NULL;
20695 	struct lpfc_dmabuf *mp = NULL;
20696 	struct lpfc_mqe *mqe;
20697 	uint32_t data_length = 0;
20698 	int rc;
20699 
20700 	if (!rgn23_data)
20701 		return 0;
20702 
20703 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20704 	if (!mboxq) {
20705 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20706 				"3105 failed to allocate mailbox memory\n");
20707 		return 0;
20708 	}
20709 
20710 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20711 		goto out;
20712 	mqe = &mboxq->u.mqe;
20713 	mp = mboxq->ctx_buf;
20714 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20715 	if (rc)
20716 		goto out;
20717 	data_length = mqe->un.mb_words[5];
20718 	if (data_length == 0)
20719 		goto out;
20720 	if (data_length > DMP_RGN23_SIZE) {
20721 		data_length = 0;
20722 		goto out;
20723 	}
20724 	lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20725 out:
20726 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
20727 	return data_length;
20728 }
20729 
20730 /**
20731  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20732  * @phba: pointer to lpfc hba data structure.
20733  *
20734  * This function read region 23 and parse TLV for port status to
20735  * decide if the user disaled the port. If the TLV indicates the
20736  * port is disabled, the hba_flag is set accordingly.
20737  **/
20738 void
lpfc_sli_read_link_ste(struct lpfc_hba * phba)20739 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20740 {
20741 	uint8_t *rgn23_data = NULL;
20742 	uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20743 	uint32_t offset = 0;
20744 
20745 	/* Get adapter Region 23 data */
20746 	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20747 	if (!rgn23_data)
20748 		goto out;
20749 
20750 	if (phba->sli_rev < LPFC_SLI_REV4)
20751 		data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20752 	else {
20753 		if_type = bf_get(lpfc_sli_intf_if_type,
20754 				 &phba->sli4_hba.sli_intf);
20755 		if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20756 			goto out;
20757 		data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20758 	}
20759 
20760 	if (!data_size)
20761 		goto out;
20762 
20763 	/* Check the region signature first */
20764 	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
20765 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20766 			"2619 Config region 23 has bad signature\n");
20767 			goto out;
20768 	}
20769 	offset += 4;
20770 
20771 	/* Check the data structure version */
20772 	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
20773 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20774 			"2620 Config region 23 has bad version\n");
20775 		goto out;
20776 	}
20777 	offset += 4;
20778 
20779 	/* Parse TLV entries in the region */
20780 	while (offset < data_size) {
20781 		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20782 			break;
20783 		/*
20784 		 * If the TLV is not driver specific TLV or driver id is
20785 		 * not linux driver id, skip the record.
20786 		 */
20787 		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20788 		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20789 		    (rgn23_data[offset + 3] != 0)) {
20790 			offset += rgn23_data[offset + 1] * 4 + 4;
20791 			continue;
20792 		}
20793 
20794 		/* Driver found a driver specific TLV in the config region */
20795 		sub_tlv_len = rgn23_data[offset + 1] * 4;
20796 		offset += 4;
20797 		tlv_offset = 0;
20798 
20799 		/*
20800 		 * Search for configured port state sub-TLV.
20801 		 */
20802 		while ((offset < data_size) &&
20803 			(tlv_offset < sub_tlv_len)) {
20804 			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20805 				offset += 4;
20806 				tlv_offset += 4;
20807 				break;
20808 			}
20809 			if (rgn23_data[offset] != PORT_STE_TYPE) {
20810 				offset += rgn23_data[offset + 1] * 4 + 4;
20811 				tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20812 				continue;
20813 			}
20814 
20815 			/* This HBA contains PORT_STE configured */
20816 			if (!rgn23_data[offset + 2])
20817 				set_bit(LINK_DISABLED, &phba->hba_flag);
20818 
20819 			goto out;
20820 		}
20821 	}
20822 
20823 out:
20824 	kfree(rgn23_data);
20825 	return;
20826 }
20827 
20828 /**
20829  * lpfc_log_fw_write_cmpl - logs firmware write completion status
20830  * @phba: pointer to lpfc hba data structure
20831  * @shdr_status: wr_object rsp's status field
20832  * @shdr_add_status: wr_object rsp's add_status field
20833  * @shdr_add_status_2: wr_object rsp's add_status_2 field
20834  * @shdr_change_status: wr_object rsp's change_status field
20835  * @shdr_csf: wr_object rsp's csf bit
20836  *
20837  * This routine is intended to be called after a firmware write completes.
20838  * It will log next action items to be performed by the user to instantiate
20839  * the newly downloaded firmware or reason for incompatibility.
20840  **/
20841 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)20842 lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20843 		       u32 shdr_add_status, u32 shdr_add_status_2,
20844 		       u32 shdr_change_status, u32 shdr_csf)
20845 {
20846 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20847 			"4198 %s: flash_id x%02x, asic_rev x%02x, "
20848 			"status x%02x, add_status x%02x, add_status_2 x%02x, "
20849 			"change_status x%02x, csf %01x\n", __func__,
20850 			phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20851 			shdr_status, shdr_add_status, shdr_add_status_2,
20852 			shdr_change_status, shdr_csf);
20853 
20854 	if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20855 		switch (shdr_add_status_2) {
20856 		case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20857 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20858 				     "4199 Firmware write failed: "
20859 				     "image incompatible with flash x%02x\n",
20860 				     phba->sli4_hba.flash_id);
20861 			break;
20862 		case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20863 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20864 				     "4200 Firmware write failed: "
20865 				     "image incompatible with ASIC "
20866 				     "architecture x%02x\n",
20867 				     phba->sli4_hba.asic_rev);
20868 			break;
20869 		default:
20870 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20871 				     "4210 Firmware write failed: "
20872 				     "add_status_2 x%02x\n",
20873 				     shdr_add_status_2);
20874 			break;
20875 		}
20876 	} else if (!shdr_status && !shdr_add_status) {
20877 		if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20878 		    shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20879 			if (shdr_csf)
20880 				shdr_change_status =
20881 						   LPFC_CHANGE_STATUS_PCI_RESET;
20882 		}
20883 
20884 		switch (shdr_change_status) {
20885 		case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20886 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20887 				     "3198 Firmware write complete: System "
20888 				     "reboot required to instantiate\n");
20889 			break;
20890 		case (LPFC_CHANGE_STATUS_FW_RESET):
20891 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20892 				     "3199 Firmware write complete: "
20893 				     "Firmware reset required to "
20894 				     "instantiate\n");
20895 			break;
20896 		case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20897 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20898 				     "3200 Firmware write complete: Port "
20899 				     "Migration or PCI Reset required to "
20900 				     "instantiate\n");
20901 			break;
20902 		case (LPFC_CHANGE_STATUS_PCI_RESET):
20903 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20904 				     "3201 Firmware write complete: PCI "
20905 				     "Reset required to instantiate\n");
20906 			break;
20907 		default:
20908 			break;
20909 		}
20910 	}
20911 }
20912 
20913 /**
20914  * lpfc_wr_object - write an object to the firmware
20915  * @phba: HBA structure that indicates port to create a queue on.
20916  * @dmabuf_list: list of dmabufs to write to the port.
20917  * @size: the total byte value of the objects to write to the port.
20918  * @offset: the current offset to be used to start the transfer.
20919  *
20920  * This routine will create a wr_object mailbox command to send to the port.
20921  * the mailbox command will be constructed using the dma buffers described in
20922  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20923  * BDEs that the imbedded mailbox can support. The @offset variable will be
20924  * used to indicate the starting offset of the transfer and will also return
20925  * the offset after the write object mailbox has completed. @size is used to
20926  * determine the end of the object and whether the eof bit should be set.
20927  *
20928  * Return 0 is successful and offset will contain the new offset to use
20929  * for the next write.
20930  * Return negative value for error cases.
20931  **/
20932 int
lpfc_wr_object(struct lpfc_hba * phba,struct list_head * dmabuf_list,uint32_t size,uint32_t * offset)20933 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20934 	       uint32_t size, uint32_t *offset)
20935 {
20936 	struct lpfc_mbx_wr_object *wr_object;
20937 	LPFC_MBOXQ_t *mbox;
20938 	int rc = 0, i = 0;
20939 	int mbox_status = 0;
20940 	uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20941 	uint32_t shdr_change_status = 0, shdr_csf = 0;
20942 	uint32_t mbox_tmo;
20943 	struct lpfc_dmabuf *dmabuf;
20944 	uint32_t written = 0;
20945 	bool check_change_status = false;
20946 
20947 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20948 	if (!mbox)
20949 		return -ENOMEM;
20950 
20951 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20952 			LPFC_MBOX_OPCODE_WRITE_OBJECT,
20953 			sizeof(struct lpfc_mbx_wr_object) -
20954 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20955 
20956 	wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20957 	wr_object->u.request.write_offset = *offset;
20958 	sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20959 	wr_object->u.request.object_name[0] =
20960 		cpu_to_le32(wr_object->u.request.object_name[0]);
20961 	bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20962 	list_for_each_entry(dmabuf, dmabuf_list, list) {
20963 		if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20964 			break;
20965 		wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20966 		wr_object->u.request.bde[i].addrHigh =
20967 			putPaddrHigh(dmabuf->phys);
20968 		if (written + SLI4_PAGE_SIZE >= size) {
20969 			wr_object->u.request.bde[i].tus.f.bdeSize =
20970 				(size - written);
20971 			written += (size - written);
20972 			bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
20973 			bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20974 			check_change_status = true;
20975 		} else {
20976 			wr_object->u.request.bde[i].tus.f.bdeSize =
20977 				SLI4_PAGE_SIZE;
20978 			written += SLI4_PAGE_SIZE;
20979 		}
20980 		i++;
20981 	}
20982 	wr_object->u.request.bde_count = i;
20983 	bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20984 	if (!phba->sli4_hba.intr_enable)
20985 		mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20986 	else {
20987 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
20988 		mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20989 	}
20990 
20991 	/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */
20992 	rc = mbox_status;
20993 
20994 	/* The IOCTL status is embedded in the mailbox subheader. */
20995 	shdr_status = bf_get(lpfc_mbox_hdr_status,
20996 			     &wr_object->header.cfg_shdr.response);
20997 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20998 				 &wr_object->header.cfg_shdr.response);
20999 	shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
21000 				   &wr_object->header.cfg_shdr.response);
21001 	if (check_change_status) {
21002 		shdr_change_status = bf_get(lpfc_wr_object_change_status,
21003 					    &wr_object->u.response);
21004 		shdr_csf = bf_get(lpfc_wr_object_csf,
21005 				  &wr_object->u.response);
21006 	}
21007 
21008 	if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
21009 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21010 				"3025 Write Object mailbox failed with "
21011 				"status x%x add_status x%x, add_status_2 x%x, "
21012 				"mbx status x%x\n",
21013 				shdr_status, shdr_add_status, shdr_add_status_2,
21014 				rc);
21015 		rc = -ENXIO;
21016 		*offset = shdr_add_status;
21017 	} else {
21018 		*offset += wr_object->u.response.actual_write_length;
21019 	}
21020 
21021 	if (rc || check_change_status)
21022 		lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
21023 				       shdr_add_status_2, shdr_change_status,
21024 				       shdr_csf);
21025 
21026 	if (!phba->sli4_hba.intr_enable)
21027 		mempool_free(mbox, phba->mbox_mem_pool);
21028 	else if (mbox_status != MBX_TIMEOUT)
21029 		mempool_free(mbox, phba->mbox_mem_pool);
21030 
21031 	return rc;
21032 }
21033 
21034 /**
21035  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
21036  * @vport: pointer to vport data structure.
21037  *
21038  * This function iterate through the mailboxq and clean up all REG_LOGIN
21039  * and REG_VPI mailbox commands associated with the vport. This function
21040  * is called when driver want to restart discovery of the vport due to
21041  * a Clear Virtual Link event.
21042  **/
21043 void
lpfc_cleanup_pending_mbox(struct lpfc_vport * vport)21044 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
21045 {
21046 	struct lpfc_hba *phba = vport->phba;
21047 	LPFC_MBOXQ_t *mb, *nextmb;
21048 	struct lpfc_nodelist *ndlp;
21049 	struct lpfc_nodelist *act_mbx_ndlp = NULL;
21050 	LIST_HEAD(mbox_cmd_list);
21051 	uint8_t restart_loop;
21052 
21053 	/* Clean up internally queued mailbox commands with the vport */
21054 	spin_lock_irq(&phba->hbalock);
21055 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
21056 		if (mb->vport != vport)
21057 			continue;
21058 
21059 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21060 			(mb->u.mb.mbxCommand != MBX_REG_VPI))
21061 			continue;
21062 
21063 		list_move_tail(&mb->list, &mbox_cmd_list);
21064 	}
21065 	/* Clean up active mailbox command with the vport */
21066 	mb = phba->sli.mbox_active;
21067 	if (mb && (mb->vport == vport)) {
21068 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
21069 			(mb->u.mb.mbxCommand == MBX_REG_VPI))
21070 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21071 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21072 			act_mbx_ndlp = mb->ctx_ndlp;
21073 
21074 			/* This reference is local to this routine.  The
21075 			 * reference is removed at routine exit.
21076 			 */
21077 			act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
21078 
21079 			/* Unregister the RPI when mailbox complete */
21080 			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21081 		}
21082 	}
21083 	/* Cleanup any mailbox completions which are not yet processed */
21084 	do {
21085 		restart_loop = 0;
21086 		list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
21087 			/*
21088 			 * If this mailox is already processed or it is
21089 			 * for another vport ignore it.
21090 			 */
21091 			if ((mb->vport != vport) ||
21092 				(mb->mbox_flag & LPFC_MBX_IMED_UNREG))
21093 				continue;
21094 
21095 			if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21096 				(mb->u.mb.mbxCommand != MBX_REG_VPI))
21097 				continue;
21098 
21099 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21100 			if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21101 				ndlp = mb->ctx_ndlp;
21102 				/* Unregister the RPI when mailbox complete */
21103 				mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21104 				restart_loop = 1;
21105 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21106 				break;
21107 			}
21108 		}
21109 	} while (restart_loop);
21110 
21111 	spin_unlock_irq(&phba->hbalock);
21112 
21113 	/* Release the cleaned-up mailbox commands */
21114 	while (!list_empty(&mbox_cmd_list)) {
21115 		list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
21116 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21117 			ndlp = mb->ctx_ndlp;
21118 			mb->ctx_ndlp = NULL;
21119 			if (ndlp) {
21120 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21121 				lpfc_nlp_put(ndlp);
21122 			}
21123 		}
21124 		lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_UNLOCKED);
21125 	}
21126 
21127 	/* Release the ndlp with the cleaned-up active mailbox command */
21128 	if (act_mbx_ndlp) {
21129 		clear_bit(NLP_IGNR_REG_CMPL, &act_mbx_ndlp->nlp_flag);
21130 		lpfc_nlp_put(act_mbx_ndlp);
21131 	}
21132 }
21133 
21134 /**
21135  * lpfc_drain_txq - Drain the txq
21136  * @phba: Pointer to HBA context object.
21137  *
21138  * This function attempt to submit IOCBs on the txq
21139  * to the adapter.  For SLI4 adapters, the txq contains
21140  * ELS IOCBs that have been deferred because the there
21141  * are no SGLs.  This congestion can occur with large
21142  * vport counts during node discovery.
21143  **/
21144 
21145 uint32_t
lpfc_drain_txq(struct lpfc_hba * phba)21146 lpfc_drain_txq(struct lpfc_hba *phba)
21147 {
21148 	LIST_HEAD(completions);
21149 	struct lpfc_sli_ring *pring;
21150 	struct lpfc_iocbq *piocbq = NULL;
21151 	unsigned long iflags = 0;
21152 	char *fail_msg = NULL;
21153 	uint32_t txq_cnt = 0;
21154 	struct lpfc_queue *wq;
21155 	int ret = 0;
21156 
21157 	if (phba->link_flag & LS_MDS_LOOPBACK) {
21158 		/* MDS WQE are posted only to first WQ*/
21159 		wq = phba->sli4_hba.hdwq[0].io_wq;
21160 		if (unlikely(!wq))
21161 			return 0;
21162 		pring = wq->pring;
21163 	} else {
21164 		wq = phba->sli4_hba.els_wq;
21165 		if (unlikely(!wq))
21166 			return 0;
21167 		pring = lpfc_phba_elsring(phba);
21168 	}
21169 
21170 	if (unlikely(!pring) || list_empty(&pring->txq))
21171 		return 0;
21172 
21173 	spin_lock_irqsave(&pring->ring_lock, iflags);
21174 	list_for_each_entry(piocbq, &pring->txq, list) {
21175 		txq_cnt++;
21176 	}
21177 
21178 	if (txq_cnt > pring->txq_max)
21179 		pring->txq_max = txq_cnt;
21180 
21181 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
21182 
21183 	while (!list_empty(&pring->txq)) {
21184 		spin_lock_irqsave(&pring->ring_lock, iflags);
21185 
21186 		piocbq = lpfc_sli_ringtx_get(phba, pring);
21187 		if (!piocbq) {
21188 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21189 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21190 				"2823 txq empty and txq_cnt is %d\n",
21191 				txq_cnt);
21192 			break;
21193 		}
21194 		txq_cnt--;
21195 
21196 		ret = __lpfc_sli_issue_iocb(phba, pring->ringno, piocbq, 0);
21197 
21198 		if (ret && ret != IOCB_BUSY) {
21199 			fail_msg = " - Cannot send IO ";
21200 			piocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21201 		}
21202 		if (fail_msg) {
21203 			piocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
21204 			/* Failed means we can't issue and need to cancel */
21205 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21206 					"2822 IOCB failed %s iotag 0x%x "
21207 					"xri 0x%x %d flg x%x\n",
21208 					fail_msg, piocbq->iotag,
21209 					piocbq->sli4_xritag, ret,
21210 					piocbq->cmd_flag);
21211 			list_add_tail(&piocbq->list, &completions);
21212 			fail_msg = NULL;
21213 		}
21214 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21215 		if (txq_cnt == 0 || ret == IOCB_BUSY)
21216 			break;
21217 	}
21218 	/* Cancel all the IOCBs that cannot be issued */
21219 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21220 			      IOERR_SLI_ABORTED);
21221 
21222 	return txq_cnt;
21223 }
21224 
21225 /**
21226  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21227  * @phba: Pointer to HBA context object.
21228  * @pwqeq: Pointer to command WQE.
21229  * @sglq: Pointer to the scatter gather queue object.
21230  *
21231  * This routine converts the bpl or bde that is in the WQE
21232  * to a sgl list for the sli4 hardware. The physical address
21233  * of the bpl/bde is converted back to a virtual address.
21234  * If the WQE contains a BPL then the list of BDE's is
21235  * converted to sli4_sge's. If the WQE contains a single
21236  * BDE then it is converted to a single sli_sge.
21237  * The WQE is still in cpu endianness so the contents of
21238  * the bpl can be used without byte swapping.
21239  *
21240  * Returns valid XRI = Success, NO_XRI = Failure.
21241  */
21242 static uint16_t
lpfc_wqe_bpl2sgl(struct lpfc_hba * phba,struct lpfc_iocbq * pwqeq,struct lpfc_sglq * sglq)21243 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21244 		 struct lpfc_sglq *sglq)
21245 {
21246 	uint16_t xritag = NO_XRI;
21247 	struct ulp_bde64 *bpl = NULL;
21248 	struct ulp_bde64 bde;
21249 	struct sli4_sge *sgl  = NULL;
21250 	struct lpfc_dmabuf *dmabuf;
21251 	union lpfc_wqe128 *wqe;
21252 	int numBdes = 0;
21253 	int i = 0;
21254 	uint32_t offset = 0; /* accumulated offset in the sg request list */
21255 	int inbound = 0; /* number of sg reply entries inbound from firmware */
21256 	uint32_t cmd;
21257 
21258 	if (!pwqeq || !sglq)
21259 		return xritag;
21260 
21261 	sgl  = (struct sli4_sge *)sglq->sgl;
21262 	wqe = &pwqeq->wqe;
21263 	pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21264 
21265 	cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21266 	if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21267 		return sglq->sli4_xritag;
21268 	numBdes = pwqeq->num_bdes;
21269 	if (numBdes) {
21270 		/* The addrHigh and addrLow fields within the WQE
21271 		 * have not been byteswapped yet so there is no
21272 		 * need to swap them back.
21273 		 */
21274 		if (pwqeq->bpl_dmabuf)
21275 			dmabuf = pwqeq->bpl_dmabuf;
21276 		else
21277 			return xritag;
21278 
21279 		bpl  = (struct ulp_bde64 *)dmabuf->virt;
21280 		if (!bpl)
21281 			return xritag;
21282 
21283 		for (i = 0; i < numBdes; i++) {
21284 			/* Should already be byte swapped. */
21285 			sgl->addr_hi = bpl->addrHigh;
21286 			sgl->addr_lo = bpl->addrLow;
21287 
21288 			sgl->word2 = le32_to_cpu(sgl->word2);
21289 			if ((i+1) == numBdes)
21290 				bf_set(lpfc_sli4_sge_last, sgl, 1);
21291 			else
21292 				bf_set(lpfc_sli4_sge_last, sgl, 0);
21293 			/* swap the size field back to the cpu so we
21294 			 * can assign it to the sgl.
21295 			 */
21296 			bde.tus.w = le32_to_cpu(bpl->tus.w);
21297 			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21298 			/* The offsets in the sgl need to be accumulated
21299 			 * separately for the request and reply lists.
21300 			 * The request is always first, the reply follows.
21301 			 */
21302 			switch (cmd) {
21303 			case CMD_GEN_REQUEST64_WQE:
21304 				/* add up the reply sg entries */
21305 				if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21306 					inbound++;
21307 				/* first inbound? reset the offset */
21308 				if (inbound == 1)
21309 					offset = 0;
21310 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21311 				bf_set(lpfc_sli4_sge_type, sgl,
21312 					LPFC_SGE_TYPE_DATA);
21313 				offset += bde.tus.f.bdeSize;
21314 				break;
21315 			case CMD_FCP_TRSP64_WQE:
21316 				bf_set(lpfc_sli4_sge_offset, sgl, 0);
21317 				bf_set(lpfc_sli4_sge_type, sgl,
21318 					LPFC_SGE_TYPE_DATA);
21319 				break;
21320 			case CMD_FCP_TSEND64_WQE:
21321 			case CMD_FCP_TRECEIVE64_WQE:
21322 				bf_set(lpfc_sli4_sge_type, sgl,
21323 					bpl->tus.f.bdeFlags);
21324 				if (i < 3)
21325 					offset = 0;
21326 				else
21327 					offset += bde.tus.f.bdeSize;
21328 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21329 				break;
21330 			}
21331 			sgl->word2 = cpu_to_le32(sgl->word2);
21332 			bpl++;
21333 			sgl++;
21334 		}
21335 	} else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21336 		/* The addrHigh and addrLow fields of the BDE have not
21337 		 * been byteswapped yet so they need to be swapped
21338 		 * before putting them in the sgl.
21339 		 */
21340 		sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21341 		sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21342 		sgl->word2 = le32_to_cpu(sgl->word2);
21343 		bf_set(lpfc_sli4_sge_last, sgl, 1);
21344 		sgl->word2 = cpu_to_le32(sgl->word2);
21345 		sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21346 	}
21347 	return sglq->sli4_xritag;
21348 }
21349 
21350 /**
21351  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21352  * @phba: Pointer to HBA context object.
21353  * @qp: Pointer to HDW queue.
21354  * @pwqe: Pointer to command WQE.
21355  **/
21356 int
lpfc_sli4_issue_wqe(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * qp,struct lpfc_iocbq * pwqe)21357 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21358 		    struct lpfc_iocbq *pwqe)
21359 {
21360 	union lpfc_wqe128 *wqe = &pwqe->wqe;
21361 	struct lpfc_async_xchg_ctx *ctxp;
21362 	struct lpfc_queue *wq;
21363 	struct lpfc_sglq *sglq;
21364 	struct lpfc_sli_ring *pring;
21365 	unsigned long iflags;
21366 	uint32_t ret = 0;
21367 
21368 	/* NVME_LS and NVME_LS ABTS requests. */
21369 	if (pwqe->cmd_flag & LPFC_IO_NVME_LS) {
21370 		pring =  phba->sli4_hba.nvmels_wq->pring;
21371 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21372 					  qp, wq_access);
21373 		sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21374 		if (!sglq) {
21375 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21376 			return WQE_BUSY;
21377 		}
21378 		pwqe->sli4_lxritag = sglq->sli4_lxritag;
21379 		pwqe->sli4_xritag = sglq->sli4_xritag;
21380 		if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21381 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21382 			return WQE_ERROR;
21383 		}
21384 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21385 		       pwqe->sli4_xritag);
21386 		ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21387 		if (ret) {
21388 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21389 			return ret;
21390 		}
21391 
21392 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21393 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21394 
21395 		lpfc_sli4_poll_eq(qp->hba_eq);
21396 		return 0;
21397 	}
21398 
21399 	/* NVME_FCREQ and NVME_ABTS requests */
21400 	if (pwqe->cmd_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
21401 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21402 		wq = qp->io_wq;
21403 		pring = wq->pring;
21404 
21405 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21406 
21407 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21408 					  qp, wq_access);
21409 		ret = lpfc_sli4_wq_put(wq, wqe);
21410 		if (ret) {
21411 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21412 			return ret;
21413 		}
21414 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21415 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21416 
21417 		lpfc_sli4_poll_eq(qp->hba_eq);
21418 		return 0;
21419 	}
21420 
21421 	/* NVMET requests */
21422 	if (pwqe->cmd_flag & LPFC_IO_NVMET) {
21423 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21424 		wq = qp->io_wq;
21425 		pring = wq->pring;
21426 
21427 		ctxp = pwqe->context_un.axchg;
21428 		sglq = ctxp->ctxbuf->sglq;
21429 		if (pwqe->sli4_xritag ==  NO_XRI) {
21430 			pwqe->sli4_lxritag = sglq->sli4_lxritag;
21431 			pwqe->sli4_xritag = sglq->sli4_xritag;
21432 		}
21433 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21434 		       pwqe->sli4_xritag);
21435 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21436 
21437 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21438 					  qp, wq_access);
21439 		ret = lpfc_sli4_wq_put(wq, wqe);
21440 		if (ret) {
21441 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21442 			return ret;
21443 		}
21444 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21445 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21446 
21447 		lpfc_sli4_poll_eq(qp->hba_eq);
21448 		return 0;
21449 	}
21450 	return WQE_ERROR;
21451 }
21452 
21453 /**
21454  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21455  * @phba: Pointer to HBA context object.
21456  * @cmdiocb: Pointer to driver command iocb object.
21457  * @cmpl: completion function.
21458  *
21459  * Fill the appropriate fields for the abort WQE and call
21460  * internal routine lpfc_sli4_issue_wqe to send the WQE
21461  * This function is called with hbalock held and no ring_lock held.
21462  *
21463  * RETURNS 0 - SUCCESS
21464  **/
21465 
21466 int
lpfc_sli4_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,void * cmpl)21467 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21468 			    void *cmpl)
21469 {
21470 	struct lpfc_vport *vport = cmdiocb->vport;
21471 	struct lpfc_iocbq *abtsiocb = NULL;
21472 	union lpfc_wqe128 *abtswqe;
21473 	struct lpfc_io_buf *lpfc_cmd;
21474 	int retval = IOCB_ERROR;
21475 	u16 xritag = cmdiocb->sli4_xritag;
21476 
21477 	/*
21478 	 * The scsi command can not be in txq and it is in flight because the
21479 	 * pCmd is still pointing at the SCSI command we have to abort. There
21480 	 * is no need to search the txcmplq. Just send an abort to the FW.
21481 	 */
21482 
21483 	abtsiocb = __lpfc_sli_get_iocbq(phba);
21484 	if (!abtsiocb)
21485 		return WQE_NORESOURCE;
21486 
21487 	/* Indicate the IO is being aborted by the driver. */
21488 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
21489 
21490 	abtswqe = &abtsiocb->wqe;
21491 	memset(abtswqe, 0, sizeof(*abtswqe));
21492 
21493 	if (!lpfc_is_link_up(phba) || (phba->link_flag & LS_EXTERNAL_LOOPBACK))
21494 		bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
21495 	bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21496 	abtswqe->abort_cmd.rsrvd5 = 0;
21497 	abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21498 	bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21499 	bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21500 	bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21501 	bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21502 	bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21503 	bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21504 
21505 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
21506 	abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21507 	abtsiocb->cmd_flag |= LPFC_USE_FCPWQIDX;
21508 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
21509 		abtsiocb->cmd_flag |= LPFC_IO_FCP;
21510 	if (cmdiocb->cmd_flag & LPFC_IO_NVME)
21511 		abtsiocb->cmd_flag |= LPFC_IO_NVME;
21512 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
21513 		abtsiocb->cmd_flag |= LPFC_IO_FOF;
21514 	abtsiocb->vport = vport;
21515 	abtsiocb->cmd_cmpl = cmpl;
21516 
21517 	lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21518 	retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21519 
21520 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21521 			 "0359 Abort xri x%x, original iotag x%x, "
21522 			 "abort cmd iotag x%x retval x%x\n",
21523 			 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21524 
21525 	if (retval) {
21526 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21527 		__lpfc_sli_release_iocbq(phba, abtsiocb);
21528 	}
21529 
21530 	return retval;
21531 }
21532 
21533 #ifdef LPFC_MXP_STAT
21534 /**
21535  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21536  * @phba: pointer to lpfc hba data structure.
21537  * @hwqid: belong to which HWQ.
21538  *
21539  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21540  * 15 seconds after a test case is running.
21541  *
21542  * The user should call lpfc_debugfs_multixripools_write before running a test
21543  * case to clear stat_snapshot_taken. Then the user starts a test case. During
21544  * test case is running, stat_snapshot_taken is incremented by 1 every time when
21545  * this routine is called from heartbeat timer. When stat_snapshot_taken is
21546  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21547  **/
lpfc_snapshot_mxp(struct lpfc_hba * phba,u32 hwqid)21548 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21549 {
21550 	struct lpfc_sli4_hdw_queue *qp;
21551 	struct lpfc_multixri_pool *multixri_pool;
21552 	struct lpfc_pvt_pool *pvt_pool;
21553 	struct lpfc_pbl_pool *pbl_pool;
21554 	u32 txcmplq_cnt;
21555 
21556 	qp = &phba->sli4_hba.hdwq[hwqid];
21557 	multixri_pool = qp->p_multixri_pool;
21558 	if (!multixri_pool)
21559 		return;
21560 
21561 	if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21562 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21563 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21564 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21565 
21566 		multixri_pool->stat_pbl_count = pbl_pool->count;
21567 		multixri_pool->stat_pvt_count = pvt_pool->count;
21568 		multixri_pool->stat_busy_count = txcmplq_cnt;
21569 	}
21570 
21571 	multixri_pool->stat_snapshot_taken++;
21572 }
21573 #endif
21574 
21575 /**
21576  * lpfc_adjust_pvt_pool_count - Adjust private pool count
21577  * @phba: pointer to lpfc hba data structure.
21578  * @hwqid: belong to which HWQ.
21579  *
21580  * This routine moves some XRIs from private to public pool when private pool
21581  * is not busy.
21582  **/
lpfc_adjust_pvt_pool_count(struct lpfc_hba * phba,u32 hwqid)21583 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21584 {
21585 	struct lpfc_multixri_pool *multixri_pool;
21586 	u32 io_req_count;
21587 	u32 prev_io_req_count;
21588 
21589 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21590 	if (!multixri_pool)
21591 		return;
21592 	io_req_count = multixri_pool->io_req_count;
21593 	prev_io_req_count = multixri_pool->prev_io_req_count;
21594 
21595 	if (prev_io_req_count != io_req_count) {
21596 		/* Private pool is busy */
21597 		multixri_pool->prev_io_req_count = io_req_count;
21598 	} else {
21599 		/* Private pool is not busy.
21600 		 * Move XRIs from private to public pool.
21601 		 */
21602 		lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21603 	}
21604 }
21605 
21606 /**
21607  * lpfc_adjust_high_watermark - Adjust high watermark
21608  * @phba: pointer to lpfc hba data structure.
21609  * @hwqid: belong to which HWQ.
21610  *
21611  * This routine sets high watermark as number of outstanding XRIs,
21612  * but make sure the new value is between xri_limit/2 and xri_limit.
21613  **/
lpfc_adjust_high_watermark(struct lpfc_hba * phba,u32 hwqid)21614 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21615 {
21616 	u32 new_watermark;
21617 	u32 watermark_max;
21618 	u32 watermark_min;
21619 	u32 xri_limit;
21620 	u32 txcmplq_cnt;
21621 	u32 abts_io_bufs;
21622 	struct lpfc_multixri_pool *multixri_pool;
21623 	struct lpfc_sli4_hdw_queue *qp;
21624 
21625 	qp = &phba->sli4_hba.hdwq[hwqid];
21626 	multixri_pool = qp->p_multixri_pool;
21627 	if (!multixri_pool)
21628 		return;
21629 	xri_limit = multixri_pool->xri_limit;
21630 
21631 	watermark_max = xri_limit;
21632 	watermark_min = xri_limit / 2;
21633 
21634 	txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21635 	abts_io_bufs = qp->abts_scsi_io_bufs;
21636 	abts_io_bufs += qp->abts_nvme_io_bufs;
21637 
21638 	new_watermark = txcmplq_cnt + abts_io_bufs;
21639 	new_watermark = min(watermark_max, new_watermark);
21640 	new_watermark = max(watermark_min, new_watermark);
21641 	multixri_pool->pvt_pool.high_watermark = new_watermark;
21642 
21643 #ifdef LPFC_MXP_STAT
21644 	multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21645 					  new_watermark);
21646 #endif
21647 }
21648 
21649 /**
21650  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21651  * @phba: pointer to lpfc hba data structure.
21652  * @hwqid: belong to which HWQ.
21653  *
21654  * This routine is called from hearbeat timer when pvt_pool is idle.
21655  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21656  * The first step moves (all - low_watermark) amount of XRIs.
21657  * The second step moves the rest of XRIs.
21658  **/
lpfc_move_xri_pvt_to_pbl(struct lpfc_hba * phba,u32 hwqid)21659 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21660 {
21661 	struct lpfc_pbl_pool *pbl_pool;
21662 	struct lpfc_pvt_pool *pvt_pool;
21663 	struct lpfc_sli4_hdw_queue *qp;
21664 	struct lpfc_io_buf *lpfc_ncmd;
21665 	struct lpfc_io_buf *lpfc_ncmd_next;
21666 	unsigned long iflag;
21667 	struct list_head tmp_list;
21668 	u32 tmp_count;
21669 
21670 	qp = &phba->sli4_hba.hdwq[hwqid];
21671 	pbl_pool = &qp->p_multixri_pool->pbl_pool;
21672 	pvt_pool = &qp->p_multixri_pool->pvt_pool;
21673 	tmp_count = 0;
21674 
21675 	lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21676 	lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
21677 
21678 	if (pvt_pool->count > pvt_pool->low_watermark) {
21679 		/* Step 1: move (all - low_watermark) from pvt_pool
21680 		 * to pbl_pool
21681 		 */
21682 
21683 		/* Move low watermark of bufs from pvt_pool to tmp_list */
21684 		INIT_LIST_HEAD(&tmp_list);
21685 		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21686 					 &pvt_pool->list, list) {
21687 			list_move_tail(&lpfc_ncmd->list, &tmp_list);
21688 			tmp_count++;
21689 			if (tmp_count >= pvt_pool->low_watermark)
21690 				break;
21691 		}
21692 
21693 		/* Move all bufs from pvt_pool to pbl_pool */
21694 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21695 
21696 		/* Move all bufs from tmp_list to pvt_pool */
21697 		list_splice(&tmp_list, &pvt_pool->list);
21698 
21699 		pbl_pool->count += (pvt_pool->count - tmp_count);
21700 		pvt_pool->count = tmp_count;
21701 	} else {
21702 		/* Step 2: move the rest from pvt_pool to pbl_pool */
21703 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21704 		pbl_pool->count += pvt_pool->count;
21705 		pvt_pool->count = 0;
21706 	}
21707 
21708 	spin_unlock(&pvt_pool->lock);
21709 	spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21710 }
21711 
21712 /**
21713  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21714  * @phba: pointer to lpfc hba data structure
21715  * @qp: pointer to HDW queue
21716  * @pbl_pool: specified public free XRI pool
21717  * @pvt_pool: specified private free XRI pool
21718  * @count: number of XRIs to move
21719  *
21720  * This routine tries to move some free common bufs from the specified pbl_pool
21721  * to the specified pvt_pool. It might move less than count XRIs if there's not
21722  * enough in public pool.
21723  *
21724  * Return:
21725  *   true - if XRIs are successfully moved from the specified pbl_pool to the
21726  *          specified pvt_pool
21727  *   false - if the specified pbl_pool is empty or locked by someone else
21728  **/
21729 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)21730 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21731 			  struct lpfc_pbl_pool *pbl_pool,
21732 			  struct lpfc_pvt_pool *pvt_pool, u32 count)
21733 {
21734 	struct lpfc_io_buf *lpfc_ncmd;
21735 	struct lpfc_io_buf *lpfc_ncmd_next;
21736 	unsigned long iflag;
21737 	int ret;
21738 
21739 	ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21740 	if (ret) {
21741 		if (pbl_pool->count) {
21742 			/* Move a batch of XRIs from public to private pool */
21743 			lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
21744 			list_for_each_entry_safe(lpfc_ncmd,
21745 						 lpfc_ncmd_next,
21746 						 &pbl_pool->list,
21747 						 list) {
21748 				list_move_tail(&lpfc_ncmd->list,
21749 					       &pvt_pool->list);
21750 				pvt_pool->count++;
21751 				pbl_pool->count--;
21752 				count--;
21753 				if (count == 0)
21754 					break;
21755 			}
21756 
21757 			spin_unlock(&pvt_pool->lock);
21758 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21759 			return true;
21760 		}
21761 		spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21762 	}
21763 
21764 	return false;
21765 }
21766 
21767 /**
21768  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21769  * @phba: pointer to lpfc hba data structure.
21770  * @hwqid: belong to which HWQ.
21771  * @count: number of XRIs to move
21772  *
21773  * This routine tries to find some free common bufs in one of public pools with
21774  * Round Robin method. The search always starts from local hwqid, then the next
21775  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21776  * a batch of free common bufs are moved to private pool on hwqid.
21777  * It might move less than count XRIs if there's not enough in public pool.
21778  **/
lpfc_move_xri_pbl_to_pvt(struct lpfc_hba * phba,u32 hwqid,u32 count)21779 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21780 {
21781 	struct lpfc_multixri_pool *multixri_pool;
21782 	struct lpfc_multixri_pool *next_multixri_pool;
21783 	struct lpfc_pvt_pool *pvt_pool;
21784 	struct lpfc_pbl_pool *pbl_pool;
21785 	struct lpfc_sli4_hdw_queue *qp;
21786 	u32 next_hwqid;
21787 	u32 hwq_count;
21788 	int ret;
21789 
21790 	qp = &phba->sli4_hba.hdwq[hwqid];
21791 	multixri_pool = qp->p_multixri_pool;
21792 	pvt_pool = &multixri_pool->pvt_pool;
21793 	pbl_pool = &multixri_pool->pbl_pool;
21794 
21795 	/* Check if local pbl_pool is available */
21796 	ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
21797 	if (ret) {
21798 #ifdef LPFC_MXP_STAT
21799 		multixri_pool->local_pbl_hit_count++;
21800 #endif
21801 		return;
21802 	}
21803 
21804 	hwq_count = phba->cfg_hdw_queue;
21805 
21806 	/* Get the next hwqid which was found last time */
21807 	next_hwqid = multixri_pool->rrb_next_hwqid;
21808 
21809 	do {
21810 		/* Go to next hwq */
21811 		next_hwqid = (next_hwqid + 1) % hwq_count;
21812 
21813 		next_multixri_pool =
21814 			phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21815 		pbl_pool = &next_multixri_pool->pbl_pool;
21816 
21817 		/* Check if the public free xri pool is available */
21818 		ret = _lpfc_move_xri_pbl_to_pvt(
21819 			phba, qp, pbl_pool, pvt_pool, count);
21820 
21821 		/* Exit while-loop if success or all hwqid are checked */
21822 	} while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21823 
21824 	/* Starting point for the next time */
21825 	multixri_pool->rrb_next_hwqid = next_hwqid;
21826 
21827 	if (!ret) {
21828 		/* stats: all public pools are empty*/
21829 		multixri_pool->pbl_empty_count++;
21830 	}
21831 
21832 #ifdef LPFC_MXP_STAT
21833 	if (ret) {
21834 		if (next_hwqid == hwqid)
21835 			multixri_pool->local_pbl_hit_count++;
21836 		else
21837 			multixri_pool->other_pbl_hit_count++;
21838 	}
21839 #endif
21840 }
21841 
21842 /**
21843  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21844  * @phba: pointer to lpfc hba data structure.
21845  * @hwqid: belong to which HWQ.
21846  *
21847  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21848  * low watermark.
21849  **/
lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba * phba,u32 hwqid)21850 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21851 {
21852 	struct lpfc_multixri_pool *multixri_pool;
21853 	struct lpfc_pvt_pool *pvt_pool;
21854 
21855 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21856 	pvt_pool = &multixri_pool->pvt_pool;
21857 
21858 	if (pvt_pool->count < pvt_pool->low_watermark)
21859 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21860 }
21861 
21862 /**
21863  * lpfc_release_io_buf - Return one IO buf back to free pool
21864  * @phba: pointer to lpfc hba data structure.
21865  * @lpfc_ncmd: IO buf to be returned.
21866  * @qp: belong to which HWQ.
21867  *
21868  * This routine returns one IO buf back to free pool. If this is an urgent IO,
21869  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21870  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21871  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
21872  * lpfc_io_buf_list_put.
21873  **/
lpfc_release_io_buf(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_ncmd,struct lpfc_sli4_hdw_queue * qp)21874 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21875 			 struct lpfc_sli4_hdw_queue *qp)
21876 {
21877 	unsigned long iflag;
21878 	struct lpfc_pbl_pool *pbl_pool;
21879 	struct lpfc_pvt_pool *pvt_pool;
21880 	struct lpfc_epd_pool *epd_pool;
21881 	u32 txcmplq_cnt;
21882 	u32 xri_owned;
21883 	u32 xri_limit;
21884 	u32 abts_io_bufs;
21885 
21886 	/* MUST zero fields if buffer is reused by another protocol */
21887 	lpfc_ncmd->nvmeCmd = NULL;
21888 	lpfc_ncmd->cur_iocbq.cmd_cmpl = NULL;
21889 
21890 	if (phba->cfg_xpsgl && !phba->nvmet_support &&
21891 	    !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21892 		lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21893 
21894 	if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21895 		lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21896 
21897 	if (phba->cfg_xri_rebalancing) {
21898 		if (lpfc_ncmd->expedite) {
21899 			/* Return to expedite pool */
21900 			epd_pool = &phba->epd_pool;
21901 			spin_lock_irqsave(&epd_pool->lock, iflag);
21902 			list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21903 			epd_pool->count++;
21904 			spin_unlock_irqrestore(&epd_pool->lock, iflag);
21905 			return;
21906 		}
21907 
21908 		/* Avoid invalid access if an IO sneaks in and is being rejected
21909 		 * just _after_ xri pools are destroyed in lpfc_offline.
21910 		 * Nothing much can be done at this point.
21911 		 */
21912 		if (!qp->p_multixri_pool)
21913 			return;
21914 
21915 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21916 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21917 
21918 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21919 		abts_io_bufs = qp->abts_scsi_io_bufs;
21920 		abts_io_bufs += qp->abts_nvme_io_bufs;
21921 
21922 		xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21923 		xri_limit = qp->p_multixri_pool->xri_limit;
21924 
21925 #ifdef LPFC_MXP_STAT
21926 		if (xri_owned <= xri_limit)
21927 			qp->p_multixri_pool->below_limit_count++;
21928 		else
21929 			qp->p_multixri_pool->above_limit_count++;
21930 #endif
21931 
21932 		/* XRI goes to either public or private free xri pool
21933 		 *     based on watermark and xri_limit
21934 		 */
21935 		if ((pvt_pool->count < pvt_pool->low_watermark) ||
21936 		    (xri_owned < xri_limit &&
21937 		     pvt_pool->count < pvt_pool->high_watermark)) {
21938 			lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21939 						  qp, free_pvt_pool);
21940 			list_add_tail(&lpfc_ncmd->list,
21941 				      &pvt_pool->list);
21942 			pvt_pool->count++;
21943 			spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21944 		} else {
21945 			lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21946 						  qp, free_pub_pool);
21947 			list_add_tail(&lpfc_ncmd->list,
21948 				      &pbl_pool->list);
21949 			pbl_pool->count++;
21950 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21951 		}
21952 	} else {
21953 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21954 					  qp, free_xri);
21955 		list_add_tail(&lpfc_ncmd->list,
21956 			      &qp->lpfc_io_buf_list_put);
21957 		qp->put_io_bufs++;
21958 		spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21959 				       iflag);
21960 	}
21961 }
21962 
21963 /**
21964  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21965  * @phba: pointer to lpfc hba data structure.
21966  * @qp: pointer to HDW queue
21967  * @pvt_pool: pointer to private pool data structure.
21968  * @ndlp: pointer to lpfc nodelist data structure.
21969  *
21970  * This routine tries to get one free IO buf from private pool.
21971  *
21972  * Return:
21973  *   pointer to one free IO buf - if private pool is not empty
21974  *   NULL - if private pool is empty
21975  **/
21976 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)21977 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
21978 				  struct lpfc_sli4_hdw_queue *qp,
21979 				  struct lpfc_pvt_pool *pvt_pool,
21980 				  struct lpfc_nodelist *ndlp)
21981 {
21982 	struct lpfc_io_buf *lpfc_ncmd;
21983 	struct lpfc_io_buf *lpfc_ncmd_next;
21984 	unsigned long iflag;
21985 
21986 	lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
21987 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21988 				 &pvt_pool->list, list) {
21989 		if (lpfc_test_rrq_active(
21990 			phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21991 			continue;
21992 		list_del(&lpfc_ncmd->list);
21993 		pvt_pool->count--;
21994 		spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21995 		return lpfc_ncmd;
21996 	}
21997 	spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21998 
21999 	return NULL;
22000 }
22001 
22002 /**
22003  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
22004  * @phba: pointer to lpfc hba data structure.
22005  *
22006  * This routine tries to get one free IO buf from expedite pool.
22007  *
22008  * Return:
22009  *   pointer to one free IO buf - if expedite pool is not empty
22010  *   NULL - if expedite pool is empty
22011  **/
22012 static struct lpfc_io_buf *
lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba * phba)22013 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
22014 {
22015 	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
22016 	struct lpfc_io_buf *lpfc_ncmd_next;
22017 	unsigned long iflag;
22018 	struct lpfc_epd_pool *epd_pool;
22019 
22020 	epd_pool = &phba->epd_pool;
22021 
22022 	spin_lock_irqsave(&epd_pool->lock, iflag);
22023 	if (epd_pool->count > 0) {
22024 		list_for_each_entry_safe(iter, lpfc_ncmd_next,
22025 					 &epd_pool->list, list) {
22026 			list_del(&iter->list);
22027 			epd_pool->count--;
22028 			lpfc_ncmd = iter;
22029 			break;
22030 		}
22031 	}
22032 	spin_unlock_irqrestore(&epd_pool->lock, iflag);
22033 
22034 	return lpfc_ncmd;
22035 }
22036 
22037 /**
22038  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
22039  * @phba: pointer to lpfc hba data structure.
22040  * @ndlp: pointer to lpfc nodelist data structure.
22041  * @hwqid: belong to which HWQ
22042  * @expedite: 1 means this request is urgent.
22043  *
22044  * This routine will do the following actions and then return a pointer to
22045  * one free IO buf.
22046  *
22047  * 1. If private free xri count is empty, move some XRIs from public to
22048  *    private pool.
22049  * 2. Get one XRI from private free xri pool.
22050  * 3. If we fail to get one from pvt_pool and this is an expedite request,
22051  *    get one free xri from expedite pool.
22052  *
22053  * Note: ndlp is only used on SCSI side for RRQ testing.
22054  *       The caller should pass NULL for ndlp on NVME side.
22055  *
22056  * Return:
22057  *   pointer to one free IO buf - if private pool is not empty
22058  *   NULL - if private pool is empty
22059  **/
22060 static struct lpfc_io_buf *
lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int hwqid,int expedite)22061 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
22062 				    struct lpfc_nodelist *ndlp,
22063 				    int hwqid, int expedite)
22064 {
22065 	struct lpfc_sli4_hdw_queue *qp;
22066 	struct lpfc_multixri_pool *multixri_pool;
22067 	struct lpfc_pvt_pool *pvt_pool;
22068 	struct lpfc_io_buf *lpfc_ncmd;
22069 
22070 	qp = &phba->sli4_hba.hdwq[hwqid];
22071 	lpfc_ncmd = NULL;
22072 	if (!qp) {
22073 		lpfc_printf_log(phba, KERN_INFO,
22074 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22075 				"5556 NULL qp for hwqid  x%x\n", hwqid);
22076 		return lpfc_ncmd;
22077 	}
22078 	multixri_pool = qp->p_multixri_pool;
22079 	if (!multixri_pool) {
22080 		lpfc_printf_log(phba, KERN_INFO,
22081 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22082 				"5557 NULL multixri for hwqid  x%x\n", hwqid);
22083 		return lpfc_ncmd;
22084 	}
22085 	pvt_pool = &multixri_pool->pvt_pool;
22086 	if (!pvt_pool) {
22087 		lpfc_printf_log(phba, KERN_INFO,
22088 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22089 				"5558 NULL pvt_pool for hwqid  x%x\n", hwqid);
22090 		return lpfc_ncmd;
22091 	}
22092 	multixri_pool->io_req_count++;
22093 
22094 	/* If pvt_pool is empty, move some XRIs from public to private pool */
22095 	if (pvt_pool->count == 0)
22096 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
22097 
22098 	/* Get one XRI from private free xri pool */
22099 	lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
22100 
22101 	if (lpfc_ncmd) {
22102 		lpfc_ncmd->hdwq = qp;
22103 		lpfc_ncmd->hdwq_no = hwqid;
22104 	} else if (expedite) {
22105 		/* If we fail to get one from pvt_pool and this is an expedite
22106 		 * request, get one free xri from expedite pool.
22107 		 */
22108 		lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
22109 	}
22110 
22111 	return lpfc_ncmd;
22112 }
22113 
22114 static inline struct lpfc_io_buf *
lpfc_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int idx)22115 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
22116 {
22117 	struct lpfc_sli4_hdw_queue *qp;
22118 	struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
22119 
22120 	qp = &phba->sli4_hba.hdwq[idx];
22121 	list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22122 				 &qp->lpfc_io_buf_list_get, list) {
22123 		if (lpfc_test_rrq_active(phba, ndlp,
22124 					 lpfc_cmd->cur_iocbq.sli4_lxritag))
22125 			continue;
22126 
22127 		if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22128 			continue;
22129 
22130 		list_del_init(&lpfc_cmd->list);
22131 		qp->get_io_bufs--;
22132 		lpfc_cmd->hdwq = qp;
22133 		lpfc_cmd->hdwq_no = idx;
22134 		return lpfc_cmd;
22135 	}
22136 	return NULL;
22137 }
22138 
22139 /**
22140  * lpfc_get_io_buf - Get one IO buffer from free pool
22141  * @phba: The HBA for which this call is being executed.
22142  * @ndlp: pointer to lpfc nodelist data structure.
22143  * @hwqid: belong to which HWQ
22144  * @expedite: 1 means this request is urgent.
22145  *
22146  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22147  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22148  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22149  *
22150  * Note: ndlp is only used on SCSI side for RRQ testing.
22151  *       The caller should pass NULL for ndlp on NVME side.
22152  *
22153  * Return codes:
22154  *   NULL - Error
22155  *   Pointer to lpfc_io_buf - Success
22156  **/
lpfc_get_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,u32 hwqid,int expedite)22157 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22158 				    struct lpfc_nodelist *ndlp,
22159 				    u32 hwqid, int expedite)
22160 {
22161 	struct lpfc_sli4_hdw_queue *qp;
22162 	unsigned long iflag;
22163 	struct lpfc_io_buf *lpfc_cmd;
22164 
22165 	qp = &phba->sli4_hba.hdwq[hwqid];
22166 	lpfc_cmd = NULL;
22167 	if (!qp) {
22168 		lpfc_printf_log(phba, KERN_WARNING,
22169 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22170 				"5555 NULL qp for hwqid  x%x\n", hwqid);
22171 		return lpfc_cmd;
22172 	}
22173 
22174 	if (phba->cfg_xri_rebalancing)
22175 		lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22176 			phba, ndlp, hwqid, expedite);
22177 	else {
22178 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22179 					  qp, alloc_xri_get);
22180 		if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22181 			lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22182 		if (!lpfc_cmd) {
22183 			lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22184 					  qp, alloc_xri_put);
22185 			list_splice(&qp->lpfc_io_buf_list_put,
22186 				    &qp->lpfc_io_buf_list_get);
22187 			qp->get_io_bufs += qp->put_io_bufs;
22188 			INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22189 			qp->put_io_bufs = 0;
22190 			spin_unlock(&qp->io_buf_list_put_lock);
22191 			if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22192 			    expedite)
22193 				lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22194 		}
22195 		spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22196 	}
22197 
22198 	return lpfc_cmd;
22199 }
22200 
22201 /**
22202  * lpfc_read_object - Retrieve object data from HBA
22203  * @phba: The HBA for which this call is being executed.
22204  * @rdobject: Pathname of object data we want to read.
22205  * @datap: Pointer to where data will be copied to.
22206  * @datasz: size of data area
22207  *
22208  * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22209  * The data will be truncated if datasz is not large enough.
22210  * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22211  * Returns the actual bytes read from the object.
22212  *
22213  * This routine is hard coded to use a poll completion.  Unlike other
22214  * sli4_config mailboxes, it uses lpfc_mbuf memory which is not
22215  * cleaned up in lpfc_sli4_cmd_mbox_free.  If this routine is modified
22216  * to use interrupt-based completions, code is needed to fully cleanup
22217  * the memory.
22218  */
22219 int
lpfc_read_object(struct lpfc_hba * phba,char * rdobject,uint32_t * datap,uint32_t datasz)22220 lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22221 		 uint32_t datasz)
22222 {
22223 	struct lpfc_mbx_read_object *read_object;
22224 	LPFC_MBOXQ_t *mbox;
22225 	int rc, length, eof, j, byte_cnt = 0;
22226 	uint32_t shdr_status, shdr_add_status;
22227 	union lpfc_sli4_cfg_shdr *shdr;
22228 	struct lpfc_dmabuf *pcmd;
22229 	u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
22230 
22231 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22232 	if (!mbox)
22233 		return -ENOMEM;
22234 	length = (sizeof(struct lpfc_mbx_read_object) -
22235 		  sizeof(struct lpfc_sli4_cfg_mhdr));
22236 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22237 			 LPFC_MBOX_OPCODE_READ_OBJECT,
22238 			 length, LPFC_SLI4_MBX_EMBED);
22239 	read_object = &mbox->u.mqe.un.read_object;
22240 	shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22241 
22242 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22243 	bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22244 	read_object->u.request.rd_object_offset = 0;
22245 	read_object->u.request.rd_object_cnt = 1;
22246 
22247 	memset((void *)read_object->u.request.rd_object_name, 0,
22248 	       LPFC_OBJ_NAME_SZ);
22249 	scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
22250 	for (j = 0; j < strlen(rdobject); j++)
22251 		read_object->u.request.rd_object_name[j] =
22252 			cpu_to_le32(rd_object_name[j]);
22253 
22254 	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22255 	if (pcmd)
22256 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22257 	if (!pcmd || !pcmd->virt) {
22258 		kfree(pcmd);
22259 		mempool_free(mbox, phba->mbox_mem_pool);
22260 		return -ENOMEM;
22261 	}
22262 	memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22263 	read_object->u.request.rd_object_hbuf[0].pa_lo =
22264 		putPaddrLow(pcmd->phys);
22265 	read_object->u.request.rd_object_hbuf[0].pa_hi =
22266 		putPaddrHigh(pcmd->phys);
22267 	read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22268 
22269 	mbox->vport = phba->pport;
22270 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22271 	mbox->ctx_ndlp = NULL;
22272 
22273 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22274 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22275 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22276 
22277 	if (shdr_status == STATUS_FAILED &&
22278 	    shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22279 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22280 				"4674 No port cfg file in FW.\n");
22281 		byte_cnt = -ENOENT;
22282 	} else if (shdr_status || shdr_add_status || rc) {
22283 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22284 				"2625 READ_OBJECT mailbox failed with "
22285 				"status x%x add_status x%x, mbx status x%x\n",
22286 				shdr_status, shdr_add_status, rc);
22287 		byte_cnt = -ENXIO;
22288 	} else {
22289 		/* Success */
22290 		length = read_object->u.response.rd_object_actual_rlen;
22291 		eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22292 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22293 				"2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22294 				length, datasz, eof);
22295 
22296 		/* Detect the port config file exists but is empty */
22297 		if (!length && eof) {
22298 			byte_cnt = 0;
22299 			goto exit;
22300 		}
22301 
22302 		byte_cnt = length;
22303 		lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22304 	}
22305 
22306  exit:
22307 	/* This is an embedded SLI4 mailbox with an external buffer allocated.
22308 	 * Free the pcmd and then cleanup with the correct routine.
22309 	 */
22310 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22311 	kfree(pcmd);
22312 	lpfc_sli4_mbox_cmd_free(phba, mbox);
22313 	return byte_cnt;
22314 }
22315 
22316 /**
22317  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22318  * @phba: The HBA for which this call is being executed.
22319  * @lpfc_buf: IO buf structure to append the SGL chunk
22320  *
22321  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22322  * and will allocate an SGL chunk if the pool is empty.
22323  *
22324  * Return codes:
22325  *   NULL - Error
22326  *   Pointer to sli4_hybrid_sgl - Success
22327  **/
22328 struct sli4_hybrid_sgl *
lpfc_get_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22329 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22330 {
22331 	struct sli4_hybrid_sgl *list_entry = NULL;
22332 	struct sli4_hybrid_sgl *tmp = NULL;
22333 	struct sli4_hybrid_sgl *allocated_sgl = NULL;
22334 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22335 	struct list_head *buf_list = &hdwq->sgl_list;
22336 	unsigned long iflags;
22337 
22338 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22339 
22340 	if (likely(!list_empty(buf_list))) {
22341 		/* break off 1 chunk from the sgl_list */
22342 		list_for_each_entry_safe(list_entry, tmp,
22343 					 buf_list, list_node) {
22344 			list_move_tail(&list_entry->list_node,
22345 				       &lpfc_buf->dma_sgl_xtra_list);
22346 			break;
22347 		}
22348 	} else {
22349 		/* allocate more */
22350 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22351 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22352 				   cpu_to_node(hdwq->io_wq->chann));
22353 		if (!tmp) {
22354 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22355 					"8353 error kmalloc memory for HDWQ "
22356 					"%d %s\n",
22357 					lpfc_buf->hdwq_no, __func__);
22358 			return NULL;
22359 		}
22360 
22361 		tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22362 					      GFP_ATOMIC, &tmp->dma_phys_sgl);
22363 		if (!tmp->dma_sgl) {
22364 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22365 					"8354 error pool_alloc memory for HDWQ "
22366 					"%d %s\n",
22367 					lpfc_buf->hdwq_no, __func__);
22368 			kfree(tmp);
22369 			return NULL;
22370 		}
22371 
22372 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22373 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22374 	}
22375 
22376 	allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22377 					struct sli4_hybrid_sgl,
22378 					list_node);
22379 
22380 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22381 
22382 	return allocated_sgl;
22383 }
22384 
22385 /**
22386  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22387  * @phba: The HBA for which this call is being executed.
22388  * @lpfc_buf: IO buf structure with the SGL chunk
22389  *
22390  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22391  *
22392  * Return codes:
22393  *   0 - Success
22394  *   -EINVAL - Error
22395  **/
22396 int
lpfc_put_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22397 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22398 {
22399 	int rc = 0;
22400 	struct sli4_hybrid_sgl *list_entry = NULL;
22401 	struct sli4_hybrid_sgl *tmp = NULL;
22402 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22403 	struct list_head *buf_list = &hdwq->sgl_list;
22404 	unsigned long iflags;
22405 
22406 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22407 
22408 	if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22409 		list_for_each_entry_safe(list_entry, tmp,
22410 					 &lpfc_buf->dma_sgl_xtra_list,
22411 					 list_node) {
22412 			list_move_tail(&list_entry->list_node,
22413 				       buf_list);
22414 		}
22415 	} else {
22416 		rc = -EINVAL;
22417 	}
22418 
22419 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22420 	return rc;
22421 }
22422 
22423 /**
22424  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22425  * @phba: phba object
22426  * @hdwq: hdwq to cleanup sgl buff resources on
22427  *
22428  * This routine frees all SGL chunks of hdwq SGL chunk pool.
22429  *
22430  * Return codes:
22431  *   None
22432  **/
22433 void
lpfc_free_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22434 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22435 		       struct lpfc_sli4_hdw_queue *hdwq)
22436 {
22437 	struct list_head *buf_list = &hdwq->sgl_list;
22438 	struct sli4_hybrid_sgl *list_entry = NULL;
22439 	struct sli4_hybrid_sgl *tmp = NULL;
22440 	unsigned long iflags;
22441 
22442 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22443 
22444 	/* Free sgl pool */
22445 	list_for_each_entry_safe(list_entry, tmp,
22446 				 buf_list, list_node) {
22447 		list_del(&list_entry->list_node);
22448 		dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22449 			      list_entry->dma_sgl,
22450 			      list_entry->dma_phys_sgl);
22451 		kfree(list_entry);
22452 	}
22453 
22454 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22455 }
22456 
22457 /**
22458  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22459  * @phba: The HBA for which this call is being executed.
22460  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22461  *
22462  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22463  * and will allocate an CMD/RSP buffer if the pool is empty.
22464  *
22465  * Return codes:
22466  *   NULL - Error
22467  *   Pointer to fcp_cmd_rsp_buf - Success
22468  **/
22469 struct fcp_cmd_rsp_buf *
lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22470 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22471 			      struct lpfc_io_buf *lpfc_buf)
22472 {
22473 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22474 	struct fcp_cmd_rsp_buf *tmp = NULL;
22475 	struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22476 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22477 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22478 	unsigned long iflags;
22479 
22480 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22481 
22482 	if (likely(!list_empty(buf_list))) {
22483 		/* break off 1 chunk from the list */
22484 		list_for_each_entry_safe(list_entry, tmp,
22485 					 buf_list,
22486 					 list_node) {
22487 			list_move_tail(&list_entry->list_node,
22488 				       &lpfc_buf->dma_cmd_rsp_list);
22489 			break;
22490 		}
22491 	} else {
22492 		/* allocate more */
22493 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22494 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22495 				   cpu_to_node(hdwq->io_wq->chann));
22496 		if (!tmp) {
22497 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22498 					"8355 error kmalloc memory for HDWQ "
22499 					"%d %s\n",
22500 					lpfc_buf->hdwq_no, __func__);
22501 			return NULL;
22502 		}
22503 
22504 		tmp->fcp_cmnd = dma_pool_zalloc(phba->lpfc_cmd_rsp_buf_pool,
22505 						GFP_ATOMIC,
22506 						&tmp->fcp_cmd_rsp_dma_handle);
22507 
22508 		if (!tmp->fcp_cmnd) {
22509 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22510 					"8356 error pool_alloc memory for HDWQ "
22511 					"%d %s\n",
22512 					lpfc_buf->hdwq_no, __func__);
22513 			kfree(tmp);
22514 			return NULL;
22515 		}
22516 
22517 		tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22518 				sizeof(struct fcp_cmnd32));
22519 
22520 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22521 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22522 	}
22523 
22524 	allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22525 					struct fcp_cmd_rsp_buf,
22526 					list_node);
22527 
22528 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22529 
22530 	return allocated_buf;
22531 }
22532 
22533 /**
22534  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22535  * @phba: The HBA for which this call is being executed.
22536  * @lpfc_buf: IO buf structure with the CMD/RSP buf
22537  *
22538  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22539  *
22540  * Return codes:
22541  *   0 - Success
22542  *   -EINVAL - Error
22543  **/
22544 int
lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22545 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22546 			      struct lpfc_io_buf *lpfc_buf)
22547 {
22548 	int rc = 0;
22549 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22550 	struct fcp_cmd_rsp_buf *tmp = NULL;
22551 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22552 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22553 	unsigned long iflags;
22554 
22555 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22556 
22557 	if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22558 		list_for_each_entry_safe(list_entry, tmp,
22559 					 &lpfc_buf->dma_cmd_rsp_list,
22560 					 list_node) {
22561 			list_move_tail(&list_entry->list_node,
22562 				       buf_list);
22563 		}
22564 	} else {
22565 		rc = -EINVAL;
22566 	}
22567 
22568 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22569 	return rc;
22570 }
22571 
22572 /**
22573  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22574  * @phba: phba object
22575  * @hdwq: hdwq to cleanup cmd rsp buff resources on
22576  *
22577  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22578  *
22579  * Return codes:
22580  *   None
22581  **/
22582 void
lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22583 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22584 			       struct lpfc_sli4_hdw_queue *hdwq)
22585 {
22586 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22587 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22588 	struct fcp_cmd_rsp_buf *tmp = NULL;
22589 	unsigned long iflags;
22590 
22591 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22592 
22593 	/* Free cmd_rsp buf pool */
22594 	list_for_each_entry_safe(list_entry, tmp,
22595 				 buf_list,
22596 				 list_node) {
22597 		list_del(&list_entry->list_node);
22598 		dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22599 			      list_entry->fcp_cmnd,
22600 			      list_entry->fcp_cmd_rsp_dma_handle);
22601 		kfree(list_entry);
22602 	}
22603 
22604 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22605 }
22606 
22607 /**
22608  * lpfc_sli_prep_wqe - Prepare WQE for the command to be posted
22609  * @phba: phba object
22610  * @job: job entry of the command to be posted.
22611  *
22612  * Fill the common fields of the wqe for each of the command.
22613  *
22614  * Return codes:
22615  *	None
22616  **/
22617 void
lpfc_sli_prep_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * job)22618 lpfc_sli_prep_wqe(struct lpfc_hba *phba, struct lpfc_iocbq *job)
22619 {
22620 	u8 cmnd;
22621 	u32 *pcmd;
22622 	u32 if_type = 0;
22623 	u32 abort_tag;
22624 	bool fip;
22625 	struct lpfc_nodelist *ndlp = NULL;
22626 	union lpfc_wqe128 *wqe = &job->wqe;
22627 	u8 command_type = ELS_COMMAND_NON_FIP;
22628 
22629 	fip = test_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
22630 	/* The fcp commands will set command type */
22631 	if (job->cmd_flag &  LPFC_IO_FCP)
22632 		command_type = FCP_COMMAND;
22633 	else if (fip && (job->cmd_flag & LPFC_FIP_ELS_ID_MASK))
22634 		command_type = ELS_COMMAND_FIP;
22635 	else
22636 		command_type = ELS_COMMAND_NON_FIP;
22637 
22638 	abort_tag = job->iotag;
22639 	cmnd = bf_get(wqe_cmnd, &wqe->els_req.wqe_com);
22640 
22641 	switch (cmnd) {
22642 	case CMD_ELS_REQUEST64_WQE:
22643 		ndlp = job->ndlp;
22644 
22645 		if_type = bf_get(lpfc_sli_intf_if_type,
22646 				 &phba->sli4_hba.sli_intf);
22647 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22648 			pcmd = (u32 *)job->cmd_dmabuf->virt;
22649 			if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
22650 				     *pcmd == ELS_CMD_SCR ||
22651 				     *pcmd == ELS_CMD_RDF ||
22652 				     *pcmd == ELS_CMD_EDC ||
22653 				     *pcmd == ELS_CMD_RSCN_XMT ||
22654 				     *pcmd == ELS_CMD_FDISC ||
22655 				     *pcmd == ELS_CMD_LOGO ||
22656 				     *pcmd == ELS_CMD_QFPA ||
22657 				     *pcmd == ELS_CMD_UVEM ||
22658 				     *pcmd == ELS_CMD_PLOGI)) {
22659 				bf_set(els_req64_sp, &wqe->els_req, 1);
22660 				bf_set(els_req64_sid, &wqe->els_req,
22661 				       job->vport->fc_myDID);
22662 
22663 				if ((*pcmd == ELS_CMD_FLOGI) &&
22664 				    !(phba->fc_topology ==
22665 				      LPFC_TOPOLOGY_LOOP))
22666 					bf_set(els_req64_sid, &wqe->els_req, 0);
22667 
22668 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
22669 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22670 				       phba->vpi_ids[job->vport->vpi]);
22671 			} else if (pcmd) {
22672 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
22673 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22674 				       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22675 			}
22676 		}
22677 
22678 		bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
22679 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22680 
22681 		bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
22682 		bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
22683 		bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
22684 		bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22685 		bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
22686 		break;
22687 	case CMD_XMIT_ELS_RSP64_WQE:
22688 		ndlp = job->ndlp;
22689 
22690 		/* word4 */
22691 		wqe->xmit_els_rsp.word4 = 0;
22692 
22693 		if_type = bf_get(lpfc_sli_intf_if_type,
22694 				 &phba->sli4_hba.sli_intf);
22695 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22696 			if (test_bit(FC_PT2PT, &job->vport->fc_flag)) {
22697 				bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22698 				bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22699 				       job->vport->fc_myDID);
22700 				if (job->vport->fc_myDID == Fabric_DID) {
22701 					bf_set(wqe_els_did,
22702 					       &wqe->xmit_els_rsp.wqe_dest, 0);
22703 				}
22704 			}
22705 		}
22706 
22707 		bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
22708 		bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
22709 		bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
22710 		bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
22711 		       LPFC_WQE_LENLOC_WORD3);
22712 		bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
22713 
22714 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
22715 			bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22716 			bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22717 			       job->vport->fc_myDID);
22718 			bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
22719 		}
22720 
22721 		if (phba->sli_rev == LPFC_SLI_REV4) {
22722 			bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
22723 			       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22724 
22725 			if (bf_get(wqe_ct, &wqe->xmit_els_rsp.wqe_com))
22726 				bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
22727 				       phba->vpi_ids[job->vport->vpi]);
22728 		}
22729 		command_type = OTHER_COMMAND;
22730 		break;
22731 	case CMD_GEN_REQUEST64_WQE:
22732 		/* Word 10 */
22733 		bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
22734 		bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
22735 		bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
22736 		bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22737 		bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
22738 		command_type = OTHER_COMMAND;
22739 		break;
22740 	case CMD_XMIT_SEQUENCE64_WQE:
22741 		if (phba->link_flag & LS_LOOPBACK_MODE)
22742 			bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
22743 
22744 		wqe->xmit_sequence.rsvd3 = 0;
22745 		bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
22746 		bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
22747 		bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
22748 		       LPFC_WQE_IOD_WRITE);
22749 		bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
22750 		       LPFC_WQE_LENLOC_WORD12);
22751 		bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
22752 		command_type = OTHER_COMMAND;
22753 		break;
22754 	case CMD_XMIT_BLS_RSP64_WQE:
22755 		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
22756 		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
22757 		bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
22758 		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
22759 		       phba->vpi_ids[phba->pport->vpi]);
22760 		bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
22761 		bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
22762 		       LPFC_WQE_LENLOC_NONE);
22763 		/* Overwrite the pre-set comnd type with OTHER_COMMAND */
22764 		command_type = OTHER_COMMAND;
22765 		break;
22766 	case CMD_FCP_ICMND64_WQE:	/* task mgmt commands */
22767 	case CMD_ABORT_XRI_WQE:		/* abort iotag */
22768 	case CMD_SEND_FRAME:		/* mds loopback */
22769 		/* cases already formatted for sli4 wqe - no chgs necessary */
22770 		return;
22771 	default:
22772 		dump_stack();
22773 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
22774 				"6207 Invalid command 0x%x\n",
22775 				cmnd);
22776 		break;
22777 	}
22778 
22779 	wqe->generic.wqe_com.abort_tag = abort_tag;
22780 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, job->iotag);
22781 	bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
22782 	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
22783 }
22784