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 + msecs_to_jiffies(1000 * (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 +
1212 				msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1213 	rrq->nlp_DID = ndlp->nlp_DID;
1214 	rrq->vport = ndlp->vport;
1215 	rrq->rxid = rxid;
1216 
1217 	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
1218 	empty = list_empty(&phba->active_rrq_list);
1219 	list_add_tail(&rrq->list, &phba->active_rrq_list);
1220 	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
1221 	set_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
1222 	if (empty)
1223 		lpfc_worker_wake_up(phba);
1224 	return 0;
1225 out:
1226 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1227 outnl:
1228 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1229 			"2921 Can't set rrq active xri:0x%x rxid:0x%x"
1230 			" DID:0x%x Send:%d\n",
1231 			xritag, rxid, ndlp->nlp_DID, send_rrq);
1232 	return -EINVAL;
1233 }
1234 
1235 /**
1236  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1237  * @phba: Pointer to HBA context object.
1238  * @piocbq: Pointer to the iocbq.
1239  *
1240  * The driver calls this function with either the nvme ls ring lock
1241  * or the fc els ring lock held depending on the iocb usage.  This function
1242  * gets a new driver sglq object from the sglq list. If the list is not empty
1243  * then it is successful, it returns pointer to the newly allocated sglq
1244  * object else it returns NULL.
1245  **/
1246 static struct lpfc_sglq *
__lpfc_sli_get_els_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1247 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1248 {
1249 	struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1250 	struct lpfc_sglq *sglq = NULL;
1251 	struct lpfc_sglq *start_sglq = NULL;
1252 	struct lpfc_io_buf *lpfc_cmd;
1253 	struct lpfc_nodelist *ndlp;
1254 	int found = 0;
1255 	u8 cmnd;
1256 
1257 	cmnd = get_job_cmnd(phba, piocbq);
1258 
1259 	if (piocbq->cmd_flag & LPFC_IO_FCP) {
1260 		lpfc_cmd = piocbq->io_buf;
1261 		ndlp = lpfc_cmd->rdata->pnode;
1262 	} else  if ((cmnd == CMD_GEN_REQUEST64_CR) &&
1263 			!(piocbq->cmd_flag & LPFC_IO_LIBDFC)) {
1264 		ndlp = piocbq->ndlp;
1265 	} else  if (piocbq->cmd_flag & LPFC_IO_LIBDFC) {
1266 		if (piocbq->cmd_flag & LPFC_IO_LOOPBACK)
1267 			ndlp = NULL;
1268 		else
1269 			ndlp = piocbq->ndlp;
1270 	} else {
1271 		ndlp = piocbq->ndlp;
1272 	}
1273 
1274 	spin_lock(&phba->sli4_hba.sgl_list_lock);
1275 	list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1276 	start_sglq = sglq;
1277 	while (!found) {
1278 		if (!sglq)
1279 			break;
1280 		if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1281 		    test_bit(sglq->sli4_lxritag,
1282 		    ndlp->active_rrqs_xri_bitmap)) {
1283 			/* This xri has an rrq outstanding for this DID.
1284 			 * put it back in the list and get another xri.
1285 			 */
1286 			list_add_tail(&sglq->list, lpfc_els_sgl_list);
1287 			sglq = NULL;
1288 			list_remove_head(lpfc_els_sgl_list, sglq,
1289 						struct lpfc_sglq, list);
1290 			if (sglq == start_sglq) {
1291 				list_add_tail(&sglq->list, lpfc_els_sgl_list);
1292 				sglq = NULL;
1293 				break;
1294 			} else
1295 				continue;
1296 		}
1297 		sglq->ndlp = ndlp;
1298 		found = 1;
1299 		phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1300 		sglq->state = SGL_ALLOCATED;
1301 	}
1302 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
1303 	return sglq;
1304 }
1305 
1306 /**
1307  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1308  * @phba: Pointer to HBA context object.
1309  * @piocbq: Pointer to the iocbq.
1310  *
1311  * This function is called with the sgl_list lock held. This function
1312  * gets a new driver sglq object from the sglq list. If the
1313  * list is not empty then it is successful, it returns pointer to the newly
1314  * allocated sglq object else it returns NULL.
1315  **/
1316 struct lpfc_sglq *
__lpfc_sli_get_nvmet_sglq(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq)1317 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1318 {
1319 	struct list_head *lpfc_nvmet_sgl_list;
1320 	struct lpfc_sglq *sglq = NULL;
1321 
1322 	lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1323 
1324 	lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1325 
1326 	list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1327 	if (!sglq)
1328 		return NULL;
1329 	phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1330 	sglq->state = SGL_ALLOCATED;
1331 	return sglq;
1332 }
1333 
1334 /**
1335  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1336  * @phba: Pointer to HBA context object.
1337  *
1338  * This function is called with no lock held. This function
1339  * allocates a new driver iocb object from the iocb pool. If the
1340  * allocation is successful, it returns pointer to the newly
1341  * allocated iocb object else it returns NULL.
1342  **/
1343 struct lpfc_iocbq *
lpfc_sli_get_iocbq(struct lpfc_hba * phba)1344 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1345 {
1346 	struct lpfc_iocbq * iocbq = NULL;
1347 	unsigned long iflags;
1348 
1349 	spin_lock_irqsave(&phba->hbalock, iflags);
1350 	iocbq = __lpfc_sli_get_iocbq(phba);
1351 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1352 	return iocbq;
1353 }
1354 
1355 /**
1356  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1357  * @phba: Pointer to HBA context object.
1358  * @iocbq: Pointer to driver iocb object.
1359  *
1360  * This function is called to release the driver iocb object
1361  * to the iocb pool. The iotag in the iocb object
1362  * does not change for each use of the iocb object. This function
1363  * clears all other fields of the iocb object when it is freed.
1364  * The sqlq structure that holds the xritag and phys and virtual
1365  * mappings for the scatter gather list is retrieved from the
1366  * active array of sglq. The get of the sglq pointer also clears
1367  * the entry in the array. If the status of the IO indiactes that
1368  * this IO was aborted then the sglq entry it put on the
1369  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1370  * IO has good status or fails for any other reason then the sglq
1371  * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1372  *  asserted held in the code path calling this routine.
1373  **/
1374 static void
__lpfc_sli_release_iocbq_s4(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1375 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1376 {
1377 	struct lpfc_sglq *sglq;
1378 	unsigned long iflag = 0;
1379 	struct lpfc_sli_ring *pring;
1380 
1381 	if (iocbq->sli4_xritag == NO_XRI)
1382 		sglq = NULL;
1383 	else
1384 		sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1385 
1386 
1387 	if (sglq)  {
1388 		if (iocbq->cmd_flag & LPFC_IO_NVMET) {
1389 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1390 					  iflag);
1391 			sglq->state = SGL_FREED;
1392 			sglq->ndlp = NULL;
1393 			list_add_tail(&sglq->list,
1394 				      &phba->sli4_hba.lpfc_nvmet_sgl_list);
1395 			spin_unlock_irqrestore(
1396 				&phba->sli4_hba.sgl_list_lock, iflag);
1397 			goto out;
1398 		}
1399 
1400 		if ((iocbq->cmd_flag & LPFC_EXCHANGE_BUSY) &&
1401 		    (!(unlikely(pci_channel_offline(phba->pcidev)))) &&
1402 		    sglq->state != SGL_XRI_ABORTED) {
1403 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1404 					  iflag);
1405 
1406 			/* Check if we can get a reference on ndlp */
1407 			if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1408 				sglq->ndlp = NULL;
1409 
1410 			list_add(&sglq->list,
1411 				 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1412 			spin_unlock_irqrestore(
1413 				&phba->sli4_hba.sgl_list_lock, iflag);
1414 		} else {
1415 			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1416 					  iflag);
1417 			sglq->state = SGL_FREED;
1418 			sglq->ndlp = NULL;
1419 			list_add_tail(&sglq->list,
1420 				      &phba->sli4_hba.lpfc_els_sgl_list);
1421 			spin_unlock_irqrestore(
1422 				&phba->sli4_hba.sgl_list_lock, iflag);
1423 			pring = lpfc_phba_elsring(phba);
1424 			/* Check if TXQ queue needs to be serviced */
1425 			if (pring && (!list_empty(&pring->txq)))
1426 				lpfc_worker_wake_up(phba);
1427 		}
1428 	}
1429 
1430 out:
1431 	/*
1432 	 * Clean all volatile data fields, preserve iotag and node struct.
1433 	 */
1434 	memset_startat(iocbq, 0, wqe);
1435 	iocbq->sli4_lxritag = NO_XRI;
1436 	iocbq->sli4_xritag = NO_XRI;
1437 	iocbq->cmd_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |
1438 			      LPFC_IO_NVME_LS);
1439 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1440 }
1441 
1442 
1443 /**
1444  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1445  * @phba: Pointer to HBA context object.
1446  * @iocbq: Pointer to driver iocb object.
1447  *
1448  * This function is called to release the driver iocb object to the
1449  * iocb pool. The iotag in the iocb object does not change for each
1450  * use of the iocb object. This function clears all other fields of
1451  * the iocb object when it is freed. The hbalock is asserted held in
1452  * the code path calling this routine.
1453  **/
1454 static void
__lpfc_sli_release_iocbq_s3(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1455 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1456 {
1457 
1458 	/*
1459 	 * Clean all volatile data fields, preserve iotag and node struct.
1460 	 */
1461 	memset_startat(iocbq, 0, iocb);
1462 	iocbq->sli4_xritag = NO_XRI;
1463 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1464 }
1465 
1466 /**
1467  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1468  * @phba: Pointer to HBA context object.
1469  * @iocbq: Pointer to driver iocb object.
1470  *
1471  * This function is called with hbalock held to release driver
1472  * iocb object to the iocb pool. The iotag in the iocb object
1473  * does not change for each use of the iocb object. This function
1474  * clears all other fields of the iocb object when it is freed.
1475  **/
1476 static void
__lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1477 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1478 {
1479 	lockdep_assert_held(&phba->hbalock);
1480 
1481 	phba->__lpfc_sli_release_iocbq(phba, iocbq);
1482 	phba->iocb_cnt--;
1483 }
1484 
1485 /**
1486  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1487  * @phba: Pointer to HBA context object.
1488  * @iocbq: Pointer to driver iocb object.
1489  *
1490  * This function is called with no lock held to release the iocb to
1491  * iocb pool.
1492  **/
1493 void
lpfc_sli_release_iocbq(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)1494 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1495 {
1496 	unsigned long iflags;
1497 
1498 	/*
1499 	 * Clean all volatile data fields, preserve iotag and node struct.
1500 	 */
1501 	spin_lock_irqsave(&phba->hbalock, iflags);
1502 	__lpfc_sli_release_iocbq(phba, iocbq);
1503 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1504 }
1505 
1506 /**
1507  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1508  * @phba: Pointer to HBA context object.
1509  * @iocblist: List of IOCBs.
1510  * @ulpstatus: ULP status in IOCB command field.
1511  * @ulpWord4: ULP word-4 in IOCB command field.
1512  *
1513  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1514  * on the list by invoking the complete callback function associated with the
1515  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1516  * fields.
1517  **/
1518 void
lpfc_sli_cancel_iocbs(struct lpfc_hba * phba,struct list_head * iocblist,uint32_t ulpstatus,uint32_t ulpWord4)1519 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1520 		      uint32_t ulpstatus, uint32_t ulpWord4)
1521 {
1522 	struct lpfc_iocbq *piocb;
1523 
1524 	while (!list_empty(iocblist)) {
1525 		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1526 		if (piocb->cmd_cmpl) {
1527 			if (piocb->cmd_flag & LPFC_IO_NVME) {
1528 				lpfc_nvme_cancel_iocb(phba, piocb,
1529 						      ulpstatus, ulpWord4);
1530 			} else {
1531 				if (phba->sli_rev == LPFC_SLI_REV4) {
1532 					bf_set(lpfc_wcqe_c_status,
1533 					       &piocb->wcqe_cmpl, ulpstatus);
1534 					piocb->wcqe_cmpl.parameter = ulpWord4;
1535 				} else {
1536 					piocb->iocb.ulpStatus = ulpstatus;
1537 					piocb->iocb.un.ulpWord[4] = ulpWord4;
1538 				}
1539 				(piocb->cmd_cmpl) (phba, piocb, piocb);
1540 			}
1541 		} else {
1542 			lpfc_sli_release_iocbq(phba, piocb);
1543 		}
1544 	}
1545 	return;
1546 }
1547 
1548 /**
1549  * lpfc_sli_iocb_cmd_type - Get the iocb type
1550  * @iocb_cmnd: iocb command code.
1551  *
1552  * This function is called by ring event handler function to get the iocb type.
1553  * This function translates the iocb command to an iocb command type used to
1554  * decide the final disposition of each completed IOCB.
1555  * The function returns
1556  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1557  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1558  * LPFC_ABORT_IOCB   if it is an abort iocb
1559  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1560  *
1561  * The caller is not required to hold any lock.
1562  **/
1563 static lpfc_iocb_type
lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)1564 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1565 {
1566 	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1567 
1568 	if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1569 		return 0;
1570 
1571 	switch (iocb_cmnd) {
1572 	case CMD_XMIT_SEQUENCE_CR:
1573 	case CMD_XMIT_SEQUENCE_CX:
1574 	case CMD_XMIT_BCAST_CN:
1575 	case CMD_XMIT_BCAST_CX:
1576 	case CMD_ELS_REQUEST_CR:
1577 	case CMD_ELS_REQUEST_CX:
1578 	case CMD_CREATE_XRI_CR:
1579 	case CMD_CREATE_XRI_CX:
1580 	case CMD_GET_RPI_CN:
1581 	case CMD_XMIT_ELS_RSP_CX:
1582 	case CMD_GET_RPI_CR:
1583 	case CMD_FCP_IWRITE_CR:
1584 	case CMD_FCP_IWRITE_CX:
1585 	case CMD_FCP_IREAD_CR:
1586 	case CMD_FCP_IREAD_CX:
1587 	case CMD_FCP_ICMND_CR:
1588 	case CMD_FCP_ICMND_CX:
1589 	case CMD_FCP_TSEND_CX:
1590 	case CMD_FCP_TRSP_CX:
1591 	case CMD_FCP_TRECEIVE_CX:
1592 	case CMD_FCP_AUTO_TRSP_CX:
1593 	case CMD_ADAPTER_MSG:
1594 	case CMD_ADAPTER_DUMP:
1595 	case CMD_XMIT_SEQUENCE64_CR:
1596 	case CMD_XMIT_SEQUENCE64_CX:
1597 	case CMD_XMIT_BCAST64_CN:
1598 	case CMD_XMIT_BCAST64_CX:
1599 	case CMD_ELS_REQUEST64_CR:
1600 	case CMD_ELS_REQUEST64_CX:
1601 	case CMD_FCP_IWRITE64_CR:
1602 	case CMD_FCP_IWRITE64_CX:
1603 	case CMD_FCP_IREAD64_CR:
1604 	case CMD_FCP_IREAD64_CX:
1605 	case CMD_FCP_ICMND64_CR:
1606 	case CMD_FCP_ICMND64_CX:
1607 	case CMD_FCP_TSEND64_CX:
1608 	case CMD_FCP_TRSP64_CX:
1609 	case CMD_FCP_TRECEIVE64_CX:
1610 	case CMD_GEN_REQUEST64_CR:
1611 	case CMD_GEN_REQUEST64_CX:
1612 	case CMD_XMIT_ELS_RSP64_CX:
1613 	case DSSCMD_IWRITE64_CR:
1614 	case DSSCMD_IWRITE64_CX:
1615 	case DSSCMD_IREAD64_CR:
1616 	case DSSCMD_IREAD64_CX:
1617 	case CMD_SEND_FRAME:
1618 		type = LPFC_SOL_IOCB;
1619 		break;
1620 	case CMD_ABORT_XRI_CN:
1621 	case CMD_ABORT_XRI_CX:
1622 	case CMD_CLOSE_XRI_CN:
1623 	case CMD_CLOSE_XRI_CX:
1624 	case CMD_XRI_ABORTED_CX:
1625 	case CMD_ABORT_MXRI64_CN:
1626 	case CMD_XMIT_BLS_RSP64_CX:
1627 		type = LPFC_ABORT_IOCB;
1628 		break;
1629 	case CMD_RCV_SEQUENCE_CX:
1630 	case CMD_RCV_ELS_REQ_CX:
1631 	case CMD_RCV_SEQUENCE64_CX:
1632 	case CMD_RCV_ELS_REQ64_CX:
1633 	case CMD_ASYNC_STATUS:
1634 	case CMD_IOCB_RCV_SEQ64_CX:
1635 	case CMD_IOCB_RCV_ELS64_CX:
1636 	case CMD_IOCB_RCV_CONT64_CX:
1637 	case CMD_IOCB_RET_XRI64_CX:
1638 		type = LPFC_UNSOL_IOCB;
1639 		break;
1640 	case CMD_IOCB_XMIT_MSEQ64_CR:
1641 	case CMD_IOCB_XMIT_MSEQ64_CX:
1642 	case CMD_IOCB_RCV_SEQ_LIST64_CX:
1643 	case CMD_IOCB_RCV_ELS_LIST64_CX:
1644 	case CMD_IOCB_CLOSE_EXTENDED_CN:
1645 	case CMD_IOCB_ABORT_EXTENDED_CN:
1646 	case CMD_IOCB_RET_HBQE64_CN:
1647 	case CMD_IOCB_FCP_IBIDIR64_CR:
1648 	case CMD_IOCB_FCP_IBIDIR64_CX:
1649 	case CMD_IOCB_FCP_ITASKMGT64_CX:
1650 	case CMD_IOCB_LOGENTRY_CN:
1651 	case CMD_IOCB_LOGENTRY_ASYNC_CN:
1652 		printk("%s - Unhandled SLI-3 Command x%x\n",
1653 				__func__, iocb_cmnd);
1654 		type = LPFC_UNKNOWN_IOCB;
1655 		break;
1656 	default:
1657 		type = LPFC_UNKNOWN_IOCB;
1658 		break;
1659 	}
1660 
1661 	return type;
1662 }
1663 
1664 /**
1665  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1666  * @phba: Pointer to HBA context object.
1667  *
1668  * This function is called from SLI initialization code
1669  * to configure every ring of the HBA's SLI interface. The
1670  * caller is not required to hold any lock. This function issues
1671  * a config_ring mailbox command for each ring.
1672  * This function returns zero if successful else returns a negative
1673  * error code.
1674  **/
1675 static int
lpfc_sli_ring_map(struct lpfc_hba * phba)1676 lpfc_sli_ring_map(struct lpfc_hba *phba)
1677 {
1678 	struct lpfc_sli *psli = &phba->sli;
1679 	LPFC_MBOXQ_t *pmb;
1680 	MAILBOX_t *pmbox;
1681 	int i, rc, ret = 0;
1682 
1683 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1684 	if (!pmb)
1685 		return -ENOMEM;
1686 	pmbox = &pmb->u.mb;
1687 	phba->link_state = LPFC_INIT_MBX_CMDS;
1688 	for (i = 0; i < psli->num_rings; i++) {
1689 		lpfc_config_ring(phba, i, pmb);
1690 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1691 		if (rc != MBX_SUCCESS) {
1692 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1693 					"0446 Adapter failed to init (%d), "
1694 					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
1695 					"ring %d\n",
1696 					rc, pmbox->mbxCommand,
1697 					pmbox->mbxStatus, i);
1698 			phba->link_state = LPFC_HBA_ERROR;
1699 			ret = -ENXIO;
1700 			break;
1701 		}
1702 	}
1703 	mempool_free(pmb, phba->mbox_mem_pool);
1704 	return ret;
1705 }
1706 
1707 /**
1708  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1709  * @phba: Pointer to HBA context object.
1710  * @pring: Pointer to driver SLI ring object.
1711  * @piocb: Pointer to the driver iocb object.
1712  *
1713  * The driver calls this function with the hbalock held for SLI3 ports or
1714  * the ring lock held for SLI4 ports. The function adds the
1715  * new iocb to txcmplq of the given ring. This function always returns
1716  * 0. If this function is called for ELS ring, this function checks if
1717  * there is a vport associated with the ELS command. This function also
1718  * starts els_tmofunc timer if this is an ELS command.
1719  **/
1720 static int
lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)1721 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1722 			struct lpfc_iocbq *piocb)
1723 {
1724 	u32 ulp_command = 0;
1725 
1726 	BUG_ON(!piocb);
1727 	ulp_command = get_job_cmnd(phba, piocb);
1728 
1729 	list_add_tail(&piocb->list, &pring->txcmplq);
1730 	piocb->cmd_flag |= LPFC_IO_ON_TXCMPLQ;
1731 	pring->txcmplq_cnt++;
1732 	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1733 	   (ulp_command != CMD_ABORT_XRI_WQE) &&
1734 	   (ulp_command != CMD_ABORT_XRI_CN) &&
1735 	   (ulp_command != CMD_CLOSE_XRI_CN)) {
1736 		BUG_ON(!piocb->vport);
1737 		if (!test_bit(FC_UNLOADING, &piocb->vport->load_flag))
1738 			mod_timer(&piocb->vport->els_tmofunc,
1739 				  jiffies +
1740 				  msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1741 	}
1742 
1743 	return 0;
1744 }
1745 
1746 /**
1747  * lpfc_sli_ringtx_get - Get first element of the txq
1748  * @phba: Pointer to HBA context object.
1749  * @pring: Pointer to driver SLI ring object.
1750  *
1751  * This function is called with hbalock held to get next
1752  * iocb in txq of the given ring. If there is any iocb in
1753  * the txq, the function returns first iocb in the list after
1754  * removing the iocb from the list, else it returns NULL.
1755  **/
1756 struct lpfc_iocbq *
lpfc_sli_ringtx_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)1757 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1758 {
1759 	struct lpfc_iocbq *cmd_iocb;
1760 
1761 	lockdep_assert_held(&phba->hbalock);
1762 
1763 	list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1764 	return cmd_iocb;
1765 }
1766 
1767 /**
1768  * lpfc_cmf_sync_cmpl - Process a CMF_SYNC_WQE cmpl
1769  * @phba: Pointer to HBA context object.
1770  * @cmdiocb: Pointer to driver command iocb object.
1771  * @rspiocb: Pointer to driver response iocb object.
1772  *
1773  * This routine will inform the driver of any BW adjustments we need
1774  * to make. These changes will be picked up during the next CMF
1775  * timer interrupt. In addition, any BW changes will be logged
1776  * with LOG_CGN_MGMT.
1777  **/
1778 static void
lpfc_cmf_sync_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1779 lpfc_cmf_sync_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1780 		   struct lpfc_iocbq *rspiocb)
1781 {
1782 	union lpfc_wqe128 *wqe;
1783 	uint32_t status, info;
1784 	struct lpfc_wcqe_complete *wcqe = &rspiocb->wcqe_cmpl;
1785 	uint64_t bw, bwdif, slop;
1786 	uint64_t pcent, bwpcent;
1787 	int asig, afpin, sigcnt, fpincnt;
1788 	int wsigmax, wfpinmax, cg, tdp;
1789 	char *s;
1790 
1791 	/* First check for error */
1792 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1793 	if (status) {
1794 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1795 				"6211 CMF_SYNC_WQE Error "
1796 				"req_tag x%x status x%x hwstatus x%x "
1797 				"tdatap x%x parm x%x\n",
1798 				bf_get(lpfc_wcqe_c_request_tag, wcqe),
1799 				bf_get(lpfc_wcqe_c_status, wcqe),
1800 				bf_get(lpfc_wcqe_c_hw_status, wcqe),
1801 				wcqe->total_data_placed,
1802 				wcqe->parameter);
1803 		goto out;
1804 	}
1805 
1806 	/* Gather congestion information on a successful cmpl */
1807 	info = wcqe->parameter;
1808 	phba->cmf_active_info = info;
1809 
1810 	/* See if firmware info count is valid or has changed */
1811 	if (info > LPFC_MAX_CMF_INFO || phba->cmf_info_per_interval == info)
1812 		info = 0;
1813 	else
1814 		phba->cmf_info_per_interval = info;
1815 
1816 	tdp = bf_get(lpfc_wcqe_c_cmf_bw, wcqe);
1817 	cg = bf_get(lpfc_wcqe_c_cmf_cg, wcqe);
1818 
1819 	/* Get BW requirement from firmware */
1820 	bw = (uint64_t)tdp * LPFC_CMF_BLK_SIZE;
1821 	if (!bw) {
1822 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1823 				"6212 CMF_SYNC_WQE x%x: NULL bw\n",
1824 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
1825 		goto out;
1826 	}
1827 
1828 	/* Gather information needed for logging if a BW change is required */
1829 	wqe = &cmdiocb->wqe;
1830 	asig = bf_get(cmf_sync_asig, &wqe->cmf_sync);
1831 	afpin = bf_get(cmf_sync_afpin, &wqe->cmf_sync);
1832 	fpincnt = bf_get(cmf_sync_wfpincnt, &wqe->cmf_sync);
1833 	sigcnt = bf_get(cmf_sync_wsigcnt, &wqe->cmf_sync);
1834 	if (phba->cmf_max_bytes_per_interval != bw ||
1835 	    (asig || afpin || sigcnt || fpincnt)) {
1836 		/* Are we increasing or decreasing BW */
1837 		if (phba->cmf_max_bytes_per_interval <  bw) {
1838 			bwdif = bw - phba->cmf_max_bytes_per_interval;
1839 			s = "Increase";
1840 		} else {
1841 			bwdif = phba->cmf_max_bytes_per_interval - bw;
1842 			s = "Decrease";
1843 		}
1844 
1845 		/* What is the change percentage */
1846 		slop = div_u64(phba->cmf_link_byte_count, 200); /*For rounding*/
1847 		pcent = div64_u64(bwdif * 100 + slop,
1848 				  phba->cmf_link_byte_count);
1849 		bwpcent = div64_u64(bw * 100 + slop,
1850 				    phba->cmf_link_byte_count);
1851 		/* Because of bytes adjustment due to shorter timer in
1852 		 * lpfc_cmf_timer() the cmf_link_byte_count can be shorter and
1853 		 * may seem like BW is above 100%.
1854 		 */
1855 		if (bwpcent > 100)
1856 			bwpcent = 100;
1857 
1858 		if (phba->cmf_max_bytes_per_interval < bw &&
1859 		    bwpcent > 95)
1860 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1861 					"6208 Congestion bandwidth "
1862 					"limits removed\n");
1863 		else if ((phba->cmf_max_bytes_per_interval > bw) &&
1864 			 ((bwpcent + pcent) <= 100) && ((bwpcent + pcent) > 95))
1865 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1866 					"6209 Congestion bandwidth "
1867 					"limits in effect\n");
1868 
1869 		if (asig) {
1870 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1871 					"6237 BW Threshold %lld%% (%lld): "
1872 					"%lld%% %s: Signal Alarm: cg:%d "
1873 					"Info:%u\n",
1874 					bwpcent, bw, pcent, s, cg,
1875 					phba->cmf_active_info);
1876 		} else if (afpin) {
1877 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1878 					"6238 BW Threshold %lld%% (%lld): "
1879 					"%lld%% %s: FPIN Alarm: cg:%d "
1880 					"Info:%u\n",
1881 					bwpcent, bw, pcent, s, cg,
1882 					phba->cmf_active_info);
1883 		} else if (sigcnt) {
1884 			wsigmax = bf_get(cmf_sync_wsigmax, &wqe->cmf_sync);
1885 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1886 					"6239 BW Threshold %lld%% (%lld): "
1887 					"%lld%% %s: Signal Warning: "
1888 					"Cnt %d Max %d: cg:%d Info:%u\n",
1889 					bwpcent, bw, pcent, s, sigcnt,
1890 					wsigmax, cg, phba->cmf_active_info);
1891 		} else if (fpincnt) {
1892 			wfpinmax = bf_get(cmf_sync_wfpinmax, &wqe->cmf_sync);
1893 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1894 					"6240 BW Threshold %lld%% (%lld): "
1895 					"%lld%% %s: FPIN Warning: "
1896 					"Cnt %d Max %d: cg:%d Info:%u\n",
1897 					bwpcent, bw, pcent, s, fpincnt,
1898 					wfpinmax, cg, phba->cmf_active_info);
1899 		} else {
1900 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1901 					"6241 BW Threshold %lld%% (%lld): "
1902 					"CMF %lld%% %s: cg:%d Info:%u\n",
1903 					bwpcent, bw, pcent, s, cg,
1904 					phba->cmf_active_info);
1905 		}
1906 	} else if (info) {
1907 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1908 				"6246 Info Threshold %u\n", info);
1909 	}
1910 
1911 	/* Save BW change to be picked up during next timer interrupt */
1912 	phba->cmf_last_sync_bw = bw;
1913 out:
1914 	lpfc_sli_release_iocbq(phba, cmdiocb);
1915 }
1916 
1917 /**
1918  * lpfc_issue_cmf_sync_wqe - Issue a CMF_SYNC_WQE
1919  * @phba: Pointer to HBA context object.
1920  * @ms:   ms to set in WQE interval, 0 means use init op
1921  * @total: Total rcv bytes for this interval
1922  *
1923  * This routine is called every CMF timer interrupt. Its purpose is
1924  * to issue a CMF_SYNC_WQE to the firmware to inform it of any events
1925  * that may indicate we have congestion (FPINs or Signals). Upon
1926  * completion, the firmware will indicate any BW restrictions the
1927  * driver may need to take.
1928  **/
1929 int
lpfc_issue_cmf_sync_wqe(struct lpfc_hba * phba,u32 ms,u64 total)1930 lpfc_issue_cmf_sync_wqe(struct lpfc_hba *phba, u32 ms, u64 total)
1931 {
1932 	union lpfc_wqe128 *wqe;
1933 	struct lpfc_iocbq *sync_buf;
1934 	unsigned long iflags;
1935 	u32 ret_val, cgn_sig_freq;
1936 	u32 atot, wtot, max;
1937 	u8 warn_sync_period = 0;
1938 
1939 	/* First address any alarm / warning activity */
1940 	atot = atomic_xchg(&phba->cgn_sync_alarm_cnt, 0);
1941 	wtot = atomic_xchg(&phba->cgn_sync_warn_cnt, 0);
1942 
1943 	spin_lock_irqsave(&phba->hbalock, iflags);
1944 
1945 	/* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
1946 	if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
1947 	    phba->link_state < LPFC_LINK_UP) {
1948 		ret_val = 0;
1949 		goto out_unlock;
1950 	}
1951 
1952 	sync_buf = __lpfc_sli_get_iocbq(phba);
1953 	if (!sync_buf) {
1954 		lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
1955 				"6244 No available WQEs for CMF_SYNC_WQE\n");
1956 		ret_val = ENOMEM;
1957 		goto out_unlock;
1958 	}
1959 
1960 	wqe = &sync_buf->wqe;
1961 
1962 	/* WQEs are reused.  Clear stale data and set key fields to zero */
1963 	memset(wqe, 0, sizeof(*wqe));
1964 
1965 	/* If this is the very first CMF_SYNC_WQE, issue an init operation */
1966 	if (!ms) {
1967 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1968 				"6441 CMF Init %d - CMF_SYNC_WQE\n",
1969 				phba->fc_eventTag);
1970 		bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
1971 		bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
1972 		goto initpath;
1973 	}
1974 
1975 	bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
1976 	bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
1977 
1978 	/* Check for alarms / warnings */
1979 	if (atot) {
1980 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1981 			/* We hit an Signal alarm condition */
1982 			bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
1983 		} else {
1984 			/* We hit a FPIN alarm condition */
1985 			bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
1986 		}
1987 	} else if (wtot) {
1988 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
1989 		    phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1990 			cgn_sig_freq = phba->cgn_sig_freq ? phba->cgn_sig_freq :
1991 					lpfc_fabric_cgn_frequency;
1992 			/* We hit an Signal warning condition */
1993 			max = LPFC_SEC_TO_MSEC / cgn_sig_freq *
1994 				lpfc_acqe_cgn_frequency;
1995 			bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
1996 			bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
1997 			warn_sync_period = lpfc_acqe_cgn_frequency;
1998 		} else {
1999 			/* We hit a FPIN warning condition */
2000 			bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
2001 			bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
2002 			if (phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ)
2003 				warn_sync_period =
2004 				LPFC_MSECS_TO_SECS(phba->cgn_fpin_frequency);
2005 		}
2006 	}
2007 
2008 	/* Update total read blocks during previous timer interval */
2009 	wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
2010 
2011 initpath:
2012 	bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
2013 	wqe->cmf_sync.event_tag = phba->fc_eventTag;
2014 	bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
2015 
2016 	/* Setup reqtag to match the wqe completion. */
2017 	bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
2018 
2019 	bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
2020 	bf_set(cmf_sync_period, &wqe->cmf_sync, warn_sync_period);
2021 
2022 	bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
2023 	bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
2024 	bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
2025 
2026 	sync_buf->vport = phba->pport;
2027 	sync_buf->cmd_cmpl = lpfc_cmf_sync_cmpl;
2028 	sync_buf->cmd_dmabuf = NULL;
2029 	sync_buf->rsp_dmabuf = NULL;
2030 	sync_buf->bpl_dmabuf = NULL;
2031 	sync_buf->sli4_xritag = NO_XRI;
2032 
2033 	sync_buf->cmd_flag |= LPFC_IO_CMF;
2034 	ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
2035 	if (ret_val) {
2036 		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2037 				"6214 Cannot issue CMF_SYNC_WQE: x%x\n",
2038 				ret_val);
2039 		__lpfc_sli_release_iocbq(phba, sync_buf);
2040 	}
2041 out_unlock:
2042 	spin_unlock_irqrestore(&phba->hbalock, iflags);
2043 	return ret_val;
2044 }
2045 
2046 /**
2047  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
2048  * @phba: Pointer to HBA context object.
2049  * @pring: Pointer to driver SLI ring object.
2050  *
2051  * This function is called with hbalock held and the caller must post the
2052  * iocb without releasing the lock. If the caller releases the lock,
2053  * iocb slot returned by the function is not guaranteed to be available.
2054  * The function returns pointer to the next available iocb slot if there
2055  * is available slot in the ring, else it returns NULL.
2056  * If the get index of the ring is ahead of the put index, the function
2057  * will post an error attention event to the worker thread to take the
2058  * HBA to offline state.
2059  **/
2060 static IOCB_t *
lpfc_sli_next_iocb_slot(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2061 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2062 {
2063 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2064 	uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
2065 
2066 	lockdep_assert_held(&phba->hbalock);
2067 
2068 	if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
2069 	   (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
2070 		pring->sli.sli3.next_cmdidx = 0;
2071 
2072 	if (unlikely(pring->sli.sli3.local_getidx ==
2073 		pring->sli.sli3.next_cmdidx)) {
2074 
2075 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
2076 
2077 		if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
2078 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2079 					"0315 Ring %d issue: portCmdGet %d "
2080 					"is bigger than cmd ring %d\n",
2081 					pring->ringno,
2082 					pring->sli.sli3.local_getidx,
2083 					max_cmd_idx);
2084 
2085 			phba->link_state = LPFC_HBA_ERROR;
2086 			/*
2087 			 * All error attention handlers are posted to
2088 			 * worker thread
2089 			 */
2090 			phba->work_ha |= HA_ERATT;
2091 			phba->work_hs = HS_FFER3;
2092 
2093 			lpfc_worker_wake_up(phba);
2094 
2095 			return NULL;
2096 		}
2097 
2098 		if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
2099 			return NULL;
2100 	}
2101 
2102 	return lpfc_cmd_iocb(phba, pring);
2103 }
2104 
2105 /**
2106  * lpfc_sli_next_iotag - Get an iotag for the iocb
2107  * @phba: Pointer to HBA context object.
2108  * @iocbq: Pointer to driver iocb object.
2109  *
2110  * This function gets an iotag for the iocb. If there is no unused iotag and
2111  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
2112  * array and assigns a new iotag.
2113  * The function returns the allocated iotag if successful, else returns zero.
2114  * Zero is not a valid iotag.
2115  * The caller is not required to hold any lock.
2116  **/
2117 uint16_t
lpfc_sli_next_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)2118 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
2119 {
2120 	struct lpfc_iocbq **new_arr;
2121 	struct lpfc_iocbq **old_arr;
2122 	size_t new_len;
2123 	struct lpfc_sli *psli = &phba->sli;
2124 	uint16_t iotag;
2125 
2126 	spin_lock_irq(&phba->hbalock);
2127 	iotag = psli->last_iotag;
2128 	if(++iotag < psli->iocbq_lookup_len) {
2129 		psli->last_iotag = iotag;
2130 		psli->iocbq_lookup[iotag] = iocbq;
2131 		spin_unlock_irq(&phba->hbalock);
2132 		iocbq->iotag = iotag;
2133 		return iotag;
2134 	} else if (psli->iocbq_lookup_len < (0xffff
2135 					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
2136 		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2137 		spin_unlock_irq(&phba->hbalock);
2138 		new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
2139 				  GFP_KERNEL);
2140 		if (new_arr) {
2141 			spin_lock_irq(&phba->hbalock);
2142 			old_arr = psli->iocbq_lookup;
2143 			if (new_len <= psli->iocbq_lookup_len) {
2144 				/* highly unprobable case */
2145 				kfree(new_arr);
2146 				iotag = psli->last_iotag;
2147 				if(++iotag < psli->iocbq_lookup_len) {
2148 					psli->last_iotag = iotag;
2149 					psli->iocbq_lookup[iotag] = iocbq;
2150 					spin_unlock_irq(&phba->hbalock);
2151 					iocbq->iotag = iotag;
2152 					return iotag;
2153 				}
2154 				spin_unlock_irq(&phba->hbalock);
2155 				return 0;
2156 			}
2157 			if (psli->iocbq_lookup)
2158 				memcpy(new_arr, old_arr,
2159 				       ((psli->last_iotag  + 1) *
2160 					sizeof (struct lpfc_iocbq *)));
2161 			psli->iocbq_lookup = new_arr;
2162 			psli->iocbq_lookup_len = new_len;
2163 			psli->last_iotag = iotag;
2164 			psli->iocbq_lookup[iotag] = iocbq;
2165 			spin_unlock_irq(&phba->hbalock);
2166 			iocbq->iotag = iotag;
2167 			kfree(old_arr);
2168 			return iotag;
2169 		}
2170 	} else
2171 		spin_unlock_irq(&phba->hbalock);
2172 
2173 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2174 			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",
2175 			psli->last_iotag);
2176 
2177 	return 0;
2178 }
2179 
2180 /**
2181  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
2182  * @phba: Pointer to HBA context object.
2183  * @pring: Pointer to driver SLI ring object.
2184  * @iocb: Pointer to iocb slot in the ring.
2185  * @nextiocb: Pointer to driver iocb object which need to be
2186  *            posted to firmware.
2187  *
2188  * This function is called to post a new iocb to the firmware. This
2189  * function copies the new iocb to ring iocb slot and updates the
2190  * ring pointers. It adds the new iocb to txcmplq if there is
2191  * a completion call back for this iocb else the function will free the
2192  * iocb object.  The hbalock is asserted held in the code path calling
2193  * this routine.
2194  **/
2195 static void
lpfc_sli_submit_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,IOCB_t * iocb,struct lpfc_iocbq * nextiocb)2196 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2197 		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
2198 {
2199 	/*
2200 	 * Set up an iotag
2201 	 */
2202 	nextiocb->iocb.ulpIoTag = (nextiocb->cmd_cmpl) ? nextiocb->iotag : 0;
2203 
2204 
2205 	if (pring->ringno == LPFC_ELS_RING) {
2206 		lpfc_debugfs_slow_ring_trc(phba,
2207 			"IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2208 			*(((uint32_t *) &nextiocb->iocb) + 4),
2209 			*(((uint32_t *) &nextiocb->iocb) + 6),
2210 			*(((uint32_t *) &nextiocb->iocb) + 7));
2211 	}
2212 
2213 	/*
2214 	 * Issue iocb command to adapter
2215 	 */
2216 	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
2217 	wmb();
2218 	pring->stats.iocb_cmd++;
2219 
2220 	/*
2221 	 * If there is no completion routine to call, we can release the
2222 	 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
2223 	 * that have no rsp ring completion, cmd_cmpl MUST be NULL.
2224 	 */
2225 	if (nextiocb->cmd_cmpl)
2226 		lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
2227 	else
2228 		__lpfc_sli_release_iocbq(phba, nextiocb);
2229 
2230 	/*
2231 	 * Let the HBA know what IOCB slot will be the next one the
2232 	 * driver will put a command into.
2233 	 */
2234 	pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
2235 	writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
2236 }
2237 
2238 /**
2239  * lpfc_sli_update_full_ring - Update the chip attention register
2240  * @phba: Pointer to HBA context object.
2241  * @pring: Pointer to driver SLI ring object.
2242  *
2243  * The caller is not required to hold any lock for calling this function.
2244  * This function updates the chip attention bits for the ring to inform firmware
2245  * that there are pending work to be done for this ring and requests an
2246  * interrupt when there is space available in the ring. This function is
2247  * called when the driver is unable to post more iocbs to the ring due
2248  * to unavailability of space in the ring.
2249  **/
2250 static void
lpfc_sli_update_full_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2251 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2252 {
2253 	int ringno = pring->ringno;
2254 
2255 	pring->flag |= LPFC_CALL_RING_AVAILABLE;
2256 
2257 	wmb();
2258 
2259 	/*
2260 	 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
2261 	 * The HBA will tell us when an IOCB entry is available.
2262 	 */
2263 	writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
2264 	readl(phba->CAregaddr); /* flush */
2265 
2266 	pring->stats.iocb_cmd_full++;
2267 }
2268 
2269 /**
2270  * lpfc_sli_update_ring - Update chip attention register
2271  * @phba: Pointer to HBA context object.
2272  * @pring: Pointer to driver SLI ring object.
2273  *
2274  * This function updates the chip attention register bit for the
2275  * given ring to inform HBA that there is more work to be done
2276  * in this ring. The caller is not required to hold any lock.
2277  **/
2278 static void
lpfc_sli_update_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2279 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2280 {
2281 	int ringno = pring->ringno;
2282 
2283 	/*
2284 	 * Tell the HBA that there is work to do in this ring.
2285 	 */
2286 	if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2287 		wmb();
2288 		writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2289 		readl(phba->CAregaddr); /* flush */
2290 	}
2291 }
2292 
2293 /**
2294  * lpfc_sli_resume_iocb - Process iocbs in the txq
2295  * @phba: Pointer to HBA context object.
2296  * @pring: Pointer to driver SLI ring object.
2297  *
2298  * This function is called with hbalock held to post pending iocbs
2299  * in the txq to the firmware. This function is called when driver
2300  * detects space available in the ring.
2301  **/
2302 static void
lpfc_sli_resume_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)2303 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2304 {
2305 	IOCB_t *iocb;
2306 	struct lpfc_iocbq *nextiocb;
2307 
2308 	lockdep_assert_held(&phba->hbalock);
2309 
2310 	/*
2311 	 * Check to see if:
2312 	 *  (a) there is anything on the txq to send
2313 	 *  (b) link is up
2314 	 *  (c) link attention events can be processed (fcp ring only)
2315 	 *  (d) IOCB processing is not blocked by the outstanding mbox command.
2316 	 */
2317 
2318 	if (lpfc_is_link_up(phba) &&
2319 	    (!list_empty(&pring->txq)) &&
2320 	    (pring->ringno != LPFC_FCP_RING ||
2321 	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {
2322 
2323 		while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2324 		       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2325 			lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2326 
2327 		if (iocb)
2328 			lpfc_sli_update_ring(phba, pring);
2329 		else
2330 			lpfc_sli_update_full_ring(phba, pring);
2331 	}
2332 
2333 	return;
2334 }
2335 
2336 /**
2337  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
2338  * @phba: Pointer to HBA context object.
2339  * @hbqno: HBQ number.
2340  *
2341  * This function is called with hbalock held to get the next
2342  * available slot for the given HBQ. If there is free slot
2343  * available for the HBQ it will return pointer to the next available
2344  * HBQ entry else it will return NULL.
2345  **/
2346 static struct lpfc_hbq_entry *
lpfc_sli_next_hbq_slot(struct lpfc_hba * phba,uint32_t hbqno)2347 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2348 {
2349 	struct hbq_s *hbqp = &phba->hbqs[hbqno];
2350 
2351 	lockdep_assert_held(&phba->hbalock);
2352 
2353 	if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2354 	    ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2355 		hbqp->next_hbqPutIdx = 0;
2356 
2357 	if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
2358 		uint32_t raw_index = phba->hbq_get[hbqno];
2359 		uint32_t getidx = le32_to_cpu(raw_index);
2360 
2361 		hbqp->local_hbqGetIdx = getidx;
2362 
2363 		if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
2364 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2365 					"1802 HBQ %d: local_hbqGetIdx "
2366 					"%u is > than hbqp->entry_count %u\n",
2367 					hbqno, hbqp->local_hbqGetIdx,
2368 					hbqp->entry_count);
2369 
2370 			phba->link_state = LPFC_HBA_ERROR;
2371 			return NULL;
2372 		}
2373 
2374 		if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2375 			return NULL;
2376 	}
2377 
2378 	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2379 			hbqp->hbqPutIdx;
2380 }
2381 
2382 /**
2383  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
2384  * @phba: Pointer to HBA context object.
2385  *
2386  * This function is called with no lock held to free all the
2387  * hbq buffers while uninitializing the SLI interface. It also
2388  * frees the HBQ buffers returned by the firmware but not yet
2389  * processed by the upper layers.
2390  **/
2391 void
lpfc_sli_hbqbuf_free_all(struct lpfc_hba * phba)2392 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2393 {
2394 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2395 	struct hbq_dmabuf *hbq_buf;
2396 	unsigned long flags;
2397 	int i, hbq_count;
2398 
2399 	hbq_count = lpfc_sli_hbq_count();
2400 	/* Return all memory used by all HBQs */
2401 	spin_lock_irqsave(&phba->hbalock, flags);
2402 	for (i = 0; i < hbq_count; ++i) {
2403 		list_for_each_entry_safe(dmabuf, next_dmabuf,
2404 				&phba->hbqs[i].hbq_buffer_list, list) {
2405 			hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2406 			list_del(&hbq_buf->dbuf.list);
2407 			(phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2408 		}
2409 		phba->hbqs[i].buffer_count = 0;
2410 	}
2411 
2412 	/* Mark the HBQs not in use */
2413 	phba->hbq_in_use = 0;
2414 	spin_unlock_irqrestore(&phba->hbalock, flags);
2415 }
2416 
2417 /**
2418  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2419  * @phba: Pointer to HBA context object.
2420  * @hbqno: HBQ number.
2421  * @hbq_buf: Pointer to HBQ buffer.
2422  *
2423  * This function is called with the hbalock held to post a
2424  * hbq buffer to the firmware. If the function finds an empty
2425  * slot in the HBQ, it will post the buffer. The function will return
2426  * pointer to the hbq entry if it successfully post the buffer
2427  * else it will return NULL.
2428  **/
2429 static int
lpfc_sli_hbq_to_firmware(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2430 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2431 			 struct hbq_dmabuf *hbq_buf)
2432 {
2433 	lockdep_assert_held(&phba->hbalock);
2434 	return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2435 }
2436 
2437 /**
2438  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2439  * @phba: Pointer to HBA context object.
2440  * @hbqno: HBQ number.
2441  * @hbq_buf: Pointer to HBQ buffer.
2442  *
2443  * This function is called with the hbalock held to post a hbq buffer to the
2444  * firmware. If the function finds an empty slot in the HBQ, it will post the
2445  * buffer and place it on the hbq_buffer_list. The function will return zero if
2446  * it successfully post the buffer else it will return an error.
2447  **/
2448 static int
lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2449 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2450 			    struct hbq_dmabuf *hbq_buf)
2451 {
2452 	struct lpfc_hbq_entry *hbqe;
2453 	dma_addr_t physaddr = hbq_buf->dbuf.phys;
2454 
2455 	lockdep_assert_held(&phba->hbalock);
2456 	/* Get next HBQ entry slot to use */
2457 	hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2458 	if (hbqe) {
2459 		struct hbq_s *hbqp = &phba->hbqs[hbqno];
2460 
2461 		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2462 		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2463 		hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2464 		hbqe->bde.tus.f.bdeFlags = 0;
2465 		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2466 		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2467 				/* Sync SLIM */
2468 		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2469 		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2470 				/* flush */
2471 		readl(phba->hbq_put + hbqno);
2472 		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2473 		return 0;
2474 	} else
2475 		return -ENOMEM;
2476 }
2477 
2478 /**
2479  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2480  * @phba: Pointer to HBA context object.
2481  * @hbqno: HBQ number.
2482  * @hbq_buf: Pointer to HBQ buffer.
2483  *
2484  * This function is called with the hbalock held to post an RQE to the SLI4
2485  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2486  * the hbq_buffer_list and return zero, otherwise it will return an error.
2487  **/
2488 static int
lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba * phba,uint32_t hbqno,struct hbq_dmabuf * hbq_buf)2489 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2490 			    struct hbq_dmabuf *hbq_buf)
2491 {
2492 	int rc;
2493 	struct lpfc_rqe hrqe;
2494 	struct lpfc_rqe drqe;
2495 	struct lpfc_queue *hrq;
2496 	struct lpfc_queue *drq;
2497 
2498 	if (hbqno != LPFC_ELS_HBQ)
2499 		return 1;
2500 	hrq = phba->sli4_hba.hdr_rq;
2501 	drq = phba->sli4_hba.dat_rq;
2502 
2503 	lockdep_assert_held(&phba->hbalock);
2504 	hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2505 	hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2506 	drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2507 	drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2508 	rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2509 	if (rc < 0)
2510 		return rc;
2511 	hbq_buf->tag = (rc | (hbqno << 16));
2512 	list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2513 	return 0;
2514 }
2515 
2516 /* HBQ for ELS and CT traffic. */
2517 static struct lpfc_hbq_init lpfc_els_hbq = {
2518 	.rn = 1,
2519 	.entry_count = 256,
2520 	.mask_count = 0,
2521 	.profile = 0,
2522 	.ring_mask = (1 << LPFC_ELS_RING),
2523 	.buffer_count = 0,
2524 	.init_count = 40,
2525 	.add_count = 40,
2526 };
2527 
2528 /* Array of HBQs */
2529 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2530 	&lpfc_els_hbq,
2531 };
2532 
2533 /**
2534  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2535  * @phba: Pointer to HBA context object.
2536  * @hbqno: HBQ number.
2537  * @count: Number of HBQ buffers to be posted.
2538  *
2539  * This function is called with no lock held to post more hbq buffers to the
2540  * given HBQ. The function returns the number of HBQ buffers successfully
2541  * posted.
2542  **/
2543 static int
lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba * phba,uint32_t hbqno,uint32_t count)2544 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2545 {
2546 	uint32_t i, posted = 0;
2547 	unsigned long flags;
2548 	struct hbq_dmabuf *hbq_buffer;
2549 	LIST_HEAD(hbq_buf_list);
2550 	if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2551 		return 0;
2552 
2553 	if ((phba->hbqs[hbqno].buffer_count + count) >
2554 	    lpfc_hbq_defs[hbqno]->entry_count)
2555 		count = lpfc_hbq_defs[hbqno]->entry_count -
2556 					phba->hbqs[hbqno].buffer_count;
2557 	if (!count)
2558 		return 0;
2559 	/* Allocate HBQ entries */
2560 	for (i = 0; i < count; i++) {
2561 		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2562 		if (!hbq_buffer)
2563 			break;
2564 		list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2565 	}
2566 	/* Check whether HBQ is still in use */
2567 	spin_lock_irqsave(&phba->hbalock, flags);
2568 	if (!phba->hbq_in_use)
2569 		goto err;
2570 	while (!list_empty(&hbq_buf_list)) {
2571 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2572 				 dbuf.list);
2573 		hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2574 				      (hbqno << 16));
2575 		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2576 			phba->hbqs[hbqno].buffer_count++;
2577 			posted++;
2578 		} else
2579 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2580 	}
2581 	spin_unlock_irqrestore(&phba->hbalock, flags);
2582 	return posted;
2583 err:
2584 	spin_unlock_irqrestore(&phba->hbalock, flags);
2585 	while (!list_empty(&hbq_buf_list)) {
2586 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2587 				 dbuf.list);
2588 		(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2589 	}
2590 	return 0;
2591 }
2592 
2593 /**
2594  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2595  * @phba: Pointer to HBA context object.
2596  * @qno: HBQ number.
2597  *
2598  * This function posts more buffers to the HBQ. This function
2599  * is called with no lock held. The function returns the number of HBQ entries
2600  * successfully allocated.
2601  **/
2602 int
lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba * phba,uint32_t qno)2603 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2604 {
2605 	if (phba->sli_rev == LPFC_SLI_REV4)
2606 		return 0;
2607 	else
2608 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2609 					 lpfc_hbq_defs[qno]->add_count);
2610 }
2611 
2612 /**
2613  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2614  * @phba: Pointer to HBA context object.
2615  * @qno:  HBQ queue number.
2616  *
2617  * This function is called from SLI initialization code path with
2618  * no lock held to post initial HBQ buffers to firmware. The
2619  * function returns the number of HBQ entries successfully allocated.
2620  **/
2621 static int
lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba * phba,uint32_t qno)2622 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2623 {
2624 	if (phba->sli_rev == LPFC_SLI_REV4)
2625 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2626 					lpfc_hbq_defs[qno]->entry_count);
2627 	else
2628 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2629 					 lpfc_hbq_defs[qno]->init_count);
2630 }
2631 
2632 /*
2633  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2634  *
2635  * This function removes the first hbq buffer on an hbq list and returns a
2636  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2637  **/
2638 static struct hbq_dmabuf *
lpfc_sli_hbqbuf_get(struct list_head * rb_list)2639 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2640 {
2641 	struct lpfc_dmabuf *d_buf;
2642 
2643 	list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2644 	if (!d_buf)
2645 		return NULL;
2646 	return container_of(d_buf, struct hbq_dmabuf, dbuf);
2647 }
2648 
2649 /**
2650  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2651  * @phba: Pointer to HBA context object.
2652  * @hrq: HBQ number.
2653  *
2654  * This function removes the first RQ buffer on an RQ buffer list and returns a
2655  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2656  **/
2657 static struct rqb_dmabuf *
lpfc_sli_rqbuf_get(struct lpfc_hba * phba,struct lpfc_queue * hrq)2658 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2659 {
2660 	struct lpfc_dmabuf *h_buf;
2661 	struct lpfc_rqb *rqbp;
2662 
2663 	rqbp = hrq->rqbp;
2664 	list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2665 			 struct lpfc_dmabuf, list);
2666 	if (!h_buf)
2667 		return NULL;
2668 	rqbp->buffer_count--;
2669 	return container_of(h_buf, struct rqb_dmabuf, hbuf);
2670 }
2671 
2672 /**
2673  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2674  * @phba: Pointer to HBA context object.
2675  * @tag: Tag of the hbq buffer.
2676  *
2677  * This function searches for the hbq buffer associated with the given tag in
2678  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2679  * otherwise it returns NULL.
2680  **/
2681 static struct hbq_dmabuf *
lpfc_sli_hbqbuf_find(struct lpfc_hba * phba,uint32_t tag)2682 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2683 {
2684 	struct lpfc_dmabuf *d_buf;
2685 	struct hbq_dmabuf *hbq_buf;
2686 	uint32_t hbqno;
2687 
2688 	hbqno = tag >> 16;
2689 	if (hbqno >= LPFC_MAX_HBQS)
2690 		return NULL;
2691 
2692 	spin_lock_irq(&phba->hbalock);
2693 	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2694 		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2695 		if (hbq_buf->tag == tag) {
2696 			spin_unlock_irq(&phba->hbalock);
2697 			return hbq_buf;
2698 		}
2699 	}
2700 	spin_unlock_irq(&phba->hbalock);
2701 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2702 			"1803 Bad hbq tag. Data: x%x x%x\n",
2703 			tag, phba->hbqs[tag >> 16].buffer_count);
2704 	return NULL;
2705 }
2706 
2707 /**
2708  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2709  * @phba: Pointer to HBA context object.
2710  * @hbq_buffer: Pointer to HBQ buffer.
2711  *
2712  * This function is called with hbalock. This function gives back
2713  * the hbq buffer to firmware. If the HBQ does not have space to
2714  * post the buffer, it will free the buffer.
2715  **/
2716 void
lpfc_sli_free_hbq(struct lpfc_hba * phba,struct hbq_dmabuf * hbq_buffer)2717 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2718 {
2719 	uint32_t hbqno;
2720 
2721 	if (hbq_buffer) {
2722 		hbqno = hbq_buffer->tag >> 16;
2723 		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2724 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2725 	}
2726 }
2727 
2728 /**
2729  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2730  * @mbxCommand: mailbox command code.
2731  *
2732  * This function is called by the mailbox event handler function to verify
2733  * that the completed mailbox command is a legitimate mailbox command. If the
2734  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2735  * and the mailbox event handler will take the HBA offline.
2736  **/
2737 static int
lpfc_sli_chk_mbx_command(uint8_t mbxCommand)2738 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2739 {
2740 	uint8_t ret;
2741 
2742 	switch (mbxCommand) {
2743 	case MBX_LOAD_SM:
2744 	case MBX_READ_NV:
2745 	case MBX_WRITE_NV:
2746 	case MBX_WRITE_VPARMS:
2747 	case MBX_RUN_BIU_DIAG:
2748 	case MBX_INIT_LINK:
2749 	case MBX_DOWN_LINK:
2750 	case MBX_CONFIG_LINK:
2751 	case MBX_CONFIG_RING:
2752 	case MBX_RESET_RING:
2753 	case MBX_READ_CONFIG:
2754 	case MBX_READ_RCONFIG:
2755 	case MBX_READ_SPARM:
2756 	case MBX_READ_STATUS:
2757 	case MBX_READ_RPI:
2758 	case MBX_READ_XRI:
2759 	case MBX_READ_REV:
2760 	case MBX_READ_LNK_STAT:
2761 	case MBX_REG_LOGIN:
2762 	case MBX_UNREG_LOGIN:
2763 	case MBX_CLEAR_LA:
2764 	case MBX_DUMP_MEMORY:
2765 	case MBX_DUMP_CONTEXT:
2766 	case MBX_RUN_DIAGS:
2767 	case MBX_RESTART:
2768 	case MBX_UPDATE_CFG:
2769 	case MBX_DOWN_LOAD:
2770 	case MBX_DEL_LD_ENTRY:
2771 	case MBX_RUN_PROGRAM:
2772 	case MBX_SET_MASK:
2773 	case MBX_SET_VARIABLE:
2774 	case MBX_UNREG_D_ID:
2775 	case MBX_KILL_BOARD:
2776 	case MBX_CONFIG_FARP:
2777 	case MBX_BEACON:
2778 	case MBX_LOAD_AREA:
2779 	case MBX_RUN_BIU_DIAG64:
2780 	case MBX_CONFIG_PORT:
2781 	case MBX_READ_SPARM64:
2782 	case MBX_READ_RPI64:
2783 	case MBX_REG_LOGIN64:
2784 	case MBX_READ_TOPOLOGY:
2785 	case MBX_WRITE_WWN:
2786 	case MBX_SET_DEBUG:
2787 	case MBX_LOAD_EXP_ROM:
2788 	case MBX_ASYNCEVT_ENABLE:
2789 	case MBX_REG_VPI:
2790 	case MBX_UNREG_VPI:
2791 	case MBX_HEARTBEAT:
2792 	case MBX_PORT_CAPABILITIES:
2793 	case MBX_PORT_IOV_CONTROL:
2794 	case MBX_SLI4_CONFIG:
2795 	case MBX_SLI4_REQ_FTRS:
2796 	case MBX_REG_FCFI:
2797 	case MBX_UNREG_FCFI:
2798 	case MBX_REG_VFI:
2799 	case MBX_UNREG_VFI:
2800 	case MBX_INIT_VPI:
2801 	case MBX_INIT_VFI:
2802 	case MBX_RESUME_RPI:
2803 	case MBX_READ_EVENT_LOG_STATUS:
2804 	case MBX_READ_EVENT_LOG:
2805 	case MBX_SECURITY_MGMT:
2806 	case MBX_AUTH_PORT:
2807 	case MBX_ACCESS_VDATA:
2808 		ret = mbxCommand;
2809 		break;
2810 	default:
2811 		ret = MBX_SHUTDOWN;
2812 		break;
2813 	}
2814 	return ret;
2815 }
2816 
2817 /**
2818  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2819  * @phba: Pointer to HBA context object.
2820  * @pmboxq: Pointer to mailbox command.
2821  *
2822  * This is completion handler function for mailbox commands issued from
2823  * lpfc_sli_issue_mbox_wait function. This function is called by the
2824  * mailbox event handler function with no lock held. This function
2825  * will wake up thread waiting on the wait queue pointed by context1
2826  * of the mailbox.
2827  **/
2828 void
lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq)2829 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2830 {
2831 	unsigned long drvr_flag;
2832 	struct completion *pmbox_done;
2833 
2834 	/*
2835 	 * If pmbox_done is empty, the driver thread gave up waiting and
2836 	 * continued running.
2837 	 */
2838 	pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2839 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
2840 	pmbox_done = pmboxq->ctx_u.mbox_wait;
2841 	if (pmbox_done)
2842 		complete(pmbox_done);
2843 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2844 	return;
2845 }
2846 
2847 /**
2848  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2849  * @phba: Pointer to HBA context object.
2850  * @pmb: Pointer to mailbox object.
2851  *
2852  * This function is the default mailbox completion handler. It
2853  * frees the memory resources associated with the completed mailbox
2854  * command. If the completed command is a REG_LOGIN mailbox command,
2855  * this function will issue a UREG_LOGIN to re-claim the RPI.
2856  **/
2857 void
lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2858 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2859 {
2860 	struct lpfc_vport  *vport = pmb->vport;
2861 	struct lpfc_dmabuf *mp;
2862 	struct lpfc_nodelist *ndlp;
2863 	struct Scsi_Host *shost;
2864 	uint16_t rpi, vpi;
2865 	int rc;
2866 
2867 	/*
2868 	 * If a REG_LOGIN succeeded  after node is destroyed or node
2869 	 * is in re-discovery driver need to cleanup the RPI.
2870 	 */
2871 	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
2872 	    pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2873 	    !pmb->u.mb.mbxStatus) {
2874 		mp = pmb->ctx_buf;
2875 		if (mp) {
2876 			pmb->ctx_buf = NULL;
2877 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
2878 			kfree(mp);
2879 		}
2880 		rpi = pmb->u.mb.un.varWords[0];
2881 		vpi = pmb->u.mb.un.varRegLogin.vpi;
2882 		if (phba->sli_rev == LPFC_SLI_REV4)
2883 			vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2884 		lpfc_unreg_login(phba, vpi, rpi, pmb);
2885 		pmb->vport = vport;
2886 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2887 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2888 		if (rc != MBX_NOT_FINISHED)
2889 			return;
2890 	}
2891 
2892 	if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2893 		!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
2894 		!pmb->u.mb.mbxStatus) {
2895 		shost = lpfc_shost_from_vport(vport);
2896 		spin_lock_irq(shost->host_lock);
2897 		vport->vpi_state |= LPFC_VPI_REGISTERED;
2898 		spin_unlock_irq(shost->host_lock);
2899 		clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
2900 	}
2901 
2902 	if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2903 		ndlp = pmb->ctx_ndlp;
2904 		lpfc_nlp_put(ndlp);
2905 	}
2906 
2907 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2908 		ndlp = pmb->ctx_ndlp;
2909 
2910 		/* Check to see if there are any deferred events to process */
2911 		if (ndlp) {
2912 			lpfc_printf_vlog(
2913 				vport,
2914 				KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2915 				"1438 UNREG cmpl deferred mbox x%x "
2916 				"on NPort x%x Data: x%lx x%x x%px x%lx x%x\n",
2917 				ndlp->nlp_rpi, ndlp->nlp_DID,
2918 				ndlp->nlp_flag, ndlp->nlp_defer_did,
2919 				ndlp, vport->load_flag, kref_read(&ndlp->kref));
2920 
2921 			if (test_bit(NLP_UNREG_INP, &ndlp->nlp_flag) &&
2922 			    ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING) {
2923 				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
2924 				ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2925 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2926 			} else {
2927 				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
2928 			}
2929 
2930 			/* The unreg_login mailbox is complete and had a
2931 			 * reference that has to be released.  The PLOGI
2932 			 * got its own ref.
2933 			 */
2934 			lpfc_nlp_put(ndlp);
2935 			pmb->ctx_ndlp = NULL;
2936 		}
2937 	}
2938 
2939 	/* This nlp_put pairs with lpfc_sli4_resume_rpi */
2940 	if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
2941 		ndlp = pmb->ctx_ndlp;
2942 		lpfc_nlp_put(ndlp);
2943 	}
2944 
2945 	/* Check security permission status on INIT_LINK mailbox command */
2946 	if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2947 	    (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2948 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2949 				"2860 SLI authentication is required "
2950 				"for INIT_LINK but has not done yet\n");
2951 
2952 	if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2953 		lpfc_sli4_mbox_cmd_free(phba, pmb);
2954 	else
2955 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
2956 }
2957  /**
2958  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2959  * @phba: Pointer to HBA context object.
2960  * @pmb: Pointer to mailbox object.
2961  *
2962  * This function is the unreg rpi mailbox completion handler. It
2963  * frees the memory resources associated with the completed mailbox
2964  * command. An additional reference is put on the ndlp to prevent
2965  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2966  * the unreg mailbox command completes, this routine puts the
2967  * reference back.
2968  *
2969  **/
2970 void
lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2971 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2972 {
2973 	struct lpfc_vport  *vport = pmb->vport;
2974 	struct lpfc_nodelist *ndlp;
2975 	bool unreg_inp;
2976 
2977 	ndlp = pmb->ctx_ndlp;
2978 	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2979 		if (phba->sli_rev == LPFC_SLI_REV4 &&
2980 		    (bf_get(lpfc_sli_intf_if_type,
2981 		     &phba->sli4_hba.sli_intf) >=
2982 		     LPFC_SLI_INTF_IF_TYPE_2)) {
2983 			if (ndlp) {
2984 				lpfc_printf_vlog(
2985 					 vport, KERN_INFO,
2986 					 LOG_MBOX | LOG_SLI | LOG_NODE,
2987 					 "0010 UNREG_LOGIN vpi:x%x "
2988 					 "rpi:%x DID:%x defer x%x flg x%lx "
2989 					 "x%px\n",
2990 					 vport->vpi, ndlp->nlp_rpi,
2991 					 ndlp->nlp_DID, ndlp->nlp_defer_did,
2992 					 ndlp->nlp_flag,
2993 					 ndlp);
2994 
2995 				/* Cleanup the nlp_flag now that the UNREG RPI
2996 				 * has completed.
2997 				 */
2998 				unreg_inp = test_and_clear_bit(NLP_UNREG_INP,
2999 							       &ndlp->nlp_flag);
3000 				clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
3001 
3002 				/* Check to see if there are any deferred
3003 				 * events to process
3004 				 */
3005 				if (unreg_inp &&
3006 				    ndlp->nlp_defer_did !=
3007 				    NLP_EVT_NOTHING_PENDING) {
3008 					lpfc_printf_vlog(
3009 						vport, KERN_INFO,
3010 						LOG_MBOX | LOG_SLI | LOG_NODE,
3011 						"4111 UNREG cmpl deferred "
3012 						"clr x%x on "
3013 						"NPort x%x Data: x%x x%px\n",
3014 						ndlp->nlp_rpi, ndlp->nlp_DID,
3015 						ndlp->nlp_defer_did, ndlp);
3016 					ndlp->nlp_defer_did =
3017 						NLP_EVT_NOTHING_PENDING;
3018 					lpfc_issue_els_plogi(
3019 						vport, ndlp->nlp_DID, 0);
3020 				}
3021 
3022 				lpfc_nlp_put(ndlp);
3023 			}
3024 		}
3025 	}
3026 
3027 	mempool_free(pmb, phba->mbox_mem_pool);
3028 }
3029 
3030 /**
3031  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
3032  * @phba: Pointer to HBA context object.
3033  *
3034  * This function is called with no lock held. This function processes all
3035  * the completed mailbox commands and gives it to upper layers. The interrupt
3036  * service routine processes mailbox completion interrupt and adds completed
3037  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
3038  * Worker thread call lpfc_sli_handle_mb_event, which will return the
3039  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
3040  * function returns the mailbox commands to the upper layer by calling the
3041  * completion handler function of each mailbox.
3042  **/
3043 int
lpfc_sli_handle_mb_event(struct lpfc_hba * phba)3044 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
3045 {
3046 	MAILBOX_t *pmbox;
3047 	LPFC_MBOXQ_t *pmb;
3048 	int rc;
3049 	LIST_HEAD(cmplq);
3050 
3051 	phba->sli.slistat.mbox_event++;
3052 
3053 	/* Get all completed mailboxe buffers into the cmplq */
3054 	spin_lock_irq(&phba->hbalock);
3055 	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
3056 	spin_unlock_irq(&phba->hbalock);
3057 
3058 	/* Get a Mailbox buffer to setup mailbox commands for callback */
3059 	do {
3060 		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
3061 		if (pmb == NULL)
3062 			break;
3063 
3064 		pmbox = &pmb->u.mb;
3065 
3066 		if (pmbox->mbxCommand != MBX_HEARTBEAT) {
3067 			if (pmb->vport) {
3068 				lpfc_debugfs_disc_trc(pmb->vport,
3069 					LPFC_DISC_TRC_MBOX_VPORT,
3070 					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",
3071 					(uint32_t)pmbox->mbxCommand,
3072 					pmbox->un.varWords[0],
3073 					pmbox->un.varWords[1]);
3074 			}
3075 			else {
3076 				lpfc_debugfs_disc_trc(phba->pport,
3077 					LPFC_DISC_TRC_MBOX,
3078 					"MBOX cmpl:       cmd:x%x mb:x%x x%x",
3079 					(uint32_t)pmbox->mbxCommand,
3080 					pmbox->un.varWords[0],
3081 					pmbox->un.varWords[1]);
3082 			}
3083 		}
3084 
3085 		/*
3086 		 * It is a fatal error if unknown mbox command completion.
3087 		 */
3088 		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
3089 		    MBX_SHUTDOWN) {
3090 			/* Unknown mailbox command compl */
3091 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3092 					"(%d):0323 Unknown Mailbox command "
3093 					"x%x (x%x/x%x) Cmpl\n",
3094 					pmb->vport ? pmb->vport->vpi :
3095 					LPFC_VPORT_UNKNOWN,
3096 					pmbox->mbxCommand,
3097 					lpfc_sli_config_mbox_subsys_get(phba,
3098 									pmb),
3099 					lpfc_sli_config_mbox_opcode_get(phba,
3100 									pmb));
3101 			phba->link_state = LPFC_HBA_ERROR;
3102 			phba->work_hs = HS_FFER3;
3103 			lpfc_handle_eratt(phba);
3104 			continue;
3105 		}
3106 
3107 		if (pmbox->mbxStatus) {
3108 			phba->sli.slistat.mbox_stat_err++;
3109 			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
3110 				/* Mbox cmd cmpl error - RETRYing */
3111 				lpfc_printf_log(phba, KERN_INFO,
3112 					LOG_MBOX | LOG_SLI,
3113 					"(%d):0305 Mbox cmd cmpl "
3114 					"error - RETRYing Data: x%x "
3115 					"(x%x/x%x) x%x x%x x%x\n",
3116 					pmb->vport ? pmb->vport->vpi :
3117 					LPFC_VPORT_UNKNOWN,
3118 					pmbox->mbxCommand,
3119 					lpfc_sli_config_mbox_subsys_get(phba,
3120 									pmb),
3121 					lpfc_sli_config_mbox_opcode_get(phba,
3122 									pmb),
3123 					pmbox->mbxStatus,
3124 					pmbox->un.varWords[0],
3125 					pmb->vport ? pmb->vport->port_state :
3126 					LPFC_VPORT_UNKNOWN);
3127 				pmbox->mbxStatus = 0;
3128 				pmbox->mbxOwner = OWN_HOST;
3129 				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3130 				if (rc != MBX_NOT_FINISHED)
3131 					continue;
3132 			}
3133 		}
3134 
3135 		/* Mailbox cmd <cmd> Cmpl <cmpl> */
3136 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3137 				"(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
3138 				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3139 				"x%x x%x x%x\n",
3140 				pmb->vport ? pmb->vport->vpi : 0,
3141 				pmbox->mbxCommand,
3142 				lpfc_sli_config_mbox_subsys_get(phba, pmb),
3143 				lpfc_sli_config_mbox_opcode_get(phba, pmb),
3144 				pmb->mbox_cmpl,
3145 				*((uint32_t *) pmbox),
3146 				pmbox->un.varWords[0],
3147 				pmbox->un.varWords[1],
3148 				pmbox->un.varWords[2],
3149 				pmbox->un.varWords[3],
3150 				pmbox->un.varWords[4],
3151 				pmbox->un.varWords[5],
3152 				pmbox->un.varWords[6],
3153 				pmbox->un.varWords[7],
3154 				pmbox->un.varWords[8],
3155 				pmbox->un.varWords[9],
3156 				pmbox->un.varWords[10]);
3157 
3158 		if (pmb->mbox_cmpl)
3159 			pmb->mbox_cmpl(phba,pmb);
3160 	} while (1);
3161 	return 0;
3162 }
3163 
3164 /**
3165  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
3166  * @phba: Pointer to HBA context object.
3167  * @pring: Pointer to driver SLI ring object.
3168  * @tag: buffer tag.
3169  *
3170  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
3171  * is set in the tag the buffer is posted for a particular exchange,
3172  * the function will return the buffer without replacing the buffer.
3173  * If the buffer is for unsolicited ELS or CT traffic, this function
3174  * returns the buffer and also posts another buffer to the firmware.
3175  **/
3176 static struct lpfc_dmabuf *
lpfc_sli_get_buff(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)3177 lpfc_sli_get_buff(struct lpfc_hba *phba,
3178 		  struct lpfc_sli_ring *pring,
3179 		  uint32_t tag)
3180 {
3181 	struct hbq_dmabuf *hbq_entry;
3182 
3183 	if (tag & QUE_BUFTAG_BIT)
3184 		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
3185 	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
3186 	if (!hbq_entry)
3187 		return NULL;
3188 	return &hbq_entry->dbuf;
3189 }
3190 
3191 /**
3192  * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
3193  *                              containing a NVME LS request.
3194  * @phba: pointer to lpfc hba data structure.
3195  * @piocb: pointer to the iocbq struct representing the sequence starting
3196  *        frame.
3197  *
3198  * This routine initially validates the NVME LS, validates there is a login
3199  * with the port that sent the LS, and then calls the appropriate nvme host
3200  * or target LS request handler.
3201  **/
3202 static void
lpfc_nvme_unsol_ls_handler(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)3203 lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
3204 {
3205 	struct lpfc_nodelist *ndlp;
3206 	struct lpfc_dmabuf *d_buf;
3207 	struct hbq_dmabuf *nvmebuf;
3208 	struct fc_frame_header *fc_hdr;
3209 	struct lpfc_async_xchg_ctx *axchg = NULL;
3210 	char *failwhy = NULL;
3211 	uint32_t oxid, sid, did, fctl, size;
3212 	int ret = 1;
3213 
3214 	d_buf = piocb->cmd_dmabuf;
3215 
3216 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
3217 	fc_hdr = nvmebuf->hbuf.virt;
3218 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
3219 	sid = sli4_sid_from_fc_hdr(fc_hdr);
3220 	did = sli4_did_from_fc_hdr(fc_hdr);
3221 	fctl = (fc_hdr->fh_f_ctl[0] << 16 |
3222 		fc_hdr->fh_f_ctl[1] << 8 |
3223 		fc_hdr->fh_f_ctl[2]);
3224 	size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
3225 
3226 	lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
3227 			 oxid, size, sid);
3228 
3229 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
3230 		failwhy = "Driver Unloading";
3231 	} else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
3232 		failwhy = "NVME FC4 Disabled";
3233 	} else if (!phba->nvmet_support && !phba->pport->localport) {
3234 		failwhy = "No Localport";
3235 	} else if (phba->nvmet_support && !phba->targetport) {
3236 		failwhy = "No Targetport";
3237 	} else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
3238 		failwhy = "Bad NVME LS R_CTL";
3239 	} else if (unlikely((fctl & 0x00FF0000) !=
3240 			(FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
3241 		failwhy = "Bad NVME LS F_CTL";
3242 	} else {
3243 		axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
3244 		if (!axchg)
3245 			failwhy = "No CTX memory";
3246 	}
3247 
3248 	if (unlikely(failwhy)) {
3249 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3250 				"6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
3251 				sid, oxid, failwhy);
3252 		goto out_fail;
3253 	}
3254 
3255 	/* validate the source of the LS is logged in */
3256 	ndlp = lpfc_findnode_did(phba->pport, sid);
3257 	if (!ndlp ||
3258 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3259 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3260 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
3261 				"6216 NVME Unsol rcv: No ndlp: "
3262 				"NPort_ID x%x oxid x%x\n",
3263 				sid, oxid);
3264 		goto out_fail;
3265 	}
3266 
3267 	axchg->phba = phba;
3268 	axchg->ndlp = ndlp;
3269 	axchg->size = size;
3270 	axchg->oxid = oxid;
3271 	axchg->sid = sid;
3272 	axchg->wqeq = NULL;
3273 	axchg->state = LPFC_NVME_STE_LS_RCV;
3274 	axchg->entry_cnt = 1;
3275 	axchg->rqb_buffer = (void *)nvmebuf;
3276 	axchg->hdwq = &phba->sli4_hba.hdwq[0];
3277 	axchg->payload = nvmebuf->dbuf.virt;
3278 	INIT_LIST_HEAD(&axchg->list);
3279 
3280 	if (phba->nvmet_support) {
3281 		ret = lpfc_nvmet_handle_lsreq(phba, axchg);
3282 		spin_lock_irq(&ndlp->lock);
3283 		if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3284 			ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3285 			spin_unlock_irq(&ndlp->lock);
3286 
3287 			/* This reference is a single occurrence to hold the
3288 			 * node valid until the nvmet transport calls
3289 			 * host_release.
3290 			 */
3291 			if (!lpfc_nlp_get(ndlp))
3292 				goto out_fail;
3293 
3294 			lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
3295 					"6206 NVMET unsol ls_req ndlp x%px "
3296 					"DID x%x xflags x%x refcnt %d\n",
3297 					ndlp, ndlp->nlp_DID,
3298 					ndlp->fc4_xpt_flags,
3299 					kref_read(&ndlp->kref));
3300 		} else {
3301 			spin_unlock_irq(&ndlp->lock);
3302 		}
3303 	} else {
3304 		ret = lpfc_nvme_handle_lsreq(phba, axchg);
3305 	}
3306 
3307 	/* if zero, LS was successfully handled. If non-zero, LS not handled */
3308 	if (!ret)
3309 		return;
3310 
3311 out_fail:
3312 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3313 			"6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3314 			"NVMe%s handler failed %d\n",
3315 			did, sid, oxid,
3316 			(phba->nvmet_support) ? "T" : "I", ret);
3317 
3318 	/* recycle receive buffer */
3319 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3320 
3321 	/* If start of new exchange, abort it */
3322 	if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3323 		ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3324 
3325 	if (ret)
3326 		kfree(axchg);
3327 }
3328 
3329 /**
3330  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3331  * @phba: Pointer to HBA context object.
3332  * @pring: Pointer to driver SLI ring object.
3333  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3334  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3335  * @fch_type: the type for the first frame of the sequence.
3336  *
3337  * This function is called with no lock held. This function uses the r_ctl and
3338  * type of the received sequence to find the correct callback function to call
3339  * to process the sequence.
3340  **/
3341 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)3342 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3343 			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3344 			 uint32_t fch_type)
3345 {
3346 	int i;
3347 
3348 	switch (fch_type) {
3349 	case FC_TYPE_NVME:
3350 		lpfc_nvme_unsol_ls_handler(phba, saveq);
3351 		return 1;
3352 	default:
3353 		break;
3354 	}
3355 
3356 	/* unSolicited Responses */
3357 	if (pring->prt[0].profile) {
3358 		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3359 			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3360 									saveq);
3361 		return 1;
3362 	}
3363 	/* We must search, based on rctl / type
3364 	   for the right routine */
3365 	for (i = 0; i < pring->num_mask; i++) {
3366 		if ((pring->prt[i].rctl == fch_r_ctl) &&
3367 		    (pring->prt[i].type == fch_type)) {
3368 			if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3369 				(pring->prt[i].lpfc_sli_rcv_unsol_event)
3370 						(phba, pring, saveq);
3371 			return 1;
3372 		}
3373 	}
3374 	return 0;
3375 }
3376 
3377 static void
lpfc_sli_prep_unsol_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * saveq)3378 lpfc_sli_prep_unsol_wqe(struct lpfc_hba *phba,
3379 			struct lpfc_iocbq *saveq)
3380 {
3381 	IOCB_t *irsp;
3382 	union lpfc_wqe128 *wqe;
3383 	u16 i = 0;
3384 
3385 	irsp = &saveq->iocb;
3386 	wqe = &saveq->wqe;
3387 
3388 	/* Fill wcqe with the IOCB status fields */
3389 	bf_set(lpfc_wcqe_c_status, &saveq->wcqe_cmpl, irsp->ulpStatus);
3390 	saveq->wcqe_cmpl.word3 = irsp->ulpBdeCount;
3391 	saveq->wcqe_cmpl.parameter = irsp->un.ulpWord[4];
3392 	saveq->wcqe_cmpl.total_data_placed = irsp->unsli3.rcvsli3.acc_len;
3393 
3394 	/* Source ID */
3395 	bf_set(els_rsp64_sid, &wqe->xmit_els_rsp, irsp->un.rcvels.parmRo);
3396 
3397 	/* rx-id of the response frame */
3398 	bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com, irsp->ulpContext);
3399 
3400 	/* ox-id of the frame */
3401 	bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
3402 	       irsp->unsli3.rcvsli3.ox_id);
3403 
3404 	/* DID */
3405 	bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
3406 	       irsp->un.rcvels.remoteID);
3407 
3408 	/* unsol data len */
3409 	for (i = 0; i < irsp->ulpBdeCount; i++) {
3410 		struct lpfc_hbq_entry *hbqe = NULL;
3411 
3412 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3413 			if (i == 0) {
3414 				hbqe = (struct lpfc_hbq_entry *)
3415 					&irsp->un.ulpWord[0];
3416 				saveq->wqe.gen_req.bde.tus.f.bdeSize =
3417 					hbqe->bde.tus.f.bdeSize;
3418 			} else if (i == 1) {
3419 				hbqe = (struct lpfc_hbq_entry *)
3420 					&irsp->unsli3.sli3Words[4];
3421 				saveq->unsol_rcv_len = hbqe->bde.tus.f.bdeSize;
3422 			}
3423 		}
3424 	}
3425 }
3426 
3427 /**
3428  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
3429  * @phba: Pointer to HBA context object.
3430  * @pring: Pointer to driver SLI ring object.
3431  * @saveq: Pointer to the unsolicited iocb.
3432  *
3433  * This function is called with no lock held by the ring event handler
3434  * when there is an unsolicited iocb posted to the response ring by the
3435  * firmware. This function gets the buffer associated with the iocbs
3436  * and calls the event handler for the ring. This function handles both
3437  * qring buffers and hbq buffers.
3438  * When the function returns 1 the caller can free the iocb object otherwise
3439  * upper layer functions will free the iocb objects.
3440  **/
3441 static int
lpfc_sli_process_unsol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3442 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3443 			    struct lpfc_iocbq *saveq)
3444 {
3445 	IOCB_t           * irsp;
3446 	WORD5            * w5p;
3447 	dma_addr_t	 paddr;
3448 	uint32_t           Rctl, Type;
3449 	struct lpfc_iocbq *iocbq;
3450 	struct lpfc_dmabuf *dmzbuf;
3451 
3452 	irsp = &saveq->iocb;
3453 	saveq->vport = phba->pport;
3454 
3455 	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3456 		if (pring->lpfc_sli_rcv_async_status)
3457 			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3458 		else
3459 			lpfc_printf_log(phba,
3460 					KERN_WARNING,
3461 					LOG_SLI,
3462 					"0316 Ring %d handler: unexpected "
3463 					"ASYNC_STATUS iocb received evt_code "
3464 					"0x%x\n",
3465 					pring->ringno,
3466 					irsp->un.asyncstat.evt_code);
3467 		return 1;
3468 	}
3469 
3470 	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3471 	    (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3472 		if (irsp->ulpBdeCount > 0) {
3473 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3474 						   irsp->un.ulpWord[3]);
3475 			lpfc_in_buf_free(phba, dmzbuf);
3476 		}
3477 
3478 		if (irsp->ulpBdeCount > 1) {
3479 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3480 						   irsp->unsli3.sli3Words[3]);
3481 			lpfc_in_buf_free(phba, dmzbuf);
3482 		}
3483 
3484 		if (irsp->ulpBdeCount > 2) {
3485 			dmzbuf = lpfc_sli_get_buff(phba, pring,
3486 						   irsp->unsli3.sli3Words[7]);
3487 			lpfc_in_buf_free(phba, dmzbuf);
3488 		}
3489 
3490 		return 1;
3491 	}
3492 
3493 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3494 		if (irsp->ulpBdeCount != 0) {
3495 			saveq->cmd_dmabuf = lpfc_sli_get_buff(phba, pring,
3496 						irsp->un.ulpWord[3]);
3497 			if (!saveq->cmd_dmabuf)
3498 				lpfc_printf_log(phba,
3499 					KERN_ERR,
3500 					LOG_SLI,
3501 					"0341 Ring %d Cannot find buffer for "
3502 					"an unsolicited iocb. tag 0x%x\n",
3503 					pring->ringno,
3504 					irsp->un.ulpWord[3]);
3505 		}
3506 		if (irsp->ulpBdeCount == 2) {
3507 			saveq->bpl_dmabuf = lpfc_sli_get_buff(phba, pring,
3508 						irsp->unsli3.sli3Words[7]);
3509 			if (!saveq->bpl_dmabuf)
3510 				lpfc_printf_log(phba,
3511 					KERN_ERR,
3512 					LOG_SLI,
3513 					"0342 Ring %d Cannot find buffer for an"
3514 					" unsolicited iocb. tag 0x%x\n",
3515 					pring->ringno,
3516 					irsp->unsli3.sli3Words[7]);
3517 		}
3518 		list_for_each_entry(iocbq, &saveq->list, list) {
3519 			irsp = &iocbq->iocb;
3520 			if (irsp->ulpBdeCount != 0) {
3521 				iocbq->cmd_dmabuf = lpfc_sli_get_buff(phba,
3522 							pring,
3523 							irsp->un.ulpWord[3]);
3524 				if (!iocbq->cmd_dmabuf)
3525 					lpfc_printf_log(phba,
3526 						KERN_ERR,
3527 						LOG_SLI,
3528 						"0343 Ring %d Cannot find "
3529 						"buffer for an unsolicited iocb"
3530 						". tag 0x%x\n", pring->ringno,
3531 						irsp->un.ulpWord[3]);
3532 			}
3533 			if (irsp->ulpBdeCount == 2) {
3534 				iocbq->bpl_dmabuf = lpfc_sli_get_buff(phba,
3535 						pring,
3536 						irsp->unsli3.sli3Words[7]);
3537 				if (!iocbq->bpl_dmabuf)
3538 					lpfc_printf_log(phba,
3539 						KERN_ERR,
3540 						LOG_SLI,
3541 						"0344 Ring %d Cannot find "
3542 						"buffer for an unsolicited "
3543 						"iocb. tag 0x%x\n",
3544 						pring->ringno,
3545 						irsp->unsli3.sli3Words[7]);
3546 			}
3547 		}
3548 	} else {
3549 		paddr = getPaddr(irsp->un.cont64[0].addrHigh,
3550 				 irsp->un.cont64[0].addrLow);
3551 		saveq->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
3552 							     paddr);
3553 		if (irsp->ulpBdeCount == 2) {
3554 			paddr = getPaddr(irsp->un.cont64[1].addrHigh,
3555 					 irsp->un.cont64[1].addrLow);
3556 			saveq->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
3557 								   pring,
3558 								   paddr);
3559 		}
3560 	}
3561 
3562 	if (irsp->ulpBdeCount != 0 &&
3563 	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3564 	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3565 		int found = 0;
3566 
3567 		/* search continue save q for same XRI */
3568 		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
3569 			if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3570 				saveq->iocb.unsli3.rcvsli3.ox_id) {
3571 				list_add_tail(&saveq->list, &iocbq->list);
3572 				found = 1;
3573 				break;
3574 			}
3575 		}
3576 		if (!found)
3577 			list_add_tail(&saveq->clist,
3578 				      &pring->iocb_continue_saveq);
3579 
3580 		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3581 			list_del_init(&iocbq->clist);
3582 			saveq = iocbq;
3583 			irsp = &saveq->iocb;
3584 		} else {
3585 			return 0;
3586 		}
3587 	}
3588 	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3589 	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3590 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
3591 		Rctl = FC_RCTL_ELS_REQ;
3592 		Type = FC_TYPE_ELS;
3593 	} else {
3594 		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3595 		Rctl = w5p->hcsw.Rctl;
3596 		Type = w5p->hcsw.Type;
3597 
3598 		/* Firmware Workaround */
3599 		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3600 			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3601 			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3602 			Rctl = FC_RCTL_ELS_REQ;
3603 			Type = FC_TYPE_ELS;
3604 			w5p->hcsw.Rctl = Rctl;
3605 			w5p->hcsw.Type = Type;
3606 		}
3607 	}
3608 
3609 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3610 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
3611 	    irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3612 		if (irsp->unsli3.rcvsli3.vpi == 0xffff)
3613 			saveq->vport = phba->pport;
3614 		else
3615 			saveq->vport = lpfc_find_vport_by_vpid(phba,
3616 					       irsp->unsli3.rcvsli3.vpi);
3617 	}
3618 
3619 	/* Prepare WQE with Unsol frame */
3620 	lpfc_sli_prep_unsol_wqe(phba, saveq);
3621 
3622 	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
3623 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3624 				"0313 Ring %d handler: unexpected Rctl x%x "
3625 				"Type x%x received\n",
3626 				pring->ringno, Rctl, Type);
3627 
3628 	return 1;
3629 }
3630 
3631 /**
3632  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
3633  * @phba: Pointer to HBA context object.
3634  * @pring: Pointer to driver SLI ring object.
3635  * @prspiocb: Pointer to response iocb object.
3636  *
3637  * This function looks up the iocb_lookup table to get the command iocb
3638  * corresponding to the given response iocb using the iotag of the
3639  * response iocb. The driver calls this function with the hbalock held
3640  * for SLI3 ports or the ring lock held for SLI4 ports.
3641  * This function returns the command iocb object if it finds the command
3642  * iocb else returns NULL.
3643  **/
3644 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * prspiocb)3645 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3646 		      struct lpfc_sli_ring *pring,
3647 		      struct lpfc_iocbq *prspiocb)
3648 {
3649 	struct lpfc_iocbq *cmd_iocb = NULL;
3650 	u16 iotag;
3651 
3652 	if (phba->sli_rev == LPFC_SLI_REV4)
3653 		iotag = get_wqe_reqtag(prspiocb);
3654 	else
3655 		iotag = prspiocb->iocb.ulpIoTag;
3656 
3657 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3658 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3659 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3660 			/* remove from txcmpl queue list */
3661 			list_del_init(&cmd_iocb->list);
3662 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3663 			pring->txcmplq_cnt--;
3664 			return cmd_iocb;
3665 		}
3666 	}
3667 
3668 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3669 			"0317 iotag x%x is out of "
3670 			"range: max iotag x%x\n",
3671 			iotag, phba->sli.last_iotag);
3672 	return NULL;
3673 }
3674 
3675 /**
3676  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3677  * @phba: Pointer to HBA context object.
3678  * @pring: Pointer to driver SLI ring object.
3679  * @iotag: IOCB tag.
3680  *
3681  * This function looks up the iocb_lookup table to get the command iocb
3682  * corresponding to the given iotag. The driver calls this function with
3683  * the ring lock held because this function is an SLI4 port only helper.
3684  * This function returns the command iocb object if it finds the command
3685  * iocb else returns NULL.
3686  **/
3687 static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint16_t iotag)3688 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3689 			     struct lpfc_sli_ring *pring, uint16_t iotag)
3690 {
3691 	struct lpfc_iocbq *cmd_iocb = NULL;
3692 
3693 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3694 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
3695 		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
3696 			/* remove from txcmpl queue list */
3697 			list_del_init(&cmd_iocb->list);
3698 			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
3699 			pring->txcmplq_cnt--;
3700 			return cmd_iocb;
3701 		}
3702 	}
3703 
3704 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3705 			"0372 iotag x%x lookup error: max iotag (x%x) "
3706 			"cmd_flag x%x\n",
3707 			iotag, phba->sli.last_iotag,
3708 			cmd_iocb ? cmd_iocb->cmd_flag : 0xffff);
3709 	return NULL;
3710 }
3711 
3712 /**
3713  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3714  * @phba: Pointer to HBA context object.
3715  * @pring: Pointer to driver SLI ring object.
3716  * @saveq: Pointer to the response iocb to be processed.
3717  *
3718  * This function is called by the ring event handler for non-fcp
3719  * rings when there is a new response iocb in the response ring.
3720  * The caller is not required to hold any locks. This function
3721  * gets the command iocb associated with the response iocb and
3722  * calls the completion handler for the command iocb. If there
3723  * is no completion handler, the function will free the resources
3724  * associated with command iocb. If the response iocb is for
3725  * an already aborted command iocb, the status of the completion
3726  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3727  * This function always returns 1.
3728  **/
3729 static int
lpfc_sli_process_sol_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * saveq)3730 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3731 			  struct lpfc_iocbq *saveq)
3732 {
3733 	struct lpfc_iocbq *cmdiocbp;
3734 	unsigned long iflag;
3735 	u32 ulp_command, ulp_status, ulp_word4, ulp_context, iotag;
3736 
3737 	if (phba->sli_rev == LPFC_SLI_REV4)
3738 		spin_lock_irqsave(&pring->ring_lock, iflag);
3739 	else
3740 		spin_lock_irqsave(&phba->hbalock, iflag);
3741 	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3742 	if (phba->sli_rev == LPFC_SLI_REV4)
3743 		spin_unlock_irqrestore(&pring->ring_lock, iflag);
3744 	else
3745 		spin_unlock_irqrestore(&phba->hbalock, iflag);
3746 
3747 	ulp_command = get_job_cmnd(phba, saveq);
3748 	ulp_status = get_job_ulpstatus(phba, saveq);
3749 	ulp_word4 = get_job_word4(phba, saveq);
3750 	ulp_context = get_job_ulpcontext(phba, saveq);
3751 	if (phba->sli_rev == LPFC_SLI_REV4)
3752 		iotag = get_wqe_reqtag(saveq);
3753 	else
3754 		iotag = saveq->iocb.ulpIoTag;
3755 
3756 	if (cmdiocbp) {
3757 		ulp_command = get_job_cmnd(phba, cmdiocbp);
3758 		if (cmdiocbp->cmd_cmpl) {
3759 			/*
3760 			 * If an ELS command failed send an event to mgmt
3761 			 * application.
3762 			 */
3763 			if (ulp_status &&
3764 			     (pring->ringno == LPFC_ELS_RING) &&
3765 			     (ulp_command == CMD_ELS_REQUEST64_CR))
3766 				lpfc_send_els_failure_event(phba,
3767 					cmdiocbp, saveq);
3768 
3769 			/*
3770 			 * Post all ELS completions to the worker thread.
3771 			 * All other are passed to the completion callback.
3772 			 */
3773 			if (pring->ringno == LPFC_ELS_RING) {
3774 				if ((phba->sli_rev < LPFC_SLI_REV4) &&
3775 				    (cmdiocbp->cmd_flag &
3776 							LPFC_DRIVER_ABORTED)) {
3777 					spin_lock_irqsave(&phba->hbalock,
3778 							  iflag);
3779 					cmdiocbp->cmd_flag &=
3780 						~LPFC_DRIVER_ABORTED;
3781 					spin_unlock_irqrestore(&phba->hbalock,
3782 							       iflag);
3783 					saveq->iocb.ulpStatus =
3784 						IOSTAT_LOCAL_REJECT;
3785 					saveq->iocb.un.ulpWord[4] =
3786 						IOERR_SLI_ABORTED;
3787 
3788 					/* Firmware could still be in progress
3789 					 * of DMAing payload, so don't free data
3790 					 * buffer till after a hbeat.
3791 					 */
3792 					spin_lock_irqsave(&phba->hbalock,
3793 							  iflag);
3794 					saveq->cmd_flag |= LPFC_DELAY_MEM_FREE;
3795 					spin_unlock_irqrestore(&phba->hbalock,
3796 							       iflag);
3797 				}
3798 				if (phba->sli_rev == LPFC_SLI_REV4) {
3799 					if (saveq->cmd_flag &
3800 					    LPFC_EXCHANGE_BUSY) {
3801 						/* Set cmdiocb flag for the
3802 						 * exchange busy so sgl (xri)
3803 						 * will not be released until
3804 						 * the abort xri is received
3805 						 * from hba.
3806 						 */
3807 						spin_lock_irqsave(
3808 							&phba->hbalock, iflag);
3809 						cmdiocbp->cmd_flag |=
3810 							LPFC_EXCHANGE_BUSY;
3811 						spin_unlock_irqrestore(
3812 							&phba->hbalock, iflag);
3813 					}
3814 					if (cmdiocbp->cmd_flag &
3815 					    LPFC_DRIVER_ABORTED) {
3816 						/*
3817 						 * Clear LPFC_DRIVER_ABORTED
3818 						 * bit in case it was driver
3819 						 * initiated abort.
3820 						 */
3821 						spin_lock_irqsave(
3822 							&phba->hbalock, iflag);
3823 						cmdiocbp->cmd_flag &=
3824 							~LPFC_DRIVER_ABORTED;
3825 						spin_unlock_irqrestore(
3826 							&phba->hbalock, iflag);
3827 						set_job_ulpstatus(cmdiocbp,
3828 								  IOSTAT_LOCAL_REJECT);
3829 						set_job_ulpword4(cmdiocbp,
3830 								 IOERR_ABORT_REQUESTED);
3831 						/*
3832 						 * For SLI4, irspiocb contains
3833 						 * NO_XRI in sli_xritag, it
3834 						 * shall not affect releasing
3835 						 * sgl (xri) process.
3836 						 */
3837 						set_job_ulpstatus(saveq,
3838 								  IOSTAT_LOCAL_REJECT);
3839 						set_job_ulpword4(saveq,
3840 								 IOERR_SLI_ABORTED);
3841 						spin_lock_irqsave(
3842 							&phba->hbalock, iflag);
3843 						saveq->cmd_flag |=
3844 							LPFC_DELAY_MEM_FREE;
3845 						spin_unlock_irqrestore(
3846 							&phba->hbalock, iflag);
3847 					}
3848 				}
3849 			}
3850 			cmdiocbp->cmd_cmpl(phba, cmdiocbp, saveq);
3851 		} else
3852 			lpfc_sli_release_iocbq(phba, cmdiocbp);
3853 	} else {
3854 		/*
3855 		 * Unknown initiating command based on the response iotag.
3856 		 * This could be the case on the ELS ring because of
3857 		 * lpfc_els_abort().
3858 		 */
3859 		if (pring->ringno != LPFC_ELS_RING) {
3860 			/*
3861 			 * Ring <ringno> handler: unexpected completion IoTag
3862 			 * <IoTag>
3863 			 */
3864 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3865 					 "0322 Ring %d handler: "
3866 					 "unexpected completion IoTag x%x "
3867 					 "Data: x%x x%x x%x x%x\n",
3868 					 pring->ringno, iotag, ulp_status,
3869 					 ulp_word4, ulp_command, ulp_context);
3870 		}
3871 	}
3872 
3873 	return 1;
3874 }
3875 
3876 /**
3877  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3878  * @phba: Pointer to HBA context object.
3879  * @pring: Pointer to driver SLI ring object.
3880  *
3881  * This function is called from the iocb ring event handlers when
3882  * put pointer is ahead of the get pointer for a ring. This function signal
3883  * an error attention condition to the worker thread and the worker
3884  * thread will transition the HBA to offline state.
3885  **/
3886 static void
lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)3887 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3888 {
3889 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3890 	/*
3891 	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3892 	 * rsp ring <portRspMax>
3893 	 */
3894 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3895 			"0312 Ring %d handler: portRspPut %d "
3896 			"is bigger than rsp ring %d\n",
3897 			pring->ringno, le32_to_cpu(pgp->rspPutInx),
3898 			pring->sli.sli3.numRiocb);
3899 
3900 	phba->link_state = LPFC_HBA_ERROR;
3901 
3902 	/*
3903 	 * All error attention handlers are posted to
3904 	 * worker thread
3905 	 */
3906 	phba->work_ha |= HA_ERATT;
3907 	phba->work_hs = HS_FFER3;
3908 
3909 	lpfc_worker_wake_up(phba);
3910 
3911 	return;
3912 }
3913 
3914 /**
3915  * lpfc_poll_eratt - Error attention polling timer timeout handler
3916  * @t: Context to fetch pointer to address of HBA context object from.
3917  *
3918  * This function is invoked by the Error Attention polling timer when the
3919  * timer times out. It will check the SLI Error Attention register for
3920  * possible attention events. If so, it will post an Error Attention event
3921  * and wake up worker thread to process it. Otherwise, it will set up the
3922  * Error Attention polling timer for the next poll.
3923  **/
lpfc_poll_eratt(struct timer_list * t)3924 void lpfc_poll_eratt(struct timer_list *t)
3925 {
3926 	struct lpfc_hba *phba;
3927 	uint32_t eratt = 0;
3928 	uint64_t sli_intr, cnt;
3929 
3930 	phba = from_timer(phba, t, eratt_poll);
3931 	if (!test_bit(HBA_SETUP, &phba->hba_flag))
3932 		return;
3933 
3934 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
3935 		return;
3936 
3937 	/* Here we will also keep track of interrupts per sec of the hba */
3938 	sli_intr = phba->sli.slistat.sli_intr;
3939 
3940 	if (phba->sli.slistat.sli_prev_intr > sli_intr)
3941 		cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3942 			sli_intr);
3943 	else
3944 		cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3945 
3946 	/* 64-bit integer division not supported on 32-bit x86 - use do_div */
3947 	do_div(cnt, phba->eratt_poll_interval);
3948 	phba->sli.slistat.sli_ips = cnt;
3949 
3950 	phba->sli.slistat.sli_prev_intr = sli_intr;
3951 
3952 	/* Check chip HA register for error event */
3953 	eratt = lpfc_sli_check_eratt(phba);
3954 
3955 	if (eratt)
3956 		/* Tell the worker thread there is work to do */
3957 		lpfc_worker_wake_up(phba);
3958 	else
3959 		/* Restart the timer for next eratt poll */
3960 		mod_timer(&phba->eratt_poll,
3961 			  jiffies +
3962 			  msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3963 	return;
3964 }
3965 
3966 
3967 /**
3968  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3969  * @phba: Pointer to HBA context object.
3970  * @pring: Pointer to driver SLI ring object.
3971  * @mask: Host attention register mask for this ring.
3972  *
3973  * This function is called from the interrupt context when there is a ring
3974  * event for the fcp ring. The caller does not hold any lock.
3975  * The function processes each response iocb in the response ring until it
3976  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3977  * LE bit set. The function will call the completion handler of the command iocb
3978  * if the response iocb indicates a completion for a command iocb or it is
3979  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3980  * function if this is an unsolicited iocb.
3981  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3982  * to check it explicitly.
3983  */
3984 int
lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)3985 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3986 				struct lpfc_sli_ring *pring, uint32_t mask)
3987 {
3988 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3989 	IOCB_t *irsp = NULL;
3990 	IOCB_t *entry = NULL;
3991 	struct lpfc_iocbq *cmdiocbq = NULL;
3992 	struct lpfc_iocbq rspiocbq;
3993 	uint32_t status;
3994 	uint32_t portRspPut, portRspMax;
3995 	int rc = 1;
3996 	lpfc_iocb_type type;
3997 	unsigned long iflag;
3998 	uint32_t rsp_cmpl = 0;
3999 
4000 	spin_lock_irqsave(&phba->hbalock, iflag);
4001 	pring->stats.iocb_event++;
4002 
4003 	/*
4004 	 * The next available response entry should never exceed the maximum
4005 	 * entries.  If it does, treat it as an adapter hardware error.
4006 	 */
4007 	portRspMax = pring->sli.sli3.numRiocb;
4008 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4009 	if (unlikely(portRspPut >= portRspMax)) {
4010 		lpfc_sli_rsp_pointers_error(phba, pring);
4011 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4012 		return 1;
4013 	}
4014 	if (phba->fcp_ring_in_use) {
4015 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4016 		return 1;
4017 	} else
4018 		phba->fcp_ring_in_use = 1;
4019 
4020 	rmb();
4021 	while (pring->sli.sli3.rspidx != portRspPut) {
4022 		/*
4023 		 * Fetch an entry off the ring and copy it into a local data
4024 		 * structure.  The copy involves a byte-swap since the
4025 		 * network byte order and pci byte orders are different.
4026 		 */
4027 		entry = lpfc_resp_iocb(phba, pring);
4028 		phba->last_completion_time = jiffies;
4029 
4030 		if (++pring->sli.sli3.rspidx >= portRspMax)
4031 			pring->sli.sli3.rspidx = 0;
4032 
4033 		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
4034 				      (uint32_t *) &rspiocbq.iocb,
4035 				      phba->iocb_rsp_size);
4036 		INIT_LIST_HEAD(&(rspiocbq.list));
4037 		irsp = &rspiocbq.iocb;
4038 
4039 		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
4040 		pring->stats.iocb_rsp++;
4041 		rsp_cmpl++;
4042 
4043 		if (unlikely(irsp->ulpStatus)) {
4044 			/*
4045 			 * If resource errors reported from HBA, reduce
4046 			 * queuedepths of the SCSI device.
4047 			 */
4048 			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
4049 			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4050 			     IOERR_NO_RESOURCES)) {
4051 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4052 				phba->lpfc_rampdown_queue_depth(phba);
4053 				spin_lock_irqsave(&phba->hbalock, iflag);
4054 			}
4055 
4056 			/* Rsp ring <ringno> error: IOCB */
4057 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4058 					"0336 Rsp Ring %d error: IOCB Data: "
4059 					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
4060 					pring->ringno,
4061 					irsp->un.ulpWord[0],
4062 					irsp->un.ulpWord[1],
4063 					irsp->un.ulpWord[2],
4064 					irsp->un.ulpWord[3],
4065 					irsp->un.ulpWord[4],
4066 					irsp->un.ulpWord[5],
4067 					*(uint32_t *)&irsp->un1,
4068 					*((uint32_t *)&irsp->un1 + 1));
4069 		}
4070 
4071 		switch (type) {
4072 		case LPFC_ABORT_IOCB:
4073 		case LPFC_SOL_IOCB:
4074 			/*
4075 			 * Idle exchange closed via ABTS from port.  No iocb
4076 			 * resources need to be recovered.
4077 			 */
4078 			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
4079 				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4080 						"0333 IOCB cmd 0x%x"
4081 						" processed. Skipping"
4082 						" completion\n",
4083 						irsp->ulpCommand);
4084 				break;
4085 			}
4086 
4087 			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
4088 							 &rspiocbq);
4089 			if (unlikely(!cmdiocbq))
4090 				break;
4091 			if (cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED)
4092 				cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
4093 			if (cmdiocbq->cmd_cmpl) {
4094 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4095 				cmdiocbq->cmd_cmpl(phba, cmdiocbq, &rspiocbq);
4096 				spin_lock_irqsave(&phba->hbalock, iflag);
4097 			}
4098 			break;
4099 		case LPFC_UNSOL_IOCB:
4100 			spin_unlock_irqrestore(&phba->hbalock, iflag);
4101 			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
4102 			spin_lock_irqsave(&phba->hbalock, iflag);
4103 			break;
4104 		default:
4105 			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4106 				char adaptermsg[LPFC_MAX_ADPTMSG];
4107 				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4108 				memcpy(&adaptermsg[0], (uint8_t *) irsp,
4109 				       MAX_MSG_DATA);
4110 				dev_warn(&((phba->pcidev)->dev),
4111 					 "lpfc%d: %s\n",
4112 					 phba->brd_no, adaptermsg);
4113 			} else {
4114 				/* Unknown IOCB command */
4115 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4116 						"0334 Unknown IOCB command "
4117 						"Data: x%x, x%x x%x x%x x%x\n",
4118 						type, irsp->ulpCommand,
4119 						irsp->ulpStatus,
4120 						irsp->ulpIoTag,
4121 						irsp->ulpContext);
4122 			}
4123 			break;
4124 		}
4125 
4126 		/*
4127 		 * The response IOCB has been processed.  Update the ring
4128 		 * pointer in SLIM.  If the port response put pointer has not
4129 		 * been updated, sync the pgp->rspPutInx and fetch the new port
4130 		 * response put pointer.
4131 		 */
4132 		writel(pring->sli.sli3.rspidx,
4133 			&phba->host_gp[pring->ringno].rspGetInx);
4134 
4135 		if (pring->sli.sli3.rspidx == portRspPut)
4136 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4137 	}
4138 
4139 	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4140 		pring->stats.iocb_rsp_full++;
4141 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4142 		writel(status, phba->CAregaddr);
4143 		readl(phba->CAregaddr);
4144 	}
4145 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4146 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4147 		pring->stats.iocb_cmd_empty++;
4148 
4149 		/* Force update of the local copy of cmdGetInx */
4150 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4151 		lpfc_sli_resume_iocb(phba, pring);
4152 
4153 		if ((pring->lpfc_sli_cmd_available))
4154 			(pring->lpfc_sli_cmd_available) (phba, pring);
4155 
4156 	}
4157 
4158 	phba->fcp_ring_in_use = 0;
4159 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4160 	return rc;
4161 }
4162 
4163 /**
4164  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4165  * @phba: Pointer to HBA context object.
4166  * @pring: Pointer to driver SLI ring object.
4167  * @rspiocbp: Pointer to driver response IOCB object.
4168  *
4169  * This function is called from the worker thread when there is a slow-path
4170  * response IOCB to process. This function chains all the response iocbs until
4171  * seeing the iocb with the LE bit set. The function will call
4172  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4173  * completion of a command iocb. The function will call the
4174  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4175  * The function frees the resources or calls the completion handler if this
4176  * iocb is an abort completion. The function returns NULL when the response
4177  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4178  * this function shall chain the iocb on to the iocb_continueq and return the
4179  * response iocb passed in.
4180  **/
4181 static struct lpfc_iocbq *
lpfc_sli_sp_handle_rspiocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * rspiocbp)4182 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4183 			struct lpfc_iocbq *rspiocbp)
4184 {
4185 	struct lpfc_iocbq *saveq;
4186 	struct lpfc_iocbq *cmdiocb;
4187 	struct lpfc_iocbq *next_iocb;
4188 	IOCB_t *irsp;
4189 	uint32_t free_saveq;
4190 	u8 cmd_type;
4191 	lpfc_iocb_type type;
4192 	unsigned long iflag;
4193 	u32 ulp_status = get_job_ulpstatus(phba, rspiocbp);
4194 	u32 ulp_word4 = get_job_word4(phba, rspiocbp);
4195 	u32 ulp_command = get_job_cmnd(phba, rspiocbp);
4196 	int rc;
4197 
4198 	spin_lock_irqsave(&phba->hbalock, iflag);
4199 	/* First add the response iocb to the countinueq list */
4200 	list_add_tail(&rspiocbp->list, &pring->iocb_continueq);
4201 	pring->iocb_continueq_cnt++;
4202 
4203 	/*
4204 	 * By default, the driver expects to free all resources
4205 	 * associated with this iocb completion.
4206 	 */
4207 	free_saveq = 1;
4208 	saveq = list_get_first(&pring->iocb_continueq,
4209 			       struct lpfc_iocbq, list);
4210 	list_del_init(&pring->iocb_continueq);
4211 	pring->iocb_continueq_cnt = 0;
4212 
4213 	pring->stats.iocb_rsp++;
4214 
4215 	/*
4216 	 * If resource errors reported from HBA, reduce
4217 	 * queuedepths of the SCSI device.
4218 	 */
4219 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
4220 	    ((ulp_word4 & IOERR_PARAM_MASK) ==
4221 	     IOERR_NO_RESOURCES)) {
4222 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4223 		phba->lpfc_rampdown_queue_depth(phba);
4224 		spin_lock_irqsave(&phba->hbalock, iflag);
4225 	}
4226 
4227 	if (ulp_status) {
4228 		/* Rsp ring <ringno> error: IOCB */
4229 		if (phba->sli_rev < LPFC_SLI_REV4) {
4230 			irsp = &rspiocbp->iocb;
4231 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4232 					"0328 Rsp Ring %d error: ulp_status x%x "
4233 					"IOCB Data: "
4234 					"x%08x x%08x x%08x x%08x "
4235 					"x%08x x%08x x%08x x%08x "
4236 					"x%08x x%08x x%08x x%08x "
4237 					"x%08x x%08x x%08x x%08x\n",
4238 					pring->ringno, ulp_status,
4239 					get_job_ulpword(rspiocbp, 0),
4240 					get_job_ulpword(rspiocbp, 1),
4241 					get_job_ulpword(rspiocbp, 2),
4242 					get_job_ulpword(rspiocbp, 3),
4243 					get_job_ulpword(rspiocbp, 4),
4244 					get_job_ulpword(rspiocbp, 5),
4245 					*(((uint32_t *)irsp) + 6),
4246 					*(((uint32_t *)irsp) + 7),
4247 					*(((uint32_t *)irsp) + 8),
4248 					*(((uint32_t *)irsp) + 9),
4249 					*(((uint32_t *)irsp) + 10),
4250 					*(((uint32_t *)irsp) + 11),
4251 					*(((uint32_t *)irsp) + 12),
4252 					*(((uint32_t *)irsp) + 13),
4253 					*(((uint32_t *)irsp) + 14),
4254 					*(((uint32_t *)irsp) + 15));
4255 		} else {
4256 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4257 					"0321 Rsp Ring %d error: "
4258 					"IOCB Data: "
4259 					"x%x x%x x%x x%x\n",
4260 					pring->ringno,
4261 					rspiocbp->wcqe_cmpl.word0,
4262 					rspiocbp->wcqe_cmpl.total_data_placed,
4263 					rspiocbp->wcqe_cmpl.parameter,
4264 					rspiocbp->wcqe_cmpl.word3);
4265 		}
4266 	}
4267 
4268 
4269 	/*
4270 	 * Fetch the iocb command type and call the correct completion
4271 	 * routine. Solicited and Unsolicited IOCBs on the ELS ring
4272 	 * get freed back to the lpfc_iocb_list by the discovery
4273 	 * kernel thread.
4274 	 */
4275 	cmd_type = ulp_command & CMD_IOCB_MASK;
4276 	type = lpfc_sli_iocb_cmd_type(cmd_type);
4277 	switch (type) {
4278 	case LPFC_SOL_IOCB:
4279 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4280 		rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4281 		spin_lock_irqsave(&phba->hbalock, iflag);
4282 		break;
4283 	case LPFC_UNSOL_IOCB:
4284 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4285 		rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4286 		spin_lock_irqsave(&phba->hbalock, iflag);
4287 		if (!rc)
4288 			free_saveq = 0;
4289 		break;
4290 	case LPFC_ABORT_IOCB:
4291 		cmdiocb = NULL;
4292 		if (ulp_command != CMD_XRI_ABORTED_CX)
4293 			cmdiocb = lpfc_sli_iocbq_lookup(phba, pring,
4294 							saveq);
4295 		if (cmdiocb) {
4296 			/* Call the specified completion routine */
4297 			if (cmdiocb->cmd_cmpl) {
4298 				spin_unlock_irqrestore(&phba->hbalock, iflag);
4299 				cmdiocb->cmd_cmpl(phba, cmdiocb, saveq);
4300 				spin_lock_irqsave(&phba->hbalock, iflag);
4301 			} else {
4302 				__lpfc_sli_release_iocbq(phba, cmdiocb);
4303 			}
4304 		}
4305 		break;
4306 	case LPFC_UNKNOWN_IOCB:
4307 		if (ulp_command == CMD_ADAPTER_MSG) {
4308 			char adaptermsg[LPFC_MAX_ADPTMSG];
4309 
4310 			memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4311 			memcpy(&adaptermsg[0], (uint8_t *)&rspiocbp->wqe,
4312 			       MAX_MSG_DATA);
4313 			dev_warn(&((phba->pcidev)->dev),
4314 				 "lpfc%d: %s\n",
4315 				 phba->brd_no, adaptermsg);
4316 		} else {
4317 			/* Unknown command */
4318 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4319 					"0335 Unknown IOCB "
4320 					"command Data: x%x "
4321 					"x%x x%x x%x\n",
4322 					ulp_command,
4323 					ulp_status,
4324 					get_wqe_reqtag(rspiocbp),
4325 					get_job_ulpcontext(phba, rspiocbp));
4326 		}
4327 		break;
4328 	}
4329 
4330 	if (free_saveq) {
4331 		list_for_each_entry_safe(rspiocbp, next_iocb,
4332 					 &saveq->list, list) {
4333 			list_del_init(&rspiocbp->list);
4334 			__lpfc_sli_release_iocbq(phba, rspiocbp);
4335 		}
4336 		__lpfc_sli_release_iocbq(phba, saveq);
4337 	}
4338 	rspiocbp = NULL;
4339 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4340 	return rspiocbp;
4341 }
4342 
4343 /**
4344  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
4345  * @phba: Pointer to HBA context object.
4346  * @pring: Pointer to driver SLI ring object.
4347  * @mask: Host attention register mask for this ring.
4348  *
4349  * This routine wraps the actual slow_ring event process routine from the
4350  * API jump table function pointer from the lpfc_hba struct.
4351  **/
4352 void
lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4353 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4354 				struct lpfc_sli_ring *pring, uint32_t mask)
4355 {
4356 	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4357 }
4358 
4359 /**
4360  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4361  * @phba: Pointer to HBA context object.
4362  * @pring: Pointer to driver SLI ring object.
4363  * @mask: Host attention register mask for this ring.
4364  *
4365  * This function is called from the worker thread when there is a ring event
4366  * for non-fcp rings. The caller does not hold any lock. The function will
4367  * remove each response iocb in the response ring and calls the handle
4368  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4369  **/
4370 static void
lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4371 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4372 				   struct lpfc_sli_ring *pring, uint32_t mask)
4373 {
4374 	struct lpfc_pgp *pgp;
4375 	IOCB_t *entry;
4376 	IOCB_t *irsp = NULL;
4377 	struct lpfc_iocbq *rspiocbp = NULL;
4378 	uint32_t portRspPut, portRspMax;
4379 	unsigned long iflag;
4380 	uint32_t status;
4381 
4382 	pgp = &phba->port_gp[pring->ringno];
4383 	spin_lock_irqsave(&phba->hbalock, iflag);
4384 	pring->stats.iocb_event++;
4385 
4386 	/*
4387 	 * The next available response entry should never exceed the maximum
4388 	 * entries.  If it does, treat it as an adapter hardware error.
4389 	 */
4390 	portRspMax = pring->sli.sli3.numRiocb;
4391 	portRspPut = le32_to_cpu(pgp->rspPutInx);
4392 	if (portRspPut >= portRspMax) {
4393 		/*
4394 		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4395 		 * rsp ring <portRspMax>
4396 		 */
4397 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4398 				"0303 Ring %d handler: portRspPut %d "
4399 				"is bigger than rsp ring %d\n",
4400 				pring->ringno, portRspPut, portRspMax);
4401 
4402 		phba->link_state = LPFC_HBA_ERROR;
4403 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4404 
4405 		phba->work_hs = HS_FFER3;
4406 		lpfc_handle_eratt(phba);
4407 
4408 		return;
4409 	}
4410 
4411 	rmb();
4412 	while (pring->sli.sli3.rspidx != portRspPut) {
4413 		/*
4414 		 * Build a completion list and call the appropriate handler.
4415 		 * The process is to get the next available response iocb, get
4416 		 * a free iocb from the list, copy the response data into the
4417 		 * free iocb, insert to the continuation list, and update the
4418 		 * next response index to slim.  This process makes response
4419 		 * iocb's in the ring available to DMA as fast as possible but
4420 		 * pays a penalty for a copy operation.  Since the iocb is
4421 		 * only 32 bytes, this penalty is considered small relative to
4422 		 * the PCI reads for register values and a slim write.  When
4423 		 * the ulpLe field is set, the entire Command has been
4424 		 * received.
4425 		 */
4426 		entry = lpfc_resp_iocb(phba, pring);
4427 
4428 		phba->last_completion_time = jiffies;
4429 		rspiocbp = __lpfc_sli_get_iocbq(phba);
4430 		if (rspiocbp == NULL) {
4431 			printk(KERN_ERR "%s: out of buffers! Failing "
4432 			       "completion.\n", __func__);
4433 			break;
4434 		}
4435 
4436 		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4437 				      phba->iocb_rsp_size);
4438 		irsp = &rspiocbp->iocb;
4439 
4440 		if (++pring->sli.sli3.rspidx >= portRspMax)
4441 			pring->sli.sli3.rspidx = 0;
4442 
4443 		if (pring->ringno == LPFC_ELS_RING) {
4444 			lpfc_debugfs_slow_ring_trc(phba,
4445 			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4446 				*(((uint32_t *) irsp) + 4),
4447 				*(((uint32_t *) irsp) + 6),
4448 				*(((uint32_t *) irsp) + 7));
4449 		}
4450 
4451 		writel(pring->sli.sli3.rspidx,
4452 			&phba->host_gp[pring->ringno].rspGetInx);
4453 
4454 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4455 		/* Handle the response IOCB */
4456 		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4457 		spin_lock_irqsave(&phba->hbalock, iflag);
4458 
4459 		/*
4460 		 * If the port response put pointer has not been updated, sync
4461 		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4462 		 * response put pointer.
4463 		 */
4464 		if (pring->sli.sli3.rspidx == portRspPut) {
4465 			portRspPut = le32_to_cpu(pgp->rspPutInx);
4466 		}
4467 	} /* while (pring->sli.sli3.rspidx != portRspPut) */
4468 
4469 	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4470 		/* At least one response entry has been freed */
4471 		pring->stats.iocb_rsp_full++;
4472 		/* SET RxRE_RSP in Chip Att register */
4473 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4474 		writel(status, phba->CAregaddr);
4475 		readl(phba->CAregaddr); /* flush */
4476 	}
4477 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4478 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4479 		pring->stats.iocb_cmd_empty++;
4480 
4481 		/* Force update of the local copy of cmdGetInx */
4482 		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4483 		lpfc_sli_resume_iocb(phba, pring);
4484 
4485 		if ((pring->lpfc_sli_cmd_available))
4486 			(pring->lpfc_sli_cmd_available) (phba, pring);
4487 
4488 	}
4489 
4490 	spin_unlock_irqrestore(&phba->hbalock, iflag);
4491 	return;
4492 }
4493 
4494 /**
4495  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4496  * @phba: Pointer to HBA context object.
4497  * @pring: Pointer to driver SLI ring object.
4498  * @mask: Host attention register mask for this ring.
4499  *
4500  * This function is called from the worker thread when there is a pending
4501  * ELS response iocb on the driver internal slow-path response iocb worker
4502  * queue. The caller does not hold any lock. The function will remove each
4503  * response iocb from the response worker queue and calls the handle
4504  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4505  **/
4506 static void
lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t mask)4507 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4508 				   struct lpfc_sli_ring *pring, uint32_t mask)
4509 {
4510 	struct lpfc_iocbq *irspiocbq;
4511 	struct hbq_dmabuf *dmabuf;
4512 	struct lpfc_cq_event *cq_event;
4513 	unsigned long iflag;
4514 	int count = 0;
4515 
4516 	clear_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
4517 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4518 		/* Get the response iocb from the head of work queue */
4519 		spin_lock_irqsave(&phba->hbalock, iflag);
4520 		list_remove_head(&phba->sli4_hba.sp_queue_event,
4521 				 cq_event, struct lpfc_cq_event, list);
4522 		spin_unlock_irqrestore(&phba->hbalock, iflag);
4523 
4524 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4525 		case CQE_CODE_COMPL_WQE:
4526 			irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4527 						 cq_event);
4528 			/* Translate ELS WCQE to response IOCBQ */
4529 			irspiocbq = lpfc_sli4_els_preprocess_rspiocbq(phba,
4530 								      irspiocbq);
4531 			if (irspiocbq)
4532 				lpfc_sli_sp_handle_rspiocb(phba, pring,
4533 							   irspiocbq);
4534 			count++;
4535 			break;
4536 		case CQE_CODE_RECEIVE:
4537 		case CQE_CODE_RECEIVE_V1:
4538 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
4539 					      cq_event);
4540 			lpfc_sli4_handle_received_buffer(phba, dmabuf);
4541 			count++;
4542 			break;
4543 		default:
4544 			break;
4545 		}
4546 
4547 		/* Limit the number of events to 64 to avoid soft lockups */
4548 		if (count == 64)
4549 			break;
4550 	}
4551 }
4552 
4553 /**
4554  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4555  * @phba: Pointer to HBA context object.
4556  * @pring: Pointer to driver SLI ring object.
4557  *
4558  * This function aborts all iocbs in the given ring and frees all the iocb
4559  * objects in txq. This function issues an abort iocb for all the iocb commands
4560  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4561  * the return of this function. The caller is not required to hold any locks.
4562  **/
4563 void
lpfc_sli_abort_iocb_ring(struct lpfc_hba * phba,struct lpfc_sli_ring * pring)4564 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4565 {
4566 	LIST_HEAD(tx_completions);
4567 	LIST_HEAD(txcmplq_completions);
4568 	struct lpfc_iocbq *iocb, *next_iocb;
4569 	int offline;
4570 
4571 	if (pring->ringno == LPFC_ELS_RING) {
4572 		lpfc_fabric_abort_hba(phba);
4573 	}
4574 	offline = pci_channel_offline(phba->pcidev);
4575 
4576 	/* Error everything on txq and txcmplq
4577 	 * First do the txq.
4578 	 */
4579 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4580 		spin_lock_irq(&pring->ring_lock);
4581 		list_splice_init(&pring->txq, &tx_completions);
4582 		pring->txq_cnt = 0;
4583 
4584 		if (offline) {
4585 			list_splice_init(&pring->txcmplq,
4586 					 &txcmplq_completions);
4587 		} else {
4588 			/* Next issue ABTS for everything on the txcmplq */
4589 			list_for_each_entry_safe(iocb, next_iocb,
4590 						 &pring->txcmplq, list)
4591 				lpfc_sli_issue_abort_iotag(phba, pring,
4592 							   iocb, NULL);
4593 		}
4594 		spin_unlock_irq(&pring->ring_lock);
4595 	} else {
4596 		spin_lock_irq(&phba->hbalock);
4597 		list_splice_init(&pring->txq, &tx_completions);
4598 		pring->txq_cnt = 0;
4599 
4600 		if (offline) {
4601 			list_splice_init(&pring->txcmplq, &txcmplq_completions);
4602 		} else {
4603 			/* Next issue ABTS for everything on the txcmplq */
4604 			list_for_each_entry_safe(iocb, next_iocb,
4605 						 &pring->txcmplq, list)
4606 				lpfc_sli_issue_abort_iotag(phba, pring,
4607 							   iocb, NULL);
4608 		}
4609 		spin_unlock_irq(&phba->hbalock);
4610 	}
4611 
4612 	if (offline) {
4613 		/* Cancel all the IOCBs from the completions list */
4614 		lpfc_sli_cancel_iocbs(phba, &txcmplq_completions,
4615 				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
4616 	} else {
4617 		/* Make sure HBA is alive */
4618 		lpfc_issue_hb_tmo(phba);
4619 	}
4620 	/* Cancel all the IOCBs from the completions list */
4621 	lpfc_sli_cancel_iocbs(phba, &tx_completions, IOSTAT_LOCAL_REJECT,
4622 			      IOERR_SLI_ABORTED);
4623 }
4624 
4625 /**
4626  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4627  * @phba: Pointer to HBA context object.
4628  *
4629  * This function aborts all iocbs in FCP rings and frees all the iocb
4630  * objects in txq. This function issues an abort iocb for all the iocb commands
4631  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4632  * the return of this function. The caller is not required to hold any locks.
4633  **/
4634 void
lpfc_sli_abort_fcp_rings(struct lpfc_hba * phba)4635 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4636 {
4637 	struct lpfc_sli *psli = &phba->sli;
4638 	struct lpfc_sli_ring  *pring;
4639 	uint32_t i;
4640 
4641 	/* Look on all the FCP Rings for the iotag */
4642 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4643 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4644 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4645 			lpfc_sli_abort_iocb_ring(phba, pring);
4646 		}
4647 	} else {
4648 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4649 		lpfc_sli_abort_iocb_ring(phba, pring);
4650 	}
4651 }
4652 
4653 /**
4654  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4655  * @phba: Pointer to HBA context object.
4656  *
4657  * This function flushes all iocbs in the IO ring and frees all the iocb
4658  * objects in txq and txcmplq. This function will not issue abort iocbs
4659  * for all the iocb commands in txcmplq, they will just be returned with
4660  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4661  * slot has been permanently disabled.
4662  **/
4663 void
lpfc_sli_flush_io_rings(struct lpfc_hba * phba)4664 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4665 {
4666 	LIST_HEAD(txq);
4667 	LIST_HEAD(txcmplq);
4668 	struct lpfc_sli *psli = &phba->sli;
4669 	struct lpfc_sli_ring  *pring;
4670 	uint32_t i;
4671 	struct lpfc_iocbq *piocb, *next_iocb;
4672 
4673 	/* Indicate the I/O queues are flushed */
4674 	set_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
4675 
4676 	/* Look on all the FCP Rings for the iotag */
4677 	if (phba->sli_rev >= LPFC_SLI_REV4) {
4678 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
4679 			if (!phba->sli4_hba.hdwq ||
4680 			    !phba->sli4_hba.hdwq[i].io_wq) {
4681 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4682 						"7777 hdwq's deleted %lx "
4683 						"%lx %x %x\n",
4684 						phba->pport->load_flag,
4685 						phba->hba_flag,
4686 						phba->link_state,
4687 						phba->sli.sli_flag);
4688 				return;
4689 			}
4690 			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4691 
4692 			spin_lock_irq(&pring->ring_lock);
4693 			/* Retrieve everything on txq */
4694 			list_splice_init(&pring->txq, &txq);
4695 			list_for_each_entry_safe(piocb, next_iocb,
4696 						 &pring->txcmplq, list)
4697 				piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4698 			/* Retrieve everything on the txcmplq */
4699 			list_splice_init(&pring->txcmplq, &txcmplq);
4700 			pring->txq_cnt = 0;
4701 			pring->txcmplq_cnt = 0;
4702 			spin_unlock_irq(&pring->ring_lock);
4703 
4704 			/* Flush the txq */
4705 			lpfc_sli_cancel_iocbs(phba, &txq,
4706 					      IOSTAT_LOCAL_REJECT,
4707 					      IOERR_SLI_DOWN);
4708 			/* Flush the txcmplq */
4709 			lpfc_sli_cancel_iocbs(phba, &txcmplq,
4710 					      IOSTAT_LOCAL_REJECT,
4711 					      IOERR_SLI_DOWN);
4712 			if (unlikely(pci_channel_offline(phba->pcidev)))
4713 				lpfc_sli4_io_xri_aborted(phba, NULL, 0);
4714 		}
4715 	} else {
4716 		pring = &psli->sli3_ring[LPFC_FCP_RING];
4717 
4718 		spin_lock_irq(&phba->hbalock);
4719 		/* Retrieve everything on txq */
4720 		list_splice_init(&pring->txq, &txq);
4721 		list_for_each_entry_safe(piocb, next_iocb,
4722 					 &pring->txcmplq, list)
4723 			piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
4724 		/* Retrieve everything on the txcmplq */
4725 		list_splice_init(&pring->txcmplq, &txcmplq);
4726 		pring->txq_cnt = 0;
4727 		pring->txcmplq_cnt = 0;
4728 		spin_unlock_irq(&phba->hbalock);
4729 
4730 		/* Flush the txq */
4731 		lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4732 				      IOERR_SLI_DOWN);
4733 		/* Flush the txcmpq */
4734 		lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4735 				      IOERR_SLI_DOWN);
4736 	}
4737 }
4738 
4739 /**
4740  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4741  * @phba: Pointer to HBA context object.
4742  * @mask: Bit mask to be checked.
4743  *
4744  * This function reads the host status register and compares
4745  * with the provided bit mask to check if HBA completed
4746  * the restart. This function will wait in a loop for the
4747  * HBA to complete restart. If the HBA does not restart within
4748  * 15 iterations, the function will reset the HBA again. The
4749  * function returns 1 when HBA fail to restart otherwise returns
4750  * zero.
4751  **/
4752 static int
lpfc_sli_brdready_s3(struct lpfc_hba * phba,uint32_t mask)4753 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4754 {
4755 	uint32_t status;
4756 	int i = 0;
4757 	int retval = 0;
4758 
4759 	/* Read the HBA Host Status Register */
4760 	if (lpfc_readl(phba->HSregaddr, &status))
4761 		return 1;
4762 
4763 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
4764 
4765 	/*
4766 	 * Check status register every 100ms for 5 retries, then every
4767 	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4768 	 * every 2.5 sec for 4.
4769 	 * Break our of the loop if errors occurred during init.
4770 	 */
4771 	while (((status & mask) != mask) &&
4772 	       !(status & HS_FFERM) &&
4773 	       i++ < 20) {
4774 
4775 		if (i <= 5)
4776 			msleep(10);
4777 		else if (i <= 10)
4778 			msleep(500);
4779 		else
4780 			msleep(2500);
4781 
4782 		if (i == 15) {
4783 				/* Do post */
4784 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4785 			lpfc_sli_brdrestart(phba);
4786 		}
4787 		/* Read the HBA Host Status Register */
4788 		if (lpfc_readl(phba->HSregaddr, &status)) {
4789 			retval = 1;
4790 			break;
4791 		}
4792 	}
4793 
4794 	/* Check to see if any errors occurred during init */
4795 	if ((status & HS_FFERM) || (i >= 20)) {
4796 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4797 				"2751 Adapter failed to restart, "
4798 				"status reg x%x, FW Data: A8 x%x AC x%x\n",
4799 				status,
4800 				readl(phba->MBslimaddr + 0xa8),
4801 				readl(phba->MBslimaddr + 0xac));
4802 		phba->link_state = LPFC_HBA_ERROR;
4803 		retval = 1;
4804 	}
4805 
4806 	return retval;
4807 }
4808 
4809 /**
4810  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4811  * @phba: Pointer to HBA context object.
4812  * @mask: Bit mask to be checked.
4813  *
4814  * This function checks the host status register to check if HBA is
4815  * ready. This function will wait in a loop for the HBA to be ready
4816  * If the HBA is not ready , the function will will reset the HBA PCI
4817  * function again. The function returns 1 when HBA fail to be ready
4818  * otherwise returns zero.
4819  **/
4820 static int
lpfc_sli_brdready_s4(struct lpfc_hba * phba,uint32_t mask)4821 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4822 {
4823 	uint32_t status;
4824 	int retval = 0;
4825 
4826 	/* Read the HBA Host Status Register */
4827 	status = lpfc_sli4_post_status_check(phba);
4828 
4829 	if (status) {
4830 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4831 		lpfc_sli_brdrestart(phba);
4832 		status = lpfc_sli4_post_status_check(phba);
4833 	}
4834 
4835 	/* Check to see if any errors occurred during init */
4836 	if (status) {
4837 		phba->link_state = LPFC_HBA_ERROR;
4838 		retval = 1;
4839 	} else
4840 		phba->sli4_hba.intr_enable = 0;
4841 
4842 	clear_bit(HBA_SETUP, &phba->hba_flag);
4843 	return retval;
4844 }
4845 
4846 /**
4847  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4848  * @phba: Pointer to HBA context object.
4849  * @mask: Bit mask to be checked.
4850  *
4851  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4852  * from the API jump table function pointer from the lpfc_hba struct.
4853  **/
4854 int
lpfc_sli_brdready(struct lpfc_hba * phba,uint32_t mask)4855 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4856 {
4857 	return phba->lpfc_sli_brdready(phba, mask);
4858 }
4859 
4860 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4861 
4862 /**
4863  * lpfc_reset_barrier - Make HBA ready for HBA reset
4864  * @phba: Pointer to HBA context object.
4865  *
4866  * This function is called before resetting an HBA. This function is called
4867  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4868  **/
lpfc_reset_barrier(struct lpfc_hba * phba)4869 void lpfc_reset_barrier(struct lpfc_hba *phba)
4870 {
4871 	uint32_t __iomem *resp_buf;
4872 	uint32_t __iomem *mbox_buf;
4873 	volatile struct MAILBOX_word0 mbox;
4874 	uint32_t hc_copy, ha_copy, resp_data;
4875 	int  i;
4876 	uint8_t hdrtype;
4877 
4878 	lockdep_assert_held(&phba->hbalock);
4879 
4880 	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4881 	if (hdrtype != PCI_HEADER_TYPE_MFD ||
4882 	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4883 	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4884 		return;
4885 
4886 	/*
4887 	 * Tell the other part of the chip to suspend temporarily all
4888 	 * its DMA activity.
4889 	 */
4890 	resp_buf = phba->MBslimaddr;
4891 
4892 	/* Disable the error attention */
4893 	if (lpfc_readl(phba->HCregaddr, &hc_copy))
4894 		return;
4895 	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4896 	readl(phba->HCregaddr); /* flush */
4897 	phba->link_flag |= LS_IGNORE_ERATT;
4898 
4899 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
4900 		return;
4901 	if (ha_copy & HA_ERATT) {
4902 		/* Clear Chip error bit */
4903 		writel(HA_ERATT, phba->HAregaddr);
4904 		phba->pport->stopped = 1;
4905 	}
4906 
4907 	mbox.word0 = 0;
4908 	mbox.mbxCommand = MBX_KILL_BOARD;
4909 	mbox.mbxOwner = OWN_CHIP;
4910 
4911 	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4912 	mbox_buf = phba->MBslimaddr;
4913 	writel(mbox.word0, mbox_buf);
4914 
4915 	for (i = 0; i < 50; i++) {
4916 		if (lpfc_readl((resp_buf + 1), &resp_data))
4917 			return;
4918 		if (resp_data != ~(BARRIER_TEST_PATTERN))
4919 			mdelay(1);
4920 		else
4921 			break;
4922 	}
4923 	resp_data = 0;
4924 	if (lpfc_readl((resp_buf + 1), &resp_data))
4925 		return;
4926 	if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4927 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4928 		    phba->pport->stopped)
4929 			goto restore_hc;
4930 		else
4931 			goto clear_errat;
4932 	}
4933 
4934 	mbox.mbxOwner = OWN_HOST;
4935 	resp_data = 0;
4936 	for (i = 0; i < 500; i++) {
4937 		if (lpfc_readl(resp_buf, &resp_data))
4938 			return;
4939 		if (resp_data != mbox.word0)
4940 			mdelay(1);
4941 		else
4942 			break;
4943 	}
4944 
4945 clear_errat:
4946 
4947 	while (++i < 500) {
4948 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
4949 			return;
4950 		if (!(ha_copy & HA_ERATT))
4951 			mdelay(1);
4952 		else
4953 			break;
4954 	}
4955 
4956 	if (readl(phba->HAregaddr) & HA_ERATT) {
4957 		writel(HA_ERATT, phba->HAregaddr);
4958 		phba->pport->stopped = 1;
4959 	}
4960 
4961 restore_hc:
4962 	phba->link_flag &= ~LS_IGNORE_ERATT;
4963 	writel(hc_copy, phba->HCregaddr);
4964 	readl(phba->HCregaddr); /* flush */
4965 }
4966 
4967 /**
4968  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4969  * @phba: Pointer to HBA context object.
4970  *
4971  * This function issues a kill_board mailbox command and waits for
4972  * the error attention interrupt. This function is called for stopping
4973  * the firmware processing. The caller is not required to hold any
4974  * locks. This function calls lpfc_hba_down_post function to free
4975  * any pending commands after the kill. The function will return 1 when it
4976  * fails to kill the board else will return 0.
4977  **/
4978 int
lpfc_sli_brdkill(struct lpfc_hba * phba)4979 lpfc_sli_brdkill(struct lpfc_hba *phba)
4980 {
4981 	struct lpfc_sli *psli;
4982 	LPFC_MBOXQ_t *pmb;
4983 	uint32_t status;
4984 	uint32_t ha_copy;
4985 	int retval;
4986 	int i = 0;
4987 
4988 	psli = &phba->sli;
4989 
4990 	/* Kill HBA */
4991 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4992 			"0329 Kill HBA Data: x%x x%x\n",
4993 			phba->pport->port_state, psli->sli_flag);
4994 
4995 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4996 	if (!pmb)
4997 		return 1;
4998 
4999 	/* Disable the error attention */
5000 	spin_lock_irq(&phba->hbalock);
5001 	if (lpfc_readl(phba->HCregaddr, &status)) {
5002 		spin_unlock_irq(&phba->hbalock);
5003 		mempool_free(pmb, phba->mbox_mem_pool);
5004 		return 1;
5005 	}
5006 	status &= ~HC_ERINT_ENA;
5007 	writel(status, phba->HCregaddr);
5008 	readl(phba->HCregaddr); /* flush */
5009 	phba->link_flag |= LS_IGNORE_ERATT;
5010 	spin_unlock_irq(&phba->hbalock);
5011 
5012 	lpfc_kill_board(phba, pmb);
5013 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5014 	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5015 
5016 	if (retval != MBX_SUCCESS) {
5017 		if (retval != MBX_BUSY)
5018 			mempool_free(pmb, phba->mbox_mem_pool);
5019 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5020 				"2752 KILL_BOARD command failed retval %d\n",
5021 				retval);
5022 		spin_lock_irq(&phba->hbalock);
5023 		phba->link_flag &= ~LS_IGNORE_ERATT;
5024 		spin_unlock_irq(&phba->hbalock);
5025 		return 1;
5026 	}
5027 
5028 	spin_lock_irq(&phba->hbalock);
5029 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5030 	spin_unlock_irq(&phba->hbalock);
5031 
5032 	mempool_free(pmb, phba->mbox_mem_pool);
5033 
5034 	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
5035 	 * attention every 100ms for 3 seconds. If we don't get ERATT after
5036 	 * 3 seconds we still set HBA_ERROR state because the status of the
5037 	 * board is now undefined.
5038 	 */
5039 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
5040 		return 1;
5041 	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
5042 		mdelay(100);
5043 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
5044 			return 1;
5045 	}
5046 
5047 	del_timer_sync(&psli->mbox_tmo);
5048 	if (ha_copy & HA_ERATT) {
5049 		writel(HA_ERATT, phba->HAregaddr);
5050 		phba->pport->stopped = 1;
5051 	}
5052 	spin_lock_irq(&phba->hbalock);
5053 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5054 	psli->mbox_active = NULL;
5055 	phba->link_flag &= ~LS_IGNORE_ERATT;
5056 	spin_unlock_irq(&phba->hbalock);
5057 
5058 	lpfc_hba_down_post(phba);
5059 	phba->link_state = LPFC_HBA_ERROR;
5060 
5061 	return ha_copy & HA_ERATT ? 0 : 1;
5062 }
5063 
5064 /**
5065  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
5066  * @phba: Pointer to HBA context object.
5067  *
5068  * This function resets the HBA by writing HC_INITFF to the control
5069  * register. After the HBA resets, this function resets all the iocb ring
5070  * indices. This function disables PCI layer parity checking during
5071  * the reset.
5072  * This function returns 0 always.
5073  * The caller is not required to hold any locks.
5074  **/
5075 int
lpfc_sli_brdreset(struct lpfc_hba * phba)5076 lpfc_sli_brdreset(struct lpfc_hba *phba)
5077 {
5078 	struct lpfc_sli *psli;
5079 	struct lpfc_sli_ring *pring;
5080 	uint16_t cfg_value;
5081 	int i;
5082 
5083 	psli = &phba->sli;
5084 
5085 	/* Reset HBA */
5086 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5087 			"0325 Reset HBA Data: x%x x%x\n",
5088 			(phba->pport) ? phba->pport->port_state : 0,
5089 			psli->sli_flag);
5090 
5091 	/* perform board reset */
5092 	phba->fc_eventTag = 0;
5093 	phba->link_events = 0;
5094 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5095 	if (phba->pport) {
5096 		phba->pport->fc_myDID = 0;
5097 		phba->pport->fc_prevDID = 0;
5098 	}
5099 
5100 	/* Turn off parity checking and serr during the physical reset */
5101 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
5102 		return -EIO;
5103 
5104 	pci_write_config_word(phba->pcidev, PCI_COMMAND,
5105 			      (cfg_value &
5106 			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5107 
5108 	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
5109 
5110 	/* Now toggle INITFF bit in the Host Control Register */
5111 	writel(HC_INITFF, phba->HCregaddr);
5112 	mdelay(1);
5113 	readl(phba->HCregaddr); /* flush */
5114 	writel(0, phba->HCregaddr);
5115 	readl(phba->HCregaddr); /* flush */
5116 
5117 	/* Restore PCI cmd register */
5118 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5119 
5120 	/* Initialize relevant SLI info */
5121 	for (i = 0; i < psli->num_rings; i++) {
5122 		pring = &psli->sli3_ring[i];
5123 		pring->flag = 0;
5124 		pring->sli.sli3.rspidx = 0;
5125 		pring->sli.sli3.next_cmdidx  = 0;
5126 		pring->sli.sli3.local_getidx = 0;
5127 		pring->sli.sli3.cmdidx = 0;
5128 		pring->missbufcnt = 0;
5129 	}
5130 
5131 	phba->link_state = LPFC_WARM_START;
5132 	return 0;
5133 }
5134 
5135 /**
5136  * lpfc_sli4_brdreset - Reset a sli-4 HBA
5137  * @phba: Pointer to HBA context object.
5138  *
5139  * This function resets a SLI4 HBA. This function disables PCI layer parity
5140  * checking during resets the device. The caller is not required to hold
5141  * any locks.
5142  *
5143  * This function returns 0 on success else returns negative error code.
5144  **/
5145 int
lpfc_sli4_brdreset(struct lpfc_hba * phba)5146 lpfc_sli4_brdreset(struct lpfc_hba *phba)
5147 {
5148 	struct lpfc_sli *psli = &phba->sli;
5149 	uint16_t cfg_value;
5150 	int rc = 0;
5151 
5152 	/* Reset HBA */
5153 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5154 			"0295 Reset HBA Data: x%x x%x x%lx\n",
5155 			phba->pport->port_state, psli->sli_flag,
5156 			phba->hba_flag);
5157 
5158 	/* perform board reset */
5159 	phba->fc_eventTag = 0;
5160 	phba->link_events = 0;
5161 	phba->pport->fc_myDID = 0;
5162 	phba->pport->fc_prevDID = 0;
5163 	clear_bit(HBA_SETUP, &phba->hba_flag);
5164 
5165 	spin_lock_irq(&phba->hbalock);
5166 	psli->sli_flag &= ~(LPFC_PROCESS_LA);
5167 	phba->fcf.fcf_flag = 0;
5168 	spin_unlock_irq(&phba->hbalock);
5169 
5170 	/* Now physically reset the device */
5171 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5172 			"0389 Performing PCI function reset!\n");
5173 
5174 	/* Turn off parity checking and serr during the physical reset */
5175 	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5176 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5177 				"3205 PCI read Config failed\n");
5178 		return -EIO;
5179 	}
5180 
5181 	pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5182 			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5183 
5184 	/* Perform FCoE PCI function reset before freeing queue memory */
5185 	rc = lpfc_pci_function_reset(phba);
5186 
5187 	/* Restore PCI cmd register */
5188 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5189 
5190 	return rc;
5191 }
5192 
5193 /**
5194  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
5195  * @phba: Pointer to HBA context object.
5196  *
5197  * This function is called in the SLI initialization code path to
5198  * restart the HBA. The caller is not required to hold any lock.
5199  * This function writes MBX_RESTART mailbox command to the SLIM and
5200  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5201  * function to free any pending commands. The function enables
5202  * POST only during the first initialization. The function returns zero.
5203  * The function does not guarantee completion of MBX_RESTART mailbox
5204  * command before the return of this function.
5205  **/
5206 static int
lpfc_sli_brdrestart_s3(struct lpfc_hba * phba)5207 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
5208 {
5209 	volatile struct MAILBOX_word0 mb;
5210 	struct lpfc_sli *psli;
5211 	void __iomem *to_slim;
5212 
5213 	spin_lock_irq(&phba->hbalock);
5214 
5215 	psli = &phba->sli;
5216 
5217 	/* Restart HBA */
5218 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5219 			"0337 Restart HBA Data: x%x x%x\n",
5220 			(phba->pport) ? phba->pport->port_state : 0,
5221 			psli->sli_flag);
5222 
5223 	mb.word0 = 0;
5224 	mb.mbxCommand = MBX_RESTART;
5225 	mb.mbxHc = 1;
5226 
5227 	lpfc_reset_barrier(phba);
5228 
5229 	to_slim = phba->MBslimaddr;
5230 	writel(mb.word0, to_slim);
5231 	readl(to_slim); /* flush */
5232 
5233 	/* Only skip post after fc_ffinit is completed */
5234 	if (phba->pport && phba->pport->port_state)
5235 		mb.word0 = 1;	/* This is really setting up word1 */
5236 	else
5237 		mb.word0 = 0;	/* This is really setting up word1 */
5238 	to_slim = phba->MBslimaddr + sizeof (uint32_t);
5239 	writel(mb.word0, to_slim);
5240 	readl(to_slim); /* flush */
5241 
5242 	lpfc_sli_brdreset(phba);
5243 	if (phba->pport)
5244 		phba->pport->stopped = 0;
5245 	phba->link_state = LPFC_INIT_START;
5246 	phba->hba_flag = 0;
5247 	spin_unlock_irq(&phba->hbalock);
5248 
5249 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5250 	psli->stats_start = ktime_get_seconds();
5251 
5252 	/* Give the INITFF and Post time to settle. */
5253 	mdelay(100);
5254 
5255 	lpfc_hba_down_post(phba);
5256 
5257 	return 0;
5258 }
5259 
5260 /**
5261  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5262  * @phba: Pointer to HBA context object.
5263  *
5264  * This function is called in the SLI initialization code path to restart
5265  * a SLI4 HBA. The caller is not required to hold any lock.
5266  * At the end of the function, it calls lpfc_hba_down_post function to
5267  * free any pending commands.
5268  **/
5269 static int
lpfc_sli_brdrestart_s4(struct lpfc_hba * phba)5270 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5271 {
5272 	struct lpfc_sli *psli = &phba->sli;
5273 	int rc;
5274 
5275 	/* Restart HBA */
5276 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5277 			"0296 Restart HBA Data: x%x x%x\n",
5278 			phba->pport->port_state, psli->sli_flag);
5279 
5280 	lpfc_sli4_queue_unset(phba);
5281 
5282 	rc = lpfc_sli4_brdreset(phba);
5283 	if (rc) {
5284 		phba->link_state = LPFC_HBA_ERROR;
5285 		goto hba_down_queue;
5286 	}
5287 
5288 	spin_lock_irq(&phba->hbalock);
5289 	phba->pport->stopped = 0;
5290 	phba->link_state = LPFC_INIT_START;
5291 	phba->hba_flag = 0;
5292 	/* Preserve FA-PWWN expectation */
5293 	phba->sli4_hba.fawwpn_flag &= LPFC_FAWWPN_FABRIC;
5294 	spin_unlock_irq(&phba->hbalock);
5295 
5296 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5297 	psli->stats_start = ktime_get_seconds();
5298 
5299 hba_down_queue:
5300 	lpfc_hba_down_post(phba);
5301 	lpfc_sli4_queue_destroy(phba);
5302 
5303 	return rc;
5304 }
5305 
5306 /**
5307  * lpfc_sli_brdrestart - Wrapper func for restarting hba
5308  * @phba: Pointer to HBA context object.
5309  *
5310  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5311  * API jump table function pointer from the lpfc_hba struct.
5312 **/
5313 int
lpfc_sli_brdrestart(struct lpfc_hba * phba)5314 lpfc_sli_brdrestart(struct lpfc_hba *phba)
5315 {
5316 	return phba->lpfc_sli_brdrestart(phba);
5317 }
5318 
5319 /**
5320  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
5321  * @phba: Pointer to HBA context object.
5322  *
5323  * This function is called after a HBA restart to wait for successful
5324  * restart of the HBA. Successful restart of the HBA is indicated by
5325  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5326  * iteration, the function will restart the HBA again. The function returns
5327  * zero if HBA successfully restarted else returns negative error code.
5328  **/
5329 int
lpfc_sli_chipset_init(struct lpfc_hba * phba)5330 lpfc_sli_chipset_init(struct lpfc_hba *phba)
5331 {
5332 	uint32_t status, i = 0;
5333 
5334 	/* Read the HBA Host Status Register */
5335 	if (lpfc_readl(phba->HSregaddr, &status))
5336 		return -EIO;
5337 
5338 	/* Check status register to see what current state is */
5339 	i = 0;
5340 	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5341 
5342 		/* Check every 10ms for 10 retries, then every 100ms for 90
5343 		 * retries, then every 1 sec for 50 retires for a total of
5344 		 * ~60 seconds before reset the board again and check every
5345 		 * 1 sec for 50 retries. The up to 60 seconds before the
5346 		 * board ready is required by the Falcon FIPS zeroization
5347 		 * complete, and any reset the board in between shall cause
5348 		 * restart of zeroization, further delay the board ready.
5349 		 */
5350 		if (i++ >= 200) {
5351 			/* Adapter failed to init, timeout, status reg
5352 			   <status> */
5353 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5354 					"0436 Adapter failed to init, "
5355 					"timeout, status reg x%x, "
5356 					"FW Data: A8 x%x AC x%x\n", status,
5357 					readl(phba->MBslimaddr + 0xa8),
5358 					readl(phba->MBslimaddr + 0xac));
5359 			phba->link_state = LPFC_HBA_ERROR;
5360 			return -ETIMEDOUT;
5361 		}
5362 
5363 		/* Check to see if any errors occurred during init */
5364 		if (status & HS_FFERM) {
5365 			/* ERROR: During chipset initialization */
5366 			/* Adapter failed to init, chipset, status reg
5367 			   <status> */
5368 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5369 					"0437 Adapter failed to init, "
5370 					"chipset, status reg x%x, "
5371 					"FW Data: A8 x%x AC x%x\n", status,
5372 					readl(phba->MBslimaddr + 0xa8),
5373 					readl(phba->MBslimaddr + 0xac));
5374 			phba->link_state = LPFC_HBA_ERROR;
5375 			return -EIO;
5376 		}
5377 
5378 		if (i <= 10)
5379 			msleep(10);
5380 		else if (i <= 100)
5381 			msleep(100);
5382 		else
5383 			msleep(1000);
5384 
5385 		if (i == 150) {
5386 			/* Do post */
5387 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5388 			lpfc_sli_brdrestart(phba);
5389 		}
5390 		/* Read the HBA Host Status Register */
5391 		if (lpfc_readl(phba->HSregaddr, &status))
5392 			return -EIO;
5393 	}
5394 
5395 	/* Check to see if any errors occurred during init */
5396 	if (status & HS_FFERM) {
5397 		/* ERROR: During chipset initialization */
5398 		/* Adapter failed to init, chipset, status reg <status> */
5399 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5400 				"0438 Adapter failed to init, chipset, "
5401 				"status reg x%x, "
5402 				"FW Data: A8 x%x AC x%x\n", status,
5403 				readl(phba->MBslimaddr + 0xa8),
5404 				readl(phba->MBslimaddr + 0xac));
5405 		phba->link_state = LPFC_HBA_ERROR;
5406 		return -EIO;
5407 	}
5408 
5409 	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5410 
5411 	/* Clear all interrupt enable conditions */
5412 	writel(0, phba->HCregaddr);
5413 	readl(phba->HCregaddr); /* flush */
5414 
5415 	/* setup host attn register */
5416 	writel(0xffffffff, phba->HAregaddr);
5417 	readl(phba->HAregaddr); /* flush */
5418 	return 0;
5419 }
5420 
5421 /**
5422  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5423  *
5424  * This function calculates and returns the number of HBQs required to be
5425  * configured.
5426  **/
5427 int
lpfc_sli_hbq_count(void)5428 lpfc_sli_hbq_count(void)
5429 {
5430 	return ARRAY_SIZE(lpfc_hbq_defs);
5431 }
5432 
5433 /**
5434  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5435  *
5436  * This function adds the number of hbq entries in every HBQ to get
5437  * the total number of hbq entries required for the HBA and returns
5438  * the total count.
5439  **/
5440 static int
lpfc_sli_hbq_entry_count(void)5441 lpfc_sli_hbq_entry_count(void)
5442 {
5443 	int  hbq_count = lpfc_sli_hbq_count();
5444 	int  count = 0;
5445 	int  i;
5446 
5447 	for (i = 0; i < hbq_count; ++i)
5448 		count += lpfc_hbq_defs[i]->entry_count;
5449 	return count;
5450 }
5451 
5452 /**
5453  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5454  *
5455  * This function calculates amount of memory required for all hbq entries
5456  * to be configured and returns the total memory required.
5457  **/
5458 int
lpfc_sli_hbq_size(void)5459 lpfc_sli_hbq_size(void)
5460 {
5461 	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5462 }
5463 
5464 /**
5465  * lpfc_sli_hbq_setup - configure and initialize HBQs
5466  * @phba: Pointer to HBA context object.
5467  *
5468  * This function is called during the SLI initialization to configure
5469  * all the HBQs and post buffers to the HBQ. The caller is not
5470  * required to hold any locks. This function will return zero if successful
5471  * else it will return negative error code.
5472  **/
5473 static int
lpfc_sli_hbq_setup(struct lpfc_hba * phba)5474 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5475 {
5476 	int  hbq_count = lpfc_sli_hbq_count();
5477 	LPFC_MBOXQ_t *pmb;
5478 	MAILBOX_t *pmbox;
5479 	uint32_t hbqno;
5480 	uint32_t hbq_entry_index;
5481 
5482 				/* Get a Mailbox buffer to setup mailbox
5483 				 * commands for HBA initialization
5484 				 */
5485 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5486 
5487 	if (!pmb)
5488 		return -ENOMEM;
5489 
5490 	pmbox = &pmb->u.mb;
5491 
5492 	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
5493 	phba->link_state = LPFC_INIT_MBX_CMDS;
5494 	phba->hbq_in_use = 1;
5495 
5496 	hbq_entry_index = 0;
5497 	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5498 		phba->hbqs[hbqno].next_hbqPutIdx = 0;
5499 		phba->hbqs[hbqno].hbqPutIdx      = 0;
5500 		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5501 		phba->hbqs[hbqno].entry_count =
5502 			lpfc_hbq_defs[hbqno]->entry_count;
5503 		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5504 			hbq_entry_index, pmb);
5505 		hbq_entry_index += phba->hbqs[hbqno].entry_count;
5506 
5507 		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5508 			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5509 			   mbxStatus <status>, ring <num> */
5510 
5511 			lpfc_printf_log(phba, KERN_ERR,
5512 					LOG_SLI | LOG_VPORT,
5513 					"1805 Adapter failed to init. "
5514 					"Data: x%x x%x x%x\n",
5515 					pmbox->mbxCommand,
5516 					pmbox->mbxStatus, hbqno);
5517 
5518 			phba->link_state = LPFC_HBA_ERROR;
5519 			mempool_free(pmb, phba->mbox_mem_pool);
5520 			return -ENXIO;
5521 		}
5522 	}
5523 	phba->hbq_count = hbq_count;
5524 
5525 	mempool_free(pmb, phba->mbox_mem_pool);
5526 
5527 	/* Initially populate or replenish the HBQs */
5528 	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5529 		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5530 	return 0;
5531 }
5532 
5533 /**
5534  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5535  * @phba: Pointer to HBA context object.
5536  *
5537  * This function is called during the SLI initialization to configure
5538  * all the HBQs and post buffers to the HBQ. The caller is not
5539  * required to hold any locks. This function will return zero if successful
5540  * else it will return negative error code.
5541  **/
5542 static int
lpfc_sli4_rb_setup(struct lpfc_hba * phba)5543 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5544 {
5545 	phba->hbq_in_use = 1;
5546 	/**
5547 	 * Specific case when the MDS diagnostics is enabled and supported.
5548 	 * The receive buffer count is truncated to manage the incoming
5549 	 * traffic.
5550 	 **/
5551 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5552 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5553 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5554 	else
5555 		phba->hbqs[LPFC_ELS_HBQ].entry_count =
5556 			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5557 	phba->hbq_count = 1;
5558 	lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5559 	/* Initially populate or replenish the HBQs */
5560 	return 0;
5561 }
5562 
5563 /**
5564  * lpfc_sli_config_port - Issue config port mailbox command
5565  * @phba: Pointer to HBA context object.
5566  * @sli_mode: sli mode - 2/3
5567  *
5568  * This function is called by the sli initialization code path
5569  * to issue config_port mailbox command. This function restarts the
5570  * HBA firmware and issues a config_port mailbox command to configure
5571  * the SLI interface in the sli mode specified by sli_mode
5572  * variable. The caller is not required to hold any locks.
5573  * The function returns 0 if successful, else returns negative error
5574  * code.
5575  **/
5576 int
lpfc_sli_config_port(struct lpfc_hba * phba,int sli_mode)5577 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5578 {
5579 	LPFC_MBOXQ_t *pmb;
5580 	uint32_t resetcount = 0, rc = 0, done = 0;
5581 
5582 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5583 	if (!pmb) {
5584 		phba->link_state = LPFC_HBA_ERROR;
5585 		return -ENOMEM;
5586 	}
5587 
5588 	phba->sli_rev = sli_mode;
5589 	while (resetcount < 2 && !done) {
5590 		spin_lock_irq(&phba->hbalock);
5591 		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5592 		spin_unlock_irq(&phba->hbalock);
5593 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5594 		lpfc_sli_brdrestart(phba);
5595 		rc = lpfc_sli_chipset_init(phba);
5596 		if (rc)
5597 			break;
5598 
5599 		spin_lock_irq(&phba->hbalock);
5600 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5601 		spin_unlock_irq(&phba->hbalock);
5602 		resetcount++;
5603 
5604 		/* Call pre CONFIG_PORT mailbox command initialization.  A
5605 		 * value of 0 means the call was successful.  Any other
5606 		 * nonzero value is a failure, but if ERESTART is returned,
5607 		 * the driver may reset the HBA and try again.
5608 		 */
5609 		rc = lpfc_config_port_prep(phba);
5610 		if (rc == -ERESTART) {
5611 			phba->link_state = LPFC_LINK_UNKNOWN;
5612 			continue;
5613 		} else if (rc)
5614 			break;
5615 
5616 		phba->link_state = LPFC_INIT_MBX_CMDS;
5617 		lpfc_config_port(phba, pmb);
5618 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5619 		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5620 					LPFC_SLI3_HBQ_ENABLED |
5621 					LPFC_SLI3_CRP_ENABLED |
5622 					LPFC_SLI3_DSS_ENABLED);
5623 		if (rc != MBX_SUCCESS) {
5624 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5625 				"0442 Adapter failed to init, mbxCmd x%x "
5626 				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5627 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5628 			spin_lock_irq(&phba->hbalock);
5629 			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5630 			spin_unlock_irq(&phba->hbalock);
5631 			rc = -ENXIO;
5632 		} else {
5633 			/* Allow asynchronous mailbox command to go through */
5634 			spin_lock_irq(&phba->hbalock);
5635 			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5636 			spin_unlock_irq(&phba->hbalock);
5637 			done = 1;
5638 
5639 			if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5640 			    (pmb->u.mb.un.varCfgPort.gasabt == 0))
5641 				lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5642 					"3110 Port did not grant ASABT\n");
5643 		}
5644 	}
5645 	if (!done) {
5646 		rc = -EINVAL;
5647 		goto do_prep_failed;
5648 	}
5649 	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5650 		if (!pmb->u.mb.un.varCfgPort.cMA) {
5651 			rc = -ENXIO;
5652 			goto do_prep_failed;
5653 		}
5654 		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5655 			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5656 			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5657 			phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5658 				phba->max_vpi : phba->max_vports;
5659 
5660 		} else
5661 			phba->max_vpi = 0;
5662 		if (pmb->u.mb.un.varCfgPort.gerbm)
5663 			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5664 		if (pmb->u.mb.un.varCfgPort.gcrp)
5665 			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5666 
5667 		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5668 		phba->port_gp = phba->mbox->us.s3_pgp.port;
5669 
5670 		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5671 			if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5672 				phba->cfg_enable_bg = 0;
5673 				phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5674 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5675 						"0443 Adapter did not grant "
5676 						"BlockGuard\n");
5677 			}
5678 		}
5679 	} else {
5680 		phba->hbq_get = NULL;
5681 		phba->port_gp = phba->mbox->us.s2.port;
5682 		phba->max_vpi = 0;
5683 	}
5684 do_prep_failed:
5685 	mempool_free(pmb, phba->mbox_mem_pool);
5686 	return rc;
5687 }
5688 
5689 
5690 /**
5691  * lpfc_sli_hba_setup - SLI initialization function
5692  * @phba: Pointer to HBA context object.
5693  *
5694  * This function is the main SLI initialization function. This function
5695  * is called by the HBA initialization code, HBA reset code and HBA
5696  * error attention handler code. Caller is not required to hold any
5697  * locks. This function issues config_port mailbox command to configure
5698  * the SLI, setup iocb rings and HBQ rings. In the end the function
5699  * calls the config_port_post function to issue init_link mailbox
5700  * command and to start the discovery. The function will return zero
5701  * if successful, else it will return negative error code.
5702  **/
5703 int
lpfc_sli_hba_setup(struct lpfc_hba * phba)5704 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5705 {
5706 	uint32_t rc;
5707 	int  i;
5708 	int longs;
5709 
5710 	/* Enable ISR already does config_port because of config_msi mbx */
5711 	if (test_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag)) {
5712 		rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5713 		if (rc)
5714 			return -EIO;
5715 		clear_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
5716 	}
5717 	phba->fcp_embed_io = 0;	/* SLI4 FC support only */
5718 
5719 	if (phba->sli_rev == 3) {
5720 		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5721 		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5722 	} else {
5723 		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5724 		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5725 		phba->sli3_options = 0;
5726 	}
5727 
5728 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5729 			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
5730 			phba->sli_rev, phba->max_vpi);
5731 	rc = lpfc_sli_ring_map(phba);
5732 
5733 	if (rc)
5734 		goto lpfc_sli_hba_setup_error;
5735 
5736 	/* Initialize VPIs. */
5737 	if (phba->sli_rev == LPFC_SLI_REV3) {
5738 		/*
5739 		 * The VPI bitmask and physical ID array are allocated
5740 		 * and initialized once only - at driver load.  A port
5741 		 * reset doesn't need to reinitialize this memory.
5742 		 */
5743 		if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5744 			longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5745 			phba->vpi_bmask = kcalloc(longs,
5746 						  sizeof(unsigned long),
5747 						  GFP_KERNEL);
5748 			if (!phba->vpi_bmask) {
5749 				rc = -ENOMEM;
5750 				goto lpfc_sli_hba_setup_error;
5751 			}
5752 
5753 			phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5754 						sizeof(uint16_t),
5755 						GFP_KERNEL);
5756 			if (!phba->vpi_ids) {
5757 				kfree(phba->vpi_bmask);
5758 				rc = -ENOMEM;
5759 				goto lpfc_sli_hba_setup_error;
5760 			}
5761 			for (i = 0; i < phba->max_vpi; i++)
5762 				phba->vpi_ids[i] = i;
5763 		}
5764 	}
5765 
5766 	/* Init HBQs */
5767 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5768 		rc = lpfc_sli_hbq_setup(phba);
5769 		if (rc)
5770 			goto lpfc_sli_hba_setup_error;
5771 	}
5772 	spin_lock_irq(&phba->hbalock);
5773 	phba->sli.sli_flag |= LPFC_PROCESS_LA;
5774 	spin_unlock_irq(&phba->hbalock);
5775 
5776 	rc = lpfc_config_port_post(phba);
5777 	if (rc)
5778 		goto lpfc_sli_hba_setup_error;
5779 
5780 	return rc;
5781 
5782 lpfc_sli_hba_setup_error:
5783 	phba->link_state = LPFC_HBA_ERROR;
5784 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5785 			"0445 Firmware initialization failed\n");
5786 	return rc;
5787 }
5788 
5789 /**
5790  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5791  * @phba: Pointer to HBA context object.
5792  *
5793  * This function issue a dump mailbox command to read config region
5794  * 23 and parse the records in the region and populate driver
5795  * data structure.
5796  **/
5797 static int
lpfc_sli4_read_fcoe_params(struct lpfc_hba * phba)5798 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5799 {
5800 	LPFC_MBOXQ_t *mboxq;
5801 	struct lpfc_dmabuf *mp;
5802 	struct lpfc_mqe *mqe;
5803 	uint32_t data_length;
5804 	int rc;
5805 
5806 	/* Program the default value of vlan_id and fc_map */
5807 	phba->valid_vlan = 0;
5808 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5809 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5810 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5811 
5812 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5813 	if (!mboxq)
5814 		return -ENOMEM;
5815 
5816 	mqe = &mboxq->u.mqe;
5817 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5818 		rc = -ENOMEM;
5819 		goto out_free_mboxq;
5820 	}
5821 
5822 	mp = mboxq->ctx_buf;
5823 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5824 
5825 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5826 			"(%d):2571 Mailbox cmd x%x Status x%x "
5827 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5828 			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5829 			"CQ: x%x x%x x%x x%x\n",
5830 			mboxq->vport ? mboxq->vport->vpi : 0,
5831 			bf_get(lpfc_mqe_command, mqe),
5832 			bf_get(lpfc_mqe_status, mqe),
5833 			mqe->un.mb_words[0], mqe->un.mb_words[1],
5834 			mqe->un.mb_words[2], mqe->un.mb_words[3],
5835 			mqe->un.mb_words[4], mqe->un.mb_words[5],
5836 			mqe->un.mb_words[6], mqe->un.mb_words[7],
5837 			mqe->un.mb_words[8], mqe->un.mb_words[9],
5838 			mqe->un.mb_words[10], mqe->un.mb_words[11],
5839 			mqe->un.mb_words[12], mqe->un.mb_words[13],
5840 			mqe->un.mb_words[14], mqe->un.mb_words[15],
5841 			mqe->un.mb_words[16], mqe->un.mb_words[50],
5842 			mboxq->mcqe.word0,
5843 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
5844 			mboxq->mcqe.trailer);
5845 
5846 	if (rc) {
5847 		rc = -EIO;
5848 		goto out_free_mboxq;
5849 	}
5850 	data_length = mqe->un.mb_words[5];
5851 	if (data_length > DMP_RGN23_SIZE) {
5852 		rc = -EIO;
5853 		goto out_free_mboxq;
5854 	}
5855 
5856 	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5857 	rc = 0;
5858 
5859 out_free_mboxq:
5860 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
5861 	return rc;
5862 }
5863 
5864 /**
5865  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5866  * @phba: pointer to lpfc hba data structure.
5867  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5868  * @vpd: pointer to the memory to hold resulting port vpd data.
5869  * @vpd_size: On input, the number of bytes allocated to @vpd.
5870  *	      On output, the number of data bytes in @vpd.
5871  *
5872  * This routine executes a READ_REV SLI4 mailbox command.  In
5873  * addition, this routine gets the port vpd data.
5874  *
5875  * Return codes
5876  * 	0 - successful
5877  * 	-ENOMEM - could not allocated memory.
5878  **/
5879 static int
lpfc_sli4_read_rev(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint8_t * vpd,uint32_t * vpd_size)5880 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5881 		    uint8_t *vpd, uint32_t *vpd_size)
5882 {
5883 	int rc = 0;
5884 	uint32_t dma_size;
5885 	struct lpfc_dmabuf *dmabuf;
5886 	struct lpfc_mqe *mqe;
5887 
5888 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5889 	if (!dmabuf)
5890 		return -ENOMEM;
5891 
5892 	/*
5893 	 * Get a DMA buffer for the vpd data resulting from the READ_REV
5894 	 * mailbox command.
5895 	 */
5896 	dma_size = *vpd_size;
5897 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5898 					  &dmabuf->phys, GFP_KERNEL);
5899 	if (!dmabuf->virt) {
5900 		kfree(dmabuf);
5901 		return -ENOMEM;
5902 	}
5903 
5904 	/*
5905 	 * The SLI4 implementation of READ_REV conflicts at word1,
5906 	 * bits 31:16 and SLI4 adds vpd functionality not present
5907 	 * in SLI3.  This code corrects the conflicts.
5908 	 */
5909 	lpfc_read_rev(phba, mboxq);
5910 	mqe = &mboxq->u.mqe;
5911 	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5912 	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5913 	mqe->un.read_rev.word1 &= 0x0000FFFF;
5914 	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5915 	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5916 
5917 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5918 	if (rc) {
5919 		dma_free_coherent(&phba->pcidev->dev, dma_size,
5920 				  dmabuf->virt, dmabuf->phys);
5921 		kfree(dmabuf);
5922 		return -EIO;
5923 	}
5924 
5925 	/*
5926 	 * The available vpd length cannot be bigger than the
5927 	 * DMA buffer passed to the port.  Catch the less than
5928 	 * case and update the caller's size.
5929 	 */
5930 	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5931 		*vpd_size = mqe->un.read_rev.avail_vpd_len;
5932 
5933 	memcpy(vpd, dmabuf->virt, *vpd_size);
5934 
5935 	dma_free_coherent(&phba->pcidev->dev, dma_size,
5936 			  dmabuf->virt, dmabuf->phys);
5937 	kfree(dmabuf);
5938 	return 0;
5939 }
5940 
5941 /**
5942  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5943  * @phba: pointer to lpfc hba data structure.
5944  *
5945  * This routine retrieves SLI4 device physical port name this PCI function
5946  * is attached to.
5947  *
5948  * Return codes
5949  *      0 - successful
5950  *      otherwise - failed to retrieve controller attributes
5951  **/
5952 static int
lpfc_sli4_get_ctl_attr(struct lpfc_hba * phba)5953 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5954 {
5955 	LPFC_MBOXQ_t *mboxq;
5956 	struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5957 	struct lpfc_controller_attribute *cntl_attr;
5958 	void *virtaddr = NULL;
5959 	uint32_t alloclen, reqlen;
5960 	uint32_t shdr_status, shdr_add_status;
5961 	union lpfc_sli4_cfg_shdr *shdr;
5962 	int rc;
5963 
5964 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5965 	if (!mboxq)
5966 		return -ENOMEM;
5967 
5968 	/* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5969 	reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5970 	alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5971 			LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5972 			LPFC_SLI4_MBX_NEMBED);
5973 
5974 	if (alloclen < reqlen) {
5975 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5976 				"3084 Allocated DMA memory size (%d) is "
5977 				"less than the requested DMA memory size "
5978 				"(%d)\n", alloclen, reqlen);
5979 		rc = -ENOMEM;
5980 		goto out_free_mboxq;
5981 	}
5982 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5983 	virtaddr = mboxq->sge_array->addr[0];
5984 	mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5985 	shdr = &mbx_cntl_attr->cfg_shdr;
5986 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5987 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5988 	if (shdr_status || shdr_add_status || rc) {
5989 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5990 				"3085 Mailbox x%x (x%x/x%x) failed, "
5991 				"rc:x%x, status:x%x, add_status:x%x\n",
5992 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5993 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5994 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5995 				rc, shdr_status, shdr_add_status);
5996 		rc = -ENXIO;
5997 		goto out_free_mboxq;
5998 	}
5999 
6000 	cntl_attr = &mbx_cntl_attr->cntl_attr;
6001 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6002 	phba->sli4_hba.lnk_info.lnk_tp =
6003 		bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
6004 	phba->sli4_hba.lnk_info.lnk_no =
6005 		bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
6006 	phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
6007 	phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
6008 
6009 	memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
6010 	strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
6011 		sizeof(phba->BIOSVersion));
6012 
6013 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6014 			"3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
6015 			"flash_id: x%02x, asic_rev: x%02x\n",
6016 			phba->sli4_hba.lnk_info.lnk_tp,
6017 			phba->sli4_hba.lnk_info.lnk_no,
6018 			phba->BIOSVersion, phba->sli4_hba.flash_id,
6019 			phba->sli4_hba.asic_rev);
6020 out_free_mboxq:
6021 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6022 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6023 	else
6024 		mempool_free(mboxq, phba->mbox_mem_pool);
6025 	return rc;
6026 }
6027 
6028 /**
6029  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
6030  * @phba: pointer to lpfc hba data structure.
6031  *
6032  * This routine retrieves SLI4 device physical port name this PCI function
6033  * is attached to.
6034  *
6035  * Return codes
6036  *      0 - successful
6037  *      otherwise - failed to retrieve physical port name
6038  **/
6039 static int
lpfc_sli4_retrieve_pport_name(struct lpfc_hba * phba)6040 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
6041 {
6042 	LPFC_MBOXQ_t *mboxq;
6043 	struct lpfc_mbx_get_port_name *get_port_name;
6044 	uint32_t shdr_status, shdr_add_status;
6045 	union lpfc_sli4_cfg_shdr *shdr;
6046 	char cport_name = 0;
6047 	int rc;
6048 
6049 	/* We assume nothing at this point */
6050 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6051 	phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
6052 
6053 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6054 	if (!mboxq)
6055 		return -ENOMEM;
6056 	/* obtain link type and link number via READ_CONFIG */
6057 	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
6058 	lpfc_sli4_read_config(phba);
6059 
6060 	if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG)
6061 		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
6062 
6063 	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
6064 		goto retrieve_ppname;
6065 
6066 	/* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
6067 	rc = lpfc_sli4_get_ctl_attr(phba);
6068 	if (rc)
6069 		goto out_free_mboxq;
6070 
6071 retrieve_ppname:
6072 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6073 		LPFC_MBOX_OPCODE_GET_PORT_NAME,
6074 		sizeof(struct lpfc_mbx_get_port_name) -
6075 		sizeof(struct lpfc_sli4_cfg_mhdr),
6076 		LPFC_SLI4_MBX_EMBED);
6077 	get_port_name = &mboxq->u.mqe.un.get_port_name;
6078 	shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
6079 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
6080 	bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
6081 		phba->sli4_hba.lnk_info.lnk_tp);
6082 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6083 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6084 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6085 	if (shdr_status || shdr_add_status || rc) {
6086 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6087 				"3087 Mailbox x%x (x%x/x%x) failed: "
6088 				"rc:x%x, status:x%x, add_status:x%x\n",
6089 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6090 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6091 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6092 				rc, shdr_status, shdr_add_status);
6093 		rc = -ENXIO;
6094 		goto out_free_mboxq;
6095 	}
6096 	switch (phba->sli4_hba.lnk_info.lnk_no) {
6097 	case LPFC_LINK_NUMBER_0:
6098 		cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6099 				&get_port_name->u.response);
6100 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6101 		break;
6102 	case LPFC_LINK_NUMBER_1:
6103 		cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6104 				&get_port_name->u.response);
6105 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6106 		break;
6107 	case LPFC_LINK_NUMBER_2:
6108 		cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6109 				&get_port_name->u.response);
6110 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6111 		break;
6112 	case LPFC_LINK_NUMBER_3:
6113 		cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6114 				&get_port_name->u.response);
6115 		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6116 		break;
6117 	default:
6118 		break;
6119 	}
6120 
6121 	if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6122 		phba->Port[0] = cport_name;
6123 		phba->Port[1] = '\0';
6124 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6125 				"3091 SLI get port name: %s\n", phba->Port);
6126 	}
6127 
6128 out_free_mboxq:
6129 	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6130 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
6131 	else
6132 		mempool_free(mboxq, phba->mbox_mem_pool);
6133 	return rc;
6134 }
6135 
6136 /**
6137  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6138  * @phba: pointer to lpfc hba data structure.
6139  *
6140  * This routine is called to explicitly arm the SLI4 device's completion and
6141  * event queues
6142  **/
6143 static void
lpfc_sli4_arm_cqeq_intr(struct lpfc_hba * phba)6144 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6145 {
6146 	int qidx;
6147 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
6148 	struct lpfc_sli4_hdw_queue *qp;
6149 	struct lpfc_queue *eq;
6150 
6151 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6152 	sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
6153 	if (sli4_hba->nvmels_cq)
6154 		sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6155 					   LPFC_QUEUE_REARM);
6156 
6157 	if (sli4_hba->hdwq) {
6158 		/* Loop thru all Hardware Queues */
6159 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
6160 			qp = &sli4_hba->hdwq[qidx];
6161 			/* ARM the corresponding CQ */
6162 			sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
6163 						LPFC_QUEUE_REARM);
6164 		}
6165 
6166 		/* Loop thru all IRQ vectors */
6167 		for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6168 			eq = sli4_hba->hba_eq_hdl[qidx].eq;
6169 			/* ARM the corresponding EQ */
6170 			sli4_hba->sli4_write_eq_db(phba, eq,
6171 						   0, LPFC_QUEUE_REARM);
6172 		}
6173 	}
6174 
6175 	if (phba->nvmet_support) {
6176 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
6177 			sli4_hba->sli4_write_cq_db(phba,
6178 				sli4_hba->nvmet_cqset[qidx], 0,
6179 				LPFC_QUEUE_REARM);
6180 		}
6181 	}
6182 }
6183 
6184 /**
6185  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6186  * @phba: Pointer to HBA context object.
6187  * @type: The resource extent type.
6188  * @extnt_count: buffer to hold port available extent count.
6189  * @extnt_size: buffer to hold element count per extent.
6190  *
6191  * This function calls the port and retrievs the number of available
6192  * extents and their size for a particular extent type.
6193  *
6194  * Returns: 0 if successful.  Nonzero otherwise.
6195  **/
6196 int
lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_count,uint16_t * extnt_size)6197 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6198 			       uint16_t *extnt_count, uint16_t *extnt_size)
6199 {
6200 	int rc = 0;
6201 	uint32_t length;
6202 	uint32_t mbox_tmo;
6203 	struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6204 	LPFC_MBOXQ_t *mbox;
6205 
6206 	*extnt_count = 0;
6207 	*extnt_size = 0;
6208 
6209 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6210 	if (!mbox)
6211 		return -ENOMEM;
6212 
6213 	/* Find out how many extents are available for this resource type */
6214 	length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6215 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6216 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6217 			 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6218 			 length, LPFC_SLI4_MBX_EMBED);
6219 
6220 	/* Send an extents count of 0 - the GET doesn't use it. */
6221 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6222 					LPFC_SLI4_MBX_EMBED);
6223 	if (unlikely(rc)) {
6224 		rc = -EIO;
6225 		goto err_exit;
6226 	}
6227 
6228 	if (!phba->sli4_hba.intr_enable)
6229 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6230 	else {
6231 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6232 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6233 	}
6234 	if (unlikely(rc)) {
6235 		rc = -EIO;
6236 		goto err_exit;
6237 	}
6238 
6239 	rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6240 	if (bf_get(lpfc_mbox_hdr_status,
6241 		   &rsrc_info->header.cfg_shdr.response)) {
6242 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6243 				"2930 Failed to get resource extents "
6244 				"Status 0x%x Add'l Status 0x%x\n",
6245 				bf_get(lpfc_mbox_hdr_status,
6246 				       &rsrc_info->header.cfg_shdr.response),
6247 				bf_get(lpfc_mbox_hdr_add_status,
6248 				       &rsrc_info->header.cfg_shdr.response));
6249 		rc = -EIO;
6250 		goto err_exit;
6251 	}
6252 
6253 	*extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6254 			      &rsrc_info->u.rsp);
6255 	*extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6256 			     &rsrc_info->u.rsp);
6257 
6258 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6259 			"3162 Retrieved extents type-%d from port: count:%d, "
6260 			"size:%d\n", type, *extnt_count, *extnt_size);
6261 
6262 err_exit:
6263 	mempool_free(mbox, phba->mbox_mem_pool);
6264 	return rc;
6265 }
6266 
6267 /**
6268  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6269  * @phba: Pointer to HBA context object.
6270  * @type: The extent type to check.
6271  *
6272  * This function reads the current available extents from the port and checks
6273  * if the extent count or extent size has changed since the last access.
6274  * Callers use this routine post port reset to understand if there is a
6275  * extent reprovisioning requirement.
6276  *
6277  * Returns:
6278  *   -Error: error indicates problem.
6279  *   1: Extent count or size has changed.
6280  *   0: No changes.
6281  **/
6282 static int
lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba * phba,uint16_t type)6283 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6284 {
6285 	uint16_t curr_ext_cnt, rsrc_ext_cnt;
6286 	uint16_t size_diff, rsrc_ext_size;
6287 	int rc = 0;
6288 	struct lpfc_rsrc_blks *rsrc_entry;
6289 	struct list_head *rsrc_blk_list = NULL;
6290 
6291 	size_diff = 0;
6292 	curr_ext_cnt = 0;
6293 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6294 					    &rsrc_ext_cnt,
6295 					    &rsrc_ext_size);
6296 	if (unlikely(rc))
6297 		return -EIO;
6298 
6299 	switch (type) {
6300 	case LPFC_RSC_TYPE_FCOE_RPI:
6301 		rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6302 		break;
6303 	case LPFC_RSC_TYPE_FCOE_VPI:
6304 		rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6305 		break;
6306 	case LPFC_RSC_TYPE_FCOE_XRI:
6307 		rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6308 		break;
6309 	case LPFC_RSC_TYPE_FCOE_VFI:
6310 		rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6311 		break;
6312 	default:
6313 		break;
6314 	}
6315 
6316 	list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6317 		curr_ext_cnt++;
6318 		if (rsrc_entry->rsrc_size != rsrc_ext_size)
6319 			size_diff++;
6320 	}
6321 
6322 	if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6323 		rc = 1;
6324 
6325 	return rc;
6326 }
6327 
6328 /**
6329  * lpfc_sli4_cfg_post_extnts -
6330  * @phba: Pointer to HBA context object.
6331  * @extnt_cnt: number of available extents.
6332  * @type: the extent type (rpi, xri, vfi, vpi).
6333  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6334  * @mbox: pointer to the caller's allocated mailbox structure.
6335  *
6336  * This function executes the extents allocation request.  It also
6337  * takes care of the amount of memory needed to allocate or get the
6338  * allocated extents. It is the caller's responsibility to evaluate
6339  * the response.
6340  *
6341  * Returns:
6342  *   -Error:  Error value describes the condition found.
6343  *   0: if successful
6344  **/
6345 static int
lpfc_sli4_cfg_post_extnts(struct lpfc_hba * phba,uint16_t extnt_cnt,uint16_t type,bool * emb,LPFC_MBOXQ_t * mbox)6346 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6347 			  uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6348 {
6349 	int rc = 0;
6350 	uint32_t req_len;
6351 	uint32_t emb_len;
6352 	uint32_t alloc_len, mbox_tmo;
6353 
6354 	/* Calculate the total requested length of the dma memory */
6355 	req_len = extnt_cnt * sizeof(uint16_t);
6356 
6357 	/*
6358 	 * Calculate the size of an embedded mailbox.  The uint32_t
6359 	 * accounts for extents-specific word.
6360 	 */
6361 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6362 		sizeof(uint32_t);
6363 
6364 	/*
6365 	 * Presume the allocation and response will fit into an embedded
6366 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6367 	 */
6368 	*emb = LPFC_SLI4_MBX_EMBED;
6369 	if (req_len > emb_len) {
6370 		req_len = extnt_cnt * sizeof(uint16_t) +
6371 			sizeof(union lpfc_sli4_cfg_shdr) +
6372 			sizeof(uint32_t);
6373 		*emb = LPFC_SLI4_MBX_NEMBED;
6374 	}
6375 
6376 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6377 				     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6378 				     req_len, *emb);
6379 	if (alloc_len < req_len) {
6380 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6381 			"2982 Allocated DMA memory size (x%x) is "
6382 			"less than the requested DMA memory "
6383 			"size (x%x)\n", alloc_len, req_len);
6384 		return -ENOMEM;
6385 	}
6386 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6387 	if (unlikely(rc))
6388 		return -EIO;
6389 
6390 	if (!phba->sli4_hba.intr_enable)
6391 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6392 	else {
6393 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6394 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6395 	}
6396 
6397 	if (unlikely(rc))
6398 		rc = -EIO;
6399 	return rc;
6400 }
6401 
6402 /**
6403  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6404  * @phba: Pointer to HBA context object.
6405  * @type:  The resource extent type to allocate.
6406  *
6407  * This function allocates the number of elements for the specified
6408  * resource type.
6409  **/
6410 static int
lpfc_sli4_alloc_extent(struct lpfc_hba * phba,uint16_t type)6411 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6412 {
6413 	bool emb = false;
6414 	uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6415 	uint16_t rsrc_id, rsrc_start, j, k;
6416 	uint16_t *ids;
6417 	int i, rc;
6418 	unsigned long longs;
6419 	unsigned long *bmask;
6420 	struct lpfc_rsrc_blks *rsrc_blks;
6421 	LPFC_MBOXQ_t *mbox;
6422 	uint32_t length;
6423 	struct lpfc_id_range *id_array = NULL;
6424 	void *virtaddr = NULL;
6425 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6426 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6427 	struct list_head *ext_blk_list;
6428 
6429 	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6430 					    &rsrc_cnt,
6431 					    &rsrc_size);
6432 	if (unlikely(rc))
6433 		return -EIO;
6434 
6435 	if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6436 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6437 			"3009 No available Resource Extents "
6438 			"for resource type 0x%x: Count: 0x%x, "
6439 			"Size 0x%x\n", type, rsrc_cnt,
6440 			rsrc_size);
6441 		return -ENOMEM;
6442 	}
6443 
6444 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6445 			"2903 Post resource extents type-0x%x: "
6446 			"count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6447 
6448 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6449 	if (!mbox)
6450 		return -ENOMEM;
6451 
6452 	rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6453 	if (unlikely(rc)) {
6454 		rc = -EIO;
6455 		goto err_exit;
6456 	}
6457 
6458 	/*
6459 	 * Figure out where the response is located.  Then get local pointers
6460 	 * to the response data.  The port does not guarantee to respond to
6461 	 * all extents counts request so update the local variable with the
6462 	 * allocated count from the port.
6463 	 */
6464 	if (emb == LPFC_SLI4_MBX_EMBED) {
6465 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6466 		id_array = &rsrc_ext->u.rsp.id[0];
6467 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6468 	} else {
6469 		virtaddr = mbox->sge_array->addr[0];
6470 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6471 		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6472 		id_array = &n_rsrc->id;
6473 	}
6474 
6475 	longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6476 	rsrc_id_cnt = rsrc_cnt * rsrc_size;
6477 
6478 	/*
6479 	 * Based on the resource size and count, correct the base and max
6480 	 * resource values.
6481 	 */
6482 	length = sizeof(struct lpfc_rsrc_blks);
6483 	switch (type) {
6484 	case LPFC_RSC_TYPE_FCOE_RPI:
6485 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
6486 						   sizeof(unsigned long),
6487 						   GFP_KERNEL);
6488 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6489 			rc = -ENOMEM;
6490 			goto err_exit;
6491 		}
6492 		phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6493 						 sizeof(uint16_t),
6494 						 GFP_KERNEL);
6495 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
6496 			kfree(phba->sli4_hba.rpi_bmask);
6497 			rc = -ENOMEM;
6498 			goto err_exit;
6499 		}
6500 
6501 		/*
6502 		 * The next_rpi was initialized with the maximum available
6503 		 * count but the port may allocate a smaller number.  Catch
6504 		 * that case and update the next_rpi.
6505 		 */
6506 		phba->sli4_hba.next_rpi = rsrc_id_cnt;
6507 
6508 		/* Initialize local ptrs for common extent processing later. */
6509 		bmask = phba->sli4_hba.rpi_bmask;
6510 		ids = phba->sli4_hba.rpi_ids;
6511 		ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6512 		break;
6513 	case LPFC_RSC_TYPE_FCOE_VPI:
6514 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6515 					  GFP_KERNEL);
6516 		if (unlikely(!phba->vpi_bmask)) {
6517 			rc = -ENOMEM;
6518 			goto err_exit;
6519 		}
6520 		phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6521 					 GFP_KERNEL);
6522 		if (unlikely(!phba->vpi_ids)) {
6523 			kfree(phba->vpi_bmask);
6524 			rc = -ENOMEM;
6525 			goto err_exit;
6526 		}
6527 
6528 		/* Initialize local ptrs for common extent processing later. */
6529 		bmask = phba->vpi_bmask;
6530 		ids = phba->vpi_ids;
6531 		ext_blk_list = &phba->lpfc_vpi_blk_list;
6532 		break;
6533 	case LPFC_RSC_TYPE_FCOE_XRI:
6534 		phba->sli4_hba.xri_bmask = kcalloc(longs,
6535 						   sizeof(unsigned long),
6536 						   GFP_KERNEL);
6537 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
6538 			rc = -ENOMEM;
6539 			goto err_exit;
6540 		}
6541 		phba->sli4_hba.max_cfg_param.xri_used = 0;
6542 		phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6543 						 sizeof(uint16_t),
6544 						 GFP_KERNEL);
6545 		if (unlikely(!phba->sli4_hba.xri_ids)) {
6546 			kfree(phba->sli4_hba.xri_bmask);
6547 			rc = -ENOMEM;
6548 			goto err_exit;
6549 		}
6550 
6551 		/* Initialize local ptrs for common extent processing later. */
6552 		bmask = phba->sli4_hba.xri_bmask;
6553 		ids = phba->sli4_hba.xri_ids;
6554 		ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6555 		break;
6556 	case LPFC_RSC_TYPE_FCOE_VFI:
6557 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
6558 						   sizeof(unsigned long),
6559 						   GFP_KERNEL);
6560 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6561 			rc = -ENOMEM;
6562 			goto err_exit;
6563 		}
6564 		phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6565 						 sizeof(uint16_t),
6566 						 GFP_KERNEL);
6567 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
6568 			kfree(phba->sli4_hba.vfi_bmask);
6569 			rc = -ENOMEM;
6570 			goto err_exit;
6571 		}
6572 
6573 		/* Initialize local ptrs for common extent processing later. */
6574 		bmask = phba->sli4_hba.vfi_bmask;
6575 		ids = phba->sli4_hba.vfi_ids;
6576 		ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6577 		break;
6578 	default:
6579 		/* Unsupported Opcode.  Fail call. */
6580 		id_array = NULL;
6581 		bmask = NULL;
6582 		ids = NULL;
6583 		ext_blk_list = NULL;
6584 		goto err_exit;
6585 	}
6586 
6587 	/*
6588 	 * Complete initializing the extent configuration with the
6589 	 * allocated ids assigned to this function.  The bitmask serves
6590 	 * as an index into the array and manages the available ids.  The
6591 	 * array just stores the ids communicated to the port via the wqes.
6592 	 */
6593 	for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6594 		if ((i % 2) == 0)
6595 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6596 					 &id_array[k]);
6597 		else
6598 			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6599 					 &id_array[k]);
6600 
6601 		rsrc_blks = kzalloc(length, GFP_KERNEL);
6602 		if (unlikely(!rsrc_blks)) {
6603 			rc = -ENOMEM;
6604 			kfree(bmask);
6605 			kfree(ids);
6606 			goto err_exit;
6607 		}
6608 		rsrc_blks->rsrc_start = rsrc_id;
6609 		rsrc_blks->rsrc_size = rsrc_size;
6610 		list_add_tail(&rsrc_blks->list, ext_blk_list);
6611 		rsrc_start = rsrc_id;
6612 		if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6613 			phba->sli4_hba.io_xri_start = rsrc_start +
6614 				lpfc_sli4_get_iocb_cnt(phba);
6615 		}
6616 
6617 		while (rsrc_id < (rsrc_start + rsrc_size)) {
6618 			ids[j] = rsrc_id;
6619 			rsrc_id++;
6620 			j++;
6621 		}
6622 		/* Entire word processed.  Get next word.*/
6623 		if ((i % 2) == 1)
6624 			k++;
6625 	}
6626  err_exit:
6627 	lpfc_sli4_mbox_cmd_free(phba, mbox);
6628 	return rc;
6629 }
6630 
6631 
6632 
6633 /**
6634  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6635  * @phba: Pointer to HBA context object.
6636  * @type: the extent's type.
6637  *
6638  * This function deallocates all extents of a particular resource type.
6639  * SLI4 does not allow for deallocating a particular extent range.  It
6640  * is the caller's responsibility to release all kernel memory resources.
6641  **/
6642 static int
lpfc_sli4_dealloc_extent(struct lpfc_hba * phba,uint16_t type)6643 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6644 {
6645 	int rc;
6646 	uint32_t length, mbox_tmo = 0;
6647 	LPFC_MBOXQ_t *mbox;
6648 	struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6649 	struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6650 
6651 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6652 	if (!mbox)
6653 		return -ENOMEM;
6654 
6655 	/*
6656 	 * This function sends an embedded mailbox because it only sends the
6657 	 * the resource type.  All extents of this type are released by the
6658 	 * port.
6659 	 */
6660 	length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6661 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6662 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6663 			 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6664 			 length, LPFC_SLI4_MBX_EMBED);
6665 
6666 	/* Send an extents count of 0 - the dealloc doesn't use it. */
6667 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6668 					LPFC_SLI4_MBX_EMBED);
6669 	if (unlikely(rc)) {
6670 		rc = -EIO;
6671 		goto out_free_mbox;
6672 	}
6673 	if (!phba->sli4_hba.intr_enable)
6674 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6675 	else {
6676 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6677 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6678 	}
6679 	if (unlikely(rc)) {
6680 		rc = -EIO;
6681 		goto out_free_mbox;
6682 	}
6683 
6684 	dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6685 	if (bf_get(lpfc_mbox_hdr_status,
6686 		   &dealloc_rsrc->header.cfg_shdr.response)) {
6687 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6688 				"2919 Failed to release resource extents "
6689 				"for type %d - Status 0x%x Add'l Status 0x%x. "
6690 				"Resource memory not released.\n",
6691 				type,
6692 				bf_get(lpfc_mbox_hdr_status,
6693 				    &dealloc_rsrc->header.cfg_shdr.response),
6694 				bf_get(lpfc_mbox_hdr_add_status,
6695 				    &dealloc_rsrc->header.cfg_shdr.response));
6696 		rc = -EIO;
6697 		goto out_free_mbox;
6698 	}
6699 
6700 	/* Release kernel memory resources for the specific type. */
6701 	switch (type) {
6702 	case LPFC_RSC_TYPE_FCOE_VPI:
6703 		kfree(phba->vpi_bmask);
6704 		kfree(phba->vpi_ids);
6705 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6706 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6707 				    &phba->lpfc_vpi_blk_list, list) {
6708 			list_del_init(&rsrc_blk->list);
6709 			kfree(rsrc_blk);
6710 		}
6711 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
6712 		break;
6713 	case LPFC_RSC_TYPE_FCOE_XRI:
6714 		kfree(phba->sli4_hba.xri_bmask);
6715 		kfree(phba->sli4_hba.xri_ids);
6716 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6717 				    &phba->sli4_hba.lpfc_xri_blk_list, list) {
6718 			list_del_init(&rsrc_blk->list);
6719 			kfree(rsrc_blk);
6720 		}
6721 		break;
6722 	case LPFC_RSC_TYPE_FCOE_VFI:
6723 		kfree(phba->sli4_hba.vfi_bmask);
6724 		kfree(phba->sli4_hba.vfi_ids);
6725 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6726 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6727 				    &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6728 			list_del_init(&rsrc_blk->list);
6729 			kfree(rsrc_blk);
6730 		}
6731 		break;
6732 	case LPFC_RSC_TYPE_FCOE_RPI:
6733 		/* RPI bitmask and physical id array are cleaned up earlier. */
6734 		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6735 				    &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6736 			list_del_init(&rsrc_blk->list);
6737 			kfree(rsrc_blk);
6738 		}
6739 		break;
6740 	default:
6741 		break;
6742 	}
6743 
6744 	bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6745 
6746  out_free_mbox:
6747 	mempool_free(mbox, phba->mbox_mem_pool);
6748 	return rc;
6749 }
6750 
6751 static void
lpfc_set_features(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox,uint32_t feature)6752 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6753 		  uint32_t feature)
6754 {
6755 	uint32_t len;
6756 	u32 sig_freq = 0;
6757 
6758 	len = sizeof(struct lpfc_mbx_set_feature) -
6759 		sizeof(struct lpfc_sli4_cfg_mhdr);
6760 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6761 			 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6762 			 LPFC_SLI4_MBX_EMBED);
6763 
6764 	switch (feature) {
6765 	case LPFC_SET_UE_RECOVERY:
6766 		bf_set(lpfc_mbx_set_feature_UER,
6767 		       &mbox->u.mqe.un.set_feature, 1);
6768 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6769 		mbox->u.mqe.un.set_feature.param_len = 8;
6770 		break;
6771 	case LPFC_SET_MDS_DIAGS:
6772 		bf_set(lpfc_mbx_set_feature_mds,
6773 		       &mbox->u.mqe.un.set_feature, 1);
6774 		bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6775 		       &mbox->u.mqe.un.set_feature, 1);
6776 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6777 		mbox->u.mqe.un.set_feature.param_len = 8;
6778 		break;
6779 	case LPFC_SET_CGN_SIGNAL:
6780 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
6781 			sig_freq = 0;
6782 		else
6783 			sig_freq = phba->cgn_sig_freq;
6784 
6785 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6786 			bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6787 			       &mbox->u.mqe.un.set_feature, sig_freq);
6788 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6789 			       &mbox->u.mqe.un.set_feature, sig_freq);
6790 		}
6791 
6792 		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6793 			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6794 			       &mbox->u.mqe.un.set_feature, sig_freq);
6795 
6796 		if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6797 		    phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6798 			sig_freq = 0;
6799 		else
6800 			sig_freq = lpfc_acqe_cgn_frequency;
6801 
6802 		bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6803 		       &mbox->u.mqe.un.set_feature, sig_freq);
6804 
6805 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6806 		mbox->u.mqe.un.set_feature.param_len = 12;
6807 		break;
6808 	case LPFC_SET_DUAL_DUMP:
6809 		bf_set(lpfc_mbx_set_feature_dd,
6810 		       &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6811 		bf_set(lpfc_mbx_set_feature_ddquery,
6812 		       &mbox->u.mqe.un.set_feature, 0);
6813 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6814 		mbox->u.mqe.un.set_feature.param_len = 4;
6815 		break;
6816 	case LPFC_SET_ENABLE_MI:
6817 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6818 		mbox->u.mqe.un.set_feature.param_len = 4;
6819 		bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6820 		       phba->pport->cfg_lun_queue_depth);
6821 		bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6822 		       phba->sli4_hba.pc_sli4_params.mi_ver);
6823 		break;
6824 	case LPFC_SET_LD_SIGNAL:
6825 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_LD_SIGNAL;
6826 		mbox->u.mqe.un.set_feature.param_len = 16;
6827 		bf_set(lpfc_mbx_set_feature_lds_qry,
6828 		       &mbox->u.mqe.un.set_feature, LPFC_QUERY_LDS_OP);
6829 		break;
6830 	case LPFC_SET_ENABLE_CMF:
6831 		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6832 		mbox->u.mqe.un.set_feature.param_len = 4;
6833 		bf_set(lpfc_mbx_set_feature_cmf,
6834 		       &mbox->u.mqe.un.set_feature, 1);
6835 		break;
6836 	}
6837 	return;
6838 }
6839 
6840 /**
6841  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6842  * @phba: Pointer to HBA context object.
6843  *
6844  * Disable FW logging into host memory on the adapter. To
6845  * be done before reading logs from the host memory.
6846  **/
6847 void
lpfc_ras_stop_fwlog(struct lpfc_hba * phba)6848 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6849 {
6850 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6851 
6852 	spin_lock_irq(&phba->ras_fwlog_lock);
6853 	ras_fwlog->state = INACTIVE;
6854 	spin_unlock_irq(&phba->ras_fwlog_lock);
6855 
6856 	/* Disable FW logging to host memory */
6857 	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6858 	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6859 
6860 	/* Wait 10ms for firmware to stop using DMA buffer */
6861 	usleep_range(10 * 1000, 20 * 1000);
6862 }
6863 
6864 /**
6865  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6866  * @phba: Pointer to HBA context object.
6867  *
6868  * This function is called to free memory allocated for RAS FW logging
6869  * support in the driver.
6870  **/
6871 void
lpfc_sli4_ras_dma_free(struct lpfc_hba * phba)6872 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6873 {
6874 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6875 	struct lpfc_dmabuf *dmabuf, *next;
6876 
6877 	if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6878 		list_for_each_entry_safe(dmabuf, next,
6879 				    &ras_fwlog->fwlog_buff_list,
6880 				    list) {
6881 			list_del(&dmabuf->list);
6882 			dma_free_coherent(&phba->pcidev->dev,
6883 					  LPFC_RAS_MAX_ENTRY_SIZE,
6884 					  dmabuf->virt, dmabuf->phys);
6885 			kfree(dmabuf);
6886 		}
6887 	}
6888 
6889 	if (ras_fwlog->lwpd.virt) {
6890 		dma_free_coherent(&phba->pcidev->dev,
6891 				  sizeof(uint32_t) * 2,
6892 				  ras_fwlog->lwpd.virt,
6893 				  ras_fwlog->lwpd.phys);
6894 		ras_fwlog->lwpd.virt = NULL;
6895 	}
6896 
6897 	spin_lock_irq(&phba->ras_fwlog_lock);
6898 	ras_fwlog->state = INACTIVE;
6899 	spin_unlock_irq(&phba->ras_fwlog_lock);
6900 }
6901 
6902 /**
6903  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6904  * @phba: Pointer to HBA context object.
6905  * @fwlog_buff_count: Count of buffers to be created.
6906  *
6907  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6908  * to update FW log is posted to the adapter.
6909  * Buffer count is calculated based on module param ras_fwlog_buffsize
6910  * Size of each buffer posted to FW is 64K.
6911  **/
6912 
6913 static int
lpfc_sli4_ras_dma_alloc(struct lpfc_hba * phba,uint32_t fwlog_buff_count)6914 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6915 			uint32_t fwlog_buff_count)
6916 {
6917 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6918 	struct lpfc_dmabuf *dmabuf;
6919 	int rc = 0, i = 0;
6920 
6921 	/* Initialize List */
6922 	INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6923 
6924 	/* Allocate memory for the LWPD */
6925 	ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6926 					    sizeof(uint32_t) * 2,
6927 					    &ras_fwlog->lwpd.phys,
6928 					    GFP_KERNEL);
6929 	if (!ras_fwlog->lwpd.virt) {
6930 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6931 				"6185 LWPD Memory Alloc Failed\n");
6932 
6933 		return -ENOMEM;
6934 	}
6935 
6936 	ras_fwlog->fw_buffcount = fwlog_buff_count;
6937 	for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6938 		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6939 				 GFP_KERNEL);
6940 		if (!dmabuf) {
6941 			rc = -ENOMEM;
6942 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6943 					"6186 Memory Alloc failed FW logging");
6944 			goto free_mem;
6945 		}
6946 
6947 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6948 						  LPFC_RAS_MAX_ENTRY_SIZE,
6949 						  &dmabuf->phys, GFP_KERNEL);
6950 		if (!dmabuf->virt) {
6951 			kfree(dmabuf);
6952 			rc = -ENOMEM;
6953 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6954 					"6187 DMA Alloc Failed FW logging");
6955 			goto free_mem;
6956 		}
6957 		dmabuf->buffer_tag = i;
6958 		list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6959 	}
6960 
6961 free_mem:
6962 	if (rc)
6963 		lpfc_sli4_ras_dma_free(phba);
6964 
6965 	return rc;
6966 }
6967 
6968 /**
6969  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6970  * @phba: pointer to lpfc hba data structure.
6971  * @pmb: pointer to the driver internal queue element for mailbox command.
6972  *
6973  * Completion handler for driver's RAS MBX command to the device.
6974  **/
6975 static void
lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)6976 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6977 {
6978 	MAILBOX_t *mb;
6979 	union lpfc_sli4_cfg_shdr *shdr;
6980 	uint32_t shdr_status, shdr_add_status;
6981 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6982 
6983 	mb = &pmb->u.mb;
6984 
6985 	shdr = (union lpfc_sli4_cfg_shdr *)
6986 		&pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6987 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6988 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6989 
6990 	if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6991 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6992 				"6188 FW LOG mailbox "
6993 				"completed with status x%x add_status x%x,"
6994 				" mbx status x%x\n",
6995 				shdr_status, shdr_add_status, mb->mbxStatus);
6996 
6997 		ras_fwlog->ras_hwsupport = false;
6998 		goto disable_ras;
6999 	}
7000 
7001 	spin_lock_irq(&phba->ras_fwlog_lock);
7002 	ras_fwlog->state = ACTIVE;
7003 	spin_unlock_irq(&phba->ras_fwlog_lock);
7004 	mempool_free(pmb, phba->mbox_mem_pool);
7005 
7006 	return;
7007 
7008 disable_ras:
7009 	/* Free RAS DMA memory */
7010 	lpfc_sli4_ras_dma_free(phba);
7011 	mempool_free(pmb, phba->mbox_mem_pool);
7012 }
7013 
7014 /**
7015  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
7016  * @phba: pointer to lpfc hba data structure.
7017  * @fwlog_level: Logging verbosity level.
7018  * @fwlog_enable: Enable/Disable logging.
7019  *
7020  * Initialize memory and post mailbox command to enable FW logging in host
7021  * memory.
7022  **/
7023 int
lpfc_sli4_ras_fwlog_init(struct lpfc_hba * phba,uint32_t fwlog_level,uint32_t fwlog_enable)7024 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
7025 			 uint32_t fwlog_level,
7026 			 uint32_t fwlog_enable)
7027 {
7028 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
7029 	struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
7030 	struct lpfc_dmabuf *dmabuf;
7031 	LPFC_MBOXQ_t *mbox;
7032 	uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
7033 	int rc = 0;
7034 
7035 	spin_lock_irq(&phba->ras_fwlog_lock);
7036 	ras_fwlog->state = INACTIVE;
7037 	spin_unlock_irq(&phba->ras_fwlog_lock);
7038 
7039 	fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
7040 			  phba->cfg_ras_fwlog_buffsize);
7041 	fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
7042 
7043 	/*
7044 	 * If re-enabling FW logging support use earlier allocated
7045 	 * DMA buffers while posting MBX command.
7046 	 **/
7047 	if (!ras_fwlog->lwpd.virt) {
7048 		rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
7049 		if (rc) {
7050 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7051 					"6189 FW Log Memory Allocation Failed");
7052 			return rc;
7053 		}
7054 	}
7055 
7056 	/* Setup Mailbox command */
7057 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7058 	if (!mbox) {
7059 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7060 				"6190 RAS MBX Alloc Failed");
7061 		rc = -ENOMEM;
7062 		goto mem_free;
7063 	}
7064 
7065 	ras_fwlog->fw_loglevel = fwlog_level;
7066 	len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
7067 		sizeof(struct lpfc_sli4_cfg_mhdr));
7068 
7069 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
7070 			 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
7071 			 len, LPFC_SLI4_MBX_EMBED);
7072 
7073 	mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
7074 	bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
7075 	       fwlog_enable);
7076 	bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
7077 	       ras_fwlog->fw_loglevel);
7078 	bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
7079 	       ras_fwlog->fw_buffcount);
7080 	bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
7081 	       LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
7082 
7083 	/* Update DMA buffer address */
7084 	list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
7085 		memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
7086 
7087 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
7088 			putPaddrLow(dmabuf->phys);
7089 
7090 		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
7091 			putPaddrHigh(dmabuf->phys);
7092 	}
7093 
7094 	/* Update LPWD address */
7095 	mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7096 	mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7097 
7098 	spin_lock_irq(&phba->ras_fwlog_lock);
7099 	ras_fwlog->state = REG_INPROGRESS;
7100 	spin_unlock_irq(&phba->ras_fwlog_lock);
7101 	mbox->vport = phba->pport;
7102 	mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7103 
7104 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7105 
7106 	if (rc == MBX_NOT_FINISHED) {
7107 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7108 				"6191 FW-Log Mailbox failed. "
7109 				"status %d mbxStatus : x%x", rc,
7110 				bf_get(lpfc_mqe_status, &mbox->u.mqe));
7111 		mempool_free(mbox, phba->mbox_mem_pool);
7112 		rc = -EIO;
7113 		goto mem_free;
7114 	} else
7115 		rc = 0;
7116 mem_free:
7117 	if (rc)
7118 		lpfc_sli4_ras_dma_free(phba);
7119 
7120 	return rc;
7121 }
7122 
7123 /**
7124  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7125  * @phba: Pointer to HBA context object.
7126  *
7127  * Check if RAS is supported on the adapter and initialize it.
7128  **/
7129 void
lpfc_sli4_ras_setup(struct lpfc_hba * phba)7130 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7131 {
7132 	/* Check RAS FW Log needs to be enabled or not */
7133 	if (lpfc_check_fwlog_support(phba))
7134 		return;
7135 
7136 	lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7137 				 LPFC_RAS_ENABLE_LOGGING);
7138 }
7139 
7140 /**
7141  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7142  * @phba: Pointer to HBA context object.
7143  *
7144  * This function allocates all SLI4 resource identifiers.
7145  **/
7146 int
lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba * phba)7147 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7148 {
7149 	int i, rc, error = 0;
7150 	uint16_t count, base;
7151 	unsigned long longs;
7152 
7153 	if (!phba->sli4_hba.rpi_hdrs_in_use)
7154 		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
7155 	if (phba->sli4_hba.extents_in_use) {
7156 		/*
7157 		 * The port supports resource extents. The XRI, VPI, VFI, RPI
7158 		 * resource extent count must be read and allocated before
7159 		 * provisioning the resource id arrays.
7160 		 */
7161 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7162 		    LPFC_IDX_RSRC_RDY) {
7163 			/*
7164 			 * Extent-based resources are set - the driver could
7165 			 * be in a port reset. Figure out if any corrective
7166 			 * actions need to be taken.
7167 			 */
7168 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7169 						 LPFC_RSC_TYPE_FCOE_VFI);
7170 			if (rc != 0)
7171 				error++;
7172 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7173 						 LPFC_RSC_TYPE_FCOE_VPI);
7174 			if (rc != 0)
7175 				error++;
7176 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7177 						 LPFC_RSC_TYPE_FCOE_XRI);
7178 			if (rc != 0)
7179 				error++;
7180 			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7181 						 LPFC_RSC_TYPE_FCOE_RPI);
7182 			if (rc != 0)
7183 				error++;
7184 
7185 			/*
7186 			 * It's possible that the number of resources
7187 			 * provided to this port instance changed between
7188 			 * resets.  Detect this condition and reallocate
7189 			 * resources.  Otherwise, there is no action.
7190 			 */
7191 			if (error) {
7192 				lpfc_printf_log(phba, KERN_INFO,
7193 						LOG_MBOX | LOG_INIT,
7194 						"2931 Detected extent resource "
7195 						"change.  Reallocating all "
7196 						"extents.\n");
7197 				rc = lpfc_sli4_dealloc_extent(phba,
7198 						 LPFC_RSC_TYPE_FCOE_VFI);
7199 				rc = lpfc_sli4_dealloc_extent(phba,
7200 						 LPFC_RSC_TYPE_FCOE_VPI);
7201 				rc = lpfc_sli4_dealloc_extent(phba,
7202 						 LPFC_RSC_TYPE_FCOE_XRI);
7203 				rc = lpfc_sli4_dealloc_extent(phba,
7204 						 LPFC_RSC_TYPE_FCOE_RPI);
7205 			} else
7206 				return 0;
7207 		}
7208 
7209 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7210 		if (unlikely(rc))
7211 			goto err_exit;
7212 
7213 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7214 		if (unlikely(rc))
7215 			goto err_exit;
7216 
7217 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7218 		if (unlikely(rc))
7219 			goto err_exit;
7220 
7221 		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7222 		if (unlikely(rc))
7223 			goto err_exit;
7224 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7225 		       LPFC_IDX_RSRC_RDY);
7226 		return rc;
7227 	} else {
7228 		/*
7229 		 * The port does not support resource extents.  The XRI, VPI,
7230 		 * VFI, RPI resource ids were determined from READ_CONFIG.
7231 		 * Just allocate the bitmasks and provision the resource id
7232 		 * arrays.  If a port reset is active, the resources don't
7233 		 * need any action - just exit.
7234 		 */
7235 		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7236 		    LPFC_IDX_RSRC_RDY) {
7237 			lpfc_sli4_dealloc_resource_identifiers(phba);
7238 			lpfc_sli4_remove_rpis(phba);
7239 		}
7240 		/* RPIs. */
7241 		count = phba->sli4_hba.max_cfg_param.max_rpi;
7242 		if (count <= 0) {
7243 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7244 					"3279 Invalid provisioning of "
7245 					"rpi:%d\n", count);
7246 			rc = -EINVAL;
7247 			goto err_exit;
7248 		}
7249 		base = phba->sli4_hba.max_cfg_param.rpi_base;
7250 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7251 		phba->sli4_hba.rpi_bmask = kcalloc(longs,
7252 						   sizeof(unsigned long),
7253 						   GFP_KERNEL);
7254 		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7255 			rc = -ENOMEM;
7256 			goto err_exit;
7257 		}
7258 		phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
7259 						 GFP_KERNEL);
7260 		if (unlikely(!phba->sli4_hba.rpi_ids)) {
7261 			rc = -ENOMEM;
7262 			goto free_rpi_bmask;
7263 		}
7264 
7265 		for (i = 0; i < count; i++)
7266 			phba->sli4_hba.rpi_ids[i] = base + i;
7267 
7268 		/* VPIs. */
7269 		count = phba->sli4_hba.max_cfg_param.max_vpi;
7270 		if (count <= 0) {
7271 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7272 					"3280 Invalid provisioning of "
7273 					"vpi:%d\n", count);
7274 			rc = -EINVAL;
7275 			goto free_rpi_ids;
7276 		}
7277 		base = phba->sli4_hba.max_cfg_param.vpi_base;
7278 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7279 		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
7280 					  GFP_KERNEL);
7281 		if (unlikely(!phba->vpi_bmask)) {
7282 			rc = -ENOMEM;
7283 			goto free_rpi_ids;
7284 		}
7285 		phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
7286 					GFP_KERNEL);
7287 		if (unlikely(!phba->vpi_ids)) {
7288 			rc = -ENOMEM;
7289 			goto free_vpi_bmask;
7290 		}
7291 
7292 		for (i = 0; i < count; i++)
7293 			phba->vpi_ids[i] = base + i;
7294 
7295 		/* XRIs. */
7296 		count = phba->sli4_hba.max_cfg_param.max_xri;
7297 		if (count <= 0) {
7298 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7299 					"3281 Invalid provisioning of "
7300 					"xri:%d\n", count);
7301 			rc = -EINVAL;
7302 			goto free_vpi_ids;
7303 		}
7304 		base = phba->sli4_hba.max_cfg_param.xri_base;
7305 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7306 		phba->sli4_hba.xri_bmask = kcalloc(longs,
7307 						   sizeof(unsigned long),
7308 						   GFP_KERNEL);
7309 		if (unlikely(!phba->sli4_hba.xri_bmask)) {
7310 			rc = -ENOMEM;
7311 			goto free_vpi_ids;
7312 		}
7313 		phba->sli4_hba.max_cfg_param.xri_used = 0;
7314 		phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
7315 						 GFP_KERNEL);
7316 		if (unlikely(!phba->sli4_hba.xri_ids)) {
7317 			rc = -ENOMEM;
7318 			goto free_xri_bmask;
7319 		}
7320 
7321 		for (i = 0; i < count; i++)
7322 			phba->sli4_hba.xri_ids[i] = base + i;
7323 
7324 		/* VFIs. */
7325 		count = phba->sli4_hba.max_cfg_param.max_vfi;
7326 		if (count <= 0) {
7327 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7328 					"3282 Invalid provisioning of "
7329 					"vfi:%d\n", count);
7330 			rc = -EINVAL;
7331 			goto free_xri_ids;
7332 		}
7333 		base = phba->sli4_hba.max_cfg_param.vfi_base;
7334 		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7335 		phba->sli4_hba.vfi_bmask = kcalloc(longs,
7336 						   sizeof(unsigned long),
7337 						   GFP_KERNEL);
7338 		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7339 			rc = -ENOMEM;
7340 			goto free_xri_ids;
7341 		}
7342 		phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
7343 						 GFP_KERNEL);
7344 		if (unlikely(!phba->sli4_hba.vfi_ids)) {
7345 			rc = -ENOMEM;
7346 			goto free_vfi_bmask;
7347 		}
7348 
7349 		for (i = 0; i < count; i++)
7350 			phba->sli4_hba.vfi_ids[i] = base + i;
7351 
7352 		/*
7353 		 * Mark all resources ready.  An HBA reset doesn't need
7354 		 * to reset the initialization.
7355 		 */
7356 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7357 		       LPFC_IDX_RSRC_RDY);
7358 		return 0;
7359 	}
7360 
7361  free_vfi_bmask:
7362 	kfree(phba->sli4_hba.vfi_bmask);
7363 	phba->sli4_hba.vfi_bmask = NULL;
7364  free_xri_ids:
7365 	kfree(phba->sli4_hba.xri_ids);
7366 	phba->sli4_hba.xri_ids = NULL;
7367  free_xri_bmask:
7368 	kfree(phba->sli4_hba.xri_bmask);
7369 	phba->sli4_hba.xri_bmask = NULL;
7370  free_vpi_ids:
7371 	kfree(phba->vpi_ids);
7372 	phba->vpi_ids = NULL;
7373  free_vpi_bmask:
7374 	kfree(phba->vpi_bmask);
7375 	phba->vpi_bmask = NULL;
7376  free_rpi_ids:
7377 	kfree(phba->sli4_hba.rpi_ids);
7378 	phba->sli4_hba.rpi_ids = NULL;
7379  free_rpi_bmask:
7380 	kfree(phba->sli4_hba.rpi_bmask);
7381 	phba->sli4_hba.rpi_bmask = NULL;
7382  err_exit:
7383 	return rc;
7384 }
7385 
7386 /**
7387  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7388  * @phba: Pointer to HBA context object.
7389  *
7390  * This function allocates the number of elements for the specified
7391  * resource type.
7392  **/
7393 int
lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba * phba)7394 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7395 {
7396 	if (phba->sli4_hba.extents_in_use) {
7397 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7398 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7399 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7400 		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7401 	} else {
7402 		kfree(phba->vpi_bmask);
7403 		phba->sli4_hba.max_cfg_param.vpi_used = 0;
7404 		kfree(phba->vpi_ids);
7405 		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7406 		kfree(phba->sli4_hba.xri_bmask);
7407 		kfree(phba->sli4_hba.xri_ids);
7408 		kfree(phba->sli4_hba.vfi_bmask);
7409 		kfree(phba->sli4_hba.vfi_ids);
7410 		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7411 		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7412 	}
7413 
7414 	return 0;
7415 }
7416 
7417 /**
7418  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7419  * @phba: Pointer to HBA context object.
7420  * @type: The resource extent type.
7421  * @extnt_cnt: buffer to hold port extent count response
7422  * @extnt_size: buffer to hold port extent size response.
7423  *
7424  * This function calls the port to read the host allocated extents
7425  * for a particular type.
7426  **/
7427 int
lpfc_sli4_get_allocated_extnts(struct lpfc_hba * phba,uint16_t type,uint16_t * extnt_cnt,uint16_t * extnt_size)7428 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7429 			       uint16_t *extnt_cnt, uint16_t *extnt_size)
7430 {
7431 	bool emb;
7432 	int rc = 0;
7433 	uint16_t curr_blks = 0;
7434 	uint32_t req_len, emb_len;
7435 	uint32_t alloc_len, mbox_tmo;
7436 	struct list_head *blk_list_head;
7437 	struct lpfc_rsrc_blks *rsrc_blk;
7438 	LPFC_MBOXQ_t *mbox;
7439 	void *virtaddr = NULL;
7440 	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7441 	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7442 	union  lpfc_sli4_cfg_shdr *shdr;
7443 
7444 	switch (type) {
7445 	case LPFC_RSC_TYPE_FCOE_VPI:
7446 		blk_list_head = &phba->lpfc_vpi_blk_list;
7447 		break;
7448 	case LPFC_RSC_TYPE_FCOE_XRI:
7449 		blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7450 		break;
7451 	case LPFC_RSC_TYPE_FCOE_VFI:
7452 		blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7453 		break;
7454 	case LPFC_RSC_TYPE_FCOE_RPI:
7455 		blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7456 		break;
7457 	default:
7458 		return -EIO;
7459 	}
7460 
7461 	/* Count the number of extents currently allocatd for this type. */
7462 	list_for_each_entry(rsrc_blk, blk_list_head, list) {
7463 		if (curr_blks == 0) {
7464 			/*
7465 			 * The GET_ALLOCATED mailbox does not return the size,
7466 			 * just the count.  The size should be just the size
7467 			 * stored in the current allocated block and all sizes
7468 			 * for an extent type are the same so set the return
7469 			 * value now.
7470 			 */
7471 			*extnt_size = rsrc_blk->rsrc_size;
7472 		}
7473 		curr_blks++;
7474 	}
7475 
7476 	/*
7477 	 * Calculate the size of an embedded mailbox.  The uint32_t
7478 	 * accounts for extents-specific word.
7479 	 */
7480 	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7481 		sizeof(uint32_t);
7482 
7483 	/*
7484 	 * Presume the allocation and response will fit into an embedded
7485 	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7486 	 */
7487 	emb = LPFC_SLI4_MBX_EMBED;
7488 	req_len = emb_len;
7489 	if (req_len > emb_len) {
7490 		req_len = curr_blks * sizeof(uint16_t) +
7491 			sizeof(union lpfc_sli4_cfg_shdr) +
7492 			sizeof(uint32_t);
7493 		emb = LPFC_SLI4_MBX_NEMBED;
7494 	}
7495 
7496 	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7497 	if (!mbox)
7498 		return -ENOMEM;
7499 	memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7500 
7501 	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7502 				     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7503 				     req_len, emb);
7504 	if (alloc_len < req_len) {
7505 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7506 			"2983 Allocated DMA memory size (x%x) is "
7507 			"less than the requested DMA memory "
7508 			"size (x%x)\n", alloc_len, req_len);
7509 		rc = -ENOMEM;
7510 		goto err_exit;
7511 	}
7512 	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7513 	if (unlikely(rc)) {
7514 		rc = -EIO;
7515 		goto err_exit;
7516 	}
7517 
7518 	if (!phba->sli4_hba.intr_enable)
7519 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7520 	else {
7521 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7522 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7523 	}
7524 
7525 	if (unlikely(rc)) {
7526 		rc = -EIO;
7527 		goto err_exit;
7528 	}
7529 
7530 	/*
7531 	 * Figure out where the response is located.  Then get local pointers
7532 	 * to the response data.  The port does not guarantee to respond to
7533 	 * all extents counts request so update the local variable with the
7534 	 * allocated count from the port.
7535 	 */
7536 	if (emb == LPFC_SLI4_MBX_EMBED) {
7537 		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7538 		shdr = &rsrc_ext->header.cfg_shdr;
7539 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7540 	} else {
7541 		virtaddr = mbox->sge_array->addr[0];
7542 		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7543 		shdr = &n_rsrc->cfg_shdr;
7544 		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7545 	}
7546 
7547 	if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7548 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7549 			"2984 Failed to read allocated resources "
7550 			"for type %d - Status 0x%x Add'l Status 0x%x.\n",
7551 			type,
7552 			bf_get(lpfc_mbox_hdr_status, &shdr->response),
7553 			bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7554 		rc = -EIO;
7555 		goto err_exit;
7556 	}
7557  err_exit:
7558 	lpfc_sli4_mbox_cmd_free(phba, mbox);
7559 	return rc;
7560 }
7561 
7562 /**
7563  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7564  * @phba: pointer to lpfc hba data structure.
7565  * @sgl_list: linked link of sgl buffers to post
7566  * @cnt: number of linked list buffers
7567  *
7568  * This routine walks the list of buffers that have been allocated and
7569  * repost them to the port by using SGL block post. This is needed after a
7570  * pci_function_reset/warm_start or start. It attempts to construct blocks
7571  * of buffer sgls which contains contiguous xris and uses the non-embedded
7572  * SGL block post mailbox commands to post them to the port. For single
7573  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7574  * mailbox command for posting.
7575  *
7576  * Returns: 0 = success, non-zero failure.
7577  **/
7578 static int
lpfc_sli4_repost_sgl_list(struct lpfc_hba * phba,struct list_head * sgl_list,int cnt)7579 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7580 			  struct list_head *sgl_list, int cnt)
7581 {
7582 	struct lpfc_sglq *sglq_entry = NULL;
7583 	struct lpfc_sglq *sglq_entry_next = NULL;
7584 	struct lpfc_sglq *sglq_entry_first = NULL;
7585 	int status = 0, total_cnt;
7586 	int post_cnt = 0, num_posted = 0, block_cnt = 0;
7587 	int last_xritag = NO_XRI;
7588 	LIST_HEAD(prep_sgl_list);
7589 	LIST_HEAD(blck_sgl_list);
7590 	LIST_HEAD(allc_sgl_list);
7591 	LIST_HEAD(post_sgl_list);
7592 	LIST_HEAD(free_sgl_list);
7593 
7594 	spin_lock_irq(&phba->hbalock);
7595 	spin_lock(&phba->sli4_hba.sgl_list_lock);
7596 	list_splice_init(sgl_list, &allc_sgl_list);
7597 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
7598 	spin_unlock_irq(&phba->hbalock);
7599 
7600 	total_cnt = cnt;
7601 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7602 				 &allc_sgl_list, list) {
7603 		list_del_init(&sglq_entry->list);
7604 		block_cnt++;
7605 		if ((last_xritag != NO_XRI) &&
7606 		    (sglq_entry->sli4_xritag != last_xritag + 1)) {
7607 			/* a hole in xri block, form a sgl posting block */
7608 			list_splice_init(&prep_sgl_list, &blck_sgl_list);
7609 			post_cnt = block_cnt - 1;
7610 			/* prepare list for next posting block */
7611 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7612 			block_cnt = 1;
7613 		} else {
7614 			/* prepare list for next posting block */
7615 			list_add_tail(&sglq_entry->list, &prep_sgl_list);
7616 			/* enough sgls for non-embed sgl mbox command */
7617 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7618 				list_splice_init(&prep_sgl_list,
7619 						 &blck_sgl_list);
7620 				post_cnt = block_cnt;
7621 				block_cnt = 0;
7622 			}
7623 		}
7624 		num_posted++;
7625 
7626 		/* keep track of last sgl's xritag */
7627 		last_xritag = sglq_entry->sli4_xritag;
7628 
7629 		/* end of repost sgl list condition for buffers */
7630 		if (num_posted == total_cnt) {
7631 			if (post_cnt == 0) {
7632 				list_splice_init(&prep_sgl_list,
7633 						 &blck_sgl_list);
7634 				post_cnt = block_cnt;
7635 			} else if (block_cnt == 1) {
7636 				status = lpfc_sli4_post_sgl(phba,
7637 						sglq_entry->phys, 0,
7638 						sglq_entry->sli4_xritag);
7639 				if (!status) {
7640 					/* successful, put sgl to posted list */
7641 					list_add_tail(&sglq_entry->list,
7642 						      &post_sgl_list);
7643 				} else {
7644 					/* Failure, put sgl to free list */
7645 					lpfc_printf_log(phba, KERN_WARNING,
7646 						LOG_SLI,
7647 						"3159 Failed to post "
7648 						"sgl, xritag:x%x\n",
7649 						sglq_entry->sli4_xritag);
7650 					list_add_tail(&sglq_entry->list,
7651 						      &free_sgl_list);
7652 					total_cnt--;
7653 				}
7654 			}
7655 		}
7656 
7657 		/* continue until a nembed page worth of sgls */
7658 		if (post_cnt == 0)
7659 			continue;
7660 
7661 		/* post the buffer list sgls as a block */
7662 		status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7663 						 post_cnt);
7664 
7665 		if (!status) {
7666 			/* success, put sgl list to posted sgl list */
7667 			list_splice_init(&blck_sgl_list, &post_sgl_list);
7668 		} else {
7669 			/* Failure, put sgl list to free sgl list */
7670 			sglq_entry_first = list_first_entry(&blck_sgl_list,
7671 							    struct lpfc_sglq,
7672 							    list);
7673 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7674 					"3160 Failed to post sgl-list, "
7675 					"xritag:x%x-x%x\n",
7676 					sglq_entry_first->sli4_xritag,
7677 					(sglq_entry_first->sli4_xritag +
7678 					 post_cnt - 1));
7679 			list_splice_init(&blck_sgl_list, &free_sgl_list);
7680 			total_cnt -= post_cnt;
7681 		}
7682 
7683 		/* don't reset xirtag due to hole in xri block */
7684 		if (block_cnt == 0)
7685 			last_xritag = NO_XRI;
7686 
7687 		/* reset sgl post count for next round of posting */
7688 		post_cnt = 0;
7689 	}
7690 
7691 	/* free the sgls failed to post */
7692 	lpfc_free_sgl_list(phba, &free_sgl_list);
7693 
7694 	/* push sgls posted to the available list */
7695 	if (!list_empty(&post_sgl_list)) {
7696 		spin_lock_irq(&phba->hbalock);
7697 		spin_lock(&phba->sli4_hba.sgl_list_lock);
7698 		list_splice_init(&post_sgl_list, sgl_list);
7699 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
7700 		spin_unlock_irq(&phba->hbalock);
7701 	} else {
7702 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7703 				"3161 Failure to post sgl to port,status %x "
7704 				"blkcnt %d totalcnt %d postcnt %d\n",
7705 				status, block_cnt, total_cnt, post_cnt);
7706 		return -EIO;
7707 	}
7708 
7709 	/* return the number of XRIs actually posted */
7710 	return total_cnt;
7711 }
7712 
7713 /**
7714  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7715  * @phba: pointer to lpfc hba data structure.
7716  *
7717  * This routine walks the list of nvme buffers that have been allocated and
7718  * repost them to the port by using SGL block post. This is needed after a
7719  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7720  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7721  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7722  *
7723  * Returns: 0 = success, non-zero failure.
7724  **/
7725 static int
lpfc_sli4_repost_io_sgl_list(struct lpfc_hba * phba)7726 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7727 {
7728 	LIST_HEAD(post_nblist);
7729 	int num_posted, rc = 0;
7730 
7731 	/* get all NVME buffers need to repost to a local list */
7732 	lpfc_io_buf_flush(phba, &post_nblist);
7733 
7734 	/* post the list of nvme buffer sgls to port if available */
7735 	if (!list_empty(&post_nblist)) {
7736 		num_posted = lpfc_sli4_post_io_sgl_list(
7737 			phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7738 		/* failed to post any nvme buffer, return error */
7739 		if (num_posted == 0)
7740 			rc = -EIO;
7741 	}
7742 	return rc;
7743 }
7744 
7745 static void
lpfc_set_host_data(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)7746 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7747 {
7748 	uint32_t len;
7749 
7750 	len = sizeof(struct lpfc_mbx_set_host_data) -
7751 		sizeof(struct lpfc_sli4_cfg_mhdr);
7752 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7753 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7754 			 LPFC_SLI4_MBX_EMBED);
7755 
7756 	mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7757 	mbox->u.mqe.un.set_host_data.param_len =
7758 					LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7759 	snprintf(mbox->u.mqe.un.set_host_data.un.data,
7760 		 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7761 		 "Linux %s v"LPFC_DRIVER_VERSION,
7762 		 test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? "FCoE" : "FC");
7763 }
7764 
7765 int
lpfc_post_rq_buffer(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,int count,int idx)7766 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7767 		    struct lpfc_queue *drq, int count, int idx)
7768 {
7769 	int rc, i;
7770 	struct lpfc_rqe hrqe;
7771 	struct lpfc_rqe drqe;
7772 	struct lpfc_rqb *rqbp;
7773 	unsigned long flags;
7774 	struct rqb_dmabuf *rqb_buffer;
7775 	LIST_HEAD(rqb_buf_list);
7776 
7777 	rqbp = hrq->rqbp;
7778 	for (i = 0; i < count; i++) {
7779 		spin_lock_irqsave(&phba->hbalock, flags);
7780 		/* IF RQ is already full, don't bother */
7781 		if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7782 			spin_unlock_irqrestore(&phba->hbalock, flags);
7783 			break;
7784 		}
7785 		spin_unlock_irqrestore(&phba->hbalock, flags);
7786 
7787 		rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7788 		if (!rqb_buffer)
7789 			break;
7790 		rqb_buffer->hrq = hrq;
7791 		rqb_buffer->drq = drq;
7792 		rqb_buffer->idx = idx;
7793 		list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7794 	}
7795 
7796 	spin_lock_irqsave(&phba->hbalock, flags);
7797 	while (!list_empty(&rqb_buf_list)) {
7798 		list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7799 				 hbuf.list);
7800 
7801 		hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7802 		hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7803 		drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7804 		drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7805 		rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7806 		if (rc < 0) {
7807 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7808 					"6421 Cannot post to HRQ %d: %x %x %x "
7809 					"DRQ %x %x\n",
7810 					hrq->queue_id,
7811 					hrq->host_index,
7812 					hrq->hba_index,
7813 					hrq->entry_count,
7814 					drq->host_index,
7815 					drq->hba_index);
7816 			rqbp->rqb_free_buffer(phba, rqb_buffer);
7817 		} else {
7818 			list_add_tail(&rqb_buffer->hbuf.list,
7819 				      &rqbp->rqb_buffer_list);
7820 			rqbp->buffer_count++;
7821 		}
7822 	}
7823 	spin_unlock_irqrestore(&phba->hbalock, flags);
7824 	return 1;
7825 }
7826 
7827 static void
lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7828 lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7829 {
7830 	union lpfc_sli4_cfg_shdr *shdr;
7831 	u32 shdr_status, shdr_add_status;
7832 
7833 	shdr = (union lpfc_sli4_cfg_shdr *)
7834 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7835 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7836 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7837 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7838 		lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT | LOG_MBOX,
7839 				"4622 SET_FEATURE (x%x) mbox failed, "
7840 				"status x%x add_status x%x, mbx status x%x\n",
7841 				LPFC_SET_LD_SIGNAL, shdr_status,
7842 				shdr_add_status, pmb->u.mb.mbxStatus);
7843 		phba->degrade_activate_threshold = 0;
7844 		phba->degrade_deactivate_threshold = 0;
7845 		phba->fec_degrade_interval = 0;
7846 		goto out;
7847 	}
7848 
7849 	phba->degrade_activate_threshold = pmb->u.mqe.un.set_feature.word7;
7850 	phba->degrade_deactivate_threshold = pmb->u.mqe.un.set_feature.word8;
7851 	phba->fec_degrade_interval = pmb->u.mqe.un.set_feature.word10;
7852 
7853 	lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT,
7854 			"4624 Success: da x%x dd x%x interval x%x\n",
7855 			phba->degrade_activate_threshold,
7856 			phba->degrade_deactivate_threshold,
7857 			phba->fec_degrade_interval);
7858 out:
7859 	mempool_free(pmb, phba->mbox_mem_pool);
7860 }
7861 
7862 int
lpfc_read_lds_params(struct lpfc_hba * phba)7863 lpfc_read_lds_params(struct lpfc_hba *phba)
7864 {
7865 	LPFC_MBOXQ_t *mboxq;
7866 	int rc;
7867 
7868 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7869 	if (!mboxq)
7870 		return -ENOMEM;
7871 
7872 	lpfc_set_features(phba, mboxq, LPFC_SET_LD_SIGNAL);
7873 	mboxq->vport = phba->pport;
7874 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_lds_params;
7875 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7876 	if (rc == MBX_NOT_FINISHED) {
7877 		mempool_free(mboxq, phba->mbox_mem_pool);
7878 		return -EIO;
7879 	}
7880 	return 0;
7881 }
7882 
7883 static void
lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7884 lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7885 {
7886 	struct lpfc_vport *vport = pmb->vport;
7887 	union lpfc_sli4_cfg_shdr *shdr;
7888 	u32 shdr_status, shdr_add_status;
7889 	u32 sig, acqe;
7890 
7891 	/* Two outcomes. (1) Set featurs was successul and EDC negotiation
7892 	 * is done. (2) Mailbox failed and send FPIN support only.
7893 	 */
7894 	shdr = (union lpfc_sli4_cfg_shdr *)
7895 		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7896 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7897 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7898 	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7899 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7900 				"2516 CGN SET_FEATURE mbox failed with "
7901 				"status x%x add_status x%x, mbx status x%x "
7902 				"Reset Congestion to FPINs only\n",
7903 				shdr_status, shdr_add_status,
7904 				pmb->u.mb.mbxStatus);
7905 		/* If there is a mbox error, move on to RDF */
7906 		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7907 		phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7908 		goto out;
7909 	}
7910 
7911 	/* Zero out Congestion Signal ACQE counter */
7912 	phba->cgn_acqe_cnt = 0;
7913 
7914 	acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7915 		      &pmb->u.mqe.un.set_feature);
7916 	sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7917 		     &pmb->u.mqe.un.set_feature);
7918 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7919 			"4620 SET_FEATURES Success: Freq: %ds %dms "
7920 			" Reg: x%x x%x\n", acqe, sig,
7921 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7922 out:
7923 	mempool_free(pmb, phba->mbox_mem_pool);
7924 
7925 	/* Register for FPIN events from the fabric now that the
7926 	 * EDC common_set_features has completed.
7927 	 */
7928 	lpfc_issue_els_rdf(vport, 0);
7929 }
7930 
7931 int
lpfc_config_cgn_signal(struct lpfc_hba * phba)7932 lpfc_config_cgn_signal(struct lpfc_hba *phba)
7933 {
7934 	LPFC_MBOXQ_t *mboxq;
7935 	u32 rc;
7936 
7937 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7938 	if (!mboxq)
7939 		goto out_rdf;
7940 
7941 	lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7942 	mboxq->vport = phba->pport;
7943 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7944 
7945 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7946 			"4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7947 			"Reg: x%x x%x\n",
7948 			phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7949 			phba->cgn_reg_signal, phba->cgn_reg_fpin);
7950 
7951 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7952 	if (rc == MBX_NOT_FINISHED)
7953 		goto out;
7954 	return 0;
7955 
7956 out:
7957 	mempool_free(mboxq, phba->mbox_mem_pool);
7958 out_rdf:
7959 	/* If there is a mbox error, move on to RDF */
7960 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7961 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7962 	lpfc_issue_els_rdf(phba->pport, 0);
7963 	return -EIO;
7964 }
7965 
7966 /**
7967  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7968  * @phba: pointer to lpfc hba data structure.
7969  *
7970  * This routine initializes the per-eq idle_stat to dynamically dictate
7971  * polling decisions.
7972  *
7973  * Return codes:
7974  *   None
7975  **/
lpfc_init_idle_stat_hb(struct lpfc_hba * phba)7976 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7977 {
7978 	int i;
7979 	struct lpfc_sli4_hdw_queue *hdwq;
7980 	struct lpfc_queue *eq;
7981 	struct lpfc_idle_stat *idle_stat;
7982 	u64 wall;
7983 
7984 	for_each_present_cpu(i) {
7985 		hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7986 		eq = hdwq->hba_eq;
7987 
7988 		/* Skip if we've already handled this eq's primary CPU */
7989 		if (eq->chann != i)
7990 			continue;
7991 
7992 		idle_stat = &phba->sli4_hba.idle_stat[i];
7993 
7994 		idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7995 		idle_stat->prev_wall = wall;
7996 
7997 		if (phba->nvmet_support ||
7998 		    phba->cmf_active_mode != LPFC_CFG_OFF ||
7999 		    phba->intr_type != MSIX)
8000 			eq->poll_mode = LPFC_QUEUE_WORK;
8001 		else
8002 			eq->poll_mode = LPFC_THREADED_IRQ;
8003 	}
8004 
8005 	if (!phba->nvmet_support && phba->intr_type == MSIX)
8006 		schedule_delayed_work(&phba->idle_stat_delay_work,
8007 				      msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
8008 }
8009 
lpfc_sli4_dip(struct lpfc_hba * phba)8010 static void lpfc_sli4_dip(struct lpfc_hba *phba)
8011 {
8012 	uint32_t if_type;
8013 
8014 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8015 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
8016 	    if_type == LPFC_SLI_INTF_IF_TYPE_6) {
8017 		struct lpfc_register reg_data;
8018 
8019 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8020 			       &reg_data.word0))
8021 			return;
8022 
8023 		if (bf_get(lpfc_sliport_status_dip, &reg_data))
8024 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8025 					"2904 Firmware Dump Image Present"
8026 					" on Adapter");
8027 	}
8028 }
8029 
8030 /**
8031  * lpfc_rx_monitor_create_ring - Initialize ring buffer for rx_monitor
8032  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8033  * @entries: Number of rx_info_entry objects to allocate in ring
8034  *
8035  * Return:
8036  * 0 - Success
8037  * ENOMEM - Failure to kmalloc
8038  **/
lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor * rx_monitor,u32 entries)8039 int lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor *rx_monitor,
8040 				u32 entries)
8041 {
8042 	rx_monitor->ring = kmalloc_array(entries, sizeof(struct rx_info_entry),
8043 					 GFP_KERNEL);
8044 	if (!rx_monitor->ring)
8045 		return -ENOMEM;
8046 
8047 	rx_monitor->head_idx = 0;
8048 	rx_monitor->tail_idx = 0;
8049 	spin_lock_init(&rx_monitor->lock);
8050 	rx_monitor->entries = entries;
8051 
8052 	return 0;
8053 }
8054 
8055 /**
8056  * lpfc_rx_monitor_destroy_ring - Free ring buffer for rx_monitor
8057  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8058  *
8059  * Called after cancellation of cmf_timer.
8060  **/
lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor * rx_monitor)8061 void lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor *rx_monitor)
8062 {
8063 	kfree(rx_monitor->ring);
8064 	rx_monitor->ring = NULL;
8065 	rx_monitor->entries = 0;
8066 	rx_monitor->head_idx = 0;
8067 	rx_monitor->tail_idx = 0;
8068 }
8069 
8070 /**
8071  * lpfc_rx_monitor_record - Insert an entry into rx_monitor's ring
8072  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8073  * @entry: Pointer to rx_info_entry
8074  *
8075  * Used to insert an rx_info_entry into rx_monitor's ring.  Note that this is a
8076  * deep copy of rx_info_entry not a shallow copy of the rx_info_entry ptr.
8077  *
8078  * This is called from lpfc_cmf_timer, which is in timer/softirq context.
8079  *
8080  * In cases of old data overflow, we do a best effort of FIFO order.
8081  **/
lpfc_rx_monitor_record(struct lpfc_rx_info_monitor * rx_monitor,struct rx_info_entry * entry)8082 void lpfc_rx_monitor_record(struct lpfc_rx_info_monitor *rx_monitor,
8083 			    struct rx_info_entry *entry)
8084 {
8085 	struct rx_info_entry *ring = rx_monitor->ring;
8086 	u32 *head_idx = &rx_monitor->head_idx;
8087 	u32 *tail_idx = &rx_monitor->tail_idx;
8088 	spinlock_t *ring_lock = &rx_monitor->lock;
8089 	u32 ring_size = rx_monitor->entries;
8090 
8091 	spin_lock(ring_lock);
8092 	memcpy(&ring[*tail_idx], entry, sizeof(*entry));
8093 	*tail_idx = (*tail_idx + 1) % ring_size;
8094 
8095 	/* Best effort of FIFO saved data */
8096 	if (*tail_idx == *head_idx)
8097 		*head_idx = (*head_idx + 1) % ring_size;
8098 
8099 	spin_unlock(ring_lock);
8100 }
8101 
8102 /**
8103  * lpfc_rx_monitor_report - Read out rx_monitor's ring
8104  * @phba: Pointer to lpfc_hba object
8105  * @rx_monitor: Pointer to lpfc_rx_info_monitor object
8106  * @buf: Pointer to char buffer that will contain rx monitor info data
8107  * @buf_len: Length buf including null char
8108  * @max_read_entries: Maximum number of entries to read out of ring
8109  *
8110  * Used to dump/read what's in rx_monitor's ring buffer.
8111  *
8112  * If buf is NULL || buf_len == 0, then it is implied that we want to log the
8113  * information to kmsg instead of filling out buf.
8114  *
8115  * Return:
8116  * Number of entries read out of the ring
8117  **/
lpfc_rx_monitor_report(struct lpfc_hba * phba,struct lpfc_rx_info_monitor * rx_monitor,char * buf,u32 buf_len,u32 max_read_entries)8118 u32 lpfc_rx_monitor_report(struct lpfc_hba *phba,
8119 			   struct lpfc_rx_info_monitor *rx_monitor, char *buf,
8120 			   u32 buf_len, u32 max_read_entries)
8121 {
8122 	struct rx_info_entry *ring = rx_monitor->ring;
8123 	struct rx_info_entry *entry;
8124 	u32 *head_idx = &rx_monitor->head_idx;
8125 	u32 *tail_idx = &rx_monitor->tail_idx;
8126 	spinlock_t *ring_lock = &rx_monitor->lock;
8127 	u32 ring_size = rx_monitor->entries;
8128 	u32 cnt = 0;
8129 	char tmp[DBG_LOG_STR_SZ] = {0};
8130 	bool log_to_kmsg = (!buf || !buf_len) ? true : false;
8131 
8132 	if (!log_to_kmsg) {
8133 		/* clear the buffer to be sure */
8134 		memset(buf, 0, buf_len);
8135 
8136 		scnprintf(buf, buf_len, "\t%-16s%-16s%-16s%-16s%-8s%-8s%-8s"
8137 					"%-8s%-8s%-8s%-16s\n",
8138 					"MaxBPI", "Tot_Data_CMF",
8139 					"Tot_Data_Cmd", "Tot_Data_Cmpl",
8140 					"Lat(us)", "Avg_IO", "Max_IO", "Bsy",
8141 					"IO_cnt", "Info", "BWutil(ms)");
8142 	}
8143 
8144 	/* Needs to be _irq because record is called from timer interrupt
8145 	 * context
8146 	 */
8147 	spin_lock_irq(ring_lock);
8148 	while (*head_idx != *tail_idx) {
8149 		entry = &ring[*head_idx];
8150 
8151 		/* Read out this entry's data. */
8152 		if (!log_to_kmsg) {
8153 			/* If !log_to_kmsg, then store to buf. */
8154 			scnprintf(tmp, sizeof(tmp),
8155 				  "%03d:\t%-16llu%-16llu%-16llu%-16llu%-8llu"
8156 				  "%-8llu%-8llu%-8u%-8u%-8u%u(%u)\n",
8157 				  *head_idx, entry->max_bytes_per_interval,
8158 				  entry->cmf_bytes, entry->total_bytes,
8159 				  entry->rcv_bytes, entry->avg_io_latency,
8160 				  entry->avg_io_size, entry->max_read_cnt,
8161 				  entry->cmf_busy, entry->io_cnt,
8162 				  entry->cmf_info, entry->timer_utilization,
8163 				  entry->timer_interval);
8164 
8165 			/* Check for buffer overflow */
8166 			if ((strlen(buf) + strlen(tmp)) >= buf_len)
8167 				break;
8168 
8169 			/* Append entry's data to buffer */
8170 			strlcat(buf, tmp, buf_len);
8171 		} else {
8172 			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
8173 					"4410 %02u: MBPI %llu Xmit %llu "
8174 					"Cmpl %llu Lat %llu ASz %llu Info %02u "
8175 					"BWUtil %u Int %u slot %u\n",
8176 					cnt, entry->max_bytes_per_interval,
8177 					entry->total_bytes, entry->rcv_bytes,
8178 					entry->avg_io_latency,
8179 					entry->avg_io_size, entry->cmf_info,
8180 					entry->timer_utilization,
8181 					entry->timer_interval, *head_idx);
8182 		}
8183 
8184 		*head_idx = (*head_idx + 1) % ring_size;
8185 
8186 		/* Don't feed more than max_read_entries */
8187 		cnt++;
8188 		if (cnt >= max_read_entries)
8189 			break;
8190 	}
8191 	spin_unlock_irq(ring_lock);
8192 
8193 	return cnt;
8194 }
8195 
8196 /**
8197  * lpfc_cmf_setup - Initialize idle_stat tracking
8198  * @phba: Pointer to HBA context object.
8199  *
8200  * This is called from HBA setup during driver load or when the HBA
8201  * comes online. this does all the initialization to support CMF and MI.
8202  **/
8203 static int
lpfc_cmf_setup(struct lpfc_hba * phba)8204 lpfc_cmf_setup(struct lpfc_hba *phba)
8205 {
8206 	LPFC_MBOXQ_t *mboxq;
8207 	struct lpfc_dmabuf *mp;
8208 	struct lpfc_pc_sli4_params *sli4_params;
8209 	int rc, cmf, mi_ver;
8210 
8211 	rc = lpfc_sli4_refresh_params(phba);
8212 	if (unlikely(rc))
8213 		return rc;
8214 
8215 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8216 	if (!mboxq)
8217 		return -ENOMEM;
8218 
8219 	sli4_params = &phba->sli4_hba.pc_sli4_params;
8220 
8221 	/* Always try to enable MI feature if we can */
8222 	if (sli4_params->mi_ver) {
8223 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
8224 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8225 		mi_ver = bf_get(lpfc_mbx_set_feature_mi,
8226 				 &mboxq->u.mqe.un.set_feature);
8227 
8228 		if (rc == MBX_SUCCESS) {
8229 			if (mi_ver) {
8230 				lpfc_printf_log(phba,
8231 						KERN_WARNING, LOG_CGN_MGMT,
8232 						"6215 MI is enabled\n");
8233 				sli4_params->mi_ver = mi_ver;
8234 			} else {
8235 				lpfc_printf_log(phba,
8236 						KERN_WARNING, LOG_CGN_MGMT,
8237 						"6338 MI is disabled\n");
8238 				sli4_params->mi_ver = 0;
8239 			}
8240 		} else {
8241 			/* mi_ver is already set from GET_SLI4_PARAMETERS */
8242 			lpfc_printf_log(phba, KERN_INFO,
8243 					LOG_CGN_MGMT | LOG_INIT,
8244 					"6245 Enable MI Mailbox x%x (x%x/x%x) "
8245 					"failed, rc:x%x mi:x%x\n",
8246 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8247 					lpfc_sli_config_mbox_subsys_get
8248 						(phba, mboxq),
8249 					lpfc_sli_config_mbox_opcode_get
8250 						(phba, mboxq),
8251 					rc, sli4_params->mi_ver);
8252 		}
8253 	} else {
8254 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8255 				"6217 MI is disabled\n");
8256 	}
8257 
8258 	/* Ensure FDMI is enabled for MI if enable_mi is set */
8259 	if (sli4_params->mi_ver)
8260 		phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
8261 
8262 	/* Always try to enable CMF feature if we can */
8263 	if (sli4_params->cmf) {
8264 		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
8265 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8266 		cmf = bf_get(lpfc_mbx_set_feature_cmf,
8267 			     &mboxq->u.mqe.un.set_feature);
8268 		if (rc == MBX_SUCCESS && cmf) {
8269 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8270 					"6218 CMF is enabled: mode %d\n",
8271 					phba->cmf_active_mode);
8272 		} else {
8273 			lpfc_printf_log(phba, KERN_WARNING,
8274 					LOG_CGN_MGMT | LOG_INIT,
8275 					"6219 Enable CMF Mailbox x%x (x%x/x%x) "
8276 					"failed, rc:x%x dd:x%x\n",
8277 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8278 					lpfc_sli_config_mbox_subsys_get
8279 						(phba, mboxq),
8280 					lpfc_sli_config_mbox_opcode_get
8281 						(phba, mboxq),
8282 					rc, cmf);
8283 			sli4_params->cmf = 0;
8284 			phba->cmf_active_mode = LPFC_CFG_OFF;
8285 			goto no_cmf;
8286 		}
8287 
8288 		/* Allocate Congestion Information Buffer */
8289 		if (!phba->cgn_i) {
8290 			mp = kmalloc(sizeof(*mp), GFP_KERNEL);
8291 			if (mp)
8292 				mp->virt = dma_alloc_coherent
8293 						(&phba->pcidev->dev,
8294 						sizeof(struct lpfc_cgn_info),
8295 						&mp->phys, GFP_KERNEL);
8296 			if (!mp || !mp->virt) {
8297 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8298 						"2640 Failed to alloc memory "
8299 						"for Congestion Info\n");
8300 				kfree(mp);
8301 				sli4_params->cmf = 0;
8302 				phba->cmf_active_mode = LPFC_CFG_OFF;
8303 				goto no_cmf;
8304 			}
8305 			phba->cgn_i = mp;
8306 
8307 			/* initialize congestion buffer info */
8308 			lpfc_init_congestion_buf(phba);
8309 			lpfc_init_congestion_stat(phba);
8310 
8311 			/* Zero out Congestion Signal counters */
8312 			atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
8313 			atomic64_set(&phba->cgn_acqe_stat.warn, 0);
8314 		}
8315 
8316 		rc = lpfc_sli4_cgn_params_read(phba);
8317 		if (rc < 0) {
8318 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8319 					"6242 Error reading Cgn Params (%d)\n",
8320 					rc);
8321 			/* Ensure CGN Mode is off */
8322 			sli4_params->cmf = 0;
8323 		} else if (!rc) {
8324 			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8325 					"6243 CGN Event empty object.\n");
8326 			/* Ensure CGN Mode is off */
8327 			sli4_params->cmf = 0;
8328 		}
8329 	} else {
8330 no_cmf:
8331 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8332 				"6220 CMF is disabled\n");
8333 	}
8334 
8335 	/* Only register congestion buffer with firmware if BOTH
8336 	 * CMF and E2E are enabled.
8337 	 */
8338 	if (sli4_params->cmf && sli4_params->mi_ver) {
8339 		rc = lpfc_reg_congestion_buf(phba);
8340 		if (rc) {
8341 			dma_free_coherent(&phba->pcidev->dev,
8342 					  sizeof(struct lpfc_cgn_info),
8343 					  phba->cgn_i->virt, phba->cgn_i->phys);
8344 			kfree(phba->cgn_i);
8345 			phba->cgn_i = NULL;
8346 			/* Ensure CGN Mode is off */
8347 			phba->cmf_active_mode = LPFC_CFG_OFF;
8348 			sli4_params->cmf = 0;
8349 			return 0;
8350 		}
8351 	}
8352 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8353 			"6470 Setup MI version %d CMF %d mode %d\n",
8354 			sli4_params->mi_ver, sli4_params->cmf,
8355 			phba->cmf_active_mode);
8356 
8357 	mempool_free(mboxq, phba->mbox_mem_pool);
8358 
8359 	/* Initialize atomic counters */
8360 	atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8361 	atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8362 	atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8363 	atomic_set(&phba->cgn_sync_warn_cnt, 0);
8364 	atomic_set(&phba->cgn_driver_evt_cnt, 0);
8365 	atomic_set(&phba->cgn_latency_evt_cnt, 0);
8366 	atomic64_set(&phba->cgn_latency_evt, 0);
8367 
8368 	phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
8369 
8370 	/* Allocate RX Monitor Buffer */
8371 	if (!phba->rx_monitor) {
8372 		phba->rx_monitor = kzalloc(sizeof(*phba->rx_monitor),
8373 					   GFP_KERNEL);
8374 
8375 		if (!phba->rx_monitor) {
8376 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8377 					"2644 Failed to alloc memory "
8378 					"for RX Monitor Buffer\n");
8379 			return -ENOMEM;
8380 		}
8381 
8382 		/* Instruct the rx_monitor object to instantiate its ring */
8383 		if (lpfc_rx_monitor_create_ring(phba->rx_monitor,
8384 						LPFC_MAX_RXMONITOR_ENTRY)) {
8385 			kfree(phba->rx_monitor);
8386 			phba->rx_monitor = NULL;
8387 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8388 					"2645 Failed to alloc memory "
8389 					"for RX Monitor's Ring\n");
8390 			return -ENOMEM;
8391 		}
8392 	}
8393 
8394 	return 0;
8395 }
8396 
8397 static int
lpfc_set_host_tm(struct lpfc_hba * phba)8398 lpfc_set_host_tm(struct lpfc_hba *phba)
8399 {
8400 	LPFC_MBOXQ_t *mboxq;
8401 	uint32_t len, rc;
8402 	struct timespec64 cur_time;
8403 	struct tm broken;
8404 	uint32_t month, day, year;
8405 	uint32_t hour, minute, second;
8406 	struct lpfc_mbx_set_host_date_time *tm;
8407 
8408 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8409 	if (!mboxq)
8410 		return -ENOMEM;
8411 
8412 	len = sizeof(struct lpfc_mbx_set_host_data) -
8413 		sizeof(struct lpfc_sli4_cfg_mhdr);
8414 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8415 			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8416 			 LPFC_SLI4_MBX_EMBED);
8417 
8418 	mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8419 	mboxq->u.mqe.un.set_host_data.param_len =
8420 			sizeof(struct lpfc_mbx_set_host_date_time);
8421 	tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8422 	ktime_get_real_ts64(&cur_time);
8423 	time64_to_tm(cur_time.tv_sec, 0, &broken);
8424 	month = broken.tm_mon + 1;
8425 	day = broken.tm_mday;
8426 	year = broken.tm_year - 100;
8427 	hour = broken.tm_hour;
8428 	minute = broken.tm_min;
8429 	second = broken.tm_sec;
8430 	bf_set(lpfc_mbx_set_host_month, tm, month);
8431 	bf_set(lpfc_mbx_set_host_day, tm, day);
8432 	bf_set(lpfc_mbx_set_host_year, tm, year);
8433 	bf_set(lpfc_mbx_set_host_hour, tm, hour);
8434 	bf_set(lpfc_mbx_set_host_min, tm, minute);
8435 	bf_set(lpfc_mbx_set_host_sec, tm, second);
8436 
8437 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8438 	mempool_free(mboxq, phba->mbox_mem_pool);
8439 	return rc;
8440 }
8441 
8442 /**
8443  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
8444  * @phba: Pointer to HBA context object.
8445  *
8446  * This function is the main SLI4 device initialization PCI function. This
8447  * function is called by the HBA initialization code, HBA reset code and
8448  * HBA error attention handler code. Caller is not required to hold any
8449  * locks.
8450  **/
8451 int
lpfc_sli4_hba_setup(struct lpfc_hba * phba)8452 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8453 {
8454 	int rc, i, cnt, len, dd;
8455 	LPFC_MBOXQ_t *mboxq;
8456 	struct lpfc_mqe *mqe;
8457 	uint8_t *vpd;
8458 	uint32_t vpd_size;
8459 	uint32_t ftr_rsp = 0;
8460 	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8461 	struct lpfc_vport *vport = phba->pport;
8462 	struct lpfc_dmabuf *mp;
8463 	struct lpfc_rqb *rqbp;
8464 	u32 flg;
8465 
8466 	/* Perform a PCI function reset to start from clean */
8467 	rc = lpfc_pci_function_reset(phba);
8468 	if (unlikely(rc))
8469 		return -ENODEV;
8470 
8471 	/* Check the HBA Host Status Register for readyness */
8472 	rc = lpfc_sli4_post_status_check(phba);
8473 	if (unlikely(rc))
8474 		return -ENODEV;
8475 	else {
8476 		spin_lock_irq(&phba->hbalock);
8477 		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
8478 		flg = phba->sli.sli_flag;
8479 		spin_unlock_irq(&phba->hbalock);
8480 		/* Allow a little time after setting SLI_ACTIVE for any polled
8481 		 * MBX commands to complete via BSG.
8482 		 */
8483 		for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {
8484 			msleep(20);
8485 			spin_lock_irq(&phba->hbalock);
8486 			flg = phba->sli.sli_flag;
8487 			spin_unlock_irq(&phba->hbalock);
8488 		}
8489 	}
8490 	clear_bit(HBA_SETUP, &phba->hba_flag);
8491 
8492 	lpfc_sli4_dip(phba);
8493 
8494 	/*
8495 	 * Allocate a single mailbox container for initializing the
8496 	 * port.
8497 	 */
8498 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8499 	if (!mboxq)
8500 		return -ENOMEM;
8501 
8502 	/* Issue READ_REV to collect vpd and FW information. */
8503 	vpd_size = SLI4_PAGE_SIZE;
8504 	vpd = kzalloc(vpd_size, GFP_KERNEL);
8505 	if (!vpd) {
8506 		rc = -ENOMEM;
8507 		goto out_free_mbox;
8508 	}
8509 
8510 	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
8511 	if (unlikely(rc)) {
8512 		kfree(vpd);
8513 		goto out_free_mbox;
8514 	}
8515 
8516 	mqe = &mboxq->u.mqe;
8517 	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
8518 	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
8519 		set_bit(HBA_FCOE_MODE, &phba->hba_flag);
8520 		phba->fcp_embed_io = 0;	/* SLI4 FC support only */
8521 	} else {
8522 		clear_bit(HBA_FCOE_MODE, &phba->hba_flag);
8523 	}
8524 
8525 	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8526 		LPFC_DCBX_CEE_MODE)
8527 		set_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8528 	else
8529 		clear_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
8530 
8531 	clear_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
8532 
8533 	if (phba->sli_rev != LPFC_SLI_REV4) {
8534 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8535 			"0376 READ_REV Error. SLI Level %d "
8536 			"FCoE enabled %d\n",
8537 			phba->sli_rev,
8538 			test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? 1 : 0);
8539 		rc = -EIO;
8540 		kfree(vpd);
8541 		goto out_free_mbox;
8542 	}
8543 
8544 	rc = lpfc_set_host_tm(phba);
8545 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
8546 			"6468 Set host date / time: Status x%x:\n", rc);
8547 
8548 	/*
8549 	 * Continue initialization with default values even if driver failed
8550 	 * to read FCoE param config regions, only read parameters if the
8551 	 * board is FCoE
8552 	 */
8553 	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
8554 	    lpfc_sli4_read_fcoe_params(phba))
8555 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
8556 			"2570 Failed to read FCoE parameters\n");
8557 
8558 	/*
8559 	 * Retrieve sli4 device physical port name, failure of doing it
8560 	 * is considered as non-fatal.
8561 	 */
8562 	rc = lpfc_sli4_retrieve_pport_name(phba);
8563 	if (!rc)
8564 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8565 				"3080 Successful retrieving SLI4 device "
8566 				"physical port name: %s.\n", phba->Port);
8567 
8568 	rc = lpfc_sli4_get_ctl_attr(phba);
8569 	if (!rc)
8570 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8571 				"8351 Successful retrieving SLI4 device "
8572 				"CTL ATTR\n");
8573 
8574 	/*
8575 	 * Evaluate the read rev and vpd data. Populate the driver
8576 	 * state with the results. If this routine fails, the failure
8577 	 * is not fatal as the driver will use generic values.
8578 	 */
8579 	rc = lpfc_parse_vpd(phba, vpd, vpd_size);
8580 	if (unlikely(!rc))
8581 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8582 				"0377 Error %d parsing vpd. "
8583 				"Using defaults.\n", rc);
8584 	kfree(vpd);
8585 
8586 	/* Save information as VPD data */
8587 	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8588 	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
8589 
8590 	/*
8591 	 * This is because first G7 ASIC doesn't support the standard
8592 	 * 0x5a NVME cmd descriptor type/subtype
8593 	 */
8594 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8595 			LPFC_SLI_INTF_IF_TYPE_6) &&
8596 	    (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8597 	    (phba->vpd.rev.smRev == 0) &&
8598 	    (phba->cfg_nvme_embed_cmd == 1))
8599 		phba->cfg_nvme_embed_cmd = 0;
8600 
8601 	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8602 	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8603 					 &mqe->un.read_rev);
8604 	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8605 				       &mqe->un.read_rev);
8606 	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8607 					    &mqe->un.read_rev);
8608 	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8609 					   &mqe->un.read_rev);
8610 	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8611 	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8612 	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8613 	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8614 	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8615 	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8616 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8617 			"(%d):0380 READ_REV Status x%x "
8618 			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8619 			mboxq->vport ? mboxq->vport->vpi : 0,
8620 			bf_get(lpfc_mqe_status, mqe),
8621 			phba->vpd.rev.opFwName,
8622 			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8623 			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
8624 
8625 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8626 	    LPFC_SLI_INTF_IF_TYPE_0) {
8627 		lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8628 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8629 		if (rc == MBX_SUCCESS) {
8630 			set_bit(HBA_RECOVERABLE_UE, &phba->hba_flag);
8631 			/* Set 1Sec interval to detect UE */
8632 			phba->eratt_poll_interval = 1;
8633 			phba->sli4_hba.ue_to_sr = bf_get(
8634 					lpfc_mbx_set_feature_UESR,
8635 					&mboxq->u.mqe.un.set_feature);
8636 			phba->sli4_hba.ue_to_rp = bf_get(
8637 					lpfc_mbx_set_feature_UERP,
8638 					&mboxq->u.mqe.un.set_feature);
8639 		}
8640 	}
8641 
8642 	if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8643 		/* Enable MDS Diagnostics only if the SLI Port supports it */
8644 		lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8645 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8646 		if (rc != MBX_SUCCESS)
8647 			phba->mds_diags_support = 0;
8648 	}
8649 
8650 	/*
8651 	 * Discover the port's supported feature set and match it against the
8652 	 * hosts requests.
8653 	 */
8654 	lpfc_request_features(phba, mboxq);
8655 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8656 	if (unlikely(rc)) {
8657 		rc = -EIO;
8658 		goto out_free_mbox;
8659 	}
8660 
8661 	/* Disable VMID if app header is not supported */
8662 	if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8663 						  &mqe->un.req_ftrs))) {
8664 		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8665 		phba->cfg_vmid_app_header = 0;
8666 		lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8667 				"1242 vmid feature not supported\n");
8668 	}
8669 
8670 	/*
8671 	 * The port must support FCP initiator mode as this is the
8672 	 * only mode running in the host.
8673 	 */
8674 	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8675 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8676 				"0378 No support for fcpi mode.\n");
8677 		ftr_rsp++;
8678 	}
8679 
8680 	/* Performance Hints are ONLY for FCoE */
8681 	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
8682 		if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
8683 			phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
8684 		else
8685 			phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
8686 	}
8687 
8688 	/*
8689 	 * If the port cannot support the host's requested features
8690 	 * then turn off the global config parameters to disable the
8691 	 * feature in the driver.  This is not a fatal error.
8692 	 */
8693 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8694 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8695 			phba->cfg_enable_bg = 0;
8696 			phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
8697 			ftr_rsp++;
8698 		}
8699 	}
8700 
8701 	if (phba->max_vpi && phba->cfg_enable_npiv &&
8702 	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8703 		ftr_rsp++;
8704 
8705 	if (ftr_rsp) {
8706 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8707 				"0379 Feature Mismatch Data: x%08x %08x "
8708 				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8709 				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8710 				phba->cfg_enable_npiv, phba->max_vpi);
8711 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8712 			phba->cfg_enable_bg = 0;
8713 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8714 			phba->cfg_enable_npiv = 0;
8715 	}
8716 
8717 	/* These SLI3 features are assumed in SLI4 */
8718 	spin_lock_irq(&phba->hbalock);
8719 	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8720 	spin_unlock_irq(&phba->hbalock);
8721 
8722 	/* Always try to enable dual dump feature if we can */
8723 	lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8724 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8725 	dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8726 	if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
8727 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8728 				"6448 Dual Dump is enabled\n");
8729 	else
8730 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8731 				"6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8732 				"rc:x%x dd:x%x\n",
8733 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8734 				lpfc_sli_config_mbox_subsys_get(
8735 					phba, mboxq),
8736 				lpfc_sli_config_mbox_opcode_get(
8737 					phba, mboxq),
8738 				rc, dd);
8739 
8740 	/*
8741 	 * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
8742 	 * calls depends on these resources to complete port setup.
8743 	 */
8744 	rc = lpfc_sli4_alloc_resource_identifiers(phba);
8745 	if (rc) {
8746 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8747 				"2920 Failed to alloc Resource IDs "
8748 				"rc = x%x\n", rc);
8749 		goto out_free_mbox;
8750 	}
8751 
8752 	lpfc_sli4_node_rpi_restore(phba);
8753 
8754 	lpfc_set_host_data(phba, mboxq);
8755 
8756 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8757 	if (rc) {
8758 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8759 				"2134 Failed to set host os driver version %x",
8760 				rc);
8761 	}
8762 
8763 	/* Read the port's service parameters. */
8764 	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8765 	if (rc) {
8766 		phba->link_state = LPFC_HBA_ERROR;
8767 		rc = -ENOMEM;
8768 		goto out_free_mbox;
8769 	}
8770 
8771 	mboxq->vport = vport;
8772 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8773 	mp = mboxq->ctx_buf;
8774 	if (rc == MBX_SUCCESS) {
8775 		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8776 		rc = 0;
8777 	}
8778 
8779 	/*
8780 	 * This memory was allocated by the lpfc_read_sparam routine but is
8781 	 * no longer needed.  It is released and ctx_buf NULLed to prevent
8782 	 * unintended pointer access as the mbox is reused.
8783 	 */
8784 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
8785 	kfree(mp);
8786 	mboxq->ctx_buf = NULL;
8787 	if (unlikely(rc)) {
8788 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8789 				"0382 READ_SPARAM command failed "
8790 				"status %d, mbxStatus x%x\n",
8791 				rc, bf_get(lpfc_mqe_status, mqe));
8792 		phba->link_state = LPFC_HBA_ERROR;
8793 		rc = -EIO;
8794 		goto out_free_mbox;
8795 	}
8796 
8797 	lpfc_update_vport_wwn(vport);
8798 
8799 	/* Update the fc_host data structures with new wwn. */
8800 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8801 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8802 
8803 	/* Create all the SLI4 queues */
8804 	rc = lpfc_sli4_queue_create(phba);
8805 	if (rc) {
8806 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8807 				"3089 Failed to allocate queues\n");
8808 		rc = -ENODEV;
8809 		goto out_free_mbox;
8810 	}
8811 	/* Set up all the queues to the device */
8812 	rc = lpfc_sli4_queue_setup(phba);
8813 	if (unlikely(rc)) {
8814 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8815 				"0381 Error %d during queue setup.\n", rc);
8816 		goto out_stop_timers;
8817 	}
8818 	/* Initialize the driver internal SLI layer lists. */
8819 	lpfc_sli4_setup(phba);
8820 	lpfc_sli4_queue_init(phba);
8821 
8822 	/* update host els xri-sgl sizes and mappings */
8823 	rc = lpfc_sli4_els_sgl_update(phba);
8824 	if (unlikely(rc)) {
8825 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8826 				"1400 Failed to update xri-sgl size and "
8827 				"mapping: %d\n", rc);
8828 		goto out_destroy_queue;
8829 	}
8830 
8831 	/* register the els sgl pool to the port */
8832 	rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8833 				       phba->sli4_hba.els_xri_cnt);
8834 	if (unlikely(rc < 0)) {
8835 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8836 				"0582 Error %d during els sgl post "
8837 				"operation\n", rc);
8838 		rc = -ENODEV;
8839 		goto out_destroy_queue;
8840 	}
8841 	phba->sli4_hba.els_xri_cnt = rc;
8842 
8843 	if (phba->nvmet_support) {
8844 		/* update host nvmet xri-sgl sizes and mappings */
8845 		rc = lpfc_sli4_nvmet_sgl_update(phba);
8846 		if (unlikely(rc)) {
8847 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8848 					"6308 Failed to update nvmet-sgl size "
8849 					"and mapping: %d\n", rc);
8850 			goto out_destroy_queue;
8851 		}
8852 
8853 		/* register the nvmet sgl pool to the port */
8854 		rc = lpfc_sli4_repost_sgl_list(
8855 			phba,
8856 			&phba->sli4_hba.lpfc_nvmet_sgl_list,
8857 			phba->sli4_hba.nvmet_xri_cnt);
8858 		if (unlikely(rc < 0)) {
8859 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8860 					"3117 Error %d during nvmet "
8861 					"sgl post\n", rc);
8862 			rc = -ENODEV;
8863 			goto out_destroy_queue;
8864 		}
8865 		phba->sli4_hba.nvmet_xri_cnt = rc;
8866 
8867 		/* We allocate an iocbq for every receive context SGL.
8868 		 * The additional allocation is for abort and ls handling.
8869 		 */
8870 		cnt = phba->sli4_hba.nvmet_xri_cnt +
8871 			phba->sli4_hba.max_cfg_param.max_xri;
8872 	} else {
8873 		/* update host common xri-sgl sizes and mappings */
8874 		rc = lpfc_sli4_io_sgl_update(phba);
8875 		if (unlikely(rc)) {
8876 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8877 					"6082 Failed to update nvme-sgl size "
8878 					"and mapping: %d\n", rc);
8879 			goto out_destroy_queue;
8880 		}
8881 
8882 		/* register the allocated common sgl pool to the port */
8883 		rc = lpfc_sli4_repost_io_sgl_list(phba);
8884 		if (unlikely(rc)) {
8885 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8886 					"6116 Error %d during nvme sgl post "
8887 					"operation\n", rc);
8888 			/* Some NVME buffers were moved to abort nvme list */
8889 			/* A pci function reset will repost them */
8890 			rc = -ENODEV;
8891 			goto out_destroy_queue;
8892 		}
8893 		/* Each lpfc_io_buf job structure has an iocbq element.
8894 		 * This cnt provides for abort, els, ct and ls requests.
8895 		 */
8896 		cnt = phba->sli4_hba.max_cfg_param.max_xri;
8897 	}
8898 
8899 	if (!phba->sli.iocbq_lookup) {
8900 		/* Initialize and populate the iocb list per host */
8901 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8902 				"2821 initialize iocb list with %d entries\n",
8903 				cnt);
8904 		rc = lpfc_init_iocb_list(phba, cnt);
8905 		if (rc) {
8906 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8907 					"1413 Failed to init iocb list.\n");
8908 			goto out_destroy_queue;
8909 		}
8910 	}
8911 
8912 	if (phba->nvmet_support)
8913 		lpfc_nvmet_create_targetport(phba);
8914 
8915 	if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
8916 		/* Post initial buffers to all RQs created */
8917 		for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8918 			rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8919 			INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8920 			rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8921 			rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
8922 			rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
8923 			rqbp->buffer_count = 0;
8924 
8925 			lpfc_post_rq_buffer(
8926 				phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8927 				phba->sli4_hba.nvmet_mrq_data[i],
8928 				phba->cfg_nvmet_mrq_post, i);
8929 		}
8930 	}
8931 
8932 	/* Post the rpi header region to the device. */
8933 	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8934 	if (unlikely(rc)) {
8935 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8936 				"0393 Error %d during rpi post operation\n",
8937 				rc);
8938 		rc = -ENODEV;
8939 		goto out_free_iocblist;
8940 	}
8941 
8942 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
8943 		if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
8944 			/*
8945 			 * The FC Port needs to register FCFI (index 0)
8946 			 */
8947 			lpfc_reg_fcfi(phba, mboxq);
8948 			mboxq->vport = phba->pport;
8949 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8950 			if (rc != MBX_SUCCESS)
8951 				goto out_unset_queue;
8952 			rc = 0;
8953 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
8954 						&mboxq->u.mqe.un.reg_fcfi);
8955 		} else {
8956 			/* We are a NVME Target mode with MRQ > 1 */
8957 
8958 			/* First register the FCFI */
8959 			lpfc_reg_fcfi_mrq(phba, mboxq, 0);
8960 			mboxq->vport = phba->pport;
8961 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8962 			if (rc != MBX_SUCCESS)
8963 				goto out_unset_queue;
8964 			rc = 0;
8965 			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
8966 						&mboxq->u.mqe.un.reg_fcfi_mrq);
8967 
8968 			/* Next register the MRQs */
8969 			lpfc_reg_fcfi_mrq(phba, mboxq, 1);
8970 			mboxq->vport = phba->pport;
8971 			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8972 			if (rc != MBX_SUCCESS)
8973 				goto out_unset_queue;
8974 			rc = 0;
8975 		}
8976 		/* Check if the port is configured to be disabled */
8977 		lpfc_sli_read_link_ste(phba);
8978 	}
8979 
8980 	/* Don't post more new bufs if repost already recovered
8981 	 * the nvme sgls.
8982 	 */
8983 	if (phba->nvmet_support == 0) {
8984 		if (phba->sli4_hba.io_xri_cnt == 0) {
8985 			len = lpfc_new_io_buf(
8986 					      phba, phba->sli4_hba.io_xri_max);
8987 			if (len == 0) {
8988 				rc = -ENOMEM;
8989 				goto out_unset_queue;
8990 			}
8991 
8992 			if (phba->cfg_xri_rebalancing)
8993 				lpfc_create_multixri_pools(phba);
8994 		}
8995 	} else {
8996 		phba->cfg_xri_rebalancing = 0;
8997 	}
8998 
8999 	/* Allow asynchronous mailbox command to go through */
9000 	spin_lock_irq(&phba->hbalock);
9001 	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9002 	spin_unlock_irq(&phba->hbalock);
9003 
9004 	/* Post receive buffers to the device */
9005 	lpfc_sli4_rb_setup(phba);
9006 
9007 	/* Reset HBA FCF states after HBA reset */
9008 	phba->fcf.fcf_flag = 0;
9009 	phba->fcf.current_rec.flag = 0;
9010 
9011 	/* Start the ELS watchdog timer */
9012 	mod_timer(&vport->els_tmofunc,
9013 		  jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
9014 
9015 	/* Start heart beat timer */
9016 	mod_timer(&phba->hb_tmofunc,
9017 		  jiffies + secs_to_jiffies(LPFC_HB_MBOX_INTERVAL));
9018 	clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
9019 	clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
9020 	phba->last_completion_time = jiffies;
9021 
9022 	/* start eq_delay heartbeat */
9023 	if (phba->cfg_auto_imax)
9024 		queue_delayed_work(phba->wq, &phba->eq_delay_work,
9025 				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
9026 
9027 	/* start per phba idle_stat_delay heartbeat */
9028 	lpfc_init_idle_stat_hb(phba);
9029 
9030 	/* Start error attention (ERATT) polling timer */
9031 	mod_timer(&phba->eratt_poll,
9032 		  jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9033 
9034 	/*
9035 	 * The port is ready, set the host's link state to LINK_DOWN
9036 	 * in preparation for link interrupts.
9037 	 */
9038 	spin_lock_irq(&phba->hbalock);
9039 	phba->link_state = LPFC_LINK_DOWN;
9040 
9041 	/* Check if physical ports are trunked */
9042 	if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
9043 		phba->trunk_link.link0.state = LPFC_LINK_DOWN;
9044 	if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
9045 		phba->trunk_link.link1.state = LPFC_LINK_DOWN;
9046 	if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
9047 		phba->trunk_link.link2.state = LPFC_LINK_DOWN;
9048 	if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
9049 		phba->trunk_link.link3.state = LPFC_LINK_DOWN;
9050 	spin_unlock_irq(&phba->hbalock);
9051 
9052 	/* Arm the CQs and then EQs on device */
9053 	lpfc_sli4_arm_cqeq_intr(phba);
9054 
9055 	/* Indicate device interrupt mode */
9056 	phba->sli4_hba.intr_enable = 1;
9057 
9058 	/* Setup CMF after HBA is initialized */
9059 	lpfc_cmf_setup(phba);
9060 
9061 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
9062 	    test_bit(LINK_DISABLED, &phba->hba_flag)) {
9063 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9064 				"3103 Adapter Link is disabled.\n");
9065 		lpfc_down_link(phba, mboxq);
9066 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9067 		if (rc != MBX_SUCCESS) {
9068 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9069 					"3104 Adapter failed to issue "
9070 					"DOWN_LINK mbox cmd, rc:x%x\n", rc);
9071 			goto out_io_buff_free;
9072 		}
9073 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
9074 		/* don't perform init_link on SLI4 FC port loopback test */
9075 		if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
9076 			rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
9077 			if (rc)
9078 				goto out_io_buff_free;
9079 		}
9080 	}
9081 	mempool_free(mboxq, phba->mbox_mem_pool);
9082 
9083 	/* Enable RAS FW log support */
9084 	lpfc_sli4_ras_setup(phba);
9085 
9086 	set_bit(HBA_SETUP, &phba->hba_flag);
9087 	return rc;
9088 
9089 out_io_buff_free:
9090 	/* Free allocated IO Buffers */
9091 	lpfc_io_free(phba);
9092 out_unset_queue:
9093 	/* Unset all the queues set up in this routine when error out */
9094 	lpfc_sli4_queue_unset(phba);
9095 out_free_iocblist:
9096 	lpfc_free_iocb_list(phba);
9097 out_destroy_queue:
9098 	lpfc_sli4_queue_destroy(phba);
9099 out_stop_timers:
9100 	lpfc_stop_hba_timers(phba);
9101 out_free_mbox:
9102 	mempool_free(mboxq, phba->mbox_mem_pool);
9103 	return rc;
9104 }
9105 
9106 /**
9107  * lpfc_mbox_timeout - Timeout call back function for mbox timer
9108  * @t: Context to fetch pointer to hba structure from.
9109  *
9110  * This is the callback function for mailbox timer. The mailbox
9111  * timer is armed when a new mailbox command is issued and the timer
9112  * is deleted when the mailbox complete. The function is called by
9113  * the kernel timer code when a mailbox does not complete within
9114  * expected time. This function wakes up the worker thread to
9115  * process the mailbox timeout and returns. All the processing is
9116  * done by the worker thread function lpfc_mbox_timeout_handler.
9117  **/
9118 void
lpfc_mbox_timeout(struct timer_list * t)9119 lpfc_mbox_timeout(struct timer_list *t)
9120 {
9121 	struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
9122 	unsigned long iflag;
9123 	uint32_t tmo_posted;
9124 
9125 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
9126 	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
9127 	if (!tmo_posted)
9128 		phba->pport->work_port_events |= WORKER_MBOX_TMO;
9129 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
9130 
9131 	if (!tmo_posted)
9132 		lpfc_worker_wake_up(phba);
9133 	return;
9134 }
9135 
9136 /**
9137  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
9138  *                                    are pending
9139  * @phba: Pointer to HBA context object.
9140  *
9141  * This function checks if any mailbox completions are present on the mailbox
9142  * completion queue.
9143  **/
9144 static bool
lpfc_sli4_mbox_completions_pending(struct lpfc_hba * phba)9145 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
9146 {
9147 
9148 	uint32_t idx;
9149 	struct lpfc_queue *mcq;
9150 	struct lpfc_mcqe *mcqe;
9151 	bool pending_completions = false;
9152 	uint8_t	qe_valid;
9153 
9154 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9155 		return false;
9156 
9157 	/* Check for completions on mailbox completion queue */
9158 
9159 	mcq = phba->sli4_hba.mbx_cq;
9160 	idx = mcq->hba_index;
9161 	qe_valid = mcq->qe_valid;
9162 	while (bf_get_le32(lpfc_cqe_valid,
9163 	       (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
9164 		mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
9165 		if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
9166 		    (!bf_get_le32(lpfc_trailer_async, mcqe))) {
9167 			pending_completions = true;
9168 			break;
9169 		}
9170 		idx = (idx + 1) % mcq->entry_count;
9171 		if (mcq->hba_index == idx)
9172 			break;
9173 
9174 		/* if the index wrapped around, toggle the valid bit */
9175 		if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
9176 			qe_valid = (qe_valid) ? 0 : 1;
9177 	}
9178 	return pending_completions;
9179 
9180 }
9181 
9182 /**
9183  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
9184  *					      that were missed.
9185  * @phba: Pointer to HBA context object.
9186  *
9187  * For sli4, it is possible to miss an interrupt. As such mbox completions
9188  * maybe missed causing erroneous mailbox timeouts to occur. This function
9189  * checks to see if mbox completions are on the mailbox completion queue
9190  * and will process all the completions associated with the eq for the
9191  * mailbox completion queue.
9192  **/
9193 static bool
lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba * phba)9194 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
9195 {
9196 	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
9197 	uint32_t eqidx;
9198 	struct lpfc_queue *fpeq = NULL;
9199 	struct lpfc_queue *eq;
9200 	bool mbox_pending;
9201 
9202 	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
9203 		return false;
9204 
9205 	/* Find the EQ associated with the mbox CQ */
9206 	if (sli4_hba->hdwq) {
9207 		for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
9208 			eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
9209 			if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
9210 				fpeq = eq;
9211 				break;
9212 			}
9213 		}
9214 	}
9215 	if (!fpeq)
9216 		return false;
9217 
9218 	/* Turn off interrupts from this EQ */
9219 
9220 	sli4_hba->sli4_eq_clr_intr(fpeq);
9221 
9222 	/* Check to see if a mbox completion is pending */
9223 
9224 	mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
9225 
9226 	/*
9227 	 * If a mbox completion is pending, process all the events on EQ
9228 	 * associated with the mbox completion queue (this could include
9229 	 * mailbox commands, async events, els commands, receive queue data
9230 	 * and fcp commands)
9231 	 */
9232 
9233 	if (mbox_pending)
9234 		/* process and rearm the EQ */
9235 		lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
9236 				     LPFC_QUEUE_WORK);
9237 	else
9238 		/* Always clear and re-arm the EQ */
9239 		sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
9240 
9241 	return mbox_pending;
9242 
9243 }
9244 
9245 /**
9246  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
9247  * @phba: Pointer to HBA context object.
9248  *
9249  * This function is called from worker thread when a mailbox command times out.
9250  * The caller is not required to hold any locks. This function will reset the
9251  * HBA and recover all the pending commands.
9252  **/
9253 void
lpfc_mbox_timeout_handler(struct lpfc_hba * phba)9254 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
9255 {
9256 	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
9257 	MAILBOX_t *mb = NULL;
9258 
9259 	struct lpfc_sli *psli = &phba->sli;
9260 
9261 	/* If the mailbox completed, process the completion */
9262 	lpfc_sli4_process_missed_mbox_completions(phba);
9263 
9264 	if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
9265 		return;
9266 
9267 	if (pmbox != NULL)
9268 		mb = &pmbox->u.mb;
9269 	/* Check the pmbox pointer first.  There is a race condition
9270 	 * between the mbox timeout handler getting executed in the
9271 	 * worklist and the mailbox actually completing. When this
9272 	 * race condition occurs, the mbox_active will be NULL.
9273 	 */
9274 	spin_lock_irq(&phba->hbalock);
9275 	if (pmbox == NULL) {
9276 		lpfc_printf_log(phba, KERN_WARNING,
9277 				LOG_MBOX | LOG_SLI,
9278 				"0353 Active Mailbox cleared - mailbox timeout "
9279 				"exiting\n");
9280 		spin_unlock_irq(&phba->hbalock);
9281 		return;
9282 	}
9283 
9284 	/* Mbox cmd <mbxCommand> timeout */
9285 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9286 			"0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
9287 			mb->mbxCommand,
9288 			phba->pport->port_state,
9289 			phba->sli.sli_flag,
9290 			phba->sli.mbox_active);
9291 	spin_unlock_irq(&phba->hbalock);
9292 
9293 	/* Setting state unknown so lpfc_sli_abort_iocb_ring
9294 	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
9295 	 * it to fail all outstanding SCSI IO.
9296 	 */
9297 	set_bit(MBX_TMO_ERR, &phba->bit_flags);
9298 	spin_lock_irq(&phba->pport->work_port_lock);
9299 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9300 	spin_unlock_irq(&phba->pport->work_port_lock);
9301 	spin_lock_irq(&phba->hbalock);
9302 	phba->link_state = LPFC_LINK_UNKNOWN;
9303 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9304 	spin_unlock_irq(&phba->hbalock);
9305 
9306 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9307 			"0345 Resetting board due to mailbox timeout\n");
9308 
9309 	/* Reset the HBA device */
9310 	lpfc_reset_hba(phba);
9311 }
9312 
9313 /**
9314  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
9315  * @phba: Pointer to HBA context object.
9316  * @pmbox: Pointer to mailbox object.
9317  * @flag: Flag indicating how the mailbox need to be processed.
9318  *
9319  * This function is called by discovery code and HBA management code
9320  * to submit a mailbox command to firmware with SLI-3 interface spec. This
9321  * function gets the hbalock to protect the data structures.
9322  * The mailbox command can be submitted in polling mode, in which case
9323  * this function will wait in a polling loop for the completion of the
9324  * mailbox.
9325  * If the mailbox is submitted in no_wait mode (not polling) the
9326  * function will submit the command and returns immediately without waiting
9327  * for the mailbox completion. The no_wait is supported only when HBA
9328  * is in SLI2/SLI3 mode - interrupts are enabled.
9329  * The SLI interface allows only one mailbox pending at a time. If the
9330  * mailbox is issued in polling mode and there is already a mailbox
9331  * pending, then the function will return an error. If the mailbox is issued
9332  * in NO_WAIT mode and there is a mailbox pending already, the function
9333  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9334  * The sli layer owns the mailbox object until the completion of mailbox
9335  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9336  * return codes the caller owns the mailbox command after the return of
9337  * the function.
9338  **/
9339 static int
lpfc_sli_issue_mbox_s3(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)9340 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9341 		       uint32_t flag)
9342 {
9343 	MAILBOX_t *mbx;
9344 	struct lpfc_sli *psli = &phba->sli;
9345 	uint32_t status, evtctr;
9346 	uint32_t ha_copy, hc_copy;
9347 	int i;
9348 	unsigned long timeout;
9349 	unsigned long drvr_flag = 0;
9350 	uint32_t word0, ldata;
9351 	void __iomem *to_slim;
9352 	int processing_queue = 0;
9353 
9354 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
9355 	if (!pmbox) {
9356 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9357 		/* processing mbox queue from intr_handler */
9358 		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9359 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9360 			return MBX_SUCCESS;
9361 		}
9362 		processing_queue = 1;
9363 		pmbox = lpfc_mbox_get(phba);
9364 		if (!pmbox) {
9365 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9366 			return MBX_SUCCESS;
9367 		}
9368 	}
9369 
9370 	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
9371 		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
9372 		if(!pmbox->vport) {
9373 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9374 			lpfc_printf_log(phba, KERN_ERR,
9375 					LOG_MBOX | LOG_VPORT,
9376 					"1806 Mbox x%x failed. No vport\n",
9377 					pmbox->u.mb.mbxCommand);
9378 			dump_stack();
9379 			goto out_not_finished;
9380 		}
9381 	}
9382 
9383 	/* If the PCI channel is in offline state, do not post mbox. */
9384 	if (unlikely(pci_channel_offline(phba->pcidev))) {
9385 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9386 		goto out_not_finished;
9387 	}
9388 
9389 	/* If HBA has a deferred error attention, fail the iocb. */
9390 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
9391 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9392 		goto out_not_finished;
9393 	}
9394 
9395 	psli = &phba->sli;
9396 
9397 	mbx = &pmbox->u.mb;
9398 	status = MBX_SUCCESS;
9399 
9400 	if (phba->link_state == LPFC_HBA_ERROR) {
9401 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9402 
9403 		/* Mbox command <mbxCommand> cannot issue */
9404 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9405 				"(%d):0311 Mailbox command x%x cannot "
9406 				"issue Data: x%x x%x\n",
9407 				pmbox->vport ? pmbox->vport->vpi : 0,
9408 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9409 		goto out_not_finished;
9410 	}
9411 
9412 	if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9413 		if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9414 			!(hc_copy & HC_MBINT_ENA)) {
9415 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9416 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9417 				"(%d):2528 Mailbox command x%x cannot "
9418 				"issue Data: x%x x%x\n",
9419 				pmbox->vport ? pmbox->vport->vpi : 0,
9420 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9421 			goto out_not_finished;
9422 		}
9423 	}
9424 
9425 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9426 		/* Polling for a mbox command when another one is already active
9427 		 * is not allowed in SLI. Also, the driver must have established
9428 		 * SLI2 mode to queue and process multiple mbox commands.
9429 		 */
9430 
9431 		if (flag & MBX_POLL) {
9432 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9433 
9434 			/* Mbox command <mbxCommand> cannot issue */
9435 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9436 					"(%d):2529 Mailbox command x%x "
9437 					"cannot issue Data: x%x x%x\n",
9438 					pmbox->vport ? pmbox->vport->vpi : 0,
9439 					pmbox->u.mb.mbxCommand,
9440 					psli->sli_flag, flag);
9441 			goto out_not_finished;
9442 		}
9443 
9444 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
9445 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9446 			/* Mbox command <mbxCommand> cannot issue */
9447 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9448 					"(%d):2530 Mailbox command x%x "
9449 					"cannot issue Data: x%x x%x\n",
9450 					pmbox->vport ? pmbox->vport->vpi : 0,
9451 					pmbox->u.mb.mbxCommand,
9452 					psli->sli_flag, flag);
9453 			goto out_not_finished;
9454 		}
9455 
9456 		/* Another mailbox command is still being processed, queue this
9457 		 * command to be processed later.
9458 		 */
9459 		lpfc_mbox_put(phba, pmbox);
9460 
9461 		/* Mbox cmd issue - BUSY */
9462 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9463 				"(%d):0308 Mbox cmd issue - BUSY Data: "
9464 				"x%x x%x x%x x%x\n",
9465 				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
9466 				mbx->mbxCommand,
9467 				phba->pport ? phba->pport->port_state : 0xff,
9468 				psli->sli_flag, flag);
9469 
9470 		psli->slistat.mbox_busy++;
9471 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9472 
9473 		if (pmbox->vport) {
9474 			lpfc_debugfs_disc_trc(pmbox->vport,
9475 				LPFC_DISC_TRC_MBOX_VPORT,
9476 				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
9477 				(uint32_t)mbx->mbxCommand,
9478 				mbx->un.varWords[0], mbx->un.varWords[1]);
9479 		}
9480 		else {
9481 			lpfc_debugfs_disc_trc(phba->pport,
9482 				LPFC_DISC_TRC_MBOX,
9483 				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
9484 				(uint32_t)mbx->mbxCommand,
9485 				mbx->un.varWords[0], mbx->un.varWords[1]);
9486 		}
9487 
9488 		return MBX_BUSY;
9489 	}
9490 
9491 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9492 
9493 	/* If we are not polling, we MUST be in SLI2 mode */
9494 	if (flag != MBX_POLL) {
9495 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
9496 		    (mbx->mbxCommand != MBX_KILL_BOARD)) {
9497 			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9498 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9499 			/* Mbox command <mbxCommand> cannot issue */
9500 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9501 					"(%d):2531 Mailbox command x%x "
9502 					"cannot issue Data: x%x x%x\n",
9503 					pmbox->vport ? pmbox->vport->vpi : 0,
9504 					pmbox->u.mb.mbxCommand,
9505 					psli->sli_flag, flag);
9506 			goto out_not_finished;
9507 		}
9508 		/* timeout active mbox command */
9509 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9510 					   1000);
9511 		mod_timer(&psli->mbox_tmo, jiffies + timeout);
9512 	}
9513 
9514 	/* Mailbox cmd <cmd> issue */
9515 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9516 			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
9517 			"x%x\n",
9518 			pmbox->vport ? pmbox->vport->vpi : 0,
9519 			mbx->mbxCommand,
9520 			phba->pport ? phba->pport->port_state : 0xff,
9521 			psli->sli_flag, flag);
9522 
9523 	if (mbx->mbxCommand != MBX_HEARTBEAT) {
9524 		if (pmbox->vport) {
9525 			lpfc_debugfs_disc_trc(pmbox->vport,
9526 				LPFC_DISC_TRC_MBOX_VPORT,
9527 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
9528 				(uint32_t)mbx->mbxCommand,
9529 				mbx->un.varWords[0], mbx->un.varWords[1]);
9530 		}
9531 		else {
9532 			lpfc_debugfs_disc_trc(phba->pport,
9533 				LPFC_DISC_TRC_MBOX,
9534 				"MBOX Send:       cmd:x%x mb:x%x x%x",
9535 				(uint32_t)mbx->mbxCommand,
9536 				mbx->un.varWords[0], mbx->un.varWords[1]);
9537 		}
9538 	}
9539 
9540 	psli->slistat.mbox_cmd++;
9541 	evtctr = psli->slistat.mbox_event;
9542 
9543 	/* next set own bit for the adapter and copy over command word */
9544 	mbx->mbxOwner = OWN_CHIP;
9545 
9546 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9547 		/* Populate mbox extension offset word. */
9548 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
9549 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9550 				= (uint8_t *)phba->mbox_ext
9551 				  - (uint8_t *)phba->mbox;
9552 		}
9553 
9554 		/* Copy the mailbox extension data */
9555 		if (pmbox->in_ext_byte_len && pmbox->ext_buf) {
9556 			lpfc_sli_pcimem_bcopy(pmbox->ext_buf,
9557 					      (uint8_t *)phba->mbox_ext,
9558 					      pmbox->in_ext_byte_len);
9559 		}
9560 		/* Copy command data to host SLIM area */
9561 		lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
9562 	} else {
9563 		/* Populate mbox extension offset word. */
9564 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
9565 			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9566 				= MAILBOX_HBA_EXT_OFFSET;
9567 
9568 		/* Copy the mailbox extension data */
9569 		if (pmbox->in_ext_byte_len && pmbox->ext_buf)
9570 			lpfc_memcpy_to_slim(phba->MBslimaddr +
9571 				MAILBOX_HBA_EXT_OFFSET,
9572 				pmbox->ext_buf, pmbox->in_ext_byte_len);
9573 
9574 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9575 			/* copy command data into host mbox for cmpl */
9576 			lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
9577 					      MAILBOX_CMD_SIZE);
9578 
9579 		/* First copy mbox command data to HBA SLIM, skip past first
9580 		   word */
9581 		to_slim = phba->MBslimaddr + sizeof (uint32_t);
9582 		lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
9583 			    MAILBOX_CMD_SIZE - sizeof (uint32_t));
9584 
9585 		/* Next copy over first word, with mbxOwner set */
9586 		ldata = *((uint32_t *)mbx);
9587 		to_slim = phba->MBslimaddr;
9588 		writel(ldata, to_slim);
9589 		readl(to_slim); /* flush */
9590 
9591 		if (mbx->mbxCommand == MBX_CONFIG_PORT)
9592 			/* switch over to host mailbox */
9593 			psli->sli_flag |= LPFC_SLI_ACTIVE;
9594 	}
9595 
9596 	wmb();
9597 
9598 	switch (flag) {
9599 	case MBX_NOWAIT:
9600 		/* Set up reference to mailbox command */
9601 		psli->mbox_active = pmbox;
9602 		/* Interrupt board to do it */
9603 		writel(CA_MBATT, phba->CAregaddr);
9604 		readl(phba->CAregaddr); /* flush */
9605 		/* Don't wait for it to finish, just return */
9606 		break;
9607 
9608 	case MBX_POLL:
9609 		/* Set up null reference to mailbox command */
9610 		psli->mbox_active = NULL;
9611 		/* Interrupt board to do it */
9612 		writel(CA_MBATT, phba->CAregaddr);
9613 		readl(phba->CAregaddr); /* flush */
9614 
9615 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9616 			/* First read mbox status word */
9617 			word0 = *((uint32_t *)phba->mbox);
9618 			word0 = le32_to_cpu(word0);
9619 		} else {
9620 			/* First read mbox status word */
9621 			if (lpfc_readl(phba->MBslimaddr, &word0)) {
9622 				spin_unlock_irqrestore(&phba->hbalock,
9623 						       drvr_flag);
9624 				goto out_not_finished;
9625 			}
9626 		}
9627 
9628 		/* Read the HBA Host Attention Register */
9629 		if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9630 			spin_unlock_irqrestore(&phba->hbalock,
9631 						       drvr_flag);
9632 			goto out_not_finished;
9633 		}
9634 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9635 							1000) + jiffies;
9636 		i = 0;
9637 		/* Wait for command to complete */
9638 		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9639 		       (!(ha_copy & HA_MBATT) &&
9640 			(phba->link_state > LPFC_WARM_START))) {
9641 			if (time_after(jiffies, timeout)) {
9642 				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9643 				spin_unlock_irqrestore(&phba->hbalock,
9644 						       drvr_flag);
9645 				goto out_not_finished;
9646 			}
9647 
9648 			/* Check if we took a mbox interrupt while we were
9649 			   polling */
9650 			if (((word0 & OWN_CHIP) != OWN_CHIP)
9651 			    && (evtctr != psli->slistat.mbox_event))
9652 				break;
9653 
9654 			if (i++ > 10) {
9655 				spin_unlock_irqrestore(&phba->hbalock,
9656 						       drvr_flag);
9657 				msleep(1);
9658 				spin_lock_irqsave(&phba->hbalock, drvr_flag);
9659 			}
9660 
9661 			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9662 				/* First copy command data */
9663 				word0 = *((uint32_t *)phba->mbox);
9664 				word0 = le32_to_cpu(word0);
9665 				if (mbx->mbxCommand == MBX_CONFIG_PORT) {
9666 					MAILBOX_t *slimmb;
9667 					uint32_t slimword0;
9668 					/* Check real SLIM for any errors */
9669 					slimword0 = readl(phba->MBslimaddr);
9670 					slimmb = (MAILBOX_t *) & slimword0;
9671 					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9672 					    && slimmb->mbxStatus) {
9673 						psli->sli_flag &=
9674 						    ~LPFC_SLI_ACTIVE;
9675 						word0 = slimword0;
9676 					}
9677 				}
9678 			} else {
9679 				/* First copy command data */
9680 				word0 = readl(phba->MBslimaddr);
9681 			}
9682 			/* Read the HBA Host Attention Register */
9683 			if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9684 				spin_unlock_irqrestore(&phba->hbalock,
9685 						       drvr_flag);
9686 				goto out_not_finished;
9687 			}
9688 		}
9689 
9690 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9691 			/* copy results back to user */
9692 			lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9693 						MAILBOX_CMD_SIZE);
9694 			/* Copy the mailbox extension data */
9695 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9696 				lpfc_sli_pcimem_bcopy(phba->mbox_ext,
9697 						      pmbox->ext_buf,
9698 						      pmbox->out_ext_byte_len);
9699 			}
9700 		} else {
9701 			/* First copy command data */
9702 			lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
9703 						MAILBOX_CMD_SIZE);
9704 			/* Copy the mailbox extension data */
9705 			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
9706 				lpfc_memcpy_from_slim(
9707 					pmbox->ext_buf,
9708 					phba->MBslimaddr +
9709 					MAILBOX_HBA_EXT_OFFSET,
9710 					pmbox->out_ext_byte_len);
9711 			}
9712 		}
9713 
9714 		writel(HA_MBATT, phba->HAregaddr);
9715 		readl(phba->HAregaddr); /* flush */
9716 
9717 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9718 		status = mbx->mbxStatus;
9719 	}
9720 
9721 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9722 	return status;
9723 
9724 out_not_finished:
9725 	if (processing_queue) {
9726 		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
9727 		lpfc_mbox_cmpl_put(phba, pmbox);
9728 	}
9729 	return MBX_NOT_FINISHED;
9730 }
9731 
9732 /**
9733  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9734  * @phba: Pointer to HBA context object.
9735  *
9736  * The function blocks the posting of SLI4 asynchronous mailbox commands from
9737  * the driver internal pending mailbox queue. It will then try to wait out the
9738  * possible outstanding mailbox command before return.
9739  *
9740  * Returns:
9741  * 	0 - the outstanding mailbox command completed; otherwise, the wait for
9742  * 	the outstanding mailbox command timed out.
9743  **/
9744 static int
lpfc_sli4_async_mbox_block(struct lpfc_hba * phba)9745 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9746 {
9747 	struct lpfc_sli *psli = &phba->sli;
9748 	LPFC_MBOXQ_t *mboxq;
9749 	int rc = 0;
9750 	unsigned long timeout = 0;
9751 	u32 sli_flag;
9752 	u8 cmd, subsys, opcode;
9753 
9754 	/* Mark the asynchronous mailbox command posting as blocked */
9755 	spin_lock_irq(&phba->hbalock);
9756 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9757 	/* Determine how long we might wait for the active mailbox
9758 	 * command to be gracefully completed by firmware.
9759 	 */
9760 	if (phba->sli.mbox_active)
9761 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9762 						phba->sli.mbox_active) *
9763 						1000) + jiffies;
9764 	spin_unlock_irq(&phba->hbalock);
9765 
9766 	/* Make sure the mailbox is really active */
9767 	if (timeout)
9768 		lpfc_sli4_process_missed_mbox_completions(phba);
9769 
9770 	/* Wait for the outstanding mailbox command to complete */
9771 	while (phba->sli.mbox_active) {
9772 		/* Check active mailbox complete status every 2ms */
9773 		msleep(2);
9774 		if (time_after(jiffies, timeout)) {
9775 			/* Timeout, mark the outstanding cmd not complete */
9776 
9777 			/* Sanity check sli.mbox_active has not completed or
9778 			 * cancelled from another context during last 2ms sleep,
9779 			 * so take hbalock to be sure before logging.
9780 			 */
9781 			spin_lock_irq(&phba->hbalock);
9782 			if (phba->sli.mbox_active) {
9783 				mboxq = phba->sli.mbox_active;
9784 				cmd = mboxq->u.mb.mbxCommand;
9785 				subsys = lpfc_sli_config_mbox_subsys_get(phba,
9786 									 mboxq);
9787 				opcode = lpfc_sli_config_mbox_opcode_get(phba,
9788 									 mboxq);
9789 				sli_flag = psli->sli_flag;
9790 				spin_unlock_irq(&phba->hbalock);
9791 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9792 						"2352 Mailbox command x%x "
9793 						"(x%x/x%x) sli_flag x%x could "
9794 						"not complete\n",
9795 						cmd, subsys, opcode,
9796 						sli_flag);
9797 			} else {
9798 				spin_unlock_irq(&phba->hbalock);
9799 			}
9800 
9801 			rc = 1;
9802 			break;
9803 		}
9804 	}
9805 
9806 	/* Can not cleanly block async mailbox command, fails it */
9807 	if (rc) {
9808 		spin_lock_irq(&phba->hbalock);
9809 		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9810 		spin_unlock_irq(&phba->hbalock);
9811 	}
9812 	return rc;
9813 }
9814 
9815 /**
9816  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9817  * @phba: Pointer to HBA context object.
9818  *
9819  * The function unblocks and resume posting of SLI4 asynchronous mailbox
9820  * commands from the driver internal pending mailbox queue. It makes sure
9821  * that there is no outstanding mailbox command before resuming posting
9822  * asynchronous mailbox commands. If, for any reason, there is outstanding
9823  * mailbox command, it will try to wait it out before resuming asynchronous
9824  * mailbox command posting.
9825  **/
9826 static void
lpfc_sli4_async_mbox_unblock(struct lpfc_hba * phba)9827 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9828 {
9829 	struct lpfc_sli *psli = &phba->sli;
9830 
9831 	spin_lock_irq(&phba->hbalock);
9832 	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9833 		/* Asynchronous mailbox posting is not blocked, do nothing */
9834 		spin_unlock_irq(&phba->hbalock);
9835 		return;
9836 	}
9837 
9838 	/* Outstanding synchronous mailbox command is guaranteed to be done,
9839 	 * successful or timeout, after timing-out the outstanding mailbox
9840 	 * command shall always be removed, so just unblock posting async
9841 	 * mailbox command and resume
9842 	 */
9843 	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9844 	spin_unlock_irq(&phba->hbalock);
9845 
9846 	/* wake up worker thread to post asynchronous mailbox command */
9847 	lpfc_worker_wake_up(phba);
9848 }
9849 
9850 /**
9851  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9852  * @phba: Pointer to HBA context object.
9853  * @mboxq: Pointer to mailbox object.
9854  *
9855  * The function waits for the bootstrap mailbox register ready bit from
9856  * port for twice the regular mailbox command timeout value.
9857  *
9858  *      0 - no timeout on waiting for bootstrap mailbox register ready.
9859  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out or port
9860  *                     is in an unrecoverable state.
9861  **/
9862 static int
lpfc_sli4_wait_bmbx_ready(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9863 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9864 {
9865 	uint32_t db_ready;
9866 	unsigned long timeout;
9867 	struct lpfc_register bmbx_reg;
9868 	struct lpfc_register portstat_reg = {-1};
9869 
9870 	/* Sanity check - there is no point to wait if the port is in an
9871 	 * unrecoverable state.
9872 	 */
9873 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
9874 	    LPFC_SLI_INTF_IF_TYPE_2) {
9875 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9876 			       &portstat_reg.word0) ||
9877 		    lpfc_sli4_unrecoverable_port(&portstat_reg)) {
9878 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9879 					"3858 Skipping bmbx ready because "
9880 					"Port Status x%x\n",
9881 					portstat_reg.word0);
9882 			return MBXERR_ERROR;
9883 		}
9884 	}
9885 
9886 	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
9887 				   * 1000) + jiffies;
9888 
9889 	do {
9890 		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9891 		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9892 		if (!db_ready)
9893 			mdelay(2);
9894 
9895 		if (time_after(jiffies, timeout))
9896 			return MBXERR_ERROR;
9897 	} while (!db_ready);
9898 
9899 	return 0;
9900 }
9901 
9902 /**
9903  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9904  * @phba: Pointer to HBA context object.
9905  * @mboxq: Pointer to mailbox object.
9906  *
9907  * The function posts a mailbox to the port.  The mailbox is expected
9908  * to be comletely filled in and ready for the port to operate on it.
9909  * This routine executes a synchronous completion operation on the
9910  * mailbox by polling for its completion.
9911  *
9912  * The caller must not be holding any locks when calling this routine.
9913  *
9914  * Returns:
9915  *	MBX_SUCCESS - mailbox posted successfully
9916  *	Any of the MBX error values.
9917  **/
9918 static int
lpfc_sli4_post_sync_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)9919 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9920 {
9921 	int rc = MBX_SUCCESS;
9922 	unsigned long iflag;
9923 	uint32_t mcqe_status;
9924 	uint32_t mbx_cmnd;
9925 	struct lpfc_sli *psli = &phba->sli;
9926 	struct lpfc_mqe *mb = &mboxq->u.mqe;
9927 	struct lpfc_bmbx_create *mbox_rgn;
9928 	struct dma_address *dma_address;
9929 
9930 	/*
9931 	 * Only one mailbox can be active to the bootstrap mailbox region
9932 	 * at a time and there is no queueing provided.
9933 	 */
9934 	spin_lock_irqsave(&phba->hbalock, iflag);
9935 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9936 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9937 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9938 				"(%d):2532 Mailbox command x%x (x%x/x%x) "
9939 				"cannot issue Data: x%x x%x\n",
9940 				mboxq->vport ? mboxq->vport->vpi : 0,
9941 				mboxq->u.mb.mbxCommand,
9942 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9943 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9944 				psli->sli_flag, MBX_POLL);
9945 		return MBXERR_ERROR;
9946 	}
9947 	/* The server grabs the token and owns it until release */
9948 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9949 	phba->sli.mbox_active = mboxq;
9950 	spin_unlock_irqrestore(&phba->hbalock, iflag);
9951 
9952 	/* wait for bootstrap mbox register for readyness */
9953 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9954 	if (rc)
9955 		goto exit;
9956 	/*
9957 	 * Initialize the bootstrap memory region to avoid stale data areas
9958 	 * in the mailbox post.  Then copy the caller's mailbox contents to
9959 	 * the bmbx mailbox region.
9960 	 */
9961 	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
9962 	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
9963 	lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
9964 			       sizeof(struct lpfc_mqe));
9965 
9966 	/* Post the high mailbox dma address to the port and wait for ready. */
9967 	dma_address = &phba->sli4_hba.bmbx.dma_address;
9968 	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
9969 
9970 	/* wait for bootstrap mbox register for hi-address write done */
9971 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9972 	if (rc)
9973 		goto exit;
9974 
9975 	/* Post the low mailbox dma address to the port. */
9976 	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
9977 
9978 	/* wait for bootstrap mbox register for low address write done */
9979 	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9980 	if (rc)
9981 		goto exit;
9982 
9983 	/*
9984 	 * Read the CQ to ensure the mailbox has completed.
9985 	 * If so, update the mailbox status so that the upper layers
9986 	 * can complete the request normally.
9987 	 */
9988 	lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
9989 			       sizeof(struct lpfc_mqe));
9990 	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
9991 	lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
9992 			       sizeof(struct lpfc_mcqe));
9993 	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
9994 	/*
9995 	 * When the CQE status indicates a failure and the mailbox status
9996 	 * indicates success then copy the CQE status into the mailbox status
9997 	 * (and prefix it with x4000).
9998 	 */
9999 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10000 		if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
10001 			bf_set(lpfc_mqe_status, mb,
10002 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
10003 		rc = MBXERR_ERROR;
10004 	} else
10005 		lpfc_sli4_swap_str(phba, mboxq);
10006 
10007 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10008 			"(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
10009 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
10010 			" x%x x%x CQ: x%x x%x x%x x%x\n",
10011 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10012 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10013 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10014 			bf_get(lpfc_mqe_status, mb),
10015 			mb->un.mb_words[0], mb->un.mb_words[1],
10016 			mb->un.mb_words[2], mb->un.mb_words[3],
10017 			mb->un.mb_words[4], mb->un.mb_words[5],
10018 			mb->un.mb_words[6], mb->un.mb_words[7],
10019 			mb->un.mb_words[8], mb->un.mb_words[9],
10020 			mb->un.mb_words[10], mb->un.mb_words[11],
10021 			mb->un.mb_words[12], mboxq->mcqe.word0,
10022 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
10023 			mboxq->mcqe.trailer);
10024 exit:
10025 	/* We are holding the token, no needed for lock when release */
10026 	spin_lock_irqsave(&phba->hbalock, iflag);
10027 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10028 	phba->sli.mbox_active = NULL;
10029 	spin_unlock_irqrestore(&phba->hbalock, iflag);
10030 	return rc;
10031 }
10032 
10033 /**
10034  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
10035  * @phba: Pointer to HBA context object.
10036  * @mboxq: Pointer to mailbox object.
10037  * @flag: Flag indicating how the mailbox need to be processed.
10038  *
10039  * This function is called by discovery code and HBA management code to submit
10040  * a mailbox command to firmware with SLI-4 interface spec.
10041  *
10042  * Return codes the caller owns the mailbox command after the return of the
10043  * function.
10044  **/
10045 static int
lpfc_sli_issue_mbox_s4(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint32_t flag)10046 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
10047 		       uint32_t flag)
10048 {
10049 	struct lpfc_sli *psli = &phba->sli;
10050 	unsigned long iflags;
10051 	int rc;
10052 
10053 	/* dump from issue mailbox command if setup */
10054 	lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
10055 
10056 	rc = lpfc_mbox_dev_check(phba);
10057 	if (unlikely(rc)) {
10058 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10059 				"(%d):2544 Mailbox command x%x (x%x/x%x) "
10060 				"cannot issue Data: x%x x%x\n",
10061 				mboxq->vport ? mboxq->vport->vpi : 0,
10062 				mboxq->u.mb.mbxCommand,
10063 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10064 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10065 				psli->sli_flag, flag);
10066 		goto out_not_finished;
10067 	}
10068 
10069 	/* Detect polling mode and jump to a handler */
10070 	if (!phba->sli4_hba.intr_enable) {
10071 		if (flag == MBX_POLL)
10072 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10073 		else
10074 			rc = -EIO;
10075 		if (rc != MBX_SUCCESS)
10076 			lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10077 					"(%d):2541 Mailbox command x%x "
10078 					"(x%x/x%x) failure: "
10079 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10080 					"Data: x%x x%x\n",
10081 					mboxq->vport ? mboxq->vport->vpi : 0,
10082 					mboxq->u.mb.mbxCommand,
10083 					lpfc_sli_config_mbox_subsys_get(phba,
10084 									mboxq),
10085 					lpfc_sli_config_mbox_opcode_get(phba,
10086 									mboxq),
10087 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10088 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10089 					bf_get(lpfc_mcqe_ext_status,
10090 					       &mboxq->mcqe),
10091 					psli->sli_flag, flag);
10092 		return rc;
10093 	} else if (flag == MBX_POLL) {
10094 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10095 				"(%d):2542 Try to issue mailbox command "
10096 				"x%x (x%x/x%x) synchronously ahead of async "
10097 				"mailbox command queue: x%x x%x\n",
10098 				mboxq->vport ? mboxq->vport->vpi : 0,
10099 				mboxq->u.mb.mbxCommand,
10100 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10101 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10102 				psli->sli_flag, flag);
10103 		/* Try to block the asynchronous mailbox posting */
10104 		rc = lpfc_sli4_async_mbox_block(phba);
10105 		if (!rc) {
10106 			/* Successfully blocked, now issue sync mbox cmd */
10107 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10108 			if (rc != MBX_SUCCESS)
10109 				lpfc_printf_log(phba, KERN_WARNING,
10110 					LOG_MBOX | LOG_SLI,
10111 					"(%d):2597 Sync Mailbox command "
10112 					"x%x (x%x/x%x) failure: "
10113 					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10114 					"Data: x%x x%x\n",
10115 					mboxq->vport ? mboxq->vport->vpi : 0,
10116 					mboxq->u.mb.mbxCommand,
10117 					lpfc_sli_config_mbox_subsys_get(phba,
10118 									mboxq),
10119 					lpfc_sli_config_mbox_opcode_get(phba,
10120 									mboxq),
10121 					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10122 					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10123 					bf_get(lpfc_mcqe_ext_status,
10124 					       &mboxq->mcqe),
10125 					psli->sli_flag, flag);
10126 			/* Unblock the async mailbox posting afterward */
10127 			lpfc_sli4_async_mbox_unblock(phba);
10128 		}
10129 		return rc;
10130 	}
10131 
10132 	/* Now, interrupt mode asynchronous mailbox command */
10133 	rc = lpfc_mbox_cmd_check(phba, mboxq);
10134 	if (rc) {
10135 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10136 				"(%d):2543 Mailbox command x%x (x%x/x%x) "
10137 				"cannot issue Data: x%x x%x\n",
10138 				mboxq->vport ? mboxq->vport->vpi : 0,
10139 				mboxq->u.mb.mbxCommand,
10140 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10141 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10142 				psli->sli_flag, flag);
10143 		goto out_not_finished;
10144 	}
10145 
10146 	/* Put the mailbox command to the driver internal FIFO */
10147 	psli->slistat.mbox_busy++;
10148 	spin_lock_irqsave(&phba->hbalock, iflags);
10149 	lpfc_mbox_put(phba, mboxq);
10150 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10151 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10152 			"(%d):0354 Mbox cmd issue - Enqueue Data: "
10153 			"x%x (x%x/x%x) x%x x%x x%x x%x\n",
10154 			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
10155 			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
10156 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10157 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10158 			mboxq->u.mb.un.varUnregLogin.rpi,
10159 			phba->pport->port_state,
10160 			psli->sli_flag, MBX_NOWAIT);
10161 	/* Wake up worker thread to transport mailbox command from head */
10162 	lpfc_worker_wake_up(phba);
10163 
10164 	return MBX_BUSY;
10165 
10166 out_not_finished:
10167 	return MBX_NOT_FINISHED;
10168 }
10169 
10170 /**
10171  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
10172  * @phba: Pointer to HBA context object.
10173  *
10174  * This function is called by worker thread to send a mailbox command to
10175  * SLI4 HBA firmware.
10176  *
10177  **/
10178 int
lpfc_sli4_post_async_mbox(struct lpfc_hba * phba)10179 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
10180 {
10181 	struct lpfc_sli *psli = &phba->sli;
10182 	LPFC_MBOXQ_t *mboxq;
10183 	int rc = MBX_SUCCESS;
10184 	unsigned long iflags;
10185 	struct lpfc_mqe *mqe;
10186 	uint32_t mbx_cmnd;
10187 
10188 	/* Check interrupt mode before post async mailbox command */
10189 	if (unlikely(!phba->sli4_hba.intr_enable))
10190 		return MBX_NOT_FINISHED;
10191 
10192 	/* Check for mailbox command service token */
10193 	spin_lock_irqsave(&phba->hbalock, iflags);
10194 	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
10195 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10196 		return MBX_NOT_FINISHED;
10197 	}
10198 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10199 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10200 		return MBX_NOT_FINISHED;
10201 	}
10202 	if (unlikely(phba->sli.mbox_active)) {
10203 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10204 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10205 				"0384 There is pending active mailbox cmd\n");
10206 		return MBX_NOT_FINISHED;
10207 	}
10208 	/* Take the mailbox command service token */
10209 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
10210 
10211 	/* Get the next mailbox command from head of queue */
10212 	mboxq = lpfc_mbox_get(phba);
10213 
10214 	/* If no more mailbox command waiting for post, we're done */
10215 	if (!mboxq) {
10216 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10217 		spin_unlock_irqrestore(&phba->hbalock, iflags);
10218 		return MBX_SUCCESS;
10219 	}
10220 	phba->sli.mbox_active = mboxq;
10221 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10222 
10223 	/* Check device readiness for posting mailbox command */
10224 	rc = lpfc_mbox_dev_check(phba);
10225 	if (unlikely(rc))
10226 		/* Driver clean routine will clean up pending mailbox */
10227 		goto out_not_finished;
10228 
10229 	/* Prepare the mbox command to be posted */
10230 	mqe = &mboxq->u.mqe;
10231 	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
10232 
10233 	/* Start timer for the mbox_tmo and log some mailbox post messages */
10234 	mod_timer(&psli->mbox_tmo, (jiffies +
10235 		  msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
10236 
10237 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10238 			"(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
10239 			"x%x x%x\n",
10240 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10241 			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10242 			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10243 			phba->pport->port_state, psli->sli_flag);
10244 
10245 	if (mbx_cmnd != MBX_HEARTBEAT) {
10246 		if (mboxq->vport) {
10247 			lpfc_debugfs_disc_trc(mboxq->vport,
10248 				LPFC_DISC_TRC_MBOX_VPORT,
10249 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
10250 				mbx_cmnd, mqe->un.mb_words[0],
10251 				mqe->un.mb_words[1]);
10252 		} else {
10253 			lpfc_debugfs_disc_trc(phba->pport,
10254 				LPFC_DISC_TRC_MBOX,
10255 				"MBOX Send: cmd:x%x mb:x%x x%x",
10256 				mbx_cmnd, mqe->un.mb_words[0],
10257 				mqe->un.mb_words[1]);
10258 		}
10259 	}
10260 	psli->slistat.mbox_cmd++;
10261 
10262 	/* Post the mailbox command to the port */
10263 	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
10264 	if (rc != MBX_SUCCESS) {
10265 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10266 				"(%d):2533 Mailbox command x%x (x%x/x%x) "
10267 				"cannot issue Data: x%x x%x\n",
10268 				mboxq->vport ? mboxq->vport->vpi : 0,
10269 				mboxq->u.mb.mbxCommand,
10270 				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10271 				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10272 				psli->sli_flag, MBX_NOWAIT);
10273 		goto out_not_finished;
10274 	}
10275 
10276 	return rc;
10277 
10278 out_not_finished:
10279 	spin_lock_irqsave(&phba->hbalock, iflags);
10280 	if (phba->sli.mbox_active) {
10281 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10282 		__lpfc_mbox_cmpl_put(phba, mboxq);
10283 		/* Release the token */
10284 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10285 		phba->sli.mbox_active = NULL;
10286 	}
10287 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10288 
10289 	return MBX_NOT_FINISHED;
10290 }
10291 
10292 /**
10293  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
10294  * @phba: Pointer to HBA context object.
10295  * @pmbox: Pointer to mailbox object.
10296  * @flag: Flag indicating how the mailbox need to be processed.
10297  *
10298  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
10299  * the API jump table function pointer from the lpfc_hba struct.
10300  *
10301  * Return codes the caller owns the mailbox command after the return of the
10302  * function.
10303  **/
10304 int
lpfc_sli_issue_mbox(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmbox,uint32_t flag)10305 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
10306 {
10307 	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
10308 }
10309 
10310 /**
10311  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
10312  * @phba: The hba struct for which this call is being executed.
10313  * @dev_grp: The HBA PCI-Device group number.
10314  *
10315  * This routine sets up the mbox interface API function jump table in @phba
10316  * struct.
10317  * Returns: 0 - success, -ENODEV - failure.
10318  **/
10319 int
lpfc_mbox_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)10320 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10321 {
10322 
10323 	switch (dev_grp) {
10324 	case LPFC_PCI_DEV_LP:
10325 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
10326 		phba->lpfc_sli_handle_slow_ring_event =
10327 				lpfc_sli_handle_slow_ring_event_s3;
10328 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
10329 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
10330 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
10331 		break;
10332 	case LPFC_PCI_DEV_OC:
10333 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
10334 		phba->lpfc_sli_handle_slow_ring_event =
10335 				lpfc_sli_handle_slow_ring_event_s4;
10336 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10337 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10338 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10339 		break;
10340 	default:
10341 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10342 				"1420 Invalid HBA PCI-device group: 0x%x\n",
10343 				dev_grp);
10344 		return -ENODEV;
10345 	}
10346 	return 0;
10347 }
10348 
10349 /**
10350  * __lpfc_sli_ringtx_put - Add an iocb to the txq
10351  * @phba: Pointer to HBA context object.
10352  * @pring: Pointer to driver SLI ring object.
10353  * @piocb: Pointer to address of newly added command iocb.
10354  *
10355  * This function is called with hbalock held for SLI3 ports or
10356  * the ring lock held for SLI4 ports to add a command
10357  * iocb to the txq when SLI layer cannot submit the command iocb
10358  * to the ring.
10359  **/
10360 void
__lpfc_sli_ringtx_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * piocb)10361 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10362 		    struct lpfc_iocbq *piocb)
10363 {
10364 	if (phba->sli_rev == LPFC_SLI_REV4)
10365 		lockdep_assert_held(&pring->ring_lock);
10366 	else
10367 		lockdep_assert_held(&phba->hbalock);
10368 	/* Insert the caller's iocb in the txq tail for later processing. */
10369 	list_add_tail(&piocb->list, &pring->txq);
10370 }
10371 
10372 /**
10373  * lpfc_sli_next_iocb - Get the next iocb in the txq
10374  * @phba: Pointer to HBA context object.
10375  * @pring: Pointer to driver SLI ring object.
10376  * @piocb: Pointer to address of newly added command iocb.
10377  *
10378  * This function is called with hbalock held before a new
10379  * iocb is submitted to the firmware. This function checks
10380  * txq to flush the iocbs in txq to Firmware before
10381  * submitting new iocbs to the Firmware.
10382  * If there are iocbs in the txq which need to be submitted
10383  * to firmware, lpfc_sli_next_iocb returns the first element
10384  * of the txq after dequeuing it from txq.
10385  * If there is no iocb in the txq then the function will return
10386  * *piocb and *piocb is set to NULL. Caller needs to check
10387  * *piocb to find if there are more commands in the txq.
10388  **/
10389 static struct lpfc_iocbq *
lpfc_sli_next_iocb(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq ** piocb)10390 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10391 		   struct lpfc_iocbq **piocb)
10392 {
10393 	struct lpfc_iocbq * nextiocb;
10394 
10395 	lockdep_assert_held(&phba->hbalock);
10396 
10397 	nextiocb = lpfc_sli_ringtx_get(phba, pring);
10398 	if (!nextiocb) {
10399 		nextiocb = *piocb;
10400 		*piocb = NULL;
10401 	}
10402 
10403 	return nextiocb;
10404 }
10405 
10406 /**
10407  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
10408  * @phba: Pointer to HBA context object.
10409  * @ring_number: SLI ring number to issue iocb on.
10410  * @piocb: Pointer to command iocb.
10411  * @flag: Flag indicating if this command can be put into txq.
10412  *
10413  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10414  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10415  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10416  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10417  * this function allows only iocbs for posting buffers. This function finds
10418  * next available slot in the command ring and posts the command to the
10419  * available slot and writes the port attention register to request HBA start
10420  * processing new iocb. If there is no slot available in the ring and
10421  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10422  * the function returns IOCB_BUSY.
10423  *
10424  * This function is called with hbalock held. The function will return success
10425  * after it successfully submit the iocb to firmware or after adding to the
10426  * txq.
10427  **/
10428 static int
__lpfc_sli_issue_iocb_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10429 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
10430 		    struct lpfc_iocbq *piocb, uint32_t flag)
10431 {
10432 	struct lpfc_iocbq *nextiocb;
10433 	IOCB_t *iocb;
10434 	struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
10435 
10436 	lockdep_assert_held(&phba->hbalock);
10437 
10438 	if (piocb->cmd_cmpl && (!piocb->vport) &&
10439 	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10440 	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
10441 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10442 				"1807 IOCB x%x failed. No vport\n",
10443 				piocb->iocb.ulpCommand);
10444 		dump_stack();
10445 		return IOCB_ERROR;
10446 	}
10447 
10448 
10449 	/* If the PCI channel is in offline state, do not post iocbs. */
10450 	if (unlikely(pci_channel_offline(phba->pcidev)))
10451 		return IOCB_ERROR;
10452 
10453 	/* If HBA has a deferred error attention, fail the iocb. */
10454 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
10455 		return IOCB_ERROR;
10456 
10457 	/*
10458 	 * We should never get an IOCB if we are in a < LINK_DOWN state
10459 	 */
10460 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10461 		return IOCB_ERROR;
10462 
10463 	/*
10464 	 * Check to see if we are blocking IOCB processing because of a
10465 	 * outstanding event.
10466 	 */
10467 	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
10468 		goto iocb_busy;
10469 
10470 	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
10471 		/*
10472 		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
10473 		 * can be issued if the link is not up.
10474 		 */
10475 		switch (piocb->iocb.ulpCommand) {
10476 		case CMD_QUE_RING_BUF_CN:
10477 		case CMD_QUE_RING_BUF64_CN:
10478 			/*
10479 			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10480 			 * completion, cmd_cmpl MUST be 0.
10481 			 */
10482 			if (piocb->cmd_cmpl)
10483 				piocb->cmd_cmpl = NULL;
10484 			fallthrough;
10485 		case CMD_CREATE_XRI_CR:
10486 		case CMD_CLOSE_XRI_CN:
10487 		case CMD_CLOSE_XRI_CX:
10488 			break;
10489 		default:
10490 			goto iocb_busy;
10491 		}
10492 
10493 	/*
10494 	 * For FCP commands, we must be in a state where we can process link
10495 	 * attention events.
10496 	 */
10497 	} else if (unlikely(pring->ringno == LPFC_FCP_RING &&
10498 			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
10499 		goto iocb_busy;
10500 	}
10501 
10502 	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10503 	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10504 		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10505 
10506 	if (iocb)
10507 		lpfc_sli_update_ring(phba, pring);
10508 	else
10509 		lpfc_sli_update_full_ring(phba, pring);
10510 
10511 	if (!piocb)
10512 		return IOCB_SUCCESS;
10513 
10514 	goto out_busy;
10515 
10516  iocb_busy:
10517 	pring->stats.iocb_cmd_delay++;
10518 
10519  out_busy:
10520 
10521 	if (!(flag & SLI_IOCB_RET_IOCB)) {
10522 		__lpfc_sli_ringtx_put(phba, pring, piocb);
10523 		return IOCB_SUCCESS;
10524 	}
10525 
10526 	return IOCB_BUSY;
10527 }
10528 
10529 /**
10530  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10531  * @phba: Pointer to HBA context object.
10532  * @ring_number: SLI ring number to issue wqe on.
10533  * @piocb: Pointer to command iocb.
10534  * @flag: Flag indicating if this command can be put into txq.
10535  *
10536  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10537  * send  an iocb command to an HBA with SLI-3 interface spec.
10538  *
10539  * This function takes the hbalock before invoking the lockless version.
10540  * The function will return success after it successfully submit the wqe to
10541  * firmware or after adding to the txq.
10542  **/
10543 static int
__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10544 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10545 			   struct lpfc_iocbq *piocb, uint32_t flag)
10546 {
10547 	unsigned long iflags;
10548 	int rc;
10549 
10550 	spin_lock_irqsave(&phba->hbalock, iflags);
10551 	rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10552 	spin_unlock_irqrestore(&phba->hbalock, iflags);
10553 
10554 	return rc;
10555 }
10556 
10557 /**
10558  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10559  * @phba: Pointer to HBA context object.
10560  * @ring_number: SLI ring number to issue wqe on.
10561  * @piocb: Pointer to command iocb.
10562  * @flag: Flag indicating if this command can be put into txq.
10563  *
10564  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10565  * an wqe command to an HBA with SLI-4 interface spec.
10566  *
10567  * This function is a lockless version. The function will return success
10568  * after it successfully submit the wqe to firmware or after adding to the
10569  * txq.
10570  **/
10571 static int
__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10572 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10573 			   struct lpfc_iocbq *piocb, uint32_t flag)
10574 {
10575 	struct lpfc_io_buf *lpfc_cmd = piocb->io_buf;
10576 
10577 	lpfc_prep_embed_io(phba, lpfc_cmd);
10578 	return lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10579 }
10580 
10581 void
lpfc_prep_embed_io(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_cmd)10582 lpfc_prep_embed_io(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
10583 {
10584 	struct lpfc_iocbq *piocb = &lpfc_cmd->cur_iocbq;
10585 	union lpfc_wqe128 *wqe = &lpfc_cmd->cur_iocbq.wqe;
10586 	struct sli4_sge_le *sgl;
10587 	u32 type_size;
10588 
10589 	/* 128 byte wqe support here */
10590 	sgl = (struct sli4_sge_le *)lpfc_cmd->dma_sgl;
10591 
10592 	if (phba->fcp_embed_io) {
10593 		struct fcp_cmnd *fcp_cmnd;
10594 		u32 *ptr;
10595 
10596 		fcp_cmnd = lpfc_cmd->fcp_cmnd;
10597 
10598 		/* Word 0-2 - FCP_CMND */
10599 		type_size = le32_to_cpu(sgl->sge_len);
10600 		type_size |= ULP_BDE64_TYPE_BDE_IMMED;
10601 		wqe->generic.bde.tus.w = type_size;
10602 		wqe->generic.bde.addrHigh = 0;
10603 		wqe->generic.bde.addrLow =  72;  /* Word 18 */
10604 
10605 		bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10606 		bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10607 
10608 		/* Word 18-29  FCP CMND Payload */
10609 		ptr = &wqe->words[18];
10610 		lpfc_sli_pcimem_bcopy(fcp_cmnd, ptr, le32_to_cpu(sgl->sge_len));
10611 	} else {
10612 		/* Word 0-2 - Inline BDE */
10613 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10614 		wqe->generic.bde.tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
10615 		wqe->generic.bde.addrHigh = le32_to_cpu(sgl->addr_hi);
10616 		wqe->generic.bde.addrLow = le32_to_cpu(sgl->addr_lo);
10617 
10618 		/* Word 10 */
10619 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10620 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10621 	}
10622 
10623 	/* add the VMID tags as per switch response */
10624 	if (unlikely(piocb->cmd_flag & LPFC_IO_VMID)) {
10625 		if (phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
10626 			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10627 			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10628 					(piocb->vmid_tag.cs_ctl_vmid));
10629 		} else if (phba->cfg_vmid_app_header) {
10630 			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
10631 			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10632 			wqe->words[31] = piocb->vmid_tag.app_id;
10633 		}
10634 	}
10635 }
10636 
10637 /**
10638  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10639  * @phba: Pointer to HBA context object.
10640  * @ring_number: SLI ring number to issue iocb on.
10641  * @piocb: Pointer to command iocb.
10642  * @flag: Flag indicating if this command can be put into txq.
10643  *
10644  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10645  * an iocb command to an HBA with SLI-4 interface spec.
10646  *
10647  * This function is called with ringlock held. The function will return success
10648  * after it successfully submit the iocb to firmware or after adding to the
10649  * txq.
10650  **/
10651 static int
__lpfc_sli_issue_iocb_s4(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10652 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10653 			 struct lpfc_iocbq *piocb, uint32_t flag)
10654 {
10655 	struct lpfc_sglq *sglq;
10656 	union lpfc_wqe128 *wqe;
10657 	struct lpfc_queue *wq;
10658 	struct lpfc_sli_ring *pring;
10659 	u32 ulp_command = get_job_cmnd(phba, piocb);
10660 
10661 	/* Get the WQ */
10662 	if ((piocb->cmd_flag & LPFC_IO_FCP) ||
10663 	    (piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
10664 		wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10665 	} else {
10666 		wq = phba->sli4_hba.els_wq;
10667 	}
10668 
10669 	/* Get corresponding ring */
10670 	pring = wq->pring;
10671 
10672 	/*
10673 	 * The WQE can be either 64 or 128 bytes,
10674 	 */
10675 
10676 	lockdep_assert_held(&pring->ring_lock);
10677 	wqe = &piocb->wqe;
10678 	if (piocb->sli4_xritag == NO_XRI) {
10679 		if (ulp_command == CMD_ABORT_XRI_CX)
10680 			sglq = NULL;
10681 		else {
10682 			sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10683 			if (!sglq) {
10684 				if (!(flag & SLI_IOCB_RET_IOCB)) {
10685 					__lpfc_sli_ringtx_put(phba,
10686 							pring,
10687 							piocb);
10688 					return IOCB_SUCCESS;
10689 				} else {
10690 					return IOCB_BUSY;
10691 				}
10692 			}
10693 		}
10694 	} else if (piocb->cmd_flag &  LPFC_IO_FCP) {
10695 		/* These IO's already have an XRI and a mapped sgl. */
10696 		sglq = NULL;
10697 	}
10698 	else {
10699 		/*
10700 		 * This is a continuation of a commandi,(CX) so this
10701 		 * sglq is on the active list
10702 		 */
10703 		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10704 		if (!sglq)
10705 			return IOCB_ERROR;
10706 	}
10707 
10708 	if (sglq) {
10709 		piocb->sli4_lxritag = sglq->sli4_lxritag;
10710 		piocb->sli4_xritag = sglq->sli4_xritag;
10711 
10712 		/* ABTS sent by initiator to CT exchange, the
10713 		 * RX_ID field will be filled with the newly
10714 		 * allocated responder XRI.
10715 		 */
10716 		if (ulp_command == CMD_XMIT_BLS_RSP64_CX &&
10717 		    piocb->abort_bls == LPFC_ABTS_UNSOL_INT)
10718 			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10719 			       piocb->sli4_xritag);
10720 
10721 		bf_set(wqe_xri_tag, &wqe->generic.wqe_com,
10722 		       piocb->sli4_xritag);
10723 
10724 		if (lpfc_wqe_bpl2sgl(phba, piocb, sglq) == NO_XRI)
10725 			return IOCB_ERROR;
10726 	}
10727 
10728 	if (lpfc_sli4_wq_put(wq, wqe))
10729 		return IOCB_ERROR;
10730 
10731 	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10732 
10733 	return 0;
10734 }
10735 
10736 /*
10737  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10738  *
10739  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10740  * or IOCB for sli-3  function.
10741  * pointer from the lpfc_hba struct.
10742  *
10743  * Return codes:
10744  * IOCB_ERROR - Error
10745  * IOCB_SUCCESS - Success
10746  * IOCB_BUSY - Busy
10747  **/
10748 int
lpfc_sli_issue_fcp_io(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10749 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10750 		      struct lpfc_iocbq *piocb, uint32_t flag)
10751 {
10752 	return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10753 }
10754 
10755 /*
10756  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10757  *
10758  * This routine wraps the actual lockless version for issusing IOCB function
10759  * pointer from the lpfc_hba struct.
10760  *
10761  * Return codes:
10762  * IOCB_ERROR - Error
10763  * IOCB_SUCCESS - Success
10764  * IOCB_BUSY - Busy
10765  **/
10766 int
__lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)10767 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10768 		struct lpfc_iocbq *piocb, uint32_t flag)
10769 {
10770 	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10771 }
10772 
10773 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)10774 __lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq *cmdiocbq,
10775 			       struct lpfc_vport *vport,
10776 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10777 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10778 {
10779 	struct lpfc_hba *phba = vport->phba;
10780 	IOCB_t *cmd;
10781 
10782 	cmd = &cmdiocbq->iocb;
10783 	memset(cmd, 0, sizeof(*cmd));
10784 
10785 	cmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10786 	cmd->un.elsreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10787 	cmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10788 
10789 	if (expect_rsp) {
10790 		cmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
10791 		cmd->un.elsreq64.remoteID = did; /* DID */
10792 		cmd->ulpCommand = CMD_ELS_REQUEST64_CR;
10793 		cmd->ulpTimeout = tmo;
10794 	} else {
10795 		cmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
10796 		cmd->un.genreq64.xmit_els_remoteID = did; /* DID */
10797 		cmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
10798 		cmd->ulpPU = PARM_NPIV_DID;
10799 	}
10800 	cmd->ulpBdeCount = 1;
10801 	cmd->ulpLe = 1;
10802 	cmd->ulpClass = CLASS3;
10803 
10804 	/* If we have NPIV enabled, we want to send ELS traffic by VPI. */
10805 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
10806 		if (expect_rsp) {
10807 			cmd->un.elsreq64.myID = vport->fc_myDID;
10808 
10809 			/* For ELS_REQUEST64_CR, use the VPI by default */
10810 			cmd->ulpContext = phba->vpi_ids[vport->vpi];
10811 		}
10812 
10813 		cmd->ulpCt_h = 0;
10814 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10815 		if (elscmd == ELS_CMD_ECHO)
10816 			cmd->ulpCt_l = 0; /* context = invalid RPI */
10817 		else
10818 			cmd->ulpCt_l = 1; /* context = VPI */
10819 	}
10820 }
10821 
10822 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)10823 __lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq *cmdiocbq,
10824 			       struct lpfc_vport *vport,
10825 			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10826 			       u32 elscmd, u8 tmo, u8 expect_rsp)
10827 {
10828 	struct lpfc_hba  *phba = vport->phba;
10829 	union lpfc_wqe128 *wqe;
10830 	struct ulp_bde64_le *bde;
10831 	u8 els_id;
10832 
10833 	wqe = &cmdiocbq->wqe;
10834 	memset(wqe, 0, sizeof(*wqe));
10835 
10836 	/* Word 0 - 2 BDE */
10837 	bde = (struct ulp_bde64_le *)&wqe->generic.bde;
10838 	bde->addr_low = cpu_to_le32(putPaddrLow(bmp->phys));
10839 	bde->addr_high = cpu_to_le32(putPaddrHigh(bmp->phys));
10840 	bde->type_size = cpu_to_le32(cmd_size);
10841 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10842 
10843 	if (expect_rsp) {
10844 		bf_set(wqe_cmnd, &wqe->els_req.wqe_com, CMD_ELS_REQUEST64_WQE);
10845 
10846 		/* Transfer length */
10847 		wqe->els_req.payload_len = cmd_size;
10848 		wqe->els_req.max_response_payload_len = FCELSSIZE;
10849 
10850 		/* DID */
10851 		bf_set(wqe_els_did, &wqe->els_req.wqe_dest, did);
10852 
10853 		/* Word 11 - ELS_ID */
10854 		switch (elscmd) {
10855 		case ELS_CMD_PLOGI:
10856 			els_id = LPFC_ELS_ID_PLOGI;
10857 			break;
10858 		case ELS_CMD_FLOGI:
10859 			els_id = LPFC_ELS_ID_FLOGI;
10860 			break;
10861 		case ELS_CMD_LOGO:
10862 			els_id = LPFC_ELS_ID_LOGO;
10863 			break;
10864 		case ELS_CMD_FDISC:
10865 			if (!vport->fc_myDID) {
10866 				els_id = LPFC_ELS_ID_FDISC;
10867 				break;
10868 			}
10869 			fallthrough;
10870 		default:
10871 			els_id = LPFC_ELS_ID_DEFAULT;
10872 			break;
10873 		}
10874 
10875 		bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10876 	} else {
10877 		/* DID */
10878 		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest, did);
10879 
10880 		/* Transfer length */
10881 		wqe->xmit_els_rsp.response_payload_len = cmd_size;
10882 
10883 		bf_set(wqe_cmnd, &wqe->xmit_els_rsp.wqe_com,
10884 		       CMD_XMIT_ELS_RSP64_WQE);
10885 	}
10886 
10887 	bf_set(wqe_tmo, &wqe->generic.wqe_com, tmo);
10888 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, cmdiocbq->iotag);
10889 	bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
10890 
10891 	/* If we have NPIV enabled, we want to send ELS traffic by VPI.
10892 	 * For SLI4, since the driver controls VPIs we also want to include
10893 	 * all ELS pt2pt protocol traffic as well.
10894 	 */
10895 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
10896 	    test_bit(FC_PT2PT, &vport->fc_flag)) {
10897 		if (expect_rsp) {
10898 			bf_set(els_req64_sid, &wqe->els_req, vport->fc_myDID);
10899 
10900 			/* For ELS_REQUEST64_WQE, use the VPI by default */
10901 			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10902 			       phba->vpi_ids[vport->vpi]);
10903 		}
10904 
10905 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10906 		if (elscmd == ELS_CMD_ECHO)
10907 			bf_set(wqe_ct, &wqe->generic.wqe_com, 0);
10908 		else
10909 			bf_set(wqe_ct, &wqe->generic.wqe_com, 1);
10910 	}
10911 }
10912 
10913 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)10914 lpfc_sli_prep_els_req_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
10915 			  struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
10916 			  u16 cmd_size, u32 did, u32 elscmd, u8 tmo,
10917 			  u8 expect_rsp)
10918 {
10919 	phba->__lpfc_sli_prep_els_req_rsp(cmdiocbq, vport, bmp, cmd_size, did,
10920 					  elscmd, tmo, expect_rsp);
10921 }
10922 
10923 static void
__lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10924 __lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10925 			   u16 rpi, u32 num_entry, u8 tmo)
10926 {
10927 	IOCB_t *cmd;
10928 
10929 	cmd = &cmdiocbq->iocb;
10930 	memset(cmd, 0, sizeof(*cmd));
10931 
10932 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10933 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10934 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10935 	cmd->un.genreq64.bdl.bdeSize = num_entry * sizeof(struct ulp_bde64);
10936 
10937 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
10938 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
10939 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
10940 
10941 	cmd->ulpContext = rpi;
10942 	cmd->ulpClass = CLASS3;
10943 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
10944 	cmd->ulpBdeCount = 1;
10945 	cmd->ulpLe = 1;
10946 	cmd->ulpOwner = OWN_CHIP;
10947 	cmd->ulpTimeout = tmo;
10948 }
10949 
10950 static void
__lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq * cmdiocbq,struct lpfc_dmabuf * bmp,u16 rpi,u32 num_entry,u8 tmo)10951 __lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10952 			   u16 rpi, u32 num_entry, u8 tmo)
10953 {
10954 	union lpfc_wqe128 *cmdwqe;
10955 	struct ulp_bde64_le *bde, *bpl;
10956 	u32 xmit_len = 0, total_len = 0, size, type, i;
10957 
10958 	cmdwqe = &cmdiocbq->wqe;
10959 	memset(cmdwqe, 0, sizeof(*cmdwqe));
10960 
10961 	/* Calculate total_len and xmit_len */
10962 	bpl = (struct ulp_bde64_le *)bmp->virt;
10963 	for (i = 0; i < num_entry; i++) {
10964 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10965 		total_len += size;
10966 	}
10967 	for (i = 0; i < num_entry; i++) {
10968 		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
10969 		type = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_TYPE_MASK;
10970 		if (type != ULP_BDE64_TYPE_BDE_64)
10971 			break;
10972 		xmit_len += size;
10973 	}
10974 
10975 	/* Words 0 - 2 */
10976 	bde = (struct ulp_bde64_le *)&cmdwqe->generic.bde;
10977 	bde->addr_low = bpl->addr_low;
10978 	bde->addr_high = bpl->addr_high;
10979 	bde->type_size = cpu_to_le32(xmit_len);
10980 	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10981 
10982 	/* Word 3 */
10983 	cmdwqe->gen_req.request_payload_len = xmit_len;
10984 
10985 	/* Word 5 */
10986 	bf_set(wqe_type, &cmdwqe->gen_req.wge_ctl, FC_TYPE_CT);
10987 	bf_set(wqe_rctl, &cmdwqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);
10988 	bf_set(wqe_si, &cmdwqe->gen_req.wge_ctl, 1);
10989 	bf_set(wqe_la, &cmdwqe->gen_req.wge_ctl, 1);
10990 
10991 	/* Word 6 */
10992 	bf_set(wqe_ctxt_tag, &cmdwqe->gen_req.wqe_com, rpi);
10993 
10994 	/* Word 7 */
10995 	bf_set(wqe_tmo, &cmdwqe->gen_req.wqe_com, tmo);
10996 	bf_set(wqe_class, &cmdwqe->gen_req.wqe_com, CLASS3);
10997 	bf_set(wqe_cmnd, &cmdwqe->gen_req.wqe_com, CMD_GEN_REQUEST64_CR);
10998 	bf_set(wqe_ct, &cmdwqe->gen_req.wqe_com, SLI4_CT_RPI);
10999 
11000 	/* Word 12 */
11001 	cmdwqe->gen_req.max_response_payload_len = total_len - xmit_len;
11002 }
11003 
11004 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)11005 lpfc_sli_prep_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11006 		      struct lpfc_dmabuf *bmp, u16 rpi, u32 num_entry, u8 tmo)
11007 {
11008 	phba->__lpfc_sli_prep_gen_req(cmdiocbq, bmp, rpi, num_entry, tmo);
11009 }
11010 
11011 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)11012 __lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq *cmdiocbq,
11013 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11014 			      u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11015 {
11016 	IOCB_t *icmd;
11017 
11018 	icmd = &cmdiocbq->iocb;
11019 	memset(icmd, 0, sizeof(*icmd));
11020 
11021 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
11022 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
11023 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
11024 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
11025 	icmd->un.xseq64.w5.hcsw.Fctl = LA;
11026 	if (last_seq)
11027 		icmd->un.xseq64.w5.hcsw.Fctl |= LS;
11028 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11029 	icmd->un.xseq64.w5.hcsw.Rctl = rctl;
11030 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
11031 
11032 	icmd->ulpBdeCount = 1;
11033 	icmd->ulpLe = 1;
11034 	icmd->ulpClass = CLASS3;
11035 
11036 	switch (cr_cx_cmd) {
11037 	case CMD_XMIT_SEQUENCE64_CR:
11038 		icmd->ulpContext = rpi;
11039 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
11040 		break;
11041 	case CMD_XMIT_SEQUENCE64_CX:
11042 		icmd->ulpContext = ox_id;
11043 		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
11044 		break;
11045 	default:
11046 		break;
11047 	}
11048 }
11049 
11050 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)11051 __lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq *cmdiocbq,
11052 			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11053 			      u32 full_size, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11054 {
11055 	union lpfc_wqe128 *wqe;
11056 	struct ulp_bde64 *bpl;
11057 
11058 	wqe = &cmdiocbq->wqe;
11059 	memset(wqe, 0, sizeof(*wqe));
11060 
11061 	/* Words 0 - 2 */
11062 	bpl = (struct ulp_bde64 *)bmp->virt;
11063 	wqe->xmit_sequence.bde.addrHigh = bpl->addrHigh;
11064 	wqe->xmit_sequence.bde.addrLow = bpl->addrLow;
11065 	wqe->xmit_sequence.bde.tus.w = bpl->tus.w;
11066 
11067 	/* Word 5 */
11068 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, last_seq);
11069 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 1);
11070 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
11071 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, rctl);
11072 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_CT);
11073 
11074 	/* Word 6 */
11075 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com, rpi);
11076 
11077 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
11078 	       CMD_XMIT_SEQUENCE64_WQE);
11079 
11080 	/* Word 7 */
11081 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
11082 
11083 	/* Word 9 */
11084 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ox_id);
11085 
11086 	if (cmdiocbq->cmd_flag & (LPFC_IO_LIBDFC | LPFC_IO_LOOPBACK)) {
11087 		/* Word 10 */
11088 		if (cmdiocbq->cmd_flag & LPFC_IO_VMID) {
11089 			bf_set(wqe_appid, &wqe->xmit_sequence.wqe_com, 1);
11090 			bf_set(wqe_wqes, &wqe->xmit_sequence.wqe_com, 1);
11091 			wqe->words[31] = LOOPBACK_SRC_APPID;
11092 		}
11093 
11094 		/* Word 12 */
11095 		wqe->xmit_sequence.xmit_len = full_size;
11096 	}
11097 	else
11098 		wqe->xmit_sequence.xmit_len =
11099 			wqe->xmit_sequence.bde.tus.f.bdeSize;
11100 }
11101 
11102 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)11103 lpfc_sli_prep_xmit_seq64(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11104 			 struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11105 			 u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11106 {
11107 	phba->__lpfc_sli_prep_xmit_seq64(cmdiocbq, bmp, rpi, ox_id, num_entry,
11108 					 rctl, last_seq, cr_cx_cmd);
11109 }
11110 
11111 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)11112 __lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11113 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11114 			     bool wqec)
11115 {
11116 	IOCB_t *icmd = NULL;
11117 
11118 	icmd = &cmdiocbq->iocb;
11119 	memset(icmd, 0, sizeof(*icmd));
11120 
11121 	/* Word 5 */
11122 	icmd->un.acxri.abortContextTag = ulp_context;
11123 	icmd->un.acxri.abortIoTag = iotag;
11124 
11125 	if (ia) {
11126 		/* Word 7 */
11127 		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
11128 	} else {
11129 		/* Word 3 */
11130 		icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
11131 
11132 		/* Word 7 */
11133 		icmd->ulpClass = ulp_class;
11134 		icmd->ulpCommand = CMD_ABORT_XRI_CN;
11135 	}
11136 
11137 	/* Word 7 */
11138 	icmd->ulpLe = 1;
11139 }
11140 
11141 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)11142 __lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11143 			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11144 			     bool wqec)
11145 {
11146 	union lpfc_wqe128 *wqe;
11147 
11148 	wqe = &cmdiocbq->wqe;
11149 	memset(wqe, 0, sizeof(*wqe));
11150 
11151 	/* Word 3 */
11152 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
11153 	if (ia)
11154 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
11155 	else
11156 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
11157 
11158 	/* Word 7 */
11159 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_WQE);
11160 
11161 	/* Word 8 */
11162 	wqe->abort_cmd.wqe_com.abort_tag = ulp_context;
11163 
11164 	/* Word 9 */
11165 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, iotag);
11166 
11167 	/* Word 10 */
11168 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
11169 
11170 	/* Word 11 */
11171 	if (wqec)
11172 		bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
11173 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, cqid);
11174 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11175 }
11176 
11177 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)11178 lpfc_sli_prep_abort_xri(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11179 			u16 ulp_context, u16 iotag, u8 ulp_class, u16 cqid,
11180 			bool ia, bool wqec)
11181 {
11182 	phba->__lpfc_sli_prep_abort_xri(cmdiocbq, ulp_context, iotag, ulp_class,
11183 					cqid, ia, wqec);
11184 }
11185 
11186 /**
11187  * lpfc_sli_api_table_setup - Set up sli api function jump table
11188  * @phba: The hba struct for which this call is being executed.
11189  * @dev_grp: The HBA PCI-Device group number.
11190  *
11191  * This routine sets up the SLI interface API function jump table in @phba
11192  * struct.
11193  * Returns: 0 - success, -ENODEV - failure.
11194  **/
11195 int
lpfc_sli_api_table_setup(struct lpfc_hba * phba,uint8_t dev_grp)11196 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11197 {
11198 
11199 	switch (dev_grp) {
11200 	case LPFC_PCI_DEV_LP:
11201 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11202 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
11203 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
11204 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s3;
11205 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s3;
11206 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s3;
11207 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s3;
11208 		break;
11209 	case LPFC_PCI_DEV_OC:
11210 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11211 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
11212 		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
11213 		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s4;
11214 		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s4;
11215 		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s4;
11216 		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s4;
11217 		break;
11218 	default:
11219 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11220 				"1419 Invalid HBA PCI-device group: 0x%x\n",
11221 				dev_grp);
11222 		return -ENODEV;
11223 	}
11224 	return 0;
11225 }
11226 
11227 /**
11228  * lpfc_sli4_calc_ring - Calculates which ring to use
11229  * @phba: Pointer to HBA context object.
11230  * @piocb: Pointer to command iocb.
11231  *
11232  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11233  * hba_wqidx, thus we need to calculate the corresponding ring.
11234  * Since ABORTS must go on the same WQ of the command they are
11235  * aborting, we use command's hba_wqidx.
11236  */
11237 struct lpfc_sli_ring *
lpfc_sli4_calc_ring(struct lpfc_hba * phba,struct lpfc_iocbq * piocb)11238 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
11239 {
11240 	struct lpfc_io_buf *lpfc_cmd;
11241 
11242 	if (piocb->cmd_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
11243 		if (unlikely(!phba->sli4_hba.hdwq))
11244 			return NULL;
11245 		/*
11246 		 * for abort iocb hba_wqidx should already
11247 		 * be setup based on what work queue we used.
11248 		 */
11249 		if (!(piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
11250 			lpfc_cmd = piocb->io_buf;
11251 			piocb->hba_wqidx = lpfc_cmd->hdwq_no;
11252 		}
11253 		return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
11254 	} else {
11255 		if (unlikely(!phba->sli4_hba.els_wq))
11256 			return NULL;
11257 		piocb->hba_wqidx = 0;
11258 		return phba->sli4_hba.els_wq->pring;
11259 	}
11260 }
11261 
lpfc_sli4_poll_eq(struct lpfc_queue * eq)11262 inline void lpfc_sli4_poll_eq(struct lpfc_queue *eq)
11263 {
11264 	struct lpfc_hba *phba = eq->phba;
11265 
11266 	/*
11267 	 * Unlocking an irq is one of the entry point to check
11268 	 * for re-schedule, but we are good for io submission
11269 	 * path as midlayer does a get_cpu to glue us in. Flush
11270 	 * out the invalidate queue so we can see the updated
11271 	 * value for flag.
11272 	 */
11273 	smp_rmb();
11274 
11275 	if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
11276 		/* We will not likely get the completion for the caller
11277 		 * during this iteration but i guess that's fine.
11278 		 * Future io's coming on this eq should be able to
11279 		 * pick it up.  As for the case of single io's, they
11280 		 * will be handled through a sched from polling timer
11281 		 * function which is currently triggered every 1msec.
11282 		 */
11283 		lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM,
11284 				     LPFC_QUEUE_WORK);
11285 }
11286 
11287 /**
11288  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
11289  * @phba: Pointer to HBA context object.
11290  * @ring_number: Ring number
11291  * @piocb: Pointer to command iocb.
11292  * @flag: Flag indicating if this command can be put into txq.
11293  *
11294  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11295  * function. This function gets the hbalock and calls
11296  * __lpfc_sli_issue_iocb function and will return the error returned
11297  * by __lpfc_sli_issue_iocb function. This wrapper is used by
11298  * functions which do not hold hbalock.
11299  **/
11300 int
lpfc_sli_issue_iocb(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,uint32_t flag)11301 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11302 		    struct lpfc_iocbq *piocb, uint32_t flag)
11303 {
11304 	struct lpfc_sli_ring *pring;
11305 	struct lpfc_queue *eq;
11306 	unsigned long iflags;
11307 	int rc;
11308 
11309 	/* If the PCI channel is in offline state, do not post iocbs. */
11310 	if (unlikely(pci_channel_offline(phba->pcidev)))
11311 		return IOCB_ERROR;
11312 
11313 	if (phba->sli_rev == LPFC_SLI_REV4) {
11314 		lpfc_sli_prep_wqe(phba, piocb);
11315 
11316 		eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11317 
11318 		pring = lpfc_sli4_calc_ring(phba, piocb);
11319 		if (unlikely(pring == NULL))
11320 			return IOCB_ERROR;
11321 
11322 		spin_lock_irqsave(&pring->ring_lock, iflags);
11323 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11324 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
11325 
11326 		lpfc_sli4_poll_eq(eq);
11327 	} else {
11328 		/* For now, SLI2/3 will still use hbalock */
11329 		spin_lock_irqsave(&phba->hbalock, iflags);
11330 		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11331 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11332 	}
11333 	return rc;
11334 }
11335 
11336 /**
11337  * lpfc_extra_ring_setup - Extra ring setup function
11338  * @phba: Pointer to HBA context object.
11339  *
11340  * This function is called while driver attaches with the
11341  * HBA to setup the extra ring. The extra ring is used
11342  * only when driver needs to support target mode functionality
11343  * or IP over FC functionalities.
11344  *
11345  * This function is called with no lock held. SLI3 only.
11346  **/
11347 static int
lpfc_extra_ring_setup(struct lpfc_hba * phba)11348 lpfc_extra_ring_setup( struct lpfc_hba *phba)
11349 {
11350 	struct lpfc_sli *psli;
11351 	struct lpfc_sli_ring *pring;
11352 
11353 	psli = &phba->sli;
11354 
11355 	/* Adjust cmd/rsp ring iocb entries more evenly */
11356 
11357 	/* Take some away from the FCP ring */
11358 	pring = &psli->sli3_ring[LPFC_FCP_RING];
11359 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11360 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11361 	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11362 	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11363 
11364 	/* and give them to the extra ring */
11365 	pring = &psli->sli3_ring[LPFC_EXTRA_RING];
11366 
11367 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11368 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11369 	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11370 	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11371 
11372 	/* Setup default profile for this ring */
11373 	pring->iotag_max = 4096;
11374 	pring->num_mask = 1;
11375 	pring->prt[0].profile = 0;      /* Mask 0 */
11376 	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11377 	pring->prt[0].type = phba->cfg_multi_ring_type;
11378 	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11379 	return 0;
11380 }
11381 
11382 static void
lpfc_sli_post_recovery_event(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)11383 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11384 			     struct lpfc_nodelist *ndlp)
11385 {
11386 	unsigned long iflags;
11387 	struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
11388 
11389 	/* Hold a node reference for outstanding queued work */
11390 	if (!lpfc_nlp_get(ndlp))
11391 		return;
11392 
11393 	spin_lock_irqsave(&phba->hbalock, iflags);
11394 	if (!list_empty(&evtp->evt_listp)) {
11395 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11396 		lpfc_nlp_put(ndlp);
11397 		return;
11398 	}
11399 
11400 	evtp->evt_arg1 = ndlp;
11401 	evtp->evt = LPFC_EVT_RECOVER_PORT;
11402 	list_add_tail(&evtp->evt_listp, &phba->work_list);
11403 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11404 
11405 	lpfc_worker_wake_up(phba);
11406 }
11407 
11408 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11409  * @phba: Pointer to HBA context object.
11410  * @iocbq: Pointer to iocb object.
11411  *
11412  * The async_event handler calls this routine when it receives
11413  * an ASYNC_STATUS_CN event from the port.  The port generates
11414  * this event when an Abort Sequence request to an rport fails
11415  * twice in succession.  The abort could be originated by the
11416  * driver or by the port.  The ABTS could have been for an ELS
11417  * or FCP IO.  The port only generates this event when an ABTS
11418  * fails to complete after one retry.
11419  */
11420 static void
lpfc_sli_abts_err_handler(struct lpfc_hba * phba,struct lpfc_iocbq * iocbq)11421 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11422 			  struct lpfc_iocbq *iocbq)
11423 {
11424 	struct lpfc_nodelist *ndlp = NULL;
11425 	uint16_t rpi = 0, vpi = 0;
11426 	struct lpfc_vport *vport = NULL;
11427 
11428 	/* The rpi in the ulpContext is vport-sensitive. */
11429 	vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11430 	rpi = iocbq->iocb.ulpContext;
11431 
11432 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11433 			"3092 Port generated ABTS async event "
11434 			"on vpi %d rpi %d status 0x%x\n",
11435 			vpi, rpi, iocbq->iocb.ulpStatus);
11436 
11437 	vport = lpfc_find_vport_by_vpid(phba, vpi);
11438 	if (!vport)
11439 		goto err_exit;
11440 	ndlp = lpfc_findnode_rpi(vport, rpi);
11441 	if (!ndlp)
11442 		goto err_exit;
11443 
11444 	if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11445 		lpfc_sli_abts_recover_port(vport, ndlp);
11446 	return;
11447 
11448  err_exit:
11449 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11450 			"3095 Event Context not found, no "
11451 			"action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11452 			vpi, rpi, iocbq->iocb.ulpStatus,
11453 			iocbq->iocb.ulpContext);
11454 }
11455 
11456 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11457  * @phba: pointer to HBA context object.
11458  * @ndlp: nodelist pointer for the impacted rport.
11459  * @axri: pointer to the wcqe containing the failed exchange.
11460  *
11461  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11462  * port.  The port generates this event when an abort exchange request to an
11463  * rport fails twice in succession with no reply.  The abort could be originated
11464  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
11465  */
11466 void
lpfc_sli4_abts_err_handler(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct sli4_wcqe_xri_aborted * axri)11467 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11468 			   struct lpfc_nodelist *ndlp,
11469 			   struct sli4_wcqe_xri_aborted *axri)
11470 {
11471 	uint32_t ext_status = 0;
11472 
11473 	if (!ndlp) {
11474 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11475 				"3115 Node Context not found, driver "
11476 				"ignoring abts err event\n");
11477 		return;
11478 	}
11479 
11480 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11481 			"3116 Port generated FCP XRI ABORT event on "
11482 			"vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
11483 			ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
11484 			bf_get(lpfc_wcqe_xa_xri, axri),
11485 			bf_get(lpfc_wcqe_xa_status, axri),
11486 			axri->parameter);
11487 
11488 	/*
11489 	 * Catch the ABTS protocol failure case.  Older OCe FW releases returned
11490 	 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11491 	 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11492 	 */
11493 	ext_status = axri->parameter & IOERR_PARAM_MASK;
11494 	if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11495 	    ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
11496 		lpfc_sli_post_recovery_event(phba, ndlp);
11497 }
11498 
11499 /**
11500  * lpfc_sli_async_event_handler - ASYNC iocb handler function
11501  * @phba: Pointer to HBA context object.
11502  * @pring: Pointer to driver SLI ring object.
11503  * @iocbq: Pointer to iocb object.
11504  *
11505  * This function is called by the slow ring event handler
11506  * function when there is an ASYNC event iocb in the ring.
11507  * This function is called with no lock held.
11508  * Currently this function handles only temperature related
11509  * ASYNC events. The function decodes the temperature sensor
11510  * event message and posts events for the management applications.
11511  **/
11512 static void
lpfc_sli_async_event_handler(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * iocbq)11513 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11514 	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11515 {
11516 	IOCB_t *icmd;
11517 	uint16_t evt_code;
11518 	struct temp_event temp_event_data;
11519 	struct Scsi_Host *shost;
11520 	uint32_t *iocb_w;
11521 
11522 	icmd = &iocbq->iocb;
11523 	evt_code = icmd->un.asyncstat.evt_code;
11524 
11525 	switch (evt_code) {
11526 	case ASYNC_TEMP_WARN:
11527 	case ASYNC_TEMP_SAFE:
11528 		temp_event_data.data = (uint32_t) icmd->ulpContext;
11529 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11530 		if (evt_code == ASYNC_TEMP_WARN) {
11531 			temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
11532 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11533 				"0347 Adapter is very hot, please take "
11534 				"corrective action. temperature : %d Celsius\n",
11535 				(uint32_t) icmd->ulpContext);
11536 		} else {
11537 			temp_event_data.event_code = LPFC_NORMAL_TEMP;
11538 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11539 				"0340 Adapter temperature is OK now. "
11540 				"temperature : %d Celsius\n",
11541 				(uint32_t) icmd->ulpContext);
11542 		}
11543 
11544 		/* Send temperature change event to applications */
11545 		shost = lpfc_shost_from_vport(phba->pport);
11546 		fc_host_post_vendor_event(shost, fc_get_event_number(),
11547 			sizeof(temp_event_data), (char *) &temp_event_data,
11548 			LPFC_NL_VENDOR_ID);
11549 		break;
11550 	case ASYNC_STATUS_CN:
11551 		lpfc_sli_abts_err_handler(phba, iocbq);
11552 		break;
11553 	default:
11554 		iocb_w = (uint32_t *) icmd;
11555 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11556 			"0346 Ring %d handler: unexpected ASYNC_STATUS"
11557 			" evt_code 0x%x\n"
11558 			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
11559 			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
11560 			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
11561 			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
11562 			pring->ringno, icmd->un.asyncstat.evt_code,
11563 			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11564 			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11565 			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11566 			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11567 
11568 		break;
11569 	}
11570 }
11571 
11572 
11573 /**
11574  * lpfc_sli4_setup - SLI ring setup function
11575  * @phba: Pointer to HBA context object.
11576  *
11577  * lpfc_sli_setup sets up rings of the SLI interface with
11578  * number of iocbs per ring and iotags. This function is
11579  * called while driver attach to the HBA and before the
11580  * interrupts are enabled. So there is no need for locking.
11581  *
11582  * This function always returns 0.
11583  **/
11584 int
lpfc_sli4_setup(struct lpfc_hba * phba)11585 lpfc_sli4_setup(struct lpfc_hba *phba)
11586 {
11587 	struct lpfc_sli_ring *pring;
11588 
11589 	pring = phba->sli4_hba.els_wq->pring;
11590 	pring->num_mask = LPFC_MAX_RING_MASK;
11591 	pring->prt[0].profile = 0;	/* Mask 0 */
11592 	pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11593 	pring->prt[0].type = FC_TYPE_ELS;
11594 	pring->prt[0].lpfc_sli_rcv_unsol_event =
11595 	    lpfc_els_unsol_event;
11596 	pring->prt[1].profile = 0;	/* Mask 1 */
11597 	pring->prt[1].rctl = FC_RCTL_ELS_REP;
11598 	pring->prt[1].type = FC_TYPE_ELS;
11599 	pring->prt[1].lpfc_sli_rcv_unsol_event =
11600 	    lpfc_els_unsol_event;
11601 	pring->prt[2].profile = 0;	/* Mask 2 */
11602 	/* NameServer Inquiry */
11603 	pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11604 	/* NameServer */
11605 	pring->prt[2].type = FC_TYPE_CT;
11606 	pring->prt[2].lpfc_sli_rcv_unsol_event =
11607 	    lpfc_ct_unsol_event;
11608 	pring->prt[3].profile = 0;	/* Mask 3 */
11609 	/* NameServer response */
11610 	pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11611 	/* NameServer */
11612 	pring->prt[3].type = FC_TYPE_CT;
11613 	pring->prt[3].lpfc_sli_rcv_unsol_event =
11614 	    lpfc_ct_unsol_event;
11615 	return 0;
11616 }
11617 
11618 /**
11619  * lpfc_sli_setup - SLI ring setup function
11620  * @phba: Pointer to HBA context object.
11621  *
11622  * lpfc_sli_setup sets up rings of the SLI interface with
11623  * number of iocbs per ring and iotags. This function is
11624  * called while driver attach to the HBA and before the
11625  * interrupts are enabled. So there is no need for locking.
11626  *
11627  * This function always returns 0. SLI3 only.
11628  **/
11629 int
lpfc_sli_setup(struct lpfc_hba * phba)11630 lpfc_sli_setup(struct lpfc_hba *phba)
11631 {
11632 	int i, totiocbsize = 0;
11633 	struct lpfc_sli *psli = &phba->sli;
11634 	struct lpfc_sli_ring *pring;
11635 
11636 	psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
11637 	psli->sli_flag = 0;
11638 
11639 	psli->iocbq_lookup = NULL;
11640 	psli->iocbq_lookup_len = 0;
11641 	psli->last_iotag = 0;
11642 
11643 	for (i = 0; i < psli->num_rings; i++) {
11644 		pring = &psli->sli3_ring[i];
11645 		switch (i) {
11646 		case LPFC_FCP_RING:	/* ring 0 - FCP */
11647 			/* numCiocb and numRiocb are used in config_port */
11648 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11649 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11650 			pring->sli.sli3.numCiocb +=
11651 				SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11652 			pring->sli.sli3.numRiocb +=
11653 				SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11654 			pring->sli.sli3.numCiocb +=
11655 				SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11656 			pring->sli.sli3.numRiocb +=
11657 				SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11658 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11659 							SLI3_IOCB_CMD_SIZE :
11660 							SLI2_IOCB_CMD_SIZE;
11661 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11662 							SLI3_IOCB_RSP_SIZE :
11663 							SLI2_IOCB_RSP_SIZE;
11664 			pring->iotag_ctr = 0;
11665 			pring->iotag_max =
11666 			    (phba->cfg_hba_queue_depth * 2);
11667 			pring->fast_iotag = pring->iotag_max;
11668 			pring->num_mask = 0;
11669 			break;
11670 		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
11671 			/* numCiocb and numRiocb are used in config_port */
11672 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11673 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11674 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11675 							SLI3_IOCB_CMD_SIZE :
11676 							SLI2_IOCB_CMD_SIZE;
11677 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11678 							SLI3_IOCB_RSP_SIZE :
11679 							SLI2_IOCB_RSP_SIZE;
11680 			pring->iotag_max = phba->cfg_hba_queue_depth;
11681 			pring->num_mask = 0;
11682 			break;
11683 		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
11684 			/* numCiocb and numRiocb are used in config_port */
11685 			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11686 			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11687 			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11688 							SLI3_IOCB_CMD_SIZE :
11689 							SLI2_IOCB_CMD_SIZE;
11690 			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11691 							SLI3_IOCB_RSP_SIZE :
11692 							SLI2_IOCB_RSP_SIZE;
11693 			pring->fast_iotag = 0;
11694 			pring->iotag_ctr = 0;
11695 			pring->iotag_max = 4096;
11696 			pring->lpfc_sli_rcv_async_status =
11697 				lpfc_sli_async_event_handler;
11698 			pring->num_mask = LPFC_MAX_RING_MASK;
11699 			pring->prt[0].profile = 0;	/* Mask 0 */
11700 			pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11701 			pring->prt[0].type = FC_TYPE_ELS;
11702 			pring->prt[0].lpfc_sli_rcv_unsol_event =
11703 			    lpfc_els_unsol_event;
11704 			pring->prt[1].profile = 0;	/* Mask 1 */
11705 			pring->prt[1].rctl = FC_RCTL_ELS_REP;
11706 			pring->prt[1].type = FC_TYPE_ELS;
11707 			pring->prt[1].lpfc_sli_rcv_unsol_event =
11708 			    lpfc_els_unsol_event;
11709 			pring->prt[2].profile = 0;	/* Mask 2 */
11710 			/* NameServer Inquiry */
11711 			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11712 			/* NameServer */
11713 			pring->prt[2].type = FC_TYPE_CT;
11714 			pring->prt[2].lpfc_sli_rcv_unsol_event =
11715 			    lpfc_ct_unsol_event;
11716 			pring->prt[3].profile = 0;	/* Mask 3 */
11717 			/* NameServer response */
11718 			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11719 			/* NameServer */
11720 			pring->prt[3].type = FC_TYPE_CT;
11721 			pring->prt[3].lpfc_sli_rcv_unsol_event =
11722 			    lpfc_ct_unsol_event;
11723 			break;
11724 		}
11725 		totiocbsize += (pring->sli.sli3.numCiocb *
11726 			pring->sli.sli3.sizeCiocb) +
11727 			(pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
11728 	}
11729 	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
11730 		/* Too many cmd / rsp ring entries in SLI2 SLIM */
11731 		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11732 		       "SLI2 SLIM Data: x%x x%lx\n",
11733 		       phba->brd_no, totiocbsize,
11734 		       (unsigned long) MAX_SLIM_IOCB_SIZE);
11735 	}
11736 	if (phba->cfg_multi_ring_support == 2)
11737 		lpfc_extra_ring_setup(phba);
11738 
11739 	return 0;
11740 }
11741 
11742 /**
11743  * lpfc_sli4_queue_init - Queue initialization function
11744  * @phba: Pointer to HBA context object.
11745  *
11746  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
11747  * ring. This function also initializes ring indices of each ring.
11748  * This function is called during the initialization of the SLI
11749  * interface of an HBA.
11750  * This function is called with no lock held and always returns
11751  * 1.
11752  **/
11753 void
lpfc_sli4_queue_init(struct lpfc_hba * phba)11754 lpfc_sli4_queue_init(struct lpfc_hba *phba)
11755 {
11756 	struct lpfc_sli *psli;
11757 	struct lpfc_sli_ring *pring;
11758 	int i;
11759 
11760 	psli = &phba->sli;
11761 	spin_lock_irq(&phba->hbalock);
11762 	INIT_LIST_HEAD(&psli->mboxq);
11763 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11764 	/* Initialize list headers for txq and txcmplq as double linked lists */
11765 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
11766 		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11767 		pring->flag = 0;
11768 		pring->ringno = LPFC_FCP_RING;
11769 		pring->txcmplq_cnt = 0;
11770 		INIT_LIST_HEAD(&pring->txq);
11771 		INIT_LIST_HEAD(&pring->txcmplq);
11772 		INIT_LIST_HEAD(&pring->iocb_continueq);
11773 		spin_lock_init(&pring->ring_lock);
11774 	}
11775 	pring = phba->sli4_hba.els_wq->pring;
11776 	pring->flag = 0;
11777 	pring->ringno = LPFC_ELS_RING;
11778 	pring->txcmplq_cnt = 0;
11779 	INIT_LIST_HEAD(&pring->txq);
11780 	INIT_LIST_HEAD(&pring->txcmplq);
11781 	INIT_LIST_HEAD(&pring->iocb_continueq);
11782 	spin_lock_init(&pring->ring_lock);
11783 
11784 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11785 		pring = phba->sli4_hba.nvmels_wq->pring;
11786 		pring->flag = 0;
11787 		pring->ringno = LPFC_ELS_RING;
11788 		pring->txcmplq_cnt = 0;
11789 		INIT_LIST_HEAD(&pring->txq);
11790 		INIT_LIST_HEAD(&pring->txcmplq);
11791 		INIT_LIST_HEAD(&pring->iocb_continueq);
11792 		spin_lock_init(&pring->ring_lock);
11793 	}
11794 
11795 	spin_unlock_irq(&phba->hbalock);
11796 }
11797 
11798 /**
11799  * lpfc_sli_queue_init - Queue initialization function
11800  * @phba: Pointer to HBA context object.
11801  *
11802  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11803  * ring. This function also initializes ring indices of each ring.
11804  * This function is called during the initialization of the SLI
11805  * interface of an HBA.
11806  * This function is called with no lock held and always returns
11807  * 1.
11808  **/
11809 void
lpfc_sli_queue_init(struct lpfc_hba * phba)11810 lpfc_sli_queue_init(struct lpfc_hba *phba)
11811 {
11812 	struct lpfc_sli *psli;
11813 	struct lpfc_sli_ring *pring;
11814 	int i;
11815 
11816 	psli = &phba->sli;
11817 	spin_lock_irq(&phba->hbalock);
11818 	INIT_LIST_HEAD(&psli->mboxq);
11819 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11820 	/* Initialize list headers for txq and txcmplq as double linked lists */
11821 	for (i = 0; i < psli->num_rings; i++) {
11822 		pring = &psli->sli3_ring[i];
11823 		pring->ringno = i;
11824 		pring->sli.sli3.next_cmdidx  = 0;
11825 		pring->sli.sli3.local_getidx = 0;
11826 		pring->sli.sli3.cmdidx = 0;
11827 		INIT_LIST_HEAD(&pring->iocb_continueq);
11828 		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11829 		INIT_LIST_HEAD(&pring->postbufq);
11830 		pring->flag = 0;
11831 		INIT_LIST_HEAD(&pring->txq);
11832 		INIT_LIST_HEAD(&pring->txcmplq);
11833 		spin_lock_init(&pring->ring_lock);
11834 	}
11835 	spin_unlock_irq(&phba->hbalock);
11836 }
11837 
11838 /**
11839  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11840  * @phba: Pointer to HBA context object.
11841  *
11842  * This routine flushes the mailbox command subsystem. It will unconditionally
11843  * flush all the mailbox commands in the three possible stages in the mailbox
11844  * command sub-system: pending mailbox command queue; the outstanding mailbox
11845  * command; and completed mailbox command queue. It is caller's responsibility
11846  * to make sure that the driver is in the proper state to flush the mailbox
11847  * command sub-system. Namely, the posting of mailbox commands into the
11848  * pending mailbox command queue from the various clients must be stopped;
11849  * either the HBA is in a state that it will never works on the outstanding
11850  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11851  * mailbox command has been completed.
11852  **/
11853 static void
lpfc_sli_mbox_sys_flush(struct lpfc_hba * phba)11854 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11855 {
11856 	LIST_HEAD(completions);
11857 	struct lpfc_sli *psli = &phba->sli;
11858 	LPFC_MBOXQ_t *pmb;
11859 	unsigned long iflag;
11860 
11861 	/* Disable softirqs, including timers from obtaining phba->hbalock */
11862 	local_bh_disable();
11863 
11864 	/* Flush all the mailbox commands in the mbox system */
11865 	spin_lock_irqsave(&phba->hbalock, iflag);
11866 
11867 	/* The pending mailbox command queue */
11868 	list_splice_init(&phba->sli.mboxq, &completions);
11869 	/* The outstanding active mailbox command */
11870 	if (psli->mbox_active) {
11871 		list_add_tail(&psli->mbox_active->list, &completions);
11872 		psli->mbox_active = NULL;
11873 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11874 	}
11875 	/* The completed mailbox command queue */
11876 	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11877 	spin_unlock_irqrestore(&phba->hbalock, iflag);
11878 
11879 	/* Enable softirqs again, done with phba->hbalock */
11880 	local_bh_enable();
11881 
11882 	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11883 	while (!list_empty(&completions)) {
11884 		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11885 		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11886 		if (pmb->mbox_cmpl)
11887 			pmb->mbox_cmpl(phba, pmb);
11888 	}
11889 }
11890 
11891 /**
11892  * lpfc_sli_host_down - Vport cleanup function
11893  * @vport: Pointer to virtual port object.
11894  *
11895  * lpfc_sli_host_down is called to clean up the resources
11896  * associated with a vport before destroying virtual
11897  * port data structures.
11898  * This function does following operations:
11899  * - Free discovery resources associated with this virtual
11900  *   port.
11901  * - Free iocbs associated with this virtual port in
11902  *   the txq.
11903  * - Send abort for all iocb commands associated with this
11904  *   vport in txcmplq.
11905  *
11906  * This function is called with no lock held and always returns 1.
11907  **/
11908 int
lpfc_sli_host_down(struct lpfc_vport * vport)11909 lpfc_sli_host_down(struct lpfc_vport *vport)
11910 {
11911 	LIST_HEAD(completions);
11912 	struct lpfc_hba *phba = vport->phba;
11913 	struct lpfc_sli *psli = &phba->sli;
11914 	struct lpfc_queue *qp = NULL;
11915 	struct lpfc_sli_ring *pring;
11916 	struct lpfc_iocbq *iocb, *next_iocb;
11917 	int i;
11918 	unsigned long flags = 0;
11919 	uint16_t prev_pring_flag;
11920 
11921 	lpfc_cleanup_discovery_resources(vport);
11922 
11923 	spin_lock_irqsave(&phba->hbalock, flags);
11924 
11925 	/*
11926 	 * Error everything on the txq since these iocbs
11927 	 * have not been given to the FW yet.
11928 	 * Also issue ABTS for everything on the txcmplq
11929 	 */
11930 	if (phba->sli_rev != LPFC_SLI_REV4) {
11931 		for (i = 0; i < psli->num_rings; i++) {
11932 			pring = &psli->sli3_ring[i];
11933 			prev_pring_flag = pring->flag;
11934 			/* Only slow rings */
11935 			if (pring->ringno == LPFC_ELS_RING) {
11936 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11937 				/* Set the lpfc data pending flag */
11938 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11939 			}
11940 			list_for_each_entry_safe(iocb, next_iocb,
11941 						 &pring->txq, list) {
11942 				if (iocb->vport != vport)
11943 					continue;
11944 				list_move_tail(&iocb->list, &completions);
11945 			}
11946 			list_for_each_entry_safe(iocb, next_iocb,
11947 						 &pring->txcmplq, list) {
11948 				if (iocb->vport != vport)
11949 					continue;
11950 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11951 							   NULL);
11952 			}
11953 			pring->flag = prev_pring_flag;
11954 		}
11955 	} else {
11956 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11957 			pring = qp->pring;
11958 			if (!pring)
11959 				continue;
11960 			if (pring == phba->sli4_hba.els_wq->pring) {
11961 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
11962 				/* Set the lpfc data pending flag */
11963 				set_bit(LPFC_DATA_READY, &phba->data_flags);
11964 			}
11965 			prev_pring_flag = pring->flag;
11966 			spin_lock(&pring->ring_lock);
11967 			list_for_each_entry_safe(iocb, next_iocb,
11968 						 &pring->txq, list) {
11969 				if (iocb->vport != vport)
11970 					continue;
11971 				list_move_tail(&iocb->list, &completions);
11972 			}
11973 			spin_unlock(&pring->ring_lock);
11974 			list_for_each_entry_safe(iocb, next_iocb,
11975 						 &pring->txcmplq, list) {
11976 				if (iocb->vport != vport)
11977 					continue;
11978 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11979 							   NULL);
11980 			}
11981 			pring->flag = prev_pring_flag;
11982 		}
11983 	}
11984 	spin_unlock_irqrestore(&phba->hbalock, flags);
11985 
11986 	/* Make sure HBA is alive */
11987 	lpfc_issue_hb_tmo(phba);
11988 
11989 	/* Cancel all the IOCBs from the completions list */
11990 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11991 			      IOERR_SLI_DOWN);
11992 	return 1;
11993 }
11994 
11995 /**
11996  * lpfc_sli_hba_down - Resource cleanup function for the HBA
11997  * @phba: Pointer to HBA context object.
11998  *
11999  * This function cleans up all iocb, buffers, mailbox commands
12000  * while shutting down the HBA. This function is called with no
12001  * lock held and always returns 1.
12002  * This function does the following to cleanup driver resources:
12003  * - Free discovery resources for each virtual port
12004  * - Cleanup any pending fabric iocbs
12005  * - Iterate through the iocb txq and free each entry
12006  *   in the list.
12007  * - Free up any buffer posted to the HBA
12008  * - Free mailbox commands in the mailbox queue.
12009  **/
12010 int
lpfc_sli_hba_down(struct lpfc_hba * phba)12011 lpfc_sli_hba_down(struct lpfc_hba *phba)
12012 {
12013 	LIST_HEAD(completions);
12014 	struct lpfc_sli *psli = &phba->sli;
12015 	struct lpfc_queue *qp = NULL;
12016 	struct lpfc_sli_ring *pring;
12017 	struct lpfc_dmabuf *buf_ptr;
12018 	unsigned long flags = 0;
12019 	int i;
12020 
12021 	/* Shutdown the mailbox command sub-system */
12022 	lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
12023 
12024 	lpfc_hba_down_prep(phba);
12025 
12026 	/* Disable softirqs, including timers from obtaining phba->hbalock */
12027 	local_bh_disable();
12028 
12029 	lpfc_fabric_abort_hba(phba);
12030 
12031 	spin_lock_irqsave(&phba->hbalock, flags);
12032 
12033 	/*
12034 	 * Error everything on the txq since these iocbs
12035 	 * have not been given to the FW yet.
12036 	 */
12037 	if (phba->sli_rev != LPFC_SLI_REV4) {
12038 		for (i = 0; i < psli->num_rings; i++) {
12039 			pring = &psli->sli3_ring[i];
12040 			/* Only slow rings */
12041 			if (pring->ringno == LPFC_ELS_RING) {
12042 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12043 				/* Set the lpfc data pending flag */
12044 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12045 			}
12046 			list_splice_init(&pring->txq, &completions);
12047 		}
12048 	} else {
12049 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12050 			pring = qp->pring;
12051 			if (!pring)
12052 				continue;
12053 			spin_lock(&pring->ring_lock);
12054 			list_splice_init(&pring->txq, &completions);
12055 			spin_unlock(&pring->ring_lock);
12056 			if (pring == phba->sli4_hba.els_wq->pring) {
12057 				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12058 				/* Set the lpfc data pending flag */
12059 				set_bit(LPFC_DATA_READY, &phba->data_flags);
12060 			}
12061 		}
12062 	}
12063 	spin_unlock_irqrestore(&phba->hbalock, flags);
12064 
12065 	/* Cancel all the IOCBs from the completions list */
12066 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12067 			      IOERR_SLI_DOWN);
12068 
12069 	spin_lock_irqsave(&phba->hbalock, flags);
12070 	list_splice_init(&phba->elsbuf, &completions);
12071 	phba->elsbuf_cnt = 0;
12072 	phba->elsbuf_prev_cnt = 0;
12073 	spin_unlock_irqrestore(&phba->hbalock, flags);
12074 
12075 	while (!list_empty(&completions)) {
12076 		list_remove_head(&completions, buf_ptr,
12077 			struct lpfc_dmabuf, list);
12078 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12079 		kfree(buf_ptr);
12080 	}
12081 
12082 	/* Enable softirqs again, done with phba->hbalock */
12083 	local_bh_enable();
12084 
12085 	/* Return any active mbox cmds */
12086 	del_timer_sync(&psli->mbox_tmo);
12087 
12088 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
12089 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12090 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
12091 
12092 	return 1;
12093 }
12094 
12095 /**
12096  * lpfc_sli_pcimem_bcopy - SLI memory copy function
12097  * @srcp: Source memory pointer.
12098  * @destp: Destination memory pointer.
12099  * @cnt: Number of words required to be copied.
12100  *
12101  * This function is used for copying data between driver memory
12102  * and the SLI memory. This function also changes the endianness
12103  * of each word if native endianness is different from SLI
12104  * endianness. This function can be called with or without
12105  * lock.
12106  **/
12107 void
lpfc_sli_pcimem_bcopy(void * srcp,void * destp,uint32_t cnt)12108 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12109 {
12110 	uint32_t *src = srcp;
12111 	uint32_t *dest = destp;
12112 	uint32_t ldata;
12113 	int i;
12114 
12115 	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12116 		ldata = *src;
12117 		ldata = le32_to_cpu(ldata);
12118 		*dest = ldata;
12119 		src++;
12120 		dest++;
12121 	}
12122 }
12123 
12124 
12125 /**
12126  * lpfc_sli_bemem_bcopy - SLI memory copy function
12127  * @srcp: Source memory pointer.
12128  * @destp: Destination memory pointer.
12129  * @cnt: Number of words required to be copied.
12130  *
12131  * This function is used for copying data between a data structure
12132  * with big endian representation to local endianness.
12133  * This function can be called with or without lock.
12134  **/
12135 void
lpfc_sli_bemem_bcopy(void * srcp,void * destp,uint32_t cnt)12136 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12137 {
12138 	uint32_t *src = srcp;
12139 	uint32_t *dest = destp;
12140 	uint32_t ldata;
12141 	int i;
12142 
12143 	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12144 		ldata = *src;
12145 		ldata = be32_to_cpu(ldata);
12146 		*dest = ldata;
12147 		src++;
12148 		dest++;
12149 	}
12150 }
12151 
12152 /**
12153  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
12154  * @phba: Pointer to HBA context object.
12155  * @pring: Pointer to driver SLI ring object.
12156  * @mp: Pointer to driver buffer object.
12157  *
12158  * This function is called with no lock held.
12159  * It always return zero after adding the buffer to the postbufq
12160  * buffer list.
12161  **/
12162 int
lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_dmabuf * mp)12163 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12164 			 struct lpfc_dmabuf *mp)
12165 {
12166 	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12167 	   later */
12168 	spin_lock_irq(&phba->hbalock);
12169 	list_add_tail(&mp->list, &pring->postbufq);
12170 	pring->postbufq_cnt++;
12171 	spin_unlock_irq(&phba->hbalock);
12172 	return 0;
12173 }
12174 
12175 /**
12176  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
12177  * @phba: Pointer to HBA context object.
12178  *
12179  * When HBQ is enabled, buffers are searched based on tags. This function
12180  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12181  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12182  * does not conflict with tags of buffer posted for unsolicited events.
12183  * The function returns the allocated tag. The function is called with
12184  * no locks held.
12185  **/
12186 uint32_t
lpfc_sli_get_buffer_tag(struct lpfc_hba * phba)12187 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12188 {
12189 	spin_lock_irq(&phba->hbalock);
12190 	phba->buffer_tag_count++;
12191 	/*
12192 	 * Always set the QUE_BUFTAG_BIT to distiguish between
12193 	 * a tag assigned by HBQ.
12194 	 */
12195 	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12196 	spin_unlock_irq(&phba->hbalock);
12197 	return phba->buffer_tag_count;
12198 }
12199 
12200 /**
12201  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
12202  * @phba: Pointer to HBA context object.
12203  * @pring: Pointer to driver SLI ring object.
12204  * @tag: Buffer tag.
12205  *
12206  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12207  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12208  * iocb is posted to the response ring with the tag of the buffer.
12209  * This function searches the pring->postbufq list using the tag
12210  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12211  * iocb. If the buffer is found then lpfc_dmabuf object of the
12212  * buffer is returned to the caller else NULL is returned.
12213  * This function is called with no lock held.
12214  **/
12215 struct lpfc_dmabuf *
lpfc_sli_ring_taggedbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,uint32_t tag)12216 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12217 			uint32_t tag)
12218 {
12219 	struct lpfc_dmabuf *mp, *next_mp;
12220 	struct list_head *slp = &pring->postbufq;
12221 
12222 	/* Search postbufq, from the beginning, looking for a match on tag */
12223 	spin_lock_irq(&phba->hbalock);
12224 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12225 		if (mp->buffer_tag == tag) {
12226 			list_del_init(&mp->list);
12227 			pring->postbufq_cnt--;
12228 			spin_unlock_irq(&phba->hbalock);
12229 			return mp;
12230 		}
12231 	}
12232 
12233 	spin_unlock_irq(&phba->hbalock);
12234 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12235 			"0402 Cannot find virtual addr for buffer tag on "
12236 			"ring %d Data x%lx x%px x%px x%x\n",
12237 			pring->ringno, (unsigned long) tag,
12238 			slp->next, slp->prev, pring->postbufq_cnt);
12239 
12240 	return NULL;
12241 }
12242 
12243 /**
12244  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
12245  * @phba: Pointer to HBA context object.
12246  * @pring: Pointer to driver SLI ring object.
12247  * @phys: DMA address of the buffer.
12248  *
12249  * This function searches the buffer list using the dma_address
12250  * of unsolicited event to find the driver's lpfc_dmabuf object
12251  * corresponding to the dma_address. The function returns the
12252  * lpfc_dmabuf object if a buffer is found else it returns NULL.
12253  * This function is called by the ct and els unsolicited event
12254  * handlers to get the buffer associated with the unsolicited
12255  * event.
12256  *
12257  * This function is called with no lock held.
12258  **/
12259 struct lpfc_dmabuf *
lpfc_sli_ringpostbuf_get(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,dma_addr_t phys)12260 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12261 			 dma_addr_t phys)
12262 {
12263 	struct lpfc_dmabuf *mp, *next_mp;
12264 	struct list_head *slp = &pring->postbufq;
12265 
12266 	/* Search postbufq, from the beginning, looking for a match on phys */
12267 	spin_lock_irq(&phba->hbalock);
12268 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12269 		if (mp->phys == phys) {
12270 			list_del_init(&mp->list);
12271 			pring->postbufq_cnt--;
12272 			spin_unlock_irq(&phba->hbalock);
12273 			return mp;
12274 		}
12275 	}
12276 
12277 	spin_unlock_irq(&phba->hbalock);
12278 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12279 			"0410 Cannot find virtual addr for mapped buf on "
12280 			"ring %d Data x%llx x%px x%px x%x\n",
12281 			pring->ringno, (unsigned long long)phys,
12282 			slp->next, slp->prev, pring->postbufq_cnt);
12283 	return NULL;
12284 }
12285 
12286 /**
12287  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
12288  * @phba: Pointer to HBA context object.
12289  * @cmdiocb: Pointer to driver command iocb object.
12290  * @rspiocb: Pointer to driver response iocb object.
12291  *
12292  * This function is the completion handler for the abort iocbs for
12293  * ELS commands. This function is called from the ELS ring event
12294  * handler with no lock held. This function frees memory resources
12295  * associated with the abort iocb.
12296  **/
12297 static void
lpfc_sli_abort_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12298 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12299 			struct lpfc_iocbq *rspiocb)
12300 {
12301 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12302 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12303 	u8 cmnd = get_job_cmnd(phba, cmdiocb);
12304 
12305 	if (ulp_status) {
12306 		/*
12307 		 * Assume that the port already completed and returned, or
12308 		 * will return the iocb. Just Log the message.
12309 		 */
12310 		if (phba->sli_rev < LPFC_SLI_REV4) {
12311 			if (cmnd == CMD_ABORT_XRI_CX &&
12312 			    ulp_status == IOSTAT_LOCAL_REJECT &&
12313 			    ulp_word4 == IOERR_ABORT_REQUESTED) {
12314 				goto release_iocb;
12315 			}
12316 		}
12317 	}
12318 
12319 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
12320 			"0327 Abort els iocb complete x%px with io cmd xri %x "
12321 			"abort tag x%x abort status %x abort code %x\n",
12322 			cmdiocb, get_job_abtsiotag(phba, cmdiocb),
12323 			(phba->sli_rev == LPFC_SLI_REV4) ?
12324 			get_wqe_reqtag(cmdiocb) :
12325 			cmdiocb->iocb.ulpIoTag,
12326 			ulp_status, ulp_word4);
12327 release_iocb:
12328 	lpfc_sli_release_iocbq(phba, cmdiocb);
12329 	return;
12330 }
12331 
12332 /**
12333  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
12334  * @phba: Pointer to HBA context object.
12335  * @cmdiocb: Pointer to driver command iocb object.
12336  * @rspiocb: Pointer to driver response iocb object.
12337  *
12338  * The function is called from SLI ring event handler with no
12339  * lock held. This function is the completion handler for ELS commands
12340  * which are aborted. The function frees memory resources used for
12341  * the aborted ELS commands.
12342  **/
12343 void
lpfc_ignore_els_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12344 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12345 		     struct lpfc_iocbq *rspiocb)
12346 {
12347 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12348 	IOCB_t *irsp;
12349 	LPFC_MBOXQ_t *mbox;
12350 	u32 ulp_command, ulp_status, ulp_word4, iotag;
12351 
12352 	ulp_command = get_job_cmnd(phba, cmdiocb);
12353 	ulp_status = get_job_ulpstatus(phba, rspiocb);
12354 	ulp_word4 = get_job_word4(phba, rspiocb);
12355 
12356 	if (phba->sli_rev == LPFC_SLI_REV4) {
12357 		iotag = get_wqe_reqtag(cmdiocb);
12358 	} else {
12359 		irsp = &rspiocb->iocb;
12360 		iotag = irsp->ulpIoTag;
12361 
12362 		/* It is possible a PLOGI_RJT for NPIV ports to get aborted.
12363 		 * The MBX_REG_LOGIN64 mbox command is freed back to the
12364 		 * mbox_mem_pool here.
12365 		 */
12366 		if (cmdiocb->context_un.mbox) {
12367 			mbox = cmdiocb->context_un.mbox;
12368 			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
12369 			cmdiocb->context_un.mbox = NULL;
12370 		}
12371 	}
12372 
12373 	/* ELS cmd tag <ulpIoTag> completes */
12374 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12375 			"0139 Ignoring ELS cmd code x%x ref cnt x%x Data: "
12376 			"x%x x%x x%x x%px\n",
12377 			ulp_command, kref_read(&cmdiocb->ndlp->kref),
12378 			ulp_status, ulp_word4, iotag, cmdiocb->ndlp);
12379 	/*
12380 	 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12381 	 * if exchange is busy.
12382 	 */
12383 	if (ulp_command == CMD_GEN_REQUEST64_CR)
12384 		lpfc_ct_free_iocb(phba, cmdiocb);
12385 	else
12386 		lpfc_els_free_iocb(phba, cmdiocb);
12387 
12388 	lpfc_nlp_put(ndlp);
12389 }
12390 
12391 /**
12392  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
12393  * @phba: Pointer to HBA context object.
12394  * @pring: Pointer to driver SLI ring object.
12395  * @cmdiocb: Pointer to driver command iocb object.
12396  * @cmpl: completion function.
12397  *
12398  * This function issues an abort iocb for the provided command iocb. In case
12399  * of unloading, the abort iocb will not be issued to commands on the ELS
12400  * ring. Instead, the callback function shall be changed to those commands
12401  * so that nothing happens when them finishes. This function is called with
12402  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12403  * when the command iocb is an abort request.
12404  *
12405  **/
12406 int
lpfc_sli_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * cmdiocb,void * cmpl)12407 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12408 			   struct lpfc_iocbq *cmdiocb, void *cmpl)
12409 {
12410 	struct lpfc_vport *vport = cmdiocb->vport;
12411 	struct lpfc_iocbq *abtsiocbp;
12412 	int retval = IOCB_ERROR;
12413 	unsigned long iflags;
12414 	struct lpfc_nodelist *ndlp = NULL;
12415 	u32 ulp_command = get_job_cmnd(phba, cmdiocb);
12416 	u16 ulp_context, iotag;
12417 	bool ia;
12418 
12419 	/*
12420 	 * There are certain command types we don't want to abort.  And we
12421 	 * don't want to abort commands that are already in the process of
12422 	 * being aborted.
12423 	 */
12424 	if (ulp_command == CMD_ABORT_XRI_WQE ||
12425 	    ulp_command == CMD_ABORT_XRI_CN ||
12426 	    ulp_command == CMD_CLOSE_XRI_CN ||
12427 	    cmdiocb->cmd_flag & LPFC_DRIVER_ABORTED)
12428 		return IOCB_ABORTING;
12429 
12430 	if (!pring) {
12431 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12432 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12433 		else
12434 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12435 		return retval;
12436 	}
12437 
12438 	/*
12439 	 * If we're unloading, don't abort iocb on the ELS ring, but change
12440 	 * the callback so that nothing happens when it finishes.
12441 	 */
12442 	if (test_bit(FC_UNLOADING, &vport->load_flag) &&
12443 	    pring->ringno == LPFC_ELS_RING) {
12444 		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12445 			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12446 		else
12447 			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12448 		return retval;
12449 	}
12450 
12451 	/* issue ABTS for this IOCB based on iotag */
12452 	abtsiocbp = __lpfc_sli_get_iocbq(phba);
12453 	if (abtsiocbp == NULL)
12454 		return IOCB_NORESOURCE;
12455 
12456 	/* This signals the response to set the correct status
12457 	 * before calling the completion handler
12458 	 */
12459 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
12460 
12461 	if (phba->sli_rev == LPFC_SLI_REV4) {
12462 		ulp_context = cmdiocb->sli4_xritag;
12463 		iotag = abtsiocbp->iotag;
12464 	} else {
12465 		iotag = cmdiocb->iocb.ulpIoTag;
12466 		if (pring->ringno == LPFC_ELS_RING) {
12467 			ndlp = cmdiocb->ndlp;
12468 			ulp_context = ndlp->nlp_rpi;
12469 		} else {
12470 			ulp_context = cmdiocb->iocb.ulpContext;
12471 		}
12472 	}
12473 
12474 	/* Just close the exchange under certain conditions. */
12475 	if (test_bit(FC_UNLOADING, &vport->load_flag) ||
12476 	    phba->link_state < LPFC_LINK_UP ||
12477 	    (phba->sli_rev == LPFC_SLI_REV4 &&
12478 	     phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN) ||
12479 	    (phba->link_flag & LS_EXTERNAL_LOOPBACK))
12480 		ia = true;
12481 	else
12482 		ia = false;
12483 
12484 	lpfc_sli_prep_abort_xri(phba, abtsiocbp, ulp_context, iotag,
12485 				cmdiocb->iocb.ulpClass,
12486 				LPFC_WQE_CQ_ID_DEFAULT, ia, false);
12487 
12488 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12489 	abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
12490 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
12491 		abtsiocbp->cmd_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);
12492 
12493 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
12494 		abtsiocbp->cmd_flag |= LPFC_IO_FOF;
12495 
12496 	if (cmpl)
12497 		abtsiocbp->cmd_cmpl = cmpl;
12498 	else
12499 		abtsiocbp->cmd_cmpl = lpfc_sli_abort_els_cmpl;
12500 	abtsiocbp->vport = vport;
12501 
12502 	if (phba->sli_rev == LPFC_SLI_REV4) {
12503 		pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12504 		if (unlikely(pring == NULL))
12505 			goto abort_iotag_exit;
12506 		/* Note: both hbalock and ring_lock need to be set here */
12507 		spin_lock_irqsave(&pring->ring_lock, iflags);
12508 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12509 			abtsiocbp, 0);
12510 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
12511 	} else {
12512 		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12513 			abtsiocbp, 0);
12514 	}
12515 
12516 abort_iotag_exit:
12517 
12518 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12519 			 "0339 Abort IO XRI x%x, Original iotag x%x, "
12520 			 "abort tag x%x Cmdjob : x%px Abortjob : x%px "
12521 			 "retval x%x : IA %d cmd_cmpl %ps\n",
12522 			 ulp_context, (phba->sli_rev == LPFC_SLI_REV4) ?
12523 			 cmdiocb->iotag : iotag, iotag, cmdiocb, abtsiocbp,
12524 			 retval, ia, abtsiocbp->cmd_cmpl);
12525 	if (retval) {
12526 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
12527 		__lpfc_sli_release_iocbq(phba, abtsiocbp);
12528 	}
12529 
12530 	/*
12531 	 * Caller to this routine should check for IOCB_ERROR
12532 	 * and handle it properly.  This routine no longer removes
12533 	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
12534 	 */
12535 	return retval;
12536 }
12537 
12538 /**
12539  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12540  * @phba: pointer to lpfc HBA data structure.
12541  *
12542  * This routine will abort all pending and outstanding iocbs to an HBA.
12543  **/
12544 void
lpfc_sli_hba_iocb_abort(struct lpfc_hba * phba)12545 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12546 {
12547 	struct lpfc_sli *psli = &phba->sli;
12548 	struct lpfc_sli_ring *pring;
12549 	struct lpfc_queue *qp = NULL;
12550 	int i;
12551 
12552 	if (phba->sli_rev != LPFC_SLI_REV4) {
12553 		for (i = 0; i < psli->num_rings; i++) {
12554 			pring = &psli->sli3_ring[i];
12555 			lpfc_sli_abort_iocb_ring(phba, pring);
12556 		}
12557 		return;
12558 	}
12559 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12560 		pring = qp->pring;
12561 		if (!pring)
12562 			continue;
12563 		lpfc_sli_abort_iocb_ring(phba, pring);
12564 	}
12565 }
12566 
12567 /**
12568  * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts
12569  * @iocbq: Pointer to iocb object.
12570  * @vport: Pointer to driver virtual port object.
12571  *
12572  * This function acts as an iocb filter for functions which abort FCP iocbs.
12573  *
12574  * Return values
12575  * -ENODEV, if a null iocb or vport ptr is encountered
12576  * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as
12577  *          driver already started the abort process, or is an abort iocb itself
12578  * 0, passes criteria for aborting the FCP I/O iocb
12579  **/
12580 static int
lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq * iocbq,struct lpfc_vport * vport)12581 lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,
12582 				     struct lpfc_vport *vport)
12583 {
12584 	u8 ulp_command;
12585 
12586 	/* No null ptr vports */
12587 	if (!iocbq || iocbq->vport != vport)
12588 		return -ENODEV;
12589 
12590 	/* iocb must be for FCP IO, already exists on the TX cmpl queue,
12591 	 * can't be premarked as driver aborted, nor be an ABORT iocb itself
12592 	 */
12593 	ulp_command = get_job_cmnd(vport->phba, iocbq);
12594 	if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12595 	    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ) ||
12596 	    (iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12597 	    (ulp_command == CMD_ABORT_XRI_CN ||
12598 	     ulp_command == CMD_CLOSE_XRI_CN ||
12599 	     ulp_command == CMD_ABORT_XRI_WQE))
12600 		return -EINVAL;
12601 
12602 	return 0;
12603 }
12604 
12605 /**
12606  * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target
12607  * @iocbq: Pointer to driver iocb object.
12608  * @vport: Pointer to driver virtual port object.
12609  * @tgt_id: SCSI ID of the target.
12610  * @lun_id: LUN ID of the scsi device.
12611  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12612  *
12613  * This function acts as an iocb filter for validating a lun/SCSI target/SCSI
12614  * host.
12615  *
12616  * It will return
12617  * 0 if the filtering criteria is met for the given iocb and will return
12618  * 1 if the filtering criteria is not met.
12619  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12620  * given iocb is for the SCSI device specified by vport, tgt_id and
12621  * lun_id parameter.
12622  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
12623  * given iocb is for the SCSI target specified by vport and tgt_id
12624  * parameters.
12625  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12626  * given iocb is for the SCSI host associated with the given vport.
12627  * This function is called with no locks held.
12628  **/
12629 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)12630 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12631 			   uint16_t tgt_id, uint64_t lun_id,
12632 			   lpfc_ctx_cmd ctx_cmd)
12633 {
12634 	struct lpfc_io_buf *lpfc_cmd;
12635 	int rc = 1;
12636 
12637 	lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12638 
12639 	if (lpfc_cmd->pCmd == NULL)
12640 		return rc;
12641 
12642 	switch (ctx_cmd) {
12643 	case LPFC_CTX_LUN:
12644 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12645 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12646 		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
12647 			rc = 0;
12648 		break;
12649 	case LPFC_CTX_TGT:
12650 		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12651 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
12652 			rc = 0;
12653 		break;
12654 	case LPFC_CTX_HOST:
12655 		rc = 0;
12656 		break;
12657 	default:
12658 		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
12659 			__func__, ctx_cmd);
12660 		break;
12661 	}
12662 
12663 	return rc;
12664 }
12665 
12666 /**
12667  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
12668  * @vport: Pointer to virtual port.
12669  * @tgt_id: SCSI ID of the target.
12670  * @lun_id: LUN ID of the scsi device.
12671  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12672  *
12673  * This function returns number of FCP commands pending for the vport.
12674  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12675  * commands pending on the vport associated with SCSI device specified
12676  * by tgt_id and lun_id parameters.
12677  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12678  * commands pending on the vport associated with SCSI target specified
12679  * by tgt_id parameter.
12680  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12681  * commands pending on the vport.
12682  * This function returns the number of iocbs which satisfy the filter.
12683  * This function is called without any lock held.
12684  **/
12685 int
lpfc_sli_sum_iocb(struct lpfc_vport * vport,uint16_t tgt_id,uint64_t lun_id,lpfc_ctx_cmd ctx_cmd)12686 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12687 		  lpfc_ctx_cmd ctx_cmd)
12688 {
12689 	struct lpfc_hba *phba = vport->phba;
12690 	struct lpfc_iocbq *iocbq;
12691 	int sum, i;
12692 	unsigned long iflags;
12693 	u8 ulp_command;
12694 
12695 	spin_lock_irqsave(&phba->hbalock, iflags);
12696 	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12697 		iocbq = phba->sli.iocbq_lookup[i];
12698 
12699 		if (!iocbq || iocbq->vport != vport)
12700 			continue;
12701 		if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12702 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ))
12703 			continue;
12704 
12705 		/* Include counting outstanding aborts */
12706 		ulp_command = get_job_cmnd(phba, iocbq);
12707 		if (ulp_command == CMD_ABORT_XRI_CN ||
12708 		    ulp_command == CMD_CLOSE_XRI_CN ||
12709 		    ulp_command == CMD_ABORT_XRI_WQE) {
12710 			sum++;
12711 			continue;
12712 		}
12713 
12714 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12715 					       ctx_cmd) == 0)
12716 			sum++;
12717 	}
12718 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12719 
12720 	return sum;
12721 }
12722 
12723 /**
12724  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12725  * @phba: Pointer to HBA context object
12726  * @cmdiocb: Pointer to command iocb object.
12727  * @rspiocb: Pointer to response iocb object.
12728  *
12729  * This function is called when an aborted FCP iocb completes. This
12730  * function is called by the ring event handler with no lock held.
12731  * This function frees the iocb.
12732  **/
12733 void
lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)12734 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12735 			struct lpfc_iocbq *rspiocb)
12736 {
12737 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12738 			"3096 ABORT_XRI_CX completing on rpi x%x "
12739 			"original iotag x%x, abort cmd iotag x%x "
12740 			"status 0x%x, reason 0x%x\n",
12741 			(phba->sli_rev == LPFC_SLI_REV4) ?
12742 			cmdiocb->sli4_xritag :
12743 			cmdiocb->iocb.un.acxri.abortContextTag,
12744 			get_job_abtsiotag(phba, cmdiocb),
12745 			cmdiocb->iotag, get_job_ulpstatus(phba, rspiocb),
12746 			get_job_word4(phba, rspiocb));
12747 	lpfc_sli_release_iocbq(phba, cmdiocb);
12748 	return;
12749 }
12750 
12751 /**
12752  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
12753  * @vport: Pointer to virtual port.
12754  * @tgt_id: SCSI ID of the target.
12755  * @lun_id: LUN ID of the scsi device.
12756  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12757  *
12758  * This function sends an abort command for every SCSI command
12759  * associated with the given virtual port pending on the ring
12760  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12761  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12762  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12763  * followed by lpfc_sli_validate_fcp_iocb.
12764  *
12765  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12766  * FCP iocbs associated with lun specified by tgt_id and lun_id
12767  * parameters
12768  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12769  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12770  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12771  * FCP iocbs associated with virtual port.
12772  * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12773  * lpfc_sli4_calc_ring is used.
12774  * This function returns number of iocbs it failed to abort.
12775  * This function is called with no locks held.
12776  **/
12777 int
lpfc_sli_abort_iocb(struct lpfc_vport * vport,u16 tgt_id,u64 lun_id,lpfc_ctx_cmd abort_cmd)12778 lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12779 		    lpfc_ctx_cmd abort_cmd)
12780 {
12781 	struct lpfc_hba *phba = vport->phba;
12782 	struct lpfc_sli_ring *pring = NULL;
12783 	struct lpfc_iocbq *iocbq;
12784 	int errcnt = 0, ret_val = 0;
12785 	unsigned long iflags;
12786 	int i;
12787 
12788 	/* all I/Os are in process of being flushed */
12789 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12790 		return errcnt;
12791 
12792 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12793 		iocbq = phba->sli.iocbq_lookup[i];
12794 
12795 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12796 			continue;
12797 
12798 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12799 					       abort_cmd) != 0)
12800 			continue;
12801 
12802 		spin_lock_irqsave(&phba->hbalock, iflags);
12803 		if (phba->sli_rev == LPFC_SLI_REV3) {
12804 			pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12805 		} else if (phba->sli_rev == LPFC_SLI_REV4) {
12806 			pring = lpfc_sli4_calc_ring(phba, iocbq);
12807 		}
12808 		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
12809 						     lpfc_sli_abort_fcp_cmpl);
12810 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12811 		if (ret_val != IOCB_SUCCESS)
12812 			errcnt++;
12813 	}
12814 
12815 	return errcnt;
12816 }
12817 
12818 /**
12819  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12820  * @vport: Pointer to virtual port.
12821  * @pring: Pointer to driver SLI ring object.
12822  * @tgt_id: SCSI ID of the target.
12823  * @lun_id: LUN ID of the scsi device.
12824  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12825  *
12826  * This function sends an abort command for every SCSI command
12827  * associated with the given virtual port pending on the ring
12828  * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12829  * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12830  * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12831  * followed by lpfc_sli_validate_fcp_iocb.
12832  *
12833  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12834  * FCP iocbs associated with lun specified by tgt_id and lun_id
12835  * parameters
12836  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12837  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12838  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12839  * FCP iocbs associated with virtual port.
12840  * This function returns number of iocbs it aborted .
12841  * This function is called with no locks held right after a taskmgmt
12842  * command is sent.
12843  **/
12844 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)12845 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12846 			uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12847 {
12848 	struct lpfc_hba *phba = vport->phba;
12849 	struct lpfc_io_buf *lpfc_cmd;
12850 	struct lpfc_iocbq *abtsiocbq;
12851 	struct lpfc_nodelist *ndlp = NULL;
12852 	struct lpfc_iocbq *iocbq;
12853 	int sum, i, ret_val;
12854 	unsigned long iflags;
12855 	struct lpfc_sli_ring *pring_s4 = NULL;
12856 	u16 ulp_context, iotag, cqid = LPFC_WQE_CQ_ID_DEFAULT;
12857 	bool ia;
12858 
12859 	/* all I/Os are in process of being flushed */
12860 	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12861 		return 0;
12862 
12863 	sum = 0;
12864 
12865 	spin_lock_irqsave(&phba->hbalock, iflags);
12866 	for (i = 1; i <= phba->sli.last_iotag; i++) {
12867 		iocbq = phba->sli.iocbq_lookup[i];
12868 
12869 		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12870 			continue;
12871 
12872 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12873 					       cmd) != 0)
12874 			continue;
12875 
12876 		/* Guard against IO completion being called at same time */
12877 		lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12878 		spin_lock(&lpfc_cmd->buf_lock);
12879 
12880 		if (!lpfc_cmd->pCmd) {
12881 			spin_unlock(&lpfc_cmd->buf_lock);
12882 			continue;
12883 		}
12884 
12885 		if (phba->sli_rev == LPFC_SLI_REV4) {
12886 			pring_s4 =
12887 			    phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12888 			if (!pring_s4) {
12889 				spin_unlock(&lpfc_cmd->buf_lock);
12890 				continue;
12891 			}
12892 			/* Note: both hbalock and ring_lock must be set here */
12893 			spin_lock(&pring_s4->ring_lock);
12894 		}
12895 
12896 		/*
12897 		 * If the iocbq is already being aborted, don't take a second
12898 		 * action, but do count it.
12899 		 */
12900 		if ((iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12901 		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
12902 			if (phba->sli_rev == LPFC_SLI_REV4)
12903 				spin_unlock(&pring_s4->ring_lock);
12904 			spin_unlock(&lpfc_cmd->buf_lock);
12905 			continue;
12906 		}
12907 
12908 		/* issue ABTS for this IOCB based on iotag */
12909 		abtsiocbq = __lpfc_sli_get_iocbq(phba);
12910 		if (!abtsiocbq) {
12911 			if (phba->sli_rev == LPFC_SLI_REV4)
12912 				spin_unlock(&pring_s4->ring_lock);
12913 			spin_unlock(&lpfc_cmd->buf_lock);
12914 			continue;
12915 		}
12916 
12917 		if (phba->sli_rev == LPFC_SLI_REV4) {
12918 			iotag = abtsiocbq->iotag;
12919 			ulp_context = iocbq->sli4_xritag;
12920 			cqid = lpfc_cmd->hdwq->io_cq_map;
12921 		} else {
12922 			iotag = iocbq->iocb.ulpIoTag;
12923 			if (pring->ringno == LPFC_ELS_RING) {
12924 				ndlp = iocbq->ndlp;
12925 				ulp_context = ndlp->nlp_rpi;
12926 			} else {
12927 				ulp_context = iocbq->iocb.ulpContext;
12928 			}
12929 		}
12930 
12931 		ndlp = lpfc_cmd->rdata->pnode;
12932 
12933 		if (lpfc_is_link_up(phba) &&
12934 		    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE) &&
12935 		    !(phba->link_flag & LS_EXTERNAL_LOOPBACK))
12936 			ia = false;
12937 		else
12938 			ia = true;
12939 
12940 		lpfc_sli_prep_abort_xri(phba, abtsiocbq, ulp_context, iotag,
12941 					iocbq->iocb.ulpClass, cqid,
12942 					ia, false);
12943 
12944 		abtsiocbq->vport = vport;
12945 
12946 		/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12947 		abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12948 		if (iocbq->cmd_flag & LPFC_IO_FCP)
12949 			abtsiocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
12950 		if (iocbq->cmd_flag & LPFC_IO_FOF)
12951 			abtsiocbq->cmd_flag |= LPFC_IO_FOF;
12952 
12953 		/* Setup callback routine and issue the command. */
12954 		abtsiocbq->cmd_cmpl = lpfc_sli_abort_fcp_cmpl;
12955 
12956 		/*
12957 		 * Indicate the IO is being aborted by the driver and set
12958 		 * the caller's flag into the aborted IO.
12959 		 */
12960 		iocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
12961 
12962 		if (phba->sli_rev == LPFC_SLI_REV4) {
12963 			ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12964 							abtsiocbq, 0);
12965 			spin_unlock(&pring_s4->ring_lock);
12966 		} else {
12967 			ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12968 							abtsiocbq, 0);
12969 		}
12970 
12971 		spin_unlock(&lpfc_cmd->buf_lock);
12972 
12973 		if (ret_val == IOCB_ERROR)
12974 			__lpfc_sli_release_iocbq(phba, abtsiocbq);
12975 		else
12976 			sum++;
12977 	}
12978 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12979 	return sum;
12980 }
12981 
12982 /**
12983  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
12984  * @phba: Pointer to HBA context object.
12985  * @cmdiocbq: Pointer to command iocb.
12986  * @rspiocbq: Pointer to response iocb.
12987  *
12988  * This function is the completion handler for iocbs issued using
12989  * lpfc_sli_issue_iocb_wait function. This function is called by the
12990  * ring event handler function without any lock held. This function
12991  * can be called from both worker thread context and interrupt
12992  * context. This function also can be called from other thread which
12993  * cleans up the SLI layer objects.
12994  * This function copy the contents of the response iocb to the
12995  * response iocb memory object provided by the caller of
12996  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12997  * sleeps for the iocb completion.
12998  **/
12999 static void
lpfc_sli_wake_iocb_wait(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbq,struct lpfc_iocbq * rspiocbq)13000 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
13001 			struct lpfc_iocbq *cmdiocbq,
13002 			struct lpfc_iocbq *rspiocbq)
13003 {
13004 	wait_queue_head_t *pdone_q;
13005 	unsigned long iflags;
13006 	struct lpfc_io_buf *lpfc_cmd;
13007 	size_t offset = offsetof(struct lpfc_iocbq, wqe);
13008 
13009 	spin_lock_irqsave(&phba->hbalock, iflags);
13010 	if (cmdiocbq->cmd_flag & LPFC_IO_WAKE_TMO) {
13011 
13012 		/*
13013 		 * A time out has occurred for the iocb.  If a time out
13014 		 * completion handler has been supplied, call it.  Otherwise,
13015 		 * just free the iocbq.
13016 		 */
13017 
13018 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13019 		cmdiocbq->cmd_cmpl = cmdiocbq->wait_cmd_cmpl;
13020 		cmdiocbq->wait_cmd_cmpl = NULL;
13021 		if (cmdiocbq->cmd_cmpl)
13022 			cmdiocbq->cmd_cmpl(phba, cmdiocbq, NULL);
13023 		else
13024 			lpfc_sli_release_iocbq(phba, cmdiocbq);
13025 		return;
13026 	}
13027 
13028 	/* Copy the contents of the local rspiocb into the caller's buffer. */
13029 	cmdiocbq->cmd_flag |= LPFC_IO_WAKE;
13030 	if (cmdiocbq->rsp_iocb && rspiocbq)
13031 		memcpy((char *)cmdiocbq->rsp_iocb + offset,
13032 		       (char *)rspiocbq + offset, sizeof(*rspiocbq) - offset);
13033 
13034 	/* Set the exchange busy flag for task management commands */
13035 	if ((cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
13036 	    !(cmdiocbq->cmd_flag & LPFC_IO_LIBDFC)) {
13037 		lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
13038 					cur_iocbq);
13039 		if (rspiocbq && (rspiocbq->cmd_flag & LPFC_EXCHANGE_BUSY))
13040 			lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
13041 		else
13042 			lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
13043 	}
13044 
13045 	pdone_q = cmdiocbq->context_un.wait_queue;
13046 	if (pdone_q)
13047 		wake_up(pdone_q);
13048 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13049 	return;
13050 }
13051 
13052 /**
13053  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
13054  * @phba: Pointer to HBA context object..
13055  * @piocbq: Pointer to command iocb.
13056  * @flag: Flag to test.
13057  *
13058  * This routine grabs the hbalock and then test the cmd_flag to
13059  * see if the passed in flag is set.
13060  * Returns:
13061  * 1 if flag is set.
13062  * 0 if flag is not set.
13063  **/
13064 static int
lpfc_chk_iocb_flg(struct lpfc_hba * phba,struct lpfc_iocbq * piocbq,uint32_t flag)13065 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
13066 		 struct lpfc_iocbq *piocbq, uint32_t flag)
13067 {
13068 	unsigned long iflags;
13069 	int ret;
13070 
13071 	spin_lock_irqsave(&phba->hbalock, iflags);
13072 	ret = piocbq->cmd_flag & flag;
13073 	spin_unlock_irqrestore(&phba->hbalock, iflags);
13074 	return ret;
13075 
13076 }
13077 
13078 /**
13079  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
13080  * @phba: Pointer to HBA context object..
13081  * @ring_number: Ring number
13082  * @piocb: Pointer to command iocb.
13083  * @prspiocbq: Pointer to response iocb.
13084  * @timeout: Timeout in number of seconds.
13085  *
13086  * This function issues the iocb to firmware and waits for the
13087  * iocb to complete. The cmd_cmpl field of the shall be used
13088  * to handle iocbs which time out. If the field is NULL, the
13089  * function shall free the iocbq structure.  If more clean up is
13090  * needed, the caller is expected to provide a completion function
13091  * that will provide the needed clean up.  If the iocb command is
13092  * not completed within timeout seconds, the function will either
13093  * free the iocbq structure (if cmd_cmpl == NULL) or execute the
13094  * completion function set in the cmd_cmpl field and then return
13095  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
13096  * resources if this function returns IOCB_TIMEDOUT.
13097  * The function waits for the iocb completion using an
13098  * non-interruptible wait.
13099  * This function will sleep while waiting for iocb completion.
13100  * So, this function should not be called from any context which
13101  * does not allow sleeping. Due to the same reason, this function
13102  * cannot be called with interrupt disabled.
13103  * This function assumes that the iocb completions occur while
13104  * this function sleep. So, this function cannot be called from
13105  * the thread which process iocb completion for this ring.
13106  * This function clears the cmd_flag of the iocb object before
13107  * issuing the iocb and the iocb completion handler sets this
13108  * flag and wakes this thread when the iocb completes.
13109  * The contents of the response iocb will be copied to prspiocbq
13110  * by the completion handler when the command completes.
13111  * This function returns IOCB_SUCCESS when success.
13112  * This function is called with no lock held.
13113  **/
13114 int
lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,uint32_t ring_number,struct lpfc_iocbq * piocb,struct lpfc_iocbq * prspiocbq,uint32_t timeout)13115 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
13116 			 uint32_t ring_number,
13117 			 struct lpfc_iocbq *piocb,
13118 			 struct lpfc_iocbq *prspiocbq,
13119 			 uint32_t timeout)
13120 {
13121 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
13122 	long timeleft, timeout_req = 0;
13123 	int retval = IOCB_SUCCESS;
13124 	uint32_t creg_val;
13125 	struct lpfc_iocbq *iocb;
13126 	int txq_cnt = 0;
13127 	int txcmplq_cnt = 0;
13128 	struct lpfc_sli_ring *pring;
13129 	unsigned long iflags;
13130 	bool iocb_completed = true;
13131 
13132 	if (phba->sli_rev >= LPFC_SLI_REV4) {
13133 		lpfc_sli_prep_wqe(phba, piocb);
13134 
13135 		pring = lpfc_sli4_calc_ring(phba, piocb);
13136 	} else
13137 		pring = &phba->sli.sli3_ring[ring_number];
13138 	/*
13139 	 * If the caller has provided a response iocbq buffer, then rsp_iocb
13140 	 * is NULL or its an error.
13141 	 */
13142 	if (prspiocbq) {
13143 		if (piocb->rsp_iocb)
13144 			return IOCB_ERROR;
13145 		piocb->rsp_iocb = prspiocbq;
13146 	}
13147 
13148 	piocb->wait_cmd_cmpl = piocb->cmd_cmpl;
13149 	piocb->cmd_cmpl = lpfc_sli_wake_iocb_wait;
13150 	piocb->context_un.wait_queue = &done_q;
13151 	piocb->cmd_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
13152 
13153 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13154 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13155 			return IOCB_ERROR;
13156 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13157 		writel(creg_val, phba->HCregaddr);
13158 		readl(phba->HCregaddr); /* flush */
13159 	}
13160 
13161 	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13162 				     SLI_IOCB_RET_IOCB);
13163 	if (retval == IOCB_SUCCESS) {
13164 		timeout_req = msecs_to_jiffies(timeout * 1000);
13165 		timeleft = wait_event_timeout(done_q,
13166 				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
13167 				timeout_req);
13168 		spin_lock_irqsave(&phba->hbalock, iflags);
13169 		if (!(piocb->cmd_flag & LPFC_IO_WAKE)) {
13170 
13171 			/*
13172 			 * IOCB timed out.  Inform the wake iocb wait
13173 			 * completion function and set local status
13174 			 */
13175 
13176 			iocb_completed = false;
13177 			piocb->cmd_flag |= LPFC_IO_WAKE_TMO;
13178 		}
13179 		spin_unlock_irqrestore(&phba->hbalock, iflags);
13180 		if (iocb_completed) {
13181 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13182 					"0331 IOCB wake signaled\n");
13183 			/* Note: we are not indicating if the IOCB has a success
13184 			 * status or not - that's for the caller to check.
13185 			 * IOCB_SUCCESS means just that the command was sent and
13186 			 * completed. Not that it completed successfully.
13187 			 * */
13188 		} else if (timeleft == 0) {
13189 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13190 					"0338 IOCB wait timeout error - no "
13191 					"wake response Data x%x\n", timeout);
13192 			retval = IOCB_TIMEDOUT;
13193 		} else {
13194 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13195 					"0330 IOCB wake NOT set, "
13196 					"Data x%x x%lx\n",
13197 					timeout, (timeleft / jiffies));
13198 			retval = IOCB_TIMEDOUT;
13199 		}
13200 	} else if (retval == IOCB_BUSY) {
13201 		if (phba->cfg_log_verbose & LOG_SLI) {
13202 			list_for_each_entry(iocb, &pring->txq, list) {
13203 				txq_cnt++;
13204 			}
13205 			list_for_each_entry(iocb, &pring->txcmplq, list) {
13206 				txcmplq_cnt++;
13207 			}
13208 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13209 				"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13210 				phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13211 		}
13212 		return retval;
13213 	} else {
13214 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13215 				"0332 IOCB wait issue failed, Data x%x\n",
13216 				retval);
13217 		retval = IOCB_ERROR;
13218 	}
13219 
13220 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13221 		if (lpfc_readl(phba->HCregaddr, &creg_val))
13222 			return IOCB_ERROR;
13223 		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13224 		writel(creg_val, phba->HCregaddr);
13225 		readl(phba->HCregaddr); /* flush */
13226 	}
13227 
13228 	if (prspiocbq)
13229 		piocb->rsp_iocb = NULL;
13230 
13231 	piocb->context_un.wait_queue = NULL;
13232 	piocb->cmd_cmpl = NULL;
13233 	return retval;
13234 }
13235 
13236 /**
13237  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
13238  * @phba: Pointer to HBA context object.
13239  * @pmboxq: Pointer to driver mailbox object.
13240  * @timeout: Timeout in number of seconds.
13241  *
13242  * This function issues the mailbox to firmware and waits for the
13243  * mailbox command to complete. If the mailbox command is not
13244  * completed within timeout seconds, it returns MBX_TIMEOUT.
13245  * The function waits for the mailbox completion using an
13246  * interruptible wait. If the thread is woken up due to a
13247  * signal, MBX_TIMEOUT error is returned to the caller. Caller
13248  * should not free the mailbox resources, if this function returns
13249  * MBX_TIMEOUT.
13250  * This function will sleep while waiting for mailbox completion.
13251  * So, this function should not be called from any context which
13252  * does not allow sleeping. Due to the same reason, this function
13253  * cannot be called with interrupt disabled.
13254  * This function assumes that the mailbox completion occurs while
13255  * this function sleep. So, this function cannot be called from
13256  * the worker thread which processes mailbox completion.
13257  * This function is called in the context of HBA management
13258  * applications.
13259  * This function returns MBX_SUCCESS when successful.
13260  * This function is called with no lock held.
13261  **/
13262 int
lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmboxq,uint32_t timeout)13263 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
13264 			 uint32_t timeout)
13265 {
13266 	struct completion mbox_done;
13267 	int retval;
13268 	unsigned long flag;
13269 
13270 	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
13271 	/* setup wake call as IOCB callback */
13272 	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
13273 
13274 	/* setup ctx_u field to pass wait_queue pointer to wake function  */
13275 	init_completion(&mbox_done);
13276 	pmboxq->ctx_u.mbox_wait = &mbox_done;
13277 	/* now issue the command */
13278 	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
13279 	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
13280 		wait_for_completion_timeout(&mbox_done,
13281 					    msecs_to_jiffies(timeout * 1000));
13282 
13283 		spin_lock_irqsave(&phba->hbalock, flag);
13284 		pmboxq->ctx_u.mbox_wait = NULL;
13285 		/*
13286 		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13287 		 * else do not free the resources.
13288 		 */
13289 		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
13290 			retval = MBX_SUCCESS;
13291 		} else {
13292 			retval = MBX_TIMEOUT;
13293 			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13294 		}
13295 		spin_unlock_irqrestore(&phba->hbalock, flag);
13296 	}
13297 	return retval;
13298 }
13299 
13300 /**
13301  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
13302  * @phba: Pointer to HBA context.
13303  * @mbx_action: Mailbox shutdown options.
13304  *
13305  * This function is called to shutdown the driver's mailbox sub-system.
13306  * It first marks the mailbox sub-system is in a block state to prevent
13307  * the asynchronous mailbox command from issued off the pending mailbox
13308  * command queue. If the mailbox command sub-system shutdown is due to
13309  * HBA error conditions such as EEH or ERATT, this routine shall invoke
13310  * the mailbox sub-system flush routine to forcefully bring down the
13311  * mailbox sub-system. Otherwise, if it is due to normal condition (such
13312  * as with offline or HBA function reset), this routine will wait for the
13313  * outstanding mailbox command to complete before invoking the mailbox
13314  * sub-system flush routine to gracefully bring down mailbox sub-system.
13315  **/
13316 void
lpfc_sli_mbox_sys_shutdown(struct lpfc_hba * phba,int mbx_action)13317 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
13318 {
13319 	struct lpfc_sli *psli = &phba->sli;
13320 	unsigned long timeout;
13321 
13322 	if (mbx_action == LPFC_MBX_NO_WAIT) {
13323 		/* delay 100ms for port state */
13324 		msleep(100);
13325 		lpfc_sli_mbox_sys_flush(phba);
13326 		return;
13327 	}
13328 	timeout = secs_to_jiffies(LPFC_MBOX_TMO) + jiffies;
13329 
13330 	/* Disable softirqs, including timers from obtaining phba->hbalock */
13331 	local_bh_disable();
13332 
13333 	spin_lock_irq(&phba->hbalock);
13334 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13335 
13336 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
13337 		/* Determine how long we might wait for the active mailbox
13338 		 * command to be gracefully completed by firmware.
13339 		 */
13340 		if (phba->sli.mbox_active)
13341 			timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
13342 						phba->sli.mbox_active) *
13343 						1000) + jiffies;
13344 		spin_unlock_irq(&phba->hbalock);
13345 
13346 		/* Enable softirqs again, done with phba->hbalock */
13347 		local_bh_enable();
13348 
13349 		while (phba->sli.mbox_active) {
13350 			/* Check active mailbox complete status every 2ms */
13351 			msleep(2);
13352 			if (time_after(jiffies, timeout))
13353 				/* Timeout, let the mailbox flush routine to
13354 				 * forcefully release active mailbox command
13355 				 */
13356 				break;
13357 		}
13358 	} else {
13359 		spin_unlock_irq(&phba->hbalock);
13360 
13361 		/* Enable softirqs again, done with phba->hbalock */
13362 		local_bh_enable();
13363 	}
13364 
13365 	lpfc_sli_mbox_sys_flush(phba);
13366 }
13367 
13368 /**
13369  * lpfc_sli_eratt_read - read sli-3 error attention events
13370  * @phba: Pointer to HBA context.
13371  *
13372  * This function is called to read the SLI3 device error attention registers
13373  * for possible error attention events. The caller must hold the hostlock
13374  * with spin_lock_irq().
13375  *
13376  * This function returns 1 when there is Error Attention in the Host Attention
13377  * Register and returns 0 otherwise.
13378  **/
13379 static int
lpfc_sli_eratt_read(struct lpfc_hba * phba)13380 lpfc_sli_eratt_read(struct lpfc_hba *phba)
13381 {
13382 	uint32_t ha_copy;
13383 
13384 	/* Read chip Host Attention (HA) register */
13385 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
13386 		goto unplug_err;
13387 
13388 	if (ha_copy & HA_ERATT) {
13389 		/* Read host status register to retrieve error event */
13390 		if (lpfc_sli_read_hs(phba))
13391 			goto unplug_err;
13392 
13393 		/* Check if there is a deferred error condition is active */
13394 		if ((HS_FFER1 & phba->work_hs) &&
13395 		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13396 		      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
13397 			set_bit(DEFER_ERATT, &phba->hba_flag);
13398 			/* Clear all interrupt enable conditions */
13399 			writel(0, phba->HCregaddr);
13400 			readl(phba->HCregaddr);
13401 		}
13402 
13403 		/* Set the driver HA work bitmap */
13404 		phba->work_ha |= HA_ERATT;
13405 		/* Indicate polling handles this ERATT */
13406 		set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13407 		return 1;
13408 	}
13409 	return 0;
13410 
13411 unplug_err:
13412 	/* Set the driver HS work bitmap */
13413 	phba->work_hs |= UNPLUG_ERR;
13414 	/* Set the driver HA work bitmap */
13415 	phba->work_ha |= HA_ERATT;
13416 	/* Indicate polling handles this ERATT */
13417 	set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13418 	return 1;
13419 }
13420 
13421 /**
13422  * lpfc_sli4_eratt_read - read sli-4 error attention events
13423  * @phba: Pointer to HBA context.
13424  *
13425  * This function is called to read the SLI4 device error attention registers
13426  * for possible error attention events. The caller must hold the hostlock
13427  * with spin_lock_irq().
13428  *
13429  * This function returns 1 when there is Error Attention in the Host Attention
13430  * Register and returns 0 otherwise.
13431  **/
13432 static int
lpfc_sli4_eratt_read(struct lpfc_hba * phba)13433 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13434 {
13435 	uint32_t uerr_sta_hi, uerr_sta_lo;
13436 	uint32_t if_type, portsmphr;
13437 	struct lpfc_register portstat_reg;
13438 	u32 logmask;
13439 
13440 	/*
13441 	 * For now, use the SLI4 device internal unrecoverable error
13442 	 * registers for error attention. This can be changed later.
13443 	 */
13444 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13445 	switch (if_type) {
13446 	case LPFC_SLI_INTF_IF_TYPE_0:
13447 		if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13448 			&uerr_sta_lo) ||
13449 			lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13450 			&uerr_sta_hi)) {
13451 			phba->work_hs |= UNPLUG_ERR;
13452 			phba->work_ha |= HA_ERATT;
13453 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13454 			return 1;
13455 		}
13456 		if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13457 		    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
13458 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13459 					"1423 HBA Unrecoverable error: "
13460 					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13461 					"ue_mask_lo_reg=0x%x, "
13462 					"ue_mask_hi_reg=0x%x\n",
13463 					uerr_sta_lo, uerr_sta_hi,
13464 					phba->sli4_hba.ue_mask_lo,
13465 					phba->sli4_hba.ue_mask_hi);
13466 			phba->work_status[0] = uerr_sta_lo;
13467 			phba->work_status[1] = uerr_sta_hi;
13468 			phba->work_ha |= HA_ERATT;
13469 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13470 			return 1;
13471 		}
13472 		break;
13473 	case LPFC_SLI_INTF_IF_TYPE_2:
13474 	case LPFC_SLI_INTF_IF_TYPE_6:
13475 		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13476 			&portstat_reg.word0) ||
13477 			lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13478 			&portsmphr)){
13479 			phba->work_hs |= UNPLUG_ERR;
13480 			phba->work_ha |= HA_ERATT;
13481 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13482 			return 1;
13483 		}
13484 		if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13485 			phba->work_status[0] =
13486 				readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13487 			phba->work_status[1] =
13488 				readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
13489 			logmask = LOG_TRACE_EVENT;
13490 			if (phba->work_status[0] ==
13491 				SLIPORT_ERR1_REG_ERR_CODE_2 &&
13492 			    phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)
13493 				logmask = LOG_SLI;
13494 			lpfc_printf_log(phba, KERN_ERR, logmask,
13495 					"2885 Port Status Event: "
13496 					"port status reg 0x%x, "
13497 					"port smphr reg 0x%x, "
13498 					"error 1=0x%x, error 2=0x%x\n",
13499 					portstat_reg.word0,
13500 					portsmphr,
13501 					phba->work_status[0],
13502 					phba->work_status[1]);
13503 			phba->work_ha |= HA_ERATT;
13504 			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13505 			return 1;
13506 		}
13507 		break;
13508 	case LPFC_SLI_INTF_IF_TYPE_1:
13509 	default:
13510 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13511 				"2886 HBA Error Attention on unsupported "
13512 				"if type %d.", if_type);
13513 		return 1;
13514 	}
13515 
13516 	return 0;
13517 }
13518 
13519 /**
13520  * lpfc_sli_check_eratt - check error attention events
13521  * @phba: Pointer to HBA context.
13522  *
13523  * This function is called from timer soft interrupt context to check HBA's
13524  * error attention register bit for error attention events.
13525  *
13526  * This function returns 1 when there is Error Attention in the Host Attention
13527  * Register and returns 0 otherwise.
13528  **/
13529 int
lpfc_sli_check_eratt(struct lpfc_hba * phba)13530 lpfc_sli_check_eratt(struct lpfc_hba *phba)
13531 {
13532 	uint32_t ha_copy;
13533 
13534 	/* If somebody is waiting to handle an eratt, don't process it
13535 	 * here. The brdkill function will do this.
13536 	 */
13537 	if (phba->link_flag & LS_IGNORE_ERATT)
13538 		return 0;
13539 
13540 	/* Check if interrupt handler handles this ERATT */
13541 	if (test_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
13542 		/* Interrupt handler has handled ERATT */
13543 		return 0;
13544 
13545 	/*
13546 	 * If there is deferred error attention, do not check for error
13547 	 * attention
13548 	 */
13549 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13550 		return 0;
13551 
13552 	spin_lock_irq(&phba->hbalock);
13553 	/* If PCI channel is offline, don't process it */
13554 	if (unlikely(pci_channel_offline(phba->pcidev))) {
13555 		spin_unlock_irq(&phba->hbalock);
13556 		return 0;
13557 	}
13558 
13559 	switch (phba->sli_rev) {
13560 	case LPFC_SLI_REV2:
13561 	case LPFC_SLI_REV3:
13562 		/* Read chip Host Attention (HA) register */
13563 		ha_copy = lpfc_sli_eratt_read(phba);
13564 		break;
13565 	case LPFC_SLI_REV4:
13566 		/* Read device Uncoverable Error (UERR) registers */
13567 		ha_copy = lpfc_sli4_eratt_read(phba);
13568 		break;
13569 	default:
13570 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13571 				"0299 Invalid SLI revision (%d)\n",
13572 				phba->sli_rev);
13573 		ha_copy = 0;
13574 		break;
13575 	}
13576 	spin_unlock_irq(&phba->hbalock);
13577 
13578 	return ha_copy;
13579 }
13580 
13581 /**
13582  * lpfc_intr_state_check - Check device state for interrupt handling
13583  * @phba: Pointer to HBA context.
13584  *
13585  * This inline routine checks whether a device or its PCI slot is in a state
13586  * that the interrupt should be handled.
13587  *
13588  * This function returns 0 if the device or the PCI slot is in a state that
13589  * interrupt should be handled, otherwise -EIO.
13590  */
13591 static inline int
lpfc_intr_state_check(struct lpfc_hba * phba)13592 lpfc_intr_state_check(struct lpfc_hba *phba)
13593 {
13594 	/* If the pci channel is offline, ignore all the interrupts */
13595 	if (unlikely(pci_channel_offline(phba->pcidev)))
13596 		return -EIO;
13597 
13598 	/* Update device level interrupt statistics */
13599 	phba->sli.slistat.sli_intr++;
13600 
13601 	/* Ignore all interrupts during initialization. */
13602 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13603 		return -EIO;
13604 
13605 	return 0;
13606 }
13607 
13608 /**
13609  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
13610  * @irq: Interrupt number.
13611  * @dev_id: The device context pointer.
13612  *
13613  * This function is directly called from the PCI layer as an interrupt
13614  * service routine when device with SLI-3 interface spec is enabled with
13615  * MSI-X multi-message interrupt mode and there are slow-path events in
13616  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13617  * interrupt mode, this function is called as part of the device-level
13618  * interrupt handler. When the PCI slot is in error recovery or the HBA
13619  * is undergoing initialization, the interrupt handler will not process
13620  * the interrupt. The link attention and ELS ring attention events are
13621  * handled by the worker thread. The interrupt handler signals the worker
13622  * thread and returns for these events. This function is called without
13623  * any lock held. It gets the hbalock to access and update SLI data
13624  * structures.
13625  *
13626  * This function returns IRQ_HANDLED when interrupt is handled else it
13627  * returns IRQ_NONE.
13628  **/
13629 irqreturn_t
lpfc_sli_sp_intr_handler(int irq,void * dev_id)13630 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
13631 {
13632 	struct lpfc_hba  *phba;
13633 	uint32_t ha_copy, hc_copy;
13634 	uint32_t work_ha_copy;
13635 	unsigned long status;
13636 	unsigned long iflag;
13637 	uint32_t control;
13638 
13639 	MAILBOX_t *mbox, *pmbox;
13640 	struct lpfc_vport *vport;
13641 	struct lpfc_nodelist *ndlp;
13642 	struct lpfc_dmabuf *mp;
13643 	LPFC_MBOXQ_t *pmb;
13644 	int rc;
13645 
13646 	/*
13647 	 * Get the driver's phba structure from the dev_id and
13648 	 * assume the HBA is not interrupting.
13649 	 */
13650 	phba = (struct lpfc_hba *)dev_id;
13651 
13652 	if (unlikely(!phba))
13653 		return IRQ_NONE;
13654 
13655 	/*
13656 	 * Stuff needs to be attented to when this function is invoked as an
13657 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13658 	 */
13659 	if (phba->intr_type == MSIX) {
13660 		/* Check device state for handling interrupt */
13661 		if (lpfc_intr_state_check(phba))
13662 			return IRQ_NONE;
13663 		/* Need to read HA REG for slow-path events */
13664 		spin_lock_irqsave(&phba->hbalock, iflag);
13665 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13666 			goto unplug_error;
13667 		/* If somebody is waiting to handle an eratt don't process it
13668 		 * here. The brdkill function will do this.
13669 		 */
13670 		if (phba->link_flag & LS_IGNORE_ERATT)
13671 			ha_copy &= ~HA_ERATT;
13672 		/* Check the need for handling ERATT in interrupt handler */
13673 		if (ha_copy & HA_ERATT) {
13674 			if (test_and_set_bit(HBA_ERATT_HANDLED,
13675 					     &phba->hba_flag))
13676 				/* ERATT polling has handled ERATT */
13677 				ha_copy &= ~HA_ERATT;
13678 		}
13679 
13680 		/*
13681 		 * If there is deferred error attention, do not check for any
13682 		 * interrupt.
13683 		 */
13684 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
13685 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13686 			return IRQ_NONE;
13687 		}
13688 
13689 		/* Clear up only attention source related to slow-path */
13690 		if (lpfc_readl(phba->HCregaddr, &hc_copy))
13691 			goto unplug_error;
13692 
13693 		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13694 			HC_LAINT_ENA | HC_ERINT_ENA),
13695 			phba->HCregaddr);
13696 		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13697 			phba->HAregaddr);
13698 		writel(hc_copy, phba->HCregaddr);
13699 		readl(phba->HAregaddr); /* flush */
13700 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13701 	} else
13702 		ha_copy = phba->ha_copy;
13703 
13704 	work_ha_copy = ha_copy & phba->work_ha_mask;
13705 
13706 	if (work_ha_copy) {
13707 		if (work_ha_copy & HA_LATT) {
13708 			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13709 				/*
13710 				 * Turn off Link Attention interrupts
13711 				 * until CLEAR_LA done
13712 				 */
13713 				spin_lock_irqsave(&phba->hbalock, iflag);
13714 				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
13715 				if (lpfc_readl(phba->HCregaddr, &control))
13716 					goto unplug_error;
13717 				control &= ~HC_LAINT_ENA;
13718 				writel(control, phba->HCregaddr);
13719 				readl(phba->HCregaddr); /* flush */
13720 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13721 			}
13722 			else
13723 				work_ha_copy &= ~HA_LATT;
13724 		}
13725 
13726 		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
13727 			/*
13728 			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13729 			 * the only slow ring.
13730 			 */
13731 			status = (work_ha_copy &
13732 				(HA_RXMASK  << (4*LPFC_ELS_RING)));
13733 			status >>= (4*LPFC_ELS_RING);
13734 			if (status & HA_RXMASK) {
13735 				spin_lock_irqsave(&phba->hbalock, iflag);
13736 				if (lpfc_readl(phba->HCregaddr, &control))
13737 					goto unplug_error;
13738 
13739 				lpfc_debugfs_slow_ring_trc(phba,
13740 				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
13741 				control, status,
13742 				(uint32_t)phba->sli.slistat.sli_intr);
13743 
13744 				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
13745 					lpfc_debugfs_slow_ring_trc(phba,
13746 						"ISR Disable ring:"
13747 						"pwork:x%x hawork:x%x wait:x%x",
13748 						phba->work_ha, work_ha_copy,
13749 						(uint32_t)((unsigned long)
13750 						&phba->work_waitq));
13751 
13752 					control &=
13753 					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
13754 					writel(control, phba->HCregaddr);
13755 					readl(phba->HCregaddr); /* flush */
13756 				}
13757 				else {
13758 					lpfc_debugfs_slow_ring_trc(phba,
13759 						"ISR slow ring:   pwork:"
13760 						"x%x hawork:x%x wait:x%x",
13761 						phba->work_ha, work_ha_copy,
13762 						(uint32_t)((unsigned long)
13763 						&phba->work_waitq));
13764 				}
13765 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13766 			}
13767 		}
13768 		spin_lock_irqsave(&phba->hbalock, iflag);
13769 		if (work_ha_copy & HA_ERATT) {
13770 			if (lpfc_sli_read_hs(phba))
13771 				goto unplug_error;
13772 			/*
13773 			 * Check if there is a deferred error condition
13774 			 * is active
13775 			 */
13776 			if ((HS_FFER1 & phba->work_hs) &&
13777 				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13778 				  HS_FFER6 | HS_FFER7 | HS_FFER8) &
13779 				  phba->work_hs)) {
13780 				set_bit(DEFER_ERATT, &phba->hba_flag);
13781 				/* Clear all interrupt enable conditions */
13782 				writel(0, phba->HCregaddr);
13783 				readl(phba->HCregaddr);
13784 			}
13785 		}
13786 
13787 		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
13788 			pmb = phba->sli.mbox_active;
13789 			pmbox = &pmb->u.mb;
13790 			mbox = phba->mbox;
13791 			vport = pmb->vport;
13792 
13793 			/* First check out the status word */
13794 			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13795 			if (pmbox->mbxOwner != OWN_HOST) {
13796 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13797 				/*
13798 				 * Stray Mailbox Interrupt, mbxCommand <cmd>
13799 				 * mbxStatus <status>
13800 				 */
13801 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13802 						"(%d):0304 Stray Mailbox "
13803 						"Interrupt mbxCommand x%x "
13804 						"mbxStatus x%x\n",
13805 						(vport ? vport->vpi : 0),
13806 						pmbox->mbxCommand,
13807 						pmbox->mbxStatus);
13808 				/* clear mailbox attention bit */
13809 				work_ha_copy &= ~HA_MBATT;
13810 			} else {
13811 				phba->sli.mbox_active = NULL;
13812 				spin_unlock_irqrestore(&phba->hbalock, iflag);
13813 				phba->last_completion_time = jiffies;
13814 				del_timer(&phba->sli.mbox_tmo);
13815 				if (pmb->mbox_cmpl) {
13816 					lpfc_sli_pcimem_bcopy(mbox, pmbox,
13817 							MAILBOX_CMD_SIZE);
13818 					if (pmb->out_ext_byte_len &&
13819 						pmb->ext_buf)
13820 						lpfc_sli_pcimem_bcopy(
13821 						phba->mbox_ext,
13822 						pmb->ext_buf,
13823 						pmb->out_ext_byte_len);
13824 				}
13825 				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13826 					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13827 
13828 					lpfc_debugfs_disc_trc(vport,
13829 						LPFC_DISC_TRC_MBOX_VPORT,
13830 						"MBOX dflt rpi: : "
13831 						"status:x%x rpi:x%x",
13832 						(uint32_t)pmbox->mbxStatus,
13833 						pmbox->un.varWords[0], 0);
13834 
13835 					if (!pmbox->mbxStatus) {
13836 						mp = pmb->ctx_buf;
13837 						ndlp = pmb->ctx_ndlp;
13838 
13839 						/* Reg_LOGIN of dflt RPI was
13840 						 * successful. new lets get
13841 						 * rid of the RPI using the
13842 						 * same mbox buffer.
13843 						 */
13844 						lpfc_unreg_login(phba,
13845 							vport->vpi,
13846 							pmbox->un.varWords[0],
13847 							pmb);
13848 						pmb->mbox_cmpl =
13849 							lpfc_mbx_cmpl_dflt_rpi;
13850 						pmb->ctx_buf = mp;
13851 						pmb->ctx_ndlp = ndlp;
13852 						pmb->vport = vport;
13853 						rc = lpfc_sli_issue_mbox(phba,
13854 								pmb,
13855 								MBX_NOWAIT);
13856 						if (rc != MBX_BUSY)
13857 							lpfc_printf_log(phba,
13858 							KERN_ERR,
13859 							LOG_TRACE_EVENT,
13860 							"0350 rc should have"
13861 							"been MBX_BUSY\n");
13862 						if (rc != MBX_NOT_FINISHED)
13863 							goto send_current_mbox;
13864 					}
13865 				}
13866 				spin_lock_irqsave(
13867 						&phba->pport->work_port_lock,
13868 						iflag);
13869 				phba->pport->work_port_events &=
13870 					~WORKER_MBOX_TMO;
13871 				spin_unlock_irqrestore(
13872 						&phba->pport->work_port_lock,
13873 						iflag);
13874 
13875 				/* Do NOT queue MBX_HEARTBEAT to the worker
13876 				 * thread for processing.
13877 				 */
13878 				if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13879 					/* Process mbox now */
13880 					phba->sli.mbox_active = NULL;
13881 					phba->sli.sli_flag &=
13882 						~LPFC_SLI_MBOX_ACTIVE;
13883 					if (pmb->mbox_cmpl)
13884 						pmb->mbox_cmpl(phba, pmb);
13885 				} else {
13886 					/* Queue to worker thread to process */
13887 					lpfc_mbox_cmpl_put(phba, pmb);
13888 				}
13889 			}
13890 		} else
13891 			spin_unlock_irqrestore(&phba->hbalock, iflag);
13892 
13893 		if ((work_ha_copy & HA_MBATT) &&
13894 		    (phba->sli.mbox_active == NULL)) {
13895 send_current_mbox:
13896 			/* Process next mailbox command if there is one */
13897 			do {
13898 				rc = lpfc_sli_issue_mbox(phba, NULL,
13899 							 MBX_NOWAIT);
13900 			} while (rc == MBX_NOT_FINISHED);
13901 			if (rc != MBX_SUCCESS)
13902 				lpfc_printf_log(phba, KERN_ERR,
13903 						LOG_TRACE_EVENT,
13904 						"0349 rc should be "
13905 						"MBX_SUCCESS\n");
13906 		}
13907 
13908 		spin_lock_irqsave(&phba->hbalock, iflag);
13909 		phba->work_ha |= work_ha_copy;
13910 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13911 		lpfc_worker_wake_up(phba);
13912 	}
13913 	return IRQ_HANDLED;
13914 unplug_error:
13915 	spin_unlock_irqrestore(&phba->hbalock, iflag);
13916 	return IRQ_HANDLED;
13917 
13918 } /* lpfc_sli_sp_intr_handler */
13919 
13920 /**
13921  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13922  * @irq: Interrupt number.
13923  * @dev_id: The device context pointer.
13924  *
13925  * This function is directly called from the PCI layer as an interrupt
13926  * service routine when device with SLI-3 interface spec is enabled with
13927  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13928  * ring event in the HBA. However, when the device is enabled with either
13929  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13930  * device-level interrupt handler. When the PCI slot is in error recovery
13931  * or the HBA is undergoing initialization, the interrupt handler will not
13932  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13933  * the intrrupt context. This function is called without any lock held.
13934  * It gets the hbalock to access and update SLI data structures.
13935  *
13936  * This function returns IRQ_HANDLED when interrupt is handled else it
13937  * returns IRQ_NONE.
13938  **/
13939 irqreturn_t
lpfc_sli_fp_intr_handler(int irq,void * dev_id)13940 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13941 {
13942 	struct lpfc_hba  *phba;
13943 	uint32_t ha_copy;
13944 	unsigned long status;
13945 	unsigned long iflag;
13946 	struct lpfc_sli_ring *pring;
13947 
13948 	/* Get the driver's phba structure from the dev_id and
13949 	 * assume the HBA is not interrupting.
13950 	 */
13951 	phba = (struct lpfc_hba *) dev_id;
13952 
13953 	if (unlikely(!phba))
13954 		return IRQ_NONE;
13955 
13956 	/*
13957 	 * Stuff needs to be attented to when this function is invoked as an
13958 	 * individual interrupt handler in MSI-X multi-message interrupt mode
13959 	 */
13960 	if (phba->intr_type == MSIX) {
13961 		/* Check device state for handling interrupt */
13962 		if (lpfc_intr_state_check(phba))
13963 			return IRQ_NONE;
13964 		/* Need to read HA REG for FCP ring and other ring events */
13965 		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13966 			return IRQ_HANDLED;
13967 
13968 		/*
13969 		 * If there is deferred error attention, do not check for
13970 		 * any interrupt.
13971 		 */
13972 		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13973 			return IRQ_NONE;
13974 
13975 		/* Clear up only attention source related to fast-path */
13976 		spin_lock_irqsave(&phba->hbalock, iflag);
13977 		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13978 			phba->HAregaddr);
13979 		readl(phba->HAregaddr); /* flush */
13980 		spin_unlock_irqrestore(&phba->hbalock, iflag);
13981 	} else
13982 		ha_copy = phba->ha_copy;
13983 
13984 	/*
13985 	 * Process all events on FCP ring. Take the optimized path for FCP IO.
13986 	 */
13987 	ha_copy &= ~(phba->work_ha_mask);
13988 
13989 	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13990 	status >>= (4*LPFC_FCP_RING);
13991 	pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
13992 	if (status & HA_RXMASK)
13993 		lpfc_sli_handle_fast_ring_event(phba, pring, status);
13994 
13995 	if (phba->cfg_multi_ring_support == 2) {
13996 		/*
13997 		 * Process all events on extra ring. Take the optimized path
13998 		 * for extra ring IO.
13999 		 */
14000 		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14001 		status >>= (4*LPFC_EXTRA_RING);
14002 		if (status & HA_RXMASK) {
14003 			lpfc_sli_handle_fast_ring_event(phba,
14004 					&phba->sli.sli3_ring[LPFC_EXTRA_RING],
14005 					status);
14006 		}
14007 	}
14008 	return IRQ_HANDLED;
14009 }  /* lpfc_sli_fp_intr_handler */
14010 
14011 /**
14012  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
14013  * @irq: Interrupt number.
14014  * @dev_id: The device context pointer.
14015  *
14016  * This function is the HBA device-level interrupt handler to device with
14017  * SLI-3 interface spec, called from the PCI layer when either MSI or
14018  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
14019  * requires driver attention. This function invokes the slow-path interrupt
14020  * attention handling function and fast-path interrupt attention handling
14021  * function in turn to process the relevant HBA attention events. This
14022  * function is called without any lock held. It gets the hbalock to access
14023  * and update SLI data structures.
14024  *
14025  * This function returns IRQ_HANDLED when interrupt is handled, else it
14026  * returns IRQ_NONE.
14027  **/
14028 irqreturn_t
lpfc_sli_intr_handler(int irq,void * dev_id)14029 lpfc_sli_intr_handler(int irq, void *dev_id)
14030 {
14031 	struct lpfc_hba  *phba;
14032 	irqreturn_t sp_irq_rc, fp_irq_rc;
14033 	unsigned long status1, status2;
14034 	uint32_t hc_copy;
14035 
14036 	/*
14037 	 * Get the driver's phba structure from the dev_id and
14038 	 * assume the HBA is not interrupting.
14039 	 */
14040 	phba = (struct lpfc_hba *) dev_id;
14041 
14042 	if (unlikely(!phba))
14043 		return IRQ_NONE;
14044 
14045 	/* Check device state for handling interrupt */
14046 	if (lpfc_intr_state_check(phba))
14047 		return IRQ_NONE;
14048 
14049 	spin_lock(&phba->hbalock);
14050 	if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
14051 		spin_unlock(&phba->hbalock);
14052 		return IRQ_HANDLED;
14053 	}
14054 
14055 	if (unlikely(!phba->ha_copy)) {
14056 		spin_unlock(&phba->hbalock);
14057 		return IRQ_NONE;
14058 	} else if (phba->ha_copy & HA_ERATT) {
14059 		if (test_and_set_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
14060 			/* ERATT polling has handled ERATT */
14061 			phba->ha_copy &= ~HA_ERATT;
14062 	}
14063 
14064 	/*
14065 	 * If there is deferred error attention, do not check for any interrupt.
14066 	 */
14067 	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
14068 		spin_unlock(&phba->hbalock);
14069 		return IRQ_NONE;
14070 	}
14071 
14072 	/* Clear attention sources except link and error attentions */
14073 	if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
14074 		spin_unlock(&phba->hbalock);
14075 		return IRQ_HANDLED;
14076 	}
14077 	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
14078 		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
14079 		phba->HCregaddr);
14080 	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
14081 	writel(hc_copy, phba->HCregaddr);
14082 	readl(phba->HAregaddr); /* flush */
14083 	spin_unlock(&phba->hbalock);
14084 
14085 	/*
14086 	 * Invokes slow-path host attention interrupt handling as appropriate.
14087 	 */
14088 
14089 	/* status of events with mailbox and link attention */
14090 	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
14091 
14092 	/* status of events with ELS ring */
14093 	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
14094 	status2 >>= (4*LPFC_ELS_RING);
14095 
14096 	if (status1 || (status2 & HA_RXMASK))
14097 		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
14098 	else
14099 		sp_irq_rc = IRQ_NONE;
14100 
14101 	/*
14102 	 * Invoke fast-path host attention interrupt handling as appropriate.
14103 	 */
14104 
14105 	/* status of events with FCP ring */
14106 	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14107 	status1 >>= (4*LPFC_FCP_RING);
14108 
14109 	/* status of events with extra ring */
14110 	if (phba->cfg_multi_ring_support == 2) {
14111 		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14112 		status2 >>= (4*LPFC_EXTRA_RING);
14113 	} else
14114 		status2 = 0;
14115 
14116 	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
14117 		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
14118 	else
14119 		fp_irq_rc = IRQ_NONE;
14120 
14121 	/* Return device-level interrupt handling status */
14122 	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
14123 }  /* lpfc_sli_intr_handler */
14124 
14125 /**
14126  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14127  * @phba: pointer to lpfc hba data structure.
14128  *
14129  * This routine is invoked by the worker thread to process all the pending
14130  * SLI4 els abort xri events.
14131  **/
lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba * phba)14132 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14133 {
14134 	struct lpfc_cq_event *cq_event;
14135 	unsigned long iflags;
14136 
14137 	/* First, declare the els xri abort event has been handled */
14138 	clear_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14139 
14140 	/* Now, handle all the els xri abort events */
14141 	spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14142 	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14143 		/* Get the first event from the head of the event queue */
14144 		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14145 				 cq_event, struct lpfc_cq_event, list);
14146 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14147 				       iflags);
14148 		/* Notify aborted XRI for ELS work queue */
14149 		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
14150 
14151 		/* Free the event processed back to the free pool */
14152 		lpfc_sli4_cq_event_release(phba, cq_event);
14153 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14154 				  iflags);
14155 	}
14156 	spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14157 }
14158 
14159 /**
14160  * lpfc_sli4_els_preprocess_rspiocbq - Get response iocbq from els wcqe
14161  * @phba: Pointer to HBA context object.
14162  * @irspiocbq: Pointer to work-queue completion queue entry.
14163  *
14164  * This routine handles an ELS work-queue completion event and construct
14165  * a pseudo response ELS IOCBQ from the SLI4 ELS WCQE for the common
14166  * discovery engine to handle.
14167  *
14168  * Return: Pointer to the receive IOCBQ, NULL otherwise.
14169  **/
14170 static struct lpfc_iocbq *
lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba * phba,struct lpfc_iocbq * irspiocbq)14171 lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
14172 				  struct lpfc_iocbq *irspiocbq)
14173 {
14174 	struct lpfc_sli_ring *pring;
14175 	struct lpfc_iocbq *cmdiocbq;
14176 	struct lpfc_wcqe_complete *wcqe;
14177 	unsigned long iflags;
14178 
14179 	pring = lpfc_phba_elsring(phba);
14180 	if (unlikely(!pring))
14181 		return NULL;
14182 
14183 	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
14184 	spin_lock_irqsave(&pring->ring_lock, iflags);
14185 	pring->stats.iocb_event++;
14186 	/* Look up the ELS command IOCB and create pseudo response IOCB */
14187 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14188 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
14189 	if (unlikely(!cmdiocbq)) {
14190 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
14191 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14192 				"0386 ELS complete with no corresponding "
14193 				"cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14194 				wcqe->word0, wcqe->total_data_placed,
14195 				wcqe->parameter, wcqe->word3);
14196 		lpfc_sli_release_iocbq(phba, irspiocbq);
14197 		return NULL;
14198 	}
14199 
14200 	memcpy(&irspiocbq->wqe, &cmdiocbq->wqe, sizeof(union lpfc_wqe128));
14201 	memcpy(&irspiocbq->wcqe_cmpl, wcqe, sizeof(*wcqe));
14202 
14203 	/* Put the iocb back on the txcmplq */
14204 	lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14205 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
14206 
14207 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14208 		spin_lock_irqsave(&phba->hbalock, iflags);
14209 		irspiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
14210 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14211 	}
14212 
14213 	return irspiocbq;
14214 }
14215 
14216 inline struct lpfc_cq_event *
lpfc_cq_event_setup(struct lpfc_hba * phba,void * entry,int size)14217 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14218 {
14219 	struct lpfc_cq_event *cq_event;
14220 
14221 	/* Allocate a new internal CQ_EVENT entry */
14222 	cq_event = lpfc_sli4_cq_event_alloc(phba);
14223 	if (!cq_event) {
14224 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14225 				"0602 Failed to alloc CQ_EVENT entry\n");
14226 		return NULL;
14227 	}
14228 
14229 	/* Move the CQE into the event */
14230 	memcpy(&cq_event->cqe, entry, size);
14231 	return cq_event;
14232 }
14233 
14234 /**
14235  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
14236  * @phba: Pointer to HBA context object.
14237  * @mcqe: Pointer to mailbox completion queue entry.
14238  *
14239  * This routine process a mailbox completion queue entry with asynchronous
14240  * event.
14241  *
14242  * Return: true if work posted to worker thread, otherwise false.
14243  **/
14244 static bool
lpfc_sli4_sp_handle_async_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14245 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14246 {
14247 	struct lpfc_cq_event *cq_event;
14248 	unsigned long iflags;
14249 
14250 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14251 			"0392 Async Event: word0:x%x, word1:x%x, "
14252 			"word2:x%x, word3:x%x\n", mcqe->word0,
14253 			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14254 
14255 	cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14256 	if (!cq_event)
14257 		return false;
14258 
14259 	spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
14260 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
14261 	spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14262 
14263 	/* Set the async event flag */
14264 	set_bit(ASYNC_EVENT, &phba->hba_flag);
14265 
14266 	return true;
14267 }
14268 
14269 /**
14270  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14271  * @phba: Pointer to HBA context object.
14272  * @mcqe: Pointer to mailbox completion queue entry.
14273  *
14274  * This routine process a mailbox completion queue entry with mailbox
14275  * completion event.
14276  *
14277  * Return: true if work posted to worker thread, otherwise false.
14278  **/
14279 static bool
lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba * phba,struct lpfc_mcqe * mcqe)14280 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14281 {
14282 	uint32_t mcqe_status;
14283 	MAILBOX_t *mbox, *pmbox;
14284 	struct lpfc_mqe *mqe;
14285 	struct lpfc_vport *vport;
14286 	struct lpfc_nodelist *ndlp;
14287 	struct lpfc_dmabuf *mp;
14288 	unsigned long iflags;
14289 	LPFC_MBOXQ_t *pmb;
14290 	bool workposted = false;
14291 	int rc;
14292 
14293 	/* If not a mailbox complete MCQE, out by checking mailbox consume */
14294 	if (!bf_get(lpfc_trailer_completed, mcqe))
14295 		goto out_no_mqe_complete;
14296 
14297 	/* Get the reference to the active mbox command */
14298 	spin_lock_irqsave(&phba->hbalock, iflags);
14299 	pmb = phba->sli.mbox_active;
14300 	if (unlikely(!pmb)) {
14301 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14302 				"1832 No pending MBOX command to handle\n");
14303 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14304 		goto out_no_mqe_complete;
14305 	}
14306 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14307 	mqe = &pmb->u.mqe;
14308 	pmbox = (MAILBOX_t *)&pmb->u.mqe;
14309 	mbox = phba->mbox;
14310 	vport = pmb->vport;
14311 
14312 	/* Reset heartbeat timer */
14313 	phba->last_completion_time = jiffies;
14314 	del_timer(&phba->sli.mbox_tmo);
14315 
14316 	/* Move mbox data to caller's mailbox region, do endian swapping */
14317 	if (pmb->mbox_cmpl && mbox)
14318 		lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
14319 
14320 	/*
14321 	 * For mcqe errors, conditionally move a modified error code to
14322 	 * the mbox so that the error will not be missed.
14323 	 */
14324 	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14325 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14326 		if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14327 			bf_set(lpfc_mqe_status, mqe,
14328 			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
14329 	}
14330 	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14331 		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14332 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14333 				      "MBOX dflt rpi: status:x%x rpi:x%x",
14334 				      mcqe_status,
14335 				      pmbox->un.varWords[0], 0);
14336 		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
14337 			mp = pmb->ctx_buf;
14338 			ndlp = pmb->ctx_ndlp;
14339 
14340 			/* Reg_LOGIN of dflt RPI was successful. Mark the
14341 			 * node as having an UNREG_LOGIN in progress to stop
14342 			 * an unsolicited PLOGI from the same NPortId from
14343 			 * starting another mailbox transaction.
14344 			 */
14345 			set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
14346 			lpfc_unreg_login(phba, vport->vpi,
14347 					 pmbox->un.varWords[0], pmb);
14348 			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
14349 			pmb->ctx_buf = mp;
14350 
14351 			/* No reference taken here.  This is a default
14352 			 * RPI reg/immediate unreg cycle. The reference was
14353 			 * taken in the reg rpi path and is released when
14354 			 * this mailbox completes.
14355 			 */
14356 			pmb->ctx_ndlp = ndlp;
14357 			pmb->vport = vport;
14358 			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14359 			if (rc != MBX_BUSY)
14360 				lpfc_printf_log(phba, KERN_ERR,
14361 						LOG_TRACE_EVENT,
14362 						"0385 rc should "
14363 						"have been MBX_BUSY\n");
14364 			if (rc != MBX_NOT_FINISHED)
14365 				goto send_current_mbox;
14366 		}
14367 	}
14368 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14369 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14370 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14371 
14372 	/* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14373 	if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14374 		spin_lock_irqsave(&phba->hbalock, iflags);
14375 		/* Release the mailbox command posting token */
14376 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14377 		phba->sli.mbox_active = NULL;
14378 		if (bf_get(lpfc_trailer_consumed, mcqe))
14379 			lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14380 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14381 
14382 		/* Post the next mbox command, if there is one */
14383 		lpfc_sli4_post_async_mbox(phba);
14384 
14385 		/* Process cmpl now */
14386 		if (pmb->mbox_cmpl)
14387 			pmb->mbox_cmpl(phba, pmb);
14388 		return false;
14389 	}
14390 
14391 	/* There is mailbox completion work to queue to the worker thread */
14392 	spin_lock_irqsave(&phba->hbalock, iflags);
14393 	__lpfc_mbox_cmpl_put(phba, pmb);
14394 	phba->work_ha |= HA_MBATT;
14395 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14396 	workposted = true;
14397 
14398 send_current_mbox:
14399 	spin_lock_irqsave(&phba->hbalock, iflags);
14400 	/* Release the mailbox command posting token */
14401 	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14402 	/* Setting active mailbox pointer need to be in sync to flag clear */
14403 	phba->sli.mbox_active = NULL;
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 	/* Wake up worker thread to post the next pending mailbox command */
14408 	lpfc_worker_wake_up(phba);
14409 	return workposted;
14410 
14411 out_no_mqe_complete:
14412 	spin_lock_irqsave(&phba->hbalock, iflags);
14413 	if (bf_get(lpfc_trailer_consumed, mcqe))
14414 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14415 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14416 	return false;
14417 }
14418 
14419 /**
14420  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14421  * @phba: Pointer to HBA context object.
14422  * @cq: Pointer to associated CQ
14423  * @cqe: Pointer to mailbox completion queue entry.
14424  *
14425  * This routine process a mailbox completion queue entry, it invokes the
14426  * proper mailbox complete handling or asynchronous event handling routine
14427  * according to the MCQE's async bit.
14428  *
14429  * Return: true if work posted to worker thread, otherwise false.
14430  **/
14431 static bool
lpfc_sli4_sp_handle_mcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14432 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14433 			 struct lpfc_cqe *cqe)
14434 {
14435 	struct lpfc_mcqe mcqe;
14436 	bool workposted;
14437 
14438 	cq->CQ_mbox++;
14439 
14440 	/* Copy the mailbox MCQE and convert endian order as needed */
14441 	lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
14442 
14443 	/* Invoke the proper event handling routine */
14444 	if (!bf_get(lpfc_trailer_async, &mcqe))
14445 		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14446 	else
14447 		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14448 	return workposted;
14449 }
14450 
14451 /**
14452  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14453  * @phba: Pointer to HBA context object.
14454  * @cq: Pointer to associated CQ
14455  * @wcqe: Pointer to work-queue completion queue entry.
14456  *
14457  * This routine handles an ELS work-queue completion event.
14458  *
14459  * Return: true if work posted to worker thread, otherwise false.
14460  **/
14461 static bool
lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)14462 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14463 			     struct lpfc_wcqe_complete *wcqe)
14464 {
14465 	struct lpfc_iocbq *irspiocbq;
14466 	unsigned long iflags;
14467 	struct lpfc_sli_ring *pring = cq->pring;
14468 	int txq_cnt = 0;
14469 	int txcmplq_cnt = 0;
14470 
14471 	/* Check for response status */
14472 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14473 		/* Log the error status */
14474 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14475 				"0357 ELS CQE error: status=x%x: "
14476 				"CQE: %08x %08x %08x %08x\n",
14477 				bf_get(lpfc_wcqe_c_status, wcqe),
14478 				wcqe->word0, wcqe->total_data_placed,
14479 				wcqe->parameter, wcqe->word3);
14480 	}
14481 
14482 	/* Get an irspiocbq for later ELS response processing use */
14483 	irspiocbq = lpfc_sli_get_iocbq(phba);
14484 	if (!irspiocbq) {
14485 		if (!list_empty(&pring->txq))
14486 			txq_cnt++;
14487 		if (!list_empty(&pring->txcmplq))
14488 			txcmplq_cnt++;
14489 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14490 			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
14491 			"els_txcmplq_cnt=%d\n",
14492 			txq_cnt, phba->iocb_cnt,
14493 			txcmplq_cnt);
14494 		return false;
14495 	}
14496 
14497 	/* Save off the slow-path queue event for work thread to process */
14498 	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
14499 	spin_lock_irqsave(&phba->hbalock, iflags);
14500 	list_add_tail(&irspiocbq->cq_event.list,
14501 		      &phba->sli4_hba.sp_queue_event);
14502 	spin_unlock_irqrestore(&phba->hbalock, iflags);
14503 	set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14504 
14505 	return true;
14506 }
14507 
14508 /**
14509  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14510  * @phba: Pointer to HBA context object.
14511  * @wcqe: Pointer to work-queue completion queue entry.
14512  *
14513  * This routine handles slow-path WQ entry consumed event by invoking the
14514  * proper WQ release routine to the slow-path WQ.
14515  **/
14516 static void
lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_wcqe_release * wcqe)14517 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14518 			     struct lpfc_wcqe_release *wcqe)
14519 {
14520 	/* sanity check on queue memory */
14521 	if (unlikely(!phba->sli4_hba.els_wq))
14522 		return;
14523 	/* Check for the slow-path ELS work queue */
14524 	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14525 		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14526 				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14527 	else
14528 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14529 				"2579 Slow-path wqe consume event carries "
14530 				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14531 				bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14532 				phba->sli4_hba.els_wq->queue_id);
14533 }
14534 
14535 /**
14536  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14537  * @phba: Pointer to HBA context object.
14538  * @cq: Pointer to a WQ completion queue.
14539  * @wcqe: Pointer to work-queue completion queue entry.
14540  *
14541  * This routine handles an XRI abort event.
14542  *
14543  * Return: true if work posted to worker thread, otherwise false.
14544  **/
14545 static bool
lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct sli4_wcqe_xri_aborted * wcqe)14546 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14547 				   struct lpfc_queue *cq,
14548 				   struct sli4_wcqe_xri_aborted *wcqe)
14549 {
14550 	bool workposted = false;
14551 	struct lpfc_cq_event *cq_event;
14552 	unsigned long iflags;
14553 
14554 	switch (cq->subtype) {
14555 	case LPFC_IO:
14556 		lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14557 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14558 			/* Notify aborted XRI for NVME work queue */
14559 			if (phba->nvmet_support)
14560 				lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14561 		}
14562 		workposted = false;
14563 		break;
14564 	case LPFC_NVME_LS: /* NVME LS uses ELS resources */
14565 	case LPFC_ELS:
14566 		cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14567 		if (!cq_event) {
14568 			workposted = false;
14569 			break;
14570 		}
14571 		cq_event->hdwq = cq->hdwq;
14572 		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14573 				  iflags);
14574 		list_add_tail(&cq_event->list,
14575 			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14576 		/* Set the els xri abort event flag */
14577 		set_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14578 		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14579 				       iflags);
14580 		workposted = true;
14581 		break;
14582 	default:
14583 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14584 				"0603 Invalid CQ subtype %d: "
14585 				"%08x %08x %08x %08x\n",
14586 				cq->subtype, wcqe->word0, wcqe->parameter,
14587 				wcqe->word2, wcqe->word3);
14588 		workposted = false;
14589 		break;
14590 	}
14591 	return workposted;
14592 }
14593 
14594 #define FC_RCTL_MDS_DIAGS	0xF4
14595 
14596 /**
14597  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
14598  * @phba: Pointer to HBA context object.
14599  * @rcqe: Pointer to receive-queue completion queue entry.
14600  *
14601  * This routine process a receive-queue completion queue entry.
14602  *
14603  * Return: true if work posted to worker thread, otherwise false.
14604  **/
14605 static bool
lpfc_sli4_sp_handle_rcqe(struct lpfc_hba * phba,struct lpfc_rcqe * rcqe)14606 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14607 {
14608 	bool workposted = false;
14609 	struct fc_frame_header *fc_hdr;
14610 	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14611 	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
14612 	struct lpfc_nvmet_tgtport *tgtp;
14613 	struct hbq_dmabuf *dma_buf;
14614 	uint32_t status, rq_id;
14615 	unsigned long iflags;
14616 
14617 	/* sanity check on queue memory */
14618 	if (unlikely(!hrq) || unlikely(!drq))
14619 		return workposted;
14620 
14621 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14622 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14623 	else
14624 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14625 	if (rq_id != hrq->queue_id)
14626 		goto out;
14627 
14628 	status = bf_get(lpfc_rcqe_status, rcqe);
14629 	switch (status) {
14630 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14631 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14632 				"2537 Receive Frame Truncated!!\n");
14633 		fallthrough;
14634 	case FC_STATUS_RQ_SUCCESS:
14635 		spin_lock_irqsave(&phba->hbalock, iflags);
14636 		lpfc_sli4_rq_release(hrq, drq);
14637 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14638 		if (!dma_buf) {
14639 			hrq->RQ_no_buf_found++;
14640 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14641 			goto out;
14642 		}
14643 		hrq->RQ_rcv_buf++;
14644 		hrq->RQ_buf_posted--;
14645 		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
14646 
14647 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14648 
14649 		if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14650 		    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14651 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14652 			/* Handle MDS Loopback frames */
14653 			if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
14654 				lpfc_sli4_handle_mds_loopback(phba->pport,
14655 							      dma_buf);
14656 			else
14657 				lpfc_in_buf_free(phba, &dma_buf->dbuf);
14658 			break;
14659 		}
14660 
14661 		/* save off the frame for the work thread to process */
14662 		list_add_tail(&dma_buf->cq_event.list,
14663 			      &phba->sli4_hba.sp_queue_event);
14664 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14665 		/* Frame received */
14666 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14667 		workposted = true;
14668 		break;
14669 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
14670 		if (phba->nvmet_support) {
14671 			tgtp = phba->targetport->private;
14672 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14673 					"6402 RQE Error x%x, posted %d err_cnt "
14674 					"%d: %x %x %x\n",
14675 					status, hrq->RQ_buf_posted,
14676 					hrq->RQ_no_posted_buf,
14677 					atomic_read(&tgtp->rcv_fcp_cmd_in),
14678 					atomic_read(&tgtp->rcv_fcp_cmd_out),
14679 					atomic_read(&tgtp->xmt_fcp_release));
14680 		}
14681 		fallthrough;
14682 
14683 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
14684 		hrq->RQ_no_posted_buf++;
14685 		/* Post more buffers if possible */
14686 		set_bit(HBA_POST_RECEIVE_BUFFER, &phba->hba_flag);
14687 		workposted = true;
14688 		break;
14689 	case FC_STATUS_RQ_DMA_FAILURE:
14690 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14691 				"2564 RQE DMA Error x%x, x%08x x%08x x%08x "
14692 				"x%08x\n",
14693 				status, rcqe->word0, rcqe->word1,
14694 				rcqe->word2, rcqe->word3);
14695 
14696 		/* If IV set, no further recovery */
14697 		if (bf_get(lpfc_rcqe_iv, rcqe))
14698 			break;
14699 
14700 		/* recycle consumed resource */
14701 		spin_lock_irqsave(&phba->hbalock, iflags);
14702 		lpfc_sli4_rq_release(hrq, drq);
14703 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14704 		if (!dma_buf) {
14705 			hrq->RQ_no_buf_found++;
14706 			spin_unlock_irqrestore(&phba->hbalock, iflags);
14707 			break;
14708 		}
14709 		hrq->RQ_rcv_buf++;
14710 		hrq->RQ_buf_posted--;
14711 		spin_unlock_irqrestore(&phba->hbalock, iflags);
14712 		lpfc_in_buf_free(phba, &dma_buf->dbuf);
14713 		break;
14714 	default:
14715 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14716 				"2565 Unexpected RQE Status x%x, w0-3 x%08x "
14717 				"x%08x x%08x x%08x\n",
14718 				status, rcqe->word0, rcqe->word1,
14719 				rcqe->word2, rcqe->word3);
14720 		break;
14721 	}
14722 out:
14723 	return workposted;
14724 }
14725 
14726 /**
14727  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14728  * @phba: Pointer to HBA context object.
14729  * @cq: Pointer to the completion queue.
14730  * @cqe: Pointer to a completion queue entry.
14731  *
14732  * This routine process a slow-path work-queue or receive queue completion queue
14733  * entry.
14734  *
14735  * Return: true if work posted to worker thread, otherwise false.
14736  **/
14737 static bool
lpfc_sli4_sp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)14738 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14739 			 struct lpfc_cqe *cqe)
14740 {
14741 	struct lpfc_cqe cqevt;
14742 	bool workposted = false;
14743 
14744 	/* Copy the work queue CQE and convert endian order if needed */
14745 	lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
14746 
14747 	/* Check and process for different type of WCQE and dispatch */
14748 	switch (bf_get(lpfc_cqe_code, &cqevt)) {
14749 	case CQE_CODE_COMPL_WQE:
14750 		/* Process the WQ/RQ complete event */
14751 		phba->last_completion_time = jiffies;
14752 		workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
14753 				(struct lpfc_wcqe_complete *)&cqevt);
14754 		break;
14755 	case CQE_CODE_RELEASE_WQE:
14756 		/* Process the WQ release event */
14757 		lpfc_sli4_sp_handle_rel_wcqe(phba,
14758 				(struct lpfc_wcqe_release *)&cqevt);
14759 		break;
14760 	case CQE_CODE_XRI_ABORTED:
14761 		/* Process the WQ XRI abort event */
14762 		phba->last_completion_time = jiffies;
14763 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14764 				(struct sli4_wcqe_xri_aborted *)&cqevt);
14765 		break;
14766 	case CQE_CODE_RECEIVE:
14767 	case CQE_CODE_RECEIVE_V1:
14768 		/* Process the RQ event */
14769 		phba->last_completion_time = jiffies;
14770 		workposted = lpfc_sli4_sp_handle_rcqe(phba,
14771 				(struct lpfc_rcqe *)&cqevt);
14772 		break;
14773 	default:
14774 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14775 				"0388 Not a valid WCQE code: x%x\n",
14776 				bf_get(lpfc_cqe_code, &cqevt));
14777 		break;
14778 	}
14779 	return workposted;
14780 }
14781 
14782 /**
14783  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14784  * @phba: Pointer to HBA context object.
14785  * @eqe: Pointer to fast-path event queue entry.
14786  * @speq: Pointer to slow-path event queue.
14787  *
14788  * This routine process a event queue entry from the slow-path event queue.
14789  * It will check the MajorCode and MinorCode to determine this is for a
14790  * completion event on a completion queue, if not, an error shall be logged
14791  * and just return. Otherwise, it will get to the corresponding completion
14792  * queue and process all the entries on that completion queue, rearm the
14793  * completion queue, and then return.
14794  *
14795  **/
14796 static void
lpfc_sli4_sp_handle_eqe(struct lpfc_hba * phba,struct lpfc_eqe * eqe,struct lpfc_queue * speq)14797 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14798 	struct lpfc_queue *speq)
14799 {
14800 	struct lpfc_queue *cq = NULL, *childq;
14801 	uint16_t cqid;
14802 	int ret = 0;
14803 
14804 	/* Get the reference to the corresponding CQ */
14805 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14806 
14807 	list_for_each_entry(childq, &speq->child_list, list) {
14808 		if (childq->queue_id == cqid) {
14809 			cq = childq;
14810 			break;
14811 		}
14812 	}
14813 	if (unlikely(!cq)) {
14814 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14815 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14816 					"0365 Slow-path CQ identifier "
14817 					"(%d) does not exist\n", cqid);
14818 		return;
14819 	}
14820 
14821 	/* Save EQ associated with this CQ */
14822 	cq->assoc_qp = speq;
14823 
14824 	if (is_kdump_kernel())
14825 		ret = queue_work(phba->wq, &cq->spwork);
14826 	else
14827 		ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14828 
14829 	if (!ret)
14830 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14831 				"0390 Cannot schedule queue work "
14832 				"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14833 				cqid, cq->queue_id, raw_smp_processor_id());
14834 }
14835 
14836 /**
14837  * __lpfc_sli4_process_cq - Process elements of a CQ
14838  * @phba: Pointer to HBA context object.
14839  * @cq: Pointer to CQ to be processed
14840  * @handler: Routine to process each cqe
14841  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14842  *
14843  * This routine processes completion queue entries in a CQ. While a valid
14844  * queue element is found, the handler is called. During processing checks
14845  * are made for periodic doorbell writes to let the hardware know of
14846  * element consumption.
14847  *
14848  * If the max limit on cqes to process is hit, or there are no more valid
14849  * entries, the loop stops. If we processed a sufficient number of elements,
14850  * meaning there is sufficient load, rather than rearming and generating
14851  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14852  * indicates no rescheduling.
14853  *
14854  * Returns True if work scheduled, False otherwise.
14855  **/
14856 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)14857 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14858 	bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14859 			struct lpfc_cqe *), unsigned long *delay)
14860 {
14861 	struct lpfc_cqe *cqe;
14862 	bool workposted = false;
14863 	int count = 0, consumed = 0;
14864 	bool arm = true;
14865 
14866 	/* default - no reschedule */
14867 	*delay = 0;
14868 
14869 	if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14870 		goto rearm_and_exit;
14871 
14872 	/* Process all the entries to the CQ */
14873 	cq->q_flag = 0;
14874 	cqe = lpfc_sli4_cq_get(cq);
14875 	while (cqe) {
14876 		workposted |= handler(phba, cq, cqe);
14877 		__lpfc_sli4_consume_cqe(phba, cq, cqe);
14878 
14879 		consumed++;
14880 		if (!(++count % cq->max_proc_limit))
14881 			break;
14882 
14883 		if (!(count % cq->notify_interval)) {
14884 			phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14885 						LPFC_QUEUE_NOARM);
14886 			consumed = 0;
14887 			cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14888 		}
14889 
14890 		if (count == LPFC_NVMET_CQ_NOTIFY)
14891 			cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14892 
14893 		cqe = lpfc_sli4_cq_get(cq);
14894 	}
14895 	if (count >= phba->cfg_cq_poll_threshold) {
14896 		*delay = 1;
14897 		arm = false;
14898 	}
14899 
14900 	/* Track the max number of CQEs processed in 1 EQ */
14901 	if (count > cq->CQ_max_cqe)
14902 		cq->CQ_max_cqe = count;
14903 
14904 	cq->assoc_qp->EQ_cqe_cnt += count;
14905 
14906 	/* Catch the no cq entry condition */
14907 	if (unlikely(count == 0))
14908 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14909 				"0369 No entry from completion queue "
14910 				"qid=%d\n", cq->queue_id);
14911 
14912 	xchg(&cq->queue_claimed, 0);
14913 
14914 rearm_and_exit:
14915 	phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14916 			arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14917 
14918 	return workposted;
14919 }
14920 
14921 /**
14922  * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14923  * @cq: pointer to CQ to process
14924  *
14925  * This routine calls the cq processing routine with a handler specific
14926  * to the type of queue bound to it.
14927  *
14928  * The CQ routine returns two values: the first is the calling status,
14929  * which indicates whether work was queued to the  background discovery
14930  * thread. If true, the routine should wakeup the discovery thread;
14931  * the second is the delay parameter. If non-zero, rather than rearming
14932  * the CQ and yet another interrupt, the CQ handler should be queued so
14933  * that it is processed in a subsequent polling action. The value of
14934  * the delay indicates when to reschedule it.
14935  **/
14936 static void
__lpfc_sli4_sp_process_cq(struct lpfc_queue * cq)14937 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14938 {
14939 	struct lpfc_hba *phba = cq->phba;
14940 	unsigned long delay;
14941 	bool workposted = false;
14942 	int ret = 0;
14943 
14944 	/* Process and rearm the CQ */
14945 	switch (cq->type) {
14946 	case LPFC_MCQ:
14947 		workposted |= __lpfc_sli4_process_cq(phba, cq,
14948 						lpfc_sli4_sp_handle_mcqe,
14949 						&delay);
14950 		break;
14951 	case LPFC_WCQ:
14952 		if (cq->subtype == LPFC_IO)
14953 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14954 						lpfc_sli4_fp_handle_cqe,
14955 						&delay);
14956 		else
14957 			workposted |= __lpfc_sli4_process_cq(phba, cq,
14958 						lpfc_sli4_sp_handle_cqe,
14959 						&delay);
14960 		break;
14961 	default:
14962 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14963 				"0370 Invalid completion queue type (%d)\n",
14964 				cq->type);
14965 		return;
14966 	}
14967 
14968 	if (delay) {
14969 		if (is_kdump_kernel())
14970 			ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14971 						delay);
14972 		else
14973 			ret = queue_delayed_work_on(cq->chann, phba->wq,
14974 						&cq->sched_spwork, delay);
14975 		if (!ret)
14976 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14977 				"0394 Cannot schedule queue work "
14978 				"for cqid=%d on CPU %d\n",
14979 				cq->queue_id, cq->chann);
14980 	}
14981 
14982 	/* wake up worker thread if there are works to be done */
14983 	if (workposted)
14984 		lpfc_worker_wake_up(phba);
14985 }
14986 
14987 /**
14988  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14989  *   interrupt
14990  * @work: pointer to work element
14991  *
14992  * translates from the work handler and calls the slow-path handler.
14993  **/
14994 static void
lpfc_sli4_sp_process_cq(struct work_struct * work)14995 lpfc_sli4_sp_process_cq(struct work_struct *work)
14996 {
14997 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14998 
14999 	__lpfc_sli4_sp_process_cq(cq);
15000 }
15001 
15002 /**
15003  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
15004  * @work: pointer to work element
15005  *
15006  * translates from the work handler and calls the slow-path handler.
15007  **/
15008 static void
lpfc_sli4_dly_sp_process_cq(struct work_struct * work)15009 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
15010 {
15011 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15012 					struct lpfc_queue, sched_spwork);
15013 
15014 	__lpfc_sli4_sp_process_cq(cq);
15015 }
15016 
15017 /**
15018  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
15019  * @phba: Pointer to HBA context object.
15020  * @cq: Pointer to associated CQ
15021  * @wcqe: Pointer to work-queue completion queue entry.
15022  *
15023  * This routine process a fast-path work queue completion entry from fast-path
15024  * event queue for FCP command response completion.
15025  **/
15026 static void
lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_complete * wcqe)15027 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15028 			     struct lpfc_wcqe_complete *wcqe)
15029 {
15030 	struct lpfc_sli_ring *pring = cq->pring;
15031 	struct lpfc_iocbq *cmdiocbq;
15032 	unsigned long iflags;
15033 
15034 	/* Check for response status */
15035 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15036 		/* If resource errors reported from HBA, reduce queue
15037 		 * depth of the SCSI device.
15038 		 */
15039 		if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15040 		     IOSTAT_LOCAL_REJECT)) &&
15041 		    ((wcqe->parameter & IOERR_PARAM_MASK) ==
15042 		     IOERR_NO_RESOURCES))
15043 			phba->lpfc_rampdown_queue_depth(phba);
15044 
15045 		/* Log the cmpl status */
15046 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15047 				"0373 FCP CQE cmpl: status=x%x: "
15048 				"CQE: %08x %08x %08x %08x\n",
15049 				bf_get(lpfc_wcqe_c_status, wcqe),
15050 				wcqe->word0, wcqe->total_data_placed,
15051 				wcqe->parameter, wcqe->word3);
15052 	}
15053 
15054 	/* Look up the FCP command IOCB and create pseudo response IOCB */
15055 	spin_lock_irqsave(&pring->ring_lock, iflags);
15056 	pring->stats.iocb_event++;
15057 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15058 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15059 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
15060 	if (unlikely(!cmdiocbq)) {
15061 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15062 				"0374 FCP complete with no corresponding "
15063 				"cmdiocb: iotag (%d)\n",
15064 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15065 		return;
15066 	}
15067 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15068 	cmdiocbq->isr_timestamp = cq->isr_timestamp;
15069 #endif
15070 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
15071 		spin_lock_irqsave(&phba->hbalock, iflags);
15072 		cmdiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
15073 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15074 	}
15075 
15076 	if (cmdiocbq->cmd_cmpl) {
15077 		/* For FCP the flag is cleared in cmd_cmpl */
15078 		if (!(cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
15079 		    cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED) {
15080 			spin_lock_irqsave(&phba->hbalock, iflags);
15081 			cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
15082 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15083 		}
15084 
15085 		/* Pass the cmd_iocb and the wcqe to the upper layer */
15086 		memcpy(&cmdiocbq->wcqe_cmpl, wcqe,
15087 		       sizeof(struct lpfc_wcqe_complete));
15088 		cmdiocbq->cmd_cmpl(phba, cmdiocbq, cmdiocbq);
15089 	} else {
15090 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15091 				"0375 FCP cmdiocb not callback function "
15092 				"iotag: (%d)\n",
15093 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15094 	}
15095 }
15096 
15097 /**
15098  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15099  * @phba: Pointer to HBA context object.
15100  * @cq: Pointer to completion queue.
15101  * @wcqe: Pointer to work-queue completion queue entry.
15102  *
15103  * This routine handles an fast-path WQ entry consumed event by invoking the
15104  * proper WQ release routine to the slow-path WQ.
15105  **/
15106 static void
lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_wcqe_release * wcqe)15107 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15108 			     struct lpfc_wcqe_release *wcqe)
15109 {
15110 	struct lpfc_queue *childwq;
15111 	bool wqid_matched = false;
15112 	uint16_t hba_wqid;
15113 
15114 	/* Check for fast-path FCP work queue release */
15115 	hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
15116 	list_for_each_entry(childwq, &cq->child_list, list) {
15117 		if (childwq->queue_id == hba_wqid) {
15118 			lpfc_sli4_wq_release(childwq,
15119 					bf_get(lpfc_wcqe_r_wqe_index, wcqe));
15120 			if (childwq->q_flag & HBA_NVMET_WQFULL)
15121 				lpfc_nvmet_wqfull_process(phba, childwq);
15122 			wqid_matched = true;
15123 			break;
15124 		}
15125 	}
15126 	/* Report warning log message if no match found */
15127 	if (wqid_matched != true)
15128 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15129 				"2580 Fast-path wqe consume event carries "
15130 				"miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
15131 }
15132 
15133 /**
15134  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15135  * @phba: Pointer to HBA context object.
15136  * @cq: Pointer to completion queue.
15137  * @rcqe: Pointer to receive-queue completion queue entry.
15138  *
15139  * This routine process a receive-queue completion queue entry.
15140  *
15141  * Return: true if work posted to worker thread, otherwise false.
15142  **/
15143 static bool
lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_rcqe * rcqe)15144 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15145 			    struct lpfc_rcqe *rcqe)
15146 {
15147 	bool workposted = false;
15148 	struct lpfc_queue *hrq;
15149 	struct lpfc_queue *drq;
15150 	struct rqb_dmabuf *dma_buf;
15151 	struct fc_frame_header *fc_hdr;
15152 	struct lpfc_nvmet_tgtport *tgtp;
15153 	uint32_t status, rq_id;
15154 	unsigned long iflags;
15155 	uint32_t fctl, idx;
15156 
15157 	if ((phba->nvmet_support == 0) ||
15158 	    (phba->sli4_hba.nvmet_cqset == NULL))
15159 		return workposted;
15160 
15161 	idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15162 	hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15163 	drq = phba->sli4_hba.nvmet_mrq_data[idx];
15164 
15165 	/* sanity check on queue memory */
15166 	if (unlikely(!hrq) || unlikely(!drq))
15167 		return workposted;
15168 
15169 	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15170 		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15171 	else
15172 		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15173 
15174 	if ((phba->nvmet_support == 0) ||
15175 	    (rq_id != hrq->queue_id))
15176 		return workposted;
15177 
15178 	status = bf_get(lpfc_rcqe_status, rcqe);
15179 	switch (status) {
15180 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
15181 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15182 				"6126 Receive Frame Truncated!!\n");
15183 		fallthrough;
15184 	case FC_STATUS_RQ_SUCCESS:
15185 		spin_lock_irqsave(&phba->hbalock, iflags);
15186 		lpfc_sli4_rq_release(hrq, drq);
15187 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15188 		if (!dma_buf) {
15189 			hrq->RQ_no_buf_found++;
15190 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15191 			goto out;
15192 		}
15193 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15194 		hrq->RQ_rcv_buf++;
15195 		hrq->RQ_buf_posted--;
15196 		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15197 
15198 		/* Just some basic sanity checks on FCP Command frame */
15199 		fctl = (fc_hdr->fh_f_ctl[0] << 16 |
15200 			fc_hdr->fh_f_ctl[1] << 8 |
15201 			fc_hdr->fh_f_ctl[2]);
15202 		if (((fctl &
15203 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15204 		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15205 		    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15206 			goto drop;
15207 
15208 		if (fc_hdr->fh_type == FC_TYPE_FCP) {
15209 			dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
15210 			lpfc_nvmet_unsol_fcp_event(
15211 				phba, idx, dma_buf, cq->isr_timestamp,
15212 				cq->q_flag & HBA_NVMET_CQ_NOTIFY);
15213 			return false;
15214 		}
15215 drop:
15216 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15217 		break;
15218 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
15219 		if (phba->nvmet_support) {
15220 			tgtp = phba->targetport->private;
15221 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15222 					"6401 RQE Error x%x, posted %d err_cnt "
15223 					"%d: %x %x %x\n",
15224 					status, hrq->RQ_buf_posted,
15225 					hrq->RQ_no_posted_buf,
15226 					atomic_read(&tgtp->rcv_fcp_cmd_in),
15227 					atomic_read(&tgtp->rcv_fcp_cmd_out),
15228 					atomic_read(&tgtp->xmt_fcp_release));
15229 		}
15230 		fallthrough;
15231 
15232 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
15233 		hrq->RQ_no_posted_buf++;
15234 		/* Post more buffers if possible */
15235 		break;
15236 	case FC_STATUS_RQ_DMA_FAILURE:
15237 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15238 				"2575 RQE DMA Error x%x, x%08x x%08x x%08x "
15239 				"x%08x\n",
15240 				status, rcqe->word0, rcqe->word1,
15241 				rcqe->word2, rcqe->word3);
15242 
15243 		/* If IV set, no further recovery */
15244 		if (bf_get(lpfc_rcqe_iv, rcqe))
15245 			break;
15246 
15247 		/* recycle consumed resource */
15248 		spin_lock_irqsave(&phba->hbalock, iflags);
15249 		lpfc_sli4_rq_release(hrq, drq);
15250 		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15251 		if (!dma_buf) {
15252 			hrq->RQ_no_buf_found++;
15253 			spin_unlock_irqrestore(&phba->hbalock, iflags);
15254 			break;
15255 		}
15256 		hrq->RQ_rcv_buf++;
15257 		hrq->RQ_buf_posted--;
15258 		spin_unlock_irqrestore(&phba->hbalock, iflags);
15259 		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15260 		break;
15261 	default:
15262 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15263 				"2576 Unexpected RQE Status x%x, w0-3 x%08x "
15264 				"x%08x x%08x x%08x\n",
15265 				status, rcqe->word0, rcqe->word1,
15266 				rcqe->word2, rcqe->word3);
15267 		break;
15268 	}
15269 out:
15270 	return workposted;
15271 }
15272 
15273 /**
15274  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
15275  * @phba: adapter with cq
15276  * @cq: Pointer to the completion queue.
15277  * @cqe: Pointer to fast-path completion queue entry.
15278  *
15279  * This routine process a fast-path work queue completion entry from fast-path
15280  * event queue for FCP command response completion.
15281  *
15282  * Return: true if work posted to worker thread, otherwise false.
15283  **/
15284 static bool
lpfc_sli4_fp_handle_cqe(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_cqe * cqe)15285 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15286 			 struct lpfc_cqe *cqe)
15287 {
15288 	struct lpfc_wcqe_release wcqe;
15289 	bool workposted = false;
15290 
15291 	/* Copy the work queue CQE and convert endian order if needed */
15292 	lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
15293 
15294 	/* Check and process for different type of WCQE and dispatch */
15295 	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15296 	case CQE_CODE_COMPL_WQE:
15297 	case CQE_CODE_NVME_ERSP:
15298 		cq->CQ_wq++;
15299 		/* Process the WQ complete event */
15300 		phba->last_completion_time = jiffies;
15301 		if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
15302 			lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
15303 				(struct lpfc_wcqe_complete *)&wcqe);
15304 		break;
15305 	case CQE_CODE_RELEASE_WQE:
15306 		cq->CQ_release_wqe++;
15307 		/* Process the WQ release event */
15308 		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15309 				(struct lpfc_wcqe_release *)&wcqe);
15310 		break;
15311 	case CQE_CODE_XRI_ABORTED:
15312 		cq->CQ_xri_aborted++;
15313 		/* Process the WQ XRI abort event */
15314 		phba->last_completion_time = jiffies;
15315 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15316 				(struct sli4_wcqe_xri_aborted *)&wcqe);
15317 		break;
15318 	case CQE_CODE_RECEIVE_V1:
15319 	case CQE_CODE_RECEIVE:
15320 		phba->last_completion_time = jiffies;
15321 		if (cq->subtype == LPFC_NVMET) {
15322 			workposted = lpfc_sli4_nvmet_handle_rcqe(
15323 				phba, cq, (struct lpfc_rcqe *)&wcqe);
15324 		}
15325 		break;
15326 	default:
15327 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15328 				"0144 Not a valid CQE code: x%x\n",
15329 				bf_get(lpfc_wcqe_c_code, &wcqe));
15330 		break;
15331 	}
15332 	return workposted;
15333 }
15334 
15335 /**
15336  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15337  * @cq: Pointer to CQ to be processed
15338  *
15339  * This routine calls the cq processing routine with the handler for
15340  * fast path CQEs.
15341  *
15342  * The CQ routine returns two values: the first is the calling status,
15343  * which indicates whether work was queued to the  background discovery
15344  * thread. If true, the routine should wakeup the discovery thread;
15345  * the second is the delay parameter. If non-zero, rather than rearming
15346  * the CQ and yet another interrupt, the CQ handler should be queued so
15347  * that it is processed in a subsequent polling action. The value of
15348  * the delay indicates when to reschedule it.
15349  **/
15350 static void
__lpfc_sli4_hba_process_cq(struct lpfc_queue * cq)15351 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
15352 {
15353 	struct lpfc_hba *phba = cq->phba;
15354 	unsigned long delay;
15355 	bool workposted = false;
15356 	int ret;
15357 
15358 	/* process and rearm the CQ */
15359 	workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
15360 					     &delay);
15361 
15362 	if (delay) {
15363 		if (is_kdump_kernel())
15364 			ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15365 						delay);
15366 		else
15367 			ret = queue_delayed_work_on(cq->chann, phba->wq,
15368 						&cq->sched_irqwork, delay);
15369 		if (!ret)
15370 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15371 					"0367 Cannot schedule queue work "
15372 					"for cqid=%d on CPU %d\n",
15373 					cq->queue_id, cq->chann);
15374 	}
15375 
15376 	/* wake up worker thread if there are works to be done */
15377 	if (workposted)
15378 		lpfc_worker_wake_up(phba);
15379 }
15380 
15381 /**
15382  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15383  *   interrupt
15384  * @work: pointer to work element
15385  *
15386  * translates from the work handler and calls the fast-path handler.
15387  **/
15388 static void
lpfc_sli4_hba_process_cq(struct work_struct * work)15389 lpfc_sli4_hba_process_cq(struct work_struct *work)
15390 {
15391 	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
15392 
15393 	__lpfc_sli4_hba_process_cq(cq);
15394 }
15395 
15396 /**
15397  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
15398  * @phba: Pointer to HBA context object.
15399  * @eq: Pointer to the queue structure.
15400  * @eqe: Pointer to fast-path event queue entry.
15401  * @poll_mode: poll_mode to execute processing the cq.
15402  *
15403  * This routine process a event queue entry from the fast-path event queue.
15404  * It will check the MajorCode and MinorCode to determine this is for a
15405  * completion event on a completion queue, if not, an error shall be logged
15406  * and just return. Otherwise, it will get to the corresponding completion
15407  * queue and process all the entries on the completion queue, rearm the
15408  * completion queue, and then return.
15409  **/
15410 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)15411 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15412 			 struct lpfc_eqe *eqe, enum lpfc_poll_mode poll_mode)
15413 {
15414 	struct lpfc_queue *cq = NULL;
15415 	uint32_t qidx = eq->hdwq;
15416 	uint16_t cqid, id;
15417 	int ret;
15418 
15419 	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
15420 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15421 				"0366 Not a valid completion "
15422 				"event: majorcode=x%x, minorcode=x%x\n",
15423 				bf_get_le32(lpfc_eqe_major_code, eqe),
15424 				bf_get_le32(lpfc_eqe_minor_code, eqe));
15425 		return;
15426 	}
15427 
15428 	/* Get the reference to the corresponding CQ */
15429 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15430 
15431 	/* Use the fast lookup method first */
15432 	if (cqid <= phba->sli4_hba.cq_max) {
15433 		cq = phba->sli4_hba.cq_lookup[cqid];
15434 		if (cq)
15435 			goto  work_cq;
15436 	}
15437 
15438 	/* Next check for NVMET completion */
15439 	if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15440 		id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15441 		if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15442 			/* Process NVMET unsol rcv */
15443 			cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15444 			goto  process_cq;
15445 		}
15446 	}
15447 
15448 	if (phba->sli4_hba.nvmels_cq &&
15449 	    (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15450 		/* Process NVME unsol rcv */
15451 		cq = phba->sli4_hba.nvmels_cq;
15452 	}
15453 
15454 	/* Otherwise this is a Slow path event */
15455 	if (cq == NULL) {
15456 		lpfc_sli4_sp_handle_eqe(phba, eqe,
15457 					phba->sli4_hba.hdwq[qidx].hba_eq);
15458 		return;
15459 	}
15460 
15461 process_cq:
15462 	if (unlikely(cqid != cq->queue_id)) {
15463 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15464 				"0368 Miss-matched fast-path completion "
15465 				"queue identifier: eqcqid=%d, fcpcqid=%d\n",
15466 				cqid, cq->queue_id);
15467 		return;
15468 	}
15469 
15470 work_cq:
15471 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15472 	if (phba->ktime_on)
15473 		cq->isr_timestamp = ktime_get_ns();
15474 	else
15475 		cq->isr_timestamp = 0;
15476 #endif
15477 
15478 	switch (poll_mode) {
15479 	case LPFC_THREADED_IRQ:
15480 		__lpfc_sli4_hba_process_cq(cq);
15481 		break;
15482 	case LPFC_QUEUE_WORK:
15483 	default:
15484 		if (is_kdump_kernel())
15485 			ret = queue_work(phba->wq, &cq->irqwork);
15486 		else
15487 			ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15488 		if (!ret)
15489 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15490 					"0383 Cannot schedule queue work "
15491 					"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15492 					cqid, cq->queue_id,
15493 					raw_smp_processor_id());
15494 		break;
15495 	}
15496 }
15497 
15498 /**
15499  * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
15500  * @work: pointer to work element
15501  *
15502  * translates from the work handler and calls the fast-path handler.
15503  **/
15504 static void
lpfc_sli4_dly_hba_process_cq(struct work_struct * work)15505 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
15506 {
15507 	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15508 					struct lpfc_queue, sched_irqwork);
15509 
15510 	__lpfc_sli4_hba_process_cq(cq);
15511 }
15512 
15513 /**
15514  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
15515  * @irq: Interrupt number.
15516  * @dev_id: The device context pointer.
15517  *
15518  * This function is directly called from the PCI layer as an interrupt
15519  * service routine when device with SLI-4 interface spec is enabled with
15520  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15521  * ring event in the HBA. However, when the device is enabled with either
15522  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15523  * device-level interrupt handler. When the PCI slot is in error recovery
15524  * or the HBA is undergoing initialization, the interrupt handler will not
15525  * process the interrupt. The SCSI FCP fast-path ring event are handled in
15526  * the intrrupt context. This function is called without any lock held.
15527  * It gets the hbalock to access and update SLI data structures. Note that,
15528  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15529  * equal to that of FCP CQ index.
15530  *
15531  * The link attention and ELS ring attention events are handled
15532  * by the worker thread. The interrupt handler signals the worker thread
15533  * and returns for these events. This function is called without any lock
15534  * held. It gets the hbalock to access and update SLI data structures.
15535  *
15536  * This function returns IRQ_HANDLED when interrupt is handled, IRQ_WAKE_THREAD
15537  * when interrupt is scheduled to be handled from a threaded irq context, or
15538  * else returns IRQ_NONE.
15539  **/
15540 irqreturn_t
lpfc_sli4_hba_intr_handler(int irq,void * dev_id)15541 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
15542 {
15543 	struct lpfc_hba *phba;
15544 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
15545 	struct lpfc_queue *fpeq;
15546 	unsigned long iflag;
15547 	int hba_eqidx;
15548 	int ecount = 0;
15549 	struct lpfc_eq_intr_info *eqi;
15550 
15551 	/* Get the driver's phba structure from the dev_id */
15552 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15553 	phba = hba_eq_hdl->phba;
15554 	hba_eqidx = hba_eq_hdl->idx;
15555 
15556 	if (unlikely(!phba))
15557 		return IRQ_NONE;
15558 	if (unlikely(!phba->sli4_hba.hdwq))
15559 		return IRQ_NONE;
15560 
15561 	/* Get to the EQ struct associated with this vector */
15562 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
15563 	if (unlikely(!fpeq))
15564 		return IRQ_NONE;
15565 
15566 	/* Check device state for handling interrupt */
15567 	if (unlikely(lpfc_intr_state_check(phba))) {
15568 		/* Check again for link_state with lock held */
15569 		spin_lock_irqsave(&phba->hbalock, iflag);
15570 		if (phba->link_state < LPFC_LINK_DOWN)
15571 			/* Flush, clear interrupt, and rearm the EQ */
15572 			lpfc_sli4_eqcq_flush(phba, fpeq);
15573 		spin_unlock_irqrestore(&phba->hbalock, iflag);
15574 		return IRQ_NONE;
15575 	}
15576 
15577 	switch (fpeq->poll_mode) {
15578 	case LPFC_THREADED_IRQ:
15579 		/* CGN mgmt is mutually exclusive from irq processing */
15580 		if (phba->cmf_active_mode == LPFC_CFG_OFF)
15581 			return IRQ_WAKE_THREAD;
15582 		fallthrough;
15583 	case LPFC_QUEUE_WORK:
15584 	default:
15585 		eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15586 		eqi->icnt++;
15587 
15588 		fpeq->last_cpu = raw_smp_processor_id();
15589 
15590 		if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
15591 		    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
15592 		    phba->cfg_auto_imax &&
15593 		    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15594 		    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15595 			lpfc_sli4_mod_hba_eq_delay(phba, fpeq,
15596 						   LPFC_MAX_AUTO_EQ_DELAY);
15597 
15598 		/* process and rearm the EQ */
15599 		ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
15600 					      LPFC_QUEUE_WORK);
15601 
15602 		if (unlikely(ecount == 0)) {
15603 			fpeq->EQ_no_entry++;
15604 			if (phba->intr_type == MSIX)
15605 				/* MSI-X treated interrupt served as no EQ share INT */
15606 				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15607 						"0358 MSI-X interrupt with no EQE\n");
15608 			else
15609 				/* Non MSI-X treated on interrupt as EQ share INT */
15610 				return IRQ_NONE;
15611 		}
15612 	}
15613 
15614 	return IRQ_HANDLED;
15615 } /* lpfc_sli4_hba_intr_handler */
15616 
15617 /**
15618  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15619  * @irq: Interrupt number.
15620  * @dev_id: The device context pointer.
15621  *
15622  * This function is the device-level interrupt handler to device with SLI-4
15623  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15624  * interrupt mode is enabled and there is an event in the HBA which requires
15625  * driver attention. This function invokes the slow-path interrupt attention
15626  * handling function and fast-path interrupt attention handling function in
15627  * turn to process the relevant HBA attention events. This function is called
15628  * without any lock held. It gets the hbalock to access and update SLI data
15629  * structures.
15630  *
15631  * This function returns IRQ_HANDLED when interrupt is handled, else it
15632  * returns IRQ_NONE.
15633  **/
15634 irqreturn_t
lpfc_sli4_intr_handler(int irq,void * dev_id)15635 lpfc_sli4_intr_handler(int irq, void *dev_id)
15636 {
15637 	struct lpfc_hba  *phba;
15638 	irqreturn_t hba_irq_rc;
15639 	bool hba_handled = false;
15640 	int qidx;
15641 
15642 	/* Get the driver's phba structure from the dev_id */
15643 	phba = (struct lpfc_hba *)dev_id;
15644 
15645 	if (unlikely(!phba))
15646 		return IRQ_NONE;
15647 
15648 	/*
15649 	 * Invoke fast-path host attention interrupt handling as appropriate.
15650 	 */
15651 	for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
15652 		hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
15653 					&phba->sli4_hba.hba_eq_hdl[qidx]);
15654 		if (hba_irq_rc == IRQ_HANDLED)
15655 			hba_handled |= true;
15656 	}
15657 
15658 	return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
15659 } /* lpfc_sli4_intr_handler */
15660 
lpfc_sli4_poll_hbtimer(struct timer_list * t)15661 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15662 {
15663 	struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
15664 	struct lpfc_queue *eq;
15665 
15666 	rcu_read_lock();
15667 
15668 	list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15669 		lpfc_sli4_poll_eq(eq);
15670 	if (!list_empty(&phba->poll_list))
15671 		mod_timer(&phba->cpuhp_poll_timer,
15672 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15673 
15674 	rcu_read_unlock();
15675 }
15676 
lpfc_sli4_add_to_poll_list(struct lpfc_queue * eq)15677 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15678 {
15679 	struct lpfc_hba *phba = eq->phba;
15680 
15681 	/* kickstart slowpath processing if needed */
15682 	if (list_empty(&phba->poll_list))
15683 		mod_timer(&phba->cpuhp_poll_timer,
15684 			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15685 
15686 	list_add_rcu(&eq->_poll_list, &phba->poll_list);
15687 	synchronize_rcu();
15688 }
15689 
lpfc_sli4_remove_from_poll_list(struct lpfc_queue * eq)15690 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15691 {
15692 	struct lpfc_hba *phba = eq->phba;
15693 
15694 	/* Disable slowpath processing for this eq.  Kick start the eq
15695 	 * by RE-ARMING the eq's ASAP
15696 	 */
15697 	list_del_rcu(&eq->_poll_list);
15698 	synchronize_rcu();
15699 
15700 	if (list_empty(&phba->poll_list))
15701 		del_timer_sync(&phba->cpuhp_poll_timer);
15702 }
15703 
lpfc_sli4_cleanup_poll_list(struct lpfc_hba * phba)15704 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
15705 {
15706 	struct lpfc_queue *eq, *next;
15707 
15708 	list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15709 		list_del(&eq->_poll_list);
15710 
15711 	INIT_LIST_HEAD(&phba->poll_list);
15712 	synchronize_rcu();
15713 }
15714 
15715 static inline void
__lpfc_sli4_switch_eqmode(struct lpfc_queue * eq,uint8_t mode)15716 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15717 {
15718 	if (mode == eq->mode)
15719 		return;
15720 	/*
15721 	 * currently this function is only called during a hotplug
15722 	 * event and the cpu on which this function is executing
15723 	 * is going offline.  By now the hotplug has instructed
15724 	 * the scheduler to remove this cpu from cpu active mask.
15725 	 * So we don't need to work about being put aside by the
15726 	 * scheduler for a high priority process.  Yes, the inte-
15727 	 * rrupts could come but they are known to retire ASAP.
15728 	 */
15729 
15730 	/* Disable polling in the fastpath */
15731 	WRITE_ONCE(eq->mode, mode);
15732 	/* flush out the store buffer */
15733 	smp_wmb();
15734 
15735 	/*
15736 	 * Add this eq to the polling list and start polling. For
15737 	 * a grace period both interrupt handler and poller will
15738 	 * try to process the eq _but_ that's fine.  We have a
15739 	 * synchronization mechanism in place (queue_claimed) to
15740 	 * deal with it.  This is just a draining phase for int-
15741 	 * errupt handler (not eq's) as we have guranteed through
15742 	 * barrier that all the CPUs have seen the new CQ_POLLED
15743 	 * state. which will effectively disable the REARMING of
15744 	 * the EQ.  The whole idea is eq's die off eventually as
15745 	 * we are not rearming EQ's anymore.
15746 	 */
15747 	mode ? lpfc_sli4_add_to_poll_list(eq) :
15748 	       lpfc_sli4_remove_from_poll_list(eq);
15749 }
15750 
lpfc_sli4_start_polling(struct lpfc_queue * eq)15751 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15752 {
15753 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15754 }
15755 
lpfc_sli4_stop_polling(struct lpfc_queue * eq)15756 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15757 {
15758 	struct lpfc_hba *phba = eq->phba;
15759 
15760 	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15761 
15762 	/* Kick start for the pending io's in h/w.
15763 	 * Once we switch back to interrupt processing on a eq
15764 	 * the io path completion will only arm eq's when it
15765 	 * receives a completion.  But since eq's are in disa-
15766 	 * rmed state it doesn't receive a completion.  This
15767 	 * creates a deadlock scenaro.
15768 	 */
15769 	phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15770 }
15771 
15772 /**
15773  * lpfc_sli4_queue_free - free a queue structure and associated memory
15774  * @queue: The queue structure to free.
15775  *
15776  * This function frees a queue structure and the DMAable memory used for
15777  * the host resident queue. This function must be called after destroying the
15778  * queue on the HBA.
15779  **/
15780 void
lpfc_sli4_queue_free(struct lpfc_queue * queue)15781 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15782 {
15783 	struct lpfc_dmabuf *dmabuf;
15784 
15785 	if (!queue)
15786 		return;
15787 
15788 	if (!list_empty(&queue->wq_list))
15789 		list_del(&queue->wq_list);
15790 
15791 	while (!list_empty(&queue->page_list)) {
15792 		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15793 				 list);
15794 		dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15795 				  dmabuf->virt, dmabuf->phys);
15796 		kfree(dmabuf);
15797 	}
15798 	if (queue->rqbp) {
15799 		lpfc_free_rq_buffer(queue->phba, queue);
15800 		kfree(queue->rqbp);
15801 	}
15802 
15803 	if (!list_empty(&queue->cpu_list))
15804 		list_del(&queue->cpu_list);
15805 
15806 	kfree(queue);
15807 	return;
15808 }
15809 
15810 /**
15811  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15812  * @phba: The HBA that this queue is being created on.
15813  * @page_size: The size of a queue page
15814  * @entry_size: The size of each queue entry for this queue.
15815  * @entry_count: The number of entries that this queue will handle.
15816  * @cpu: The cpu that will primarily utilize this queue.
15817  *
15818  * This function allocates a queue structure and the DMAable memory used for
15819  * the host resident queue. This function must be called before creating the
15820  * queue on the HBA.
15821  **/
15822 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)15823 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15824 		      uint32_t entry_size, uint32_t entry_count, int cpu)
15825 {
15826 	struct lpfc_queue *queue;
15827 	struct lpfc_dmabuf *dmabuf;
15828 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15829 	uint16_t x, pgcnt;
15830 
15831 	if (!phba->sli4_hba.pc_sli4_params.supported)
15832 		hw_page_size = page_size;
15833 
15834 	pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15835 
15836 	/* If needed, Adjust page count to match the max the adapter supports */
15837 	if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15838 		pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15839 
15840 	queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15841 			     GFP_KERNEL, cpu_to_node(cpu));
15842 	if (!queue)
15843 		return NULL;
15844 
15845 	INIT_LIST_HEAD(&queue->list);
15846 	INIT_LIST_HEAD(&queue->_poll_list);
15847 	INIT_LIST_HEAD(&queue->wq_list);
15848 	INIT_LIST_HEAD(&queue->wqfull_list);
15849 	INIT_LIST_HEAD(&queue->page_list);
15850 	INIT_LIST_HEAD(&queue->child_list);
15851 	INIT_LIST_HEAD(&queue->cpu_list);
15852 
15853 	/* Set queue parameters now.  If the system cannot provide memory
15854 	 * resources, the free routine needs to know what was allocated.
15855 	 */
15856 	queue->page_count = pgcnt;
15857 	queue->q_pgs = (void **)&queue[1];
15858 	queue->entry_cnt_per_pg = hw_page_size / entry_size;
15859 	queue->entry_size = entry_size;
15860 	queue->entry_count = entry_count;
15861 	queue->page_size = hw_page_size;
15862 	queue->phba = phba;
15863 
15864 	for (x = 0; x < queue->page_count; x++) {
15865 		dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15866 				      dev_to_node(&phba->pcidev->dev));
15867 		if (!dmabuf)
15868 			goto out_fail;
15869 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15870 						  hw_page_size, &dmabuf->phys,
15871 						  GFP_KERNEL);
15872 		if (!dmabuf->virt) {
15873 			kfree(dmabuf);
15874 			goto out_fail;
15875 		}
15876 		dmabuf->buffer_tag = x;
15877 		list_add_tail(&dmabuf->list, &queue->page_list);
15878 		/* use lpfc_sli4_qe to index a paritcular entry in this page */
15879 		queue->q_pgs[x] = dmabuf->virt;
15880 	}
15881 	INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15882 	INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15883 	INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15884 	INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15885 
15886 	/* notify_interval will be set during q creation */
15887 
15888 	return queue;
15889 out_fail:
15890 	lpfc_sli4_queue_free(queue);
15891 	return NULL;
15892 }
15893 
15894 /**
15895  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15896  * @phba: HBA structure that indicates port to create a queue on.
15897  * @pci_barset: PCI BAR set flag.
15898  *
15899  * This function shall perform iomap of the specified PCI BAR address to host
15900  * memory address if not already done so and return it. The returned host
15901  * memory address can be NULL.
15902  */
15903 static void __iomem *
lpfc_dual_chute_pci_bar_map(struct lpfc_hba * phba,uint16_t pci_barset)15904 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15905 {
15906 	if (!phba->pcidev)
15907 		return NULL;
15908 
15909 	switch (pci_barset) {
15910 	case WQ_PCI_BAR_0_AND_1:
15911 		return phba->pci_bar0_memmap_p;
15912 	case WQ_PCI_BAR_2_AND_3:
15913 		return phba->pci_bar2_memmap_p;
15914 	case WQ_PCI_BAR_4_AND_5:
15915 		return phba->pci_bar4_memmap_p;
15916 	default:
15917 		break;
15918 	}
15919 	return NULL;
15920 }
15921 
15922 /**
15923  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15924  * @phba: HBA structure that EQs are on.
15925  * @startq: The starting EQ index to modify
15926  * @numq: The number of EQs (consecutive indexes) to modify
15927  * @usdelay: amount of delay
15928  *
15929  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15930  * is set either by writing to a register (if supported by the SLI Port)
15931  * or by mailbox command. The mailbox command allows several EQs to be
15932  * updated at once.
15933  *
15934  * The @phba struct is used to send a mailbox command to HBA. The @startq
15935  * is used to get the starting EQ index to change. The @numq value is
15936  * used to specify how many consecutive EQ indexes, starting at EQ index,
15937  * are to be changed. This function is asynchronous and will wait for any
15938  * mailbox commands to finish before returning.
15939  *
15940  * On success this function will return a zero. If unable to allocate
15941  * enough memory this function will return -ENOMEM. If a mailbox command
15942  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15943  * have had their delay multipler changed.
15944  **/
15945 void
lpfc_modify_hba_eq_delay(struct lpfc_hba * phba,uint32_t startq,uint32_t numq,uint32_t usdelay)15946 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
15947 			 uint32_t numq, uint32_t usdelay)
15948 {
15949 	struct lpfc_mbx_modify_eq_delay *eq_delay;
15950 	LPFC_MBOXQ_t *mbox;
15951 	struct lpfc_queue *eq;
15952 	int cnt = 0, rc, length;
15953 	uint32_t shdr_status, shdr_add_status;
15954 	uint32_t dmult;
15955 	int qidx;
15956 	union lpfc_sli4_cfg_shdr *shdr;
15957 
15958 	if (startq >= phba->cfg_irq_chann)
15959 		return;
15960 
15961 	if (usdelay > 0xFFFF) {
15962 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15963 				"6429 usdelay %d too large. Scaled down to "
15964 				"0xFFFF.\n", usdelay);
15965 		usdelay = 0xFFFF;
15966 	}
15967 
15968 	/* set values by EQ_DELAY register if supported */
15969 	if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15970 		for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15971 			eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15972 			if (!eq)
15973 				continue;
15974 
15975 			lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
15976 
15977 			if (++cnt >= numq)
15978 				break;
15979 		}
15980 		return;
15981 	}
15982 
15983 	/* Otherwise, set values by mailbox cmd */
15984 
15985 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15986 	if (!mbox) {
15987 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15988 				"6428 Failed allocating mailbox cmd buffer."
15989 				" EQ delay was not set.\n");
15990 		return;
15991 	}
15992 	length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15993 		  sizeof(struct lpfc_sli4_cfg_mhdr));
15994 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15995 			 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15996 			 length, LPFC_SLI4_MBX_EMBED);
15997 	eq_delay = &mbox->u.mqe.un.eq_delay;
15998 
15999 	/* Calculate delay multiper from maximum interrupt per second */
16000 	dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
16001 	if (dmult)
16002 		dmult--;
16003 	if (dmult > LPFC_DMULT_MAX)
16004 		dmult = LPFC_DMULT_MAX;
16005 
16006 	for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16007 		eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16008 		if (!eq)
16009 			continue;
16010 		eq->q_mode = usdelay;
16011 		eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16012 		eq_delay->u.request.eq[cnt].phase = 0;
16013 		eq_delay->u.request.eq[cnt].delay_multi = dmult;
16014 
16015 		if (++cnt >= numq)
16016 			break;
16017 	}
16018 	eq_delay->u.request.num_eq = cnt;
16019 
16020 	mbox->vport = phba->pport;
16021 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16022 	mbox->ctx_ndlp = NULL;
16023 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16024 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16025 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16026 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16027 	if (shdr_status || shdr_add_status || rc) {
16028 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16029 				"2512 MODIFY_EQ_DELAY mailbox failed with "
16030 				"status x%x add_status x%x, mbx status x%x\n",
16031 				shdr_status, shdr_add_status, rc);
16032 	}
16033 	mempool_free(mbox, phba->mbox_mem_pool);
16034 	return;
16035 }
16036 
16037 /**
16038  * lpfc_eq_create - Create an Event Queue on the HBA
16039  * @phba: HBA structure that indicates port to create a queue on.
16040  * @eq: The queue structure to use to create the event queue.
16041  * @imax: The maximum interrupt per second limit.
16042  *
16043  * This function creates an event queue, as detailed in @eq, on a port,
16044  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16045  *
16046  * The @phba struct is used to send mailbox command to HBA. The @eq struct
16047  * is used to get the entry count and entry size that are necessary to
16048  * determine the number of pages to allocate and use for this queue. This
16049  * function will send the EQ_CREATE mailbox command to the HBA to setup the
16050  * event queue. This function is asynchronous and will wait for the mailbox
16051  * command to finish before continuing.
16052  *
16053  * On success this function will return a zero. If unable to allocate enough
16054  * memory this function will return -ENOMEM. If the queue create mailbox command
16055  * fails this function will return -ENXIO.
16056  **/
16057 int
lpfc_eq_create(struct lpfc_hba * phba,struct lpfc_queue * eq,uint32_t imax)16058 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
16059 {
16060 	struct lpfc_mbx_eq_create *eq_create;
16061 	LPFC_MBOXQ_t *mbox;
16062 	int rc, length, status = 0;
16063 	struct lpfc_dmabuf *dmabuf;
16064 	uint32_t shdr_status, shdr_add_status;
16065 	union lpfc_sli4_cfg_shdr *shdr;
16066 	uint16_t dmult;
16067 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16068 
16069 	/* sanity check on queue memory */
16070 	if (!eq)
16071 		return -ENODEV;
16072 	if (!phba->sli4_hba.pc_sli4_params.supported)
16073 		hw_page_size = SLI4_PAGE_SIZE;
16074 
16075 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16076 	if (!mbox)
16077 		return -ENOMEM;
16078 	length = (sizeof(struct lpfc_mbx_eq_create) -
16079 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16080 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16081 			 LPFC_MBOX_OPCODE_EQ_CREATE,
16082 			 length, LPFC_SLI4_MBX_EMBED);
16083 	eq_create = &mbox->u.mqe.un.eq_create;
16084 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
16085 	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16086 	       eq->page_count);
16087 	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16088 	       LPFC_EQE_SIZE);
16089 	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
16090 
16091 	/* Use version 2 of CREATE_EQ if eqav is set */
16092 	if (phba->sli4_hba.pc_sli4_params.eqav) {
16093 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16094 		       LPFC_Q_CREATE_VERSION_2);
16095 		bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16096 		       phba->sli4_hba.pc_sli4_params.eqav);
16097 	}
16098 
16099 	/* don't setup delay multiplier using EQ_CREATE */
16100 	dmult = 0;
16101 	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16102 	       dmult);
16103 	switch (eq->entry_count) {
16104 	default:
16105 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16106 				"0360 Unsupported EQ count. (%d)\n",
16107 				eq->entry_count);
16108 		if (eq->entry_count < 256) {
16109 			status = -EINVAL;
16110 			goto out;
16111 		}
16112 		fallthrough;	/* otherwise default to smallest count */
16113 	case 256:
16114 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16115 		       LPFC_EQ_CNT_256);
16116 		break;
16117 	case 512:
16118 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16119 		       LPFC_EQ_CNT_512);
16120 		break;
16121 	case 1024:
16122 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16123 		       LPFC_EQ_CNT_1024);
16124 		break;
16125 	case 2048:
16126 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16127 		       LPFC_EQ_CNT_2048);
16128 		break;
16129 	case 4096:
16130 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16131 		       LPFC_EQ_CNT_4096);
16132 		break;
16133 	}
16134 	list_for_each_entry(dmabuf, &eq->page_list, list) {
16135 		memset(dmabuf->virt, 0, hw_page_size);
16136 		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16137 					putPaddrLow(dmabuf->phys);
16138 		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16139 					putPaddrHigh(dmabuf->phys);
16140 	}
16141 	mbox->vport = phba->pport;
16142 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16143 	mbox->ctx_buf = NULL;
16144 	mbox->ctx_ndlp = NULL;
16145 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16146 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16147 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16148 	if (shdr_status || shdr_add_status || rc) {
16149 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16150 				"2500 EQ_CREATE mailbox failed with "
16151 				"status x%x add_status x%x, mbx status x%x\n",
16152 				shdr_status, shdr_add_status, rc);
16153 		status = -ENXIO;
16154 	}
16155 	eq->type = LPFC_EQ;
16156 	eq->subtype = LPFC_NONE;
16157 	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16158 	if (eq->queue_id == 0xFFFF)
16159 		status = -ENXIO;
16160 	eq->host_index = 0;
16161 	eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16162 	eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
16163 out:
16164 	mempool_free(mbox, phba->mbox_mem_pool);
16165 	return status;
16166 }
16167 
16168 /**
16169  * lpfc_sli4_hba_intr_handler_th - SLI4 HBA threaded interrupt handler
16170  * @irq: Interrupt number.
16171  * @dev_id: The device context pointer.
16172  *
16173  * This routine is a mirror of lpfc_sli4_hba_intr_handler, but executed within
16174  * threaded irq context.
16175  *
16176  * Returns
16177  * IRQ_HANDLED - interrupt is handled
16178  * IRQ_NONE - otherwise
16179  **/
lpfc_sli4_hba_intr_handler_th(int irq,void * dev_id)16180 irqreturn_t lpfc_sli4_hba_intr_handler_th(int irq, void *dev_id)
16181 {
16182 	struct lpfc_hba *phba;
16183 	struct lpfc_hba_eq_hdl *hba_eq_hdl;
16184 	struct lpfc_queue *fpeq;
16185 	int ecount = 0;
16186 	int hba_eqidx;
16187 	struct lpfc_eq_intr_info *eqi;
16188 
16189 	/* Get the driver's phba structure from the dev_id */
16190 	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
16191 	phba = hba_eq_hdl->phba;
16192 	hba_eqidx = hba_eq_hdl->idx;
16193 
16194 	if (unlikely(!phba))
16195 		return IRQ_NONE;
16196 	if (unlikely(!phba->sli4_hba.hdwq))
16197 		return IRQ_NONE;
16198 
16199 	/* Get to the EQ struct associated with this vector */
16200 	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
16201 	if (unlikely(!fpeq))
16202 		return IRQ_NONE;
16203 
16204 	eqi = per_cpu_ptr(phba->sli4_hba.eq_info, raw_smp_processor_id());
16205 	eqi->icnt++;
16206 
16207 	fpeq->last_cpu = raw_smp_processor_id();
16208 
16209 	if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
16210 	    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
16211 	    phba->cfg_auto_imax &&
16212 	    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
16213 	    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
16214 		lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
16215 
16216 	/* process and rearm the EQ */
16217 	ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
16218 				      LPFC_THREADED_IRQ);
16219 
16220 	if (unlikely(ecount == 0)) {
16221 		fpeq->EQ_no_entry++;
16222 		if (phba->intr_type == MSIX)
16223 			/* MSI-X treated interrupt served as no EQ share INT */
16224 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16225 					"3358 MSI-X interrupt with no EQE\n");
16226 		else
16227 			/* Non MSI-X treated on interrupt as EQ share INT */
16228 			return IRQ_NONE;
16229 	}
16230 	return IRQ_HANDLED;
16231 }
16232 
16233 /**
16234  * lpfc_cq_create - Create a Completion Queue on the HBA
16235  * @phba: HBA structure that indicates port to create a queue on.
16236  * @cq: The queue structure to use to create the completion queue.
16237  * @eq: The event queue to bind this completion queue to.
16238  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16239  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16240  *
16241  * This function creates a completion queue, as detailed in @wq, on a port,
16242  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16243  *
16244  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16245  * is used to get the entry count and entry size that are necessary to
16246  * determine the number of pages to allocate and use for this queue. The @eq
16247  * is used to indicate which event queue to bind this completion queue to. This
16248  * function will send the CQ_CREATE mailbox command to the HBA to setup the
16249  * completion queue. This function is asynchronous and will wait for the mailbox
16250  * command to finish before continuing.
16251  *
16252  * On success this function will return a zero. If unable to allocate enough
16253  * memory this function will return -ENOMEM. If the queue create mailbox command
16254  * fails this function will return -ENXIO.
16255  **/
16256 int
lpfc_cq_create(struct lpfc_hba * phba,struct lpfc_queue * cq,struct lpfc_queue * eq,uint32_t type,uint32_t subtype)16257 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16258 	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16259 {
16260 	struct lpfc_mbx_cq_create *cq_create;
16261 	struct lpfc_dmabuf *dmabuf;
16262 	LPFC_MBOXQ_t *mbox;
16263 	int rc, length, status = 0;
16264 	uint32_t shdr_status, shdr_add_status;
16265 	union lpfc_sli4_cfg_shdr *shdr;
16266 
16267 	/* sanity check on queue memory */
16268 	if (!cq || !eq)
16269 		return -ENODEV;
16270 
16271 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16272 	if (!mbox)
16273 		return -ENOMEM;
16274 	length = (sizeof(struct lpfc_mbx_cq_create) -
16275 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16276 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16277 			 LPFC_MBOX_OPCODE_CQ_CREATE,
16278 			 length, LPFC_SLI4_MBX_EMBED);
16279 	cq_create = &mbox->u.mqe.un.cq_create;
16280 	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
16281 	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16282 		    cq->page_count);
16283 	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16284 	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
16285 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16286 	       phba->sli4_hba.pc_sli4_params.cqv);
16287 	if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
16288 		bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16289 		       (cq->page_size / SLI4_PAGE_SIZE));
16290 		bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16291 		       eq->queue_id);
16292 		bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16293 		       phba->sli4_hba.pc_sli4_params.cqav);
16294 	} else {
16295 		bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16296 		       eq->queue_id);
16297 	}
16298 	switch (cq->entry_count) {
16299 	case 2048:
16300 	case 4096:
16301 		if (phba->sli4_hba.pc_sli4_params.cqv ==
16302 		    LPFC_Q_CREATE_VERSION_2) {
16303 			cq_create->u.request.context.lpfc_cq_context_count =
16304 				cq->entry_count;
16305 			bf_set(lpfc_cq_context_count,
16306 			       &cq_create->u.request.context,
16307 			       LPFC_CQ_CNT_WORD7);
16308 			break;
16309 		}
16310 		fallthrough;
16311 	default:
16312 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16313 				"0361 Unsupported CQ count: "
16314 				"entry cnt %d sz %d pg cnt %d\n",
16315 				cq->entry_count, cq->entry_size,
16316 				cq->page_count);
16317 		if (cq->entry_count < 256) {
16318 			status = -EINVAL;
16319 			goto out;
16320 		}
16321 		fallthrough;	/* otherwise default to smallest count */
16322 	case 256:
16323 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16324 		       LPFC_CQ_CNT_256);
16325 		break;
16326 	case 512:
16327 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16328 		       LPFC_CQ_CNT_512);
16329 		break;
16330 	case 1024:
16331 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16332 		       LPFC_CQ_CNT_1024);
16333 		break;
16334 	}
16335 	list_for_each_entry(dmabuf, &cq->page_list, list) {
16336 		memset(dmabuf->virt, 0, cq->page_size);
16337 		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16338 					putPaddrLow(dmabuf->phys);
16339 		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16340 					putPaddrHigh(dmabuf->phys);
16341 	}
16342 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16343 
16344 	/* The IOCTL status is embedded in the mailbox subheader. */
16345 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16346 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16347 	if (shdr_status || shdr_add_status || rc) {
16348 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16349 				"2501 CQ_CREATE mailbox failed with "
16350 				"status x%x add_status x%x, mbx status x%x\n",
16351 				shdr_status, shdr_add_status, rc);
16352 		status = -ENXIO;
16353 		goto out;
16354 	}
16355 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16356 	if (cq->queue_id == 0xFFFF) {
16357 		status = -ENXIO;
16358 		goto out;
16359 	}
16360 	/* link the cq onto the parent eq child list */
16361 	list_add_tail(&cq->list, &eq->child_list);
16362 	/* Set up completion queue's type and subtype */
16363 	cq->type = type;
16364 	cq->subtype = subtype;
16365 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16366 	cq->assoc_qid = eq->queue_id;
16367 	cq->assoc_qp = eq;
16368 	cq->host_index = 0;
16369 	cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16370 	cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
16371 
16372 	if (cq->queue_id > phba->sli4_hba.cq_max)
16373 		phba->sli4_hba.cq_max = cq->queue_id;
16374 out:
16375 	mempool_free(mbox, phba->mbox_mem_pool);
16376 	return status;
16377 }
16378 
16379 /**
16380  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16381  * @phba: HBA structure that indicates port to create a queue on.
16382  * @cqp: The queue structure array to use to create the completion queues.
16383  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
16384  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16385  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16386  *
16387  * This function creates a set of  completion queue, s to support MRQ
16388  * as detailed in @cqp, on a port,
16389  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16390  *
16391  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16392  * is used to get the entry count and entry size that are necessary to
16393  * determine the number of pages to allocate and use for this queue. The @eq
16394  * is used to indicate which event queue to bind this completion queue to. This
16395  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16396  * completion queue. This function is asynchronous and will wait for the mailbox
16397  * command to finish before continuing.
16398  *
16399  * On success this function will return a zero. If unable to allocate enough
16400  * memory this function will return -ENOMEM. If the queue create mailbox command
16401  * fails this function will return -ENXIO.
16402  **/
16403 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)16404 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
16405 		   struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16406 		   uint32_t subtype)
16407 {
16408 	struct lpfc_queue *cq;
16409 	struct lpfc_queue *eq;
16410 	struct lpfc_mbx_cq_create_set *cq_set;
16411 	struct lpfc_dmabuf *dmabuf;
16412 	LPFC_MBOXQ_t *mbox;
16413 	int rc, length, alloclen, status = 0;
16414 	int cnt, idx, numcq, page_idx = 0;
16415 	uint32_t shdr_status, shdr_add_status;
16416 	union lpfc_sli4_cfg_shdr *shdr;
16417 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16418 
16419 	/* sanity check on queue memory */
16420 	numcq = phba->cfg_nvmet_mrq;
16421 	if (!cqp || !hdwq || !numcq)
16422 		return -ENODEV;
16423 
16424 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16425 	if (!mbox)
16426 		return -ENOMEM;
16427 
16428 	length = sizeof(struct lpfc_mbx_cq_create_set);
16429 	length += ((numcq * cqp[0]->page_count) *
16430 		   sizeof(struct dma_address));
16431 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16432 			LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16433 			LPFC_SLI4_MBX_NEMBED);
16434 	if (alloclen < length) {
16435 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16436 				"3098 Allocated DMA memory size (%d) is "
16437 				"less than the requested DMA memory size "
16438 				"(%d)\n", alloclen, length);
16439 		status = -ENOMEM;
16440 		goto out;
16441 	}
16442 	cq_set = mbox->sge_array->addr[0];
16443 	shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16444 	bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16445 
16446 	for (idx = 0; idx < numcq; idx++) {
16447 		cq = cqp[idx];
16448 		eq = hdwq[idx].hba_eq;
16449 		if (!cq || !eq) {
16450 			status = -ENOMEM;
16451 			goto out;
16452 		}
16453 		if (!phba->sli4_hba.pc_sli4_params.supported)
16454 			hw_page_size = cq->page_size;
16455 
16456 		switch (idx) {
16457 		case 0:
16458 			bf_set(lpfc_mbx_cq_create_set_page_size,
16459 			       &cq_set->u.request,
16460 			       (hw_page_size / SLI4_PAGE_SIZE));
16461 			bf_set(lpfc_mbx_cq_create_set_num_pages,
16462 			       &cq_set->u.request, cq->page_count);
16463 			bf_set(lpfc_mbx_cq_create_set_evt,
16464 			       &cq_set->u.request, 1);
16465 			bf_set(lpfc_mbx_cq_create_set_valid,
16466 			       &cq_set->u.request, 1);
16467 			bf_set(lpfc_mbx_cq_create_set_cqe_size,
16468 			       &cq_set->u.request, 0);
16469 			bf_set(lpfc_mbx_cq_create_set_num_cq,
16470 			       &cq_set->u.request, numcq);
16471 			bf_set(lpfc_mbx_cq_create_set_autovalid,
16472 			       &cq_set->u.request,
16473 			       phba->sli4_hba.pc_sli4_params.cqav);
16474 			switch (cq->entry_count) {
16475 			case 2048:
16476 			case 4096:
16477 				if (phba->sli4_hba.pc_sli4_params.cqv ==
16478 				    LPFC_Q_CREATE_VERSION_2) {
16479 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16480 					       &cq_set->u.request,
16481 						cq->entry_count);
16482 					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16483 					       &cq_set->u.request,
16484 					       LPFC_CQ_CNT_WORD7);
16485 					break;
16486 				}
16487 				fallthrough;
16488 			default:
16489 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16490 						"3118 Bad CQ count. (%d)\n",
16491 						cq->entry_count);
16492 				if (cq->entry_count < 256) {
16493 					status = -EINVAL;
16494 					goto out;
16495 				}
16496 				fallthrough;	/* otherwise default to smallest */
16497 			case 256:
16498 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16499 				       &cq_set->u.request, LPFC_CQ_CNT_256);
16500 				break;
16501 			case 512:
16502 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16503 				       &cq_set->u.request, LPFC_CQ_CNT_512);
16504 				break;
16505 			case 1024:
16506 				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16507 				       &cq_set->u.request, LPFC_CQ_CNT_1024);
16508 				break;
16509 			}
16510 			bf_set(lpfc_mbx_cq_create_set_eq_id0,
16511 			       &cq_set->u.request, eq->queue_id);
16512 			break;
16513 		case 1:
16514 			bf_set(lpfc_mbx_cq_create_set_eq_id1,
16515 			       &cq_set->u.request, eq->queue_id);
16516 			break;
16517 		case 2:
16518 			bf_set(lpfc_mbx_cq_create_set_eq_id2,
16519 			       &cq_set->u.request, eq->queue_id);
16520 			break;
16521 		case 3:
16522 			bf_set(lpfc_mbx_cq_create_set_eq_id3,
16523 			       &cq_set->u.request, eq->queue_id);
16524 			break;
16525 		case 4:
16526 			bf_set(lpfc_mbx_cq_create_set_eq_id4,
16527 			       &cq_set->u.request, eq->queue_id);
16528 			break;
16529 		case 5:
16530 			bf_set(lpfc_mbx_cq_create_set_eq_id5,
16531 			       &cq_set->u.request, eq->queue_id);
16532 			break;
16533 		case 6:
16534 			bf_set(lpfc_mbx_cq_create_set_eq_id6,
16535 			       &cq_set->u.request, eq->queue_id);
16536 			break;
16537 		case 7:
16538 			bf_set(lpfc_mbx_cq_create_set_eq_id7,
16539 			       &cq_set->u.request, eq->queue_id);
16540 			break;
16541 		case 8:
16542 			bf_set(lpfc_mbx_cq_create_set_eq_id8,
16543 			       &cq_set->u.request, eq->queue_id);
16544 			break;
16545 		case 9:
16546 			bf_set(lpfc_mbx_cq_create_set_eq_id9,
16547 			       &cq_set->u.request, eq->queue_id);
16548 			break;
16549 		case 10:
16550 			bf_set(lpfc_mbx_cq_create_set_eq_id10,
16551 			       &cq_set->u.request, eq->queue_id);
16552 			break;
16553 		case 11:
16554 			bf_set(lpfc_mbx_cq_create_set_eq_id11,
16555 			       &cq_set->u.request, eq->queue_id);
16556 			break;
16557 		case 12:
16558 			bf_set(lpfc_mbx_cq_create_set_eq_id12,
16559 			       &cq_set->u.request, eq->queue_id);
16560 			break;
16561 		case 13:
16562 			bf_set(lpfc_mbx_cq_create_set_eq_id13,
16563 			       &cq_set->u.request, eq->queue_id);
16564 			break;
16565 		case 14:
16566 			bf_set(lpfc_mbx_cq_create_set_eq_id14,
16567 			       &cq_set->u.request, eq->queue_id);
16568 			break;
16569 		case 15:
16570 			bf_set(lpfc_mbx_cq_create_set_eq_id15,
16571 			       &cq_set->u.request, eq->queue_id);
16572 			break;
16573 		}
16574 
16575 		/* link the cq onto the parent eq child list */
16576 		list_add_tail(&cq->list, &eq->child_list);
16577 		/* Set up completion queue's type and subtype */
16578 		cq->type = type;
16579 		cq->subtype = subtype;
16580 		cq->assoc_qid = eq->queue_id;
16581 		cq->assoc_qp = eq;
16582 		cq->host_index = 0;
16583 		cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16584 		cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16585 					 cq->entry_count);
16586 		cq->chann = idx;
16587 
16588 		rc = 0;
16589 		list_for_each_entry(dmabuf, &cq->page_list, list) {
16590 			memset(dmabuf->virt, 0, hw_page_size);
16591 			cnt = page_idx + dmabuf->buffer_tag;
16592 			cq_set->u.request.page[cnt].addr_lo =
16593 					putPaddrLow(dmabuf->phys);
16594 			cq_set->u.request.page[cnt].addr_hi =
16595 					putPaddrHigh(dmabuf->phys);
16596 			rc++;
16597 		}
16598 		page_idx += rc;
16599 	}
16600 
16601 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16602 
16603 	/* The IOCTL status is embedded in the mailbox subheader. */
16604 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16605 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16606 	if (shdr_status || shdr_add_status || rc) {
16607 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16608 				"3119 CQ_CREATE_SET mailbox failed with "
16609 				"status x%x add_status x%x, mbx status x%x\n",
16610 				shdr_status, shdr_add_status, rc);
16611 		status = -ENXIO;
16612 		goto out;
16613 	}
16614 	rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16615 	if (rc == 0xFFFF) {
16616 		status = -ENXIO;
16617 		goto out;
16618 	}
16619 
16620 	for (idx = 0; idx < numcq; idx++) {
16621 		cq = cqp[idx];
16622 		cq->queue_id = rc + idx;
16623 		if (cq->queue_id > phba->sli4_hba.cq_max)
16624 			phba->sli4_hba.cq_max = cq->queue_id;
16625 	}
16626 
16627 out:
16628 	lpfc_sli4_mbox_cmd_free(phba, mbox);
16629 	return status;
16630 }
16631 
16632 /**
16633  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
16634  * @phba: HBA structure that indicates port to create a queue on.
16635  * @mq: The queue structure to use to create the mailbox queue.
16636  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16637  * @cq: The completion queue to associate with this cq.
16638  *
16639  * This function provides failback (fb) functionality when the
16640  * mq_create_ext fails on older FW generations.  It's purpose is identical
16641  * to mq_create_ext otherwise.
16642  *
16643  * This routine cannot fail as all attributes were previously accessed and
16644  * initialized in mq_create_ext.
16645  **/
16646 static void
lpfc_mq_create_fb_init(struct lpfc_hba * phba,struct lpfc_queue * mq,LPFC_MBOXQ_t * mbox,struct lpfc_queue * cq)16647 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16648 		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
16649 {
16650 	struct lpfc_mbx_mq_create *mq_create;
16651 	struct lpfc_dmabuf *dmabuf;
16652 	int length;
16653 
16654 	length = (sizeof(struct lpfc_mbx_mq_create) -
16655 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16656 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16657 			 LPFC_MBOX_OPCODE_MQ_CREATE,
16658 			 length, LPFC_SLI4_MBX_EMBED);
16659 	mq_create = &mbox->u.mqe.un.mq_create;
16660 	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
16661 	       mq->page_count);
16662 	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
16663 	       cq->queue_id);
16664 	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16665 	switch (mq->entry_count) {
16666 	case 16:
16667 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16668 		       LPFC_MQ_RING_SIZE_16);
16669 		break;
16670 	case 32:
16671 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16672 		       LPFC_MQ_RING_SIZE_32);
16673 		break;
16674 	case 64:
16675 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16676 		       LPFC_MQ_RING_SIZE_64);
16677 		break;
16678 	case 128:
16679 		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16680 		       LPFC_MQ_RING_SIZE_128);
16681 		break;
16682 	}
16683 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16684 		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16685 			putPaddrLow(dmabuf->phys);
16686 		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16687 			putPaddrHigh(dmabuf->phys);
16688 	}
16689 }
16690 
16691 /**
16692  * lpfc_mq_create - Create a mailbox Queue on the HBA
16693  * @phba: HBA structure that indicates port to create a queue on.
16694  * @mq: The queue structure to use to create the mailbox queue.
16695  * @cq: The completion queue to associate with this cq.
16696  * @subtype: The queue's subtype.
16697  *
16698  * This function creates a mailbox queue, as detailed in @mq, on a port,
16699  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16700  *
16701  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16702  * is used to get the entry count and entry size that are necessary to
16703  * determine the number of pages to allocate and use for this queue. This
16704  * function will send the MQ_CREATE mailbox command to the HBA to setup the
16705  * mailbox queue. This function is asynchronous and will wait for the mailbox
16706  * command to finish before continuing.
16707  *
16708  * On success this function will return a zero. If unable to allocate enough
16709  * memory this function will return -ENOMEM. If the queue create mailbox command
16710  * fails this function will return -ENXIO.
16711  **/
16712 int32_t
lpfc_mq_create(struct lpfc_hba * phba,struct lpfc_queue * mq,struct lpfc_queue * cq,uint32_t subtype)16713 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16714 	       struct lpfc_queue *cq, uint32_t subtype)
16715 {
16716 	struct lpfc_mbx_mq_create *mq_create;
16717 	struct lpfc_mbx_mq_create_ext *mq_create_ext;
16718 	struct lpfc_dmabuf *dmabuf;
16719 	LPFC_MBOXQ_t *mbox;
16720 	int rc, length, status = 0;
16721 	uint32_t shdr_status, shdr_add_status;
16722 	union lpfc_sli4_cfg_shdr *shdr;
16723 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16724 
16725 	/* sanity check on queue memory */
16726 	if (!mq || !cq)
16727 		return -ENODEV;
16728 	if (!phba->sli4_hba.pc_sli4_params.supported)
16729 		hw_page_size = SLI4_PAGE_SIZE;
16730 
16731 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16732 	if (!mbox)
16733 		return -ENOMEM;
16734 	length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16735 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16736 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16737 			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16738 			 length, LPFC_SLI4_MBX_EMBED);
16739 
16740 	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
16741 	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
16742 	bf_set(lpfc_mbx_mq_create_ext_num_pages,
16743 	       &mq_create_ext->u.request, mq->page_count);
16744 	bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16745 	       &mq_create_ext->u.request, 1);
16746 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
16747 	       &mq_create_ext->u.request, 1);
16748 	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16749 	       &mq_create_ext->u.request, 1);
16750 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16751 	       &mq_create_ext->u.request, 1);
16752 	bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16753 	       &mq_create_ext->u.request, 1);
16754 	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
16755 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16756 	       phba->sli4_hba.pc_sli4_params.mqv);
16757 	if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16758 		bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16759 		       cq->queue_id);
16760 	else
16761 		bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16762 		       cq->queue_id);
16763 	switch (mq->entry_count) {
16764 	default:
16765 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16766 				"0362 Unsupported MQ count. (%d)\n",
16767 				mq->entry_count);
16768 		if (mq->entry_count < 16) {
16769 			status = -EINVAL;
16770 			goto out;
16771 		}
16772 		fallthrough;	/* otherwise default to smallest count */
16773 	case 16:
16774 		bf_set(lpfc_mq_context_ring_size,
16775 		       &mq_create_ext->u.request.context,
16776 		       LPFC_MQ_RING_SIZE_16);
16777 		break;
16778 	case 32:
16779 		bf_set(lpfc_mq_context_ring_size,
16780 		       &mq_create_ext->u.request.context,
16781 		       LPFC_MQ_RING_SIZE_32);
16782 		break;
16783 	case 64:
16784 		bf_set(lpfc_mq_context_ring_size,
16785 		       &mq_create_ext->u.request.context,
16786 		       LPFC_MQ_RING_SIZE_64);
16787 		break;
16788 	case 128:
16789 		bf_set(lpfc_mq_context_ring_size,
16790 		       &mq_create_ext->u.request.context,
16791 		       LPFC_MQ_RING_SIZE_128);
16792 		break;
16793 	}
16794 	list_for_each_entry(dmabuf, &mq->page_list, list) {
16795 		memset(dmabuf->virt, 0, hw_page_size);
16796 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16797 					putPaddrLow(dmabuf->phys);
16798 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16799 					putPaddrHigh(dmabuf->phys);
16800 	}
16801 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16802 	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16803 			      &mq_create_ext->u.response);
16804 	if (rc != MBX_SUCCESS) {
16805 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16806 				"2795 MQ_CREATE_EXT failed with "
16807 				"status x%x. Failback to MQ_CREATE.\n",
16808 				rc);
16809 		lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16810 		mq_create = &mbox->u.mqe.un.mq_create;
16811 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16812 		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16813 		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16814 				      &mq_create->u.response);
16815 	}
16816 
16817 	/* The IOCTL status is embedded in the mailbox subheader. */
16818 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16819 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16820 	if (shdr_status || shdr_add_status || rc) {
16821 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16822 				"2502 MQ_CREATE mailbox failed with "
16823 				"status x%x add_status x%x, mbx status x%x\n",
16824 				shdr_status, shdr_add_status, rc);
16825 		status = -ENXIO;
16826 		goto out;
16827 	}
16828 	if (mq->queue_id == 0xFFFF) {
16829 		status = -ENXIO;
16830 		goto out;
16831 	}
16832 	mq->type = LPFC_MQ;
16833 	mq->assoc_qid = cq->queue_id;
16834 	mq->subtype = subtype;
16835 	mq->host_index = 0;
16836 	mq->hba_index = 0;
16837 
16838 	/* link the mq onto the parent cq child list */
16839 	list_add_tail(&mq->list, &cq->child_list);
16840 out:
16841 	mempool_free(mbox, phba->mbox_mem_pool);
16842 	return status;
16843 }
16844 
16845 /**
16846  * lpfc_wq_create - Create a Work Queue on the HBA
16847  * @phba: HBA structure that indicates port to create a queue on.
16848  * @wq: The queue structure to use to create the work queue.
16849  * @cq: The completion queue to bind this work queue to.
16850  * @subtype: The subtype of the work queue indicating its functionality.
16851  *
16852  * This function creates a work queue, as detailed in @wq, on a port, described
16853  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16854  *
16855  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16856  * is used to get the entry count and entry size that are necessary to
16857  * determine the number of pages to allocate and use for this queue. The @cq
16858  * is used to indicate which completion queue to bind this work queue to. This
16859  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16860  * work queue. This function is asynchronous and will wait for the mailbox
16861  * command to finish before continuing.
16862  *
16863  * On success this function will return a zero. If unable to allocate enough
16864  * memory this function will return -ENOMEM. If the queue create mailbox command
16865  * fails this function will return -ENXIO.
16866  **/
16867 int
lpfc_wq_create(struct lpfc_hba * phba,struct lpfc_queue * wq,struct lpfc_queue * cq,uint32_t subtype)16868 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16869 	       struct lpfc_queue *cq, uint32_t subtype)
16870 {
16871 	struct lpfc_mbx_wq_create *wq_create;
16872 	struct lpfc_dmabuf *dmabuf;
16873 	LPFC_MBOXQ_t *mbox;
16874 	int rc, length, status = 0;
16875 	uint32_t shdr_status, shdr_add_status;
16876 	union lpfc_sli4_cfg_shdr *shdr;
16877 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16878 	struct dma_address *page;
16879 	void __iomem *bar_memmap_p;
16880 	uint32_t db_offset;
16881 	uint16_t pci_barset;
16882 	uint8_t dpp_barset;
16883 	uint32_t dpp_offset;
16884 	uint8_t wq_create_version;
16885 #ifdef CONFIG_X86
16886 	unsigned long pg_addr;
16887 #endif
16888 
16889 	/* sanity check on queue memory */
16890 	if (!wq || !cq)
16891 		return -ENODEV;
16892 	if (!phba->sli4_hba.pc_sli4_params.supported)
16893 		hw_page_size = wq->page_size;
16894 
16895 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16896 	if (!mbox)
16897 		return -ENOMEM;
16898 	length = (sizeof(struct lpfc_mbx_wq_create) -
16899 		  sizeof(struct lpfc_sli4_cfg_mhdr));
16900 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16901 			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16902 			 length, LPFC_SLI4_MBX_EMBED);
16903 	wq_create = &mbox->u.mqe.un.wq_create;
16904 	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16905 	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16906 		    wq->page_count);
16907 	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16908 		    cq->queue_id);
16909 
16910 	/* wqv is the earliest version supported, NOT the latest */
16911 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16912 	       phba->sli4_hba.pc_sli4_params.wqv);
16913 
16914 	if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16915 	    (wq->page_size > SLI4_PAGE_SIZE))
16916 		wq_create_version = LPFC_Q_CREATE_VERSION_1;
16917 	else
16918 		wq_create_version = LPFC_Q_CREATE_VERSION_0;
16919 
16920 	switch (wq_create_version) {
16921 	case LPFC_Q_CREATE_VERSION_1:
16922 		bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16923 		       wq->entry_count);
16924 		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16925 		       LPFC_Q_CREATE_VERSION_1);
16926 
16927 		switch (wq->entry_size) {
16928 		default:
16929 		case 64:
16930 			bf_set(lpfc_mbx_wq_create_wqe_size,
16931 			       &wq_create->u.request_1,
16932 			       LPFC_WQ_WQE_SIZE_64);
16933 			break;
16934 		case 128:
16935 			bf_set(lpfc_mbx_wq_create_wqe_size,
16936 			       &wq_create->u.request_1,
16937 			       LPFC_WQ_WQE_SIZE_128);
16938 			break;
16939 		}
16940 		/* Request DPP by default */
16941 		bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
16942 		bf_set(lpfc_mbx_wq_create_page_size,
16943 		       &wq_create->u.request_1,
16944 		       (wq->page_size / SLI4_PAGE_SIZE));
16945 		page = wq_create->u.request_1.page;
16946 		break;
16947 	default:
16948 		page = wq_create->u.request.page;
16949 		break;
16950 	}
16951 
16952 	list_for_each_entry(dmabuf, &wq->page_list, list) {
16953 		memset(dmabuf->virt, 0, hw_page_size);
16954 		page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16955 		page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
16956 	}
16957 
16958 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16959 		bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16960 
16961 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16962 	/* The IOCTL status is embedded in the mailbox subheader. */
16963 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16964 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16965 	if (shdr_status || shdr_add_status || rc) {
16966 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16967 				"2503 WQ_CREATE mailbox failed with "
16968 				"status x%x add_status x%x, mbx status x%x\n",
16969 				shdr_status, shdr_add_status, rc);
16970 		status = -ENXIO;
16971 		goto out;
16972 	}
16973 
16974 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16975 		wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16976 					&wq_create->u.response);
16977 	else
16978 		wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16979 					&wq_create->u.response_1);
16980 
16981 	if (wq->queue_id == 0xFFFF) {
16982 		status = -ENXIO;
16983 		goto out;
16984 	}
16985 
16986 	wq->db_format = LPFC_DB_LIST_FORMAT;
16987 	if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16988 		if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16989 			wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16990 					       &wq_create->u.response);
16991 			if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16992 			    (wq->db_format != LPFC_DB_RING_FORMAT)) {
16993 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16994 						"3265 WQ[%d] doorbell format "
16995 						"not supported: x%x\n",
16996 						wq->queue_id, wq->db_format);
16997 				status = -EINVAL;
16998 				goto out;
16999 			}
17000 			pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
17001 					    &wq_create->u.response);
17002 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17003 								   pci_barset);
17004 			if (!bar_memmap_p) {
17005 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17006 						"3263 WQ[%d] failed to memmap "
17007 						"pci barset:x%x\n",
17008 						wq->queue_id, pci_barset);
17009 				status = -ENOMEM;
17010 				goto out;
17011 			}
17012 			db_offset = wq_create->u.response.doorbell_offset;
17013 			if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
17014 			    (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
17015 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17016 						"3252 WQ[%d] doorbell offset "
17017 						"not supported: x%x\n",
17018 						wq->queue_id, db_offset);
17019 				status = -EINVAL;
17020 				goto out;
17021 			}
17022 			wq->db_regaddr = bar_memmap_p + db_offset;
17023 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17024 					"3264 WQ[%d]: barset:x%x, offset:x%x, "
17025 					"format:x%x\n", wq->queue_id,
17026 					pci_barset, db_offset, wq->db_format);
17027 		} else
17028 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17029 	} else {
17030 		/* Check if DPP was honored by the firmware */
17031 		wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
17032 				    &wq_create->u.response_1);
17033 		if (wq->dpp_enable) {
17034 			pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
17035 					    &wq_create->u.response_1);
17036 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17037 								   pci_barset);
17038 			if (!bar_memmap_p) {
17039 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17040 						"3267 WQ[%d] failed to memmap "
17041 						"pci barset:x%x\n",
17042 						wq->queue_id, pci_barset);
17043 				status = -ENOMEM;
17044 				goto out;
17045 			}
17046 			db_offset = wq_create->u.response_1.doorbell_offset;
17047 			wq->db_regaddr = bar_memmap_p + db_offset;
17048 			wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
17049 					    &wq_create->u.response_1);
17050 			dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
17051 					    &wq_create->u.response_1);
17052 			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17053 								   dpp_barset);
17054 			if (!bar_memmap_p) {
17055 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17056 						"3268 WQ[%d] failed to memmap "
17057 						"pci barset:x%x\n",
17058 						wq->queue_id, dpp_barset);
17059 				status = -ENOMEM;
17060 				goto out;
17061 			}
17062 			dpp_offset = wq_create->u.response_1.dpp_offset;
17063 			wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17064 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17065 					"3271 WQ[%d]: barset:x%x, offset:x%x, "
17066 					"dpp_id:x%x dpp_barset:x%x "
17067 					"dpp_offset:x%x\n",
17068 					wq->queue_id, pci_barset, db_offset,
17069 					wq->dpp_id, dpp_barset, dpp_offset);
17070 
17071 #ifdef CONFIG_X86
17072 			/* Enable combined writes for DPP aperture */
17073 			pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
17074 			rc = set_memory_wc(pg_addr, 1);
17075 			if (rc) {
17076 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17077 					"3272 Cannot setup Combined "
17078 					"Write on WQ[%d] - disable DPP\n",
17079 					wq->queue_id);
17080 				phba->cfg_enable_dpp = 0;
17081 			}
17082 #else
17083 			phba->cfg_enable_dpp = 0;
17084 #endif
17085 		} else
17086 			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17087 	}
17088 	wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17089 	if (wq->pring == NULL) {
17090 		status = -ENOMEM;
17091 		goto out;
17092 	}
17093 	wq->type = LPFC_WQ;
17094 	wq->assoc_qid = cq->queue_id;
17095 	wq->subtype = subtype;
17096 	wq->host_index = 0;
17097 	wq->hba_index = 0;
17098 	wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
17099 
17100 	/* link the wq onto the parent cq child list */
17101 	list_add_tail(&wq->list, &cq->child_list);
17102 out:
17103 	mempool_free(mbox, phba->mbox_mem_pool);
17104 	return status;
17105 }
17106 
17107 /**
17108  * lpfc_rq_create - Create a Receive Queue on the HBA
17109  * @phba: HBA structure that indicates port to create a queue on.
17110  * @hrq: The queue structure to use to create the header receive queue.
17111  * @drq: The queue structure to use to create the data receive queue.
17112  * @cq: The completion queue to bind this work queue to.
17113  * @subtype: The subtype of the work queue indicating its functionality.
17114  *
17115  * This function creates a receive buffer queue pair , as detailed in @hrq and
17116  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17117  * to the HBA.
17118  *
17119  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17120  * struct is used to get the entry count that is necessary to determine the
17121  * number of pages to use for this queue. The @cq is used to indicate which
17122  * completion queue to bind received buffers that are posted to these queues to.
17123  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17124  * receive queue pair. This function is asynchronous and will wait for the
17125  * mailbox command to finish before continuing.
17126  *
17127  * On success this function will return a zero. If unable to allocate enough
17128  * memory this function will return -ENOMEM. If the queue create mailbox command
17129  * fails this function will return -ENXIO.
17130  **/
17131 int
lpfc_rq_create(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq,struct lpfc_queue * cq,uint32_t subtype)17132 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17133 	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17134 {
17135 	struct lpfc_mbx_rq_create *rq_create;
17136 	struct lpfc_dmabuf *dmabuf;
17137 	LPFC_MBOXQ_t *mbox;
17138 	int rc, length, status = 0;
17139 	uint32_t shdr_status, shdr_add_status;
17140 	union lpfc_sli4_cfg_shdr *shdr;
17141 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17142 	void __iomem *bar_memmap_p;
17143 	uint32_t db_offset;
17144 	uint16_t pci_barset;
17145 
17146 	/* sanity check on queue memory */
17147 	if (!hrq || !drq || !cq)
17148 		return -ENODEV;
17149 	if (!phba->sli4_hba.pc_sli4_params.supported)
17150 		hw_page_size = SLI4_PAGE_SIZE;
17151 
17152 	if (hrq->entry_count != drq->entry_count)
17153 		return -EINVAL;
17154 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17155 	if (!mbox)
17156 		return -ENOMEM;
17157 	length = (sizeof(struct lpfc_mbx_rq_create) -
17158 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17159 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17160 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17161 			 length, LPFC_SLI4_MBX_EMBED);
17162 	rq_create = &mbox->u.mqe.un.rq_create;
17163 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17164 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17165 	       phba->sli4_hba.pc_sli4_params.rqv);
17166 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17167 		bf_set(lpfc_rq_context_rqe_count_1,
17168 		       &rq_create->u.request.context,
17169 		       hrq->entry_count);
17170 		rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
17171 		bf_set(lpfc_rq_context_rqe_size,
17172 		       &rq_create->u.request.context,
17173 		       LPFC_RQE_SIZE_8);
17174 		bf_set(lpfc_rq_context_page_size,
17175 		       &rq_create->u.request.context,
17176 		       LPFC_RQ_PAGE_SIZE_4096);
17177 	} else {
17178 		switch (hrq->entry_count) {
17179 		default:
17180 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17181 					"2535 Unsupported RQ count. (%d)\n",
17182 					hrq->entry_count);
17183 			if (hrq->entry_count < 512) {
17184 				status = -EINVAL;
17185 				goto out;
17186 			}
17187 			fallthrough;	/* otherwise default to smallest count */
17188 		case 512:
17189 			bf_set(lpfc_rq_context_rqe_count,
17190 			       &rq_create->u.request.context,
17191 			       LPFC_RQ_RING_SIZE_512);
17192 			break;
17193 		case 1024:
17194 			bf_set(lpfc_rq_context_rqe_count,
17195 			       &rq_create->u.request.context,
17196 			       LPFC_RQ_RING_SIZE_1024);
17197 			break;
17198 		case 2048:
17199 			bf_set(lpfc_rq_context_rqe_count,
17200 			       &rq_create->u.request.context,
17201 			       LPFC_RQ_RING_SIZE_2048);
17202 			break;
17203 		case 4096:
17204 			bf_set(lpfc_rq_context_rqe_count,
17205 			       &rq_create->u.request.context,
17206 			       LPFC_RQ_RING_SIZE_4096);
17207 			break;
17208 		}
17209 		bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17210 		       LPFC_HDR_BUF_SIZE);
17211 	}
17212 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17213 	       cq->queue_id);
17214 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17215 	       hrq->page_count);
17216 	list_for_each_entry(dmabuf, &hrq->page_list, list) {
17217 		memset(dmabuf->virt, 0, hw_page_size);
17218 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17219 					putPaddrLow(dmabuf->phys);
17220 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17221 					putPaddrHigh(dmabuf->phys);
17222 	}
17223 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17224 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17225 
17226 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17227 	/* The IOCTL status is embedded in the mailbox subheader. */
17228 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17229 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17230 	if (shdr_status || shdr_add_status || rc) {
17231 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17232 				"2504 RQ_CREATE mailbox failed with "
17233 				"status x%x add_status x%x, mbx status x%x\n",
17234 				shdr_status, shdr_add_status, rc);
17235 		status = -ENXIO;
17236 		goto out;
17237 	}
17238 	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17239 	if (hrq->queue_id == 0xFFFF) {
17240 		status = -ENXIO;
17241 		goto out;
17242 	}
17243 
17244 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17245 		hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17246 					&rq_create->u.response);
17247 		if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17248 		    (hrq->db_format != LPFC_DB_RING_FORMAT)) {
17249 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17250 					"3262 RQ [%d] doorbell format not "
17251 					"supported: x%x\n", hrq->queue_id,
17252 					hrq->db_format);
17253 			status = -EINVAL;
17254 			goto out;
17255 		}
17256 
17257 		pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17258 				    &rq_create->u.response);
17259 		bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17260 		if (!bar_memmap_p) {
17261 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17262 					"3269 RQ[%d] failed to memmap pci "
17263 					"barset:x%x\n", hrq->queue_id,
17264 					pci_barset);
17265 			status = -ENOMEM;
17266 			goto out;
17267 		}
17268 
17269 		db_offset = rq_create->u.response.doorbell_offset;
17270 		if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17271 		    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
17272 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17273 					"3270 RQ[%d] doorbell offset not "
17274 					"supported: x%x\n", hrq->queue_id,
17275 					db_offset);
17276 			status = -EINVAL;
17277 			goto out;
17278 		}
17279 		hrq->db_regaddr = bar_memmap_p + db_offset;
17280 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17281 				"3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17282 				"format:x%x\n", hrq->queue_id, pci_barset,
17283 				db_offset, hrq->db_format);
17284 	} else {
17285 		hrq->db_format = LPFC_DB_RING_FORMAT;
17286 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17287 	}
17288 	hrq->type = LPFC_HRQ;
17289 	hrq->assoc_qid = cq->queue_id;
17290 	hrq->subtype = subtype;
17291 	hrq->host_index = 0;
17292 	hrq->hba_index = 0;
17293 	hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17294 
17295 	/* now create the data queue */
17296 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17297 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17298 			 length, LPFC_SLI4_MBX_EMBED);
17299 	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17300 	       phba->sli4_hba.pc_sli4_params.rqv);
17301 	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17302 		bf_set(lpfc_rq_context_rqe_count_1,
17303 		       &rq_create->u.request.context, hrq->entry_count);
17304 		if (subtype == LPFC_NVMET)
17305 			rq_create->u.request.context.buffer_size =
17306 				LPFC_NVMET_DATA_BUF_SIZE;
17307 		else
17308 			rq_create->u.request.context.buffer_size =
17309 				LPFC_DATA_BUF_SIZE;
17310 		bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17311 		       LPFC_RQE_SIZE_8);
17312 		bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17313 		       (PAGE_SIZE/SLI4_PAGE_SIZE));
17314 	} else {
17315 		switch (drq->entry_count) {
17316 		default:
17317 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17318 					"2536 Unsupported RQ count. (%d)\n",
17319 					drq->entry_count);
17320 			if (drq->entry_count < 512) {
17321 				status = -EINVAL;
17322 				goto out;
17323 			}
17324 			fallthrough;	/* otherwise default to smallest count */
17325 		case 512:
17326 			bf_set(lpfc_rq_context_rqe_count,
17327 			       &rq_create->u.request.context,
17328 			       LPFC_RQ_RING_SIZE_512);
17329 			break;
17330 		case 1024:
17331 			bf_set(lpfc_rq_context_rqe_count,
17332 			       &rq_create->u.request.context,
17333 			       LPFC_RQ_RING_SIZE_1024);
17334 			break;
17335 		case 2048:
17336 			bf_set(lpfc_rq_context_rqe_count,
17337 			       &rq_create->u.request.context,
17338 			       LPFC_RQ_RING_SIZE_2048);
17339 			break;
17340 		case 4096:
17341 			bf_set(lpfc_rq_context_rqe_count,
17342 			       &rq_create->u.request.context,
17343 			       LPFC_RQ_RING_SIZE_4096);
17344 			break;
17345 		}
17346 		if (subtype == LPFC_NVMET)
17347 			bf_set(lpfc_rq_context_buf_size,
17348 			       &rq_create->u.request.context,
17349 			       LPFC_NVMET_DATA_BUF_SIZE);
17350 		else
17351 			bf_set(lpfc_rq_context_buf_size,
17352 			       &rq_create->u.request.context,
17353 			       LPFC_DATA_BUF_SIZE);
17354 	}
17355 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17356 	       cq->queue_id);
17357 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17358 	       drq->page_count);
17359 	list_for_each_entry(dmabuf, &drq->page_list, list) {
17360 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17361 					putPaddrLow(dmabuf->phys);
17362 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17363 					putPaddrHigh(dmabuf->phys);
17364 	}
17365 	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17366 		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17367 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17368 	/* The IOCTL status is embedded in the mailbox subheader. */
17369 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17370 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17371 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17372 	if (shdr_status || shdr_add_status || rc) {
17373 		status = -ENXIO;
17374 		goto out;
17375 	}
17376 	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17377 	if (drq->queue_id == 0xFFFF) {
17378 		status = -ENXIO;
17379 		goto out;
17380 	}
17381 	drq->type = LPFC_DRQ;
17382 	drq->assoc_qid = cq->queue_id;
17383 	drq->subtype = subtype;
17384 	drq->host_index = 0;
17385 	drq->hba_index = 0;
17386 	drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17387 
17388 	/* link the header and data RQs onto the parent cq child list */
17389 	list_add_tail(&hrq->list, &cq->child_list);
17390 	list_add_tail(&drq->list, &cq->child_list);
17391 
17392 out:
17393 	mempool_free(mbox, phba->mbox_mem_pool);
17394 	return status;
17395 }
17396 
17397 /**
17398  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17399  * @phba: HBA structure that indicates port to create a queue on.
17400  * @hrqp: The queue structure array to use to create the header receive queues.
17401  * @drqp: The queue structure array to use to create the data receive queues.
17402  * @cqp: The completion queue array to bind these receive queues to.
17403  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
17404  *
17405  * This function creates a receive buffer queue pair , as detailed in @hrq and
17406  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17407  * to the HBA.
17408  *
17409  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17410  * struct is used to get the entry count that is necessary to determine the
17411  * number of pages to use for this queue. The @cq is used to indicate which
17412  * completion queue to bind received buffers that are posted to these queues to.
17413  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17414  * receive queue pair. This function is asynchronous and will wait for the
17415  * mailbox command to finish before continuing.
17416  *
17417  * On success this function will return a zero. If unable to allocate enough
17418  * memory this function will return -ENOMEM. If the queue create mailbox command
17419  * fails this function will return -ENXIO.
17420  **/
17421 int
lpfc_mrq_create(struct lpfc_hba * phba,struct lpfc_queue ** hrqp,struct lpfc_queue ** drqp,struct lpfc_queue ** cqp,uint32_t subtype)17422 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17423 		struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17424 		uint32_t subtype)
17425 {
17426 	struct lpfc_queue *hrq, *drq, *cq;
17427 	struct lpfc_mbx_rq_create_v2 *rq_create;
17428 	struct lpfc_dmabuf *dmabuf;
17429 	LPFC_MBOXQ_t *mbox;
17430 	int rc, length, alloclen, status = 0;
17431 	int cnt, idx, numrq, page_idx = 0;
17432 	uint32_t shdr_status, shdr_add_status;
17433 	union lpfc_sli4_cfg_shdr *shdr;
17434 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17435 
17436 	numrq = phba->cfg_nvmet_mrq;
17437 	/* sanity check on array memory */
17438 	if (!hrqp || !drqp || !cqp || !numrq)
17439 		return -ENODEV;
17440 	if (!phba->sli4_hba.pc_sli4_params.supported)
17441 		hw_page_size = SLI4_PAGE_SIZE;
17442 
17443 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17444 	if (!mbox)
17445 		return -ENOMEM;
17446 
17447 	length = sizeof(struct lpfc_mbx_rq_create_v2);
17448 	length += ((2 * numrq * hrqp[0]->page_count) *
17449 		   sizeof(struct dma_address));
17450 
17451 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17452 				    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17453 				    LPFC_SLI4_MBX_NEMBED);
17454 	if (alloclen < length) {
17455 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17456 				"3099 Allocated DMA memory size (%d) is "
17457 				"less than the requested DMA memory size "
17458 				"(%d)\n", alloclen, length);
17459 		status = -ENOMEM;
17460 		goto out;
17461 	}
17462 
17463 
17464 
17465 	rq_create = mbox->sge_array->addr[0];
17466 	shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17467 
17468 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17469 	cnt = 0;
17470 
17471 	for (idx = 0; idx < numrq; idx++) {
17472 		hrq = hrqp[idx];
17473 		drq = drqp[idx];
17474 		cq  = cqp[idx];
17475 
17476 		/* sanity check on queue memory */
17477 		if (!hrq || !drq || !cq) {
17478 			status = -ENODEV;
17479 			goto out;
17480 		}
17481 
17482 		if (hrq->entry_count != drq->entry_count) {
17483 			status = -EINVAL;
17484 			goto out;
17485 		}
17486 
17487 		if (idx == 0) {
17488 			bf_set(lpfc_mbx_rq_create_num_pages,
17489 			       &rq_create->u.request,
17490 			       hrq->page_count);
17491 			bf_set(lpfc_mbx_rq_create_rq_cnt,
17492 			       &rq_create->u.request, (numrq * 2));
17493 			bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17494 			       1);
17495 			bf_set(lpfc_rq_context_base_cq,
17496 			       &rq_create->u.request.context,
17497 			       cq->queue_id);
17498 			bf_set(lpfc_rq_context_data_size,
17499 			       &rq_create->u.request.context,
17500 			       LPFC_NVMET_DATA_BUF_SIZE);
17501 			bf_set(lpfc_rq_context_hdr_size,
17502 			       &rq_create->u.request.context,
17503 			       LPFC_HDR_BUF_SIZE);
17504 			bf_set(lpfc_rq_context_rqe_count_1,
17505 			       &rq_create->u.request.context,
17506 			       hrq->entry_count);
17507 			bf_set(lpfc_rq_context_rqe_size,
17508 			       &rq_create->u.request.context,
17509 			       LPFC_RQE_SIZE_8);
17510 			bf_set(lpfc_rq_context_page_size,
17511 			       &rq_create->u.request.context,
17512 			       (PAGE_SIZE/SLI4_PAGE_SIZE));
17513 		}
17514 		rc = 0;
17515 		list_for_each_entry(dmabuf, &hrq->page_list, list) {
17516 			memset(dmabuf->virt, 0, hw_page_size);
17517 			cnt = page_idx + dmabuf->buffer_tag;
17518 			rq_create->u.request.page[cnt].addr_lo =
17519 					putPaddrLow(dmabuf->phys);
17520 			rq_create->u.request.page[cnt].addr_hi =
17521 					putPaddrHigh(dmabuf->phys);
17522 			rc++;
17523 		}
17524 		page_idx += rc;
17525 
17526 		rc = 0;
17527 		list_for_each_entry(dmabuf, &drq->page_list, list) {
17528 			memset(dmabuf->virt, 0, hw_page_size);
17529 			cnt = page_idx + dmabuf->buffer_tag;
17530 			rq_create->u.request.page[cnt].addr_lo =
17531 					putPaddrLow(dmabuf->phys);
17532 			rq_create->u.request.page[cnt].addr_hi =
17533 					putPaddrHigh(dmabuf->phys);
17534 			rc++;
17535 		}
17536 		page_idx += rc;
17537 
17538 		hrq->db_format = LPFC_DB_RING_FORMAT;
17539 		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17540 		hrq->type = LPFC_HRQ;
17541 		hrq->assoc_qid = cq->queue_id;
17542 		hrq->subtype = subtype;
17543 		hrq->host_index = 0;
17544 		hrq->hba_index = 0;
17545 		hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17546 
17547 		drq->db_format = LPFC_DB_RING_FORMAT;
17548 		drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17549 		drq->type = LPFC_DRQ;
17550 		drq->assoc_qid = cq->queue_id;
17551 		drq->subtype = subtype;
17552 		drq->host_index = 0;
17553 		drq->hba_index = 0;
17554 		drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17555 
17556 		list_add_tail(&hrq->list, &cq->child_list);
17557 		list_add_tail(&drq->list, &cq->child_list);
17558 	}
17559 
17560 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17561 	/* The IOCTL status is embedded in the mailbox subheader. */
17562 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17563 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17564 	if (shdr_status || shdr_add_status || rc) {
17565 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17566 				"3120 RQ_CREATE mailbox failed with "
17567 				"status x%x add_status x%x, mbx status x%x\n",
17568 				shdr_status, shdr_add_status, rc);
17569 		status = -ENXIO;
17570 		goto out;
17571 	}
17572 	rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17573 	if (rc == 0xFFFF) {
17574 		status = -ENXIO;
17575 		goto out;
17576 	}
17577 
17578 	/* Initialize all RQs with associated queue id */
17579 	for (idx = 0; idx < numrq; idx++) {
17580 		hrq = hrqp[idx];
17581 		hrq->queue_id = rc + (2 * idx);
17582 		drq = drqp[idx];
17583 		drq->queue_id = rc + (2 * idx) + 1;
17584 	}
17585 
17586 out:
17587 	lpfc_sli4_mbox_cmd_free(phba, mbox);
17588 	return status;
17589 }
17590 
17591 /**
17592  * lpfc_eq_destroy - Destroy an event Queue on the HBA
17593  * @phba: HBA structure that indicates port to destroy a queue on.
17594  * @eq: The queue structure associated with the queue to destroy.
17595  *
17596  * This function destroys a queue, as detailed in @eq by sending an mailbox
17597  * command, specific to the type of queue, to the HBA.
17598  *
17599  * The @eq struct is used to get the queue ID of the queue to destroy.
17600  *
17601  * On success this function will return a zero. If the queue destroy mailbox
17602  * command fails this function will return -ENXIO.
17603  **/
17604 int
lpfc_eq_destroy(struct lpfc_hba * phba,struct lpfc_queue * eq)17605 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17606 {
17607 	LPFC_MBOXQ_t *mbox;
17608 	int rc, length, status = 0;
17609 	uint32_t shdr_status, shdr_add_status;
17610 	union lpfc_sli4_cfg_shdr *shdr;
17611 
17612 	/* sanity check on queue memory */
17613 	if (!eq)
17614 		return -ENODEV;
17615 
17616 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17617 		goto list_remove;
17618 
17619 	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17620 	if (!mbox)
17621 		return -ENOMEM;
17622 	length = (sizeof(struct lpfc_mbx_eq_destroy) -
17623 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17624 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17625 			 LPFC_MBOX_OPCODE_EQ_DESTROY,
17626 			 length, LPFC_SLI4_MBX_EMBED);
17627 	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17628 	       eq->queue_id);
17629 	mbox->vport = eq->phba->pport;
17630 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17631 
17632 	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17633 	/* The IOCTL status is embedded in the mailbox subheader. */
17634 	shdr = (union lpfc_sli4_cfg_shdr *)
17635 		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17636 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17637 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17638 	if (shdr_status || shdr_add_status || rc) {
17639 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17640 				"2505 EQ_DESTROY mailbox failed with "
17641 				"status x%x add_status x%x, mbx status x%x\n",
17642 				shdr_status, shdr_add_status, rc);
17643 		status = -ENXIO;
17644 	}
17645 	mempool_free(mbox, eq->phba->mbox_mem_pool);
17646 
17647 list_remove:
17648 	/* Remove eq from any list */
17649 	list_del_init(&eq->list);
17650 
17651 	return status;
17652 }
17653 
17654 /**
17655  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
17656  * @phba: HBA structure that indicates port to destroy a queue on.
17657  * @cq: The queue structure associated with the queue to destroy.
17658  *
17659  * This function destroys a queue, as detailed in @cq by sending an mailbox
17660  * command, specific to the type of queue, to the HBA.
17661  *
17662  * The @cq struct is used to get the queue ID of the queue to destroy.
17663  *
17664  * On success this function will return a zero. If the queue destroy mailbox
17665  * command fails this function will return -ENXIO.
17666  **/
17667 int
lpfc_cq_destroy(struct lpfc_hba * phba,struct lpfc_queue * cq)17668 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17669 {
17670 	LPFC_MBOXQ_t *mbox;
17671 	int rc, length, status = 0;
17672 	uint32_t shdr_status, shdr_add_status;
17673 	union lpfc_sli4_cfg_shdr *shdr;
17674 
17675 	/* sanity check on queue memory */
17676 	if (!cq)
17677 		return -ENODEV;
17678 
17679 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17680 		goto list_remove;
17681 
17682 	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17683 	if (!mbox)
17684 		return -ENOMEM;
17685 	length = (sizeof(struct lpfc_mbx_cq_destroy) -
17686 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17687 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17688 			 LPFC_MBOX_OPCODE_CQ_DESTROY,
17689 			 length, LPFC_SLI4_MBX_EMBED);
17690 	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17691 	       cq->queue_id);
17692 	mbox->vport = cq->phba->pport;
17693 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17694 	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17695 	/* The IOCTL status is embedded in the mailbox subheader. */
17696 	shdr = (union lpfc_sli4_cfg_shdr *)
17697 		&mbox->u.mqe.un.wq_create.header.cfg_shdr;
17698 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17699 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17700 	if (shdr_status || shdr_add_status || rc) {
17701 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17702 				"2506 CQ_DESTROY mailbox failed with "
17703 				"status x%x add_status x%x, mbx status x%x\n",
17704 				shdr_status, shdr_add_status, rc);
17705 		status = -ENXIO;
17706 	}
17707 	mempool_free(mbox, cq->phba->mbox_mem_pool);
17708 
17709 list_remove:
17710 	/* Remove cq from any list */
17711 	list_del_init(&cq->list);
17712 	return status;
17713 }
17714 
17715 /**
17716  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
17717  * @phba: HBA structure that indicates port to destroy a queue on.
17718  * @mq: The queue structure associated with the queue to destroy.
17719  *
17720  * This function destroys a queue, as detailed in @mq by sending an mailbox
17721  * command, specific to the type of queue, to the HBA.
17722  *
17723  * The @mq struct is used to get the queue ID of the queue to destroy.
17724  *
17725  * On success this function will return a zero. If the queue destroy mailbox
17726  * command fails this function will return -ENXIO.
17727  **/
17728 int
lpfc_mq_destroy(struct lpfc_hba * phba,struct lpfc_queue * mq)17729 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17730 {
17731 	LPFC_MBOXQ_t *mbox;
17732 	int rc, length, status = 0;
17733 	uint32_t shdr_status, shdr_add_status;
17734 	union lpfc_sli4_cfg_shdr *shdr;
17735 
17736 	/* sanity check on queue memory */
17737 	if (!mq)
17738 		return -ENODEV;
17739 
17740 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17741 		goto list_remove;
17742 
17743 	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17744 	if (!mbox)
17745 		return -ENOMEM;
17746 	length = (sizeof(struct lpfc_mbx_mq_destroy) -
17747 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17748 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17749 			 LPFC_MBOX_OPCODE_MQ_DESTROY,
17750 			 length, LPFC_SLI4_MBX_EMBED);
17751 	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17752 	       mq->queue_id);
17753 	mbox->vport = mq->phba->pport;
17754 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17755 	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17756 	/* The IOCTL status is embedded in the mailbox subheader. */
17757 	shdr = (union lpfc_sli4_cfg_shdr *)
17758 		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17759 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17760 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17761 	if (shdr_status || shdr_add_status || rc) {
17762 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17763 				"2507 MQ_DESTROY mailbox failed with "
17764 				"status x%x add_status x%x, mbx status x%x\n",
17765 				shdr_status, shdr_add_status, rc);
17766 		status = -ENXIO;
17767 	}
17768 	mempool_free(mbox, mq->phba->mbox_mem_pool);
17769 
17770 list_remove:
17771 	/* Remove mq from any list */
17772 	list_del_init(&mq->list);
17773 	return status;
17774 }
17775 
17776 /**
17777  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
17778  * @phba: HBA structure that indicates port to destroy a queue on.
17779  * @wq: The queue structure associated with the queue to destroy.
17780  *
17781  * This function destroys a queue, as detailed in @wq by sending an mailbox
17782  * command, specific to the type of queue, to the HBA.
17783  *
17784  * The @wq struct is used to get the queue ID of the queue to destroy.
17785  *
17786  * On success this function will return a zero. If the queue destroy mailbox
17787  * command fails this function will return -ENXIO.
17788  **/
17789 int
lpfc_wq_destroy(struct lpfc_hba * phba,struct lpfc_queue * wq)17790 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17791 {
17792 	LPFC_MBOXQ_t *mbox;
17793 	int rc, length, status = 0;
17794 	uint32_t shdr_status, shdr_add_status;
17795 	union lpfc_sli4_cfg_shdr *shdr;
17796 
17797 	/* sanity check on queue memory */
17798 	if (!wq)
17799 		return -ENODEV;
17800 
17801 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17802 		goto list_remove;
17803 
17804 	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17805 	if (!mbox)
17806 		return -ENOMEM;
17807 	length = (sizeof(struct lpfc_mbx_wq_destroy) -
17808 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17809 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17810 			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17811 			 length, LPFC_SLI4_MBX_EMBED);
17812 	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17813 	       wq->queue_id);
17814 	mbox->vport = wq->phba->pport;
17815 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17816 	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17817 	shdr = (union lpfc_sli4_cfg_shdr *)
17818 		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17819 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17820 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17821 	if (shdr_status || shdr_add_status || rc) {
17822 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17823 				"2508 WQ_DESTROY mailbox failed with "
17824 				"status x%x add_status x%x, mbx status x%x\n",
17825 				shdr_status, shdr_add_status, rc);
17826 		status = -ENXIO;
17827 	}
17828 	mempool_free(mbox, wq->phba->mbox_mem_pool);
17829 
17830 list_remove:
17831 	/* Remove wq from any list */
17832 	list_del_init(&wq->list);
17833 	kfree(wq->pring);
17834 	wq->pring = NULL;
17835 	return status;
17836 }
17837 
17838 /**
17839  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17840  * @phba: HBA structure that indicates port to destroy a queue on.
17841  * @hrq: The queue structure associated with the queue to destroy.
17842  * @drq: The queue structure associated with the queue to destroy.
17843  *
17844  * This function destroys a queue, as detailed in @rq by sending an mailbox
17845  * command, specific to the type of queue, to the HBA.
17846  *
17847  * The @rq struct is used to get the queue ID of the queue to destroy.
17848  *
17849  * On success this function will return a zero. If the queue destroy mailbox
17850  * command fails this function will return -ENXIO.
17851  **/
17852 int
lpfc_rq_destroy(struct lpfc_hba * phba,struct lpfc_queue * hrq,struct lpfc_queue * drq)17853 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17854 		struct lpfc_queue *drq)
17855 {
17856 	LPFC_MBOXQ_t *mbox;
17857 	int rc, length, status = 0;
17858 	uint32_t shdr_status, shdr_add_status;
17859 	union lpfc_sli4_cfg_shdr *shdr;
17860 
17861 	/* sanity check on queue memory */
17862 	if (!hrq || !drq)
17863 		return -ENODEV;
17864 
17865 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17866 		goto list_remove;
17867 
17868 	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17869 	if (!mbox)
17870 		return -ENOMEM;
17871 	length = (sizeof(struct lpfc_mbx_rq_destroy) -
17872 		  sizeof(struct lpfc_sli4_cfg_mhdr));
17873 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17874 			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17875 			 length, LPFC_SLI4_MBX_EMBED);
17876 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17877 	       hrq->queue_id);
17878 	mbox->vport = hrq->phba->pport;
17879 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17880 	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17881 	/* The IOCTL status is embedded in the mailbox subheader. */
17882 	shdr = (union lpfc_sli4_cfg_shdr *)
17883 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17884 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17885 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17886 	if (shdr_status || shdr_add_status || rc) {
17887 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17888 				"2509 RQ_DESTROY mailbox failed with "
17889 				"status x%x add_status x%x, mbx status x%x\n",
17890 				shdr_status, shdr_add_status, rc);
17891 		mempool_free(mbox, hrq->phba->mbox_mem_pool);
17892 		return -ENXIO;
17893 	}
17894 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17895 	       drq->queue_id);
17896 	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17897 	shdr = (union lpfc_sli4_cfg_shdr *)
17898 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17899 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17900 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17901 	if (shdr_status || shdr_add_status || rc) {
17902 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17903 				"2510 RQ_DESTROY mailbox failed with "
17904 				"status x%x add_status x%x, mbx status x%x\n",
17905 				shdr_status, shdr_add_status, rc);
17906 		status = -ENXIO;
17907 	}
17908 	mempool_free(mbox, hrq->phba->mbox_mem_pool);
17909 
17910 list_remove:
17911 	list_del_init(&hrq->list);
17912 	list_del_init(&drq->list);
17913 	return status;
17914 }
17915 
17916 /**
17917  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17918  * @phba: The virtual port for which this call being executed.
17919  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17920  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17921  * @xritag: the xritag that ties this io to the SGL pages.
17922  *
17923  * This routine will post the sgl pages for the IO that has the xritag
17924  * that is in the iocbq structure. The xritag is assigned during iocbq
17925  * creation and persists for as long as the driver is loaded.
17926  * if the caller has fewer than 256 scatter gather segments to map then
17927  * pdma_phys_addr1 should be 0.
17928  * If the caller needs to map more than 256 scatter gather segment then
17929  * pdma_phys_addr1 should be a valid physical address.
17930  * physical address for SGLs must be 64 byte aligned.
17931  * If you are going to map 2 SGL's then the first one must have 256 entries
17932  * the second sgl can have between 1 and 256 entries.
17933  *
17934  * Return codes:
17935  * 	0 - Success
17936  * 	-ENXIO, -ENOMEM - Failure
17937  **/
17938 int
lpfc_sli4_post_sgl(struct lpfc_hba * phba,dma_addr_t pdma_phys_addr0,dma_addr_t pdma_phys_addr1,uint16_t xritag)17939 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17940 		dma_addr_t pdma_phys_addr0,
17941 		dma_addr_t pdma_phys_addr1,
17942 		uint16_t xritag)
17943 {
17944 	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17945 	LPFC_MBOXQ_t *mbox;
17946 	int rc;
17947 	uint32_t shdr_status, shdr_add_status;
17948 	uint32_t mbox_tmo;
17949 	union lpfc_sli4_cfg_shdr *shdr;
17950 
17951 	if (xritag == NO_XRI) {
17952 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17953 				"0364 Invalid param:\n");
17954 		return -EINVAL;
17955 	}
17956 
17957 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17958 	if (!mbox)
17959 		return -ENOMEM;
17960 
17961 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17962 			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17963 			sizeof(struct lpfc_mbx_post_sgl_pages) -
17964 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17965 
17966 	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17967 				&mbox->u.mqe.un.post_sgl_pages;
17968 	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17969 	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17970 
17971 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=
17972 				cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17973 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17974 				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17975 
17976 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=
17977 				cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17978 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17979 				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17980 	if (!phba->sli4_hba.intr_enable)
17981 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17982 	else {
17983 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17984 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17985 	}
17986 	/* The IOCTL status is embedded in the mailbox subheader. */
17987 	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17988 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17989 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17990 	if (!phba->sli4_hba.intr_enable)
17991 		mempool_free(mbox, phba->mbox_mem_pool);
17992 	else if (rc != MBX_TIMEOUT)
17993 		mempool_free(mbox, phba->mbox_mem_pool);
17994 	if (shdr_status || shdr_add_status || rc) {
17995 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17996 				"2511 POST_SGL mailbox failed with "
17997 				"status x%x add_status x%x, mbx status x%x\n",
17998 				shdr_status, shdr_add_status, rc);
17999 	}
18000 	return 0;
18001 }
18002 
18003 /**
18004  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
18005  * @phba: pointer to lpfc hba data structure.
18006  *
18007  * This routine is invoked to post rpi header templates to the
18008  * HBA consistent with the SLI-4 interface spec.  This routine
18009  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18010  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18011  *
18012  * Returns
18013  *	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18014  *	LPFC_RPI_ALLOC_ERROR if no rpis are available.
18015  **/
18016 static uint16_t
lpfc_sli4_alloc_xri(struct lpfc_hba * phba)18017 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
18018 {
18019 	unsigned long xri;
18020 
18021 	/*
18022 	 * Fetch the next logical xri.  Because this index is logical,
18023 	 * the driver starts at 0 each time.
18024 	 */
18025 	spin_lock_irq(&phba->hbalock);
18026 	xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,
18027 				 phba->sli4_hba.max_cfg_param.max_xri);
18028 	if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
18029 		spin_unlock_irq(&phba->hbalock);
18030 		return NO_XRI;
18031 	} else {
18032 		set_bit(xri, phba->sli4_hba.xri_bmask);
18033 		phba->sli4_hba.max_cfg_param.xri_used++;
18034 	}
18035 	spin_unlock_irq(&phba->hbalock);
18036 	return xri;
18037 }
18038 
18039 /**
18040  * __lpfc_sli4_free_xri - Release an xri for reuse.
18041  * @phba: pointer to lpfc hba data structure.
18042  * @xri: xri to release.
18043  *
18044  * This routine is invoked to release an xri to the pool of
18045  * available rpis maintained by the driver.
18046  **/
18047 static void
__lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18048 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18049 {
18050 	if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
18051 		phba->sli4_hba.max_cfg_param.xri_used--;
18052 	}
18053 }
18054 
18055 /**
18056  * lpfc_sli4_free_xri - Release an xri for reuse.
18057  * @phba: pointer to lpfc hba data structure.
18058  * @xri: xri to release.
18059  *
18060  * This routine is invoked to release an xri to the pool of
18061  * available rpis maintained by the driver.
18062  **/
18063 void
lpfc_sli4_free_xri(struct lpfc_hba * phba,int xri)18064 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18065 {
18066 	spin_lock_irq(&phba->hbalock);
18067 	__lpfc_sli4_free_xri(phba, xri);
18068 	spin_unlock_irq(&phba->hbalock);
18069 }
18070 
18071 /**
18072  * lpfc_sli4_next_xritag - Get an xritag for the io
18073  * @phba: Pointer to HBA context object.
18074  *
18075  * This function gets an xritag for the iocb. If there is no unused xritag
18076  * it will return 0xffff.
18077  * The function returns the allocated xritag if successful, else returns zero.
18078  * Zero is not a valid xritag.
18079  * The caller is not required to hold any lock.
18080  **/
18081 uint16_t
lpfc_sli4_next_xritag(struct lpfc_hba * phba)18082 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
18083 {
18084 	uint16_t xri_index;
18085 
18086 	xri_index = lpfc_sli4_alloc_xri(phba);
18087 	if (xri_index == NO_XRI)
18088 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18089 				"2004 Failed to allocate XRI.last XRITAG is %d"
18090 				" Max XRI is %d, Used XRI is %d\n",
18091 				xri_index,
18092 				phba->sli4_hba.max_cfg_param.max_xri,
18093 				phba->sli4_hba.max_cfg_param.xri_used);
18094 	return xri_index;
18095 }
18096 
18097 /**
18098  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
18099  * @phba: pointer to lpfc hba data structure.
18100  * @post_sgl_list: pointer to els sgl entry list.
18101  * @post_cnt: number of els sgl entries on the list.
18102  *
18103  * This routine is invoked to post a block of driver's sgl pages to the
18104  * HBA using non-embedded mailbox command. No Lock is held. This routine
18105  * is only called when the driver is loading and after all IO has been
18106  * stopped.
18107  **/
18108 static int
lpfc_sli4_post_sgl_list(struct lpfc_hba * phba,struct list_head * post_sgl_list,int post_cnt)18109 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
18110 			    struct list_head *post_sgl_list,
18111 			    int post_cnt)
18112 {
18113 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
18114 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18115 	struct sgl_page_pairs *sgl_pg_pairs;
18116 	void *viraddr;
18117 	LPFC_MBOXQ_t *mbox;
18118 	uint32_t reqlen, alloclen, pg_pairs;
18119 	uint32_t mbox_tmo;
18120 	uint16_t xritag_start = 0;
18121 	int rc = 0;
18122 	uint32_t shdr_status, shdr_add_status;
18123 	union lpfc_sli4_cfg_shdr *shdr;
18124 
18125 	reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
18126 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18127 	if (reqlen > SLI4_PAGE_SIZE) {
18128 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18129 				"2559 Block sgl registration required DMA "
18130 				"size (%d) great than a page\n", reqlen);
18131 		return -ENOMEM;
18132 	}
18133 
18134 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18135 	if (!mbox)
18136 		return -ENOMEM;
18137 
18138 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18139 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18140 			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18141 			 LPFC_SLI4_MBX_NEMBED);
18142 
18143 	if (alloclen < reqlen) {
18144 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18145 				"0285 Allocated DMA memory size (%d) is "
18146 				"less than the requested DMA memory "
18147 				"size (%d)\n", alloclen, reqlen);
18148 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18149 		return -ENOMEM;
18150 	}
18151 	/* Set up the SGL pages in the non-embedded DMA pages */
18152 	viraddr = mbox->sge_array->addr[0];
18153 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18154 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18155 
18156 	pg_pairs = 0;
18157 	list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
18158 		/* Set up the sge entry */
18159 		sgl_pg_pairs->sgl_pg0_addr_lo =
18160 				cpu_to_le32(putPaddrLow(sglq_entry->phys));
18161 		sgl_pg_pairs->sgl_pg0_addr_hi =
18162 				cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18163 		sgl_pg_pairs->sgl_pg1_addr_lo =
18164 				cpu_to_le32(putPaddrLow(0));
18165 		sgl_pg_pairs->sgl_pg1_addr_hi =
18166 				cpu_to_le32(putPaddrHigh(0));
18167 
18168 		/* Keep the first xritag on the list */
18169 		if (pg_pairs == 0)
18170 			xritag_start = sglq_entry->sli4_xritag;
18171 		sgl_pg_pairs++;
18172 		pg_pairs++;
18173 	}
18174 
18175 	/* Complete initialization and perform endian conversion. */
18176 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18177 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
18178 	sgl->word0 = cpu_to_le32(sgl->word0);
18179 
18180 	if (!phba->sli4_hba.intr_enable)
18181 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18182 	else {
18183 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18184 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18185 	}
18186 	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18187 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18188 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18189 	if (!phba->sli4_hba.intr_enable)
18190 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18191 	else if (rc != MBX_TIMEOUT)
18192 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18193 	if (shdr_status || shdr_add_status || rc) {
18194 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18195 				"2513 POST_SGL_BLOCK mailbox command failed "
18196 				"status x%x add_status x%x mbx status x%x\n",
18197 				shdr_status, shdr_add_status, rc);
18198 		rc = -ENXIO;
18199 	}
18200 	return rc;
18201 }
18202 
18203 /**
18204  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
18205  * @phba: pointer to lpfc hba data structure.
18206  * @nblist: pointer to nvme buffer list.
18207  * @count: number of scsi buffers on the list.
18208  *
18209  * This routine is invoked to post a block of @count scsi sgl pages from a
18210  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
18211  * No Lock is held.
18212  *
18213  **/
18214 static int
lpfc_sli4_post_io_sgl_block(struct lpfc_hba * phba,struct list_head * nblist,int count)18215 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18216 			    int count)
18217 {
18218 	struct lpfc_io_buf *lpfc_ncmd;
18219 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18220 	struct sgl_page_pairs *sgl_pg_pairs;
18221 	void *viraddr;
18222 	LPFC_MBOXQ_t *mbox;
18223 	uint32_t reqlen, alloclen, pg_pairs;
18224 	uint32_t mbox_tmo;
18225 	uint16_t xritag_start = 0;
18226 	int rc = 0;
18227 	uint32_t shdr_status, shdr_add_status;
18228 	dma_addr_t pdma_phys_bpl1;
18229 	union lpfc_sli4_cfg_shdr *shdr;
18230 
18231 	/* Calculate the requested length of the dma memory */
18232 	reqlen = count * sizeof(struct sgl_page_pairs) +
18233 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18234 	if (reqlen > SLI4_PAGE_SIZE) {
18235 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
18236 				"6118 Block sgl registration required DMA "
18237 				"size (%d) great than a page\n", reqlen);
18238 		return -ENOMEM;
18239 	}
18240 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18241 	if (!mbox) {
18242 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18243 				"6119 Failed to allocate mbox cmd memory\n");
18244 		return -ENOMEM;
18245 	}
18246 
18247 	/* Allocate DMA memory and set up the non-embedded mailbox command */
18248 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18249 				    LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18250 				    reqlen, LPFC_SLI4_MBX_NEMBED);
18251 
18252 	if (alloclen < reqlen) {
18253 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18254 				"6120 Allocated DMA memory size (%d) is "
18255 				"less than the requested DMA memory "
18256 				"size (%d)\n", alloclen, reqlen);
18257 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18258 		return -ENOMEM;
18259 	}
18260 
18261 	/* Get the first SGE entry from the non-embedded DMA memory */
18262 	viraddr = mbox->sge_array->addr[0];
18263 
18264 	/* Set up the SGL pages in the non-embedded DMA pages */
18265 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18266 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18267 
18268 	pg_pairs = 0;
18269 	list_for_each_entry(lpfc_ncmd, nblist, list) {
18270 		/* Set up the sge entry */
18271 		sgl_pg_pairs->sgl_pg0_addr_lo =
18272 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
18273 		sgl_pg_pairs->sgl_pg0_addr_hi =
18274 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
18275 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
18276 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18277 						SGL_PAGE_SIZE;
18278 		else
18279 			pdma_phys_bpl1 = 0;
18280 		sgl_pg_pairs->sgl_pg1_addr_lo =
18281 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18282 		sgl_pg_pairs->sgl_pg1_addr_hi =
18283 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18284 		/* Keep the first xritag on the list */
18285 		if (pg_pairs == 0)
18286 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
18287 		sgl_pg_pairs++;
18288 		pg_pairs++;
18289 	}
18290 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18291 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18292 	/* Perform endian conversion if necessary */
18293 	sgl->word0 = cpu_to_le32(sgl->word0);
18294 
18295 	if (!phba->sli4_hba.intr_enable) {
18296 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18297 	} else {
18298 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18299 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18300 	}
18301 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
18302 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18303 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18304 	if (!phba->sli4_hba.intr_enable)
18305 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18306 	else if (rc != MBX_TIMEOUT)
18307 		lpfc_sli4_mbox_cmd_free(phba, mbox);
18308 	if (shdr_status || shdr_add_status || rc) {
18309 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18310 				"6125 POST_SGL_BLOCK mailbox command failed "
18311 				"status x%x add_status x%x mbx status x%x\n",
18312 				shdr_status, shdr_add_status, rc);
18313 		rc = -ENXIO;
18314 	}
18315 	return rc;
18316 }
18317 
18318 /**
18319  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
18320  * @phba: pointer to lpfc hba data structure.
18321  * @post_nblist: pointer to the nvme buffer list.
18322  * @sb_count: number of nvme buffers.
18323  *
18324  * This routine walks a list of nvme buffers that was passed in. It attempts
18325  * to construct blocks of nvme buffer sgls which contains contiguous xris and
18326  * uses the non-embedded SGL block post mailbox commands to post to the port.
18327  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18328  * embedded SGL post mailbox command for posting. The @post_nblist passed in
18329  * must be local list, thus no lock is needed when manipulate the list.
18330  *
18331  * Returns: 0 = failure, non-zero number of successfully posted buffers.
18332  **/
18333 int
lpfc_sli4_post_io_sgl_list(struct lpfc_hba * phba,struct list_head * post_nblist,int sb_count)18334 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18335 			   struct list_head *post_nblist, int sb_count)
18336 {
18337 	struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
18338 	int status, sgl_size;
18339 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18340 	dma_addr_t pdma_phys_sgl1;
18341 	int last_xritag = NO_XRI;
18342 	int cur_xritag;
18343 	LIST_HEAD(prep_nblist);
18344 	LIST_HEAD(blck_nblist);
18345 	LIST_HEAD(nvme_nblist);
18346 
18347 	/* sanity check */
18348 	if (sb_count <= 0)
18349 		return -EINVAL;
18350 
18351 	sgl_size = phba->cfg_sg_dma_buf_size;
18352 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18353 		list_del_init(&lpfc_ncmd->list);
18354 		block_cnt++;
18355 		if ((last_xritag != NO_XRI) &&
18356 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18357 			/* a hole in xri block, form a sgl posting block */
18358 			list_splice_init(&prep_nblist, &blck_nblist);
18359 			post_cnt = block_cnt - 1;
18360 			/* prepare list for next posting block */
18361 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18362 			block_cnt = 1;
18363 		} else {
18364 			/* prepare list for next posting block */
18365 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18366 			/* enough sgls for non-embed sgl mbox command */
18367 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18368 				list_splice_init(&prep_nblist, &blck_nblist);
18369 				post_cnt = block_cnt;
18370 				block_cnt = 0;
18371 			}
18372 		}
18373 		num_posting++;
18374 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18375 
18376 		/* end of repost sgl list condition for NVME buffers */
18377 		if (num_posting == sb_count) {
18378 			if (post_cnt == 0) {
18379 				/* last sgl posting block */
18380 				list_splice_init(&prep_nblist, &blck_nblist);
18381 				post_cnt = block_cnt;
18382 			} else if (block_cnt == 1) {
18383 				/* last single sgl with non-contiguous xri */
18384 				if (sgl_size > SGL_PAGE_SIZE)
18385 					pdma_phys_sgl1 =
18386 						lpfc_ncmd->dma_phys_sgl +
18387 						SGL_PAGE_SIZE;
18388 				else
18389 					pdma_phys_sgl1 = 0;
18390 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18391 				status = lpfc_sli4_post_sgl(
18392 						phba, lpfc_ncmd->dma_phys_sgl,
18393 						pdma_phys_sgl1, cur_xritag);
18394 				if (status) {
18395 					/* Post error.  Buffer unavailable. */
18396 					lpfc_ncmd->flags |=
18397 						LPFC_SBUF_NOT_POSTED;
18398 				} else {
18399 					/* Post success. Bffer available. */
18400 					lpfc_ncmd->flags &=
18401 						~LPFC_SBUF_NOT_POSTED;
18402 					lpfc_ncmd->status = IOSTAT_SUCCESS;
18403 					num_posted++;
18404 				}
18405 				/* success, put on NVME buffer sgl list */
18406 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18407 			}
18408 		}
18409 
18410 		/* continue until a nembed page worth of sgls */
18411 		if (post_cnt == 0)
18412 			continue;
18413 
18414 		/* post block of NVME buffer list sgls */
18415 		status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18416 						     post_cnt);
18417 
18418 		/* don't reset xirtag due to hole in xri block */
18419 		if (block_cnt == 0)
18420 			last_xritag = NO_XRI;
18421 
18422 		/* reset NVME buffer post count for next round of posting */
18423 		post_cnt = 0;
18424 
18425 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18426 		while (!list_empty(&blck_nblist)) {
18427 			list_remove_head(&blck_nblist, lpfc_ncmd,
18428 					 struct lpfc_io_buf, list);
18429 			if (status) {
18430 				/* Post error.  Mark buffer unavailable. */
18431 				lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
18432 			} else {
18433 				/* Post success, Mark buffer available. */
18434 				lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
18435 				lpfc_ncmd->status = IOSTAT_SUCCESS;
18436 				num_posted++;
18437 			}
18438 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18439 		}
18440 	}
18441 	/* Push NVME buffers with sgl posted to the available list */
18442 	lpfc_io_buf_replenish(phba, &nvme_nblist);
18443 
18444 	return num_posted;
18445 }
18446 
18447 /**
18448  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18449  * @phba: pointer to lpfc_hba struct that the frame was received on
18450  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18451  *
18452  * This function checks the fields in the @fc_hdr to see if the FC frame is a
18453  * valid type of frame that the LPFC driver will handle. This function will
18454  * return a zero if the frame is a valid frame or a non zero value when the
18455  * frame does not pass the check.
18456  **/
18457 static int
lpfc_fc_frame_check(struct lpfc_hba * phba,struct fc_frame_header * fc_hdr)18458 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18459 {
18460 	/*  make rctl_names static to save stack space */
18461 	struct fc_vft_header *fc_vft_hdr;
18462 	struct fc_app_header *fc_app_hdr;
18463 	uint32_t *header = (uint32_t *) fc_hdr;
18464 
18465 #define FC_RCTL_MDS_DIAGS	0xF4
18466 
18467 	switch (fc_hdr->fh_r_ctl) {
18468 	case FC_RCTL_DD_UNCAT:		/* uncategorized information */
18469 	case FC_RCTL_DD_SOL_DATA:	/* solicited data */
18470 	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */
18471 	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */
18472 	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */
18473 	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */
18474 	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */
18475 	case FC_RCTL_DD_CMD_STATUS:	/* command status */
18476 	case FC_RCTL_ELS_REQ:	/* extended link services request */
18477 	case FC_RCTL_ELS_REP:	/* extended link services reply */
18478 	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */
18479 	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */
18480 	case FC_RCTL_BA_ABTS: 	/* basic link service abort */
18481 	case FC_RCTL_BA_RMC: 	/* remove connection */
18482 	case FC_RCTL_BA_ACC:	/* basic accept */
18483 	case FC_RCTL_BA_RJT:	/* basic reject */
18484 	case FC_RCTL_BA_PRMT:
18485 	case FC_RCTL_ACK_1:	/* acknowledge_1 */
18486 	case FC_RCTL_ACK_0:	/* acknowledge_0 */
18487 	case FC_RCTL_P_RJT:	/* port reject */
18488 	case FC_RCTL_F_RJT:	/* fabric reject */
18489 	case FC_RCTL_P_BSY:	/* port busy */
18490 	case FC_RCTL_F_BSY:	/* fabric busy to data frame */
18491 	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */
18492 	case FC_RCTL_LCR:	/* link credit reset */
18493 	case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
18494 	case FC_RCTL_END:	/* end */
18495 		break;
18496 	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */
18497 		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18498 		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18499 		return lpfc_fc_frame_check(phba, fc_hdr);
18500 	case FC_RCTL_BA_NOP:	/* basic link service NOP */
18501 	default:
18502 		goto drop;
18503 	}
18504 
18505 	switch (fc_hdr->fh_type) {
18506 	case FC_TYPE_BLS:
18507 	case FC_TYPE_ELS:
18508 	case FC_TYPE_FCP:
18509 	case FC_TYPE_CT:
18510 	case FC_TYPE_NVME:
18511 		break;
18512 	case FC_TYPE_IP:
18513 	case FC_TYPE_ILS:
18514 	default:
18515 		goto drop;
18516 	}
18517 
18518 	if (unlikely(phba->link_flag == LS_LOOPBACK_MODE &&
18519 				phba->cfg_vmid_app_header)) {
18520 		/* Application header is 16B device header */
18521 		if (fc_hdr->fh_df_ctl & LPFC_FC_16B_DEVICE_HEADER) {
18522 			fc_app_hdr = (struct fc_app_header *) (fc_hdr + 1);
18523 			if (be32_to_cpu(fc_app_hdr->src_app_id) !=
18524 					LOOPBACK_SRC_APPID) {
18525 				lpfc_printf_log(phba, KERN_WARNING,
18526 						LOG_ELS | LOG_LIBDFC,
18527 						"1932 Loopback src app id "
18528 						"not matched, app_id:x%x\n",
18529 						be32_to_cpu(fc_app_hdr->src_app_id));
18530 
18531 				goto drop;
18532 			}
18533 		} else {
18534 			lpfc_printf_log(phba, KERN_WARNING,
18535 					LOG_ELS | LOG_LIBDFC,
18536 					"1933 Loopback df_ctl bit not set, "
18537 					"df_ctl:x%x\n",
18538 					fc_hdr->fh_df_ctl);
18539 
18540 			goto drop;
18541 		}
18542 	}
18543 
18544 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
18545 			"2538 Received frame rctl:x%x, type:x%x, "
18546 			"frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
18547 			fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18548 			be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18549 			be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18550 			be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18551 			be32_to_cpu(header[6]));
18552 	return 0;
18553 drop:
18554 	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
18555 			"2539 Dropped frame rctl:x%x type:x%x\n",
18556 			fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18557 	return 1;
18558 }
18559 
18560 /**
18561  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18562  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18563  *
18564  * This function processes the FC header to retrieve the VFI from the VF
18565  * header, if one exists. This function will return the VFI if one exists
18566  * or 0 if no VSAN Header exists.
18567  **/
18568 static uint32_t
lpfc_fc_hdr_get_vfi(struct fc_frame_header * fc_hdr)18569 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18570 {
18571 	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18572 
18573 	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18574 		return 0;
18575 	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18576 }
18577 
18578 /**
18579  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18580  * @phba: Pointer to the HBA structure to search for the vport on
18581  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18582  * @fcfi: The FC Fabric ID that the frame came from
18583  * @did: Destination ID to match against
18584  *
18585  * This function searches the @phba for a vport that matches the content of the
18586  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18587  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18588  * returns the matching vport pointer or NULL if unable to match frame to a
18589  * vport.
18590  **/
18591 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)18592 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
18593 		       uint16_t fcfi, uint32_t did)
18594 {
18595 	struct lpfc_vport **vports;
18596 	struct lpfc_vport *vport = NULL;
18597 	int i;
18598 
18599 	if (did == Fabric_DID)
18600 		return phba->pport;
18601 	if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
18602 	    phba->link_state != LPFC_HBA_READY)
18603 		return phba->pport;
18604 
18605 	vports = lpfc_create_vport_work_array(phba);
18606 	if (vports != NULL) {
18607 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18608 			if (phba->fcf.fcfi == fcfi &&
18609 			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18610 			    vports[i]->fc_myDID == did) {
18611 				vport = vports[i];
18612 				break;
18613 			}
18614 		}
18615 	}
18616 	lpfc_destroy_vport_work_array(phba, vports);
18617 	return vport;
18618 }
18619 
18620 /**
18621  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18622  * @vport: The vport to work on.
18623  *
18624  * This function updates the receive sequence time stamp for this vport. The
18625  * receive sequence time stamp indicates the time that the last frame of the
18626  * the sequence that has been idle for the longest amount of time was received.
18627  * the driver uses this time stamp to indicate if any received sequences have
18628  * timed out.
18629  **/
18630 static void
lpfc_update_rcv_time_stamp(struct lpfc_vport * vport)18631 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18632 {
18633 	struct lpfc_dmabuf *h_buf;
18634 	struct hbq_dmabuf *dmabuf = NULL;
18635 
18636 	/* get the oldest sequence on the rcv list */
18637 	h_buf = list_get_first(&vport->rcv_buffer_list,
18638 			       struct lpfc_dmabuf, list);
18639 	if (!h_buf)
18640 		return;
18641 	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18642 	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18643 }
18644 
18645 /**
18646  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18647  * @vport: The vport that the received sequences were sent to.
18648  *
18649  * This function cleans up all outstanding received sequences. This is called
18650  * by the driver when a link event or user action invalidates all the received
18651  * sequences.
18652  **/
18653 void
lpfc_cleanup_rcv_buffers(struct lpfc_vport * vport)18654 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18655 {
18656 	struct lpfc_dmabuf *h_buf, *hnext;
18657 	struct lpfc_dmabuf *d_buf, *dnext;
18658 	struct hbq_dmabuf *dmabuf = NULL;
18659 
18660 	/* start with the oldest sequence on the rcv list */
18661 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18662 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18663 		list_del_init(&dmabuf->hbuf.list);
18664 		list_for_each_entry_safe(d_buf, dnext,
18665 					 &dmabuf->dbuf.list, list) {
18666 			list_del_init(&d_buf->list);
18667 			lpfc_in_buf_free(vport->phba, d_buf);
18668 		}
18669 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18670 	}
18671 }
18672 
18673 /**
18674  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18675  * @vport: The vport that the received sequences were sent to.
18676  *
18677  * This function determines whether any received sequences have timed out by
18678  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18679  * indicates that there is at least one timed out sequence this routine will
18680  * go through the received sequences one at a time from most inactive to most
18681  * active to determine which ones need to be cleaned up. Once it has determined
18682  * that a sequence needs to be cleaned up it will simply free up the resources
18683  * without sending an abort.
18684  **/
18685 void
lpfc_rcv_seq_check_edtov(struct lpfc_vport * vport)18686 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18687 {
18688 	struct lpfc_dmabuf *h_buf, *hnext;
18689 	struct lpfc_dmabuf *d_buf, *dnext;
18690 	struct hbq_dmabuf *dmabuf = NULL;
18691 	unsigned long timeout;
18692 	int abort_count = 0;
18693 
18694 	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18695 		   vport->rcv_buffer_time_stamp);
18696 	if (list_empty(&vport->rcv_buffer_list) ||
18697 	    time_before(jiffies, timeout))
18698 		return;
18699 	/* start with the oldest sequence on the rcv list */
18700 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18701 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18702 		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18703 			   dmabuf->time_stamp);
18704 		if (time_before(jiffies, timeout))
18705 			break;
18706 		abort_count++;
18707 		list_del_init(&dmabuf->hbuf.list);
18708 		list_for_each_entry_safe(d_buf, dnext,
18709 					 &dmabuf->dbuf.list, list) {
18710 			list_del_init(&d_buf->list);
18711 			lpfc_in_buf_free(vport->phba, d_buf);
18712 		}
18713 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18714 	}
18715 	if (abort_count)
18716 		lpfc_update_rcv_time_stamp(vport);
18717 }
18718 
18719 /**
18720  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
18721  * @vport: pointer to a vitural port
18722  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18723  *
18724  * This function searches through the existing incomplete sequences that have
18725  * been sent to this @vport. If the frame matches one of the incomplete
18726  * sequences then the dbuf in the @dmabuf is added to the list of frames that
18727  * make up that sequence. If no sequence is found that matches this frame then
18728  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18729  * This function returns a pointer to the first dmabuf in the sequence list that
18730  * the frame was linked to.
18731  **/
18732 static struct hbq_dmabuf *
lpfc_fc_frame_add(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18733 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18734 {
18735 	struct fc_frame_header *new_hdr;
18736 	struct fc_frame_header *temp_hdr;
18737 	struct lpfc_dmabuf *d_buf;
18738 	struct lpfc_dmabuf *h_buf;
18739 	struct hbq_dmabuf *seq_dmabuf = NULL;
18740 	struct hbq_dmabuf *temp_dmabuf = NULL;
18741 	uint8_t	found = 0;
18742 
18743 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18744 	dmabuf->time_stamp = jiffies;
18745 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18746 
18747 	/* Use the hdr_buf to find the sequence that this frame belongs to */
18748 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18749 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18750 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18751 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18752 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18753 			continue;
18754 		/* found a pending sequence that matches this frame */
18755 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18756 		break;
18757 	}
18758 	if (!seq_dmabuf) {
18759 		/*
18760 		 * This indicates first frame received for this sequence.
18761 		 * Queue the buffer on the vport's rcv_buffer_list.
18762 		 */
18763 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18764 		lpfc_update_rcv_time_stamp(vport);
18765 		return dmabuf;
18766 	}
18767 	temp_hdr = seq_dmabuf->hbuf.virt;
18768 	if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18769 		be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18770 		list_del_init(&seq_dmabuf->hbuf.list);
18771 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18772 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18773 		lpfc_update_rcv_time_stamp(vport);
18774 		return dmabuf;
18775 	}
18776 	/* move this sequence to the tail to indicate a young sequence */
18777 	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18778 	seq_dmabuf->time_stamp = jiffies;
18779 	lpfc_update_rcv_time_stamp(vport);
18780 	if (list_empty(&seq_dmabuf->dbuf.list)) {
18781 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18782 		return seq_dmabuf;
18783 	}
18784 	/* find the correct place in the sequence to insert this frame */
18785 	d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18786 	while (!found) {
18787 		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18788 		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18789 		/*
18790 		 * If the frame's sequence count is greater than the frame on
18791 		 * the list then insert the frame right after this frame
18792 		 */
18793 		if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18794 			be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18795 			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
18796 			found = 1;
18797 			break;
18798 		}
18799 
18800 		if (&d_buf->list == &seq_dmabuf->dbuf.list)
18801 			break;
18802 		d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
18803 	}
18804 
18805 	if (found)
18806 		return seq_dmabuf;
18807 	return NULL;
18808 }
18809 
18810 /**
18811  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18812  * @vport: pointer to a vitural port
18813  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18814  *
18815  * This function tries to abort from the partially assembed sequence, described
18816  * by the information from basic abbort @dmabuf. It checks to see whether such
18817  * partially assembled sequence held by the driver. If so, it shall free up all
18818  * the frames from the partially assembled sequence.
18819  *
18820  * Return
18821  * true  -- if there is matching partially assembled sequence present and all
18822  *          the frames freed with the sequence;
18823  * false -- if there is no matching partially assembled sequence present so
18824  *          nothing got aborted in the lower layer driver
18825  **/
18826 static bool
lpfc_sli4_abort_partial_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18827 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18828 			    struct hbq_dmabuf *dmabuf)
18829 {
18830 	struct fc_frame_header *new_hdr;
18831 	struct fc_frame_header *temp_hdr;
18832 	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18833 	struct hbq_dmabuf *seq_dmabuf = NULL;
18834 
18835 	/* Use the hdr_buf to find the sequence that matches this frame */
18836 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18837 	INIT_LIST_HEAD(&dmabuf->hbuf.list);
18838 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18839 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18840 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18841 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18842 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18843 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18844 			continue;
18845 		/* found a pending sequence that matches this frame */
18846 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18847 		break;
18848 	}
18849 
18850 	/* Free up all the frames from the partially assembled sequence */
18851 	if (seq_dmabuf) {
18852 		list_for_each_entry_safe(d_buf, n_buf,
18853 					 &seq_dmabuf->dbuf.list, list) {
18854 			list_del_init(&d_buf->list);
18855 			lpfc_in_buf_free(vport->phba, d_buf);
18856 		}
18857 		return true;
18858 	}
18859 	return false;
18860 }
18861 
18862 /**
18863  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18864  * @vport: pointer to a vitural port
18865  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18866  *
18867  * This function tries to abort from the assembed sequence from upper level
18868  * protocol, described by the information from basic abbort @dmabuf. It
18869  * checks to see whether such pending context exists at upper level protocol.
18870  * If so, it shall clean up the pending context.
18871  *
18872  * Return
18873  * true  -- if there is matching pending context of the sequence cleaned
18874  *          at ulp;
18875  * false -- if there is no matching pending context of the sequence present
18876  *          at ulp.
18877  **/
18878 static bool
lpfc_sli4_abort_ulp_seq(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)18879 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18880 {
18881 	struct lpfc_hba *phba = vport->phba;
18882 	int handled;
18883 
18884 	/* Accepting abort at ulp with SLI4 only */
18885 	if (phba->sli_rev < LPFC_SLI_REV4)
18886 		return false;
18887 
18888 	/* Register all caring upper level protocols to attend abort */
18889 	handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18890 	if (handled)
18891 		return true;
18892 
18893 	return false;
18894 }
18895 
18896 /**
18897  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18898  * @phba: Pointer to HBA context object.
18899  * @cmd_iocbq: pointer to the command iocbq structure.
18900  * @rsp_iocbq: pointer to the response iocbq structure.
18901  *
18902  * This function handles the sequence abort response iocb command complete
18903  * event. It properly releases the memory allocated to the sequence abort
18904  * accept iocb.
18905  **/
18906 static void
lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmd_iocbq,struct lpfc_iocbq * rsp_iocbq)18907 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18908 			     struct lpfc_iocbq *cmd_iocbq,
18909 			     struct lpfc_iocbq *rsp_iocbq)
18910 {
18911 	if (cmd_iocbq) {
18912 		lpfc_nlp_put(cmd_iocbq->ndlp);
18913 		lpfc_sli_release_iocbq(phba, cmd_iocbq);
18914 	}
18915 
18916 	/* Failure means BLS ABORT RSP did not get delivered to remote node*/
18917 	if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18918 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18919 			"3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18920 			get_job_ulpstatus(phba, rsp_iocbq),
18921 			get_job_word4(phba, rsp_iocbq));
18922 }
18923 
18924 /**
18925  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18926  * @phba: Pointer to HBA context object.
18927  * @xri: xri id in transaction.
18928  *
18929  * This function validates the xri maps to the known range of XRIs allocated an
18930  * used by the driver.
18931  **/
18932 uint16_t
lpfc_sli4_xri_inrange(struct lpfc_hba * phba,uint16_t xri)18933 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18934 		      uint16_t xri)
18935 {
18936 	uint16_t i;
18937 
18938 	for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18939 		if (xri == phba->sli4_hba.xri_ids[i])
18940 			return i;
18941 	}
18942 	return NO_XRI;
18943 }
18944 
18945 /**
18946  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
18947  * @vport: pointer to a virtual port.
18948  * @fc_hdr: pointer to a FC frame header.
18949  * @aborted: was the partially assembled receive sequence successfully aborted
18950  *
18951  * This function sends a basic response to a previous unsol sequence abort
18952  * event after aborting the sequence handling.
18953  **/
18954 void
lpfc_sli4_seq_abort_rsp(struct lpfc_vport * vport,struct fc_frame_header * fc_hdr,bool aborted)18955 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18956 			struct fc_frame_header *fc_hdr, bool aborted)
18957 {
18958 	struct lpfc_hba *phba = vport->phba;
18959 	struct lpfc_iocbq *ctiocb = NULL;
18960 	struct lpfc_nodelist *ndlp;
18961 	uint16_t oxid, rxid, xri, lxri;
18962 	uint32_t sid, fctl;
18963 	union lpfc_wqe128 *icmd;
18964 	int rc;
18965 
18966 	if (!lpfc_is_link_up(phba))
18967 		return;
18968 
18969 	sid = sli4_sid_from_fc_hdr(fc_hdr);
18970 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
18971 	rxid = be16_to_cpu(fc_hdr->fh_rx_id);
18972 
18973 	ndlp = lpfc_findnode_did(vport, sid);
18974 	if (!ndlp) {
18975 		ndlp = lpfc_nlp_init(vport, sid);
18976 		if (!ndlp) {
18977 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18978 					 "1268 Failed to allocate ndlp for "
18979 					 "oxid:x%x SID:x%x\n", oxid, sid);
18980 			return;
18981 		}
18982 		/* Put ndlp onto vport node list */
18983 		lpfc_enqueue_node(vport, ndlp);
18984 	}
18985 
18986 	/* Allocate buffer for rsp iocb */
18987 	ctiocb = lpfc_sli_get_iocbq(phba);
18988 	if (!ctiocb)
18989 		return;
18990 
18991 	icmd = &ctiocb->wqe;
18992 
18993 	/* Extract the F_CTL field from FC_HDR */
18994 	fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18995 
18996 	ctiocb->ndlp = lpfc_nlp_get(ndlp);
18997 	if (!ctiocb->ndlp) {
18998 		lpfc_sli_release_iocbq(phba, ctiocb);
18999 		return;
19000 	}
19001 
19002 	ctiocb->vport = vport;
19003 	ctiocb->cmd_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
19004 	ctiocb->sli4_lxritag = NO_XRI;
19005 	ctiocb->sli4_xritag = NO_XRI;
19006 	ctiocb->abort_rctl = FC_RCTL_BA_ACC;
19007 
19008 	if (fctl & FC_FC_EX_CTX)
19009 		/* Exchange responder sent the abort so we
19010 		 * own the oxid.
19011 		 */
19012 		xri = oxid;
19013 	else
19014 		xri = rxid;
19015 	lxri = lpfc_sli4_xri_inrange(phba, xri);
19016 	if (lxri != NO_XRI)
19017 		lpfc_set_rrq_active(phba, ndlp, lxri,
19018 			(xri == oxid) ? rxid : oxid, 0);
19019 	/* For BA_ABTS from exchange responder, if the logical xri with
19020 	 * the oxid maps to the FCP XRI range, the port no longer has
19021 	 * that exchange context, send a BLS_RJT. Override the IOCB for
19022 	 * a BA_RJT.
19023 	 */
19024 	if ((fctl & FC_FC_EX_CTX) &&
19025 	    (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
19026 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19027 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19028 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19029 		       FC_BA_RJT_INV_XID);
19030 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19031 		       FC_BA_RJT_UNABLE);
19032 	}
19033 
19034 	/* If BA_ABTS failed to abort a partially assembled receive sequence,
19035 	 * the driver no longer has that exchange, send a BLS_RJT. Override
19036 	 * the IOCB for a BA_RJT.
19037 	 */
19038 	if (aborted == false) {
19039 		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19040 		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19041 		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19042 		       FC_BA_RJT_INV_XID);
19043 		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19044 		       FC_BA_RJT_UNABLE);
19045 	}
19046 
19047 	if (fctl & FC_FC_EX_CTX) {
19048 		/* ABTS sent by responder to CT exchange, construction
19049 		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
19050 		 * field and RX_ID from ABTS for RX_ID field.
19051 		 */
19052 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_RSP;
19053 		bf_set(xmit_bls_rsp64_rxid, &icmd->xmit_bls_rsp, rxid);
19054 	} else {
19055 		/* ABTS sent by initiator to CT exchange, construction
19056 		 * of BA_ACC will need to allocate a new XRI as for the
19057 		 * XRI_TAG field.
19058 		 */
19059 		ctiocb->abort_bls = LPFC_ABTS_UNSOL_INT;
19060 	}
19061 
19062 	/* OX_ID is invariable to who sent ABTS to CT exchange */
19063 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, oxid);
19064 	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, rxid);
19065 
19066 	/* Use CT=VPI */
19067 	bf_set(wqe_els_did, &icmd->xmit_bls_rsp.wqe_dest,
19068 	       ndlp->nlp_DID);
19069 	bf_set(xmit_bls_rsp64_temprpi, &icmd->xmit_bls_rsp,
19070 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
19071 	bf_set(wqe_cmnd, &icmd->generic.wqe_com, CMD_XMIT_BLS_RSP64_CX);
19072 
19073 	/* Xmit CT abts response on exchange <xid> */
19074 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
19075 			 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
19076 			 ctiocb->abort_rctl, oxid, phba->link_state);
19077 
19078 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
19079 	if (rc == IOCB_ERROR) {
19080 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19081 				 "2925 Failed to issue CT ABTS RSP x%x on "
19082 				 "xri x%x, Data x%x\n",
19083 				 ctiocb->abort_rctl, oxid,
19084 				 phba->link_state);
19085 		lpfc_nlp_put(ndlp);
19086 		ctiocb->ndlp = NULL;
19087 		lpfc_sli_release_iocbq(phba, ctiocb);
19088 	}
19089 
19090 	/* if only usage of this nodelist is BLS response, release initial ref
19091 	 * to free ndlp when transmit completes
19092 	 */
19093 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE &&
19094 	    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
19095 	    !(ndlp->fc4_xpt_flags & (NVME_XPT_REGD | SCSI_XPT_REGD))) {
19096 		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
19097 		lpfc_nlp_put(ndlp);
19098 	}
19099 }
19100 
19101 /**
19102  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
19103  * @vport: Pointer to the vport on which this sequence was received
19104  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19105  *
19106  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
19107  * receive sequence is only partially assembed by the driver, it shall abort
19108  * the partially assembled frames for the sequence. Otherwise, if the
19109  * unsolicited receive sequence has been completely assembled and passed to
19110  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
19111  * unsolicited sequence has been aborted. After that, it will issue a basic
19112  * accept to accept the abort.
19113  **/
19114 static void
lpfc_sli4_handle_unsol_abort(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19115 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
19116 			     struct hbq_dmabuf *dmabuf)
19117 {
19118 	struct lpfc_hba *phba = vport->phba;
19119 	struct fc_frame_header fc_hdr;
19120 	uint32_t fctl;
19121 	bool aborted;
19122 
19123 	/* Make a copy of fc_hdr before the dmabuf being released */
19124 	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
19125 	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
19126 
19127 	if (fctl & FC_FC_EX_CTX) {
19128 		/* ABTS by responder to exchange, no cleanup needed */
19129 		aborted = true;
19130 	} else {
19131 		/* ABTS by initiator to exchange, need to do cleanup */
19132 		aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19133 		if (aborted == false)
19134 			aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
19135 	}
19136 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19137 
19138 	if (phba->nvmet_support) {
19139 		lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19140 		return;
19141 	}
19142 
19143 	/* Respond with BA_ACC or BA_RJT accordingly */
19144 	lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
19145 }
19146 
19147 /**
19148  * lpfc_seq_complete - Indicates if a sequence is complete
19149  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19150  *
19151  * This function checks the sequence, starting with the frame described by
19152  * @dmabuf, to see if all the frames associated with this sequence are present.
19153  * the frames associated with this sequence are linked to the @dmabuf using the
19154  * dbuf list. This function looks for two major things. 1) That the first frame
19155  * has a sequence count of zero. 2) There is a frame with last frame of sequence
19156  * set. 3) That there are no holes in the sequence count. The function will
19157  * return 1 when the sequence is complete, otherwise it will return 0.
19158  **/
19159 static int
lpfc_seq_complete(struct hbq_dmabuf * dmabuf)19160 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19161 {
19162 	struct fc_frame_header *hdr;
19163 	struct lpfc_dmabuf *d_buf;
19164 	struct hbq_dmabuf *seq_dmabuf;
19165 	uint32_t fctl;
19166 	int seq_count = 0;
19167 
19168 	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19169 	/* make sure first fame of sequence has a sequence count of zero */
19170 	if (hdr->fh_seq_cnt != seq_count)
19171 		return 0;
19172 	fctl = (hdr->fh_f_ctl[0] << 16 |
19173 		hdr->fh_f_ctl[1] << 8 |
19174 		hdr->fh_f_ctl[2]);
19175 	/* If last frame of sequence we can return success. */
19176 	if (fctl & FC_FC_END_SEQ)
19177 		return 1;
19178 	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19179 		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19180 		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19181 		/* If there is a hole in the sequence count then fail. */
19182 		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
19183 			return 0;
19184 		fctl = (hdr->fh_f_ctl[0] << 16 |
19185 			hdr->fh_f_ctl[1] << 8 |
19186 			hdr->fh_f_ctl[2]);
19187 		/* If last frame of sequence we can return success. */
19188 		if (fctl & FC_FC_END_SEQ)
19189 			return 1;
19190 	}
19191 	return 0;
19192 }
19193 
19194 /**
19195  * lpfc_prep_seq - Prep sequence for ULP processing
19196  * @vport: Pointer to the vport on which this sequence was received
19197  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
19198  *
19199  * This function takes a sequence, described by a list of frames, and creates
19200  * a list of iocbq structures to describe the sequence. This iocbq list will be
19201  * used to issue to the generic unsolicited sequence handler. This routine
19202  * returns a pointer to the first iocbq in the list. If the function is unable
19203  * to allocate an iocbq then it throw out the received frames that were not
19204  * able to be described and return a pointer to the first iocbq. If unable to
19205  * allocate any iocbqs (including the first) this function will return NULL.
19206  **/
19207 static struct lpfc_iocbq *
lpfc_prep_seq(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19208 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19209 {
19210 	struct hbq_dmabuf *hbq_buf;
19211 	struct lpfc_dmabuf *d_buf, *n_buf;
19212 	struct lpfc_iocbq *first_iocbq, *iocbq;
19213 	struct fc_frame_header *fc_hdr;
19214 	uint32_t sid;
19215 	uint32_t len, tot_len;
19216 
19217 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19218 	/* remove from receive buffer list */
19219 	list_del_init(&seq_dmabuf->hbuf.list);
19220 	lpfc_update_rcv_time_stamp(vport);
19221 	/* get the Remote Port's SID */
19222 	sid = sli4_sid_from_fc_hdr(fc_hdr);
19223 	tot_len = 0;
19224 	/* Get an iocbq struct to fill in. */
19225 	first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19226 	if (first_iocbq) {
19227 		/* Initialize the first IOCB. */
19228 		first_iocbq->wcqe_cmpl.total_data_placed = 0;
19229 		bf_set(lpfc_wcqe_c_status, &first_iocbq->wcqe_cmpl,
19230 		       IOSTAT_SUCCESS);
19231 		first_iocbq->vport = vport;
19232 
19233 		/* Check FC Header to see what TYPE of frame we are rcv'ing */
19234 		if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19235 			bf_set(els_rsp64_sid, &first_iocbq->wqe.xmit_els_rsp,
19236 			       sli4_did_from_fc_hdr(fc_hdr));
19237 		}
19238 
19239 		bf_set(wqe_ctxt_tag, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19240 		       NO_XRI);
19241 		bf_set(wqe_rcvoxid, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19242 		       be16_to_cpu(fc_hdr->fh_ox_id));
19243 
19244 		/* put the first buffer into the first iocb */
19245 		tot_len = bf_get(lpfc_rcqe_length,
19246 				 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19247 
19248 		first_iocbq->cmd_dmabuf = &seq_dmabuf->dbuf;
19249 		first_iocbq->bpl_dmabuf = NULL;
19250 		/* Keep track of the BDE count */
19251 		first_iocbq->wcqe_cmpl.word3 = 1;
19252 
19253 		if (tot_len > LPFC_DATA_BUF_SIZE)
19254 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize =
19255 				LPFC_DATA_BUF_SIZE;
19256 		else
19257 			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize = tot_len;
19258 
19259 		first_iocbq->wcqe_cmpl.total_data_placed = tot_len;
19260 		bf_set(wqe_els_did, &first_iocbq->wqe.xmit_els_rsp.wqe_dest,
19261 		       sid);
19262 	}
19263 	iocbq = first_iocbq;
19264 	/*
19265 	 * Each IOCBq can have two Buffers assigned, so go through the list
19266 	 * of buffers for this sequence and save two buffers in each IOCBq
19267 	 */
19268 	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19269 		if (!iocbq) {
19270 			lpfc_in_buf_free(vport->phba, d_buf);
19271 			continue;
19272 		}
19273 		if (!iocbq->bpl_dmabuf) {
19274 			iocbq->bpl_dmabuf = d_buf;
19275 			iocbq->wcqe_cmpl.word3++;
19276 			/* We need to get the size out of the right CQE */
19277 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19278 			len = bf_get(lpfc_rcqe_length,
19279 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19280 			iocbq->unsol_rcv_len = len;
19281 			iocbq->wcqe_cmpl.total_data_placed += len;
19282 			tot_len += len;
19283 		} else {
19284 			iocbq = lpfc_sli_get_iocbq(vport->phba);
19285 			if (!iocbq) {
19286 				if (first_iocbq) {
19287 					bf_set(lpfc_wcqe_c_status,
19288 					       &first_iocbq->wcqe_cmpl,
19289 					       IOSTAT_SUCCESS);
19290 					first_iocbq->wcqe_cmpl.parameter =
19291 						IOERR_NO_RESOURCES;
19292 				}
19293 				lpfc_in_buf_free(vport->phba, d_buf);
19294 				continue;
19295 			}
19296 			/* We need to get the size out of the right CQE */
19297 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19298 			len = bf_get(lpfc_rcqe_length,
19299 				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19300 			iocbq->cmd_dmabuf = d_buf;
19301 			iocbq->bpl_dmabuf = NULL;
19302 			iocbq->wcqe_cmpl.word3 = 1;
19303 
19304 			if (len > LPFC_DATA_BUF_SIZE)
19305 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19306 					LPFC_DATA_BUF_SIZE;
19307 			else
19308 				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19309 					len;
19310 
19311 			tot_len += len;
19312 			iocbq->wcqe_cmpl.total_data_placed = tot_len;
19313 			bf_set(wqe_els_did, &iocbq->wqe.xmit_els_rsp.wqe_dest,
19314 			       sid);
19315 			list_add_tail(&iocbq->list, &first_iocbq->list);
19316 		}
19317 	}
19318 	/* Free the sequence's header buffer */
19319 	if (!first_iocbq)
19320 		lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19321 
19322 	return first_iocbq;
19323 }
19324 
19325 static void
lpfc_sli4_send_seq_to_ulp(struct lpfc_vport * vport,struct hbq_dmabuf * seq_dmabuf)19326 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19327 			  struct hbq_dmabuf *seq_dmabuf)
19328 {
19329 	struct fc_frame_header *fc_hdr;
19330 	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19331 	struct lpfc_hba *phba = vport->phba;
19332 
19333 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19334 	iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19335 	if (!iocbq) {
19336 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19337 				"2707 Ring %d handler: Failed to allocate "
19338 				"iocb Rctl x%x Type x%x received\n",
19339 				LPFC_ELS_RING,
19340 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19341 		return;
19342 	}
19343 	if (!lpfc_complete_unsol_iocb(phba,
19344 				      phba->sli4_hba.els_wq->pring,
19345 				      iocbq, fc_hdr->fh_r_ctl,
19346 				      fc_hdr->fh_type)) {
19347 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19348 				"2540 Ring %d handler: unexpected Rctl "
19349 				"x%x Type x%x received\n",
19350 				LPFC_ELS_RING,
19351 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19352 		lpfc_in_buf_free(phba, &seq_dmabuf->dbuf);
19353 	}
19354 
19355 	/* Free iocb created in lpfc_prep_seq */
19356 	list_for_each_entry_safe(curr_iocb, next_iocb,
19357 				 &iocbq->list, list) {
19358 		list_del_init(&curr_iocb->list);
19359 		lpfc_sli_release_iocbq(phba, curr_iocb);
19360 	}
19361 	lpfc_sli_release_iocbq(phba, iocbq);
19362 }
19363 
19364 static void
lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)19365 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19366 			    struct lpfc_iocbq *rspiocb)
19367 {
19368 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
19369 
19370 	if (pcmd && pcmd->virt)
19371 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19372 	kfree(pcmd);
19373 	lpfc_sli_release_iocbq(phba, cmdiocb);
19374 	lpfc_drain_txq(phba);
19375 }
19376 
19377 static void
lpfc_sli4_handle_mds_loopback(struct lpfc_vport * vport,struct hbq_dmabuf * dmabuf)19378 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19379 			      struct hbq_dmabuf *dmabuf)
19380 {
19381 	struct fc_frame_header *fc_hdr;
19382 	struct lpfc_hba *phba = vport->phba;
19383 	struct lpfc_iocbq *iocbq = NULL;
19384 	union  lpfc_wqe128 *pwqe;
19385 	struct lpfc_dmabuf *pcmd = NULL;
19386 	uint32_t frame_len;
19387 	int rc;
19388 	unsigned long iflags;
19389 
19390 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19391 	frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19392 
19393 	/* Send the received frame back */
19394 	iocbq = lpfc_sli_get_iocbq(phba);
19395 	if (!iocbq) {
19396 		/* Queue cq event and wakeup worker thread to process it */
19397 		spin_lock_irqsave(&phba->hbalock, iflags);
19398 		list_add_tail(&dmabuf->cq_event.list,
19399 			      &phba->sli4_hba.sp_queue_event);
19400 		spin_unlock_irqrestore(&phba->hbalock, iflags);
19401 		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
19402 		lpfc_worker_wake_up(phba);
19403 		return;
19404 	}
19405 
19406 	/* Allocate buffer for command payload */
19407 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19408 	if (pcmd)
19409 		pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
19410 					    &pcmd->phys);
19411 	if (!pcmd || !pcmd->virt)
19412 		goto exit;
19413 
19414 	INIT_LIST_HEAD(&pcmd->list);
19415 
19416 	/* copyin the payload */
19417 	memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19418 
19419 	iocbq->cmd_dmabuf = pcmd;
19420 	iocbq->vport = vport;
19421 	iocbq->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
19422 	iocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
19423 	iocbq->num_bdes = 0;
19424 
19425 	pwqe = &iocbq->wqe;
19426 	/* fill in BDE's for command */
19427 	pwqe->gen_req.bde.addrHigh = putPaddrHigh(pcmd->phys);
19428 	pwqe->gen_req.bde.addrLow = putPaddrLow(pcmd->phys);
19429 	pwqe->gen_req.bde.tus.f.bdeSize = frame_len;
19430 	pwqe->gen_req.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
19431 
19432 	pwqe->send_frame.frame_len = frame_len;
19433 	pwqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((__be32 *)fc_hdr));
19434 	pwqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((__be32 *)fc_hdr + 1));
19435 	pwqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((__be32 *)fc_hdr + 2));
19436 	pwqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((__be32 *)fc_hdr + 3));
19437 	pwqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((__be32 *)fc_hdr + 4));
19438 	pwqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((__be32 *)fc_hdr + 5));
19439 
19440 	pwqe->generic.wqe_com.word7 = 0;
19441 	pwqe->generic.wqe_com.word10 = 0;
19442 
19443 	bf_set(wqe_cmnd, &pwqe->generic.wqe_com, CMD_SEND_FRAME);
19444 	bf_set(wqe_sof, &pwqe->generic.wqe_com, 0x2E); /* SOF byte */
19445 	bf_set(wqe_eof, &pwqe->generic.wqe_com, 0x41); /* EOF byte */
19446 	bf_set(wqe_lenloc, &pwqe->generic.wqe_com, 1);
19447 	bf_set(wqe_xbl, &pwqe->generic.wqe_com, 1);
19448 	bf_set(wqe_dbde, &pwqe->generic.wqe_com, 1);
19449 	bf_set(wqe_xc, &pwqe->generic.wqe_com, 1);
19450 	bf_set(wqe_cmd_type, &pwqe->generic.wqe_com, 0xA);
19451 	bf_set(wqe_cqid, &pwqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
19452 	bf_set(wqe_xri_tag, &pwqe->generic.wqe_com, iocbq->sli4_xritag);
19453 	bf_set(wqe_reqtag, &pwqe->generic.wqe_com, iocbq->iotag);
19454 	bf_set(wqe_class, &pwqe->generic.wqe_com, CLASS3);
19455 	pwqe->generic.wqe_com.abort_tag = iocbq->iotag;
19456 
19457 	iocbq->cmd_cmpl = lpfc_sli4_mds_loopback_cmpl;
19458 
19459 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19460 	if (rc == IOCB_ERROR)
19461 		goto exit;
19462 
19463 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19464 	return;
19465 
19466 exit:
19467 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19468 			"2023 Unable to process MDS loopback frame\n");
19469 	if (pcmd && pcmd->virt)
19470 		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19471 	kfree(pcmd);
19472 	if (iocbq)
19473 		lpfc_sli_release_iocbq(phba, iocbq);
19474 	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19475 }
19476 
19477 /**
19478  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19479  * @phba: Pointer to HBA context object.
19480  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
19481  *
19482  * This function is called with no lock held. This function processes all
19483  * the received buffers and gives it to upper layers when a received buffer
19484  * indicates that it is the final frame in the sequence. The interrupt
19485  * service routine processes received buffers at interrupt contexts.
19486  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19487  * appropriate receive function when the final frame in a sequence is received.
19488  **/
19489 void
lpfc_sli4_handle_received_buffer(struct lpfc_hba * phba,struct hbq_dmabuf * dmabuf)19490 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19491 				 struct hbq_dmabuf *dmabuf)
19492 {
19493 	struct hbq_dmabuf *seq_dmabuf;
19494 	struct fc_frame_header *fc_hdr;
19495 	struct lpfc_vport *vport;
19496 	uint32_t fcfi;
19497 	uint32_t did;
19498 
19499 	/* Process each received buffer */
19500 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19501 
19502 	if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19503 	    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19504 		vport = phba->pport;
19505 		/* Handle MDS Loopback frames */
19506 		if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
19507 			lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19508 		else
19509 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19510 		return;
19511 	}
19512 
19513 	/* check to see if this a valid type of frame */
19514 	if (lpfc_fc_frame_check(phba, fc_hdr)) {
19515 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19516 		return;
19517 	}
19518 
19519 	if ((bf_get(lpfc_cqe_code,
19520 		    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19521 		fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19522 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19523 	else
19524 		fcfi = bf_get(lpfc_rcqe_fcf_id,
19525 			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19526 
19527 	if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19528 		vport = phba->pport;
19529 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19530 				"2023 MDS Loopback %d bytes\n",
19531 				bf_get(lpfc_rcqe_length,
19532 				       &dmabuf->cq_event.cqe.rcqe_cmpl));
19533 		/* Handle MDS Loopback frames */
19534 		lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19535 		return;
19536 	}
19537 
19538 	/* d_id this frame is directed to */
19539 	did = sli4_did_from_fc_hdr(fc_hdr);
19540 
19541 	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
19542 	if (!vport) {
19543 		/* throw out the frame */
19544 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19545 		return;
19546 	}
19547 
19548 	/* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19549 	if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19550 		(did != Fabric_DID)) {
19551 		/*
19552 		 * Throw out the frame if we are not pt2pt.
19553 		 * The pt2pt protocol allows for discovery frames
19554 		 * to be received without a registered VPI.
19555 		 */
19556 		if (!test_bit(FC_PT2PT, &vport->fc_flag) ||
19557 		    phba->link_state == LPFC_HBA_READY) {
19558 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19559 			return;
19560 		}
19561 	}
19562 
19563 	/* Handle the basic abort sequence (BA_ABTS) event */
19564 	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19565 		lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19566 		return;
19567 	}
19568 
19569 	/* Link this frame */
19570 	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19571 	if (!seq_dmabuf) {
19572 		/* unable to add frame to vport - throw it out */
19573 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19574 		return;
19575 	}
19576 	/* If not last frame in sequence continue processing frames. */
19577 	if (!lpfc_seq_complete(seq_dmabuf))
19578 		return;
19579 
19580 	/* Send the complete sequence to the upper layer protocol */
19581 	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
19582 }
19583 
19584 /**
19585  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19586  * @phba: pointer to lpfc hba data structure.
19587  *
19588  * This routine is invoked to post rpi header templates to the
19589  * HBA consistent with the SLI-4 interface spec.  This routine
19590  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19591  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19592  *
19593  * This routine does not require any locks.  It's usage is expected
19594  * to be driver load or reset recovery when the driver is
19595  * sequential.
19596  *
19597  * Return codes
19598  * 	0 - successful
19599  *      -EIO - The mailbox failed to complete successfully.
19600  * 	When this error occurs, the driver is not guaranteed
19601  *	to have any rpi regions posted to the device and
19602  *	must either attempt to repost the regions or take a
19603  *	fatal error.
19604  **/
19605 int
lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba * phba)19606 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19607 {
19608 	struct lpfc_rpi_hdr *rpi_page;
19609 	uint32_t rc = 0;
19610 	uint16_t lrpi = 0;
19611 
19612 	/* SLI4 ports that support extents do not require RPI headers. */
19613 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19614 		goto exit;
19615 	if (phba->sli4_hba.extents_in_use)
19616 		return -EIO;
19617 
19618 	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
19619 		/*
19620 		 * Assign the rpi headers a physical rpi only if the driver
19621 		 * has not initialized those resources.  A port reset only
19622 		 * needs the headers posted.
19623 		 */
19624 		if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19625 		    LPFC_RPI_RSRC_RDY)
19626 			rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19627 
19628 		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19629 		if (rc != MBX_SUCCESS) {
19630 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19631 					"2008 Error %d posting all rpi "
19632 					"headers\n", rc);
19633 			rc = -EIO;
19634 			break;
19635 		}
19636 	}
19637 
19638  exit:
19639 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19640 	       LPFC_RPI_RSRC_RDY);
19641 	return rc;
19642 }
19643 
19644 /**
19645  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19646  * @phba: pointer to lpfc hba data structure.
19647  * @rpi_page:  pointer to the rpi memory region.
19648  *
19649  * This routine is invoked to post a single rpi header to the
19650  * HBA consistent with the SLI-4 interface spec.  This memory region
19651  * maps up to 64 rpi context regions.
19652  *
19653  * Return codes
19654  * 	0 - successful
19655  * 	-ENOMEM - No available memory
19656  *      -EIO - The mailbox failed to complete successfully.
19657  **/
19658 int
lpfc_sli4_post_rpi_hdr(struct lpfc_hba * phba,struct lpfc_rpi_hdr * rpi_page)19659 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19660 {
19661 	LPFC_MBOXQ_t *mboxq;
19662 	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19663 	uint32_t rc = 0;
19664 	uint32_t shdr_status, shdr_add_status;
19665 	union lpfc_sli4_cfg_shdr *shdr;
19666 
19667 	/* SLI4 ports that support extents do not require RPI headers. */
19668 	if (!phba->sli4_hba.rpi_hdrs_in_use)
19669 		return rc;
19670 	if (phba->sli4_hba.extents_in_use)
19671 		return -EIO;
19672 
19673 	/* The port is notified of the header region via a mailbox command. */
19674 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19675 	if (!mboxq) {
19676 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19677 				"2001 Unable to allocate memory for issuing "
19678 				"SLI_CONFIG_SPECIAL mailbox command\n");
19679 		return -ENOMEM;
19680 	}
19681 
19682 	/* Post all rpi memory regions to the port. */
19683 	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
19684 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19685 			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19686 			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
19687 			 sizeof(struct lpfc_sli4_cfg_mhdr),
19688 			 LPFC_SLI4_MBX_EMBED);
19689 
19690 
19691 	/* Post the physical rpi to the port for this rpi header. */
19692 	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19693 	       rpi_page->start_rpi);
19694 	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19695 	       hdr_tmpl, rpi_page->page_count);
19696 
19697 	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19698 	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
19699 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19700 	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19701 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19702 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19703 	mempool_free(mboxq, phba->mbox_mem_pool);
19704 	if (shdr_status || shdr_add_status || rc) {
19705 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19706 				"2514 POST_RPI_HDR mailbox failed with "
19707 				"status x%x add_status x%x, mbx status x%x\n",
19708 				shdr_status, shdr_add_status, rc);
19709 		rc = -ENXIO;
19710 	} else {
19711 		/*
19712 		 * The next_rpi stores the next logical module-64 rpi value used
19713 		 * to post physical rpis in subsequent rpi postings.
19714 		 */
19715 		spin_lock_irq(&phba->hbalock);
19716 		phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19717 		spin_unlock_irq(&phba->hbalock);
19718 	}
19719 	return rc;
19720 }
19721 
19722 /**
19723  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19724  * @phba: pointer to lpfc hba data structure.
19725  *
19726  * This routine is invoked to post rpi header templates to the
19727  * HBA consistent with the SLI-4 interface spec.  This routine
19728  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19729  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19730  *
19731  * Returns
19732  * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
19733  * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.
19734  **/
19735 int
lpfc_sli4_alloc_rpi(struct lpfc_hba * phba)19736 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19737 {
19738 	unsigned long rpi;
19739 	uint16_t max_rpi, rpi_limit;
19740 	uint16_t rpi_remaining, lrpi = 0;
19741 	struct lpfc_rpi_hdr *rpi_hdr;
19742 	unsigned long iflag;
19743 
19744 	/*
19745 	 * Fetch the next logical rpi.  Because this index is logical,
19746 	 * the  driver starts at 0 each time.
19747 	 */
19748 	spin_lock_irqsave(&phba->hbalock, iflag);
19749 	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19750 	rpi_limit = phba->sli4_hba.next_rpi;
19751 
19752 	rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);
19753 	if (rpi >= rpi_limit)
19754 		rpi = LPFC_RPI_ALLOC_ERROR;
19755 	else {
19756 		set_bit(rpi, phba->sli4_hba.rpi_bmask);
19757 		phba->sli4_hba.max_cfg_param.rpi_used++;
19758 		phba->sli4_hba.rpi_count++;
19759 	}
19760 	lpfc_printf_log(phba, KERN_INFO,
19761 			LOG_NODE | LOG_DISCOVERY,
19762 			"0001 Allocated rpi:x%x max:x%x lim:x%x\n",
19763 			(int) rpi, max_rpi, rpi_limit);
19764 
19765 	/*
19766 	 * Don't try to allocate more rpi header regions if the device limit
19767 	 * has been exhausted.
19768 	 */
19769 	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19770 	    (phba->sli4_hba.rpi_count >= max_rpi)) {
19771 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19772 		return rpi;
19773 	}
19774 
19775 	/*
19776 	 * RPI header postings are not required for SLI4 ports capable of
19777 	 * extents.
19778 	 */
19779 	if (!phba->sli4_hba.rpi_hdrs_in_use) {
19780 		spin_unlock_irqrestore(&phba->hbalock, iflag);
19781 		return rpi;
19782 	}
19783 
19784 	/*
19785 	 * If the driver is running low on rpi resources, allocate another
19786 	 * page now.  Note that the next_rpi value is used because
19787 	 * it represents how many are actually in use whereas max_rpi notes
19788 	 * how many are supported max by the device.
19789 	 */
19790 	rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
19791 	spin_unlock_irqrestore(&phba->hbalock, iflag);
19792 	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19793 		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19794 		if (!rpi_hdr) {
19795 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19796 					"2002 Error Could not grow rpi "
19797 					"count\n");
19798 		} else {
19799 			lrpi = rpi_hdr->start_rpi;
19800 			rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19801 			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19802 		}
19803 	}
19804 
19805 	return rpi;
19806 }
19807 
19808 /**
19809  * __lpfc_sli4_free_rpi - Release an rpi for reuse.
19810  * @phba: pointer to lpfc hba data structure.
19811  * @rpi: rpi to free
19812  *
19813  * This routine is invoked to release an rpi to the pool of
19814  * available rpis maintained by the driver.
19815  **/
19816 static void
__lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19817 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19818 {
19819 	/*
19820 	 * if the rpi value indicates a prior unreg has already
19821 	 * been done, skip the unreg.
19822 	 */
19823 	if (rpi == LPFC_RPI_ALLOC_ERROR)
19824 		return;
19825 
19826 	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19827 		phba->sli4_hba.rpi_count--;
19828 		phba->sli4_hba.max_cfg_param.rpi_used--;
19829 	} else {
19830 		lpfc_printf_log(phba, KERN_INFO,
19831 				LOG_NODE | LOG_DISCOVERY,
19832 				"2016 rpi %x not inuse\n",
19833 				rpi);
19834 	}
19835 }
19836 
19837 /**
19838  * lpfc_sli4_free_rpi - Release an rpi for reuse.
19839  * @phba: pointer to lpfc hba data structure.
19840  * @rpi: rpi to free
19841  *
19842  * This routine is invoked to release an rpi to the pool of
19843  * available rpis maintained by the driver.
19844  **/
19845 void
lpfc_sli4_free_rpi(struct lpfc_hba * phba,int rpi)19846 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19847 {
19848 	spin_lock_irq(&phba->hbalock);
19849 	__lpfc_sli4_free_rpi(phba, rpi);
19850 	spin_unlock_irq(&phba->hbalock);
19851 }
19852 
19853 /**
19854  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19855  * @phba: pointer to lpfc hba data structure.
19856  *
19857  * This routine is invoked to remove the memory region that
19858  * provided rpi via a bitmask.
19859  **/
19860 void
lpfc_sli4_remove_rpis(struct lpfc_hba * phba)19861 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19862 {
19863 	kfree(phba->sli4_hba.rpi_bmask);
19864 	kfree(phba->sli4_hba.rpi_ids);
19865 	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
19866 }
19867 
19868 /**
19869  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
19870  * @ndlp: pointer to lpfc nodelist data structure.
19871  * @cmpl: completion call-back.
19872  * @iocbq: data to load as mbox ctx_u information
19873  *
19874  * This routine is invoked to remove the memory region that
19875  * provided rpi via a bitmask.
19876  **/
19877 int
lpfc_sli4_resume_rpi(struct lpfc_nodelist * ndlp,void (* cmpl)(struct lpfc_hba *,LPFC_MBOXQ_t *),struct lpfc_iocbq * iocbq)19878 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19879 		     void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *),
19880 		     struct lpfc_iocbq *iocbq)
19881 {
19882 	LPFC_MBOXQ_t *mboxq;
19883 	struct lpfc_hba *phba = ndlp->phba;
19884 	int rc;
19885 
19886 	/* The port is notified of the header region via a mailbox command. */
19887 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19888 	if (!mboxq)
19889 		return -ENOMEM;
19890 
19891 	/* If cmpl assigned, then this nlp_get pairs with
19892 	 * lpfc_mbx_cmpl_resume_rpi.
19893 	 *
19894 	 * Else cmpl is NULL, then this nlp_get pairs with
19895 	 * lpfc_sli_def_mbox_cmpl.
19896 	 */
19897 	if (!lpfc_nlp_get(ndlp)) {
19898 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19899 				"2122 %s: Failed to get nlp ref\n",
19900 				__func__);
19901 		mempool_free(mboxq, phba->mbox_mem_pool);
19902 		return -EIO;
19903 	}
19904 
19905 	/* Post all rpi memory regions to the port. */
19906 	lpfc_resume_rpi(mboxq, ndlp);
19907 	if (cmpl) {
19908 		mboxq->mbox_cmpl = cmpl;
19909 		mboxq->ctx_u.save_iocb = iocbq;
19910 	} else
19911 		mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19912 	mboxq->ctx_ndlp = ndlp;
19913 	mboxq->vport = ndlp->vport;
19914 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19915 	if (rc == MBX_NOT_FINISHED) {
19916 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19917 				"2010 Resume RPI Mailbox failed "
19918 				"status %d, mbxStatus x%x\n", rc,
19919 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19920 		lpfc_nlp_put(ndlp);
19921 		mempool_free(mboxq, phba->mbox_mem_pool);
19922 		return -EIO;
19923 	}
19924 	return 0;
19925 }
19926 
19927 /**
19928  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19929  * @vport: Pointer to the vport for which the vpi is being initialized
19930  *
19931  * This routine is invoked to activate a vpi with the port.
19932  *
19933  * Returns:
19934  *    0 success
19935  *    -Evalue otherwise
19936  **/
19937 int
lpfc_sli4_init_vpi(struct lpfc_vport * vport)19938 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
19939 {
19940 	LPFC_MBOXQ_t *mboxq;
19941 	int rc = 0;
19942 	int retval = MBX_SUCCESS;
19943 	uint32_t mbox_tmo;
19944 	struct lpfc_hba *phba = vport->phba;
19945 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19946 	if (!mboxq)
19947 		return -ENOMEM;
19948 	lpfc_init_vpi(phba, mboxq, vport->vpi);
19949 	mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
19950 	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
19951 	if (rc != MBX_SUCCESS) {
19952 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19953 				"2022 INIT VPI Mailbox failed "
19954 				"status %d, mbxStatus x%x\n", rc,
19955 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19956 		retval = -EIO;
19957 	}
19958 	if (rc != MBX_TIMEOUT)
19959 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
19960 
19961 	return retval;
19962 }
19963 
19964 /**
19965  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19966  * @phba: pointer to lpfc hba data structure.
19967  * @mboxq: Pointer to mailbox object.
19968  *
19969  * This routine is invoked to manually add a single FCF record. The caller
19970  * must pass a completely initialized FCF_Record.  This routine takes
19971  * care of the nonembedded mailbox operations.
19972  **/
19973 static void
lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)19974 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19975 {
19976 	void *virt_addr;
19977 	union lpfc_sli4_cfg_shdr *shdr;
19978 	uint32_t shdr_status, shdr_add_status;
19979 
19980 	virt_addr = mboxq->sge_array->addr[0];
19981 	/* The IOCTL status is embedded in the mailbox subheader. */
19982 	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19983 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19984 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19985 
19986 	if ((shdr_status || shdr_add_status) &&
19987 		(shdr_status != STATUS_FCF_IN_USE))
19988 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19989 			"2558 ADD_FCF_RECORD mailbox failed with "
19990 			"status x%x add_status x%x\n",
19991 			shdr_status, shdr_add_status);
19992 
19993 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
19994 }
19995 
19996 /**
19997  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19998  * @phba: pointer to lpfc hba data structure.
19999  * @fcf_record:  pointer to the initialized fcf record to add.
20000  *
20001  * This routine is invoked to manually add a single FCF record. The caller
20002  * must pass a completely initialized FCF_Record.  This routine takes
20003  * care of the nonembedded mailbox operations.
20004  **/
20005 int
lpfc_sli4_add_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record)20006 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
20007 {
20008 	int rc = 0;
20009 	LPFC_MBOXQ_t *mboxq;
20010 	uint8_t *bytep;
20011 	void *virt_addr;
20012 	struct lpfc_mbx_sge sge;
20013 	uint32_t alloc_len, req_len;
20014 	uint32_t fcfindex;
20015 
20016 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20017 	if (!mboxq) {
20018 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20019 			"2009 Failed to allocate mbox for ADD_FCF cmd\n");
20020 		return -ENOMEM;
20021 	}
20022 
20023 	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
20024 		  sizeof(uint32_t);
20025 
20026 	/* Allocate DMA memory and set up the non-embedded mailbox command */
20027 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
20028 				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
20029 				     req_len, LPFC_SLI4_MBX_NEMBED);
20030 	if (alloc_len < req_len) {
20031 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20032 			"2523 Allocated DMA memory size (x%x) is "
20033 			"less than the requested DMA memory "
20034 			"size (x%x)\n", alloc_len, req_len);
20035 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20036 		return -ENOMEM;
20037 	}
20038 
20039 	/*
20040 	 * Get the first SGE entry from the non-embedded DMA memory.  This
20041 	 * routine only uses a single SGE.
20042 	 */
20043 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
20044 	virt_addr = mboxq->sge_array->addr[0];
20045 	/*
20046 	 * Configure the FCF record for FCFI 0.  This is the driver's
20047 	 * hardcoded default and gets used in nonFIP mode.
20048 	 */
20049 	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
20050 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
20051 	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
20052 
20053 	/*
20054 	 * Copy the fcf_index and the FCF Record Data. The data starts after
20055 	 * the FCoE header plus word10. The data copy needs to be endian
20056 	 * correct.
20057 	 */
20058 	bytep += sizeof(uint32_t);
20059 	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
20060 	mboxq->vport = phba->pport;
20061 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
20062 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20063 	if (rc == MBX_NOT_FINISHED) {
20064 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20065 			"2515 ADD_FCF_RECORD mailbox failed with "
20066 			"status 0x%x\n", rc);
20067 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20068 		rc = -EIO;
20069 	} else
20070 		rc = 0;
20071 
20072 	return rc;
20073 }
20074 
20075 /**
20076  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
20077  * @phba: pointer to lpfc hba data structure.
20078  * @fcf_record:  pointer to the fcf record to write the default data.
20079  * @fcf_index: FCF table entry index.
20080  *
20081  * This routine is invoked to build the driver's default FCF record.  The
20082  * values used are hardcoded.  This routine handles memory initialization.
20083  *
20084  **/
20085 void
lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba * phba,struct fcf_record * fcf_record,uint16_t fcf_index)20086 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
20087 				struct fcf_record *fcf_record,
20088 				uint16_t fcf_index)
20089 {
20090 	memset(fcf_record, 0, sizeof(struct fcf_record));
20091 	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
20092 	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
20093 	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
20094 	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
20095 	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
20096 	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
20097 	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
20098 	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
20099 	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
20100 	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
20101 	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
20102 	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
20103 	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
20104 	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
20105 	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
20106 	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
20107 		LPFC_FCF_FPMA | LPFC_FCF_SPMA);
20108 	/* Set the VLAN bit map */
20109 	if (phba->valid_vlan) {
20110 		fcf_record->vlan_bitmap[phba->vlan_id / 8]
20111 			= 1 << (phba->vlan_id % 8);
20112 	}
20113 }
20114 
20115 /**
20116  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
20117  * @phba: pointer to lpfc hba data structure.
20118  * @fcf_index: FCF table entry offset.
20119  *
20120  * This routine is invoked to scan the entire FCF table by reading FCF
20121  * record and processing it one at a time starting from the @fcf_index
20122  * for initial FCF discovery or fast FCF failover rediscovery.
20123  *
20124  * Return 0 if the mailbox command is submitted successfully, none 0
20125  * otherwise.
20126  **/
20127 int
lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20128 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20129 {
20130 	int rc = 0, error;
20131 	LPFC_MBOXQ_t *mboxq;
20132 
20133 	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
20134 	phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
20135 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20136 	if (!mboxq) {
20137 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20138 				"2000 Failed to allocate mbox for "
20139 				"READ_FCF cmd\n");
20140 		error = -ENOMEM;
20141 		goto fail_fcf_scan;
20142 	}
20143 	/* Construct the read FCF record mailbox command */
20144 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20145 	if (rc) {
20146 		error = -EINVAL;
20147 		goto fail_fcf_scan;
20148 	}
20149 	/* Issue the mailbox command asynchronously */
20150 	mboxq->vport = phba->pport;
20151 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
20152 
20153 	set_bit(FCF_TS_INPROG, &phba->hba_flag);
20154 
20155 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20156 	if (rc == MBX_NOT_FINISHED)
20157 		error = -EIO;
20158 	else {
20159 		/* Reset eligible FCF count for new scan */
20160 		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
20161 			phba->fcf.eligible_fcf_cnt = 0;
20162 		error = 0;
20163 	}
20164 fail_fcf_scan:
20165 	if (error) {
20166 		if (mboxq)
20167 			lpfc_sli4_mbox_cmd_free(phba, mboxq);
20168 		/* FCF scan failed, clear FCF_TS_INPROG flag */
20169 		clear_bit(FCF_TS_INPROG, &phba->hba_flag);
20170 	}
20171 	return error;
20172 }
20173 
20174 /**
20175  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
20176  * @phba: pointer to lpfc hba data structure.
20177  * @fcf_index: FCF table entry offset.
20178  *
20179  * This routine is invoked to read an FCF record indicated by @fcf_index
20180  * and to use it for FLOGI roundrobin FCF failover.
20181  *
20182  * Return 0 if the mailbox command is submitted successfully, none 0
20183  * otherwise.
20184  **/
20185 int
lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20186 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20187 {
20188 	int rc = 0, error;
20189 	LPFC_MBOXQ_t *mboxq;
20190 
20191 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20192 	if (!mboxq) {
20193 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20194 				"2763 Failed to allocate mbox for "
20195 				"READ_FCF cmd\n");
20196 		error = -ENOMEM;
20197 		goto fail_fcf_read;
20198 	}
20199 	/* Construct the read FCF record mailbox command */
20200 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20201 	if (rc) {
20202 		error = -EINVAL;
20203 		goto fail_fcf_read;
20204 	}
20205 	/* Issue the mailbox command asynchronously */
20206 	mboxq->vport = phba->pport;
20207 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20208 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20209 	if (rc == MBX_NOT_FINISHED)
20210 		error = -EIO;
20211 	else
20212 		error = 0;
20213 
20214 fail_fcf_read:
20215 	if (error && mboxq)
20216 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20217 	return error;
20218 }
20219 
20220 /**
20221  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20222  * @phba: pointer to lpfc hba data structure.
20223  * @fcf_index: FCF table entry offset.
20224  *
20225  * This routine is invoked to read an FCF record indicated by @fcf_index to
20226  * determine whether it's eligible for FLOGI roundrobin failover list.
20227  *
20228  * Return 0 if the mailbox command is submitted successfully, none 0
20229  * otherwise.
20230  **/
20231 int
lpfc_sli4_read_fcf_rec(struct lpfc_hba * phba,uint16_t fcf_index)20232 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20233 {
20234 	int rc = 0, error;
20235 	LPFC_MBOXQ_t *mboxq;
20236 
20237 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20238 	if (!mboxq) {
20239 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20240 				"2758 Failed to allocate mbox for "
20241 				"READ_FCF cmd\n");
20242 				error = -ENOMEM;
20243 				goto fail_fcf_read;
20244 	}
20245 	/* Construct the read FCF record mailbox command */
20246 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20247 	if (rc) {
20248 		error = -EINVAL;
20249 		goto fail_fcf_read;
20250 	}
20251 	/* Issue the mailbox command asynchronously */
20252 	mboxq->vport = phba->pport;
20253 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20254 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20255 	if (rc == MBX_NOT_FINISHED)
20256 		error = -EIO;
20257 	else
20258 		error = 0;
20259 
20260 fail_fcf_read:
20261 	if (error && mboxq)
20262 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20263 	return error;
20264 }
20265 
20266 /**
20267  * lpfc_check_next_fcf_pri_level
20268  * @phba: pointer to the lpfc_hba struct for this port.
20269  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20270  * routine when the rr_bmask is empty. The FCF indecies are put into the
20271  * rr_bmask based on their priority level. Starting from the highest priority
20272  * to the lowest. The most likely FCF candidate will be in the highest
20273  * priority group. When this routine is called it searches the fcf_pri list for
20274  * next lowest priority group and repopulates the rr_bmask with only those
20275  * fcf_indexes.
20276  * returns:
20277  * 1=success 0=failure
20278  **/
20279 static int
lpfc_check_next_fcf_pri_level(struct lpfc_hba * phba)20280 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20281 {
20282 	uint16_t next_fcf_pri;
20283 	uint16_t last_index;
20284 	struct lpfc_fcf_pri *fcf_pri;
20285 	int rc;
20286 	int ret = 0;
20287 
20288 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20289 			LPFC_SLI4_FCF_TBL_INDX_MAX);
20290 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20291 			"3060 Last IDX %d\n", last_index);
20292 
20293 	/* Verify the priority list has 2 or more entries */
20294 	spin_lock_irq(&phba->hbalock);
20295 	if (list_empty(&phba->fcf.fcf_pri_list) ||
20296 	    list_is_singular(&phba->fcf.fcf_pri_list)) {
20297 		spin_unlock_irq(&phba->hbalock);
20298 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20299 			"3061 Last IDX %d\n", last_index);
20300 		return 0; /* Empty rr list */
20301 	}
20302 	spin_unlock_irq(&phba->hbalock);
20303 
20304 	next_fcf_pri = 0;
20305 	/*
20306 	 * Clear the rr_bmask and set all of the bits that are at this
20307 	 * priority.
20308 	 */
20309 	memset(phba->fcf.fcf_rr_bmask, 0,
20310 			sizeof(*phba->fcf.fcf_rr_bmask));
20311 	spin_lock_irq(&phba->hbalock);
20312 	list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20313 		if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20314 			continue;
20315 		/*
20316 		 * the 1st priority that has not FLOGI failed
20317 		 * will be the highest.
20318 		 */
20319 		if (!next_fcf_pri)
20320 			next_fcf_pri = fcf_pri->fcf_rec.priority;
20321 		spin_unlock_irq(&phba->hbalock);
20322 		if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20323 			rc = lpfc_sli4_fcf_rr_index_set(phba,
20324 						fcf_pri->fcf_rec.fcf_index);
20325 			if (rc)
20326 				return 0;
20327 		}
20328 		spin_lock_irq(&phba->hbalock);
20329 	}
20330 	/*
20331 	 * if next_fcf_pri was not set above and the list is not empty then
20332 	 * we have failed flogis on all of them. So reset flogi failed
20333 	 * and start at the beginning.
20334 	 */
20335 	if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20336 		list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20337 			fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20338 			/*
20339 			 * the 1st priority that has not FLOGI failed
20340 			 * will be the highest.
20341 			 */
20342 			if (!next_fcf_pri)
20343 				next_fcf_pri = fcf_pri->fcf_rec.priority;
20344 			spin_unlock_irq(&phba->hbalock);
20345 			if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20346 				rc = lpfc_sli4_fcf_rr_index_set(phba,
20347 						fcf_pri->fcf_rec.fcf_index);
20348 				if (rc)
20349 					return 0;
20350 			}
20351 			spin_lock_irq(&phba->hbalock);
20352 		}
20353 	} else
20354 		ret = 1;
20355 	spin_unlock_irq(&phba->hbalock);
20356 
20357 	return ret;
20358 }
20359 /**
20360  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20361  * @phba: pointer to lpfc hba data structure.
20362  *
20363  * This routine is to get the next eligible FCF record index in a round
20364  * robin fashion. If the next eligible FCF record index equals to the
20365  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
20366  * shall be returned, otherwise, the next eligible FCF record's index
20367  * shall be returned.
20368  **/
20369 uint16_t
lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba * phba)20370 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20371 {
20372 	uint16_t next_fcf_index;
20373 
20374 initial_priority:
20375 	/* Search start from next bit of currently registered FCF index */
20376 	next_fcf_index = phba->fcf.current_rec.fcf_indx;
20377 
20378 next_priority:
20379 	/* Determine the next fcf index to check */
20380 	next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
20381 	next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20382 				       LPFC_SLI4_FCF_TBL_INDX_MAX,
20383 				       next_fcf_index);
20384 
20385 	/* Wrap around condition on phba->fcf.fcf_rr_bmask */
20386 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20387 		/*
20388 		 * If we have wrapped then we need to clear the bits that
20389 		 * have been tested so that we can detect when we should
20390 		 * change the priority level.
20391 		 */
20392 		next_fcf_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20393 					       LPFC_SLI4_FCF_TBL_INDX_MAX);
20394 	}
20395 
20396 
20397 	/* Check roundrobin failover list empty condition */
20398 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20399 		next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20400 		/*
20401 		 * If next fcf index is not found check if there are lower
20402 		 * Priority level fcf's in the fcf_priority list.
20403 		 * Set up the rr_bmask with all of the avaiable fcf bits
20404 		 * at that level and continue the selection process.
20405 		 */
20406 		if (lpfc_check_next_fcf_pri_level(phba))
20407 			goto initial_priority;
20408 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20409 				"2844 No roundrobin failover FCF available\n");
20410 
20411 		return LPFC_FCOE_FCF_NEXT_NONE;
20412 	}
20413 
20414 	if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20415 		phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
20416 		LPFC_FCF_FLOGI_FAILED) {
20417 		if (list_is_singular(&phba->fcf.fcf_pri_list))
20418 			return LPFC_FCOE_FCF_NEXT_NONE;
20419 
20420 		goto next_priority;
20421 	}
20422 
20423 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20424 			"2845 Get next roundrobin failover FCF (x%x)\n",
20425 			next_fcf_index);
20426 
20427 	return next_fcf_index;
20428 }
20429 
20430 /**
20431  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20432  * @phba: pointer to lpfc hba data structure.
20433  * @fcf_index: index into the FCF table to 'set'
20434  *
20435  * This routine sets the FCF record index in to the eligible bmask for
20436  * roundrobin failover search. It checks to make sure that the index
20437  * does not go beyond the range of the driver allocated bmask dimension
20438  * before setting the bit.
20439  *
20440  * Returns 0 if the index bit successfully set, otherwise, it returns
20441  * -EINVAL.
20442  **/
20443 int
lpfc_sli4_fcf_rr_index_set(struct lpfc_hba * phba,uint16_t fcf_index)20444 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20445 {
20446 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20447 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20448 				"2610 FCF (x%x) reached driver's book "
20449 				"keeping dimension:x%x\n",
20450 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20451 		return -EINVAL;
20452 	}
20453 	/* Set the eligible FCF record index bmask */
20454 	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20455 
20456 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20457 			"2790 Set FCF (x%x) to roundrobin FCF failover "
20458 			"bmask\n", fcf_index);
20459 
20460 	return 0;
20461 }
20462 
20463 /**
20464  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
20465  * @phba: pointer to lpfc hba data structure.
20466  * @fcf_index: index into the FCF table to 'clear'
20467  *
20468  * This routine clears the FCF record index from the eligible bmask for
20469  * roundrobin failover search. It checks to make sure that the index
20470  * does not go beyond the range of the driver allocated bmask dimension
20471  * before clearing the bit.
20472  **/
20473 void
lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba * phba,uint16_t fcf_index)20474 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20475 {
20476 	struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
20477 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20478 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20479 				"2762 FCF (x%x) reached driver's book "
20480 				"keeping dimension:x%x\n",
20481 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20482 		return;
20483 	}
20484 	/* Clear the eligible FCF record index bmask */
20485 	spin_lock_irq(&phba->hbalock);
20486 	list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20487 				 list) {
20488 		if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20489 			list_del_init(&fcf_pri->list);
20490 			break;
20491 		}
20492 	}
20493 	spin_unlock_irq(&phba->hbalock);
20494 	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20495 
20496 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20497 			"2791 Clear FCF (x%x) from roundrobin failover "
20498 			"bmask\n", fcf_index);
20499 }
20500 
20501 /**
20502  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20503  * @phba: pointer to lpfc hba data structure.
20504  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
20505  *
20506  * This routine is the completion routine for the rediscover FCF table mailbox
20507  * command. If the mailbox command returned failure, it will try to stop the
20508  * FCF rediscover wait timer.
20509  **/
20510 static void
lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba * phba,LPFC_MBOXQ_t * mbox)20511 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20512 {
20513 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20514 	uint32_t shdr_status, shdr_add_status;
20515 
20516 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20517 
20518 	shdr_status = bf_get(lpfc_mbox_hdr_status,
20519 			     &redisc_fcf->header.cfg_shdr.response);
20520 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20521 			     &redisc_fcf->header.cfg_shdr.response);
20522 	if (shdr_status || shdr_add_status) {
20523 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20524 				"2746 Requesting for FCF rediscovery failed "
20525 				"status x%x add_status x%x\n",
20526 				shdr_status, shdr_add_status);
20527 		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
20528 			spin_lock_irq(&phba->hbalock);
20529 			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
20530 			spin_unlock_irq(&phba->hbalock);
20531 			/*
20532 			 * CVL event triggered FCF rediscover request failed,
20533 			 * last resort to re-try current registered FCF entry.
20534 			 */
20535 			lpfc_retry_pport_discovery(phba);
20536 		} else {
20537 			spin_lock_irq(&phba->hbalock);
20538 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
20539 			spin_unlock_irq(&phba->hbalock);
20540 			/*
20541 			 * DEAD FCF event triggered FCF rediscover request
20542 			 * failed, last resort to fail over as a link down
20543 			 * to FCF registration.
20544 			 */
20545 			lpfc_sli4_fcf_dead_failthrough(phba);
20546 		}
20547 	} else {
20548 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20549 				"2775 Start FCF rediscover quiescent timer\n");
20550 		/*
20551 		 * Start FCF rediscovery wait timer for pending FCF
20552 		 * before rescan FCF record table.
20553 		 */
20554 		lpfc_fcf_redisc_wait_start_timer(phba);
20555 	}
20556 
20557 	mempool_free(mbox, phba->mbox_mem_pool);
20558 }
20559 
20560 /**
20561  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
20562  * @phba: pointer to lpfc hba data structure.
20563  *
20564  * This routine is invoked to request for rediscovery of the entire FCF table
20565  * by the port.
20566  **/
20567 int
lpfc_sli4_redisc_fcf_table(struct lpfc_hba * phba)20568 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20569 {
20570 	LPFC_MBOXQ_t *mbox;
20571 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20572 	int rc, length;
20573 
20574 	/* Cancel retry delay timers to all vports before FCF rediscover */
20575 	lpfc_cancel_all_vport_retry_delay_timer(phba);
20576 
20577 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20578 	if (!mbox) {
20579 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20580 				"2745 Failed to allocate mbox for "
20581 				"requesting FCF rediscover.\n");
20582 		return -ENOMEM;
20583 	}
20584 
20585 	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20586 		  sizeof(struct lpfc_sli4_cfg_mhdr));
20587 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20588 			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20589 			 length, LPFC_SLI4_MBX_EMBED);
20590 
20591 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20592 	/* Set count to 0 for invalidating the entire FCF database */
20593 	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20594 
20595 	/* Issue the mailbox command asynchronously */
20596 	mbox->vport = phba->pport;
20597 	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20598 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20599 
20600 	if (rc == MBX_NOT_FINISHED) {
20601 		mempool_free(mbox, phba->mbox_mem_pool);
20602 		return -EIO;
20603 	}
20604 	return 0;
20605 }
20606 
20607 /**
20608  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20609  * @phba: pointer to lpfc hba data structure.
20610  *
20611  * This function is the failover routine as a last resort to the FCF DEAD
20612  * event when driver failed to perform fast FCF failover.
20613  **/
20614 void
lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba * phba)20615 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20616 {
20617 	uint32_t link_state;
20618 
20619 	/*
20620 	 * Last resort as FCF DEAD event failover will treat this as
20621 	 * a link down, but save the link state because we don't want
20622 	 * it to be changed to Link Down unless it is already down.
20623 	 */
20624 	link_state = phba->link_state;
20625 	lpfc_linkdown(phba);
20626 	phba->link_state = link_state;
20627 
20628 	/* Unregister FCF if no devices connected to it */
20629 	lpfc_unregister_unused_fcf(phba);
20630 }
20631 
20632 /**
20633  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
20634  * @phba: pointer to lpfc hba data structure.
20635  * @rgn23_data: pointer to configure region 23 data.
20636  *
20637  * This function gets SLI3 port configure region 23 data through memory dump
20638  * mailbox command. When it successfully retrieves data, the size of the data
20639  * will be returned, otherwise, 0 will be returned.
20640  **/
20641 static uint32_t
lpfc_sli_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20642 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20643 {
20644 	LPFC_MBOXQ_t *pmb = NULL;
20645 	MAILBOX_t *mb;
20646 	uint32_t offset = 0;
20647 	int rc;
20648 
20649 	if (!rgn23_data)
20650 		return 0;
20651 
20652 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20653 	if (!pmb) {
20654 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20655 				"2600 failed to allocate mailbox memory\n");
20656 		return 0;
20657 	}
20658 	mb = &pmb->u.mb;
20659 
20660 	do {
20661 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20662 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20663 
20664 		if (rc != MBX_SUCCESS) {
20665 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20666 					"2601 failed to read config "
20667 					"region 23, rc 0x%x Status 0x%x\n",
20668 					rc, mb->mbxStatus);
20669 			mb->un.varDmp.word_cnt = 0;
20670 		}
20671 		/*
20672 		 * dump mem may return a zero when finished or we got a
20673 		 * mailbox error, either way we are done.
20674 		 */
20675 		if (mb->un.varDmp.word_cnt == 0)
20676 			break;
20677 
20678 		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20679 			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20680 
20681 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
20682 				       rgn23_data + offset,
20683 				       mb->un.varDmp.word_cnt);
20684 		offset += mb->un.varDmp.word_cnt;
20685 	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
20686 
20687 	mempool_free(pmb, phba->mbox_mem_pool);
20688 	return offset;
20689 }
20690 
20691 /**
20692  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20693  * @phba: pointer to lpfc hba data structure.
20694  * @rgn23_data: pointer to configure region 23 data.
20695  *
20696  * This function gets SLI4 port configure region 23 data through memory dump
20697  * mailbox command. When it successfully retrieves data, the size of the data
20698  * will be returned, otherwise, 0 will be returned.
20699  **/
20700 static uint32_t
lpfc_sli4_get_config_region23(struct lpfc_hba * phba,char * rgn23_data)20701 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20702 {
20703 	LPFC_MBOXQ_t *mboxq = NULL;
20704 	struct lpfc_dmabuf *mp = NULL;
20705 	struct lpfc_mqe *mqe;
20706 	uint32_t data_length = 0;
20707 	int rc;
20708 
20709 	if (!rgn23_data)
20710 		return 0;
20711 
20712 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20713 	if (!mboxq) {
20714 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20715 				"3105 failed to allocate mailbox memory\n");
20716 		return 0;
20717 	}
20718 
20719 	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20720 		goto out;
20721 	mqe = &mboxq->u.mqe;
20722 	mp = mboxq->ctx_buf;
20723 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20724 	if (rc)
20725 		goto out;
20726 	data_length = mqe->un.mb_words[5];
20727 	if (data_length == 0)
20728 		goto out;
20729 	if (data_length > DMP_RGN23_SIZE) {
20730 		data_length = 0;
20731 		goto out;
20732 	}
20733 	lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20734 out:
20735 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
20736 	return data_length;
20737 }
20738 
20739 /**
20740  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20741  * @phba: pointer to lpfc hba data structure.
20742  *
20743  * This function read region 23 and parse TLV for port status to
20744  * decide if the user disaled the port. If the TLV indicates the
20745  * port is disabled, the hba_flag is set accordingly.
20746  **/
20747 void
lpfc_sli_read_link_ste(struct lpfc_hba * phba)20748 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20749 {
20750 	uint8_t *rgn23_data = NULL;
20751 	uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20752 	uint32_t offset = 0;
20753 
20754 	/* Get adapter Region 23 data */
20755 	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20756 	if (!rgn23_data)
20757 		goto out;
20758 
20759 	if (phba->sli_rev < LPFC_SLI_REV4)
20760 		data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20761 	else {
20762 		if_type = bf_get(lpfc_sli_intf_if_type,
20763 				 &phba->sli4_hba.sli_intf);
20764 		if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20765 			goto out;
20766 		data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20767 	}
20768 
20769 	if (!data_size)
20770 		goto out;
20771 
20772 	/* Check the region signature first */
20773 	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
20774 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20775 			"2619 Config region 23 has bad signature\n");
20776 			goto out;
20777 	}
20778 	offset += 4;
20779 
20780 	/* Check the data structure version */
20781 	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
20782 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20783 			"2620 Config region 23 has bad version\n");
20784 		goto out;
20785 	}
20786 	offset += 4;
20787 
20788 	/* Parse TLV entries in the region */
20789 	while (offset < data_size) {
20790 		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20791 			break;
20792 		/*
20793 		 * If the TLV is not driver specific TLV or driver id is
20794 		 * not linux driver id, skip the record.
20795 		 */
20796 		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20797 		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20798 		    (rgn23_data[offset + 3] != 0)) {
20799 			offset += rgn23_data[offset + 1] * 4 + 4;
20800 			continue;
20801 		}
20802 
20803 		/* Driver found a driver specific TLV in the config region */
20804 		sub_tlv_len = rgn23_data[offset + 1] * 4;
20805 		offset += 4;
20806 		tlv_offset = 0;
20807 
20808 		/*
20809 		 * Search for configured port state sub-TLV.
20810 		 */
20811 		while ((offset < data_size) &&
20812 			(tlv_offset < sub_tlv_len)) {
20813 			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20814 				offset += 4;
20815 				tlv_offset += 4;
20816 				break;
20817 			}
20818 			if (rgn23_data[offset] != PORT_STE_TYPE) {
20819 				offset += rgn23_data[offset + 1] * 4 + 4;
20820 				tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20821 				continue;
20822 			}
20823 
20824 			/* This HBA contains PORT_STE configured */
20825 			if (!rgn23_data[offset + 2])
20826 				set_bit(LINK_DISABLED, &phba->hba_flag);
20827 
20828 			goto out;
20829 		}
20830 	}
20831 
20832 out:
20833 	kfree(rgn23_data);
20834 	return;
20835 }
20836 
20837 /**
20838  * lpfc_log_fw_write_cmpl - logs firmware write completion status
20839  * @phba: pointer to lpfc hba data structure
20840  * @shdr_status: wr_object rsp's status field
20841  * @shdr_add_status: wr_object rsp's add_status field
20842  * @shdr_add_status_2: wr_object rsp's add_status_2 field
20843  * @shdr_change_status: wr_object rsp's change_status field
20844  * @shdr_csf: wr_object rsp's csf bit
20845  *
20846  * This routine is intended to be called after a firmware write completes.
20847  * It will log next action items to be performed by the user to instantiate
20848  * the newly downloaded firmware or reason for incompatibility.
20849  **/
20850 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)20851 lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20852 		       u32 shdr_add_status, u32 shdr_add_status_2,
20853 		       u32 shdr_change_status, u32 shdr_csf)
20854 {
20855 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20856 			"4198 %s: flash_id x%02x, asic_rev x%02x, "
20857 			"status x%02x, add_status x%02x, add_status_2 x%02x, "
20858 			"change_status x%02x, csf %01x\n", __func__,
20859 			phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20860 			shdr_status, shdr_add_status, shdr_add_status_2,
20861 			shdr_change_status, shdr_csf);
20862 
20863 	if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20864 		switch (shdr_add_status_2) {
20865 		case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20866 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20867 				     "4199 Firmware write failed: "
20868 				     "image incompatible with flash x%02x\n",
20869 				     phba->sli4_hba.flash_id);
20870 			break;
20871 		case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20872 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20873 				     "4200 Firmware write failed: "
20874 				     "image incompatible with ASIC "
20875 				     "architecture x%02x\n",
20876 				     phba->sli4_hba.asic_rev);
20877 			break;
20878 		default:
20879 			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20880 				     "4210 Firmware write failed: "
20881 				     "add_status_2 x%02x\n",
20882 				     shdr_add_status_2);
20883 			break;
20884 		}
20885 	} else if (!shdr_status && !shdr_add_status) {
20886 		if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20887 		    shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20888 			if (shdr_csf)
20889 				shdr_change_status =
20890 						   LPFC_CHANGE_STATUS_PCI_RESET;
20891 		}
20892 
20893 		switch (shdr_change_status) {
20894 		case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20895 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20896 				     "3198 Firmware write complete: System "
20897 				     "reboot required to instantiate\n");
20898 			break;
20899 		case (LPFC_CHANGE_STATUS_FW_RESET):
20900 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20901 				     "3199 Firmware write complete: "
20902 				     "Firmware reset required to "
20903 				     "instantiate\n");
20904 			break;
20905 		case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20906 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20907 				     "3200 Firmware write complete: Port "
20908 				     "Migration or PCI Reset required to "
20909 				     "instantiate\n");
20910 			break;
20911 		case (LPFC_CHANGE_STATUS_PCI_RESET):
20912 			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20913 				     "3201 Firmware write complete: PCI "
20914 				     "Reset required to instantiate\n");
20915 			break;
20916 		default:
20917 			break;
20918 		}
20919 	}
20920 }
20921 
20922 /**
20923  * lpfc_wr_object - write an object to the firmware
20924  * @phba: HBA structure that indicates port to create a queue on.
20925  * @dmabuf_list: list of dmabufs to write to the port.
20926  * @size: the total byte value of the objects to write to the port.
20927  * @offset: the current offset to be used to start the transfer.
20928  *
20929  * This routine will create a wr_object mailbox command to send to the port.
20930  * the mailbox command will be constructed using the dma buffers described in
20931  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20932  * BDEs that the imbedded mailbox can support. The @offset variable will be
20933  * used to indicate the starting offset of the transfer and will also return
20934  * the offset after the write object mailbox has completed. @size is used to
20935  * determine the end of the object and whether the eof bit should be set.
20936  *
20937  * Return 0 is successful and offset will contain the new offset to use
20938  * for the next write.
20939  * Return negative value for error cases.
20940  **/
20941 int
lpfc_wr_object(struct lpfc_hba * phba,struct list_head * dmabuf_list,uint32_t size,uint32_t * offset)20942 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20943 	       uint32_t size, uint32_t *offset)
20944 {
20945 	struct lpfc_mbx_wr_object *wr_object;
20946 	LPFC_MBOXQ_t *mbox;
20947 	int rc = 0, i = 0;
20948 	int mbox_status = 0;
20949 	uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20950 	uint32_t shdr_change_status = 0, shdr_csf = 0;
20951 	uint32_t mbox_tmo;
20952 	struct lpfc_dmabuf *dmabuf;
20953 	uint32_t written = 0;
20954 	bool check_change_status = false;
20955 
20956 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20957 	if (!mbox)
20958 		return -ENOMEM;
20959 
20960 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20961 			LPFC_MBOX_OPCODE_WRITE_OBJECT,
20962 			sizeof(struct lpfc_mbx_wr_object) -
20963 			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20964 
20965 	wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20966 	wr_object->u.request.write_offset = *offset;
20967 	sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20968 	wr_object->u.request.object_name[0] =
20969 		cpu_to_le32(wr_object->u.request.object_name[0]);
20970 	bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20971 	list_for_each_entry(dmabuf, dmabuf_list, list) {
20972 		if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20973 			break;
20974 		wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20975 		wr_object->u.request.bde[i].addrHigh =
20976 			putPaddrHigh(dmabuf->phys);
20977 		if (written + SLI4_PAGE_SIZE >= size) {
20978 			wr_object->u.request.bde[i].tus.f.bdeSize =
20979 				(size - written);
20980 			written += (size - written);
20981 			bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
20982 			bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20983 			check_change_status = true;
20984 		} else {
20985 			wr_object->u.request.bde[i].tus.f.bdeSize =
20986 				SLI4_PAGE_SIZE;
20987 			written += SLI4_PAGE_SIZE;
20988 		}
20989 		i++;
20990 	}
20991 	wr_object->u.request.bde_count = i;
20992 	bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20993 	if (!phba->sli4_hba.intr_enable)
20994 		mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20995 	else {
20996 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
20997 		mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20998 	}
20999 
21000 	/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */
21001 	rc = mbox_status;
21002 
21003 	/* The IOCTL status is embedded in the mailbox subheader. */
21004 	shdr_status = bf_get(lpfc_mbox_hdr_status,
21005 			     &wr_object->header.cfg_shdr.response);
21006 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
21007 				 &wr_object->header.cfg_shdr.response);
21008 	shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
21009 				   &wr_object->header.cfg_shdr.response);
21010 	if (check_change_status) {
21011 		shdr_change_status = bf_get(lpfc_wr_object_change_status,
21012 					    &wr_object->u.response);
21013 		shdr_csf = bf_get(lpfc_wr_object_csf,
21014 				  &wr_object->u.response);
21015 	}
21016 
21017 	if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
21018 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21019 				"3025 Write Object mailbox failed with "
21020 				"status x%x add_status x%x, add_status_2 x%x, "
21021 				"mbx status x%x\n",
21022 				shdr_status, shdr_add_status, shdr_add_status_2,
21023 				rc);
21024 		rc = -ENXIO;
21025 		*offset = shdr_add_status;
21026 	} else {
21027 		*offset += wr_object->u.response.actual_write_length;
21028 	}
21029 
21030 	if (rc || check_change_status)
21031 		lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
21032 				       shdr_add_status_2, shdr_change_status,
21033 				       shdr_csf);
21034 
21035 	if (!phba->sli4_hba.intr_enable)
21036 		mempool_free(mbox, phba->mbox_mem_pool);
21037 	else if (mbox_status != MBX_TIMEOUT)
21038 		mempool_free(mbox, phba->mbox_mem_pool);
21039 
21040 	return rc;
21041 }
21042 
21043 /**
21044  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
21045  * @vport: pointer to vport data structure.
21046  *
21047  * This function iterate through the mailboxq and clean up all REG_LOGIN
21048  * and REG_VPI mailbox commands associated with the vport. This function
21049  * is called when driver want to restart discovery of the vport due to
21050  * a Clear Virtual Link event.
21051  **/
21052 void
lpfc_cleanup_pending_mbox(struct lpfc_vport * vport)21053 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
21054 {
21055 	struct lpfc_hba *phba = vport->phba;
21056 	LPFC_MBOXQ_t *mb, *nextmb;
21057 	struct lpfc_nodelist *ndlp;
21058 	struct lpfc_nodelist *act_mbx_ndlp = NULL;
21059 	LIST_HEAD(mbox_cmd_list);
21060 	uint8_t restart_loop;
21061 
21062 	/* Clean up internally queued mailbox commands with the vport */
21063 	spin_lock_irq(&phba->hbalock);
21064 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
21065 		if (mb->vport != vport)
21066 			continue;
21067 
21068 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21069 			(mb->u.mb.mbxCommand != MBX_REG_VPI))
21070 			continue;
21071 
21072 		list_move_tail(&mb->list, &mbox_cmd_list);
21073 	}
21074 	/* Clean up active mailbox command with the vport */
21075 	mb = phba->sli.mbox_active;
21076 	if (mb && (mb->vport == vport)) {
21077 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
21078 			(mb->u.mb.mbxCommand == MBX_REG_VPI))
21079 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21080 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21081 			act_mbx_ndlp = mb->ctx_ndlp;
21082 
21083 			/* This reference is local to this routine.  The
21084 			 * reference is removed at routine exit.
21085 			 */
21086 			act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
21087 
21088 			/* Unregister the RPI when mailbox complete */
21089 			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21090 		}
21091 	}
21092 	/* Cleanup any mailbox completions which are not yet processed */
21093 	do {
21094 		restart_loop = 0;
21095 		list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
21096 			/*
21097 			 * If this mailox is already processed or it is
21098 			 * for another vport ignore it.
21099 			 */
21100 			if ((mb->vport != vport) ||
21101 				(mb->mbox_flag & LPFC_MBX_IMED_UNREG))
21102 				continue;
21103 
21104 			if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21105 				(mb->u.mb.mbxCommand != MBX_REG_VPI))
21106 				continue;
21107 
21108 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21109 			if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21110 				ndlp = mb->ctx_ndlp;
21111 				/* Unregister the RPI when mailbox complete */
21112 				mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21113 				restart_loop = 1;
21114 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21115 				break;
21116 			}
21117 		}
21118 	} while (restart_loop);
21119 
21120 	spin_unlock_irq(&phba->hbalock);
21121 
21122 	/* Release the cleaned-up mailbox commands */
21123 	while (!list_empty(&mbox_cmd_list)) {
21124 		list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
21125 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21126 			ndlp = mb->ctx_ndlp;
21127 			mb->ctx_ndlp = NULL;
21128 			if (ndlp) {
21129 				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21130 				lpfc_nlp_put(ndlp);
21131 			}
21132 		}
21133 		lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_UNLOCKED);
21134 	}
21135 
21136 	/* Release the ndlp with the cleaned-up active mailbox command */
21137 	if (act_mbx_ndlp) {
21138 		clear_bit(NLP_IGNR_REG_CMPL, &act_mbx_ndlp->nlp_flag);
21139 		lpfc_nlp_put(act_mbx_ndlp);
21140 	}
21141 }
21142 
21143 /**
21144  * lpfc_drain_txq - Drain the txq
21145  * @phba: Pointer to HBA context object.
21146  *
21147  * This function attempt to submit IOCBs on the txq
21148  * to the adapter.  For SLI4 adapters, the txq contains
21149  * ELS IOCBs that have been deferred because the there
21150  * are no SGLs.  This congestion can occur with large
21151  * vport counts during node discovery.
21152  **/
21153 
21154 uint32_t
lpfc_drain_txq(struct lpfc_hba * phba)21155 lpfc_drain_txq(struct lpfc_hba *phba)
21156 {
21157 	LIST_HEAD(completions);
21158 	struct lpfc_sli_ring *pring;
21159 	struct lpfc_iocbq *piocbq = NULL;
21160 	unsigned long iflags = 0;
21161 	char *fail_msg = NULL;
21162 	uint32_t txq_cnt = 0;
21163 	struct lpfc_queue *wq;
21164 	int ret = 0;
21165 
21166 	if (phba->link_flag & LS_MDS_LOOPBACK) {
21167 		/* MDS WQE are posted only to first WQ*/
21168 		wq = phba->sli4_hba.hdwq[0].io_wq;
21169 		if (unlikely(!wq))
21170 			return 0;
21171 		pring = wq->pring;
21172 	} else {
21173 		wq = phba->sli4_hba.els_wq;
21174 		if (unlikely(!wq))
21175 			return 0;
21176 		pring = lpfc_phba_elsring(phba);
21177 	}
21178 
21179 	if (unlikely(!pring) || list_empty(&pring->txq))
21180 		return 0;
21181 
21182 	spin_lock_irqsave(&pring->ring_lock, iflags);
21183 	list_for_each_entry(piocbq, &pring->txq, list) {
21184 		txq_cnt++;
21185 	}
21186 
21187 	if (txq_cnt > pring->txq_max)
21188 		pring->txq_max = txq_cnt;
21189 
21190 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
21191 
21192 	while (!list_empty(&pring->txq)) {
21193 		spin_lock_irqsave(&pring->ring_lock, iflags);
21194 
21195 		piocbq = lpfc_sli_ringtx_get(phba, pring);
21196 		if (!piocbq) {
21197 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21198 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21199 				"2823 txq empty and txq_cnt is %d\n",
21200 				txq_cnt);
21201 			break;
21202 		}
21203 		txq_cnt--;
21204 
21205 		ret = __lpfc_sli_issue_iocb(phba, pring->ringno, piocbq, 0);
21206 
21207 		if (ret && ret != IOCB_BUSY) {
21208 			fail_msg = " - Cannot send IO ";
21209 			piocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21210 		}
21211 		if (fail_msg) {
21212 			piocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
21213 			/* Failed means we can't issue and need to cancel */
21214 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21215 					"2822 IOCB failed %s iotag 0x%x "
21216 					"xri 0x%x %d flg x%x\n",
21217 					fail_msg, piocbq->iotag,
21218 					piocbq->sli4_xritag, ret,
21219 					piocbq->cmd_flag);
21220 			list_add_tail(&piocbq->list, &completions);
21221 			fail_msg = NULL;
21222 		}
21223 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21224 		if (txq_cnt == 0 || ret == IOCB_BUSY)
21225 			break;
21226 	}
21227 	/* Cancel all the IOCBs that cannot be issued */
21228 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21229 			      IOERR_SLI_ABORTED);
21230 
21231 	return txq_cnt;
21232 }
21233 
21234 /**
21235  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21236  * @phba: Pointer to HBA context object.
21237  * @pwqeq: Pointer to command WQE.
21238  * @sglq: Pointer to the scatter gather queue object.
21239  *
21240  * This routine converts the bpl or bde that is in the WQE
21241  * to a sgl list for the sli4 hardware. The physical address
21242  * of the bpl/bde is converted back to a virtual address.
21243  * If the WQE contains a BPL then the list of BDE's is
21244  * converted to sli4_sge's. If the WQE contains a single
21245  * BDE then it is converted to a single sli_sge.
21246  * The WQE is still in cpu endianness so the contents of
21247  * the bpl can be used without byte swapping.
21248  *
21249  * Returns valid XRI = Success, NO_XRI = Failure.
21250  */
21251 static uint16_t
lpfc_wqe_bpl2sgl(struct lpfc_hba * phba,struct lpfc_iocbq * pwqeq,struct lpfc_sglq * sglq)21252 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21253 		 struct lpfc_sglq *sglq)
21254 {
21255 	uint16_t xritag = NO_XRI;
21256 	struct ulp_bde64 *bpl = NULL;
21257 	struct ulp_bde64 bde;
21258 	struct sli4_sge *sgl  = NULL;
21259 	struct lpfc_dmabuf *dmabuf;
21260 	union lpfc_wqe128 *wqe;
21261 	int numBdes = 0;
21262 	int i = 0;
21263 	uint32_t offset = 0; /* accumulated offset in the sg request list */
21264 	int inbound = 0; /* number of sg reply entries inbound from firmware */
21265 	uint32_t cmd;
21266 
21267 	if (!pwqeq || !sglq)
21268 		return xritag;
21269 
21270 	sgl  = (struct sli4_sge *)sglq->sgl;
21271 	wqe = &pwqeq->wqe;
21272 	pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21273 
21274 	cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21275 	if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21276 		return sglq->sli4_xritag;
21277 	numBdes = pwqeq->num_bdes;
21278 	if (numBdes) {
21279 		/* The addrHigh and addrLow fields within the WQE
21280 		 * have not been byteswapped yet so there is no
21281 		 * need to swap them back.
21282 		 */
21283 		if (pwqeq->bpl_dmabuf)
21284 			dmabuf = pwqeq->bpl_dmabuf;
21285 		else
21286 			return xritag;
21287 
21288 		bpl  = (struct ulp_bde64 *)dmabuf->virt;
21289 		if (!bpl)
21290 			return xritag;
21291 
21292 		for (i = 0; i < numBdes; i++) {
21293 			/* Should already be byte swapped. */
21294 			sgl->addr_hi = bpl->addrHigh;
21295 			sgl->addr_lo = bpl->addrLow;
21296 
21297 			sgl->word2 = le32_to_cpu(sgl->word2);
21298 			if ((i+1) == numBdes)
21299 				bf_set(lpfc_sli4_sge_last, sgl, 1);
21300 			else
21301 				bf_set(lpfc_sli4_sge_last, sgl, 0);
21302 			/* swap the size field back to the cpu so we
21303 			 * can assign it to the sgl.
21304 			 */
21305 			bde.tus.w = le32_to_cpu(bpl->tus.w);
21306 			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21307 			/* The offsets in the sgl need to be accumulated
21308 			 * separately for the request and reply lists.
21309 			 * The request is always first, the reply follows.
21310 			 */
21311 			switch (cmd) {
21312 			case CMD_GEN_REQUEST64_WQE:
21313 				/* add up the reply sg entries */
21314 				if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21315 					inbound++;
21316 				/* first inbound? reset the offset */
21317 				if (inbound == 1)
21318 					offset = 0;
21319 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21320 				bf_set(lpfc_sli4_sge_type, sgl,
21321 					LPFC_SGE_TYPE_DATA);
21322 				offset += bde.tus.f.bdeSize;
21323 				break;
21324 			case CMD_FCP_TRSP64_WQE:
21325 				bf_set(lpfc_sli4_sge_offset, sgl, 0);
21326 				bf_set(lpfc_sli4_sge_type, sgl,
21327 					LPFC_SGE_TYPE_DATA);
21328 				break;
21329 			case CMD_FCP_TSEND64_WQE:
21330 			case CMD_FCP_TRECEIVE64_WQE:
21331 				bf_set(lpfc_sli4_sge_type, sgl,
21332 					bpl->tus.f.bdeFlags);
21333 				if (i < 3)
21334 					offset = 0;
21335 				else
21336 					offset += bde.tus.f.bdeSize;
21337 				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21338 				break;
21339 			}
21340 			sgl->word2 = cpu_to_le32(sgl->word2);
21341 			bpl++;
21342 			sgl++;
21343 		}
21344 	} else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21345 		/* The addrHigh and addrLow fields of the BDE have not
21346 		 * been byteswapped yet so they need to be swapped
21347 		 * before putting them in the sgl.
21348 		 */
21349 		sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21350 		sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21351 		sgl->word2 = le32_to_cpu(sgl->word2);
21352 		bf_set(lpfc_sli4_sge_last, sgl, 1);
21353 		sgl->word2 = cpu_to_le32(sgl->word2);
21354 		sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21355 	}
21356 	return sglq->sli4_xritag;
21357 }
21358 
21359 /**
21360  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21361  * @phba: Pointer to HBA context object.
21362  * @qp: Pointer to HDW queue.
21363  * @pwqe: Pointer to command WQE.
21364  **/
21365 int
lpfc_sli4_issue_wqe(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * qp,struct lpfc_iocbq * pwqe)21366 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21367 		    struct lpfc_iocbq *pwqe)
21368 {
21369 	union lpfc_wqe128 *wqe = &pwqe->wqe;
21370 	struct lpfc_async_xchg_ctx *ctxp;
21371 	struct lpfc_queue *wq;
21372 	struct lpfc_sglq *sglq;
21373 	struct lpfc_sli_ring *pring;
21374 	unsigned long iflags;
21375 	uint32_t ret = 0;
21376 
21377 	/* NVME_LS and NVME_LS ABTS requests. */
21378 	if (pwqe->cmd_flag & LPFC_IO_NVME_LS) {
21379 		pring =  phba->sli4_hba.nvmels_wq->pring;
21380 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21381 					  qp, wq_access);
21382 		sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21383 		if (!sglq) {
21384 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21385 			return WQE_BUSY;
21386 		}
21387 		pwqe->sli4_lxritag = sglq->sli4_lxritag;
21388 		pwqe->sli4_xritag = sglq->sli4_xritag;
21389 		if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21390 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21391 			return WQE_ERROR;
21392 		}
21393 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21394 		       pwqe->sli4_xritag);
21395 		ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21396 		if (ret) {
21397 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21398 			return ret;
21399 		}
21400 
21401 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21402 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21403 
21404 		lpfc_sli4_poll_eq(qp->hba_eq);
21405 		return 0;
21406 	}
21407 
21408 	/* NVME_FCREQ and NVME_ABTS requests */
21409 	if (pwqe->cmd_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
21410 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21411 		wq = qp->io_wq;
21412 		pring = wq->pring;
21413 
21414 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21415 
21416 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21417 					  qp, wq_access);
21418 		ret = lpfc_sli4_wq_put(wq, wqe);
21419 		if (ret) {
21420 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21421 			return ret;
21422 		}
21423 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21424 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21425 
21426 		lpfc_sli4_poll_eq(qp->hba_eq);
21427 		return 0;
21428 	}
21429 
21430 	/* NVMET requests */
21431 	if (pwqe->cmd_flag & LPFC_IO_NVMET) {
21432 		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21433 		wq = qp->io_wq;
21434 		pring = wq->pring;
21435 
21436 		ctxp = pwqe->context_un.axchg;
21437 		sglq = ctxp->ctxbuf->sglq;
21438 		if (pwqe->sli4_xritag ==  NO_XRI) {
21439 			pwqe->sli4_lxritag = sglq->sli4_lxritag;
21440 			pwqe->sli4_xritag = sglq->sli4_xritag;
21441 		}
21442 		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21443 		       pwqe->sli4_xritag);
21444 		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21445 
21446 		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21447 					  qp, wq_access);
21448 		ret = lpfc_sli4_wq_put(wq, wqe);
21449 		if (ret) {
21450 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21451 			return ret;
21452 		}
21453 		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21454 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21455 
21456 		lpfc_sli4_poll_eq(qp->hba_eq);
21457 		return 0;
21458 	}
21459 	return WQE_ERROR;
21460 }
21461 
21462 /**
21463  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21464  * @phba: Pointer to HBA context object.
21465  * @cmdiocb: Pointer to driver command iocb object.
21466  * @cmpl: completion function.
21467  *
21468  * Fill the appropriate fields for the abort WQE and call
21469  * internal routine lpfc_sli4_issue_wqe to send the WQE
21470  * This function is called with hbalock held and no ring_lock held.
21471  *
21472  * RETURNS 0 - SUCCESS
21473  **/
21474 
21475 int
lpfc_sli4_issue_abort_iotag(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,void * cmpl)21476 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21477 			    void *cmpl)
21478 {
21479 	struct lpfc_vport *vport = cmdiocb->vport;
21480 	struct lpfc_iocbq *abtsiocb = NULL;
21481 	union lpfc_wqe128 *abtswqe;
21482 	struct lpfc_io_buf *lpfc_cmd;
21483 	int retval = IOCB_ERROR;
21484 	u16 xritag = cmdiocb->sli4_xritag;
21485 
21486 	/*
21487 	 * The scsi command can not be in txq and it is in flight because the
21488 	 * pCmd is still pointing at the SCSI command we have to abort. There
21489 	 * is no need to search the txcmplq. Just send an abort to the FW.
21490 	 */
21491 
21492 	abtsiocb = __lpfc_sli_get_iocbq(phba);
21493 	if (!abtsiocb)
21494 		return WQE_NORESOURCE;
21495 
21496 	/* Indicate the IO is being aborted by the driver. */
21497 	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
21498 
21499 	abtswqe = &abtsiocb->wqe;
21500 	memset(abtswqe, 0, sizeof(*abtswqe));
21501 
21502 	if (!lpfc_is_link_up(phba) || (phba->link_flag & LS_EXTERNAL_LOOPBACK))
21503 		bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
21504 	bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21505 	abtswqe->abort_cmd.rsrvd5 = 0;
21506 	abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21507 	bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21508 	bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21509 	bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21510 	bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21511 	bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21512 	bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21513 
21514 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
21515 	abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21516 	abtsiocb->cmd_flag |= LPFC_USE_FCPWQIDX;
21517 	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
21518 		abtsiocb->cmd_flag |= LPFC_IO_FCP;
21519 	if (cmdiocb->cmd_flag & LPFC_IO_NVME)
21520 		abtsiocb->cmd_flag |= LPFC_IO_NVME;
21521 	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
21522 		abtsiocb->cmd_flag |= LPFC_IO_FOF;
21523 	abtsiocb->vport = vport;
21524 	abtsiocb->cmd_cmpl = cmpl;
21525 
21526 	lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21527 	retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21528 
21529 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21530 			 "0359 Abort xri x%x, original iotag x%x, "
21531 			 "abort cmd iotag x%x retval x%x\n",
21532 			 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21533 
21534 	if (retval) {
21535 		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21536 		__lpfc_sli_release_iocbq(phba, abtsiocb);
21537 	}
21538 
21539 	return retval;
21540 }
21541 
21542 #ifdef LPFC_MXP_STAT
21543 /**
21544  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21545  * @phba: pointer to lpfc hba data structure.
21546  * @hwqid: belong to which HWQ.
21547  *
21548  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21549  * 15 seconds after a test case is running.
21550  *
21551  * The user should call lpfc_debugfs_multixripools_write before running a test
21552  * case to clear stat_snapshot_taken. Then the user starts a test case. During
21553  * test case is running, stat_snapshot_taken is incremented by 1 every time when
21554  * this routine is called from heartbeat timer. When stat_snapshot_taken is
21555  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21556  **/
lpfc_snapshot_mxp(struct lpfc_hba * phba,u32 hwqid)21557 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21558 {
21559 	struct lpfc_sli4_hdw_queue *qp;
21560 	struct lpfc_multixri_pool *multixri_pool;
21561 	struct lpfc_pvt_pool *pvt_pool;
21562 	struct lpfc_pbl_pool *pbl_pool;
21563 	u32 txcmplq_cnt;
21564 
21565 	qp = &phba->sli4_hba.hdwq[hwqid];
21566 	multixri_pool = qp->p_multixri_pool;
21567 	if (!multixri_pool)
21568 		return;
21569 
21570 	if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21571 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21572 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21573 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21574 
21575 		multixri_pool->stat_pbl_count = pbl_pool->count;
21576 		multixri_pool->stat_pvt_count = pvt_pool->count;
21577 		multixri_pool->stat_busy_count = txcmplq_cnt;
21578 	}
21579 
21580 	multixri_pool->stat_snapshot_taken++;
21581 }
21582 #endif
21583 
21584 /**
21585  * lpfc_adjust_pvt_pool_count - Adjust private pool count
21586  * @phba: pointer to lpfc hba data structure.
21587  * @hwqid: belong to which HWQ.
21588  *
21589  * This routine moves some XRIs from private to public pool when private pool
21590  * is not busy.
21591  **/
lpfc_adjust_pvt_pool_count(struct lpfc_hba * phba,u32 hwqid)21592 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21593 {
21594 	struct lpfc_multixri_pool *multixri_pool;
21595 	u32 io_req_count;
21596 	u32 prev_io_req_count;
21597 
21598 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21599 	if (!multixri_pool)
21600 		return;
21601 	io_req_count = multixri_pool->io_req_count;
21602 	prev_io_req_count = multixri_pool->prev_io_req_count;
21603 
21604 	if (prev_io_req_count != io_req_count) {
21605 		/* Private pool is busy */
21606 		multixri_pool->prev_io_req_count = io_req_count;
21607 	} else {
21608 		/* Private pool is not busy.
21609 		 * Move XRIs from private to public pool.
21610 		 */
21611 		lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21612 	}
21613 }
21614 
21615 /**
21616  * lpfc_adjust_high_watermark - Adjust high watermark
21617  * @phba: pointer to lpfc hba data structure.
21618  * @hwqid: belong to which HWQ.
21619  *
21620  * This routine sets high watermark as number of outstanding XRIs,
21621  * but make sure the new value is between xri_limit/2 and xri_limit.
21622  **/
lpfc_adjust_high_watermark(struct lpfc_hba * phba,u32 hwqid)21623 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21624 {
21625 	u32 new_watermark;
21626 	u32 watermark_max;
21627 	u32 watermark_min;
21628 	u32 xri_limit;
21629 	u32 txcmplq_cnt;
21630 	u32 abts_io_bufs;
21631 	struct lpfc_multixri_pool *multixri_pool;
21632 	struct lpfc_sli4_hdw_queue *qp;
21633 
21634 	qp = &phba->sli4_hba.hdwq[hwqid];
21635 	multixri_pool = qp->p_multixri_pool;
21636 	if (!multixri_pool)
21637 		return;
21638 	xri_limit = multixri_pool->xri_limit;
21639 
21640 	watermark_max = xri_limit;
21641 	watermark_min = xri_limit / 2;
21642 
21643 	txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21644 	abts_io_bufs = qp->abts_scsi_io_bufs;
21645 	abts_io_bufs += qp->abts_nvme_io_bufs;
21646 
21647 	new_watermark = txcmplq_cnt + abts_io_bufs;
21648 	new_watermark = min(watermark_max, new_watermark);
21649 	new_watermark = max(watermark_min, new_watermark);
21650 	multixri_pool->pvt_pool.high_watermark = new_watermark;
21651 
21652 #ifdef LPFC_MXP_STAT
21653 	multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21654 					  new_watermark);
21655 #endif
21656 }
21657 
21658 /**
21659  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21660  * @phba: pointer to lpfc hba data structure.
21661  * @hwqid: belong to which HWQ.
21662  *
21663  * This routine is called from hearbeat timer when pvt_pool is idle.
21664  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21665  * The first step moves (all - low_watermark) amount of XRIs.
21666  * The second step moves the rest of XRIs.
21667  **/
lpfc_move_xri_pvt_to_pbl(struct lpfc_hba * phba,u32 hwqid)21668 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21669 {
21670 	struct lpfc_pbl_pool *pbl_pool;
21671 	struct lpfc_pvt_pool *pvt_pool;
21672 	struct lpfc_sli4_hdw_queue *qp;
21673 	struct lpfc_io_buf *lpfc_ncmd;
21674 	struct lpfc_io_buf *lpfc_ncmd_next;
21675 	unsigned long iflag;
21676 	struct list_head tmp_list;
21677 	u32 tmp_count;
21678 
21679 	qp = &phba->sli4_hba.hdwq[hwqid];
21680 	pbl_pool = &qp->p_multixri_pool->pbl_pool;
21681 	pvt_pool = &qp->p_multixri_pool->pvt_pool;
21682 	tmp_count = 0;
21683 
21684 	lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21685 	lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
21686 
21687 	if (pvt_pool->count > pvt_pool->low_watermark) {
21688 		/* Step 1: move (all - low_watermark) from pvt_pool
21689 		 * to pbl_pool
21690 		 */
21691 
21692 		/* Move low watermark of bufs from pvt_pool to tmp_list */
21693 		INIT_LIST_HEAD(&tmp_list);
21694 		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21695 					 &pvt_pool->list, list) {
21696 			list_move_tail(&lpfc_ncmd->list, &tmp_list);
21697 			tmp_count++;
21698 			if (tmp_count >= pvt_pool->low_watermark)
21699 				break;
21700 		}
21701 
21702 		/* Move all bufs from pvt_pool to pbl_pool */
21703 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21704 
21705 		/* Move all bufs from tmp_list to pvt_pool */
21706 		list_splice(&tmp_list, &pvt_pool->list);
21707 
21708 		pbl_pool->count += (pvt_pool->count - tmp_count);
21709 		pvt_pool->count = tmp_count;
21710 	} else {
21711 		/* Step 2: move the rest from pvt_pool to pbl_pool */
21712 		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21713 		pbl_pool->count += pvt_pool->count;
21714 		pvt_pool->count = 0;
21715 	}
21716 
21717 	spin_unlock(&pvt_pool->lock);
21718 	spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21719 }
21720 
21721 /**
21722  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21723  * @phba: pointer to lpfc hba data structure
21724  * @qp: pointer to HDW queue
21725  * @pbl_pool: specified public free XRI pool
21726  * @pvt_pool: specified private free XRI pool
21727  * @count: number of XRIs to move
21728  *
21729  * This routine tries to move some free common bufs from the specified pbl_pool
21730  * to the specified pvt_pool. It might move less than count XRIs if there's not
21731  * enough in public pool.
21732  *
21733  * Return:
21734  *   true - if XRIs are successfully moved from the specified pbl_pool to the
21735  *          specified pvt_pool
21736  *   false - if the specified pbl_pool is empty or locked by someone else
21737  **/
21738 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)21739 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21740 			  struct lpfc_pbl_pool *pbl_pool,
21741 			  struct lpfc_pvt_pool *pvt_pool, u32 count)
21742 {
21743 	struct lpfc_io_buf *lpfc_ncmd;
21744 	struct lpfc_io_buf *lpfc_ncmd_next;
21745 	unsigned long iflag;
21746 	int ret;
21747 
21748 	ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21749 	if (ret) {
21750 		if (pbl_pool->count) {
21751 			/* Move a batch of XRIs from public to private pool */
21752 			lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
21753 			list_for_each_entry_safe(lpfc_ncmd,
21754 						 lpfc_ncmd_next,
21755 						 &pbl_pool->list,
21756 						 list) {
21757 				list_move_tail(&lpfc_ncmd->list,
21758 					       &pvt_pool->list);
21759 				pvt_pool->count++;
21760 				pbl_pool->count--;
21761 				count--;
21762 				if (count == 0)
21763 					break;
21764 			}
21765 
21766 			spin_unlock(&pvt_pool->lock);
21767 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21768 			return true;
21769 		}
21770 		spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21771 	}
21772 
21773 	return false;
21774 }
21775 
21776 /**
21777  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21778  * @phba: pointer to lpfc hba data structure.
21779  * @hwqid: belong to which HWQ.
21780  * @count: number of XRIs to move
21781  *
21782  * This routine tries to find some free common bufs in one of public pools with
21783  * Round Robin method. The search always starts from local hwqid, then the next
21784  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21785  * a batch of free common bufs are moved to private pool on hwqid.
21786  * It might move less than count XRIs if there's not enough in public pool.
21787  **/
lpfc_move_xri_pbl_to_pvt(struct lpfc_hba * phba,u32 hwqid,u32 count)21788 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21789 {
21790 	struct lpfc_multixri_pool *multixri_pool;
21791 	struct lpfc_multixri_pool *next_multixri_pool;
21792 	struct lpfc_pvt_pool *pvt_pool;
21793 	struct lpfc_pbl_pool *pbl_pool;
21794 	struct lpfc_sli4_hdw_queue *qp;
21795 	u32 next_hwqid;
21796 	u32 hwq_count;
21797 	int ret;
21798 
21799 	qp = &phba->sli4_hba.hdwq[hwqid];
21800 	multixri_pool = qp->p_multixri_pool;
21801 	pvt_pool = &multixri_pool->pvt_pool;
21802 	pbl_pool = &multixri_pool->pbl_pool;
21803 
21804 	/* Check if local pbl_pool is available */
21805 	ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
21806 	if (ret) {
21807 #ifdef LPFC_MXP_STAT
21808 		multixri_pool->local_pbl_hit_count++;
21809 #endif
21810 		return;
21811 	}
21812 
21813 	hwq_count = phba->cfg_hdw_queue;
21814 
21815 	/* Get the next hwqid which was found last time */
21816 	next_hwqid = multixri_pool->rrb_next_hwqid;
21817 
21818 	do {
21819 		/* Go to next hwq */
21820 		next_hwqid = (next_hwqid + 1) % hwq_count;
21821 
21822 		next_multixri_pool =
21823 			phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21824 		pbl_pool = &next_multixri_pool->pbl_pool;
21825 
21826 		/* Check if the public free xri pool is available */
21827 		ret = _lpfc_move_xri_pbl_to_pvt(
21828 			phba, qp, pbl_pool, pvt_pool, count);
21829 
21830 		/* Exit while-loop if success or all hwqid are checked */
21831 	} while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21832 
21833 	/* Starting point for the next time */
21834 	multixri_pool->rrb_next_hwqid = next_hwqid;
21835 
21836 	if (!ret) {
21837 		/* stats: all public pools are empty*/
21838 		multixri_pool->pbl_empty_count++;
21839 	}
21840 
21841 #ifdef LPFC_MXP_STAT
21842 	if (ret) {
21843 		if (next_hwqid == hwqid)
21844 			multixri_pool->local_pbl_hit_count++;
21845 		else
21846 			multixri_pool->other_pbl_hit_count++;
21847 	}
21848 #endif
21849 }
21850 
21851 /**
21852  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21853  * @phba: pointer to lpfc hba data structure.
21854  * @hwqid: belong to which HWQ.
21855  *
21856  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21857  * low watermark.
21858  **/
lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba * phba,u32 hwqid)21859 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21860 {
21861 	struct lpfc_multixri_pool *multixri_pool;
21862 	struct lpfc_pvt_pool *pvt_pool;
21863 
21864 	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21865 	pvt_pool = &multixri_pool->pvt_pool;
21866 
21867 	if (pvt_pool->count < pvt_pool->low_watermark)
21868 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21869 }
21870 
21871 /**
21872  * lpfc_release_io_buf - Return one IO buf back to free pool
21873  * @phba: pointer to lpfc hba data structure.
21874  * @lpfc_ncmd: IO buf to be returned.
21875  * @qp: belong to which HWQ.
21876  *
21877  * This routine returns one IO buf back to free pool. If this is an urgent IO,
21878  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21879  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21880  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
21881  * lpfc_io_buf_list_put.
21882  **/
lpfc_release_io_buf(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_ncmd,struct lpfc_sli4_hdw_queue * qp)21883 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21884 			 struct lpfc_sli4_hdw_queue *qp)
21885 {
21886 	unsigned long iflag;
21887 	struct lpfc_pbl_pool *pbl_pool;
21888 	struct lpfc_pvt_pool *pvt_pool;
21889 	struct lpfc_epd_pool *epd_pool;
21890 	u32 txcmplq_cnt;
21891 	u32 xri_owned;
21892 	u32 xri_limit;
21893 	u32 abts_io_bufs;
21894 
21895 	/* MUST zero fields if buffer is reused by another protocol */
21896 	lpfc_ncmd->nvmeCmd = NULL;
21897 	lpfc_ncmd->cur_iocbq.cmd_cmpl = NULL;
21898 
21899 	if (phba->cfg_xpsgl && !phba->nvmet_support &&
21900 	    !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21901 		lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21902 
21903 	if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21904 		lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21905 
21906 	if (phba->cfg_xri_rebalancing) {
21907 		if (lpfc_ncmd->expedite) {
21908 			/* Return to expedite pool */
21909 			epd_pool = &phba->epd_pool;
21910 			spin_lock_irqsave(&epd_pool->lock, iflag);
21911 			list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21912 			epd_pool->count++;
21913 			spin_unlock_irqrestore(&epd_pool->lock, iflag);
21914 			return;
21915 		}
21916 
21917 		/* Avoid invalid access if an IO sneaks in and is being rejected
21918 		 * just _after_ xri pools are destroyed in lpfc_offline.
21919 		 * Nothing much can be done at this point.
21920 		 */
21921 		if (!qp->p_multixri_pool)
21922 			return;
21923 
21924 		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21925 		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21926 
21927 		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21928 		abts_io_bufs = qp->abts_scsi_io_bufs;
21929 		abts_io_bufs += qp->abts_nvme_io_bufs;
21930 
21931 		xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21932 		xri_limit = qp->p_multixri_pool->xri_limit;
21933 
21934 #ifdef LPFC_MXP_STAT
21935 		if (xri_owned <= xri_limit)
21936 			qp->p_multixri_pool->below_limit_count++;
21937 		else
21938 			qp->p_multixri_pool->above_limit_count++;
21939 #endif
21940 
21941 		/* XRI goes to either public or private free xri pool
21942 		 *     based on watermark and xri_limit
21943 		 */
21944 		if ((pvt_pool->count < pvt_pool->low_watermark) ||
21945 		    (xri_owned < xri_limit &&
21946 		     pvt_pool->count < pvt_pool->high_watermark)) {
21947 			lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21948 						  qp, free_pvt_pool);
21949 			list_add_tail(&lpfc_ncmd->list,
21950 				      &pvt_pool->list);
21951 			pvt_pool->count++;
21952 			spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21953 		} else {
21954 			lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21955 						  qp, free_pub_pool);
21956 			list_add_tail(&lpfc_ncmd->list,
21957 				      &pbl_pool->list);
21958 			pbl_pool->count++;
21959 			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21960 		}
21961 	} else {
21962 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21963 					  qp, free_xri);
21964 		list_add_tail(&lpfc_ncmd->list,
21965 			      &qp->lpfc_io_buf_list_put);
21966 		qp->put_io_bufs++;
21967 		spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21968 				       iflag);
21969 	}
21970 }
21971 
21972 /**
21973  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21974  * @phba: pointer to lpfc hba data structure.
21975  * @qp: pointer to HDW queue
21976  * @pvt_pool: pointer to private pool data structure.
21977  * @ndlp: pointer to lpfc nodelist data structure.
21978  *
21979  * This routine tries to get one free IO buf from private pool.
21980  *
21981  * Return:
21982  *   pointer to one free IO buf - if private pool is not empty
21983  *   NULL - if private pool is empty
21984  **/
21985 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)21986 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
21987 				  struct lpfc_sli4_hdw_queue *qp,
21988 				  struct lpfc_pvt_pool *pvt_pool,
21989 				  struct lpfc_nodelist *ndlp)
21990 {
21991 	struct lpfc_io_buf *lpfc_ncmd;
21992 	struct lpfc_io_buf *lpfc_ncmd_next;
21993 	unsigned long iflag;
21994 
21995 	lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
21996 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21997 				 &pvt_pool->list, list) {
21998 		if (lpfc_test_rrq_active(
21999 			phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
22000 			continue;
22001 		list_del(&lpfc_ncmd->list);
22002 		pvt_pool->count--;
22003 		spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22004 		return lpfc_ncmd;
22005 	}
22006 	spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22007 
22008 	return NULL;
22009 }
22010 
22011 /**
22012  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
22013  * @phba: pointer to lpfc hba data structure.
22014  *
22015  * This routine tries to get one free IO buf from expedite pool.
22016  *
22017  * Return:
22018  *   pointer to one free IO buf - if expedite pool is not empty
22019  *   NULL - if expedite pool is empty
22020  **/
22021 static struct lpfc_io_buf *
lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba * phba)22022 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
22023 {
22024 	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
22025 	struct lpfc_io_buf *lpfc_ncmd_next;
22026 	unsigned long iflag;
22027 	struct lpfc_epd_pool *epd_pool;
22028 
22029 	epd_pool = &phba->epd_pool;
22030 
22031 	spin_lock_irqsave(&epd_pool->lock, iflag);
22032 	if (epd_pool->count > 0) {
22033 		list_for_each_entry_safe(iter, lpfc_ncmd_next,
22034 					 &epd_pool->list, list) {
22035 			list_del(&iter->list);
22036 			epd_pool->count--;
22037 			lpfc_ncmd = iter;
22038 			break;
22039 		}
22040 	}
22041 	spin_unlock_irqrestore(&epd_pool->lock, iflag);
22042 
22043 	return lpfc_ncmd;
22044 }
22045 
22046 /**
22047  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
22048  * @phba: pointer to lpfc hba data structure.
22049  * @ndlp: pointer to lpfc nodelist data structure.
22050  * @hwqid: belong to which HWQ
22051  * @expedite: 1 means this request is urgent.
22052  *
22053  * This routine will do the following actions and then return a pointer to
22054  * one free IO buf.
22055  *
22056  * 1. If private free xri count is empty, move some XRIs from public to
22057  *    private pool.
22058  * 2. Get one XRI from private free xri pool.
22059  * 3. If we fail to get one from pvt_pool and this is an expedite request,
22060  *    get one free xri from expedite pool.
22061  *
22062  * Note: ndlp is only used on SCSI side for RRQ testing.
22063  *       The caller should pass NULL for ndlp on NVME side.
22064  *
22065  * Return:
22066  *   pointer to one free IO buf - if private pool is not empty
22067  *   NULL - if private pool is empty
22068  **/
22069 static struct lpfc_io_buf *
lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int hwqid,int expedite)22070 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
22071 				    struct lpfc_nodelist *ndlp,
22072 				    int hwqid, int expedite)
22073 {
22074 	struct lpfc_sli4_hdw_queue *qp;
22075 	struct lpfc_multixri_pool *multixri_pool;
22076 	struct lpfc_pvt_pool *pvt_pool;
22077 	struct lpfc_io_buf *lpfc_ncmd;
22078 
22079 	qp = &phba->sli4_hba.hdwq[hwqid];
22080 	lpfc_ncmd = NULL;
22081 	if (!qp) {
22082 		lpfc_printf_log(phba, KERN_INFO,
22083 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22084 				"5556 NULL qp for hwqid  x%x\n", hwqid);
22085 		return lpfc_ncmd;
22086 	}
22087 	multixri_pool = qp->p_multixri_pool;
22088 	if (!multixri_pool) {
22089 		lpfc_printf_log(phba, KERN_INFO,
22090 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22091 				"5557 NULL multixri for hwqid  x%x\n", hwqid);
22092 		return lpfc_ncmd;
22093 	}
22094 	pvt_pool = &multixri_pool->pvt_pool;
22095 	if (!pvt_pool) {
22096 		lpfc_printf_log(phba, KERN_INFO,
22097 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22098 				"5558 NULL pvt_pool for hwqid  x%x\n", hwqid);
22099 		return lpfc_ncmd;
22100 	}
22101 	multixri_pool->io_req_count++;
22102 
22103 	/* If pvt_pool is empty, move some XRIs from public to private pool */
22104 	if (pvt_pool->count == 0)
22105 		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
22106 
22107 	/* Get one XRI from private free xri pool */
22108 	lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
22109 
22110 	if (lpfc_ncmd) {
22111 		lpfc_ncmd->hdwq = qp;
22112 		lpfc_ncmd->hdwq_no = hwqid;
22113 	} else if (expedite) {
22114 		/* If we fail to get one from pvt_pool and this is an expedite
22115 		 * request, get one free xri from expedite pool.
22116 		 */
22117 		lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
22118 	}
22119 
22120 	return lpfc_ncmd;
22121 }
22122 
22123 static inline struct lpfc_io_buf *
lpfc_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,int idx)22124 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
22125 {
22126 	struct lpfc_sli4_hdw_queue *qp;
22127 	struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
22128 
22129 	qp = &phba->sli4_hba.hdwq[idx];
22130 	list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22131 				 &qp->lpfc_io_buf_list_get, list) {
22132 		if (lpfc_test_rrq_active(phba, ndlp,
22133 					 lpfc_cmd->cur_iocbq.sli4_lxritag))
22134 			continue;
22135 
22136 		if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22137 			continue;
22138 
22139 		list_del_init(&lpfc_cmd->list);
22140 		qp->get_io_bufs--;
22141 		lpfc_cmd->hdwq = qp;
22142 		lpfc_cmd->hdwq_no = idx;
22143 		return lpfc_cmd;
22144 	}
22145 	return NULL;
22146 }
22147 
22148 /**
22149  * lpfc_get_io_buf - Get one IO buffer from free pool
22150  * @phba: The HBA for which this call is being executed.
22151  * @ndlp: pointer to lpfc nodelist data structure.
22152  * @hwqid: belong to which HWQ
22153  * @expedite: 1 means this request is urgent.
22154  *
22155  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22156  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22157  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22158  *
22159  * Note: ndlp is only used on SCSI side for RRQ testing.
22160  *       The caller should pass NULL for ndlp on NVME side.
22161  *
22162  * Return codes:
22163  *   NULL - Error
22164  *   Pointer to lpfc_io_buf - Success
22165  **/
lpfc_get_io_buf(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,u32 hwqid,int expedite)22166 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22167 				    struct lpfc_nodelist *ndlp,
22168 				    u32 hwqid, int expedite)
22169 {
22170 	struct lpfc_sli4_hdw_queue *qp;
22171 	unsigned long iflag;
22172 	struct lpfc_io_buf *lpfc_cmd;
22173 
22174 	qp = &phba->sli4_hba.hdwq[hwqid];
22175 	lpfc_cmd = NULL;
22176 	if (!qp) {
22177 		lpfc_printf_log(phba, KERN_WARNING,
22178 				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22179 				"5555 NULL qp for hwqid  x%x\n", hwqid);
22180 		return lpfc_cmd;
22181 	}
22182 
22183 	if (phba->cfg_xri_rebalancing)
22184 		lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22185 			phba, ndlp, hwqid, expedite);
22186 	else {
22187 		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22188 					  qp, alloc_xri_get);
22189 		if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22190 			lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22191 		if (!lpfc_cmd) {
22192 			lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22193 					  qp, alloc_xri_put);
22194 			list_splice(&qp->lpfc_io_buf_list_put,
22195 				    &qp->lpfc_io_buf_list_get);
22196 			qp->get_io_bufs += qp->put_io_bufs;
22197 			INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22198 			qp->put_io_bufs = 0;
22199 			spin_unlock(&qp->io_buf_list_put_lock);
22200 			if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22201 			    expedite)
22202 				lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22203 		}
22204 		spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22205 	}
22206 
22207 	return lpfc_cmd;
22208 }
22209 
22210 /**
22211  * lpfc_read_object - Retrieve object data from HBA
22212  * @phba: The HBA for which this call is being executed.
22213  * @rdobject: Pathname of object data we want to read.
22214  * @datap: Pointer to where data will be copied to.
22215  * @datasz: size of data area
22216  *
22217  * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22218  * The data will be truncated if datasz is not large enough.
22219  * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22220  * Returns the actual bytes read from the object.
22221  *
22222  * This routine is hard coded to use a poll completion.  Unlike other
22223  * sli4_config mailboxes, it uses lpfc_mbuf memory which is not
22224  * cleaned up in lpfc_sli4_cmd_mbox_free.  If this routine is modified
22225  * to use interrupt-based completions, code is needed to fully cleanup
22226  * the memory.
22227  */
22228 int
lpfc_read_object(struct lpfc_hba * phba,char * rdobject,uint32_t * datap,uint32_t datasz)22229 lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22230 		 uint32_t datasz)
22231 {
22232 	struct lpfc_mbx_read_object *read_object;
22233 	LPFC_MBOXQ_t *mbox;
22234 	int rc, length, eof, j, byte_cnt = 0;
22235 	uint32_t shdr_status, shdr_add_status;
22236 	union lpfc_sli4_cfg_shdr *shdr;
22237 	struct lpfc_dmabuf *pcmd;
22238 	u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
22239 
22240 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22241 	if (!mbox)
22242 		return -ENOMEM;
22243 	length = (sizeof(struct lpfc_mbx_read_object) -
22244 		  sizeof(struct lpfc_sli4_cfg_mhdr));
22245 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22246 			 LPFC_MBOX_OPCODE_READ_OBJECT,
22247 			 length, LPFC_SLI4_MBX_EMBED);
22248 	read_object = &mbox->u.mqe.un.read_object;
22249 	shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22250 
22251 	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22252 	bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22253 	read_object->u.request.rd_object_offset = 0;
22254 	read_object->u.request.rd_object_cnt = 1;
22255 
22256 	memset((void *)read_object->u.request.rd_object_name, 0,
22257 	       LPFC_OBJ_NAME_SZ);
22258 	scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
22259 	for (j = 0; j < strlen(rdobject); j++)
22260 		read_object->u.request.rd_object_name[j] =
22261 			cpu_to_le32(rd_object_name[j]);
22262 
22263 	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22264 	if (pcmd)
22265 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22266 	if (!pcmd || !pcmd->virt) {
22267 		kfree(pcmd);
22268 		mempool_free(mbox, phba->mbox_mem_pool);
22269 		return -ENOMEM;
22270 	}
22271 	memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22272 	read_object->u.request.rd_object_hbuf[0].pa_lo =
22273 		putPaddrLow(pcmd->phys);
22274 	read_object->u.request.rd_object_hbuf[0].pa_hi =
22275 		putPaddrHigh(pcmd->phys);
22276 	read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22277 
22278 	mbox->vport = phba->pport;
22279 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22280 	mbox->ctx_ndlp = NULL;
22281 
22282 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22283 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22284 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22285 
22286 	if (shdr_status == STATUS_FAILED &&
22287 	    shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22288 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22289 				"4674 No port cfg file in FW.\n");
22290 		byte_cnt = -ENOENT;
22291 	} else if (shdr_status || shdr_add_status || rc) {
22292 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22293 				"2625 READ_OBJECT mailbox failed with "
22294 				"status x%x add_status x%x, mbx status x%x\n",
22295 				shdr_status, shdr_add_status, rc);
22296 		byte_cnt = -ENXIO;
22297 	} else {
22298 		/* Success */
22299 		length = read_object->u.response.rd_object_actual_rlen;
22300 		eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22301 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22302 				"2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22303 				length, datasz, eof);
22304 
22305 		/* Detect the port config file exists but is empty */
22306 		if (!length && eof) {
22307 			byte_cnt = 0;
22308 			goto exit;
22309 		}
22310 
22311 		byte_cnt = length;
22312 		lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22313 	}
22314 
22315  exit:
22316 	/* This is an embedded SLI4 mailbox with an external buffer allocated.
22317 	 * Free the pcmd and then cleanup with the correct routine.
22318 	 */
22319 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22320 	kfree(pcmd);
22321 	lpfc_sli4_mbox_cmd_free(phba, mbox);
22322 	return byte_cnt;
22323 }
22324 
22325 /**
22326  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22327  * @phba: The HBA for which this call is being executed.
22328  * @lpfc_buf: IO buf structure to append the SGL chunk
22329  *
22330  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22331  * and will allocate an SGL chunk if the pool is empty.
22332  *
22333  * Return codes:
22334  *   NULL - Error
22335  *   Pointer to sli4_hybrid_sgl - Success
22336  **/
22337 struct sli4_hybrid_sgl *
lpfc_get_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22338 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22339 {
22340 	struct sli4_hybrid_sgl *list_entry = NULL;
22341 	struct sli4_hybrid_sgl *tmp = NULL;
22342 	struct sli4_hybrid_sgl *allocated_sgl = NULL;
22343 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22344 	struct list_head *buf_list = &hdwq->sgl_list;
22345 	unsigned long iflags;
22346 
22347 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22348 
22349 	if (likely(!list_empty(buf_list))) {
22350 		/* break off 1 chunk from the sgl_list */
22351 		list_for_each_entry_safe(list_entry, tmp,
22352 					 buf_list, list_node) {
22353 			list_move_tail(&list_entry->list_node,
22354 				       &lpfc_buf->dma_sgl_xtra_list);
22355 			break;
22356 		}
22357 	} else {
22358 		/* allocate more */
22359 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22360 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22361 				   cpu_to_node(hdwq->io_wq->chann));
22362 		if (!tmp) {
22363 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22364 					"8353 error kmalloc memory for HDWQ "
22365 					"%d %s\n",
22366 					lpfc_buf->hdwq_no, __func__);
22367 			return NULL;
22368 		}
22369 
22370 		tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22371 					      GFP_ATOMIC, &tmp->dma_phys_sgl);
22372 		if (!tmp->dma_sgl) {
22373 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22374 					"8354 error pool_alloc memory for HDWQ "
22375 					"%d %s\n",
22376 					lpfc_buf->hdwq_no, __func__);
22377 			kfree(tmp);
22378 			return NULL;
22379 		}
22380 
22381 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22382 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22383 	}
22384 
22385 	allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22386 					struct sli4_hybrid_sgl,
22387 					list_node);
22388 
22389 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22390 
22391 	return allocated_sgl;
22392 }
22393 
22394 /**
22395  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22396  * @phba: The HBA for which this call is being executed.
22397  * @lpfc_buf: IO buf structure with the SGL chunk
22398  *
22399  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22400  *
22401  * Return codes:
22402  *   0 - Success
22403  *   -EINVAL - Error
22404  **/
22405 int
lpfc_put_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22406 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22407 {
22408 	int rc = 0;
22409 	struct sli4_hybrid_sgl *list_entry = NULL;
22410 	struct sli4_hybrid_sgl *tmp = NULL;
22411 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22412 	struct list_head *buf_list = &hdwq->sgl_list;
22413 	unsigned long iflags;
22414 
22415 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22416 
22417 	if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22418 		list_for_each_entry_safe(list_entry, tmp,
22419 					 &lpfc_buf->dma_sgl_xtra_list,
22420 					 list_node) {
22421 			list_move_tail(&list_entry->list_node,
22422 				       buf_list);
22423 		}
22424 	} else {
22425 		rc = -EINVAL;
22426 	}
22427 
22428 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22429 	return rc;
22430 }
22431 
22432 /**
22433  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22434  * @phba: phba object
22435  * @hdwq: hdwq to cleanup sgl buff resources on
22436  *
22437  * This routine frees all SGL chunks of hdwq SGL chunk pool.
22438  *
22439  * Return codes:
22440  *   None
22441  **/
22442 void
lpfc_free_sgl_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22443 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22444 		       struct lpfc_sli4_hdw_queue *hdwq)
22445 {
22446 	struct list_head *buf_list = &hdwq->sgl_list;
22447 	struct sli4_hybrid_sgl *list_entry = NULL;
22448 	struct sli4_hybrid_sgl *tmp = NULL;
22449 	unsigned long iflags;
22450 
22451 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22452 
22453 	/* Free sgl pool */
22454 	list_for_each_entry_safe(list_entry, tmp,
22455 				 buf_list, list_node) {
22456 		list_del(&list_entry->list_node);
22457 		dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22458 			      list_entry->dma_sgl,
22459 			      list_entry->dma_phys_sgl);
22460 		kfree(list_entry);
22461 	}
22462 
22463 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22464 }
22465 
22466 /**
22467  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22468  * @phba: The HBA for which this call is being executed.
22469  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22470  *
22471  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22472  * and will allocate an CMD/RSP buffer if the pool is empty.
22473  *
22474  * Return codes:
22475  *   NULL - Error
22476  *   Pointer to fcp_cmd_rsp_buf - Success
22477  **/
22478 struct fcp_cmd_rsp_buf *
lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22479 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22480 			      struct lpfc_io_buf *lpfc_buf)
22481 {
22482 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22483 	struct fcp_cmd_rsp_buf *tmp = NULL;
22484 	struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22485 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22486 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22487 	unsigned long iflags;
22488 
22489 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22490 
22491 	if (likely(!list_empty(buf_list))) {
22492 		/* break off 1 chunk from the list */
22493 		list_for_each_entry_safe(list_entry, tmp,
22494 					 buf_list,
22495 					 list_node) {
22496 			list_move_tail(&list_entry->list_node,
22497 				       &lpfc_buf->dma_cmd_rsp_list);
22498 			break;
22499 		}
22500 	} else {
22501 		/* allocate more */
22502 		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22503 		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22504 				   cpu_to_node(hdwq->io_wq->chann));
22505 		if (!tmp) {
22506 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22507 					"8355 error kmalloc memory for HDWQ "
22508 					"%d %s\n",
22509 					lpfc_buf->hdwq_no, __func__);
22510 			return NULL;
22511 		}
22512 
22513 		tmp->fcp_cmnd = dma_pool_zalloc(phba->lpfc_cmd_rsp_buf_pool,
22514 						GFP_ATOMIC,
22515 						&tmp->fcp_cmd_rsp_dma_handle);
22516 
22517 		if (!tmp->fcp_cmnd) {
22518 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22519 					"8356 error pool_alloc memory for HDWQ "
22520 					"%d %s\n",
22521 					lpfc_buf->hdwq_no, __func__);
22522 			kfree(tmp);
22523 			return NULL;
22524 		}
22525 
22526 		tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22527 				sizeof(struct fcp_cmnd32));
22528 
22529 		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22530 		list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22531 	}
22532 
22533 	allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22534 					struct fcp_cmd_rsp_buf,
22535 					list_node);
22536 
22537 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22538 
22539 	return allocated_buf;
22540 }
22541 
22542 /**
22543  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22544  * @phba: The HBA for which this call is being executed.
22545  * @lpfc_buf: IO buf structure with the CMD/RSP buf
22546  *
22547  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22548  *
22549  * Return codes:
22550  *   0 - Success
22551  *   -EINVAL - Error
22552  **/
22553 int
lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_io_buf * lpfc_buf)22554 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22555 			      struct lpfc_io_buf *lpfc_buf)
22556 {
22557 	int rc = 0;
22558 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22559 	struct fcp_cmd_rsp_buf *tmp = NULL;
22560 	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22561 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22562 	unsigned long iflags;
22563 
22564 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22565 
22566 	if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22567 		list_for_each_entry_safe(list_entry, tmp,
22568 					 &lpfc_buf->dma_cmd_rsp_list,
22569 					 list_node) {
22570 			list_move_tail(&list_entry->list_node,
22571 				       buf_list);
22572 		}
22573 	} else {
22574 		rc = -EINVAL;
22575 	}
22576 
22577 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22578 	return rc;
22579 }
22580 
22581 /**
22582  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22583  * @phba: phba object
22584  * @hdwq: hdwq to cleanup cmd rsp buff resources on
22585  *
22586  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22587  *
22588  * Return codes:
22589  *   None
22590  **/
22591 void
lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba * phba,struct lpfc_sli4_hdw_queue * hdwq)22592 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22593 			       struct lpfc_sli4_hdw_queue *hdwq)
22594 {
22595 	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22596 	struct fcp_cmd_rsp_buf *list_entry = NULL;
22597 	struct fcp_cmd_rsp_buf *tmp = NULL;
22598 	unsigned long iflags;
22599 
22600 	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22601 
22602 	/* Free cmd_rsp buf pool */
22603 	list_for_each_entry_safe(list_entry, tmp,
22604 				 buf_list,
22605 				 list_node) {
22606 		list_del(&list_entry->list_node);
22607 		dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22608 			      list_entry->fcp_cmnd,
22609 			      list_entry->fcp_cmd_rsp_dma_handle);
22610 		kfree(list_entry);
22611 	}
22612 
22613 	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22614 }
22615 
22616 /**
22617  * lpfc_sli_prep_wqe - Prepare WQE for the command to be posted
22618  * @phba: phba object
22619  * @job: job entry of the command to be posted.
22620  *
22621  * Fill the common fields of the wqe for each of the command.
22622  *
22623  * Return codes:
22624  *	None
22625  **/
22626 void
lpfc_sli_prep_wqe(struct lpfc_hba * phba,struct lpfc_iocbq * job)22627 lpfc_sli_prep_wqe(struct lpfc_hba *phba, struct lpfc_iocbq *job)
22628 {
22629 	u8 cmnd;
22630 	u32 *pcmd;
22631 	u32 if_type = 0;
22632 	u32 abort_tag;
22633 	bool fip;
22634 	struct lpfc_nodelist *ndlp = NULL;
22635 	union lpfc_wqe128 *wqe = &job->wqe;
22636 	u8 command_type = ELS_COMMAND_NON_FIP;
22637 
22638 	fip = test_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
22639 	/* The fcp commands will set command type */
22640 	if (job->cmd_flag &  LPFC_IO_FCP)
22641 		command_type = FCP_COMMAND;
22642 	else if (fip && (job->cmd_flag & LPFC_FIP_ELS_ID_MASK))
22643 		command_type = ELS_COMMAND_FIP;
22644 	else
22645 		command_type = ELS_COMMAND_NON_FIP;
22646 
22647 	abort_tag = job->iotag;
22648 	cmnd = bf_get(wqe_cmnd, &wqe->els_req.wqe_com);
22649 
22650 	switch (cmnd) {
22651 	case CMD_ELS_REQUEST64_WQE:
22652 		ndlp = job->ndlp;
22653 
22654 		if_type = bf_get(lpfc_sli_intf_if_type,
22655 				 &phba->sli4_hba.sli_intf);
22656 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22657 			pcmd = (u32 *)job->cmd_dmabuf->virt;
22658 			if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
22659 				     *pcmd == ELS_CMD_SCR ||
22660 				     *pcmd == ELS_CMD_RDF ||
22661 				     *pcmd == ELS_CMD_EDC ||
22662 				     *pcmd == ELS_CMD_RSCN_XMT ||
22663 				     *pcmd == ELS_CMD_FDISC ||
22664 				     *pcmd == ELS_CMD_LOGO ||
22665 				     *pcmd == ELS_CMD_QFPA ||
22666 				     *pcmd == ELS_CMD_UVEM ||
22667 				     *pcmd == ELS_CMD_PLOGI)) {
22668 				bf_set(els_req64_sp, &wqe->els_req, 1);
22669 				bf_set(els_req64_sid, &wqe->els_req,
22670 				       job->vport->fc_myDID);
22671 
22672 				if ((*pcmd == ELS_CMD_FLOGI) &&
22673 				    !(phba->fc_topology ==
22674 				      LPFC_TOPOLOGY_LOOP))
22675 					bf_set(els_req64_sid, &wqe->els_req, 0);
22676 
22677 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
22678 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22679 				       phba->vpi_ids[job->vport->vpi]);
22680 			} else if (pcmd) {
22681 				bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
22682 				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22683 				       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22684 			}
22685 		}
22686 
22687 		bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
22688 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22689 
22690 		bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
22691 		bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
22692 		bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
22693 		bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22694 		bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
22695 		break;
22696 	case CMD_XMIT_ELS_RSP64_WQE:
22697 		ndlp = job->ndlp;
22698 
22699 		/* word4 */
22700 		wqe->xmit_els_rsp.word4 = 0;
22701 
22702 		if_type = bf_get(lpfc_sli_intf_if_type,
22703 				 &phba->sli4_hba.sli_intf);
22704 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22705 			if (test_bit(FC_PT2PT, &job->vport->fc_flag)) {
22706 				bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22707 				bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22708 				       job->vport->fc_myDID);
22709 				if (job->vport->fc_myDID == Fabric_DID) {
22710 					bf_set(wqe_els_did,
22711 					       &wqe->xmit_els_rsp.wqe_dest, 0);
22712 				}
22713 			}
22714 		}
22715 
22716 		bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
22717 		bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
22718 		bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
22719 		bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
22720 		       LPFC_WQE_LENLOC_WORD3);
22721 		bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
22722 
22723 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
22724 			bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22725 			bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22726 			       job->vport->fc_myDID);
22727 			bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
22728 		}
22729 
22730 		if (phba->sli_rev == LPFC_SLI_REV4) {
22731 			bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
22732 			       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22733 
22734 			if (bf_get(wqe_ct, &wqe->xmit_els_rsp.wqe_com))
22735 				bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
22736 				       phba->vpi_ids[job->vport->vpi]);
22737 		}
22738 		command_type = OTHER_COMMAND;
22739 		break;
22740 	case CMD_GEN_REQUEST64_WQE:
22741 		/* Word 10 */
22742 		bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
22743 		bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
22744 		bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
22745 		bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22746 		bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
22747 		command_type = OTHER_COMMAND;
22748 		break;
22749 	case CMD_XMIT_SEQUENCE64_WQE:
22750 		if (phba->link_flag & LS_LOOPBACK_MODE)
22751 			bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
22752 
22753 		wqe->xmit_sequence.rsvd3 = 0;
22754 		bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
22755 		bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
22756 		bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
22757 		       LPFC_WQE_IOD_WRITE);
22758 		bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
22759 		       LPFC_WQE_LENLOC_WORD12);
22760 		bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
22761 		command_type = OTHER_COMMAND;
22762 		break;
22763 	case CMD_XMIT_BLS_RSP64_WQE:
22764 		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
22765 		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
22766 		bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
22767 		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
22768 		       phba->vpi_ids[phba->pport->vpi]);
22769 		bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
22770 		bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
22771 		       LPFC_WQE_LENLOC_NONE);
22772 		/* Overwrite the pre-set comnd type with OTHER_COMMAND */
22773 		command_type = OTHER_COMMAND;
22774 		break;
22775 	case CMD_FCP_ICMND64_WQE:	/* task mgmt commands */
22776 	case CMD_ABORT_XRI_WQE:		/* abort iotag */
22777 	case CMD_SEND_FRAME:		/* mds loopback */
22778 		/* cases already formatted for sli4 wqe - no chgs necessary */
22779 		return;
22780 	default:
22781 		dump_stack();
22782 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
22783 				"6207 Invalid command 0x%x\n",
22784 				cmnd);
22785 		break;
22786 	}
22787 
22788 	wqe->generic.wqe_com.abort_tag = abort_tag;
22789 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, job->iotag);
22790 	bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
22791 	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
22792 }
22793