xref: /linux/drivers/scsi/lpfc/lpfc_sli.c (revision 5d4a2e29fba5b2bef95b96a46b338ec4d76fa4fd)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21 
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35 
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49 
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52 	LPFC_UNKNOWN_IOCB,
53 	LPFC_UNSOL_IOCB,
54 	LPFC_SOL_IOCB,
55 	LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57 
58 
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 				  uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 			      uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 							 struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 				      struct hbq_dmabuf *);
68 static IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71 	return &iocbq->iocb;
72 }
73 
74 /**
75  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76  * @q: The Work Queue to operate on.
77  * @wqe: The work Queue Entry to put on the Work queue.
78  *
79  * This routine will copy the contents of @wqe to the next available entry on
80  * the @q. This function will then ring the Work Queue Doorbell to signal the
81  * HBA to start processing the Work Queue Entry. This function returns 0 if
82  * successful. If no entries are available on @q then this function will return
83  * -ENOMEM.
84  * The caller is expected to hold the hbalock when calling this routine.
85  **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89 	union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 	struct lpfc_register doorbell;
91 	uint32_t host_index;
92 
93 	/* If the host has not yet processed the next entry then we are done */
94 	if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 		return -ENOMEM;
96 	/* set consumption flag every once in a while */
97 	if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98 		bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99 
100 	lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101 
102 	/* Update the host index before invoking device */
103 	host_index = q->host_index;
104 	q->host_index = ((q->host_index + 1) % q->entry_count);
105 
106 	/* Ring Doorbell */
107 	doorbell.word0 = 0;
108 	bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 	bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 	bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 	writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 	readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113 
114 	return 0;
115 }
116 
117 /**
118  * lpfc_sli4_wq_release - Updates internal hba index for WQ
119  * @q: The Work Queue to operate on.
120  * @index: The index to advance the hba index to.
121  *
122  * This routine will update the HBA index of a queue to reflect consumption of
123  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124  * an entry the host calls this function to update the queue's internal
125  * pointers. This routine returns the number of entries that were consumed by
126  * the HBA.
127  **/
128 static uint32_t
129 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130 {
131 	uint32_t released = 0;
132 
133 	if (q->hba_index == index)
134 		return 0;
135 	do {
136 		q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 		released++;
138 	} while (q->hba_index != index);
139 	return released;
140 }
141 
142 /**
143  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144  * @q: The Mailbox Queue to operate on.
145  * @wqe: The Mailbox Queue Entry to put on the Work queue.
146  *
147  * This routine will copy the contents of @mqe to the next available entry on
148  * the @q. This function will then ring the Work Queue Doorbell to signal the
149  * HBA to start processing the Work Queue Entry. This function returns 0 if
150  * successful. If no entries are available on @q then this function will return
151  * -ENOMEM.
152  * The caller is expected to hold the hbalock when calling this routine.
153  **/
154 static uint32_t
155 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156 {
157 	struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 	struct lpfc_register doorbell;
159 	uint32_t host_index;
160 
161 	/* If the host has not yet processed the next entry then we are done */
162 	if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 		return -ENOMEM;
164 	lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 	/* Save off the mailbox pointer for completion */
166 	q->phba->mbox = (MAILBOX_t *)temp_mqe;
167 
168 	/* Update the host index before invoking device */
169 	host_index = q->host_index;
170 	q->host_index = ((q->host_index + 1) % q->entry_count);
171 
172 	/* Ring Doorbell */
173 	doorbell.word0 = 0;
174 	bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 	bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 	writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 	readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 	return 0;
179 }
180 
181 /**
182  * lpfc_sli4_mq_release - Updates internal hba index for MQ
183  * @q: The Mailbox Queue to operate on.
184  *
185  * This routine will update the HBA index of a queue to reflect consumption of
186  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187  * an entry the host calls this function to update the queue's internal
188  * pointers. This routine returns the number of entries that were consumed by
189  * the HBA.
190  **/
191 static uint32_t
192 lpfc_sli4_mq_release(struct lpfc_queue *q)
193 {
194 	/* Clear the mailbox pointer for completion */
195 	q->phba->mbox = NULL;
196 	q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 	return 1;
198 }
199 
200 /**
201  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202  * @q: The Event Queue to get the first valid EQE from
203  *
204  * This routine will get the first valid Event Queue Entry from @q, update
205  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206  * the Queue (no more work to do), or the Queue is full of EQEs that have been
207  * processed, but not popped back to the HBA then this routine will return NULL.
208  **/
209 static struct lpfc_eqe *
210 lpfc_sli4_eq_get(struct lpfc_queue *q)
211 {
212 	struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213 
214 	/* If the next EQE is not valid then we are done */
215 	if (!bf_get_le32(lpfc_eqe_valid, eqe))
216 		return NULL;
217 	/* If the host has not yet processed the next entry then we are done */
218 	if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 		return NULL;
220 
221 	q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 	return eqe;
223 }
224 
225 /**
226  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227  * @q: The Event Queue that the host has completed processing for.
228  * @arm: Indicates whether the host wants to arms this CQ.
229  *
230  * This routine will mark all Event Queue Entries on @q, from the last
231  * known completed entry to the last entry that was processed, as completed
232  * by clearing the valid bit for each completion queue entry. Then it will
233  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234  * The internal host index in the @q will be updated by this routine to indicate
235  * that the host has finished processing the entries. The @arm parameter
236  * indicates that the queue should be rearmed when ringing the doorbell.
237  *
238  * This function will return the number of EQEs that were popped.
239  **/
240 uint32_t
241 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242 {
243 	uint32_t released = 0;
244 	struct lpfc_eqe *temp_eqe;
245 	struct lpfc_register doorbell;
246 
247 	/* while there are valid entries */
248 	while (q->hba_index != q->host_index) {
249 		temp_eqe = q->qe[q->host_index].eqe;
250 		bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
251 		released++;
252 		q->host_index = ((q->host_index + 1) % q->entry_count);
253 	}
254 	if (unlikely(released == 0 && !arm))
255 		return 0;
256 
257 	/* ring doorbell for number popped */
258 	doorbell.word0 = 0;
259 	if (arm) {
260 		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 		bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 	}
263 	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 	bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 	writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
267 	/* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 		readl(q->phba->sli4_hba.EQCQDBregaddr);
270 	return released;
271 }
272 
273 /**
274  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275  * @q: The Completion Queue to get the first valid CQE from
276  *
277  * This routine will get the first valid Completion Queue Entry from @q, update
278  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279  * the Queue (no more work to do), or the Queue is full of CQEs that have been
280  * processed, but not popped back to the HBA then this routine will return NULL.
281  **/
282 static struct lpfc_cqe *
283 lpfc_sli4_cq_get(struct lpfc_queue *q)
284 {
285 	struct lpfc_cqe *cqe;
286 
287 	/* If the next CQE is not valid then we are done */
288 	if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
289 		return NULL;
290 	/* If the host has not yet processed the next entry then we are done */
291 	if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 		return NULL;
293 
294 	cqe = q->qe[q->hba_index].cqe;
295 	q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 	return cqe;
297 }
298 
299 /**
300  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301  * @q: The Completion Queue that the host has completed processing for.
302  * @arm: Indicates whether the host wants to arms this CQ.
303  *
304  * This routine will mark all Completion queue entries on @q, from the last
305  * known completed entry to the last entry that was processed, as completed
306  * by clearing the valid bit for each completion queue entry. Then it will
307  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308  * The internal host index in the @q will be updated by this routine to indicate
309  * that the host has finished processing the entries. The @arm parameter
310  * indicates that the queue should be rearmed when ringing the doorbell.
311  *
312  * This function will return the number of CQEs that were released.
313  **/
314 uint32_t
315 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316 {
317 	uint32_t released = 0;
318 	struct lpfc_cqe *temp_qe;
319 	struct lpfc_register doorbell;
320 
321 	/* while there are valid entries */
322 	while (q->hba_index != q->host_index) {
323 		temp_qe = q->qe[q->host_index].cqe;
324 		bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
325 		released++;
326 		q->host_index = ((q->host_index + 1) % q->entry_count);
327 	}
328 	if (unlikely(released == 0 && !arm))
329 		return 0;
330 
331 	/* ring doorbell for number popped */
332 	doorbell.word0 = 0;
333 	if (arm)
334 		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 	bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 	writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 	return released;
340 }
341 
342 /**
343  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344  * @q: The Header Receive Queue to operate on.
345  * @wqe: The Receive Queue Entry to put on the Receive queue.
346  *
347  * This routine will copy the contents of @wqe to the next available entry on
348  * the @q. This function will then ring the Receive Queue Doorbell to signal the
349  * HBA to start processing the Receive Queue Entry. This function returns the
350  * index that the rqe was copied to if successful. If no entries are available
351  * on @q then this function will return -ENOMEM.
352  * The caller is expected to hold the hbalock when calling this routine.
353  **/
354 static int
355 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 		 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357 {
358 	struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 	struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 	struct lpfc_register doorbell;
361 	int put_index = hq->host_index;
362 
363 	if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 		return -EINVAL;
365 	if (hq->host_index != dq->host_index)
366 		return -EINVAL;
367 	/* If the host has not yet processed the next entry then we are done */
368 	if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 		return -EBUSY;
370 	lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 	lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372 
373 	/* Update the host index to point to the next slot */
374 	hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 	dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376 
377 	/* Ring The Header Receive Queue Doorbell */
378 	if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 		doorbell.word0 = 0;
380 		bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 		       LPFC_RQ_POST_BATCH);
382 		bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 		writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 	}
385 	return put_index;
386 }
387 
388 /**
389  * lpfc_sli4_rq_release - Updates internal hba index for RQ
390  * @q: The Header Receive Queue to operate on.
391  *
392  * This routine will update the HBA index of a queue to reflect consumption of
393  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394  * consumed an entry the host calls this function to update the queue's
395  * internal pointers. This routine returns the number of entries that were
396  * consumed by the HBA.
397  **/
398 static uint32_t
399 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400 {
401 	if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 		return 0;
403 	hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 	dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 	return 1;
406 }
407 
408 /**
409  * lpfc_cmd_iocb - Get next command iocb entry in the ring
410  * @phba: Pointer to HBA context object.
411  * @pring: Pointer to driver SLI ring object.
412  *
413  * This function returns pointer to next command iocb entry
414  * in the command ring. The caller must hold hbalock to prevent
415  * other threads consume the next command iocb.
416  * SLI-2/SLI-3 provide different sized iocbs.
417  **/
418 static inline IOCB_t *
419 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420 {
421 	return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 			   pring->cmdidx * phba->iocb_cmd_size);
423 }
424 
425 /**
426  * lpfc_resp_iocb - Get next response iocb entry in the ring
427  * @phba: Pointer to HBA context object.
428  * @pring: Pointer to driver SLI ring object.
429  *
430  * This function returns pointer to next response iocb entry
431  * in the response ring. The caller must hold hbalock to make sure
432  * that no other thread consume the next response iocb.
433  * SLI-2/SLI-3 provide different sized iocbs.
434  **/
435 static inline IOCB_t *
436 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437 {
438 	return (IOCB_t *) (((char *) pring->rspringaddr) +
439 			   pring->rspidx * phba->iocb_rsp_size);
440 }
441 
442 /**
443  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
444  * @phba: Pointer to HBA context object.
445  *
446  * This function is called with hbalock held. This function
447  * allocates a new driver iocb object from the iocb pool. If the
448  * allocation is successful, it returns pointer to the newly
449  * allocated iocb object else it returns NULL.
450  **/
451 static struct lpfc_iocbq *
452 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
453 {
454 	struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 	struct lpfc_iocbq * iocbq = NULL;
456 
457 	list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
458 
459 	if (iocbq)
460 		phba->iocb_cnt++;
461 	if (phba->iocb_cnt > phba->iocb_max)
462 		phba->iocb_max = phba->iocb_cnt;
463 	return iocbq;
464 }
465 
466 /**
467  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
468  * @phba: Pointer to HBA context object.
469  * @xritag: XRI value.
470  *
471  * This function clears the sglq pointer from the array of acive
472  * sglq's. The xritag that is passed in is used to index into the
473  * array. Before the xritag can be used it needs to be adjusted
474  * by subtracting the xribase.
475  *
476  * Returns sglq ponter = success, NULL = Failure.
477  **/
478 static struct lpfc_sglq *
479 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
480 {
481 	uint16_t adj_xri;
482 	struct lpfc_sglq *sglq;
483 	adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
484 	if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
485 		return NULL;
486 	sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
487 	phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
488 	return sglq;
489 }
490 
491 /**
492  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493  * @phba: Pointer to HBA context object.
494  * @xritag: XRI value.
495  *
496  * This function returns the sglq pointer from the array of acive
497  * sglq's. The xritag that is passed in is used to index into the
498  * array. Before the xritag can be used it needs to be adjusted
499  * by subtracting the xribase.
500  *
501  * Returns sglq ponter = success, NULL = Failure.
502  **/
503 struct lpfc_sglq *
504 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505 {
506 	uint16_t adj_xri;
507 	struct lpfc_sglq *sglq;
508 	adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
509 	if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
510 		return NULL;
511 	sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
512 	return sglq;
513 }
514 
515 /**
516  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
517  * @phba: Pointer to HBA context object.
518  *
519  * This function is called with hbalock held. This function
520  * Gets a new driver sglq object from the sglq list. If the
521  * list is not empty then it is successful, it returns pointer to the newly
522  * allocated sglq object else it returns NULL.
523  **/
524 static struct lpfc_sglq *
525 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
526 {
527 	struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
528 	struct lpfc_sglq *sglq = NULL;
529 	uint16_t adj_xri;
530 	list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
531 	if (!sglq)
532 		return NULL;
533 	adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
534 	phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
535 	sglq->state = SGL_ALLOCATED;
536 	return sglq;
537 }
538 
539 /**
540  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
541  * @phba: Pointer to HBA context object.
542  *
543  * This function is called with no lock held. This function
544  * allocates a new driver iocb object from the iocb pool. If the
545  * allocation is successful, it returns pointer to the newly
546  * allocated iocb object else it returns NULL.
547  **/
548 struct lpfc_iocbq *
549 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
550 {
551 	struct lpfc_iocbq * iocbq = NULL;
552 	unsigned long iflags;
553 
554 	spin_lock_irqsave(&phba->hbalock, iflags);
555 	iocbq = __lpfc_sli_get_iocbq(phba);
556 	spin_unlock_irqrestore(&phba->hbalock, iflags);
557 	return iocbq;
558 }
559 
560 /**
561  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
562  * @phba: Pointer to HBA context object.
563  * @iocbq: Pointer to driver iocb object.
564  *
565  * This function is called with hbalock held to release driver
566  * iocb object to the iocb pool. The iotag in the iocb object
567  * does not change for each use of the iocb object. This function
568  * clears all other fields of the iocb object when it is freed.
569  * The sqlq structure that holds the xritag and phys and virtual
570  * mappings for the scatter gather list is retrieved from the
571  * active array of sglq. The get of the sglq pointer also clears
572  * the entry in the array. If the status of the IO indiactes that
573  * this IO was aborted then the sglq entry it put on the
574  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
575  * IO has good status or fails for any other reason then the sglq
576  * entry is added to the free list (lpfc_sgl_list).
577  **/
578 static void
579 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
580 {
581 	struct lpfc_sglq *sglq;
582 	size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
583 	unsigned long iflag = 0;
584 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
585 
586 	if (iocbq->sli4_xritag == NO_XRI)
587 		sglq = NULL;
588 	else
589 		sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
590 	if (sglq)  {
591 		if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
592 			(sglq->state != SGL_XRI_ABORTED)) {
593 			spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
594 					iflag);
595 			list_add(&sglq->list,
596 				&phba->sli4_hba.lpfc_abts_els_sgl_list);
597 			spin_unlock_irqrestore(
598 				&phba->sli4_hba.abts_sgl_list_lock, iflag);
599 		} else {
600 			sglq->state = SGL_FREED;
601 			list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
602 
603 			/* Check if TXQ queue needs to be serviced */
604 			if (pring->txq_cnt) {
605 				spin_lock_irqsave(
606 					&phba->pport->work_port_lock, iflag);
607 				phba->pport->work_port_events |=
608 					WORKER_SERVICE_TXQ;
609 				lpfc_worker_wake_up(phba);
610 				spin_unlock_irqrestore(
611 					&phba->pport->work_port_lock, iflag);
612 			}
613 		}
614 	}
615 
616 
617 	/*
618 	 * Clean all volatile data fields, preserve iotag and node struct.
619 	 */
620 	memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
621 	iocbq->sli4_xritag = NO_XRI;
622 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
623 }
624 
625 
626 /**
627  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
628  * @phba: Pointer to HBA context object.
629  * @iocbq: Pointer to driver iocb object.
630  *
631  * This function is called with hbalock held to release driver
632  * iocb object to the iocb pool. The iotag in the iocb object
633  * does not change for each use of the iocb object. This function
634  * clears all other fields of the iocb object when it is freed.
635  **/
636 static void
637 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
638 {
639 	size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
640 
641 	/*
642 	 * Clean all volatile data fields, preserve iotag and node struct.
643 	 */
644 	memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
645 	iocbq->sli4_xritag = NO_XRI;
646 	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
647 }
648 
649 /**
650  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
651  * @phba: Pointer to HBA context object.
652  * @iocbq: Pointer to driver iocb object.
653  *
654  * This function is called with hbalock held to release driver
655  * iocb object to the iocb pool. The iotag in the iocb object
656  * does not change for each use of the iocb object. This function
657  * clears all other fields of the iocb object when it is freed.
658  **/
659 static void
660 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
661 {
662 	phba->__lpfc_sli_release_iocbq(phba, iocbq);
663 	phba->iocb_cnt--;
664 }
665 
666 /**
667  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
668  * @phba: Pointer to HBA context object.
669  * @iocbq: Pointer to driver iocb object.
670  *
671  * This function is called with no lock held to release the iocb to
672  * iocb pool.
673  **/
674 void
675 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
676 {
677 	unsigned long iflags;
678 
679 	/*
680 	 * Clean all volatile data fields, preserve iotag and node struct.
681 	 */
682 	spin_lock_irqsave(&phba->hbalock, iflags);
683 	__lpfc_sli_release_iocbq(phba, iocbq);
684 	spin_unlock_irqrestore(&phba->hbalock, iflags);
685 }
686 
687 /**
688  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
689  * @phba: Pointer to HBA context object.
690  * @iocblist: List of IOCBs.
691  * @ulpstatus: ULP status in IOCB command field.
692  * @ulpWord4: ULP word-4 in IOCB command field.
693  *
694  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
695  * on the list by invoking the complete callback function associated with the
696  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
697  * fields.
698  **/
699 void
700 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
701 		      uint32_t ulpstatus, uint32_t ulpWord4)
702 {
703 	struct lpfc_iocbq *piocb;
704 
705 	while (!list_empty(iocblist)) {
706 		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
707 
708 		if (!piocb->iocb_cmpl)
709 			lpfc_sli_release_iocbq(phba, piocb);
710 		else {
711 			piocb->iocb.ulpStatus = ulpstatus;
712 			piocb->iocb.un.ulpWord[4] = ulpWord4;
713 			(piocb->iocb_cmpl) (phba, piocb, piocb);
714 		}
715 	}
716 	return;
717 }
718 
719 /**
720  * lpfc_sli_iocb_cmd_type - Get the iocb type
721  * @iocb_cmnd: iocb command code.
722  *
723  * This function is called by ring event handler function to get the iocb type.
724  * This function translates the iocb command to an iocb command type used to
725  * decide the final disposition of each completed IOCB.
726  * The function returns
727  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
728  * LPFC_SOL_IOCB     if it is a solicited iocb completion
729  * LPFC_ABORT_IOCB   if it is an abort iocb
730  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
731  *
732  * The caller is not required to hold any lock.
733  **/
734 static lpfc_iocb_type
735 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
736 {
737 	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
738 
739 	if (iocb_cmnd > CMD_MAX_IOCB_CMD)
740 		return 0;
741 
742 	switch (iocb_cmnd) {
743 	case CMD_XMIT_SEQUENCE_CR:
744 	case CMD_XMIT_SEQUENCE_CX:
745 	case CMD_XMIT_BCAST_CN:
746 	case CMD_XMIT_BCAST_CX:
747 	case CMD_ELS_REQUEST_CR:
748 	case CMD_ELS_REQUEST_CX:
749 	case CMD_CREATE_XRI_CR:
750 	case CMD_CREATE_XRI_CX:
751 	case CMD_GET_RPI_CN:
752 	case CMD_XMIT_ELS_RSP_CX:
753 	case CMD_GET_RPI_CR:
754 	case CMD_FCP_IWRITE_CR:
755 	case CMD_FCP_IWRITE_CX:
756 	case CMD_FCP_IREAD_CR:
757 	case CMD_FCP_IREAD_CX:
758 	case CMD_FCP_ICMND_CR:
759 	case CMD_FCP_ICMND_CX:
760 	case CMD_FCP_TSEND_CX:
761 	case CMD_FCP_TRSP_CX:
762 	case CMD_FCP_TRECEIVE_CX:
763 	case CMD_FCP_AUTO_TRSP_CX:
764 	case CMD_ADAPTER_MSG:
765 	case CMD_ADAPTER_DUMP:
766 	case CMD_XMIT_SEQUENCE64_CR:
767 	case CMD_XMIT_SEQUENCE64_CX:
768 	case CMD_XMIT_BCAST64_CN:
769 	case CMD_XMIT_BCAST64_CX:
770 	case CMD_ELS_REQUEST64_CR:
771 	case CMD_ELS_REQUEST64_CX:
772 	case CMD_FCP_IWRITE64_CR:
773 	case CMD_FCP_IWRITE64_CX:
774 	case CMD_FCP_IREAD64_CR:
775 	case CMD_FCP_IREAD64_CX:
776 	case CMD_FCP_ICMND64_CR:
777 	case CMD_FCP_ICMND64_CX:
778 	case CMD_FCP_TSEND64_CX:
779 	case CMD_FCP_TRSP64_CX:
780 	case CMD_FCP_TRECEIVE64_CX:
781 	case CMD_GEN_REQUEST64_CR:
782 	case CMD_GEN_REQUEST64_CX:
783 	case CMD_XMIT_ELS_RSP64_CX:
784 	case DSSCMD_IWRITE64_CR:
785 	case DSSCMD_IWRITE64_CX:
786 	case DSSCMD_IREAD64_CR:
787 	case DSSCMD_IREAD64_CX:
788 		type = LPFC_SOL_IOCB;
789 		break;
790 	case CMD_ABORT_XRI_CN:
791 	case CMD_ABORT_XRI_CX:
792 	case CMD_CLOSE_XRI_CN:
793 	case CMD_CLOSE_XRI_CX:
794 	case CMD_XRI_ABORTED_CX:
795 	case CMD_ABORT_MXRI64_CN:
796 	case CMD_XMIT_BLS_RSP64_CX:
797 		type = LPFC_ABORT_IOCB;
798 		break;
799 	case CMD_RCV_SEQUENCE_CX:
800 	case CMD_RCV_ELS_REQ_CX:
801 	case CMD_RCV_SEQUENCE64_CX:
802 	case CMD_RCV_ELS_REQ64_CX:
803 	case CMD_ASYNC_STATUS:
804 	case CMD_IOCB_RCV_SEQ64_CX:
805 	case CMD_IOCB_RCV_ELS64_CX:
806 	case CMD_IOCB_RCV_CONT64_CX:
807 	case CMD_IOCB_RET_XRI64_CX:
808 		type = LPFC_UNSOL_IOCB;
809 		break;
810 	case CMD_IOCB_XMIT_MSEQ64_CR:
811 	case CMD_IOCB_XMIT_MSEQ64_CX:
812 	case CMD_IOCB_RCV_SEQ_LIST64_CX:
813 	case CMD_IOCB_RCV_ELS_LIST64_CX:
814 	case CMD_IOCB_CLOSE_EXTENDED_CN:
815 	case CMD_IOCB_ABORT_EXTENDED_CN:
816 	case CMD_IOCB_RET_HBQE64_CN:
817 	case CMD_IOCB_FCP_IBIDIR64_CR:
818 	case CMD_IOCB_FCP_IBIDIR64_CX:
819 	case CMD_IOCB_FCP_ITASKMGT64_CX:
820 	case CMD_IOCB_LOGENTRY_CN:
821 	case CMD_IOCB_LOGENTRY_ASYNC_CN:
822 		printk("%s - Unhandled SLI-3 Command x%x\n",
823 				__func__, iocb_cmnd);
824 		type = LPFC_UNKNOWN_IOCB;
825 		break;
826 	default:
827 		type = LPFC_UNKNOWN_IOCB;
828 		break;
829 	}
830 
831 	return type;
832 }
833 
834 /**
835  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
836  * @phba: Pointer to HBA context object.
837  *
838  * This function is called from SLI initialization code
839  * to configure every ring of the HBA's SLI interface. The
840  * caller is not required to hold any lock. This function issues
841  * a config_ring mailbox command for each ring.
842  * This function returns zero if successful else returns a negative
843  * error code.
844  **/
845 static int
846 lpfc_sli_ring_map(struct lpfc_hba *phba)
847 {
848 	struct lpfc_sli *psli = &phba->sli;
849 	LPFC_MBOXQ_t *pmb;
850 	MAILBOX_t *pmbox;
851 	int i, rc, ret = 0;
852 
853 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
854 	if (!pmb)
855 		return -ENOMEM;
856 	pmbox = &pmb->u.mb;
857 	phba->link_state = LPFC_INIT_MBX_CMDS;
858 	for (i = 0; i < psli->num_rings; i++) {
859 		lpfc_config_ring(phba, i, pmb);
860 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
861 		if (rc != MBX_SUCCESS) {
862 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
863 					"0446 Adapter failed to init (%d), "
864 					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
865 					"ring %d\n",
866 					rc, pmbox->mbxCommand,
867 					pmbox->mbxStatus, i);
868 			phba->link_state = LPFC_HBA_ERROR;
869 			ret = -ENXIO;
870 			break;
871 		}
872 	}
873 	mempool_free(pmb, phba->mbox_mem_pool);
874 	return ret;
875 }
876 
877 /**
878  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
879  * @phba: Pointer to HBA context object.
880  * @pring: Pointer to driver SLI ring object.
881  * @piocb: Pointer to the driver iocb object.
882  *
883  * This function is called with hbalock held. The function adds the
884  * new iocb to txcmplq of the given ring. This function always returns
885  * 0. If this function is called for ELS ring, this function checks if
886  * there is a vport associated with the ELS command. This function also
887  * starts els_tmofunc timer if this is an ELS command.
888  **/
889 static int
890 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
891 			struct lpfc_iocbq *piocb)
892 {
893 	list_add_tail(&piocb->list, &pring->txcmplq);
894 	piocb->iocb_flag |= LPFC_IO_ON_Q;
895 	pring->txcmplq_cnt++;
896 	if (pring->txcmplq_cnt > pring->txcmplq_max)
897 		pring->txcmplq_max = pring->txcmplq_cnt;
898 
899 	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
900 	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
901 	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
902 		if (!piocb->vport)
903 			BUG();
904 		else
905 			mod_timer(&piocb->vport->els_tmofunc,
906 				  jiffies + HZ * (phba->fc_ratov << 1));
907 	}
908 
909 
910 	return 0;
911 }
912 
913 /**
914  * lpfc_sli_ringtx_get - Get first element of the txq
915  * @phba: Pointer to HBA context object.
916  * @pring: Pointer to driver SLI ring object.
917  *
918  * This function is called with hbalock held to get next
919  * iocb in txq of the given ring. If there is any iocb in
920  * the txq, the function returns first iocb in the list after
921  * removing the iocb from the list, else it returns NULL.
922  **/
923 struct lpfc_iocbq *
924 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
925 {
926 	struct lpfc_iocbq *cmd_iocb;
927 
928 	list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
929 	if (cmd_iocb != NULL)
930 		pring->txq_cnt--;
931 	return cmd_iocb;
932 }
933 
934 /**
935  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
936  * @phba: Pointer to HBA context object.
937  * @pring: Pointer to driver SLI ring object.
938  *
939  * This function is called with hbalock held and the caller must post the
940  * iocb without releasing the lock. If the caller releases the lock,
941  * iocb slot returned by the function is not guaranteed to be available.
942  * The function returns pointer to the next available iocb slot if there
943  * is available slot in the ring, else it returns NULL.
944  * If the get index of the ring is ahead of the put index, the function
945  * will post an error attention event to the worker thread to take the
946  * HBA to offline state.
947  **/
948 static IOCB_t *
949 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
950 {
951 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
952 	uint32_t  max_cmd_idx = pring->numCiocb;
953 	if ((pring->next_cmdidx == pring->cmdidx) &&
954 	   (++pring->next_cmdidx >= max_cmd_idx))
955 		pring->next_cmdidx = 0;
956 
957 	if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
958 
959 		pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
960 
961 		if (unlikely(pring->local_getidx >= max_cmd_idx)) {
962 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
963 					"0315 Ring %d issue: portCmdGet %d "
964 					"is bigger than cmd ring %d\n",
965 					pring->ringno,
966 					pring->local_getidx, max_cmd_idx);
967 
968 			phba->link_state = LPFC_HBA_ERROR;
969 			/*
970 			 * All error attention handlers are posted to
971 			 * worker thread
972 			 */
973 			phba->work_ha |= HA_ERATT;
974 			phba->work_hs = HS_FFER3;
975 
976 			lpfc_worker_wake_up(phba);
977 
978 			return NULL;
979 		}
980 
981 		if (pring->local_getidx == pring->next_cmdidx)
982 			return NULL;
983 	}
984 
985 	return lpfc_cmd_iocb(phba, pring);
986 }
987 
988 /**
989  * lpfc_sli_next_iotag - Get an iotag for the iocb
990  * @phba: Pointer to HBA context object.
991  * @iocbq: Pointer to driver iocb object.
992  *
993  * This function gets an iotag for the iocb. If there is no unused iotag and
994  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
995  * array and assigns a new iotag.
996  * The function returns the allocated iotag if successful, else returns zero.
997  * Zero is not a valid iotag.
998  * The caller is not required to hold any lock.
999  **/
1000 uint16_t
1001 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1002 {
1003 	struct lpfc_iocbq **new_arr;
1004 	struct lpfc_iocbq **old_arr;
1005 	size_t new_len;
1006 	struct lpfc_sli *psli = &phba->sli;
1007 	uint16_t iotag;
1008 
1009 	spin_lock_irq(&phba->hbalock);
1010 	iotag = psli->last_iotag;
1011 	if(++iotag < psli->iocbq_lookup_len) {
1012 		psli->last_iotag = iotag;
1013 		psli->iocbq_lookup[iotag] = iocbq;
1014 		spin_unlock_irq(&phba->hbalock);
1015 		iocbq->iotag = iotag;
1016 		return iotag;
1017 	} else if (psli->iocbq_lookup_len < (0xffff
1018 					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1019 		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1020 		spin_unlock_irq(&phba->hbalock);
1021 		new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1022 				  GFP_KERNEL);
1023 		if (new_arr) {
1024 			spin_lock_irq(&phba->hbalock);
1025 			old_arr = psli->iocbq_lookup;
1026 			if (new_len <= psli->iocbq_lookup_len) {
1027 				/* highly unprobable case */
1028 				kfree(new_arr);
1029 				iotag = psli->last_iotag;
1030 				if(++iotag < psli->iocbq_lookup_len) {
1031 					psli->last_iotag = iotag;
1032 					psli->iocbq_lookup[iotag] = iocbq;
1033 					spin_unlock_irq(&phba->hbalock);
1034 					iocbq->iotag = iotag;
1035 					return iotag;
1036 				}
1037 				spin_unlock_irq(&phba->hbalock);
1038 				return 0;
1039 			}
1040 			if (psli->iocbq_lookup)
1041 				memcpy(new_arr, old_arr,
1042 				       ((psli->last_iotag  + 1) *
1043 					sizeof (struct lpfc_iocbq *)));
1044 			psli->iocbq_lookup = new_arr;
1045 			psli->iocbq_lookup_len = new_len;
1046 			psli->last_iotag = iotag;
1047 			psli->iocbq_lookup[iotag] = iocbq;
1048 			spin_unlock_irq(&phba->hbalock);
1049 			iocbq->iotag = iotag;
1050 			kfree(old_arr);
1051 			return iotag;
1052 		}
1053 	} else
1054 		spin_unlock_irq(&phba->hbalock);
1055 
1056 	lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1057 			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1058 			psli->last_iotag);
1059 
1060 	return 0;
1061 }
1062 
1063 /**
1064  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1065  * @phba: Pointer to HBA context object.
1066  * @pring: Pointer to driver SLI ring object.
1067  * @iocb: Pointer to iocb slot in the ring.
1068  * @nextiocb: Pointer to driver iocb object which need to be
1069  *            posted to firmware.
1070  *
1071  * This function is called with hbalock held to post a new iocb to
1072  * the firmware. This function copies the new iocb to ring iocb slot and
1073  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1074  * a completion call back for this iocb else the function will free the
1075  * iocb object.
1076  **/
1077 static void
1078 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1079 		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1080 {
1081 	/*
1082 	 * Set up an iotag
1083 	 */
1084 	nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1085 
1086 
1087 	if (pring->ringno == LPFC_ELS_RING) {
1088 		lpfc_debugfs_slow_ring_trc(phba,
1089 			"IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1090 			*(((uint32_t *) &nextiocb->iocb) + 4),
1091 			*(((uint32_t *) &nextiocb->iocb) + 6),
1092 			*(((uint32_t *) &nextiocb->iocb) + 7));
1093 	}
1094 
1095 	/*
1096 	 * Issue iocb command to adapter
1097 	 */
1098 	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1099 	wmb();
1100 	pring->stats.iocb_cmd++;
1101 
1102 	/*
1103 	 * If there is no completion routine to call, we can release the
1104 	 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1105 	 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1106 	 */
1107 	if (nextiocb->iocb_cmpl)
1108 		lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1109 	else
1110 		__lpfc_sli_release_iocbq(phba, nextiocb);
1111 
1112 	/*
1113 	 * Let the HBA know what IOCB slot will be the next one the
1114 	 * driver will put a command into.
1115 	 */
1116 	pring->cmdidx = pring->next_cmdidx;
1117 	writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1118 }
1119 
1120 /**
1121  * lpfc_sli_update_full_ring - Update the chip attention register
1122  * @phba: Pointer to HBA context object.
1123  * @pring: Pointer to driver SLI ring object.
1124  *
1125  * The caller is not required to hold any lock for calling this function.
1126  * This function updates the chip attention bits for the ring to inform firmware
1127  * that there are pending work to be done for this ring and requests an
1128  * interrupt when there is space available in the ring. This function is
1129  * called when the driver is unable to post more iocbs to the ring due
1130  * to unavailability of space in the ring.
1131  **/
1132 static void
1133 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1134 {
1135 	int ringno = pring->ringno;
1136 
1137 	pring->flag |= LPFC_CALL_RING_AVAILABLE;
1138 
1139 	wmb();
1140 
1141 	/*
1142 	 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1143 	 * The HBA will tell us when an IOCB entry is available.
1144 	 */
1145 	writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1146 	readl(phba->CAregaddr); /* flush */
1147 
1148 	pring->stats.iocb_cmd_full++;
1149 }
1150 
1151 /**
1152  * lpfc_sli_update_ring - Update chip attention register
1153  * @phba: Pointer to HBA context object.
1154  * @pring: Pointer to driver SLI ring object.
1155  *
1156  * This function updates the chip attention register bit for the
1157  * given ring to inform HBA that there is more work to be done
1158  * in this ring. The caller is not required to hold any lock.
1159  **/
1160 static void
1161 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1162 {
1163 	int ringno = pring->ringno;
1164 
1165 	/*
1166 	 * Tell the HBA that there is work to do in this ring.
1167 	 */
1168 	if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1169 		wmb();
1170 		writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1171 		readl(phba->CAregaddr); /* flush */
1172 	}
1173 }
1174 
1175 /**
1176  * lpfc_sli_resume_iocb - Process iocbs in the txq
1177  * @phba: Pointer to HBA context object.
1178  * @pring: Pointer to driver SLI ring object.
1179  *
1180  * This function is called with hbalock held to post pending iocbs
1181  * in the txq to the firmware. This function is called when driver
1182  * detects space available in the ring.
1183  **/
1184 static void
1185 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1186 {
1187 	IOCB_t *iocb;
1188 	struct lpfc_iocbq *nextiocb;
1189 
1190 	/*
1191 	 * Check to see if:
1192 	 *  (a) there is anything on the txq to send
1193 	 *  (b) link is up
1194 	 *  (c) link attention events can be processed (fcp ring only)
1195 	 *  (d) IOCB processing is not blocked by the outstanding mbox command.
1196 	 */
1197 	if (pring->txq_cnt &&
1198 	    lpfc_is_link_up(phba) &&
1199 	    (pring->ringno != phba->sli.fcp_ring ||
1200 	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1201 
1202 		while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1203 		       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1204 			lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1205 
1206 		if (iocb)
1207 			lpfc_sli_update_ring(phba, pring);
1208 		else
1209 			lpfc_sli_update_full_ring(phba, pring);
1210 	}
1211 
1212 	return;
1213 }
1214 
1215 /**
1216  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1217  * @phba: Pointer to HBA context object.
1218  * @hbqno: HBQ number.
1219  *
1220  * This function is called with hbalock held to get the next
1221  * available slot for the given HBQ. If there is free slot
1222  * available for the HBQ it will return pointer to the next available
1223  * HBQ entry else it will return NULL.
1224  **/
1225 static struct lpfc_hbq_entry *
1226 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1227 {
1228 	struct hbq_s *hbqp = &phba->hbqs[hbqno];
1229 
1230 	if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1231 	    ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1232 		hbqp->next_hbqPutIdx = 0;
1233 
1234 	if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1235 		uint32_t raw_index = phba->hbq_get[hbqno];
1236 		uint32_t getidx = le32_to_cpu(raw_index);
1237 
1238 		hbqp->local_hbqGetIdx = getidx;
1239 
1240 		if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1241 			lpfc_printf_log(phba, KERN_ERR,
1242 					LOG_SLI | LOG_VPORT,
1243 					"1802 HBQ %d: local_hbqGetIdx "
1244 					"%u is > than hbqp->entry_count %u\n",
1245 					hbqno, hbqp->local_hbqGetIdx,
1246 					hbqp->entry_count);
1247 
1248 			phba->link_state = LPFC_HBA_ERROR;
1249 			return NULL;
1250 		}
1251 
1252 		if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1253 			return NULL;
1254 	}
1255 
1256 	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1257 			hbqp->hbqPutIdx;
1258 }
1259 
1260 /**
1261  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1262  * @phba: Pointer to HBA context object.
1263  *
1264  * This function is called with no lock held to free all the
1265  * hbq buffers while uninitializing the SLI interface. It also
1266  * frees the HBQ buffers returned by the firmware but not yet
1267  * processed by the upper layers.
1268  **/
1269 void
1270 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1271 {
1272 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1273 	struct hbq_dmabuf *hbq_buf;
1274 	unsigned long flags;
1275 	int i, hbq_count;
1276 	uint32_t hbqno;
1277 
1278 	hbq_count = lpfc_sli_hbq_count();
1279 	/* Return all memory used by all HBQs */
1280 	spin_lock_irqsave(&phba->hbalock, flags);
1281 	for (i = 0; i < hbq_count; ++i) {
1282 		list_for_each_entry_safe(dmabuf, next_dmabuf,
1283 				&phba->hbqs[i].hbq_buffer_list, list) {
1284 			hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1285 			list_del(&hbq_buf->dbuf.list);
1286 			(phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1287 		}
1288 		phba->hbqs[i].buffer_count = 0;
1289 	}
1290 	/* Return all HBQ buffer that are in-fly */
1291 	list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1292 				 list) {
1293 		hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1294 		list_del(&hbq_buf->dbuf.list);
1295 		if (hbq_buf->tag == -1) {
1296 			(phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1297 				(phba, hbq_buf);
1298 		} else {
1299 			hbqno = hbq_buf->tag >> 16;
1300 			if (hbqno >= LPFC_MAX_HBQS)
1301 				(phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1302 					(phba, hbq_buf);
1303 			else
1304 				(phba->hbqs[hbqno].hbq_free_buffer)(phba,
1305 					hbq_buf);
1306 		}
1307 	}
1308 
1309 	/* Mark the HBQs not in use */
1310 	phba->hbq_in_use = 0;
1311 	spin_unlock_irqrestore(&phba->hbalock, flags);
1312 }
1313 
1314 /**
1315  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1316  * @phba: Pointer to HBA context object.
1317  * @hbqno: HBQ number.
1318  * @hbq_buf: Pointer to HBQ buffer.
1319  *
1320  * This function is called with the hbalock held to post a
1321  * hbq buffer to the firmware. If the function finds an empty
1322  * slot in the HBQ, it will post the buffer. The function will return
1323  * pointer to the hbq entry if it successfully post the buffer
1324  * else it will return NULL.
1325  **/
1326 static int
1327 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1328 			 struct hbq_dmabuf *hbq_buf)
1329 {
1330 	return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1331 }
1332 
1333 /**
1334  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1335  * @phba: Pointer to HBA context object.
1336  * @hbqno: HBQ number.
1337  * @hbq_buf: Pointer to HBQ buffer.
1338  *
1339  * This function is called with the hbalock held to post a hbq buffer to the
1340  * firmware. If the function finds an empty slot in the HBQ, it will post the
1341  * buffer and place it on the hbq_buffer_list. The function will return zero if
1342  * it successfully post the buffer else it will return an error.
1343  **/
1344 static int
1345 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1346 			    struct hbq_dmabuf *hbq_buf)
1347 {
1348 	struct lpfc_hbq_entry *hbqe;
1349 	dma_addr_t physaddr = hbq_buf->dbuf.phys;
1350 
1351 	/* Get next HBQ entry slot to use */
1352 	hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1353 	if (hbqe) {
1354 		struct hbq_s *hbqp = &phba->hbqs[hbqno];
1355 
1356 		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1357 		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1358 		hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1359 		hbqe->bde.tus.f.bdeFlags = 0;
1360 		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1361 		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1362 				/* Sync SLIM */
1363 		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1364 		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1365 				/* flush */
1366 		readl(phba->hbq_put + hbqno);
1367 		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1368 		return 0;
1369 	} else
1370 		return -ENOMEM;
1371 }
1372 
1373 /**
1374  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1375  * @phba: Pointer to HBA context object.
1376  * @hbqno: HBQ number.
1377  * @hbq_buf: Pointer to HBQ buffer.
1378  *
1379  * This function is called with the hbalock held to post an RQE to the SLI4
1380  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1381  * the hbq_buffer_list and return zero, otherwise it will return an error.
1382  **/
1383 static int
1384 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1385 			    struct hbq_dmabuf *hbq_buf)
1386 {
1387 	int rc;
1388 	struct lpfc_rqe hrqe;
1389 	struct lpfc_rqe drqe;
1390 
1391 	hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1392 	hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1393 	drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1394 	drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1395 	rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1396 			      &hrqe, &drqe);
1397 	if (rc < 0)
1398 		return rc;
1399 	hbq_buf->tag = rc;
1400 	list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1401 	return 0;
1402 }
1403 
1404 /* HBQ for ELS and CT traffic. */
1405 static struct lpfc_hbq_init lpfc_els_hbq = {
1406 	.rn = 1,
1407 	.entry_count = 256,
1408 	.mask_count = 0,
1409 	.profile = 0,
1410 	.ring_mask = (1 << LPFC_ELS_RING),
1411 	.buffer_count = 0,
1412 	.init_count = 40,
1413 	.add_count = 40,
1414 };
1415 
1416 /* HBQ for the extra ring if needed */
1417 static struct lpfc_hbq_init lpfc_extra_hbq = {
1418 	.rn = 1,
1419 	.entry_count = 200,
1420 	.mask_count = 0,
1421 	.profile = 0,
1422 	.ring_mask = (1 << LPFC_EXTRA_RING),
1423 	.buffer_count = 0,
1424 	.init_count = 0,
1425 	.add_count = 5,
1426 };
1427 
1428 /* Array of HBQs */
1429 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1430 	&lpfc_els_hbq,
1431 	&lpfc_extra_hbq,
1432 };
1433 
1434 /**
1435  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1436  * @phba: Pointer to HBA context object.
1437  * @hbqno: HBQ number.
1438  * @count: Number of HBQ buffers to be posted.
1439  *
1440  * This function is called with no lock held to post more hbq buffers to the
1441  * given HBQ. The function returns the number of HBQ buffers successfully
1442  * posted.
1443  **/
1444 static int
1445 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1446 {
1447 	uint32_t i, posted = 0;
1448 	unsigned long flags;
1449 	struct hbq_dmabuf *hbq_buffer;
1450 	LIST_HEAD(hbq_buf_list);
1451 	if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1452 		return 0;
1453 
1454 	if ((phba->hbqs[hbqno].buffer_count + count) >
1455 	    lpfc_hbq_defs[hbqno]->entry_count)
1456 		count = lpfc_hbq_defs[hbqno]->entry_count -
1457 					phba->hbqs[hbqno].buffer_count;
1458 	if (!count)
1459 		return 0;
1460 	/* Allocate HBQ entries */
1461 	for (i = 0; i < count; i++) {
1462 		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1463 		if (!hbq_buffer)
1464 			break;
1465 		list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1466 	}
1467 	/* Check whether HBQ is still in use */
1468 	spin_lock_irqsave(&phba->hbalock, flags);
1469 	if (!phba->hbq_in_use)
1470 		goto err;
1471 	while (!list_empty(&hbq_buf_list)) {
1472 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1473 				 dbuf.list);
1474 		hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1475 				      (hbqno << 16));
1476 		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1477 			phba->hbqs[hbqno].buffer_count++;
1478 			posted++;
1479 		} else
1480 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1481 	}
1482 	spin_unlock_irqrestore(&phba->hbalock, flags);
1483 	return posted;
1484 err:
1485 	spin_unlock_irqrestore(&phba->hbalock, flags);
1486 	while (!list_empty(&hbq_buf_list)) {
1487 		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1488 				 dbuf.list);
1489 		(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1490 	}
1491 	return 0;
1492 }
1493 
1494 /**
1495  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1496  * @phba: Pointer to HBA context object.
1497  * @qno: HBQ number.
1498  *
1499  * This function posts more buffers to the HBQ. This function
1500  * is called with no lock held. The function returns the number of HBQ entries
1501  * successfully allocated.
1502  **/
1503 int
1504 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1505 {
1506 	if (phba->sli_rev == LPFC_SLI_REV4)
1507 		return 0;
1508 	else
1509 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1510 					 lpfc_hbq_defs[qno]->add_count);
1511 }
1512 
1513 /**
1514  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1515  * @phba: Pointer to HBA context object.
1516  * @qno:  HBQ queue number.
1517  *
1518  * This function is called from SLI initialization code path with
1519  * no lock held to post initial HBQ buffers to firmware. The
1520  * function returns the number of HBQ entries successfully allocated.
1521  **/
1522 static int
1523 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1524 {
1525 	if (phba->sli_rev == LPFC_SLI_REV4)
1526 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1527 					 lpfc_hbq_defs[qno]->entry_count);
1528 	else
1529 		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1530 					 lpfc_hbq_defs[qno]->init_count);
1531 }
1532 
1533 /**
1534  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1535  * @phba: Pointer to HBA context object.
1536  * @hbqno: HBQ number.
1537  *
1538  * This function removes the first hbq buffer on an hbq list and returns a
1539  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1540  **/
1541 static struct hbq_dmabuf *
1542 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1543 {
1544 	struct lpfc_dmabuf *d_buf;
1545 
1546 	list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1547 	if (!d_buf)
1548 		return NULL;
1549 	return container_of(d_buf, struct hbq_dmabuf, dbuf);
1550 }
1551 
1552 /**
1553  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1554  * @phba: Pointer to HBA context object.
1555  * @tag: Tag of the hbq buffer.
1556  *
1557  * This function is called with hbalock held. This function searches
1558  * for the hbq buffer associated with the given tag in the hbq buffer
1559  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1560  * it returns NULL.
1561  **/
1562 static struct hbq_dmabuf *
1563 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1564 {
1565 	struct lpfc_dmabuf *d_buf;
1566 	struct hbq_dmabuf *hbq_buf;
1567 	uint32_t hbqno;
1568 
1569 	hbqno = tag >> 16;
1570 	if (hbqno >= LPFC_MAX_HBQS)
1571 		return NULL;
1572 
1573 	spin_lock_irq(&phba->hbalock);
1574 	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1575 		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1576 		if (hbq_buf->tag == tag) {
1577 			spin_unlock_irq(&phba->hbalock);
1578 			return hbq_buf;
1579 		}
1580 	}
1581 	spin_unlock_irq(&phba->hbalock);
1582 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1583 			"1803 Bad hbq tag. Data: x%x x%x\n",
1584 			tag, phba->hbqs[tag >> 16].buffer_count);
1585 	return NULL;
1586 }
1587 
1588 /**
1589  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1590  * @phba: Pointer to HBA context object.
1591  * @hbq_buffer: Pointer to HBQ buffer.
1592  *
1593  * This function is called with hbalock. This function gives back
1594  * the hbq buffer to firmware. If the HBQ does not have space to
1595  * post the buffer, it will free the buffer.
1596  **/
1597 void
1598 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1599 {
1600 	uint32_t hbqno;
1601 
1602 	if (hbq_buffer) {
1603 		hbqno = hbq_buffer->tag >> 16;
1604 		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1605 			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1606 	}
1607 }
1608 
1609 /**
1610  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1611  * @mbxCommand: mailbox command code.
1612  *
1613  * This function is called by the mailbox event handler function to verify
1614  * that the completed mailbox command is a legitimate mailbox command. If the
1615  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1616  * and the mailbox event handler will take the HBA offline.
1617  **/
1618 static int
1619 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1620 {
1621 	uint8_t ret;
1622 
1623 	switch (mbxCommand) {
1624 	case MBX_LOAD_SM:
1625 	case MBX_READ_NV:
1626 	case MBX_WRITE_NV:
1627 	case MBX_WRITE_VPARMS:
1628 	case MBX_RUN_BIU_DIAG:
1629 	case MBX_INIT_LINK:
1630 	case MBX_DOWN_LINK:
1631 	case MBX_CONFIG_LINK:
1632 	case MBX_CONFIG_RING:
1633 	case MBX_RESET_RING:
1634 	case MBX_READ_CONFIG:
1635 	case MBX_READ_RCONFIG:
1636 	case MBX_READ_SPARM:
1637 	case MBX_READ_STATUS:
1638 	case MBX_READ_RPI:
1639 	case MBX_READ_XRI:
1640 	case MBX_READ_REV:
1641 	case MBX_READ_LNK_STAT:
1642 	case MBX_REG_LOGIN:
1643 	case MBX_UNREG_LOGIN:
1644 	case MBX_READ_LA:
1645 	case MBX_CLEAR_LA:
1646 	case MBX_DUMP_MEMORY:
1647 	case MBX_DUMP_CONTEXT:
1648 	case MBX_RUN_DIAGS:
1649 	case MBX_RESTART:
1650 	case MBX_UPDATE_CFG:
1651 	case MBX_DOWN_LOAD:
1652 	case MBX_DEL_LD_ENTRY:
1653 	case MBX_RUN_PROGRAM:
1654 	case MBX_SET_MASK:
1655 	case MBX_SET_VARIABLE:
1656 	case MBX_UNREG_D_ID:
1657 	case MBX_KILL_BOARD:
1658 	case MBX_CONFIG_FARP:
1659 	case MBX_BEACON:
1660 	case MBX_LOAD_AREA:
1661 	case MBX_RUN_BIU_DIAG64:
1662 	case MBX_CONFIG_PORT:
1663 	case MBX_READ_SPARM64:
1664 	case MBX_READ_RPI64:
1665 	case MBX_REG_LOGIN64:
1666 	case MBX_READ_LA64:
1667 	case MBX_WRITE_WWN:
1668 	case MBX_SET_DEBUG:
1669 	case MBX_LOAD_EXP_ROM:
1670 	case MBX_ASYNCEVT_ENABLE:
1671 	case MBX_REG_VPI:
1672 	case MBX_UNREG_VPI:
1673 	case MBX_HEARTBEAT:
1674 	case MBX_PORT_CAPABILITIES:
1675 	case MBX_PORT_IOV_CONTROL:
1676 	case MBX_SLI4_CONFIG:
1677 	case MBX_SLI4_REQ_FTRS:
1678 	case MBX_REG_FCFI:
1679 	case MBX_UNREG_FCFI:
1680 	case MBX_REG_VFI:
1681 	case MBX_UNREG_VFI:
1682 	case MBX_INIT_VPI:
1683 	case MBX_INIT_VFI:
1684 	case MBX_RESUME_RPI:
1685 	case MBX_READ_EVENT_LOG_STATUS:
1686 	case MBX_READ_EVENT_LOG:
1687 		ret = mbxCommand;
1688 		break;
1689 	default:
1690 		ret = MBX_SHUTDOWN;
1691 		break;
1692 	}
1693 	return ret;
1694 }
1695 
1696 /**
1697  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1698  * @phba: Pointer to HBA context object.
1699  * @pmboxq: Pointer to mailbox command.
1700  *
1701  * This is completion handler function for mailbox commands issued from
1702  * lpfc_sli_issue_mbox_wait function. This function is called by the
1703  * mailbox event handler function with no lock held. This function
1704  * will wake up thread waiting on the wait queue pointed by context1
1705  * of the mailbox.
1706  **/
1707 void
1708 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1709 {
1710 	wait_queue_head_t *pdone_q;
1711 	unsigned long drvr_flag;
1712 
1713 	/*
1714 	 * If pdone_q is empty, the driver thread gave up waiting and
1715 	 * continued running.
1716 	 */
1717 	pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1718 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
1719 	pdone_q = (wait_queue_head_t *) pmboxq->context1;
1720 	if (pdone_q)
1721 		wake_up_interruptible(pdone_q);
1722 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1723 	return;
1724 }
1725 
1726 
1727 /**
1728  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1729  * @phba: Pointer to HBA context object.
1730  * @pmb: Pointer to mailbox object.
1731  *
1732  * This function is the default mailbox completion handler. It
1733  * frees the memory resources associated with the completed mailbox
1734  * command. If the completed command is a REG_LOGIN mailbox command,
1735  * this function will issue a UREG_LOGIN to re-claim the RPI.
1736  **/
1737 void
1738 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1739 {
1740 	struct lpfc_dmabuf *mp;
1741 	uint16_t rpi, vpi;
1742 	int rc;
1743 	struct lpfc_vport  *vport = pmb->vport;
1744 
1745 	mp = (struct lpfc_dmabuf *) (pmb->context1);
1746 
1747 	if (mp) {
1748 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
1749 		kfree(mp);
1750 	}
1751 
1752 	if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1753 	    (phba->sli_rev == LPFC_SLI_REV4))
1754 		lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1755 
1756 	/*
1757 	 * If a REG_LOGIN succeeded  after node is destroyed or node
1758 	 * is in re-discovery driver need to cleanup the RPI.
1759 	 */
1760 	if (!(phba->pport->load_flag & FC_UNLOADING) &&
1761 	    pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1762 	    !pmb->u.mb.mbxStatus) {
1763 		rpi = pmb->u.mb.un.varWords[0];
1764 		vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1765 		lpfc_unreg_login(phba, vpi, rpi, pmb);
1766 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1767 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1768 		if (rc != MBX_NOT_FINISHED)
1769 			return;
1770 	}
1771 
1772 	/* Unreg VPI, if the REG_VPI succeed after VLink failure */
1773 	if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1774 		!(phba->pport->load_flag & FC_UNLOADING) &&
1775 		!pmb->u.mb.mbxStatus) {
1776 		lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1777 		pmb->vport = vport;
1778 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1779 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1780 		if (rc != MBX_NOT_FINISHED)
1781 			return;
1782 	}
1783 
1784 	if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1785 		lpfc_sli4_mbox_cmd_free(phba, pmb);
1786 	else
1787 		mempool_free(pmb, phba->mbox_mem_pool);
1788 }
1789 
1790 /**
1791  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1792  * @phba: Pointer to HBA context object.
1793  *
1794  * This function is called with no lock held. This function processes all
1795  * the completed mailbox commands and gives it to upper layers. The interrupt
1796  * service routine processes mailbox completion interrupt and adds completed
1797  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1798  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1799  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1800  * function returns the mailbox commands to the upper layer by calling the
1801  * completion handler function of each mailbox.
1802  **/
1803 int
1804 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1805 {
1806 	MAILBOX_t *pmbox;
1807 	LPFC_MBOXQ_t *pmb;
1808 	int rc;
1809 	LIST_HEAD(cmplq);
1810 
1811 	phba->sli.slistat.mbox_event++;
1812 
1813 	/* Get all completed mailboxe buffers into the cmplq */
1814 	spin_lock_irq(&phba->hbalock);
1815 	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1816 	spin_unlock_irq(&phba->hbalock);
1817 
1818 	/* Get a Mailbox buffer to setup mailbox commands for callback */
1819 	do {
1820 		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1821 		if (pmb == NULL)
1822 			break;
1823 
1824 		pmbox = &pmb->u.mb;
1825 
1826 		if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1827 			if (pmb->vport) {
1828 				lpfc_debugfs_disc_trc(pmb->vport,
1829 					LPFC_DISC_TRC_MBOX_VPORT,
1830 					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1831 					(uint32_t)pmbox->mbxCommand,
1832 					pmbox->un.varWords[0],
1833 					pmbox->un.varWords[1]);
1834 			}
1835 			else {
1836 				lpfc_debugfs_disc_trc(phba->pport,
1837 					LPFC_DISC_TRC_MBOX,
1838 					"MBOX cmpl:       cmd:x%x mb:x%x x%x",
1839 					(uint32_t)pmbox->mbxCommand,
1840 					pmbox->un.varWords[0],
1841 					pmbox->un.varWords[1]);
1842 			}
1843 		}
1844 
1845 		/*
1846 		 * It is a fatal error if unknown mbox command completion.
1847 		 */
1848 		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1849 		    MBX_SHUTDOWN) {
1850 			/* Unknown mailbox command compl */
1851 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1852 					"(%d):0323 Unknown Mailbox command "
1853 					"x%x (x%x) Cmpl\n",
1854 					pmb->vport ? pmb->vport->vpi : 0,
1855 					pmbox->mbxCommand,
1856 					lpfc_sli4_mbox_opcode_get(phba, pmb));
1857 			phba->link_state = LPFC_HBA_ERROR;
1858 			phba->work_hs = HS_FFER3;
1859 			lpfc_handle_eratt(phba);
1860 			continue;
1861 		}
1862 
1863 		if (pmbox->mbxStatus) {
1864 			phba->sli.slistat.mbox_stat_err++;
1865 			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1866 				/* Mbox cmd cmpl error - RETRYing */
1867 				lpfc_printf_log(phba, KERN_INFO,
1868 						LOG_MBOX | LOG_SLI,
1869 						"(%d):0305 Mbox cmd cmpl "
1870 						"error - RETRYing Data: x%x "
1871 						"(x%x) x%x x%x x%x\n",
1872 						pmb->vport ? pmb->vport->vpi :0,
1873 						pmbox->mbxCommand,
1874 						lpfc_sli4_mbox_opcode_get(phba,
1875 									  pmb),
1876 						pmbox->mbxStatus,
1877 						pmbox->un.varWords[0],
1878 						pmb->vport->port_state);
1879 				pmbox->mbxStatus = 0;
1880 				pmbox->mbxOwner = OWN_HOST;
1881 				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1882 				if (rc != MBX_NOT_FINISHED)
1883 					continue;
1884 			}
1885 		}
1886 
1887 		/* Mailbox cmd <cmd> Cmpl <cmpl> */
1888 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1889 				"(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1890 				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1891 				pmb->vport ? pmb->vport->vpi : 0,
1892 				pmbox->mbxCommand,
1893 				lpfc_sli4_mbox_opcode_get(phba, pmb),
1894 				pmb->mbox_cmpl,
1895 				*((uint32_t *) pmbox),
1896 				pmbox->un.varWords[0],
1897 				pmbox->un.varWords[1],
1898 				pmbox->un.varWords[2],
1899 				pmbox->un.varWords[3],
1900 				pmbox->un.varWords[4],
1901 				pmbox->un.varWords[5],
1902 				pmbox->un.varWords[6],
1903 				pmbox->un.varWords[7]);
1904 
1905 		if (pmb->mbox_cmpl)
1906 			pmb->mbox_cmpl(phba,pmb);
1907 	} while (1);
1908 	return 0;
1909 }
1910 
1911 /**
1912  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1913  * @phba: Pointer to HBA context object.
1914  * @pring: Pointer to driver SLI ring object.
1915  * @tag: buffer tag.
1916  *
1917  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1918  * is set in the tag the buffer is posted for a particular exchange,
1919  * the function will return the buffer without replacing the buffer.
1920  * If the buffer is for unsolicited ELS or CT traffic, this function
1921  * returns the buffer and also posts another buffer to the firmware.
1922  **/
1923 static struct lpfc_dmabuf *
1924 lpfc_sli_get_buff(struct lpfc_hba *phba,
1925 		  struct lpfc_sli_ring *pring,
1926 		  uint32_t tag)
1927 {
1928 	struct hbq_dmabuf *hbq_entry;
1929 
1930 	if (tag & QUE_BUFTAG_BIT)
1931 		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1932 	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1933 	if (!hbq_entry)
1934 		return NULL;
1935 	return &hbq_entry->dbuf;
1936 }
1937 
1938 /**
1939  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1940  * @phba: Pointer to HBA context object.
1941  * @pring: Pointer to driver SLI ring object.
1942  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1943  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1944  * @fch_type: the type for the first frame of the sequence.
1945  *
1946  * This function is called with no lock held. This function uses the r_ctl and
1947  * type of the received sequence to find the correct callback function to call
1948  * to process the sequence.
1949  **/
1950 static int
1951 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1952 			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1953 			 uint32_t fch_type)
1954 {
1955 	int i;
1956 
1957 	/* unSolicited Responses */
1958 	if (pring->prt[0].profile) {
1959 		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1960 			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1961 									saveq);
1962 		return 1;
1963 	}
1964 	/* We must search, based on rctl / type
1965 	   for the right routine */
1966 	for (i = 0; i < pring->num_mask; i++) {
1967 		if ((pring->prt[i].rctl == fch_r_ctl) &&
1968 		    (pring->prt[i].type == fch_type)) {
1969 			if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1970 				(pring->prt[i].lpfc_sli_rcv_unsol_event)
1971 						(phba, pring, saveq);
1972 			return 1;
1973 		}
1974 	}
1975 	return 0;
1976 }
1977 
1978 /**
1979  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1980  * @phba: Pointer to HBA context object.
1981  * @pring: Pointer to driver SLI ring object.
1982  * @saveq: Pointer to the unsolicited iocb.
1983  *
1984  * This function is called with no lock held by the ring event handler
1985  * when there is an unsolicited iocb posted to the response ring by the
1986  * firmware. This function gets the buffer associated with the iocbs
1987  * and calls the event handler for the ring. This function handles both
1988  * qring buffers and hbq buffers.
1989  * When the function returns 1 the caller can free the iocb object otherwise
1990  * upper layer functions will free the iocb objects.
1991  **/
1992 static int
1993 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1994 			    struct lpfc_iocbq *saveq)
1995 {
1996 	IOCB_t           * irsp;
1997 	WORD5            * w5p;
1998 	uint32_t           Rctl, Type;
1999 	uint32_t           match;
2000 	struct lpfc_iocbq *iocbq;
2001 	struct lpfc_dmabuf *dmzbuf;
2002 
2003 	match = 0;
2004 	irsp = &(saveq->iocb);
2005 
2006 	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2007 		if (pring->lpfc_sli_rcv_async_status)
2008 			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2009 		else
2010 			lpfc_printf_log(phba,
2011 					KERN_WARNING,
2012 					LOG_SLI,
2013 					"0316 Ring %d handler: unexpected "
2014 					"ASYNC_STATUS iocb received evt_code "
2015 					"0x%x\n",
2016 					pring->ringno,
2017 					irsp->un.asyncstat.evt_code);
2018 		return 1;
2019 	}
2020 
2021 	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2022 		(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2023 		if (irsp->ulpBdeCount > 0) {
2024 			dmzbuf = lpfc_sli_get_buff(phba, pring,
2025 					irsp->un.ulpWord[3]);
2026 			lpfc_in_buf_free(phba, dmzbuf);
2027 		}
2028 
2029 		if (irsp->ulpBdeCount > 1) {
2030 			dmzbuf = lpfc_sli_get_buff(phba, pring,
2031 					irsp->unsli3.sli3Words[3]);
2032 			lpfc_in_buf_free(phba, dmzbuf);
2033 		}
2034 
2035 		if (irsp->ulpBdeCount > 2) {
2036 			dmzbuf = lpfc_sli_get_buff(phba, pring,
2037 				irsp->unsli3.sli3Words[7]);
2038 			lpfc_in_buf_free(phba, dmzbuf);
2039 		}
2040 
2041 		return 1;
2042 	}
2043 
2044 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2045 		if (irsp->ulpBdeCount != 0) {
2046 			saveq->context2 = lpfc_sli_get_buff(phba, pring,
2047 						irsp->un.ulpWord[3]);
2048 			if (!saveq->context2)
2049 				lpfc_printf_log(phba,
2050 					KERN_ERR,
2051 					LOG_SLI,
2052 					"0341 Ring %d Cannot find buffer for "
2053 					"an unsolicited iocb. tag 0x%x\n",
2054 					pring->ringno,
2055 					irsp->un.ulpWord[3]);
2056 		}
2057 		if (irsp->ulpBdeCount == 2) {
2058 			saveq->context3 = lpfc_sli_get_buff(phba, pring,
2059 						irsp->unsli3.sli3Words[7]);
2060 			if (!saveq->context3)
2061 				lpfc_printf_log(phba,
2062 					KERN_ERR,
2063 					LOG_SLI,
2064 					"0342 Ring %d Cannot find buffer for an"
2065 					" unsolicited iocb. tag 0x%x\n",
2066 					pring->ringno,
2067 					irsp->unsli3.sli3Words[7]);
2068 		}
2069 		list_for_each_entry(iocbq, &saveq->list, list) {
2070 			irsp = &(iocbq->iocb);
2071 			if (irsp->ulpBdeCount != 0) {
2072 				iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2073 							irsp->un.ulpWord[3]);
2074 				if (!iocbq->context2)
2075 					lpfc_printf_log(phba,
2076 						KERN_ERR,
2077 						LOG_SLI,
2078 						"0343 Ring %d Cannot find "
2079 						"buffer for an unsolicited iocb"
2080 						". tag 0x%x\n", pring->ringno,
2081 						irsp->un.ulpWord[3]);
2082 			}
2083 			if (irsp->ulpBdeCount == 2) {
2084 				iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2085 						irsp->unsli3.sli3Words[7]);
2086 				if (!iocbq->context3)
2087 					lpfc_printf_log(phba,
2088 						KERN_ERR,
2089 						LOG_SLI,
2090 						"0344 Ring %d Cannot find "
2091 						"buffer for an unsolicited "
2092 						"iocb. tag 0x%x\n",
2093 						pring->ringno,
2094 						irsp->unsli3.sli3Words[7]);
2095 			}
2096 		}
2097 	}
2098 	if (irsp->ulpBdeCount != 0 &&
2099 	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2100 	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2101 		int found = 0;
2102 
2103 		/* search continue save q for same XRI */
2104 		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2105 			if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2106 				list_add_tail(&saveq->list, &iocbq->list);
2107 				found = 1;
2108 				break;
2109 			}
2110 		}
2111 		if (!found)
2112 			list_add_tail(&saveq->clist,
2113 				      &pring->iocb_continue_saveq);
2114 		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2115 			list_del_init(&iocbq->clist);
2116 			saveq = iocbq;
2117 			irsp = &(saveq->iocb);
2118 		} else
2119 			return 0;
2120 	}
2121 	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2122 	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2123 	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2124 		Rctl = FC_RCTL_ELS_REQ;
2125 		Type = FC_TYPE_ELS;
2126 	} else {
2127 		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2128 		Rctl = w5p->hcsw.Rctl;
2129 		Type = w5p->hcsw.Type;
2130 
2131 		/* Firmware Workaround */
2132 		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2133 			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2134 			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2135 			Rctl = FC_RCTL_ELS_REQ;
2136 			Type = FC_TYPE_ELS;
2137 			w5p->hcsw.Rctl = Rctl;
2138 			w5p->hcsw.Type = Type;
2139 		}
2140 	}
2141 
2142 	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2143 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2144 				"0313 Ring %d handler: unexpected Rctl x%x "
2145 				"Type x%x received\n",
2146 				pring->ringno, Rctl, Type);
2147 
2148 	return 1;
2149 }
2150 
2151 /**
2152  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2153  * @phba: Pointer to HBA context object.
2154  * @pring: Pointer to driver SLI ring object.
2155  * @prspiocb: Pointer to response iocb object.
2156  *
2157  * This function looks up the iocb_lookup table to get the command iocb
2158  * corresponding to the given response iocb using the iotag of the
2159  * response iocb. This function is called with the hbalock held.
2160  * This function returns the command iocb object if it finds the command
2161  * iocb else returns NULL.
2162  **/
2163 static struct lpfc_iocbq *
2164 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2165 		      struct lpfc_sli_ring *pring,
2166 		      struct lpfc_iocbq *prspiocb)
2167 {
2168 	struct lpfc_iocbq *cmd_iocb = NULL;
2169 	uint16_t iotag;
2170 
2171 	iotag = prspiocb->iocb.ulpIoTag;
2172 
2173 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2174 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
2175 		list_del_init(&cmd_iocb->list);
2176 		if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2177 			pring->txcmplq_cnt--;
2178 			cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2179 		}
2180 		return cmd_iocb;
2181 	}
2182 
2183 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2184 			"0317 iotag x%x is out off "
2185 			"range: max iotag x%x wd0 x%x\n",
2186 			iotag, phba->sli.last_iotag,
2187 			*(((uint32_t *) &prspiocb->iocb) + 7));
2188 	return NULL;
2189 }
2190 
2191 /**
2192  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2193  * @phba: Pointer to HBA context object.
2194  * @pring: Pointer to driver SLI ring object.
2195  * @iotag: IOCB tag.
2196  *
2197  * This function looks up the iocb_lookup table to get the command iocb
2198  * corresponding to the given iotag. This function is called with the
2199  * hbalock held.
2200  * This function returns the command iocb object if it finds the command
2201  * iocb else returns NULL.
2202  **/
2203 static struct lpfc_iocbq *
2204 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2205 			     struct lpfc_sli_ring *pring, uint16_t iotag)
2206 {
2207 	struct lpfc_iocbq *cmd_iocb;
2208 
2209 	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2210 		cmd_iocb = phba->sli.iocbq_lookup[iotag];
2211 		list_del_init(&cmd_iocb->list);
2212 		if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2213 			cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2214 			pring->txcmplq_cnt--;
2215 		}
2216 		return cmd_iocb;
2217 	}
2218 
2219 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2220 			"0372 iotag x%x is out off range: max iotag (x%x)\n",
2221 			iotag, phba->sli.last_iotag);
2222 	return NULL;
2223 }
2224 
2225 /**
2226  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2227  * @phba: Pointer to HBA context object.
2228  * @pring: Pointer to driver SLI ring object.
2229  * @saveq: Pointer to the response iocb to be processed.
2230  *
2231  * This function is called by the ring event handler for non-fcp
2232  * rings when there is a new response iocb in the response ring.
2233  * The caller is not required to hold any locks. This function
2234  * gets the command iocb associated with the response iocb and
2235  * calls the completion handler for the command iocb. If there
2236  * is no completion handler, the function will free the resources
2237  * associated with command iocb. If the response iocb is for
2238  * an already aborted command iocb, the status of the completion
2239  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2240  * This function always returns 1.
2241  **/
2242 static int
2243 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2244 			  struct lpfc_iocbq *saveq)
2245 {
2246 	struct lpfc_iocbq *cmdiocbp;
2247 	int rc = 1;
2248 	unsigned long iflag;
2249 
2250 	/* Based on the iotag field, get the cmd IOCB from the txcmplq */
2251 	spin_lock_irqsave(&phba->hbalock, iflag);
2252 	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2253 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2254 
2255 	if (cmdiocbp) {
2256 		if (cmdiocbp->iocb_cmpl) {
2257 			/*
2258 			 * If an ELS command failed send an event to mgmt
2259 			 * application.
2260 			 */
2261 			if (saveq->iocb.ulpStatus &&
2262 			     (pring->ringno == LPFC_ELS_RING) &&
2263 			     (cmdiocbp->iocb.ulpCommand ==
2264 				CMD_ELS_REQUEST64_CR))
2265 				lpfc_send_els_failure_event(phba,
2266 					cmdiocbp, saveq);
2267 
2268 			/*
2269 			 * Post all ELS completions to the worker thread.
2270 			 * All other are passed to the completion callback.
2271 			 */
2272 			if (pring->ringno == LPFC_ELS_RING) {
2273 				if ((phba->sli_rev < LPFC_SLI_REV4) &&
2274 				    (cmdiocbp->iocb_flag &
2275 							LPFC_DRIVER_ABORTED)) {
2276 					spin_lock_irqsave(&phba->hbalock,
2277 							  iflag);
2278 					cmdiocbp->iocb_flag &=
2279 						~LPFC_DRIVER_ABORTED;
2280 					spin_unlock_irqrestore(&phba->hbalock,
2281 							       iflag);
2282 					saveq->iocb.ulpStatus =
2283 						IOSTAT_LOCAL_REJECT;
2284 					saveq->iocb.un.ulpWord[4] =
2285 						IOERR_SLI_ABORTED;
2286 
2287 					/* Firmware could still be in progress
2288 					 * of DMAing payload, so don't free data
2289 					 * buffer till after a hbeat.
2290 					 */
2291 					spin_lock_irqsave(&phba->hbalock,
2292 							  iflag);
2293 					saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2294 					spin_unlock_irqrestore(&phba->hbalock,
2295 							       iflag);
2296 				}
2297 				if (phba->sli_rev == LPFC_SLI_REV4) {
2298 					if (saveq->iocb_flag &
2299 					    LPFC_EXCHANGE_BUSY) {
2300 						/* Set cmdiocb flag for the
2301 						 * exchange busy so sgl (xri)
2302 						 * will not be released until
2303 						 * the abort xri is received
2304 						 * from hba.
2305 						 */
2306 						spin_lock_irqsave(
2307 							&phba->hbalock, iflag);
2308 						cmdiocbp->iocb_flag |=
2309 							LPFC_EXCHANGE_BUSY;
2310 						spin_unlock_irqrestore(
2311 							&phba->hbalock, iflag);
2312 					}
2313 					if (cmdiocbp->iocb_flag &
2314 					    LPFC_DRIVER_ABORTED) {
2315 						/*
2316 						 * Clear LPFC_DRIVER_ABORTED
2317 						 * bit in case it was driver
2318 						 * initiated abort.
2319 						 */
2320 						spin_lock_irqsave(
2321 							&phba->hbalock, iflag);
2322 						cmdiocbp->iocb_flag &=
2323 							~LPFC_DRIVER_ABORTED;
2324 						spin_unlock_irqrestore(
2325 							&phba->hbalock, iflag);
2326 						cmdiocbp->iocb.ulpStatus =
2327 							IOSTAT_LOCAL_REJECT;
2328 						cmdiocbp->iocb.un.ulpWord[4] =
2329 							IOERR_ABORT_REQUESTED;
2330 						/*
2331 						 * For SLI4, irsiocb contains
2332 						 * NO_XRI in sli_xritag, it
2333 						 * shall not affect releasing
2334 						 * sgl (xri) process.
2335 						 */
2336 						saveq->iocb.ulpStatus =
2337 							IOSTAT_LOCAL_REJECT;
2338 						saveq->iocb.un.ulpWord[4] =
2339 							IOERR_SLI_ABORTED;
2340 						spin_lock_irqsave(
2341 							&phba->hbalock, iflag);
2342 						saveq->iocb_flag |=
2343 							LPFC_DELAY_MEM_FREE;
2344 						spin_unlock_irqrestore(
2345 							&phba->hbalock, iflag);
2346 					}
2347 				}
2348 			}
2349 			(cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2350 		} else
2351 			lpfc_sli_release_iocbq(phba, cmdiocbp);
2352 	} else {
2353 		/*
2354 		 * Unknown initiating command based on the response iotag.
2355 		 * This could be the case on the ELS ring because of
2356 		 * lpfc_els_abort().
2357 		 */
2358 		if (pring->ringno != LPFC_ELS_RING) {
2359 			/*
2360 			 * Ring <ringno> handler: unexpected completion IoTag
2361 			 * <IoTag>
2362 			 */
2363 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2364 					 "0322 Ring %d handler: "
2365 					 "unexpected completion IoTag x%x "
2366 					 "Data: x%x x%x x%x x%x\n",
2367 					 pring->ringno,
2368 					 saveq->iocb.ulpIoTag,
2369 					 saveq->iocb.ulpStatus,
2370 					 saveq->iocb.un.ulpWord[4],
2371 					 saveq->iocb.ulpCommand,
2372 					 saveq->iocb.ulpContext);
2373 		}
2374 	}
2375 
2376 	return rc;
2377 }
2378 
2379 /**
2380  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2381  * @phba: Pointer to HBA context object.
2382  * @pring: Pointer to driver SLI ring object.
2383  *
2384  * This function is called from the iocb ring event handlers when
2385  * put pointer is ahead of the get pointer for a ring. This function signal
2386  * an error attention condition to the worker thread and the worker
2387  * thread will transition the HBA to offline state.
2388  **/
2389 static void
2390 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2391 {
2392 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2393 	/*
2394 	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2395 	 * rsp ring <portRspMax>
2396 	 */
2397 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2398 			"0312 Ring %d handler: portRspPut %d "
2399 			"is bigger than rsp ring %d\n",
2400 			pring->ringno, le32_to_cpu(pgp->rspPutInx),
2401 			pring->numRiocb);
2402 
2403 	phba->link_state = LPFC_HBA_ERROR;
2404 
2405 	/*
2406 	 * All error attention handlers are posted to
2407 	 * worker thread
2408 	 */
2409 	phba->work_ha |= HA_ERATT;
2410 	phba->work_hs = HS_FFER3;
2411 
2412 	lpfc_worker_wake_up(phba);
2413 
2414 	return;
2415 }
2416 
2417 /**
2418  * lpfc_poll_eratt - Error attention polling timer timeout handler
2419  * @ptr: Pointer to address of HBA context object.
2420  *
2421  * This function is invoked by the Error Attention polling timer when the
2422  * timer times out. It will check the SLI Error Attention register for
2423  * possible attention events. If so, it will post an Error Attention event
2424  * and wake up worker thread to process it. Otherwise, it will set up the
2425  * Error Attention polling timer for the next poll.
2426  **/
2427 void lpfc_poll_eratt(unsigned long ptr)
2428 {
2429 	struct lpfc_hba *phba;
2430 	uint32_t eratt = 0;
2431 
2432 	phba = (struct lpfc_hba *)ptr;
2433 
2434 	/* Check chip HA register for error event */
2435 	eratt = lpfc_sli_check_eratt(phba);
2436 
2437 	if (eratt)
2438 		/* Tell the worker thread there is work to do */
2439 		lpfc_worker_wake_up(phba);
2440 	else
2441 		/* Restart the timer for next eratt poll */
2442 		mod_timer(&phba->eratt_poll, jiffies +
2443 					HZ * LPFC_ERATT_POLL_INTERVAL);
2444 	return;
2445 }
2446 
2447 
2448 /**
2449  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2450  * @phba: Pointer to HBA context object.
2451  * @pring: Pointer to driver SLI ring object.
2452  * @mask: Host attention register mask for this ring.
2453  *
2454  * This function is called from the interrupt context when there is a ring
2455  * event for the fcp ring. The caller does not hold any lock.
2456  * The function processes each response iocb in the response ring until it
2457  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2458  * LE bit set. The function will call the completion handler of the command iocb
2459  * if the response iocb indicates a completion for a command iocb or it is
2460  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2461  * function if this is an unsolicited iocb.
2462  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2463  * to check it explicitly.
2464  */
2465 int
2466 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2467 				struct lpfc_sli_ring *pring, uint32_t mask)
2468 {
2469 	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2470 	IOCB_t *irsp = NULL;
2471 	IOCB_t *entry = NULL;
2472 	struct lpfc_iocbq *cmdiocbq = NULL;
2473 	struct lpfc_iocbq rspiocbq;
2474 	uint32_t status;
2475 	uint32_t portRspPut, portRspMax;
2476 	int rc = 1;
2477 	lpfc_iocb_type type;
2478 	unsigned long iflag;
2479 	uint32_t rsp_cmpl = 0;
2480 
2481 	spin_lock_irqsave(&phba->hbalock, iflag);
2482 	pring->stats.iocb_event++;
2483 
2484 	/*
2485 	 * The next available response entry should never exceed the maximum
2486 	 * entries.  If it does, treat it as an adapter hardware error.
2487 	 */
2488 	portRspMax = pring->numRiocb;
2489 	portRspPut = le32_to_cpu(pgp->rspPutInx);
2490 	if (unlikely(portRspPut >= portRspMax)) {
2491 		lpfc_sli_rsp_pointers_error(phba, pring);
2492 		spin_unlock_irqrestore(&phba->hbalock, iflag);
2493 		return 1;
2494 	}
2495 	if (phba->fcp_ring_in_use) {
2496 		spin_unlock_irqrestore(&phba->hbalock, iflag);
2497 		return 1;
2498 	} else
2499 		phba->fcp_ring_in_use = 1;
2500 
2501 	rmb();
2502 	while (pring->rspidx != portRspPut) {
2503 		/*
2504 		 * Fetch an entry off the ring and copy it into a local data
2505 		 * structure.  The copy involves a byte-swap since the
2506 		 * network byte order and pci byte orders are different.
2507 		 */
2508 		entry = lpfc_resp_iocb(phba, pring);
2509 		phba->last_completion_time = jiffies;
2510 
2511 		if (++pring->rspidx >= portRspMax)
2512 			pring->rspidx = 0;
2513 
2514 		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2515 				      (uint32_t *) &rspiocbq.iocb,
2516 				      phba->iocb_rsp_size);
2517 		INIT_LIST_HEAD(&(rspiocbq.list));
2518 		irsp = &rspiocbq.iocb;
2519 
2520 		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2521 		pring->stats.iocb_rsp++;
2522 		rsp_cmpl++;
2523 
2524 		if (unlikely(irsp->ulpStatus)) {
2525 			/*
2526 			 * If resource errors reported from HBA, reduce
2527 			 * queuedepths of the SCSI device.
2528 			 */
2529 			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2530 				(irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2531 				spin_unlock_irqrestore(&phba->hbalock, iflag);
2532 				phba->lpfc_rampdown_queue_depth(phba);
2533 				spin_lock_irqsave(&phba->hbalock, iflag);
2534 			}
2535 
2536 			/* Rsp ring <ringno> error: IOCB */
2537 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2538 					"0336 Rsp Ring %d error: IOCB Data: "
2539 					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
2540 					pring->ringno,
2541 					irsp->un.ulpWord[0],
2542 					irsp->un.ulpWord[1],
2543 					irsp->un.ulpWord[2],
2544 					irsp->un.ulpWord[3],
2545 					irsp->un.ulpWord[4],
2546 					irsp->un.ulpWord[5],
2547 					*(uint32_t *)&irsp->un1,
2548 					*((uint32_t *)&irsp->un1 + 1));
2549 		}
2550 
2551 		switch (type) {
2552 		case LPFC_ABORT_IOCB:
2553 		case LPFC_SOL_IOCB:
2554 			/*
2555 			 * Idle exchange closed via ABTS from port.  No iocb
2556 			 * resources need to be recovered.
2557 			 */
2558 			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2559 				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2560 						"0333 IOCB cmd 0x%x"
2561 						" processed. Skipping"
2562 						" completion\n",
2563 						irsp->ulpCommand);
2564 				break;
2565 			}
2566 
2567 			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2568 							 &rspiocbq);
2569 			if (unlikely(!cmdiocbq))
2570 				break;
2571 			if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2572 				cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2573 			if (cmdiocbq->iocb_cmpl) {
2574 				spin_unlock_irqrestore(&phba->hbalock, iflag);
2575 				(cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2576 						      &rspiocbq);
2577 				spin_lock_irqsave(&phba->hbalock, iflag);
2578 			}
2579 			break;
2580 		case LPFC_UNSOL_IOCB:
2581 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2582 			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2583 			spin_lock_irqsave(&phba->hbalock, iflag);
2584 			break;
2585 		default:
2586 			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2587 				char adaptermsg[LPFC_MAX_ADPTMSG];
2588 				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2589 				memcpy(&adaptermsg[0], (uint8_t *) irsp,
2590 				       MAX_MSG_DATA);
2591 				dev_warn(&((phba->pcidev)->dev),
2592 					 "lpfc%d: %s\n",
2593 					 phba->brd_no, adaptermsg);
2594 			} else {
2595 				/* Unknown IOCB command */
2596 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2597 						"0334 Unknown IOCB command "
2598 						"Data: x%x, x%x x%x x%x x%x\n",
2599 						type, irsp->ulpCommand,
2600 						irsp->ulpStatus,
2601 						irsp->ulpIoTag,
2602 						irsp->ulpContext);
2603 			}
2604 			break;
2605 		}
2606 
2607 		/*
2608 		 * The response IOCB has been processed.  Update the ring
2609 		 * pointer in SLIM.  If the port response put pointer has not
2610 		 * been updated, sync the pgp->rspPutInx and fetch the new port
2611 		 * response put pointer.
2612 		 */
2613 		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2614 
2615 		if (pring->rspidx == portRspPut)
2616 			portRspPut = le32_to_cpu(pgp->rspPutInx);
2617 	}
2618 
2619 	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2620 		pring->stats.iocb_rsp_full++;
2621 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2622 		writel(status, phba->CAregaddr);
2623 		readl(phba->CAregaddr);
2624 	}
2625 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2626 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2627 		pring->stats.iocb_cmd_empty++;
2628 
2629 		/* Force update of the local copy of cmdGetInx */
2630 		pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2631 		lpfc_sli_resume_iocb(phba, pring);
2632 
2633 		if ((pring->lpfc_sli_cmd_available))
2634 			(pring->lpfc_sli_cmd_available) (phba, pring);
2635 
2636 	}
2637 
2638 	phba->fcp_ring_in_use = 0;
2639 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2640 	return rc;
2641 }
2642 
2643 /**
2644  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2645  * @phba: Pointer to HBA context object.
2646  * @pring: Pointer to driver SLI ring object.
2647  * @rspiocbp: Pointer to driver response IOCB object.
2648  *
2649  * This function is called from the worker thread when there is a slow-path
2650  * response IOCB to process. This function chains all the response iocbs until
2651  * seeing the iocb with the LE bit set. The function will call
2652  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2653  * completion of a command iocb. The function will call the
2654  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2655  * The function frees the resources or calls the completion handler if this
2656  * iocb is an abort completion. The function returns NULL when the response
2657  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2658  * this function shall chain the iocb on to the iocb_continueq and return the
2659  * response iocb passed in.
2660  **/
2661 static struct lpfc_iocbq *
2662 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2663 			struct lpfc_iocbq *rspiocbp)
2664 {
2665 	struct lpfc_iocbq *saveq;
2666 	struct lpfc_iocbq *cmdiocbp;
2667 	struct lpfc_iocbq *next_iocb;
2668 	IOCB_t *irsp = NULL;
2669 	uint32_t free_saveq;
2670 	uint8_t iocb_cmd_type;
2671 	lpfc_iocb_type type;
2672 	unsigned long iflag;
2673 	int rc;
2674 
2675 	spin_lock_irqsave(&phba->hbalock, iflag);
2676 	/* First add the response iocb to the countinueq list */
2677 	list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2678 	pring->iocb_continueq_cnt++;
2679 
2680 	/* Now, determine whetehr the list is completed for processing */
2681 	irsp = &rspiocbp->iocb;
2682 	if (irsp->ulpLe) {
2683 		/*
2684 		 * By default, the driver expects to free all resources
2685 		 * associated with this iocb completion.
2686 		 */
2687 		free_saveq = 1;
2688 		saveq = list_get_first(&pring->iocb_continueq,
2689 				       struct lpfc_iocbq, list);
2690 		irsp = &(saveq->iocb);
2691 		list_del_init(&pring->iocb_continueq);
2692 		pring->iocb_continueq_cnt = 0;
2693 
2694 		pring->stats.iocb_rsp++;
2695 
2696 		/*
2697 		 * If resource errors reported from HBA, reduce
2698 		 * queuedepths of the SCSI device.
2699 		 */
2700 		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2701 		    (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2702 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2703 			phba->lpfc_rampdown_queue_depth(phba);
2704 			spin_lock_irqsave(&phba->hbalock, iflag);
2705 		}
2706 
2707 		if (irsp->ulpStatus) {
2708 			/* Rsp ring <ringno> error: IOCB */
2709 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2710 					"0328 Rsp Ring %d error: "
2711 					"IOCB Data: "
2712 					"x%x x%x x%x x%x "
2713 					"x%x x%x x%x x%x "
2714 					"x%x x%x x%x x%x "
2715 					"x%x x%x x%x x%x\n",
2716 					pring->ringno,
2717 					irsp->un.ulpWord[0],
2718 					irsp->un.ulpWord[1],
2719 					irsp->un.ulpWord[2],
2720 					irsp->un.ulpWord[3],
2721 					irsp->un.ulpWord[4],
2722 					irsp->un.ulpWord[5],
2723 					*(((uint32_t *) irsp) + 6),
2724 					*(((uint32_t *) irsp) + 7),
2725 					*(((uint32_t *) irsp) + 8),
2726 					*(((uint32_t *) irsp) + 9),
2727 					*(((uint32_t *) irsp) + 10),
2728 					*(((uint32_t *) irsp) + 11),
2729 					*(((uint32_t *) irsp) + 12),
2730 					*(((uint32_t *) irsp) + 13),
2731 					*(((uint32_t *) irsp) + 14),
2732 					*(((uint32_t *) irsp) + 15));
2733 		}
2734 
2735 		/*
2736 		 * Fetch the IOCB command type and call the correct completion
2737 		 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2738 		 * get freed back to the lpfc_iocb_list by the discovery
2739 		 * kernel thread.
2740 		 */
2741 		iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2742 		type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2743 		switch (type) {
2744 		case LPFC_SOL_IOCB:
2745 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2746 			rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2747 			spin_lock_irqsave(&phba->hbalock, iflag);
2748 			break;
2749 
2750 		case LPFC_UNSOL_IOCB:
2751 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2752 			rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2753 			spin_lock_irqsave(&phba->hbalock, iflag);
2754 			if (!rc)
2755 				free_saveq = 0;
2756 			break;
2757 
2758 		case LPFC_ABORT_IOCB:
2759 			cmdiocbp = NULL;
2760 			if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2761 				cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2762 								 saveq);
2763 			if (cmdiocbp) {
2764 				/* Call the specified completion routine */
2765 				if (cmdiocbp->iocb_cmpl) {
2766 					spin_unlock_irqrestore(&phba->hbalock,
2767 							       iflag);
2768 					(cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2769 							      saveq);
2770 					spin_lock_irqsave(&phba->hbalock,
2771 							  iflag);
2772 				} else
2773 					__lpfc_sli_release_iocbq(phba,
2774 								 cmdiocbp);
2775 			}
2776 			break;
2777 
2778 		case LPFC_UNKNOWN_IOCB:
2779 			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2780 				char adaptermsg[LPFC_MAX_ADPTMSG];
2781 				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2782 				memcpy(&adaptermsg[0], (uint8_t *)irsp,
2783 				       MAX_MSG_DATA);
2784 				dev_warn(&((phba->pcidev)->dev),
2785 					 "lpfc%d: %s\n",
2786 					 phba->brd_no, adaptermsg);
2787 			} else {
2788 				/* Unknown IOCB command */
2789 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2790 						"0335 Unknown IOCB "
2791 						"command Data: x%x "
2792 						"x%x x%x x%x\n",
2793 						irsp->ulpCommand,
2794 						irsp->ulpStatus,
2795 						irsp->ulpIoTag,
2796 						irsp->ulpContext);
2797 			}
2798 			break;
2799 		}
2800 
2801 		if (free_saveq) {
2802 			list_for_each_entry_safe(rspiocbp, next_iocb,
2803 						 &saveq->list, list) {
2804 				list_del(&rspiocbp->list);
2805 				__lpfc_sli_release_iocbq(phba, rspiocbp);
2806 			}
2807 			__lpfc_sli_release_iocbq(phba, saveq);
2808 		}
2809 		rspiocbp = NULL;
2810 	}
2811 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2812 	return rspiocbp;
2813 }
2814 
2815 /**
2816  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2817  * @phba: Pointer to HBA context object.
2818  * @pring: Pointer to driver SLI ring object.
2819  * @mask: Host attention register mask for this ring.
2820  *
2821  * This routine wraps the actual slow_ring event process routine from the
2822  * API jump table function pointer from the lpfc_hba struct.
2823  **/
2824 void
2825 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2826 				struct lpfc_sli_ring *pring, uint32_t mask)
2827 {
2828 	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2829 }
2830 
2831 /**
2832  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2833  * @phba: Pointer to HBA context object.
2834  * @pring: Pointer to driver SLI ring object.
2835  * @mask: Host attention register mask for this ring.
2836  *
2837  * This function is called from the worker thread when there is a ring event
2838  * for non-fcp rings. The caller does not hold any lock. The function will
2839  * remove each response iocb in the response ring and calls the handle
2840  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2841  **/
2842 static void
2843 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2844 				   struct lpfc_sli_ring *pring, uint32_t mask)
2845 {
2846 	struct lpfc_pgp *pgp;
2847 	IOCB_t *entry;
2848 	IOCB_t *irsp = NULL;
2849 	struct lpfc_iocbq *rspiocbp = NULL;
2850 	uint32_t portRspPut, portRspMax;
2851 	unsigned long iflag;
2852 	uint32_t status;
2853 
2854 	pgp = &phba->port_gp[pring->ringno];
2855 	spin_lock_irqsave(&phba->hbalock, iflag);
2856 	pring->stats.iocb_event++;
2857 
2858 	/*
2859 	 * The next available response entry should never exceed the maximum
2860 	 * entries.  If it does, treat it as an adapter hardware error.
2861 	 */
2862 	portRspMax = pring->numRiocb;
2863 	portRspPut = le32_to_cpu(pgp->rspPutInx);
2864 	if (portRspPut >= portRspMax) {
2865 		/*
2866 		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2867 		 * rsp ring <portRspMax>
2868 		 */
2869 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2870 				"0303 Ring %d handler: portRspPut %d "
2871 				"is bigger than rsp ring %d\n",
2872 				pring->ringno, portRspPut, portRspMax);
2873 
2874 		phba->link_state = LPFC_HBA_ERROR;
2875 		spin_unlock_irqrestore(&phba->hbalock, iflag);
2876 
2877 		phba->work_hs = HS_FFER3;
2878 		lpfc_handle_eratt(phba);
2879 
2880 		return;
2881 	}
2882 
2883 	rmb();
2884 	while (pring->rspidx != portRspPut) {
2885 		/*
2886 		 * Build a completion list and call the appropriate handler.
2887 		 * The process is to get the next available response iocb, get
2888 		 * a free iocb from the list, copy the response data into the
2889 		 * free iocb, insert to the continuation list, and update the
2890 		 * next response index to slim.  This process makes response
2891 		 * iocb's in the ring available to DMA as fast as possible but
2892 		 * pays a penalty for a copy operation.  Since the iocb is
2893 		 * only 32 bytes, this penalty is considered small relative to
2894 		 * the PCI reads for register values and a slim write.  When
2895 		 * the ulpLe field is set, the entire Command has been
2896 		 * received.
2897 		 */
2898 		entry = lpfc_resp_iocb(phba, pring);
2899 
2900 		phba->last_completion_time = jiffies;
2901 		rspiocbp = __lpfc_sli_get_iocbq(phba);
2902 		if (rspiocbp == NULL) {
2903 			printk(KERN_ERR "%s: out of buffers! Failing "
2904 			       "completion.\n", __func__);
2905 			break;
2906 		}
2907 
2908 		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2909 				      phba->iocb_rsp_size);
2910 		irsp = &rspiocbp->iocb;
2911 
2912 		if (++pring->rspidx >= portRspMax)
2913 			pring->rspidx = 0;
2914 
2915 		if (pring->ringno == LPFC_ELS_RING) {
2916 			lpfc_debugfs_slow_ring_trc(phba,
2917 			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2918 				*(((uint32_t *) irsp) + 4),
2919 				*(((uint32_t *) irsp) + 6),
2920 				*(((uint32_t *) irsp) + 7));
2921 		}
2922 
2923 		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2924 
2925 		spin_unlock_irqrestore(&phba->hbalock, iflag);
2926 		/* Handle the response IOCB */
2927 		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2928 		spin_lock_irqsave(&phba->hbalock, iflag);
2929 
2930 		/*
2931 		 * If the port response put pointer has not been updated, sync
2932 		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2933 		 * response put pointer.
2934 		 */
2935 		if (pring->rspidx == portRspPut) {
2936 			portRspPut = le32_to_cpu(pgp->rspPutInx);
2937 		}
2938 	} /* while (pring->rspidx != portRspPut) */
2939 
2940 	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2941 		/* At least one response entry has been freed */
2942 		pring->stats.iocb_rsp_full++;
2943 		/* SET RxRE_RSP in Chip Att register */
2944 		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2945 		writel(status, phba->CAregaddr);
2946 		readl(phba->CAregaddr); /* flush */
2947 	}
2948 	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2949 		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2950 		pring->stats.iocb_cmd_empty++;
2951 
2952 		/* Force update of the local copy of cmdGetInx */
2953 		pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2954 		lpfc_sli_resume_iocb(phba, pring);
2955 
2956 		if ((pring->lpfc_sli_cmd_available))
2957 			(pring->lpfc_sli_cmd_available) (phba, pring);
2958 
2959 	}
2960 
2961 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2962 	return;
2963 }
2964 
2965 /**
2966  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2967  * @phba: Pointer to HBA context object.
2968  * @pring: Pointer to driver SLI ring object.
2969  * @mask: Host attention register mask for this ring.
2970  *
2971  * This function is called from the worker thread when there is a pending
2972  * ELS response iocb on the driver internal slow-path response iocb worker
2973  * queue. The caller does not hold any lock. The function will remove each
2974  * response iocb from the response worker queue and calls the handle
2975  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2976  **/
2977 static void
2978 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2979 				   struct lpfc_sli_ring *pring, uint32_t mask)
2980 {
2981 	struct lpfc_iocbq *irspiocbq;
2982 	struct hbq_dmabuf *dmabuf;
2983 	struct lpfc_cq_event *cq_event;
2984 	unsigned long iflag;
2985 
2986 	spin_lock_irqsave(&phba->hbalock, iflag);
2987 	phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2988 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2989 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2990 		/* Get the response iocb from the head of work queue */
2991 		spin_lock_irqsave(&phba->hbalock, iflag);
2992 		list_remove_head(&phba->sli4_hba.sp_queue_event,
2993 				 cq_event, struct lpfc_cq_event, list);
2994 		spin_unlock_irqrestore(&phba->hbalock, iflag);
2995 
2996 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2997 		case CQE_CODE_COMPL_WQE:
2998 			irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2999 						 cq_event);
3000 			/* Translate ELS WCQE to response IOCBQ */
3001 			irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3002 								   irspiocbq);
3003 			if (irspiocbq)
3004 				lpfc_sli_sp_handle_rspiocb(phba, pring,
3005 							   irspiocbq);
3006 			break;
3007 		case CQE_CODE_RECEIVE:
3008 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
3009 					      cq_event);
3010 			lpfc_sli4_handle_received_buffer(phba, dmabuf);
3011 			break;
3012 		default:
3013 			break;
3014 		}
3015 	}
3016 }
3017 
3018 /**
3019  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3020  * @phba: Pointer to HBA context object.
3021  * @pring: Pointer to driver SLI ring object.
3022  *
3023  * This function aborts all iocbs in the given ring and frees all the iocb
3024  * objects in txq. This function issues an abort iocb for all the iocb commands
3025  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3026  * the return of this function. The caller is not required to hold any locks.
3027  **/
3028 void
3029 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3030 {
3031 	LIST_HEAD(completions);
3032 	struct lpfc_iocbq *iocb, *next_iocb;
3033 
3034 	if (pring->ringno == LPFC_ELS_RING) {
3035 		lpfc_fabric_abort_hba(phba);
3036 	}
3037 
3038 	/* Error everything on txq and txcmplq
3039 	 * First do the txq.
3040 	 */
3041 	spin_lock_irq(&phba->hbalock);
3042 	list_splice_init(&pring->txq, &completions);
3043 	pring->txq_cnt = 0;
3044 
3045 	/* Next issue ABTS for everything on the txcmplq */
3046 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3047 		lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3048 
3049 	spin_unlock_irq(&phba->hbalock);
3050 
3051 	/* Cancel all the IOCBs from the completions list */
3052 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3053 			      IOERR_SLI_ABORTED);
3054 }
3055 
3056 /**
3057  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3058  * @phba: Pointer to HBA context object.
3059  *
3060  * This function flushes all iocbs in the fcp ring and frees all the iocb
3061  * objects in txq and txcmplq. This function will not issue abort iocbs
3062  * for all the iocb commands in txcmplq, they will just be returned with
3063  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3064  * slot has been permanently disabled.
3065  **/
3066 void
3067 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3068 {
3069 	LIST_HEAD(txq);
3070 	LIST_HEAD(txcmplq);
3071 	struct lpfc_sli *psli = &phba->sli;
3072 	struct lpfc_sli_ring  *pring;
3073 
3074 	/* Currently, only one fcp ring */
3075 	pring = &psli->ring[psli->fcp_ring];
3076 
3077 	spin_lock_irq(&phba->hbalock);
3078 	/* Retrieve everything on txq */
3079 	list_splice_init(&pring->txq, &txq);
3080 	pring->txq_cnt = 0;
3081 
3082 	/* Retrieve everything on the txcmplq */
3083 	list_splice_init(&pring->txcmplq, &txcmplq);
3084 	pring->txcmplq_cnt = 0;
3085 	spin_unlock_irq(&phba->hbalock);
3086 
3087 	/* Flush the txq */
3088 	lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3089 			      IOERR_SLI_DOWN);
3090 
3091 	/* Flush the txcmpq */
3092 	lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3093 			      IOERR_SLI_DOWN);
3094 }
3095 
3096 /**
3097  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3098  * @phba: Pointer to HBA context object.
3099  * @mask: Bit mask to be checked.
3100  *
3101  * This function reads the host status register and compares
3102  * with the provided bit mask to check if HBA completed
3103  * the restart. This function will wait in a loop for the
3104  * HBA to complete restart. If the HBA does not restart within
3105  * 15 iterations, the function will reset the HBA again. The
3106  * function returns 1 when HBA fail to restart otherwise returns
3107  * zero.
3108  **/
3109 static int
3110 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3111 {
3112 	uint32_t status;
3113 	int i = 0;
3114 	int retval = 0;
3115 
3116 	/* Read the HBA Host Status Register */
3117 	status = readl(phba->HSregaddr);
3118 
3119 	/*
3120 	 * Check status register every 100ms for 5 retries, then every
3121 	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3122 	 * every 2.5 sec for 4.
3123 	 * Break our of the loop if errors occurred during init.
3124 	 */
3125 	while (((status & mask) != mask) &&
3126 	       !(status & HS_FFERM) &&
3127 	       i++ < 20) {
3128 
3129 		if (i <= 5)
3130 			msleep(10);
3131 		else if (i <= 10)
3132 			msleep(500);
3133 		else
3134 			msleep(2500);
3135 
3136 		if (i == 15) {
3137 				/* Do post */
3138 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3139 			lpfc_sli_brdrestart(phba);
3140 		}
3141 		/* Read the HBA Host Status Register */
3142 		status = readl(phba->HSregaddr);
3143 	}
3144 
3145 	/* Check to see if any errors occurred during init */
3146 	if ((status & HS_FFERM) || (i >= 20)) {
3147 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3148 				"2751 Adapter failed to restart, "
3149 				"status reg x%x, FW Data: A8 x%x AC x%x\n",
3150 				status,
3151 				readl(phba->MBslimaddr + 0xa8),
3152 				readl(phba->MBslimaddr + 0xac));
3153 		phba->link_state = LPFC_HBA_ERROR;
3154 		retval = 1;
3155 	}
3156 
3157 	return retval;
3158 }
3159 
3160 /**
3161  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3162  * @phba: Pointer to HBA context object.
3163  * @mask: Bit mask to be checked.
3164  *
3165  * This function checks the host status register to check if HBA is
3166  * ready. This function will wait in a loop for the HBA to be ready
3167  * If the HBA is not ready , the function will will reset the HBA PCI
3168  * function again. The function returns 1 when HBA fail to be ready
3169  * otherwise returns zero.
3170  **/
3171 static int
3172 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3173 {
3174 	uint32_t status;
3175 	int retval = 0;
3176 
3177 	/* Read the HBA Host Status Register */
3178 	status = lpfc_sli4_post_status_check(phba);
3179 
3180 	if (status) {
3181 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3182 		lpfc_sli_brdrestart(phba);
3183 		status = lpfc_sli4_post_status_check(phba);
3184 	}
3185 
3186 	/* Check to see if any errors occurred during init */
3187 	if (status) {
3188 		phba->link_state = LPFC_HBA_ERROR;
3189 		retval = 1;
3190 	} else
3191 		phba->sli4_hba.intr_enable = 0;
3192 
3193 	return retval;
3194 }
3195 
3196 /**
3197  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3198  * @phba: Pointer to HBA context object.
3199  * @mask: Bit mask to be checked.
3200  *
3201  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3202  * from the API jump table function pointer from the lpfc_hba struct.
3203  **/
3204 int
3205 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3206 {
3207 	return phba->lpfc_sli_brdready(phba, mask);
3208 }
3209 
3210 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3211 
3212 /**
3213  * lpfc_reset_barrier - Make HBA ready for HBA reset
3214  * @phba: Pointer to HBA context object.
3215  *
3216  * This function is called before resetting an HBA. This
3217  * function requests HBA to quiesce DMAs before a reset.
3218  **/
3219 void lpfc_reset_barrier(struct lpfc_hba *phba)
3220 {
3221 	uint32_t __iomem *resp_buf;
3222 	uint32_t __iomem *mbox_buf;
3223 	volatile uint32_t mbox;
3224 	uint32_t hc_copy;
3225 	int  i;
3226 	uint8_t hdrtype;
3227 
3228 	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3229 	if (hdrtype != 0x80 ||
3230 	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3231 	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3232 		return;
3233 
3234 	/*
3235 	 * Tell the other part of the chip to suspend temporarily all
3236 	 * its DMA activity.
3237 	 */
3238 	resp_buf = phba->MBslimaddr;
3239 
3240 	/* Disable the error attention */
3241 	hc_copy = readl(phba->HCregaddr);
3242 	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3243 	readl(phba->HCregaddr); /* flush */
3244 	phba->link_flag |= LS_IGNORE_ERATT;
3245 
3246 	if (readl(phba->HAregaddr) & HA_ERATT) {
3247 		/* Clear Chip error bit */
3248 		writel(HA_ERATT, phba->HAregaddr);
3249 		phba->pport->stopped = 1;
3250 	}
3251 
3252 	mbox = 0;
3253 	((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3254 	((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3255 
3256 	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3257 	mbox_buf = phba->MBslimaddr;
3258 	writel(mbox, mbox_buf);
3259 
3260 	for (i = 0;
3261 	     readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3262 		mdelay(1);
3263 
3264 	if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3265 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3266 		    phba->pport->stopped)
3267 			goto restore_hc;
3268 		else
3269 			goto clear_errat;
3270 	}
3271 
3272 	((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3273 	for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3274 		mdelay(1);
3275 
3276 clear_errat:
3277 
3278 	while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3279 		mdelay(1);
3280 
3281 	if (readl(phba->HAregaddr) & HA_ERATT) {
3282 		writel(HA_ERATT, phba->HAregaddr);
3283 		phba->pport->stopped = 1;
3284 	}
3285 
3286 restore_hc:
3287 	phba->link_flag &= ~LS_IGNORE_ERATT;
3288 	writel(hc_copy, phba->HCregaddr);
3289 	readl(phba->HCregaddr); /* flush */
3290 }
3291 
3292 /**
3293  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3294  * @phba: Pointer to HBA context object.
3295  *
3296  * This function issues a kill_board mailbox command and waits for
3297  * the error attention interrupt. This function is called for stopping
3298  * the firmware processing. The caller is not required to hold any
3299  * locks. This function calls lpfc_hba_down_post function to free
3300  * any pending commands after the kill. The function will return 1 when it
3301  * fails to kill the board else will return 0.
3302  **/
3303 int
3304 lpfc_sli_brdkill(struct lpfc_hba *phba)
3305 {
3306 	struct lpfc_sli *psli;
3307 	LPFC_MBOXQ_t *pmb;
3308 	uint32_t status;
3309 	uint32_t ha_copy;
3310 	int retval;
3311 	int i = 0;
3312 
3313 	psli = &phba->sli;
3314 
3315 	/* Kill HBA */
3316 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3317 			"0329 Kill HBA Data: x%x x%x\n",
3318 			phba->pport->port_state, psli->sli_flag);
3319 
3320 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3321 	if (!pmb)
3322 		return 1;
3323 
3324 	/* Disable the error attention */
3325 	spin_lock_irq(&phba->hbalock);
3326 	status = readl(phba->HCregaddr);
3327 	status &= ~HC_ERINT_ENA;
3328 	writel(status, phba->HCregaddr);
3329 	readl(phba->HCregaddr); /* flush */
3330 	phba->link_flag |= LS_IGNORE_ERATT;
3331 	spin_unlock_irq(&phba->hbalock);
3332 
3333 	lpfc_kill_board(phba, pmb);
3334 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3335 	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3336 
3337 	if (retval != MBX_SUCCESS) {
3338 		if (retval != MBX_BUSY)
3339 			mempool_free(pmb, phba->mbox_mem_pool);
3340 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3341 				"2752 KILL_BOARD command failed retval %d\n",
3342 				retval);
3343 		spin_lock_irq(&phba->hbalock);
3344 		phba->link_flag &= ~LS_IGNORE_ERATT;
3345 		spin_unlock_irq(&phba->hbalock);
3346 		return 1;
3347 	}
3348 
3349 	spin_lock_irq(&phba->hbalock);
3350 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3351 	spin_unlock_irq(&phba->hbalock);
3352 
3353 	mempool_free(pmb, phba->mbox_mem_pool);
3354 
3355 	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3356 	 * attention every 100ms for 3 seconds. If we don't get ERATT after
3357 	 * 3 seconds we still set HBA_ERROR state because the status of the
3358 	 * board is now undefined.
3359 	 */
3360 	ha_copy = readl(phba->HAregaddr);
3361 
3362 	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3363 		mdelay(100);
3364 		ha_copy = readl(phba->HAregaddr);
3365 	}
3366 
3367 	del_timer_sync(&psli->mbox_tmo);
3368 	if (ha_copy & HA_ERATT) {
3369 		writel(HA_ERATT, phba->HAregaddr);
3370 		phba->pport->stopped = 1;
3371 	}
3372 	spin_lock_irq(&phba->hbalock);
3373 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3374 	psli->mbox_active = NULL;
3375 	phba->link_flag &= ~LS_IGNORE_ERATT;
3376 	spin_unlock_irq(&phba->hbalock);
3377 
3378 	lpfc_hba_down_post(phba);
3379 	phba->link_state = LPFC_HBA_ERROR;
3380 
3381 	return ha_copy & HA_ERATT ? 0 : 1;
3382 }
3383 
3384 /**
3385  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3386  * @phba: Pointer to HBA context object.
3387  *
3388  * This function resets the HBA by writing HC_INITFF to the control
3389  * register. After the HBA resets, this function resets all the iocb ring
3390  * indices. This function disables PCI layer parity checking during
3391  * the reset.
3392  * This function returns 0 always.
3393  * The caller is not required to hold any locks.
3394  **/
3395 int
3396 lpfc_sli_brdreset(struct lpfc_hba *phba)
3397 {
3398 	struct lpfc_sli *psli;
3399 	struct lpfc_sli_ring *pring;
3400 	uint16_t cfg_value;
3401 	int i;
3402 
3403 	psli = &phba->sli;
3404 
3405 	/* Reset HBA */
3406 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3407 			"0325 Reset HBA Data: x%x x%x\n",
3408 			phba->pport->port_state, psli->sli_flag);
3409 
3410 	/* perform board reset */
3411 	phba->fc_eventTag = 0;
3412 	phba->link_events = 0;
3413 	phba->pport->fc_myDID = 0;
3414 	phba->pport->fc_prevDID = 0;
3415 
3416 	/* Turn off parity checking and serr during the physical reset */
3417 	pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3418 	pci_write_config_word(phba->pcidev, PCI_COMMAND,
3419 			      (cfg_value &
3420 			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3421 
3422 	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3423 
3424 	/* Now toggle INITFF bit in the Host Control Register */
3425 	writel(HC_INITFF, phba->HCregaddr);
3426 	mdelay(1);
3427 	readl(phba->HCregaddr); /* flush */
3428 	writel(0, phba->HCregaddr);
3429 	readl(phba->HCregaddr); /* flush */
3430 
3431 	/* Restore PCI cmd register */
3432 	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3433 
3434 	/* Initialize relevant SLI info */
3435 	for (i = 0; i < psli->num_rings; i++) {
3436 		pring = &psli->ring[i];
3437 		pring->flag = 0;
3438 		pring->rspidx = 0;
3439 		pring->next_cmdidx  = 0;
3440 		pring->local_getidx = 0;
3441 		pring->cmdidx = 0;
3442 		pring->missbufcnt = 0;
3443 	}
3444 
3445 	phba->link_state = LPFC_WARM_START;
3446 	return 0;
3447 }
3448 
3449 /**
3450  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3451  * @phba: Pointer to HBA context object.
3452  *
3453  * This function resets a SLI4 HBA. This function disables PCI layer parity
3454  * checking during resets the device. The caller is not required to hold
3455  * any locks.
3456  *
3457  * This function returns 0 always.
3458  **/
3459 int
3460 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3461 {
3462 	struct lpfc_sli *psli = &phba->sli;
3463 	uint16_t cfg_value;
3464 	uint8_t qindx;
3465 
3466 	/* Reset HBA */
3467 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3468 			"0295 Reset HBA Data: x%x x%x\n",
3469 			phba->pport->port_state, psli->sli_flag);
3470 
3471 	/* perform board reset */
3472 	phba->fc_eventTag = 0;
3473 	phba->link_events = 0;
3474 	phba->pport->fc_myDID = 0;
3475 	phba->pport->fc_prevDID = 0;
3476 
3477 	/* Turn off parity checking and serr during the physical reset */
3478 	pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3479 	pci_write_config_word(phba->pcidev, PCI_COMMAND,
3480 			      (cfg_value &
3481 			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3482 
3483 	spin_lock_irq(&phba->hbalock);
3484 	psli->sli_flag &= ~(LPFC_PROCESS_LA);
3485 	phba->fcf.fcf_flag = 0;
3486 	/* Clean up the child queue list for the CQs */
3487 	list_del_init(&phba->sli4_hba.mbx_wq->list);
3488 	list_del_init(&phba->sli4_hba.els_wq->list);
3489 	list_del_init(&phba->sli4_hba.hdr_rq->list);
3490 	list_del_init(&phba->sli4_hba.dat_rq->list);
3491 	list_del_init(&phba->sli4_hba.mbx_cq->list);
3492 	list_del_init(&phba->sli4_hba.els_cq->list);
3493 	for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3494 		list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3495 	for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3496 		list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3497 	spin_unlock_irq(&phba->hbalock);
3498 
3499 	/* Now physically reset the device */
3500 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3501 			"0389 Performing PCI function reset!\n");
3502 	/* Perform FCoE PCI function reset */
3503 	lpfc_pci_function_reset(phba);
3504 
3505 	return 0;
3506 }
3507 
3508 /**
3509  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3510  * @phba: Pointer to HBA context object.
3511  *
3512  * This function is called in the SLI initialization code path to
3513  * restart the HBA. The caller is not required to hold any lock.
3514  * This function writes MBX_RESTART mailbox command to the SLIM and
3515  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3516  * function to free any pending commands. The function enables
3517  * POST only during the first initialization. The function returns zero.
3518  * The function does not guarantee completion of MBX_RESTART mailbox
3519  * command before the return of this function.
3520  **/
3521 static int
3522 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3523 {
3524 	MAILBOX_t *mb;
3525 	struct lpfc_sli *psli;
3526 	volatile uint32_t word0;
3527 	void __iomem *to_slim;
3528 	uint32_t hba_aer_enabled;
3529 
3530 	spin_lock_irq(&phba->hbalock);
3531 
3532 	/* Take PCIe device Advanced Error Reporting (AER) state */
3533 	hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3534 
3535 	psli = &phba->sli;
3536 
3537 	/* Restart HBA */
3538 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3539 			"0337 Restart HBA Data: x%x x%x\n",
3540 			phba->pport->port_state, psli->sli_flag);
3541 
3542 	word0 = 0;
3543 	mb = (MAILBOX_t *) &word0;
3544 	mb->mbxCommand = MBX_RESTART;
3545 	mb->mbxHc = 1;
3546 
3547 	lpfc_reset_barrier(phba);
3548 
3549 	to_slim = phba->MBslimaddr;
3550 	writel(*(uint32_t *) mb, to_slim);
3551 	readl(to_slim); /* flush */
3552 
3553 	/* Only skip post after fc_ffinit is completed */
3554 	if (phba->pport->port_state)
3555 		word0 = 1;	/* This is really setting up word1 */
3556 	else
3557 		word0 = 0;	/* This is really setting up word1 */
3558 	to_slim = phba->MBslimaddr + sizeof (uint32_t);
3559 	writel(*(uint32_t *) mb, to_slim);
3560 	readl(to_slim); /* flush */
3561 
3562 	lpfc_sli_brdreset(phba);
3563 	phba->pport->stopped = 0;
3564 	phba->link_state = LPFC_INIT_START;
3565 	phba->hba_flag = 0;
3566 	spin_unlock_irq(&phba->hbalock);
3567 
3568 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3569 	psli->stats_start = get_seconds();
3570 
3571 	/* Give the INITFF and Post time to settle. */
3572 	mdelay(100);
3573 
3574 	/* Reset HBA AER if it was enabled, note hba_flag was reset above */
3575 	if (hba_aer_enabled)
3576 		pci_disable_pcie_error_reporting(phba->pcidev);
3577 
3578 	lpfc_hba_down_post(phba);
3579 
3580 	return 0;
3581 }
3582 
3583 /**
3584  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3585  * @phba: Pointer to HBA context object.
3586  *
3587  * This function is called in the SLI initialization code path to restart
3588  * a SLI4 HBA. The caller is not required to hold any lock.
3589  * At the end of the function, it calls lpfc_hba_down_post function to
3590  * free any pending commands.
3591  **/
3592 static int
3593 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3594 {
3595 	struct lpfc_sli *psli = &phba->sli;
3596 	uint32_t hba_aer_enabled;
3597 
3598 	/* Restart HBA */
3599 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3600 			"0296 Restart HBA Data: x%x x%x\n",
3601 			phba->pport->port_state, psli->sli_flag);
3602 
3603 	/* Take PCIe device Advanced Error Reporting (AER) state */
3604 	hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3605 
3606 	lpfc_sli4_brdreset(phba);
3607 
3608 	spin_lock_irq(&phba->hbalock);
3609 	phba->pport->stopped = 0;
3610 	phba->link_state = LPFC_INIT_START;
3611 	phba->hba_flag = 0;
3612 	spin_unlock_irq(&phba->hbalock);
3613 
3614 	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3615 	psli->stats_start = get_seconds();
3616 
3617 	/* Reset HBA AER if it was enabled, note hba_flag was reset above */
3618 	if (hba_aer_enabled)
3619 		pci_disable_pcie_error_reporting(phba->pcidev);
3620 
3621 	lpfc_hba_down_post(phba);
3622 
3623 	return 0;
3624 }
3625 
3626 /**
3627  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3628  * @phba: Pointer to HBA context object.
3629  *
3630  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3631  * API jump table function pointer from the lpfc_hba struct.
3632 **/
3633 int
3634 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3635 {
3636 	return phba->lpfc_sli_brdrestart(phba);
3637 }
3638 
3639 /**
3640  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3641  * @phba: Pointer to HBA context object.
3642  *
3643  * This function is called after a HBA restart to wait for successful
3644  * restart of the HBA. Successful restart of the HBA is indicated by
3645  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3646  * iteration, the function will restart the HBA again. The function returns
3647  * zero if HBA successfully restarted else returns negative error code.
3648  **/
3649 static int
3650 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3651 {
3652 	uint32_t status, i = 0;
3653 
3654 	/* Read the HBA Host Status Register */
3655 	status = readl(phba->HSregaddr);
3656 
3657 	/* Check status register to see what current state is */
3658 	i = 0;
3659 	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3660 
3661 		/* Check every 100ms for 5 retries, then every 500ms for 5, then
3662 		 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3663 		 * 4.
3664 		 */
3665 		if (i++ >= 20) {
3666 			/* Adapter failed to init, timeout, status reg
3667 			   <status> */
3668 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3669 					"0436 Adapter failed to init, "
3670 					"timeout, status reg x%x, "
3671 					"FW Data: A8 x%x AC x%x\n", status,
3672 					readl(phba->MBslimaddr + 0xa8),
3673 					readl(phba->MBslimaddr + 0xac));
3674 			phba->link_state = LPFC_HBA_ERROR;
3675 			return -ETIMEDOUT;
3676 		}
3677 
3678 		/* Check to see if any errors occurred during init */
3679 		if (status & HS_FFERM) {
3680 			/* ERROR: During chipset initialization */
3681 			/* Adapter failed to init, chipset, status reg
3682 			   <status> */
3683 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3684 					"0437 Adapter failed to init, "
3685 					"chipset, status reg x%x, "
3686 					"FW Data: A8 x%x AC x%x\n", status,
3687 					readl(phba->MBslimaddr + 0xa8),
3688 					readl(phba->MBslimaddr + 0xac));
3689 			phba->link_state = LPFC_HBA_ERROR;
3690 			return -EIO;
3691 		}
3692 
3693 		if (i <= 5) {
3694 			msleep(10);
3695 		} else if (i <= 10) {
3696 			msleep(500);
3697 		} else {
3698 			msleep(2500);
3699 		}
3700 
3701 		if (i == 15) {
3702 				/* Do post */
3703 			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3704 			lpfc_sli_brdrestart(phba);
3705 		}
3706 		/* Read the HBA Host Status Register */
3707 		status = readl(phba->HSregaddr);
3708 	}
3709 
3710 	/* Check to see if any errors occurred during init */
3711 	if (status & HS_FFERM) {
3712 		/* ERROR: During chipset initialization */
3713 		/* Adapter failed to init, chipset, status reg <status> */
3714 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3715 				"0438 Adapter failed to init, chipset, "
3716 				"status reg x%x, "
3717 				"FW Data: A8 x%x AC x%x\n", status,
3718 				readl(phba->MBslimaddr + 0xa8),
3719 				readl(phba->MBslimaddr + 0xac));
3720 		phba->link_state = LPFC_HBA_ERROR;
3721 		return -EIO;
3722 	}
3723 
3724 	/* Clear all interrupt enable conditions */
3725 	writel(0, phba->HCregaddr);
3726 	readl(phba->HCregaddr); /* flush */
3727 
3728 	/* setup host attn register */
3729 	writel(0xffffffff, phba->HAregaddr);
3730 	readl(phba->HAregaddr); /* flush */
3731 	return 0;
3732 }
3733 
3734 /**
3735  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3736  *
3737  * This function calculates and returns the number of HBQs required to be
3738  * configured.
3739  **/
3740 int
3741 lpfc_sli_hbq_count(void)
3742 {
3743 	return ARRAY_SIZE(lpfc_hbq_defs);
3744 }
3745 
3746 /**
3747  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3748  *
3749  * This function adds the number of hbq entries in every HBQ to get
3750  * the total number of hbq entries required for the HBA and returns
3751  * the total count.
3752  **/
3753 static int
3754 lpfc_sli_hbq_entry_count(void)
3755 {
3756 	int  hbq_count = lpfc_sli_hbq_count();
3757 	int  count = 0;
3758 	int  i;
3759 
3760 	for (i = 0; i < hbq_count; ++i)
3761 		count += lpfc_hbq_defs[i]->entry_count;
3762 	return count;
3763 }
3764 
3765 /**
3766  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3767  *
3768  * This function calculates amount of memory required for all hbq entries
3769  * to be configured and returns the total memory required.
3770  **/
3771 int
3772 lpfc_sli_hbq_size(void)
3773 {
3774 	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3775 }
3776 
3777 /**
3778  * lpfc_sli_hbq_setup - configure and initialize HBQs
3779  * @phba: Pointer to HBA context object.
3780  *
3781  * This function is called during the SLI initialization to configure
3782  * all the HBQs and post buffers to the HBQ. The caller is not
3783  * required to hold any locks. This function will return zero if successful
3784  * else it will return negative error code.
3785  **/
3786 static int
3787 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3788 {
3789 	int  hbq_count = lpfc_sli_hbq_count();
3790 	LPFC_MBOXQ_t *pmb;
3791 	MAILBOX_t *pmbox;
3792 	uint32_t hbqno;
3793 	uint32_t hbq_entry_index;
3794 
3795 				/* Get a Mailbox buffer to setup mailbox
3796 				 * commands for HBA initialization
3797 				 */
3798 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3799 
3800 	if (!pmb)
3801 		return -ENOMEM;
3802 
3803 	pmbox = &pmb->u.mb;
3804 
3805 	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
3806 	phba->link_state = LPFC_INIT_MBX_CMDS;
3807 	phba->hbq_in_use = 1;
3808 
3809 	hbq_entry_index = 0;
3810 	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3811 		phba->hbqs[hbqno].next_hbqPutIdx = 0;
3812 		phba->hbqs[hbqno].hbqPutIdx      = 0;
3813 		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
3814 		phba->hbqs[hbqno].entry_count =
3815 			lpfc_hbq_defs[hbqno]->entry_count;
3816 		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3817 			hbq_entry_index, pmb);
3818 		hbq_entry_index += phba->hbqs[hbqno].entry_count;
3819 
3820 		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3821 			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3822 			   mbxStatus <status>, ring <num> */
3823 
3824 			lpfc_printf_log(phba, KERN_ERR,
3825 					LOG_SLI | LOG_VPORT,
3826 					"1805 Adapter failed to init. "
3827 					"Data: x%x x%x x%x\n",
3828 					pmbox->mbxCommand,
3829 					pmbox->mbxStatus, hbqno);
3830 
3831 			phba->link_state = LPFC_HBA_ERROR;
3832 			mempool_free(pmb, phba->mbox_mem_pool);
3833 			return -ENXIO;
3834 		}
3835 	}
3836 	phba->hbq_count = hbq_count;
3837 
3838 	mempool_free(pmb, phba->mbox_mem_pool);
3839 
3840 	/* Initially populate or replenish the HBQs */
3841 	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3842 		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3843 	return 0;
3844 }
3845 
3846 /**
3847  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3848  * @phba: Pointer to HBA context object.
3849  *
3850  * This function is called during the SLI initialization to configure
3851  * all the HBQs and post buffers to the HBQ. The caller is not
3852  * required to hold any locks. This function will return zero if successful
3853  * else it will return negative error code.
3854  **/
3855 static int
3856 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3857 {
3858 	phba->hbq_in_use = 1;
3859 	phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3860 	phba->hbq_count = 1;
3861 	/* Initially populate or replenish the HBQs */
3862 	lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3863 	return 0;
3864 }
3865 
3866 /**
3867  * lpfc_sli_config_port - Issue config port mailbox command
3868  * @phba: Pointer to HBA context object.
3869  * @sli_mode: sli mode - 2/3
3870  *
3871  * This function is called by the sli intialization code path
3872  * to issue config_port mailbox command. This function restarts the
3873  * HBA firmware and issues a config_port mailbox command to configure
3874  * the SLI interface in the sli mode specified by sli_mode
3875  * variable. The caller is not required to hold any locks.
3876  * The function returns 0 if successful, else returns negative error
3877  * code.
3878  **/
3879 int
3880 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3881 {
3882 	LPFC_MBOXQ_t *pmb;
3883 	uint32_t resetcount = 0, rc = 0, done = 0;
3884 
3885 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3886 	if (!pmb) {
3887 		phba->link_state = LPFC_HBA_ERROR;
3888 		return -ENOMEM;
3889 	}
3890 
3891 	phba->sli_rev = sli_mode;
3892 	while (resetcount < 2 && !done) {
3893 		spin_lock_irq(&phba->hbalock);
3894 		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3895 		spin_unlock_irq(&phba->hbalock);
3896 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3897 		lpfc_sli_brdrestart(phba);
3898 		rc = lpfc_sli_chipset_init(phba);
3899 		if (rc)
3900 			break;
3901 
3902 		spin_lock_irq(&phba->hbalock);
3903 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3904 		spin_unlock_irq(&phba->hbalock);
3905 		resetcount++;
3906 
3907 		/* Call pre CONFIG_PORT mailbox command initialization.  A
3908 		 * value of 0 means the call was successful.  Any other
3909 		 * nonzero value is a failure, but if ERESTART is returned,
3910 		 * the driver may reset the HBA and try again.
3911 		 */
3912 		rc = lpfc_config_port_prep(phba);
3913 		if (rc == -ERESTART) {
3914 			phba->link_state = LPFC_LINK_UNKNOWN;
3915 			continue;
3916 		} else if (rc)
3917 			break;
3918 		phba->link_state = LPFC_INIT_MBX_CMDS;
3919 		lpfc_config_port(phba, pmb);
3920 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3921 		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3922 					LPFC_SLI3_HBQ_ENABLED |
3923 					LPFC_SLI3_CRP_ENABLED |
3924 					LPFC_SLI3_BG_ENABLED);
3925 		if (rc != MBX_SUCCESS) {
3926 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3927 				"0442 Adapter failed to init, mbxCmd x%x "
3928 				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3929 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
3930 			spin_lock_irq(&phba->hbalock);
3931 			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
3932 			spin_unlock_irq(&phba->hbalock);
3933 			rc = -ENXIO;
3934 		} else {
3935 			/* Allow asynchronous mailbox command to go through */
3936 			spin_lock_irq(&phba->hbalock);
3937 			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3938 			spin_unlock_irq(&phba->hbalock);
3939 			done = 1;
3940 		}
3941 	}
3942 	if (!done) {
3943 		rc = -EINVAL;
3944 		goto do_prep_failed;
3945 	}
3946 	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3947 		if (!pmb->u.mb.un.varCfgPort.cMA) {
3948 			rc = -ENXIO;
3949 			goto do_prep_failed;
3950 		}
3951 		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
3952 			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3953 			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3954 			phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3955 				phba->max_vpi : phba->max_vports;
3956 
3957 		} else
3958 			phba->max_vpi = 0;
3959 		if (pmb->u.mb.un.varCfgPort.gdss)
3960 			phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3961 		if (pmb->u.mb.un.varCfgPort.gerbm)
3962 			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3963 		if (pmb->u.mb.un.varCfgPort.gcrp)
3964 			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3965 
3966 		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3967 		phba->port_gp = phba->mbox->us.s3_pgp.port;
3968 
3969 		if (phba->cfg_enable_bg) {
3970 			if (pmb->u.mb.un.varCfgPort.gbg)
3971 				phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3972 			else
3973 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3974 						"0443 Adapter did not grant "
3975 						"BlockGuard\n");
3976 		}
3977 	} else {
3978 		phba->hbq_get = NULL;
3979 		phba->port_gp = phba->mbox->us.s2.port;
3980 		phba->max_vpi = 0;
3981 	}
3982 do_prep_failed:
3983 	mempool_free(pmb, phba->mbox_mem_pool);
3984 	return rc;
3985 }
3986 
3987 
3988 /**
3989  * lpfc_sli_hba_setup - SLI intialization function
3990  * @phba: Pointer to HBA context object.
3991  *
3992  * This function is the main SLI intialization function. This function
3993  * is called by the HBA intialization code, HBA reset code and HBA
3994  * error attention handler code. Caller is not required to hold any
3995  * locks. This function issues config_port mailbox command to configure
3996  * the SLI, setup iocb rings and HBQ rings. In the end the function
3997  * calls the config_port_post function to issue init_link mailbox
3998  * command and to start the discovery. The function will return zero
3999  * if successful, else it will return negative error code.
4000  **/
4001 int
4002 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4003 {
4004 	uint32_t rc;
4005 	int  mode = 3;
4006 
4007 	switch (lpfc_sli_mode) {
4008 	case 2:
4009 		if (phba->cfg_enable_npiv) {
4010 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4011 				"1824 NPIV enabled: Override lpfc_sli_mode "
4012 				"parameter (%d) to auto (0).\n",
4013 				lpfc_sli_mode);
4014 			break;
4015 		}
4016 		mode = 2;
4017 		break;
4018 	case 0:
4019 	case 3:
4020 		break;
4021 	default:
4022 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4023 				"1819 Unrecognized lpfc_sli_mode "
4024 				"parameter: %d.\n", lpfc_sli_mode);
4025 
4026 		break;
4027 	}
4028 
4029 	rc = lpfc_sli_config_port(phba, mode);
4030 
4031 	if (rc && lpfc_sli_mode == 3)
4032 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4033 				"1820 Unable to select SLI-3.  "
4034 				"Not supported by adapter.\n");
4035 	if (rc && mode != 2)
4036 		rc = lpfc_sli_config_port(phba, 2);
4037 	if (rc)
4038 		goto lpfc_sli_hba_setup_error;
4039 
4040 	/* Enable PCIe device Advanced Error Reporting (AER) if configured */
4041 	if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4042 		rc = pci_enable_pcie_error_reporting(phba->pcidev);
4043 		if (!rc) {
4044 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4045 					"2709 This device supports "
4046 					"Advanced Error Reporting (AER)\n");
4047 			spin_lock_irq(&phba->hbalock);
4048 			phba->hba_flag |= HBA_AER_ENABLED;
4049 			spin_unlock_irq(&phba->hbalock);
4050 		} else {
4051 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4052 					"2708 This device does not support "
4053 					"Advanced Error Reporting (AER)\n");
4054 			phba->cfg_aer_support = 0;
4055 		}
4056 	}
4057 
4058 	if (phba->sli_rev == 3) {
4059 		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4060 		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4061 	} else {
4062 		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4063 		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4064 		phba->sli3_options = 0;
4065 	}
4066 
4067 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4068 			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
4069 			phba->sli_rev, phba->max_vpi);
4070 	rc = lpfc_sli_ring_map(phba);
4071 
4072 	if (rc)
4073 		goto lpfc_sli_hba_setup_error;
4074 
4075 	/* Init HBQs */
4076 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4077 		rc = lpfc_sli_hbq_setup(phba);
4078 		if (rc)
4079 			goto lpfc_sli_hba_setup_error;
4080 	}
4081 	spin_lock_irq(&phba->hbalock);
4082 	phba->sli.sli_flag |= LPFC_PROCESS_LA;
4083 	spin_unlock_irq(&phba->hbalock);
4084 
4085 	rc = lpfc_config_port_post(phba);
4086 	if (rc)
4087 		goto lpfc_sli_hba_setup_error;
4088 
4089 	return rc;
4090 
4091 lpfc_sli_hba_setup_error:
4092 	phba->link_state = LPFC_HBA_ERROR;
4093 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4094 			"0445 Firmware initialization failed\n");
4095 	return rc;
4096 }
4097 
4098 /**
4099  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4100  * @phba: Pointer to HBA context object.
4101  * @mboxq: mailbox pointer.
4102  * This function issue a dump mailbox command to read config region
4103  * 23 and parse the records in the region and populate driver
4104  * data structure.
4105  **/
4106 static int
4107 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4108 		LPFC_MBOXQ_t *mboxq)
4109 {
4110 	struct lpfc_dmabuf *mp;
4111 	struct lpfc_mqe *mqe;
4112 	uint32_t data_length;
4113 	int rc;
4114 
4115 	/* Program the default value of vlan_id and fc_map */
4116 	phba->valid_vlan = 0;
4117 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4118 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4119 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4120 
4121 	mqe = &mboxq->u.mqe;
4122 	if (lpfc_dump_fcoe_param(phba, mboxq))
4123 		return -ENOMEM;
4124 
4125 	mp = (struct lpfc_dmabuf *) mboxq->context1;
4126 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4127 
4128 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4129 			"(%d):2571 Mailbox cmd x%x Status x%x "
4130 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4131 			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4132 			"CQ: x%x x%x x%x x%x\n",
4133 			mboxq->vport ? mboxq->vport->vpi : 0,
4134 			bf_get(lpfc_mqe_command, mqe),
4135 			bf_get(lpfc_mqe_status, mqe),
4136 			mqe->un.mb_words[0], mqe->un.mb_words[1],
4137 			mqe->un.mb_words[2], mqe->un.mb_words[3],
4138 			mqe->un.mb_words[4], mqe->un.mb_words[5],
4139 			mqe->un.mb_words[6], mqe->un.mb_words[7],
4140 			mqe->un.mb_words[8], mqe->un.mb_words[9],
4141 			mqe->un.mb_words[10], mqe->un.mb_words[11],
4142 			mqe->un.mb_words[12], mqe->un.mb_words[13],
4143 			mqe->un.mb_words[14], mqe->un.mb_words[15],
4144 			mqe->un.mb_words[16], mqe->un.mb_words[50],
4145 			mboxq->mcqe.word0,
4146 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
4147 			mboxq->mcqe.trailer);
4148 
4149 	if (rc) {
4150 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
4151 		kfree(mp);
4152 		return -EIO;
4153 	}
4154 	data_length = mqe->un.mb_words[5];
4155 	if (data_length > DMP_RGN23_SIZE) {
4156 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
4157 		kfree(mp);
4158 		return -EIO;
4159 	}
4160 
4161 	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4162 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
4163 	kfree(mp);
4164 	return 0;
4165 }
4166 
4167 /**
4168  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4169  * @phba: pointer to lpfc hba data structure.
4170  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4171  * @vpd: pointer to the memory to hold resulting port vpd data.
4172  * @vpd_size: On input, the number of bytes allocated to @vpd.
4173  *	      On output, the number of data bytes in @vpd.
4174  *
4175  * This routine executes a READ_REV SLI4 mailbox command.  In
4176  * addition, this routine gets the port vpd data.
4177  *
4178  * Return codes
4179  * 	0 - successful
4180  * 	ENOMEM - could not allocated memory.
4181  **/
4182 static int
4183 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4184 		    uint8_t *vpd, uint32_t *vpd_size)
4185 {
4186 	int rc = 0;
4187 	uint32_t dma_size;
4188 	struct lpfc_dmabuf *dmabuf;
4189 	struct lpfc_mqe *mqe;
4190 
4191 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4192 	if (!dmabuf)
4193 		return -ENOMEM;
4194 
4195 	/*
4196 	 * Get a DMA buffer for the vpd data resulting from the READ_REV
4197 	 * mailbox command.
4198 	 */
4199 	dma_size = *vpd_size;
4200 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4201 					  dma_size,
4202 					  &dmabuf->phys,
4203 					  GFP_KERNEL);
4204 	if (!dmabuf->virt) {
4205 		kfree(dmabuf);
4206 		return -ENOMEM;
4207 	}
4208 	memset(dmabuf->virt, 0, dma_size);
4209 
4210 	/*
4211 	 * The SLI4 implementation of READ_REV conflicts at word1,
4212 	 * bits 31:16 and SLI4 adds vpd functionality not present
4213 	 * in SLI3.  This code corrects the conflicts.
4214 	 */
4215 	lpfc_read_rev(phba, mboxq);
4216 	mqe = &mboxq->u.mqe;
4217 	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4218 	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4219 	mqe->un.read_rev.word1 &= 0x0000FFFF;
4220 	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4221 	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4222 
4223 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4224 	if (rc) {
4225 		dma_free_coherent(&phba->pcidev->dev, dma_size,
4226 				  dmabuf->virt, dmabuf->phys);
4227 		kfree(dmabuf);
4228 		return -EIO;
4229 	}
4230 
4231 	/*
4232 	 * The available vpd length cannot be bigger than the
4233 	 * DMA buffer passed to the port.  Catch the less than
4234 	 * case and update the caller's size.
4235 	 */
4236 	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4237 		*vpd_size = mqe->un.read_rev.avail_vpd_len;
4238 
4239 	memcpy(vpd, dmabuf->virt, *vpd_size);
4240 
4241 	dma_free_coherent(&phba->pcidev->dev, dma_size,
4242 			  dmabuf->virt, dmabuf->phys);
4243 	kfree(dmabuf);
4244 	return 0;
4245 }
4246 
4247 /**
4248  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4249  * @phba: pointer to lpfc hba data structure.
4250  *
4251  * This routine is called to explicitly arm the SLI4 device's completion and
4252  * event queues
4253  **/
4254 static void
4255 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4256 {
4257 	uint8_t fcp_eqidx;
4258 
4259 	lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4260 	lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4261 	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4262 		lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4263 				     LPFC_QUEUE_REARM);
4264 	lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4265 	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4266 		lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4267 				     LPFC_QUEUE_REARM);
4268 }
4269 
4270 /**
4271  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4272  * @phba: Pointer to HBA context object.
4273  *
4274  * This function is the main SLI4 device intialization PCI function. This
4275  * function is called by the HBA intialization code, HBA reset code and
4276  * HBA error attention handler code. Caller is not required to hold any
4277  * locks.
4278  **/
4279 int
4280 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4281 {
4282 	int rc;
4283 	LPFC_MBOXQ_t *mboxq;
4284 	struct lpfc_mqe *mqe;
4285 	uint8_t *vpd;
4286 	uint32_t vpd_size;
4287 	uint32_t ftr_rsp = 0;
4288 	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4289 	struct lpfc_vport *vport = phba->pport;
4290 	struct lpfc_dmabuf *mp;
4291 
4292 	/* Perform a PCI function reset to start from clean */
4293 	rc = lpfc_pci_function_reset(phba);
4294 	if (unlikely(rc))
4295 		return -ENODEV;
4296 
4297 	/* Check the HBA Host Status Register for readyness */
4298 	rc = lpfc_sli4_post_status_check(phba);
4299 	if (unlikely(rc))
4300 		return -ENODEV;
4301 	else {
4302 		spin_lock_irq(&phba->hbalock);
4303 		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4304 		spin_unlock_irq(&phba->hbalock);
4305 	}
4306 
4307 	/*
4308 	 * Allocate a single mailbox container for initializing the
4309 	 * port.
4310 	 */
4311 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4312 	if (!mboxq)
4313 		return -ENOMEM;
4314 
4315 	/*
4316 	 * Continue initialization with default values even if driver failed
4317 	 * to read FCoE param config regions
4318 	 */
4319 	if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4320 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4321 			"2570 Failed to read FCoE parameters\n");
4322 
4323 	/* Issue READ_REV to collect vpd and FW information. */
4324 	vpd_size = SLI4_PAGE_SIZE;
4325 	vpd = kzalloc(vpd_size, GFP_KERNEL);
4326 	if (!vpd) {
4327 		rc = -ENOMEM;
4328 		goto out_free_mbox;
4329 	}
4330 
4331 	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4332 	if (unlikely(rc))
4333 		goto out_free_vpd;
4334 
4335 	mqe = &mboxq->u.mqe;
4336 	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4337 	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4338 		phba->hba_flag |= HBA_FCOE_SUPPORT;
4339 
4340 	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4341 		LPFC_DCBX_CEE_MODE)
4342 		phba->hba_flag |= HBA_FIP_SUPPORT;
4343 	else
4344 		phba->hba_flag &= ~HBA_FIP_SUPPORT;
4345 
4346 	if (phba->sli_rev != LPFC_SLI_REV4 ||
4347 	    !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
4348 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4349 			"0376 READ_REV Error. SLI Level %d "
4350 			"FCoE enabled %d\n",
4351 			phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
4352 		rc = -EIO;
4353 		goto out_free_vpd;
4354 	}
4355 	/*
4356 	 * Evaluate the read rev and vpd data. Populate the driver
4357 	 * state with the results. If this routine fails, the failure
4358 	 * is not fatal as the driver will use generic values.
4359 	 */
4360 	rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4361 	if (unlikely(!rc)) {
4362 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4363 				"0377 Error %d parsing vpd. "
4364 				"Using defaults.\n", rc);
4365 		rc = 0;
4366 	}
4367 
4368 	/* Save information as VPD data */
4369 	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4370 	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4371 	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4372 	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4373 					 &mqe->un.read_rev);
4374 	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4375 				       &mqe->un.read_rev);
4376 	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4377 					    &mqe->un.read_rev);
4378 	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4379 					   &mqe->un.read_rev);
4380 	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4381 	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4382 	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4383 	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4384 	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4385 	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4386 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4387 			"(%d):0380 READ_REV Status x%x "
4388 			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4389 			mboxq->vport ? mboxq->vport->vpi : 0,
4390 			bf_get(lpfc_mqe_status, mqe),
4391 			phba->vpd.rev.opFwName,
4392 			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4393 			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4394 
4395 	/*
4396 	 * Discover the port's supported feature set and match it against the
4397 	 * hosts requests.
4398 	 */
4399 	lpfc_request_features(phba, mboxq);
4400 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4401 	if (unlikely(rc)) {
4402 		rc = -EIO;
4403 		goto out_free_vpd;
4404 	}
4405 
4406 	/*
4407 	 * The port must support FCP initiator mode as this is the
4408 	 * only mode running in the host.
4409 	 */
4410 	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4411 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4412 				"0378 No support for fcpi mode.\n");
4413 		ftr_rsp++;
4414 	}
4415 
4416 	/*
4417 	 * If the port cannot support the host's requested features
4418 	 * then turn off the global config parameters to disable the
4419 	 * feature in the driver.  This is not a fatal error.
4420 	 */
4421 	if ((phba->cfg_enable_bg) &&
4422 	    !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4423 		ftr_rsp++;
4424 
4425 	if (phba->max_vpi && phba->cfg_enable_npiv &&
4426 	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4427 		ftr_rsp++;
4428 
4429 	if (ftr_rsp) {
4430 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4431 				"0379 Feature Mismatch Data: x%08x %08x "
4432 				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4433 				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4434 				phba->cfg_enable_npiv, phba->max_vpi);
4435 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4436 			phba->cfg_enable_bg = 0;
4437 		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4438 			phba->cfg_enable_npiv = 0;
4439 	}
4440 
4441 	/* These SLI3 features are assumed in SLI4 */
4442 	spin_lock_irq(&phba->hbalock);
4443 	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4444 	spin_unlock_irq(&phba->hbalock);
4445 
4446 	/* Read the port's service parameters. */
4447 	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4448 	if (rc) {
4449 		phba->link_state = LPFC_HBA_ERROR;
4450 		rc = -ENOMEM;
4451 		goto out_free_vpd;
4452 	}
4453 
4454 	mboxq->vport = vport;
4455 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4456 	mp = (struct lpfc_dmabuf *) mboxq->context1;
4457 	if (rc == MBX_SUCCESS) {
4458 		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4459 		rc = 0;
4460 	}
4461 
4462 	/*
4463 	 * This memory was allocated by the lpfc_read_sparam routine. Release
4464 	 * it to the mbuf pool.
4465 	 */
4466 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
4467 	kfree(mp);
4468 	mboxq->context1 = NULL;
4469 	if (unlikely(rc)) {
4470 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4471 				"0382 READ_SPARAM command failed "
4472 				"status %d, mbxStatus x%x\n",
4473 				rc, bf_get(lpfc_mqe_status, mqe));
4474 		phba->link_state = LPFC_HBA_ERROR;
4475 		rc = -EIO;
4476 		goto out_free_vpd;
4477 	}
4478 
4479 	if (phba->cfg_soft_wwnn)
4480 		u64_to_wwn(phba->cfg_soft_wwnn,
4481 			   vport->fc_sparam.nodeName.u.wwn);
4482 	if (phba->cfg_soft_wwpn)
4483 		u64_to_wwn(phba->cfg_soft_wwpn,
4484 			   vport->fc_sparam.portName.u.wwn);
4485 	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4486 	       sizeof(struct lpfc_name));
4487 	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4488 	       sizeof(struct lpfc_name));
4489 
4490 	/* Update the fc_host data structures with new wwn. */
4491 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4492 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4493 
4494 	/* Register SGL pool to the device using non-embedded mailbox command */
4495 	rc = lpfc_sli4_post_sgl_list(phba);
4496 	if (unlikely(rc)) {
4497 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4498 				"0582 Error %d during sgl post operation\n",
4499 					rc);
4500 		rc = -ENODEV;
4501 		goto out_free_vpd;
4502 	}
4503 
4504 	/* Register SCSI SGL pool to the device */
4505 	rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4506 	if (unlikely(rc)) {
4507 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4508 				"0383 Error %d during scsi sgl post "
4509 				"operation\n", rc);
4510 		/* Some Scsi buffers were moved to the abort scsi list */
4511 		/* A pci function reset will repost them */
4512 		rc = -ENODEV;
4513 		goto out_free_vpd;
4514 	}
4515 
4516 	/* Post the rpi header region to the device. */
4517 	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4518 	if (unlikely(rc)) {
4519 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4520 				"0393 Error %d during rpi post operation\n",
4521 				rc);
4522 		rc = -ENODEV;
4523 		goto out_free_vpd;
4524 	}
4525 
4526 	/* Set up all the queues to the device */
4527 	rc = lpfc_sli4_queue_setup(phba);
4528 	if (unlikely(rc)) {
4529 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4530 				"0381 Error %d during queue setup.\n ", rc);
4531 		goto out_stop_timers;
4532 	}
4533 
4534 	/* Arm the CQs and then EQs on device */
4535 	lpfc_sli4_arm_cqeq_intr(phba);
4536 
4537 	/* Indicate device interrupt mode */
4538 	phba->sli4_hba.intr_enable = 1;
4539 
4540 	/* Allow asynchronous mailbox command to go through */
4541 	spin_lock_irq(&phba->hbalock);
4542 	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4543 	spin_unlock_irq(&phba->hbalock);
4544 
4545 	/* Post receive buffers to the device */
4546 	lpfc_sli4_rb_setup(phba);
4547 
4548 	/* Reset HBA FCF states after HBA reset */
4549 	phba->fcf.fcf_flag = 0;
4550 	phba->fcf.current_rec.flag = 0;
4551 
4552 	/* Start the ELS watchdog timer */
4553 	mod_timer(&vport->els_tmofunc,
4554 		  jiffies + HZ * (phba->fc_ratov * 2));
4555 
4556 	/* Start heart beat timer */
4557 	mod_timer(&phba->hb_tmofunc,
4558 		  jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4559 	phba->hb_outstanding = 0;
4560 	phba->last_completion_time = jiffies;
4561 
4562 	/* Start error attention (ERATT) polling timer */
4563 	mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4564 
4565 	/* Enable PCIe device Advanced Error Reporting (AER) if configured */
4566 	if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4567 		rc = pci_enable_pcie_error_reporting(phba->pcidev);
4568 		if (!rc) {
4569 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4570 					"2829 This device supports "
4571 					"Advanced Error Reporting (AER)\n");
4572 			spin_lock_irq(&phba->hbalock);
4573 			phba->hba_flag |= HBA_AER_ENABLED;
4574 			spin_unlock_irq(&phba->hbalock);
4575 		} else {
4576 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4577 					"2830 This device does not support "
4578 					"Advanced Error Reporting (AER)\n");
4579 			phba->cfg_aer_support = 0;
4580 		}
4581 	}
4582 
4583 	/*
4584 	 * The port is ready, set the host's link state to LINK_DOWN
4585 	 * in preparation for link interrupts.
4586 	 */
4587 	lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4588 	mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4589 	lpfc_set_loopback_flag(phba);
4590 	/* Change driver state to LPFC_LINK_DOWN right before init link */
4591 	spin_lock_irq(&phba->hbalock);
4592 	phba->link_state = LPFC_LINK_DOWN;
4593 	spin_unlock_irq(&phba->hbalock);
4594 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4595 	if (unlikely(rc != MBX_NOT_FINISHED)) {
4596 		kfree(vpd);
4597 		return 0;
4598 	} else
4599 		rc = -EIO;
4600 
4601 	/* Unset all the queues set up in this routine when error out */
4602 	if (rc)
4603 		lpfc_sli4_queue_unset(phba);
4604 
4605 out_stop_timers:
4606 	if (rc)
4607 		lpfc_stop_hba_timers(phba);
4608 out_free_vpd:
4609 	kfree(vpd);
4610 out_free_mbox:
4611 	mempool_free(mboxq, phba->mbox_mem_pool);
4612 	return rc;
4613 }
4614 
4615 /**
4616  * lpfc_mbox_timeout - Timeout call back function for mbox timer
4617  * @ptr: context object - pointer to hba structure.
4618  *
4619  * This is the callback function for mailbox timer. The mailbox
4620  * timer is armed when a new mailbox command is issued and the timer
4621  * is deleted when the mailbox complete. The function is called by
4622  * the kernel timer code when a mailbox does not complete within
4623  * expected time. This function wakes up the worker thread to
4624  * process the mailbox timeout and returns. All the processing is
4625  * done by the worker thread function lpfc_mbox_timeout_handler.
4626  **/
4627 void
4628 lpfc_mbox_timeout(unsigned long ptr)
4629 {
4630 	struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4631 	unsigned long iflag;
4632 	uint32_t tmo_posted;
4633 
4634 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4635 	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4636 	if (!tmo_posted)
4637 		phba->pport->work_port_events |= WORKER_MBOX_TMO;
4638 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4639 
4640 	if (!tmo_posted)
4641 		lpfc_worker_wake_up(phba);
4642 	return;
4643 }
4644 
4645 
4646 /**
4647  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4648  * @phba: Pointer to HBA context object.
4649  *
4650  * This function is called from worker thread when a mailbox command times out.
4651  * The caller is not required to hold any locks. This function will reset the
4652  * HBA and recover all the pending commands.
4653  **/
4654 void
4655 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4656 {
4657 	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4658 	MAILBOX_t *mb = &pmbox->u.mb;
4659 	struct lpfc_sli *psli = &phba->sli;
4660 	struct lpfc_sli_ring *pring;
4661 
4662 	/* Check the pmbox pointer first.  There is a race condition
4663 	 * between the mbox timeout handler getting executed in the
4664 	 * worklist and the mailbox actually completing. When this
4665 	 * race condition occurs, the mbox_active will be NULL.
4666 	 */
4667 	spin_lock_irq(&phba->hbalock);
4668 	if (pmbox == NULL) {
4669 		lpfc_printf_log(phba, KERN_WARNING,
4670 				LOG_MBOX | LOG_SLI,
4671 				"0353 Active Mailbox cleared - mailbox timeout "
4672 				"exiting\n");
4673 		spin_unlock_irq(&phba->hbalock);
4674 		return;
4675 	}
4676 
4677 	/* Mbox cmd <mbxCommand> timeout */
4678 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4679 			"0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4680 			mb->mbxCommand,
4681 			phba->pport->port_state,
4682 			phba->sli.sli_flag,
4683 			phba->sli.mbox_active);
4684 	spin_unlock_irq(&phba->hbalock);
4685 
4686 	/* Setting state unknown so lpfc_sli_abort_iocb_ring
4687 	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4688 	 * it to fail all oustanding SCSI IO.
4689 	 */
4690 	spin_lock_irq(&phba->pport->work_port_lock);
4691 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4692 	spin_unlock_irq(&phba->pport->work_port_lock);
4693 	spin_lock_irq(&phba->hbalock);
4694 	phba->link_state = LPFC_LINK_UNKNOWN;
4695 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4696 	spin_unlock_irq(&phba->hbalock);
4697 
4698 	pring = &psli->ring[psli->fcp_ring];
4699 	lpfc_sli_abort_iocb_ring(phba, pring);
4700 
4701 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4702 			"0345 Resetting board due to mailbox timeout\n");
4703 
4704 	/* Reset the HBA device */
4705 	lpfc_reset_hba(phba);
4706 }
4707 
4708 /**
4709  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4710  * @phba: Pointer to HBA context object.
4711  * @pmbox: Pointer to mailbox object.
4712  * @flag: Flag indicating how the mailbox need to be processed.
4713  *
4714  * This function is called by discovery code and HBA management code
4715  * to submit a mailbox command to firmware with SLI-3 interface spec. This
4716  * function gets the hbalock to protect the data structures.
4717  * The mailbox command can be submitted in polling mode, in which case
4718  * this function will wait in a polling loop for the completion of the
4719  * mailbox.
4720  * If the mailbox is submitted in no_wait mode (not polling) the
4721  * function will submit the command and returns immediately without waiting
4722  * for the mailbox completion. The no_wait is supported only when HBA
4723  * is in SLI2/SLI3 mode - interrupts are enabled.
4724  * The SLI interface allows only one mailbox pending at a time. If the
4725  * mailbox is issued in polling mode and there is already a mailbox
4726  * pending, then the function will return an error. If the mailbox is issued
4727  * in NO_WAIT mode and there is a mailbox pending already, the function
4728  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4729  * The sli layer owns the mailbox object until the completion of mailbox
4730  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4731  * return codes the caller owns the mailbox command after the return of
4732  * the function.
4733  **/
4734 static int
4735 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4736 		       uint32_t flag)
4737 {
4738 	MAILBOX_t *mb;
4739 	struct lpfc_sli *psli = &phba->sli;
4740 	uint32_t status, evtctr;
4741 	uint32_t ha_copy;
4742 	int i;
4743 	unsigned long timeout;
4744 	unsigned long drvr_flag = 0;
4745 	uint32_t word0, ldata;
4746 	void __iomem *to_slim;
4747 	int processing_queue = 0;
4748 
4749 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
4750 	if (!pmbox) {
4751 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4752 		/* processing mbox queue from intr_handler */
4753 		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4754 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4755 			return MBX_SUCCESS;
4756 		}
4757 		processing_queue = 1;
4758 		pmbox = lpfc_mbox_get(phba);
4759 		if (!pmbox) {
4760 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4761 			return MBX_SUCCESS;
4762 		}
4763 	}
4764 
4765 	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4766 		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4767 		if(!pmbox->vport) {
4768 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4769 			lpfc_printf_log(phba, KERN_ERR,
4770 					LOG_MBOX | LOG_VPORT,
4771 					"1806 Mbox x%x failed. No vport\n",
4772 					pmbox->u.mb.mbxCommand);
4773 			dump_stack();
4774 			goto out_not_finished;
4775 		}
4776 	}
4777 
4778 	/* If the PCI channel is in offline state, do not post mbox. */
4779 	if (unlikely(pci_channel_offline(phba->pcidev))) {
4780 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4781 		goto out_not_finished;
4782 	}
4783 
4784 	/* If HBA has a deferred error attention, fail the iocb. */
4785 	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4786 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4787 		goto out_not_finished;
4788 	}
4789 
4790 	psli = &phba->sli;
4791 
4792 	mb = &pmbox->u.mb;
4793 	status = MBX_SUCCESS;
4794 
4795 	if (phba->link_state == LPFC_HBA_ERROR) {
4796 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4797 
4798 		/* Mbox command <mbxCommand> cannot issue */
4799 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4800 				"(%d):0311 Mailbox command x%x cannot "
4801 				"issue Data: x%x x%x\n",
4802 				pmbox->vport ? pmbox->vport->vpi : 0,
4803 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4804 		goto out_not_finished;
4805 	}
4806 
4807 	if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4808 	    !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4809 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4810 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4811 				"(%d):2528 Mailbox command x%x cannot "
4812 				"issue Data: x%x x%x\n",
4813 				pmbox->vport ? pmbox->vport->vpi : 0,
4814 				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4815 		goto out_not_finished;
4816 	}
4817 
4818 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4819 		/* Polling for a mbox command when another one is already active
4820 		 * is not allowed in SLI. Also, the driver must have established
4821 		 * SLI2 mode to queue and process multiple mbox commands.
4822 		 */
4823 
4824 		if (flag & MBX_POLL) {
4825 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4826 
4827 			/* Mbox command <mbxCommand> cannot issue */
4828 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4829 					"(%d):2529 Mailbox command x%x "
4830 					"cannot issue Data: x%x x%x\n",
4831 					pmbox->vport ? pmbox->vport->vpi : 0,
4832 					pmbox->u.mb.mbxCommand,
4833 					psli->sli_flag, flag);
4834 			goto out_not_finished;
4835 		}
4836 
4837 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4838 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4839 			/* Mbox command <mbxCommand> cannot issue */
4840 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4841 					"(%d):2530 Mailbox command x%x "
4842 					"cannot issue Data: x%x x%x\n",
4843 					pmbox->vport ? pmbox->vport->vpi : 0,
4844 					pmbox->u.mb.mbxCommand,
4845 					psli->sli_flag, flag);
4846 			goto out_not_finished;
4847 		}
4848 
4849 		/* Another mailbox command is still being processed, queue this
4850 		 * command to be processed later.
4851 		 */
4852 		lpfc_mbox_put(phba, pmbox);
4853 
4854 		/* Mbox cmd issue - BUSY */
4855 		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4856 				"(%d):0308 Mbox cmd issue - BUSY Data: "
4857 				"x%x x%x x%x x%x\n",
4858 				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4859 				mb->mbxCommand, phba->pport->port_state,
4860 				psli->sli_flag, flag);
4861 
4862 		psli->slistat.mbox_busy++;
4863 		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4864 
4865 		if (pmbox->vport) {
4866 			lpfc_debugfs_disc_trc(pmbox->vport,
4867 				LPFC_DISC_TRC_MBOX_VPORT,
4868 				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
4869 				(uint32_t)mb->mbxCommand,
4870 				mb->un.varWords[0], mb->un.varWords[1]);
4871 		}
4872 		else {
4873 			lpfc_debugfs_disc_trc(phba->pport,
4874 				LPFC_DISC_TRC_MBOX,
4875 				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
4876 				(uint32_t)mb->mbxCommand,
4877 				mb->un.varWords[0], mb->un.varWords[1]);
4878 		}
4879 
4880 		return MBX_BUSY;
4881 	}
4882 
4883 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4884 
4885 	/* If we are not polling, we MUST be in SLI2 mode */
4886 	if (flag != MBX_POLL) {
4887 		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4888 		    (mb->mbxCommand != MBX_KILL_BOARD)) {
4889 			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4890 			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4891 			/* Mbox command <mbxCommand> cannot issue */
4892 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4893 					"(%d):2531 Mailbox command x%x "
4894 					"cannot issue Data: x%x x%x\n",
4895 					pmbox->vport ? pmbox->vport->vpi : 0,
4896 					pmbox->u.mb.mbxCommand,
4897 					psli->sli_flag, flag);
4898 			goto out_not_finished;
4899 		}
4900 		/* timeout active mbox command */
4901 		mod_timer(&psli->mbox_tmo, (jiffies +
4902 			       (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4903 	}
4904 
4905 	/* Mailbox cmd <cmd> issue */
4906 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4907 			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4908 			"x%x\n",
4909 			pmbox->vport ? pmbox->vport->vpi : 0,
4910 			mb->mbxCommand, phba->pport->port_state,
4911 			psli->sli_flag, flag);
4912 
4913 	if (mb->mbxCommand != MBX_HEARTBEAT) {
4914 		if (pmbox->vport) {
4915 			lpfc_debugfs_disc_trc(pmbox->vport,
4916 				LPFC_DISC_TRC_MBOX_VPORT,
4917 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
4918 				(uint32_t)mb->mbxCommand,
4919 				mb->un.varWords[0], mb->un.varWords[1]);
4920 		}
4921 		else {
4922 			lpfc_debugfs_disc_trc(phba->pport,
4923 				LPFC_DISC_TRC_MBOX,
4924 				"MBOX Send:       cmd:x%x mb:x%x x%x",
4925 				(uint32_t)mb->mbxCommand,
4926 				mb->un.varWords[0], mb->un.varWords[1]);
4927 		}
4928 	}
4929 
4930 	psli->slistat.mbox_cmd++;
4931 	evtctr = psli->slistat.mbox_event;
4932 
4933 	/* next set own bit for the adapter and copy over command word */
4934 	mb->mbxOwner = OWN_CHIP;
4935 
4936 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4937 		/* Populate mbox extension offset word. */
4938 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4939 			*(((uint32_t *)mb) + pmbox->mbox_offset_word)
4940 				= (uint8_t *)phba->mbox_ext
4941 				  - (uint8_t *)phba->mbox;
4942 		}
4943 
4944 		/* Copy the mailbox extension data */
4945 		if (pmbox->in_ext_byte_len && pmbox->context2) {
4946 			lpfc_sli_pcimem_bcopy(pmbox->context2,
4947 				(uint8_t *)phba->mbox_ext,
4948 				pmbox->in_ext_byte_len);
4949 		}
4950 		/* Copy command data to host SLIM area */
4951 		lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4952 	} else {
4953 		/* Populate mbox extension offset word. */
4954 		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4955 			*(((uint32_t *)mb) + pmbox->mbox_offset_word)
4956 				= MAILBOX_HBA_EXT_OFFSET;
4957 
4958 		/* Copy the mailbox extension data */
4959 		if (pmbox->in_ext_byte_len && pmbox->context2) {
4960 			lpfc_memcpy_to_slim(phba->MBslimaddr +
4961 				MAILBOX_HBA_EXT_OFFSET,
4962 				pmbox->context2, pmbox->in_ext_byte_len);
4963 
4964 		}
4965 		if (mb->mbxCommand == MBX_CONFIG_PORT) {
4966 			/* copy command data into host mbox for cmpl */
4967 			lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4968 		}
4969 
4970 		/* First copy mbox command data to HBA SLIM, skip past first
4971 		   word */
4972 		to_slim = phba->MBslimaddr + sizeof (uint32_t);
4973 		lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4974 			    MAILBOX_CMD_SIZE - sizeof (uint32_t));
4975 
4976 		/* Next copy over first word, with mbxOwner set */
4977 		ldata = *((uint32_t *)mb);
4978 		to_slim = phba->MBslimaddr;
4979 		writel(ldata, to_slim);
4980 		readl(to_slim); /* flush */
4981 
4982 		if (mb->mbxCommand == MBX_CONFIG_PORT) {
4983 			/* switch over to host mailbox */
4984 			psli->sli_flag |= LPFC_SLI_ACTIVE;
4985 		}
4986 	}
4987 
4988 	wmb();
4989 
4990 	switch (flag) {
4991 	case MBX_NOWAIT:
4992 		/* Set up reference to mailbox command */
4993 		psli->mbox_active = pmbox;
4994 		/* Interrupt board to do it */
4995 		writel(CA_MBATT, phba->CAregaddr);
4996 		readl(phba->CAregaddr); /* flush */
4997 		/* Don't wait for it to finish, just return */
4998 		break;
4999 
5000 	case MBX_POLL:
5001 		/* Set up null reference to mailbox command */
5002 		psli->mbox_active = NULL;
5003 		/* Interrupt board to do it */
5004 		writel(CA_MBATT, phba->CAregaddr);
5005 		readl(phba->CAregaddr); /* flush */
5006 
5007 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5008 			/* First read mbox status word */
5009 			word0 = *((uint32_t *)phba->mbox);
5010 			word0 = le32_to_cpu(word0);
5011 		} else {
5012 			/* First read mbox status word */
5013 			word0 = readl(phba->MBslimaddr);
5014 		}
5015 
5016 		/* Read the HBA Host Attention Register */
5017 		ha_copy = readl(phba->HAregaddr);
5018 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5019 							     mb->mbxCommand) *
5020 					   1000) + jiffies;
5021 		i = 0;
5022 		/* Wait for command to complete */
5023 		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5024 		       (!(ha_copy & HA_MBATT) &&
5025 			(phba->link_state > LPFC_WARM_START))) {
5026 			if (time_after(jiffies, timeout)) {
5027 				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5028 				spin_unlock_irqrestore(&phba->hbalock,
5029 						       drvr_flag);
5030 				goto out_not_finished;
5031 			}
5032 
5033 			/* Check if we took a mbox interrupt while we were
5034 			   polling */
5035 			if (((word0 & OWN_CHIP) != OWN_CHIP)
5036 			    && (evtctr != psli->slistat.mbox_event))
5037 				break;
5038 
5039 			if (i++ > 10) {
5040 				spin_unlock_irqrestore(&phba->hbalock,
5041 						       drvr_flag);
5042 				msleep(1);
5043 				spin_lock_irqsave(&phba->hbalock, drvr_flag);
5044 			}
5045 
5046 			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5047 				/* First copy command data */
5048 				word0 = *((uint32_t *)phba->mbox);
5049 				word0 = le32_to_cpu(word0);
5050 				if (mb->mbxCommand == MBX_CONFIG_PORT) {
5051 					MAILBOX_t *slimmb;
5052 					uint32_t slimword0;
5053 					/* Check real SLIM for any errors */
5054 					slimword0 = readl(phba->MBslimaddr);
5055 					slimmb = (MAILBOX_t *) & slimword0;
5056 					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5057 					    && slimmb->mbxStatus) {
5058 						psli->sli_flag &=
5059 						    ~LPFC_SLI_ACTIVE;
5060 						word0 = slimword0;
5061 					}
5062 				}
5063 			} else {
5064 				/* First copy command data */
5065 				word0 = readl(phba->MBslimaddr);
5066 			}
5067 			/* Read the HBA Host Attention Register */
5068 			ha_copy = readl(phba->HAregaddr);
5069 		}
5070 
5071 		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5072 			/* copy results back to user */
5073 			lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5074 			/* Copy the mailbox extension data */
5075 			if (pmbox->out_ext_byte_len && pmbox->context2) {
5076 				lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5077 						      pmbox->context2,
5078 						      pmbox->out_ext_byte_len);
5079 			}
5080 		} else {
5081 			/* First copy command data */
5082 			lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5083 							MAILBOX_CMD_SIZE);
5084 			/* Copy the mailbox extension data */
5085 			if (pmbox->out_ext_byte_len && pmbox->context2) {
5086 				lpfc_memcpy_from_slim(pmbox->context2,
5087 					phba->MBslimaddr +
5088 					MAILBOX_HBA_EXT_OFFSET,
5089 					pmbox->out_ext_byte_len);
5090 			}
5091 		}
5092 
5093 		writel(HA_MBATT, phba->HAregaddr);
5094 		readl(phba->HAregaddr); /* flush */
5095 
5096 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5097 		status = mb->mbxStatus;
5098 	}
5099 
5100 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5101 	return status;
5102 
5103 out_not_finished:
5104 	if (processing_queue) {
5105 		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5106 		lpfc_mbox_cmpl_put(phba, pmbox);
5107 	}
5108 	return MBX_NOT_FINISHED;
5109 }
5110 
5111 /**
5112  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5113  * @phba: Pointer to HBA context object.
5114  *
5115  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5116  * the driver internal pending mailbox queue. It will then try to wait out the
5117  * possible outstanding mailbox command before return.
5118  *
5119  * Returns:
5120  * 	0 - the outstanding mailbox command completed; otherwise, the wait for
5121  * 	the outstanding mailbox command timed out.
5122  **/
5123 static int
5124 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5125 {
5126 	struct lpfc_sli *psli = &phba->sli;
5127 	uint8_t actcmd = MBX_HEARTBEAT;
5128 	int rc = 0;
5129 	unsigned long timeout;
5130 
5131 	/* Mark the asynchronous mailbox command posting as blocked */
5132 	spin_lock_irq(&phba->hbalock);
5133 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5134 	if (phba->sli.mbox_active)
5135 		actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5136 	spin_unlock_irq(&phba->hbalock);
5137 	/* Determine how long we might wait for the active mailbox
5138 	 * command to be gracefully completed by firmware.
5139 	 */
5140 	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5141 				   jiffies;
5142 	/* Wait for the outstnading mailbox command to complete */
5143 	while (phba->sli.mbox_active) {
5144 		/* Check active mailbox complete status every 2ms */
5145 		msleep(2);
5146 		if (time_after(jiffies, timeout)) {
5147 			/* Timeout, marked the outstanding cmd not complete */
5148 			rc = 1;
5149 			break;
5150 		}
5151 	}
5152 
5153 	/* Can not cleanly block async mailbox command, fails it */
5154 	if (rc) {
5155 		spin_lock_irq(&phba->hbalock);
5156 		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5157 		spin_unlock_irq(&phba->hbalock);
5158 	}
5159 	return rc;
5160 }
5161 
5162 /**
5163  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5164  * @phba: Pointer to HBA context object.
5165  *
5166  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5167  * commands from the driver internal pending mailbox queue. It makes sure
5168  * that there is no outstanding mailbox command before resuming posting
5169  * asynchronous mailbox commands. If, for any reason, there is outstanding
5170  * mailbox command, it will try to wait it out before resuming asynchronous
5171  * mailbox command posting.
5172  **/
5173 static void
5174 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5175 {
5176 	struct lpfc_sli *psli = &phba->sli;
5177 
5178 	spin_lock_irq(&phba->hbalock);
5179 	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5180 		/* Asynchronous mailbox posting is not blocked, do nothing */
5181 		spin_unlock_irq(&phba->hbalock);
5182 		return;
5183 	}
5184 
5185 	/* Outstanding synchronous mailbox command is guaranteed to be done,
5186 	 * successful or timeout, after timing-out the outstanding mailbox
5187 	 * command shall always be removed, so just unblock posting async
5188 	 * mailbox command and resume
5189 	 */
5190 	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5191 	spin_unlock_irq(&phba->hbalock);
5192 
5193 	/* wake up worker thread to post asynchronlous mailbox command */
5194 	lpfc_worker_wake_up(phba);
5195 }
5196 
5197 /**
5198  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5199  * @phba: Pointer to HBA context object.
5200  * @mboxq: Pointer to mailbox object.
5201  *
5202  * The function posts a mailbox to the port.  The mailbox is expected
5203  * to be comletely filled in and ready for the port to operate on it.
5204  * This routine executes a synchronous completion operation on the
5205  * mailbox by polling for its completion.
5206  *
5207  * The caller must not be holding any locks when calling this routine.
5208  *
5209  * Returns:
5210  *	MBX_SUCCESS - mailbox posted successfully
5211  *	Any of the MBX error values.
5212  **/
5213 static int
5214 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5215 {
5216 	int rc = MBX_SUCCESS;
5217 	unsigned long iflag;
5218 	uint32_t db_ready;
5219 	uint32_t mcqe_status;
5220 	uint32_t mbx_cmnd;
5221 	unsigned long timeout;
5222 	struct lpfc_sli *psli = &phba->sli;
5223 	struct lpfc_mqe *mb = &mboxq->u.mqe;
5224 	struct lpfc_bmbx_create *mbox_rgn;
5225 	struct dma_address *dma_address;
5226 	struct lpfc_register bmbx_reg;
5227 
5228 	/*
5229 	 * Only one mailbox can be active to the bootstrap mailbox region
5230 	 * at a time and there is no queueing provided.
5231 	 */
5232 	spin_lock_irqsave(&phba->hbalock, iflag);
5233 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5234 		spin_unlock_irqrestore(&phba->hbalock, iflag);
5235 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5236 				"(%d):2532 Mailbox command x%x (x%x) "
5237 				"cannot issue Data: x%x x%x\n",
5238 				mboxq->vport ? mboxq->vport->vpi : 0,
5239 				mboxq->u.mb.mbxCommand,
5240 				lpfc_sli4_mbox_opcode_get(phba, mboxq),
5241 				psli->sli_flag, MBX_POLL);
5242 		return MBXERR_ERROR;
5243 	}
5244 	/* The server grabs the token and owns it until release */
5245 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5246 	phba->sli.mbox_active = mboxq;
5247 	spin_unlock_irqrestore(&phba->hbalock, iflag);
5248 
5249 	/*
5250 	 * Initialize the bootstrap memory region to avoid stale data areas
5251 	 * in the mailbox post.  Then copy the caller's mailbox contents to
5252 	 * the bmbx mailbox region.
5253 	 */
5254 	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5255 	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5256 	lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5257 			      sizeof(struct lpfc_mqe));
5258 
5259 	/* Post the high mailbox dma address to the port and wait for ready. */
5260 	dma_address = &phba->sli4_hba.bmbx.dma_address;
5261 	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5262 
5263 	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5264 				   * 1000) + jiffies;
5265 	do {
5266 		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5267 		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5268 		if (!db_ready)
5269 			msleep(2);
5270 
5271 		if (time_after(jiffies, timeout)) {
5272 			rc = MBXERR_ERROR;
5273 			goto exit;
5274 		}
5275 	} while (!db_ready);
5276 
5277 	/* Post the low mailbox dma address to the port. */
5278 	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5279 	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5280 				   * 1000) + jiffies;
5281 	do {
5282 		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5283 		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5284 		if (!db_ready)
5285 			msleep(2);
5286 
5287 		if (time_after(jiffies, timeout)) {
5288 			rc = MBXERR_ERROR;
5289 			goto exit;
5290 		}
5291 	} while (!db_ready);
5292 
5293 	/*
5294 	 * Read the CQ to ensure the mailbox has completed.
5295 	 * If so, update the mailbox status so that the upper layers
5296 	 * can complete the request normally.
5297 	 */
5298 	lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5299 			      sizeof(struct lpfc_mqe));
5300 	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5301 	lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5302 			      sizeof(struct lpfc_mcqe));
5303 	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5304 
5305 	/* Prefix the mailbox status with range x4000 to note SLI4 status. */
5306 	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5307 		bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5308 		rc = MBXERR_ERROR;
5309 	} else
5310 		lpfc_sli4_swap_str(phba, mboxq);
5311 
5312 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5313 			"(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5314 			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5315 			" x%x x%x CQ: x%x x%x x%x x%x\n",
5316 			mboxq->vport ? mboxq->vport->vpi : 0,
5317 			mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5318 			bf_get(lpfc_mqe_status, mb),
5319 			mb->un.mb_words[0], mb->un.mb_words[1],
5320 			mb->un.mb_words[2], mb->un.mb_words[3],
5321 			mb->un.mb_words[4], mb->un.mb_words[5],
5322 			mb->un.mb_words[6], mb->un.mb_words[7],
5323 			mb->un.mb_words[8], mb->un.mb_words[9],
5324 			mb->un.mb_words[10], mb->un.mb_words[11],
5325 			mb->un.mb_words[12], mboxq->mcqe.word0,
5326 			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
5327 			mboxq->mcqe.trailer);
5328 exit:
5329 	/* We are holding the token, no needed for lock when release */
5330 	spin_lock_irqsave(&phba->hbalock, iflag);
5331 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5332 	phba->sli.mbox_active = NULL;
5333 	spin_unlock_irqrestore(&phba->hbalock, iflag);
5334 	return rc;
5335 }
5336 
5337 /**
5338  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5339  * @phba: Pointer to HBA context object.
5340  * @pmbox: Pointer to mailbox object.
5341  * @flag: Flag indicating how the mailbox need to be processed.
5342  *
5343  * This function is called by discovery code and HBA management code to submit
5344  * a mailbox command to firmware with SLI-4 interface spec.
5345  *
5346  * Return codes the caller owns the mailbox command after the return of the
5347  * function.
5348  **/
5349 static int
5350 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5351 		       uint32_t flag)
5352 {
5353 	struct lpfc_sli *psli = &phba->sli;
5354 	unsigned long iflags;
5355 	int rc;
5356 
5357 	rc = lpfc_mbox_dev_check(phba);
5358 	if (unlikely(rc)) {
5359 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5360 				"(%d):2544 Mailbox command x%x (x%x) "
5361 				"cannot issue Data: x%x x%x\n",
5362 				mboxq->vport ? mboxq->vport->vpi : 0,
5363 				mboxq->u.mb.mbxCommand,
5364 				lpfc_sli4_mbox_opcode_get(phba, mboxq),
5365 				psli->sli_flag, flag);
5366 		goto out_not_finished;
5367 	}
5368 
5369 	/* Detect polling mode and jump to a handler */
5370 	if (!phba->sli4_hba.intr_enable) {
5371 		if (flag == MBX_POLL)
5372 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5373 		else
5374 			rc = -EIO;
5375 		if (rc != MBX_SUCCESS)
5376 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5377 					"(%d):2541 Mailbox command x%x "
5378 					"(x%x) cannot issue Data: x%x x%x\n",
5379 					mboxq->vport ? mboxq->vport->vpi : 0,
5380 					mboxq->u.mb.mbxCommand,
5381 					lpfc_sli4_mbox_opcode_get(phba, mboxq),
5382 					psli->sli_flag, flag);
5383 		return rc;
5384 	} else if (flag == MBX_POLL) {
5385 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5386 				"(%d):2542 Try to issue mailbox command "
5387 				"x%x (x%x) synchronously ahead of async"
5388 				"mailbox command queue: x%x x%x\n",
5389 				mboxq->vport ? mboxq->vport->vpi : 0,
5390 				mboxq->u.mb.mbxCommand,
5391 				lpfc_sli4_mbox_opcode_get(phba, mboxq),
5392 				psli->sli_flag, flag);
5393 		/* Try to block the asynchronous mailbox posting */
5394 		rc = lpfc_sli4_async_mbox_block(phba);
5395 		if (!rc) {
5396 			/* Successfully blocked, now issue sync mbox cmd */
5397 			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5398 			if (rc != MBX_SUCCESS)
5399 				lpfc_printf_log(phba, KERN_ERR,
5400 						LOG_MBOX | LOG_SLI,
5401 						"(%d):2597 Mailbox command "
5402 						"x%x (x%x) cannot issue "
5403 						"Data: x%x x%x\n",
5404 						mboxq->vport ?
5405 						mboxq->vport->vpi : 0,
5406 						mboxq->u.mb.mbxCommand,
5407 						lpfc_sli4_mbox_opcode_get(phba,
5408 								mboxq),
5409 						psli->sli_flag, flag);
5410 			/* Unblock the async mailbox posting afterward */
5411 			lpfc_sli4_async_mbox_unblock(phba);
5412 		}
5413 		return rc;
5414 	}
5415 
5416 	/* Now, interrupt mode asynchrous mailbox command */
5417 	rc = lpfc_mbox_cmd_check(phba, mboxq);
5418 	if (rc) {
5419 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5420 				"(%d):2543 Mailbox command x%x (x%x) "
5421 				"cannot issue Data: x%x x%x\n",
5422 				mboxq->vport ? mboxq->vport->vpi : 0,
5423 				mboxq->u.mb.mbxCommand,
5424 				lpfc_sli4_mbox_opcode_get(phba, mboxq),
5425 				psli->sli_flag, flag);
5426 		goto out_not_finished;
5427 	}
5428 
5429 	/* Put the mailbox command to the driver internal FIFO */
5430 	psli->slistat.mbox_busy++;
5431 	spin_lock_irqsave(&phba->hbalock, iflags);
5432 	lpfc_mbox_put(phba, mboxq);
5433 	spin_unlock_irqrestore(&phba->hbalock, iflags);
5434 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5435 			"(%d):0354 Mbox cmd issue - Enqueue Data: "
5436 			"x%x (x%x) x%x x%x x%x\n",
5437 			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5438 			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5439 			lpfc_sli4_mbox_opcode_get(phba, mboxq),
5440 			phba->pport->port_state,
5441 			psli->sli_flag, MBX_NOWAIT);
5442 	/* Wake up worker thread to transport mailbox command from head */
5443 	lpfc_worker_wake_up(phba);
5444 
5445 	return MBX_BUSY;
5446 
5447 out_not_finished:
5448 	return MBX_NOT_FINISHED;
5449 }
5450 
5451 /**
5452  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5453  * @phba: Pointer to HBA context object.
5454  *
5455  * This function is called by worker thread to send a mailbox command to
5456  * SLI4 HBA firmware.
5457  *
5458  **/
5459 int
5460 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5461 {
5462 	struct lpfc_sli *psli = &phba->sli;
5463 	LPFC_MBOXQ_t *mboxq;
5464 	int rc = MBX_SUCCESS;
5465 	unsigned long iflags;
5466 	struct lpfc_mqe *mqe;
5467 	uint32_t mbx_cmnd;
5468 
5469 	/* Check interrupt mode before post async mailbox command */
5470 	if (unlikely(!phba->sli4_hba.intr_enable))
5471 		return MBX_NOT_FINISHED;
5472 
5473 	/* Check for mailbox command service token */
5474 	spin_lock_irqsave(&phba->hbalock, iflags);
5475 	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5476 		spin_unlock_irqrestore(&phba->hbalock, iflags);
5477 		return MBX_NOT_FINISHED;
5478 	}
5479 	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5480 		spin_unlock_irqrestore(&phba->hbalock, iflags);
5481 		return MBX_NOT_FINISHED;
5482 	}
5483 	if (unlikely(phba->sli.mbox_active)) {
5484 		spin_unlock_irqrestore(&phba->hbalock, iflags);
5485 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5486 				"0384 There is pending active mailbox cmd\n");
5487 		return MBX_NOT_FINISHED;
5488 	}
5489 	/* Take the mailbox command service token */
5490 	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5491 
5492 	/* Get the next mailbox command from head of queue */
5493 	mboxq = lpfc_mbox_get(phba);
5494 
5495 	/* If no more mailbox command waiting for post, we're done */
5496 	if (!mboxq) {
5497 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5498 		spin_unlock_irqrestore(&phba->hbalock, iflags);
5499 		return MBX_SUCCESS;
5500 	}
5501 	phba->sli.mbox_active = mboxq;
5502 	spin_unlock_irqrestore(&phba->hbalock, iflags);
5503 
5504 	/* Check device readiness for posting mailbox command */
5505 	rc = lpfc_mbox_dev_check(phba);
5506 	if (unlikely(rc))
5507 		/* Driver clean routine will clean up pending mailbox */
5508 		goto out_not_finished;
5509 
5510 	/* Prepare the mbox command to be posted */
5511 	mqe = &mboxq->u.mqe;
5512 	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5513 
5514 	/* Start timer for the mbox_tmo and log some mailbox post messages */
5515 	mod_timer(&psli->mbox_tmo, (jiffies +
5516 		  (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5517 
5518 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5519 			"(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5520 			"x%x x%x\n",
5521 			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5522 			lpfc_sli4_mbox_opcode_get(phba, mboxq),
5523 			phba->pport->port_state, psli->sli_flag);
5524 
5525 	if (mbx_cmnd != MBX_HEARTBEAT) {
5526 		if (mboxq->vport) {
5527 			lpfc_debugfs_disc_trc(mboxq->vport,
5528 				LPFC_DISC_TRC_MBOX_VPORT,
5529 				"MBOX Send vport: cmd:x%x mb:x%x x%x",
5530 				mbx_cmnd, mqe->un.mb_words[0],
5531 				mqe->un.mb_words[1]);
5532 		} else {
5533 			lpfc_debugfs_disc_trc(phba->pport,
5534 				LPFC_DISC_TRC_MBOX,
5535 				"MBOX Send: cmd:x%x mb:x%x x%x",
5536 				mbx_cmnd, mqe->un.mb_words[0],
5537 				mqe->un.mb_words[1]);
5538 		}
5539 	}
5540 	psli->slistat.mbox_cmd++;
5541 
5542 	/* Post the mailbox command to the port */
5543 	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5544 	if (rc != MBX_SUCCESS) {
5545 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5546 				"(%d):2533 Mailbox command x%x (x%x) "
5547 				"cannot issue Data: x%x x%x\n",
5548 				mboxq->vport ? mboxq->vport->vpi : 0,
5549 				mboxq->u.mb.mbxCommand,
5550 				lpfc_sli4_mbox_opcode_get(phba, mboxq),
5551 				psli->sli_flag, MBX_NOWAIT);
5552 		goto out_not_finished;
5553 	}
5554 
5555 	return rc;
5556 
5557 out_not_finished:
5558 	spin_lock_irqsave(&phba->hbalock, iflags);
5559 	mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5560 	__lpfc_mbox_cmpl_put(phba, mboxq);
5561 	/* Release the token */
5562 	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5563 	phba->sli.mbox_active = NULL;
5564 	spin_unlock_irqrestore(&phba->hbalock, iflags);
5565 
5566 	return MBX_NOT_FINISHED;
5567 }
5568 
5569 /**
5570  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5571  * @phba: Pointer to HBA context object.
5572  * @pmbox: Pointer to mailbox object.
5573  * @flag: Flag indicating how the mailbox need to be processed.
5574  *
5575  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5576  * the API jump table function pointer from the lpfc_hba struct.
5577  *
5578  * Return codes the caller owns the mailbox command after the return of the
5579  * function.
5580  **/
5581 int
5582 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5583 {
5584 	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5585 }
5586 
5587 /**
5588  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5589  * @phba: The hba struct for which this call is being executed.
5590  * @dev_grp: The HBA PCI-Device group number.
5591  *
5592  * This routine sets up the mbox interface API function jump table in @phba
5593  * struct.
5594  * Returns: 0 - success, -ENODEV - failure.
5595  **/
5596 int
5597 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5598 {
5599 
5600 	switch (dev_grp) {
5601 	case LPFC_PCI_DEV_LP:
5602 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5603 		phba->lpfc_sli_handle_slow_ring_event =
5604 				lpfc_sli_handle_slow_ring_event_s3;
5605 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5606 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5607 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5608 		break;
5609 	case LPFC_PCI_DEV_OC:
5610 		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5611 		phba->lpfc_sli_handle_slow_ring_event =
5612 				lpfc_sli_handle_slow_ring_event_s4;
5613 		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5614 		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5615 		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5616 		break;
5617 	default:
5618 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5619 				"1420 Invalid HBA PCI-device group: 0x%x\n",
5620 				dev_grp);
5621 		return -ENODEV;
5622 		break;
5623 	}
5624 	return 0;
5625 }
5626 
5627 /**
5628  * __lpfc_sli_ringtx_put - Add an iocb to the txq
5629  * @phba: Pointer to HBA context object.
5630  * @pring: Pointer to driver SLI ring object.
5631  * @piocb: Pointer to address of newly added command iocb.
5632  *
5633  * This function is called with hbalock held to add a command
5634  * iocb to the txq when SLI layer cannot submit the command iocb
5635  * to the ring.
5636  **/
5637 void
5638 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5639 		    struct lpfc_iocbq *piocb)
5640 {
5641 	/* Insert the caller's iocb in the txq tail for later processing. */
5642 	list_add_tail(&piocb->list, &pring->txq);
5643 	pring->txq_cnt++;
5644 }
5645 
5646 /**
5647  * lpfc_sli_next_iocb - Get the next iocb in the txq
5648  * @phba: Pointer to HBA context object.
5649  * @pring: Pointer to driver SLI ring object.
5650  * @piocb: Pointer to address of newly added command iocb.
5651  *
5652  * This function is called with hbalock held before a new
5653  * iocb is submitted to the firmware. This function checks
5654  * txq to flush the iocbs in txq to Firmware before
5655  * submitting new iocbs to the Firmware.
5656  * If there are iocbs in the txq which need to be submitted
5657  * to firmware, lpfc_sli_next_iocb returns the first element
5658  * of the txq after dequeuing it from txq.
5659  * If there is no iocb in the txq then the function will return
5660  * *piocb and *piocb is set to NULL. Caller needs to check
5661  * *piocb to find if there are more commands in the txq.
5662  **/
5663 static struct lpfc_iocbq *
5664 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5665 		   struct lpfc_iocbq **piocb)
5666 {
5667 	struct lpfc_iocbq * nextiocb;
5668 
5669 	nextiocb = lpfc_sli_ringtx_get(phba, pring);
5670 	if (!nextiocb) {
5671 		nextiocb = *piocb;
5672 		*piocb = NULL;
5673 	}
5674 
5675 	return nextiocb;
5676 }
5677 
5678 /**
5679  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5680  * @phba: Pointer to HBA context object.
5681  * @ring_number: SLI ring number to issue iocb on.
5682  * @piocb: Pointer to command iocb.
5683  * @flag: Flag indicating if this command can be put into txq.
5684  *
5685  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5686  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5687  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5688  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5689  * this function allows only iocbs for posting buffers. This function finds
5690  * next available slot in the command ring and posts the command to the
5691  * available slot and writes the port attention register to request HBA start
5692  * processing new iocb. If there is no slot available in the ring and
5693  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5694  * the function returns IOCB_BUSY.
5695  *
5696  * This function is called with hbalock held. The function will return success
5697  * after it successfully submit the iocb to firmware or after adding to the
5698  * txq.
5699  **/
5700 static int
5701 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5702 		    struct lpfc_iocbq *piocb, uint32_t flag)
5703 {
5704 	struct lpfc_iocbq *nextiocb;
5705 	IOCB_t *iocb;
5706 	struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5707 
5708 	if (piocb->iocb_cmpl && (!piocb->vport) &&
5709 	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5710 	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5711 		lpfc_printf_log(phba, KERN_ERR,
5712 				LOG_SLI | LOG_VPORT,
5713 				"1807 IOCB x%x failed. No vport\n",
5714 				piocb->iocb.ulpCommand);
5715 		dump_stack();
5716 		return IOCB_ERROR;
5717 	}
5718 
5719 
5720 	/* If the PCI channel is in offline state, do not post iocbs. */
5721 	if (unlikely(pci_channel_offline(phba->pcidev)))
5722 		return IOCB_ERROR;
5723 
5724 	/* If HBA has a deferred error attention, fail the iocb. */
5725 	if (unlikely(phba->hba_flag & DEFER_ERATT))
5726 		return IOCB_ERROR;
5727 
5728 	/*
5729 	 * We should never get an IOCB if we are in a < LINK_DOWN state
5730 	 */
5731 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5732 		return IOCB_ERROR;
5733 
5734 	/*
5735 	 * Check to see if we are blocking IOCB processing because of a
5736 	 * outstanding event.
5737 	 */
5738 	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5739 		goto iocb_busy;
5740 
5741 	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5742 		/*
5743 		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5744 		 * can be issued if the link is not up.
5745 		 */
5746 		switch (piocb->iocb.ulpCommand) {
5747 		case CMD_GEN_REQUEST64_CR:
5748 		case CMD_GEN_REQUEST64_CX:
5749 			if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5750 				(piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5751 					FC_RCTL_DD_UNSOL_CMD) ||
5752 				(piocb->iocb.un.genreq64.w5.hcsw.Type !=
5753 					MENLO_TRANSPORT_TYPE))
5754 
5755 				goto iocb_busy;
5756 			break;
5757 		case CMD_QUE_RING_BUF_CN:
5758 		case CMD_QUE_RING_BUF64_CN:
5759 			/*
5760 			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5761 			 * completion, iocb_cmpl MUST be 0.
5762 			 */
5763 			if (piocb->iocb_cmpl)
5764 				piocb->iocb_cmpl = NULL;
5765 			/*FALLTHROUGH*/
5766 		case CMD_CREATE_XRI_CR:
5767 		case CMD_CLOSE_XRI_CN:
5768 		case CMD_CLOSE_XRI_CX:
5769 			break;
5770 		default:
5771 			goto iocb_busy;
5772 		}
5773 
5774 	/*
5775 	 * For FCP commands, we must be in a state where we can process link
5776 	 * attention events.
5777 	 */
5778 	} else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5779 			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5780 		goto iocb_busy;
5781 	}
5782 
5783 	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5784 	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5785 		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5786 
5787 	if (iocb)
5788 		lpfc_sli_update_ring(phba, pring);
5789 	else
5790 		lpfc_sli_update_full_ring(phba, pring);
5791 
5792 	if (!piocb)
5793 		return IOCB_SUCCESS;
5794 
5795 	goto out_busy;
5796 
5797  iocb_busy:
5798 	pring->stats.iocb_cmd_delay++;
5799 
5800  out_busy:
5801 
5802 	if (!(flag & SLI_IOCB_RET_IOCB)) {
5803 		__lpfc_sli_ringtx_put(phba, pring, piocb);
5804 		return IOCB_SUCCESS;
5805 	}
5806 
5807 	return IOCB_BUSY;
5808 }
5809 
5810 /**
5811  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5812  * @phba: Pointer to HBA context object.
5813  * @piocb: Pointer to command iocb.
5814  * @sglq: Pointer to the scatter gather queue object.
5815  *
5816  * This routine converts the bpl or bde that is in the IOCB
5817  * to a sgl list for the sli4 hardware. The physical address
5818  * of the bpl/bde is converted back to a virtual address.
5819  * If the IOCB contains a BPL then the list of BDE's is
5820  * converted to sli4_sge's. If the IOCB contains a single
5821  * BDE then it is converted to a single sli_sge.
5822  * The IOCB is still in cpu endianess so the contents of
5823  * the bpl can be used without byte swapping.
5824  *
5825  * Returns valid XRI = Success, NO_XRI = Failure.
5826 **/
5827 static uint16_t
5828 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5829 		struct lpfc_sglq *sglq)
5830 {
5831 	uint16_t xritag = NO_XRI;
5832 	struct ulp_bde64 *bpl = NULL;
5833 	struct ulp_bde64 bde;
5834 	struct sli4_sge *sgl  = NULL;
5835 	IOCB_t *icmd;
5836 	int numBdes = 0;
5837 	int i = 0;
5838 
5839 	if (!piocbq || !sglq)
5840 		return xritag;
5841 
5842 	sgl  = (struct sli4_sge *)sglq->sgl;
5843 	icmd = &piocbq->iocb;
5844 	if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5845 		numBdes = icmd->un.genreq64.bdl.bdeSize /
5846 				sizeof(struct ulp_bde64);
5847 		/* The addrHigh and addrLow fields within the IOCB
5848 		 * have not been byteswapped yet so there is no
5849 		 * need to swap them back.
5850 		 */
5851 		bpl  = (struct ulp_bde64 *)
5852 			((struct lpfc_dmabuf *)piocbq->context3)->virt;
5853 
5854 		if (!bpl)
5855 			return xritag;
5856 
5857 		for (i = 0; i < numBdes; i++) {
5858 			/* Should already be byte swapped. */
5859 			sgl->addr_hi = bpl->addrHigh;
5860 			sgl->addr_lo = bpl->addrLow;
5861 
5862 			if ((i+1) == numBdes)
5863 				bf_set(lpfc_sli4_sge_last, sgl, 1);
5864 			else
5865 				bf_set(lpfc_sli4_sge_last, sgl, 0);
5866 			sgl->word2 = cpu_to_le32(sgl->word2);
5867 			/* swap the size field back to the cpu so we
5868 			 * can assign it to the sgl.
5869 			 */
5870 			bde.tus.w = le32_to_cpu(bpl->tus.w);
5871 			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
5872 			bpl++;
5873 			sgl++;
5874 		}
5875 	} else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5876 			/* The addrHigh and addrLow fields of the BDE have not
5877 			 * been byteswapped yet so they need to be swapped
5878 			 * before putting them in the sgl.
5879 			 */
5880 			sgl->addr_hi =
5881 				cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5882 			sgl->addr_lo =
5883 				cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5884 			bf_set(lpfc_sli4_sge_last, sgl, 1);
5885 			sgl->word2 = cpu_to_le32(sgl->word2);
5886 			sgl->sge_len =
5887 				cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
5888 	}
5889 	return sglq->sli4_xritag;
5890 }
5891 
5892 /**
5893  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5894  * @phba: Pointer to HBA context object.
5895  *
5896  * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5897  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5898  * held.
5899  *
5900  * Return: index into SLI4 fast-path FCP queue index.
5901  **/
5902 static uint32_t
5903 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
5904 {
5905 	++phba->fcp_qidx;
5906 	if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5907 		phba->fcp_qidx = 0;
5908 
5909 	return phba->fcp_qidx;
5910 }
5911 
5912 /**
5913  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5914  * @phba: Pointer to HBA context object.
5915  * @piocb: Pointer to command iocb.
5916  * @wqe: Pointer to the work queue entry.
5917  *
5918  * This routine converts the iocb command to its Work Queue Entry
5919  * equivalent. The wqe pointer should not have any fields set when
5920  * this routine is called because it will memcpy over them.
5921  * This routine does not set the CQ_ID or the WQEC bits in the
5922  * wqe.
5923  *
5924  * Returns: 0 = Success, IOCB_ERROR = Failure.
5925  **/
5926 static int
5927 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5928 		union lpfc_wqe *wqe)
5929 {
5930 	uint32_t xmit_len = 0, total_len = 0;
5931 	uint8_t ct = 0;
5932 	uint32_t fip;
5933 	uint32_t abort_tag;
5934 	uint8_t command_type = ELS_COMMAND_NON_FIP;
5935 	uint8_t cmnd;
5936 	uint16_t xritag;
5937 	struct ulp_bde64 *bpl = NULL;
5938 	uint32_t els_id = ELS_ID_DEFAULT;
5939 	int numBdes, i;
5940 	struct ulp_bde64 bde;
5941 
5942 	fip = phba->hba_flag & HBA_FIP_SUPPORT;
5943 	/* The fcp commands will set command type */
5944 	if (iocbq->iocb_flag &  LPFC_IO_FCP)
5945 		command_type = FCP_COMMAND;
5946 	else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
5947 		command_type = ELS_COMMAND_FIP;
5948 	else
5949 		command_type = ELS_COMMAND_NON_FIP;
5950 
5951 	/* Some of the fields are in the right position already */
5952 	memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5953 	abort_tag = (uint32_t) iocbq->iotag;
5954 	xritag = iocbq->sli4_xritag;
5955 	wqe->words[7] = 0; /* The ct field has moved so reset */
5956 	/* words0-2 bpl convert bde */
5957 	if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5958 		numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5959 				sizeof(struct ulp_bde64);
5960 		bpl  = (struct ulp_bde64 *)
5961 			((struct lpfc_dmabuf *)iocbq->context3)->virt;
5962 		if (!bpl)
5963 			return IOCB_ERROR;
5964 
5965 		/* Should already be byte swapped. */
5966 		wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
5967 		wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
5968 		/* swap the size field back to the cpu so we
5969 		 * can assign it to the sgl.
5970 		 */
5971 		wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
5972 		xmit_len = wqe->generic.bde.tus.f.bdeSize;
5973 		total_len = 0;
5974 		for (i = 0; i < numBdes; i++) {
5975 			bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
5976 			total_len += bde.tus.f.bdeSize;
5977 		}
5978 	} else
5979 		xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5980 
5981 	iocbq->iocb.ulpIoTag = iocbq->iotag;
5982 	cmnd = iocbq->iocb.ulpCommand;
5983 
5984 	switch (iocbq->iocb.ulpCommand) {
5985 	case CMD_ELS_REQUEST64_CR:
5986 		if (!iocbq->iocb.ulpLe) {
5987 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5988 				"2007 Only Limited Edition cmd Format"
5989 				" supported 0x%x\n",
5990 				iocbq->iocb.ulpCommand);
5991 			return IOCB_ERROR;
5992 		}
5993 		wqe->els_req.payload_len = xmit_len;
5994 		/* Els_reguest64 has a TMO */
5995 		bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5996 			iocbq->iocb.ulpTimeout);
5997 		/* Need a VF for word 4 set the vf bit*/
5998 		bf_set(els_req64_vf, &wqe->els_req, 0);
5999 		/* And a VFID for word 12 */
6000 		bf_set(els_req64_vfid, &wqe->els_req, 0);
6001 		/*
6002 		 * Set ct field to 3, indicates that the context_tag field
6003 		 * contains the FCFI and remote N_Port_ID is
6004 		 * in word 5.
6005 		 */
6006 
6007 		ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6008 		bf_set(lpfc_wqe_gen_context, &wqe->generic,
6009 				iocbq->iocb.ulpContext);
6010 
6011 		bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
6012 		bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6013 		/* CCP CCPE PV PRI in word10 were set in the memcpy */
6014 
6015 		if (command_type == ELS_COMMAND_FIP) {
6016 			els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6017 					>> LPFC_FIP_ELS_ID_SHIFT);
6018 		}
6019 		bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
6020 
6021 	break;
6022 	case CMD_XMIT_SEQUENCE64_CX:
6023 		bf_set(lpfc_wqe_gen_context, &wqe->generic,
6024 					iocbq->iocb.un.ulpWord[3]);
6025 		wqe->generic.word3 = 0;
6026 		bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6027 		/* The entire sequence is transmitted for this IOCB */
6028 		xmit_len = total_len;
6029 		cmnd = CMD_XMIT_SEQUENCE64_CR;
6030 	case CMD_XMIT_SEQUENCE64_CR:
6031 		/* word3 iocb=io_tag32 wqe=payload_offset */
6032 		/* payload offset used for multilpe outstanding
6033 		 * sequences on the same exchange
6034 		 */
6035 		wqe->words[3] = 0;
6036 		/* word4 relative_offset memcpy */
6037 		/* word5 r_ctl/df_ctl memcpy */
6038 		bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6039 		wqe->xmit_sequence.xmit_len = xmit_len;
6040 		command_type = OTHER_COMMAND;
6041 	break;
6042 	case CMD_XMIT_BCAST64_CN:
6043 		/* word3 iocb=iotag32 wqe=payload_len */
6044 		wqe->words[3] = 0; /* no definition for this in wqe */
6045 		/* word4 iocb=rsvd wqe=rsvd */
6046 		/* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6047 		/* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6048 		bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6049 			((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6050 	break;
6051 	case CMD_FCP_IWRITE64_CR:
6052 		command_type = FCP_COMMAND_DATA_OUT;
6053 		/* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6054 		 * confusing.
6055 		 * word3 is payload_len: byte offset to the sgl entry for the
6056 		 * fcp_command.
6057 		 * word4 is total xfer len, same as the IOCB->ulpParameter.
6058 		 * word5 is initial xfer len 0 = wait for xfer-ready
6059 		 */
6060 
6061 		/* Always wait for xfer-ready before sending data */
6062 		wqe->fcp_iwrite.initial_xfer_len = 0;
6063 		/* word 4 (xfer length) should have been set on the memcpy */
6064 
6065 	/* allow write to fall through to read */
6066 	case CMD_FCP_IREAD64_CR:
6067 		/* FCP_CMD is always the 1st sgl entry */
6068 		wqe->fcp_iread.payload_len =
6069 			xmit_len + sizeof(struct fcp_rsp);
6070 
6071 		/* word 4 (xfer length) should have been set on the memcpy */
6072 
6073 		bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6074 			iocbq->iocb.ulpFCP2Rcvy);
6075 		bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6076 		/* The XC bit and the XS bit are similar. The driver never
6077 		 * tracked whether or not the exchange was previouslly open.
6078 		 * XC = Exchange create, 0 is create. 1 is already open.
6079 		 * XS = link cmd: 1 do not close the exchange after command.
6080 		 * XS = 0 close exchange when command completes.
6081 		 * The only time we would not set the XC bit is when the XS bit
6082 		 * is set and we are sending our 2nd or greater command on
6083 		 * this exchange.
6084 		 */
6085 		/* Always open the exchange */
6086 		bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6087 
6088 		wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6089 		bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6090 		break;
6091 	case CMD_FCP_ICMND64_CR:
6092 		/* Always open the exchange */
6093 		bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6094 
6095 		wqe->words[4] = 0;
6096 		wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6097 		bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6098 	break;
6099 	case CMD_GEN_REQUEST64_CR:
6100 		/* word3 command length is described as byte offset to the
6101 		 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6102 		 * sgl[0] = cmnd
6103 		 * sgl[1] = rsp.
6104 		 *
6105 		 */
6106 		wqe->gen_req.command_len = xmit_len;
6107 		/* Word4 parameter  copied in the memcpy */
6108 		/* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6109 		/* word6 context tag copied in memcpy */
6110 		if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6111 			ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6112 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6113 				"2015 Invalid CT %x command 0x%x\n",
6114 				ct, iocbq->iocb.ulpCommand);
6115 			return IOCB_ERROR;
6116 		}
6117 		bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6118 		bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6119 			iocbq->iocb.ulpTimeout);
6120 
6121 		bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6122 		command_type = OTHER_COMMAND;
6123 	break;
6124 	case CMD_XMIT_ELS_RSP64_CX:
6125 		/* words0-2 BDE memcpy */
6126 		/* word3 iocb=iotag32 wqe=rsvd */
6127 		wqe->words[3] = 0;
6128 		/* word4 iocb=did wge=rsvd. */
6129 		wqe->words[4] = 0;
6130 		/* word5 iocb=rsvd wge=did */
6131 		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6132 			 iocbq->iocb.un.elsreq64.remoteID);
6133 
6134 		bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6135 			((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6136 
6137 		bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6138 		bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6139 		if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6140 			bf_set(lpfc_wqe_gen_context, &wqe->generic,
6141 			       iocbq->vport->vpi + phba->vpi_base);
6142 		command_type = OTHER_COMMAND;
6143 	break;
6144 	case CMD_CLOSE_XRI_CN:
6145 	case CMD_ABORT_XRI_CN:
6146 	case CMD_ABORT_XRI_CX:
6147 		/* words 0-2 memcpy should be 0 rserved */
6148 		/* port will send abts */
6149 		if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6150 			/*
6151 			 * The link is down so the fw does not need to send abts
6152 			 * on the wire.
6153 			 */
6154 			bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6155 		else
6156 			bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6157 		bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6158 		wqe->words[5] = 0;
6159 		bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6160 			((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6161 		abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6162 		/*
6163 		 * The abort handler will send us CMD_ABORT_XRI_CN or
6164 		 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6165 		 */
6166 		bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6167 		cmnd = CMD_ABORT_XRI_CX;
6168 		command_type = OTHER_COMMAND;
6169 		xritag = 0;
6170 	break;
6171 	case CMD_XMIT_BLS_RSP64_CX:
6172 		/* As BLS ABTS-ACC WQE is very different from other WQEs,
6173 		 * we re-construct this WQE here based on information in
6174 		 * iocbq from scratch.
6175 		 */
6176 		memset(wqe, 0, sizeof(union lpfc_wqe));
6177 		/* OX_ID is invariable to who sent ABTS to CT exchange */
6178 		bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6179 		       bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6180 		if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6181 		    LPFC_ABTS_UNSOL_INT) {
6182 			/* ABTS sent by initiator to CT exchange, the
6183 			 * RX_ID field will be filled with the newly
6184 			 * allocated responder XRI.
6185 			 */
6186 			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6187 			       iocbq->sli4_xritag);
6188 		} else {
6189 			/* ABTS sent by responder to CT exchange, the
6190 			 * RX_ID field will be filled with the responder
6191 			 * RX_ID from ABTS.
6192 			 */
6193 			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6194 			       bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6195 		}
6196 		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6197 		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6198 		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6199 		       iocbq->iocb.ulpContext);
6200 		/* Overwrite the pre-set comnd type with OTHER_COMMAND */
6201 		command_type = OTHER_COMMAND;
6202 	break;
6203 	case CMD_XRI_ABORTED_CX:
6204 	case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6205 		/* words0-2 are all 0's no bde */
6206 		/* word3 and word4 are rsvrd */
6207 		wqe->words[3] = 0;
6208 		wqe->words[4] = 0;
6209 		/* word5 iocb=rsvd wge=did */
6210 		/* There is no remote port id in the IOCB? */
6211 		/* Let this fall through and fail */
6212 	case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6213 	case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6214 	case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6215 	case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6216 	default:
6217 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6218 				"2014 Invalid command 0x%x\n",
6219 				iocbq->iocb.ulpCommand);
6220 		return IOCB_ERROR;
6221 	break;
6222 
6223 	}
6224 	bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6225 	bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6226 	wqe->generic.abort_tag = abort_tag;
6227 	bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6228 	bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6229 	bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6230 	bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6231 
6232 	return 0;
6233 }
6234 
6235 /**
6236  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6237  * @phba: Pointer to HBA context object.
6238  * @ring_number: SLI ring number to issue iocb on.
6239  * @piocb: Pointer to command iocb.
6240  * @flag: Flag indicating if this command can be put into txq.
6241  *
6242  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6243  * an iocb command to an HBA with SLI-4 interface spec.
6244  *
6245  * This function is called with hbalock held. The function will return success
6246  * after it successfully submit the iocb to firmware or after adding to the
6247  * txq.
6248  **/
6249 static int
6250 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6251 			 struct lpfc_iocbq *piocb, uint32_t flag)
6252 {
6253 	struct lpfc_sglq *sglq;
6254 	union lpfc_wqe wqe;
6255 	struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6256 
6257 	if (piocb->sli4_xritag == NO_XRI) {
6258 		if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6259 		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6260 			sglq = NULL;
6261 		else {
6262 			if (pring->txq_cnt) {
6263 				if (!(flag & SLI_IOCB_RET_IOCB)) {
6264 					__lpfc_sli_ringtx_put(phba,
6265 						pring, piocb);
6266 					return IOCB_SUCCESS;
6267 				} else {
6268 					return IOCB_BUSY;
6269 				}
6270 			} else {
6271 			sglq = __lpfc_sli_get_sglq(phba);
6272 				if (!sglq) {
6273 					if (!(flag & SLI_IOCB_RET_IOCB)) {
6274 						__lpfc_sli_ringtx_put(phba,
6275 								pring,
6276 								piocb);
6277 						return IOCB_SUCCESS;
6278 					} else
6279 						return IOCB_BUSY;
6280 				}
6281 			}
6282 		}
6283 	} else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6284 		sglq = NULL; /* These IO's already have an XRI and
6285 			      * a mapped sgl.
6286 			      */
6287 	} else {
6288 		/* This is a continuation of a commandi,(CX) so this
6289 		 * sglq is on the active list
6290 		 */
6291 		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6292 		if (!sglq)
6293 			return IOCB_ERROR;
6294 	}
6295 
6296 	if (sglq) {
6297 		piocb->sli4_xritag = sglq->sli4_xritag;
6298 
6299 		if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
6300 			return IOCB_ERROR;
6301 	}
6302 
6303 	if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6304 		return IOCB_ERROR;
6305 
6306 	if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6307 		(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6308 		/*
6309 		 * For FCP command IOCB, get a new WQ index to distribute
6310 		 * WQE across the WQsr. On the other hand, for abort IOCB,
6311 		 * it carries the same WQ index to the original command
6312 		 * IOCB.
6313 		 */
6314 		if (piocb->iocb_flag & LPFC_IO_FCP)
6315 			piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6316 		if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6317 				     &wqe))
6318 			return IOCB_ERROR;
6319 	} else {
6320 		if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6321 			return IOCB_ERROR;
6322 	}
6323 	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6324 
6325 	return 0;
6326 }
6327 
6328 /**
6329  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6330  *
6331  * This routine wraps the actual lockless version for issusing IOCB function
6332  * pointer from the lpfc_hba struct.
6333  *
6334  * Return codes:
6335  * 	IOCB_ERROR - Error
6336  * 	IOCB_SUCCESS - Success
6337  * 	IOCB_BUSY - Busy
6338  **/
6339 int
6340 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6341 		struct lpfc_iocbq *piocb, uint32_t flag)
6342 {
6343 	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6344 }
6345 
6346 /**
6347  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6348  * @phba: The hba struct for which this call is being executed.
6349  * @dev_grp: The HBA PCI-Device group number.
6350  *
6351  * This routine sets up the SLI interface API function jump table in @phba
6352  * struct.
6353  * Returns: 0 - success, -ENODEV - failure.
6354  **/
6355 int
6356 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6357 {
6358 
6359 	switch (dev_grp) {
6360 	case LPFC_PCI_DEV_LP:
6361 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6362 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6363 		break;
6364 	case LPFC_PCI_DEV_OC:
6365 		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6366 		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6367 		break;
6368 	default:
6369 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6370 				"1419 Invalid HBA PCI-device group: 0x%x\n",
6371 				dev_grp);
6372 		return -ENODEV;
6373 		break;
6374 	}
6375 	phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6376 	return 0;
6377 }
6378 
6379 /**
6380  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6381  * @phba: Pointer to HBA context object.
6382  * @pring: Pointer to driver SLI ring object.
6383  * @piocb: Pointer to command iocb.
6384  * @flag: Flag indicating if this command can be put into txq.
6385  *
6386  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6387  * function. This function gets the hbalock and calls
6388  * __lpfc_sli_issue_iocb function and will return the error returned
6389  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6390  * functions which do not hold hbalock.
6391  **/
6392 int
6393 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6394 		    struct lpfc_iocbq *piocb, uint32_t flag)
6395 {
6396 	unsigned long iflags;
6397 	int rc;
6398 
6399 	spin_lock_irqsave(&phba->hbalock, iflags);
6400 	rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6401 	spin_unlock_irqrestore(&phba->hbalock, iflags);
6402 
6403 	return rc;
6404 }
6405 
6406 /**
6407  * lpfc_extra_ring_setup - Extra ring setup function
6408  * @phba: Pointer to HBA context object.
6409  *
6410  * This function is called while driver attaches with the
6411  * HBA to setup the extra ring. The extra ring is used
6412  * only when driver needs to support target mode functionality
6413  * or IP over FC functionalities.
6414  *
6415  * This function is called with no lock held.
6416  **/
6417 static int
6418 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6419 {
6420 	struct lpfc_sli *psli;
6421 	struct lpfc_sli_ring *pring;
6422 
6423 	psli = &phba->sli;
6424 
6425 	/* Adjust cmd/rsp ring iocb entries more evenly */
6426 
6427 	/* Take some away from the FCP ring */
6428 	pring = &psli->ring[psli->fcp_ring];
6429 	pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6430 	pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6431 	pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6432 	pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6433 
6434 	/* and give them to the extra ring */
6435 	pring = &psli->ring[psli->extra_ring];
6436 
6437 	pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6438 	pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6439 	pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6440 	pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6441 
6442 	/* Setup default profile for this ring */
6443 	pring->iotag_max = 4096;
6444 	pring->num_mask = 1;
6445 	pring->prt[0].profile = 0;      /* Mask 0 */
6446 	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6447 	pring->prt[0].type = phba->cfg_multi_ring_type;
6448 	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6449 	return 0;
6450 }
6451 
6452 /**
6453  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6454  * @phba: Pointer to HBA context object.
6455  * @pring: Pointer to driver SLI ring object.
6456  * @iocbq: Pointer to iocb object.
6457  *
6458  * This function is called by the slow ring event handler
6459  * function when there is an ASYNC event iocb in the ring.
6460  * This function is called with no lock held.
6461  * Currently this function handles only temperature related
6462  * ASYNC events. The function decodes the temperature sensor
6463  * event message and posts events for the management applications.
6464  **/
6465 static void
6466 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6467 	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6468 {
6469 	IOCB_t *icmd;
6470 	uint16_t evt_code;
6471 	uint16_t temp;
6472 	struct temp_event temp_event_data;
6473 	struct Scsi_Host *shost;
6474 	uint32_t *iocb_w;
6475 
6476 	icmd = &iocbq->iocb;
6477 	evt_code = icmd->un.asyncstat.evt_code;
6478 	temp = icmd->ulpContext;
6479 
6480 	if ((evt_code != ASYNC_TEMP_WARN) &&
6481 		(evt_code != ASYNC_TEMP_SAFE)) {
6482 		iocb_w = (uint32_t *) icmd;
6483 		lpfc_printf_log(phba,
6484 			KERN_ERR,
6485 			LOG_SLI,
6486 			"0346 Ring %d handler: unexpected ASYNC_STATUS"
6487 			" evt_code 0x%x\n"
6488 			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6489 			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6490 			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6491 			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6492 			pring->ringno,
6493 			icmd->un.asyncstat.evt_code,
6494 			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6495 			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6496 			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6497 			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6498 
6499 		return;
6500 	}
6501 	temp_event_data.data = (uint32_t)temp;
6502 	temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6503 	if (evt_code == ASYNC_TEMP_WARN) {
6504 		temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6505 		lpfc_printf_log(phba,
6506 				KERN_ERR,
6507 				LOG_TEMP,
6508 				"0347 Adapter is very hot, please take "
6509 				"corrective action. temperature : %d Celsius\n",
6510 				temp);
6511 	}
6512 	if (evt_code == ASYNC_TEMP_SAFE) {
6513 		temp_event_data.event_code = LPFC_NORMAL_TEMP;
6514 		lpfc_printf_log(phba,
6515 				KERN_ERR,
6516 				LOG_TEMP,
6517 				"0340 Adapter temperature is OK now. "
6518 				"temperature : %d Celsius\n",
6519 				temp);
6520 	}
6521 
6522 	/* Send temperature change event to applications */
6523 	shost = lpfc_shost_from_vport(phba->pport);
6524 	fc_host_post_vendor_event(shost, fc_get_event_number(),
6525 		sizeof(temp_event_data), (char *) &temp_event_data,
6526 		LPFC_NL_VENDOR_ID);
6527 
6528 }
6529 
6530 
6531 /**
6532  * lpfc_sli_setup - SLI ring setup function
6533  * @phba: Pointer to HBA context object.
6534  *
6535  * lpfc_sli_setup sets up rings of the SLI interface with
6536  * number of iocbs per ring and iotags. This function is
6537  * called while driver attach to the HBA and before the
6538  * interrupts are enabled. So there is no need for locking.
6539  *
6540  * This function always returns 0.
6541  **/
6542 int
6543 lpfc_sli_setup(struct lpfc_hba *phba)
6544 {
6545 	int i, totiocbsize = 0;
6546 	struct lpfc_sli *psli = &phba->sli;
6547 	struct lpfc_sli_ring *pring;
6548 
6549 	psli->num_rings = MAX_CONFIGURED_RINGS;
6550 	psli->sli_flag = 0;
6551 	psli->fcp_ring = LPFC_FCP_RING;
6552 	psli->next_ring = LPFC_FCP_NEXT_RING;
6553 	psli->extra_ring = LPFC_EXTRA_RING;
6554 
6555 	psli->iocbq_lookup = NULL;
6556 	psli->iocbq_lookup_len = 0;
6557 	psli->last_iotag = 0;
6558 
6559 	for (i = 0; i < psli->num_rings; i++) {
6560 		pring = &psli->ring[i];
6561 		switch (i) {
6562 		case LPFC_FCP_RING:	/* ring 0 - FCP */
6563 			/* numCiocb and numRiocb are used in config_port */
6564 			pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6565 			pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6566 			pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6567 			pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6568 			pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6569 			pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6570 			pring->sizeCiocb = (phba->sli_rev == 3) ?
6571 							SLI3_IOCB_CMD_SIZE :
6572 							SLI2_IOCB_CMD_SIZE;
6573 			pring->sizeRiocb = (phba->sli_rev == 3) ?
6574 							SLI3_IOCB_RSP_SIZE :
6575 							SLI2_IOCB_RSP_SIZE;
6576 			pring->iotag_ctr = 0;
6577 			pring->iotag_max =
6578 			    (phba->cfg_hba_queue_depth * 2);
6579 			pring->fast_iotag = pring->iotag_max;
6580 			pring->num_mask = 0;
6581 			break;
6582 		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
6583 			/* numCiocb and numRiocb are used in config_port */
6584 			pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6585 			pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6586 			pring->sizeCiocb = (phba->sli_rev == 3) ?
6587 							SLI3_IOCB_CMD_SIZE :
6588 							SLI2_IOCB_CMD_SIZE;
6589 			pring->sizeRiocb = (phba->sli_rev == 3) ?
6590 							SLI3_IOCB_RSP_SIZE :
6591 							SLI2_IOCB_RSP_SIZE;
6592 			pring->iotag_max = phba->cfg_hba_queue_depth;
6593 			pring->num_mask = 0;
6594 			break;
6595 		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
6596 			/* numCiocb and numRiocb are used in config_port */
6597 			pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6598 			pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6599 			pring->sizeCiocb = (phba->sli_rev == 3) ?
6600 							SLI3_IOCB_CMD_SIZE :
6601 							SLI2_IOCB_CMD_SIZE;
6602 			pring->sizeRiocb = (phba->sli_rev == 3) ?
6603 							SLI3_IOCB_RSP_SIZE :
6604 							SLI2_IOCB_RSP_SIZE;
6605 			pring->fast_iotag = 0;
6606 			pring->iotag_ctr = 0;
6607 			pring->iotag_max = 4096;
6608 			pring->lpfc_sli_rcv_async_status =
6609 				lpfc_sli_async_event_handler;
6610 			pring->num_mask = LPFC_MAX_RING_MASK;
6611 			pring->prt[0].profile = 0;	/* Mask 0 */
6612 			pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6613 			pring->prt[0].type = FC_TYPE_ELS;
6614 			pring->prt[0].lpfc_sli_rcv_unsol_event =
6615 			    lpfc_els_unsol_event;
6616 			pring->prt[1].profile = 0;	/* Mask 1 */
6617 			pring->prt[1].rctl = FC_RCTL_ELS_REP;
6618 			pring->prt[1].type = FC_TYPE_ELS;
6619 			pring->prt[1].lpfc_sli_rcv_unsol_event =
6620 			    lpfc_els_unsol_event;
6621 			pring->prt[2].profile = 0;	/* Mask 2 */
6622 			/* NameServer Inquiry */
6623 			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
6624 			/* NameServer */
6625 			pring->prt[2].type = FC_TYPE_CT;
6626 			pring->prt[2].lpfc_sli_rcv_unsol_event =
6627 			    lpfc_ct_unsol_event;
6628 			pring->prt[3].profile = 0;	/* Mask 3 */
6629 			/* NameServer response */
6630 			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
6631 			/* NameServer */
6632 			pring->prt[3].type = FC_TYPE_CT;
6633 			pring->prt[3].lpfc_sli_rcv_unsol_event =
6634 			    lpfc_ct_unsol_event;
6635 			/* abort unsolicited sequence */
6636 			pring->prt[4].profile = 0;	/* Mask 4 */
6637 			pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6638 			pring->prt[4].type = FC_TYPE_BLS;
6639 			pring->prt[4].lpfc_sli_rcv_unsol_event =
6640 			    lpfc_sli4_ct_abort_unsol_event;
6641 			break;
6642 		}
6643 		totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6644 				(pring->numRiocb * pring->sizeRiocb);
6645 	}
6646 	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6647 		/* Too many cmd / rsp ring entries in SLI2 SLIM */
6648 		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6649 		       "SLI2 SLIM Data: x%x x%lx\n",
6650 		       phba->brd_no, totiocbsize,
6651 		       (unsigned long) MAX_SLIM_IOCB_SIZE);
6652 	}
6653 	if (phba->cfg_multi_ring_support == 2)
6654 		lpfc_extra_ring_setup(phba);
6655 
6656 	return 0;
6657 }
6658 
6659 /**
6660  * lpfc_sli_queue_setup - Queue initialization function
6661  * @phba: Pointer to HBA context object.
6662  *
6663  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6664  * ring. This function also initializes ring indices of each ring.
6665  * This function is called during the initialization of the SLI
6666  * interface of an HBA.
6667  * This function is called with no lock held and always returns
6668  * 1.
6669  **/
6670 int
6671 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6672 {
6673 	struct lpfc_sli *psli;
6674 	struct lpfc_sli_ring *pring;
6675 	int i;
6676 
6677 	psli = &phba->sli;
6678 	spin_lock_irq(&phba->hbalock);
6679 	INIT_LIST_HEAD(&psli->mboxq);
6680 	INIT_LIST_HEAD(&psli->mboxq_cmpl);
6681 	/* Initialize list headers for txq and txcmplq as double linked lists */
6682 	for (i = 0; i < psli->num_rings; i++) {
6683 		pring = &psli->ring[i];
6684 		pring->ringno = i;
6685 		pring->next_cmdidx  = 0;
6686 		pring->local_getidx = 0;
6687 		pring->cmdidx = 0;
6688 		INIT_LIST_HEAD(&pring->txq);
6689 		INIT_LIST_HEAD(&pring->txcmplq);
6690 		INIT_LIST_HEAD(&pring->iocb_continueq);
6691 		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6692 		INIT_LIST_HEAD(&pring->postbufq);
6693 	}
6694 	spin_unlock_irq(&phba->hbalock);
6695 	return 1;
6696 }
6697 
6698 /**
6699  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6700  * @phba: Pointer to HBA context object.
6701  *
6702  * This routine flushes the mailbox command subsystem. It will unconditionally
6703  * flush all the mailbox commands in the three possible stages in the mailbox
6704  * command sub-system: pending mailbox command queue; the outstanding mailbox
6705  * command; and completed mailbox command queue. It is caller's responsibility
6706  * to make sure that the driver is in the proper state to flush the mailbox
6707  * command sub-system. Namely, the posting of mailbox commands into the
6708  * pending mailbox command queue from the various clients must be stopped;
6709  * either the HBA is in a state that it will never works on the outstanding
6710  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6711  * mailbox command has been completed.
6712  **/
6713 static void
6714 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6715 {
6716 	LIST_HEAD(completions);
6717 	struct lpfc_sli *psli = &phba->sli;
6718 	LPFC_MBOXQ_t *pmb;
6719 	unsigned long iflag;
6720 
6721 	/* Flush all the mailbox commands in the mbox system */
6722 	spin_lock_irqsave(&phba->hbalock, iflag);
6723 	/* The pending mailbox command queue */
6724 	list_splice_init(&phba->sli.mboxq, &completions);
6725 	/* The outstanding active mailbox command */
6726 	if (psli->mbox_active) {
6727 		list_add_tail(&psli->mbox_active->list, &completions);
6728 		psli->mbox_active = NULL;
6729 		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6730 	}
6731 	/* The completed mailbox command queue */
6732 	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6733 	spin_unlock_irqrestore(&phba->hbalock, iflag);
6734 
6735 	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6736 	while (!list_empty(&completions)) {
6737 		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6738 		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6739 		if (pmb->mbox_cmpl)
6740 			pmb->mbox_cmpl(phba, pmb);
6741 	}
6742 }
6743 
6744 /**
6745  * lpfc_sli_host_down - Vport cleanup function
6746  * @vport: Pointer to virtual port object.
6747  *
6748  * lpfc_sli_host_down is called to clean up the resources
6749  * associated with a vport before destroying virtual
6750  * port data structures.
6751  * This function does following operations:
6752  * - Free discovery resources associated with this virtual
6753  *   port.
6754  * - Free iocbs associated with this virtual port in
6755  *   the txq.
6756  * - Send abort for all iocb commands associated with this
6757  *   vport in txcmplq.
6758  *
6759  * This function is called with no lock held and always returns 1.
6760  **/
6761 int
6762 lpfc_sli_host_down(struct lpfc_vport *vport)
6763 {
6764 	LIST_HEAD(completions);
6765 	struct lpfc_hba *phba = vport->phba;
6766 	struct lpfc_sli *psli = &phba->sli;
6767 	struct lpfc_sli_ring *pring;
6768 	struct lpfc_iocbq *iocb, *next_iocb;
6769 	int i;
6770 	unsigned long flags = 0;
6771 	uint16_t prev_pring_flag;
6772 
6773 	lpfc_cleanup_discovery_resources(vport);
6774 
6775 	spin_lock_irqsave(&phba->hbalock, flags);
6776 	for (i = 0; i < psli->num_rings; i++) {
6777 		pring = &psli->ring[i];
6778 		prev_pring_flag = pring->flag;
6779 		/* Only slow rings */
6780 		if (pring->ringno == LPFC_ELS_RING) {
6781 			pring->flag |= LPFC_DEFERRED_RING_EVENT;
6782 			/* Set the lpfc data pending flag */
6783 			set_bit(LPFC_DATA_READY, &phba->data_flags);
6784 		}
6785 		/*
6786 		 * Error everything on the txq since these iocbs have not been
6787 		 * given to the FW yet.
6788 		 */
6789 		list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6790 			if (iocb->vport != vport)
6791 				continue;
6792 			list_move_tail(&iocb->list, &completions);
6793 			pring->txq_cnt--;
6794 		}
6795 
6796 		/* Next issue ABTS for everything on the txcmplq */
6797 		list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6798 									list) {
6799 			if (iocb->vport != vport)
6800 				continue;
6801 			lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6802 		}
6803 
6804 		pring->flag = prev_pring_flag;
6805 	}
6806 
6807 	spin_unlock_irqrestore(&phba->hbalock, flags);
6808 
6809 	/* Cancel all the IOCBs from the completions list */
6810 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6811 			      IOERR_SLI_DOWN);
6812 	return 1;
6813 }
6814 
6815 /**
6816  * lpfc_sli_hba_down - Resource cleanup function for the HBA
6817  * @phba: Pointer to HBA context object.
6818  *
6819  * This function cleans up all iocb, buffers, mailbox commands
6820  * while shutting down the HBA. This function is called with no
6821  * lock held and always returns 1.
6822  * This function does the following to cleanup driver resources:
6823  * - Free discovery resources for each virtual port
6824  * - Cleanup any pending fabric iocbs
6825  * - Iterate through the iocb txq and free each entry
6826  *   in the list.
6827  * - Free up any buffer posted to the HBA
6828  * - Free mailbox commands in the mailbox queue.
6829  **/
6830 int
6831 lpfc_sli_hba_down(struct lpfc_hba *phba)
6832 {
6833 	LIST_HEAD(completions);
6834 	struct lpfc_sli *psli = &phba->sli;
6835 	struct lpfc_sli_ring *pring;
6836 	struct lpfc_dmabuf *buf_ptr;
6837 	unsigned long flags = 0;
6838 	int i;
6839 
6840 	/* Shutdown the mailbox command sub-system */
6841 	lpfc_sli_mbox_sys_shutdown(phba);
6842 
6843 	lpfc_hba_down_prep(phba);
6844 
6845 	lpfc_fabric_abort_hba(phba);
6846 
6847 	spin_lock_irqsave(&phba->hbalock, flags);
6848 	for (i = 0; i < psli->num_rings; i++) {
6849 		pring = &psli->ring[i];
6850 		/* Only slow rings */
6851 		if (pring->ringno == LPFC_ELS_RING) {
6852 			pring->flag |= LPFC_DEFERRED_RING_EVENT;
6853 			/* Set the lpfc data pending flag */
6854 			set_bit(LPFC_DATA_READY, &phba->data_flags);
6855 		}
6856 
6857 		/*
6858 		 * Error everything on the txq since these iocbs have not been
6859 		 * given to the FW yet.
6860 		 */
6861 		list_splice_init(&pring->txq, &completions);
6862 		pring->txq_cnt = 0;
6863 
6864 	}
6865 	spin_unlock_irqrestore(&phba->hbalock, flags);
6866 
6867 	/* Cancel all the IOCBs from the completions list */
6868 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6869 			      IOERR_SLI_DOWN);
6870 
6871 	spin_lock_irqsave(&phba->hbalock, flags);
6872 	list_splice_init(&phba->elsbuf, &completions);
6873 	phba->elsbuf_cnt = 0;
6874 	phba->elsbuf_prev_cnt = 0;
6875 	spin_unlock_irqrestore(&phba->hbalock, flags);
6876 
6877 	while (!list_empty(&completions)) {
6878 		list_remove_head(&completions, buf_ptr,
6879 			struct lpfc_dmabuf, list);
6880 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6881 		kfree(buf_ptr);
6882 	}
6883 
6884 	/* Return any active mbox cmds */
6885 	del_timer_sync(&psli->mbox_tmo);
6886 
6887 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6888 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6889 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6890 
6891 	return 1;
6892 }
6893 
6894 /**
6895  * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6896  * @phba: Pointer to HBA context object.
6897  *
6898  * This function cleans up all queues, iocb, buffers, mailbox commands while
6899  * shutting down the SLI4 HBA FCoE function. This function is called with no
6900  * lock held and always returns 1.
6901  *
6902  * This function does the following to cleanup driver FCoE function resources:
6903  * - Free discovery resources for each virtual port
6904  * - Cleanup any pending fabric iocbs
6905  * - Iterate through the iocb txq and free each entry in the list.
6906  * - Free up any buffer posted to the HBA.
6907  * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6908  * - Free mailbox commands in the mailbox queue.
6909  **/
6910 int
6911 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6912 {
6913 	/* Stop the SLI4 device port */
6914 	lpfc_stop_port(phba);
6915 
6916 	/* Tear down the queues in the HBA */
6917 	lpfc_sli4_queue_unset(phba);
6918 
6919 	/* unregister default FCFI from the HBA */
6920 	lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6921 
6922 	return 1;
6923 }
6924 
6925 /**
6926  * lpfc_sli_pcimem_bcopy - SLI memory copy function
6927  * @srcp: Source memory pointer.
6928  * @destp: Destination memory pointer.
6929  * @cnt: Number of words required to be copied.
6930  *
6931  * This function is used for copying data between driver memory
6932  * and the SLI memory. This function also changes the endianness
6933  * of each word if native endianness is different from SLI
6934  * endianness. This function can be called with or without
6935  * lock.
6936  **/
6937 void
6938 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6939 {
6940 	uint32_t *src = srcp;
6941 	uint32_t *dest = destp;
6942 	uint32_t ldata;
6943 	int i;
6944 
6945 	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6946 		ldata = *src;
6947 		ldata = le32_to_cpu(ldata);
6948 		*dest = ldata;
6949 		src++;
6950 		dest++;
6951 	}
6952 }
6953 
6954 
6955 /**
6956  * lpfc_sli_bemem_bcopy - SLI memory copy function
6957  * @srcp: Source memory pointer.
6958  * @destp: Destination memory pointer.
6959  * @cnt: Number of words required to be copied.
6960  *
6961  * This function is used for copying data between a data structure
6962  * with big endian representation to local endianness.
6963  * This function can be called with or without lock.
6964  **/
6965 void
6966 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6967 {
6968 	uint32_t *src = srcp;
6969 	uint32_t *dest = destp;
6970 	uint32_t ldata;
6971 	int i;
6972 
6973 	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6974 		ldata = *src;
6975 		ldata = be32_to_cpu(ldata);
6976 		*dest = ldata;
6977 		src++;
6978 		dest++;
6979 	}
6980 }
6981 
6982 /**
6983  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6984  * @phba: Pointer to HBA context object.
6985  * @pring: Pointer to driver SLI ring object.
6986  * @mp: Pointer to driver buffer object.
6987  *
6988  * This function is called with no lock held.
6989  * It always return zero after adding the buffer to the postbufq
6990  * buffer list.
6991  **/
6992 int
6993 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6994 			 struct lpfc_dmabuf *mp)
6995 {
6996 	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6997 	   later */
6998 	spin_lock_irq(&phba->hbalock);
6999 	list_add_tail(&mp->list, &pring->postbufq);
7000 	pring->postbufq_cnt++;
7001 	spin_unlock_irq(&phba->hbalock);
7002 	return 0;
7003 }
7004 
7005 /**
7006  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
7007  * @phba: Pointer to HBA context object.
7008  *
7009  * When HBQ is enabled, buffers are searched based on tags. This function
7010  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7011  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7012  * does not conflict with tags of buffer posted for unsolicited events.
7013  * The function returns the allocated tag. The function is called with
7014  * no locks held.
7015  **/
7016 uint32_t
7017 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7018 {
7019 	spin_lock_irq(&phba->hbalock);
7020 	phba->buffer_tag_count++;
7021 	/*
7022 	 * Always set the QUE_BUFTAG_BIT to distiguish between
7023 	 * a tag assigned by HBQ.
7024 	 */
7025 	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7026 	spin_unlock_irq(&phba->hbalock);
7027 	return phba->buffer_tag_count;
7028 }
7029 
7030 /**
7031  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
7032  * @phba: Pointer to HBA context object.
7033  * @pring: Pointer to driver SLI ring object.
7034  * @tag: Buffer tag.
7035  *
7036  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7037  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7038  * iocb is posted to the response ring with the tag of the buffer.
7039  * This function searches the pring->postbufq list using the tag
7040  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7041  * iocb. If the buffer is found then lpfc_dmabuf object of the
7042  * buffer is returned to the caller else NULL is returned.
7043  * This function is called with no lock held.
7044  **/
7045 struct lpfc_dmabuf *
7046 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7047 			uint32_t tag)
7048 {
7049 	struct lpfc_dmabuf *mp, *next_mp;
7050 	struct list_head *slp = &pring->postbufq;
7051 
7052 	/* Search postbufq, from the begining, looking for a match on tag */
7053 	spin_lock_irq(&phba->hbalock);
7054 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7055 		if (mp->buffer_tag == tag) {
7056 			list_del_init(&mp->list);
7057 			pring->postbufq_cnt--;
7058 			spin_unlock_irq(&phba->hbalock);
7059 			return mp;
7060 		}
7061 	}
7062 
7063 	spin_unlock_irq(&phba->hbalock);
7064 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7065 			"0402 Cannot find virtual addr for buffer tag on "
7066 			"ring %d Data x%lx x%p x%p x%x\n",
7067 			pring->ringno, (unsigned long) tag,
7068 			slp->next, slp->prev, pring->postbufq_cnt);
7069 
7070 	return NULL;
7071 }
7072 
7073 /**
7074  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
7075  * @phba: Pointer to HBA context object.
7076  * @pring: Pointer to driver SLI ring object.
7077  * @phys: DMA address of the buffer.
7078  *
7079  * This function searches the buffer list using the dma_address
7080  * of unsolicited event to find the driver's lpfc_dmabuf object
7081  * corresponding to the dma_address. The function returns the
7082  * lpfc_dmabuf object if a buffer is found else it returns NULL.
7083  * This function is called by the ct and els unsolicited event
7084  * handlers to get the buffer associated with the unsolicited
7085  * event.
7086  *
7087  * This function is called with no lock held.
7088  **/
7089 struct lpfc_dmabuf *
7090 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7091 			 dma_addr_t phys)
7092 {
7093 	struct lpfc_dmabuf *mp, *next_mp;
7094 	struct list_head *slp = &pring->postbufq;
7095 
7096 	/* Search postbufq, from the begining, looking for a match on phys */
7097 	spin_lock_irq(&phba->hbalock);
7098 	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7099 		if (mp->phys == phys) {
7100 			list_del_init(&mp->list);
7101 			pring->postbufq_cnt--;
7102 			spin_unlock_irq(&phba->hbalock);
7103 			return mp;
7104 		}
7105 	}
7106 
7107 	spin_unlock_irq(&phba->hbalock);
7108 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7109 			"0410 Cannot find virtual addr for mapped buf on "
7110 			"ring %d Data x%llx x%p x%p x%x\n",
7111 			pring->ringno, (unsigned long long)phys,
7112 			slp->next, slp->prev, pring->postbufq_cnt);
7113 	return NULL;
7114 }
7115 
7116 /**
7117  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7118  * @phba: Pointer to HBA context object.
7119  * @cmdiocb: Pointer to driver command iocb object.
7120  * @rspiocb: Pointer to driver response iocb object.
7121  *
7122  * This function is the completion handler for the abort iocbs for
7123  * ELS commands. This function is called from the ELS ring event
7124  * handler with no lock held. This function frees memory resources
7125  * associated with the abort iocb.
7126  **/
7127 static void
7128 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7129 			struct lpfc_iocbq *rspiocb)
7130 {
7131 	IOCB_t *irsp = &rspiocb->iocb;
7132 	uint16_t abort_iotag, abort_context;
7133 	struct lpfc_iocbq *abort_iocb;
7134 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7135 
7136 	abort_iocb = NULL;
7137 
7138 	if (irsp->ulpStatus) {
7139 		abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7140 		abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7141 
7142 		spin_lock_irq(&phba->hbalock);
7143 		if (phba->sli_rev < LPFC_SLI_REV4) {
7144 			if (abort_iotag != 0 &&
7145 				abort_iotag <= phba->sli.last_iotag)
7146 				abort_iocb =
7147 					phba->sli.iocbq_lookup[abort_iotag];
7148 		} else
7149 			/* For sli4 the abort_tag is the XRI,
7150 			 * so the abort routine puts the iotag  of the iocb
7151 			 * being aborted in the context field of the abort
7152 			 * IOCB.
7153 			 */
7154 			abort_iocb = phba->sli.iocbq_lookup[abort_context];
7155 
7156 		/*
7157 		 *  If the iocb is not found in Firmware queue the iocb
7158 		 *  might have completed already. Do not free it again.
7159 		 */
7160 		if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7161 			if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7162 				spin_unlock_irq(&phba->hbalock);
7163 				lpfc_sli_release_iocbq(phba, cmdiocb);
7164 				return;
7165 			}
7166 			/* For SLI4 the ulpContext field for abort IOCB
7167 			 * holds the iotag of the IOCB being aborted so
7168 			 * the local abort_context needs to be reset to
7169 			 * match the aborted IOCBs ulpContext.
7170 			 */
7171 			if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7172 				abort_context = abort_iocb->iocb.ulpContext;
7173 		}
7174 
7175 		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7176 				"0327 Cannot abort els iocb %p "
7177 				"with tag %x context %x, abort status %x, "
7178 				"abort code %x\n",
7179 				abort_iocb, abort_iotag, abort_context,
7180 				irsp->ulpStatus, irsp->un.ulpWord[4]);
7181 		/*
7182 		 * make sure we have the right iocbq before taking it
7183 		 * off the txcmplq and try to call completion routine.
7184 		 */
7185 		if (!abort_iocb ||
7186 		    abort_iocb->iocb.ulpContext != abort_context ||
7187 		    (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7188 			spin_unlock_irq(&phba->hbalock);
7189 		else if (phba->sli_rev < LPFC_SLI_REV4) {
7190 			/*
7191 			 * leave the SLI4 aborted command on the txcmplq
7192 			 * list and the command complete WCQE's XB bit
7193 			 * will tell whether the SGL (XRI) can be released
7194 			 * immediately or to the aborted SGL list for the
7195 			 * following abort XRI from the HBA.
7196 			 */
7197 			list_del_init(&abort_iocb->list);
7198 			if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7199 				abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7200 				pring->txcmplq_cnt--;
7201 			}
7202 
7203 			/* Firmware could still be in progress of DMAing
7204 			 * payload, so don't free data buffer till after
7205 			 * a hbeat.
7206 			 */
7207 			abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7208 			abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7209 			spin_unlock_irq(&phba->hbalock);
7210 
7211 			abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7212 			abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7213 			(abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7214 		} else
7215 			spin_unlock_irq(&phba->hbalock);
7216 	}
7217 
7218 	lpfc_sli_release_iocbq(phba, cmdiocb);
7219 	return;
7220 }
7221 
7222 /**
7223  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7224  * @phba: Pointer to HBA context object.
7225  * @cmdiocb: Pointer to driver command iocb object.
7226  * @rspiocb: Pointer to driver response iocb object.
7227  *
7228  * The function is called from SLI ring event handler with no
7229  * lock held. This function is the completion handler for ELS commands
7230  * which are aborted. The function frees memory resources used for
7231  * the aborted ELS commands.
7232  **/
7233 static void
7234 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7235 		     struct lpfc_iocbq *rspiocb)
7236 {
7237 	IOCB_t *irsp = &rspiocb->iocb;
7238 
7239 	/* ELS cmd tag <ulpIoTag> completes */
7240 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7241 			"0139 Ignoring ELS cmd tag x%x completion Data: "
7242 			"x%x x%x x%x\n",
7243 			irsp->ulpIoTag, irsp->ulpStatus,
7244 			irsp->un.ulpWord[4], irsp->ulpTimeout);
7245 	if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7246 		lpfc_ct_free_iocb(phba, cmdiocb);
7247 	else
7248 		lpfc_els_free_iocb(phba, cmdiocb);
7249 	return;
7250 }
7251 
7252 /**
7253  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7254  * @phba: Pointer to HBA context object.
7255  * @pring: Pointer to driver SLI ring object.
7256  * @cmdiocb: Pointer to driver command iocb object.
7257  *
7258  * This function issues an abort iocb for the provided command
7259  * iocb. This function is called with hbalock held.
7260  * The function returns 0 when it fails due to memory allocation
7261  * failure or when the command iocb is an abort request.
7262  **/
7263 int
7264 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7265 			   struct lpfc_iocbq *cmdiocb)
7266 {
7267 	struct lpfc_vport *vport = cmdiocb->vport;
7268 	struct lpfc_iocbq *abtsiocbp;
7269 	IOCB_t *icmd = NULL;
7270 	IOCB_t *iabt = NULL;
7271 	int retval = IOCB_ERROR;
7272 
7273 	/*
7274 	 * There are certain command types we don't want to abort.  And we
7275 	 * don't want to abort commands that are already in the process of
7276 	 * being aborted.
7277 	 */
7278 	icmd = &cmdiocb->iocb;
7279 	if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7280 	    icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7281 	    (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7282 		return 0;
7283 
7284 	/* If we're unloading, don't abort iocb on the ELS ring, but change the
7285 	 * callback so that nothing happens when it finishes.
7286 	 */
7287 	if ((vport->load_flag & FC_UNLOADING) &&
7288 	    (pring->ringno == LPFC_ELS_RING)) {
7289 		if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7290 			cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7291 		else
7292 			cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7293 		goto abort_iotag_exit;
7294 	}
7295 
7296 	/* issue ABTS for this IOCB based on iotag */
7297 	abtsiocbp = __lpfc_sli_get_iocbq(phba);
7298 	if (abtsiocbp == NULL)
7299 		return 0;
7300 
7301 	/* This signals the response to set the correct status
7302 	 * before calling the completion handler
7303 	 */
7304 	cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7305 
7306 	iabt = &abtsiocbp->iocb;
7307 	iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7308 	iabt->un.acxri.abortContextTag = icmd->ulpContext;
7309 	if (phba->sli_rev == LPFC_SLI_REV4) {
7310 		iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7311 		iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7312 	}
7313 	else
7314 		iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7315 	iabt->ulpLe = 1;
7316 	iabt->ulpClass = icmd->ulpClass;
7317 
7318 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
7319 	abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7320 	if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7321 		abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7322 
7323 	if (phba->link_state >= LPFC_LINK_UP)
7324 		iabt->ulpCommand = CMD_ABORT_XRI_CN;
7325 	else
7326 		iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7327 
7328 	abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7329 
7330 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7331 			 "0339 Abort xri x%x, original iotag x%x, "
7332 			 "abort cmd iotag x%x\n",
7333 			 iabt->un.acxri.abortIoTag,
7334 			 iabt->un.acxri.abortContextTag,
7335 			 abtsiocbp->iotag);
7336 	retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7337 
7338 	if (retval)
7339 		__lpfc_sli_release_iocbq(phba, abtsiocbp);
7340 abort_iotag_exit:
7341 	/*
7342 	 * Caller to this routine should check for IOCB_ERROR
7343 	 * and handle it properly.  This routine no longer removes
7344 	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7345 	 */
7346 	return retval;
7347 }
7348 
7349 /**
7350  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7351  * @iocbq: Pointer to driver iocb object.
7352  * @vport: Pointer to driver virtual port object.
7353  * @tgt_id: SCSI ID of the target.
7354  * @lun_id: LUN ID of the scsi device.
7355  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7356  *
7357  * This function acts as an iocb filter for functions which abort or count
7358  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7359  * 0 if the filtering criteria is met for the given iocb and will return
7360  * 1 if the filtering criteria is not met.
7361  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7362  * given iocb is for the SCSI device specified by vport, tgt_id and
7363  * lun_id parameter.
7364  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7365  * given iocb is for the SCSI target specified by vport and tgt_id
7366  * parameters.
7367  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7368  * given iocb is for the SCSI host associated with the given vport.
7369  * This function is called with no locks held.
7370  **/
7371 static int
7372 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7373 			   uint16_t tgt_id, uint64_t lun_id,
7374 			   lpfc_ctx_cmd ctx_cmd)
7375 {
7376 	struct lpfc_scsi_buf *lpfc_cmd;
7377 	int rc = 1;
7378 
7379 	if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7380 		return rc;
7381 
7382 	if (iocbq->vport != vport)
7383 		return rc;
7384 
7385 	lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7386 
7387 	if (lpfc_cmd->pCmd == NULL)
7388 		return rc;
7389 
7390 	switch (ctx_cmd) {
7391 	case LPFC_CTX_LUN:
7392 		if ((lpfc_cmd->rdata->pnode) &&
7393 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7394 		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7395 			rc = 0;
7396 		break;
7397 	case LPFC_CTX_TGT:
7398 		if ((lpfc_cmd->rdata->pnode) &&
7399 		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7400 			rc = 0;
7401 		break;
7402 	case LPFC_CTX_HOST:
7403 		rc = 0;
7404 		break;
7405 	default:
7406 		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7407 			__func__, ctx_cmd);
7408 		break;
7409 	}
7410 
7411 	return rc;
7412 }
7413 
7414 /**
7415  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7416  * @vport: Pointer to virtual port.
7417  * @tgt_id: SCSI ID of the target.
7418  * @lun_id: LUN ID of the scsi device.
7419  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7420  *
7421  * This function returns number of FCP commands pending for the vport.
7422  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7423  * commands pending on the vport associated with SCSI device specified
7424  * by tgt_id and lun_id parameters.
7425  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7426  * commands pending on the vport associated with SCSI target specified
7427  * by tgt_id parameter.
7428  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7429  * commands pending on the vport.
7430  * This function returns the number of iocbs which satisfy the filter.
7431  * This function is called without any lock held.
7432  **/
7433 int
7434 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7435 		  lpfc_ctx_cmd ctx_cmd)
7436 {
7437 	struct lpfc_hba *phba = vport->phba;
7438 	struct lpfc_iocbq *iocbq;
7439 	int sum, i;
7440 
7441 	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7442 		iocbq = phba->sli.iocbq_lookup[i];
7443 
7444 		if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7445 						ctx_cmd) == 0)
7446 			sum++;
7447 	}
7448 
7449 	return sum;
7450 }
7451 
7452 /**
7453  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7454  * @phba: Pointer to HBA context object
7455  * @cmdiocb: Pointer to command iocb object.
7456  * @rspiocb: Pointer to response iocb object.
7457  *
7458  * This function is called when an aborted FCP iocb completes. This
7459  * function is called by the ring event handler with no lock held.
7460  * This function frees the iocb.
7461  **/
7462 void
7463 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7464 			struct lpfc_iocbq *rspiocb)
7465 {
7466 	lpfc_sli_release_iocbq(phba, cmdiocb);
7467 	return;
7468 }
7469 
7470 /**
7471  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7472  * @vport: Pointer to virtual port.
7473  * @pring: Pointer to driver SLI ring object.
7474  * @tgt_id: SCSI ID of the target.
7475  * @lun_id: LUN ID of the scsi device.
7476  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7477  *
7478  * This function sends an abort command for every SCSI command
7479  * associated with the given virtual port pending on the ring
7480  * filtered by lpfc_sli_validate_fcp_iocb function.
7481  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7482  * FCP iocbs associated with lun specified by tgt_id and lun_id
7483  * parameters
7484  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7485  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7486  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7487  * FCP iocbs associated with virtual port.
7488  * This function returns number of iocbs it failed to abort.
7489  * This function is called with no locks held.
7490  **/
7491 int
7492 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7493 		    uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7494 {
7495 	struct lpfc_hba *phba = vport->phba;
7496 	struct lpfc_iocbq *iocbq;
7497 	struct lpfc_iocbq *abtsiocb;
7498 	IOCB_t *cmd = NULL;
7499 	int errcnt = 0, ret_val = 0;
7500 	int i;
7501 
7502 	for (i = 1; i <= phba->sli.last_iotag; i++) {
7503 		iocbq = phba->sli.iocbq_lookup[i];
7504 
7505 		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7506 					       abort_cmd) != 0)
7507 			continue;
7508 
7509 		/* issue ABTS for this IOCB based on iotag */
7510 		abtsiocb = lpfc_sli_get_iocbq(phba);
7511 		if (abtsiocb == NULL) {
7512 			errcnt++;
7513 			continue;
7514 		}
7515 
7516 		cmd = &iocbq->iocb;
7517 		abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7518 		abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7519 		if (phba->sli_rev == LPFC_SLI_REV4)
7520 			abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7521 		else
7522 			abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7523 		abtsiocb->iocb.ulpLe = 1;
7524 		abtsiocb->iocb.ulpClass = cmd->ulpClass;
7525 		abtsiocb->vport = phba->pport;
7526 
7527 		/* ABTS WQE must go to the same WQ as the WQE to be aborted */
7528 		abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7529 		if (iocbq->iocb_flag & LPFC_IO_FCP)
7530 			abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
7531 
7532 		if (lpfc_is_link_up(phba))
7533 			abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7534 		else
7535 			abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7536 
7537 		/* Setup callback routine and issue the command. */
7538 		abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7539 		ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7540 					      abtsiocb, 0);
7541 		if (ret_val == IOCB_ERROR) {
7542 			lpfc_sli_release_iocbq(phba, abtsiocb);
7543 			errcnt++;
7544 			continue;
7545 		}
7546 	}
7547 
7548 	return errcnt;
7549 }
7550 
7551 /**
7552  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7553  * @phba: Pointer to HBA context object.
7554  * @cmdiocbq: Pointer to command iocb.
7555  * @rspiocbq: Pointer to response iocb.
7556  *
7557  * This function is the completion handler for iocbs issued using
7558  * lpfc_sli_issue_iocb_wait function. This function is called by the
7559  * ring event handler function without any lock held. This function
7560  * can be called from both worker thread context and interrupt
7561  * context. This function also can be called from other thread which
7562  * cleans up the SLI layer objects.
7563  * This function copy the contents of the response iocb to the
7564  * response iocb memory object provided by the caller of
7565  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7566  * sleeps for the iocb completion.
7567  **/
7568 static void
7569 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7570 			struct lpfc_iocbq *cmdiocbq,
7571 			struct lpfc_iocbq *rspiocbq)
7572 {
7573 	wait_queue_head_t *pdone_q;
7574 	unsigned long iflags;
7575 	struct lpfc_scsi_buf *lpfc_cmd;
7576 
7577 	spin_lock_irqsave(&phba->hbalock, iflags);
7578 	cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7579 	if (cmdiocbq->context2 && rspiocbq)
7580 		memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7581 		       &rspiocbq->iocb, sizeof(IOCB_t));
7582 
7583 	/* Set the exchange busy flag for task management commands */
7584 	if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7585 		!(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7586 		lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7587 			cur_iocbq);
7588 		lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7589 	}
7590 
7591 	pdone_q = cmdiocbq->context_un.wait_queue;
7592 	if (pdone_q)
7593 		wake_up(pdone_q);
7594 	spin_unlock_irqrestore(&phba->hbalock, iflags);
7595 	return;
7596 }
7597 
7598 /**
7599  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7600  * @phba: Pointer to HBA context object..
7601  * @piocbq: Pointer to command iocb.
7602  * @flag: Flag to test.
7603  *
7604  * This routine grabs the hbalock and then test the iocb_flag to
7605  * see if the passed in flag is set.
7606  * Returns:
7607  * 1 if flag is set.
7608  * 0 if flag is not set.
7609  **/
7610 static int
7611 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7612 		 struct lpfc_iocbq *piocbq, uint32_t flag)
7613 {
7614 	unsigned long iflags;
7615 	int ret;
7616 
7617 	spin_lock_irqsave(&phba->hbalock, iflags);
7618 	ret = piocbq->iocb_flag & flag;
7619 	spin_unlock_irqrestore(&phba->hbalock, iflags);
7620 	return ret;
7621 
7622 }
7623 
7624 /**
7625  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7626  * @phba: Pointer to HBA context object..
7627  * @pring: Pointer to sli ring.
7628  * @piocb: Pointer to command iocb.
7629  * @prspiocbq: Pointer to response iocb.
7630  * @timeout: Timeout in number of seconds.
7631  *
7632  * This function issues the iocb to firmware and waits for the
7633  * iocb to complete. If the iocb command is not
7634  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7635  * Caller should not free the iocb resources if this function
7636  * returns IOCB_TIMEDOUT.
7637  * The function waits for the iocb completion using an
7638  * non-interruptible wait.
7639  * This function will sleep while waiting for iocb completion.
7640  * So, this function should not be called from any context which
7641  * does not allow sleeping. Due to the same reason, this function
7642  * cannot be called with interrupt disabled.
7643  * This function assumes that the iocb completions occur while
7644  * this function sleep. So, this function cannot be called from
7645  * the thread which process iocb completion for this ring.
7646  * This function clears the iocb_flag of the iocb object before
7647  * issuing the iocb and the iocb completion handler sets this
7648  * flag and wakes this thread when the iocb completes.
7649  * The contents of the response iocb will be copied to prspiocbq
7650  * by the completion handler when the command completes.
7651  * This function returns IOCB_SUCCESS when success.
7652  * This function is called with no lock held.
7653  **/
7654 int
7655 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7656 			 uint32_t ring_number,
7657 			 struct lpfc_iocbq *piocb,
7658 			 struct lpfc_iocbq *prspiocbq,
7659 			 uint32_t timeout)
7660 {
7661 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7662 	long timeleft, timeout_req = 0;
7663 	int retval = IOCB_SUCCESS;
7664 	uint32_t creg_val;
7665 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7666 	/*
7667 	 * If the caller has provided a response iocbq buffer, then context2
7668 	 * is NULL or its an error.
7669 	 */
7670 	if (prspiocbq) {
7671 		if (piocb->context2)
7672 			return IOCB_ERROR;
7673 		piocb->context2 = prspiocbq;
7674 	}
7675 
7676 	piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7677 	piocb->context_un.wait_queue = &done_q;
7678 	piocb->iocb_flag &= ~LPFC_IO_WAKE;
7679 
7680 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7681 		creg_val = readl(phba->HCregaddr);
7682 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7683 		writel(creg_val, phba->HCregaddr);
7684 		readl(phba->HCregaddr); /* flush */
7685 	}
7686 
7687 	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7688 				     SLI_IOCB_RET_IOCB);
7689 	if (retval == IOCB_SUCCESS) {
7690 		timeout_req = timeout * HZ;
7691 		timeleft = wait_event_timeout(done_q,
7692 				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
7693 				timeout_req);
7694 
7695 		if (piocb->iocb_flag & LPFC_IO_WAKE) {
7696 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7697 					"0331 IOCB wake signaled\n");
7698 		} else if (timeleft == 0) {
7699 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7700 					"0338 IOCB wait timeout error - no "
7701 					"wake response Data x%x\n", timeout);
7702 			retval = IOCB_TIMEDOUT;
7703 		} else {
7704 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7705 					"0330 IOCB wake NOT set, "
7706 					"Data x%x x%lx\n",
7707 					timeout, (timeleft / jiffies));
7708 			retval = IOCB_TIMEDOUT;
7709 		}
7710 	} else if (retval == IOCB_BUSY) {
7711 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7712 			"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7713 			phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7714 		return retval;
7715 	} else {
7716 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7717 				"0332 IOCB wait issue failed, Data x%x\n",
7718 				retval);
7719 		retval = IOCB_ERROR;
7720 	}
7721 
7722 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7723 		creg_val = readl(phba->HCregaddr);
7724 		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7725 		writel(creg_val, phba->HCregaddr);
7726 		readl(phba->HCregaddr); /* flush */
7727 	}
7728 
7729 	if (prspiocbq)
7730 		piocb->context2 = NULL;
7731 
7732 	piocb->context_un.wait_queue = NULL;
7733 	piocb->iocb_cmpl = NULL;
7734 	return retval;
7735 }
7736 
7737 /**
7738  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7739  * @phba: Pointer to HBA context object.
7740  * @pmboxq: Pointer to driver mailbox object.
7741  * @timeout: Timeout in number of seconds.
7742  *
7743  * This function issues the mailbox to firmware and waits for the
7744  * mailbox command to complete. If the mailbox command is not
7745  * completed within timeout seconds, it returns MBX_TIMEOUT.
7746  * The function waits for the mailbox completion using an
7747  * interruptible wait. If the thread is woken up due to a
7748  * signal, MBX_TIMEOUT error is returned to the caller. Caller
7749  * should not free the mailbox resources, if this function returns
7750  * MBX_TIMEOUT.
7751  * This function will sleep while waiting for mailbox completion.
7752  * So, this function should not be called from any context which
7753  * does not allow sleeping. Due to the same reason, this function
7754  * cannot be called with interrupt disabled.
7755  * This function assumes that the mailbox completion occurs while
7756  * this function sleep. So, this function cannot be called from
7757  * the worker thread which processes mailbox completion.
7758  * This function is called in the context of HBA management
7759  * applications.
7760  * This function returns MBX_SUCCESS when successful.
7761  * This function is called with no lock held.
7762  **/
7763 int
7764 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7765 			 uint32_t timeout)
7766 {
7767 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7768 	int retval;
7769 	unsigned long flag;
7770 
7771 	/* The caller must leave context1 empty. */
7772 	if (pmboxq->context1)
7773 		return MBX_NOT_FINISHED;
7774 
7775 	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7776 	/* setup wake call as IOCB callback */
7777 	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7778 	/* setup context field to pass wait_queue pointer to wake function  */
7779 	pmboxq->context1 = &done_q;
7780 
7781 	/* now issue the command */
7782 	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7783 
7784 	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7785 		wait_event_interruptible_timeout(done_q,
7786 				pmboxq->mbox_flag & LPFC_MBX_WAKE,
7787 				timeout * HZ);
7788 
7789 		spin_lock_irqsave(&phba->hbalock, flag);
7790 		pmboxq->context1 = NULL;
7791 		/*
7792 		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7793 		 * else do not free the resources.
7794 		 */
7795 		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
7796 			retval = MBX_SUCCESS;
7797 			lpfc_sli4_swap_str(phba, pmboxq);
7798 		} else {
7799 			retval = MBX_TIMEOUT;
7800 			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7801 		}
7802 		spin_unlock_irqrestore(&phba->hbalock, flag);
7803 	}
7804 
7805 	return retval;
7806 }
7807 
7808 /**
7809  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7810  * @phba: Pointer to HBA context.
7811  *
7812  * This function is called to shutdown the driver's mailbox sub-system.
7813  * It first marks the mailbox sub-system is in a block state to prevent
7814  * the asynchronous mailbox command from issued off the pending mailbox
7815  * command queue. If the mailbox command sub-system shutdown is due to
7816  * HBA error conditions such as EEH or ERATT, this routine shall invoke
7817  * the mailbox sub-system flush routine to forcefully bring down the
7818  * mailbox sub-system. Otherwise, if it is due to normal condition (such
7819  * as with offline or HBA function reset), this routine will wait for the
7820  * outstanding mailbox command to complete before invoking the mailbox
7821  * sub-system flush routine to gracefully bring down mailbox sub-system.
7822  **/
7823 void
7824 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7825 {
7826 	struct lpfc_sli *psli = &phba->sli;
7827 	uint8_t actcmd = MBX_HEARTBEAT;
7828 	unsigned long timeout;
7829 
7830 	spin_lock_irq(&phba->hbalock);
7831 	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7832 	spin_unlock_irq(&phba->hbalock);
7833 
7834 	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7835 		spin_lock_irq(&phba->hbalock);
7836 		if (phba->sli.mbox_active)
7837 			actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7838 		spin_unlock_irq(&phba->hbalock);
7839 		/* Determine how long we might wait for the active mailbox
7840 		 * command to be gracefully completed by firmware.
7841 		 */
7842 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7843 					   1000) + jiffies;
7844 		while (phba->sli.mbox_active) {
7845 			/* Check active mailbox complete status every 2ms */
7846 			msleep(2);
7847 			if (time_after(jiffies, timeout))
7848 				/* Timeout, let the mailbox flush routine to
7849 				 * forcefully release active mailbox command
7850 				 */
7851 				break;
7852 		}
7853 	}
7854 	lpfc_sli_mbox_sys_flush(phba);
7855 }
7856 
7857 /**
7858  * lpfc_sli_eratt_read - read sli-3 error attention events
7859  * @phba: Pointer to HBA context.
7860  *
7861  * This function is called to read the SLI3 device error attention registers
7862  * for possible error attention events. The caller must hold the hostlock
7863  * with spin_lock_irq().
7864  *
7865  * This fucntion returns 1 when there is Error Attention in the Host Attention
7866  * Register and returns 0 otherwise.
7867  **/
7868 static int
7869 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7870 {
7871 	uint32_t ha_copy;
7872 
7873 	/* Read chip Host Attention (HA) register */
7874 	ha_copy = readl(phba->HAregaddr);
7875 	if (ha_copy & HA_ERATT) {
7876 		/* Read host status register to retrieve error event */
7877 		lpfc_sli_read_hs(phba);
7878 
7879 		/* Check if there is a deferred error condition is active */
7880 		if ((HS_FFER1 & phba->work_hs) &&
7881 		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7882 		     HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7883 			phba->hba_flag |= DEFER_ERATT;
7884 			/* Clear all interrupt enable conditions */
7885 			writel(0, phba->HCregaddr);
7886 			readl(phba->HCregaddr);
7887 		}
7888 
7889 		/* Set the driver HA work bitmap */
7890 		phba->work_ha |= HA_ERATT;
7891 		/* Indicate polling handles this ERATT */
7892 		phba->hba_flag |= HBA_ERATT_HANDLED;
7893 		return 1;
7894 	}
7895 	return 0;
7896 }
7897 
7898 /**
7899  * lpfc_sli4_eratt_read - read sli-4 error attention events
7900  * @phba: Pointer to HBA context.
7901  *
7902  * This function is called to read the SLI4 device error attention registers
7903  * for possible error attention events. The caller must hold the hostlock
7904  * with spin_lock_irq().
7905  *
7906  * This fucntion returns 1 when there is Error Attention in the Host Attention
7907  * Register and returns 0 otherwise.
7908  **/
7909 static int
7910 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7911 {
7912 	uint32_t uerr_sta_hi, uerr_sta_lo;
7913 
7914 	/* For now, use the SLI4 device internal unrecoverable error
7915 	 * registers for error attention. This can be changed later.
7916 	 */
7917 	uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7918 	uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7919 	if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7920 	    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7921 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7922 				"1423 HBA Unrecoverable error: "
7923 				"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7924 				"ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7925 				uerr_sta_lo, uerr_sta_hi,
7926 				phba->sli4_hba.ue_mask_lo,
7927 				phba->sli4_hba.ue_mask_hi);
7928 		phba->work_status[0] = uerr_sta_lo;
7929 		phba->work_status[1] = uerr_sta_hi;
7930 		/* Set the driver HA work bitmap */
7931 		phba->work_ha |= HA_ERATT;
7932 		/* Indicate polling handles this ERATT */
7933 		phba->hba_flag |= HBA_ERATT_HANDLED;
7934 		return 1;
7935 	}
7936 	return 0;
7937 }
7938 
7939 /**
7940  * lpfc_sli_check_eratt - check error attention events
7941  * @phba: Pointer to HBA context.
7942  *
7943  * This function is called from timer soft interrupt context to check HBA's
7944  * error attention register bit for error attention events.
7945  *
7946  * This fucntion returns 1 when there is Error Attention in the Host Attention
7947  * Register and returns 0 otherwise.
7948  **/
7949 int
7950 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7951 {
7952 	uint32_t ha_copy;
7953 
7954 	/* If somebody is waiting to handle an eratt, don't process it
7955 	 * here. The brdkill function will do this.
7956 	 */
7957 	if (phba->link_flag & LS_IGNORE_ERATT)
7958 		return 0;
7959 
7960 	/* Check if interrupt handler handles this ERATT */
7961 	spin_lock_irq(&phba->hbalock);
7962 	if (phba->hba_flag & HBA_ERATT_HANDLED) {
7963 		/* Interrupt handler has handled ERATT */
7964 		spin_unlock_irq(&phba->hbalock);
7965 		return 0;
7966 	}
7967 
7968 	/*
7969 	 * If there is deferred error attention, do not check for error
7970 	 * attention
7971 	 */
7972 	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7973 		spin_unlock_irq(&phba->hbalock);
7974 		return 0;
7975 	}
7976 
7977 	/* If PCI channel is offline, don't process it */
7978 	if (unlikely(pci_channel_offline(phba->pcidev))) {
7979 		spin_unlock_irq(&phba->hbalock);
7980 		return 0;
7981 	}
7982 
7983 	switch (phba->sli_rev) {
7984 	case LPFC_SLI_REV2:
7985 	case LPFC_SLI_REV3:
7986 		/* Read chip Host Attention (HA) register */
7987 		ha_copy = lpfc_sli_eratt_read(phba);
7988 		break;
7989 	case LPFC_SLI_REV4:
7990 		/* Read devcie Uncoverable Error (UERR) registers */
7991 		ha_copy = lpfc_sli4_eratt_read(phba);
7992 		break;
7993 	default:
7994 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7995 				"0299 Invalid SLI revision (%d)\n",
7996 				phba->sli_rev);
7997 		ha_copy = 0;
7998 		break;
7999 	}
8000 	spin_unlock_irq(&phba->hbalock);
8001 
8002 	return ha_copy;
8003 }
8004 
8005 /**
8006  * lpfc_intr_state_check - Check device state for interrupt handling
8007  * @phba: Pointer to HBA context.
8008  *
8009  * This inline routine checks whether a device or its PCI slot is in a state
8010  * that the interrupt should be handled.
8011  *
8012  * This function returns 0 if the device or the PCI slot is in a state that
8013  * interrupt should be handled, otherwise -EIO.
8014  */
8015 static inline int
8016 lpfc_intr_state_check(struct lpfc_hba *phba)
8017 {
8018 	/* If the pci channel is offline, ignore all the interrupts */
8019 	if (unlikely(pci_channel_offline(phba->pcidev)))
8020 		return -EIO;
8021 
8022 	/* Update device level interrupt statistics */
8023 	phba->sli.slistat.sli_intr++;
8024 
8025 	/* Ignore all interrupts during initialization. */
8026 	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8027 		return -EIO;
8028 
8029 	return 0;
8030 }
8031 
8032 /**
8033  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
8034  * @irq: Interrupt number.
8035  * @dev_id: The device context pointer.
8036  *
8037  * This function is directly called from the PCI layer as an interrupt
8038  * service routine when device with SLI-3 interface spec is enabled with
8039  * MSI-X multi-message interrupt mode and there are slow-path events in
8040  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8041  * interrupt mode, this function is called as part of the device-level
8042  * interrupt handler. When the PCI slot is in error recovery or the HBA
8043  * is undergoing initialization, the interrupt handler will not process
8044  * the interrupt. The link attention and ELS ring attention events are
8045  * handled by the worker thread. The interrupt handler signals the worker
8046  * thread and returns for these events. This function is called without
8047  * any lock held. It gets the hbalock to access and update SLI data
8048  * structures.
8049  *
8050  * This function returns IRQ_HANDLED when interrupt is handled else it
8051  * returns IRQ_NONE.
8052  **/
8053 irqreturn_t
8054 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
8055 {
8056 	struct lpfc_hba  *phba;
8057 	uint32_t ha_copy, hc_copy;
8058 	uint32_t work_ha_copy;
8059 	unsigned long status;
8060 	unsigned long iflag;
8061 	uint32_t control;
8062 
8063 	MAILBOX_t *mbox, *pmbox;
8064 	struct lpfc_vport *vport;
8065 	struct lpfc_nodelist *ndlp;
8066 	struct lpfc_dmabuf *mp;
8067 	LPFC_MBOXQ_t *pmb;
8068 	int rc;
8069 
8070 	/*
8071 	 * Get the driver's phba structure from the dev_id and
8072 	 * assume the HBA is not interrupting.
8073 	 */
8074 	phba = (struct lpfc_hba *)dev_id;
8075 
8076 	if (unlikely(!phba))
8077 		return IRQ_NONE;
8078 
8079 	/*
8080 	 * Stuff needs to be attented to when this function is invoked as an
8081 	 * individual interrupt handler in MSI-X multi-message interrupt mode
8082 	 */
8083 	if (phba->intr_type == MSIX) {
8084 		/* Check device state for handling interrupt */
8085 		if (lpfc_intr_state_check(phba))
8086 			return IRQ_NONE;
8087 		/* Need to read HA REG for slow-path events */
8088 		spin_lock_irqsave(&phba->hbalock, iflag);
8089 		ha_copy = readl(phba->HAregaddr);
8090 		/* If somebody is waiting to handle an eratt don't process it
8091 		 * here. The brdkill function will do this.
8092 		 */
8093 		if (phba->link_flag & LS_IGNORE_ERATT)
8094 			ha_copy &= ~HA_ERATT;
8095 		/* Check the need for handling ERATT in interrupt handler */
8096 		if (ha_copy & HA_ERATT) {
8097 			if (phba->hba_flag & HBA_ERATT_HANDLED)
8098 				/* ERATT polling has handled ERATT */
8099 				ha_copy &= ~HA_ERATT;
8100 			else
8101 				/* Indicate interrupt handler handles ERATT */
8102 				phba->hba_flag |= HBA_ERATT_HANDLED;
8103 		}
8104 
8105 		/*
8106 		 * If there is deferred error attention, do not check for any
8107 		 * interrupt.
8108 		 */
8109 		if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8110 			spin_unlock_irqrestore(&phba->hbalock, iflag);
8111 			return IRQ_NONE;
8112 		}
8113 
8114 		/* Clear up only attention source related to slow-path */
8115 		hc_copy = readl(phba->HCregaddr);
8116 		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8117 			HC_LAINT_ENA | HC_ERINT_ENA),
8118 			phba->HCregaddr);
8119 		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8120 			phba->HAregaddr);
8121 		writel(hc_copy, phba->HCregaddr);
8122 		readl(phba->HAregaddr); /* flush */
8123 		spin_unlock_irqrestore(&phba->hbalock, iflag);
8124 	} else
8125 		ha_copy = phba->ha_copy;
8126 
8127 	work_ha_copy = ha_copy & phba->work_ha_mask;
8128 
8129 	if (work_ha_copy) {
8130 		if (work_ha_copy & HA_LATT) {
8131 			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8132 				/*
8133 				 * Turn off Link Attention interrupts
8134 				 * until CLEAR_LA done
8135 				 */
8136 				spin_lock_irqsave(&phba->hbalock, iflag);
8137 				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8138 				control = readl(phba->HCregaddr);
8139 				control &= ~HC_LAINT_ENA;
8140 				writel(control, phba->HCregaddr);
8141 				readl(phba->HCregaddr); /* flush */
8142 				spin_unlock_irqrestore(&phba->hbalock, iflag);
8143 			}
8144 			else
8145 				work_ha_copy &= ~HA_LATT;
8146 		}
8147 
8148 		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8149 			/*
8150 			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8151 			 * the only slow ring.
8152 			 */
8153 			status = (work_ha_copy &
8154 				(HA_RXMASK  << (4*LPFC_ELS_RING)));
8155 			status >>= (4*LPFC_ELS_RING);
8156 			if (status & HA_RXMASK) {
8157 				spin_lock_irqsave(&phba->hbalock, iflag);
8158 				control = readl(phba->HCregaddr);
8159 
8160 				lpfc_debugfs_slow_ring_trc(phba,
8161 				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8162 				control, status,
8163 				(uint32_t)phba->sli.slistat.sli_intr);
8164 
8165 				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8166 					lpfc_debugfs_slow_ring_trc(phba,
8167 						"ISR Disable ring:"
8168 						"pwork:x%x hawork:x%x wait:x%x",
8169 						phba->work_ha, work_ha_copy,
8170 						(uint32_t)((unsigned long)
8171 						&phba->work_waitq));
8172 
8173 					control &=
8174 					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
8175 					writel(control, phba->HCregaddr);
8176 					readl(phba->HCregaddr); /* flush */
8177 				}
8178 				else {
8179 					lpfc_debugfs_slow_ring_trc(phba,
8180 						"ISR slow ring:   pwork:"
8181 						"x%x hawork:x%x wait:x%x",
8182 						phba->work_ha, work_ha_copy,
8183 						(uint32_t)((unsigned long)
8184 						&phba->work_waitq));
8185 				}
8186 				spin_unlock_irqrestore(&phba->hbalock, iflag);
8187 			}
8188 		}
8189 		spin_lock_irqsave(&phba->hbalock, iflag);
8190 		if (work_ha_copy & HA_ERATT) {
8191 			lpfc_sli_read_hs(phba);
8192 			/*
8193 			 * Check if there is a deferred error condition
8194 			 * is active
8195 			 */
8196 			if ((HS_FFER1 & phba->work_hs) &&
8197 				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8198 				HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8199 				phba->hba_flag |= DEFER_ERATT;
8200 				/* Clear all interrupt enable conditions */
8201 				writel(0, phba->HCregaddr);
8202 				readl(phba->HCregaddr);
8203 			}
8204 		}
8205 
8206 		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8207 			pmb = phba->sli.mbox_active;
8208 			pmbox = &pmb->u.mb;
8209 			mbox = phba->mbox;
8210 			vport = pmb->vport;
8211 
8212 			/* First check out the status word */
8213 			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8214 			if (pmbox->mbxOwner != OWN_HOST) {
8215 				spin_unlock_irqrestore(&phba->hbalock, iflag);
8216 				/*
8217 				 * Stray Mailbox Interrupt, mbxCommand <cmd>
8218 				 * mbxStatus <status>
8219 				 */
8220 				lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8221 						LOG_SLI,
8222 						"(%d):0304 Stray Mailbox "
8223 						"Interrupt mbxCommand x%x "
8224 						"mbxStatus x%x\n",
8225 						(vport ? vport->vpi : 0),
8226 						pmbox->mbxCommand,
8227 						pmbox->mbxStatus);
8228 				/* clear mailbox attention bit */
8229 				work_ha_copy &= ~HA_MBATT;
8230 			} else {
8231 				phba->sli.mbox_active = NULL;
8232 				spin_unlock_irqrestore(&phba->hbalock, iflag);
8233 				phba->last_completion_time = jiffies;
8234 				del_timer(&phba->sli.mbox_tmo);
8235 				if (pmb->mbox_cmpl) {
8236 					lpfc_sli_pcimem_bcopy(mbox, pmbox,
8237 							MAILBOX_CMD_SIZE);
8238 					if (pmb->out_ext_byte_len &&
8239 						pmb->context2)
8240 						lpfc_sli_pcimem_bcopy(
8241 						phba->mbox_ext,
8242 						pmb->context2,
8243 						pmb->out_ext_byte_len);
8244 				}
8245 				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8246 					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8247 
8248 					lpfc_debugfs_disc_trc(vport,
8249 						LPFC_DISC_TRC_MBOX_VPORT,
8250 						"MBOX dflt rpi: : "
8251 						"status:x%x rpi:x%x",
8252 						(uint32_t)pmbox->mbxStatus,
8253 						pmbox->un.varWords[0], 0);
8254 
8255 					if (!pmbox->mbxStatus) {
8256 						mp = (struct lpfc_dmabuf *)
8257 							(pmb->context1);
8258 						ndlp = (struct lpfc_nodelist *)
8259 							pmb->context2;
8260 
8261 						/* Reg_LOGIN of dflt RPI was
8262 						 * successful. new lets get
8263 						 * rid of the RPI using the
8264 						 * same mbox buffer.
8265 						 */
8266 						lpfc_unreg_login(phba,
8267 							vport->vpi,
8268 							pmbox->un.varWords[0],
8269 							pmb);
8270 						pmb->mbox_cmpl =
8271 							lpfc_mbx_cmpl_dflt_rpi;
8272 						pmb->context1 = mp;
8273 						pmb->context2 = ndlp;
8274 						pmb->vport = vport;
8275 						rc = lpfc_sli_issue_mbox(phba,
8276 								pmb,
8277 								MBX_NOWAIT);
8278 						if (rc != MBX_BUSY)
8279 							lpfc_printf_log(phba,
8280 							KERN_ERR,
8281 							LOG_MBOX | LOG_SLI,
8282 							"0350 rc should have"
8283 							"been MBX_BUSY\n");
8284 						if (rc != MBX_NOT_FINISHED)
8285 							goto send_current_mbox;
8286 					}
8287 				}
8288 				spin_lock_irqsave(
8289 						&phba->pport->work_port_lock,
8290 						iflag);
8291 				phba->pport->work_port_events &=
8292 					~WORKER_MBOX_TMO;
8293 				spin_unlock_irqrestore(
8294 						&phba->pport->work_port_lock,
8295 						iflag);
8296 				lpfc_mbox_cmpl_put(phba, pmb);
8297 			}
8298 		} else
8299 			spin_unlock_irqrestore(&phba->hbalock, iflag);
8300 
8301 		if ((work_ha_copy & HA_MBATT) &&
8302 		    (phba->sli.mbox_active == NULL)) {
8303 send_current_mbox:
8304 			/* Process next mailbox command if there is one */
8305 			do {
8306 				rc = lpfc_sli_issue_mbox(phba, NULL,
8307 							 MBX_NOWAIT);
8308 			} while (rc == MBX_NOT_FINISHED);
8309 			if (rc != MBX_SUCCESS)
8310 				lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8311 						LOG_SLI, "0349 rc should be "
8312 						"MBX_SUCCESS\n");
8313 		}
8314 
8315 		spin_lock_irqsave(&phba->hbalock, iflag);
8316 		phba->work_ha |= work_ha_copy;
8317 		spin_unlock_irqrestore(&phba->hbalock, iflag);
8318 		lpfc_worker_wake_up(phba);
8319 	}
8320 	return IRQ_HANDLED;
8321 
8322 } /* lpfc_sli_sp_intr_handler */
8323 
8324 /**
8325  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8326  * @irq: Interrupt number.
8327  * @dev_id: The device context pointer.
8328  *
8329  * This function is directly called from the PCI layer as an interrupt
8330  * service routine when device with SLI-3 interface spec is enabled with
8331  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8332  * ring event in the HBA. However, when the device is enabled with either
8333  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8334  * device-level interrupt handler. When the PCI slot is in error recovery
8335  * or the HBA is undergoing initialization, the interrupt handler will not
8336  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8337  * the intrrupt context. This function is called without any lock held.
8338  * It gets the hbalock to access and update SLI data structures.
8339  *
8340  * This function returns IRQ_HANDLED when interrupt is handled else it
8341  * returns IRQ_NONE.
8342  **/
8343 irqreturn_t
8344 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8345 {
8346 	struct lpfc_hba  *phba;
8347 	uint32_t ha_copy;
8348 	unsigned long status;
8349 	unsigned long iflag;
8350 
8351 	/* Get the driver's phba structure from the dev_id and
8352 	 * assume the HBA is not interrupting.
8353 	 */
8354 	phba = (struct lpfc_hba *) dev_id;
8355 
8356 	if (unlikely(!phba))
8357 		return IRQ_NONE;
8358 
8359 	/*
8360 	 * Stuff needs to be attented to when this function is invoked as an
8361 	 * individual interrupt handler in MSI-X multi-message interrupt mode
8362 	 */
8363 	if (phba->intr_type == MSIX) {
8364 		/* Check device state for handling interrupt */
8365 		if (lpfc_intr_state_check(phba))
8366 			return IRQ_NONE;
8367 		/* Need to read HA REG for FCP ring and other ring events */
8368 		ha_copy = readl(phba->HAregaddr);
8369 		/* Clear up only attention source related to fast-path */
8370 		spin_lock_irqsave(&phba->hbalock, iflag);
8371 		/*
8372 		 * If there is deferred error attention, do not check for
8373 		 * any interrupt.
8374 		 */
8375 		if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8376 			spin_unlock_irqrestore(&phba->hbalock, iflag);
8377 			return IRQ_NONE;
8378 		}
8379 		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8380 			phba->HAregaddr);
8381 		readl(phba->HAregaddr); /* flush */
8382 		spin_unlock_irqrestore(&phba->hbalock, iflag);
8383 	} else
8384 		ha_copy = phba->ha_copy;
8385 
8386 	/*
8387 	 * Process all events on FCP ring. Take the optimized path for FCP IO.
8388 	 */
8389 	ha_copy &= ~(phba->work_ha_mask);
8390 
8391 	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8392 	status >>= (4*LPFC_FCP_RING);
8393 	if (status & HA_RXMASK)
8394 		lpfc_sli_handle_fast_ring_event(phba,
8395 						&phba->sli.ring[LPFC_FCP_RING],
8396 						status);
8397 
8398 	if (phba->cfg_multi_ring_support == 2) {
8399 		/*
8400 		 * Process all events on extra ring. Take the optimized path
8401 		 * for extra ring IO.
8402 		 */
8403 		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8404 		status >>= (4*LPFC_EXTRA_RING);
8405 		if (status & HA_RXMASK) {
8406 			lpfc_sli_handle_fast_ring_event(phba,
8407 					&phba->sli.ring[LPFC_EXTRA_RING],
8408 					status);
8409 		}
8410 	}
8411 	return IRQ_HANDLED;
8412 }  /* lpfc_sli_fp_intr_handler */
8413 
8414 /**
8415  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8416  * @irq: Interrupt number.
8417  * @dev_id: The device context pointer.
8418  *
8419  * This function is the HBA device-level interrupt handler to device with
8420  * SLI-3 interface spec, called from the PCI layer when either MSI or
8421  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8422  * requires driver attention. This function invokes the slow-path interrupt
8423  * attention handling function and fast-path interrupt attention handling
8424  * function in turn to process the relevant HBA attention events. This
8425  * function is called without any lock held. It gets the hbalock to access
8426  * and update SLI data structures.
8427  *
8428  * This function returns IRQ_HANDLED when interrupt is handled, else it
8429  * returns IRQ_NONE.
8430  **/
8431 irqreturn_t
8432 lpfc_sli_intr_handler(int irq, void *dev_id)
8433 {
8434 	struct lpfc_hba  *phba;
8435 	irqreturn_t sp_irq_rc, fp_irq_rc;
8436 	unsigned long status1, status2;
8437 	uint32_t hc_copy;
8438 
8439 	/*
8440 	 * Get the driver's phba structure from the dev_id and
8441 	 * assume the HBA is not interrupting.
8442 	 */
8443 	phba = (struct lpfc_hba *) dev_id;
8444 
8445 	if (unlikely(!phba))
8446 		return IRQ_NONE;
8447 
8448 	/* Check device state for handling interrupt */
8449 	if (lpfc_intr_state_check(phba))
8450 		return IRQ_NONE;
8451 
8452 	spin_lock(&phba->hbalock);
8453 	phba->ha_copy = readl(phba->HAregaddr);
8454 	if (unlikely(!phba->ha_copy)) {
8455 		spin_unlock(&phba->hbalock);
8456 		return IRQ_NONE;
8457 	} else if (phba->ha_copy & HA_ERATT) {
8458 		if (phba->hba_flag & HBA_ERATT_HANDLED)
8459 			/* ERATT polling has handled ERATT */
8460 			phba->ha_copy &= ~HA_ERATT;
8461 		else
8462 			/* Indicate interrupt handler handles ERATT */
8463 			phba->hba_flag |= HBA_ERATT_HANDLED;
8464 	}
8465 
8466 	/*
8467 	 * If there is deferred error attention, do not check for any interrupt.
8468 	 */
8469 	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8470 		spin_unlock_irq(&phba->hbalock);
8471 		return IRQ_NONE;
8472 	}
8473 
8474 	/* Clear attention sources except link and error attentions */
8475 	hc_copy = readl(phba->HCregaddr);
8476 	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8477 		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8478 		phba->HCregaddr);
8479 	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8480 	writel(hc_copy, phba->HCregaddr);
8481 	readl(phba->HAregaddr); /* flush */
8482 	spin_unlock(&phba->hbalock);
8483 
8484 	/*
8485 	 * Invokes slow-path host attention interrupt handling as appropriate.
8486 	 */
8487 
8488 	/* status of events with mailbox and link attention */
8489 	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8490 
8491 	/* status of events with ELS ring */
8492 	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
8493 	status2 >>= (4*LPFC_ELS_RING);
8494 
8495 	if (status1 || (status2 & HA_RXMASK))
8496 		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8497 	else
8498 		sp_irq_rc = IRQ_NONE;
8499 
8500 	/*
8501 	 * Invoke fast-path host attention interrupt handling as appropriate.
8502 	 */
8503 
8504 	/* status of events with FCP ring */
8505 	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8506 	status1 >>= (4*LPFC_FCP_RING);
8507 
8508 	/* status of events with extra ring */
8509 	if (phba->cfg_multi_ring_support == 2) {
8510 		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8511 		status2 >>= (4*LPFC_EXTRA_RING);
8512 	} else
8513 		status2 = 0;
8514 
8515 	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8516 		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8517 	else
8518 		fp_irq_rc = IRQ_NONE;
8519 
8520 	/* Return device-level interrupt handling status */
8521 	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8522 }  /* lpfc_sli_intr_handler */
8523 
8524 /**
8525  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8526  * @phba: pointer to lpfc hba data structure.
8527  *
8528  * This routine is invoked by the worker thread to process all the pending
8529  * SLI4 FCP abort XRI events.
8530  **/
8531 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8532 {
8533 	struct lpfc_cq_event *cq_event;
8534 
8535 	/* First, declare the fcp xri abort event has been handled */
8536 	spin_lock_irq(&phba->hbalock);
8537 	phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8538 	spin_unlock_irq(&phba->hbalock);
8539 	/* Now, handle all the fcp xri abort events */
8540 	while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8541 		/* Get the first event from the head of the event queue */
8542 		spin_lock_irq(&phba->hbalock);
8543 		list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8544 				 cq_event, struct lpfc_cq_event, list);
8545 		spin_unlock_irq(&phba->hbalock);
8546 		/* Notify aborted XRI for FCP work queue */
8547 		lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8548 		/* Free the event processed back to the free pool */
8549 		lpfc_sli4_cq_event_release(phba, cq_event);
8550 	}
8551 }
8552 
8553 /**
8554  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8555  * @phba: pointer to lpfc hba data structure.
8556  *
8557  * This routine is invoked by the worker thread to process all the pending
8558  * SLI4 els abort xri events.
8559  **/
8560 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8561 {
8562 	struct lpfc_cq_event *cq_event;
8563 
8564 	/* First, declare the els xri abort event has been handled */
8565 	spin_lock_irq(&phba->hbalock);
8566 	phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8567 	spin_unlock_irq(&phba->hbalock);
8568 	/* Now, handle all the els xri abort events */
8569 	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8570 		/* Get the first event from the head of the event queue */
8571 		spin_lock_irq(&phba->hbalock);
8572 		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8573 				 cq_event, struct lpfc_cq_event, list);
8574 		spin_unlock_irq(&phba->hbalock);
8575 		/* Notify aborted XRI for ELS work queue */
8576 		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8577 		/* Free the event processed back to the free pool */
8578 		lpfc_sli4_cq_event_release(phba, cq_event);
8579 	}
8580 }
8581 
8582 /**
8583  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8584  * @phba: pointer to lpfc hba data structure
8585  * @pIocbIn: pointer to the rspiocbq
8586  * @pIocbOut: pointer to the cmdiocbq
8587  * @wcqe: pointer to the complete wcqe
8588  *
8589  * This routine transfers the fields of a command iocbq to a response iocbq
8590  * by copying all the IOCB fields from command iocbq and transferring the
8591  * completion status information from the complete wcqe.
8592  **/
8593 static void
8594 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8595 			      struct lpfc_iocbq *pIocbIn,
8596 			      struct lpfc_iocbq *pIocbOut,
8597 			      struct lpfc_wcqe_complete *wcqe)
8598 {
8599 	unsigned long iflags;
8600 	size_t offset = offsetof(struct lpfc_iocbq, iocb);
8601 
8602 	memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8603 	       sizeof(struct lpfc_iocbq) - offset);
8604 	/* Map WCQE parameters into irspiocb parameters */
8605 	pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8606 	if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8607 		if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8608 			pIocbIn->iocb.un.fcpi.fcpi_parm =
8609 					pIocbOut->iocb.un.fcpi.fcpi_parm -
8610 					wcqe->total_data_placed;
8611 		else
8612 			pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8613 	else {
8614 		pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8615 		pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8616 	}
8617 
8618 	/* Pick up HBA exchange busy condition */
8619 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8620 		spin_lock_irqsave(&phba->hbalock, iflags);
8621 		pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8622 		spin_unlock_irqrestore(&phba->hbalock, iflags);
8623 	}
8624 }
8625 
8626 /**
8627  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8628  * @phba: Pointer to HBA context object.
8629  * @wcqe: Pointer to work-queue completion queue entry.
8630  *
8631  * This routine handles an ELS work-queue completion event and construct
8632  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8633  * discovery engine to handle.
8634  *
8635  * Return: Pointer to the receive IOCBQ, NULL otherwise.
8636  **/
8637 static struct lpfc_iocbq *
8638 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8639 			       struct lpfc_iocbq *irspiocbq)
8640 {
8641 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8642 	struct lpfc_iocbq *cmdiocbq;
8643 	struct lpfc_wcqe_complete *wcqe;
8644 	unsigned long iflags;
8645 
8646 	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8647 	spin_lock_irqsave(&phba->hbalock, iflags);
8648 	pring->stats.iocb_event++;
8649 	/* Look up the ELS command IOCB and create pseudo response IOCB */
8650 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8651 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
8652 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8653 
8654 	if (unlikely(!cmdiocbq)) {
8655 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8656 				"0386 ELS complete with no corresponding "
8657 				"cmdiocb: iotag (%d)\n",
8658 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
8659 		lpfc_sli_release_iocbq(phba, irspiocbq);
8660 		return NULL;
8661 	}
8662 
8663 	/* Fake the irspiocbq and copy necessary response information */
8664 	lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
8665 
8666 	return irspiocbq;
8667 }
8668 
8669 /**
8670  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8671  * @phba: Pointer to HBA context object.
8672  * @cqe: Pointer to mailbox completion queue entry.
8673  *
8674  * This routine process a mailbox completion queue entry with asynchrous
8675  * event.
8676  *
8677  * Return: true if work posted to worker thread, otherwise false.
8678  **/
8679 static bool
8680 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8681 {
8682 	struct lpfc_cq_event *cq_event;
8683 	unsigned long iflags;
8684 
8685 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8686 			"0392 Async Event: word0:x%x, word1:x%x, "
8687 			"word2:x%x, word3:x%x\n", mcqe->word0,
8688 			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8689 
8690 	/* Allocate a new internal CQ_EVENT entry */
8691 	cq_event = lpfc_sli4_cq_event_alloc(phba);
8692 	if (!cq_event) {
8693 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8694 				"0394 Failed to allocate CQ_EVENT entry\n");
8695 		return false;
8696 	}
8697 
8698 	/* Move the CQE into an asynchronous event entry */
8699 	memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8700 	spin_lock_irqsave(&phba->hbalock, iflags);
8701 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8702 	/* Set the async event flag */
8703 	phba->hba_flag |= ASYNC_EVENT;
8704 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8705 
8706 	return true;
8707 }
8708 
8709 /**
8710  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8711  * @phba: Pointer to HBA context object.
8712  * @cqe: Pointer to mailbox completion queue entry.
8713  *
8714  * This routine process a mailbox completion queue entry with mailbox
8715  * completion event.
8716  *
8717  * Return: true if work posted to worker thread, otherwise false.
8718  **/
8719 static bool
8720 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8721 {
8722 	uint32_t mcqe_status;
8723 	MAILBOX_t *mbox, *pmbox;
8724 	struct lpfc_mqe *mqe;
8725 	struct lpfc_vport *vport;
8726 	struct lpfc_nodelist *ndlp;
8727 	struct lpfc_dmabuf *mp;
8728 	unsigned long iflags;
8729 	LPFC_MBOXQ_t *pmb;
8730 	bool workposted = false;
8731 	int rc;
8732 
8733 	/* If not a mailbox complete MCQE, out by checking mailbox consume */
8734 	if (!bf_get(lpfc_trailer_completed, mcqe))
8735 		goto out_no_mqe_complete;
8736 
8737 	/* Get the reference to the active mbox command */
8738 	spin_lock_irqsave(&phba->hbalock, iflags);
8739 	pmb = phba->sli.mbox_active;
8740 	if (unlikely(!pmb)) {
8741 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8742 				"1832 No pending MBOX command to handle\n");
8743 		spin_unlock_irqrestore(&phba->hbalock, iflags);
8744 		goto out_no_mqe_complete;
8745 	}
8746 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8747 	mqe = &pmb->u.mqe;
8748 	pmbox = (MAILBOX_t *)&pmb->u.mqe;
8749 	mbox = phba->mbox;
8750 	vport = pmb->vport;
8751 
8752 	/* Reset heartbeat timer */
8753 	phba->last_completion_time = jiffies;
8754 	del_timer(&phba->sli.mbox_tmo);
8755 
8756 	/* Move mbox data to caller's mailbox region, do endian swapping */
8757 	if (pmb->mbox_cmpl && mbox)
8758 		lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8759 	/* Set the mailbox status with SLI4 range 0x4000 */
8760 	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8761 	if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8762 		bf_set(lpfc_mqe_status, mqe,
8763 		       (LPFC_MBX_ERROR_RANGE | mcqe_status));
8764 
8765 	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8766 		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8767 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8768 				      "MBOX dflt rpi: status:x%x rpi:x%x",
8769 				      mcqe_status,
8770 				      pmbox->un.varWords[0], 0);
8771 		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8772 			mp = (struct lpfc_dmabuf *)(pmb->context1);
8773 			ndlp = (struct lpfc_nodelist *)pmb->context2;
8774 			/* Reg_LOGIN of dflt RPI was successful. Now lets get
8775 			 * RID of the PPI using the same mbox buffer.
8776 			 */
8777 			lpfc_unreg_login(phba, vport->vpi,
8778 					 pmbox->un.varWords[0], pmb);
8779 			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8780 			pmb->context1 = mp;
8781 			pmb->context2 = ndlp;
8782 			pmb->vport = vport;
8783 			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8784 			if (rc != MBX_BUSY)
8785 				lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8786 						LOG_SLI, "0385 rc should "
8787 						"have been MBX_BUSY\n");
8788 			if (rc != MBX_NOT_FINISHED)
8789 				goto send_current_mbox;
8790 		}
8791 	}
8792 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8793 	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8794 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8795 
8796 	/* There is mailbox completion work to do */
8797 	spin_lock_irqsave(&phba->hbalock, iflags);
8798 	__lpfc_mbox_cmpl_put(phba, pmb);
8799 	phba->work_ha |= HA_MBATT;
8800 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8801 	workposted = true;
8802 
8803 send_current_mbox:
8804 	spin_lock_irqsave(&phba->hbalock, iflags);
8805 	/* Release the mailbox command posting token */
8806 	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8807 	/* Setting active mailbox pointer need to be in sync to flag clear */
8808 	phba->sli.mbox_active = NULL;
8809 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8810 	/* Wake up worker thread to post the next pending mailbox command */
8811 	lpfc_worker_wake_up(phba);
8812 out_no_mqe_complete:
8813 	if (bf_get(lpfc_trailer_consumed, mcqe))
8814 		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8815 	return workposted;
8816 }
8817 
8818 /**
8819  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8820  * @phba: Pointer to HBA context object.
8821  * @cqe: Pointer to mailbox completion queue entry.
8822  *
8823  * This routine process a mailbox completion queue entry, it invokes the
8824  * proper mailbox complete handling or asynchrous event handling routine
8825  * according to the MCQE's async bit.
8826  *
8827  * Return: true if work posted to worker thread, otherwise false.
8828  **/
8829 static bool
8830 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8831 {
8832 	struct lpfc_mcqe mcqe;
8833 	bool workposted;
8834 
8835 	/* Copy the mailbox MCQE and convert endian order as needed */
8836 	lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8837 
8838 	/* Invoke the proper event handling routine */
8839 	if (!bf_get(lpfc_trailer_async, &mcqe))
8840 		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8841 	else
8842 		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8843 	return workposted;
8844 }
8845 
8846 /**
8847  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8848  * @phba: Pointer to HBA context object.
8849  * @wcqe: Pointer to work-queue completion queue entry.
8850  *
8851  * This routine handles an ELS work-queue completion event.
8852  *
8853  * Return: true if work posted to worker thread, otherwise false.
8854  **/
8855 static bool
8856 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8857 			     struct lpfc_wcqe_complete *wcqe)
8858 {
8859 	struct lpfc_iocbq *irspiocbq;
8860 	unsigned long iflags;
8861 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8862 
8863 	/* Get an irspiocbq for later ELS response processing use */
8864 	irspiocbq = lpfc_sli_get_iocbq(phba);
8865 	if (!irspiocbq) {
8866 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8867 			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
8868 			"fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
8869 			pring->txq_cnt, phba->iocb_cnt,
8870 			phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
8871 			phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
8872 		return false;
8873 	}
8874 
8875 	/* Save off the slow-path queue event for work thread to process */
8876 	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
8877 	spin_lock_irqsave(&phba->hbalock, iflags);
8878 	list_add_tail(&irspiocbq->cq_event.list,
8879 		      &phba->sli4_hba.sp_queue_event);
8880 	phba->hba_flag |= HBA_SP_QUEUE_EVT;
8881 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8882 
8883 	return true;
8884 }
8885 
8886 /**
8887  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8888  * @phba: Pointer to HBA context object.
8889  * @wcqe: Pointer to work-queue completion queue entry.
8890  *
8891  * This routine handles slow-path WQ entry comsumed event by invoking the
8892  * proper WQ release routine to the slow-path WQ.
8893  **/
8894 static void
8895 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8896 			     struct lpfc_wcqe_release *wcqe)
8897 {
8898 	/* Check for the slow-path ELS work queue */
8899 	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8900 		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8901 				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8902 	else
8903 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8904 				"2579 Slow-path wqe consume event carries "
8905 				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8906 				bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8907 				phba->sli4_hba.els_wq->queue_id);
8908 }
8909 
8910 /**
8911  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8912  * @phba: Pointer to HBA context object.
8913  * @cq: Pointer to a WQ completion queue.
8914  * @wcqe: Pointer to work-queue completion queue entry.
8915  *
8916  * This routine handles an XRI abort event.
8917  *
8918  * Return: true if work posted to worker thread, otherwise false.
8919  **/
8920 static bool
8921 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8922 				   struct lpfc_queue *cq,
8923 				   struct sli4_wcqe_xri_aborted *wcqe)
8924 {
8925 	bool workposted = false;
8926 	struct lpfc_cq_event *cq_event;
8927 	unsigned long iflags;
8928 
8929 	/* Allocate a new internal CQ_EVENT entry */
8930 	cq_event = lpfc_sli4_cq_event_alloc(phba);
8931 	if (!cq_event) {
8932 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8933 				"0602 Failed to allocate CQ_EVENT entry\n");
8934 		return false;
8935 	}
8936 
8937 	/* Move the CQE into the proper xri abort event list */
8938 	memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8939 	switch (cq->subtype) {
8940 	case LPFC_FCP:
8941 		spin_lock_irqsave(&phba->hbalock, iflags);
8942 		list_add_tail(&cq_event->list,
8943 			      &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8944 		/* Set the fcp xri abort event flag */
8945 		phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8946 		spin_unlock_irqrestore(&phba->hbalock, iflags);
8947 		workposted = true;
8948 		break;
8949 	case LPFC_ELS:
8950 		spin_lock_irqsave(&phba->hbalock, iflags);
8951 		list_add_tail(&cq_event->list,
8952 			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8953 		/* Set the els xri abort event flag */
8954 		phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8955 		spin_unlock_irqrestore(&phba->hbalock, iflags);
8956 		workposted = true;
8957 		break;
8958 	default:
8959 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8960 				"0603 Invalid work queue CQE subtype (x%x)\n",
8961 				cq->subtype);
8962 		workposted = false;
8963 		break;
8964 	}
8965 	return workposted;
8966 }
8967 
8968 /**
8969  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8970  * @phba: Pointer to HBA context object.
8971  * @rcqe: Pointer to receive-queue completion queue entry.
8972  *
8973  * This routine process a receive-queue completion queue entry.
8974  *
8975  * Return: true if work posted to worker thread, otherwise false.
8976  **/
8977 static bool
8978 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8979 {
8980 	bool workposted = false;
8981 	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8982 	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8983 	struct hbq_dmabuf *dma_buf;
8984 	uint32_t status;
8985 	unsigned long iflags;
8986 
8987 	if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8988 		goto out;
8989 
8990 	status = bf_get(lpfc_rcqe_status, rcqe);
8991 	switch (status) {
8992 	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8993 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8994 				"2537 Receive Frame Truncated!!\n");
8995 	case FC_STATUS_RQ_SUCCESS:
8996 		lpfc_sli4_rq_release(hrq, drq);
8997 		spin_lock_irqsave(&phba->hbalock, iflags);
8998 		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8999 		if (!dma_buf) {
9000 			spin_unlock_irqrestore(&phba->hbalock, iflags);
9001 			goto out;
9002 		}
9003 		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9004 		/* save off the frame for the word thread to process */
9005 		list_add_tail(&dma_buf->cq_event.list,
9006 			      &phba->sli4_hba.sp_queue_event);
9007 		/* Frame received */
9008 		phba->hba_flag |= HBA_SP_QUEUE_EVT;
9009 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9010 		workposted = true;
9011 		break;
9012 	case FC_STATUS_INSUFF_BUF_NEED_BUF:
9013 	case FC_STATUS_INSUFF_BUF_FRM_DISC:
9014 		/* Post more buffers if possible */
9015 		spin_lock_irqsave(&phba->hbalock, iflags);
9016 		phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9017 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9018 		workposted = true;
9019 		break;
9020 	}
9021 out:
9022 	return workposted;
9023 }
9024 
9025 /**
9026  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9027  * @phba: Pointer to HBA context object.
9028  * @cq: Pointer to the completion queue.
9029  * @wcqe: Pointer to a completion queue entry.
9030  *
9031  * This routine process a slow-path work-queue or recieve queue completion queue
9032  * entry.
9033  *
9034  * Return: true if work posted to worker thread, otherwise false.
9035  **/
9036 static bool
9037 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9038 			 struct lpfc_cqe *cqe)
9039 {
9040 	struct lpfc_cqe cqevt;
9041 	bool workposted = false;
9042 
9043 	/* Copy the work queue CQE and convert endian order if needed */
9044 	lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
9045 
9046 	/* Check and process for different type of WCQE and dispatch */
9047 	switch (bf_get(lpfc_cqe_code, &cqevt)) {
9048 	case CQE_CODE_COMPL_WQE:
9049 		/* Process the WQ/RQ complete event */
9050 		workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
9051 				(struct lpfc_wcqe_complete *)&cqevt);
9052 		break;
9053 	case CQE_CODE_RELEASE_WQE:
9054 		/* Process the WQ release event */
9055 		lpfc_sli4_sp_handle_rel_wcqe(phba,
9056 				(struct lpfc_wcqe_release *)&cqevt);
9057 		break;
9058 	case CQE_CODE_XRI_ABORTED:
9059 		/* Process the WQ XRI abort event */
9060 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9061 				(struct sli4_wcqe_xri_aborted *)&cqevt);
9062 		break;
9063 	case CQE_CODE_RECEIVE:
9064 		/* Process the RQ event */
9065 		workposted = lpfc_sli4_sp_handle_rcqe(phba,
9066 				(struct lpfc_rcqe *)&cqevt);
9067 		break;
9068 	default:
9069 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9070 				"0388 Not a valid WCQE code: x%x\n",
9071 				bf_get(lpfc_cqe_code, &cqevt));
9072 		break;
9073 	}
9074 	return workposted;
9075 }
9076 
9077 /**
9078  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9079  * @phba: Pointer to HBA context object.
9080  * @eqe: Pointer to fast-path event queue entry.
9081  *
9082  * This routine process a event queue entry from the slow-path event queue.
9083  * It will check the MajorCode and MinorCode to determine this is for a
9084  * completion event on a completion queue, if not, an error shall be logged
9085  * and just return. Otherwise, it will get to the corresponding completion
9086  * queue and process all the entries on that completion queue, rearm the
9087  * completion queue, and then return.
9088  *
9089  **/
9090 static void
9091 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9092 {
9093 	struct lpfc_queue *cq = NULL, *childq, *speq;
9094 	struct lpfc_cqe *cqe;
9095 	bool workposted = false;
9096 	int ecount = 0;
9097 	uint16_t cqid;
9098 
9099 	if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
9100 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9101 				"0359 Not a valid slow-path completion "
9102 				"event: majorcode=x%x, minorcode=x%x\n",
9103 				bf_get_le32(lpfc_eqe_major_code, eqe),
9104 				bf_get_le32(lpfc_eqe_minor_code, eqe));
9105 		return;
9106 	}
9107 
9108 	/* Get the reference to the corresponding CQ */
9109 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9110 
9111 	/* Search for completion queue pointer matching this cqid */
9112 	speq = phba->sli4_hba.sp_eq;
9113 	list_for_each_entry(childq, &speq->child_list, list) {
9114 		if (childq->queue_id == cqid) {
9115 			cq = childq;
9116 			break;
9117 		}
9118 	}
9119 	if (unlikely(!cq)) {
9120 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9121 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9122 					"0365 Slow-path CQ identifier "
9123 					"(%d) does not exist\n", cqid);
9124 		return;
9125 	}
9126 
9127 	/* Process all the entries to the CQ */
9128 	switch (cq->type) {
9129 	case LPFC_MCQ:
9130 		while ((cqe = lpfc_sli4_cq_get(cq))) {
9131 			workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9132 			if (!(++ecount % LPFC_GET_QE_REL_INT))
9133 				lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9134 		}
9135 		break;
9136 	case LPFC_WCQ:
9137 		while ((cqe = lpfc_sli4_cq_get(cq))) {
9138 			workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
9139 			if (!(++ecount % LPFC_GET_QE_REL_INT))
9140 				lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9141 		}
9142 		break;
9143 	default:
9144 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9145 				"0370 Invalid completion queue type (%d)\n",
9146 				cq->type);
9147 		return;
9148 	}
9149 
9150 	/* Catch the no cq entry condition, log an error */
9151 	if (unlikely(ecount == 0))
9152 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9153 				"0371 No entry from the CQ: identifier "
9154 				"(x%x), type (%d)\n", cq->queue_id, cq->type);
9155 
9156 	/* In any case, flash and re-arm the RCQ */
9157 	lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9158 
9159 	/* wake up worker thread if there are works to be done */
9160 	if (workposted)
9161 		lpfc_worker_wake_up(phba);
9162 }
9163 
9164 /**
9165  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9166  * @eqe: Pointer to fast-path completion queue entry.
9167  *
9168  * This routine process a fast-path work queue completion entry from fast-path
9169  * event queue for FCP command response completion.
9170  **/
9171 static void
9172 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9173 			     struct lpfc_wcqe_complete *wcqe)
9174 {
9175 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9176 	struct lpfc_iocbq *cmdiocbq;
9177 	struct lpfc_iocbq irspiocbq;
9178 	unsigned long iflags;
9179 
9180 	spin_lock_irqsave(&phba->hbalock, iflags);
9181 	pring->stats.iocb_event++;
9182 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9183 
9184 	/* Check for response status */
9185 	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9186 		/* If resource errors reported from HBA, reduce queue
9187 		 * depth of the SCSI device.
9188 		 */
9189 		if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9190 		     IOSTAT_LOCAL_REJECT) &&
9191 		    (wcqe->parameter == IOERR_NO_RESOURCES)) {
9192 			phba->lpfc_rampdown_queue_depth(phba);
9193 		}
9194 		/* Log the error status */
9195 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9196 				"0373 FCP complete error: status=x%x, "
9197 				"hw_status=x%x, total_data_specified=%d, "
9198 				"parameter=x%x, word3=x%x\n",
9199 				bf_get(lpfc_wcqe_c_status, wcqe),
9200 				bf_get(lpfc_wcqe_c_hw_status, wcqe),
9201 				wcqe->total_data_placed, wcqe->parameter,
9202 				wcqe->word3);
9203 	}
9204 
9205 	/* Look up the FCP command IOCB and create pseudo response IOCB */
9206 	spin_lock_irqsave(&phba->hbalock, iflags);
9207 	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9208 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
9209 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9210 	if (unlikely(!cmdiocbq)) {
9211 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9212 				"0374 FCP complete with no corresponding "
9213 				"cmdiocb: iotag (%d)\n",
9214 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
9215 		return;
9216 	}
9217 	if (unlikely(!cmdiocbq->iocb_cmpl)) {
9218 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9219 				"0375 FCP cmdiocb not callback function "
9220 				"iotag: (%d)\n",
9221 				bf_get(lpfc_wcqe_c_request_tag, wcqe));
9222 		return;
9223 	}
9224 
9225 	/* Fake the irspiocb and copy necessary response information */
9226 	lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9227 
9228 	if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9229 		spin_lock_irqsave(&phba->hbalock, iflags);
9230 		cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9231 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9232 	}
9233 
9234 	/* Pass the cmd_iocb and the rsp state to the upper layer */
9235 	(cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9236 }
9237 
9238 /**
9239  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9240  * @phba: Pointer to HBA context object.
9241  * @cq: Pointer to completion queue.
9242  * @wcqe: Pointer to work-queue completion queue entry.
9243  *
9244  * This routine handles an fast-path WQ entry comsumed event by invoking the
9245  * proper WQ release routine to the slow-path WQ.
9246  **/
9247 static void
9248 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9249 			     struct lpfc_wcqe_release *wcqe)
9250 {
9251 	struct lpfc_queue *childwq;
9252 	bool wqid_matched = false;
9253 	uint16_t fcp_wqid;
9254 
9255 	/* Check for fast-path FCP work queue release */
9256 	fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9257 	list_for_each_entry(childwq, &cq->child_list, list) {
9258 		if (childwq->queue_id == fcp_wqid) {
9259 			lpfc_sli4_wq_release(childwq,
9260 					bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9261 			wqid_matched = true;
9262 			break;
9263 		}
9264 	}
9265 	/* Report warning log message if no match found */
9266 	if (wqid_matched != true)
9267 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9268 				"2580 Fast-path wqe consume event carries "
9269 				"miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9270 }
9271 
9272 /**
9273  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9274  * @cq: Pointer to the completion queue.
9275  * @eqe: Pointer to fast-path completion queue entry.
9276  *
9277  * This routine process a fast-path work queue completion entry from fast-path
9278  * event queue for FCP command response completion.
9279  **/
9280 static int
9281 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9282 			 struct lpfc_cqe *cqe)
9283 {
9284 	struct lpfc_wcqe_release wcqe;
9285 	bool workposted = false;
9286 	unsigned long iflag;
9287 
9288 	/* Copy the work queue CQE and convert endian order if needed */
9289 	lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9290 
9291 	/* Check and process for different type of WCQE and dispatch */
9292 	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9293 	case CQE_CODE_COMPL_WQE:
9294 		/* Process the WQ complete event */
9295 		spin_lock_irqsave(&phba->hbalock, iflag);
9296 		phba->last_completion_time = jiffies;
9297 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9298 		lpfc_sli4_fp_handle_fcp_wcqe(phba,
9299 				(struct lpfc_wcqe_complete *)&wcqe);
9300 		break;
9301 	case CQE_CODE_RELEASE_WQE:
9302 		/* Process the WQ release event */
9303 		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9304 				(struct lpfc_wcqe_release *)&wcqe);
9305 		break;
9306 	case CQE_CODE_XRI_ABORTED:
9307 		/* Process the WQ XRI abort event */
9308 		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9309 				(struct sli4_wcqe_xri_aborted *)&wcqe);
9310 		break;
9311 	default:
9312 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9313 				"0144 Not a valid WCQE code: x%x\n",
9314 				bf_get(lpfc_wcqe_c_code, &wcqe));
9315 		break;
9316 	}
9317 	return workposted;
9318 }
9319 
9320 /**
9321  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9322  * @phba: Pointer to HBA context object.
9323  * @eqe: Pointer to fast-path event queue entry.
9324  *
9325  * This routine process a event queue entry from the fast-path event queue.
9326  * It will check the MajorCode and MinorCode to determine this is for a
9327  * completion event on a completion queue, if not, an error shall be logged
9328  * and just return. Otherwise, it will get to the corresponding completion
9329  * queue and process all the entries on the completion queue, rearm the
9330  * completion queue, and then return.
9331  **/
9332 static void
9333 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9334 			uint32_t fcp_cqidx)
9335 {
9336 	struct lpfc_queue *cq;
9337 	struct lpfc_cqe *cqe;
9338 	bool workposted = false;
9339 	uint16_t cqid;
9340 	int ecount = 0;
9341 
9342 	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
9343 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9344 				"0366 Not a valid fast-path completion "
9345 				"event: majorcode=x%x, minorcode=x%x\n",
9346 				bf_get_le32(lpfc_eqe_major_code, eqe),
9347 				bf_get_le32(lpfc_eqe_minor_code, eqe));
9348 		return;
9349 	}
9350 
9351 	cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9352 	if (unlikely(!cq)) {
9353 		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9354 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9355 					"0367 Fast-path completion queue "
9356 					"does not exist\n");
9357 		return;
9358 	}
9359 
9360 	/* Get the reference to the corresponding CQ */
9361 	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9362 	if (unlikely(cqid != cq->queue_id)) {
9363 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9364 				"0368 Miss-matched fast-path completion "
9365 				"queue identifier: eqcqid=%d, fcpcqid=%d\n",
9366 				cqid, cq->queue_id);
9367 		return;
9368 	}
9369 
9370 	/* Process all the entries to the CQ */
9371 	while ((cqe = lpfc_sli4_cq_get(cq))) {
9372 		workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9373 		if (!(++ecount % LPFC_GET_QE_REL_INT))
9374 			lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9375 	}
9376 
9377 	/* Catch the no cq entry condition */
9378 	if (unlikely(ecount == 0))
9379 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9380 				"0369 No entry from fast-path completion "
9381 				"queue fcpcqid=%d\n", cq->queue_id);
9382 
9383 	/* In any case, flash and re-arm the CQ */
9384 	lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9385 
9386 	/* wake up worker thread if there are works to be done */
9387 	if (workposted)
9388 		lpfc_worker_wake_up(phba);
9389 }
9390 
9391 static void
9392 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9393 {
9394 	struct lpfc_eqe *eqe;
9395 
9396 	/* walk all the EQ entries and drop on the floor */
9397 	while ((eqe = lpfc_sli4_eq_get(eq)))
9398 		;
9399 
9400 	/* Clear and re-arm the EQ */
9401 	lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9402 }
9403 
9404 /**
9405  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9406  * @irq: Interrupt number.
9407  * @dev_id: The device context pointer.
9408  *
9409  * This function is directly called from the PCI layer as an interrupt
9410  * service routine when device with SLI-4 interface spec is enabled with
9411  * MSI-X multi-message interrupt mode and there are slow-path events in
9412  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9413  * interrupt mode, this function is called as part of the device-level
9414  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9415  * undergoing initialization, the interrupt handler will not process the
9416  * interrupt. The link attention and ELS ring attention events are handled
9417  * by the worker thread. The interrupt handler signals the worker thread
9418  * and returns for these events. This function is called without any lock
9419  * held. It gets the hbalock to access and update SLI data structures.
9420  *
9421  * This function returns IRQ_HANDLED when interrupt is handled else it
9422  * returns IRQ_NONE.
9423  **/
9424 irqreturn_t
9425 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9426 {
9427 	struct lpfc_hba *phba;
9428 	struct lpfc_queue *speq;
9429 	struct lpfc_eqe *eqe;
9430 	unsigned long iflag;
9431 	int ecount = 0;
9432 
9433 	/*
9434 	 * Get the driver's phba structure from the dev_id
9435 	 */
9436 	phba = (struct lpfc_hba *)dev_id;
9437 
9438 	if (unlikely(!phba))
9439 		return IRQ_NONE;
9440 
9441 	/* Get to the EQ struct associated with this vector */
9442 	speq = phba->sli4_hba.sp_eq;
9443 
9444 	/* Check device state for handling interrupt */
9445 	if (unlikely(lpfc_intr_state_check(phba))) {
9446 		/* Check again for link_state with lock held */
9447 		spin_lock_irqsave(&phba->hbalock, iflag);
9448 		if (phba->link_state < LPFC_LINK_DOWN)
9449 			/* Flush, clear interrupt, and rearm the EQ */
9450 			lpfc_sli4_eq_flush(phba, speq);
9451 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9452 		return IRQ_NONE;
9453 	}
9454 
9455 	/*
9456 	 * Process all the event on FCP slow-path EQ
9457 	 */
9458 	while ((eqe = lpfc_sli4_eq_get(speq))) {
9459 		lpfc_sli4_sp_handle_eqe(phba, eqe);
9460 		if (!(++ecount % LPFC_GET_QE_REL_INT))
9461 			lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9462 	}
9463 
9464 	/* Always clear and re-arm the slow-path EQ */
9465 	lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9466 
9467 	/* Catch the no cq entry condition */
9468 	if (unlikely(ecount == 0)) {
9469 		if (phba->intr_type == MSIX)
9470 			/* MSI-X treated interrupt served as no EQ share INT */
9471 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9472 					"0357 MSI-X interrupt with no EQE\n");
9473 		else
9474 			/* Non MSI-X treated on interrupt as EQ share INT */
9475 			return IRQ_NONE;
9476 	}
9477 
9478 	return IRQ_HANDLED;
9479 } /* lpfc_sli4_sp_intr_handler */
9480 
9481 /**
9482  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9483  * @irq: Interrupt number.
9484  * @dev_id: The device context pointer.
9485  *
9486  * This function is directly called from the PCI layer as an interrupt
9487  * service routine when device with SLI-4 interface spec is enabled with
9488  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9489  * ring event in the HBA. However, when the device is enabled with either
9490  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9491  * device-level interrupt handler. When the PCI slot is in error recovery
9492  * or the HBA is undergoing initialization, the interrupt handler will not
9493  * process the interrupt. The SCSI FCP fast-path ring event are handled in
9494  * the intrrupt context. This function is called without any lock held.
9495  * It gets the hbalock to access and update SLI data structures. Note that,
9496  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9497  * equal to that of FCP CQ index.
9498  *
9499  * This function returns IRQ_HANDLED when interrupt is handled else it
9500  * returns IRQ_NONE.
9501  **/
9502 irqreturn_t
9503 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9504 {
9505 	struct lpfc_hba *phba;
9506 	struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9507 	struct lpfc_queue *fpeq;
9508 	struct lpfc_eqe *eqe;
9509 	unsigned long iflag;
9510 	int ecount = 0;
9511 	uint32_t fcp_eqidx;
9512 
9513 	/* Get the driver's phba structure from the dev_id */
9514 	fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9515 	phba = fcp_eq_hdl->phba;
9516 	fcp_eqidx = fcp_eq_hdl->idx;
9517 
9518 	if (unlikely(!phba))
9519 		return IRQ_NONE;
9520 
9521 	/* Get to the EQ struct associated with this vector */
9522 	fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9523 
9524 	/* Check device state for handling interrupt */
9525 	if (unlikely(lpfc_intr_state_check(phba))) {
9526 		/* Check again for link_state with lock held */
9527 		spin_lock_irqsave(&phba->hbalock, iflag);
9528 		if (phba->link_state < LPFC_LINK_DOWN)
9529 			/* Flush, clear interrupt, and rearm the EQ */
9530 			lpfc_sli4_eq_flush(phba, fpeq);
9531 		spin_unlock_irqrestore(&phba->hbalock, iflag);
9532 		return IRQ_NONE;
9533 	}
9534 
9535 	/*
9536 	 * Process all the event on FCP fast-path EQ
9537 	 */
9538 	while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9539 		lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9540 		if (!(++ecount % LPFC_GET_QE_REL_INT))
9541 			lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9542 	}
9543 
9544 	/* Always clear and re-arm the fast-path EQ */
9545 	lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9546 
9547 	if (unlikely(ecount == 0)) {
9548 		if (phba->intr_type == MSIX)
9549 			/* MSI-X treated interrupt served as no EQ share INT */
9550 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9551 					"0358 MSI-X interrupt with no EQE\n");
9552 		else
9553 			/* Non MSI-X treated on interrupt as EQ share INT */
9554 			return IRQ_NONE;
9555 	}
9556 
9557 	return IRQ_HANDLED;
9558 } /* lpfc_sli4_fp_intr_handler */
9559 
9560 /**
9561  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9562  * @irq: Interrupt number.
9563  * @dev_id: The device context pointer.
9564  *
9565  * This function is the device-level interrupt handler to device with SLI-4
9566  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9567  * interrupt mode is enabled and there is an event in the HBA which requires
9568  * driver attention. This function invokes the slow-path interrupt attention
9569  * handling function and fast-path interrupt attention handling function in
9570  * turn to process the relevant HBA attention events. This function is called
9571  * without any lock held. It gets the hbalock to access and update SLI data
9572  * structures.
9573  *
9574  * This function returns IRQ_HANDLED when interrupt is handled, else it
9575  * returns IRQ_NONE.
9576  **/
9577 irqreturn_t
9578 lpfc_sli4_intr_handler(int irq, void *dev_id)
9579 {
9580 	struct lpfc_hba  *phba;
9581 	irqreturn_t sp_irq_rc, fp_irq_rc;
9582 	bool fp_handled = false;
9583 	uint32_t fcp_eqidx;
9584 
9585 	/* Get the driver's phba structure from the dev_id */
9586 	phba = (struct lpfc_hba *)dev_id;
9587 
9588 	if (unlikely(!phba))
9589 		return IRQ_NONE;
9590 
9591 	/*
9592 	 * Invokes slow-path host attention interrupt handling as appropriate.
9593 	 */
9594 	sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9595 
9596 	/*
9597 	 * Invoke fast-path host attention interrupt handling as appropriate.
9598 	 */
9599 	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9600 		fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9601 					&phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9602 		if (fp_irq_rc == IRQ_HANDLED)
9603 			fp_handled |= true;
9604 	}
9605 
9606 	return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9607 } /* lpfc_sli4_intr_handler */
9608 
9609 /**
9610  * lpfc_sli4_queue_free - free a queue structure and associated memory
9611  * @queue: The queue structure to free.
9612  *
9613  * This function frees a queue structure and the DMAable memeory used for
9614  * the host resident queue. This function must be called after destroying the
9615  * queue on the HBA.
9616  **/
9617 void
9618 lpfc_sli4_queue_free(struct lpfc_queue *queue)
9619 {
9620 	struct lpfc_dmabuf *dmabuf;
9621 
9622 	if (!queue)
9623 		return;
9624 
9625 	while (!list_empty(&queue->page_list)) {
9626 		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9627 				 list);
9628 		dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
9629 				  dmabuf->virt, dmabuf->phys);
9630 		kfree(dmabuf);
9631 	}
9632 	kfree(queue);
9633 	return;
9634 }
9635 
9636 /**
9637  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9638  * @phba: The HBA that this queue is being created on.
9639  * @entry_size: The size of each queue entry for this queue.
9640  * @entry count: The number of entries that this queue will handle.
9641  *
9642  * This function allocates a queue structure and the DMAable memory used for
9643  * the host resident queue. This function must be called before creating the
9644  * queue on the HBA.
9645  **/
9646 struct lpfc_queue *
9647 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9648 		      uint32_t entry_count)
9649 {
9650 	struct lpfc_queue *queue;
9651 	struct lpfc_dmabuf *dmabuf;
9652 	int x, total_qe_count;
9653 	void *dma_pointer;
9654 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9655 
9656 	if (!phba->sli4_hba.pc_sli4_params.supported)
9657 		hw_page_size = SLI4_PAGE_SIZE;
9658 
9659 	queue = kzalloc(sizeof(struct lpfc_queue) +
9660 			(sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9661 	if (!queue)
9662 		return NULL;
9663 	queue->page_count = (ALIGN(entry_size * entry_count,
9664 			hw_page_size))/hw_page_size;
9665 	INIT_LIST_HEAD(&queue->list);
9666 	INIT_LIST_HEAD(&queue->page_list);
9667 	INIT_LIST_HEAD(&queue->child_list);
9668 	for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9669 		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9670 		if (!dmabuf)
9671 			goto out_fail;
9672 		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9673 						  hw_page_size, &dmabuf->phys,
9674 						  GFP_KERNEL);
9675 		if (!dmabuf->virt) {
9676 			kfree(dmabuf);
9677 			goto out_fail;
9678 		}
9679 		memset(dmabuf->virt, 0, hw_page_size);
9680 		dmabuf->buffer_tag = x;
9681 		list_add_tail(&dmabuf->list, &queue->page_list);
9682 		/* initialize queue's entry array */
9683 		dma_pointer = dmabuf->virt;
9684 		for (; total_qe_count < entry_count &&
9685 		     dma_pointer < (hw_page_size + dmabuf->virt);
9686 		     total_qe_count++, dma_pointer += entry_size) {
9687 			queue->qe[total_qe_count].address = dma_pointer;
9688 		}
9689 	}
9690 	queue->entry_size = entry_size;
9691 	queue->entry_count = entry_count;
9692 	queue->phba = phba;
9693 
9694 	return queue;
9695 out_fail:
9696 	lpfc_sli4_queue_free(queue);
9697 	return NULL;
9698 }
9699 
9700 /**
9701  * lpfc_eq_create - Create an Event Queue on the HBA
9702  * @phba: HBA structure that indicates port to create a queue on.
9703  * @eq: The queue structure to use to create the event queue.
9704  * @imax: The maximum interrupt per second limit.
9705  *
9706  * This function creates an event queue, as detailed in @eq, on a port,
9707  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9708  *
9709  * The @phba struct is used to send mailbox command to HBA. The @eq struct
9710  * is used to get the entry count and entry size that are necessary to
9711  * determine the number of pages to allocate and use for this queue. This
9712  * function will send the EQ_CREATE mailbox command to the HBA to setup the
9713  * event queue. This function is asynchronous and will wait for the mailbox
9714  * command to finish before continuing.
9715  *
9716  * On success this function will return a zero. If unable to allocate enough
9717  * memory this function will return ENOMEM. If the queue create mailbox command
9718  * fails this function will return ENXIO.
9719  **/
9720 uint32_t
9721 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9722 {
9723 	struct lpfc_mbx_eq_create *eq_create;
9724 	LPFC_MBOXQ_t *mbox;
9725 	int rc, length, status = 0;
9726 	struct lpfc_dmabuf *dmabuf;
9727 	uint32_t shdr_status, shdr_add_status;
9728 	union lpfc_sli4_cfg_shdr *shdr;
9729 	uint16_t dmult;
9730 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9731 
9732 	if (!phba->sli4_hba.pc_sli4_params.supported)
9733 		hw_page_size = SLI4_PAGE_SIZE;
9734 
9735 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9736 	if (!mbox)
9737 		return -ENOMEM;
9738 	length = (sizeof(struct lpfc_mbx_eq_create) -
9739 		  sizeof(struct lpfc_sli4_cfg_mhdr));
9740 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9741 			 LPFC_MBOX_OPCODE_EQ_CREATE,
9742 			 length, LPFC_SLI4_MBX_EMBED);
9743 	eq_create = &mbox->u.mqe.un.eq_create;
9744 	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9745 	       eq->page_count);
9746 	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9747 	       LPFC_EQE_SIZE);
9748 	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9749 	/* Calculate delay multiper from maximum interrupt per second */
9750 	dmult = LPFC_DMULT_CONST/imax - 1;
9751 	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9752 	       dmult);
9753 	switch (eq->entry_count) {
9754 	default:
9755 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9756 				"0360 Unsupported EQ count. (%d)\n",
9757 				eq->entry_count);
9758 		if (eq->entry_count < 256)
9759 			return -EINVAL;
9760 		/* otherwise default to smallest count (drop through) */
9761 	case 256:
9762 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9763 		       LPFC_EQ_CNT_256);
9764 		break;
9765 	case 512:
9766 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9767 		       LPFC_EQ_CNT_512);
9768 		break;
9769 	case 1024:
9770 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9771 		       LPFC_EQ_CNT_1024);
9772 		break;
9773 	case 2048:
9774 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9775 		       LPFC_EQ_CNT_2048);
9776 		break;
9777 	case 4096:
9778 		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9779 		       LPFC_EQ_CNT_4096);
9780 		break;
9781 	}
9782 	list_for_each_entry(dmabuf, &eq->page_list, list) {
9783 		memset(dmabuf->virt, 0, hw_page_size);
9784 		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9785 					putPaddrLow(dmabuf->phys);
9786 		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9787 					putPaddrHigh(dmabuf->phys);
9788 	}
9789 	mbox->vport = phba->pport;
9790 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9791 	mbox->context1 = NULL;
9792 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9793 	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9794 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9795 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9796 	if (shdr_status || shdr_add_status || rc) {
9797 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9798 				"2500 EQ_CREATE mailbox failed with "
9799 				"status x%x add_status x%x, mbx status x%x\n",
9800 				shdr_status, shdr_add_status, rc);
9801 		status = -ENXIO;
9802 	}
9803 	eq->type = LPFC_EQ;
9804 	eq->subtype = LPFC_NONE;
9805 	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9806 	if (eq->queue_id == 0xFFFF)
9807 		status = -ENXIO;
9808 	eq->host_index = 0;
9809 	eq->hba_index = 0;
9810 
9811 	mempool_free(mbox, phba->mbox_mem_pool);
9812 	return status;
9813 }
9814 
9815 /**
9816  * lpfc_cq_create - Create a Completion Queue on the HBA
9817  * @phba: HBA structure that indicates port to create a queue on.
9818  * @cq: The queue structure to use to create the completion queue.
9819  * @eq: The event queue to bind this completion queue to.
9820  *
9821  * This function creates a completion queue, as detailed in @wq, on a port,
9822  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9823  *
9824  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9825  * is used to get the entry count and entry size that are necessary to
9826  * determine the number of pages to allocate and use for this queue. The @eq
9827  * is used to indicate which event queue to bind this completion queue to. This
9828  * function will send the CQ_CREATE mailbox command to the HBA to setup the
9829  * completion queue. This function is asynchronous and will wait for the mailbox
9830  * command to finish before continuing.
9831  *
9832  * On success this function will return a zero. If unable to allocate enough
9833  * memory this function will return ENOMEM. If the queue create mailbox command
9834  * fails this function will return ENXIO.
9835  **/
9836 uint32_t
9837 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9838 	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9839 {
9840 	struct lpfc_mbx_cq_create *cq_create;
9841 	struct lpfc_dmabuf *dmabuf;
9842 	LPFC_MBOXQ_t *mbox;
9843 	int rc, length, status = 0;
9844 	uint32_t shdr_status, shdr_add_status;
9845 	union lpfc_sli4_cfg_shdr *shdr;
9846 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9847 
9848 	if (!phba->sli4_hba.pc_sli4_params.supported)
9849 		hw_page_size = SLI4_PAGE_SIZE;
9850 
9851 
9852 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9853 	if (!mbox)
9854 		return -ENOMEM;
9855 	length = (sizeof(struct lpfc_mbx_cq_create) -
9856 		  sizeof(struct lpfc_sli4_cfg_mhdr));
9857 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9858 			 LPFC_MBOX_OPCODE_CQ_CREATE,
9859 			 length, LPFC_SLI4_MBX_EMBED);
9860 	cq_create = &mbox->u.mqe.un.cq_create;
9861 	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9862 		    cq->page_count);
9863 	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9864 	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9865 	bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9866 	switch (cq->entry_count) {
9867 	default:
9868 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9869 				"0361 Unsupported CQ count. (%d)\n",
9870 				cq->entry_count);
9871 		if (cq->entry_count < 256)
9872 			return -EINVAL;
9873 		/* otherwise default to smallest count (drop through) */
9874 	case 256:
9875 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9876 		       LPFC_CQ_CNT_256);
9877 		break;
9878 	case 512:
9879 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9880 		       LPFC_CQ_CNT_512);
9881 		break;
9882 	case 1024:
9883 		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9884 		       LPFC_CQ_CNT_1024);
9885 		break;
9886 	}
9887 	list_for_each_entry(dmabuf, &cq->page_list, list) {
9888 		memset(dmabuf->virt, 0, hw_page_size);
9889 		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9890 					putPaddrLow(dmabuf->phys);
9891 		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9892 					putPaddrHigh(dmabuf->phys);
9893 	}
9894 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9895 
9896 	/* The IOCTL status is embedded in the mailbox subheader. */
9897 	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9898 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9899 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9900 	if (shdr_status || shdr_add_status || rc) {
9901 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9902 				"2501 CQ_CREATE mailbox failed with "
9903 				"status x%x add_status x%x, mbx status x%x\n",
9904 				shdr_status, shdr_add_status, rc);
9905 		status = -ENXIO;
9906 		goto out;
9907 	}
9908 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9909 	if (cq->queue_id == 0xFFFF) {
9910 		status = -ENXIO;
9911 		goto out;
9912 	}
9913 	/* link the cq onto the parent eq child list */
9914 	list_add_tail(&cq->list, &eq->child_list);
9915 	/* Set up completion queue's type and subtype */
9916 	cq->type = type;
9917 	cq->subtype = subtype;
9918 	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9919 	cq->host_index = 0;
9920 	cq->hba_index = 0;
9921 
9922 out:
9923 	mempool_free(mbox, phba->mbox_mem_pool);
9924 	return status;
9925 }
9926 
9927 /**
9928  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
9929  * @phba: HBA structure that indicates port to create a queue on.
9930  * @mq: The queue structure to use to create the mailbox queue.
9931  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9932  * @cq: The completion queue to associate with this cq.
9933  *
9934  * This function provides failback (fb) functionality when the
9935  * mq_create_ext fails on older FW generations.  It's purpose is identical
9936  * to mq_create_ext otherwise.
9937  *
9938  * This routine cannot fail as all attributes were previously accessed and
9939  * initialized in mq_create_ext.
9940  **/
9941 static void
9942 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9943 		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
9944 {
9945 	struct lpfc_mbx_mq_create *mq_create;
9946 	struct lpfc_dmabuf *dmabuf;
9947 	int length;
9948 
9949 	length = (sizeof(struct lpfc_mbx_mq_create) -
9950 		  sizeof(struct lpfc_sli4_cfg_mhdr));
9951 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9952 			 LPFC_MBOX_OPCODE_MQ_CREATE,
9953 			 length, LPFC_SLI4_MBX_EMBED);
9954 	mq_create = &mbox->u.mqe.un.mq_create;
9955 	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9956 	       mq->page_count);
9957 	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9958 	       cq->queue_id);
9959 	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9960 	switch (mq->entry_count) {
9961 	case 16:
9962 		bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9963 		       LPFC_MQ_CNT_16);
9964 		break;
9965 	case 32:
9966 		bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9967 		       LPFC_MQ_CNT_32);
9968 		break;
9969 	case 64:
9970 		bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9971 		       LPFC_MQ_CNT_64);
9972 		break;
9973 	case 128:
9974 		bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9975 		       LPFC_MQ_CNT_128);
9976 		break;
9977 	}
9978 	list_for_each_entry(dmabuf, &mq->page_list, list) {
9979 		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9980 			putPaddrLow(dmabuf->phys);
9981 		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9982 			putPaddrHigh(dmabuf->phys);
9983 	}
9984 }
9985 
9986 /**
9987  * lpfc_mq_create - Create a mailbox Queue on the HBA
9988  * @phba: HBA structure that indicates port to create a queue on.
9989  * @mq: The queue structure to use to create the mailbox queue.
9990  * @cq: The completion queue to associate with this cq.
9991  * @subtype: The queue's subtype.
9992  *
9993  * This function creates a mailbox queue, as detailed in @mq, on a port,
9994  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9995  *
9996  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9997  * is used to get the entry count and entry size that are necessary to
9998  * determine the number of pages to allocate and use for this queue. This
9999  * function will send the MQ_CREATE mailbox command to the HBA to setup the
10000  * mailbox queue. This function is asynchronous and will wait for the mailbox
10001  * command to finish before continuing.
10002  *
10003  * On success this function will return a zero. If unable to allocate enough
10004  * memory this function will return ENOMEM. If the queue create mailbox command
10005  * fails this function will return ENXIO.
10006  **/
10007 int32_t
10008 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10009 	       struct lpfc_queue *cq, uint32_t subtype)
10010 {
10011 	struct lpfc_mbx_mq_create *mq_create;
10012 	struct lpfc_mbx_mq_create_ext *mq_create_ext;
10013 	struct lpfc_dmabuf *dmabuf;
10014 	LPFC_MBOXQ_t *mbox;
10015 	int rc, length, status = 0;
10016 	uint32_t shdr_status, shdr_add_status;
10017 	union lpfc_sli4_cfg_shdr *shdr;
10018 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10019 
10020 	if (!phba->sli4_hba.pc_sli4_params.supported)
10021 		hw_page_size = SLI4_PAGE_SIZE;
10022 
10023 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10024 	if (!mbox)
10025 		return -ENOMEM;
10026 	length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10027 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10028 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10029 			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10030 			 length, LPFC_SLI4_MBX_EMBED);
10031 
10032 	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10033 	bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10034 		    mq->page_count);
10035 	bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10036 	       1);
10037 	bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10038 	       &mq_create_ext->u.request, 1);
10039 	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10040 	       &mq_create_ext->u.request, 1);
10041 	bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10042 	       cq->queue_id);
10043 	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10044 	switch (mq->entry_count) {
10045 	default:
10046 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10047 				"0362 Unsupported MQ count. (%d)\n",
10048 				mq->entry_count);
10049 		if (mq->entry_count < 16)
10050 			return -EINVAL;
10051 		/* otherwise default to smallest count (drop through) */
10052 	case 16:
10053 		bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10054 		       LPFC_MQ_CNT_16);
10055 		break;
10056 	case 32:
10057 		bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10058 		       LPFC_MQ_CNT_32);
10059 		break;
10060 	case 64:
10061 		bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10062 		       LPFC_MQ_CNT_64);
10063 		break;
10064 	case 128:
10065 		bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10066 		       LPFC_MQ_CNT_128);
10067 		break;
10068 	}
10069 	list_for_each_entry(dmabuf, &mq->page_list, list) {
10070 		memset(dmabuf->virt, 0, hw_page_size);
10071 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10072 					putPaddrLow(dmabuf->phys);
10073 		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
10074 					putPaddrHigh(dmabuf->phys);
10075 	}
10076 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10077 	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10078 	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10079 			      &mq_create_ext->u.response);
10080 	if (rc != MBX_SUCCESS) {
10081 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10082 				"2795 MQ_CREATE_EXT failed with "
10083 				"status x%x. Failback to MQ_CREATE.\n",
10084 				rc);
10085 		lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10086 		mq_create = &mbox->u.mqe.un.mq_create;
10087 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10088 		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10089 		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10090 				      &mq_create->u.response);
10091 	}
10092 
10093 	/* The IOCTL status is embedded in the mailbox subheader. */
10094 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10095 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10096 	if (shdr_status || shdr_add_status || rc) {
10097 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10098 				"2502 MQ_CREATE mailbox failed with "
10099 				"status x%x add_status x%x, mbx status x%x\n",
10100 				shdr_status, shdr_add_status, rc);
10101 		status = -ENXIO;
10102 		goto out;
10103 	}
10104 	if (mq->queue_id == 0xFFFF) {
10105 		status = -ENXIO;
10106 		goto out;
10107 	}
10108 	mq->type = LPFC_MQ;
10109 	mq->subtype = subtype;
10110 	mq->host_index = 0;
10111 	mq->hba_index = 0;
10112 
10113 	/* link the mq onto the parent cq child list */
10114 	list_add_tail(&mq->list, &cq->child_list);
10115 out:
10116 	mempool_free(mbox, phba->mbox_mem_pool);
10117 	return status;
10118 }
10119 
10120 /**
10121  * lpfc_wq_create - Create a Work Queue on the HBA
10122  * @phba: HBA structure that indicates port to create a queue on.
10123  * @wq: The queue structure to use to create the work queue.
10124  * @cq: The completion queue to bind this work queue to.
10125  * @subtype: The subtype of the work queue indicating its functionality.
10126  *
10127  * This function creates a work queue, as detailed in @wq, on a port, described
10128  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10129  *
10130  * The @phba struct is used to send mailbox command to HBA. The @wq struct
10131  * is used to get the entry count and entry size that are necessary to
10132  * determine the number of pages to allocate and use for this queue. The @cq
10133  * is used to indicate which completion queue to bind this work queue to. This
10134  * function will send the WQ_CREATE mailbox command to the HBA to setup the
10135  * work queue. This function is asynchronous and will wait for the mailbox
10136  * command to finish before continuing.
10137  *
10138  * On success this function will return a zero. If unable to allocate enough
10139  * memory this function will return ENOMEM. If the queue create mailbox command
10140  * fails this function will return ENXIO.
10141  **/
10142 uint32_t
10143 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10144 	       struct lpfc_queue *cq, uint32_t subtype)
10145 {
10146 	struct lpfc_mbx_wq_create *wq_create;
10147 	struct lpfc_dmabuf *dmabuf;
10148 	LPFC_MBOXQ_t *mbox;
10149 	int rc, length, status = 0;
10150 	uint32_t shdr_status, shdr_add_status;
10151 	union lpfc_sli4_cfg_shdr *shdr;
10152 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10153 
10154 	if (!phba->sli4_hba.pc_sli4_params.supported)
10155 		hw_page_size = SLI4_PAGE_SIZE;
10156 
10157 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10158 	if (!mbox)
10159 		return -ENOMEM;
10160 	length = (sizeof(struct lpfc_mbx_wq_create) -
10161 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10162 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10163 			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10164 			 length, LPFC_SLI4_MBX_EMBED);
10165 	wq_create = &mbox->u.mqe.un.wq_create;
10166 	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10167 		    wq->page_count);
10168 	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10169 		    cq->queue_id);
10170 	list_for_each_entry(dmabuf, &wq->page_list, list) {
10171 		memset(dmabuf->virt, 0, hw_page_size);
10172 		wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10173 					putPaddrLow(dmabuf->phys);
10174 		wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10175 					putPaddrHigh(dmabuf->phys);
10176 	}
10177 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10178 	/* The IOCTL status is embedded in the mailbox subheader. */
10179 	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10180 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10181 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10182 	if (shdr_status || shdr_add_status || rc) {
10183 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10184 				"2503 WQ_CREATE mailbox failed with "
10185 				"status x%x add_status x%x, mbx status x%x\n",
10186 				shdr_status, shdr_add_status, rc);
10187 		status = -ENXIO;
10188 		goto out;
10189 	}
10190 	wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10191 	if (wq->queue_id == 0xFFFF) {
10192 		status = -ENXIO;
10193 		goto out;
10194 	}
10195 	wq->type = LPFC_WQ;
10196 	wq->subtype = subtype;
10197 	wq->host_index = 0;
10198 	wq->hba_index = 0;
10199 
10200 	/* link the wq onto the parent cq child list */
10201 	list_add_tail(&wq->list, &cq->child_list);
10202 out:
10203 	mempool_free(mbox, phba->mbox_mem_pool);
10204 	return status;
10205 }
10206 
10207 /**
10208  * lpfc_rq_create - Create a Receive Queue on the HBA
10209  * @phba: HBA structure that indicates port to create a queue on.
10210  * @hrq: The queue structure to use to create the header receive queue.
10211  * @drq: The queue structure to use to create the data receive queue.
10212  * @cq: The completion queue to bind this work queue to.
10213  *
10214  * This function creates a receive buffer queue pair , as detailed in @hrq and
10215  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10216  * to the HBA.
10217  *
10218  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10219  * struct is used to get the entry count that is necessary to determine the
10220  * number of pages to use for this queue. The @cq is used to indicate which
10221  * completion queue to bind received buffers that are posted to these queues to.
10222  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10223  * receive queue pair. This function is asynchronous and will wait for the
10224  * mailbox command to finish before continuing.
10225  *
10226  * On success this function will return a zero. If unable to allocate enough
10227  * memory this function will return ENOMEM. If the queue create mailbox command
10228  * fails this function will return ENXIO.
10229  **/
10230 uint32_t
10231 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10232 	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10233 {
10234 	struct lpfc_mbx_rq_create *rq_create;
10235 	struct lpfc_dmabuf *dmabuf;
10236 	LPFC_MBOXQ_t *mbox;
10237 	int rc, length, status = 0;
10238 	uint32_t shdr_status, shdr_add_status;
10239 	union lpfc_sli4_cfg_shdr *shdr;
10240 	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10241 
10242 	if (!phba->sli4_hba.pc_sli4_params.supported)
10243 		hw_page_size = SLI4_PAGE_SIZE;
10244 
10245 	if (hrq->entry_count != drq->entry_count)
10246 		return -EINVAL;
10247 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10248 	if (!mbox)
10249 		return -ENOMEM;
10250 	length = (sizeof(struct lpfc_mbx_rq_create) -
10251 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10252 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10253 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10254 			 length, LPFC_SLI4_MBX_EMBED);
10255 	rq_create = &mbox->u.mqe.un.rq_create;
10256 	switch (hrq->entry_count) {
10257 	default:
10258 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10259 				"2535 Unsupported RQ count. (%d)\n",
10260 				hrq->entry_count);
10261 		if (hrq->entry_count < 512)
10262 			return -EINVAL;
10263 		/* otherwise default to smallest count (drop through) */
10264 	case 512:
10265 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10266 		       LPFC_RQ_RING_SIZE_512);
10267 		break;
10268 	case 1024:
10269 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10270 		       LPFC_RQ_RING_SIZE_1024);
10271 		break;
10272 	case 2048:
10273 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10274 		       LPFC_RQ_RING_SIZE_2048);
10275 		break;
10276 	case 4096:
10277 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10278 		       LPFC_RQ_RING_SIZE_4096);
10279 		break;
10280 	}
10281 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10282 	       cq->queue_id);
10283 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10284 	       hrq->page_count);
10285 	bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10286 	       LPFC_HDR_BUF_SIZE);
10287 	list_for_each_entry(dmabuf, &hrq->page_list, list) {
10288 		memset(dmabuf->virt, 0, hw_page_size);
10289 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10290 					putPaddrLow(dmabuf->phys);
10291 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10292 					putPaddrHigh(dmabuf->phys);
10293 	}
10294 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10295 	/* The IOCTL status is embedded in the mailbox subheader. */
10296 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10297 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10298 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10299 	if (shdr_status || shdr_add_status || rc) {
10300 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10301 				"2504 RQ_CREATE mailbox failed with "
10302 				"status x%x add_status x%x, mbx status x%x\n",
10303 				shdr_status, shdr_add_status, rc);
10304 		status = -ENXIO;
10305 		goto out;
10306 	}
10307 	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10308 	if (hrq->queue_id == 0xFFFF) {
10309 		status = -ENXIO;
10310 		goto out;
10311 	}
10312 	hrq->type = LPFC_HRQ;
10313 	hrq->subtype = subtype;
10314 	hrq->host_index = 0;
10315 	hrq->hba_index = 0;
10316 
10317 	/* now create the data queue */
10318 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10319 			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10320 			 length, LPFC_SLI4_MBX_EMBED);
10321 	switch (drq->entry_count) {
10322 	default:
10323 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10324 				"2536 Unsupported RQ count. (%d)\n",
10325 				drq->entry_count);
10326 		if (drq->entry_count < 512)
10327 			return -EINVAL;
10328 		/* otherwise default to smallest count (drop through) */
10329 	case 512:
10330 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10331 		       LPFC_RQ_RING_SIZE_512);
10332 		break;
10333 	case 1024:
10334 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10335 		       LPFC_RQ_RING_SIZE_1024);
10336 		break;
10337 	case 2048:
10338 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10339 		       LPFC_RQ_RING_SIZE_2048);
10340 		break;
10341 	case 4096:
10342 		bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10343 		       LPFC_RQ_RING_SIZE_4096);
10344 		break;
10345 	}
10346 	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10347 	       cq->queue_id);
10348 	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10349 	       drq->page_count);
10350 	bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10351 	       LPFC_DATA_BUF_SIZE);
10352 	list_for_each_entry(dmabuf, &drq->page_list, list) {
10353 		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10354 					putPaddrLow(dmabuf->phys);
10355 		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10356 					putPaddrHigh(dmabuf->phys);
10357 	}
10358 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10359 	/* The IOCTL status is embedded in the mailbox subheader. */
10360 	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10361 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10362 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10363 	if (shdr_status || shdr_add_status || rc) {
10364 		status = -ENXIO;
10365 		goto out;
10366 	}
10367 	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10368 	if (drq->queue_id == 0xFFFF) {
10369 		status = -ENXIO;
10370 		goto out;
10371 	}
10372 	drq->type = LPFC_DRQ;
10373 	drq->subtype = subtype;
10374 	drq->host_index = 0;
10375 	drq->hba_index = 0;
10376 
10377 	/* link the header and data RQs onto the parent cq child list */
10378 	list_add_tail(&hrq->list, &cq->child_list);
10379 	list_add_tail(&drq->list, &cq->child_list);
10380 
10381 out:
10382 	mempool_free(mbox, phba->mbox_mem_pool);
10383 	return status;
10384 }
10385 
10386 /**
10387  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10388  * @eq: The queue structure associated with the queue to destroy.
10389  *
10390  * This function destroys a queue, as detailed in @eq by sending an mailbox
10391  * command, specific to the type of queue, to the HBA.
10392  *
10393  * The @eq struct is used to get the queue ID of the queue to destroy.
10394  *
10395  * On success this function will return a zero. If the queue destroy mailbox
10396  * command fails this function will return ENXIO.
10397  **/
10398 uint32_t
10399 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10400 {
10401 	LPFC_MBOXQ_t *mbox;
10402 	int rc, length, status = 0;
10403 	uint32_t shdr_status, shdr_add_status;
10404 	union lpfc_sli4_cfg_shdr *shdr;
10405 
10406 	if (!eq)
10407 		return -ENODEV;
10408 	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10409 	if (!mbox)
10410 		return -ENOMEM;
10411 	length = (sizeof(struct lpfc_mbx_eq_destroy) -
10412 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10413 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10414 			 LPFC_MBOX_OPCODE_EQ_DESTROY,
10415 			 length, LPFC_SLI4_MBX_EMBED);
10416 	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10417 	       eq->queue_id);
10418 	mbox->vport = eq->phba->pport;
10419 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10420 
10421 	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10422 	/* The IOCTL status is embedded in the mailbox subheader. */
10423 	shdr = (union lpfc_sli4_cfg_shdr *)
10424 		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10425 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10426 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10427 	if (shdr_status || shdr_add_status || rc) {
10428 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10429 				"2505 EQ_DESTROY mailbox failed with "
10430 				"status x%x add_status x%x, mbx status x%x\n",
10431 				shdr_status, shdr_add_status, rc);
10432 		status = -ENXIO;
10433 	}
10434 
10435 	/* Remove eq from any list */
10436 	list_del_init(&eq->list);
10437 	mempool_free(mbox, eq->phba->mbox_mem_pool);
10438 	return status;
10439 }
10440 
10441 /**
10442  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10443  * @cq: The queue structure associated with the queue to destroy.
10444  *
10445  * This function destroys a queue, as detailed in @cq by sending an mailbox
10446  * command, specific to the type of queue, to the HBA.
10447  *
10448  * The @cq struct is used to get the queue ID of the queue to destroy.
10449  *
10450  * On success this function will return a zero. If the queue destroy mailbox
10451  * command fails this function will return ENXIO.
10452  **/
10453 uint32_t
10454 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10455 {
10456 	LPFC_MBOXQ_t *mbox;
10457 	int rc, length, status = 0;
10458 	uint32_t shdr_status, shdr_add_status;
10459 	union lpfc_sli4_cfg_shdr *shdr;
10460 
10461 	if (!cq)
10462 		return -ENODEV;
10463 	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10464 	if (!mbox)
10465 		return -ENOMEM;
10466 	length = (sizeof(struct lpfc_mbx_cq_destroy) -
10467 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10468 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10469 			 LPFC_MBOX_OPCODE_CQ_DESTROY,
10470 			 length, LPFC_SLI4_MBX_EMBED);
10471 	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10472 	       cq->queue_id);
10473 	mbox->vport = cq->phba->pport;
10474 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10475 	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10476 	/* The IOCTL status is embedded in the mailbox subheader. */
10477 	shdr = (union lpfc_sli4_cfg_shdr *)
10478 		&mbox->u.mqe.un.wq_create.header.cfg_shdr;
10479 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10480 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10481 	if (shdr_status || shdr_add_status || rc) {
10482 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10483 				"2506 CQ_DESTROY mailbox failed with "
10484 				"status x%x add_status x%x, mbx status x%x\n",
10485 				shdr_status, shdr_add_status, rc);
10486 		status = -ENXIO;
10487 	}
10488 	/* Remove cq from any list */
10489 	list_del_init(&cq->list);
10490 	mempool_free(mbox, cq->phba->mbox_mem_pool);
10491 	return status;
10492 }
10493 
10494 /**
10495  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10496  * @qm: The queue structure associated with the queue to destroy.
10497  *
10498  * This function destroys a queue, as detailed in @mq by sending an mailbox
10499  * command, specific to the type of queue, to the HBA.
10500  *
10501  * The @mq struct is used to get the queue ID of the queue to destroy.
10502  *
10503  * On success this function will return a zero. If the queue destroy mailbox
10504  * command fails this function will return ENXIO.
10505  **/
10506 uint32_t
10507 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10508 {
10509 	LPFC_MBOXQ_t *mbox;
10510 	int rc, length, status = 0;
10511 	uint32_t shdr_status, shdr_add_status;
10512 	union lpfc_sli4_cfg_shdr *shdr;
10513 
10514 	if (!mq)
10515 		return -ENODEV;
10516 	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10517 	if (!mbox)
10518 		return -ENOMEM;
10519 	length = (sizeof(struct lpfc_mbx_mq_destroy) -
10520 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10521 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10522 			 LPFC_MBOX_OPCODE_MQ_DESTROY,
10523 			 length, LPFC_SLI4_MBX_EMBED);
10524 	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10525 	       mq->queue_id);
10526 	mbox->vport = mq->phba->pport;
10527 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10528 	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10529 	/* The IOCTL status is embedded in the mailbox subheader. */
10530 	shdr = (union lpfc_sli4_cfg_shdr *)
10531 		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10532 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10533 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10534 	if (shdr_status || shdr_add_status || rc) {
10535 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10536 				"2507 MQ_DESTROY mailbox failed with "
10537 				"status x%x add_status x%x, mbx status x%x\n",
10538 				shdr_status, shdr_add_status, rc);
10539 		status = -ENXIO;
10540 	}
10541 	/* Remove mq from any list */
10542 	list_del_init(&mq->list);
10543 	mempool_free(mbox, mq->phba->mbox_mem_pool);
10544 	return status;
10545 }
10546 
10547 /**
10548  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10549  * @wq: The queue structure associated with the queue to destroy.
10550  *
10551  * This function destroys a queue, as detailed in @wq by sending an mailbox
10552  * command, specific to the type of queue, to the HBA.
10553  *
10554  * The @wq struct is used to get the queue ID of the queue to destroy.
10555  *
10556  * On success this function will return a zero. If the queue destroy mailbox
10557  * command fails this function will return ENXIO.
10558  **/
10559 uint32_t
10560 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10561 {
10562 	LPFC_MBOXQ_t *mbox;
10563 	int rc, length, status = 0;
10564 	uint32_t shdr_status, shdr_add_status;
10565 	union lpfc_sli4_cfg_shdr *shdr;
10566 
10567 	if (!wq)
10568 		return -ENODEV;
10569 	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10570 	if (!mbox)
10571 		return -ENOMEM;
10572 	length = (sizeof(struct lpfc_mbx_wq_destroy) -
10573 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10574 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10575 			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10576 			 length, LPFC_SLI4_MBX_EMBED);
10577 	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10578 	       wq->queue_id);
10579 	mbox->vport = wq->phba->pport;
10580 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10581 	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10582 	shdr = (union lpfc_sli4_cfg_shdr *)
10583 		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10584 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10585 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10586 	if (shdr_status || shdr_add_status || rc) {
10587 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10588 				"2508 WQ_DESTROY mailbox failed with "
10589 				"status x%x add_status x%x, mbx status x%x\n",
10590 				shdr_status, shdr_add_status, rc);
10591 		status = -ENXIO;
10592 	}
10593 	/* Remove wq from any list */
10594 	list_del_init(&wq->list);
10595 	mempool_free(mbox, wq->phba->mbox_mem_pool);
10596 	return status;
10597 }
10598 
10599 /**
10600  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10601  * @rq: The queue structure associated with the queue to destroy.
10602  *
10603  * This function destroys a queue, as detailed in @rq by sending an mailbox
10604  * command, specific to the type of queue, to the HBA.
10605  *
10606  * The @rq struct is used to get the queue ID of the queue to destroy.
10607  *
10608  * On success this function will return a zero. If the queue destroy mailbox
10609  * command fails this function will return ENXIO.
10610  **/
10611 uint32_t
10612 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10613 		struct lpfc_queue *drq)
10614 {
10615 	LPFC_MBOXQ_t *mbox;
10616 	int rc, length, status = 0;
10617 	uint32_t shdr_status, shdr_add_status;
10618 	union lpfc_sli4_cfg_shdr *shdr;
10619 
10620 	if (!hrq || !drq)
10621 		return -ENODEV;
10622 	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10623 	if (!mbox)
10624 		return -ENOMEM;
10625 	length = (sizeof(struct lpfc_mbx_rq_destroy) -
10626 		  sizeof(struct mbox_header));
10627 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10628 			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10629 			 length, LPFC_SLI4_MBX_EMBED);
10630 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10631 	       hrq->queue_id);
10632 	mbox->vport = hrq->phba->pport;
10633 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10634 	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10635 	/* The IOCTL status is embedded in the mailbox subheader. */
10636 	shdr = (union lpfc_sli4_cfg_shdr *)
10637 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10638 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10639 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10640 	if (shdr_status || shdr_add_status || rc) {
10641 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10642 				"2509 RQ_DESTROY mailbox failed with "
10643 				"status x%x add_status x%x, mbx status x%x\n",
10644 				shdr_status, shdr_add_status, rc);
10645 		if (rc != MBX_TIMEOUT)
10646 			mempool_free(mbox, hrq->phba->mbox_mem_pool);
10647 		return -ENXIO;
10648 	}
10649 	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10650 	       drq->queue_id);
10651 	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10652 	shdr = (union lpfc_sli4_cfg_shdr *)
10653 		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10654 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10655 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10656 	if (shdr_status || shdr_add_status || rc) {
10657 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10658 				"2510 RQ_DESTROY mailbox failed with "
10659 				"status x%x add_status x%x, mbx status x%x\n",
10660 				shdr_status, shdr_add_status, rc);
10661 		status = -ENXIO;
10662 	}
10663 	list_del_init(&hrq->list);
10664 	list_del_init(&drq->list);
10665 	mempool_free(mbox, hrq->phba->mbox_mem_pool);
10666 	return status;
10667 }
10668 
10669 /**
10670  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10671  * @phba: The virtual port for which this call being executed.
10672  * @pdma_phys_addr0: Physical address of the 1st SGL page.
10673  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10674  * @xritag: the xritag that ties this io to the SGL pages.
10675  *
10676  * This routine will post the sgl pages for the IO that has the xritag
10677  * that is in the iocbq structure. The xritag is assigned during iocbq
10678  * creation and persists for as long as the driver is loaded.
10679  * if the caller has fewer than 256 scatter gather segments to map then
10680  * pdma_phys_addr1 should be 0.
10681  * If the caller needs to map more than 256 scatter gather segment then
10682  * pdma_phys_addr1 should be a valid physical address.
10683  * physical address for SGLs must be 64 byte aligned.
10684  * If you are going to map 2 SGL's then the first one must have 256 entries
10685  * the second sgl can have between 1 and 256 entries.
10686  *
10687  * Return codes:
10688  * 	0 - Success
10689  * 	-ENXIO, -ENOMEM - Failure
10690  **/
10691 int
10692 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10693 		dma_addr_t pdma_phys_addr0,
10694 		dma_addr_t pdma_phys_addr1,
10695 		uint16_t xritag)
10696 {
10697 	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10698 	LPFC_MBOXQ_t *mbox;
10699 	int rc;
10700 	uint32_t shdr_status, shdr_add_status;
10701 	union lpfc_sli4_cfg_shdr *shdr;
10702 
10703 	if (xritag == NO_XRI) {
10704 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10705 				"0364 Invalid param:\n");
10706 		return -EINVAL;
10707 	}
10708 
10709 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10710 	if (!mbox)
10711 		return -ENOMEM;
10712 
10713 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10714 			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10715 			sizeof(struct lpfc_mbx_post_sgl_pages) -
10716 			sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10717 
10718 	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10719 				&mbox->u.mqe.un.post_sgl_pages;
10720 	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10721 	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10722 
10723 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=
10724 				cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10725 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10726 				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10727 
10728 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=
10729 				cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10730 	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10731 				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10732 	if (!phba->sli4_hba.intr_enable)
10733 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10734 	else
10735 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10736 	/* The IOCTL status is embedded in the mailbox subheader. */
10737 	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10738 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10739 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10740 	if (rc != MBX_TIMEOUT)
10741 		mempool_free(mbox, phba->mbox_mem_pool);
10742 	if (shdr_status || shdr_add_status || rc) {
10743 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10744 				"2511 POST_SGL mailbox failed with "
10745 				"status x%x add_status x%x, mbx status x%x\n",
10746 				shdr_status, shdr_add_status, rc);
10747 		rc = -ENXIO;
10748 	}
10749 	return 0;
10750 }
10751 /**
10752  * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10753  * @phba: The virtual port for which this call being executed.
10754  *
10755  * This routine will remove all of the sgl pages registered with the hba.
10756  *
10757  * Return codes:
10758  * 	0 - Success
10759  * 	-ENXIO, -ENOMEM - Failure
10760  **/
10761 int
10762 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10763 {
10764 	LPFC_MBOXQ_t *mbox;
10765 	int rc;
10766 	uint32_t shdr_status, shdr_add_status;
10767 	union lpfc_sli4_cfg_shdr *shdr;
10768 
10769 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10770 	if (!mbox)
10771 		return -ENOMEM;
10772 
10773 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10774 			LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10775 			LPFC_SLI4_MBX_EMBED);
10776 	if (!phba->sli4_hba.intr_enable)
10777 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10778 	else
10779 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10780 	/* The IOCTL status is embedded in the mailbox subheader. */
10781 	shdr = (union lpfc_sli4_cfg_shdr *)
10782 		&mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10783 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10784 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10785 	if (rc != MBX_TIMEOUT)
10786 		mempool_free(mbox, phba->mbox_mem_pool);
10787 	if (shdr_status || shdr_add_status || rc) {
10788 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10789 				"2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10790 				"status x%x add_status x%x, mbx status x%x\n",
10791 				shdr_status, shdr_add_status, rc);
10792 		rc = -ENXIO;
10793 	}
10794 	return rc;
10795 }
10796 
10797 /**
10798  * lpfc_sli4_next_xritag - Get an xritag for the io
10799  * @phba: Pointer to HBA context object.
10800  *
10801  * This function gets an xritag for the iocb. If there is no unused xritag
10802  * it will return 0xffff.
10803  * The function returns the allocated xritag if successful, else returns zero.
10804  * Zero is not a valid xritag.
10805  * The caller is not required to hold any lock.
10806  **/
10807 uint16_t
10808 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10809 {
10810 	uint16_t xritag;
10811 
10812 	spin_lock_irq(&phba->hbalock);
10813 	xritag = phba->sli4_hba.next_xri;
10814 	if ((xritag != (uint16_t) -1) && xritag <
10815 		(phba->sli4_hba.max_cfg_param.max_xri
10816 			+ phba->sli4_hba.max_cfg_param.xri_base)) {
10817 		phba->sli4_hba.next_xri++;
10818 		phba->sli4_hba.max_cfg_param.xri_used++;
10819 		spin_unlock_irq(&phba->hbalock);
10820 		return xritag;
10821 	}
10822 	spin_unlock_irq(&phba->hbalock);
10823 	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10824 			"2004 Failed to allocate XRI.last XRITAG is %d"
10825 			" Max XRI is %d, Used XRI is %d\n",
10826 			phba->sli4_hba.next_xri,
10827 			phba->sli4_hba.max_cfg_param.max_xri,
10828 			phba->sli4_hba.max_cfg_param.xri_used);
10829 	return -1;
10830 }
10831 
10832 /**
10833  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10834  * @phba: pointer to lpfc hba data structure.
10835  *
10836  * This routine is invoked to post a block of driver's sgl pages to the
10837  * HBA using non-embedded mailbox command. No Lock is held. This routine
10838  * is only called when the driver is loading and after all IO has been
10839  * stopped.
10840  **/
10841 int
10842 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10843 {
10844 	struct lpfc_sglq *sglq_entry;
10845 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10846 	struct sgl_page_pairs *sgl_pg_pairs;
10847 	void *viraddr;
10848 	LPFC_MBOXQ_t *mbox;
10849 	uint32_t reqlen, alloclen, pg_pairs;
10850 	uint32_t mbox_tmo;
10851 	uint16_t xritag_start = 0;
10852 	int els_xri_cnt, rc = 0;
10853 	uint32_t shdr_status, shdr_add_status;
10854 	union lpfc_sli4_cfg_shdr *shdr;
10855 
10856 	/* The number of sgls to be posted */
10857 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10858 
10859 	reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10860 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10861 	if (reqlen > SLI4_PAGE_SIZE) {
10862 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10863 				"2559 Block sgl registration required DMA "
10864 				"size (%d) great than a page\n", reqlen);
10865 		return -ENOMEM;
10866 	}
10867 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10868 	if (!mbox) {
10869 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10870 				"2560 Failed to allocate mbox cmd memory\n");
10871 		return -ENOMEM;
10872 	}
10873 
10874 	/* Allocate DMA memory and set up the non-embedded mailbox command */
10875 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10876 			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10877 			 LPFC_SLI4_MBX_NEMBED);
10878 
10879 	if (alloclen < reqlen) {
10880 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10881 				"0285 Allocated DMA memory size (%d) is "
10882 				"less than the requested DMA memory "
10883 				"size (%d)\n", alloclen, reqlen);
10884 		lpfc_sli4_mbox_cmd_free(phba, mbox);
10885 		return -ENOMEM;
10886 	}
10887 	/* Get the first SGE entry from the non-embedded DMA memory */
10888 	viraddr = mbox->sge_array->addr[0];
10889 
10890 	/* Set up the SGL pages in the non-embedded DMA pages */
10891 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10892 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
10893 
10894 	for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10895 		sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10896 		/* Set up the sge entry */
10897 		sgl_pg_pairs->sgl_pg0_addr_lo =
10898 				cpu_to_le32(putPaddrLow(sglq_entry->phys));
10899 		sgl_pg_pairs->sgl_pg0_addr_hi =
10900 				cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10901 		sgl_pg_pairs->sgl_pg1_addr_lo =
10902 				cpu_to_le32(putPaddrLow(0));
10903 		sgl_pg_pairs->sgl_pg1_addr_hi =
10904 				cpu_to_le32(putPaddrHigh(0));
10905 		/* Keep the first xritag on the list */
10906 		if (pg_pairs == 0)
10907 			xritag_start = sglq_entry->sli4_xritag;
10908 		sgl_pg_pairs++;
10909 	}
10910 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10911 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
10912 	/* Perform endian conversion if necessary */
10913 	sgl->word0 = cpu_to_le32(sgl->word0);
10914 
10915 	if (!phba->sli4_hba.intr_enable)
10916 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10917 	else {
10918 		mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10919 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10920 	}
10921 	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10922 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10923 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10924 	if (rc != MBX_TIMEOUT)
10925 		lpfc_sli4_mbox_cmd_free(phba, mbox);
10926 	if (shdr_status || shdr_add_status || rc) {
10927 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10928 				"2513 POST_SGL_BLOCK mailbox command failed "
10929 				"status x%x add_status x%x mbx status x%x\n",
10930 				shdr_status, shdr_add_status, rc);
10931 		rc = -ENXIO;
10932 	}
10933 	return rc;
10934 }
10935 
10936 /**
10937  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10938  * @phba: pointer to lpfc hba data structure.
10939  * @sblist: pointer to scsi buffer list.
10940  * @count: number of scsi buffers on the list.
10941  *
10942  * This routine is invoked to post a block of @count scsi sgl pages from a
10943  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10944  * No Lock is held.
10945  *
10946  **/
10947 int
10948 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10949 			      int cnt)
10950 {
10951 	struct lpfc_scsi_buf *psb;
10952 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10953 	struct sgl_page_pairs *sgl_pg_pairs;
10954 	void *viraddr;
10955 	LPFC_MBOXQ_t *mbox;
10956 	uint32_t reqlen, alloclen, pg_pairs;
10957 	uint32_t mbox_tmo;
10958 	uint16_t xritag_start = 0;
10959 	int rc = 0;
10960 	uint32_t shdr_status, shdr_add_status;
10961 	dma_addr_t pdma_phys_bpl1;
10962 	union lpfc_sli4_cfg_shdr *shdr;
10963 
10964 	/* Calculate the requested length of the dma memory */
10965 	reqlen = cnt * sizeof(struct sgl_page_pairs) +
10966 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10967 	if (reqlen > SLI4_PAGE_SIZE) {
10968 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10969 				"0217 Block sgl registration required DMA "
10970 				"size (%d) great than a page\n", reqlen);
10971 		return -ENOMEM;
10972 	}
10973 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10974 	if (!mbox) {
10975 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10976 				"0283 Failed to allocate mbox cmd memory\n");
10977 		return -ENOMEM;
10978 	}
10979 
10980 	/* Allocate DMA memory and set up the non-embedded mailbox command */
10981 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10982 				LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10983 				LPFC_SLI4_MBX_NEMBED);
10984 
10985 	if (alloclen < reqlen) {
10986 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10987 				"2561 Allocated DMA memory size (%d) is "
10988 				"less than the requested DMA memory "
10989 				"size (%d)\n", alloclen, reqlen);
10990 		lpfc_sli4_mbox_cmd_free(phba, mbox);
10991 		return -ENOMEM;
10992 	}
10993 	/* Get the first SGE entry from the non-embedded DMA memory */
10994 	viraddr = mbox->sge_array->addr[0];
10995 
10996 	/* Set up the SGL pages in the non-embedded DMA pages */
10997 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10998 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
10999 
11000 	pg_pairs = 0;
11001 	list_for_each_entry(psb, sblist, list) {
11002 		/* Set up the sge entry */
11003 		sgl_pg_pairs->sgl_pg0_addr_lo =
11004 			cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11005 		sgl_pg_pairs->sgl_pg0_addr_hi =
11006 			cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11007 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11008 			pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11009 		else
11010 			pdma_phys_bpl1 = 0;
11011 		sgl_pg_pairs->sgl_pg1_addr_lo =
11012 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11013 		sgl_pg_pairs->sgl_pg1_addr_hi =
11014 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11015 		/* Keep the first xritag on the list */
11016 		if (pg_pairs == 0)
11017 			xritag_start = psb->cur_iocbq.sli4_xritag;
11018 		sgl_pg_pairs++;
11019 		pg_pairs++;
11020 	}
11021 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11022 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11023 	/* Perform endian conversion if necessary */
11024 	sgl->word0 = cpu_to_le32(sgl->word0);
11025 
11026 	if (!phba->sli4_hba.intr_enable)
11027 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11028 	else {
11029 		mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11030 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11031 	}
11032 	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11033 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11034 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11035 	if (rc != MBX_TIMEOUT)
11036 		lpfc_sli4_mbox_cmd_free(phba, mbox);
11037 	if (shdr_status || shdr_add_status || rc) {
11038 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11039 				"2564 POST_SGL_BLOCK mailbox command failed "
11040 				"status x%x add_status x%x mbx status x%x\n",
11041 				shdr_status, shdr_add_status, rc);
11042 		rc = -ENXIO;
11043 	}
11044 	return rc;
11045 }
11046 
11047 /**
11048  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11049  * @phba: pointer to lpfc_hba struct that the frame was received on
11050  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11051  *
11052  * This function checks the fields in the @fc_hdr to see if the FC frame is a
11053  * valid type of frame that the LPFC driver will handle. This function will
11054  * return a zero if the frame is a valid frame or a non zero value when the
11055  * frame does not pass the check.
11056  **/
11057 static int
11058 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11059 {
11060 	char *rctl_names[] = FC_RCTL_NAMES_INIT;
11061 	char *type_names[] = FC_TYPE_NAMES_INIT;
11062 	struct fc_vft_header *fc_vft_hdr;
11063 
11064 	switch (fc_hdr->fh_r_ctl) {
11065 	case FC_RCTL_DD_UNCAT:		/* uncategorized information */
11066 	case FC_RCTL_DD_SOL_DATA:	/* solicited data */
11067 	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */
11068 	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */
11069 	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */
11070 	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */
11071 	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */
11072 	case FC_RCTL_DD_CMD_STATUS:	/* command status */
11073 	case FC_RCTL_ELS_REQ:	/* extended link services request */
11074 	case FC_RCTL_ELS_REP:	/* extended link services reply */
11075 	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */
11076 	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */
11077 	case FC_RCTL_BA_NOP:  	/* basic link service NOP */
11078 	case FC_RCTL_BA_ABTS: 	/* basic link service abort */
11079 	case FC_RCTL_BA_RMC: 	/* remove connection */
11080 	case FC_RCTL_BA_ACC:	/* basic accept */
11081 	case FC_RCTL_BA_RJT:	/* basic reject */
11082 	case FC_RCTL_BA_PRMT:
11083 	case FC_RCTL_ACK_1:	/* acknowledge_1 */
11084 	case FC_RCTL_ACK_0:	/* acknowledge_0 */
11085 	case FC_RCTL_P_RJT:	/* port reject */
11086 	case FC_RCTL_F_RJT:	/* fabric reject */
11087 	case FC_RCTL_P_BSY:	/* port busy */
11088 	case FC_RCTL_F_BSY:	/* fabric busy to data frame */
11089 	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */
11090 	case FC_RCTL_LCR:	/* link credit reset */
11091 	case FC_RCTL_END:	/* end */
11092 		break;
11093 	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */
11094 		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11095 		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11096 		return lpfc_fc_frame_check(phba, fc_hdr);
11097 	default:
11098 		goto drop;
11099 	}
11100 	switch (fc_hdr->fh_type) {
11101 	case FC_TYPE_BLS:
11102 	case FC_TYPE_ELS:
11103 	case FC_TYPE_FCP:
11104 	case FC_TYPE_CT:
11105 		break;
11106 	case FC_TYPE_IP:
11107 	case FC_TYPE_ILS:
11108 	default:
11109 		goto drop;
11110 	}
11111 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11112 			"2538 Received frame rctl:%s type:%s\n",
11113 			rctl_names[fc_hdr->fh_r_ctl],
11114 			type_names[fc_hdr->fh_type]);
11115 	return 0;
11116 drop:
11117 	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11118 			"2539 Dropped frame rctl:%s type:%s\n",
11119 			rctl_names[fc_hdr->fh_r_ctl],
11120 			type_names[fc_hdr->fh_type]);
11121 	return 1;
11122 }
11123 
11124 /**
11125  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11126  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11127  *
11128  * This function processes the FC header to retrieve the VFI from the VF
11129  * header, if one exists. This function will return the VFI if one exists
11130  * or 0 if no VSAN Header exists.
11131  **/
11132 static uint32_t
11133 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11134 {
11135 	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11136 
11137 	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11138 		return 0;
11139 	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11140 }
11141 
11142 /**
11143  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11144  * @phba: Pointer to the HBA structure to search for the vport on
11145  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11146  * @fcfi: The FC Fabric ID that the frame came from
11147  *
11148  * This function searches the @phba for a vport that matches the content of the
11149  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11150  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11151  * returns the matching vport pointer or NULL if unable to match frame to a
11152  * vport.
11153  **/
11154 static struct lpfc_vport *
11155 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11156 		       uint16_t fcfi)
11157 {
11158 	struct lpfc_vport **vports;
11159 	struct lpfc_vport *vport = NULL;
11160 	int i;
11161 	uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11162 			fc_hdr->fh_d_id[1] << 8 |
11163 			fc_hdr->fh_d_id[2]);
11164 
11165 	vports = lpfc_create_vport_work_array(phba);
11166 	if (vports != NULL)
11167 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11168 			if (phba->fcf.fcfi == fcfi &&
11169 			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11170 			    vports[i]->fc_myDID == did) {
11171 				vport = vports[i];
11172 				break;
11173 			}
11174 		}
11175 	lpfc_destroy_vport_work_array(phba, vports);
11176 	return vport;
11177 }
11178 
11179 /**
11180  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11181  * @vport: The vport to work on.
11182  *
11183  * This function updates the receive sequence time stamp for this vport. The
11184  * receive sequence time stamp indicates the time that the last frame of the
11185  * the sequence that has been idle for the longest amount of time was received.
11186  * the driver uses this time stamp to indicate if any received sequences have
11187  * timed out.
11188  **/
11189 void
11190 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11191 {
11192 	struct lpfc_dmabuf *h_buf;
11193 	struct hbq_dmabuf *dmabuf = NULL;
11194 
11195 	/* get the oldest sequence on the rcv list */
11196 	h_buf = list_get_first(&vport->rcv_buffer_list,
11197 			       struct lpfc_dmabuf, list);
11198 	if (!h_buf)
11199 		return;
11200 	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11201 	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11202 }
11203 
11204 /**
11205  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11206  * @vport: The vport that the received sequences were sent to.
11207  *
11208  * This function cleans up all outstanding received sequences. This is called
11209  * by the driver when a link event or user action invalidates all the received
11210  * sequences.
11211  **/
11212 void
11213 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11214 {
11215 	struct lpfc_dmabuf *h_buf, *hnext;
11216 	struct lpfc_dmabuf *d_buf, *dnext;
11217 	struct hbq_dmabuf *dmabuf = NULL;
11218 
11219 	/* start with the oldest sequence on the rcv list */
11220 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11221 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11222 		list_del_init(&dmabuf->hbuf.list);
11223 		list_for_each_entry_safe(d_buf, dnext,
11224 					 &dmabuf->dbuf.list, list) {
11225 			list_del_init(&d_buf->list);
11226 			lpfc_in_buf_free(vport->phba, d_buf);
11227 		}
11228 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11229 	}
11230 }
11231 
11232 /**
11233  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11234  * @vport: The vport that the received sequences were sent to.
11235  *
11236  * This function determines whether any received sequences have timed out by
11237  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11238  * indicates that there is at least one timed out sequence this routine will
11239  * go through the received sequences one at a time from most inactive to most
11240  * active to determine which ones need to be cleaned up. Once it has determined
11241  * that a sequence needs to be cleaned up it will simply free up the resources
11242  * without sending an abort.
11243  **/
11244 void
11245 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11246 {
11247 	struct lpfc_dmabuf *h_buf, *hnext;
11248 	struct lpfc_dmabuf *d_buf, *dnext;
11249 	struct hbq_dmabuf *dmabuf = NULL;
11250 	unsigned long timeout;
11251 	int abort_count = 0;
11252 
11253 	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11254 		   vport->rcv_buffer_time_stamp);
11255 	if (list_empty(&vport->rcv_buffer_list) ||
11256 	    time_before(jiffies, timeout))
11257 		return;
11258 	/* start with the oldest sequence on the rcv list */
11259 	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11260 		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11261 		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11262 			   dmabuf->time_stamp);
11263 		if (time_before(jiffies, timeout))
11264 			break;
11265 		abort_count++;
11266 		list_del_init(&dmabuf->hbuf.list);
11267 		list_for_each_entry_safe(d_buf, dnext,
11268 					 &dmabuf->dbuf.list, list) {
11269 			list_del_init(&d_buf->list);
11270 			lpfc_in_buf_free(vport->phba, d_buf);
11271 		}
11272 		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11273 	}
11274 	if (abort_count)
11275 		lpfc_update_rcv_time_stamp(vport);
11276 }
11277 
11278 /**
11279  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11280  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11281  *
11282  * This function searches through the existing incomplete sequences that have
11283  * been sent to this @vport. If the frame matches one of the incomplete
11284  * sequences then the dbuf in the @dmabuf is added to the list of frames that
11285  * make up that sequence. If no sequence is found that matches this frame then
11286  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11287  * This function returns a pointer to the first dmabuf in the sequence list that
11288  * the frame was linked to.
11289  **/
11290 static struct hbq_dmabuf *
11291 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11292 {
11293 	struct fc_frame_header *new_hdr;
11294 	struct fc_frame_header *temp_hdr;
11295 	struct lpfc_dmabuf *d_buf;
11296 	struct lpfc_dmabuf *h_buf;
11297 	struct hbq_dmabuf *seq_dmabuf = NULL;
11298 	struct hbq_dmabuf *temp_dmabuf = NULL;
11299 
11300 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
11301 	dmabuf->time_stamp = jiffies;
11302 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11303 	/* Use the hdr_buf to find the sequence that this frame belongs to */
11304 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11305 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
11306 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11307 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11308 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11309 			continue;
11310 		/* found a pending sequence that matches this frame */
11311 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11312 		break;
11313 	}
11314 	if (!seq_dmabuf) {
11315 		/*
11316 		 * This indicates first frame received for this sequence.
11317 		 * Queue the buffer on the vport's rcv_buffer_list.
11318 		 */
11319 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11320 		lpfc_update_rcv_time_stamp(vport);
11321 		return dmabuf;
11322 	}
11323 	temp_hdr = seq_dmabuf->hbuf.virt;
11324 	if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11325 		be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11326 		list_del_init(&seq_dmabuf->hbuf.list);
11327 		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11328 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11329 		lpfc_update_rcv_time_stamp(vport);
11330 		return dmabuf;
11331 	}
11332 	/* move this sequence to the tail to indicate a young sequence */
11333 	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11334 	seq_dmabuf->time_stamp = jiffies;
11335 	lpfc_update_rcv_time_stamp(vport);
11336 	if (list_empty(&seq_dmabuf->dbuf.list)) {
11337 		temp_hdr = dmabuf->hbuf.virt;
11338 		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11339 		return seq_dmabuf;
11340 	}
11341 	/* find the correct place in the sequence to insert this frame */
11342 	list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11343 		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11344 		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11345 		/*
11346 		 * If the frame's sequence count is greater than the frame on
11347 		 * the list then insert the frame right after this frame
11348 		 */
11349 		if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11350 			be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11351 			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11352 			return seq_dmabuf;
11353 		}
11354 	}
11355 	return NULL;
11356 }
11357 
11358 /**
11359  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11360  * @vport: pointer to a vitural port
11361  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11362  *
11363  * This function tries to abort from the partially assembed sequence, described
11364  * by the information from basic abbort @dmabuf. It checks to see whether such
11365  * partially assembled sequence held by the driver. If so, it shall free up all
11366  * the frames from the partially assembled sequence.
11367  *
11368  * Return
11369  * true  -- if there is matching partially assembled sequence present and all
11370  *          the frames freed with the sequence;
11371  * false -- if there is no matching partially assembled sequence present so
11372  *          nothing got aborted in the lower layer driver
11373  **/
11374 static bool
11375 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11376 			    struct hbq_dmabuf *dmabuf)
11377 {
11378 	struct fc_frame_header *new_hdr;
11379 	struct fc_frame_header *temp_hdr;
11380 	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11381 	struct hbq_dmabuf *seq_dmabuf = NULL;
11382 
11383 	/* Use the hdr_buf to find the sequence that matches this frame */
11384 	INIT_LIST_HEAD(&dmabuf->dbuf.list);
11385 	INIT_LIST_HEAD(&dmabuf->hbuf.list);
11386 	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11387 	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11388 		temp_hdr = (struct fc_frame_header *)h_buf->virt;
11389 		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11390 		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11391 		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11392 			continue;
11393 		/* found a pending sequence that matches this frame */
11394 		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11395 		break;
11396 	}
11397 
11398 	/* Free up all the frames from the partially assembled sequence */
11399 	if (seq_dmabuf) {
11400 		list_for_each_entry_safe(d_buf, n_buf,
11401 					 &seq_dmabuf->dbuf.list, list) {
11402 			list_del_init(&d_buf->list);
11403 			lpfc_in_buf_free(vport->phba, d_buf);
11404 		}
11405 		return true;
11406 	}
11407 	return false;
11408 }
11409 
11410 /**
11411  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11412  * @phba: Pointer to HBA context object.
11413  * @cmd_iocbq: pointer to the command iocbq structure.
11414  * @rsp_iocbq: pointer to the response iocbq structure.
11415  *
11416  * This function handles the sequence abort accept iocb command complete
11417  * event. It properly releases the memory allocated to the sequence abort
11418  * accept iocb.
11419  **/
11420 static void
11421 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11422 			     struct lpfc_iocbq *cmd_iocbq,
11423 			     struct lpfc_iocbq *rsp_iocbq)
11424 {
11425 	if (cmd_iocbq)
11426 		lpfc_sli_release_iocbq(phba, cmd_iocbq);
11427 }
11428 
11429 /**
11430  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11431  * @phba: Pointer to HBA context object.
11432  * @fc_hdr: pointer to a FC frame header.
11433  *
11434  * This function sends a basic accept to a previous unsol sequence abort
11435  * event after aborting the sequence handling.
11436  **/
11437 static void
11438 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11439 			struct fc_frame_header *fc_hdr)
11440 {
11441 	struct lpfc_iocbq *ctiocb = NULL;
11442 	struct lpfc_nodelist *ndlp;
11443 	uint16_t oxid, rxid;
11444 	uint32_t sid, fctl;
11445 	IOCB_t *icmd;
11446 
11447 	if (!lpfc_is_link_up(phba))
11448 		return;
11449 
11450 	sid = sli4_sid_from_fc_hdr(fc_hdr);
11451 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11452 	rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11453 
11454 	ndlp = lpfc_findnode_did(phba->pport, sid);
11455 	if (!ndlp) {
11456 		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11457 				"1268 Find ndlp returned NULL for oxid:x%x "
11458 				"SID:x%x\n", oxid, sid);
11459 		return;
11460 	}
11461 
11462 	/* Allocate buffer for acc iocb */
11463 	ctiocb = lpfc_sli_get_iocbq(phba);
11464 	if (!ctiocb)
11465 		return;
11466 
11467 	/* Extract the F_CTL field from FC_HDR */
11468 	fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11469 
11470 	icmd = &ctiocb->iocb;
11471 	icmd->un.xseq64.bdl.bdeSize = 0;
11472 	icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11473 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11474 	icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11475 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11476 
11477 	/* Fill in the rest of iocb fields */
11478 	icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11479 	icmd->ulpBdeCount = 0;
11480 	icmd->ulpLe = 1;
11481 	icmd->ulpClass = CLASS3;
11482 	icmd->ulpContext = ndlp->nlp_rpi;
11483 
11484 	ctiocb->iocb_cmpl = NULL;
11485 	ctiocb->vport = phba->pport;
11486 	ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11487 
11488 	if (fctl & FC_FC_EX_CTX) {
11489 		/* ABTS sent by responder to CT exchange, construction
11490 		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11491 		 * field and RX_ID from ABTS for RX_ID field.
11492 		 */
11493 		bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11494 		bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11495 		ctiocb->sli4_xritag = oxid;
11496 	} else {
11497 		/* ABTS sent by initiator to CT exchange, construction
11498 		 * of BA_ACC will need to allocate a new XRI as for the
11499 		 * XRI_TAG and RX_ID fields.
11500 		 */
11501 		bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11502 		bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11503 		ctiocb->sli4_xritag = NO_XRI;
11504 	}
11505 	bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11506 
11507 	/* Xmit CT abts accept on exchange <xid> */
11508 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11509 			"1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11510 			CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11511 	lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11512 }
11513 
11514 /**
11515  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11516  * @vport: Pointer to the vport on which this sequence was received
11517  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11518  *
11519  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11520  * receive sequence is only partially assembed by the driver, it shall abort
11521  * the partially assembled frames for the sequence. Otherwise, if the
11522  * unsolicited receive sequence has been completely assembled and passed to
11523  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11524  * unsolicited sequence has been aborted. After that, it will issue a basic
11525  * accept to accept the abort.
11526  **/
11527 void
11528 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11529 			     struct hbq_dmabuf *dmabuf)
11530 {
11531 	struct lpfc_hba *phba = vport->phba;
11532 	struct fc_frame_header fc_hdr;
11533 	uint32_t fctl;
11534 	bool abts_par;
11535 
11536 	/* Make a copy of fc_hdr before the dmabuf being released */
11537 	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11538 	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
11539 
11540 	if (fctl & FC_FC_EX_CTX) {
11541 		/*
11542 		 * ABTS sent by responder to exchange, just free the buffer
11543 		 */
11544 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
11545 	} else {
11546 		/*
11547 		 * ABTS sent by initiator to exchange, need to do cleanup
11548 		 */
11549 		/* Try to abort partially assembled seq */
11550 		abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11551 
11552 		/* Send abort to ULP if partially seq abort failed */
11553 		if (abts_par == false)
11554 			lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11555 		else
11556 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
11557 	}
11558 	/* Send basic accept (BA_ACC) to the abort requester */
11559 	lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11560 }
11561 
11562 /**
11563  * lpfc_seq_complete - Indicates if a sequence is complete
11564  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11565  *
11566  * This function checks the sequence, starting with the frame described by
11567  * @dmabuf, to see if all the frames associated with this sequence are present.
11568  * the frames associated with this sequence are linked to the @dmabuf using the
11569  * dbuf list. This function looks for two major things. 1) That the first frame
11570  * has a sequence count of zero. 2) There is a frame with last frame of sequence
11571  * set. 3) That there are no holes in the sequence count. The function will
11572  * return 1 when the sequence is complete, otherwise it will return 0.
11573  **/
11574 static int
11575 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11576 {
11577 	struct fc_frame_header *hdr;
11578 	struct lpfc_dmabuf *d_buf;
11579 	struct hbq_dmabuf *seq_dmabuf;
11580 	uint32_t fctl;
11581 	int seq_count = 0;
11582 
11583 	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11584 	/* make sure first fame of sequence has a sequence count of zero */
11585 	if (hdr->fh_seq_cnt != seq_count)
11586 		return 0;
11587 	fctl = (hdr->fh_f_ctl[0] << 16 |
11588 		hdr->fh_f_ctl[1] << 8 |
11589 		hdr->fh_f_ctl[2]);
11590 	/* If last frame of sequence we can return success. */
11591 	if (fctl & FC_FC_END_SEQ)
11592 		return 1;
11593 	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11594 		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11595 		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11596 		/* If there is a hole in the sequence count then fail. */
11597 		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
11598 			return 0;
11599 		fctl = (hdr->fh_f_ctl[0] << 16 |
11600 			hdr->fh_f_ctl[1] << 8 |
11601 			hdr->fh_f_ctl[2]);
11602 		/* If last frame of sequence we can return success. */
11603 		if (fctl & FC_FC_END_SEQ)
11604 			return 1;
11605 	}
11606 	return 0;
11607 }
11608 
11609 /**
11610  * lpfc_prep_seq - Prep sequence for ULP processing
11611  * @vport: Pointer to the vport on which this sequence was received
11612  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11613  *
11614  * This function takes a sequence, described by a list of frames, and creates
11615  * a list of iocbq structures to describe the sequence. This iocbq list will be
11616  * used to issue to the generic unsolicited sequence handler. This routine
11617  * returns a pointer to the first iocbq in the list. If the function is unable
11618  * to allocate an iocbq then it throw out the received frames that were not
11619  * able to be described and return a pointer to the first iocbq. If unable to
11620  * allocate any iocbqs (including the first) this function will return NULL.
11621  **/
11622 static struct lpfc_iocbq *
11623 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11624 {
11625 	struct lpfc_dmabuf *d_buf, *n_buf;
11626 	struct lpfc_iocbq *first_iocbq, *iocbq;
11627 	struct fc_frame_header *fc_hdr;
11628 	uint32_t sid;
11629 	struct ulp_bde64 *pbde;
11630 
11631 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11632 	/* remove from receive buffer list */
11633 	list_del_init(&seq_dmabuf->hbuf.list);
11634 	lpfc_update_rcv_time_stamp(vport);
11635 	/* get the Remote Port's SID */
11636 	sid = sli4_sid_from_fc_hdr(fc_hdr);
11637 	/* Get an iocbq struct to fill in. */
11638 	first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11639 	if (first_iocbq) {
11640 		/* Initialize the first IOCB. */
11641 		first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
11642 		first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11643 		first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11644 		first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11645 		first_iocbq->iocb.unsli3.rcvsli3.vpi =
11646 					vport->vpi + vport->phba->vpi_base;
11647 		/* put the first buffer into the first IOCBq */
11648 		first_iocbq->context2 = &seq_dmabuf->dbuf;
11649 		first_iocbq->context3 = NULL;
11650 		first_iocbq->iocb.ulpBdeCount = 1;
11651 		first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11652 							LPFC_DATA_BUF_SIZE;
11653 		first_iocbq->iocb.un.rcvels.remoteID = sid;
11654 		first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11655 				bf_get(lpfc_rcqe_length,
11656 				       &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11657 	}
11658 	iocbq = first_iocbq;
11659 	/*
11660 	 * Each IOCBq can have two Buffers assigned, so go through the list
11661 	 * of buffers for this sequence and save two buffers in each IOCBq
11662 	 */
11663 	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11664 		if (!iocbq) {
11665 			lpfc_in_buf_free(vport->phba, d_buf);
11666 			continue;
11667 		}
11668 		if (!iocbq->context3) {
11669 			iocbq->context3 = d_buf;
11670 			iocbq->iocb.ulpBdeCount++;
11671 			pbde = (struct ulp_bde64 *)
11672 					&iocbq->iocb.unsli3.sli3Words[4];
11673 			pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
11674 			first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11675 				bf_get(lpfc_rcqe_length,
11676 				       &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11677 		} else {
11678 			iocbq = lpfc_sli_get_iocbq(vport->phba);
11679 			if (!iocbq) {
11680 				if (first_iocbq) {
11681 					first_iocbq->iocb.ulpStatus =
11682 							IOSTAT_FCP_RSP_ERROR;
11683 					first_iocbq->iocb.un.ulpWord[4] =
11684 							IOERR_NO_RESOURCES;
11685 				}
11686 				lpfc_in_buf_free(vport->phba, d_buf);
11687 				continue;
11688 			}
11689 			iocbq->context2 = d_buf;
11690 			iocbq->context3 = NULL;
11691 			iocbq->iocb.ulpBdeCount = 1;
11692 			iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11693 							LPFC_DATA_BUF_SIZE;
11694 			first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11695 				bf_get(lpfc_rcqe_length,
11696 				       &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11697 			iocbq->iocb.un.rcvels.remoteID = sid;
11698 			list_add_tail(&iocbq->list, &first_iocbq->list);
11699 		}
11700 	}
11701 	return first_iocbq;
11702 }
11703 
11704 static void
11705 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11706 			  struct hbq_dmabuf *seq_dmabuf)
11707 {
11708 	struct fc_frame_header *fc_hdr;
11709 	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11710 	struct lpfc_hba *phba = vport->phba;
11711 
11712 	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11713 	iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11714 	if (!iocbq) {
11715 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11716 				"2707 Ring %d handler: Failed to allocate "
11717 				"iocb Rctl x%x Type x%x received\n",
11718 				LPFC_ELS_RING,
11719 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11720 		return;
11721 	}
11722 	if (!lpfc_complete_unsol_iocb(phba,
11723 				      &phba->sli.ring[LPFC_ELS_RING],
11724 				      iocbq, fc_hdr->fh_r_ctl,
11725 				      fc_hdr->fh_type))
11726 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11727 				"2540 Ring %d handler: unexpected Rctl "
11728 				"x%x Type x%x received\n",
11729 				LPFC_ELS_RING,
11730 				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11731 
11732 	/* Free iocb created in lpfc_prep_seq */
11733 	list_for_each_entry_safe(curr_iocb, next_iocb,
11734 		&iocbq->list, list) {
11735 		list_del_init(&curr_iocb->list);
11736 		lpfc_sli_release_iocbq(phba, curr_iocb);
11737 	}
11738 	lpfc_sli_release_iocbq(phba, iocbq);
11739 }
11740 
11741 /**
11742  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11743  * @phba: Pointer to HBA context object.
11744  *
11745  * This function is called with no lock held. This function processes all
11746  * the received buffers and gives it to upper layers when a received buffer
11747  * indicates that it is the final frame in the sequence. The interrupt
11748  * service routine processes received buffers at interrupt contexts and adds
11749  * received dma buffers to the rb_pend_list queue and signals the worker thread.
11750  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11751  * appropriate receive function when the final frame in a sequence is received.
11752  **/
11753 void
11754 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11755 				 struct hbq_dmabuf *dmabuf)
11756 {
11757 	struct hbq_dmabuf *seq_dmabuf;
11758 	struct fc_frame_header *fc_hdr;
11759 	struct lpfc_vport *vport;
11760 	uint32_t fcfi;
11761 
11762 	/* Process each received buffer */
11763 	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11764 	/* check to see if this a valid type of frame */
11765 	if (lpfc_fc_frame_check(phba, fc_hdr)) {
11766 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
11767 		return;
11768 	}
11769 	fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11770 	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11771 	if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
11772 		/* throw out the frame */
11773 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
11774 		return;
11775 	}
11776 	/* Handle the basic abort sequence (BA_ABTS) event */
11777 	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11778 		lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11779 		return;
11780 	}
11781 
11782 	/* Link this frame */
11783 	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11784 	if (!seq_dmabuf) {
11785 		/* unable to add frame to vport - throw it out */
11786 		lpfc_in_buf_free(phba, &dmabuf->dbuf);
11787 		return;
11788 	}
11789 	/* If not last frame in sequence continue processing frames. */
11790 	if (!lpfc_seq_complete(seq_dmabuf))
11791 		return;
11792 
11793 	/* Send the complete sequence to the upper layer protocol */
11794 	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
11795 }
11796 
11797 /**
11798  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11799  * @phba: pointer to lpfc hba data structure.
11800  *
11801  * This routine is invoked to post rpi header templates to the
11802  * HBA consistent with the SLI-4 interface spec.  This routine
11803  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11804  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11805  *
11806  * This routine does not require any locks.  It's usage is expected
11807  * to be driver load or reset recovery when the driver is
11808  * sequential.
11809  *
11810  * Return codes
11811  * 	0 - successful
11812  *      EIO - The mailbox failed to complete successfully.
11813  * 	When this error occurs, the driver is not guaranteed
11814  *	to have any rpi regions posted to the device and
11815  *	must either attempt to repost the regions or take a
11816  *	fatal error.
11817  **/
11818 int
11819 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11820 {
11821 	struct lpfc_rpi_hdr *rpi_page;
11822 	uint32_t rc = 0;
11823 
11824 	/* Post all rpi memory regions to the port. */
11825 	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11826 		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11827 		if (rc != MBX_SUCCESS) {
11828 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11829 					"2008 Error %d posting all rpi "
11830 					"headers\n", rc);
11831 			rc = -EIO;
11832 			break;
11833 		}
11834 	}
11835 
11836 	return rc;
11837 }
11838 
11839 /**
11840  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11841  * @phba: pointer to lpfc hba data structure.
11842  * @rpi_page:  pointer to the rpi memory region.
11843  *
11844  * This routine is invoked to post a single rpi header to the
11845  * HBA consistent with the SLI-4 interface spec.  This memory region
11846  * maps up to 64 rpi context regions.
11847  *
11848  * Return codes
11849  * 	0 - successful
11850  * 	ENOMEM - No available memory
11851  *      EIO - The mailbox failed to complete successfully.
11852  **/
11853 int
11854 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11855 {
11856 	LPFC_MBOXQ_t *mboxq;
11857 	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11858 	uint32_t rc = 0;
11859 	uint32_t mbox_tmo;
11860 	uint32_t shdr_status, shdr_add_status;
11861 	union lpfc_sli4_cfg_shdr *shdr;
11862 
11863 	/* The port is notified of the header region via a mailbox command. */
11864 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11865 	if (!mboxq) {
11866 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11867 				"2001 Unable to allocate memory for issuing "
11868 				"SLI_CONFIG_SPECIAL mailbox command\n");
11869 		return -ENOMEM;
11870 	}
11871 
11872 	/* Post all rpi memory regions to the port. */
11873 	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11874 	mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11875 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11876 			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11877 			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11878 			 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11879 	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11880 	       hdr_tmpl, rpi_page->page_count);
11881 	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11882 	       rpi_page->start_rpi);
11883 	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11884 	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
11885 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11886 	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11887 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11888 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11889 	if (rc != MBX_TIMEOUT)
11890 		mempool_free(mboxq, phba->mbox_mem_pool);
11891 	if (shdr_status || shdr_add_status || rc) {
11892 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11893 				"2514 POST_RPI_HDR mailbox failed with "
11894 				"status x%x add_status x%x, mbx status x%x\n",
11895 				shdr_status, shdr_add_status, rc);
11896 		rc = -ENXIO;
11897 	}
11898 	return rc;
11899 }
11900 
11901 /**
11902  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11903  * @phba: pointer to lpfc hba data structure.
11904  *
11905  * This routine is invoked to post rpi header templates to the
11906  * HBA consistent with the SLI-4 interface spec.  This routine
11907  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11908  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11909  *
11910  * Returns
11911  * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
11912  * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.
11913  **/
11914 int
11915 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11916 {
11917 	int rpi;
11918 	uint16_t max_rpi, rpi_base, rpi_limit;
11919 	uint16_t rpi_remaining;
11920 	struct lpfc_rpi_hdr *rpi_hdr;
11921 
11922 	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11923 	rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11924 	rpi_limit = phba->sli4_hba.next_rpi;
11925 
11926 	/*
11927 	 * The valid rpi range is not guaranteed to be zero-based.  Start
11928 	 * the search at the rpi_base as reported by the port.
11929 	 */
11930 	spin_lock_irq(&phba->hbalock);
11931 	rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11932 	if (rpi >= rpi_limit || rpi < rpi_base)
11933 		rpi = LPFC_RPI_ALLOC_ERROR;
11934 	else {
11935 		set_bit(rpi, phba->sli4_hba.rpi_bmask);
11936 		phba->sli4_hba.max_cfg_param.rpi_used++;
11937 		phba->sli4_hba.rpi_count++;
11938 	}
11939 
11940 	/*
11941 	 * Don't try to allocate more rpi header regions if the device limit
11942 	 * on available rpis max has been exhausted.
11943 	 */
11944 	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11945 	    (phba->sli4_hba.rpi_count >= max_rpi)) {
11946 		spin_unlock_irq(&phba->hbalock);
11947 		return rpi;
11948 	}
11949 
11950 	/*
11951 	 * If the driver is running low on rpi resources, allocate another
11952 	 * page now.  Note that the next_rpi value is used because
11953 	 * it represents how many are actually in use whereas max_rpi notes
11954 	 * how many are supported max by the device.
11955 	 */
11956 	rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11957 		phba->sli4_hba.rpi_count;
11958 	spin_unlock_irq(&phba->hbalock);
11959 	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11960 		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11961 		if (!rpi_hdr) {
11962 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11963 					"2002 Error Could not grow rpi "
11964 					"count\n");
11965 		} else {
11966 			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11967 		}
11968 	}
11969 
11970 	return rpi;
11971 }
11972 
11973 /**
11974  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11975  * @phba: pointer to lpfc hba data structure.
11976  *
11977  * This routine is invoked to release an rpi to the pool of
11978  * available rpis maintained by the driver.
11979  **/
11980 void
11981 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11982 {
11983 	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
11984 		phba->sli4_hba.rpi_count--;
11985 		phba->sli4_hba.max_cfg_param.rpi_used--;
11986 	}
11987 }
11988 
11989 /**
11990  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11991  * @phba: pointer to lpfc hba data structure.
11992  *
11993  * This routine is invoked to release an rpi to the pool of
11994  * available rpis maintained by the driver.
11995  **/
11996 void
11997 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11998 {
11999 	spin_lock_irq(&phba->hbalock);
12000 	__lpfc_sli4_free_rpi(phba, rpi);
12001 	spin_unlock_irq(&phba->hbalock);
12002 }
12003 
12004 /**
12005  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12006  * @phba: pointer to lpfc hba data structure.
12007  *
12008  * This routine is invoked to remove the memory region that
12009  * provided rpi via a bitmask.
12010  **/
12011 void
12012 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12013 {
12014 	kfree(phba->sli4_hba.rpi_bmask);
12015 }
12016 
12017 /**
12018  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12019  * @phba: pointer to lpfc hba data structure.
12020  *
12021  * This routine is invoked to remove the memory region that
12022  * provided rpi via a bitmask.
12023  **/
12024 int
12025 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12026 {
12027 	LPFC_MBOXQ_t *mboxq;
12028 	struct lpfc_hba *phba = ndlp->phba;
12029 	int rc;
12030 
12031 	/* The port is notified of the header region via a mailbox command. */
12032 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12033 	if (!mboxq)
12034 		return -ENOMEM;
12035 
12036 	/* Post all rpi memory regions to the port. */
12037 	lpfc_resume_rpi(mboxq, ndlp);
12038 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12039 	if (rc == MBX_NOT_FINISHED) {
12040 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12041 				"2010 Resume RPI Mailbox failed "
12042 				"status %d, mbxStatus x%x\n", rc,
12043 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12044 		mempool_free(mboxq, phba->mbox_mem_pool);
12045 		return -EIO;
12046 	}
12047 	return 0;
12048 }
12049 
12050 /**
12051  * lpfc_sli4_init_vpi - Initialize a vpi with the port
12052  * @phba: pointer to lpfc hba data structure.
12053  * @vpi: vpi value to activate with the port.
12054  *
12055  * This routine is invoked to activate a vpi with the
12056  * port when the host intends to use vports with a
12057  * nonzero vpi.
12058  *
12059  * Returns:
12060  *    0 success
12061  *    -Evalue otherwise
12062  **/
12063 int
12064 lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12065 {
12066 	LPFC_MBOXQ_t *mboxq;
12067 	int rc = 0;
12068 	int retval = MBX_SUCCESS;
12069 	uint32_t mbox_tmo;
12070 
12071 	if (vpi == 0)
12072 		return -EINVAL;
12073 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12074 	if (!mboxq)
12075 		return -ENOMEM;
12076 	lpfc_init_vpi(phba, mboxq, vpi);
12077 	mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12078 	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12079 	if (rc != MBX_SUCCESS) {
12080 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12081 				"2022 INIT VPI Mailbox failed "
12082 				"status %d, mbxStatus x%x\n", rc,
12083 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12084 		retval = -EIO;
12085 	}
12086 	if (rc != MBX_TIMEOUT)
12087 		mempool_free(mboxq, phba->mbox_mem_pool);
12088 
12089 	return retval;
12090 }
12091 
12092 /**
12093  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12094  * @phba: pointer to lpfc hba data structure.
12095  * @mboxq: Pointer to mailbox object.
12096  *
12097  * This routine is invoked to manually add a single FCF record. The caller
12098  * must pass a completely initialized FCF_Record.  This routine takes
12099  * care of the nonembedded mailbox operations.
12100  **/
12101 static void
12102 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12103 {
12104 	void *virt_addr;
12105 	union lpfc_sli4_cfg_shdr *shdr;
12106 	uint32_t shdr_status, shdr_add_status;
12107 
12108 	virt_addr = mboxq->sge_array->addr[0];
12109 	/* The IOCTL status is embedded in the mailbox subheader. */
12110 	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12111 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12112 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12113 
12114 	if ((shdr_status || shdr_add_status) &&
12115 		(shdr_status != STATUS_FCF_IN_USE))
12116 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12117 			"2558 ADD_FCF_RECORD mailbox failed with "
12118 			"status x%x add_status x%x\n",
12119 			shdr_status, shdr_add_status);
12120 
12121 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
12122 }
12123 
12124 /**
12125  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12126  * @phba: pointer to lpfc hba data structure.
12127  * @fcf_record:  pointer to the initialized fcf record to add.
12128  *
12129  * This routine is invoked to manually add a single FCF record. The caller
12130  * must pass a completely initialized FCF_Record.  This routine takes
12131  * care of the nonembedded mailbox operations.
12132  **/
12133 int
12134 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12135 {
12136 	int rc = 0;
12137 	LPFC_MBOXQ_t *mboxq;
12138 	uint8_t *bytep;
12139 	void *virt_addr;
12140 	dma_addr_t phys_addr;
12141 	struct lpfc_mbx_sge sge;
12142 	uint32_t alloc_len, req_len;
12143 	uint32_t fcfindex;
12144 
12145 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12146 	if (!mboxq) {
12147 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12148 			"2009 Failed to allocate mbox for ADD_FCF cmd\n");
12149 		return -ENOMEM;
12150 	}
12151 
12152 	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12153 		  sizeof(uint32_t);
12154 
12155 	/* Allocate DMA memory and set up the non-embedded mailbox command */
12156 	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12157 				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12158 				     req_len, LPFC_SLI4_MBX_NEMBED);
12159 	if (alloc_len < req_len) {
12160 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12161 			"2523 Allocated DMA memory size (x%x) is "
12162 			"less than the requested DMA memory "
12163 			"size (x%x)\n", alloc_len, req_len);
12164 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
12165 		return -ENOMEM;
12166 	}
12167 
12168 	/*
12169 	 * Get the first SGE entry from the non-embedded DMA memory.  This
12170 	 * routine only uses a single SGE.
12171 	 */
12172 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12173 	phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
12174 	virt_addr = mboxq->sge_array->addr[0];
12175 	/*
12176 	 * Configure the FCF record for FCFI 0.  This is the driver's
12177 	 * hardcoded default and gets used in nonFIP mode.
12178 	 */
12179 	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12180 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12181 	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12182 
12183 	/*
12184 	 * Copy the fcf_index and the FCF Record Data. The data starts after
12185 	 * the FCoE header plus word10. The data copy needs to be endian
12186 	 * correct.
12187 	 */
12188 	bytep += sizeof(uint32_t);
12189 	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12190 	mboxq->vport = phba->pport;
12191 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12192 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12193 	if (rc == MBX_NOT_FINISHED) {
12194 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12195 			"2515 ADD_FCF_RECORD mailbox failed with "
12196 			"status 0x%x\n", rc);
12197 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
12198 		rc = -EIO;
12199 	} else
12200 		rc = 0;
12201 
12202 	return rc;
12203 }
12204 
12205 /**
12206  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12207  * @phba: pointer to lpfc hba data structure.
12208  * @fcf_record:  pointer to the fcf record to write the default data.
12209  * @fcf_index: FCF table entry index.
12210  *
12211  * This routine is invoked to build the driver's default FCF record.  The
12212  * values used are hardcoded.  This routine handles memory initialization.
12213  *
12214  **/
12215 void
12216 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12217 				struct fcf_record *fcf_record,
12218 				uint16_t fcf_index)
12219 {
12220 	memset(fcf_record, 0, sizeof(struct fcf_record));
12221 	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12222 	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12223 	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12224 	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12225 	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12226 	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12227 	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12228 	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12229 	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12230 	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12231 	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12232 	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12233 	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
12234 	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
12235 	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12236 	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12237 		LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12238 	/* Set the VLAN bit map */
12239 	if (phba->valid_vlan) {
12240 		fcf_record->vlan_bitmap[phba->vlan_id / 8]
12241 			= 1 << (phba->vlan_id % 8);
12242 	}
12243 }
12244 
12245 /**
12246  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12247  * @phba: pointer to lpfc hba data structure.
12248  * @fcf_index: FCF table entry offset.
12249  *
12250  * This routine is invoked to scan the entire FCF table by reading FCF
12251  * record and processing it one at a time starting from the @fcf_index
12252  * for initial FCF discovery or fast FCF failover rediscovery.
12253  *
12254  * Return 0 if the mailbox command is submitted sucessfully, none 0
12255  * otherwise.
12256  **/
12257 int
12258 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12259 {
12260 	int rc = 0, error;
12261 	LPFC_MBOXQ_t *mboxq;
12262 
12263 	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
12264 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12265 	if (!mboxq) {
12266 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12267 				"2000 Failed to allocate mbox for "
12268 				"READ_FCF cmd\n");
12269 		error = -ENOMEM;
12270 		goto fail_fcf_scan;
12271 	}
12272 	/* Construct the read FCF record mailbox command */
12273 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12274 	if (rc) {
12275 		error = -EINVAL;
12276 		goto fail_fcf_scan;
12277 	}
12278 	/* Issue the mailbox command asynchronously */
12279 	mboxq->vport = phba->pport;
12280 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
12281 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12282 	if (rc == MBX_NOT_FINISHED)
12283 		error = -EIO;
12284 	else {
12285 		spin_lock_irq(&phba->hbalock);
12286 		phba->hba_flag |= FCF_DISC_INPROGRESS;
12287 		spin_unlock_irq(&phba->hbalock);
12288 		/* Reset FCF round robin index bmask for new scan */
12289 		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
12290 			memset(phba->fcf.fcf_rr_bmask, 0,
12291 			       sizeof(*phba->fcf.fcf_rr_bmask));
12292 			phba->fcf.eligible_fcf_cnt = 0;
12293 		}
12294 		error = 0;
12295 	}
12296 fail_fcf_scan:
12297 	if (error) {
12298 		if (mboxq)
12299 			lpfc_sli4_mbox_cmd_free(phba, mboxq);
12300 		/* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12301 		spin_lock_irq(&phba->hbalock);
12302 		phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12303 		spin_unlock_irq(&phba->hbalock);
12304 	}
12305 	return error;
12306 }
12307 
12308 /**
12309  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12310  * @phba: pointer to lpfc hba data structure.
12311  * @fcf_index: FCF table entry offset.
12312  *
12313  * This routine is invoked to read an FCF record indicated by @fcf_index
12314  * and to use it for FLOGI round robin FCF failover.
12315  *
12316  * Return 0 if the mailbox command is submitted sucessfully, none 0
12317  * otherwise.
12318  **/
12319 int
12320 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12321 {
12322 	int rc = 0, error;
12323 	LPFC_MBOXQ_t *mboxq;
12324 
12325 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12326 	if (!mboxq) {
12327 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12328 				"2763 Failed to allocate mbox for "
12329 				"READ_FCF cmd\n");
12330 		error = -ENOMEM;
12331 		goto fail_fcf_read;
12332 	}
12333 	/* Construct the read FCF record mailbox command */
12334 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12335 	if (rc) {
12336 		error = -EINVAL;
12337 		goto fail_fcf_read;
12338 	}
12339 	/* Issue the mailbox command asynchronously */
12340 	mboxq->vport = phba->pport;
12341 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12342 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12343 	if (rc == MBX_NOT_FINISHED)
12344 		error = -EIO;
12345 	else
12346 		error = 0;
12347 
12348 fail_fcf_read:
12349 	if (error && mboxq)
12350 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
12351 	return error;
12352 }
12353 
12354 /**
12355  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12356  * @phba: pointer to lpfc hba data structure.
12357  * @fcf_index: FCF table entry offset.
12358  *
12359  * This routine is invoked to read an FCF record indicated by @fcf_index to
12360  * determine whether it's eligible for FLOGI round robin failover list.
12361  *
12362  * Return 0 if the mailbox command is submitted sucessfully, none 0
12363  * otherwise.
12364  **/
12365 int
12366 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12367 {
12368 	int rc = 0, error;
12369 	LPFC_MBOXQ_t *mboxq;
12370 
12371 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12372 	if (!mboxq) {
12373 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12374 				"2758 Failed to allocate mbox for "
12375 				"READ_FCF cmd\n");
12376 				error = -ENOMEM;
12377 				goto fail_fcf_read;
12378 	}
12379 	/* Construct the read FCF record mailbox command */
12380 	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12381 	if (rc) {
12382 		error = -EINVAL;
12383 		goto fail_fcf_read;
12384 	}
12385 	/* Issue the mailbox command asynchronously */
12386 	mboxq->vport = phba->pport;
12387 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12388 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12389 	if (rc == MBX_NOT_FINISHED)
12390 		error = -EIO;
12391 	else
12392 		error = 0;
12393 
12394 fail_fcf_read:
12395 	if (error && mboxq)
12396 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
12397 	return error;
12398 }
12399 
12400 /**
12401  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12402  * @phba: pointer to lpfc hba data structure.
12403  *
12404  * This routine is to get the next eligible FCF record index in a round
12405  * robin fashion. If the next eligible FCF record index equals to the
12406  * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12407  * shall be returned, otherwise, the next eligible FCF record's index
12408  * shall be returned.
12409  **/
12410 uint16_t
12411 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12412 {
12413 	uint16_t next_fcf_index;
12414 
12415 	/* Search from the currently registered FCF index */
12416 	next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12417 				       LPFC_SLI4_FCF_TBL_INDX_MAX,
12418 				       phba->fcf.current_rec.fcf_indx);
12419 	/* Wrap around condition on phba->fcf.fcf_rr_bmask */
12420 	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12421 		next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12422 					       LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12423 	/* Round robin failover stop condition */
12424 	if ((next_fcf_index == phba->fcf.fcf_rr_init_indx) ||
12425 		(next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX))
12426 		return LPFC_FCOE_FCF_NEXT_NONE;
12427 
12428 	return next_fcf_index;
12429 }
12430 
12431 /**
12432  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12433  * @phba: pointer to lpfc hba data structure.
12434  *
12435  * This routine sets the FCF record index in to the eligible bmask for
12436  * round robin failover search. It checks to make sure that the index
12437  * does not go beyond the range of the driver allocated bmask dimension
12438  * before setting the bit.
12439  *
12440  * Returns 0 if the index bit successfully set, otherwise, it returns
12441  * -EINVAL.
12442  **/
12443 int
12444 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12445 {
12446 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12447 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12448 				"2610 HBA FCF index reached driver's "
12449 				"book keeping dimension: fcf_index:%d, "
12450 				"driver_bmask_max:%d\n",
12451 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12452 		return -EINVAL;
12453 	}
12454 	/* Set the eligible FCF record index bmask */
12455 	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12456 
12457 	return 0;
12458 }
12459 
12460 /**
12461  * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12462  * @phba: pointer to lpfc hba data structure.
12463  *
12464  * This routine clears the FCF record index from the eligible bmask for
12465  * round robin failover search. It checks to make sure that the index
12466  * does not go beyond the range of the driver allocated bmask dimension
12467  * before clearing the bit.
12468  **/
12469 void
12470 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12471 {
12472 	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12473 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12474 				"2762 HBA FCF index goes beyond driver's "
12475 				"book keeping dimension: fcf_index:%d, "
12476 				"driver_bmask_max:%d\n",
12477 				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12478 		return;
12479 	}
12480 	/* Clear the eligible FCF record index bmask */
12481 	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12482 }
12483 
12484 /**
12485  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12486  * @phba: pointer to lpfc hba data structure.
12487  *
12488  * This routine is the completion routine for the rediscover FCF table mailbox
12489  * command. If the mailbox command returned failure, it will try to stop the
12490  * FCF rediscover wait timer.
12491  **/
12492 void
12493 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12494 {
12495 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12496 	uint32_t shdr_status, shdr_add_status;
12497 
12498 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12499 
12500 	shdr_status = bf_get(lpfc_mbox_hdr_status,
12501 			     &redisc_fcf->header.cfg_shdr.response);
12502 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12503 			     &redisc_fcf->header.cfg_shdr.response);
12504 	if (shdr_status || shdr_add_status) {
12505 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12506 				"2746 Requesting for FCF rediscovery failed "
12507 				"status x%x add_status x%x\n",
12508 				shdr_status, shdr_add_status);
12509 		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
12510 			spin_lock_irq(&phba->hbalock);
12511 			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
12512 			spin_unlock_irq(&phba->hbalock);
12513 			/*
12514 			 * CVL event triggered FCF rediscover request failed,
12515 			 * last resort to re-try current registered FCF entry.
12516 			 */
12517 			lpfc_retry_pport_discovery(phba);
12518 		} else {
12519 			spin_lock_irq(&phba->hbalock);
12520 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
12521 			spin_unlock_irq(&phba->hbalock);
12522 			/*
12523 			 * DEAD FCF event triggered FCF rediscover request
12524 			 * failed, last resort to fail over as a link down
12525 			 * to FCF registration.
12526 			 */
12527 			lpfc_sli4_fcf_dead_failthrough(phba);
12528 		}
12529 	} else {
12530 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12531 				"2775 Start FCF rediscovery quiescent period "
12532 				"wait timer before scaning FCF table\n");
12533 		/*
12534 		 * Start FCF rediscovery wait timer for pending FCF
12535 		 * before rescan FCF record table.
12536 		 */
12537 		lpfc_fcf_redisc_wait_start_timer(phba);
12538 	}
12539 
12540 	mempool_free(mbox, phba->mbox_mem_pool);
12541 }
12542 
12543 /**
12544  * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12545  * @phba: pointer to lpfc hba data structure.
12546  *
12547  * This routine is invoked to request for rediscovery of the entire FCF table
12548  * by the port.
12549  **/
12550 int
12551 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12552 {
12553 	LPFC_MBOXQ_t *mbox;
12554 	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12555 	int rc, length;
12556 
12557 	/* Cancel retry delay timers to all vports before FCF rediscover */
12558 	lpfc_cancel_all_vport_retry_delay_timer(phba);
12559 
12560 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12561 	if (!mbox) {
12562 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12563 				"2745 Failed to allocate mbox for "
12564 				"requesting FCF rediscover.\n");
12565 		return -ENOMEM;
12566 	}
12567 
12568 	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12569 		  sizeof(struct lpfc_sli4_cfg_mhdr));
12570 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12571 			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12572 			 length, LPFC_SLI4_MBX_EMBED);
12573 
12574 	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12575 	/* Set count to 0 for invalidating the entire FCF database */
12576 	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12577 
12578 	/* Issue the mailbox command asynchronously */
12579 	mbox->vport = phba->pport;
12580 	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12581 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12582 
12583 	if (rc == MBX_NOT_FINISHED) {
12584 		mempool_free(mbox, phba->mbox_mem_pool);
12585 		return -EIO;
12586 	}
12587 	return 0;
12588 }
12589 
12590 /**
12591  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12592  * @phba: pointer to lpfc hba data structure.
12593  *
12594  * This function is the failover routine as a last resort to the FCF DEAD
12595  * event when driver failed to perform fast FCF failover.
12596  **/
12597 void
12598 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12599 {
12600 	uint32_t link_state;
12601 
12602 	/*
12603 	 * Last resort as FCF DEAD event failover will treat this as
12604 	 * a link down, but save the link state because we don't want
12605 	 * it to be changed to Link Down unless it is already down.
12606 	 */
12607 	link_state = phba->link_state;
12608 	lpfc_linkdown(phba);
12609 	phba->link_state = link_state;
12610 
12611 	/* Unregister FCF if no devices connected to it */
12612 	lpfc_unregister_unused_fcf(phba);
12613 }
12614 
12615 /**
12616  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12617  * @phba: pointer to lpfc hba data structure.
12618  *
12619  * This function read region 23 and parse TLV for port status to
12620  * decide if the user disaled the port. If the TLV indicates the
12621  * port is disabled, the hba_flag is set accordingly.
12622  **/
12623 void
12624 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12625 {
12626 	LPFC_MBOXQ_t *pmb = NULL;
12627 	MAILBOX_t *mb;
12628 	uint8_t *rgn23_data = NULL;
12629 	uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12630 	int rc;
12631 
12632 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12633 	if (!pmb) {
12634 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12635 			"2600 lpfc_sli_read_serdes_param failed to"
12636 			" allocate mailbox memory\n");
12637 		goto out;
12638 	}
12639 	mb = &pmb->u.mb;
12640 
12641 	/* Get adapter Region 23 data */
12642 	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12643 	if (!rgn23_data)
12644 		goto out;
12645 
12646 	do {
12647 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12648 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12649 
12650 		if (rc != MBX_SUCCESS) {
12651 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12652 				"2601 lpfc_sli_read_link_ste failed to"
12653 				" read config region 23 rc 0x%x Status 0x%x\n",
12654 				rc, mb->mbxStatus);
12655 			mb->un.varDmp.word_cnt = 0;
12656 		}
12657 		/*
12658 		 * dump mem may return a zero when finished or we got a
12659 		 * mailbox error, either way we are done.
12660 		 */
12661 		if (mb->un.varDmp.word_cnt == 0)
12662 			break;
12663 		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12664 			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12665 
12666 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12667 			rgn23_data + offset,
12668 			mb->un.varDmp.word_cnt);
12669 		offset += mb->un.varDmp.word_cnt;
12670 	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12671 
12672 	data_size = offset;
12673 	offset = 0;
12674 
12675 	if (!data_size)
12676 		goto out;
12677 
12678 	/* Check the region signature first */
12679 	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12680 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12681 			"2619 Config region 23 has bad signature\n");
12682 			goto out;
12683 	}
12684 	offset += 4;
12685 
12686 	/* Check the data structure version */
12687 	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12688 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12689 			"2620 Config region 23 has bad version\n");
12690 		goto out;
12691 	}
12692 	offset += 4;
12693 
12694 	/* Parse TLV entries in the region */
12695 	while (offset < data_size) {
12696 		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12697 			break;
12698 		/*
12699 		 * If the TLV is not driver specific TLV or driver id is
12700 		 * not linux driver id, skip the record.
12701 		 */
12702 		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12703 		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12704 		    (rgn23_data[offset + 3] != 0)) {
12705 			offset += rgn23_data[offset + 1] * 4 + 4;
12706 			continue;
12707 		}
12708 
12709 		/* Driver found a driver specific TLV in the config region */
12710 		sub_tlv_len = rgn23_data[offset + 1] * 4;
12711 		offset += 4;
12712 		tlv_offset = 0;
12713 
12714 		/*
12715 		 * Search for configured port state sub-TLV.
12716 		 */
12717 		while ((offset < data_size) &&
12718 			(tlv_offset < sub_tlv_len)) {
12719 			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12720 				offset += 4;
12721 				tlv_offset += 4;
12722 				break;
12723 			}
12724 			if (rgn23_data[offset] != PORT_STE_TYPE) {
12725 				offset += rgn23_data[offset + 1] * 4 + 4;
12726 				tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12727 				continue;
12728 			}
12729 
12730 			/* This HBA contains PORT_STE configured */
12731 			if (!rgn23_data[offset + 2])
12732 				phba->hba_flag |= LINK_DISABLED;
12733 
12734 			goto out;
12735 		}
12736 	}
12737 out:
12738 	if (pmb)
12739 		mempool_free(pmb, phba->mbox_mem_pool);
12740 	kfree(rgn23_data);
12741 	return;
12742 }
12743 
12744 /**
12745  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12746  * @vport: pointer to vport data structure.
12747  *
12748  * This function iterate through the mailboxq and clean up all REG_LOGIN
12749  * and REG_VPI mailbox commands associated with the vport. This function
12750  * is called when driver want to restart discovery of the vport due to
12751  * a Clear Virtual Link event.
12752  **/
12753 void
12754 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12755 {
12756 	struct lpfc_hba *phba = vport->phba;
12757 	LPFC_MBOXQ_t *mb, *nextmb;
12758 	struct lpfc_dmabuf *mp;
12759 	struct lpfc_nodelist *ndlp;
12760 
12761 	spin_lock_irq(&phba->hbalock);
12762 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12763 		if (mb->vport != vport)
12764 			continue;
12765 
12766 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12767 			(mb->u.mb.mbxCommand != MBX_REG_VPI))
12768 			continue;
12769 
12770 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12771 			if (phba->sli_rev == LPFC_SLI_REV4)
12772 				__lpfc_sli4_free_rpi(phba,
12773 						mb->u.mb.un.varRegLogin.rpi);
12774 			mp = (struct lpfc_dmabuf *) (mb->context1);
12775 			if (mp) {
12776 				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
12777 				kfree(mp);
12778 			}
12779 			ndlp = (struct lpfc_nodelist *) mb->context2;
12780 			if (ndlp) {
12781 				lpfc_nlp_put(ndlp);
12782 				mb->context2 = NULL;
12783 			}
12784 		}
12785 		list_del(&mb->list);
12786 		mempool_free(mb, phba->mbox_mem_pool);
12787 	}
12788 	mb = phba->sli.mbox_active;
12789 	if (mb && (mb->vport == vport)) {
12790 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12791 			(mb->u.mb.mbxCommand == MBX_REG_VPI))
12792 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12793 		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12794 			ndlp = (struct lpfc_nodelist *) mb->context2;
12795 			if (ndlp) {
12796 				lpfc_nlp_put(ndlp);
12797 				mb->context2 = NULL;
12798 			}
12799 			/* Unregister the RPI when mailbox complete */
12800 			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12801 		}
12802 	}
12803 	spin_unlock_irq(&phba->hbalock);
12804 }
12805 
12806 /**
12807  * lpfc_drain_txq - Drain the txq
12808  * @phba: Pointer to HBA context object.
12809  *
12810  * This function attempt to submit IOCBs on the txq
12811  * to the adapter.  For SLI4 adapters, the txq contains
12812  * ELS IOCBs that have been deferred because the there
12813  * are no SGLs.  This congestion can occur with large
12814  * vport counts during node discovery.
12815  **/
12816 
12817 uint32_t
12818 lpfc_drain_txq(struct lpfc_hba *phba)
12819 {
12820 	LIST_HEAD(completions);
12821 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12822 	struct lpfc_iocbq *piocbq = 0;
12823 	unsigned long iflags = 0;
12824 	char *fail_msg = NULL;
12825 	struct lpfc_sglq *sglq;
12826 	union lpfc_wqe wqe;
12827 
12828 	spin_lock_irqsave(&phba->hbalock, iflags);
12829 	if (pring->txq_cnt > pring->txq_max)
12830 		pring->txq_max = pring->txq_cnt;
12831 
12832 	spin_unlock_irqrestore(&phba->hbalock, iflags);
12833 
12834 	while (pring->txq_cnt) {
12835 		spin_lock_irqsave(&phba->hbalock, iflags);
12836 
12837 		sglq = __lpfc_sli_get_sglq(phba);
12838 		if (!sglq) {
12839 			spin_unlock_irqrestore(&phba->hbalock, iflags);
12840 			break;
12841 		} else {
12842 			piocbq = lpfc_sli_ringtx_get(phba, pring);
12843 			if (!piocbq) {
12844 				/* The txq_cnt out of sync. This should
12845 				 * never happen
12846 				 */
12847 				sglq = __lpfc_clear_active_sglq(phba,
12848 						 sglq->sli4_xritag);
12849 				spin_unlock_irqrestore(&phba->hbalock, iflags);
12850 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12851 					"2823 txq empty and txq_cnt is %d\n ",
12852 					pring->txq_cnt);
12853 				break;
12854 			}
12855 		}
12856 
12857 		/* The xri and iocb resources secured,
12858 		 * attempt to issue request
12859 		 */
12860 		piocbq->sli4_xritag = sglq->sli4_xritag;
12861 		if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
12862 			fail_msg = "to convert bpl to sgl";
12863 		else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
12864 			fail_msg = "to convert iocb to wqe";
12865 		else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
12866 			fail_msg = " - Wq is full";
12867 		else
12868 			lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
12869 
12870 		if (fail_msg) {
12871 			/* Failed means we can't issue and need to cancel */
12872 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12873 					"2822 IOCB failed %s iotag 0x%x "
12874 					"xri 0x%x\n",
12875 					fail_msg,
12876 					piocbq->iotag, piocbq->sli4_xritag);
12877 			list_add_tail(&piocbq->list, &completions);
12878 		}
12879 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12880 	}
12881 
12882 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12883 	phba->pport->work_port_events &= ~WORKER_SERVICE_TXQ;
12884 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12885 
12886 	/* Cancel all the IOCBs that cannot be issued */
12887 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12888 				IOERR_SLI_ABORTED);
12889 
12890 	return pring->txq_cnt;
12891 }
12892