[SCSI] lpfc 8.3.27: Changed worst case mailbox timeout
[linux-block.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
792581de 4 * Copyright (C) 2004-2011 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
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. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
5a0e3ad6 26#include <linux/slab.h>
dea3101e 27
91886523 28#include <scsi/scsi.h>
dea3101e 29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
f888ba3c 32#include <scsi/scsi_transport_fc.h>
da0436e9 33#include <scsi/fc/fc_fs.h>
0d878419 34#include <linux/aer.h>
dea3101e 35
da0436e9 36#include "lpfc_hw4.h"
dea3101e 37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
da0436e9 39#include "lpfc_sli4.h"
ea2151b4 40#include "lpfc_nl.h"
dea3101e 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"
858c9f6c 47#include "lpfc_debugfs.h"
04c68496 48#include "lpfc_vport.h"
dea3101e 49
50/* There are only four IOCB completion types. */
51typedef 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
4f774513
JS
58
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
63 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
6669f9bb
JS
66static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
0558056c
JS
68static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
70
4f774513
JS
71static IOCB_t *
72lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73{
74 return &iocbq->iocb;
75}
76
77/**
78 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79 * @q: The Work Queue to operate on.
80 * @wqe: The work Queue Entry to put on the Work queue.
81 *
82 * This routine will copy the contents of @wqe to the next available entry on
83 * the @q. This function will then ring the Work Queue Doorbell to signal the
84 * HBA to start processing the Work Queue Entry. This function returns 0 if
85 * successful. If no entries are available on @q then this function will return
86 * -ENOMEM.
87 * The caller is expected to hold the hbalock when calling this routine.
88 **/
89static uint32_t
90lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91{
92 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
93 struct lpfc_register doorbell;
94 uint32_t host_index;
95
96 /* If the host has not yet processed the next entry then we are done */
97 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
98 return -ENOMEM;
99 /* set consumption flag every once in a while */
100 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
f0d9bccc 101 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
fedd3b7b
JS
102 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
103 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513
JS
104 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
105
106 /* Update the host index before invoking device */
107 host_index = q->host_index;
108 q->host_index = ((q->host_index + 1) % q->entry_count);
109
110 /* Ring Doorbell */
111 doorbell.word0 = 0;
112 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
113 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
114 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
115 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
116 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
117
118 return 0;
119}
120
121/**
122 * lpfc_sli4_wq_release - Updates internal hba index for WQ
123 * @q: The Work Queue to operate on.
124 * @index: The index to advance the hba index to.
125 *
126 * This routine will update the HBA index of a queue to reflect consumption of
127 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
128 * an entry the host calls this function to update the queue's internal
129 * pointers. This routine returns the number of entries that were consumed by
130 * the HBA.
131 **/
132static uint32_t
133lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
134{
135 uint32_t released = 0;
136
137 if (q->hba_index == index)
138 return 0;
139 do {
140 q->hba_index = ((q->hba_index + 1) % q->entry_count);
141 released++;
142 } while (q->hba_index != index);
143 return released;
144}
145
146/**
147 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
148 * @q: The Mailbox Queue to operate on.
149 * @wqe: The Mailbox Queue Entry to put on the Work queue.
150 *
151 * This routine will copy the contents of @mqe to the next available entry on
152 * the @q. This function will then ring the Work Queue Doorbell to signal the
153 * HBA to start processing the Work Queue Entry. This function returns 0 if
154 * successful. If no entries are available on @q then this function will return
155 * -ENOMEM.
156 * The caller is expected to hold the hbalock when calling this routine.
157 **/
158static uint32_t
159lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
160{
161 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
162 struct lpfc_register doorbell;
163 uint32_t host_index;
164
165 /* If the host has not yet processed the next entry then we are done */
166 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
167 return -ENOMEM;
168 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
169 /* Save off the mailbox pointer for completion */
170 q->phba->mbox = (MAILBOX_t *)temp_mqe;
171
172 /* Update the host index before invoking device */
173 host_index = q->host_index;
174 q->host_index = ((q->host_index + 1) % q->entry_count);
175
176 /* Ring Doorbell */
177 doorbell.word0 = 0;
178 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
179 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
180 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
181 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
182 return 0;
183}
184
185/**
186 * lpfc_sli4_mq_release - Updates internal hba index for MQ
187 * @q: The Mailbox Queue to operate on.
188 *
189 * This routine will update the HBA index of a queue to reflect consumption of
190 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
191 * an entry the host calls this function to update the queue's internal
192 * pointers. This routine returns the number of entries that were consumed by
193 * the HBA.
194 **/
195static uint32_t
196lpfc_sli4_mq_release(struct lpfc_queue *q)
197{
198 /* Clear the mailbox pointer for completion */
199 q->phba->mbox = NULL;
200 q->hba_index = ((q->hba_index + 1) % q->entry_count);
201 return 1;
202}
203
204/**
205 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
206 * @q: The Event Queue to get the first valid EQE from
207 *
208 * This routine will get the first valid Event Queue Entry from @q, update
209 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
210 * the Queue (no more work to do), or the Queue is full of EQEs that have been
211 * processed, but not popped back to the HBA then this routine will return NULL.
212 **/
213static struct lpfc_eqe *
214lpfc_sli4_eq_get(struct lpfc_queue *q)
215{
216 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
217
218 /* If the next EQE is not valid then we are done */
cb5172ea 219 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
220 return NULL;
221 /* If the host has not yet processed the next entry then we are done */
222 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
223 return NULL;
224
225 q->hba_index = ((q->hba_index + 1) % q->entry_count);
226 return eqe;
227}
228
229/**
230 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
231 * @q: The Event Queue that the host has completed processing for.
232 * @arm: Indicates whether the host wants to arms this CQ.
233 *
234 * This routine will mark all Event Queue Entries on @q, from the last
235 * known completed entry to the last entry that was processed, as completed
236 * by clearing the valid bit for each completion queue entry. Then it will
237 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
238 * The internal host index in the @q will be updated by this routine to indicate
239 * that the host has finished processing the entries. The @arm parameter
240 * indicates that the queue should be rearmed when ringing the doorbell.
241 *
242 * This function will return the number of EQEs that were popped.
243 **/
244uint32_t
245lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
246{
247 uint32_t released = 0;
248 struct lpfc_eqe *temp_eqe;
249 struct lpfc_register doorbell;
250
251 /* while there are valid entries */
252 while (q->hba_index != q->host_index) {
253 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 254 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
255 released++;
256 q->host_index = ((q->host_index + 1) % q->entry_count);
257 }
258 if (unlikely(released == 0 && !arm))
259 return 0;
260
261 /* ring doorbell for number popped */
262 doorbell.word0 = 0;
263 if (arm) {
264 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
265 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
266 }
267 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
268 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
269 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
270 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
271 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
272 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
273 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
274 return released;
275}
276
277/**
278 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
279 * @q: The Completion Queue to get the first valid CQE from
280 *
281 * This routine will get the first valid Completion Queue Entry from @q, update
282 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
283 * the Queue (no more work to do), or the Queue is full of CQEs that have been
284 * processed, but not popped back to the HBA then this routine will return NULL.
285 **/
286static struct lpfc_cqe *
287lpfc_sli4_cq_get(struct lpfc_queue *q)
288{
289 struct lpfc_cqe *cqe;
290
291 /* If the next CQE is not valid then we are done */
cb5172ea 292 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
293 return NULL;
294 /* If the host has not yet processed the next entry then we are done */
295 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
296 return NULL;
297
298 cqe = q->qe[q->hba_index].cqe;
299 q->hba_index = ((q->hba_index + 1) % q->entry_count);
300 return cqe;
301}
302
303/**
304 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
305 * @q: The Completion Queue that the host has completed processing for.
306 * @arm: Indicates whether the host wants to arms this CQ.
307 *
308 * This routine will mark all Completion queue entries on @q, from the last
309 * known completed entry to the last entry that was processed, as completed
310 * by clearing the valid bit for each completion queue entry. Then it will
311 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
312 * The internal host index in the @q will be updated by this routine to indicate
313 * that the host has finished processing the entries. The @arm parameter
314 * indicates that the queue should be rearmed when ringing the doorbell.
315 *
316 * This function will return the number of CQEs that were released.
317 **/
318uint32_t
319lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
320{
321 uint32_t released = 0;
322 struct lpfc_cqe *temp_qe;
323 struct lpfc_register doorbell;
324
325 /* while there are valid entries */
326 while (q->hba_index != q->host_index) {
327 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 328 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
329 released++;
330 q->host_index = ((q->host_index + 1) % q->entry_count);
331 }
332 if (unlikely(released == 0 && !arm))
333 return 0;
334
335 /* ring doorbell for number popped */
336 doorbell.word0 = 0;
337 if (arm)
338 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
340 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
341 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
342 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
343 return released;
344}
345
346/**
347 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
348 * @q: The Header Receive Queue to operate on.
349 * @wqe: The Receive Queue Entry to put on the Receive queue.
350 *
351 * This routine will copy the contents of @wqe to the next available entry on
352 * the @q. This function will then ring the Receive Queue Doorbell to signal the
353 * HBA to start processing the Receive Queue Entry. This function returns the
354 * index that the rqe was copied to if successful. If no entries are available
355 * on @q then this function will return -ENOMEM.
356 * The caller is expected to hold the hbalock when calling this routine.
357 **/
358static int
359lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
360 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
361{
362 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
363 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
364 struct lpfc_register doorbell;
365 int put_index = hq->host_index;
366
367 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
368 return -EINVAL;
369 if (hq->host_index != dq->host_index)
370 return -EINVAL;
371 /* If the host has not yet processed the next entry then we are done */
372 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
373 return -EBUSY;
374 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
375 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
376
377 /* Update the host index to point to the next slot */
378 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
379 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
380
381 /* Ring The Header Receive Queue Doorbell */
73d91e50 382 if (!(hq->host_index % hq->entry_repost)) {
4f774513
JS
383 doorbell.word0 = 0;
384 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
73d91e50 385 hq->entry_repost);
4f774513
JS
386 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
387 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
388 }
389 return put_index;
390}
391
392/**
393 * lpfc_sli4_rq_release - Updates internal hba index for RQ
394 * @q: The Header Receive Queue to operate on.
395 *
396 * This routine will update the HBA index of a queue to reflect consumption of
397 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
398 * consumed an entry the host calls this function to update the queue's
399 * internal pointers. This routine returns the number of entries that were
400 * consumed by the HBA.
401 **/
402static uint32_t
403lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
404{
405 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
406 return 0;
407 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
408 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
409 return 1;
410}
411
e59058c4 412/**
3621a710 413 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
414 * @phba: Pointer to HBA context object.
415 * @pring: Pointer to driver SLI ring object.
416 *
417 * This function returns pointer to next command iocb entry
418 * in the command ring. The caller must hold hbalock to prevent
419 * other threads consume the next command iocb.
420 * SLI-2/SLI-3 provide different sized iocbs.
421 **/
ed957684
JS
422static inline IOCB_t *
423lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
424{
425 return (IOCB_t *) (((char *) pring->cmdringaddr) +
426 pring->cmdidx * phba->iocb_cmd_size);
427}
428
e59058c4 429/**
3621a710 430 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
431 * @phba: Pointer to HBA context object.
432 * @pring: Pointer to driver SLI ring object.
433 *
434 * This function returns pointer to next response iocb entry
435 * in the response ring. The caller must hold hbalock to make sure
436 * that no other thread consume the next response iocb.
437 * SLI-2/SLI-3 provide different sized iocbs.
438 **/
ed957684
JS
439static inline IOCB_t *
440lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
441{
442 return (IOCB_t *) (((char *) pring->rspringaddr) +
443 pring->rspidx * phba->iocb_rsp_size);
444}
445
e59058c4 446/**
3621a710 447 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
448 * @phba: Pointer to HBA context object.
449 *
450 * This function is called with hbalock held. This function
451 * allocates a new driver iocb object from the iocb pool. If the
452 * allocation is successful, it returns pointer to the newly
453 * allocated iocb object else it returns NULL.
454 **/
2e0fef85
JS
455static struct lpfc_iocbq *
456__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
457{
458 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
459 struct lpfc_iocbq * iocbq = NULL;
460
461 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
462 if (iocbq)
463 phba->iocb_cnt++;
464 if (phba->iocb_cnt > phba->iocb_max)
465 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
466 return iocbq;
467}
468
da0436e9
JS
469/**
470 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
471 * @phba: Pointer to HBA context object.
472 * @xritag: XRI value.
473 *
474 * This function clears the sglq pointer from the array of acive
475 * sglq's. The xritag that is passed in is used to index into the
476 * array. Before the xritag can be used it needs to be adjusted
477 * by subtracting the xribase.
478 *
479 * Returns sglq ponter = success, NULL = Failure.
480 **/
481static struct lpfc_sglq *
482__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
483{
da0436e9 484 struct lpfc_sglq *sglq;
6d368e53
JS
485
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
487 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
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 **/
0f65ff68 503struct lpfc_sglq *
da0436e9
JS
504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
da0436e9 506 struct lpfc_sglq *sglq;
6d368e53
JS
507
508 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
509 return sglq;
510}
511
19ca7609
JS
512/**
513 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
514 * @phba: Pointer to HBA context object.
515 * @ndlp: nodelist pointer for this target.
516 * @xritag: xri used in this exchange.
517 * @rxid: Remote Exchange ID.
518 * @send_rrq: Flag used to determine if we should send rrq els cmd.
519 *
520 * This function is called with hbalock held.
521 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
522 * rrq struct and adds it to the active_rrq_list.
523 *
524 * returns 0 for rrq slot for this xri
525 * < 0 Were not able to get rrq mem or invalid parameter.
526 **/
527static int
528__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
529 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
530{
19ca7609
JS
531 struct lpfc_node_rrq *rrq;
532 int empty;
1151e3ec
JS
533 uint32_t did = 0;
534
535
536 if (!ndlp)
537 return -EINVAL;
538
539 if (!phba->cfg_enable_rrq)
540 return -EINVAL;
541
542 if (phba->pport->load_flag & FC_UNLOADING) {
543 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
544 goto out;
545 }
546 did = ndlp->nlp_DID;
19ca7609
JS
547
548 /*
549 * set the active bit even if there is no mem available.
550 */
1151e3ec
JS
551 if (NLP_CHK_FREE_REQ(ndlp))
552 goto out;
553
554 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
555 goto out;
556
6d368e53 557 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
1151e3ec
JS
558 goto out;
559
19ca7609
JS
560 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
561 if (rrq) {
562 rrq->send_rrq = send_rrq;
7851fe2c 563 rrq->xritag = xritag;
19ca7609
JS
564 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
565 rrq->ndlp = ndlp;
566 rrq->nlp_DID = ndlp->nlp_DID;
567 rrq->vport = ndlp->vport;
568 rrq->rxid = rxid;
569 empty = list_empty(&phba->active_rrq_list);
1151e3ec 570 rrq->send_rrq = send_rrq;
19ca7609
JS
571 list_add_tail(&rrq->list, &phba->active_rrq_list);
572 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
573 phba->hba_flag |= HBA_RRQ_ACTIVE;
574 if (empty)
575 lpfc_worker_wake_up(phba);
576 }
577 return 0;
578 }
1151e3ec
JS
579out:
580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
581 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
582 " DID:0x%x Send:%d\n",
583 xritag, rxid, did, send_rrq);
584 return -EINVAL;
19ca7609
JS
585}
586
587/**
1151e3ec 588 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
589 * @phba: Pointer to HBA context object.
590 * @xritag: xri used in this exchange.
591 * @rrq: The RRQ to be cleared.
592 *
19ca7609 593 **/
1151e3ec
JS
594void
595lpfc_clr_rrq_active(struct lpfc_hba *phba,
596 uint16_t xritag,
597 struct lpfc_node_rrq *rrq)
19ca7609 598{
1151e3ec 599 struct lpfc_nodelist *ndlp = NULL;
19ca7609 600
1151e3ec
JS
601 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
602 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
603
604 /* The target DID could have been swapped (cable swap)
605 * we should use the ndlp from the findnode if it is
606 * available.
607 */
1151e3ec 608 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
609 ndlp = rrq->ndlp;
610
1151e3ec
JS
611 if (!ndlp)
612 goto out;
613
6d368e53 614 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
19ca7609
JS
615 rrq->send_rrq = 0;
616 rrq->xritag = 0;
617 rrq->rrq_stop_time = 0;
618 }
1151e3ec 619out:
19ca7609
JS
620 mempool_free(rrq, phba->rrq_pool);
621}
622
623/**
624 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
625 * @phba: Pointer to HBA context object.
626 *
627 * This function is called with hbalock held. This function
628 * Checks if stop_time (ratov from setting rrq active) has
629 * been reached, if it has and the send_rrq flag is set then
630 * it will call lpfc_send_rrq. If the send_rrq flag is not set
631 * then it will just call the routine to clear the rrq and
632 * free the rrq resource.
633 * The timer is set to the next rrq that is going to expire before
634 * leaving the routine.
635 *
636 **/
637void
638lpfc_handle_rrq_active(struct lpfc_hba *phba)
639{
640 struct lpfc_node_rrq *rrq;
641 struct lpfc_node_rrq *nextrrq;
642 unsigned long next_time;
643 unsigned long iflags;
1151e3ec 644 LIST_HEAD(send_rrq);
19ca7609
JS
645
646 spin_lock_irqsave(&phba->hbalock, iflags);
647 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
648 next_time = jiffies + HZ * (phba->fc_ratov + 1);
649 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
650 &phba->active_rrq_list, list) {
651 if (time_after(jiffies, rrq->rrq_stop_time))
652 list_move(&rrq->list, &send_rrq);
653 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
654 next_time = rrq->rrq_stop_time;
655 }
656 spin_unlock_irqrestore(&phba->hbalock, iflags);
657 if (!list_empty(&phba->active_rrq_list))
658 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
659 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
660 list_del(&rrq->list);
661 if (!rrq->send_rrq)
662 /* this call will free the rrq */
663 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
664 else if (lpfc_send_rrq(phba, rrq)) {
665 /* if we send the rrq then the completion handler
666 * will clear the bit in the xribitmap.
667 */
668 lpfc_clr_rrq_active(phba, rrq->xritag,
669 rrq);
670 }
671 }
19ca7609
JS
672}
673
674/**
675 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
676 * @vport: Pointer to vport context object.
677 * @xri: The xri used in the exchange.
678 * @did: The targets DID for this exchange.
679 *
680 * returns NULL = rrq not found in the phba->active_rrq_list.
681 * rrq = rrq for this xri and target.
682 **/
683struct lpfc_node_rrq *
684lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
685{
686 struct lpfc_hba *phba = vport->phba;
687 struct lpfc_node_rrq *rrq;
688 struct lpfc_node_rrq *nextrrq;
689 unsigned long iflags;
690
691 if (phba->sli_rev != LPFC_SLI_REV4)
692 return NULL;
693 spin_lock_irqsave(&phba->hbalock, iflags);
694 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
695 if (rrq->vport == vport && rrq->xritag == xri &&
696 rrq->nlp_DID == did){
697 list_del(&rrq->list);
698 spin_unlock_irqrestore(&phba->hbalock, iflags);
699 return rrq;
700 }
701 }
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return NULL;
704}
705
706/**
707 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
708 * @vport: Pointer to vport context object.
1151e3ec
JS
709 * @ndlp: Pointer to the lpfc_node_list structure.
710 * If ndlp is NULL Remove all active RRQs for this vport from the
711 * phba->active_rrq_list and clear the rrq.
712 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
713 **/
714void
1151e3ec 715lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
716
717{
718 struct lpfc_hba *phba = vport->phba;
719 struct lpfc_node_rrq *rrq;
720 struct lpfc_node_rrq *nextrrq;
721 unsigned long iflags;
1151e3ec 722 LIST_HEAD(rrq_list);
19ca7609
JS
723
724 if (phba->sli_rev != LPFC_SLI_REV4)
725 return;
1151e3ec
JS
726 if (!ndlp) {
727 lpfc_sli4_vport_delete_els_xri_aborted(vport);
728 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 729 }
1151e3ec
JS
730 spin_lock_irqsave(&phba->hbalock, iflags);
731 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
732 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
733 list_move(&rrq->list, &rrq_list);
19ca7609 734 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
735
736 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
737 list_del(&rrq->list);
738 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
739 }
19ca7609
JS
740}
741
742/**
743 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
744 * @phba: Pointer to HBA context object.
745 *
746 * Remove all rrqs from the phba->active_rrq_list and free them by
747 * calling __lpfc_clr_active_rrq
748 *
749 **/
750void
751lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
752{
753 struct lpfc_node_rrq *rrq;
754 struct lpfc_node_rrq *nextrrq;
755 unsigned long next_time;
756 unsigned long iflags;
1151e3ec 757 LIST_HEAD(rrq_list);
19ca7609
JS
758
759 if (phba->sli_rev != LPFC_SLI_REV4)
760 return;
761 spin_lock_irqsave(&phba->hbalock, iflags);
762 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
763 next_time = jiffies + HZ * (phba->fc_ratov * 2);
1151e3ec
JS
764 list_splice_init(&phba->active_rrq_list, &rrq_list);
765 spin_unlock_irqrestore(&phba->hbalock, iflags);
766
767 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
19ca7609 768 list_del(&rrq->list);
1151e3ec 769 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
19ca7609 770 }
19ca7609
JS
771 if (!list_empty(&phba->active_rrq_list))
772 mod_timer(&phba->rrq_tmr, next_time);
773}
774
775
776/**
1151e3ec 777 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
778 * @phba: Pointer to HBA context object.
779 * @ndlp: Targets nodelist pointer for this exchange.
780 * @xritag the xri in the bitmap to test.
781 *
782 * This function is called with hbalock held. This function
783 * returns 0 = rrq not active for this xri
784 * 1 = rrq is valid for this xri.
785 **/
1151e3ec
JS
786int
787lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
788 uint16_t xritag)
789{
19ca7609
JS
790 if (!ndlp)
791 return 0;
6d368e53 792 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
19ca7609
JS
793 return 1;
794 else
795 return 0;
796}
797
798/**
799 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
800 * @phba: Pointer to HBA context object.
801 * @ndlp: nodelist pointer for this target.
802 * @xritag: xri used in this exchange.
803 * @rxid: Remote Exchange ID.
804 * @send_rrq: Flag used to determine if we should send rrq els cmd.
805 *
806 * This function takes the hbalock.
807 * The active bit is always set in the active rrq xri_bitmap even
808 * if there is no slot avaiable for the other rrq information.
809 *
810 * returns 0 rrq actived for this xri
811 * < 0 No memory or invalid ndlp.
812 **/
813int
814lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
815 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
816{
817 int ret;
818 unsigned long iflags;
819
820 spin_lock_irqsave(&phba->hbalock, iflags);
821 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
822 spin_unlock_irqrestore(&phba->hbalock, iflags);
823 return ret;
824}
825
da0436e9
JS
826/**
827 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
828 * @phba: Pointer to HBA context object.
19ca7609 829 * @piocb: Pointer to the iocbq.
da0436e9
JS
830 *
831 * This function is called with hbalock held. This function
6d368e53 832 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
833 * list is not empty then it is successful, it returns pointer to the newly
834 * allocated sglq object else it returns NULL.
835 **/
836static struct lpfc_sglq *
19ca7609 837__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9
JS
838{
839 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
840 struct lpfc_sglq *sglq = NULL;
19ca7609 841 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
842 struct lpfc_scsi_buf *lpfc_cmd;
843 struct lpfc_nodelist *ndlp;
844 int found = 0;
845
846 if (piocbq->iocb_flag & LPFC_IO_FCP) {
847 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
848 ndlp = lpfc_cmd->rdata->pnode;
be858b65
JS
849 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
850 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
19ca7609 851 ndlp = piocbq->context_un.ndlp;
19ca7609
JS
852 else
853 ndlp = piocbq->context1;
854
da0436e9 855 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
856 start_sglq = sglq;
857 while (!found) {
858 if (!sglq)
859 return NULL;
1151e3ec 860 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
19ca7609
JS
861 /* This xri has an rrq outstanding for this DID.
862 * put it back in the list and get another xri.
863 */
864 list_add_tail(&sglq->list, lpfc_sgl_list);
865 sglq = NULL;
866 list_remove_head(lpfc_sgl_list, sglq,
867 struct lpfc_sglq, list);
868 if (sglq == start_sglq) {
869 sglq = NULL;
870 break;
871 } else
872 continue;
873 }
874 sglq->ndlp = ndlp;
875 found = 1;
6d368e53 876 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
877 sglq->state = SGL_ALLOCATED;
878 }
da0436e9
JS
879 return sglq;
880}
881
e59058c4 882/**
3621a710 883 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
884 * @phba: Pointer to HBA context object.
885 *
886 * This function is called with no lock held. This function
887 * allocates a new driver iocb object from the iocb pool. If the
888 * allocation is successful, it returns pointer to the newly
889 * allocated iocb object else it returns NULL.
890 **/
2e0fef85
JS
891struct lpfc_iocbq *
892lpfc_sli_get_iocbq(struct lpfc_hba *phba)
893{
894 struct lpfc_iocbq * iocbq = NULL;
895 unsigned long iflags;
896
897 spin_lock_irqsave(&phba->hbalock, iflags);
898 iocbq = __lpfc_sli_get_iocbq(phba);
899 spin_unlock_irqrestore(&phba->hbalock, iflags);
900 return iocbq;
901}
902
4f774513
JS
903/**
904 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
905 * @phba: Pointer to HBA context object.
906 * @iocbq: Pointer to driver iocb object.
907 *
908 * This function is called with hbalock held to release driver
909 * iocb object to the iocb pool. The iotag in the iocb object
910 * does not change for each use of the iocb object. This function
911 * clears all other fields of the iocb object when it is freed.
912 * The sqlq structure that holds the xritag and phys and virtual
913 * mappings for the scatter gather list is retrieved from the
914 * active array of sglq. The get of the sglq pointer also clears
915 * the entry in the array. If the status of the IO indiactes that
916 * this IO was aborted then the sglq entry it put on the
917 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
918 * IO has good status or fails for any other reason then the sglq
919 * entry is added to the free list (lpfc_sgl_list).
920 **/
921static void
922__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
923{
924 struct lpfc_sglq *sglq;
925 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0
JS
926 unsigned long iflag = 0;
927 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4f774513
JS
928
929 if (iocbq->sli4_xritag == NO_XRI)
930 sglq = NULL;
931 else
6d368e53
JS
932 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
933
4f774513 934 if (sglq) {
0f65ff68
JS
935 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
936 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
937 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
938 iflag);
939 list_add(&sglq->list,
940 &phba->sli4_hba.lpfc_abts_els_sgl_list);
941 spin_unlock_irqrestore(
942 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68
JS
943 } else {
944 sglq->state = SGL_FREED;
19ca7609 945 sglq->ndlp = NULL;
fedd3b7b
JS
946 list_add_tail(&sglq->list,
947 &phba->sli4_hba.lpfc_sgl_list);
2a9bf3d0
JS
948
949 /* Check if TXQ queue needs to be serviced */
589a52d6 950 if (pring->txq_cnt)
2a9bf3d0 951 lpfc_worker_wake_up(phba);
0f65ff68 952 }
4f774513
JS
953 }
954
955
956 /*
957 * Clean all volatile data fields, preserve iotag and node struct.
958 */
959 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 960 iocbq->sli4_lxritag = NO_XRI;
4f774513
JS
961 iocbq->sli4_xritag = NO_XRI;
962 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
963}
964
2a9bf3d0 965
e59058c4 966/**
3772a991 967 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
968 * @phba: Pointer to HBA context object.
969 * @iocbq: Pointer to driver iocb object.
970 *
971 * This function is called with hbalock held to release driver
972 * iocb object to the iocb pool. The iotag in the iocb object
973 * does not change for each use of the iocb object. This function
974 * clears all other fields of the iocb object when it is freed.
975 **/
a6ababd2 976static void
3772a991 977__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 978{
2e0fef85 979 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30
JB
980
981 /*
982 * Clean all volatile data fields, preserve iotag and node struct.
983 */
984 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 985 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
986 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
987}
988
3772a991
JS
989/**
990 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
991 * @phba: Pointer to HBA context object.
992 * @iocbq: Pointer to driver iocb object.
993 *
994 * This function is called with hbalock held to release driver
995 * iocb object to the iocb pool. The iotag in the iocb object
996 * does not change for each use of the iocb object. This function
997 * clears all other fields of the iocb object when it is freed.
998 **/
999static void
1000__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1001{
1002 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1003 phba->iocb_cnt--;
3772a991
JS
1004}
1005
e59058c4 1006/**
3621a710 1007 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1008 * @phba: Pointer to HBA context object.
1009 * @iocbq: Pointer to driver iocb object.
1010 *
1011 * This function is called with no lock held to release the iocb to
1012 * iocb pool.
1013 **/
2e0fef85
JS
1014void
1015lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1016{
1017 unsigned long iflags;
1018
1019 /*
1020 * Clean all volatile data fields, preserve iotag and node struct.
1021 */
1022 spin_lock_irqsave(&phba->hbalock, iflags);
1023 __lpfc_sli_release_iocbq(phba, iocbq);
1024 spin_unlock_irqrestore(&phba->hbalock, iflags);
1025}
1026
a257bf90
JS
1027/**
1028 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1029 * @phba: Pointer to HBA context object.
1030 * @iocblist: List of IOCBs.
1031 * @ulpstatus: ULP status in IOCB command field.
1032 * @ulpWord4: ULP word-4 in IOCB command field.
1033 *
1034 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1035 * on the list by invoking the complete callback function associated with the
1036 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1037 * fields.
1038 **/
1039void
1040lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1041 uint32_t ulpstatus, uint32_t ulpWord4)
1042{
1043 struct lpfc_iocbq *piocb;
1044
1045 while (!list_empty(iocblist)) {
1046 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1047
1048 if (!piocb->iocb_cmpl)
1049 lpfc_sli_release_iocbq(phba, piocb);
1050 else {
1051 piocb->iocb.ulpStatus = ulpstatus;
1052 piocb->iocb.un.ulpWord[4] = ulpWord4;
1053 (piocb->iocb_cmpl) (phba, piocb, piocb);
1054 }
1055 }
1056 return;
1057}
1058
e59058c4 1059/**
3621a710
JS
1060 * lpfc_sli_iocb_cmd_type - Get the iocb type
1061 * @iocb_cmnd: iocb command code.
e59058c4
JS
1062 *
1063 * This function is called by ring event handler function to get the iocb type.
1064 * This function translates the iocb command to an iocb command type used to
1065 * decide the final disposition of each completed IOCB.
1066 * The function returns
1067 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1068 * LPFC_SOL_IOCB if it is a solicited iocb completion
1069 * LPFC_ABORT_IOCB if it is an abort iocb
1070 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1071 *
1072 * The caller is not required to hold any lock.
1073 **/
dea3101e 1074static lpfc_iocb_type
1075lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1076{
1077 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1078
1079 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1080 return 0;
1081
1082 switch (iocb_cmnd) {
1083 case CMD_XMIT_SEQUENCE_CR:
1084 case CMD_XMIT_SEQUENCE_CX:
1085 case CMD_XMIT_BCAST_CN:
1086 case CMD_XMIT_BCAST_CX:
1087 case CMD_ELS_REQUEST_CR:
1088 case CMD_ELS_REQUEST_CX:
1089 case CMD_CREATE_XRI_CR:
1090 case CMD_CREATE_XRI_CX:
1091 case CMD_GET_RPI_CN:
1092 case CMD_XMIT_ELS_RSP_CX:
1093 case CMD_GET_RPI_CR:
1094 case CMD_FCP_IWRITE_CR:
1095 case CMD_FCP_IWRITE_CX:
1096 case CMD_FCP_IREAD_CR:
1097 case CMD_FCP_IREAD_CX:
1098 case CMD_FCP_ICMND_CR:
1099 case CMD_FCP_ICMND_CX:
f5603511
JS
1100 case CMD_FCP_TSEND_CX:
1101 case CMD_FCP_TRSP_CX:
1102 case CMD_FCP_TRECEIVE_CX:
1103 case CMD_FCP_AUTO_TRSP_CX:
dea3101e 1104 case CMD_ADAPTER_MSG:
1105 case CMD_ADAPTER_DUMP:
1106 case CMD_XMIT_SEQUENCE64_CR:
1107 case CMD_XMIT_SEQUENCE64_CX:
1108 case CMD_XMIT_BCAST64_CN:
1109 case CMD_XMIT_BCAST64_CX:
1110 case CMD_ELS_REQUEST64_CR:
1111 case CMD_ELS_REQUEST64_CX:
1112 case CMD_FCP_IWRITE64_CR:
1113 case CMD_FCP_IWRITE64_CX:
1114 case CMD_FCP_IREAD64_CR:
1115 case CMD_FCP_IREAD64_CX:
1116 case CMD_FCP_ICMND64_CR:
1117 case CMD_FCP_ICMND64_CX:
f5603511
JS
1118 case CMD_FCP_TSEND64_CX:
1119 case CMD_FCP_TRSP64_CX:
1120 case CMD_FCP_TRECEIVE64_CX:
dea3101e 1121 case CMD_GEN_REQUEST64_CR:
1122 case CMD_GEN_REQUEST64_CX:
1123 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1124 case DSSCMD_IWRITE64_CR:
1125 case DSSCMD_IWRITE64_CX:
1126 case DSSCMD_IREAD64_CR:
1127 case DSSCMD_IREAD64_CX:
dea3101e 1128 type = LPFC_SOL_IOCB;
1129 break;
1130 case CMD_ABORT_XRI_CN:
1131 case CMD_ABORT_XRI_CX:
1132 case CMD_CLOSE_XRI_CN:
1133 case CMD_CLOSE_XRI_CX:
1134 case CMD_XRI_ABORTED_CX:
1135 case CMD_ABORT_MXRI64_CN:
6669f9bb 1136 case CMD_XMIT_BLS_RSP64_CX:
dea3101e 1137 type = LPFC_ABORT_IOCB;
1138 break;
1139 case CMD_RCV_SEQUENCE_CX:
1140 case CMD_RCV_ELS_REQ_CX:
1141 case CMD_RCV_SEQUENCE64_CX:
1142 case CMD_RCV_ELS_REQ64_CX:
57127f15 1143 case CMD_ASYNC_STATUS:
ed957684
JS
1144 case CMD_IOCB_RCV_SEQ64_CX:
1145 case CMD_IOCB_RCV_ELS64_CX:
1146 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1147 case CMD_IOCB_RET_XRI64_CX:
dea3101e 1148 type = LPFC_UNSOL_IOCB;
1149 break;
3163f725
JS
1150 case CMD_IOCB_XMIT_MSEQ64_CR:
1151 case CMD_IOCB_XMIT_MSEQ64_CX:
1152 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1153 case CMD_IOCB_RCV_ELS_LIST64_CX:
1154 case CMD_IOCB_CLOSE_EXTENDED_CN:
1155 case CMD_IOCB_ABORT_EXTENDED_CN:
1156 case CMD_IOCB_RET_HBQE64_CN:
1157 case CMD_IOCB_FCP_IBIDIR64_CR:
1158 case CMD_IOCB_FCP_IBIDIR64_CX:
1159 case CMD_IOCB_FCP_ITASKMGT64_CX:
1160 case CMD_IOCB_LOGENTRY_CN:
1161 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1162 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1163 __func__, iocb_cmnd);
3163f725
JS
1164 type = LPFC_UNKNOWN_IOCB;
1165 break;
dea3101e 1166 default:
1167 type = LPFC_UNKNOWN_IOCB;
1168 break;
1169 }
1170
1171 return type;
1172}
1173
e59058c4 1174/**
3621a710 1175 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1176 * @phba: Pointer to HBA context object.
1177 *
1178 * This function is called from SLI initialization code
1179 * to configure every ring of the HBA's SLI interface. The
1180 * caller is not required to hold any lock. This function issues
1181 * a config_ring mailbox command for each ring.
1182 * This function returns zero if successful else returns a negative
1183 * error code.
1184 **/
dea3101e 1185static int
ed957684 1186lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e 1187{
1188 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1189 LPFC_MBOXQ_t *pmb;
1190 MAILBOX_t *pmbox;
1191 int i, rc, ret = 0;
dea3101e 1192
ed957684
JS
1193 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1194 if (!pmb)
1195 return -ENOMEM;
04c68496 1196 pmbox = &pmb->u.mb;
ed957684 1197 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1198 for (i = 0; i < psli->num_rings; i++) {
dea3101e 1199 lpfc_config_ring(phba, i, pmb);
1200 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1201 if (rc != MBX_SUCCESS) {
92d7f7b0 1202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1203 "0446 Adapter failed to init (%d), "
dea3101e 1204 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1205 "ring %d\n",
e8b62011
JS
1206 rc, pmbox->mbxCommand,
1207 pmbox->mbxStatus, i);
2e0fef85 1208 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1209 ret = -ENXIO;
1210 break;
dea3101e 1211 }
1212 }
ed957684
JS
1213 mempool_free(pmb, phba->mbox_mem_pool);
1214 return ret;
dea3101e 1215}
1216
e59058c4 1217/**
3621a710 1218 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1219 * @phba: Pointer to HBA context object.
1220 * @pring: Pointer to driver SLI ring object.
1221 * @piocb: Pointer to the driver iocb object.
1222 *
1223 * This function is called with hbalock held. The function adds the
1224 * new iocb to txcmplq of the given ring. This function always returns
1225 * 0. If this function is called for ELS ring, this function checks if
1226 * there is a vport associated with the ELS command. This function also
1227 * starts els_tmofunc timer if this is an ELS command.
1228 **/
dea3101e 1229static int
2e0fef85
JS
1230lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1231 struct lpfc_iocbq *piocb)
dea3101e 1232{
dea3101e 1233 list_add_tail(&piocb->list, &pring->txcmplq);
2a9bf3d0 1234 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea3101e 1235 pring->txcmplq_cnt++;
2a9bf3d0
JS
1236 if (pring->txcmplq_cnt > pring->txcmplq_max)
1237 pring->txcmplq_max = pring->txcmplq_cnt;
1238
92d7f7b0
JS
1239 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1240 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1241 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1242 if (!piocb->vport)
1243 BUG();
1244 else
1245 mod_timer(&piocb->vport->els_tmofunc,
1246 jiffies + HZ * (phba->fc_ratov << 1));
1247 }
1248
dea3101e 1249
2e0fef85 1250 return 0;
dea3101e 1251}
1252
e59058c4 1253/**
3621a710 1254 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1255 * @phba: Pointer to HBA context object.
1256 * @pring: Pointer to driver SLI ring object.
1257 *
1258 * This function is called with hbalock held to get next
1259 * iocb in txq of the given ring. If there is any iocb in
1260 * the txq, the function returns first iocb in the list after
1261 * removing the iocb from the list, else it returns NULL.
1262 **/
2a9bf3d0 1263struct lpfc_iocbq *
2e0fef85 1264lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1265{
dea3101e 1266 struct lpfc_iocbq *cmd_iocb;
1267
858c9f6c
JS
1268 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1269 if (cmd_iocb != NULL)
dea3101e 1270 pring->txq_cnt--;
2e0fef85 1271 return cmd_iocb;
dea3101e 1272}
1273
e59058c4 1274/**
3621a710 1275 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1276 * @phba: Pointer to HBA context object.
1277 * @pring: Pointer to driver SLI ring object.
1278 *
1279 * This function is called with hbalock held and the caller must post the
1280 * iocb without releasing the lock. If the caller releases the lock,
1281 * iocb slot returned by the function is not guaranteed to be available.
1282 * The function returns pointer to the next available iocb slot if there
1283 * is available slot in the ring, else it returns NULL.
1284 * If the get index of the ring is ahead of the put index, the function
1285 * will post an error attention event to the worker thread to take the
1286 * HBA to offline state.
1287 **/
dea3101e 1288static IOCB_t *
1289lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1290{
34b02dcd 1291 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 1292 uint32_t max_cmd_idx = pring->numCiocb;
dea3101e 1293 if ((pring->next_cmdidx == pring->cmdidx) &&
1294 (++pring->next_cmdidx >= max_cmd_idx))
1295 pring->next_cmdidx = 0;
1296
1297 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1298
1299 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1300
1301 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1303 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1304 "is bigger than cmd ring %d\n",
e8b62011 1305 pring->ringno,
dea3101e 1306 pring->local_getidx, max_cmd_idx);
1307
2e0fef85 1308 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1309 /*
1310 * All error attention handlers are posted to
1311 * worker thread
1312 */
1313 phba->work_ha |= HA_ERATT;
1314 phba->work_hs = HS_FFER3;
92d7f7b0 1315
5e9d9b82 1316 lpfc_worker_wake_up(phba);
dea3101e 1317
1318 return NULL;
1319 }
1320
1321 if (pring->local_getidx == pring->next_cmdidx)
1322 return NULL;
1323 }
1324
ed957684 1325 return lpfc_cmd_iocb(phba, pring);
dea3101e 1326}
1327
e59058c4 1328/**
3621a710 1329 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1332 *
1333 * This function gets an iotag for the iocb. If there is no unused iotag and
1334 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1335 * array and assigns a new iotag.
1336 * The function returns the allocated iotag if successful, else returns zero.
1337 * Zero is not a valid iotag.
1338 * The caller is not required to hold any lock.
1339 **/
604a3e30 1340uint16_t
2e0fef85 1341lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1342{
2e0fef85
JS
1343 struct lpfc_iocbq **new_arr;
1344 struct lpfc_iocbq **old_arr;
604a3e30
JB
1345 size_t new_len;
1346 struct lpfc_sli *psli = &phba->sli;
1347 uint16_t iotag;
dea3101e 1348
2e0fef85 1349 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1350 iotag = psli->last_iotag;
1351 if(++iotag < psli->iocbq_lookup_len) {
1352 psli->last_iotag = iotag;
1353 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1354 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1355 iocbq->iotag = iotag;
1356 return iotag;
2e0fef85 1357 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1358 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1359 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1360 spin_unlock_irq(&phba->hbalock);
1361 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1362 GFP_KERNEL);
1363 if (new_arr) {
2e0fef85 1364 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1365 old_arr = psli->iocbq_lookup;
1366 if (new_len <= psli->iocbq_lookup_len) {
1367 /* highly unprobable case */
1368 kfree(new_arr);
1369 iotag = psli->last_iotag;
1370 if(++iotag < psli->iocbq_lookup_len) {
1371 psli->last_iotag = iotag;
1372 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1373 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1374 iocbq->iotag = iotag;
1375 return iotag;
1376 }
2e0fef85 1377 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1378 return 0;
1379 }
1380 if (psli->iocbq_lookup)
1381 memcpy(new_arr, old_arr,
1382 ((psli->last_iotag + 1) *
311464ec 1383 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1384 psli->iocbq_lookup = new_arr;
1385 psli->iocbq_lookup_len = new_len;
1386 psli->last_iotag = iotag;
1387 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1388 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1389 iocbq->iotag = iotag;
1390 kfree(old_arr);
1391 return iotag;
1392 }
8f6d98d2 1393 } else
2e0fef85 1394 spin_unlock_irq(&phba->hbalock);
dea3101e 1395
bc73905a 1396 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1397 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1398 psli->last_iotag);
dea3101e 1399
604a3e30 1400 return 0;
dea3101e 1401}
1402
e59058c4 1403/**
3621a710 1404 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1405 * @phba: Pointer to HBA context object.
1406 * @pring: Pointer to driver SLI ring object.
1407 * @iocb: Pointer to iocb slot in the ring.
1408 * @nextiocb: Pointer to driver iocb object which need to be
1409 * posted to firmware.
1410 *
1411 * This function is called with hbalock held to post a new iocb to
1412 * the firmware. This function copies the new iocb to ring iocb slot and
1413 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1414 * a completion call back for this iocb else the function will free the
1415 * iocb object.
1416 **/
dea3101e 1417static void
1418lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1419 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1420{
1421 /*
604a3e30 1422 * Set up an iotag
dea3101e 1423 */
604a3e30 1424 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1425
e2a0a9d6 1426
a58cbd52
JS
1427 if (pring->ringno == LPFC_ELS_RING) {
1428 lpfc_debugfs_slow_ring_trc(phba,
1429 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1430 *(((uint32_t *) &nextiocb->iocb) + 4),
1431 *(((uint32_t *) &nextiocb->iocb) + 6),
1432 *(((uint32_t *) &nextiocb->iocb) + 7));
1433 }
1434
dea3101e 1435 /*
1436 * Issue iocb command to adapter
1437 */
92d7f7b0 1438 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e 1439 wmb();
1440 pring->stats.iocb_cmd++;
1441
1442 /*
1443 * If there is no completion routine to call, we can release the
1444 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1445 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1446 */
1447 if (nextiocb->iocb_cmpl)
1448 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1449 else
2e0fef85 1450 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e 1451
1452 /*
1453 * Let the HBA know what IOCB slot will be the next one the
1454 * driver will put a command into.
1455 */
1456 pring->cmdidx = pring->next_cmdidx;
ed957684 1457 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e 1458}
1459
e59058c4 1460/**
3621a710 1461 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1462 * @phba: Pointer to HBA context object.
1463 * @pring: Pointer to driver SLI ring object.
1464 *
1465 * The caller is not required to hold any lock for calling this function.
1466 * This function updates the chip attention bits for the ring to inform firmware
1467 * that there are pending work to be done for this ring and requests an
1468 * interrupt when there is space available in the ring. This function is
1469 * called when the driver is unable to post more iocbs to the ring due
1470 * to unavailability of space in the ring.
1471 **/
dea3101e 1472static void
2e0fef85 1473lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1474{
1475 int ringno = pring->ringno;
1476
1477 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1478
1479 wmb();
1480
1481 /*
1482 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1483 * The HBA will tell us when an IOCB entry is available.
1484 */
1485 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1486 readl(phba->CAregaddr); /* flush */
1487
1488 pring->stats.iocb_cmd_full++;
1489}
1490
e59058c4 1491/**
3621a710 1492 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1493 * @phba: Pointer to HBA context object.
1494 * @pring: Pointer to driver SLI ring object.
1495 *
1496 * This function updates the chip attention register bit for the
1497 * given ring to inform HBA that there is more work to be done
1498 * in this ring. The caller is not required to hold any lock.
1499 **/
dea3101e 1500static void
2e0fef85 1501lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1502{
1503 int ringno = pring->ringno;
1504
1505 /*
1506 * Tell the HBA that there is work to do in this ring.
1507 */
34b02dcd
JS
1508 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1509 wmb();
1510 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1511 readl(phba->CAregaddr); /* flush */
1512 }
dea3101e 1513}
1514
e59058c4 1515/**
3621a710 1516 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1517 * @phba: Pointer to HBA context object.
1518 * @pring: Pointer to driver SLI ring object.
1519 *
1520 * This function is called with hbalock held to post pending iocbs
1521 * in the txq to the firmware. This function is called when driver
1522 * detects space available in the ring.
1523 **/
dea3101e 1524static void
2e0fef85 1525lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1526{
1527 IOCB_t *iocb;
1528 struct lpfc_iocbq *nextiocb;
1529
1530 /*
1531 * Check to see if:
1532 * (a) there is anything on the txq to send
1533 * (b) link is up
1534 * (c) link attention events can be processed (fcp ring only)
1535 * (d) IOCB processing is not blocked by the outstanding mbox command.
1536 */
1537 if (pring->txq_cnt &&
2e0fef85 1538 lpfc_is_link_up(phba) &&
dea3101e 1539 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1540 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e 1541
1542 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1543 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1544 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1545
1546 if (iocb)
1547 lpfc_sli_update_ring(phba, pring);
1548 else
1549 lpfc_sli_update_full_ring(phba, pring);
1550 }
1551
1552 return;
1553}
1554
e59058c4 1555/**
3621a710 1556 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1557 * @phba: Pointer to HBA context object.
1558 * @hbqno: HBQ number.
1559 *
1560 * This function is called with hbalock held to get the next
1561 * available slot for the given HBQ. If there is free slot
1562 * available for the HBQ it will return pointer to the next available
1563 * HBQ entry else it will return NULL.
1564 **/
a6ababd2 1565static struct lpfc_hbq_entry *
ed957684
JS
1566lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1567{
1568 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1569
1570 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1571 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1572 hbqp->next_hbqPutIdx = 0;
1573
1574 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1575 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1576 uint32_t getidx = le32_to_cpu(raw_index);
1577
1578 hbqp->local_hbqGetIdx = getidx;
1579
1580 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1581 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1582 LOG_SLI | LOG_VPORT,
e8b62011 1583 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1584 "%u is > than hbqp->entry_count %u\n",
e8b62011 1585 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1586 hbqp->entry_count);
1587
1588 phba->link_state = LPFC_HBA_ERROR;
1589 return NULL;
1590 }
1591
1592 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1593 return NULL;
1594 }
1595
51ef4c26
JS
1596 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1597 hbqp->hbqPutIdx;
ed957684
JS
1598}
1599
e59058c4 1600/**
3621a710 1601 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1602 * @phba: Pointer to HBA context object.
1603 *
1604 * This function is called with no lock held to free all the
1605 * hbq buffers while uninitializing the SLI interface. It also
1606 * frees the HBQ buffers returned by the firmware but not yet
1607 * processed by the upper layers.
1608 **/
ed957684
JS
1609void
1610lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1611{
92d7f7b0
JS
1612 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1613 struct hbq_dmabuf *hbq_buf;
3163f725 1614 unsigned long flags;
51ef4c26 1615 int i, hbq_count;
3163f725 1616 uint32_t hbqno;
ed957684 1617
51ef4c26 1618 hbq_count = lpfc_sli_hbq_count();
ed957684 1619 /* Return all memory used by all HBQs */
3163f725 1620 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1621 for (i = 0; i < hbq_count; ++i) {
1622 list_for_each_entry_safe(dmabuf, next_dmabuf,
1623 &phba->hbqs[i].hbq_buffer_list, list) {
1624 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1625 list_del(&hbq_buf->dbuf.list);
1626 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1627 }
a8adb832 1628 phba->hbqs[i].buffer_count = 0;
ed957684 1629 }
3163f725 1630 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1631 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1632 list) {
3163f725
JS
1633 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1634 list_del(&hbq_buf->dbuf.list);
1635 if (hbq_buf->tag == -1) {
1636 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1637 (phba, hbq_buf);
1638 } else {
1639 hbqno = hbq_buf->tag >> 16;
1640 if (hbqno >= LPFC_MAX_HBQS)
1641 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1642 (phba, hbq_buf);
1643 else
1644 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1645 hbq_buf);
1646 }
1647 }
1648
1649 /* Mark the HBQs not in use */
1650 phba->hbq_in_use = 0;
1651 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1652}
1653
e59058c4 1654/**
3621a710 1655 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1656 * @phba: Pointer to HBA context object.
1657 * @hbqno: HBQ number.
1658 * @hbq_buf: Pointer to HBQ buffer.
1659 *
1660 * This function is called with the hbalock held to post a
1661 * hbq buffer to the firmware. If the function finds an empty
1662 * slot in the HBQ, it will post the buffer. The function will return
1663 * pointer to the hbq entry if it successfully post the buffer
1664 * else it will return NULL.
1665 **/
3772a991 1666static int
ed957684 1667lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1668 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1669{
1670 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1671}
1672
1673/**
1674 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1675 * @phba: Pointer to HBA context object.
1676 * @hbqno: HBQ number.
1677 * @hbq_buf: Pointer to HBQ buffer.
1678 *
1679 * This function is called with the hbalock held to post a hbq buffer to the
1680 * firmware. If the function finds an empty slot in the HBQ, it will post the
1681 * buffer and place it on the hbq_buffer_list. The function will return zero if
1682 * it successfully post the buffer else it will return an error.
1683 **/
1684static int
1685lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1686 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1687{
1688 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1689 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1690
1691 /* Get next HBQ entry slot to use */
1692 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1693 if (hbqe) {
1694 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1695
92d7f7b0
JS
1696 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1697 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1698 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1699 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1700 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1701 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1702 /* Sync SLIM */
ed957684
JS
1703 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1704 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1705 /* flush */
ed957684 1706 readl(phba->hbq_put + hbqno);
51ef4c26 1707 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1708 return 0;
1709 } else
1710 return -ENOMEM;
ed957684
JS
1711}
1712
4f774513
JS
1713/**
1714 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1715 * @phba: Pointer to HBA context object.
1716 * @hbqno: HBQ number.
1717 * @hbq_buf: Pointer to HBQ buffer.
1718 *
1719 * This function is called with the hbalock held to post an RQE to the SLI4
1720 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1721 * the hbq_buffer_list and return zero, otherwise it will return an error.
1722 **/
1723static int
1724lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1725 struct hbq_dmabuf *hbq_buf)
1726{
1727 int rc;
1728 struct lpfc_rqe hrqe;
1729 struct lpfc_rqe drqe;
1730
1731 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1732 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1733 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1734 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1735 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1736 &hrqe, &drqe);
1737 if (rc < 0)
1738 return rc;
1739 hbq_buf->tag = rc;
1740 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1741 return 0;
1742}
1743
e59058c4 1744/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1745static struct lpfc_hbq_init lpfc_els_hbq = {
1746 .rn = 1,
def9c7a9 1747 .entry_count = 256,
92d7f7b0
JS
1748 .mask_count = 0,
1749 .profile = 0,
51ef4c26 1750 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1751 .buffer_count = 0,
a257bf90
JS
1752 .init_count = 40,
1753 .add_count = 40,
92d7f7b0 1754};
ed957684 1755
e59058c4 1756/* HBQ for the extra ring if needed */
51ef4c26
JS
1757static struct lpfc_hbq_init lpfc_extra_hbq = {
1758 .rn = 1,
1759 .entry_count = 200,
1760 .mask_count = 0,
1761 .profile = 0,
1762 .ring_mask = (1 << LPFC_EXTRA_RING),
1763 .buffer_count = 0,
1764 .init_count = 0,
1765 .add_count = 5,
1766};
1767
e59058c4 1768/* Array of HBQs */
78b2d852 1769struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1770 &lpfc_els_hbq,
51ef4c26 1771 &lpfc_extra_hbq,
92d7f7b0 1772};
ed957684 1773
e59058c4 1774/**
3621a710 1775 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1776 * @phba: Pointer to HBA context object.
1777 * @hbqno: HBQ number.
1778 * @count: Number of HBQ buffers to be posted.
1779 *
d7c255b2
JS
1780 * This function is called with no lock held to post more hbq buffers to the
1781 * given HBQ. The function returns the number of HBQ buffers successfully
1782 * posted.
e59058c4 1783 **/
311464ec 1784static int
92d7f7b0 1785lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1786{
d7c255b2 1787 uint32_t i, posted = 0;
3163f725 1788 unsigned long flags;
92d7f7b0 1789 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1790 LIST_HEAD(hbq_buf_list);
eafe1df9 1791 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1792 return 0;
51ef4c26 1793
d7c255b2
JS
1794 if ((phba->hbqs[hbqno].buffer_count + count) >
1795 lpfc_hbq_defs[hbqno]->entry_count)
1796 count = lpfc_hbq_defs[hbqno]->entry_count -
1797 phba->hbqs[hbqno].buffer_count;
1798 if (!count)
1799 return 0;
1800 /* Allocate HBQ entries */
1801 for (i = 0; i < count; i++) {
1802 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1803 if (!hbq_buffer)
1804 break;
1805 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1806 }
3163f725
JS
1807 /* Check whether HBQ is still in use */
1808 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1809 if (!phba->hbq_in_use)
d7c255b2
JS
1810 goto err;
1811 while (!list_empty(&hbq_buf_list)) {
1812 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1813 dbuf.list);
1814 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1815 (hbqno << 16));
3772a991 1816 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1817 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1818 posted++;
1819 } else
51ef4c26 1820 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1821 }
3163f725 1822 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1823 return posted;
1824err:
eafe1df9 1825 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1826 while (!list_empty(&hbq_buf_list)) {
1827 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1828 dbuf.list);
1829 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1830 }
1831 return 0;
ed957684
JS
1832}
1833
e59058c4 1834/**
3621a710 1835 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1836 * @phba: Pointer to HBA context object.
1837 * @qno: HBQ number.
1838 *
1839 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1840 * is called with no lock held. The function returns the number of HBQ entries
1841 * successfully allocated.
e59058c4 1842 **/
92d7f7b0
JS
1843int
1844lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1845{
def9c7a9
JS
1846 if (phba->sli_rev == LPFC_SLI_REV4)
1847 return 0;
1848 else
1849 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1850 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1851}
ed957684 1852
e59058c4 1853/**
3621a710 1854 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1855 * @phba: Pointer to HBA context object.
1856 * @qno: HBQ queue number.
1857 *
1858 * This function is called from SLI initialization code path with
1859 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1860 * function returns the number of HBQ entries successfully allocated.
e59058c4 1861 **/
a6ababd2 1862static int
92d7f7b0
JS
1863lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1864{
def9c7a9
JS
1865 if (phba->sli_rev == LPFC_SLI_REV4)
1866 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 1867 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
1868 else
1869 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1870 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1871}
1872
3772a991
JS
1873/**
1874 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1875 * @phba: Pointer to HBA context object.
1876 * @hbqno: HBQ number.
1877 *
1878 * This function removes the first hbq buffer on an hbq list and returns a
1879 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1880 **/
1881static struct hbq_dmabuf *
1882lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1883{
1884 struct lpfc_dmabuf *d_buf;
1885
1886 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1887 if (!d_buf)
1888 return NULL;
1889 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1890}
1891
e59058c4 1892/**
3621a710 1893 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1894 * @phba: Pointer to HBA context object.
1895 * @tag: Tag of the hbq buffer.
1896 *
1897 * This function is called with hbalock held. This function searches
1898 * for the hbq buffer associated with the given tag in the hbq buffer
1899 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1900 * it returns NULL.
1901 **/
a6ababd2 1902static struct hbq_dmabuf *
92d7f7b0 1903lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1904{
92d7f7b0
JS
1905 struct lpfc_dmabuf *d_buf;
1906 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1907 uint32_t hbqno;
1908
1909 hbqno = tag >> 16;
a0a74e45 1910 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1911 return NULL;
ed957684 1912
3772a991 1913 spin_lock_irq(&phba->hbalock);
51ef4c26 1914 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1915 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1916 if (hbq_buf->tag == tag) {
3772a991 1917 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1918 return hbq_buf;
ed957684
JS
1919 }
1920 }
3772a991 1921 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 1923 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 1924 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 1925 return NULL;
ed957684
JS
1926}
1927
e59058c4 1928/**
3621a710 1929 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
1930 * @phba: Pointer to HBA context object.
1931 * @hbq_buffer: Pointer to HBQ buffer.
1932 *
1933 * This function is called with hbalock. This function gives back
1934 * the hbq buffer to firmware. If the HBQ does not have space to
1935 * post the buffer, it will free the buffer.
1936 **/
ed957684 1937void
51ef4c26 1938lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
1939{
1940 uint32_t hbqno;
1941
51ef4c26
JS
1942 if (hbq_buffer) {
1943 hbqno = hbq_buffer->tag >> 16;
3772a991 1944 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 1945 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
1946 }
1947}
1948
e59058c4 1949/**
3621a710 1950 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
1951 * @mbxCommand: mailbox command code.
1952 *
1953 * This function is called by the mailbox event handler function to verify
1954 * that the completed mailbox command is a legitimate mailbox command. If the
1955 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1956 * and the mailbox event handler will take the HBA offline.
1957 **/
dea3101e 1958static int
1959lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1960{
1961 uint8_t ret;
1962
1963 switch (mbxCommand) {
1964 case MBX_LOAD_SM:
1965 case MBX_READ_NV:
1966 case MBX_WRITE_NV:
a8adb832 1967 case MBX_WRITE_VPARMS:
dea3101e 1968 case MBX_RUN_BIU_DIAG:
1969 case MBX_INIT_LINK:
1970 case MBX_DOWN_LINK:
1971 case MBX_CONFIG_LINK:
1972 case MBX_CONFIG_RING:
1973 case MBX_RESET_RING:
1974 case MBX_READ_CONFIG:
1975 case MBX_READ_RCONFIG:
1976 case MBX_READ_SPARM:
1977 case MBX_READ_STATUS:
1978 case MBX_READ_RPI:
1979 case MBX_READ_XRI:
1980 case MBX_READ_REV:
1981 case MBX_READ_LNK_STAT:
1982 case MBX_REG_LOGIN:
1983 case MBX_UNREG_LOGIN:
dea3101e 1984 case MBX_CLEAR_LA:
1985 case MBX_DUMP_MEMORY:
1986 case MBX_DUMP_CONTEXT:
1987 case MBX_RUN_DIAGS:
1988 case MBX_RESTART:
1989 case MBX_UPDATE_CFG:
1990 case MBX_DOWN_LOAD:
1991 case MBX_DEL_LD_ENTRY:
1992 case MBX_RUN_PROGRAM:
1993 case MBX_SET_MASK:
09372820 1994 case MBX_SET_VARIABLE:
dea3101e 1995 case MBX_UNREG_D_ID:
41415862 1996 case MBX_KILL_BOARD:
dea3101e 1997 case MBX_CONFIG_FARP:
41415862 1998 case MBX_BEACON:
dea3101e 1999 case MBX_LOAD_AREA:
2000 case MBX_RUN_BIU_DIAG64:
2001 case MBX_CONFIG_PORT:
2002 case MBX_READ_SPARM64:
2003 case MBX_READ_RPI64:
2004 case MBX_REG_LOGIN64:
76a95d75 2005 case MBX_READ_TOPOLOGY:
09372820 2006 case MBX_WRITE_WWN:
dea3101e 2007 case MBX_SET_DEBUG:
2008 case MBX_LOAD_EXP_ROM:
57127f15 2009 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2010 case MBX_REG_VPI:
2011 case MBX_UNREG_VPI:
858c9f6c 2012 case MBX_HEARTBEAT:
84774a4d
JS
2013 case MBX_PORT_CAPABILITIES:
2014 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2015 case MBX_SLI4_CONFIG:
2016 case MBX_SLI4_REQ_FTRS:
2017 case MBX_REG_FCFI:
2018 case MBX_UNREG_FCFI:
2019 case MBX_REG_VFI:
2020 case MBX_UNREG_VFI:
2021 case MBX_INIT_VPI:
2022 case MBX_INIT_VFI:
2023 case MBX_RESUME_RPI:
c7495937
JS
2024 case MBX_READ_EVENT_LOG_STATUS:
2025 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2026 case MBX_SECURITY_MGMT:
2027 case MBX_AUTH_PORT:
dea3101e 2028 ret = mbxCommand;
2029 break;
2030 default:
2031 ret = MBX_SHUTDOWN;
2032 break;
2033 }
2e0fef85 2034 return ret;
dea3101e 2035}
e59058c4
JS
2036
2037/**
3621a710 2038 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2039 * @phba: Pointer to HBA context object.
2040 * @pmboxq: Pointer to mailbox command.
2041 *
2042 * This is completion handler function for mailbox commands issued from
2043 * lpfc_sli_issue_mbox_wait function. This function is called by the
2044 * mailbox event handler function with no lock held. This function
2045 * will wake up thread waiting on the wait queue pointed by context1
2046 * of the mailbox.
2047 **/
04c68496 2048void
2e0fef85 2049lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 2050{
2051 wait_queue_head_t *pdone_q;
858c9f6c 2052 unsigned long drvr_flag;
dea3101e 2053
2054 /*
2055 * If pdone_q is empty, the driver thread gave up waiting and
2056 * continued running.
2057 */
7054a606 2058 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2059 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e 2060 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2061 if (pdone_q)
2062 wake_up_interruptible(pdone_q);
858c9f6c 2063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 2064 return;
2065}
2066
e59058c4
JS
2067
2068/**
3621a710 2069 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2070 * @phba: Pointer to HBA context object.
2071 * @pmb: Pointer to mailbox object.
2072 *
2073 * This function is the default mailbox completion handler. It
2074 * frees the memory resources associated with the completed mailbox
2075 * command. If the completed command is a REG_LOGIN mailbox command,
2076 * this function will issue a UREG_LOGIN to re-claim the RPI.
2077 **/
dea3101e 2078void
2e0fef85 2079lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2080{
d439d286 2081 struct lpfc_vport *vport = pmb->vport;
dea3101e 2082 struct lpfc_dmabuf *mp;
d439d286 2083 struct lpfc_nodelist *ndlp;
5af5eee7 2084 struct Scsi_Host *shost;
04c68496 2085 uint16_t rpi, vpi;
7054a606
JS
2086 int rc;
2087
dea3101e 2088 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2089
dea3101e 2090 if (mp) {
2091 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2092 kfree(mp);
2093 }
7054a606
JS
2094
2095 /*
2096 * If a REG_LOGIN succeeded after node is destroyed or node
2097 * is in re-discovery driver need to cleanup the RPI.
2098 */
2e0fef85 2099 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2100 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2101 !pmb->u.mb.mbxStatus) {
2102 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2103 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2104 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 2105 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2106 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2107 if (rc != MBX_NOT_FINISHED)
2108 return;
2109 }
2110
695a814e
JS
2111 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2112 !(phba->pport->load_flag & FC_UNLOADING) &&
2113 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2114 shost = lpfc_shost_from_vport(vport);
2115 spin_lock_irq(shost->host_lock);
2116 vport->vpi_state |= LPFC_VPI_REGISTERED;
2117 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2118 spin_unlock_irq(shost->host_lock);
695a814e
JS
2119 }
2120
d439d286
JS
2121 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2122 ndlp = (struct lpfc_nodelist *)pmb->context2;
2123 lpfc_nlp_put(ndlp);
2124 pmb->context2 = NULL;
2125 }
2126
dcf2a4e0
JS
2127 /* Check security permission status on INIT_LINK mailbox command */
2128 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2129 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2130 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2131 "2860 SLI authentication is required "
2132 "for INIT_LINK but has not done yet\n");
2133
04c68496
JS
2134 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2135 lpfc_sli4_mbox_cmd_free(phba, pmb);
2136 else
2137 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2138}
2139
e59058c4 2140/**
3621a710 2141 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2142 * @phba: Pointer to HBA context object.
2143 *
2144 * This function is called with no lock held. This function processes all
2145 * the completed mailbox commands and gives it to upper layers. The interrupt
2146 * service routine processes mailbox completion interrupt and adds completed
2147 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2148 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2149 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2150 * function returns the mailbox commands to the upper layer by calling the
2151 * completion handler function of each mailbox.
2152 **/
dea3101e 2153int
2e0fef85 2154lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2155{
92d7f7b0 2156 MAILBOX_t *pmbox;
dea3101e 2157 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2158 int rc;
2159 LIST_HEAD(cmplq);
dea3101e 2160
2161 phba->sli.slistat.mbox_event++;
2162
92d7f7b0
JS
2163 /* Get all completed mailboxe buffers into the cmplq */
2164 spin_lock_irq(&phba->hbalock);
2165 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2166 spin_unlock_irq(&phba->hbalock);
dea3101e 2167
92d7f7b0
JS
2168 /* Get a Mailbox buffer to setup mailbox commands for callback */
2169 do {
2170 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2171 if (pmb == NULL)
2172 break;
2e0fef85 2173
04c68496 2174 pmbox = &pmb->u.mb;
dea3101e 2175
858c9f6c
JS
2176 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2177 if (pmb->vport) {
2178 lpfc_debugfs_disc_trc(pmb->vport,
2179 LPFC_DISC_TRC_MBOX_VPORT,
2180 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2181 (uint32_t)pmbox->mbxCommand,
2182 pmbox->un.varWords[0],
2183 pmbox->un.varWords[1]);
2184 }
2185 else {
2186 lpfc_debugfs_disc_trc(phba->pport,
2187 LPFC_DISC_TRC_MBOX,
2188 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2189 (uint32_t)pmbox->mbxCommand,
2190 pmbox->un.varWords[0],
2191 pmbox->un.varWords[1]);
2192 }
2193 }
2194
dea3101e 2195 /*
2196 * It is a fatal error if unknown mbox command completion.
2197 */
2198 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2199 MBX_SHUTDOWN) {
af901ca1 2200 /* Unknown mailbox command compl */
92d7f7b0 2201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2202 "(%d):0323 Unknown Mailbox command "
a183a15f 2203 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2204 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2205 pmbox->mbxCommand,
a183a15f
JS
2206 lpfc_sli_config_mbox_subsys_get(phba,
2207 pmb),
2208 lpfc_sli_config_mbox_opcode_get(phba,
2209 pmb));
2e0fef85 2210 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2211 phba->work_hs = HS_FFER3;
2212 lpfc_handle_eratt(phba);
92d7f7b0 2213 continue;
dea3101e 2214 }
2215
dea3101e 2216 if (pmbox->mbxStatus) {
2217 phba->sli.slistat.mbox_stat_err++;
2218 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2219 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2220 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2221 LOG_MBOX | LOG_SLI,
2222 "(%d):0305 Mbox cmd cmpl "
2223 "error - RETRYing Data: x%x "
2224 "(x%x/x%x) x%x x%x x%x\n",
2225 pmb->vport ? pmb->vport->vpi : 0,
2226 pmbox->mbxCommand,
2227 lpfc_sli_config_mbox_subsys_get(phba,
2228 pmb),
2229 lpfc_sli_config_mbox_opcode_get(phba,
2230 pmb),
2231 pmbox->mbxStatus,
2232 pmbox->un.varWords[0],
2233 pmb->vport->port_state);
dea3101e 2234 pmbox->mbxStatus = 0;
2235 pmbox->mbxOwner = OWN_HOST;
dea3101e 2236 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2237 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2238 continue;
dea3101e 2239 }
2240 }
2241
2242 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2243 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2244 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea3101e 2245 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 2246 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2247 pmbox->mbxCommand,
a183a15f
JS
2248 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2249 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e 2250 pmb->mbox_cmpl,
2251 *((uint32_t *) pmbox),
2252 pmbox->un.varWords[0],
2253 pmbox->un.varWords[1],
2254 pmbox->un.varWords[2],
2255 pmbox->un.varWords[3],
2256 pmbox->un.varWords[4],
2257 pmbox->un.varWords[5],
2258 pmbox->un.varWords[6],
2259 pmbox->un.varWords[7]);
2260
92d7f7b0 2261 if (pmb->mbox_cmpl)
dea3101e 2262 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2263 } while (1);
2264 return 0;
2265}
dea3101e 2266
e59058c4 2267/**
3621a710 2268 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2269 * @phba: Pointer to HBA context object.
2270 * @pring: Pointer to driver SLI ring object.
2271 * @tag: buffer tag.
2272 *
2273 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2274 * is set in the tag the buffer is posted for a particular exchange,
2275 * the function will return the buffer without replacing the buffer.
2276 * If the buffer is for unsolicited ELS or CT traffic, this function
2277 * returns the buffer and also posts another buffer to the firmware.
2278 **/
76bb24ef
JS
2279static struct lpfc_dmabuf *
2280lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2281 struct lpfc_sli_ring *pring,
2282 uint32_t tag)
76bb24ef 2283{
9f1e1b50
JS
2284 struct hbq_dmabuf *hbq_entry;
2285
76bb24ef
JS
2286 if (tag & QUE_BUFTAG_BIT)
2287 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2288 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2289 if (!hbq_entry)
2290 return NULL;
2291 return &hbq_entry->dbuf;
76bb24ef 2292}
57127f15 2293
3772a991
JS
2294/**
2295 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2296 * @phba: Pointer to HBA context object.
2297 * @pring: Pointer to driver SLI ring object.
2298 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2299 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2300 * @fch_type: the type for the first frame of the sequence.
2301 *
2302 * This function is called with no lock held. This function uses the r_ctl and
2303 * type of the received sequence to find the correct callback function to call
2304 * to process the sequence.
2305 **/
2306static int
2307lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2308 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2309 uint32_t fch_type)
2310{
2311 int i;
2312
2313 /* unSolicited Responses */
2314 if (pring->prt[0].profile) {
2315 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2316 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2317 saveq);
2318 return 1;
2319 }
2320 /* We must search, based on rctl / type
2321 for the right routine */
2322 for (i = 0; i < pring->num_mask; i++) {
2323 if ((pring->prt[i].rctl == fch_r_ctl) &&
2324 (pring->prt[i].type == fch_type)) {
2325 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2326 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2327 (phba, pring, saveq);
2328 return 1;
2329 }
2330 }
2331 return 0;
2332}
e59058c4
JS
2333
2334/**
3621a710 2335 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2336 * @phba: Pointer to HBA context object.
2337 * @pring: Pointer to driver SLI ring object.
2338 * @saveq: Pointer to the unsolicited iocb.
2339 *
2340 * This function is called with no lock held by the ring event handler
2341 * when there is an unsolicited iocb posted to the response ring by the
2342 * firmware. This function gets the buffer associated with the iocbs
2343 * and calls the event handler for the ring. This function handles both
2344 * qring buffers and hbq buffers.
2345 * When the function returns 1 the caller can free the iocb object otherwise
2346 * upper layer functions will free the iocb objects.
2347 **/
dea3101e 2348static int
2349lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2350 struct lpfc_iocbq *saveq)
2351{
2352 IOCB_t * irsp;
2353 WORD5 * w5p;
2354 uint32_t Rctl, Type;
3772a991 2355 uint32_t match;
76bb24ef 2356 struct lpfc_iocbq *iocbq;
3163f725 2357 struct lpfc_dmabuf *dmzbuf;
dea3101e 2358
2359 match = 0;
2360 irsp = &(saveq->iocb);
57127f15
JS
2361
2362 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2363 if (pring->lpfc_sli_rcv_async_status)
2364 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2365 else
2366 lpfc_printf_log(phba,
2367 KERN_WARNING,
2368 LOG_SLI,
2369 "0316 Ring %d handler: unexpected "
2370 "ASYNC_STATUS iocb received evt_code "
2371 "0x%x\n",
2372 pring->ringno,
2373 irsp->un.asyncstat.evt_code);
2374 return 1;
2375 }
2376
3163f725
JS
2377 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2378 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2379 if (irsp->ulpBdeCount > 0) {
2380 dmzbuf = lpfc_sli_get_buff(phba, pring,
2381 irsp->un.ulpWord[3]);
2382 lpfc_in_buf_free(phba, dmzbuf);
2383 }
2384
2385 if (irsp->ulpBdeCount > 1) {
2386 dmzbuf = lpfc_sli_get_buff(phba, pring,
2387 irsp->unsli3.sli3Words[3]);
2388 lpfc_in_buf_free(phba, dmzbuf);
2389 }
2390
2391 if (irsp->ulpBdeCount > 2) {
2392 dmzbuf = lpfc_sli_get_buff(phba, pring,
2393 irsp->unsli3.sli3Words[7]);
2394 lpfc_in_buf_free(phba, dmzbuf);
2395 }
2396
2397 return 1;
2398 }
2399
92d7f7b0 2400 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2401 if (irsp->ulpBdeCount != 0) {
2402 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2403 irsp->un.ulpWord[3]);
2404 if (!saveq->context2)
2405 lpfc_printf_log(phba,
2406 KERN_ERR,
2407 LOG_SLI,
2408 "0341 Ring %d Cannot find buffer for "
2409 "an unsolicited iocb. tag 0x%x\n",
2410 pring->ringno,
2411 irsp->un.ulpWord[3]);
76bb24ef
JS
2412 }
2413 if (irsp->ulpBdeCount == 2) {
2414 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2415 irsp->unsli3.sli3Words[7]);
2416 if (!saveq->context3)
2417 lpfc_printf_log(phba,
2418 KERN_ERR,
2419 LOG_SLI,
2420 "0342 Ring %d Cannot find buffer for an"
2421 " unsolicited iocb. tag 0x%x\n",
2422 pring->ringno,
2423 irsp->unsli3.sli3Words[7]);
2424 }
2425 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2426 irsp = &(iocbq->iocb);
76bb24ef
JS
2427 if (irsp->ulpBdeCount != 0) {
2428 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2429 irsp->un.ulpWord[3]);
9c2face6 2430 if (!iocbq->context2)
76bb24ef
JS
2431 lpfc_printf_log(phba,
2432 KERN_ERR,
2433 LOG_SLI,
2434 "0343 Ring %d Cannot find "
2435 "buffer for an unsolicited iocb"
2436 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2437 irsp->un.ulpWord[3]);
76bb24ef
JS
2438 }
2439 if (irsp->ulpBdeCount == 2) {
2440 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2441 irsp->unsli3.sli3Words[7]);
9c2face6 2442 if (!iocbq->context3)
76bb24ef
JS
2443 lpfc_printf_log(phba,
2444 KERN_ERR,
2445 LOG_SLI,
2446 "0344 Ring %d Cannot find "
2447 "buffer for an unsolicited "
2448 "iocb. tag 0x%x\n",
2449 pring->ringno,
2450 irsp->unsli3.sli3Words[7]);
2451 }
2452 }
92d7f7b0 2453 }
9c2face6
JS
2454 if (irsp->ulpBdeCount != 0 &&
2455 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2456 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2457 int found = 0;
2458
2459 /* search continue save q for same XRI */
2460 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2461 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2462 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2463 list_add_tail(&saveq->list, &iocbq->list);
2464 found = 1;
2465 break;
2466 }
2467 }
2468 if (!found)
2469 list_add_tail(&saveq->clist,
2470 &pring->iocb_continue_saveq);
2471 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2472 list_del_init(&iocbq->clist);
2473 saveq = iocbq;
2474 irsp = &(saveq->iocb);
2475 } else
2476 return 0;
2477 }
2478 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2479 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2480 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2481 Rctl = FC_RCTL_ELS_REQ;
2482 Type = FC_TYPE_ELS;
9c2face6
JS
2483 } else {
2484 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2485 Rctl = w5p->hcsw.Rctl;
2486 Type = w5p->hcsw.Type;
2487
2488 /* Firmware Workaround */
2489 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2490 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2491 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2492 Rctl = FC_RCTL_ELS_REQ;
2493 Type = FC_TYPE_ELS;
9c2face6
JS
2494 w5p->hcsw.Rctl = Rctl;
2495 w5p->hcsw.Type = Type;
2496 }
2497 }
92d7f7b0 2498
3772a991 2499 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2500 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2501 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2502 "Type x%x received\n",
e8b62011 2503 pring->ringno, Rctl, Type);
3772a991 2504
92d7f7b0 2505 return 1;
dea3101e 2506}
2507
e59058c4 2508/**
3621a710 2509 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2510 * @phba: Pointer to HBA context object.
2511 * @pring: Pointer to driver SLI ring object.
2512 * @prspiocb: Pointer to response iocb object.
2513 *
2514 * This function looks up the iocb_lookup table to get the command iocb
2515 * corresponding to the given response iocb using the iotag of the
2516 * response iocb. This function is called with the hbalock held.
2517 * This function returns the command iocb object if it finds the command
2518 * iocb else returns NULL.
2519 **/
dea3101e 2520static struct lpfc_iocbq *
2e0fef85
JS
2521lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2522 struct lpfc_sli_ring *pring,
2523 struct lpfc_iocbq *prspiocb)
dea3101e 2524{
dea3101e 2525 struct lpfc_iocbq *cmd_iocb = NULL;
2526 uint16_t iotag;
2527
604a3e30
JB
2528 iotag = prspiocb->iocb.ulpIoTag;
2529
2530 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2531 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2532 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2533 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2534 pring->txcmplq_cnt--;
2535 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2536 }
604a3e30 2537 return cmd_iocb;
dea3101e 2538 }
2539
dea3101e 2540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2541 "0317 iotag x%x is out off "
604a3e30 2542 "range: max iotag x%x wd0 x%x\n",
e8b62011 2543 iotag, phba->sli.last_iotag,
604a3e30 2544 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e 2545 return NULL;
2546}
2547
3772a991
JS
2548/**
2549 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2550 * @phba: Pointer to HBA context object.
2551 * @pring: Pointer to driver SLI ring object.
2552 * @iotag: IOCB tag.
2553 *
2554 * This function looks up the iocb_lookup table to get the command iocb
2555 * corresponding to the given iotag. This function is called with the
2556 * hbalock held.
2557 * This function returns the command iocb object if it finds the command
2558 * iocb else returns NULL.
2559 **/
2560static struct lpfc_iocbq *
2561lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2562 struct lpfc_sli_ring *pring, uint16_t iotag)
2563{
2564 struct lpfc_iocbq *cmd_iocb;
2565
2566 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2567 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2568 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2569 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2570 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2571 pring->txcmplq_cnt--;
2572 }
3772a991
JS
2573 return cmd_iocb;
2574 }
2575
2576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2577 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2578 iotag, phba->sli.last_iotag);
2579 return NULL;
2580}
2581
e59058c4 2582/**
3621a710 2583 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2584 * @phba: Pointer to HBA context object.
2585 * @pring: Pointer to driver SLI ring object.
2586 * @saveq: Pointer to the response iocb to be processed.
2587 *
2588 * This function is called by the ring event handler for non-fcp
2589 * rings when there is a new response iocb in the response ring.
2590 * The caller is not required to hold any locks. This function
2591 * gets the command iocb associated with the response iocb and
2592 * calls the completion handler for the command iocb. If there
2593 * is no completion handler, the function will free the resources
2594 * associated with command iocb. If the response iocb is for
2595 * an already aborted command iocb, the status of the completion
2596 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2597 * This function always returns 1.
2598 **/
dea3101e 2599static int
2e0fef85 2600lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e 2601 struct lpfc_iocbq *saveq)
2602{
2e0fef85 2603 struct lpfc_iocbq *cmdiocbp;
dea3101e 2604 int rc = 1;
2605 unsigned long iflag;
2606
2607 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2608 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2609 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2610 spin_unlock_irqrestore(&phba->hbalock, iflag);
2611
dea3101e 2612 if (cmdiocbp) {
2613 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2614 /*
2615 * If an ELS command failed send an event to mgmt
2616 * application.
2617 */
2618 if (saveq->iocb.ulpStatus &&
2619 (pring->ringno == LPFC_ELS_RING) &&
2620 (cmdiocbp->iocb.ulpCommand ==
2621 CMD_ELS_REQUEST64_CR))
2622 lpfc_send_els_failure_event(phba,
2623 cmdiocbp, saveq);
2624
dea3101e 2625 /*
2626 * Post all ELS completions to the worker thread.
2627 * All other are passed to the completion callback.
2628 */
2629 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2630 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2631 (cmdiocbp->iocb_flag &
2632 LPFC_DRIVER_ABORTED)) {
2633 spin_lock_irqsave(&phba->hbalock,
2634 iflag);
07951076
JS
2635 cmdiocbp->iocb_flag &=
2636 ~LPFC_DRIVER_ABORTED;
341af102
JS
2637 spin_unlock_irqrestore(&phba->hbalock,
2638 iflag);
07951076
JS
2639 saveq->iocb.ulpStatus =
2640 IOSTAT_LOCAL_REJECT;
2641 saveq->iocb.un.ulpWord[4] =
2642 IOERR_SLI_ABORTED;
0ff10d46
JS
2643
2644 /* Firmware could still be in progress
2645 * of DMAing payload, so don't free data
2646 * buffer till after a hbeat.
2647 */
341af102
JS
2648 spin_lock_irqsave(&phba->hbalock,
2649 iflag);
0ff10d46 2650 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2651 spin_unlock_irqrestore(&phba->hbalock,
2652 iflag);
2653 }
0f65ff68
JS
2654 if (phba->sli_rev == LPFC_SLI_REV4) {
2655 if (saveq->iocb_flag &
2656 LPFC_EXCHANGE_BUSY) {
2657 /* Set cmdiocb flag for the
2658 * exchange busy so sgl (xri)
2659 * will not be released until
2660 * the abort xri is received
2661 * from hba.
2662 */
2663 spin_lock_irqsave(
2664 &phba->hbalock, iflag);
2665 cmdiocbp->iocb_flag |=
2666 LPFC_EXCHANGE_BUSY;
2667 spin_unlock_irqrestore(
2668 &phba->hbalock, iflag);
2669 }
2670 if (cmdiocbp->iocb_flag &
2671 LPFC_DRIVER_ABORTED) {
2672 /*
2673 * Clear LPFC_DRIVER_ABORTED
2674 * bit in case it was driver
2675 * initiated abort.
2676 */
2677 spin_lock_irqsave(
2678 &phba->hbalock, iflag);
2679 cmdiocbp->iocb_flag &=
2680 ~LPFC_DRIVER_ABORTED;
2681 spin_unlock_irqrestore(
2682 &phba->hbalock, iflag);
2683 cmdiocbp->iocb.ulpStatus =
2684 IOSTAT_LOCAL_REJECT;
2685 cmdiocbp->iocb.un.ulpWord[4] =
2686 IOERR_ABORT_REQUESTED;
2687 /*
2688 * For SLI4, irsiocb contains
2689 * NO_XRI in sli_xritag, it
2690 * shall not affect releasing
2691 * sgl (xri) process.
2692 */
2693 saveq->iocb.ulpStatus =
2694 IOSTAT_LOCAL_REJECT;
2695 saveq->iocb.un.ulpWord[4] =
2696 IOERR_SLI_ABORTED;
2697 spin_lock_irqsave(
2698 &phba->hbalock, iflag);
2699 saveq->iocb_flag |=
2700 LPFC_DELAY_MEM_FREE;
2701 spin_unlock_irqrestore(
2702 &phba->hbalock, iflag);
2703 }
07951076 2704 }
dea3101e 2705 }
2e0fef85 2706 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2707 } else
2708 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e 2709 } else {
2710 /*
2711 * Unknown initiating command based on the response iotag.
2712 * This could be the case on the ELS ring because of
2713 * lpfc_els_abort().
2714 */
2715 if (pring->ringno != LPFC_ELS_RING) {
2716 /*
2717 * Ring <ringno> handler: unexpected completion IoTag
2718 * <IoTag>
2719 */
a257bf90 2720 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2721 "0322 Ring %d handler: "
2722 "unexpected completion IoTag x%x "
2723 "Data: x%x x%x x%x x%x\n",
2724 pring->ringno,
2725 saveq->iocb.ulpIoTag,
2726 saveq->iocb.ulpStatus,
2727 saveq->iocb.un.ulpWord[4],
2728 saveq->iocb.ulpCommand,
2729 saveq->iocb.ulpContext);
dea3101e 2730 }
2731 }
68876920 2732
dea3101e 2733 return rc;
2734}
2735
e59058c4 2736/**
3621a710 2737 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2738 * @phba: Pointer to HBA context object.
2739 * @pring: Pointer to driver SLI ring object.
2740 *
2741 * This function is called from the iocb ring event handlers when
2742 * put pointer is ahead of the get pointer for a ring. This function signal
2743 * an error attention condition to the worker thread and the worker
2744 * thread will transition the HBA to offline state.
2745 **/
2e0fef85
JS
2746static void
2747lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2748{
34b02dcd 2749 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2750 /*
025dfdaf 2751 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2752 * rsp ring <portRspMax>
2753 */
2754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2755 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2756 "is bigger than rsp ring %d\n",
e8b62011 2757 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2758 pring->numRiocb);
2759
2e0fef85 2760 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2761
2762 /*
2763 * All error attention handlers are posted to
2764 * worker thread
2765 */
2766 phba->work_ha |= HA_ERATT;
2767 phba->work_hs = HS_FFER3;
92d7f7b0 2768
5e9d9b82 2769 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2770
2771 return;
2772}
2773
9399627f 2774/**
3621a710 2775 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2776 * @ptr: Pointer to address of HBA context object.
2777 *
2778 * This function is invoked by the Error Attention polling timer when the
2779 * timer times out. It will check the SLI Error Attention register for
2780 * possible attention events. If so, it will post an Error Attention event
2781 * and wake up worker thread to process it. Otherwise, it will set up the
2782 * Error Attention polling timer for the next poll.
2783 **/
2784void lpfc_poll_eratt(unsigned long ptr)
2785{
2786 struct lpfc_hba *phba;
2787 uint32_t eratt = 0;
2788
2789 phba = (struct lpfc_hba *)ptr;
2790
2791 /* Check chip HA register for error event */
2792 eratt = lpfc_sli_check_eratt(phba);
2793
2794 if (eratt)
2795 /* Tell the worker thread there is work to do */
2796 lpfc_worker_wake_up(phba);
2797 else
2798 /* Restart the timer for next eratt poll */
2799 mod_timer(&phba->eratt_poll, jiffies +
2800 HZ * LPFC_ERATT_POLL_INTERVAL);
2801 return;
2802}
2803
875fbdfe 2804
e59058c4 2805/**
3621a710 2806 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2807 * @phba: Pointer to HBA context object.
2808 * @pring: Pointer to driver SLI ring object.
2809 * @mask: Host attention register mask for this ring.
2810 *
2811 * This function is called from the interrupt context when there is a ring
2812 * event for the fcp ring. The caller does not hold any lock.
2813 * The function processes each response iocb in the response ring until it
25985edc 2814 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
2815 * LE bit set. The function will call the completion handler of the command iocb
2816 * if the response iocb indicates a completion for a command iocb or it is
2817 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2818 * function if this is an unsolicited iocb.
dea3101e 2819 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2820 * to check it explicitly.
2821 */
2822int
2e0fef85
JS
2823lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2824 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2825{
34b02dcd 2826 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2827 IOCB_t *irsp = NULL;
87f6eaff 2828 IOCB_t *entry = NULL;
dea3101e 2829 struct lpfc_iocbq *cmdiocbq = NULL;
2830 struct lpfc_iocbq rspiocbq;
dea3101e 2831 uint32_t status;
2832 uint32_t portRspPut, portRspMax;
2833 int rc = 1;
2834 lpfc_iocb_type type;
2835 unsigned long iflag;
2836 uint32_t rsp_cmpl = 0;
dea3101e 2837
2e0fef85 2838 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2839 pring->stats.iocb_event++;
2840
dea3101e 2841 /*
2842 * The next available response entry should never exceed the maximum
2843 * entries. If it does, treat it as an adapter hardware error.
2844 */
2845 portRspMax = pring->numRiocb;
2846 portRspPut = le32_to_cpu(pgp->rspPutInx);
2847 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2848 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2849 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2850 return 1;
2851 }
45ed1190
JS
2852 if (phba->fcp_ring_in_use) {
2853 spin_unlock_irqrestore(&phba->hbalock, iflag);
2854 return 1;
2855 } else
2856 phba->fcp_ring_in_use = 1;
dea3101e 2857
2858 rmb();
2859 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2860 /*
2861 * Fetch an entry off the ring and copy it into a local data
2862 * structure. The copy involves a byte-swap since the
2863 * network byte order and pci byte orders are different.
2864 */
ed957684 2865 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2866 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2867
2868 if (++pring->rspidx >= portRspMax)
2869 pring->rspidx = 0;
2870
87f6eaff
JSEC
2871 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2872 (uint32_t *) &rspiocbq.iocb,
ed957684 2873 phba->iocb_rsp_size);
a4bc3379 2874 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2875 irsp = &rspiocbq.iocb;
2876
dea3101e 2877 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2878 pring->stats.iocb_rsp++;
2879 rsp_cmpl++;
2880
2881 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2882 /*
2883 * If resource errors reported from HBA, reduce
2884 * queuedepths of the SCSI device.
2885 */
2886 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2887 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2888 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2889 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2890 spin_lock_irqsave(&phba->hbalock, iflag);
2891 }
2892
dea3101e 2893 /* Rsp ring <ringno> error: IOCB */
2894 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2895 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2896 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2897 pring->ringno,
92d7f7b0
JS
2898 irsp->un.ulpWord[0],
2899 irsp->un.ulpWord[1],
2900 irsp->un.ulpWord[2],
2901 irsp->un.ulpWord[3],
2902 irsp->un.ulpWord[4],
2903 irsp->un.ulpWord[5],
d7c255b2
JS
2904 *(uint32_t *)&irsp->un1,
2905 *((uint32_t *)&irsp->un1 + 1));
dea3101e 2906 }
2907
2908 switch (type) {
2909 case LPFC_ABORT_IOCB:
2910 case LPFC_SOL_IOCB:
2911 /*
2912 * Idle exchange closed via ABTS from port. No iocb
2913 * resources need to be recovered.
2914 */
2915 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2917 "0333 IOCB cmd 0x%x"
dca9479b 2918 " processed. Skipping"
92d7f7b0 2919 " completion\n",
dca9479b 2920 irsp->ulpCommand);
dea3101e 2921 break;
2922 }
2923
604a3e30
JB
2924 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2925 &rspiocbq);
0f65ff68
JS
2926 if (unlikely(!cmdiocbq))
2927 break;
2928 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2929 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2930 if (cmdiocbq->iocb_cmpl) {
2931 spin_unlock_irqrestore(&phba->hbalock, iflag);
2932 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2933 &rspiocbq);
2934 spin_lock_irqsave(&phba->hbalock, iflag);
2935 }
dea3101e 2936 break;
a4bc3379 2937 case LPFC_UNSOL_IOCB:
2e0fef85 2938 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2939 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2940 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2941 break;
dea3101e 2942 default:
2943 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2944 char adaptermsg[LPFC_MAX_ADPTMSG];
2945 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2946 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2947 MAX_MSG_DATA);
898eb71c
JP
2948 dev_warn(&((phba->pcidev)->dev),
2949 "lpfc%d: %s\n",
dea3101e 2950 phba->brd_no, adaptermsg);
2951 } else {
2952 /* Unknown IOCB command */
2953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2954 "0334 Unknown IOCB command "
92d7f7b0 2955 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2956 type, irsp->ulpCommand,
92d7f7b0
JS
2957 irsp->ulpStatus,
2958 irsp->ulpIoTag,
2959 irsp->ulpContext);
dea3101e 2960 }
2961 break;
2962 }
2963
2964 /*
2965 * The response IOCB has been processed. Update the ring
2966 * pointer in SLIM. If the port response put pointer has not
2967 * been updated, sync the pgp->rspPutInx and fetch the new port
2968 * response put pointer.
2969 */
ed957684 2970 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2971
2972 if (pring->rspidx == portRspPut)
2973 portRspPut = le32_to_cpu(pgp->rspPutInx);
2974 }
2975
2976 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2977 pring->stats.iocb_rsp_full++;
2978 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2979 writel(status, phba->CAregaddr);
2980 readl(phba->CAregaddr);
2981 }
2982 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2983 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2984 pring->stats.iocb_cmd_empty++;
2985
2986 /* Force update of the local copy of cmdGetInx */
2987 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2988 lpfc_sli_resume_iocb(phba, pring);
2989
2990 if ((pring->lpfc_sli_cmd_available))
2991 (pring->lpfc_sli_cmd_available) (phba, pring);
2992
2993 }
2994
45ed1190 2995 phba->fcp_ring_in_use = 0;
2e0fef85 2996 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2997 return rc;
2998}
2999
e59058c4 3000/**
3772a991
JS
3001 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3002 * @phba: Pointer to HBA context object.
3003 * @pring: Pointer to driver SLI ring object.
3004 * @rspiocbp: Pointer to driver response IOCB object.
3005 *
3006 * This function is called from the worker thread when there is a slow-path
3007 * response IOCB to process. This function chains all the response iocbs until
3008 * seeing the iocb with the LE bit set. The function will call
3009 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3010 * completion of a command iocb. The function will call the
3011 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3012 * The function frees the resources or calls the completion handler if this
3013 * iocb is an abort completion. The function returns NULL when the response
3014 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3015 * this function shall chain the iocb on to the iocb_continueq and return the
3016 * response iocb passed in.
3017 **/
3018static struct lpfc_iocbq *
3019lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3020 struct lpfc_iocbq *rspiocbp)
3021{
3022 struct lpfc_iocbq *saveq;
3023 struct lpfc_iocbq *cmdiocbp;
3024 struct lpfc_iocbq *next_iocb;
3025 IOCB_t *irsp = NULL;
3026 uint32_t free_saveq;
3027 uint8_t iocb_cmd_type;
3028 lpfc_iocb_type type;
3029 unsigned long iflag;
3030 int rc;
3031
3032 spin_lock_irqsave(&phba->hbalock, iflag);
3033 /* First add the response iocb to the countinueq list */
3034 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3035 pring->iocb_continueq_cnt++;
3036
70f23fd6 3037 /* Now, determine whether the list is completed for processing */
3772a991
JS
3038 irsp = &rspiocbp->iocb;
3039 if (irsp->ulpLe) {
3040 /*
3041 * By default, the driver expects to free all resources
3042 * associated with this iocb completion.
3043 */
3044 free_saveq = 1;
3045 saveq = list_get_first(&pring->iocb_continueq,
3046 struct lpfc_iocbq, list);
3047 irsp = &(saveq->iocb);
3048 list_del_init(&pring->iocb_continueq);
3049 pring->iocb_continueq_cnt = 0;
3050
3051 pring->stats.iocb_rsp++;
3052
3053 /*
3054 * If resource errors reported from HBA, reduce
3055 * queuedepths of the SCSI device.
3056 */
3057 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3058 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3059 spin_unlock_irqrestore(&phba->hbalock, iflag);
3060 phba->lpfc_rampdown_queue_depth(phba);
3061 spin_lock_irqsave(&phba->hbalock, iflag);
3062 }
3063
3064 if (irsp->ulpStatus) {
3065 /* Rsp ring <ringno> error: IOCB */
3066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3067 "0328 Rsp Ring %d error: "
3068 "IOCB Data: "
3069 "x%x x%x x%x x%x "
3070 "x%x x%x x%x x%x "
3071 "x%x x%x x%x x%x "
3072 "x%x x%x x%x x%x\n",
3073 pring->ringno,
3074 irsp->un.ulpWord[0],
3075 irsp->un.ulpWord[1],
3076 irsp->un.ulpWord[2],
3077 irsp->un.ulpWord[3],
3078 irsp->un.ulpWord[4],
3079 irsp->un.ulpWord[5],
3080 *(((uint32_t *) irsp) + 6),
3081 *(((uint32_t *) irsp) + 7),
3082 *(((uint32_t *) irsp) + 8),
3083 *(((uint32_t *) irsp) + 9),
3084 *(((uint32_t *) irsp) + 10),
3085 *(((uint32_t *) irsp) + 11),
3086 *(((uint32_t *) irsp) + 12),
3087 *(((uint32_t *) irsp) + 13),
3088 *(((uint32_t *) irsp) + 14),
3089 *(((uint32_t *) irsp) + 15));
3090 }
3091
3092 /*
3093 * Fetch the IOCB command type and call the correct completion
3094 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3095 * get freed back to the lpfc_iocb_list by the discovery
3096 * kernel thread.
3097 */
3098 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3099 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3100 switch (type) {
3101 case LPFC_SOL_IOCB:
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 break;
3106
3107 case LPFC_UNSOL_IOCB:
3108 spin_unlock_irqrestore(&phba->hbalock, iflag);
3109 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3110 spin_lock_irqsave(&phba->hbalock, iflag);
3111 if (!rc)
3112 free_saveq = 0;
3113 break;
3114
3115 case LPFC_ABORT_IOCB:
3116 cmdiocbp = NULL;
3117 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3118 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3119 saveq);
3120 if (cmdiocbp) {
3121 /* Call the specified completion routine */
3122 if (cmdiocbp->iocb_cmpl) {
3123 spin_unlock_irqrestore(&phba->hbalock,
3124 iflag);
3125 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3126 saveq);
3127 spin_lock_irqsave(&phba->hbalock,
3128 iflag);
3129 } else
3130 __lpfc_sli_release_iocbq(phba,
3131 cmdiocbp);
3132 }
3133 break;
3134
3135 case LPFC_UNKNOWN_IOCB:
3136 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3137 char adaptermsg[LPFC_MAX_ADPTMSG];
3138 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3139 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3140 MAX_MSG_DATA);
3141 dev_warn(&((phba->pcidev)->dev),
3142 "lpfc%d: %s\n",
3143 phba->brd_no, adaptermsg);
3144 } else {
3145 /* Unknown IOCB command */
3146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3147 "0335 Unknown IOCB "
3148 "command Data: x%x "
3149 "x%x x%x x%x\n",
3150 irsp->ulpCommand,
3151 irsp->ulpStatus,
3152 irsp->ulpIoTag,
3153 irsp->ulpContext);
3154 }
3155 break;
3156 }
3157
3158 if (free_saveq) {
3159 list_for_each_entry_safe(rspiocbp, next_iocb,
3160 &saveq->list, list) {
3161 list_del(&rspiocbp->list);
3162 __lpfc_sli_release_iocbq(phba, rspiocbp);
3163 }
3164 __lpfc_sli_release_iocbq(phba, saveq);
3165 }
3166 rspiocbp = NULL;
3167 }
3168 spin_unlock_irqrestore(&phba->hbalock, iflag);
3169 return rspiocbp;
3170}
3171
3172/**
3173 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3174 * @phba: Pointer to HBA context object.
3175 * @pring: Pointer to driver SLI ring object.
3176 * @mask: Host attention register mask for this ring.
3177 *
3772a991
JS
3178 * This routine wraps the actual slow_ring event process routine from the
3179 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3180 **/
3772a991 3181void
2e0fef85
JS
3182lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3183 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3184{
3185 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3186}
3187
3188/**
3189 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3190 * @phba: Pointer to HBA context object.
3191 * @pring: Pointer to driver SLI ring object.
3192 * @mask: Host attention register mask for this ring.
3193 *
3194 * This function is called from the worker thread when there is a ring event
3195 * for non-fcp rings. The caller does not hold any lock. The function will
3196 * remove each response iocb in the response ring and calls the handle
3197 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3198 **/
3199static void
3200lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3201 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3202{
34b02dcd 3203 struct lpfc_pgp *pgp;
dea3101e 3204 IOCB_t *entry;
3205 IOCB_t *irsp = NULL;
3206 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3207 uint32_t portRspPut, portRspMax;
dea3101e 3208 unsigned long iflag;
3772a991 3209 uint32_t status;
dea3101e 3210
34b02dcd 3211 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3212 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 3213 pring->stats.iocb_event++;
3214
dea3101e 3215 /*
3216 * The next available response entry should never exceed the maximum
3217 * entries. If it does, treat it as an adapter hardware error.
3218 */
3219 portRspMax = pring->numRiocb;
3220 portRspPut = le32_to_cpu(pgp->rspPutInx);
3221 if (portRspPut >= portRspMax) {
3222 /*
025dfdaf 3223 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e 3224 * rsp ring <portRspMax>
3225 */
ed957684 3226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3227 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3228 "is bigger than rsp ring %d\n",
e8b62011 3229 pring->ringno, portRspPut, portRspMax);
dea3101e 3230
2e0fef85
JS
3231 phba->link_state = LPFC_HBA_ERROR;
3232 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 3233
3234 phba->work_hs = HS_FFER3;
3235 lpfc_handle_eratt(phba);
3236
3772a991 3237 return;
dea3101e 3238 }
3239
3240 rmb();
dea3101e 3241 while (pring->rspidx != portRspPut) {
3242 /*
3243 * Build a completion list and call the appropriate handler.
3244 * The process is to get the next available response iocb, get
3245 * a free iocb from the list, copy the response data into the
3246 * free iocb, insert to the continuation list, and update the
3247 * next response index to slim. This process makes response
3248 * iocb's in the ring available to DMA as fast as possible but
3249 * pays a penalty for a copy operation. Since the iocb is
3250 * only 32 bytes, this penalty is considered small relative to
3251 * the PCI reads for register values and a slim write. When
3252 * the ulpLe field is set, the entire Command has been
3253 * received.
3254 */
ed957684
JS
3255 entry = lpfc_resp_iocb(phba, pring);
3256
858c9f6c 3257 phba->last_completion_time = jiffies;
2e0fef85 3258 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 3259 if (rspiocbp == NULL) {
3260 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3261 "completion.\n", __func__);
dea3101e 3262 break;
3263 }
3264
ed957684
JS
3265 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3266 phba->iocb_rsp_size);
dea3101e 3267 irsp = &rspiocbp->iocb;
3268
3269 if (++pring->rspidx >= portRspMax)
3270 pring->rspidx = 0;
3271
a58cbd52
JS
3272 if (pring->ringno == LPFC_ELS_RING) {
3273 lpfc_debugfs_slow_ring_trc(phba,
3274 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3275 *(((uint32_t *) irsp) + 4),
3276 *(((uint32_t *) irsp) + 6),
3277 *(((uint32_t *) irsp) + 7));
3278 }
3279
ed957684 3280 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3281
3772a991
JS
3282 spin_unlock_irqrestore(&phba->hbalock, iflag);
3283 /* Handle the response IOCB */
3284 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3285 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 3286
3287 /*
3288 * If the port response put pointer has not been updated, sync
3289 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3290 * response put pointer.
3291 */
3292 if (pring->rspidx == portRspPut) {
3293 portRspPut = le32_to_cpu(pgp->rspPutInx);
3294 }
3295 } /* while (pring->rspidx != portRspPut) */
3296
92d7f7b0 3297 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e 3298 /* At least one response entry has been freed */
3299 pring->stats.iocb_rsp_full++;
3300 /* SET RxRE_RSP in Chip Att register */
3301 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3302 writel(status, phba->CAregaddr);
3303 readl(phba->CAregaddr); /* flush */
3304 }
3305 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3306 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3307 pring->stats.iocb_cmd_empty++;
3308
3309 /* Force update of the local copy of cmdGetInx */
3310 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3311 lpfc_sli_resume_iocb(phba, pring);
3312
3313 if ((pring->lpfc_sli_cmd_available))
3314 (pring->lpfc_sli_cmd_available) (phba, pring);
3315
3316 }
3317
2e0fef85 3318 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3319 return;
dea3101e 3320}
3321
4f774513
JS
3322/**
3323 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3324 * @phba: Pointer to HBA context object.
3325 * @pring: Pointer to driver SLI ring object.
3326 * @mask: Host attention register mask for this ring.
3327 *
3328 * This function is called from the worker thread when there is a pending
3329 * ELS response iocb on the driver internal slow-path response iocb worker
3330 * queue. The caller does not hold any lock. The function will remove each
3331 * response iocb from the response worker queue and calls the handle
3332 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3333 **/
3334static void
3335lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3336 struct lpfc_sli_ring *pring, uint32_t mask)
3337{
3338 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3339 struct hbq_dmabuf *dmabuf;
3340 struct lpfc_cq_event *cq_event;
4f774513
JS
3341 unsigned long iflag;
3342
45ed1190
JS
3343 spin_lock_irqsave(&phba->hbalock, iflag);
3344 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3345 spin_unlock_irqrestore(&phba->hbalock, iflag);
3346 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3347 /* Get the response iocb from the head of work queue */
3348 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3349 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3350 cq_event, struct lpfc_cq_event, list);
4f774513 3351 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3352
3353 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3354 case CQE_CODE_COMPL_WQE:
3355 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3356 cq_event);
45ed1190
JS
3357 /* Translate ELS WCQE to response IOCBQ */
3358 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3359 irspiocbq);
3360 if (irspiocbq)
3361 lpfc_sli_sp_handle_rspiocb(phba, pring,
3362 irspiocbq);
4d9ab994
JS
3363 break;
3364 case CQE_CODE_RECEIVE:
7851fe2c 3365 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3366 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3367 cq_event);
3368 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3369 break;
3370 default:
3371 break;
3372 }
4f774513
JS
3373 }
3374}
3375
e59058c4 3376/**
3621a710 3377 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3378 * @phba: Pointer to HBA context object.
3379 * @pring: Pointer to driver SLI ring object.
3380 *
3381 * This function aborts all iocbs in the given ring and frees all the iocb
3382 * objects in txq. This function issues an abort iocb for all the iocb commands
3383 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3384 * the return of this function. The caller is not required to hold any locks.
3385 **/
2e0fef85 3386void
dea3101e 3387lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3388{
2534ba75 3389 LIST_HEAD(completions);
dea3101e 3390 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3391
92d7f7b0
JS
3392 if (pring->ringno == LPFC_ELS_RING) {
3393 lpfc_fabric_abort_hba(phba);
3394 }
3395
dea3101e 3396 /* Error everything on txq and txcmplq
3397 * First do the txq.
3398 */
2e0fef85 3399 spin_lock_irq(&phba->hbalock);
2534ba75 3400 list_splice_init(&pring->txq, &completions);
dea3101e 3401 pring->txq_cnt = 0;
dea3101e 3402
3403 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3404 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3405 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3406
2e0fef85 3407 spin_unlock_irq(&phba->hbalock);
dea3101e 3408
a257bf90
JS
3409 /* Cancel all the IOCBs from the completions list */
3410 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3411 IOERR_SLI_ABORTED);
dea3101e 3412}
3413
a8e497d5 3414/**
3621a710 3415 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3416 * @phba: Pointer to HBA context object.
3417 *
3418 * This function flushes all iocbs in the fcp ring and frees all the iocb
3419 * objects in txq and txcmplq. This function will not issue abort iocbs
3420 * for all the iocb commands in txcmplq, they will just be returned with
3421 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3422 * slot has been permanently disabled.
3423 **/
3424void
3425lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3426{
3427 LIST_HEAD(txq);
3428 LIST_HEAD(txcmplq);
a8e497d5
JS
3429 struct lpfc_sli *psli = &phba->sli;
3430 struct lpfc_sli_ring *pring;
3431
3432 /* Currently, only one fcp ring */
3433 pring = &psli->ring[psli->fcp_ring];
3434
3435 spin_lock_irq(&phba->hbalock);
3436 /* Retrieve everything on txq */
3437 list_splice_init(&pring->txq, &txq);
3438 pring->txq_cnt = 0;
3439
3440 /* Retrieve everything on the txcmplq */
3441 list_splice_init(&pring->txcmplq, &txcmplq);
3442 pring->txcmplq_cnt = 0;
3443 spin_unlock_irq(&phba->hbalock);
3444
3445 /* Flush the txq */
a257bf90
JS
3446 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3447 IOERR_SLI_DOWN);
a8e497d5
JS
3448
3449 /* Flush the txcmpq */
a257bf90
JS
3450 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3451 IOERR_SLI_DOWN);
a8e497d5
JS
3452}
3453
e59058c4 3454/**
3772a991 3455 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3456 * @phba: Pointer to HBA context object.
3457 * @mask: Bit mask to be checked.
3458 *
3459 * This function reads the host status register and compares
3460 * with the provided bit mask to check if HBA completed
3461 * the restart. This function will wait in a loop for the
3462 * HBA to complete restart. If the HBA does not restart within
3463 * 15 iterations, the function will reset the HBA again. The
3464 * function returns 1 when HBA fail to restart otherwise returns
3465 * zero.
3466 **/
3772a991
JS
3467static int
3468lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3469{
41415862
JW
3470 uint32_t status;
3471 int i = 0;
3472 int retval = 0;
dea3101e 3473
41415862 3474 /* Read the HBA Host Status Register */
9940b97b
JS
3475 if (lpfc_readl(phba->HSregaddr, &status))
3476 return 1;
dea3101e 3477
41415862
JW
3478 /*
3479 * Check status register every 100ms for 5 retries, then every
3480 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3481 * every 2.5 sec for 4.
3482 * Break our of the loop if errors occurred during init.
3483 */
3484 while (((status & mask) != mask) &&
3485 !(status & HS_FFERM) &&
3486 i++ < 20) {
dea3101e 3487
41415862
JW
3488 if (i <= 5)
3489 msleep(10);
3490 else if (i <= 10)
3491 msleep(500);
3492 else
3493 msleep(2500);
dea3101e 3494
41415862 3495 if (i == 15) {
2e0fef85 3496 /* Do post */
92d7f7b0 3497 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3498 lpfc_sli_brdrestart(phba);
3499 }
3500 /* Read the HBA Host Status Register */
9940b97b
JS
3501 if (lpfc_readl(phba->HSregaddr, &status)) {
3502 retval = 1;
3503 break;
3504 }
41415862 3505 }
dea3101e 3506
41415862
JW
3507 /* Check to see if any errors occurred during init */
3508 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3510 "2751 Adapter failed to restart, "
3511 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3512 status,
3513 readl(phba->MBslimaddr + 0xa8),
3514 readl(phba->MBslimaddr + 0xac));
2e0fef85 3515 phba->link_state = LPFC_HBA_ERROR;
41415862 3516 retval = 1;
dea3101e 3517 }
dea3101e 3518
41415862
JW
3519 return retval;
3520}
dea3101e 3521
da0436e9
JS
3522/**
3523 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3524 * @phba: Pointer to HBA context object.
3525 * @mask: Bit mask to be checked.
3526 *
3527 * This function checks the host status register to check if HBA is
3528 * ready. This function will wait in a loop for the HBA to be ready
3529 * If the HBA is not ready , the function will will reset the HBA PCI
3530 * function again. The function returns 1 when HBA fail to be ready
3531 * otherwise returns zero.
3532 **/
3533static int
3534lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3535{
3536 uint32_t status;
3537 int retval = 0;
3538
3539 /* Read the HBA Host Status Register */
3540 status = lpfc_sli4_post_status_check(phba);
3541
3542 if (status) {
3543 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3544 lpfc_sli_brdrestart(phba);
3545 status = lpfc_sli4_post_status_check(phba);
3546 }
3547
3548 /* Check to see if any errors occurred during init */
3549 if (status) {
3550 phba->link_state = LPFC_HBA_ERROR;
3551 retval = 1;
3552 } else
3553 phba->sli4_hba.intr_enable = 0;
3554
3555 return retval;
3556}
3557
3558/**
3559 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3560 * @phba: Pointer to HBA context object.
3561 * @mask: Bit mask to be checked.
3562 *
3563 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3564 * from the API jump table function pointer from the lpfc_hba struct.
3565 **/
3566int
3567lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3568{
3569 return phba->lpfc_sli_brdready(phba, mask);
3570}
3571
9290831f
JS
3572#define BARRIER_TEST_PATTERN (0xdeadbeef)
3573
e59058c4 3574/**
3621a710 3575 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3576 * @phba: Pointer to HBA context object.
3577 *
3578 * This function is called before resetting an HBA. This
3579 * function requests HBA to quiesce DMAs before a reset.
3580 **/
2e0fef85 3581void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3582{
65a29c16
JS
3583 uint32_t __iomem *resp_buf;
3584 uint32_t __iomem *mbox_buf;
9290831f 3585 volatile uint32_t mbox;
9940b97b 3586 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
3587 int i;
3588 uint8_t hdrtype;
3589
3590 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3591 if (hdrtype != 0x80 ||
3592 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3593 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3594 return;
3595
3596 /*
3597 * Tell the other part of the chip to suspend temporarily all
3598 * its DMA activity.
3599 */
65a29c16 3600 resp_buf = phba->MBslimaddr;
9290831f
JS
3601
3602 /* Disable the error attention */
9940b97b
JS
3603 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3604 return;
9290831f
JS
3605 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3606 readl(phba->HCregaddr); /* flush */
2e0fef85 3607 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 3608
9940b97b
JS
3609 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3610 return;
3611 if (ha_copy & HA_ERATT) {
9290831f
JS
3612 /* Clear Chip error bit */
3613 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3614 phba->pport->stopped = 1;
9290831f
JS
3615 }
3616
3617 mbox = 0;
3618 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3619 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3620
3621 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3622 mbox_buf = phba->MBslimaddr;
9290831f
JS
3623 writel(mbox, mbox_buf);
3624
9940b97b
JS
3625 for (i = 0; i < 50; i++) {
3626 if (lpfc_readl((resp_buf + 1), &resp_data))
3627 return;
3628 if (resp_data != ~(BARRIER_TEST_PATTERN))
3629 mdelay(1);
3630 else
3631 break;
3632 }
3633 resp_data = 0;
3634 if (lpfc_readl((resp_buf + 1), &resp_data))
3635 return;
3636 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3637 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3638 phba->pport->stopped)
9290831f
JS
3639 goto restore_hc;
3640 else
3641 goto clear_errat;
3642 }
3643
3644 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
3645 resp_data = 0;
3646 for (i = 0; i < 500; i++) {
3647 if (lpfc_readl(resp_buf, &resp_data))
3648 return;
3649 if (resp_data != mbox)
3650 mdelay(1);
3651 else
3652 break;
3653 }
9290831f
JS
3654
3655clear_errat:
3656
9940b97b
JS
3657 while (++i < 500) {
3658 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3659 return;
3660 if (!(ha_copy & HA_ERATT))
3661 mdelay(1);
3662 else
3663 break;
3664 }
9290831f
JS
3665
3666 if (readl(phba->HAregaddr) & HA_ERATT) {
3667 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3668 phba->pport->stopped = 1;
9290831f
JS
3669 }
3670
3671restore_hc:
2e0fef85 3672 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3673 writel(hc_copy, phba->HCregaddr);
3674 readl(phba->HCregaddr); /* flush */
3675}
3676
e59058c4 3677/**
3621a710 3678 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3679 * @phba: Pointer to HBA context object.
3680 *
3681 * This function issues a kill_board mailbox command and waits for
3682 * the error attention interrupt. This function is called for stopping
3683 * the firmware processing. The caller is not required to hold any
3684 * locks. This function calls lpfc_hba_down_post function to free
3685 * any pending commands after the kill. The function will return 1 when it
3686 * fails to kill the board else will return 0.
3687 **/
41415862 3688int
2e0fef85 3689lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3690{
3691 struct lpfc_sli *psli;
3692 LPFC_MBOXQ_t *pmb;
3693 uint32_t status;
3694 uint32_t ha_copy;
3695 int retval;
3696 int i = 0;
dea3101e 3697
41415862 3698 psli = &phba->sli;
dea3101e 3699
41415862 3700 /* Kill HBA */
ed957684 3701 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3702 "0329 Kill HBA Data: x%x x%x\n",
3703 phba->pport->port_state, psli->sli_flag);
41415862 3704
98c9ea5c
JS
3705 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3706 if (!pmb)
41415862 3707 return 1;
41415862
JW
3708
3709 /* Disable the error attention */
2e0fef85 3710 spin_lock_irq(&phba->hbalock);
9940b97b
JS
3711 if (lpfc_readl(phba->HCregaddr, &status)) {
3712 spin_unlock_irq(&phba->hbalock);
3713 mempool_free(pmb, phba->mbox_mem_pool);
3714 return 1;
3715 }
41415862
JW
3716 status &= ~HC_ERINT_ENA;
3717 writel(status, phba->HCregaddr);
3718 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3719 phba->link_flag |= LS_IGNORE_ERATT;
3720 spin_unlock_irq(&phba->hbalock);
41415862
JW
3721
3722 lpfc_kill_board(phba, pmb);
3723 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3724 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3725
3726 if (retval != MBX_SUCCESS) {
3727 if (retval != MBX_BUSY)
3728 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3730 "2752 KILL_BOARD command failed retval %d\n",
3731 retval);
2e0fef85
JS
3732 spin_lock_irq(&phba->hbalock);
3733 phba->link_flag &= ~LS_IGNORE_ERATT;
3734 spin_unlock_irq(&phba->hbalock);
41415862
JW
3735 return 1;
3736 }
3737
f4b4c68f
JS
3738 spin_lock_irq(&phba->hbalock);
3739 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3740 spin_unlock_irq(&phba->hbalock);
9290831f 3741
41415862
JW
3742 mempool_free(pmb, phba->mbox_mem_pool);
3743
3744 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3745 * attention every 100ms for 3 seconds. If we don't get ERATT after
3746 * 3 seconds we still set HBA_ERROR state because the status of the
3747 * board is now undefined.
3748 */
9940b97b
JS
3749 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3750 return 1;
41415862
JW
3751 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3752 mdelay(100);
9940b97b
JS
3753 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3754 return 1;
41415862
JW
3755 }
3756
3757 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3758 if (ha_copy & HA_ERATT) {
3759 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3760 phba->pport->stopped = 1;
9290831f 3761 }
2e0fef85 3762 spin_lock_irq(&phba->hbalock);
41415862 3763 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3764 psli->mbox_active = NULL;
2e0fef85
JS
3765 phba->link_flag &= ~LS_IGNORE_ERATT;
3766 spin_unlock_irq(&phba->hbalock);
41415862 3767
41415862 3768 lpfc_hba_down_post(phba);
2e0fef85 3769 phba->link_state = LPFC_HBA_ERROR;
41415862 3770
2e0fef85 3771 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e 3772}
3773
e59058c4 3774/**
3772a991 3775 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3776 * @phba: Pointer to HBA context object.
3777 *
3778 * This function resets the HBA by writing HC_INITFF to the control
3779 * register. After the HBA resets, this function resets all the iocb ring
3780 * indices. This function disables PCI layer parity checking during
3781 * the reset.
3782 * This function returns 0 always.
3783 * The caller is not required to hold any locks.
3784 **/
41415862 3785int
2e0fef85 3786lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3787{
41415862 3788 struct lpfc_sli *psli;
dea3101e 3789 struct lpfc_sli_ring *pring;
41415862 3790 uint16_t cfg_value;
dea3101e 3791 int i;
dea3101e 3792
41415862 3793 psli = &phba->sli;
dea3101e 3794
41415862
JW
3795 /* Reset HBA */
3796 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3797 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3798 phba->pport->port_state, psli->sli_flag);
dea3101e 3799
3800 /* perform board reset */
3801 phba->fc_eventTag = 0;
4d9ab994 3802 phba->link_events = 0;
2e0fef85
JS
3803 phba->pport->fc_myDID = 0;
3804 phba->pport->fc_prevDID = 0;
dea3101e 3805
41415862
JW
3806 /* Turn off parity checking and serr during the physical reset */
3807 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3808 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3809 (cfg_value &
3810 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3811
3772a991
JS
3812 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3813
41415862
JW
3814 /* Now toggle INITFF bit in the Host Control Register */
3815 writel(HC_INITFF, phba->HCregaddr);
3816 mdelay(1);
3817 readl(phba->HCregaddr); /* flush */
3818 writel(0, phba->HCregaddr);
3819 readl(phba->HCregaddr); /* flush */
3820
3821 /* Restore PCI cmd register */
3822 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e 3823
3824 /* Initialize relevant SLI info */
41415862
JW
3825 for (i = 0; i < psli->num_rings; i++) {
3826 pring = &psli->ring[i];
dea3101e 3827 pring->flag = 0;
3828 pring->rspidx = 0;
3829 pring->next_cmdidx = 0;
3830 pring->local_getidx = 0;
3831 pring->cmdidx = 0;
3832 pring->missbufcnt = 0;
3833 }
dea3101e 3834
2e0fef85 3835 phba->link_state = LPFC_WARM_START;
41415862
JW
3836 return 0;
3837}
3838
e59058c4 3839/**
da0436e9
JS
3840 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3841 * @phba: Pointer to HBA context object.
3842 *
3843 * This function resets a SLI4 HBA. This function disables PCI layer parity
3844 * checking during resets the device. The caller is not required to hold
3845 * any locks.
3846 *
3847 * This function returns 0 always.
3848 **/
3849int
3850lpfc_sli4_brdreset(struct lpfc_hba *phba)
3851{
3852 struct lpfc_sli *psli = &phba->sli;
3853 uint16_t cfg_value;
3854 uint8_t qindx;
3855
3856 /* Reset HBA */
3857 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3858 "0295 Reset HBA Data: x%x x%x\n",
3859 phba->pport->port_state, psli->sli_flag);
3860
3861 /* perform board reset */
3862 phba->fc_eventTag = 0;
4d9ab994 3863 phba->link_events = 0;
da0436e9
JS
3864 phba->pport->fc_myDID = 0;
3865 phba->pport->fc_prevDID = 0;
3866
da0436e9
JS
3867 spin_lock_irq(&phba->hbalock);
3868 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3869 phba->fcf.fcf_flag = 0;
3870 /* Clean up the child queue list for the CQs */
3871 list_del_init(&phba->sli4_hba.mbx_wq->list);
3872 list_del_init(&phba->sli4_hba.els_wq->list);
3873 list_del_init(&phba->sli4_hba.hdr_rq->list);
3874 list_del_init(&phba->sli4_hba.dat_rq->list);
3875 list_del_init(&phba->sli4_hba.mbx_cq->list);
3876 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3877 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3878 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
0558056c
JS
3879 qindx = 0;
3880 do
da0436e9 3881 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
0558056c 3882 while (++qindx < phba->cfg_fcp_eq_count);
da0436e9
JS
3883 spin_unlock_irq(&phba->hbalock);
3884
3885 /* Now physically reset the device */
3886 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3887 "0389 Performing PCI function reset!\n");
be858b65
JS
3888
3889 /* Turn off parity checking and serr during the physical reset */
3890 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3891 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3892 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3893
da0436e9
JS
3894 /* Perform FCoE PCI function reset */
3895 lpfc_pci_function_reset(phba);
3896
be858b65
JS
3897 /* Restore PCI cmd register */
3898 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3899
da0436e9
JS
3900 return 0;
3901}
3902
3903/**
3904 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3905 * @phba: Pointer to HBA context object.
3906 *
3907 * This function is called in the SLI initialization code path to
3908 * restart the HBA. The caller is not required to hold any lock.
3909 * This function writes MBX_RESTART mailbox command to the SLIM and
3910 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3911 * function to free any pending commands. The function enables
3912 * POST only during the first initialization. The function returns zero.
3913 * The function does not guarantee completion of MBX_RESTART mailbox
3914 * command before the return of this function.
3915 **/
da0436e9
JS
3916static int
3917lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3918{
3919 MAILBOX_t *mb;
3920 struct lpfc_sli *psli;
41415862
JW
3921 volatile uint32_t word0;
3922 void __iomem *to_slim;
0d878419 3923 uint32_t hba_aer_enabled;
41415862 3924
2e0fef85 3925 spin_lock_irq(&phba->hbalock);
41415862 3926
0d878419
JS
3927 /* Take PCIe device Advanced Error Reporting (AER) state */
3928 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3929
41415862
JW
3930 psli = &phba->sli;
3931
3932 /* Restart HBA */
3933 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3934 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3935 phba->pport->port_state, psli->sli_flag);
41415862
JW
3936
3937 word0 = 0;
3938 mb = (MAILBOX_t *) &word0;
3939 mb->mbxCommand = MBX_RESTART;
3940 mb->mbxHc = 1;
3941
9290831f
JS
3942 lpfc_reset_barrier(phba);
3943
41415862
JW
3944 to_slim = phba->MBslimaddr;
3945 writel(*(uint32_t *) mb, to_slim);
3946 readl(to_slim); /* flush */
3947
3948 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3949 if (phba->pport->port_state)
41415862 3950 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3951 else
41415862 3952 word0 = 0; /* This is really setting up word1 */
65a29c16 3953 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3954 writel(*(uint32_t *) mb, to_slim);
3955 readl(to_slim); /* flush */
dea3101e 3956
41415862 3957 lpfc_sli_brdreset(phba);
2e0fef85
JS
3958 phba->pport->stopped = 0;
3959 phba->link_state = LPFC_INIT_START;
da0436e9 3960 phba->hba_flag = 0;
2e0fef85 3961 spin_unlock_irq(&phba->hbalock);
41415862 3962
64ba8818
JS
3963 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3964 psli->stats_start = get_seconds();
3965
eaf15d5b
JS
3966 /* Give the INITFF and Post time to settle. */
3967 mdelay(100);
41415862 3968
0d878419
JS
3969 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3970 if (hba_aer_enabled)
3971 pci_disable_pcie_error_reporting(phba->pcidev);
3972
41415862 3973 lpfc_hba_down_post(phba);
dea3101e 3974
3975 return 0;
3976}
3977
da0436e9
JS
3978/**
3979 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3980 * @phba: Pointer to HBA context object.
3981 *
3982 * This function is called in the SLI initialization code path to restart
3983 * a SLI4 HBA. The caller is not required to hold any lock.
3984 * At the end of the function, it calls lpfc_hba_down_post function to
3985 * free any pending commands.
3986 **/
3987static int
3988lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3989{
3990 struct lpfc_sli *psli = &phba->sli;
75baf696 3991 uint32_t hba_aer_enabled;
da0436e9
JS
3992
3993 /* Restart HBA */
3994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3995 "0296 Restart HBA Data: x%x x%x\n",
3996 phba->pport->port_state, psli->sli_flag);
3997
75baf696
JS
3998 /* Take PCIe device Advanced Error Reporting (AER) state */
3999 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4000
da0436e9
JS
4001 lpfc_sli4_brdreset(phba);
4002
4003 spin_lock_irq(&phba->hbalock);
4004 phba->pport->stopped = 0;
4005 phba->link_state = LPFC_INIT_START;
4006 phba->hba_flag = 0;
4007 spin_unlock_irq(&phba->hbalock);
4008
4009 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4010 psli->stats_start = get_seconds();
4011
75baf696
JS
4012 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4013 if (hba_aer_enabled)
4014 pci_disable_pcie_error_reporting(phba->pcidev);
4015
da0436e9
JS
4016 lpfc_hba_down_post(phba);
4017
4018 return 0;
4019}
4020
4021/**
4022 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4023 * @phba: Pointer to HBA context object.
4024 *
4025 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4026 * API jump table function pointer from the lpfc_hba struct.
4027**/
4028int
4029lpfc_sli_brdrestart(struct lpfc_hba *phba)
4030{
4031 return phba->lpfc_sli_brdrestart(phba);
4032}
4033
e59058c4 4034/**
3621a710 4035 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4036 * @phba: Pointer to HBA context object.
4037 *
4038 * This function is called after a HBA restart to wait for successful
4039 * restart of the HBA. Successful restart of the HBA is indicated by
4040 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4041 * iteration, the function will restart the HBA again. The function returns
4042 * zero if HBA successfully restarted else returns negative error code.
4043 **/
dea3101e 4044static int
4045lpfc_sli_chipset_init(struct lpfc_hba *phba)
4046{
4047 uint32_t status, i = 0;
4048
4049 /* Read the HBA Host Status Register */
9940b97b
JS
4050 if (lpfc_readl(phba->HSregaddr, &status))
4051 return -EIO;
dea3101e 4052
4053 /* Check status register to see what current state is */
4054 i = 0;
4055 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4056
dcf2a4e0
JS
4057 /* Check every 10ms for 10 retries, then every 100ms for 90
4058 * retries, then every 1 sec for 50 retires for a total of
4059 * ~60 seconds before reset the board again and check every
4060 * 1 sec for 50 retries. The up to 60 seconds before the
4061 * board ready is required by the Falcon FIPS zeroization
4062 * complete, and any reset the board in between shall cause
4063 * restart of zeroization, further delay the board ready.
dea3101e 4064 */
dcf2a4e0 4065 if (i++ >= 200) {
dea3101e 4066 /* Adapter failed to init, timeout, status reg
4067 <status> */
ed957684 4068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4069 "0436 Adapter failed to init, "
09372820
JS
4070 "timeout, status reg x%x, "
4071 "FW Data: A8 x%x AC x%x\n", status,
4072 readl(phba->MBslimaddr + 0xa8),
4073 readl(phba->MBslimaddr + 0xac));
2e0fef85 4074 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4075 return -ETIMEDOUT;
4076 }
4077
4078 /* Check to see if any errors occurred during init */
4079 if (status & HS_FFERM) {
4080 /* ERROR: During chipset initialization */
4081 /* Adapter failed to init, chipset, status reg
4082 <status> */
ed957684 4083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4084 "0437 Adapter failed to init, "
09372820
JS
4085 "chipset, status reg x%x, "
4086 "FW Data: A8 x%x AC x%x\n", status,
4087 readl(phba->MBslimaddr + 0xa8),
4088 readl(phba->MBslimaddr + 0xac));
2e0fef85 4089 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4090 return -EIO;
4091 }
4092
dcf2a4e0 4093 if (i <= 10)
dea3101e 4094 msleep(10);
dcf2a4e0
JS
4095 else if (i <= 100)
4096 msleep(100);
4097 else
4098 msleep(1000);
dea3101e 4099
dcf2a4e0
JS
4100 if (i == 150) {
4101 /* Do post */
92d7f7b0 4102 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4103 lpfc_sli_brdrestart(phba);
dea3101e 4104 }
4105 /* Read the HBA Host Status Register */
9940b97b
JS
4106 if (lpfc_readl(phba->HSregaddr, &status))
4107 return -EIO;
dea3101e 4108 }
4109
4110 /* Check to see if any errors occurred during init */
4111 if (status & HS_FFERM) {
4112 /* ERROR: During chipset initialization */
4113 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4115 "0438 Adapter failed to init, chipset, "
09372820
JS
4116 "status reg x%x, "
4117 "FW Data: A8 x%x AC x%x\n", status,
4118 readl(phba->MBslimaddr + 0xa8),
4119 readl(phba->MBslimaddr + 0xac));
2e0fef85 4120 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4121 return -EIO;
4122 }
4123
4124 /* Clear all interrupt enable conditions */
4125 writel(0, phba->HCregaddr);
4126 readl(phba->HCregaddr); /* flush */
4127
4128 /* setup host attn register */
4129 writel(0xffffffff, phba->HAregaddr);
4130 readl(phba->HAregaddr); /* flush */
4131 return 0;
4132}
4133
e59058c4 4134/**
3621a710 4135 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4136 *
4137 * This function calculates and returns the number of HBQs required to be
4138 * configured.
4139 **/
78b2d852 4140int
ed957684
JS
4141lpfc_sli_hbq_count(void)
4142{
92d7f7b0 4143 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4144}
4145
e59058c4 4146/**
3621a710 4147 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4148 *
4149 * This function adds the number of hbq entries in every HBQ to get
4150 * the total number of hbq entries required for the HBA and returns
4151 * the total count.
4152 **/
ed957684
JS
4153static int
4154lpfc_sli_hbq_entry_count(void)
4155{
4156 int hbq_count = lpfc_sli_hbq_count();
4157 int count = 0;
4158 int i;
4159
4160 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4161 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4162 return count;
4163}
4164
e59058c4 4165/**
3621a710 4166 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4167 *
4168 * This function calculates amount of memory required for all hbq entries
4169 * to be configured and returns the total memory required.
4170 **/
dea3101e 4171int
ed957684
JS
4172lpfc_sli_hbq_size(void)
4173{
4174 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4175}
4176
e59058c4 4177/**
3621a710 4178 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4179 * @phba: Pointer to HBA context object.
4180 *
4181 * This function is called during the SLI initialization to configure
4182 * all the HBQs and post buffers to the HBQ. The caller is not
4183 * required to hold any locks. This function will return zero if successful
4184 * else it will return negative error code.
4185 **/
ed957684
JS
4186static int
4187lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4188{
4189 int hbq_count = lpfc_sli_hbq_count();
4190 LPFC_MBOXQ_t *pmb;
4191 MAILBOX_t *pmbox;
4192 uint32_t hbqno;
4193 uint32_t hbq_entry_index;
ed957684 4194
92d7f7b0
JS
4195 /* Get a Mailbox buffer to setup mailbox
4196 * commands for HBA initialization
4197 */
ed957684
JS
4198 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4199
4200 if (!pmb)
4201 return -ENOMEM;
4202
04c68496 4203 pmbox = &pmb->u.mb;
ed957684
JS
4204
4205 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4206 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4207 phba->hbq_in_use = 1;
ed957684
JS
4208
4209 hbq_entry_index = 0;
4210 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4211 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4212 phba->hbqs[hbqno].hbqPutIdx = 0;
4213 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4214 phba->hbqs[hbqno].entry_count =
92d7f7b0 4215 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4216 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4217 hbq_entry_index, pmb);
ed957684
JS
4218 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4219
4220 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4221 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4222 mbxStatus <status>, ring <num> */
4223
4224 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4225 LOG_SLI | LOG_VPORT,
e8b62011 4226 "1805 Adapter failed to init. "
ed957684 4227 "Data: x%x x%x x%x\n",
e8b62011 4228 pmbox->mbxCommand,
ed957684
JS
4229 pmbox->mbxStatus, hbqno);
4230
4231 phba->link_state = LPFC_HBA_ERROR;
4232 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4233 return -ENXIO;
ed957684
JS
4234 }
4235 }
4236 phba->hbq_count = hbq_count;
4237
ed957684
JS
4238 mempool_free(pmb, phba->mbox_mem_pool);
4239
92d7f7b0 4240 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4241 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4242 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4243 return 0;
4244}
4245
4f774513
JS
4246/**
4247 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4248 * @phba: Pointer to HBA context object.
4249 *
4250 * This function is called during the SLI initialization to configure
4251 * all the HBQs and post buffers to the HBQ. The caller is not
4252 * required to hold any locks. This function will return zero if successful
4253 * else it will return negative error code.
4254 **/
4255static int
4256lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4257{
4258 phba->hbq_in_use = 1;
4259 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4260 phba->hbq_count = 1;
4261 /* Initially populate or replenish the HBQs */
4262 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4263 return 0;
4264}
4265
e59058c4 4266/**
3621a710 4267 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4268 * @phba: Pointer to HBA context object.
4269 * @sli_mode: sli mode - 2/3
4270 *
4271 * This function is called by the sli intialization code path
4272 * to issue config_port mailbox command. This function restarts the
4273 * HBA firmware and issues a config_port mailbox command to configure
4274 * the SLI interface in the sli mode specified by sli_mode
4275 * variable. The caller is not required to hold any locks.
4276 * The function returns 0 if successful, else returns negative error
4277 * code.
4278 **/
9399627f
JS
4279int
4280lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e 4281{
4282 LPFC_MBOXQ_t *pmb;
4283 uint32_t resetcount = 0, rc = 0, done = 0;
4284
4285 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4286 if (!pmb) {
2e0fef85 4287 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4288 return -ENOMEM;
4289 }
4290
ed957684 4291 phba->sli_rev = sli_mode;
dea3101e 4292 while (resetcount < 2 && !done) {
2e0fef85 4293 spin_lock_irq(&phba->hbalock);
1c067a42 4294 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4295 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4296 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4297 lpfc_sli_brdrestart(phba);
dea3101e 4298 rc = lpfc_sli_chipset_init(phba);
4299 if (rc)
4300 break;
4301
2e0fef85 4302 spin_lock_irq(&phba->hbalock);
1c067a42 4303 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4304 spin_unlock_irq(&phba->hbalock);
dea3101e 4305 resetcount++;
4306
ed957684
JS
4307 /* Call pre CONFIG_PORT mailbox command initialization. A
4308 * value of 0 means the call was successful. Any other
4309 * nonzero value is a failure, but if ERESTART is returned,
4310 * the driver may reset the HBA and try again.
4311 */
dea3101e 4312 rc = lpfc_config_port_prep(phba);
4313 if (rc == -ERESTART) {
ed957684 4314 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4315 continue;
34b02dcd 4316 } else if (rc)
dea3101e 4317 break;
6d368e53 4318
2e0fef85 4319 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 4320 lpfc_config_port(phba, pmb);
4321 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4322 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4323 LPFC_SLI3_HBQ_ENABLED |
4324 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4325 LPFC_SLI3_BG_ENABLED |
4326 LPFC_SLI3_DSS_ENABLED);
ed957684 4327 if (rc != MBX_SUCCESS) {
dea3101e 4328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4329 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4330 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4331 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4332 spin_lock_irq(&phba->hbalock);
04c68496 4333 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4334 spin_unlock_irq(&phba->hbalock);
4335 rc = -ENXIO;
04c68496
JS
4336 } else {
4337 /* Allow asynchronous mailbox command to go through */
4338 spin_lock_irq(&phba->hbalock);
4339 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4340 spin_unlock_irq(&phba->hbalock);
ed957684 4341 done = 1;
04c68496 4342 }
dea3101e 4343 }
ed957684
JS
4344 if (!done) {
4345 rc = -EINVAL;
4346 goto do_prep_failed;
4347 }
04c68496
JS
4348 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4349 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4350 rc = -ENXIO;
4351 goto do_prep_failed;
4352 }
04c68496 4353 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4354 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4355 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4356 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4357 phba->max_vpi : phba->max_vports;
4358
34b02dcd
JS
4359 } else
4360 phba->max_vpi = 0;
bc73905a
JS
4361 phba->fips_level = 0;
4362 phba->fips_spec_rev = 0;
4363 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4364 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4365 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4366 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4367 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4368 "2850 Security Crypto Active. FIPS x%d "
4369 "(Spec Rev: x%d)",
4370 phba->fips_level, phba->fips_spec_rev);
4371 }
4372 if (pmb->u.mb.un.varCfgPort.sec_err) {
4373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4374 "2856 Config Port Security Crypto "
4375 "Error: x%x ",
4376 pmb->u.mb.un.varCfgPort.sec_err);
4377 }
04c68496 4378 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4379 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4380 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4381 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4382
4383 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4384 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4385
4386 if (phba->cfg_enable_bg) {
04c68496 4387 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4388 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4389 else
4390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4391 "0443 Adapter did not grant "
4392 "BlockGuard\n");
4393 }
34b02dcd 4394 } else {
8f34f4ce 4395 phba->hbq_get = NULL;
34b02dcd 4396 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4397 phba->max_vpi = 0;
ed957684 4398 }
92d7f7b0 4399do_prep_failed:
ed957684
JS
4400 mempool_free(pmb, phba->mbox_mem_pool);
4401 return rc;
4402}
4403
e59058c4
JS
4404
4405/**
3621a710 4406 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4407 * @phba: Pointer to HBA context object.
4408 *
4409 * This function is the main SLI intialization function. This function
4410 * is called by the HBA intialization code, HBA reset code and HBA
4411 * error attention handler code. Caller is not required to hold any
4412 * locks. This function issues config_port mailbox command to configure
4413 * the SLI, setup iocb rings and HBQ rings. In the end the function
4414 * calls the config_port_post function to issue init_link mailbox
4415 * command and to start the discovery. The function will return zero
4416 * if successful, else it will return negative error code.
4417 **/
ed957684
JS
4418int
4419lpfc_sli_hba_setup(struct lpfc_hba *phba)
4420{
4421 uint32_t rc;
6d368e53
JS
4422 int mode = 3, i;
4423 int longs;
ed957684
JS
4424
4425 switch (lpfc_sli_mode) {
4426 case 2:
78b2d852 4427 if (phba->cfg_enable_npiv) {
92d7f7b0 4428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4429 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4430 "parameter (%d) to auto (0).\n",
e8b62011 4431 lpfc_sli_mode);
92d7f7b0
JS
4432 break;
4433 }
ed957684
JS
4434 mode = 2;
4435 break;
4436 case 0:
4437 case 3:
4438 break;
4439 default:
92d7f7b0 4440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4441 "1819 Unrecognized lpfc_sli_mode "
4442 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4443
4444 break;
4445 }
4446
9399627f
JS
4447 rc = lpfc_sli_config_port(phba, mode);
4448
ed957684 4449 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4451 "1820 Unable to select SLI-3. "
4452 "Not supported by adapter.\n");
ed957684 4453 if (rc && mode != 2)
9399627f 4454 rc = lpfc_sli_config_port(phba, 2);
ed957684 4455 if (rc)
dea3101e 4456 goto lpfc_sli_hba_setup_error;
4457
0d878419
JS
4458 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4459 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4460 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4461 if (!rc) {
4462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4463 "2709 This device supports "
4464 "Advanced Error Reporting (AER)\n");
4465 spin_lock_irq(&phba->hbalock);
4466 phba->hba_flag |= HBA_AER_ENABLED;
4467 spin_unlock_irq(&phba->hbalock);
4468 } else {
4469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4470 "2708 This device does not support "
4471 "Advanced Error Reporting (AER)\n");
4472 phba->cfg_aer_support = 0;
4473 }
4474 }
4475
ed957684
JS
4476 if (phba->sli_rev == 3) {
4477 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4478 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4479 } else {
4480 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4481 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4482 phba->sli3_options = 0;
ed957684
JS
4483 }
4484
4485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4486 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4487 phba->sli_rev, phba->max_vpi);
ed957684 4488 rc = lpfc_sli_ring_map(phba);
dea3101e 4489
4490 if (rc)
4491 goto lpfc_sli_hba_setup_error;
4492
6d368e53
JS
4493 /* Initialize VPIs. */
4494 if (phba->sli_rev == LPFC_SLI_REV3) {
4495 /*
4496 * The VPI bitmask and physical ID array are allocated
4497 * and initialized once only - at driver load. A port
4498 * reset doesn't need to reinitialize this memory.
4499 */
4500 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4501 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4502 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4503 GFP_KERNEL);
4504 if (!phba->vpi_bmask) {
4505 rc = -ENOMEM;
4506 goto lpfc_sli_hba_setup_error;
4507 }
4508
4509 phba->vpi_ids = kzalloc(
4510 (phba->max_vpi+1) * sizeof(uint16_t),
4511 GFP_KERNEL);
4512 if (!phba->vpi_ids) {
4513 kfree(phba->vpi_bmask);
4514 rc = -ENOMEM;
4515 goto lpfc_sli_hba_setup_error;
4516 }
4517 for (i = 0; i < phba->max_vpi; i++)
4518 phba->vpi_ids[i] = i;
4519 }
4520 }
4521
9399627f 4522 /* Init HBQs */
ed957684
JS
4523 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4524 rc = lpfc_sli_hbq_setup(phba);
4525 if (rc)
4526 goto lpfc_sli_hba_setup_error;
4527 }
04c68496 4528 spin_lock_irq(&phba->hbalock);
dea3101e 4529 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4530 spin_unlock_irq(&phba->hbalock);
dea3101e 4531
4532 rc = lpfc_config_port_post(phba);
4533 if (rc)
4534 goto lpfc_sli_hba_setup_error;
4535
ed957684
JS
4536 return rc;
4537
92d7f7b0 4538lpfc_sli_hba_setup_error:
2e0fef85 4539 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4541 "0445 Firmware initialization failed\n");
dea3101e 4542 return rc;
4543}
4544
e59058c4 4545/**
da0436e9
JS
4546 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4547 * @phba: Pointer to HBA context object.
4548 * @mboxq: mailbox pointer.
4549 * This function issue a dump mailbox command to read config region
4550 * 23 and parse the records in the region and populate driver
4551 * data structure.
e59058c4 4552 **/
da0436e9
JS
4553static int
4554lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4555 LPFC_MBOXQ_t *mboxq)
dea3101e 4556{
da0436e9
JS
4557 struct lpfc_dmabuf *mp;
4558 struct lpfc_mqe *mqe;
4559 uint32_t data_length;
4560 int rc;
dea3101e 4561
da0436e9
JS
4562 /* Program the default value of vlan_id and fc_map */
4563 phba->valid_vlan = 0;
4564 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4565 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4566 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4567
da0436e9
JS
4568 mqe = &mboxq->u.mqe;
4569 if (lpfc_dump_fcoe_param(phba, mboxq))
4570 return -ENOMEM;
4571
4572 mp = (struct lpfc_dmabuf *) mboxq->context1;
4573 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4574
4575 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4576 "(%d):2571 Mailbox cmd x%x Status x%x "
4577 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4578 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4579 "CQ: x%x x%x x%x x%x\n",
4580 mboxq->vport ? mboxq->vport->vpi : 0,
4581 bf_get(lpfc_mqe_command, mqe),
4582 bf_get(lpfc_mqe_status, mqe),
4583 mqe->un.mb_words[0], mqe->un.mb_words[1],
4584 mqe->un.mb_words[2], mqe->un.mb_words[3],
4585 mqe->un.mb_words[4], mqe->un.mb_words[5],
4586 mqe->un.mb_words[6], mqe->un.mb_words[7],
4587 mqe->un.mb_words[8], mqe->un.mb_words[9],
4588 mqe->un.mb_words[10], mqe->un.mb_words[11],
4589 mqe->un.mb_words[12], mqe->un.mb_words[13],
4590 mqe->un.mb_words[14], mqe->un.mb_words[15],
4591 mqe->un.mb_words[16], mqe->un.mb_words[50],
4592 mboxq->mcqe.word0,
4593 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4594 mboxq->mcqe.trailer);
4595
4596 if (rc) {
4597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4598 kfree(mp);
4599 return -EIO;
4600 }
4601 data_length = mqe->un.mb_words[5];
a0c87cbd 4602 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4603 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4604 kfree(mp);
da0436e9 4605 return -EIO;
d11e31dd 4606 }
dea3101e 4607
da0436e9
JS
4608 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4609 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4610 kfree(mp);
4611 return 0;
4612}
e59058c4
JS
4613
4614/**
da0436e9
JS
4615 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4616 * @phba: pointer to lpfc hba data structure.
4617 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4618 * @vpd: pointer to the memory to hold resulting port vpd data.
4619 * @vpd_size: On input, the number of bytes allocated to @vpd.
4620 * On output, the number of data bytes in @vpd.
e59058c4 4621 *
da0436e9
JS
4622 * This routine executes a READ_REV SLI4 mailbox command. In
4623 * addition, this routine gets the port vpd data.
4624 *
4625 * Return codes
af901ca1 4626 * 0 - successful
d439d286 4627 * -ENOMEM - could not allocated memory.
e59058c4 4628 **/
da0436e9
JS
4629static int
4630lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4631 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4632{
da0436e9
JS
4633 int rc = 0;
4634 uint32_t dma_size;
4635 struct lpfc_dmabuf *dmabuf;
4636 struct lpfc_mqe *mqe;
dea3101e 4637
da0436e9
JS
4638 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4639 if (!dmabuf)
4640 return -ENOMEM;
4641
4642 /*
4643 * Get a DMA buffer for the vpd data resulting from the READ_REV
4644 * mailbox command.
a257bf90 4645 */
da0436e9
JS
4646 dma_size = *vpd_size;
4647 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4648 dma_size,
4649 &dmabuf->phys,
4650 GFP_KERNEL);
4651 if (!dmabuf->virt) {
4652 kfree(dmabuf);
4653 return -ENOMEM;
a257bf90 4654 }
da0436e9 4655 memset(dmabuf->virt, 0, dma_size);
a257bf90 4656
da0436e9
JS
4657 /*
4658 * The SLI4 implementation of READ_REV conflicts at word1,
4659 * bits 31:16 and SLI4 adds vpd functionality not present
4660 * in SLI3. This code corrects the conflicts.
1dcb58e5 4661 */
da0436e9
JS
4662 lpfc_read_rev(phba, mboxq);
4663 mqe = &mboxq->u.mqe;
4664 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4665 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4666 mqe->un.read_rev.word1 &= 0x0000FFFF;
4667 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4668 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4669
4670 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4671 if (rc) {
4672 dma_free_coherent(&phba->pcidev->dev, dma_size,
4673 dmabuf->virt, dmabuf->phys);
def9c7a9 4674 kfree(dmabuf);
da0436e9
JS
4675 return -EIO;
4676 }
1dcb58e5 4677
da0436e9
JS
4678 /*
4679 * The available vpd length cannot be bigger than the
4680 * DMA buffer passed to the port. Catch the less than
4681 * case and update the caller's size.
4682 */
4683 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4684 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4685
d7c47992
JS
4686 memcpy(vpd, dmabuf->virt, *vpd_size);
4687
da0436e9
JS
4688 dma_free_coherent(&phba->pcidev->dev, dma_size,
4689 dmabuf->virt, dmabuf->phys);
4690 kfree(dmabuf);
4691 return 0;
dea3101e 4692}
4693
e59058c4 4694/**
da0436e9
JS
4695 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4696 * @phba: pointer to lpfc hba data structure.
e59058c4 4697 *
da0436e9
JS
4698 * This routine is called to explicitly arm the SLI4 device's completion and
4699 * event queues
4700 **/
4701static void
4702lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4703{
4704 uint8_t fcp_eqidx;
4705
4706 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4707 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
0558056c
JS
4708 fcp_eqidx = 0;
4709 do
da0436e9
JS
4710 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4711 LPFC_QUEUE_REARM);
0558056c 4712 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
da0436e9
JS
4713 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4714 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4715 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4716 LPFC_QUEUE_REARM);
4717}
4718
6d368e53
JS
4719/**
4720 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4721 * @phba: Pointer to HBA context object.
4722 * @type: The resource extent type.
b76f2dc9
JS
4723 * @extnt_count: buffer to hold port available extent count.
4724 * @extnt_size: buffer to hold element count per extent.
6d368e53 4725 *
b76f2dc9
JS
4726 * This function calls the port and retrievs the number of available
4727 * extents and their size for a particular extent type.
4728 *
4729 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 4730 **/
b76f2dc9 4731int
6d368e53
JS
4732lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4733 uint16_t *extnt_count, uint16_t *extnt_size)
4734{
4735 int rc = 0;
4736 uint32_t length;
4737 uint32_t mbox_tmo;
4738 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4739 LPFC_MBOXQ_t *mbox;
4740
4741 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4742 if (!mbox)
4743 return -ENOMEM;
4744
4745 /* Find out how many extents are available for this resource type */
4746 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4747 sizeof(struct lpfc_sli4_cfg_mhdr));
4748 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4749 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4750 length, LPFC_SLI4_MBX_EMBED);
4751
4752 /* Send an extents count of 0 - the GET doesn't use it. */
4753 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4754 LPFC_SLI4_MBX_EMBED);
4755 if (unlikely(rc)) {
4756 rc = -EIO;
4757 goto err_exit;
4758 }
4759
4760 if (!phba->sli4_hba.intr_enable)
4761 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4762 else {
a183a15f 4763 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
4764 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4765 }
4766 if (unlikely(rc)) {
4767 rc = -EIO;
4768 goto err_exit;
4769 }
4770
4771 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4772 if (bf_get(lpfc_mbox_hdr_status,
4773 &rsrc_info->header.cfg_shdr.response)) {
4774 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4775 "2930 Failed to get resource extents "
4776 "Status 0x%x Add'l Status 0x%x\n",
4777 bf_get(lpfc_mbox_hdr_status,
4778 &rsrc_info->header.cfg_shdr.response),
4779 bf_get(lpfc_mbox_hdr_add_status,
4780 &rsrc_info->header.cfg_shdr.response));
4781 rc = -EIO;
4782 goto err_exit;
4783 }
4784
4785 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4786 &rsrc_info->u.rsp);
4787 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4788 &rsrc_info->u.rsp);
4789 err_exit:
4790 mempool_free(mbox, phba->mbox_mem_pool);
4791 return rc;
4792}
4793
4794/**
4795 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4796 * @phba: Pointer to HBA context object.
4797 * @type: The extent type to check.
4798 *
4799 * This function reads the current available extents from the port and checks
4800 * if the extent count or extent size has changed since the last access.
4801 * Callers use this routine post port reset to understand if there is a
4802 * extent reprovisioning requirement.
4803 *
4804 * Returns:
4805 * -Error: error indicates problem.
4806 * 1: Extent count or size has changed.
4807 * 0: No changes.
4808 **/
4809static int
4810lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4811{
4812 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4813 uint16_t size_diff, rsrc_ext_size;
4814 int rc = 0;
4815 struct lpfc_rsrc_blks *rsrc_entry;
4816 struct list_head *rsrc_blk_list = NULL;
4817
4818 size_diff = 0;
4819 curr_ext_cnt = 0;
4820 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4821 &rsrc_ext_cnt,
4822 &rsrc_ext_size);
4823 if (unlikely(rc))
4824 return -EIO;
4825
4826 switch (type) {
4827 case LPFC_RSC_TYPE_FCOE_RPI:
4828 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4829 break;
4830 case LPFC_RSC_TYPE_FCOE_VPI:
4831 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
4832 break;
4833 case LPFC_RSC_TYPE_FCOE_XRI:
4834 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
4835 break;
4836 case LPFC_RSC_TYPE_FCOE_VFI:
4837 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
4838 break;
4839 default:
4840 break;
4841 }
4842
4843 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
4844 curr_ext_cnt++;
4845 if (rsrc_entry->rsrc_size != rsrc_ext_size)
4846 size_diff++;
4847 }
4848
4849 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
4850 rc = 1;
4851
4852 return rc;
4853}
4854
4855/**
4856 * lpfc_sli4_cfg_post_extnts -
4857 * @phba: Pointer to HBA context object.
4858 * @extnt_cnt - number of available extents.
4859 * @type - the extent type (rpi, xri, vfi, vpi).
4860 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
4861 * @mbox - pointer to the caller's allocated mailbox structure.
4862 *
4863 * This function executes the extents allocation request. It also
4864 * takes care of the amount of memory needed to allocate or get the
4865 * allocated extents. It is the caller's responsibility to evaluate
4866 * the response.
4867 *
4868 * Returns:
4869 * -Error: Error value describes the condition found.
4870 * 0: if successful
4871 **/
4872static int
4873lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
4874 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
4875{
4876 int rc = 0;
4877 uint32_t req_len;
4878 uint32_t emb_len;
4879 uint32_t alloc_len, mbox_tmo;
4880
4881 /* Calculate the total requested length of the dma memory */
4882 req_len = *extnt_cnt * sizeof(uint16_t);
4883
4884 /*
4885 * Calculate the size of an embedded mailbox. The uint32_t
4886 * accounts for extents-specific word.
4887 */
4888 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
4889 sizeof(uint32_t);
4890
4891 /*
4892 * Presume the allocation and response will fit into an embedded
4893 * mailbox. If not true, reconfigure to a non-embedded mailbox.
4894 */
4895 *emb = LPFC_SLI4_MBX_EMBED;
4896 if (req_len > emb_len) {
4897 req_len = *extnt_cnt * sizeof(uint16_t) +
4898 sizeof(union lpfc_sli4_cfg_shdr) +
4899 sizeof(uint32_t);
4900 *emb = LPFC_SLI4_MBX_NEMBED;
4901 }
4902
4903 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4904 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
4905 req_len, *emb);
4906 if (alloc_len < req_len) {
4907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 4908 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
4909 "less than the requested DMA memory "
4910 "size (x%x)\n", alloc_len, req_len);
4911 return -ENOMEM;
4912 }
4913 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
4914 if (unlikely(rc))
4915 return -EIO;
4916
4917 if (!phba->sli4_hba.intr_enable)
4918 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4919 else {
a183a15f 4920 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
4921 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4922 }
4923
4924 if (unlikely(rc))
4925 rc = -EIO;
4926 return rc;
4927}
4928
4929/**
4930 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
4931 * @phba: Pointer to HBA context object.
4932 * @type: The resource extent type to allocate.
4933 *
4934 * This function allocates the number of elements for the specified
4935 * resource type.
4936 **/
4937static int
4938lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
4939{
4940 bool emb = false;
4941 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
4942 uint16_t rsrc_id, rsrc_start, j, k;
4943 uint16_t *ids;
4944 int i, rc;
4945 unsigned long longs;
4946 unsigned long *bmask;
4947 struct lpfc_rsrc_blks *rsrc_blks;
4948 LPFC_MBOXQ_t *mbox;
4949 uint32_t length;
4950 struct lpfc_id_range *id_array = NULL;
4951 void *virtaddr = NULL;
4952 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
4953 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
4954 struct list_head *ext_blk_list;
4955
4956 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4957 &rsrc_cnt,
4958 &rsrc_size);
4959 if (unlikely(rc))
4960 return -EIO;
4961
4962 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
4963 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4964 "3009 No available Resource Extents "
4965 "for resource type 0x%x: Count: 0x%x, "
4966 "Size 0x%x\n", type, rsrc_cnt,
4967 rsrc_size);
4968 return -ENOMEM;
4969 }
4970
4971 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
4972 "2903 Available Resource Extents "
4973 "for resource type 0x%x: Count: 0x%x, "
4974 "Size 0x%x\n", type, rsrc_cnt,
4975 rsrc_size);
4976
4977 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4978 if (!mbox)
4979 return -ENOMEM;
4980
4981 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
4982 if (unlikely(rc)) {
4983 rc = -EIO;
4984 goto err_exit;
4985 }
4986
4987 /*
4988 * Figure out where the response is located. Then get local pointers
4989 * to the response data. The port does not guarantee to respond to
4990 * all extents counts request so update the local variable with the
4991 * allocated count from the port.
4992 */
4993 if (emb == LPFC_SLI4_MBX_EMBED) {
4994 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
4995 id_array = &rsrc_ext->u.rsp.id[0];
4996 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
4997 } else {
4998 virtaddr = mbox->sge_array->addr[0];
4999 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5000 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5001 id_array = &n_rsrc->id;
5002 }
5003
5004 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5005 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5006
5007 /*
5008 * Based on the resource size and count, correct the base and max
5009 * resource values.
5010 */
5011 length = sizeof(struct lpfc_rsrc_blks);
5012 switch (type) {
5013 case LPFC_RSC_TYPE_FCOE_RPI:
5014 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5015 sizeof(unsigned long),
5016 GFP_KERNEL);
5017 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5018 rc = -ENOMEM;
5019 goto err_exit;
5020 }
5021 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5022 sizeof(uint16_t),
5023 GFP_KERNEL);
5024 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5025 kfree(phba->sli4_hba.rpi_bmask);
5026 rc = -ENOMEM;
5027 goto err_exit;
5028 }
5029
5030 /*
5031 * The next_rpi was initialized with the maximum available
5032 * count but the port may allocate a smaller number. Catch
5033 * that case and update the next_rpi.
5034 */
5035 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5036
5037 /* Initialize local ptrs for common extent processing later. */
5038 bmask = phba->sli4_hba.rpi_bmask;
5039 ids = phba->sli4_hba.rpi_ids;
5040 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5041 break;
5042 case LPFC_RSC_TYPE_FCOE_VPI:
5043 phba->vpi_bmask = kzalloc(longs *
5044 sizeof(unsigned long),
5045 GFP_KERNEL);
5046 if (unlikely(!phba->vpi_bmask)) {
5047 rc = -ENOMEM;
5048 goto err_exit;
5049 }
5050 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5051 sizeof(uint16_t),
5052 GFP_KERNEL);
5053 if (unlikely(!phba->vpi_ids)) {
5054 kfree(phba->vpi_bmask);
5055 rc = -ENOMEM;
5056 goto err_exit;
5057 }
5058
5059 /* Initialize local ptrs for common extent processing later. */
5060 bmask = phba->vpi_bmask;
5061 ids = phba->vpi_ids;
5062 ext_blk_list = &phba->lpfc_vpi_blk_list;
5063 break;
5064 case LPFC_RSC_TYPE_FCOE_XRI:
5065 phba->sli4_hba.xri_bmask = kzalloc(longs *
5066 sizeof(unsigned long),
5067 GFP_KERNEL);
5068 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5069 rc = -ENOMEM;
5070 goto err_exit;
5071 }
5072 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5073 sizeof(uint16_t),
5074 GFP_KERNEL);
5075 if (unlikely(!phba->sli4_hba.xri_ids)) {
5076 kfree(phba->sli4_hba.xri_bmask);
5077 rc = -ENOMEM;
5078 goto err_exit;
5079 }
5080
5081 /* Initialize local ptrs for common extent processing later. */
5082 bmask = phba->sli4_hba.xri_bmask;
5083 ids = phba->sli4_hba.xri_ids;
5084 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5085 break;
5086 case LPFC_RSC_TYPE_FCOE_VFI:
5087 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5088 sizeof(unsigned long),
5089 GFP_KERNEL);
5090 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5091 rc = -ENOMEM;
5092 goto err_exit;
5093 }
5094 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5095 sizeof(uint16_t),
5096 GFP_KERNEL);
5097 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5098 kfree(phba->sli4_hba.vfi_bmask);
5099 rc = -ENOMEM;
5100 goto err_exit;
5101 }
5102
5103 /* Initialize local ptrs for common extent processing later. */
5104 bmask = phba->sli4_hba.vfi_bmask;
5105 ids = phba->sli4_hba.vfi_ids;
5106 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5107 break;
5108 default:
5109 /* Unsupported Opcode. Fail call. */
5110 id_array = NULL;
5111 bmask = NULL;
5112 ids = NULL;
5113 ext_blk_list = NULL;
5114 goto err_exit;
5115 }
5116
5117 /*
5118 * Complete initializing the extent configuration with the
5119 * allocated ids assigned to this function. The bitmask serves
5120 * as an index into the array and manages the available ids. The
5121 * array just stores the ids communicated to the port via the wqes.
5122 */
5123 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5124 if ((i % 2) == 0)
5125 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5126 &id_array[k]);
5127 else
5128 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5129 &id_array[k]);
5130
5131 rsrc_blks = kzalloc(length, GFP_KERNEL);
5132 if (unlikely(!rsrc_blks)) {
5133 rc = -ENOMEM;
5134 kfree(bmask);
5135 kfree(ids);
5136 goto err_exit;
5137 }
5138 rsrc_blks->rsrc_start = rsrc_id;
5139 rsrc_blks->rsrc_size = rsrc_size;
5140 list_add_tail(&rsrc_blks->list, ext_blk_list);
5141 rsrc_start = rsrc_id;
5142 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5143 phba->sli4_hba.scsi_xri_start = rsrc_start +
5144 lpfc_sli4_get_els_iocb_cnt(phba);
5145
5146 while (rsrc_id < (rsrc_start + rsrc_size)) {
5147 ids[j] = rsrc_id;
5148 rsrc_id++;
5149 j++;
5150 }
5151 /* Entire word processed. Get next word.*/
5152 if ((i % 2) == 1)
5153 k++;
5154 }
5155 err_exit:
5156 lpfc_sli4_mbox_cmd_free(phba, mbox);
5157 return rc;
5158}
5159
5160/**
5161 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5162 * @phba: Pointer to HBA context object.
5163 * @type: the extent's type.
5164 *
5165 * This function deallocates all extents of a particular resource type.
5166 * SLI4 does not allow for deallocating a particular extent range. It
5167 * is the caller's responsibility to release all kernel memory resources.
5168 **/
5169static int
5170lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5171{
5172 int rc;
5173 uint32_t length, mbox_tmo = 0;
5174 LPFC_MBOXQ_t *mbox;
5175 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5176 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5177
5178 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5179 if (!mbox)
5180 return -ENOMEM;
5181
5182 /*
5183 * This function sends an embedded mailbox because it only sends the
5184 * the resource type. All extents of this type are released by the
5185 * port.
5186 */
5187 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5188 sizeof(struct lpfc_sli4_cfg_mhdr));
5189 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5190 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5191 length, LPFC_SLI4_MBX_EMBED);
5192
5193 /* Send an extents count of 0 - the dealloc doesn't use it. */
5194 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5195 LPFC_SLI4_MBX_EMBED);
5196 if (unlikely(rc)) {
5197 rc = -EIO;
5198 goto out_free_mbox;
5199 }
5200 if (!phba->sli4_hba.intr_enable)
5201 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5202 else {
a183a15f 5203 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5204 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5205 }
5206 if (unlikely(rc)) {
5207 rc = -EIO;
5208 goto out_free_mbox;
5209 }
5210
5211 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5212 if (bf_get(lpfc_mbox_hdr_status,
5213 &dealloc_rsrc->header.cfg_shdr.response)) {
5214 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5215 "2919 Failed to release resource extents "
5216 "for type %d - Status 0x%x Add'l Status 0x%x. "
5217 "Resource memory not released.\n",
5218 type,
5219 bf_get(lpfc_mbox_hdr_status,
5220 &dealloc_rsrc->header.cfg_shdr.response),
5221 bf_get(lpfc_mbox_hdr_add_status,
5222 &dealloc_rsrc->header.cfg_shdr.response));
5223 rc = -EIO;
5224 goto out_free_mbox;
5225 }
5226
5227 /* Release kernel memory resources for the specific type. */
5228 switch (type) {
5229 case LPFC_RSC_TYPE_FCOE_VPI:
5230 kfree(phba->vpi_bmask);
5231 kfree(phba->vpi_ids);
5232 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5233 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5234 &phba->lpfc_vpi_blk_list, list) {
5235 list_del_init(&rsrc_blk->list);
5236 kfree(rsrc_blk);
5237 }
5238 break;
5239 case LPFC_RSC_TYPE_FCOE_XRI:
5240 kfree(phba->sli4_hba.xri_bmask);
5241 kfree(phba->sli4_hba.xri_ids);
5242 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5243 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5244 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5245 list_del_init(&rsrc_blk->list);
5246 kfree(rsrc_blk);
5247 }
5248 break;
5249 case LPFC_RSC_TYPE_FCOE_VFI:
5250 kfree(phba->sli4_hba.vfi_bmask);
5251 kfree(phba->sli4_hba.vfi_ids);
5252 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5253 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5254 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5255 list_del_init(&rsrc_blk->list);
5256 kfree(rsrc_blk);
5257 }
5258 break;
5259 case LPFC_RSC_TYPE_FCOE_RPI:
5260 /* RPI bitmask and physical id array are cleaned up earlier. */
5261 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5262 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5263 list_del_init(&rsrc_blk->list);
5264 kfree(rsrc_blk);
5265 }
5266 break;
5267 default:
5268 break;
5269 }
5270
5271 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5272
5273 out_free_mbox:
5274 mempool_free(mbox, phba->mbox_mem_pool);
5275 return rc;
5276}
5277
5278/**
5279 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5280 * @phba: Pointer to HBA context object.
5281 *
5282 * This function allocates all SLI4 resource identifiers.
5283 **/
5284int
5285lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5286{
5287 int i, rc, error = 0;
5288 uint16_t count, base;
5289 unsigned long longs;
5290
5291 if (phba->sli4_hba.extents_in_use) {
5292 /*
5293 * The port supports resource extents. The XRI, VPI, VFI, RPI
5294 * resource extent count must be read and allocated before
5295 * provisioning the resource id arrays.
5296 */
5297 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5298 LPFC_IDX_RSRC_RDY) {
5299 /*
5300 * Extent-based resources are set - the driver could
5301 * be in a port reset. Figure out if any corrective
5302 * actions need to be taken.
5303 */
5304 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5305 LPFC_RSC_TYPE_FCOE_VFI);
5306 if (rc != 0)
5307 error++;
5308 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5309 LPFC_RSC_TYPE_FCOE_VPI);
5310 if (rc != 0)
5311 error++;
5312 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5313 LPFC_RSC_TYPE_FCOE_XRI);
5314 if (rc != 0)
5315 error++;
5316 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5317 LPFC_RSC_TYPE_FCOE_RPI);
5318 if (rc != 0)
5319 error++;
5320
5321 /*
5322 * It's possible that the number of resources
5323 * provided to this port instance changed between
5324 * resets. Detect this condition and reallocate
5325 * resources. Otherwise, there is no action.
5326 */
5327 if (error) {
5328 lpfc_printf_log(phba, KERN_INFO,
5329 LOG_MBOX | LOG_INIT,
5330 "2931 Detected extent resource "
5331 "change. Reallocating all "
5332 "extents.\n");
5333 rc = lpfc_sli4_dealloc_extent(phba,
5334 LPFC_RSC_TYPE_FCOE_VFI);
5335 rc = lpfc_sli4_dealloc_extent(phba,
5336 LPFC_RSC_TYPE_FCOE_VPI);
5337 rc = lpfc_sli4_dealloc_extent(phba,
5338 LPFC_RSC_TYPE_FCOE_XRI);
5339 rc = lpfc_sli4_dealloc_extent(phba,
5340 LPFC_RSC_TYPE_FCOE_RPI);
5341 } else
5342 return 0;
5343 }
5344
5345 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5346 if (unlikely(rc))
5347 goto err_exit;
5348
5349 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5350 if (unlikely(rc))
5351 goto err_exit;
5352
5353 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5354 if (unlikely(rc))
5355 goto err_exit;
5356
5357 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5358 if (unlikely(rc))
5359 goto err_exit;
5360 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5361 LPFC_IDX_RSRC_RDY);
5362 return rc;
5363 } else {
5364 /*
5365 * The port does not support resource extents. The XRI, VPI,
5366 * VFI, RPI resource ids were determined from READ_CONFIG.
5367 * Just allocate the bitmasks and provision the resource id
5368 * arrays. If a port reset is active, the resources don't
5369 * need any action - just exit.
5370 */
5371 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5372 LPFC_IDX_RSRC_RDY)
5373 return 0;
5374
5375 /* RPIs. */
5376 count = phba->sli4_hba.max_cfg_param.max_rpi;
5377 base = phba->sli4_hba.max_cfg_param.rpi_base;
5378 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5379 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5380 sizeof(unsigned long),
5381 GFP_KERNEL);
5382 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5383 rc = -ENOMEM;
5384 goto err_exit;
5385 }
5386 phba->sli4_hba.rpi_ids = kzalloc(count *
5387 sizeof(uint16_t),
5388 GFP_KERNEL);
5389 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5390 rc = -ENOMEM;
5391 goto free_rpi_bmask;
5392 }
5393
5394 for (i = 0; i < count; i++)
5395 phba->sli4_hba.rpi_ids[i] = base + i;
5396
5397 /* VPIs. */
5398 count = phba->sli4_hba.max_cfg_param.max_vpi;
5399 base = phba->sli4_hba.max_cfg_param.vpi_base;
5400 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5401 phba->vpi_bmask = kzalloc(longs *
5402 sizeof(unsigned long),
5403 GFP_KERNEL);
5404 if (unlikely(!phba->vpi_bmask)) {
5405 rc = -ENOMEM;
5406 goto free_rpi_ids;
5407 }
5408 phba->vpi_ids = kzalloc(count *
5409 sizeof(uint16_t),
5410 GFP_KERNEL);
5411 if (unlikely(!phba->vpi_ids)) {
5412 rc = -ENOMEM;
5413 goto free_vpi_bmask;
5414 }
5415
5416 for (i = 0; i < count; i++)
5417 phba->vpi_ids[i] = base + i;
5418
5419 /* XRIs. */
5420 count = phba->sli4_hba.max_cfg_param.max_xri;
5421 base = phba->sli4_hba.max_cfg_param.xri_base;
5422 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5423 phba->sli4_hba.xri_bmask = kzalloc(longs *
5424 sizeof(unsigned long),
5425 GFP_KERNEL);
5426 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5427 rc = -ENOMEM;
5428 goto free_vpi_ids;
5429 }
5430 phba->sli4_hba.xri_ids = kzalloc(count *
5431 sizeof(uint16_t),
5432 GFP_KERNEL);
5433 if (unlikely(!phba->sli4_hba.xri_ids)) {
5434 rc = -ENOMEM;
5435 goto free_xri_bmask;
5436 }
5437
5438 for (i = 0; i < count; i++)
5439 phba->sli4_hba.xri_ids[i] = base + i;
5440
5441 /* VFIs. */
5442 count = phba->sli4_hba.max_cfg_param.max_vfi;
5443 base = phba->sli4_hba.max_cfg_param.vfi_base;
5444 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5445 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5446 sizeof(unsigned long),
5447 GFP_KERNEL);
5448 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5449 rc = -ENOMEM;
5450 goto free_xri_ids;
5451 }
5452 phba->sli4_hba.vfi_ids = kzalloc(count *
5453 sizeof(uint16_t),
5454 GFP_KERNEL);
5455 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5456 rc = -ENOMEM;
5457 goto free_vfi_bmask;
5458 }
5459
5460 for (i = 0; i < count; i++)
5461 phba->sli4_hba.vfi_ids[i] = base + i;
5462
5463 /*
5464 * Mark all resources ready. An HBA reset doesn't need
5465 * to reset the initialization.
5466 */
5467 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5468 LPFC_IDX_RSRC_RDY);
5469 return 0;
5470 }
5471
5472 free_vfi_bmask:
5473 kfree(phba->sli4_hba.vfi_bmask);
5474 free_xri_ids:
5475 kfree(phba->sli4_hba.xri_ids);
5476 free_xri_bmask:
5477 kfree(phba->sli4_hba.xri_bmask);
5478 free_vpi_ids:
5479 kfree(phba->vpi_ids);
5480 free_vpi_bmask:
5481 kfree(phba->vpi_bmask);
5482 free_rpi_ids:
5483 kfree(phba->sli4_hba.rpi_ids);
5484 free_rpi_bmask:
5485 kfree(phba->sli4_hba.rpi_bmask);
5486 err_exit:
5487 return rc;
5488}
5489
5490/**
5491 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5492 * @phba: Pointer to HBA context object.
5493 *
5494 * This function allocates the number of elements for the specified
5495 * resource type.
5496 **/
5497int
5498lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5499{
5500 if (phba->sli4_hba.extents_in_use) {
5501 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5502 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5503 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5504 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5505 } else {
5506 kfree(phba->vpi_bmask);
5507 kfree(phba->vpi_ids);
5508 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5509 kfree(phba->sli4_hba.xri_bmask);
5510 kfree(phba->sli4_hba.xri_ids);
5511 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5512 kfree(phba->sli4_hba.vfi_bmask);
5513 kfree(phba->sli4_hba.vfi_ids);
5514 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5515 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5516 }
5517
5518 return 0;
5519}
5520
b76f2dc9
JS
5521/**
5522 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5523 * @phba: Pointer to HBA context object.
5524 * @type: The resource extent type.
5525 * @extnt_count: buffer to hold port extent count response
5526 * @extnt_size: buffer to hold port extent size response.
5527 *
5528 * This function calls the port to read the host allocated extents
5529 * for a particular type.
5530 **/
5531int
5532lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5533 uint16_t *extnt_cnt, uint16_t *extnt_size)
5534{
5535 bool emb;
5536 int rc = 0;
5537 uint16_t curr_blks = 0;
5538 uint32_t req_len, emb_len;
5539 uint32_t alloc_len, mbox_tmo;
5540 struct list_head *blk_list_head;
5541 struct lpfc_rsrc_blks *rsrc_blk;
5542 LPFC_MBOXQ_t *mbox;
5543 void *virtaddr = NULL;
5544 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5545 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5546 union lpfc_sli4_cfg_shdr *shdr;
5547
5548 switch (type) {
5549 case LPFC_RSC_TYPE_FCOE_VPI:
5550 blk_list_head = &phba->lpfc_vpi_blk_list;
5551 break;
5552 case LPFC_RSC_TYPE_FCOE_XRI:
5553 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5554 break;
5555 case LPFC_RSC_TYPE_FCOE_VFI:
5556 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5557 break;
5558 case LPFC_RSC_TYPE_FCOE_RPI:
5559 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5560 break;
5561 default:
5562 return -EIO;
5563 }
5564
5565 /* Count the number of extents currently allocatd for this type. */
5566 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5567 if (curr_blks == 0) {
5568 /*
5569 * The GET_ALLOCATED mailbox does not return the size,
5570 * just the count. The size should be just the size
5571 * stored in the current allocated block and all sizes
5572 * for an extent type are the same so set the return
5573 * value now.
5574 */
5575 *extnt_size = rsrc_blk->rsrc_size;
5576 }
5577 curr_blks++;
5578 }
5579
5580 /* Calculate the total requested length of the dma memory. */
5581 req_len = curr_blks * sizeof(uint16_t);
5582
5583 /*
5584 * Calculate the size of an embedded mailbox. The uint32_t
5585 * accounts for extents-specific word.
5586 */
5587 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5588 sizeof(uint32_t);
5589
5590 /*
5591 * Presume the allocation and response will fit into an embedded
5592 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5593 */
5594 emb = LPFC_SLI4_MBX_EMBED;
5595 req_len = emb_len;
5596 if (req_len > emb_len) {
5597 req_len = curr_blks * sizeof(uint16_t) +
5598 sizeof(union lpfc_sli4_cfg_shdr) +
5599 sizeof(uint32_t);
5600 emb = LPFC_SLI4_MBX_NEMBED;
5601 }
5602
5603 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5604 if (!mbox)
5605 return -ENOMEM;
5606 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5607
5608 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5609 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5610 req_len, emb);
5611 if (alloc_len < req_len) {
5612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5613 "2983 Allocated DMA memory size (x%x) is "
5614 "less than the requested DMA memory "
5615 "size (x%x)\n", alloc_len, req_len);
5616 rc = -ENOMEM;
5617 goto err_exit;
5618 }
5619 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5620 if (unlikely(rc)) {
5621 rc = -EIO;
5622 goto err_exit;
5623 }
5624
5625 if (!phba->sli4_hba.intr_enable)
5626 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5627 else {
a183a15f 5628 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
5629 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5630 }
5631
5632 if (unlikely(rc)) {
5633 rc = -EIO;
5634 goto err_exit;
5635 }
5636
5637 /*
5638 * Figure out where the response is located. Then get local pointers
5639 * to the response data. The port does not guarantee to respond to
5640 * all extents counts request so update the local variable with the
5641 * allocated count from the port.
5642 */
5643 if (emb == LPFC_SLI4_MBX_EMBED) {
5644 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5645 shdr = &rsrc_ext->header.cfg_shdr;
5646 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5647 } else {
5648 virtaddr = mbox->sge_array->addr[0];
5649 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5650 shdr = &n_rsrc->cfg_shdr;
5651 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5652 }
5653
5654 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5655 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5656 "2984 Failed to read allocated resources "
5657 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5658 type,
5659 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5660 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5661 rc = -EIO;
5662 goto err_exit;
5663 }
5664 err_exit:
5665 lpfc_sli4_mbox_cmd_free(phba, mbox);
5666 return rc;
5667}
5668
da0436e9
JS
5669/**
5670 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5671 * @phba: Pointer to HBA context object.
5672 *
5673 * This function is the main SLI4 device intialization PCI function. This
5674 * function is called by the HBA intialization code, HBA reset code and
5675 * HBA error attention handler code. Caller is not required to hold any
5676 * locks.
5677 **/
5678int
5679lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5680{
5681 int rc;
5682 LPFC_MBOXQ_t *mboxq;
5683 struct lpfc_mqe *mqe;
5684 uint8_t *vpd;
5685 uint32_t vpd_size;
5686 uint32_t ftr_rsp = 0;
5687 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5688 struct lpfc_vport *vport = phba->pport;
5689 struct lpfc_dmabuf *mp;
5690
5691 /* Perform a PCI function reset to start from clean */
5692 rc = lpfc_pci_function_reset(phba);
5693 if (unlikely(rc))
5694 return -ENODEV;
5695
5696 /* Check the HBA Host Status Register for readyness */
5697 rc = lpfc_sli4_post_status_check(phba);
5698 if (unlikely(rc))
5699 return -ENODEV;
5700 else {
5701 spin_lock_irq(&phba->hbalock);
5702 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5703 spin_unlock_irq(&phba->hbalock);
5704 }
5705
5706 /*
5707 * Allocate a single mailbox container for initializing the
5708 * port.
5709 */
5710 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5711 if (!mboxq)
5712 return -ENOMEM;
5713
5714 /*
5715 * Continue initialization with default values even if driver failed
5716 * to read FCoE param config regions
5717 */
5718 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
0558056c 5719 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
e4e74273 5720 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
5721
5722 /* Issue READ_REV to collect vpd and FW information. */
49198b37 5723 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
5724 vpd = kzalloc(vpd_size, GFP_KERNEL);
5725 if (!vpd) {
5726 rc = -ENOMEM;
5727 goto out_free_mbox;
5728 }
5729
5730 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
5731 if (unlikely(rc)) {
5732 kfree(vpd);
5733 goto out_free_mbox;
5734 }
da0436e9 5735 mqe = &mboxq->u.mqe;
f1126688
JS
5736 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5737 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
76a95d75
JS
5738 phba->hba_flag |= HBA_FCOE_MODE;
5739 else
5740 phba->hba_flag &= ~HBA_FCOE_MODE;
45ed1190
JS
5741
5742 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5743 LPFC_DCBX_CEE_MODE)
5744 phba->hba_flag |= HBA_FIP_SUPPORT;
5745 else
5746 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5747
c31098ce 5748 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
5749 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5750 "0376 READ_REV Error. SLI Level %d "
5751 "FCoE enabled %d\n",
76a95d75 5752 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 5753 rc = -EIO;
76a95d75
JS
5754 kfree(vpd);
5755 goto out_free_mbox;
da0436e9 5756 }
da0436e9
JS
5757 /*
5758 * Evaluate the read rev and vpd data. Populate the driver
5759 * state with the results. If this routine fails, the failure
5760 * is not fatal as the driver will use generic values.
5761 */
5762 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5763 if (unlikely(!rc)) {
5764 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5765 "0377 Error %d parsing vpd. "
5766 "Using defaults.\n", rc);
5767 rc = 0;
5768 }
76a95d75 5769 kfree(vpd);
da0436e9 5770
f1126688
JS
5771 /* Save information as VPD data */
5772 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5773 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5774 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5775 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5776 &mqe->un.read_rev);
5777 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5778 &mqe->un.read_rev);
5779 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5780 &mqe->un.read_rev);
5781 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5782 &mqe->un.read_rev);
5783 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5784 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5785 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5786 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5787 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5788 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5789 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5790 "(%d):0380 READ_REV Status x%x "
5791 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5792 mboxq->vport ? mboxq->vport->vpi : 0,
5793 bf_get(lpfc_mqe_status, mqe),
5794 phba->vpd.rev.opFwName,
5795 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5796 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
5797
5798 /*
5799 * Discover the port's supported feature set and match it against the
5800 * hosts requests.
5801 */
5802 lpfc_request_features(phba, mboxq);
5803 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5804 if (unlikely(rc)) {
5805 rc = -EIO;
76a95d75 5806 goto out_free_mbox;
da0436e9
JS
5807 }
5808
5809 /*
5810 * The port must support FCP initiator mode as this is the
5811 * only mode running in the host.
5812 */
5813 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5814 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5815 "0378 No support for fcpi mode.\n");
5816 ftr_rsp++;
5817 }
fedd3b7b
JS
5818 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5819 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
5820 else
5821 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
5822 /*
5823 * If the port cannot support the host's requested features
5824 * then turn off the global config parameters to disable the
5825 * feature in the driver. This is not a fatal error.
5826 */
bf08611b
JS
5827 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5828 if (phba->cfg_enable_bg) {
5829 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
5830 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
5831 else
5832 ftr_rsp++;
5833 }
da0436e9
JS
5834
5835 if (phba->max_vpi && phba->cfg_enable_npiv &&
5836 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5837 ftr_rsp++;
5838
5839 if (ftr_rsp) {
5840 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5841 "0379 Feature Mismatch Data: x%08x %08x "
5842 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
5843 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
5844 phba->cfg_enable_npiv, phba->max_vpi);
5845 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
5846 phba->cfg_enable_bg = 0;
5847 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5848 phba->cfg_enable_npiv = 0;
5849 }
5850
5851 /* These SLI3 features are assumed in SLI4 */
5852 spin_lock_irq(&phba->hbalock);
5853 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
5854 spin_unlock_irq(&phba->hbalock);
5855
6d368e53
JS
5856 /*
5857 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
5858 * calls depends on these resources to complete port setup.
5859 */
5860 rc = lpfc_sli4_alloc_resource_identifiers(phba);
5861 if (rc) {
5862 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5863 "2920 Failed to alloc Resource IDs "
5864 "rc = x%x\n", rc);
5865 goto out_free_mbox;
5866 }
5867
da0436e9 5868 /* Read the port's service parameters. */
9f1177a3
JS
5869 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
5870 if (rc) {
5871 phba->link_state = LPFC_HBA_ERROR;
5872 rc = -ENOMEM;
76a95d75 5873 goto out_free_mbox;
9f1177a3
JS
5874 }
5875
da0436e9
JS
5876 mboxq->vport = vport;
5877 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5878 mp = (struct lpfc_dmabuf *) mboxq->context1;
5879 if (rc == MBX_SUCCESS) {
5880 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
5881 rc = 0;
5882 }
5883
5884 /*
5885 * This memory was allocated by the lpfc_read_sparam routine. Release
5886 * it to the mbuf pool.
5887 */
5888 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5889 kfree(mp);
5890 mboxq->context1 = NULL;
5891 if (unlikely(rc)) {
5892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5893 "0382 READ_SPARAM command failed "
5894 "status %d, mbxStatus x%x\n",
5895 rc, bf_get(lpfc_mqe_status, mqe));
5896 phba->link_state = LPFC_HBA_ERROR;
5897 rc = -EIO;
76a95d75 5898 goto out_free_mbox;
da0436e9
JS
5899 }
5900
0558056c 5901 lpfc_update_vport_wwn(vport);
da0436e9
JS
5902
5903 /* Update the fc_host data structures with new wwn. */
5904 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
5905 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
5906
5907 /* Register SGL pool to the device using non-embedded mailbox command */
6d368e53
JS
5908 if (!phba->sli4_hba.extents_in_use) {
5909 rc = lpfc_sli4_post_els_sgl_list(phba);
5910 if (unlikely(rc)) {
5911 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5912 "0582 Error %d during els sgl post "
5913 "operation\n", rc);
5914 rc = -ENODEV;
5915 goto out_free_mbox;
5916 }
5917 } else {
5918 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
5919 if (unlikely(rc)) {
5920 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5921 "2560 Error %d during els sgl post "
5922 "operation\n", rc);
5923 rc = -ENODEV;
5924 goto out_free_mbox;
5925 }
da0436e9
JS
5926 }
5927
5928 /* Register SCSI SGL pool to the device */
5929 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
5930 if (unlikely(rc)) {
6d368e53 5931 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
5932 "0383 Error %d during scsi sgl post "
5933 "operation\n", rc);
da0436e9
JS
5934 /* Some Scsi buffers were moved to the abort scsi list */
5935 /* A pci function reset will repost them */
5936 rc = -ENODEV;
76a95d75 5937 goto out_free_mbox;
da0436e9
JS
5938 }
5939
5940 /* Post the rpi header region to the device. */
5941 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
5942 if (unlikely(rc)) {
5943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5944 "0393 Error %d during rpi post operation\n",
5945 rc);
5946 rc = -ENODEV;
76a95d75 5947 goto out_free_mbox;
da0436e9 5948 }
da0436e9
JS
5949
5950 /* Set up all the queues to the device */
5951 rc = lpfc_sli4_queue_setup(phba);
5952 if (unlikely(rc)) {
5953 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5954 "0381 Error %d during queue setup.\n ", rc);
5955 goto out_stop_timers;
5956 }
5957
5958 /* Arm the CQs and then EQs on device */
5959 lpfc_sli4_arm_cqeq_intr(phba);
5960
5961 /* Indicate device interrupt mode */
5962 phba->sli4_hba.intr_enable = 1;
5963
5964 /* Allow asynchronous mailbox command to go through */
5965 spin_lock_irq(&phba->hbalock);
5966 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5967 spin_unlock_irq(&phba->hbalock);
5968
5969 /* Post receive buffers to the device */
5970 lpfc_sli4_rb_setup(phba);
5971
fc2b989b
JS
5972 /* Reset HBA FCF states after HBA reset */
5973 phba->fcf.fcf_flag = 0;
5974 phba->fcf.current_rec.flag = 0;
5975
da0436e9 5976 /* Start the ELS watchdog timer */
8fa38513
JS
5977 mod_timer(&vport->els_tmofunc,
5978 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
5979
5980 /* Start heart beat timer */
5981 mod_timer(&phba->hb_tmofunc,
5982 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
5983 phba->hb_outstanding = 0;
5984 phba->last_completion_time = jiffies;
5985
5986 /* Start error attention (ERATT) polling timer */
5987 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
5988
75baf696
JS
5989 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5990 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5991 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5992 if (!rc) {
5993 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5994 "2829 This device supports "
5995 "Advanced Error Reporting (AER)\n");
5996 spin_lock_irq(&phba->hbalock);
5997 phba->hba_flag |= HBA_AER_ENABLED;
5998 spin_unlock_irq(&phba->hbalock);
5999 } else {
6000 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6001 "2830 This device does not support "
6002 "Advanced Error Reporting (AER)\n");
6003 phba->cfg_aer_support = 0;
6004 }
0a96e975 6005 rc = 0;
75baf696
JS
6006 }
6007
76a95d75
JS
6008 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6009 /*
6010 * The FC Port needs to register FCFI (index 0)
6011 */
6012 lpfc_reg_fcfi(phba, mboxq);
6013 mboxq->vport = phba->pport;
6014 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9589b062 6015 if (rc != MBX_SUCCESS)
76a95d75 6016 goto out_unset_queue;
9589b062
JS
6017 rc = 0;
6018 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6019 &mboxq->u.mqe.un.reg_fcfi);
76a95d75 6020 }
da0436e9
JS
6021 /*
6022 * The port is ready, set the host's link state to LINK_DOWN
6023 * in preparation for link interrupts.
6024 */
da0436e9
JS
6025 spin_lock_irq(&phba->hbalock);
6026 phba->link_state = LPFC_LINK_DOWN;
6027 spin_unlock_irq(&phba->hbalock);
fedd3b7b
JS
6028 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
6029 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
76a95d75 6030out_unset_queue:
da0436e9
JS
6031 /* Unset all the queues set up in this routine when error out */
6032 if (rc)
6033 lpfc_sli4_queue_unset(phba);
da0436e9
JS
6034out_stop_timers:
6035 if (rc)
6036 lpfc_stop_hba_timers(phba);
da0436e9
JS
6037out_free_mbox:
6038 mempool_free(mboxq, phba->mbox_mem_pool);
6039 return rc;
6040}
6041
6042/**
6043 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6044 * @ptr: context object - pointer to hba structure.
6045 *
6046 * This is the callback function for mailbox timer. The mailbox
6047 * timer is armed when a new mailbox command is issued and the timer
6048 * is deleted when the mailbox complete. The function is called by
6049 * the kernel timer code when a mailbox does not complete within
6050 * expected time. This function wakes up the worker thread to
6051 * process the mailbox timeout and returns. All the processing is
6052 * done by the worker thread function lpfc_mbox_timeout_handler.
6053 **/
6054void
6055lpfc_mbox_timeout(unsigned long ptr)
6056{
6057 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6058 unsigned long iflag;
6059 uint32_t tmo_posted;
6060
6061 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6062 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6063 if (!tmo_posted)
6064 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6065 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6066
6067 if (!tmo_posted)
6068 lpfc_worker_wake_up(phba);
6069 return;
6070}
6071
6072
6073/**
6074 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6075 * @phba: Pointer to HBA context object.
6076 *
6077 * This function is called from worker thread when a mailbox command times out.
6078 * The caller is not required to hold any locks. This function will reset the
6079 * HBA and recover all the pending commands.
6080 **/
6081void
6082lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6083{
6084 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 6085 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
6086 struct lpfc_sli *psli = &phba->sli;
6087 struct lpfc_sli_ring *pring;
6088
6089 /* Check the pmbox pointer first. There is a race condition
6090 * between the mbox timeout handler getting executed in the
6091 * worklist and the mailbox actually completing. When this
6092 * race condition occurs, the mbox_active will be NULL.
6093 */
6094 spin_lock_irq(&phba->hbalock);
6095 if (pmbox == NULL) {
6096 lpfc_printf_log(phba, KERN_WARNING,
6097 LOG_MBOX | LOG_SLI,
6098 "0353 Active Mailbox cleared - mailbox timeout "
6099 "exiting\n");
6100 spin_unlock_irq(&phba->hbalock);
6101 return;
6102 }
6103
6104 /* Mbox cmd <mbxCommand> timeout */
6105 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6106 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6107 mb->mbxCommand,
6108 phba->pport->port_state,
6109 phba->sli.sli_flag,
6110 phba->sli.mbox_active);
6111 spin_unlock_irq(&phba->hbalock);
6112
6113 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6114 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 6115 * it to fail all outstanding SCSI IO.
da0436e9
JS
6116 */
6117 spin_lock_irq(&phba->pport->work_port_lock);
6118 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6119 spin_unlock_irq(&phba->pport->work_port_lock);
6120 spin_lock_irq(&phba->hbalock);
6121 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 6122 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
6123 spin_unlock_irq(&phba->hbalock);
6124
6125 pring = &psli->ring[psli->fcp_ring];
6126 lpfc_sli_abort_iocb_ring(phba, pring);
6127
6128 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6129 "0345 Resetting board due to mailbox timeout\n");
6130
6131 /* Reset the HBA device */
6132 lpfc_reset_hba(phba);
6133}
6134
6135/**
6136 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6137 * @phba: Pointer to HBA context object.
6138 * @pmbox: Pointer to mailbox object.
6139 * @flag: Flag indicating how the mailbox need to be processed.
6140 *
6141 * This function is called by discovery code and HBA management code
6142 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6143 * function gets the hbalock to protect the data structures.
6144 * The mailbox command can be submitted in polling mode, in which case
6145 * this function will wait in a polling loop for the completion of the
6146 * mailbox.
6147 * If the mailbox is submitted in no_wait mode (not polling) the
6148 * function will submit the command and returns immediately without waiting
6149 * for the mailbox completion. The no_wait is supported only when HBA
6150 * is in SLI2/SLI3 mode - interrupts are enabled.
6151 * The SLI interface allows only one mailbox pending at a time. If the
6152 * mailbox is issued in polling mode and there is already a mailbox
6153 * pending, then the function will return an error. If the mailbox is issued
6154 * in NO_WAIT mode and there is a mailbox pending already, the function
6155 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6156 * The sli layer owns the mailbox object until the completion of mailbox
6157 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6158 * return codes the caller owns the mailbox command after the return of
6159 * the function.
e59058c4 6160 **/
3772a991
JS
6161static int
6162lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6163 uint32_t flag)
dea3101e 6164{
dea3101e 6165 MAILBOX_t *mb;
2e0fef85 6166 struct lpfc_sli *psli = &phba->sli;
dea3101e 6167 uint32_t status, evtctr;
9940b97b 6168 uint32_t ha_copy, hc_copy;
dea3101e 6169 int i;
09372820 6170 unsigned long timeout;
dea3101e 6171 unsigned long drvr_flag = 0;
34b02dcd 6172 uint32_t word0, ldata;
dea3101e 6173 void __iomem *to_slim;
58da1ffb
JS
6174 int processing_queue = 0;
6175
6176 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6177 if (!pmbox) {
8568a4d2 6178 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 6179 /* processing mbox queue from intr_handler */
3772a991
JS
6180 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6181 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6182 return MBX_SUCCESS;
6183 }
58da1ffb 6184 processing_queue = 1;
58da1ffb
JS
6185 pmbox = lpfc_mbox_get(phba);
6186 if (!pmbox) {
6187 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6188 return MBX_SUCCESS;
6189 }
6190 }
dea3101e 6191
ed957684 6192 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 6193 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 6194 if(!pmbox->vport) {
58da1ffb 6195 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 6196 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 6197 LOG_MBOX | LOG_VPORT,
e8b62011 6198 "1806 Mbox x%x failed. No vport\n",
3772a991 6199 pmbox->u.mb.mbxCommand);
ed957684 6200 dump_stack();
58da1ffb 6201 goto out_not_finished;
ed957684
JS
6202 }
6203 }
6204
8d63f375 6205 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
6206 if (unlikely(pci_channel_offline(phba->pcidev))) {
6207 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6208 goto out_not_finished;
6209 }
8d63f375 6210
a257bf90
JS
6211 /* If HBA has a deferred error attention, fail the iocb. */
6212 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6213 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6214 goto out_not_finished;
6215 }
6216
dea3101e 6217 psli = &phba->sli;
92d7f7b0 6218
3772a991 6219 mb = &pmbox->u.mb;
dea3101e 6220 status = MBX_SUCCESS;
6221
2e0fef85
JS
6222 if (phba->link_state == LPFC_HBA_ERROR) {
6223 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
6224
6225 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6227 "(%d):0311 Mailbox command x%x cannot "
6228 "issue Data: x%x x%x\n",
6229 pmbox->vport ? pmbox->vport->vpi : 0,
6230 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 6231 goto out_not_finished;
41415862
JW
6232 }
6233
9940b97b
JS
6234 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6235 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6236 !(hc_copy & HC_MBINT_ENA)) {
6237 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6238 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
6239 "(%d):2528 Mailbox command x%x cannot "
6240 "issue Data: x%x x%x\n",
6241 pmbox->vport ? pmbox->vport->vpi : 0,
6242 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
6243 goto out_not_finished;
6244 }
9290831f
JS
6245 }
6246
dea3101e 6247 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6248 /* Polling for a mbox command when another one is already active
6249 * is not allowed in SLI. Also, the driver must have established
6250 * SLI2 mode to queue and process multiple mbox commands.
6251 */
6252
6253 if (flag & MBX_POLL) {
2e0fef85 6254 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6255
6256 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6257 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6258 "(%d):2529 Mailbox command x%x "
6259 "cannot issue Data: x%x x%x\n",
6260 pmbox->vport ? pmbox->vport->vpi : 0,
6261 pmbox->u.mb.mbxCommand,
6262 psli->sli_flag, flag);
58da1ffb 6263 goto out_not_finished;
dea3101e 6264 }
6265
3772a991 6266 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 6267 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6268 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6269 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6270 "(%d):2530 Mailbox command x%x "
6271 "cannot issue Data: x%x x%x\n",
6272 pmbox->vport ? pmbox->vport->vpi : 0,
6273 pmbox->u.mb.mbxCommand,
6274 psli->sli_flag, flag);
58da1ffb 6275 goto out_not_finished;
dea3101e 6276 }
6277
dea3101e 6278 /* Another mailbox command is still being processed, queue this
6279 * command to be processed later.
6280 */
6281 lpfc_mbox_put(phba, pmbox);
6282
6283 /* Mbox cmd issue - BUSY */
ed957684 6284 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6285 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 6286 "x%x x%x x%x x%x\n",
92d7f7b0
JS
6287 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6288 mb->mbxCommand, phba->pport->port_state,
6289 psli->sli_flag, flag);
dea3101e 6290
6291 psli->slistat.mbox_busy++;
2e0fef85 6292 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6293
858c9f6c
JS
6294 if (pmbox->vport) {
6295 lpfc_debugfs_disc_trc(pmbox->vport,
6296 LPFC_DISC_TRC_MBOX_VPORT,
6297 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6298 (uint32_t)mb->mbxCommand,
6299 mb->un.varWords[0], mb->un.varWords[1]);
6300 }
6301 else {
6302 lpfc_debugfs_disc_trc(phba->pport,
6303 LPFC_DISC_TRC_MBOX,
6304 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6305 (uint32_t)mb->mbxCommand,
6306 mb->un.varWords[0], mb->un.varWords[1]);
6307 }
6308
2e0fef85 6309 return MBX_BUSY;
dea3101e 6310 }
6311
dea3101e 6312 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6313
6314 /* If we are not polling, we MUST be in SLI2 mode */
6315 if (flag != MBX_POLL) {
3772a991 6316 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 6317 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 6318 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 6319 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6320 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6322 "(%d):2531 Mailbox command x%x "
6323 "cannot issue Data: x%x x%x\n",
6324 pmbox->vport ? pmbox->vport->vpi : 0,
6325 pmbox->u.mb.mbxCommand,
6326 psli->sli_flag, flag);
58da1ffb 6327 goto out_not_finished;
dea3101e 6328 }
6329 /* timeout active mbox command */
a309a6b6 6330 mod_timer(&psli->mbox_tmo, (jiffies +
a183a15f 6331 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea3101e 6332 }
6333
6334 /* Mailbox cmd <cmd> issue */
ed957684 6335 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6336 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 6337 "x%x\n",
e8b62011 6338 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
6339 mb->mbxCommand, phba->pport->port_state,
6340 psli->sli_flag, flag);
dea3101e 6341
858c9f6c
JS
6342 if (mb->mbxCommand != MBX_HEARTBEAT) {
6343 if (pmbox->vport) {
6344 lpfc_debugfs_disc_trc(pmbox->vport,
6345 LPFC_DISC_TRC_MBOX_VPORT,
6346 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6347 (uint32_t)mb->mbxCommand,
6348 mb->un.varWords[0], mb->un.varWords[1]);
6349 }
6350 else {
6351 lpfc_debugfs_disc_trc(phba->pport,
6352 LPFC_DISC_TRC_MBOX,
6353 "MBOX Send: cmd:x%x mb:x%x x%x",
6354 (uint32_t)mb->mbxCommand,
6355 mb->un.varWords[0], mb->un.varWords[1]);
6356 }
6357 }
6358
dea3101e 6359 psli->slistat.mbox_cmd++;
6360 evtctr = psli->slistat.mbox_event;
6361
6362 /* next set own bit for the adapter and copy over command word */
6363 mb->mbxOwner = OWN_CHIP;
6364
3772a991 6365 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
6366 /* Populate mbox extension offset word. */
6367 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6368 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6369 = (uint8_t *)phba->mbox_ext
6370 - (uint8_t *)phba->mbox;
6371 }
6372
6373 /* Copy the mailbox extension data */
6374 if (pmbox->in_ext_byte_len && pmbox->context2) {
6375 lpfc_sli_pcimem_bcopy(pmbox->context2,
6376 (uint8_t *)phba->mbox_ext,
6377 pmbox->in_ext_byte_len);
6378 }
6379 /* Copy command data to host SLIM area */
34b02dcd 6380 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 6381 } else {
7a470277
JS
6382 /* Populate mbox extension offset word. */
6383 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6384 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6385 = MAILBOX_HBA_EXT_OFFSET;
6386
6387 /* Copy the mailbox extension data */
6388 if (pmbox->in_ext_byte_len && pmbox->context2) {
6389 lpfc_memcpy_to_slim(phba->MBslimaddr +
6390 MAILBOX_HBA_EXT_OFFSET,
6391 pmbox->context2, pmbox->in_ext_byte_len);
6392
6393 }
9290831f 6394 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 6395 /* copy command data into host mbox for cmpl */
34b02dcd 6396 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 6397 }
6398
6399 /* First copy mbox command data to HBA SLIM, skip past first
6400 word */
6401 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6402 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6403 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6404
6405 /* Next copy over first word, with mbxOwner set */
34b02dcd 6406 ldata = *((uint32_t *)mb);
dea3101e 6407 to_slim = phba->MBslimaddr;
6408 writel(ldata, to_slim);
6409 readl(to_slim); /* flush */
6410
6411 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6412 /* switch over to host mailbox */
3772a991 6413 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e 6414 }
6415 }
6416
6417 wmb();
dea3101e 6418
6419 switch (flag) {
6420 case MBX_NOWAIT:
09372820 6421 /* Set up reference to mailbox command */
dea3101e 6422 psli->mbox_active = pmbox;
09372820
JS
6423 /* Interrupt board to do it */
6424 writel(CA_MBATT, phba->CAregaddr);
6425 readl(phba->CAregaddr); /* flush */
6426 /* Don't wait for it to finish, just return */
dea3101e 6427 break;
6428
6429 case MBX_POLL:
09372820 6430 /* Set up null reference to mailbox command */
dea3101e 6431 psli->mbox_active = NULL;
09372820
JS
6432 /* Interrupt board to do it */
6433 writel(CA_MBATT, phba->CAregaddr);
6434 readl(phba->CAregaddr); /* flush */
6435
3772a991 6436 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6437 /* First read mbox status word */
34b02dcd 6438 word0 = *((uint32_t *)phba->mbox);
dea3101e 6439 word0 = le32_to_cpu(word0);
6440 } else {
6441 /* First read mbox status word */
9940b97b
JS
6442 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6443 spin_unlock_irqrestore(&phba->hbalock,
6444 drvr_flag);
6445 goto out_not_finished;
6446 }
dea3101e 6447 }
6448
6449 /* Read the HBA Host Attention Register */
9940b97b
JS
6450 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6451 spin_unlock_irqrestore(&phba->hbalock,
6452 drvr_flag);
6453 goto out_not_finished;
6454 }
a183a15f
JS
6455 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6456 1000) + jiffies;
09372820 6457 i = 0;
dea3101e 6458 /* Wait for command to complete */
41415862
JW
6459 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6460 (!(ha_copy & HA_MBATT) &&
2e0fef85 6461 (phba->link_state > LPFC_WARM_START))) {
09372820 6462 if (time_after(jiffies, timeout)) {
dea3101e 6463 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 6464 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 6465 drvr_flag);
58da1ffb 6466 goto out_not_finished;
dea3101e 6467 }
6468
6469 /* Check if we took a mbox interrupt while we were
6470 polling */
6471 if (((word0 & OWN_CHIP) != OWN_CHIP)
6472 && (evtctr != psli->slistat.mbox_event))
6473 break;
6474
09372820
JS
6475 if (i++ > 10) {
6476 spin_unlock_irqrestore(&phba->hbalock,
6477 drvr_flag);
6478 msleep(1);
6479 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6480 }
dea3101e 6481
3772a991 6482 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6483 /* First copy command data */
34b02dcd 6484 word0 = *((uint32_t *)phba->mbox);
dea3101e 6485 word0 = le32_to_cpu(word0);
6486 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6487 MAILBOX_t *slimmb;
34b02dcd 6488 uint32_t slimword0;
dea3101e 6489 /* Check real SLIM for any errors */
6490 slimword0 = readl(phba->MBslimaddr);
6491 slimmb = (MAILBOX_t *) & slimword0;
6492 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6493 && slimmb->mbxStatus) {
6494 psli->sli_flag &=
3772a991 6495 ~LPFC_SLI_ACTIVE;
dea3101e 6496 word0 = slimword0;
6497 }
6498 }
6499 } else {
6500 /* First copy command data */
6501 word0 = readl(phba->MBslimaddr);
6502 }
6503 /* Read the HBA Host Attention Register */
9940b97b
JS
6504 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6505 spin_unlock_irqrestore(&phba->hbalock,
6506 drvr_flag);
6507 goto out_not_finished;
6508 }
dea3101e 6509 }
6510
3772a991 6511 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6512 /* copy results back to user */
34b02dcd 6513 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
7a470277
JS
6514 /* Copy the mailbox extension data */
6515 if (pmbox->out_ext_byte_len && pmbox->context2) {
6516 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6517 pmbox->context2,
6518 pmbox->out_ext_byte_len);
6519 }
dea3101e 6520 } else {
6521 /* First copy command data */
6522 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6523 MAILBOX_CMD_SIZE);
7a470277
JS
6524 /* Copy the mailbox extension data */
6525 if (pmbox->out_ext_byte_len && pmbox->context2) {
6526 lpfc_memcpy_from_slim(pmbox->context2,
6527 phba->MBslimaddr +
6528 MAILBOX_HBA_EXT_OFFSET,
6529 pmbox->out_ext_byte_len);
dea3101e 6530 }
6531 }
6532
6533 writel(HA_MBATT, phba->HAregaddr);
6534 readl(phba->HAregaddr); /* flush */
6535
6536 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6537 status = mb->mbxStatus;
6538 }
6539
2e0fef85
JS
6540 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6541 return status;
58da1ffb
JS
6542
6543out_not_finished:
6544 if (processing_queue) {
da0436e9 6545 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
6546 lpfc_mbox_cmpl_put(phba, pmbox);
6547 }
6548 return MBX_NOT_FINISHED;
dea3101e 6549}
6550
f1126688
JS
6551/**
6552 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6553 * @phba: Pointer to HBA context object.
6554 *
6555 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6556 * the driver internal pending mailbox queue. It will then try to wait out the
6557 * possible outstanding mailbox command before return.
6558 *
6559 * Returns:
6560 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6561 * the outstanding mailbox command timed out.
6562 **/
6563static int
6564lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6565{
6566 struct lpfc_sli *psli = &phba->sli;
f1126688 6567 int rc = 0;
a183a15f 6568 unsigned long timeout = 0;
f1126688
JS
6569
6570 /* Mark the asynchronous mailbox command posting as blocked */
6571 spin_lock_irq(&phba->hbalock);
6572 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
6573 /* Determine how long we might wait for the active mailbox
6574 * command to be gracefully completed by firmware.
6575 */
a183a15f
JS
6576 if (phba->sli.mbox_active)
6577 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6578 phba->sli.mbox_active) *
6579 1000) + jiffies;
6580 spin_unlock_irq(&phba->hbalock);
6581
f1126688
JS
6582 /* Wait for the outstnading mailbox command to complete */
6583 while (phba->sli.mbox_active) {
6584 /* Check active mailbox complete status every 2ms */
6585 msleep(2);
6586 if (time_after(jiffies, timeout)) {
6587 /* Timeout, marked the outstanding cmd not complete */
6588 rc = 1;
6589 break;
6590 }
6591 }
6592
6593 /* Can not cleanly block async mailbox command, fails it */
6594 if (rc) {
6595 spin_lock_irq(&phba->hbalock);
6596 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6597 spin_unlock_irq(&phba->hbalock);
6598 }
6599 return rc;
6600}
6601
6602/**
6603 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6604 * @phba: Pointer to HBA context object.
6605 *
6606 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6607 * commands from the driver internal pending mailbox queue. It makes sure
6608 * that there is no outstanding mailbox command before resuming posting
6609 * asynchronous mailbox commands. If, for any reason, there is outstanding
6610 * mailbox command, it will try to wait it out before resuming asynchronous
6611 * mailbox command posting.
6612 **/
6613static void
6614lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6615{
6616 struct lpfc_sli *psli = &phba->sli;
6617
6618 spin_lock_irq(&phba->hbalock);
6619 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6620 /* Asynchronous mailbox posting is not blocked, do nothing */
6621 spin_unlock_irq(&phba->hbalock);
6622 return;
6623 }
6624
6625 /* Outstanding synchronous mailbox command is guaranteed to be done,
6626 * successful or timeout, after timing-out the outstanding mailbox
6627 * command shall always be removed, so just unblock posting async
6628 * mailbox command and resume
6629 */
6630 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6631 spin_unlock_irq(&phba->hbalock);
6632
6633 /* wake up worker thread to post asynchronlous mailbox command */
6634 lpfc_worker_wake_up(phba);
6635}
6636
da0436e9
JS
6637/**
6638 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6639 * @phba: Pointer to HBA context object.
6640 * @mboxq: Pointer to mailbox object.
6641 *
6642 * The function posts a mailbox to the port. The mailbox is expected
6643 * to be comletely filled in and ready for the port to operate on it.
6644 * This routine executes a synchronous completion operation on the
6645 * mailbox by polling for its completion.
6646 *
6647 * The caller must not be holding any locks when calling this routine.
6648 *
6649 * Returns:
6650 * MBX_SUCCESS - mailbox posted successfully
6651 * Any of the MBX error values.
6652 **/
6653static int
6654lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6655{
6656 int rc = MBX_SUCCESS;
6657 unsigned long iflag;
6658 uint32_t db_ready;
6659 uint32_t mcqe_status;
6660 uint32_t mbx_cmnd;
6661 unsigned long timeout;
6662 struct lpfc_sli *psli = &phba->sli;
6663 struct lpfc_mqe *mb = &mboxq->u.mqe;
6664 struct lpfc_bmbx_create *mbox_rgn;
6665 struct dma_address *dma_address;
6666 struct lpfc_register bmbx_reg;
6667
6668 /*
6669 * Only one mailbox can be active to the bootstrap mailbox region
6670 * at a time and there is no queueing provided.
6671 */
6672 spin_lock_irqsave(&phba->hbalock, iflag);
6673 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6674 spin_unlock_irqrestore(&phba->hbalock, iflag);
6675 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 6676 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
6677 "cannot issue Data: x%x x%x\n",
6678 mboxq->vport ? mboxq->vport->vpi : 0,
6679 mboxq->u.mb.mbxCommand,
a183a15f
JS
6680 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6681 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
6682 psli->sli_flag, MBX_POLL);
6683 return MBXERR_ERROR;
6684 }
6685 /* The server grabs the token and owns it until release */
6686 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6687 phba->sli.mbox_active = mboxq;
6688 spin_unlock_irqrestore(&phba->hbalock, iflag);
6689
6690 /*
6691 * Initialize the bootstrap memory region to avoid stale data areas
6692 * in the mailbox post. Then copy the caller's mailbox contents to
6693 * the bmbx mailbox region.
6694 */
6695 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6696 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6697 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6698 sizeof(struct lpfc_mqe));
6699
6700 /* Post the high mailbox dma address to the port and wait for ready. */
6701 dma_address = &phba->sli4_hba.bmbx.dma_address;
6702 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6703
a183a15f 6704 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
da0436e9
JS
6705 * 1000) + jiffies;
6706 do {
6707 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6708 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6709 if (!db_ready)
6710 msleep(2);
6711
6712 if (time_after(jiffies, timeout)) {
6713 rc = MBXERR_ERROR;
6714 goto exit;
6715 }
6716 } while (!db_ready);
6717
6718 /* Post the low mailbox dma address to the port. */
6719 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
a183a15f 6720 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
da0436e9
JS
6721 * 1000) + jiffies;
6722 do {
6723 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6724 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6725 if (!db_ready)
6726 msleep(2);
6727
6728 if (time_after(jiffies, timeout)) {
6729 rc = MBXERR_ERROR;
6730 goto exit;
6731 }
6732 } while (!db_ready);
6733
6734 /*
6735 * Read the CQ to ensure the mailbox has completed.
6736 * If so, update the mailbox status so that the upper layers
6737 * can complete the request normally.
6738 */
6739 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6740 sizeof(struct lpfc_mqe));
6741 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6742 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6743 sizeof(struct lpfc_mcqe));
6744 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
6745 /*
6746 * When the CQE status indicates a failure and the mailbox status
6747 * indicates success then copy the CQE status into the mailbox status
6748 * (and prefix it with x4000).
6749 */
da0436e9 6750 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
6751 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6752 bf_set(lpfc_mqe_status, mb,
6753 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 6754 rc = MBXERR_ERROR;
d7c47992
JS
6755 } else
6756 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
6757
6758 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 6759 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
6760 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6761 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
6762 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6763 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6764 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
6765 bf_get(lpfc_mqe_status, mb),
6766 mb->un.mb_words[0], mb->un.mb_words[1],
6767 mb->un.mb_words[2], mb->un.mb_words[3],
6768 mb->un.mb_words[4], mb->un.mb_words[5],
6769 mb->un.mb_words[6], mb->un.mb_words[7],
6770 mb->un.mb_words[8], mb->un.mb_words[9],
6771 mb->un.mb_words[10], mb->un.mb_words[11],
6772 mb->un.mb_words[12], mboxq->mcqe.word0,
6773 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6774 mboxq->mcqe.trailer);
6775exit:
6776 /* We are holding the token, no needed for lock when release */
6777 spin_lock_irqsave(&phba->hbalock, iflag);
6778 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6779 phba->sli.mbox_active = NULL;
6780 spin_unlock_irqrestore(&phba->hbalock, iflag);
6781 return rc;
6782}
6783
6784/**
6785 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6786 * @phba: Pointer to HBA context object.
6787 * @pmbox: Pointer to mailbox object.
6788 * @flag: Flag indicating how the mailbox need to be processed.
6789 *
6790 * This function is called by discovery code and HBA management code to submit
6791 * a mailbox command to firmware with SLI-4 interface spec.
6792 *
6793 * Return codes the caller owns the mailbox command after the return of the
6794 * function.
6795 **/
6796static int
6797lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
6798 uint32_t flag)
6799{
6800 struct lpfc_sli *psli = &phba->sli;
6801 unsigned long iflags;
6802 int rc;
6803
b76f2dc9
JS
6804 /* dump from issue mailbox command if setup */
6805 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
6806
8fa38513
JS
6807 rc = lpfc_mbox_dev_check(phba);
6808 if (unlikely(rc)) {
6809 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 6810 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
6811 "cannot issue Data: x%x x%x\n",
6812 mboxq->vport ? mboxq->vport->vpi : 0,
6813 mboxq->u.mb.mbxCommand,
a183a15f
JS
6814 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6815 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
6816 psli->sli_flag, flag);
6817 goto out_not_finished;
6818 }
6819
da0436e9
JS
6820 /* Detect polling mode and jump to a handler */
6821 if (!phba->sli4_hba.intr_enable) {
6822 if (flag == MBX_POLL)
6823 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6824 else
6825 rc = -EIO;
6826 if (rc != MBX_SUCCESS)
0558056c 6827 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 6828 "(%d):2541 Mailbox command x%x "
a183a15f
JS
6829 "(x%x/x%x) cannot issue Data: "
6830 "x%x x%x\n",
da0436e9
JS
6831 mboxq->vport ? mboxq->vport->vpi : 0,
6832 mboxq->u.mb.mbxCommand,
a183a15f
JS
6833 lpfc_sli_config_mbox_subsys_get(phba,
6834 mboxq),
6835 lpfc_sli_config_mbox_opcode_get(phba,
6836 mboxq),
da0436e9
JS
6837 psli->sli_flag, flag);
6838 return rc;
6839 } else if (flag == MBX_POLL) {
f1126688
JS
6840 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6841 "(%d):2542 Try to issue mailbox command "
a183a15f 6842 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 6843 "mailbox command queue: x%x x%x\n",
da0436e9
JS
6844 mboxq->vport ? mboxq->vport->vpi : 0,
6845 mboxq->u.mb.mbxCommand,
a183a15f
JS
6846 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6847 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 6848 psli->sli_flag, flag);
f1126688
JS
6849 /* Try to block the asynchronous mailbox posting */
6850 rc = lpfc_sli4_async_mbox_block(phba);
6851 if (!rc) {
6852 /* Successfully blocked, now issue sync mbox cmd */
6853 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6854 if (rc != MBX_SUCCESS)
6855 lpfc_printf_log(phba, KERN_ERR,
a183a15f
JS
6856 LOG_MBOX | LOG_SLI,
6857 "(%d):2597 Mailbox command "
6858 "x%x (x%x/x%x) cannot issue "
6859 "Data: x%x x%x\n",
6860 mboxq->vport ?
6861 mboxq->vport->vpi : 0,
6862 mboxq->u.mb.mbxCommand,
6863 lpfc_sli_config_mbox_subsys_get(phba,
6864 mboxq),
6865 lpfc_sli_config_mbox_opcode_get(phba,
6866 mboxq),
6867 psli->sli_flag, flag);
f1126688
JS
6868 /* Unblock the async mailbox posting afterward */
6869 lpfc_sli4_async_mbox_unblock(phba);
6870 }
6871 return rc;
da0436e9
JS
6872 }
6873
6874 /* Now, interrupt mode asynchrous mailbox command */
6875 rc = lpfc_mbox_cmd_check(phba, mboxq);
6876 if (rc) {
6877 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 6878 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
6879 "cannot issue Data: x%x x%x\n",
6880 mboxq->vport ? mboxq->vport->vpi : 0,
6881 mboxq->u.mb.mbxCommand,
a183a15f
JS
6882 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6883 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
6884 psli->sli_flag, flag);
6885 goto out_not_finished;
6886 }
da0436e9
JS
6887
6888 /* Put the mailbox command to the driver internal FIFO */
6889 psli->slistat.mbox_busy++;
6890 spin_lock_irqsave(&phba->hbalock, iflags);
6891 lpfc_mbox_put(phba, mboxq);
6892 spin_unlock_irqrestore(&phba->hbalock, iflags);
6893 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6894 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 6895 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
6896 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
6897 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
6898 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6899 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
6900 phba->pport->port_state,
6901 psli->sli_flag, MBX_NOWAIT);
6902 /* Wake up worker thread to transport mailbox command from head */
6903 lpfc_worker_wake_up(phba);
6904
6905 return MBX_BUSY;
6906
6907out_not_finished:
6908 return MBX_NOT_FINISHED;
6909}
6910
6911/**
6912 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
6913 * @phba: Pointer to HBA context object.
6914 *
6915 * This function is called by worker thread to send a mailbox command to
6916 * SLI4 HBA firmware.
6917 *
6918 **/
6919int
6920lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
6921{
6922 struct lpfc_sli *psli = &phba->sli;
6923 LPFC_MBOXQ_t *mboxq;
6924 int rc = MBX_SUCCESS;
6925 unsigned long iflags;
6926 struct lpfc_mqe *mqe;
6927 uint32_t mbx_cmnd;
6928
6929 /* Check interrupt mode before post async mailbox command */
6930 if (unlikely(!phba->sli4_hba.intr_enable))
6931 return MBX_NOT_FINISHED;
6932
6933 /* Check for mailbox command service token */
6934 spin_lock_irqsave(&phba->hbalock, iflags);
6935 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6936 spin_unlock_irqrestore(&phba->hbalock, iflags);
6937 return MBX_NOT_FINISHED;
6938 }
6939 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6940 spin_unlock_irqrestore(&phba->hbalock, iflags);
6941 return MBX_NOT_FINISHED;
6942 }
6943 if (unlikely(phba->sli.mbox_active)) {
6944 spin_unlock_irqrestore(&phba->hbalock, iflags);
6945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6946 "0384 There is pending active mailbox cmd\n");
6947 return MBX_NOT_FINISHED;
6948 }
6949 /* Take the mailbox command service token */
6950 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6951
6952 /* Get the next mailbox command from head of queue */
6953 mboxq = lpfc_mbox_get(phba);
6954
6955 /* If no more mailbox command waiting for post, we're done */
6956 if (!mboxq) {
6957 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6958 spin_unlock_irqrestore(&phba->hbalock, iflags);
6959 return MBX_SUCCESS;
6960 }
6961 phba->sli.mbox_active = mboxq;
6962 spin_unlock_irqrestore(&phba->hbalock, iflags);
6963
6964 /* Check device readiness for posting mailbox command */
6965 rc = lpfc_mbox_dev_check(phba);
6966 if (unlikely(rc))
6967 /* Driver clean routine will clean up pending mailbox */
6968 goto out_not_finished;
6969
6970 /* Prepare the mbox command to be posted */
6971 mqe = &mboxq->u.mqe;
6972 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
6973
6974 /* Start timer for the mbox_tmo and log some mailbox post messages */
6975 mod_timer(&psli->mbox_tmo, (jiffies +
a183a15f 6976 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
6977
6978 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 6979 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
6980 "x%x x%x\n",
6981 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
6982 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6983 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
6984 phba->pport->port_state, psli->sli_flag);
6985
6986 if (mbx_cmnd != MBX_HEARTBEAT) {
6987 if (mboxq->vport) {
6988 lpfc_debugfs_disc_trc(mboxq->vport,
6989 LPFC_DISC_TRC_MBOX_VPORT,
6990 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6991 mbx_cmnd, mqe->un.mb_words[0],
6992 mqe->un.mb_words[1]);
6993 } else {
6994 lpfc_debugfs_disc_trc(phba->pport,
6995 LPFC_DISC_TRC_MBOX,
6996 "MBOX Send: cmd:x%x mb:x%x x%x",
6997 mbx_cmnd, mqe->un.mb_words[0],
6998 mqe->un.mb_words[1]);
6999 }
7000 }
7001 psli->slistat.mbox_cmd++;
7002
7003 /* Post the mailbox command to the port */
7004 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7005 if (rc != MBX_SUCCESS) {
7006 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7007 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7008 "cannot issue Data: x%x x%x\n",
7009 mboxq->vport ? mboxq->vport->vpi : 0,
7010 mboxq->u.mb.mbxCommand,
a183a15f
JS
7011 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7012 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7013 psli->sli_flag, MBX_NOWAIT);
7014 goto out_not_finished;
7015 }
7016
7017 return rc;
7018
7019out_not_finished:
7020 spin_lock_irqsave(&phba->hbalock, iflags);
7021 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7022 __lpfc_mbox_cmpl_put(phba, mboxq);
7023 /* Release the token */
7024 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7025 phba->sli.mbox_active = NULL;
7026 spin_unlock_irqrestore(&phba->hbalock, iflags);
7027
7028 return MBX_NOT_FINISHED;
7029}
7030
7031/**
7032 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7033 * @phba: Pointer to HBA context object.
7034 * @pmbox: Pointer to mailbox object.
7035 * @flag: Flag indicating how the mailbox need to be processed.
7036 *
7037 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7038 * the API jump table function pointer from the lpfc_hba struct.
7039 *
7040 * Return codes the caller owns the mailbox command after the return of the
7041 * function.
7042 **/
7043int
7044lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7045{
7046 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7047}
7048
7049/**
25985edc 7050 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
7051 * @phba: The hba struct for which this call is being executed.
7052 * @dev_grp: The HBA PCI-Device group number.
7053 *
7054 * This routine sets up the mbox interface API function jump table in @phba
7055 * struct.
7056 * Returns: 0 - success, -ENODEV - failure.
7057 **/
7058int
7059lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7060{
7061
7062 switch (dev_grp) {
7063 case LPFC_PCI_DEV_LP:
7064 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7065 phba->lpfc_sli_handle_slow_ring_event =
7066 lpfc_sli_handle_slow_ring_event_s3;
7067 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7068 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7069 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7070 break;
7071 case LPFC_PCI_DEV_OC:
7072 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7073 phba->lpfc_sli_handle_slow_ring_event =
7074 lpfc_sli_handle_slow_ring_event_s4;
7075 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7076 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7077 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7078 break;
7079 default:
7080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7081 "1420 Invalid HBA PCI-device group: 0x%x\n",
7082 dev_grp);
7083 return -ENODEV;
7084 break;
7085 }
7086 return 0;
7087}
7088
e59058c4 7089/**
3621a710 7090 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
7091 * @phba: Pointer to HBA context object.
7092 * @pring: Pointer to driver SLI ring object.
7093 * @piocb: Pointer to address of newly added command iocb.
7094 *
7095 * This function is called with hbalock held to add a command
7096 * iocb to the txq when SLI layer cannot submit the command iocb
7097 * to the ring.
7098 **/
2a9bf3d0 7099void
92d7f7b0 7100__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7101 struct lpfc_iocbq *piocb)
dea3101e 7102{
7103 /* Insert the caller's iocb in the txq tail for later processing. */
7104 list_add_tail(&piocb->list, &pring->txq);
7105 pring->txq_cnt++;
dea3101e 7106}
7107
e59058c4 7108/**
3621a710 7109 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
7110 * @phba: Pointer to HBA context object.
7111 * @pring: Pointer to driver SLI ring object.
7112 * @piocb: Pointer to address of newly added command iocb.
7113 *
7114 * This function is called with hbalock held before a new
7115 * iocb is submitted to the firmware. This function checks
7116 * txq to flush the iocbs in txq to Firmware before
7117 * submitting new iocbs to the Firmware.
7118 * If there are iocbs in the txq which need to be submitted
7119 * to firmware, lpfc_sli_next_iocb returns the first element
7120 * of the txq after dequeuing it from txq.
7121 * If there is no iocb in the txq then the function will return
7122 * *piocb and *piocb is set to NULL. Caller needs to check
7123 * *piocb to find if there are more commands in the txq.
7124 **/
dea3101e 7125static struct lpfc_iocbq *
7126lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7127 struct lpfc_iocbq **piocb)
dea3101e 7128{
7129 struct lpfc_iocbq * nextiocb;
7130
7131 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7132 if (!nextiocb) {
7133 nextiocb = *piocb;
7134 *piocb = NULL;
7135 }
7136
7137 return nextiocb;
7138}
7139
e59058c4 7140/**
3772a991 7141 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 7142 * @phba: Pointer to HBA context object.
3772a991 7143 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
7144 * @piocb: Pointer to command iocb.
7145 * @flag: Flag indicating if this command can be put into txq.
7146 *
3772a991
JS
7147 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7148 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7149 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7150 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7151 * this function allows only iocbs for posting buffers. This function finds
7152 * next available slot in the command ring and posts the command to the
7153 * available slot and writes the port attention register to request HBA start
7154 * processing new iocb. If there is no slot available in the ring and
7155 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7156 * the function returns IOCB_BUSY.
e59058c4 7157 *
3772a991
JS
7158 * This function is called with hbalock held. The function will return success
7159 * after it successfully submit the iocb to firmware or after adding to the
7160 * txq.
e59058c4 7161 **/
98c9ea5c 7162static int
3772a991 7163__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e 7164 struct lpfc_iocbq *piocb, uint32_t flag)
7165{
7166 struct lpfc_iocbq *nextiocb;
7167 IOCB_t *iocb;
3772a991 7168 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 7169
92d7f7b0
JS
7170 if (piocb->iocb_cmpl && (!piocb->vport) &&
7171 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7172 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7173 lpfc_printf_log(phba, KERN_ERR,
7174 LOG_SLI | LOG_VPORT,
e8b62011 7175 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
7176 piocb->iocb.ulpCommand);
7177 dump_stack();
7178 return IOCB_ERROR;
7179 }
7180
7181
8d63f375
LV
7182 /* If the PCI channel is in offline state, do not post iocbs. */
7183 if (unlikely(pci_channel_offline(phba->pcidev)))
7184 return IOCB_ERROR;
7185
a257bf90
JS
7186 /* If HBA has a deferred error attention, fail the iocb. */
7187 if (unlikely(phba->hba_flag & DEFER_ERATT))
7188 return IOCB_ERROR;
7189
dea3101e 7190 /*
7191 * We should never get an IOCB if we are in a < LINK_DOWN state
7192 */
2e0fef85 7193 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e 7194 return IOCB_ERROR;
7195
7196 /*
7197 * Check to see if we are blocking IOCB processing because of a
0b727fea 7198 * outstanding event.
dea3101e 7199 */
0b727fea 7200 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e 7201 goto iocb_busy;
7202
2e0fef85 7203 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 7204 /*
2680eeaa 7205 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e 7206 * can be issued if the link is not up.
7207 */
7208 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
7209 case CMD_GEN_REQUEST64_CR:
7210 case CMD_GEN_REQUEST64_CX:
7211 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7212 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 7213 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
7214 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7215 MENLO_TRANSPORT_TYPE))
7216
7217 goto iocb_busy;
7218 break;
dea3101e 7219 case CMD_QUE_RING_BUF_CN:
7220 case CMD_QUE_RING_BUF64_CN:
dea3101e 7221 /*
7222 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7223 * completion, iocb_cmpl MUST be 0.
7224 */
7225 if (piocb->iocb_cmpl)
7226 piocb->iocb_cmpl = NULL;
7227 /*FALLTHROUGH*/
7228 case CMD_CREATE_XRI_CR:
2680eeaa
JS
7229 case CMD_CLOSE_XRI_CN:
7230 case CMD_CLOSE_XRI_CX:
dea3101e 7231 break;
7232 default:
7233 goto iocb_busy;
7234 }
7235
7236 /*
7237 * For FCP commands, we must be in a state where we can process link
7238 * attention events.
7239 */
7240 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 7241 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 7242 goto iocb_busy;
92d7f7b0 7243 }
dea3101e 7244
dea3101e 7245 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7246 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7247 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7248
7249 if (iocb)
7250 lpfc_sli_update_ring(phba, pring);
7251 else
7252 lpfc_sli_update_full_ring(phba, pring);
7253
7254 if (!piocb)
7255 return IOCB_SUCCESS;
7256
7257 goto out_busy;
7258
7259 iocb_busy:
7260 pring->stats.iocb_cmd_delay++;
7261
7262 out_busy:
7263
7264 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 7265 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e 7266 return IOCB_SUCCESS;
7267 }
7268
7269 return IOCB_BUSY;
7270}
7271
3772a991 7272/**
4f774513
JS
7273 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7274 * @phba: Pointer to HBA context object.
7275 * @piocb: Pointer to command iocb.
7276 * @sglq: Pointer to the scatter gather queue object.
7277 *
7278 * This routine converts the bpl or bde that is in the IOCB
7279 * to a sgl list for the sli4 hardware. The physical address
7280 * of the bpl/bde is converted back to a virtual address.
7281 * If the IOCB contains a BPL then the list of BDE's is
7282 * converted to sli4_sge's. If the IOCB contains a single
7283 * BDE then it is converted to a single sli_sge.
7284 * The IOCB is still in cpu endianess so the contents of
7285 * the bpl can be used without byte swapping.
7286 *
7287 * Returns valid XRI = Success, NO_XRI = Failure.
7288**/
7289static uint16_t
7290lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7291 struct lpfc_sglq *sglq)
3772a991 7292{
4f774513
JS
7293 uint16_t xritag = NO_XRI;
7294 struct ulp_bde64 *bpl = NULL;
7295 struct ulp_bde64 bde;
7296 struct sli4_sge *sgl = NULL;
7297 IOCB_t *icmd;
7298 int numBdes = 0;
7299 int i = 0;
63e801ce
JS
7300 uint32_t offset = 0; /* accumulated offset in the sg request list */
7301 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 7302
4f774513
JS
7303 if (!piocbq || !sglq)
7304 return xritag;
7305
7306 sgl = (struct sli4_sge *)sglq->sgl;
7307 icmd = &piocbq->iocb;
7308 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7309 numBdes = icmd->un.genreq64.bdl.bdeSize /
7310 sizeof(struct ulp_bde64);
7311 /* The addrHigh and addrLow fields within the IOCB
7312 * have not been byteswapped yet so there is no
7313 * need to swap them back.
7314 */
7315 bpl = (struct ulp_bde64 *)
7316 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7317
7318 if (!bpl)
7319 return xritag;
7320
7321 for (i = 0; i < numBdes; i++) {
7322 /* Should already be byte swapped. */
28baac74
JS
7323 sgl->addr_hi = bpl->addrHigh;
7324 sgl->addr_lo = bpl->addrLow;
7325
0558056c 7326 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
7327 if ((i+1) == numBdes)
7328 bf_set(lpfc_sli4_sge_last, sgl, 1);
7329 else
7330 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
7331 /* swap the size field back to the cpu so we
7332 * can assign it to the sgl.
7333 */
7334 bde.tus.w = le32_to_cpu(bpl->tus.w);
7335 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
7336 /* The offsets in the sgl need to be accumulated
7337 * separately for the request and reply lists.
7338 * The request is always first, the reply follows.
7339 */
7340 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7341 /* add up the reply sg entries */
7342 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7343 inbound++;
7344 /* first inbound? reset the offset */
7345 if (inbound == 1)
7346 offset = 0;
7347 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7348 offset += bde.tus.f.bdeSize;
7349 }
546fc854 7350 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
7351 bpl++;
7352 sgl++;
7353 }
7354 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7355 /* The addrHigh and addrLow fields of the BDE have not
7356 * been byteswapped yet so they need to be swapped
7357 * before putting them in the sgl.
7358 */
7359 sgl->addr_hi =
7360 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7361 sgl->addr_lo =
7362 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 7363 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
7364 bf_set(lpfc_sli4_sge_last, sgl, 1);
7365 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
7366 sgl->sge_len =
7367 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
7368 }
7369 return sglq->sli4_xritag;
3772a991 7370}
92d7f7b0 7371
e59058c4 7372/**
4f774513 7373 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 7374 * @phba: Pointer to HBA context object.
e59058c4 7375 *
a93ff37a 7376 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
8fa38513
JS
7377 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7378 * held.
4f774513
JS
7379 *
7380 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 7381 **/
4f774513 7382static uint32_t
8fa38513 7383lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 7384{
8fa38513
JS
7385 ++phba->fcp_qidx;
7386 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7387 phba->fcp_qidx = 0;
92d7f7b0 7388
8fa38513 7389 return phba->fcp_qidx;
92d7f7b0
JS
7390}
7391
e59058c4 7392/**
4f774513 7393 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 7394 * @phba: Pointer to HBA context object.
4f774513
JS
7395 * @piocb: Pointer to command iocb.
7396 * @wqe: Pointer to the work queue entry.
e59058c4 7397 *
4f774513
JS
7398 * This routine converts the iocb command to its Work Queue Entry
7399 * equivalent. The wqe pointer should not have any fields set when
7400 * this routine is called because it will memcpy over them.
7401 * This routine does not set the CQ_ID or the WQEC bits in the
7402 * wqe.
e59058c4 7403 *
4f774513 7404 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 7405 **/
cf5bf97e 7406static int
4f774513
JS
7407lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7408 union lpfc_wqe *wqe)
cf5bf97e 7409{
5ffc266e 7410 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
7411 uint8_t ct = 0;
7412 uint32_t fip;
7413 uint32_t abort_tag;
7414 uint8_t command_type = ELS_COMMAND_NON_FIP;
7415 uint8_t cmnd;
7416 uint16_t xritag;
dcf2a4e0
JS
7417 uint16_t abrt_iotag;
7418 struct lpfc_iocbq *abrtiocbq;
4f774513 7419 struct ulp_bde64 *bpl = NULL;
f0d9bccc 7420 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
7421 int numBdes, i;
7422 struct ulp_bde64 bde;
c31098ce 7423 struct lpfc_nodelist *ndlp;
4f774513 7424
45ed1190 7425 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 7426 /* The fcp commands will set command type */
0c287589 7427 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 7428 command_type = FCP_COMMAND;
c868595d 7429 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
7430 command_type = ELS_COMMAND_FIP;
7431 else
7432 command_type = ELS_COMMAND_NON_FIP;
7433
4f774513
JS
7434 /* Some of the fields are in the right position already */
7435 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7436 abort_tag = (uint32_t) iocbq->iotag;
7437 xritag = iocbq->sli4_xritag;
f0d9bccc 7438 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
4f774513
JS
7439 /* words0-2 bpl convert bde */
7440 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
7441 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7442 sizeof(struct ulp_bde64);
4f774513
JS
7443 bpl = (struct ulp_bde64 *)
7444 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7445 if (!bpl)
7446 return IOCB_ERROR;
cf5bf97e 7447
4f774513
JS
7448 /* Should already be byte swapped. */
7449 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7450 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7451 /* swap the size field back to the cpu so we
7452 * can assign it to the sgl.
7453 */
7454 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
7455 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7456 total_len = 0;
7457 for (i = 0; i < numBdes; i++) {
7458 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7459 total_len += bde.tus.f.bdeSize;
7460 }
4f774513 7461 } else
5ffc266e 7462 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 7463
4f774513
JS
7464 iocbq->iocb.ulpIoTag = iocbq->iotag;
7465 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 7466
4f774513
JS
7467 switch (iocbq->iocb.ulpCommand) {
7468 case CMD_ELS_REQUEST64_CR:
c31098ce 7469 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
7470 if (!iocbq->iocb.ulpLe) {
7471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7472 "2007 Only Limited Edition cmd Format"
7473 " supported 0x%x\n",
7474 iocbq->iocb.ulpCommand);
7475 return IOCB_ERROR;
7476 }
5ffc266e 7477 wqe->els_req.payload_len = xmit_len;
4f774513
JS
7478 /* Els_reguest64 has a TMO */
7479 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7480 iocbq->iocb.ulpTimeout);
7481 /* Need a VF for word 4 set the vf bit*/
7482 bf_set(els_req64_vf, &wqe->els_req, 0);
7483 /* And a VFID for word 12 */
7484 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 7485 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
7486 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7487 iocbq->iocb.ulpContext);
7488 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7489 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 7490 /* CCP CCPE PV PRI in word10 were set in the memcpy */
c868595d
JS
7491 if (command_type == ELS_COMMAND_FIP) {
7492 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7493 >> LPFC_FIP_ELS_ID_SHIFT);
7494 }
6d368e53
JS
7495 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7496 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
7497 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7498 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7499 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7500 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7501 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7502 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
7851fe2c 7503 break;
5ffc266e 7504 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
7505 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7506 iocbq->iocb.un.ulpWord[3]);
7507 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 7508 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
7509 /* The entire sequence is transmitted for this IOCB */
7510 xmit_len = total_len;
7511 cmnd = CMD_XMIT_SEQUENCE64_CR;
4f774513 7512 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
7513 /* word3 iocb=io_tag32 wqe=reserved */
7514 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
7515 /* word4 relative_offset memcpy */
7516 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
7517 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7518 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7519 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7520 LPFC_WQE_IOD_WRITE);
7521 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7522 LPFC_WQE_LENLOC_WORD12);
7523 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
7524 wqe->xmit_sequence.xmit_len = xmit_len;
7525 command_type = OTHER_COMMAND;
7851fe2c 7526 break;
4f774513 7527 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
7528 /* word3 iocb=iotag32 wqe=seq_payload_len */
7529 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
7530 /* word4 iocb=rsvd wqe=rsvd */
7531 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7532 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 7533 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 7534 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
7535 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7536 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7537 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7538 LPFC_WQE_LENLOC_WORD3);
7539 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 7540 break;
4f774513
JS
7541 case CMD_FCP_IWRITE64_CR:
7542 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
7543 /* word3 iocb=iotag wqe=payload_offset_len */
7544 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7545 wqe->fcp_iwrite.payload_offset_len =
7546 xmit_len + sizeof(struct fcp_rsp);
7547 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7548 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7549 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7550 iocbq->iocb.ulpFCP2Rcvy);
7551 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7552 /* Always open the exchange */
7553 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7554 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7555 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7556 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7557 LPFC_WQE_LENLOC_WORD4);
7558 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7559 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
7851fe2c 7560 break;
4f774513 7561 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
7562 /* word3 iocb=iotag wqe=payload_offset_len */
7563 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7564 wqe->fcp_iread.payload_offset_len =
5ffc266e 7565 xmit_len + sizeof(struct fcp_rsp);
f0d9bccc
JS
7566 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7567 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7568 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7569 iocbq->iocb.ulpFCP2Rcvy);
7570 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688
JS
7571 /* Always open the exchange */
7572 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
f0d9bccc
JS
7573 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7574 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7575 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7576 LPFC_WQE_LENLOC_WORD4);
7577 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7578 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
7851fe2c 7579 break;
4f774513 7580 case CMD_FCP_ICMND64_CR:
f0d9bccc
JS
7581 /* word3 iocb=IO_TAG wqe=reserved */
7582 wqe->fcp_icmd.rsrvd3 = 0;
7583 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 7584 /* Always open the exchange */
f0d9bccc
JS
7585 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7586 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7587 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7588 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7589 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7590 LPFC_WQE_LENLOC_NONE);
7591 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
7851fe2c 7592 break;
4f774513 7593 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
7594 /* For this command calculate the xmit length of the
7595 * request bde.
7596 */
7597 xmit_len = 0;
7598 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7599 sizeof(struct ulp_bde64);
7600 for (i = 0; i < numBdes; i++) {
63e801ce 7601 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
7602 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7603 break;
63e801ce
JS
7604 xmit_len += bde.tus.f.bdeSize;
7605 }
f0d9bccc
JS
7606 /* word3 iocb=IO_TAG wqe=request_payload_len */
7607 wqe->gen_req.request_payload_len = xmit_len;
7608 /* word4 iocb=parameter wqe=relative_offset memcpy */
7609 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
7610 /* word6 context tag copied in memcpy */
7611 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7612 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7613 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7614 "2015 Invalid CT %x command 0x%x\n",
7615 ct, iocbq->iocb.ulpCommand);
7616 return IOCB_ERROR;
7617 }
f0d9bccc
JS
7618 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7619 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7620 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7621 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7622 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7623 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7624 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7625 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
4f774513 7626 command_type = OTHER_COMMAND;
7851fe2c 7627 break;
4f774513 7628 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 7629 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 7630 /* words0-2 BDE memcpy */
f0d9bccc
JS
7631 /* word3 iocb=iotag32 wqe=response_payload_len */
7632 wqe->xmit_els_rsp.response_payload_len = xmit_len;
4f774513 7633 /* word4 iocb=did wge=rsvd. */
f0d9bccc 7634 wqe->xmit_els_rsp.rsvd4 = 0;
4f774513
JS
7635 /* word5 iocb=rsvd wge=did */
7636 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7637 iocbq->iocb.un.elsreq64.remoteID);
f0d9bccc
JS
7638 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7639 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7640 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7641 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 7642 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 7643 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 7644 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 7645 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
7646 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7647 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7648 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7649 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7650 LPFC_WQE_LENLOC_WORD3);
7651 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
7652 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7653 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
4f774513 7654 command_type = OTHER_COMMAND;
7851fe2c 7655 break;
4f774513
JS
7656 case CMD_CLOSE_XRI_CN:
7657 case CMD_ABORT_XRI_CN:
7658 case CMD_ABORT_XRI_CX:
7659 /* words 0-2 memcpy should be 0 rserved */
7660 /* port will send abts */
dcf2a4e0
JS
7661 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7662 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7663 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7664 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7665 } else
7666 fip = 0;
7667
7668 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 7669 /*
dcf2a4e0
JS
7670 * The link is down, or the command was ELS_FIP
7671 * so the fw does not need to send abts
4f774513
JS
7672 * on the wire.
7673 */
7674 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7675 else
7676 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7677 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
7678 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7679 wqe->abort_cmd.rsrvd5 = 0;
7680 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
7681 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7682 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
7683 /*
7684 * The abort handler will send us CMD_ABORT_XRI_CN or
7685 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7686 */
f0d9bccc
JS
7687 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7688 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7689 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7690 LPFC_WQE_LENLOC_NONE);
4f774513
JS
7691 cmnd = CMD_ABORT_XRI_CX;
7692 command_type = OTHER_COMMAND;
7693 xritag = 0;
7851fe2c 7694 break;
6669f9bb 7695 case CMD_XMIT_BLS_RSP64_CX:
546fc854 7696 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
7697 * we re-construct this WQE here based on information in
7698 * iocbq from scratch.
7699 */
7700 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 7701 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 7702 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
7703 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7704 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
7705 LPFC_ABTS_UNSOL_INT) {
7706 /* ABTS sent by initiator to CT exchange, the
7707 * RX_ID field will be filled with the newly
7708 * allocated responder XRI.
7709 */
7710 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7711 iocbq->sli4_xritag);
7712 } else {
7713 /* ABTS sent by responder to CT exchange, the
7714 * RX_ID field will be filled with the responder
7715 * RX_ID from ABTS.
7716 */
7717 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 7718 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 7719 }
6669f9bb
JS
7720 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7721 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7722 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7723 iocbq->iocb.ulpContext);
f0d9bccc
JS
7724 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7725 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7726 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
7727 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7728 command_type = OTHER_COMMAND;
546fc854
JS
7729 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7730 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7731 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7732 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7733 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7734 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7735 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7736 }
7737
7851fe2c 7738 break;
4f774513
JS
7739 case CMD_XRI_ABORTED_CX:
7740 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
7741 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7742 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7743 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7744 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7745 default:
7746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7747 "2014 Invalid command 0x%x\n",
7748 iocbq->iocb.ulpCommand);
7749 return IOCB_ERROR;
7851fe2c 7750 break;
4f774513 7751 }
6d368e53 7752
f0d9bccc
JS
7753 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7754 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7755 wqe->generic.wqe_com.abort_tag = abort_tag;
7756 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7757 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7758 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7759 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
7760 return 0;
7761}
7762
7763/**
7764 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7765 * @phba: Pointer to HBA context object.
7766 * @ring_number: SLI ring number to issue iocb on.
7767 * @piocb: Pointer to command iocb.
7768 * @flag: Flag indicating if this command can be put into txq.
7769 *
7770 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7771 * an iocb command to an HBA with SLI-4 interface spec.
7772 *
7773 * This function is called with hbalock held. The function will return success
7774 * after it successfully submit the iocb to firmware or after adding to the
7775 * txq.
7776 **/
7777static int
7778__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7779 struct lpfc_iocbq *piocb, uint32_t flag)
7780{
7781 struct lpfc_sglq *sglq;
4f774513
JS
7782 union lpfc_wqe wqe;
7783 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
7784
7785 if (piocb->sli4_xritag == NO_XRI) {
7786 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
546fc854
JS
7787 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7788 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
4f774513
JS
7789 sglq = NULL;
7790 else {
2a9bf3d0
JS
7791 if (pring->txq_cnt) {
7792 if (!(flag & SLI_IOCB_RET_IOCB)) {
7793 __lpfc_sli_ringtx_put(phba,
7794 pring, piocb);
7795 return IOCB_SUCCESS;
7796 } else {
7797 return IOCB_BUSY;
7798 }
7799 } else {
6d368e53 7800 sglq = __lpfc_sli_get_sglq(phba, piocb);
2a9bf3d0
JS
7801 if (!sglq) {
7802 if (!(flag & SLI_IOCB_RET_IOCB)) {
7803 __lpfc_sli_ringtx_put(phba,
7804 pring,
7805 piocb);
7806 return IOCB_SUCCESS;
7807 } else
7808 return IOCB_BUSY;
7809 }
7810 }
4f774513
JS
7811 }
7812 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6d368e53
JS
7813 /* These IO's already have an XRI and a mapped sgl. */
7814 sglq = NULL;
4f774513 7815 } else {
6d368e53
JS
7816 /*
7817 * This is a continuation of a commandi,(CX) so this
4f774513
JS
7818 * sglq is on the active list
7819 */
7820 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
7821 if (!sglq)
7822 return IOCB_ERROR;
7823 }
7824
7825 if (sglq) {
6d368e53 7826 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 7827 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 7828 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
7829 return IOCB_ERROR;
7830 }
7831
7832 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
7833 return IOCB_ERROR;
7834
341af102
JS
7835 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
7836 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
5ffc266e
JS
7837 /*
7838 * For FCP command IOCB, get a new WQ index to distribute
7839 * WQE across the WQsr. On the other hand, for abort IOCB,
7840 * it carries the same WQ index to the original command
7841 * IOCB.
7842 */
341af102 7843 if (piocb->iocb_flag & LPFC_IO_FCP)
5ffc266e
JS
7844 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
7845 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
7846 &wqe))
4f774513
JS
7847 return IOCB_ERROR;
7848 } else {
7849 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
7850 return IOCB_ERROR;
7851 }
7852 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
7853
7854 return 0;
7855}
7856
7857/**
7858 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
7859 *
7860 * This routine wraps the actual lockless version for issusing IOCB function
7861 * pointer from the lpfc_hba struct.
7862 *
7863 * Return codes:
7864 * IOCB_ERROR - Error
7865 * IOCB_SUCCESS - Success
7866 * IOCB_BUSY - Busy
7867 **/
2a9bf3d0 7868int
4f774513
JS
7869__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
7870 struct lpfc_iocbq *piocb, uint32_t flag)
7871{
7872 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
7873}
7874
7875/**
25985edc 7876 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
7877 * @phba: The hba struct for which this call is being executed.
7878 * @dev_grp: The HBA PCI-Device group number.
7879 *
7880 * This routine sets up the SLI interface API function jump table in @phba
7881 * struct.
7882 * Returns: 0 - success, -ENODEV - failure.
7883 **/
7884int
7885lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7886{
7887
7888 switch (dev_grp) {
7889 case LPFC_PCI_DEV_LP:
7890 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
7891 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
7892 break;
7893 case LPFC_PCI_DEV_OC:
7894 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
7895 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
7896 break;
7897 default:
7898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7899 "1419 Invalid HBA PCI-device group: 0x%x\n",
7900 dev_grp);
7901 return -ENODEV;
7902 break;
7903 }
7904 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
7905 return 0;
7906}
7907
7908/**
7909 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
7910 * @phba: Pointer to HBA context object.
7911 * @pring: Pointer to driver SLI ring object.
7912 * @piocb: Pointer to command iocb.
7913 * @flag: Flag indicating if this command can be put into txq.
7914 *
7915 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
7916 * function. This function gets the hbalock and calls
7917 * __lpfc_sli_issue_iocb function and will return the error returned
7918 * by __lpfc_sli_issue_iocb function. This wrapper is used by
7919 * functions which do not hold hbalock.
7920 **/
7921int
7922lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
7923 struct lpfc_iocbq *piocb, uint32_t flag)
7924{
7925 unsigned long iflags;
7926 int rc;
7927
7928 spin_lock_irqsave(&phba->hbalock, iflags);
7929 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
7930 spin_unlock_irqrestore(&phba->hbalock, iflags);
7931
7932 return rc;
7933}
7934
7935/**
7936 * lpfc_extra_ring_setup - Extra ring setup function
7937 * @phba: Pointer to HBA context object.
7938 *
7939 * This function is called while driver attaches with the
7940 * HBA to setup the extra ring. The extra ring is used
7941 * only when driver needs to support target mode functionality
7942 * or IP over FC functionalities.
7943 *
7944 * This function is called with no lock held.
7945 **/
7946static int
7947lpfc_extra_ring_setup( struct lpfc_hba *phba)
7948{
7949 struct lpfc_sli *psli;
7950 struct lpfc_sli_ring *pring;
7951
7952 psli = &phba->sli;
7953
7954 /* Adjust cmd/rsp ring iocb entries more evenly */
7955
7956 /* Take some away from the FCP ring */
7957 pring = &psli->ring[psli->fcp_ring];
7958 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7959 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
7960 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7961 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7962
a4bc3379
JS
7963 /* and give them to the extra ring */
7964 pring = &psli->ring[psli->extra_ring];
7965
cf5bf97e
JW
7966 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7967 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7968 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7969 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7970
7971 /* Setup default profile for this ring */
7972 pring->iotag_max = 4096;
7973 pring->num_mask = 1;
7974 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
7975 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
7976 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
7977 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
7978 return 0;
7979}
7980
e59058c4 7981/**
3621a710 7982 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
7983 * @phba: Pointer to HBA context object.
7984 * @pring: Pointer to driver SLI ring object.
7985 * @iocbq: Pointer to iocb object.
7986 *
7987 * This function is called by the slow ring event handler
7988 * function when there is an ASYNC event iocb in the ring.
7989 * This function is called with no lock held.
7990 * Currently this function handles only temperature related
7991 * ASYNC events. The function decodes the temperature sensor
7992 * event message and posts events for the management applications.
7993 **/
98c9ea5c 7994static void
57127f15
JS
7995lpfc_sli_async_event_handler(struct lpfc_hba * phba,
7996 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
7997{
7998 IOCB_t *icmd;
7999 uint16_t evt_code;
8000 uint16_t temp;
8001 struct temp_event temp_event_data;
8002 struct Scsi_Host *shost;
a257bf90 8003 uint32_t *iocb_w;
57127f15
JS
8004
8005 icmd = &iocbq->iocb;
8006 evt_code = icmd->un.asyncstat.evt_code;
8007 temp = icmd->ulpContext;
8008
8009 if ((evt_code != ASYNC_TEMP_WARN) &&
8010 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 8011 iocb_w = (uint32_t *) icmd;
57127f15
JS
8012 lpfc_printf_log(phba,
8013 KERN_ERR,
8014 LOG_SLI,
76bb24ef 8015 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 8016 " evt_code 0x%x\n"
a257bf90
JS
8017 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8018 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8019 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8020 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 8021 pring->ringno,
a257bf90
JS
8022 icmd->un.asyncstat.evt_code,
8023 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8024 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8025 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8026 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8027
57127f15
JS
8028 return;
8029 }
8030 temp_event_data.data = (uint32_t)temp;
8031 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8032 if (evt_code == ASYNC_TEMP_WARN) {
8033 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8034 lpfc_printf_log(phba,
09372820 8035 KERN_ERR,
57127f15 8036 LOG_TEMP,
76bb24ef 8037 "0347 Adapter is very hot, please take "
57127f15
JS
8038 "corrective action. temperature : %d Celsius\n",
8039 temp);
8040 }
8041 if (evt_code == ASYNC_TEMP_SAFE) {
8042 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8043 lpfc_printf_log(phba,
09372820 8044 KERN_ERR,
57127f15
JS
8045 LOG_TEMP,
8046 "0340 Adapter temperature is OK now. "
8047 "temperature : %d Celsius\n",
8048 temp);
8049 }
8050
8051 /* Send temperature change event to applications */
8052 shost = lpfc_shost_from_vport(phba->pport);
8053 fc_host_post_vendor_event(shost, fc_get_event_number(),
8054 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 8055 LPFC_NL_VENDOR_ID);
57127f15
JS
8056
8057}
8058
8059
e59058c4 8060/**
3621a710 8061 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
8062 * @phba: Pointer to HBA context object.
8063 *
8064 * lpfc_sli_setup sets up rings of the SLI interface with
8065 * number of iocbs per ring and iotags. This function is
8066 * called while driver attach to the HBA and before the
8067 * interrupts are enabled. So there is no need for locking.
8068 *
8069 * This function always returns 0.
8070 **/
dea3101e 8071int
8072lpfc_sli_setup(struct lpfc_hba *phba)
8073{
ed957684 8074 int i, totiocbsize = 0;
dea3101e 8075 struct lpfc_sli *psli = &phba->sli;
8076 struct lpfc_sli_ring *pring;
8077
8078 psli->num_rings = MAX_CONFIGURED_RINGS;
8079 psli->sli_flag = 0;
8080 psli->fcp_ring = LPFC_FCP_RING;
8081 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 8082 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 8083
604a3e30
JB
8084 psli->iocbq_lookup = NULL;
8085 psli->iocbq_lookup_len = 0;
8086 psli->last_iotag = 0;
8087
dea3101e 8088 for (i = 0; i < psli->num_rings; i++) {
8089 pring = &psli->ring[i];
8090 switch (i) {
8091 case LPFC_FCP_RING: /* ring 0 - FCP */
8092 /* numCiocb and numRiocb are used in config_port */
8093 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8094 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8095 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8096 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8097 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8098 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 8099 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8100 SLI3_IOCB_CMD_SIZE :
8101 SLI2_IOCB_CMD_SIZE;
ed957684 8102 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8103 SLI3_IOCB_RSP_SIZE :
8104 SLI2_IOCB_RSP_SIZE;
dea3101e 8105 pring->iotag_ctr = 0;
8106 pring->iotag_max =
92d7f7b0 8107 (phba->cfg_hba_queue_depth * 2);
dea3101e 8108 pring->fast_iotag = pring->iotag_max;
8109 pring->num_mask = 0;
8110 break;
a4bc3379 8111 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 8112 /* numCiocb and numRiocb are used in config_port */
8113 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8114 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 8115 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8116 SLI3_IOCB_CMD_SIZE :
8117 SLI2_IOCB_CMD_SIZE;
ed957684 8118 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8119 SLI3_IOCB_RSP_SIZE :
8120 SLI2_IOCB_RSP_SIZE;
2e0fef85 8121 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e 8122 pring->num_mask = 0;
8123 break;
8124 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8125 /* numCiocb and numRiocb are used in config_port */
8126 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8127 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 8128 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8129 SLI3_IOCB_CMD_SIZE :
8130 SLI2_IOCB_CMD_SIZE;
ed957684 8131 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8132 SLI3_IOCB_RSP_SIZE :
8133 SLI2_IOCB_RSP_SIZE;
dea3101e 8134 pring->fast_iotag = 0;
8135 pring->iotag_ctr = 0;
8136 pring->iotag_max = 4096;
57127f15
JS
8137 pring->lpfc_sli_rcv_async_status =
8138 lpfc_sli_async_event_handler;
6669f9bb 8139 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 8140 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
8141 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8142 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 8143 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 8144 lpfc_els_unsol_event;
dea3101e 8145 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
8146 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8147 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 8148 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 8149 lpfc_els_unsol_event;
dea3101e 8150 pring->prt[2].profile = 0; /* Mask 2 */
8151 /* NameServer Inquiry */
6a9c52cf 8152 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 8153 /* NameServer */
6a9c52cf 8154 pring->prt[2].type = FC_TYPE_CT;
dea3101e 8155 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 8156 lpfc_ct_unsol_event;
dea3101e 8157 pring->prt[3].profile = 0; /* Mask 3 */
8158 /* NameServer response */
6a9c52cf 8159 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 8160 /* NameServer */
6a9c52cf 8161 pring->prt[3].type = FC_TYPE_CT;
dea3101e 8162 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 8163 lpfc_ct_unsol_event;
6669f9bb
JS
8164 /* abort unsolicited sequence */
8165 pring->prt[4].profile = 0; /* Mask 4 */
8166 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8167 pring->prt[4].type = FC_TYPE_BLS;
8168 pring->prt[4].lpfc_sli_rcv_unsol_event =
8169 lpfc_sli4_ct_abort_unsol_event;
dea3101e 8170 break;
8171 }
ed957684 8172 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 8173 (pring->numRiocb * pring->sizeRiocb);
dea3101e 8174 }
ed957684 8175 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 8176 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
8177 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8178 "SLI2 SLIM Data: x%x x%lx\n",
8179 phba->brd_no, totiocbsize,
8180 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 8181 }
cf5bf97e
JW
8182 if (phba->cfg_multi_ring_support == 2)
8183 lpfc_extra_ring_setup(phba);
dea3101e 8184
8185 return 0;
8186}
8187
e59058c4 8188/**
3621a710 8189 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
8190 * @phba: Pointer to HBA context object.
8191 *
8192 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8193 * ring. This function also initializes ring indices of each ring.
8194 * This function is called during the initialization of the SLI
8195 * interface of an HBA.
8196 * This function is called with no lock held and always returns
8197 * 1.
8198 **/
dea3101e 8199int
2e0fef85 8200lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e 8201{
8202 struct lpfc_sli *psli;
8203 struct lpfc_sli_ring *pring;
604a3e30 8204 int i;
dea3101e 8205
8206 psli = &phba->sli;
2e0fef85 8207 spin_lock_irq(&phba->hbalock);
dea3101e 8208 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 8209 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 8210 /* Initialize list headers for txq and txcmplq as double linked lists */
8211 for (i = 0; i < psli->num_rings; i++) {
8212 pring = &psli->ring[i];
8213 pring->ringno = i;
8214 pring->next_cmdidx = 0;
8215 pring->local_getidx = 0;
8216 pring->cmdidx = 0;
8217 INIT_LIST_HEAD(&pring->txq);
8218 INIT_LIST_HEAD(&pring->txcmplq);
8219 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 8220 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 8221 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 8222 }
2e0fef85
JS
8223 spin_unlock_irq(&phba->hbalock);
8224 return 1;
dea3101e 8225}
8226
04c68496
JS
8227/**
8228 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8229 * @phba: Pointer to HBA context object.
8230 *
8231 * This routine flushes the mailbox command subsystem. It will unconditionally
8232 * flush all the mailbox commands in the three possible stages in the mailbox
8233 * command sub-system: pending mailbox command queue; the outstanding mailbox
8234 * command; and completed mailbox command queue. It is caller's responsibility
8235 * to make sure that the driver is in the proper state to flush the mailbox
8236 * command sub-system. Namely, the posting of mailbox commands into the
8237 * pending mailbox command queue from the various clients must be stopped;
8238 * either the HBA is in a state that it will never works on the outstanding
8239 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8240 * mailbox command has been completed.
8241 **/
8242static void
8243lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8244{
8245 LIST_HEAD(completions);
8246 struct lpfc_sli *psli = &phba->sli;
8247 LPFC_MBOXQ_t *pmb;
8248 unsigned long iflag;
8249
8250 /* Flush all the mailbox commands in the mbox system */
8251 spin_lock_irqsave(&phba->hbalock, iflag);
8252 /* The pending mailbox command queue */
8253 list_splice_init(&phba->sli.mboxq, &completions);
8254 /* The outstanding active mailbox command */
8255 if (psli->mbox_active) {
8256 list_add_tail(&psli->mbox_active->list, &completions);
8257 psli->mbox_active = NULL;
8258 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8259 }
8260 /* The completed mailbox command queue */
8261 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8262 spin_unlock_irqrestore(&phba->hbalock, iflag);
8263
8264 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8265 while (!list_empty(&completions)) {
8266 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8267 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8268 if (pmb->mbox_cmpl)
8269 pmb->mbox_cmpl(phba, pmb);
8270 }
8271}
8272
e59058c4 8273/**
3621a710 8274 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
8275 * @vport: Pointer to virtual port object.
8276 *
8277 * lpfc_sli_host_down is called to clean up the resources
8278 * associated with a vport before destroying virtual
8279 * port data structures.
8280 * This function does following operations:
8281 * - Free discovery resources associated with this virtual
8282 * port.
8283 * - Free iocbs associated with this virtual port in
8284 * the txq.
8285 * - Send abort for all iocb commands associated with this
8286 * vport in txcmplq.
8287 *
8288 * This function is called with no lock held and always returns 1.
8289 **/
92d7f7b0
JS
8290int
8291lpfc_sli_host_down(struct lpfc_vport *vport)
8292{
858c9f6c 8293 LIST_HEAD(completions);
92d7f7b0
JS
8294 struct lpfc_hba *phba = vport->phba;
8295 struct lpfc_sli *psli = &phba->sli;
8296 struct lpfc_sli_ring *pring;
8297 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
8298 int i;
8299 unsigned long flags = 0;
8300 uint16_t prev_pring_flag;
8301
8302 lpfc_cleanup_discovery_resources(vport);
8303
8304 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
8305 for (i = 0; i < psli->num_rings; i++) {
8306 pring = &psli->ring[i];
8307 prev_pring_flag = pring->flag;
5e9d9b82
JS
8308 /* Only slow rings */
8309 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 8310 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
8311 /* Set the lpfc data pending flag */
8312 set_bit(LPFC_DATA_READY, &phba->data_flags);
8313 }
92d7f7b0
JS
8314 /*
8315 * Error everything on the txq since these iocbs have not been
8316 * given to the FW yet.
8317 */
92d7f7b0
JS
8318 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8319 if (iocb->vport != vport)
8320 continue;
858c9f6c 8321 list_move_tail(&iocb->list, &completions);
92d7f7b0 8322 pring->txq_cnt--;
92d7f7b0
JS
8323 }
8324
8325 /* Next issue ABTS for everything on the txcmplq */
8326 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8327 list) {
8328 if (iocb->vport != vport)
8329 continue;
8330 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8331 }
8332
8333 pring->flag = prev_pring_flag;
8334 }
8335
8336 spin_unlock_irqrestore(&phba->hbalock, flags);
8337
a257bf90
JS
8338 /* Cancel all the IOCBs from the completions list */
8339 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8340 IOERR_SLI_DOWN);
92d7f7b0
JS
8341 return 1;
8342}
8343
e59058c4 8344/**
3621a710 8345 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
8346 * @phba: Pointer to HBA context object.
8347 *
8348 * This function cleans up all iocb, buffers, mailbox commands
8349 * while shutting down the HBA. This function is called with no
8350 * lock held and always returns 1.
8351 * This function does the following to cleanup driver resources:
8352 * - Free discovery resources for each virtual port
8353 * - Cleanup any pending fabric iocbs
8354 * - Iterate through the iocb txq and free each entry
8355 * in the list.
8356 * - Free up any buffer posted to the HBA
8357 * - Free mailbox commands in the mailbox queue.
8358 **/
dea3101e 8359int
2e0fef85 8360lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 8361{
2534ba75 8362 LIST_HEAD(completions);
2e0fef85 8363 struct lpfc_sli *psli = &phba->sli;
dea3101e 8364 struct lpfc_sli_ring *pring;
0ff10d46 8365 struct lpfc_dmabuf *buf_ptr;
dea3101e 8366 unsigned long flags = 0;
04c68496
JS
8367 int i;
8368
8369 /* Shutdown the mailbox command sub-system */
8370 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 8371
dea3101e 8372 lpfc_hba_down_prep(phba);
8373
92d7f7b0
JS
8374 lpfc_fabric_abort_hba(phba);
8375
2e0fef85 8376 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 8377 for (i = 0; i < psli->num_rings; i++) {
8378 pring = &psli->ring[i];
5e9d9b82
JS
8379 /* Only slow rings */
8380 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 8381 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
8382 /* Set the lpfc data pending flag */
8383 set_bit(LPFC_DATA_READY, &phba->data_flags);
8384 }
dea3101e 8385
8386 /*
8387 * Error everything on the txq since these iocbs have not been
8388 * given to the FW yet.
8389 */
2534ba75 8390 list_splice_init(&pring->txq, &completions);
dea3101e 8391 pring->txq_cnt = 0;
8392
2534ba75 8393 }
2e0fef85 8394 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 8395
a257bf90
JS
8396 /* Cancel all the IOCBs from the completions list */
8397 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8398 IOERR_SLI_DOWN);
dea3101e 8399
0ff10d46
JS
8400 spin_lock_irqsave(&phba->hbalock, flags);
8401 list_splice_init(&phba->elsbuf, &completions);
8402 phba->elsbuf_cnt = 0;
8403 phba->elsbuf_prev_cnt = 0;
8404 spin_unlock_irqrestore(&phba->hbalock, flags);
8405
8406 while (!list_empty(&completions)) {
8407 list_remove_head(&completions, buf_ptr,
8408 struct lpfc_dmabuf, list);
8409 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8410 kfree(buf_ptr);
8411 }
8412
dea3101e 8413 /* Return any active mbox cmds */
8414 del_timer_sync(&psli->mbox_tmo);
2e0fef85 8415
da0436e9 8416 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 8417 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 8418 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 8419
da0436e9
JS
8420 return 1;
8421}
8422
e59058c4 8423/**
3621a710 8424 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
8425 * @srcp: Source memory pointer.
8426 * @destp: Destination memory pointer.
8427 * @cnt: Number of words required to be copied.
8428 *
8429 * This function is used for copying data between driver memory
8430 * and the SLI memory. This function also changes the endianness
8431 * of each word if native endianness is different from SLI
8432 * endianness. This function can be called with or without
8433 * lock.
8434 **/
dea3101e 8435void
8436lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8437{
8438 uint32_t *src = srcp;
8439 uint32_t *dest = destp;
8440 uint32_t ldata;
8441 int i;
8442
8443 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8444 ldata = *src;
8445 ldata = le32_to_cpu(ldata);
8446 *dest = ldata;
8447 src++;
8448 dest++;
8449 }
8450}
8451
e59058c4 8452
a0c87cbd
JS
8453/**
8454 * lpfc_sli_bemem_bcopy - SLI memory copy function
8455 * @srcp: Source memory pointer.
8456 * @destp: Destination memory pointer.
8457 * @cnt: Number of words required to be copied.
8458 *
8459 * This function is used for copying data between a data structure
8460 * with big endian representation to local endianness.
8461 * This function can be called with or without lock.
8462 **/
8463void
8464lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8465{
8466 uint32_t *src = srcp;
8467 uint32_t *dest = destp;
8468 uint32_t ldata;
8469 int i;
8470
8471 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8472 ldata = *src;
8473 ldata = be32_to_cpu(ldata);
8474 *dest = ldata;
8475 src++;
8476 dest++;
8477 }
8478}
8479
e59058c4 8480/**
3621a710 8481 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
8482 * @phba: Pointer to HBA context object.
8483 * @pring: Pointer to driver SLI ring object.
8484 * @mp: Pointer to driver buffer object.
8485 *
8486 * This function is called with no lock held.
8487 * It always return zero after adding the buffer to the postbufq
8488 * buffer list.
8489 **/
dea3101e 8490int
2e0fef85
JS
8491lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8492 struct lpfc_dmabuf *mp)
dea3101e 8493{
8494 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8495 later */
2e0fef85 8496 spin_lock_irq(&phba->hbalock);
dea3101e 8497 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 8498 pring->postbufq_cnt++;
2e0fef85 8499 spin_unlock_irq(&phba->hbalock);
dea3101e 8500 return 0;
8501}
8502
e59058c4 8503/**
3621a710 8504 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
8505 * @phba: Pointer to HBA context object.
8506 *
8507 * When HBQ is enabled, buffers are searched based on tags. This function
8508 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8509 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8510 * does not conflict with tags of buffer posted for unsolicited events.
8511 * The function returns the allocated tag. The function is called with
8512 * no locks held.
8513 **/
76bb24ef
JS
8514uint32_t
8515lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8516{
8517 spin_lock_irq(&phba->hbalock);
8518 phba->buffer_tag_count++;
8519 /*
8520 * Always set the QUE_BUFTAG_BIT to distiguish between
8521 * a tag assigned by HBQ.
8522 */
8523 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8524 spin_unlock_irq(&phba->hbalock);
8525 return phba->buffer_tag_count;
8526}
8527
e59058c4 8528/**
3621a710 8529 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
8530 * @phba: Pointer to HBA context object.
8531 * @pring: Pointer to driver SLI ring object.
8532 * @tag: Buffer tag.
8533 *
8534 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8535 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8536 * iocb is posted to the response ring with the tag of the buffer.
8537 * This function searches the pring->postbufq list using the tag
8538 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8539 * iocb. If the buffer is found then lpfc_dmabuf object of the
8540 * buffer is returned to the caller else NULL is returned.
8541 * This function is called with no lock held.
8542 **/
76bb24ef
JS
8543struct lpfc_dmabuf *
8544lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8545 uint32_t tag)
8546{
8547 struct lpfc_dmabuf *mp, *next_mp;
8548 struct list_head *slp = &pring->postbufq;
8549
25985edc 8550 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
8551 spin_lock_irq(&phba->hbalock);
8552 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8553 if (mp->buffer_tag == tag) {
8554 list_del_init(&mp->list);
8555 pring->postbufq_cnt--;
8556 spin_unlock_irq(&phba->hbalock);
8557 return mp;
8558 }
8559 }
8560
8561 spin_unlock_irq(&phba->hbalock);
8562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 8563 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
8564 "ring %d Data x%lx x%p x%p x%x\n",
8565 pring->ringno, (unsigned long) tag,
8566 slp->next, slp->prev, pring->postbufq_cnt);
8567
8568 return NULL;
8569}
dea3101e 8570
e59058c4 8571/**
3621a710 8572 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
8573 * @phba: Pointer to HBA context object.
8574 * @pring: Pointer to driver SLI ring object.
8575 * @phys: DMA address of the buffer.
8576 *
8577 * This function searches the buffer list using the dma_address
8578 * of unsolicited event to find the driver's lpfc_dmabuf object
8579 * corresponding to the dma_address. The function returns the
8580 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8581 * This function is called by the ct and els unsolicited event
8582 * handlers to get the buffer associated with the unsolicited
8583 * event.
8584 *
8585 * This function is called with no lock held.
8586 **/
dea3101e 8587struct lpfc_dmabuf *
8588lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8589 dma_addr_t phys)
8590{
8591 struct lpfc_dmabuf *mp, *next_mp;
8592 struct list_head *slp = &pring->postbufq;
8593
25985edc 8594 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 8595 spin_lock_irq(&phba->hbalock);
dea3101e 8596 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8597 if (mp->phys == phys) {
8598 list_del_init(&mp->list);
8599 pring->postbufq_cnt--;
2e0fef85 8600 spin_unlock_irq(&phba->hbalock);
dea3101e 8601 return mp;
8602 }
8603 }
8604
2e0fef85 8605 spin_unlock_irq(&phba->hbalock);
dea3101e 8606 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 8607 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 8608 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 8609 pring->ringno, (unsigned long long)phys,
dea3101e 8610 slp->next, slp->prev, pring->postbufq_cnt);
8611 return NULL;
8612}
8613
e59058c4 8614/**
3621a710 8615 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
8616 * @phba: Pointer to HBA context object.
8617 * @cmdiocb: Pointer to driver command iocb object.
8618 * @rspiocb: Pointer to driver response iocb object.
8619 *
8620 * This function is the completion handler for the abort iocbs for
8621 * ELS commands. This function is called from the ELS ring event
8622 * handler with no lock held. This function frees memory resources
8623 * associated with the abort iocb.
8624 **/
dea3101e 8625static void
2e0fef85
JS
8626lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8627 struct lpfc_iocbq *rspiocb)
dea3101e 8628{
2e0fef85 8629 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 8630 uint16_t abort_iotag, abort_context;
92d7f7b0 8631 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
8632 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8633
8634 abort_iocb = NULL;
2680eeaa
JS
8635
8636 if (irsp->ulpStatus) {
8637 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8638 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8639
2e0fef85 8640 spin_lock_irq(&phba->hbalock);
45ed1190
JS
8641 if (phba->sli_rev < LPFC_SLI_REV4) {
8642 if (abort_iotag != 0 &&
8643 abort_iotag <= phba->sli.last_iotag)
8644 abort_iocb =
8645 phba->sli.iocbq_lookup[abort_iotag];
8646 } else
8647 /* For sli4 the abort_tag is the XRI,
8648 * so the abort routine puts the iotag of the iocb
8649 * being aborted in the context field of the abort
8650 * IOCB.
8651 */
8652 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 8653
58da1ffb
JS
8654 /*
8655 * If the iocb is not found in Firmware queue the iocb
8656 * might have completed already. Do not free it again.
8657 */
9b379605 8658 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
45ed1190
JS
8659 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8660 spin_unlock_irq(&phba->hbalock);
8661 lpfc_sli_release_iocbq(phba, cmdiocb);
8662 return;
8663 }
8664 /* For SLI4 the ulpContext field for abort IOCB
8665 * holds the iotag of the IOCB being aborted so
8666 * the local abort_context needs to be reset to
8667 * match the aborted IOCBs ulpContext.
8668 */
8669 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8670 abort_context = abort_iocb->iocb.ulpContext;
58da1ffb 8671 }
2a9bf3d0
JS
8672
8673 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8674 "0327 Cannot abort els iocb %p "
8675 "with tag %x context %x, abort status %x, "
8676 "abort code %x\n",
8677 abort_iocb, abort_iotag, abort_context,
8678 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa
JS
8679 /*
8680 * make sure we have the right iocbq before taking it
8681 * off the txcmplq and try to call completion routine.
8682 */
2e0fef85
JS
8683 if (!abort_iocb ||
8684 abort_iocb->iocb.ulpContext != abort_context ||
8685 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8686 spin_unlock_irq(&phba->hbalock);
341af102
JS
8687 else if (phba->sli_rev < LPFC_SLI_REV4) {
8688 /*
8689 * leave the SLI4 aborted command on the txcmplq
8690 * list and the command complete WCQE's XB bit
8691 * will tell whether the SGL (XRI) can be released
8692 * immediately or to the aborted SGL list for the
8693 * following abort XRI from the HBA.
8694 */
92d7f7b0 8695 list_del_init(&abort_iocb->list);
2a9bf3d0
JS
8696 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8697 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8698 pring->txcmplq_cnt--;
8699 }
2680eeaa 8700
0ff10d46
JS
8701 /* Firmware could still be in progress of DMAing
8702 * payload, so don't free data buffer till after
8703 * a hbeat.
8704 */
8705 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
92d7f7b0 8706 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
341af102
JS
8707 spin_unlock_irq(&phba->hbalock);
8708
92d7f7b0 8709 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
341af102 8710 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
92d7f7b0 8711 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
49198b37
JS
8712 } else
8713 spin_unlock_irq(&phba->hbalock);
2680eeaa
JS
8714 }
8715
604a3e30 8716 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e 8717 return;
8718}
8719
e59058c4 8720/**
3621a710 8721 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
8722 * @phba: Pointer to HBA context object.
8723 * @cmdiocb: Pointer to driver command iocb object.
8724 * @rspiocb: Pointer to driver response iocb object.
8725 *
8726 * The function is called from SLI ring event handler with no
8727 * lock held. This function is the completion handler for ELS commands
8728 * which are aborted. The function frees memory resources used for
8729 * the aborted ELS commands.
8730 **/
92d7f7b0
JS
8731static void
8732lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8733 struct lpfc_iocbq *rspiocb)
8734{
8735 IOCB_t *irsp = &rspiocb->iocb;
8736
8737 /* ELS cmd tag <ulpIoTag> completes */
8738 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 8739 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 8740 "x%x x%x x%x\n",
e8b62011 8741 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 8742 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
8743 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8744 lpfc_ct_free_iocb(phba, cmdiocb);
8745 else
8746 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
8747 return;
8748}
8749
e59058c4 8750/**
5af5eee7 8751 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
8752 * @phba: Pointer to HBA context object.
8753 * @pring: Pointer to driver SLI ring object.
8754 * @cmdiocb: Pointer to driver command iocb object.
8755 *
5af5eee7
JS
8756 * This function issues an abort iocb for the provided command iocb down to
8757 * the port. Other than the case the outstanding command iocb is an abort
8758 * request, this function issues abort out unconditionally. This function is
8759 * called with hbalock held. The function returns 0 when it fails due to
8760 * memory allocation failure or when the command iocb is an abort request.
e59058c4 8761 **/
5af5eee7
JS
8762static int
8763lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8764 struct lpfc_iocbq *cmdiocb)
dea3101e 8765{
2e0fef85 8766 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 8767 struct lpfc_iocbq *abtsiocbp;
dea3101e 8768 IOCB_t *icmd = NULL;
8769 IOCB_t *iabt = NULL;
5af5eee7 8770 int retval;
07951076 8771
92d7f7b0
JS
8772 /*
8773 * There are certain command types we don't want to abort. And we
8774 * don't want to abort commands that are already in the process of
8775 * being aborted.
07951076
JS
8776 */
8777 icmd = &cmdiocb->iocb;
2e0fef85 8778 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
8779 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8780 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
8781 return 0;
8782
dea3101e 8783 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 8784 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 8785 if (abtsiocbp == NULL)
8786 return 0;
dea3101e 8787
07951076 8788 /* This signals the response to set the correct status
341af102 8789 * before calling the completion handler
07951076
JS
8790 */
8791 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
8792
dea3101e 8793 iabt = &abtsiocbp->iocb;
07951076
JS
8794 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
8795 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 8796 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 8797 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
8798 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
8799 }
da0436e9
JS
8800 else
8801 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
8802 iabt->ulpLe = 1;
8803 iabt->ulpClass = icmd->ulpClass;
dea3101e 8804
5ffc266e
JS
8805 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8806 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
8807 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
8808 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 8809
2e0fef85 8810 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
8811 iabt->ulpCommand = CMD_ABORT_XRI_CN;
8812 else
8813 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 8814
07951076 8815 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 8816
e8b62011
JS
8817 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
8818 "0339 Abort xri x%x, original iotag x%x, "
8819 "abort cmd iotag x%x\n",
2a9bf3d0 8820 iabt->un.acxri.abortIoTag,
e8b62011 8821 iabt->un.acxri.abortContextTag,
2a9bf3d0 8822 abtsiocbp->iotag);
da0436e9 8823 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 8824
d7c255b2
JS
8825 if (retval)
8826 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
8827
8828 /*
8829 * Caller to this routine should check for IOCB_ERROR
8830 * and handle it properly. This routine no longer removes
8831 * iocb off txcmplq and call compl in case of IOCB_ERROR.
8832 */
8833 return retval;
8834}
8835
8836/**
8837 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
8838 * @phba: Pointer to HBA context object.
8839 * @pring: Pointer to driver SLI ring object.
8840 * @cmdiocb: Pointer to driver command iocb object.
8841 *
8842 * This function issues an abort iocb for the provided command iocb. In case
8843 * of unloading, the abort iocb will not be issued to commands on the ELS
8844 * ring. Instead, the callback function shall be changed to those commands
8845 * so that nothing happens when them finishes. This function is called with
8846 * hbalock held. The function returns 0 when the command iocb is an abort
8847 * request.
8848 **/
8849int
8850lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8851 struct lpfc_iocbq *cmdiocb)
8852{
8853 struct lpfc_vport *vport = cmdiocb->vport;
8854 int retval = IOCB_ERROR;
8855 IOCB_t *icmd = NULL;
8856
8857 /*
8858 * There are certain command types we don't want to abort. And we
8859 * don't want to abort commands that are already in the process of
8860 * being aborted.
8861 */
8862 icmd = &cmdiocb->iocb;
8863 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
8864 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8865 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
8866 return 0;
8867
8868 /*
8869 * If we're unloading, don't abort iocb on the ELS ring, but change
8870 * the callback so that nothing happens when it finishes.
8871 */
8872 if ((vport->load_flag & FC_UNLOADING) &&
8873 (pring->ringno == LPFC_ELS_RING)) {
8874 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
8875 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
8876 else
8877 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
8878 goto abort_iotag_exit;
8879 }
8880
8881 /* Now, we try to issue the abort to the cmdiocb out */
8882 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
8883
07951076 8884abort_iotag_exit:
2e0fef85
JS
8885 /*
8886 * Caller to this routine should check for IOCB_ERROR
8887 * and handle it properly. This routine no longer removes
8888 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 8889 */
2e0fef85 8890 return retval;
dea3101e 8891}
8892
5af5eee7
JS
8893/**
8894 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
8895 * @phba: Pointer to HBA context object.
8896 * @pring: Pointer to driver SLI ring object.
8897 *
8898 * This function aborts all iocbs in the given ring and frees all the iocb
8899 * objects in txq. This function issues abort iocbs unconditionally for all
8900 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
8901 * to complete before the return of this function. The caller is not required
8902 * to hold any locks.
8903 **/
8904static void
8905lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
8906{
8907 LIST_HEAD(completions);
8908 struct lpfc_iocbq *iocb, *next_iocb;
8909
8910 if (pring->ringno == LPFC_ELS_RING)
8911 lpfc_fabric_abort_hba(phba);
8912
8913 spin_lock_irq(&phba->hbalock);
8914
8915 /* Take off all the iocbs on txq for cancelling */
8916 list_splice_init(&pring->txq, &completions);
8917 pring->txq_cnt = 0;
8918
8919 /* Next issue ABTS for everything on the txcmplq */
8920 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
8921 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
8922
8923 spin_unlock_irq(&phba->hbalock);
8924
8925 /* Cancel all the IOCBs from the completions list */
8926 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8927 IOERR_SLI_ABORTED);
8928}
8929
8930/**
8931 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
8932 * @phba: pointer to lpfc HBA data structure.
8933 *
8934 * This routine will abort all pending and outstanding iocbs to an HBA.
8935 **/
8936void
8937lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
8938{
8939 struct lpfc_sli *psli = &phba->sli;
8940 struct lpfc_sli_ring *pring;
8941 int i;
8942
8943 for (i = 0; i < psli->num_rings; i++) {
8944 pring = &psli->ring[i];
8945 lpfc_sli_iocb_ring_abort(phba, pring);
8946 }
8947}
8948
e59058c4 8949/**
3621a710 8950 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
8951 * @iocbq: Pointer to driver iocb object.
8952 * @vport: Pointer to driver virtual port object.
8953 * @tgt_id: SCSI ID of the target.
8954 * @lun_id: LUN ID of the scsi device.
8955 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
8956 *
3621a710 8957 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
8958 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
8959 * 0 if the filtering criteria is met for the given iocb and will return
8960 * 1 if the filtering criteria is not met.
8961 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
8962 * given iocb is for the SCSI device specified by vport, tgt_id and
8963 * lun_id parameter.
8964 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
8965 * given iocb is for the SCSI target specified by vport and tgt_id
8966 * parameters.
8967 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
8968 * given iocb is for the SCSI host associated with the given vport.
8969 * This function is called with no locks held.
8970 **/
dea3101e 8971static int
51ef4c26
JS
8972lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
8973 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 8974 lpfc_ctx_cmd ctx_cmd)
dea3101e 8975{
0bd4ca25 8976 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 8977 int rc = 1;
8978
0bd4ca25
JSEC
8979 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
8980 return rc;
8981
51ef4c26
JS
8982 if (iocbq->vport != vport)
8983 return rc;
8984
0bd4ca25 8985 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 8986
495a714c 8987 if (lpfc_cmd->pCmd == NULL)
dea3101e 8988 return rc;
8989
8990 switch (ctx_cmd) {
8991 case LPFC_CTX_LUN:
495a714c
JS
8992 if ((lpfc_cmd->rdata->pnode) &&
8993 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
8994 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e 8995 rc = 0;
8996 break;
8997 case LPFC_CTX_TGT:
495a714c
JS
8998 if ((lpfc_cmd->rdata->pnode) &&
8999 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e 9000 rc = 0;
9001 break;
dea3101e 9002 case LPFC_CTX_HOST:
9003 rc = 0;
9004 break;
9005 default:
9006 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 9007 __func__, ctx_cmd);
dea3101e 9008 break;
9009 }
9010
9011 return rc;
9012}
9013
e59058c4 9014/**
3621a710 9015 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
9016 * @vport: Pointer to virtual port.
9017 * @tgt_id: SCSI ID of the target.
9018 * @lun_id: LUN ID of the scsi device.
9019 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9020 *
9021 * This function returns number of FCP commands pending for the vport.
9022 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9023 * commands pending on the vport associated with SCSI device specified
9024 * by tgt_id and lun_id parameters.
9025 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9026 * commands pending on the vport associated with SCSI target specified
9027 * by tgt_id parameter.
9028 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9029 * commands pending on the vport.
9030 * This function returns the number of iocbs which satisfy the filter.
9031 * This function is called without any lock held.
9032 **/
dea3101e 9033int
51ef4c26
JS
9034lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9035 lpfc_ctx_cmd ctx_cmd)
dea3101e 9036{
51ef4c26 9037 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9038 struct lpfc_iocbq *iocbq;
9039 int sum, i;
dea3101e 9040
0bd4ca25
JSEC
9041 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9042 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9043
51ef4c26
JS
9044 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9045 ctx_cmd) == 0)
0bd4ca25 9046 sum++;
dea3101e 9047 }
0bd4ca25 9048
dea3101e 9049 return sum;
9050}
9051
e59058c4 9052/**
3621a710 9053 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
9054 * @phba: Pointer to HBA context object
9055 * @cmdiocb: Pointer to command iocb object.
9056 * @rspiocb: Pointer to response iocb object.
9057 *
9058 * This function is called when an aborted FCP iocb completes. This
9059 * function is called by the ring event handler with no lock held.
9060 * This function frees the iocb.
9061 **/
5eb95af0 9062void
2e0fef85
JS
9063lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9064 struct lpfc_iocbq *rspiocb)
5eb95af0 9065{
604a3e30 9066 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
9067 return;
9068}
9069
e59058c4 9070/**
3621a710 9071 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
9072 * @vport: Pointer to virtual port.
9073 * @pring: Pointer to driver SLI ring object.
9074 * @tgt_id: SCSI ID of the target.
9075 * @lun_id: LUN ID of the scsi device.
9076 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9077 *
9078 * This function sends an abort command for every SCSI command
9079 * associated with the given virtual port pending on the ring
9080 * filtered by lpfc_sli_validate_fcp_iocb function.
9081 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9082 * FCP iocbs associated with lun specified by tgt_id and lun_id
9083 * parameters
9084 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9085 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9086 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9087 * FCP iocbs associated with virtual port.
9088 * This function returns number of iocbs it failed to abort.
9089 * This function is called with no locks held.
9090 **/
dea3101e 9091int
51ef4c26
JS
9092lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9093 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 9094{
51ef4c26 9095 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9096 struct lpfc_iocbq *iocbq;
9097 struct lpfc_iocbq *abtsiocb;
dea3101e 9098 IOCB_t *cmd = NULL;
dea3101e 9099 int errcnt = 0, ret_val = 0;
0bd4ca25 9100 int i;
dea3101e 9101
0bd4ca25
JSEC
9102 for (i = 1; i <= phba->sli.last_iotag; i++) {
9103 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9104
51ef4c26 9105 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 9106 abort_cmd) != 0)
dea3101e 9107 continue;
9108
9109 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 9110 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e 9111 if (abtsiocb == NULL) {
9112 errcnt++;
9113 continue;
9114 }
dea3101e 9115
0bd4ca25 9116 cmd = &iocbq->iocb;
dea3101e 9117 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9118 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
9119 if (phba->sli_rev == LPFC_SLI_REV4)
9120 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9121 else
9122 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 9123 abtsiocb->iocb.ulpLe = 1;
9124 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 9125 abtsiocb->vport = phba->pport;
dea3101e 9126
5ffc266e
JS
9127 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9128 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
9129 if (iocbq->iocb_flag & LPFC_IO_FCP)
9130 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 9131
2e0fef85 9132 if (lpfc_is_link_up(phba))
dea3101e 9133 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9134 else
9135 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9136
5eb95af0
JSEC
9137 /* Setup callback routine and issue the command. */
9138 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
9139 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9140 abtsiocb, 0);
dea3101e 9141 if (ret_val == IOCB_ERROR) {
604a3e30 9142 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e 9143 errcnt++;
9144 continue;
9145 }
9146 }
9147
9148 return errcnt;
9149}
9150
e59058c4 9151/**
3621a710 9152 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
9153 * @phba: Pointer to HBA context object.
9154 * @cmdiocbq: Pointer to command iocb.
9155 * @rspiocbq: Pointer to response iocb.
9156 *
9157 * This function is the completion handler for iocbs issued using
9158 * lpfc_sli_issue_iocb_wait function. This function is called by the
9159 * ring event handler function without any lock held. This function
9160 * can be called from both worker thread context and interrupt
9161 * context. This function also can be called from other thread which
9162 * cleans up the SLI layer objects.
9163 * This function copy the contents of the response iocb to the
9164 * response iocb memory object provided by the caller of
9165 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9166 * sleeps for the iocb completion.
9167 **/
68876920
JSEC
9168static void
9169lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9170 struct lpfc_iocbq *cmdiocbq,
9171 struct lpfc_iocbq *rspiocbq)
dea3101e 9172{
68876920
JSEC
9173 wait_queue_head_t *pdone_q;
9174 unsigned long iflags;
0f65ff68 9175 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 9176
2e0fef85 9177 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
9178 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9179 if (cmdiocbq->context2 && rspiocbq)
9180 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9181 &rspiocbq->iocb, sizeof(IOCB_t));
9182
0f65ff68
JS
9183 /* Set the exchange busy flag for task management commands */
9184 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9185 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9186 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9187 cur_iocbq);
9188 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9189 }
9190
68876920 9191 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
9192 if (pdone_q)
9193 wake_up(pdone_q);
858c9f6c 9194 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e 9195 return;
9196}
9197
d11e31dd
JS
9198/**
9199 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9200 * @phba: Pointer to HBA context object..
9201 * @piocbq: Pointer to command iocb.
9202 * @flag: Flag to test.
9203 *
9204 * This routine grabs the hbalock and then test the iocb_flag to
9205 * see if the passed in flag is set.
9206 * Returns:
9207 * 1 if flag is set.
9208 * 0 if flag is not set.
9209 **/
9210static int
9211lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9212 struct lpfc_iocbq *piocbq, uint32_t flag)
9213{
9214 unsigned long iflags;
9215 int ret;
9216
9217 spin_lock_irqsave(&phba->hbalock, iflags);
9218 ret = piocbq->iocb_flag & flag;
9219 spin_unlock_irqrestore(&phba->hbalock, iflags);
9220 return ret;
9221
9222}
9223
e59058c4 9224/**
3621a710 9225 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
9226 * @phba: Pointer to HBA context object..
9227 * @pring: Pointer to sli ring.
9228 * @piocb: Pointer to command iocb.
9229 * @prspiocbq: Pointer to response iocb.
9230 * @timeout: Timeout in number of seconds.
9231 *
9232 * This function issues the iocb to firmware and waits for the
9233 * iocb to complete. If the iocb command is not
9234 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9235 * Caller should not free the iocb resources if this function
9236 * returns IOCB_TIMEDOUT.
9237 * The function waits for the iocb completion using an
9238 * non-interruptible wait.
9239 * This function will sleep while waiting for iocb completion.
9240 * So, this function should not be called from any context which
9241 * does not allow sleeping. Due to the same reason, this function
9242 * cannot be called with interrupt disabled.
9243 * This function assumes that the iocb completions occur while
9244 * this function sleep. So, this function cannot be called from
9245 * the thread which process iocb completion for this ring.
9246 * This function clears the iocb_flag of the iocb object before
9247 * issuing the iocb and the iocb completion handler sets this
9248 * flag and wakes this thread when the iocb completes.
9249 * The contents of the response iocb will be copied to prspiocbq
9250 * by the completion handler when the command completes.
9251 * This function returns IOCB_SUCCESS when success.
9252 * This function is called with no lock held.
9253 **/
dea3101e 9254int
2e0fef85 9255lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 9256 uint32_t ring_number,
2e0fef85
JS
9257 struct lpfc_iocbq *piocb,
9258 struct lpfc_iocbq *prspiocbq,
68876920 9259 uint32_t timeout)
dea3101e 9260{
7259f0d0 9261 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
9262 long timeleft, timeout_req = 0;
9263 int retval = IOCB_SUCCESS;
875fbdfe 9264 uint32_t creg_val;
2a9bf3d0 9265 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e 9266 /*
68876920
JSEC
9267 * If the caller has provided a response iocbq buffer, then context2
9268 * is NULL or its an error.
dea3101e 9269 */
68876920
JSEC
9270 if (prspiocbq) {
9271 if (piocb->context2)
9272 return IOCB_ERROR;
9273 piocb->context2 = prspiocbq;
dea3101e 9274 }
9275
68876920
JSEC
9276 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9277 piocb->context_un.wait_queue = &done_q;
9278 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 9279
875fbdfe 9280 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
9281 if (lpfc_readl(phba->HCregaddr, &creg_val))
9282 return IOCB_ERROR;
875fbdfe
JSEC
9283 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9284 writel(creg_val, phba->HCregaddr);
9285 readl(phba->HCregaddr); /* flush */
9286 }
9287
2a9bf3d0
JS
9288 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9289 SLI_IOCB_RET_IOCB);
68876920
JSEC
9290 if (retval == IOCB_SUCCESS) {
9291 timeout_req = timeout * HZ;
68876920 9292 timeleft = wait_event_timeout(done_q,
d11e31dd 9293 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 9294 timeout_req);
dea3101e 9295
7054a606
JS
9296 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9297 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 9298 "0331 IOCB wake signaled\n");
7054a606 9299 } else if (timeleft == 0) {
68876920 9300 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
9301 "0338 IOCB wait timeout error - no "
9302 "wake response Data x%x\n", timeout);
68876920 9303 retval = IOCB_TIMEDOUT;
7054a606 9304 } else {
68876920 9305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
9306 "0330 IOCB wake NOT set, "
9307 "Data x%x x%lx\n",
68876920
JSEC
9308 timeout, (timeleft / jiffies));
9309 retval = IOCB_TIMEDOUT;
dea3101e 9310 }
2a9bf3d0
JS
9311 } else if (retval == IOCB_BUSY) {
9312 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9313 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9314 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9315 return retval;
68876920
JSEC
9316 } else {
9317 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 9318 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 9319 retval);
68876920 9320 retval = IOCB_ERROR;
dea3101e 9321 }
9322
875fbdfe 9323 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
9324 if (lpfc_readl(phba->HCregaddr, &creg_val))
9325 return IOCB_ERROR;
875fbdfe
JSEC
9326 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9327 writel(creg_val, phba->HCregaddr);
9328 readl(phba->HCregaddr); /* flush */
9329 }
9330
68876920
JSEC
9331 if (prspiocbq)
9332 piocb->context2 = NULL;
9333
9334 piocb->context_un.wait_queue = NULL;
9335 piocb->iocb_cmpl = NULL;
dea3101e 9336 return retval;
9337}
68876920 9338
e59058c4 9339/**
3621a710 9340 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
9341 * @phba: Pointer to HBA context object.
9342 * @pmboxq: Pointer to driver mailbox object.
9343 * @timeout: Timeout in number of seconds.
9344 *
9345 * This function issues the mailbox to firmware and waits for the
9346 * mailbox command to complete. If the mailbox command is not
9347 * completed within timeout seconds, it returns MBX_TIMEOUT.
9348 * The function waits for the mailbox completion using an
9349 * interruptible wait. If the thread is woken up due to a
9350 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9351 * should not free the mailbox resources, if this function returns
9352 * MBX_TIMEOUT.
9353 * This function will sleep while waiting for mailbox completion.
9354 * So, this function should not be called from any context which
9355 * does not allow sleeping. Due to the same reason, this function
9356 * cannot be called with interrupt disabled.
9357 * This function assumes that the mailbox completion occurs while
9358 * this function sleep. So, this function cannot be called from
9359 * the worker thread which processes mailbox completion.
9360 * This function is called in the context of HBA management
9361 * applications.
9362 * This function returns MBX_SUCCESS when successful.
9363 * This function is called with no lock held.
9364 **/
dea3101e 9365int
2e0fef85 9366lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e 9367 uint32_t timeout)
9368{
7259f0d0 9369 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 9370 int retval;
858c9f6c 9371 unsigned long flag;
dea3101e 9372
9373 /* The caller must leave context1 empty. */
98c9ea5c 9374 if (pmboxq->context1)
2e0fef85 9375 return MBX_NOT_FINISHED;
dea3101e 9376
495a714c 9377 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e 9378 /* setup wake call as IOCB callback */
9379 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9380 /* setup context field to pass wait_queue pointer to wake function */
9381 pmboxq->context1 = &done_q;
9382
dea3101e 9383 /* now issue the command */
9384 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
9385
9386 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
9387 wait_event_interruptible_timeout(done_q,
9388 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9389 timeout * HZ);
9390
858c9f6c 9391 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 9392 pmboxq->context1 = NULL;
7054a606
JS
9393 /*
9394 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9395 * else do not free the resources.
9396 */
d7c47992 9397 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 9398 retval = MBX_SUCCESS;
d7c47992
JS
9399 lpfc_sli4_swap_str(phba, pmboxq);
9400 } else {
7054a606 9401 retval = MBX_TIMEOUT;
858c9f6c
JS
9402 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9403 }
9404 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 9405 }
9406
dea3101e 9407 return retval;
9408}
9409
e59058c4 9410/**
3772a991 9411 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
9412 * @phba: Pointer to HBA context.
9413 *
3772a991
JS
9414 * This function is called to shutdown the driver's mailbox sub-system.
9415 * It first marks the mailbox sub-system is in a block state to prevent
9416 * the asynchronous mailbox command from issued off the pending mailbox
9417 * command queue. If the mailbox command sub-system shutdown is due to
9418 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9419 * the mailbox sub-system flush routine to forcefully bring down the
9420 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9421 * as with offline or HBA function reset), this routine will wait for the
9422 * outstanding mailbox command to complete before invoking the mailbox
9423 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 9424 **/
3772a991
JS
9425void
9426lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 9427{
3772a991 9428 struct lpfc_sli *psli = &phba->sli;
3772a991 9429 unsigned long timeout;
b4c02652 9430
a183a15f 9431 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3772a991
JS
9432 spin_lock_irq(&phba->hbalock);
9433 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9434 spin_unlock_irq(&phba->hbalock);
b4c02652 9435
3772a991 9436 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 9437 spin_lock_irq(&phba->hbalock);
3772a991
JS
9438 /* Determine how long we might wait for the active mailbox
9439 * command to be gracefully completed by firmware.
9440 */
a183a15f
JS
9441 if (phba->sli.mbox_active)
9442 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9443 phba->sli.mbox_active) *
9444 1000) + jiffies;
9445 spin_unlock_irq(&phba->hbalock);
9446
3772a991
JS
9447 while (phba->sli.mbox_active) {
9448 /* Check active mailbox complete status every 2ms */
9449 msleep(2);
9450 if (time_after(jiffies, timeout))
9451 /* Timeout, let the mailbox flush routine to
9452 * forcefully release active mailbox command
9453 */
9454 break;
9455 }
9456 }
9457 lpfc_sli_mbox_sys_flush(phba);
9458}
ed957684 9459
3772a991
JS
9460/**
9461 * lpfc_sli_eratt_read - read sli-3 error attention events
9462 * @phba: Pointer to HBA context.
9463 *
9464 * This function is called to read the SLI3 device error attention registers
9465 * for possible error attention events. The caller must hold the hostlock
9466 * with spin_lock_irq().
9467 *
25985edc 9468 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
9469 * Register and returns 0 otherwise.
9470 **/
9471static int
9472lpfc_sli_eratt_read(struct lpfc_hba *phba)
9473{
9474 uint32_t ha_copy;
b4c02652 9475
3772a991 9476 /* Read chip Host Attention (HA) register */
9940b97b
JS
9477 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9478 goto unplug_err;
9479
3772a991
JS
9480 if (ha_copy & HA_ERATT) {
9481 /* Read host status register to retrieve error event */
9940b97b
JS
9482 if (lpfc_sli_read_hs(phba))
9483 goto unplug_err;
b4c02652 9484
3772a991
JS
9485 /* Check if there is a deferred error condition is active */
9486 if ((HS_FFER1 & phba->work_hs) &&
9487 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 9488 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 9489 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
9490 /* Clear all interrupt enable conditions */
9491 writel(0, phba->HCregaddr);
9492 readl(phba->HCregaddr);
9493 }
9494
9495 /* Set the driver HA work bitmap */
3772a991
JS
9496 phba->work_ha |= HA_ERATT;
9497 /* Indicate polling handles this ERATT */
9498 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
9499 return 1;
9500 }
9501 return 0;
9940b97b
JS
9502
9503unplug_err:
9504 /* Set the driver HS work bitmap */
9505 phba->work_hs |= UNPLUG_ERR;
9506 /* Set the driver HA work bitmap */
9507 phba->work_ha |= HA_ERATT;
9508 /* Indicate polling handles this ERATT */
9509 phba->hba_flag |= HBA_ERATT_HANDLED;
9510 return 1;
b4c02652
JS
9511}
9512
da0436e9
JS
9513/**
9514 * lpfc_sli4_eratt_read - read sli-4 error attention events
9515 * @phba: Pointer to HBA context.
9516 *
9517 * This function is called to read the SLI4 device error attention registers
9518 * for possible error attention events. The caller must hold the hostlock
9519 * with spin_lock_irq().
9520 *
25985edc 9521 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
9522 * Register and returns 0 otherwise.
9523 **/
9524static int
9525lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9526{
9527 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
9528 uint32_t if_type, portsmphr;
9529 struct lpfc_register portstat_reg;
da0436e9 9530
2fcee4bf
JS
9531 /*
9532 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
9533 * registers for error attention. This can be changed later.
9534 */
2fcee4bf
JS
9535 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9536 switch (if_type) {
9537 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
9538 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9539 &uerr_sta_lo) ||
9540 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9541 &uerr_sta_hi)) {
9542 phba->work_hs |= UNPLUG_ERR;
9543 phba->work_ha |= HA_ERATT;
9544 phba->hba_flag |= HBA_ERATT_HANDLED;
9545 return 1;
9546 }
2fcee4bf
JS
9547 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9548 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9550 "1423 HBA Unrecoverable error: "
9551 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9552 "ue_mask_lo_reg=0x%x, "
9553 "ue_mask_hi_reg=0x%x\n",
9554 uerr_sta_lo, uerr_sta_hi,
9555 phba->sli4_hba.ue_mask_lo,
9556 phba->sli4_hba.ue_mask_hi);
9557 phba->work_status[0] = uerr_sta_lo;
9558 phba->work_status[1] = uerr_sta_hi;
9559 phba->work_ha |= HA_ERATT;
9560 phba->hba_flag |= HBA_ERATT_HANDLED;
9561 return 1;
9562 }
9563 break;
9564 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
9565 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9566 &portstat_reg.word0) ||
9567 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9568 &portsmphr)){
9569 phba->work_hs |= UNPLUG_ERR;
9570 phba->work_ha |= HA_ERATT;
9571 phba->hba_flag |= HBA_ERATT_HANDLED;
9572 return 1;
9573 }
2fcee4bf
JS
9574 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9575 phba->work_status[0] =
9576 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9577 phba->work_status[1] =
9578 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9580 "2885 Port Error Detected: "
9581 "port status reg 0x%x, "
9582 "port smphr reg 0x%x, "
9583 "error 1=0x%x, error 2=0x%x\n",
9584 portstat_reg.word0,
9585 portsmphr,
9586 phba->work_status[0],
9587 phba->work_status[1]);
9588 phba->work_ha |= HA_ERATT;
9589 phba->hba_flag |= HBA_ERATT_HANDLED;
9590 return 1;
9591 }
9592 break;
9593 case LPFC_SLI_INTF_IF_TYPE_1:
9594 default:
a747c9ce 9595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
9596 "2886 HBA Error Attention on unsupported "
9597 "if type %d.", if_type);
a747c9ce 9598 return 1;
da0436e9 9599 }
2fcee4bf 9600
da0436e9
JS
9601 return 0;
9602}
9603
e59058c4 9604/**
3621a710 9605 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
9606 * @phba: Pointer to HBA context.
9607 *
3772a991 9608 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
9609 * error attention register bit for error attention events.
9610 *
25985edc 9611 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
9612 * Register and returns 0 otherwise.
9613 **/
9614int
9615lpfc_sli_check_eratt(struct lpfc_hba *phba)
9616{
9617 uint32_t ha_copy;
9618
9619 /* If somebody is waiting to handle an eratt, don't process it
9620 * here. The brdkill function will do this.
9621 */
9622 if (phba->link_flag & LS_IGNORE_ERATT)
9623 return 0;
9624
9625 /* Check if interrupt handler handles this ERATT */
9626 spin_lock_irq(&phba->hbalock);
9627 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9628 /* Interrupt handler has handled ERATT */
9629 spin_unlock_irq(&phba->hbalock);
9630 return 0;
9631 }
9632
a257bf90
JS
9633 /*
9634 * If there is deferred error attention, do not check for error
9635 * attention
9636 */
9637 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9638 spin_unlock_irq(&phba->hbalock);
9639 return 0;
9640 }
9641
3772a991
JS
9642 /* If PCI channel is offline, don't process it */
9643 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 9644 spin_unlock_irq(&phba->hbalock);
3772a991
JS
9645 return 0;
9646 }
9647
9648 switch (phba->sli_rev) {
9649 case LPFC_SLI_REV2:
9650 case LPFC_SLI_REV3:
9651 /* Read chip Host Attention (HA) register */
9652 ha_copy = lpfc_sli_eratt_read(phba);
9653 break;
da0436e9 9654 case LPFC_SLI_REV4:
2fcee4bf 9655 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
9656 ha_copy = lpfc_sli4_eratt_read(phba);
9657 break;
3772a991
JS
9658 default:
9659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9660 "0299 Invalid SLI revision (%d)\n",
9661 phba->sli_rev);
9662 ha_copy = 0;
9663 break;
9399627f
JS
9664 }
9665 spin_unlock_irq(&phba->hbalock);
3772a991
JS
9666
9667 return ha_copy;
9668}
9669
9670/**
9671 * lpfc_intr_state_check - Check device state for interrupt handling
9672 * @phba: Pointer to HBA context.
9673 *
9674 * This inline routine checks whether a device or its PCI slot is in a state
9675 * that the interrupt should be handled.
9676 *
9677 * This function returns 0 if the device or the PCI slot is in a state that
9678 * interrupt should be handled, otherwise -EIO.
9679 */
9680static inline int
9681lpfc_intr_state_check(struct lpfc_hba *phba)
9682{
9683 /* If the pci channel is offline, ignore all the interrupts */
9684 if (unlikely(pci_channel_offline(phba->pcidev)))
9685 return -EIO;
9686
9687 /* Update device level interrupt statistics */
9688 phba->sli.slistat.sli_intr++;
9689
9690 /* Ignore all interrupts during initialization. */
9691 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9692 return -EIO;
9693
9399627f
JS
9694 return 0;
9695}
9696
9697/**
3772a991 9698 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
9699 * @irq: Interrupt number.
9700 * @dev_id: The device context pointer.
9701 *
9399627f 9702 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
9703 * service routine when device with SLI-3 interface spec is enabled with
9704 * MSI-X multi-message interrupt mode and there are slow-path events in
9705 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9706 * interrupt mode, this function is called as part of the device-level
9707 * interrupt handler. When the PCI slot is in error recovery or the HBA
9708 * is undergoing initialization, the interrupt handler will not process
9709 * the interrupt. The link attention and ELS ring attention events are
9710 * handled by the worker thread. The interrupt handler signals the worker
9711 * thread and returns for these events. This function is called without
9712 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
9713 * structures.
9714 *
9715 * This function returns IRQ_HANDLED when interrupt is handled else it
9716 * returns IRQ_NONE.
e59058c4 9717 **/
dea3101e 9718irqreturn_t
3772a991 9719lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 9720{
2e0fef85 9721 struct lpfc_hba *phba;
a747c9ce 9722 uint32_t ha_copy, hc_copy;
dea3101e 9723 uint32_t work_ha_copy;
9724 unsigned long status;
5b75da2f 9725 unsigned long iflag;
dea3101e 9726 uint32_t control;
9727
92d7f7b0 9728 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
9729 struct lpfc_vport *vport;
9730 struct lpfc_nodelist *ndlp;
9731 struct lpfc_dmabuf *mp;
92d7f7b0
JS
9732 LPFC_MBOXQ_t *pmb;
9733 int rc;
9734
dea3101e 9735 /*
9736 * Get the driver's phba structure from the dev_id and
9737 * assume the HBA is not interrupting.
9738 */
9399627f 9739 phba = (struct lpfc_hba *)dev_id;
dea3101e 9740
9741 if (unlikely(!phba))
9742 return IRQ_NONE;
9743
dea3101e 9744 /*
9399627f
JS
9745 * Stuff needs to be attented to when this function is invoked as an
9746 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 9747 */
9399627f 9748 if (phba->intr_type == MSIX) {
3772a991
JS
9749 /* Check device state for handling interrupt */
9750 if (lpfc_intr_state_check(phba))
9399627f
JS
9751 return IRQ_NONE;
9752 /* Need to read HA REG for slow-path events */
5b75da2f 9753 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
9754 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9755 goto unplug_error;
9399627f
JS
9756 /* If somebody is waiting to handle an eratt don't process it
9757 * here. The brdkill function will do this.
9758 */
9759 if (phba->link_flag & LS_IGNORE_ERATT)
9760 ha_copy &= ~HA_ERATT;
9761 /* Check the need for handling ERATT in interrupt handler */
9762 if (ha_copy & HA_ERATT) {
9763 if (phba->hba_flag & HBA_ERATT_HANDLED)
9764 /* ERATT polling has handled ERATT */
9765 ha_copy &= ~HA_ERATT;
9766 else
9767 /* Indicate interrupt handler handles ERATT */
9768 phba->hba_flag |= HBA_ERATT_HANDLED;
9769 }
a257bf90
JS
9770
9771 /*
9772 * If there is deferred error attention, do not check for any
9773 * interrupt.
9774 */
9775 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 9776 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
9777 return IRQ_NONE;
9778 }
9779
9399627f 9780 /* Clear up only attention source related to slow-path */
9940b97b
JS
9781 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9782 goto unplug_error;
9783
a747c9ce
JS
9784 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9785 HC_LAINT_ENA | HC_ERINT_ENA),
9786 phba->HCregaddr);
9399627f
JS
9787 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9788 phba->HAregaddr);
a747c9ce 9789 writel(hc_copy, phba->HCregaddr);
9399627f 9790 readl(phba->HAregaddr); /* flush */
5b75da2f 9791 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
9792 } else
9793 ha_copy = phba->ha_copy;
dea3101e 9794
dea3101e 9795 work_ha_copy = ha_copy & phba->work_ha_mask;
9796
9399627f 9797 if (work_ha_copy) {
dea3101e 9798 if (work_ha_copy & HA_LATT) {
9799 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
9800 /*
9801 * Turn off Link Attention interrupts
9802 * until CLEAR_LA done
9803 */
5b75da2f 9804 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 9805 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
9806 if (lpfc_readl(phba->HCregaddr, &control))
9807 goto unplug_error;
dea3101e 9808 control &= ~HC_LAINT_ENA;
9809 writel(control, phba->HCregaddr);
9810 readl(phba->HCregaddr); /* flush */
5b75da2f 9811 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 9812 }
9813 else
9814 work_ha_copy &= ~HA_LATT;
9815 }
9816
9399627f 9817 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
9818 /*
9819 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
9820 * the only slow ring.
9821 */
9822 status = (work_ha_copy &
9823 (HA_RXMASK << (4*LPFC_ELS_RING)));
9824 status >>= (4*LPFC_ELS_RING);
9825 if (status & HA_RXMASK) {
5b75da2f 9826 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
9827 if (lpfc_readl(phba->HCregaddr, &control))
9828 goto unplug_error;
a58cbd52
JS
9829
9830 lpfc_debugfs_slow_ring_trc(phba,
9831 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
9832 control, status,
9833 (uint32_t)phba->sli.slistat.sli_intr);
9834
858c9f6c 9835 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
9836 lpfc_debugfs_slow_ring_trc(phba,
9837 "ISR Disable ring:"
9838 "pwork:x%x hawork:x%x wait:x%x",
9839 phba->work_ha, work_ha_copy,
9840 (uint32_t)((unsigned long)
5e9d9b82 9841 &phba->work_waitq));
a58cbd52 9842
858c9f6c
JS
9843 control &=
9844 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e 9845 writel(control, phba->HCregaddr);
9846 readl(phba->HCregaddr); /* flush */
dea3101e 9847 }
a58cbd52
JS
9848 else {
9849 lpfc_debugfs_slow_ring_trc(phba,
9850 "ISR slow ring: pwork:"
9851 "x%x hawork:x%x wait:x%x",
9852 phba->work_ha, work_ha_copy,
9853 (uint32_t)((unsigned long)
5e9d9b82 9854 &phba->work_waitq));
a58cbd52 9855 }
5b75da2f 9856 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 9857 }
9858 }
5b75da2f 9859 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 9860 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
9861 if (lpfc_sli_read_hs(phba))
9862 goto unplug_error;
a257bf90
JS
9863 /*
9864 * Check if there is a deferred error condition
9865 * is active
9866 */
9867 if ((HS_FFER1 & phba->work_hs) &&
9868 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
9869 HS_FFER6 | HS_FFER7 | HS_FFER8) &
9870 phba->work_hs)) {
a257bf90
JS
9871 phba->hba_flag |= DEFER_ERATT;
9872 /* Clear all interrupt enable conditions */
9873 writel(0, phba->HCregaddr);
9874 readl(phba->HCregaddr);
9875 }
9876 }
9877
9399627f 9878 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 9879 pmb = phba->sli.mbox_active;
04c68496 9880 pmbox = &pmb->u.mb;
34b02dcd 9881 mbox = phba->mbox;
858c9f6c 9882 vport = pmb->vport;
92d7f7b0
JS
9883
9884 /* First check out the status word */
9885 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
9886 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 9887 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
9888 /*
9889 * Stray Mailbox Interrupt, mbxCommand <cmd>
9890 * mbxStatus <status>
9891 */
09372820 9892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 9893 LOG_SLI,
e8b62011 9894 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
9895 "Interrupt mbxCommand x%x "
9896 "mbxStatus x%x\n",
e8b62011 9897 (vport ? vport->vpi : 0),
92d7f7b0
JS
9898 pmbox->mbxCommand,
9899 pmbox->mbxStatus);
09372820
JS
9900 /* clear mailbox attention bit */
9901 work_ha_copy &= ~HA_MBATT;
9902 } else {
97eab634 9903 phba->sli.mbox_active = NULL;
5b75da2f 9904 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
9905 phba->last_completion_time = jiffies;
9906 del_timer(&phba->sli.mbox_tmo);
09372820
JS
9907 if (pmb->mbox_cmpl) {
9908 lpfc_sli_pcimem_bcopy(mbox, pmbox,
9909 MAILBOX_CMD_SIZE);
7a470277
JS
9910 if (pmb->out_ext_byte_len &&
9911 pmb->context2)
9912 lpfc_sli_pcimem_bcopy(
9913 phba->mbox_ext,
9914 pmb->context2,
9915 pmb->out_ext_byte_len);
09372820
JS
9916 }
9917 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9918 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9919
9920 lpfc_debugfs_disc_trc(vport,
9921 LPFC_DISC_TRC_MBOX_VPORT,
9922 "MBOX dflt rpi: : "
9923 "status:x%x rpi:x%x",
9924 (uint32_t)pmbox->mbxStatus,
9925 pmbox->un.varWords[0], 0);
9926
9927 if (!pmbox->mbxStatus) {
9928 mp = (struct lpfc_dmabuf *)
9929 (pmb->context1);
9930 ndlp = (struct lpfc_nodelist *)
9931 pmb->context2;
9932
9933 /* Reg_LOGIN of dflt RPI was
9934 * successful. new lets get
9935 * rid of the RPI using the
9936 * same mbox buffer.
9937 */
9938 lpfc_unreg_login(phba,
9939 vport->vpi,
9940 pmbox->un.varWords[0],
9941 pmb);
9942 pmb->mbox_cmpl =
9943 lpfc_mbx_cmpl_dflt_rpi;
9944 pmb->context1 = mp;
9945 pmb->context2 = ndlp;
9946 pmb->vport = vport;
58da1ffb
JS
9947 rc = lpfc_sli_issue_mbox(phba,
9948 pmb,
9949 MBX_NOWAIT);
9950 if (rc != MBX_BUSY)
9951 lpfc_printf_log(phba,
9952 KERN_ERR,
9953 LOG_MBOX | LOG_SLI,
d7c255b2 9954 "0350 rc should have"
6a9c52cf 9955 "been MBX_BUSY\n");
3772a991
JS
9956 if (rc != MBX_NOT_FINISHED)
9957 goto send_current_mbox;
09372820 9958 }
858c9f6c 9959 }
5b75da2f
JS
9960 spin_lock_irqsave(
9961 &phba->pport->work_port_lock,
9962 iflag);
09372820
JS
9963 phba->pport->work_port_events &=
9964 ~WORKER_MBOX_TMO;
5b75da2f
JS
9965 spin_unlock_irqrestore(
9966 &phba->pport->work_port_lock,
9967 iflag);
09372820 9968 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 9969 }
97eab634 9970 } else
5b75da2f 9971 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 9972
92d7f7b0
JS
9973 if ((work_ha_copy & HA_MBATT) &&
9974 (phba->sli.mbox_active == NULL)) {
858c9f6c 9975send_current_mbox:
92d7f7b0 9976 /* Process next mailbox command if there is one */
58da1ffb
JS
9977 do {
9978 rc = lpfc_sli_issue_mbox(phba, NULL,
9979 MBX_NOWAIT);
9980 } while (rc == MBX_NOT_FINISHED);
9981 if (rc != MBX_SUCCESS)
9982 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9983 LOG_SLI, "0349 rc should be "
6a9c52cf 9984 "MBX_SUCCESS\n");
92d7f7b0
JS
9985 }
9986
5b75da2f 9987 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 9988 phba->work_ha |= work_ha_copy;
5b75da2f 9989 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 9990 lpfc_worker_wake_up(phba);
dea3101e 9991 }
9399627f 9992 return IRQ_HANDLED;
9940b97b
JS
9993unplug_error:
9994 spin_unlock_irqrestore(&phba->hbalock, iflag);
9995 return IRQ_HANDLED;
dea3101e 9996
3772a991 9997} /* lpfc_sli_sp_intr_handler */
9399627f
JS
9998
9999/**
3772a991 10000 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
10001 * @irq: Interrupt number.
10002 * @dev_id: The device context pointer.
10003 *
10004 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
10005 * service routine when device with SLI-3 interface spec is enabled with
10006 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10007 * ring event in the HBA. However, when the device is enabled with either
10008 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10009 * device-level interrupt handler. When the PCI slot is in error recovery
10010 * or the HBA is undergoing initialization, the interrupt handler will not
10011 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10012 * the intrrupt context. This function is called without any lock held.
10013 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
10014 *
10015 * This function returns IRQ_HANDLED when interrupt is handled else it
10016 * returns IRQ_NONE.
10017 **/
10018irqreturn_t
3772a991 10019lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
10020{
10021 struct lpfc_hba *phba;
10022 uint32_t ha_copy;
10023 unsigned long status;
5b75da2f 10024 unsigned long iflag;
9399627f
JS
10025
10026 /* Get the driver's phba structure from the dev_id and
10027 * assume the HBA is not interrupting.
10028 */
10029 phba = (struct lpfc_hba *) dev_id;
10030
10031 if (unlikely(!phba))
10032 return IRQ_NONE;
10033
10034 /*
10035 * Stuff needs to be attented to when this function is invoked as an
10036 * individual interrupt handler in MSI-X multi-message interrupt mode
10037 */
10038 if (phba->intr_type == MSIX) {
3772a991
JS
10039 /* Check device state for handling interrupt */
10040 if (lpfc_intr_state_check(phba))
9399627f
JS
10041 return IRQ_NONE;
10042 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
10043 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10044 return IRQ_HANDLED;
9399627f 10045 /* Clear up only attention source related to fast-path */
5b75da2f 10046 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
10047 /*
10048 * If there is deferred error attention, do not check for
10049 * any interrupt.
10050 */
10051 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 10052 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
10053 return IRQ_NONE;
10054 }
9399627f
JS
10055 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10056 phba->HAregaddr);
10057 readl(phba->HAregaddr); /* flush */
5b75da2f 10058 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
10059 } else
10060 ha_copy = phba->ha_copy;
dea3101e 10061
10062 /*
9399627f 10063 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 10064 */
9399627f
JS
10065 ha_copy &= ~(phba->work_ha_mask);
10066
10067 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 10068 status >>= (4*LPFC_FCP_RING);
858c9f6c 10069 if (status & HA_RXMASK)
dea3101e 10070 lpfc_sli_handle_fast_ring_event(phba,
10071 &phba->sli.ring[LPFC_FCP_RING],
10072 status);
a4bc3379
JS
10073
10074 if (phba->cfg_multi_ring_support == 2) {
10075 /*
9399627f
JS
10076 * Process all events on extra ring. Take the optimized path
10077 * for extra ring IO.
a4bc3379 10078 */
9399627f 10079 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 10080 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 10081 if (status & HA_RXMASK) {
a4bc3379
JS
10082 lpfc_sli_handle_fast_ring_event(phba,
10083 &phba->sli.ring[LPFC_EXTRA_RING],
10084 status);
10085 }
10086 }
dea3101e 10087 return IRQ_HANDLED;
3772a991 10088} /* lpfc_sli_fp_intr_handler */
9399627f
JS
10089
10090/**
3772a991 10091 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
10092 * @irq: Interrupt number.
10093 * @dev_id: The device context pointer.
10094 *
3772a991
JS
10095 * This function is the HBA device-level interrupt handler to device with
10096 * SLI-3 interface spec, called from the PCI layer when either MSI or
10097 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10098 * requires driver attention. This function invokes the slow-path interrupt
10099 * attention handling function and fast-path interrupt attention handling
10100 * function in turn to process the relevant HBA attention events. This
10101 * function is called without any lock held. It gets the hbalock to access
10102 * and update SLI data structures.
9399627f
JS
10103 *
10104 * This function returns IRQ_HANDLED when interrupt is handled, else it
10105 * returns IRQ_NONE.
10106 **/
10107irqreturn_t
3772a991 10108lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
10109{
10110 struct lpfc_hba *phba;
10111 irqreturn_t sp_irq_rc, fp_irq_rc;
10112 unsigned long status1, status2;
a747c9ce 10113 uint32_t hc_copy;
9399627f
JS
10114
10115 /*
10116 * Get the driver's phba structure from the dev_id and
10117 * assume the HBA is not interrupting.
10118 */
10119 phba = (struct lpfc_hba *) dev_id;
10120
10121 if (unlikely(!phba))
10122 return IRQ_NONE;
10123
3772a991
JS
10124 /* Check device state for handling interrupt */
10125 if (lpfc_intr_state_check(phba))
9399627f
JS
10126 return IRQ_NONE;
10127
10128 spin_lock(&phba->hbalock);
9940b97b
JS
10129 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10130 spin_unlock(&phba->hbalock);
10131 return IRQ_HANDLED;
10132 }
10133
9399627f
JS
10134 if (unlikely(!phba->ha_copy)) {
10135 spin_unlock(&phba->hbalock);
10136 return IRQ_NONE;
10137 } else if (phba->ha_copy & HA_ERATT) {
10138 if (phba->hba_flag & HBA_ERATT_HANDLED)
10139 /* ERATT polling has handled ERATT */
10140 phba->ha_copy &= ~HA_ERATT;
10141 else
10142 /* Indicate interrupt handler handles ERATT */
10143 phba->hba_flag |= HBA_ERATT_HANDLED;
10144 }
10145
a257bf90
JS
10146 /*
10147 * If there is deferred error attention, do not check for any interrupt.
10148 */
10149 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 10150 spin_unlock(&phba->hbalock);
a257bf90
JS
10151 return IRQ_NONE;
10152 }
10153
9399627f 10154 /* Clear attention sources except link and error attentions */
9940b97b
JS
10155 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10156 spin_unlock(&phba->hbalock);
10157 return IRQ_HANDLED;
10158 }
a747c9ce
JS
10159 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10160 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10161 phba->HCregaddr);
9399627f 10162 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 10163 writel(hc_copy, phba->HCregaddr);
9399627f
JS
10164 readl(phba->HAregaddr); /* flush */
10165 spin_unlock(&phba->hbalock);
10166
10167 /*
10168 * Invokes slow-path host attention interrupt handling as appropriate.
10169 */
10170
10171 /* status of events with mailbox and link attention */
10172 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10173
10174 /* status of events with ELS ring */
10175 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10176 status2 >>= (4*LPFC_ELS_RING);
10177
10178 if (status1 || (status2 & HA_RXMASK))
3772a991 10179 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
10180 else
10181 sp_irq_rc = IRQ_NONE;
10182
10183 /*
10184 * Invoke fast-path host attention interrupt handling as appropriate.
10185 */
10186
10187 /* status of events with FCP ring */
10188 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10189 status1 >>= (4*LPFC_FCP_RING);
10190
10191 /* status of events with extra ring */
10192 if (phba->cfg_multi_ring_support == 2) {
10193 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10194 status2 >>= (4*LPFC_EXTRA_RING);
10195 } else
10196 status2 = 0;
10197
10198 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 10199 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
10200 else
10201 fp_irq_rc = IRQ_NONE;
dea3101e 10202
9399627f
JS
10203 /* Return device-level interrupt handling status */
10204 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 10205} /* lpfc_sli_intr_handler */
4f774513
JS
10206
10207/**
10208 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10209 * @phba: pointer to lpfc hba data structure.
10210 *
10211 * This routine is invoked by the worker thread to process all the pending
10212 * SLI4 FCP abort XRI events.
10213 **/
10214void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10215{
10216 struct lpfc_cq_event *cq_event;
10217
10218 /* First, declare the fcp xri abort event has been handled */
10219 spin_lock_irq(&phba->hbalock);
10220 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10221 spin_unlock_irq(&phba->hbalock);
10222 /* Now, handle all the fcp xri abort events */
10223 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10224 /* Get the first event from the head of the event queue */
10225 spin_lock_irq(&phba->hbalock);
10226 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10227 cq_event, struct lpfc_cq_event, list);
10228 spin_unlock_irq(&phba->hbalock);
10229 /* Notify aborted XRI for FCP work queue */
10230 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10231 /* Free the event processed back to the free pool */
10232 lpfc_sli4_cq_event_release(phba, cq_event);
10233 }
10234}
10235
10236/**
10237 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10238 * @phba: pointer to lpfc hba data structure.
10239 *
10240 * This routine is invoked by the worker thread to process all the pending
10241 * SLI4 els abort xri events.
10242 **/
10243void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10244{
10245 struct lpfc_cq_event *cq_event;
10246
10247 /* First, declare the els xri abort event has been handled */
10248 spin_lock_irq(&phba->hbalock);
10249 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10250 spin_unlock_irq(&phba->hbalock);
10251 /* Now, handle all the els xri abort events */
10252 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10253 /* Get the first event from the head of the event queue */
10254 spin_lock_irq(&phba->hbalock);
10255 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10256 cq_event, struct lpfc_cq_event, list);
10257 spin_unlock_irq(&phba->hbalock);
10258 /* Notify aborted XRI for ELS work queue */
10259 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10260 /* Free the event processed back to the free pool */
10261 lpfc_sli4_cq_event_release(phba, cq_event);
10262 }
10263}
10264
341af102
JS
10265/**
10266 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10267 * @phba: pointer to lpfc hba data structure
10268 * @pIocbIn: pointer to the rspiocbq
10269 * @pIocbOut: pointer to the cmdiocbq
10270 * @wcqe: pointer to the complete wcqe
10271 *
10272 * This routine transfers the fields of a command iocbq to a response iocbq
10273 * by copying all the IOCB fields from command iocbq and transferring the
10274 * completion status information from the complete wcqe.
10275 **/
4f774513 10276static void
341af102
JS
10277lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10278 struct lpfc_iocbq *pIocbIn,
4f774513
JS
10279 struct lpfc_iocbq *pIocbOut,
10280 struct lpfc_wcqe_complete *wcqe)
10281{
341af102 10282 unsigned long iflags;
4f774513
JS
10283 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10284
10285 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10286 sizeof(struct lpfc_iocbq) - offset);
4f774513
JS
10287 /* Map WCQE parameters into irspiocb parameters */
10288 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10289 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10290 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10291 pIocbIn->iocb.un.fcpi.fcpi_parm =
10292 pIocbOut->iocb.un.fcpi.fcpi_parm -
10293 wcqe->total_data_placed;
10294 else
10295 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 10296 else {
4f774513 10297 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e
JS
10298 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10299 }
341af102
JS
10300
10301 /* Pick up HBA exchange busy condition */
10302 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10303 spin_lock_irqsave(&phba->hbalock, iflags);
10304 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10305 spin_unlock_irqrestore(&phba->hbalock, iflags);
10306 }
4f774513
JS
10307}
10308
45ed1190
JS
10309/**
10310 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10311 * @phba: Pointer to HBA context object.
10312 * @wcqe: Pointer to work-queue completion queue entry.
10313 *
10314 * This routine handles an ELS work-queue completion event and construct
10315 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10316 * discovery engine to handle.
10317 *
10318 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10319 **/
10320static struct lpfc_iocbq *
10321lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10322 struct lpfc_iocbq *irspiocbq)
10323{
10324 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10325 struct lpfc_iocbq *cmdiocbq;
10326 struct lpfc_wcqe_complete *wcqe;
10327 unsigned long iflags;
10328
10329 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10330 spin_lock_irqsave(&phba->hbalock, iflags);
10331 pring->stats.iocb_event++;
10332 /* Look up the ELS command IOCB and create pseudo response IOCB */
10333 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10334 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10335 spin_unlock_irqrestore(&phba->hbalock, iflags);
10336
10337 if (unlikely(!cmdiocbq)) {
10338 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10339 "0386 ELS complete with no corresponding "
10340 "cmdiocb: iotag (%d)\n",
10341 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10342 lpfc_sli_release_iocbq(phba, irspiocbq);
10343 return NULL;
10344 }
10345
10346 /* Fake the irspiocbq and copy necessary response information */
341af102 10347 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
10348
10349 return irspiocbq;
10350}
10351
04c68496
JS
10352/**
10353 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10354 * @phba: Pointer to HBA context object.
10355 * @cqe: Pointer to mailbox completion queue entry.
10356 *
10357 * This routine process a mailbox completion queue entry with asynchrous
10358 * event.
10359 *
10360 * Return: true if work posted to worker thread, otherwise false.
10361 **/
10362static bool
10363lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10364{
10365 struct lpfc_cq_event *cq_event;
10366 unsigned long iflags;
10367
10368 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10369 "0392 Async Event: word0:x%x, word1:x%x, "
10370 "word2:x%x, word3:x%x\n", mcqe->word0,
10371 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10372
10373 /* Allocate a new internal CQ_EVENT entry */
10374 cq_event = lpfc_sli4_cq_event_alloc(phba);
10375 if (!cq_event) {
10376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10377 "0394 Failed to allocate CQ_EVENT entry\n");
10378 return false;
10379 }
10380
10381 /* Move the CQE into an asynchronous event entry */
10382 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10383 spin_lock_irqsave(&phba->hbalock, iflags);
10384 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10385 /* Set the async event flag */
10386 phba->hba_flag |= ASYNC_EVENT;
10387 spin_unlock_irqrestore(&phba->hbalock, iflags);
10388
10389 return true;
10390}
10391
10392/**
10393 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10394 * @phba: Pointer to HBA context object.
10395 * @cqe: Pointer to mailbox completion queue entry.
10396 *
10397 * This routine process a mailbox completion queue entry with mailbox
10398 * completion event.
10399 *
10400 * Return: true if work posted to worker thread, otherwise false.
10401 **/
10402static bool
10403lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10404{
10405 uint32_t mcqe_status;
10406 MAILBOX_t *mbox, *pmbox;
10407 struct lpfc_mqe *mqe;
10408 struct lpfc_vport *vport;
10409 struct lpfc_nodelist *ndlp;
10410 struct lpfc_dmabuf *mp;
10411 unsigned long iflags;
10412 LPFC_MBOXQ_t *pmb;
10413 bool workposted = false;
10414 int rc;
10415
10416 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10417 if (!bf_get(lpfc_trailer_completed, mcqe))
10418 goto out_no_mqe_complete;
10419
10420 /* Get the reference to the active mbox command */
10421 spin_lock_irqsave(&phba->hbalock, iflags);
10422 pmb = phba->sli.mbox_active;
10423 if (unlikely(!pmb)) {
10424 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10425 "1832 No pending MBOX command to handle\n");
10426 spin_unlock_irqrestore(&phba->hbalock, iflags);
10427 goto out_no_mqe_complete;
10428 }
10429 spin_unlock_irqrestore(&phba->hbalock, iflags);
10430 mqe = &pmb->u.mqe;
10431 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10432 mbox = phba->mbox;
10433 vport = pmb->vport;
10434
10435 /* Reset heartbeat timer */
10436 phba->last_completion_time = jiffies;
10437 del_timer(&phba->sli.mbox_tmo);
10438
10439 /* Move mbox data to caller's mailbox region, do endian swapping */
10440 if (pmb->mbox_cmpl && mbox)
10441 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 10442
73d91e50
JS
10443 /*
10444 * For mcqe errors, conditionally move a modified error code to
10445 * the mbox so that the error will not be missed.
10446 */
10447 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10448 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10449 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10450 bf_set(lpfc_mqe_status, mqe,
10451 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10452 }
04c68496
JS
10453 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10454 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10455 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10456 "MBOX dflt rpi: status:x%x rpi:x%x",
10457 mcqe_status,
10458 pmbox->un.varWords[0], 0);
10459 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10460 mp = (struct lpfc_dmabuf *)(pmb->context1);
10461 ndlp = (struct lpfc_nodelist *)pmb->context2;
10462 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10463 * RID of the PPI using the same mbox buffer.
10464 */
10465 lpfc_unreg_login(phba, vport->vpi,
10466 pmbox->un.varWords[0], pmb);
10467 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10468 pmb->context1 = mp;
10469 pmb->context2 = ndlp;
10470 pmb->vport = vport;
10471 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10472 if (rc != MBX_BUSY)
10473 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10474 LOG_SLI, "0385 rc should "
10475 "have been MBX_BUSY\n");
10476 if (rc != MBX_NOT_FINISHED)
10477 goto send_current_mbox;
10478 }
10479 }
10480 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10481 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10482 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10483
10484 /* There is mailbox completion work to do */
10485 spin_lock_irqsave(&phba->hbalock, iflags);
10486 __lpfc_mbox_cmpl_put(phba, pmb);
10487 phba->work_ha |= HA_MBATT;
10488 spin_unlock_irqrestore(&phba->hbalock, iflags);
10489 workposted = true;
10490
10491send_current_mbox:
10492 spin_lock_irqsave(&phba->hbalock, iflags);
10493 /* Release the mailbox command posting token */
10494 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10495 /* Setting active mailbox pointer need to be in sync to flag clear */
10496 phba->sli.mbox_active = NULL;
10497 spin_unlock_irqrestore(&phba->hbalock, iflags);
10498 /* Wake up worker thread to post the next pending mailbox command */
10499 lpfc_worker_wake_up(phba);
10500out_no_mqe_complete:
10501 if (bf_get(lpfc_trailer_consumed, mcqe))
10502 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10503 return workposted;
10504}
10505
10506/**
10507 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10508 * @phba: Pointer to HBA context object.
10509 * @cqe: Pointer to mailbox completion queue entry.
10510 *
10511 * This routine process a mailbox completion queue entry, it invokes the
10512 * proper mailbox complete handling or asynchrous event handling routine
10513 * according to the MCQE's async bit.
10514 *
10515 * Return: true if work posted to worker thread, otherwise false.
10516 **/
10517static bool
10518lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10519{
10520 struct lpfc_mcqe mcqe;
10521 bool workposted;
10522
10523 /* Copy the mailbox MCQE and convert endian order as needed */
10524 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10525
10526 /* Invoke the proper event handling routine */
10527 if (!bf_get(lpfc_trailer_async, &mcqe))
10528 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10529 else
10530 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10531 return workposted;
10532}
10533
4f774513
JS
10534/**
10535 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10536 * @phba: Pointer to HBA context object.
10537 * @wcqe: Pointer to work-queue completion queue entry.
10538 *
10539 * This routine handles an ELS work-queue completion event.
10540 *
10541 * Return: true if work posted to worker thread, otherwise false.
10542 **/
10543static bool
10544lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10545 struct lpfc_wcqe_complete *wcqe)
10546{
4f774513
JS
10547 struct lpfc_iocbq *irspiocbq;
10548 unsigned long iflags;
2a9bf3d0 10549 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
4f774513 10550
45ed1190 10551 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
10552 irspiocbq = lpfc_sli_get_iocbq(phba);
10553 if (!irspiocbq) {
10554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
10555 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10556 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10557 pring->txq_cnt, phba->iocb_cnt,
10558 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10559 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
45ed1190 10560 return false;
4f774513 10561 }
4f774513 10562
45ed1190
JS
10563 /* Save off the slow-path queue event for work thread to process */
10564 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 10565 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 10566 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
10567 &phba->sli4_hba.sp_queue_event);
10568 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 10569 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 10570
45ed1190 10571 return true;
4f774513
JS
10572}
10573
10574/**
10575 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10576 * @phba: Pointer to HBA context object.
10577 * @wcqe: Pointer to work-queue completion queue entry.
10578 *
10579 * This routine handles slow-path WQ entry comsumed event by invoking the
10580 * proper WQ release routine to the slow-path WQ.
10581 **/
10582static void
10583lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10584 struct lpfc_wcqe_release *wcqe)
10585{
10586 /* Check for the slow-path ELS work queue */
10587 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10588 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10589 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10590 else
10591 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10592 "2579 Slow-path wqe consume event carries "
10593 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10594 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10595 phba->sli4_hba.els_wq->queue_id);
10596}
10597
10598/**
10599 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10600 * @phba: Pointer to HBA context object.
10601 * @cq: Pointer to a WQ completion queue.
10602 * @wcqe: Pointer to work-queue completion queue entry.
10603 *
10604 * This routine handles an XRI abort event.
10605 *
10606 * Return: true if work posted to worker thread, otherwise false.
10607 **/
10608static bool
10609lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10610 struct lpfc_queue *cq,
10611 struct sli4_wcqe_xri_aborted *wcqe)
10612{
10613 bool workposted = false;
10614 struct lpfc_cq_event *cq_event;
10615 unsigned long iflags;
10616
10617 /* Allocate a new internal CQ_EVENT entry */
10618 cq_event = lpfc_sli4_cq_event_alloc(phba);
10619 if (!cq_event) {
10620 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10621 "0602 Failed to allocate CQ_EVENT entry\n");
10622 return false;
10623 }
10624
10625 /* Move the CQE into the proper xri abort event list */
10626 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10627 switch (cq->subtype) {
10628 case LPFC_FCP:
10629 spin_lock_irqsave(&phba->hbalock, iflags);
10630 list_add_tail(&cq_event->list,
10631 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10632 /* Set the fcp xri abort event flag */
10633 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10634 spin_unlock_irqrestore(&phba->hbalock, iflags);
10635 workposted = true;
10636 break;
10637 case LPFC_ELS:
10638 spin_lock_irqsave(&phba->hbalock, iflags);
10639 list_add_tail(&cq_event->list,
10640 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10641 /* Set the els xri abort event flag */
10642 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10643 spin_unlock_irqrestore(&phba->hbalock, iflags);
10644 workposted = true;
10645 break;
10646 default:
10647 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10648 "0603 Invalid work queue CQE subtype (x%x)\n",
10649 cq->subtype);
10650 workposted = false;
10651 break;
10652 }
10653 return workposted;
10654}
10655
4f774513
JS
10656/**
10657 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
10658 * @phba: Pointer to HBA context object.
10659 * @rcqe: Pointer to receive-queue completion queue entry.
10660 *
10661 * This routine process a receive-queue completion queue entry.
10662 *
10663 * Return: true if work posted to worker thread, otherwise false.
10664 **/
10665static bool
4d9ab994 10666lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 10667{
4f774513
JS
10668 bool workposted = false;
10669 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10670 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10671 struct hbq_dmabuf *dma_buf;
7851fe2c 10672 uint32_t status, rq_id;
4f774513
JS
10673 unsigned long iflags;
10674
7851fe2c
JS
10675 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
10676 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
10677 else
10678 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
10679 if (rq_id != hrq->queue_id)
4f774513
JS
10680 goto out;
10681
4d9ab994 10682 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
10683 switch (status) {
10684 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10685 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10686 "2537 Receive Frame Truncated!!\n");
10687 case FC_STATUS_RQ_SUCCESS:
5ffc266e 10688 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
10689 spin_lock_irqsave(&phba->hbalock, iflags);
10690 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10691 if (!dma_buf) {
10692 spin_unlock_irqrestore(&phba->hbalock, iflags);
10693 goto out;
10694 }
4d9ab994 10695 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 10696 /* save off the frame for the word thread to process */
4d9ab994 10697 list_add_tail(&dma_buf->cq_event.list,
45ed1190 10698 &phba->sli4_hba.sp_queue_event);
4f774513 10699 /* Frame received */
45ed1190 10700 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
10701 spin_unlock_irqrestore(&phba->hbalock, iflags);
10702 workposted = true;
10703 break;
10704 case FC_STATUS_INSUFF_BUF_NEED_BUF:
10705 case FC_STATUS_INSUFF_BUF_FRM_DISC:
10706 /* Post more buffers if possible */
10707 spin_lock_irqsave(&phba->hbalock, iflags);
10708 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10709 spin_unlock_irqrestore(&phba->hbalock, iflags);
10710 workposted = true;
10711 break;
10712 }
10713out:
10714 return workposted;
4f774513
JS
10715}
10716
4d9ab994
JS
10717/**
10718 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10719 * @phba: Pointer to HBA context object.
10720 * @cq: Pointer to the completion queue.
10721 * @wcqe: Pointer to a completion queue entry.
10722 *
25985edc 10723 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
10724 * entry.
10725 *
10726 * Return: true if work posted to worker thread, otherwise false.
10727 **/
10728static bool
10729lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10730 struct lpfc_cqe *cqe)
10731{
45ed1190 10732 struct lpfc_cqe cqevt;
4d9ab994
JS
10733 bool workposted = false;
10734
10735 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 10736 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
10737
10738 /* Check and process for different type of WCQE and dispatch */
45ed1190 10739 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 10740 case CQE_CODE_COMPL_WQE:
45ed1190 10741 /* Process the WQ/RQ complete event */
bc73905a 10742 phba->last_completion_time = jiffies;
4d9ab994 10743 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
45ed1190 10744 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
10745 break;
10746 case CQE_CODE_RELEASE_WQE:
10747 /* Process the WQ release event */
10748 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 10749 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
10750 break;
10751 case CQE_CODE_XRI_ABORTED:
10752 /* Process the WQ XRI abort event */
bc73905a 10753 phba->last_completion_time = jiffies;
4d9ab994 10754 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 10755 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
10756 break;
10757 case CQE_CODE_RECEIVE:
7851fe2c 10758 case CQE_CODE_RECEIVE_V1:
4d9ab994 10759 /* Process the RQ event */
bc73905a 10760 phba->last_completion_time = jiffies;
4d9ab994 10761 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 10762 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
10763 break;
10764 default:
10765 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10766 "0388 Not a valid WCQE code: x%x\n",
45ed1190 10767 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
10768 break;
10769 }
10770 return workposted;
10771}
10772
4f774513
JS
10773/**
10774 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10775 * @phba: Pointer to HBA context object.
10776 * @eqe: Pointer to fast-path event queue entry.
10777 *
10778 * This routine process a event queue entry from the slow-path event queue.
10779 * It will check the MajorCode and MinorCode to determine this is for a
10780 * completion event on a completion queue, if not, an error shall be logged
10781 * and just return. Otherwise, it will get to the corresponding completion
10782 * queue and process all the entries on that completion queue, rearm the
10783 * completion queue, and then return.
10784 *
10785 **/
10786static void
10787lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10788{
10789 struct lpfc_queue *cq = NULL, *childq, *speq;
10790 struct lpfc_cqe *cqe;
10791 bool workposted = false;
10792 int ecount = 0;
10793 uint16_t cqid;
10794
cb5172ea 10795 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
4f774513
JS
10796 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10797 "0359 Not a valid slow-path completion "
10798 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
10799 bf_get_le32(lpfc_eqe_major_code, eqe),
10800 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
10801 return;
10802 }
10803
10804 /* Get the reference to the corresponding CQ */
cb5172ea 10805 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
10806
10807 /* Search for completion queue pointer matching this cqid */
10808 speq = phba->sli4_hba.sp_eq;
10809 list_for_each_entry(childq, &speq->child_list, list) {
10810 if (childq->queue_id == cqid) {
10811 cq = childq;
10812 break;
10813 }
10814 }
10815 if (unlikely(!cq)) {
75baf696
JS
10816 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10818 "0365 Slow-path CQ identifier "
10819 "(%d) does not exist\n", cqid);
4f774513
JS
10820 return;
10821 }
10822
10823 /* Process all the entries to the CQ */
10824 switch (cq->type) {
10825 case LPFC_MCQ:
10826 while ((cqe = lpfc_sli4_cq_get(cq))) {
10827 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
73d91e50 10828 if (!(++ecount % cq->entry_repost))
4f774513
JS
10829 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10830 }
10831 break;
10832 case LPFC_WCQ:
10833 while ((cqe = lpfc_sli4_cq_get(cq))) {
0558056c
JS
10834 if (cq->subtype == LPFC_FCP)
10835 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
10836 cqe);
10837 else
10838 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
10839 cqe);
73d91e50 10840 if (!(++ecount % cq->entry_repost))
4f774513
JS
10841 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10842 }
10843 break;
10844 default:
10845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10846 "0370 Invalid completion queue type (%d)\n",
10847 cq->type);
10848 return;
10849 }
10850
10851 /* Catch the no cq entry condition, log an error */
10852 if (unlikely(ecount == 0))
10853 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10854 "0371 No entry from the CQ: identifier "
10855 "(x%x), type (%d)\n", cq->queue_id, cq->type);
10856
10857 /* In any case, flash and re-arm the RCQ */
10858 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10859
10860 /* wake up worker thread if there are works to be done */
10861 if (workposted)
10862 lpfc_worker_wake_up(phba);
10863}
10864
10865/**
10866 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
10867 * @eqe: Pointer to fast-path completion queue entry.
10868 *
10869 * This routine process a fast-path work queue completion entry from fast-path
10870 * event queue for FCP command response completion.
10871 **/
10872static void
10873lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
10874 struct lpfc_wcqe_complete *wcqe)
10875{
10876 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10877 struct lpfc_iocbq *cmdiocbq;
10878 struct lpfc_iocbq irspiocbq;
10879 unsigned long iflags;
10880
10881 spin_lock_irqsave(&phba->hbalock, iflags);
10882 pring->stats.iocb_event++;
10883 spin_unlock_irqrestore(&phba->hbalock, iflags);
10884
10885 /* Check for response status */
10886 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
10887 /* If resource errors reported from HBA, reduce queue
10888 * depth of the SCSI device.
10889 */
10890 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
10891 IOSTAT_LOCAL_REJECT) &&
10892 (wcqe->parameter == IOERR_NO_RESOURCES)) {
10893 phba->lpfc_rampdown_queue_depth(phba);
10894 }
10895 /* Log the error status */
10896 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10897 "0373 FCP complete error: status=x%x, "
10898 "hw_status=x%x, total_data_specified=%d, "
10899 "parameter=x%x, word3=x%x\n",
10900 bf_get(lpfc_wcqe_c_status, wcqe),
10901 bf_get(lpfc_wcqe_c_hw_status, wcqe),
10902 wcqe->total_data_placed, wcqe->parameter,
10903 wcqe->word3);
10904 }
10905
10906 /* Look up the FCP command IOCB and create pseudo response IOCB */
10907 spin_lock_irqsave(&phba->hbalock, iflags);
10908 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10909 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10910 spin_unlock_irqrestore(&phba->hbalock, iflags);
10911 if (unlikely(!cmdiocbq)) {
10912 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10913 "0374 FCP complete with no corresponding "
10914 "cmdiocb: iotag (%d)\n",
10915 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10916 return;
10917 }
10918 if (unlikely(!cmdiocbq->iocb_cmpl)) {
10919 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10920 "0375 FCP cmdiocb not callback function "
10921 "iotag: (%d)\n",
10922 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10923 return;
10924 }
10925
10926 /* Fake the irspiocb and copy necessary response information */
341af102 10927 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 10928
0f65ff68
JS
10929 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
10930 spin_lock_irqsave(&phba->hbalock, iflags);
10931 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
10932 spin_unlock_irqrestore(&phba->hbalock, iflags);
10933 }
10934
4f774513
JS
10935 /* Pass the cmd_iocb and the rsp state to the upper layer */
10936 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
10937}
10938
10939/**
10940 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
10941 * @phba: Pointer to HBA context object.
10942 * @cq: Pointer to completion queue.
10943 * @wcqe: Pointer to work-queue completion queue entry.
10944 *
10945 * This routine handles an fast-path WQ entry comsumed event by invoking the
10946 * proper WQ release routine to the slow-path WQ.
10947 **/
10948static void
10949lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10950 struct lpfc_wcqe_release *wcqe)
10951{
10952 struct lpfc_queue *childwq;
10953 bool wqid_matched = false;
10954 uint16_t fcp_wqid;
10955
10956 /* Check for fast-path FCP work queue release */
10957 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
10958 list_for_each_entry(childwq, &cq->child_list, list) {
10959 if (childwq->queue_id == fcp_wqid) {
10960 lpfc_sli4_wq_release(childwq,
10961 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10962 wqid_matched = true;
10963 break;
10964 }
10965 }
10966 /* Report warning log message if no match found */
10967 if (wqid_matched != true)
10968 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10969 "2580 Fast-path wqe consume event carries "
10970 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
10971}
10972
10973/**
10974 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
10975 * @cq: Pointer to the completion queue.
10976 * @eqe: Pointer to fast-path completion queue entry.
10977 *
10978 * This routine process a fast-path work queue completion entry from fast-path
10979 * event queue for FCP command response completion.
10980 **/
10981static int
10982lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10983 struct lpfc_cqe *cqe)
10984{
10985 struct lpfc_wcqe_release wcqe;
10986 bool workposted = false;
10987
10988 /* Copy the work queue CQE and convert endian order if needed */
10989 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
10990
10991 /* Check and process for different type of WCQE and dispatch */
10992 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
10993 case CQE_CODE_COMPL_WQE:
10994 /* Process the WQ complete event */
98fc5dd9 10995 phba->last_completion_time = jiffies;
4f774513
JS
10996 lpfc_sli4_fp_handle_fcp_wcqe(phba,
10997 (struct lpfc_wcqe_complete *)&wcqe);
10998 break;
10999 case CQE_CODE_RELEASE_WQE:
11000 /* Process the WQ release event */
11001 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11002 (struct lpfc_wcqe_release *)&wcqe);
11003 break;
11004 case CQE_CODE_XRI_ABORTED:
11005 /* Process the WQ XRI abort event */
bc73905a 11006 phba->last_completion_time = jiffies;
4f774513
JS
11007 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11008 (struct sli4_wcqe_xri_aborted *)&wcqe);
11009 break;
11010 default:
11011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11012 "0144 Not a valid WCQE code: x%x\n",
11013 bf_get(lpfc_wcqe_c_code, &wcqe));
11014 break;
11015 }
11016 return workposted;
11017}
11018
11019/**
11020 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11021 * @phba: Pointer to HBA context object.
11022 * @eqe: Pointer to fast-path event queue entry.
11023 *
11024 * This routine process a event queue entry from the fast-path event queue.
11025 * It will check the MajorCode and MinorCode to determine this is for a
11026 * completion event on a completion queue, if not, an error shall be logged
11027 * and just return. Otherwise, it will get to the corresponding completion
11028 * queue and process all the entries on the completion queue, rearm the
11029 * completion queue, and then return.
11030 **/
11031static void
11032lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11033 uint32_t fcp_cqidx)
11034{
11035 struct lpfc_queue *cq;
11036 struct lpfc_cqe *cqe;
11037 bool workposted = false;
11038 uint16_t cqid;
11039 int ecount = 0;
11040
cb5172ea 11041 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513
JS
11042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11043 "0366 Not a valid fast-path completion "
11044 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
11045 bf_get_le32(lpfc_eqe_major_code, eqe),
11046 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
11047 return;
11048 }
11049
11050 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11051 if (unlikely(!cq)) {
75baf696
JS
11052 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11054 "0367 Fast-path completion queue "
11055 "does not exist\n");
4f774513
JS
11056 return;
11057 }
11058
11059 /* Get the reference to the corresponding CQ */
cb5172ea 11060 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
11061 if (unlikely(cqid != cq->queue_id)) {
11062 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11063 "0368 Miss-matched fast-path completion "
11064 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11065 cqid, cq->queue_id);
11066 return;
11067 }
11068
11069 /* Process all the entries to the CQ */
11070 while ((cqe = lpfc_sli4_cq_get(cq))) {
11071 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
73d91e50 11072 if (!(++ecount % cq->entry_repost))
4f774513
JS
11073 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11074 }
11075
11076 /* Catch the no cq entry condition */
11077 if (unlikely(ecount == 0))
11078 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11079 "0369 No entry from fast-path completion "
11080 "queue fcpcqid=%d\n", cq->queue_id);
11081
11082 /* In any case, flash and re-arm the CQ */
11083 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11084
11085 /* wake up worker thread if there are works to be done */
11086 if (workposted)
11087 lpfc_worker_wake_up(phba);
11088}
11089
11090static void
11091lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11092{
11093 struct lpfc_eqe *eqe;
11094
11095 /* walk all the EQ entries and drop on the floor */
11096 while ((eqe = lpfc_sli4_eq_get(eq)))
11097 ;
11098
11099 /* Clear and re-arm the EQ */
11100 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11101}
11102
11103/**
11104 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11105 * @irq: Interrupt number.
11106 * @dev_id: The device context pointer.
11107 *
11108 * This function is directly called from the PCI layer as an interrupt
11109 * service routine when device with SLI-4 interface spec is enabled with
11110 * MSI-X multi-message interrupt mode and there are slow-path events in
11111 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11112 * interrupt mode, this function is called as part of the device-level
11113 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11114 * undergoing initialization, the interrupt handler will not process the
11115 * interrupt. The link attention and ELS ring attention events are handled
11116 * by the worker thread. The interrupt handler signals the worker thread
11117 * and returns for these events. This function is called without any lock
11118 * held. It gets the hbalock to access and update SLI data structures.
11119 *
11120 * This function returns IRQ_HANDLED when interrupt is handled else it
11121 * returns IRQ_NONE.
11122 **/
11123irqreturn_t
11124lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11125{
11126 struct lpfc_hba *phba;
11127 struct lpfc_queue *speq;
11128 struct lpfc_eqe *eqe;
11129 unsigned long iflag;
11130 int ecount = 0;
11131
11132 /*
11133 * Get the driver's phba structure from the dev_id
11134 */
11135 phba = (struct lpfc_hba *)dev_id;
11136
11137 if (unlikely(!phba))
11138 return IRQ_NONE;
11139
11140 /* Get to the EQ struct associated with this vector */
11141 speq = phba->sli4_hba.sp_eq;
11142
11143 /* Check device state for handling interrupt */
11144 if (unlikely(lpfc_intr_state_check(phba))) {
11145 /* Check again for link_state with lock held */
11146 spin_lock_irqsave(&phba->hbalock, iflag);
11147 if (phba->link_state < LPFC_LINK_DOWN)
11148 /* Flush, clear interrupt, and rearm the EQ */
11149 lpfc_sli4_eq_flush(phba, speq);
11150 spin_unlock_irqrestore(&phba->hbalock, iflag);
11151 return IRQ_NONE;
11152 }
11153
11154 /*
11155 * Process all the event on FCP slow-path EQ
11156 */
11157 while ((eqe = lpfc_sli4_eq_get(speq))) {
11158 lpfc_sli4_sp_handle_eqe(phba, eqe);
73d91e50 11159 if (!(++ecount % speq->entry_repost))
4f774513
JS
11160 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11161 }
11162
11163 /* Always clear and re-arm the slow-path EQ */
11164 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11165
11166 /* Catch the no cq entry condition */
11167 if (unlikely(ecount == 0)) {
11168 if (phba->intr_type == MSIX)
11169 /* MSI-X treated interrupt served as no EQ share INT */
11170 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11171 "0357 MSI-X interrupt with no EQE\n");
11172 else
11173 /* Non MSI-X treated on interrupt as EQ share INT */
11174 return IRQ_NONE;
11175 }
11176
11177 return IRQ_HANDLED;
11178} /* lpfc_sli4_sp_intr_handler */
11179
11180/**
11181 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11182 * @irq: Interrupt number.
11183 * @dev_id: The device context pointer.
11184 *
11185 * This function is directly called from the PCI layer as an interrupt
11186 * service routine when device with SLI-4 interface spec is enabled with
11187 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11188 * ring event in the HBA. However, when the device is enabled with either
11189 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11190 * device-level interrupt handler. When the PCI slot is in error recovery
11191 * or the HBA is undergoing initialization, the interrupt handler will not
11192 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11193 * the intrrupt context. This function is called without any lock held.
11194 * It gets the hbalock to access and update SLI data structures. Note that,
11195 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11196 * equal to that of FCP CQ index.
11197 *
11198 * This function returns IRQ_HANDLED when interrupt is handled else it
11199 * returns IRQ_NONE.
11200 **/
11201irqreturn_t
11202lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11203{
11204 struct lpfc_hba *phba;
11205 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11206 struct lpfc_queue *fpeq;
11207 struct lpfc_eqe *eqe;
11208 unsigned long iflag;
11209 int ecount = 0;
11210 uint32_t fcp_eqidx;
11211
11212 /* Get the driver's phba structure from the dev_id */
11213 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11214 phba = fcp_eq_hdl->phba;
11215 fcp_eqidx = fcp_eq_hdl->idx;
11216
11217 if (unlikely(!phba))
11218 return IRQ_NONE;
11219
11220 /* Get to the EQ struct associated with this vector */
11221 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11222
11223 /* Check device state for handling interrupt */
11224 if (unlikely(lpfc_intr_state_check(phba))) {
11225 /* Check again for link_state with lock held */
11226 spin_lock_irqsave(&phba->hbalock, iflag);
11227 if (phba->link_state < LPFC_LINK_DOWN)
11228 /* Flush, clear interrupt, and rearm the EQ */
11229 lpfc_sli4_eq_flush(phba, fpeq);
11230 spin_unlock_irqrestore(&phba->hbalock, iflag);
11231 return IRQ_NONE;
11232 }
11233
11234 /*
11235 * Process all the event on FCP fast-path EQ
11236 */
11237 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11238 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
73d91e50 11239 if (!(++ecount % fpeq->entry_repost))
4f774513
JS
11240 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11241 }
11242
11243 /* Always clear and re-arm the fast-path EQ */
11244 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11245
11246 if (unlikely(ecount == 0)) {
11247 if (phba->intr_type == MSIX)
11248 /* MSI-X treated interrupt served as no EQ share INT */
11249 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11250 "0358 MSI-X interrupt with no EQE\n");
11251 else
11252 /* Non MSI-X treated on interrupt as EQ share INT */
11253 return IRQ_NONE;
11254 }
11255
11256 return IRQ_HANDLED;
11257} /* lpfc_sli4_fp_intr_handler */
11258
11259/**
11260 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11261 * @irq: Interrupt number.
11262 * @dev_id: The device context pointer.
11263 *
11264 * This function is the device-level interrupt handler to device with SLI-4
11265 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11266 * interrupt mode is enabled and there is an event in the HBA which requires
11267 * driver attention. This function invokes the slow-path interrupt attention
11268 * handling function and fast-path interrupt attention handling function in
11269 * turn to process the relevant HBA attention events. This function is called
11270 * without any lock held. It gets the hbalock to access and update SLI data
11271 * structures.
11272 *
11273 * This function returns IRQ_HANDLED when interrupt is handled, else it
11274 * returns IRQ_NONE.
11275 **/
11276irqreturn_t
11277lpfc_sli4_intr_handler(int irq, void *dev_id)
11278{
11279 struct lpfc_hba *phba;
11280 irqreturn_t sp_irq_rc, fp_irq_rc;
11281 bool fp_handled = false;
11282 uint32_t fcp_eqidx;
11283
11284 /* Get the driver's phba structure from the dev_id */
11285 phba = (struct lpfc_hba *)dev_id;
11286
11287 if (unlikely(!phba))
11288 return IRQ_NONE;
11289
11290 /*
11291 * Invokes slow-path host attention interrupt handling as appropriate.
11292 */
11293 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11294
11295 /*
11296 * Invoke fast-path host attention interrupt handling as appropriate.
11297 */
11298 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11299 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11300 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11301 if (fp_irq_rc == IRQ_HANDLED)
11302 fp_handled |= true;
11303 }
11304
11305 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11306} /* lpfc_sli4_intr_handler */
11307
11308/**
11309 * lpfc_sli4_queue_free - free a queue structure and associated memory
11310 * @queue: The queue structure to free.
11311 *
b595076a 11312 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
11313 * the host resident queue. This function must be called after destroying the
11314 * queue on the HBA.
11315 **/
11316void
11317lpfc_sli4_queue_free(struct lpfc_queue *queue)
11318{
11319 struct lpfc_dmabuf *dmabuf;
11320
11321 if (!queue)
11322 return;
11323
11324 while (!list_empty(&queue->page_list)) {
11325 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11326 list);
49198b37 11327 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
11328 dmabuf->virt, dmabuf->phys);
11329 kfree(dmabuf);
11330 }
11331 kfree(queue);
11332 return;
11333}
11334
11335/**
11336 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11337 * @phba: The HBA that this queue is being created on.
11338 * @entry_size: The size of each queue entry for this queue.
11339 * @entry count: The number of entries that this queue will handle.
11340 *
11341 * This function allocates a queue structure and the DMAable memory used for
11342 * the host resident queue. This function must be called before creating the
11343 * queue on the HBA.
11344 **/
11345struct lpfc_queue *
11346lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11347 uint32_t entry_count)
11348{
11349 struct lpfc_queue *queue;
11350 struct lpfc_dmabuf *dmabuf;
11351 int x, total_qe_count;
11352 void *dma_pointer;
cb5172ea 11353 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 11354
cb5172ea
JS
11355 if (!phba->sli4_hba.pc_sli4_params.supported)
11356 hw_page_size = SLI4_PAGE_SIZE;
11357
4f774513
JS
11358 queue = kzalloc(sizeof(struct lpfc_queue) +
11359 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11360 if (!queue)
11361 return NULL;
cb5172ea
JS
11362 queue->page_count = (ALIGN(entry_size * entry_count,
11363 hw_page_size))/hw_page_size;
4f774513
JS
11364 INIT_LIST_HEAD(&queue->list);
11365 INIT_LIST_HEAD(&queue->page_list);
11366 INIT_LIST_HEAD(&queue->child_list);
11367 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11368 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11369 if (!dmabuf)
11370 goto out_fail;
11371 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
cb5172ea 11372 hw_page_size, &dmabuf->phys,
4f774513
JS
11373 GFP_KERNEL);
11374 if (!dmabuf->virt) {
11375 kfree(dmabuf);
11376 goto out_fail;
11377 }
cb5172ea 11378 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
11379 dmabuf->buffer_tag = x;
11380 list_add_tail(&dmabuf->list, &queue->page_list);
11381 /* initialize queue's entry array */
11382 dma_pointer = dmabuf->virt;
11383 for (; total_qe_count < entry_count &&
cb5172ea 11384 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
11385 total_qe_count++, dma_pointer += entry_size) {
11386 queue->qe[total_qe_count].address = dma_pointer;
11387 }
11388 }
11389 queue->entry_size = entry_size;
11390 queue->entry_count = entry_count;
73d91e50
JS
11391
11392 /*
11393 * entry_repost is calculated based on the number of entries in the
11394 * queue. This works out except for RQs. If buffers are NOT initially
11395 * posted for every RQE, entry_repost should be adjusted accordingly.
11396 */
11397 queue->entry_repost = (entry_count >> 3);
11398 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11399 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
4f774513
JS
11400 queue->phba = phba;
11401
11402 return queue;
11403out_fail:
11404 lpfc_sli4_queue_free(queue);
11405 return NULL;
11406}
11407
11408/**
11409 * lpfc_eq_create - Create an Event Queue on the HBA
11410 * @phba: HBA structure that indicates port to create a queue on.
11411 * @eq: The queue structure to use to create the event queue.
11412 * @imax: The maximum interrupt per second limit.
11413 *
11414 * This function creates an event queue, as detailed in @eq, on a port,
11415 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11416 *
11417 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11418 * is used to get the entry count and entry size that are necessary to
11419 * determine the number of pages to allocate and use for this queue. This
11420 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11421 * event queue. This function is asynchronous and will wait for the mailbox
11422 * command to finish before continuing.
11423 *
11424 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
11425 * memory this function will return -ENOMEM. If the queue create mailbox command
11426 * fails this function will return -ENXIO.
4f774513
JS
11427 **/
11428uint32_t
11429lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11430{
11431 struct lpfc_mbx_eq_create *eq_create;
11432 LPFC_MBOXQ_t *mbox;
11433 int rc, length, status = 0;
11434 struct lpfc_dmabuf *dmabuf;
11435 uint32_t shdr_status, shdr_add_status;
11436 union lpfc_sli4_cfg_shdr *shdr;
11437 uint16_t dmult;
49198b37
JS
11438 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11439
11440 if (!phba->sli4_hba.pc_sli4_params.supported)
11441 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
11442
11443 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11444 if (!mbox)
11445 return -ENOMEM;
11446 length = (sizeof(struct lpfc_mbx_eq_create) -
11447 sizeof(struct lpfc_sli4_cfg_mhdr));
11448 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11449 LPFC_MBOX_OPCODE_EQ_CREATE,
11450 length, LPFC_SLI4_MBX_EMBED);
11451 eq_create = &mbox->u.mqe.un.eq_create;
11452 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11453 eq->page_count);
11454 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11455 LPFC_EQE_SIZE);
11456 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11457 /* Calculate delay multiper from maximum interrupt per second */
11458 dmult = LPFC_DMULT_CONST/imax - 1;
11459 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11460 dmult);
11461 switch (eq->entry_count) {
11462 default:
11463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11464 "0360 Unsupported EQ count. (%d)\n",
11465 eq->entry_count);
11466 if (eq->entry_count < 256)
11467 return -EINVAL;
11468 /* otherwise default to smallest count (drop through) */
11469 case 256:
11470 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11471 LPFC_EQ_CNT_256);
11472 break;
11473 case 512:
11474 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11475 LPFC_EQ_CNT_512);
11476 break;
11477 case 1024:
11478 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11479 LPFC_EQ_CNT_1024);
11480 break;
11481 case 2048:
11482 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11483 LPFC_EQ_CNT_2048);
11484 break;
11485 case 4096:
11486 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11487 LPFC_EQ_CNT_4096);
11488 break;
11489 }
11490 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 11491 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
11492 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11493 putPaddrLow(dmabuf->phys);
11494 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11495 putPaddrHigh(dmabuf->phys);
11496 }
11497 mbox->vport = phba->pport;
11498 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11499 mbox->context1 = NULL;
11500 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11501 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11502 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11503 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11504 if (shdr_status || shdr_add_status || rc) {
11505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11506 "2500 EQ_CREATE mailbox failed with "
11507 "status x%x add_status x%x, mbx status x%x\n",
11508 shdr_status, shdr_add_status, rc);
11509 status = -ENXIO;
11510 }
11511 eq->type = LPFC_EQ;
11512 eq->subtype = LPFC_NONE;
11513 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11514 if (eq->queue_id == 0xFFFF)
11515 status = -ENXIO;
11516 eq->host_index = 0;
11517 eq->hba_index = 0;
11518
8fa38513 11519 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
11520 return status;
11521}
11522
11523/**
11524 * lpfc_cq_create - Create a Completion Queue on the HBA
11525 * @phba: HBA structure that indicates port to create a queue on.
11526 * @cq: The queue structure to use to create the completion queue.
11527 * @eq: The event queue to bind this completion queue to.
11528 *
11529 * This function creates a completion queue, as detailed in @wq, on a port,
11530 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11531 *
11532 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11533 * is used to get the entry count and entry size that are necessary to
11534 * determine the number of pages to allocate and use for this queue. The @eq
11535 * is used to indicate which event queue to bind this completion queue to. This
11536 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11537 * completion queue. This function is asynchronous and will wait for the mailbox
11538 * command to finish before continuing.
11539 *
11540 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
11541 * memory this function will return -ENOMEM. If the queue create mailbox command
11542 * fails this function will return -ENXIO.
4f774513
JS
11543 **/
11544uint32_t
11545lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11546 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11547{
11548 struct lpfc_mbx_cq_create *cq_create;
11549 struct lpfc_dmabuf *dmabuf;
11550 LPFC_MBOXQ_t *mbox;
11551 int rc, length, status = 0;
11552 uint32_t shdr_status, shdr_add_status;
11553 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
11554 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11555
11556 if (!phba->sli4_hba.pc_sli4_params.supported)
11557 hw_page_size = SLI4_PAGE_SIZE;
11558
4f774513
JS
11559 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11560 if (!mbox)
11561 return -ENOMEM;
11562 length = (sizeof(struct lpfc_mbx_cq_create) -
11563 sizeof(struct lpfc_sli4_cfg_mhdr));
11564 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11565 LPFC_MBOX_OPCODE_CQ_CREATE,
11566 length, LPFC_SLI4_MBX_EMBED);
11567 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 11568 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
11569 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11570 cq->page_count);
11571 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11572 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
11573 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11574 phba->sli4_hba.pc_sli4_params.cqv);
11575 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
c31098ce
JS
11576 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11577 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
5a6f133e
JS
11578 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11579 eq->queue_id);
11580 } else {
11581 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11582 eq->queue_id);
11583 }
4f774513
JS
11584 switch (cq->entry_count) {
11585 default:
11586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11587 "0361 Unsupported CQ count. (%d)\n",
11588 cq->entry_count);
11589 if (cq->entry_count < 256)
11590 return -EINVAL;
11591 /* otherwise default to smallest count (drop through) */
11592 case 256:
11593 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11594 LPFC_CQ_CNT_256);
11595 break;
11596 case 512:
11597 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11598 LPFC_CQ_CNT_512);
11599 break;
11600 case 1024:
11601 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11602 LPFC_CQ_CNT_1024);
11603 break;
11604 }
11605 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 11606 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
11607 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11608 putPaddrLow(dmabuf->phys);
11609 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11610 putPaddrHigh(dmabuf->phys);
11611 }
11612 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11613
11614 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
11615 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11616 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11617 if (shdr_status || shdr_add_status || rc) {
11618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11619 "2501 CQ_CREATE mailbox failed with "
11620 "status x%x add_status x%x, mbx status x%x\n",
11621 shdr_status, shdr_add_status, rc);
11622 status = -ENXIO;
11623 goto out;
11624 }
11625 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11626 if (cq->queue_id == 0xFFFF) {
11627 status = -ENXIO;
11628 goto out;
11629 }
11630 /* link the cq onto the parent eq child list */
11631 list_add_tail(&cq->list, &eq->child_list);
11632 /* Set up completion queue's type and subtype */
11633 cq->type = type;
11634 cq->subtype = subtype;
11635 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 11636 cq->assoc_qid = eq->queue_id;
4f774513
JS
11637 cq->host_index = 0;
11638 cq->hba_index = 0;
4f774513 11639
8fa38513
JS
11640out:
11641 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
11642 return status;
11643}
11644
b19a061a
JS
11645/**
11646 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
11647 * @phba: HBA structure that indicates port to create a queue on.
11648 * @mq: The queue structure to use to create the mailbox queue.
11649 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11650 * @cq: The completion queue to associate with this cq.
11651 *
11652 * This function provides failback (fb) functionality when the
11653 * mq_create_ext fails on older FW generations. It's purpose is identical
11654 * to mq_create_ext otherwise.
11655 *
11656 * This routine cannot fail as all attributes were previously accessed and
11657 * initialized in mq_create_ext.
11658 **/
11659static void
11660lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11661 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
11662{
11663 struct lpfc_mbx_mq_create *mq_create;
11664 struct lpfc_dmabuf *dmabuf;
11665 int length;
11666
11667 length = (sizeof(struct lpfc_mbx_mq_create) -
11668 sizeof(struct lpfc_sli4_cfg_mhdr));
11669 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11670 LPFC_MBOX_OPCODE_MQ_CREATE,
11671 length, LPFC_SLI4_MBX_EMBED);
11672 mq_create = &mbox->u.mqe.un.mq_create;
11673 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
11674 mq->page_count);
11675 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
11676 cq->queue_id);
11677 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11678 switch (mq->entry_count) {
11679 case 16:
5a6f133e
JS
11680 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11681 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
11682 break;
11683 case 32:
5a6f133e
JS
11684 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11685 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
11686 break;
11687 case 64:
5a6f133e
JS
11688 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11689 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
11690 break;
11691 case 128:
5a6f133e
JS
11692 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11693 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
11694 break;
11695 }
11696 list_for_each_entry(dmabuf, &mq->page_list, list) {
11697 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11698 putPaddrLow(dmabuf->phys);
11699 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11700 putPaddrHigh(dmabuf->phys);
11701 }
11702}
11703
04c68496
JS
11704/**
11705 * lpfc_mq_create - Create a mailbox Queue on the HBA
11706 * @phba: HBA structure that indicates port to create a queue on.
11707 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
11708 * @cq: The completion queue to associate with this cq.
11709 * @subtype: The queue's subtype.
04c68496
JS
11710 *
11711 * This function creates a mailbox queue, as detailed in @mq, on a port,
11712 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11713 *
11714 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11715 * is used to get the entry count and entry size that are necessary to
11716 * determine the number of pages to allocate and use for this queue. This
11717 * function will send the MQ_CREATE mailbox command to the HBA to setup the
11718 * mailbox queue. This function is asynchronous and will wait for the mailbox
11719 * command to finish before continuing.
11720 *
11721 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
11722 * memory this function will return -ENOMEM. If the queue create mailbox command
11723 * fails this function will return -ENXIO.
04c68496 11724 **/
b19a061a 11725int32_t
04c68496
JS
11726lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11727 struct lpfc_queue *cq, uint32_t subtype)
11728{
11729 struct lpfc_mbx_mq_create *mq_create;
b19a061a 11730 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
11731 struct lpfc_dmabuf *dmabuf;
11732 LPFC_MBOXQ_t *mbox;
11733 int rc, length, status = 0;
11734 uint32_t shdr_status, shdr_add_status;
11735 union lpfc_sli4_cfg_shdr *shdr;
49198b37 11736 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 11737
49198b37
JS
11738 if (!phba->sli4_hba.pc_sli4_params.supported)
11739 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 11740
04c68496
JS
11741 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11742 if (!mbox)
11743 return -ENOMEM;
b19a061a 11744 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
11745 sizeof(struct lpfc_sli4_cfg_mhdr));
11746 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 11747 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 11748 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
11749
11750 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 11751 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
11752 bf_set(lpfc_mbx_mq_create_ext_num_pages,
11753 &mq_create_ext->u.request, mq->page_count);
11754 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11755 &mq_create_ext->u.request, 1);
11756 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
11757 &mq_create_ext->u.request, 1);
11758 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11759 &mq_create_ext->u.request, 1);
70f3c073
JS
11760 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11761 &mq_create_ext->u.request, 1);
11762 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11763 &mq_create_ext->u.request, 1);
b19a061a 11764 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
11765 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11766 phba->sli4_hba.pc_sli4_params.mqv);
11767 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11768 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11769 cq->queue_id);
11770 else
11771 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11772 cq->queue_id);
04c68496
JS
11773 switch (mq->entry_count) {
11774 default:
11775 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11776 "0362 Unsupported MQ count. (%d)\n",
11777 mq->entry_count);
11778 if (mq->entry_count < 16)
11779 return -EINVAL;
11780 /* otherwise default to smallest count (drop through) */
11781 case 16:
5a6f133e
JS
11782 bf_set(lpfc_mq_context_ring_size,
11783 &mq_create_ext->u.request.context,
11784 LPFC_MQ_RING_SIZE_16);
04c68496
JS
11785 break;
11786 case 32:
5a6f133e
JS
11787 bf_set(lpfc_mq_context_ring_size,
11788 &mq_create_ext->u.request.context,
11789 LPFC_MQ_RING_SIZE_32);
04c68496
JS
11790 break;
11791 case 64:
5a6f133e
JS
11792 bf_set(lpfc_mq_context_ring_size,
11793 &mq_create_ext->u.request.context,
11794 LPFC_MQ_RING_SIZE_64);
04c68496
JS
11795 break;
11796 case 128:
5a6f133e
JS
11797 bf_set(lpfc_mq_context_ring_size,
11798 &mq_create_ext->u.request.context,
11799 LPFC_MQ_RING_SIZE_128);
04c68496
JS
11800 break;
11801 }
11802 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 11803 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 11804 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 11805 putPaddrLow(dmabuf->phys);
b19a061a 11806 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
11807 putPaddrHigh(dmabuf->phys);
11808 }
11809 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
11810 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11811 &mq_create_ext->u.response);
11812 if (rc != MBX_SUCCESS) {
11813 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11814 "2795 MQ_CREATE_EXT failed with "
11815 "status x%x. Failback to MQ_CREATE.\n",
11816 rc);
11817 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
11818 mq_create = &mbox->u.mqe.un.mq_create;
11819 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11820 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
11821 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11822 &mq_create->u.response);
11823 }
11824
04c68496 11825 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
11826 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11827 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11828 if (shdr_status || shdr_add_status || rc) {
11829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11830 "2502 MQ_CREATE mailbox failed with "
11831 "status x%x add_status x%x, mbx status x%x\n",
11832 shdr_status, shdr_add_status, rc);
11833 status = -ENXIO;
11834 goto out;
11835 }
04c68496
JS
11836 if (mq->queue_id == 0xFFFF) {
11837 status = -ENXIO;
11838 goto out;
11839 }
11840 mq->type = LPFC_MQ;
2a622bfb 11841 mq->assoc_qid = cq->queue_id;
04c68496
JS
11842 mq->subtype = subtype;
11843 mq->host_index = 0;
11844 mq->hba_index = 0;
11845
11846 /* link the mq onto the parent cq child list */
11847 list_add_tail(&mq->list, &cq->child_list);
11848out:
8fa38513 11849 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
11850 return status;
11851}
11852
4f774513
JS
11853/**
11854 * lpfc_wq_create - Create a Work Queue on the HBA
11855 * @phba: HBA structure that indicates port to create a queue on.
11856 * @wq: The queue structure to use to create the work queue.
11857 * @cq: The completion queue to bind this work queue to.
11858 * @subtype: The subtype of the work queue indicating its functionality.
11859 *
11860 * This function creates a work queue, as detailed in @wq, on a port, described
11861 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
11862 *
11863 * The @phba struct is used to send mailbox command to HBA. The @wq struct
11864 * is used to get the entry count and entry size that are necessary to
11865 * determine the number of pages to allocate and use for this queue. The @cq
11866 * is used to indicate which completion queue to bind this work queue to. This
11867 * function will send the WQ_CREATE mailbox command to the HBA to setup the
11868 * work queue. This function is asynchronous and will wait for the mailbox
11869 * command to finish before continuing.
11870 *
11871 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
11872 * memory this function will return -ENOMEM. If the queue create mailbox command
11873 * fails this function will return -ENXIO.
4f774513
JS
11874 **/
11875uint32_t
11876lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
11877 struct lpfc_queue *cq, uint32_t subtype)
11878{
11879 struct lpfc_mbx_wq_create *wq_create;
11880 struct lpfc_dmabuf *dmabuf;
11881 LPFC_MBOXQ_t *mbox;
11882 int rc, length, status = 0;
11883 uint32_t shdr_status, shdr_add_status;
11884 union lpfc_sli4_cfg_shdr *shdr;
49198b37 11885 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 11886 struct dma_address *page;
49198b37
JS
11887
11888 if (!phba->sli4_hba.pc_sli4_params.supported)
11889 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
11890
11891 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11892 if (!mbox)
11893 return -ENOMEM;
11894 length = (sizeof(struct lpfc_mbx_wq_create) -
11895 sizeof(struct lpfc_sli4_cfg_mhdr));
11896 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11897 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
11898 length, LPFC_SLI4_MBX_EMBED);
11899 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 11900 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
11901 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
11902 wq->page_count);
11903 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
11904 cq->queue_id);
5a6f133e
JS
11905 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11906 phba->sli4_hba.pc_sli4_params.wqv);
11907 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
11908 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
11909 wq->entry_count);
11910 switch (wq->entry_size) {
11911 default:
11912 case 64:
11913 bf_set(lpfc_mbx_wq_create_wqe_size,
11914 &wq_create->u.request_1,
11915 LPFC_WQ_WQE_SIZE_64);
11916 break;
11917 case 128:
11918 bf_set(lpfc_mbx_wq_create_wqe_size,
11919 &wq_create->u.request_1,
11920 LPFC_WQ_WQE_SIZE_128);
11921 break;
11922 }
11923 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
11924 (PAGE_SIZE/SLI4_PAGE_SIZE));
11925 page = wq_create->u.request_1.page;
11926 } else {
11927 page = wq_create->u.request.page;
11928 }
4f774513 11929 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 11930 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
11931 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
11932 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513
JS
11933 }
11934 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11935 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
11936 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11937 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11938 if (shdr_status || shdr_add_status || rc) {
11939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11940 "2503 WQ_CREATE mailbox failed with "
11941 "status x%x add_status x%x, mbx status x%x\n",
11942 shdr_status, shdr_add_status, rc);
11943 status = -ENXIO;
11944 goto out;
11945 }
11946 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
11947 if (wq->queue_id == 0xFFFF) {
11948 status = -ENXIO;
11949 goto out;
11950 }
11951 wq->type = LPFC_WQ;
2a622bfb 11952 wq->assoc_qid = cq->queue_id;
4f774513
JS
11953 wq->subtype = subtype;
11954 wq->host_index = 0;
11955 wq->hba_index = 0;
11956
11957 /* link the wq onto the parent cq child list */
11958 list_add_tail(&wq->list, &cq->child_list);
11959out:
8fa38513 11960 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
11961 return status;
11962}
11963
73d91e50
JS
11964/**
11965 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
11966 * @phba: HBA structure that indicates port to create a queue on.
11967 * @rq: The queue structure to use for the receive queue.
11968 * @qno: The associated HBQ number
11969 *
11970 *
11971 * For SLI4 we need to adjust the RQ repost value based on
11972 * the number of buffers that are initially posted to the RQ.
11973 */
11974void
11975lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
11976{
11977 uint32_t cnt;
11978
11979 cnt = lpfc_hbq_defs[qno]->entry_count;
11980
11981 /* Recalc repost for RQs based on buffers initially posted */
11982 cnt = (cnt >> 3);
11983 if (cnt < LPFC_QUEUE_MIN_REPOST)
11984 cnt = LPFC_QUEUE_MIN_REPOST;
11985
11986 rq->entry_repost = cnt;
11987}
11988
4f774513
JS
11989/**
11990 * lpfc_rq_create - Create a Receive Queue on the HBA
11991 * @phba: HBA structure that indicates port to create a queue on.
11992 * @hrq: The queue structure to use to create the header receive queue.
11993 * @drq: The queue structure to use to create the data receive queue.
11994 * @cq: The completion queue to bind this work queue to.
11995 *
11996 * This function creates a receive buffer queue pair , as detailed in @hrq and
11997 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
11998 * to the HBA.
11999 *
12000 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12001 * struct is used to get the entry count that is necessary to determine the
12002 * number of pages to use for this queue. The @cq is used to indicate which
12003 * completion queue to bind received buffers that are posted to these queues to.
12004 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12005 * receive queue pair. This function is asynchronous and will wait for the
12006 * mailbox command to finish before continuing.
12007 *
12008 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12009 * memory this function will return -ENOMEM. If the queue create mailbox command
12010 * fails this function will return -ENXIO.
4f774513
JS
12011 **/
12012uint32_t
12013lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12014 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12015{
12016 struct lpfc_mbx_rq_create *rq_create;
12017 struct lpfc_dmabuf *dmabuf;
12018 LPFC_MBOXQ_t *mbox;
12019 int rc, length, status = 0;
12020 uint32_t shdr_status, shdr_add_status;
12021 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
12022 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12023
12024 if (!phba->sli4_hba.pc_sli4_params.supported)
12025 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
12026
12027 if (hrq->entry_count != drq->entry_count)
12028 return -EINVAL;
12029 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12030 if (!mbox)
12031 return -ENOMEM;
12032 length = (sizeof(struct lpfc_mbx_rq_create) -
12033 sizeof(struct lpfc_sli4_cfg_mhdr));
12034 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12035 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12036 length, LPFC_SLI4_MBX_EMBED);
12037 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
12038 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12039 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12040 phba->sli4_hba.pc_sli4_params.rqv);
12041 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12042 bf_set(lpfc_rq_context_rqe_count_1,
12043 &rq_create->u.request.context,
12044 hrq->entry_count);
12045 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
12046 bf_set(lpfc_rq_context_rqe_size,
12047 &rq_create->u.request.context,
12048 LPFC_RQE_SIZE_8);
12049 bf_set(lpfc_rq_context_page_size,
12050 &rq_create->u.request.context,
12051 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
12052 } else {
12053 switch (hrq->entry_count) {
12054 default:
12055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12056 "2535 Unsupported RQ count. (%d)\n",
12057 hrq->entry_count);
12058 if (hrq->entry_count < 512)
12059 return -EINVAL;
12060 /* otherwise default to smallest count (drop through) */
12061 case 512:
12062 bf_set(lpfc_rq_context_rqe_count,
12063 &rq_create->u.request.context,
12064 LPFC_RQ_RING_SIZE_512);
12065 break;
12066 case 1024:
12067 bf_set(lpfc_rq_context_rqe_count,
12068 &rq_create->u.request.context,
12069 LPFC_RQ_RING_SIZE_1024);
12070 break;
12071 case 2048:
12072 bf_set(lpfc_rq_context_rqe_count,
12073 &rq_create->u.request.context,
12074 LPFC_RQ_RING_SIZE_2048);
12075 break;
12076 case 4096:
12077 bf_set(lpfc_rq_context_rqe_count,
12078 &rq_create->u.request.context,
12079 LPFC_RQ_RING_SIZE_4096);
12080 break;
12081 }
12082 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12083 LPFC_HDR_BUF_SIZE);
4f774513
JS
12084 }
12085 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12086 cq->queue_id);
12087 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12088 hrq->page_count);
4f774513 12089 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 12090 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12091 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12092 putPaddrLow(dmabuf->phys);
12093 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12094 putPaddrHigh(dmabuf->phys);
12095 }
12096 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12097 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
12098 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12099 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12100 if (shdr_status || shdr_add_status || rc) {
12101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12102 "2504 RQ_CREATE mailbox failed with "
12103 "status x%x add_status x%x, mbx status x%x\n",
12104 shdr_status, shdr_add_status, rc);
12105 status = -ENXIO;
12106 goto out;
12107 }
12108 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12109 if (hrq->queue_id == 0xFFFF) {
12110 status = -ENXIO;
12111 goto out;
12112 }
12113 hrq->type = LPFC_HRQ;
2a622bfb 12114 hrq->assoc_qid = cq->queue_id;
4f774513
JS
12115 hrq->subtype = subtype;
12116 hrq->host_index = 0;
12117 hrq->hba_index = 0;
12118
12119 /* now create the data queue */
12120 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12121 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12122 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
12123 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12124 phba->sli4_hba.pc_sli4_params.rqv);
12125 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12126 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 12127 &rq_create->u.request.context, hrq->entry_count);
5a6f133e 12128 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
c31098ce
JS
12129 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12130 LPFC_RQE_SIZE_8);
12131 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12132 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
12133 } else {
12134 switch (drq->entry_count) {
12135 default:
12136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12137 "2536 Unsupported RQ count. (%d)\n",
12138 drq->entry_count);
12139 if (drq->entry_count < 512)
12140 return -EINVAL;
12141 /* otherwise default to smallest count (drop through) */
12142 case 512:
12143 bf_set(lpfc_rq_context_rqe_count,
12144 &rq_create->u.request.context,
12145 LPFC_RQ_RING_SIZE_512);
12146 break;
12147 case 1024:
12148 bf_set(lpfc_rq_context_rqe_count,
12149 &rq_create->u.request.context,
12150 LPFC_RQ_RING_SIZE_1024);
12151 break;
12152 case 2048:
12153 bf_set(lpfc_rq_context_rqe_count,
12154 &rq_create->u.request.context,
12155 LPFC_RQ_RING_SIZE_2048);
12156 break;
12157 case 4096:
12158 bf_set(lpfc_rq_context_rqe_count,
12159 &rq_create->u.request.context,
12160 LPFC_RQ_RING_SIZE_4096);
12161 break;
12162 }
12163 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12164 LPFC_DATA_BUF_SIZE);
4f774513
JS
12165 }
12166 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12167 cq->queue_id);
12168 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12169 drq->page_count);
4f774513
JS
12170 list_for_each_entry(dmabuf, &drq->page_list, list) {
12171 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12172 putPaddrLow(dmabuf->phys);
12173 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12174 putPaddrHigh(dmabuf->phys);
12175 }
12176 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12177 /* The IOCTL status is embedded in the mailbox subheader. */
12178 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12179 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12180 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12181 if (shdr_status || shdr_add_status || rc) {
12182 status = -ENXIO;
12183 goto out;
12184 }
12185 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12186 if (drq->queue_id == 0xFFFF) {
12187 status = -ENXIO;
12188 goto out;
12189 }
12190 drq->type = LPFC_DRQ;
2a622bfb 12191 drq->assoc_qid = cq->queue_id;
4f774513
JS
12192 drq->subtype = subtype;
12193 drq->host_index = 0;
12194 drq->hba_index = 0;
12195
12196 /* link the header and data RQs onto the parent cq child list */
12197 list_add_tail(&hrq->list, &cq->child_list);
12198 list_add_tail(&drq->list, &cq->child_list);
12199
12200out:
8fa38513 12201 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12202 return status;
12203}
12204
12205/**
12206 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12207 * @eq: The queue structure associated with the queue to destroy.
12208 *
12209 * This function destroys a queue, as detailed in @eq by sending an mailbox
12210 * command, specific to the type of queue, to the HBA.
12211 *
12212 * The @eq struct is used to get the queue ID of the queue to destroy.
12213 *
12214 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12215 * command fails this function will return -ENXIO.
4f774513
JS
12216 **/
12217uint32_t
12218lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12219{
12220 LPFC_MBOXQ_t *mbox;
12221 int rc, length, status = 0;
12222 uint32_t shdr_status, shdr_add_status;
12223 union lpfc_sli4_cfg_shdr *shdr;
12224
12225 if (!eq)
12226 return -ENODEV;
12227 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12228 if (!mbox)
12229 return -ENOMEM;
12230 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12231 sizeof(struct lpfc_sli4_cfg_mhdr));
12232 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12233 LPFC_MBOX_OPCODE_EQ_DESTROY,
12234 length, LPFC_SLI4_MBX_EMBED);
12235 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12236 eq->queue_id);
12237 mbox->vport = eq->phba->pport;
12238 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12239
12240 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12241 /* The IOCTL status is embedded in the mailbox subheader. */
12242 shdr = (union lpfc_sli4_cfg_shdr *)
12243 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12244 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12245 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12246 if (shdr_status || shdr_add_status || rc) {
12247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12248 "2505 EQ_DESTROY mailbox failed with "
12249 "status x%x add_status x%x, mbx status x%x\n",
12250 shdr_status, shdr_add_status, rc);
12251 status = -ENXIO;
12252 }
12253
12254 /* Remove eq from any list */
12255 list_del_init(&eq->list);
8fa38513 12256 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
12257 return status;
12258}
12259
12260/**
12261 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12262 * @cq: The queue structure associated with the queue to destroy.
12263 *
12264 * This function destroys a queue, as detailed in @cq by sending an mailbox
12265 * command, specific to the type of queue, to the HBA.
12266 *
12267 * The @cq struct is used to get the queue ID of the queue to destroy.
12268 *
12269 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12270 * command fails this function will return -ENXIO.
4f774513
JS
12271 **/
12272uint32_t
12273lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12274{
12275 LPFC_MBOXQ_t *mbox;
12276 int rc, length, status = 0;
12277 uint32_t shdr_status, shdr_add_status;
12278 union lpfc_sli4_cfg_shdr *shdr;
12279
12280 if (!cq)
12281 return -ENODEV;
12282 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12283 if (!mbox)
12284 return -ENOMEM;
12285 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12286 sizeof(struct lpfc_sli4_cfg_mhdr));
12287 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12288 LPFC_MBOX_OPCODE_CQ_DESTROY,
12289 length, LPFC_SLI4_MBX_EMBED);
12290 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12291 cq->queue_id);
12292 mbox->vport = cq->phba->pport;
12293 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12294 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12295 /* The IOCTL status is embedded in the mailbox subheader. */
12296 shdr = (union lpfc_sli4_cfg_shdr *)
12297 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12298 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12299 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12300 if (shdr_status || shdr_add_status || rc) {
12301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12302 "2506 CQ_DESTROY mailbox failed with "
12303 "status x%x add_status x%x, mbx status x%x\n",
12304 shdr_status, shdr_add_status, rc);
12305 status = -ENXIO;
12306 }
12307 /* Remove cq from any list */
12308 list_del_init(&cq->list);
8fa38513 12309 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
12310 return status;
12311}
12312
04c68496
JS
12313/**
12314 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12315 * @qm: The queue structure associated with the queue to destroy.
12316 *
12317 * This function destroys a queue, as detailed in @mq by sending an mailbox
12318 * command, specific to the type of queue, to the HBA.
12319 *
12320 * The @mq struct is used to get the queue ID of the queue to destroy.
12321 *
12322 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12323 * command fails this function will return -ENXIO.
04c68496
JS
12324 **/
12325uint32_t
12326lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12327{
12328 LPFC_MBOXQ_t *mbox;
12329 int rc, length, status = 0;
12330 uint32_t shdr_status, shdr_add_status;
12331 union lpfc_sli4_cfg_shdr *shdr;
12332
12333 if (!mq)
12334 return -ENODEV;
12335 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12336 if (!mbox)
12337 return -ENOMEM;
12338 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12339 sizeof(struct lpfc_sli4_cfg_mhdr));
12340 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12341 LPFC_MBOX_OPCODE_MQ_DESTROY,
12342 length, LPFC_SLI4_MBX_EMBED);
12343 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12344 mq->queue_id);
12345 mbox->vport = mq->phba->pport;
12346 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12347 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12348 /* The IOCTL status is embedded in the mailbox subheader. */
12349 shdr = (union lpfc_sli4_cfg_shdr *)
12350 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12351 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12352 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12353 if (shdr_status || shdr_add_status || rc) {
12354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12355 "2507 MQ_DESTROY mailbox failed with "
12356 "status x%x add_status x%x, mbx status x%x\n",
12357 shdr_status, shdr_add_status, rc);
12358 status = -ENXIO;
12359 }
12360 /* Remove mq from any list */
12361 list_del_init(&mq->list);
8fa38513 12362 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
12363 return status;
12364}
12365
4f774513
JS
12366/**
12367 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12368 * @wq: The queue structure associated with the queue to destroy.
12369 *
12370 * This function destroys a queue, as detailed in @wq by sending an mailbox
12371 * command, specific to the type of queue, to the HBA.
12372 *
12373 * The @wq struct is used to get the queue ID of the queue to destroy.
12374 *
12375 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12376 * command fails this function will return -ENXIO.
4f774513
JS
12377 **/
12378uint32_t
12379lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12380{
12381 LPFC_MBOXQ_t *mbox;
12382 int rc, length, status = 0;
12383 uint32_t shdr_status, shdr_add_status;
12384 union lpfc_sli4_cfg_shdr *shdr;
12385
12386 if (!wq)
12387 return -ENODEV;
12388 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12389 if (!mbox)
12390 return -ENOMEM;
12391 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12392 sizeof(struct lpfc_sli4_cfg_mhdr));
12393 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12394 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12395 length, LPFC_SLI4_MBX_EMBED);
12396 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12397 wq->queue_id);
12398 mbox->vport = wq->phba->pport;
12399 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12400 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12401 shdr = (union lpfc_sli4_cfg_shdr *)
12402 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12403 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12404 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12405 if (shdr_status || shdr_add_status || rc) {
12406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12407 "2508 WQ_DESTROY mailbox failed with "
12408 "status x%x add_status x%x, mbx status x%x\n",
12409 shdr_status, shdr_add_status, rc);
12410 status = -ENXIO;
12411 }
12412 /* Remove wq from any list */
12413 list_del_init(&wq->list);
8fa38513 12414 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
12415 return status;
12416}
12417
12418/**
12419 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12420 * @rq: The queue structure associated with the queue to destroy.
12421 *
12422 * This function destroys a queue, as detailed in @rq by sending an mailbox
12423 * command, specific to the type of queue, to the HBA.
12424 *
12425 * The @rq struct is used to get the queue ID of the queue to destroy.
12426 *
12427 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12428 * command fails this function will return -ENXIO.
4f774513
JS
12429 **/
12430uint32_t
12431lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12432 struct lpfc_queue *drq)
12433{
12434 LPFC_MBOXQ_t *mbox;
12435 int rc, length, status = 0;
12436 uint32_t shdr_status, shdr_add_status;
12437 union lpfc_sli4_cfg_shdr *shdr;
12438
12439 if (!hrq || !drq)
12440 return -ENODEV;
12441 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12442 if (!mbox)
12443 return -ENOMEM;
12444 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 12445 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
12446 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12447 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12448 length, LPFC_SLI4_MBX_EMBED);
12449 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12450 hrq->queue_id);
12451 mbox->vport = hrq->phba->pport;
12452 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12453 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12454 /* The IOCTL status is embedded in the mailbox subheader. */
12455 shdr = (union lpfc_sli4_cfg_shdr *)
12456 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12457 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12458 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12459 if (shdr_status || shdr_add_status || rc) {
12460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12461 "2509 RQ_DESTROY mailbox failed with "
12462 "status x%x add_status x%x, mbx status x%x\n",
12463 shdr_status, shdr_add_status, rc);
12464 if (rc != MBX_TIMEOUT)
12465 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12466 return -ENXIO;
12467 }
12468 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12469 drq->queue_id);
12470 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12471 shdr = (union lpfc_sli4_cfg_shdr *)
12472 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12473 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12474 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12475 if (shdr_status || shdr_add_status || rc) {
12476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12477 "2510 RQ_DESTROY mailbox failed with "
12478 "status x%x add_status x%x, mbx status x%x\n",
12479 shdr_status, shdr_add_status, rc);
12480 status = -ENXIO;
12481 }
12482 list_del_init(&hrq->list);
12483 list_del_init(&drq->list);
8fa38513 12484 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
12485 return status;
12486}
12487
12488/**
12489 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12490 * @phba: The virtual port for which this call being executed.
12491 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12492 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12493 * @xritag: the xritag that ties this io to the SGL pages.
12494 *
12495 * This routine will post the sgl pages for the IO that has the xritag
12496 * that is in the iocbq structure. The xritag is assigned during iocbq
12497 * creation and persists for as long as the driver is loaded.
12498 * if the caller has fewer than 256 scatter gather segments to map then
12499 * pdma_phys_addr1 should be 0.
12500 * If the caller needs to map more than 256 scatter gather segment then
12501 * pdma_phys_addr1 should be a valid physical address.
12502 * physical address for SGLs must be 64 byte aligned.
12503 * If you are going to map 2 SGL's then the first one must have 256 entries
12504 * the second sgl can have between 1 and 256 entries.
12505 *
12506 * Return codes:
12507 * 0 - Success
12508 * -ENXIO, -ENOMEM - Failure
12509 **/
12510int
12511lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12512 dma_addr_t pdma_phys_addr0,
12513 dma_addr_t pdma_phys_addr1,
12514 uint16_t xritag)
12515{
12516 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12517 LPFC_MBOXQ_t *mbox;
12518 int rc;
12519 uint32_t shdr_status, shdr_add_status;
6d368e53 12520 uint32_t mbox_tmo;
4f774513
JS
12521 union lpfc_sli4_cfg_shdr *shdr;
12522
12523 if (xritag == NO_XRI) {
12524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12525 "0364 Invalid param:\n");
12526 return -EINVAL;
12527 }
12528
12529 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12530 if (!mbox)
12531 return -ENOMEM;
12532
12533 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12534 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12535 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 12536 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
12537
12538 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12539 &mbox->u.mqe.un.post_sgl_pages;
12540 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12541 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12542
12543 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12544 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12545 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12546 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12547
12548 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12549 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12550 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12551 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12552 if (!phba->sli4_hba.intr_enable)
12553 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 12554 else {
a183a15f 12555 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
12556 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12557 }
4f774513
JS
12558 /* The IOCTL status is embedded in the mailbox subheader. */
12559 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12560 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12561 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12562 if (rc != MBX_TIMEOUT)
12563 mempool_free(mbox, phba->mbox_mem_pool);
12564 if (shdr_status || shdr_add_status || rc) {
12565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12566 "2511 POST_SGL mailbox failed with "
12567 "status x%x add_status x%x, mbx status x%x\n",
12568 shdr_status, shdr_add_status, rc);
12569 rc = -ENXIO;
12570 }
12571 return 0;
12572}
4f774513 12573
6d368e53 12574/**
88a2cfbb 12575 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
12576 * @phba: pointer to lpfc hba data structure.
12577 *
12578 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
12579 * HBA consistent with the SLI-4 interface spec. This routine
12580 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12581 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 12582 *
88a2cfbb
JS
12583 * Returns
12584 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12585 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12586 **/
6d368e53
JS
12587uint16_t
12588lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12589{
12590 unsigned long xri;
12591
12592 /*
12593 * Fetch the next logical xri. Because this index is logical,
12594 * the driver starts at 0 each time.
12595 */
12596 spin_lock_irq(&phba->hbalock);
12597 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12598 phba->sli4_hba.max_cfg_param.max_xri, 0);
12599 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12600 spin_unlock_irq(&phba->hbalock);
12601 return NO_XRI;
12602 } else {
12603 set_bit(xri, phba->sli4_hba.xri_bmask);
12604 phba->sli4_hba.max_cfg_param.xri_used++;
12605 phba->sli4_hba.xri_count++;
12606 }
12607
12608 spin_unlock_irq(&phba->hbalock);
12609 return xri;
12610}
12611
12612/**
12613 * lpfc_sli4_free_xri - Release an xri for reuse.
12614 * @phba: pointer to lpfc hba data structure.
12615 *
12616 * This routine is invoked to release an xri to the pool of
12617 * available rpis maintained by the driver.
12618 **/
12619void
12620__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12621{
12622 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12623 phba->sli4_hba.xri_count--;
12624 phba->sli4_hba.max_cfg_param.xri_used--;
12625 }
12626}
12627
12628/**
12629 * lpfc_sli4_free_xri - Release an xri for reuse.
12630 * @phba: pointer to lpfc hba data structure.
12631 *
12632 * This routine is invoked to release an xri to the pool of
12633 * available rpis maintained by the driver.
12634 **/
12635void
12636lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12637{
12638 spin_lock_irq(&phba->hbalock);
12639 __lpfc_sli4_free_xri(phba, xri);
12640 spin_unlock_irq(&phba->hbalock);
12641}
12642
4f774513
JS
12643/**
12644 * lpfc_sli4_next_xritag - Get an xritag for the io
12645 * @phba: Pointer to HBA context object.
12646 *
12647 * This function gets an xritag for the iocb. If there is no unused xritag
12648 * it will return 0xffff.
12649 * The function returns the allocated xritag if successful, else returns zero.
12650 * Zero is not a valid xritag.
12651 * The caller is not required to hold any lock.
12652 **/
12653uint16_t
12654lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12655{
6d368e53 12656 uint16_t xri_index;
4f774513 12657
6d368e53
JS
12658 xri_index = lpfc_sli4_alloc_xri(phba);
12659 if (xri_index != NO_XRI)
12660 return xri_index;
12661
12662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4f774513
JS
12663 "2004 Failed to allocate XRI.last XRITAG is %d"
12664 " Max XRI is %d, Used XRI is %d\n",
6d368e53 12665 xri_index,
4f774513
JS
12666 phba->sli4_hba.max_cfg_param.max_xri,
12667 phba->sli4_hba.max_cfg_param.xri_used);
6d368e53 12668 return NO_XRI;
4f774513
JS
12669}
12670
12671/**
6d368e53 12672 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
4f774513
JS
12673 * @phba: pointer to lpfc hba data structure.
12674 *
12675 * This routine is invoked to post a block of driver's sgl pages to the
12676 * HBA using non-embedded mailbox command. No Lock is held. This routine
12677 * is only called when the driver is loading and after all IO has been
12678 * stopped.
12679 **/
12680int
6d368e53 12681lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
4f774513
JS
12682{
12683 struct lpfc_sglq *sglq_entry;
12684 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12685 struct sgl_page_pairs *sgl_pg_pairs;
12686 void *viraddr;
12687 LPFC_MBOXQ_t *mbox;
12688 uint32_t reqlen, alloclen, pg_pairs;
12689 uint32_t mbox_tmo;
6d368e53 12690 uint16_t xritag_start = 0, lxri = 0;
4f774513
JS
12691 int els_xri_cnt, rc = 0;
12692 uint32_t shdr_status, shdr_add_status;
12693 union lpfc_sli4_cfg_shdr *shdr;
12694
12695 /* The number of sgls to be posted */
12696 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12697
12698 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12699 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 12700 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
12701 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12702 "2559 Block sgl registration required DMA "
12703 "size (%d) great than a page\n", reqlen);
12704 return -ENOMEM;
12705 }
12706 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 12707 if (!mbox)
4f774513 12708 return -ENOMEM;
4f774513
JS
12709
12710 /* Allocate DMA memory and set up the non-embedded mailbox command */
12711 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12712 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12713 LPFC_SLI4_MBX_NEMBED);
12714
12715 if (alloclen < reqlen) {
12716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12717 "0285 Allocated DMA memory size (%d) is "
12718 "less than the requested DMA memory "
12719 "size (%d)\n", alloclen, reqlen);
12720 lpfc_sli4_mbox_cmd_free(phba, mbox);
12721 return -ENOMEM;
12722 }
4f774513 12723 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 12724 viraddr = mbox->sge_array->addr[0];
4f774513
JS
12725 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12726 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12727
12728 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12729 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
6d368e53
JS
12730
12731 /*
12732 * Assign the sglq a physical xri only if the driver has not
12733 * initialized those resources. A port reset only needs
12734 * the sglq's posted.
12735 */
12736 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12737 LPFC_XRI_RSRC_RDY) {
12738 lxri = lpfc_sli4_next_xritag(phba);
12739 if (lxri == NO_XRI) {
12740 lpfc_sli4_mbox_cmd_free(phba, mbox);
12741 return -ENOMEM;
12742 }
12743 sglq_entry->sli4_lxritag = lxri;
12744 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12745 }
12746
4f774513
JS
12747 /* Set up the sge entry */
12748 sgl_pg_pairs->sgl_pg0_addr_lo =
12749 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12750 sgl_pg_pairs->sgl_pg0_addr_hi =
12751 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12752 sgl_pg_pairs->sgl_pg1_addr_lo =
12753 cpu_to_le32(putPaddrLow(0));
12754 sgl_pg_pairs->sgl_pg1_addr_hi =
12755 cpu_to_le32(putPaddrHigh(0));
6d368e53 12756
4f774513
JS
12757 /* Keep the first xritag on the list */
12758 if (pg_pairs == 0)
12759 xritag_start = sglq_entry->sli4_xritag;
12760 sgl_pg_pairs++;
12761 }
6d368e53
JS
12762
12763 /* Complete initialization and perform endian conversion. */
4f774513 12764 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 12765 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513 12766 sgl->word0 = cpu_to_le32(sgl->word0);
4f774513
JS
12767 if (!phba->sli4_hba.intr_enable)
12768 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12769 else {
a183a15f 12770 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
12771 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12772 }
12773 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12774 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12775 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12776 if (rc != MBX_TIMEOUT)
12777 lpfc_sli4_mbox_cmd_free(phba, mbox);
12778 if (shdr_status || shdr_add_status || rc) {
12779 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12780 "2513 POST_SGL_BLOCK mailbox command failed "
12781 "status x%x add_status x%x mbx status x%x\n",
12782 shdr_status, shdr_add_status, rc);
12783 rc = -ENXIO;
12784 }
6d368e53
JS
12785
12786 if (rc == 0)
12787 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12788 LPFC_XRI_RSRC_RDY);
12789 return rc;
12790}
12791
12792/**
12793 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
12794 * @phba: pointer to lpfc hba data structure.
12795 *
12796 * This routine is invoked to post a block of driver's sgl pages to the
12797 * HBA using non-embedded mailbox command. No Lock is held. This routine
12798 * is only called when the driver is loading and after all IO has been
12799 * stopped.
12800 **/
12801int
12802lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
12803{
12804 struct lpfc_sglq *sglq_entry;
12805 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12806 struct sgl_page_pairs *sgl_pg_pairs;
12807 void *viraddr;
12808 LPFC_MBOXQ_t *mbox;
12809 uint32_t reqlen, alloclen, index;
12810 uint32_t mbox_tmo;
12811 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
12812 uint16_t xritag_start = 0, lxri = 0;
12813 struct lpfc_rsrc_blks *rsrc_blk;
12814 int cnt, ttl_cnt, rc = 0;
12815 int loop_cnt;
12816 uint32_t shdr_status, shdr_add_status;
12817 union lpfc_sli4_cfg_shdr *shdr;
12818
12819 /* The number of sgls to be posted */
12820 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12821
12822 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12823 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12824 if (reqlen > SLI4_PAGE_SIZE) {
12825 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12826 "2989 Block sgl registration required DMA "
12827 "size (%d) great than a page\n", reqlen);
12828 return -ENOMEM;
12829 }
12830
12831 cnt = 0;
12832 ttl_cnt = 0;
12833 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
12834 list) {
12835 rsrc_start = rsrc_blk->rsrc_start;
12836 rsrc_size = rsrc_blk->rsrc_size;
12837
12838 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12839 "3014 Working ELS Extent start %d, cnt %d\n",
12840 rsrc_start, rsrc_size);
12841
12842 loop_cnt = min(els_xri_cnt, rsrc_size);
12843 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
12844 loop_cnt = els_xri_cnt - ttl_cnt;
12845 ttl_cnt = els_xri_cnt;
12846 }
12847
12848 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12849 if (!mbox)
12850 return -ENOMEM;
12851 /*
12852 * Allocate DMA memory and set up the non-embedded mailbox
12853 * command.
12854 */
12855 alloclen = lpfc_sli4_config(phba, mbox,
12856 LPFC_MBOX_SUBSYSTEM_FCOE,
12857 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12858 reqlen, LPFC_SLI4_MBX_NEMBED);
12859 if (alloclen < reqlen) {
12860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12861 "2987 Allocated DMA memory size (%d) "
12862 "is less than the requested DMA memory "
12863 "size (%d)\n", alloclen, reqlen);
12864 lpfc_sli4_mbox_cmd_free(phba, mbox);
12865 return -ENOMEM;
12866 }
12867
12868 /* Set up the SGL pages in the non-embedded DMA pages */
12869 viraddr = mbox->sge_array->addr[0];
12870 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12871 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12872
12873 /*
12874 * The starting resource may not begin at zero. Control
12875 * the loop variants via the block resource parameters,
12876 * but handle the sge pointers with a zero-based index
12877 * that doesn't get reset per loop pass.
12878 */
12879 for (index = rsrc_start;
12880 index < rsrc_start + loop_cnt;
12881 index++) {
12882 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
12883
12884 /*
12885 * Assign the sglq a physical xri only if the driver
12886 * has not initialized those resources. A port reset
12887 * only needs the sglq's posted.
12888 */
12889 if (bf_get(lpfc_xri_rsrc_rdy,
12890 &phba->sli4_hba.sli4_flags) !=
12891 LPFC_XRI_RSRC_RDY) {
12892 lxri = lpfc_sli4_next_xritag(phba);
12893 if (lxri == NO_XRI) {
12894 lpfc_sli4_mbox_cmd_free(phba, mbox);
12895 rc = -ENOMEM;
12896 goto err_exit;
12897 }
12898 sglq_entry->sli4_lxritag = lxri;
12899 sglq_entry->sli4_xritag =
12900 phba->sli4_hba.xri_ids[lxri];
12901 }
12902
12903 /* Set up the sge entry */
12904 sgl_pg_pairs->sgl_pg0_addr_lo =
12905 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12906 sgl_pg_pairs->sgl_pg0_addr_hi =
12907 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12908 sgl_pg_pairs->sgl_pg1_addr_lo =
12909 cpu_to_le32(putPaddrLow(0));
12910 sgl_pg_pairs->sgl_pg1_addr_hi =
12911 cpu_to_le32(putPaddrHigh(0));
12912
12913 /* Track the starting physical XRI for the mailbox. */
12914 if (index == rsrc_start)
12915 xritag_start = sglq_entry->sli4_xritag;
12916 sgl_pg_pairs++;
12917 cnt++;
12918 }
12919
12920 /* Complete initialization and perform endian conversion. */
12921 rsrc_blk->rsrc_used += loop_cnt;
12922 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12923 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
12924 sgl->word0 = cpu_to_le32(sgl->word0);
12925
12926 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12927 "3015 Post ELS Extent SGL, start %d, "
12928 "cnt %d, used %d\n",
12929 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
12930 if (!phba->sli4_hba.intr_enable)
12931 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12932 else {
a183a15f 12933 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
12934 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12935 }
12936 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12937 shdr_status = bf_get(lpfc_mbox_hdr_status,
12938 &shdr->response);
12939 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12940 &shdr->response);
12941 if (rc != MBX_TIMEOUT)
12942 lpfc_sli4_mbox_cmd_free(phba, mbox);
12943 if (shdr_status || shdr_add_status || rc) {
12944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12945 "2988 POST_SGL_BLOCK mailbox "
12946 "command failed status x%x "
12947 "add_status x%x mbx status x%x\n",
12948 shdr_status, shdr_add_status, rc);
12949 rc = -ENXIO;
12950 goto err_exit;
12951 }
12952 if (ttl_cnt >= els_xri_cnt)
12953 break;
12954 }
12955
12956 err_exit:
12957 if (rc == 0)
12958 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12959 LPFC_XRI_RSRC_RDY);
4f774513
JS
12960 return rc;
12961}
12962
12963/**
12964 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
12965 * @phba: pointer to lpfc hba data structure.
12966 * @sblist: pointer to scsi buffer list.
12967 * @count: number of scsi buffers on the list.
12968 *
12969 * This routine is invoked to post a block of @count scsi sgl pages from a
12970 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
12971 * No Lock is held.
12972 *
12973 **/
12974int
12975lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
12976 int cnt)
12977{
12978 struct lpfc_scsi_buf *psb;
12979 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12980 struct sgl_page_pairs *sgl_pg_pairs;
12981 void *viraddr;
12982 LPFC_MBOXQ_t *mbox;
12983 uint32_t reqlen, alloclen, pg_pairs;
12984 uint32_t mbox_tmo;
12985 uint16_t xritag_start = 0;
12986 int rc = 0;
12987 uint32_t shdr_status, shdr_add_status;
12988 dma_addr_t pdma_phys_bpl1;
12989 union lpfc_sli4_cfg_shdr *shdr;
12990
12991 /* Calculate the requested length of the dma memory */
12992 reqlen = cnt * sizeof(struct sgl_page_pairs) +
12993 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 12994 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
12995 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12996 "0217 Block sgl registration required DMA "
12997 "size (%d) great than a page\n", reqlen);
12998 return -ENOMEM;
12999 }
13000 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13001 if (!mbox) {
13002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13003 "0283 Failed to allocate mbox cmd memory\n");
13004 return -ENOMEM;
13005 }
13006
13007 /* Allocate DMA memory and set up the non-embedded mailbox command */
13008 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13009 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13010 LPFC_SLI4_MBX_NEMBED);
13011
13012 if (alloclen < reqlen) {
13013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13014 "2561 Allocated DMA memory size (%d) is "
13015 "less than the requested DMA memory "
13016 "size (%d)\n", alloclen, reqlen);
13017 lpfc_sli4_mbox_cmd_free(phba, mbox);
13018 return -ENOMEM;
13019 }
6d368e53 13020
4f774513 13021 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
13022 viraddr = mbox->sge_array->addr[0];
13023
13024 /* Set up the SGL pages in the non-embedded DMA pages */
13025 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13026 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13027
13028 pg_pairs = 0;
13029 list_for_each_entry(psb, sblist, list) {
13030 /* Set up the sge entry */
13031 sgl_pg_pairs->sgl_pg0_addr_lo =
13032 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13033 sgl_pg_pairs->sgl_pg0_addr_hi =
13034 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13035 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13036 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13037 else
13038 pdma_phys_bpl1 = 0;
13039 sgl_pg_pairs->sgl_pg1_addr_lo =
13040 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13041 sgl_pg_pairs->sgl_pg1_addr_hi =
13042 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13043 /* Keep the first xritag on the list */
13044 if (pg_pairs == 0)
13045 xritag_start = psb->cur_iocbq.sli4_xritag;
13046 sgl_pg_pairs++;
13047 pg_pairs++;
13048 }
13049 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13050 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13051 /* Perform endian conversion if necessary */
13052 sgl->word0 = cpu_to_le32(sgl->word0);
13053
13054 if (!phba->sli4_hba.intr_enable)
13055 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13056 else {
a183a15f 13057 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
13058 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13059 }
13060 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13061 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13062 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13063 if (rc != MBX_TIMEOUT)
13064 lpfc_sli4_mbox_cmd_free(phba, mbox);
13065 if (shdr_status || shdr_add_status || rc) {
13066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13067 "2564 POST_SGL_BLOCK mailbox command failed "
13068 "status x%x add_status x%x mbx status x%x\n",
13069 shdr_status, shdr_add_status, rc);
13070 rc = -ENXIO;
13071 }
13072 return rc;
13073}
13074
6d368e53
JS
13075/**
13076 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13077 * @phba: pointer to lpfc hba data structure.
13078 * @sblist: pointer to scsi buffer list.
13079 * @count: number of scsi buffers on the list.
13080 *
13081 * This routine is invoked to post a block of @count scsi sgl pages from a
13082 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13083 * No Lock is held.
13084 *
13085 **/
13086int
13087lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13088 int cnt)
13089{
13090 struct lpfc_scsi_buf *psb = NULL;
13091 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13092 struct sgl_page_pairs *sgl_pg_pairs;
13093 void *viraddr;
13094 LPFC_MBOXQ_t *mbox;
13095 uint32_t reqlen, alloclen, pg_pairs;
13096 uint32_t mbox_tmo;
13097 uint16_t xri_start = 0, scsi_xri_start;
13098 uint16_t rsrc_range;
13099 int rc = 0, avail_cnt;
13100 uint32_t shdr_status, shdr_add_status;
13101 dma_addr_t pdma_phys_bpl1;
13102 union lpfc_sli4_cfg_shdr *shdr;
13103 struct lpfc_rsrc_blks *rsrc_blk;
13104 uint32_t xri_cnt = 0;
13105
13106 /* Calculate the total requested length of the dma memory */
13107 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13108 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13109 if (reqlen > SLI4_PAGE_SIZE) {
13110 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13111 "2932 Block sgl registration required DMA "
13112 "size (%d) great than a page\n", reqlen);
13113 return -ENOMEM;
13114 }
13115
13116 /*
13117 * The use of extents requires the driver to post the sgl headers
13118 * in multiple postings to meet the contiguous resource assignment.
13119 */
13120 psb = list_prepare_entry(psb, sblist, list);
13121 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13122 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13123 list) {
13124 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13125 if (rsrc_range < scsi_xri_start)
13126 continue;
13127 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13128 continue;
13129 else
13130 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13131
13132 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13133 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13134 /*
13135 * Allocate DMA memory and set up the non-embedded mailbox
13136 * command. The mbox is used to post an SGL page per loop
13137 * but the DMA memory has a use-once semantic so the mailbox
13138 * is used and freed per loop pass.
13139 */
13140 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13141 if (!mbox) {
13142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13143 "2933 Failed to allocate mbox cmd "
13144 "memory\n");
13145 return -ENOMEM;
13146 }
13147 alloclen = lpfc_sli4_config(phba, mbox,
13148 LPFC_MBOX_SUBSYSTEM_FCOE,
13149 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13150 reqlen,
13151 LPFC_SLI4_MBX_NEMBED);
13152 if (alloclen < reqlen) {
13153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13154 "2934 Allocated DMA memory size (%d) "
13155 "is less than the requested DMA memory "
13156 "size (%d)\n", alloclen, reqlen);
13157 lpfc_sli4_mbox_cmd_free(phba, mbox);
13158 return -ENOMEM;
13159 }
13160
13161 /* Get the first SGE entry from the non-embedded DMA memory */
13162 viraddr = mbox->sge_array->addr[0];
13163
13164 /* Set up the SGL pages in the non-embedded DMA pages */
13165 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13166 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13167
13168 /* pg_pairs tracks posted SGEs per loop iteration. */
13169 pg_pairs = 0;
13170 list_for_each_entry_continue(psb, sblist, list) {
13171 /* Set up the sge entry */
13172 sgl_pg_pairs->sgl_pg0_addr_lo =
13173 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13174 sgl_pg_pairs->sgl_pg0_addr_hi =
13175 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13176 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13177 pdma_phys_bpl1 = psb->dma_phys_bpl +
13178 SGL_PAGE_SIZE;
13179 else
13180 pdma_phys_bpl1 = 0;
13181 sgl_pg_pairs->sgl_pg1_addr_lo =
13182 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13183 sgl_pg_pairs->sgl_pg1_addr_hi =
13184 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13185 /* Keep the first xri for this extent. */
13186 if (pg_pairs == 0)
13187 xri_start = psb->cur_iocbq.sli4_xritag;
13188 sgl_pg_pairs++;
13189 pg_pairs++;
13190 xri_cnt++;
13191
13192 /*
13193 * Track two exit conditions - the loop has constructed
13194 * all of the caller's SGE pairs or all available
13195 * resource IDs in this extent are consumed.
13196 */
13197 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13198 break;
13199 }
13200 rsrc_blk->rsrc_used += pg_pairs;
13201 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13202 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13203
13204 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13205 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13206 "blk use %d\n",
13207 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13208 /* Perform endian conversion if necessary */
13209 sgl->word0 = cpu_to_le32(sgl->word0);
13210 if (!phba->sli4_hba.intr_enable)
13211 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13212 else {
a183a15f 13213 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
13214 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13215 }
13216 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13217 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13218 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13219 &shdr->response);
13220 if (rc != MBX_TIMEOUT)
13221 lpfc_sli4_mbox_cmd_free(phba, mbox);
13222 if (shdr_status || shdr_add_status || rc) {
13223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13224 "2935 POST_SGL_BLOCK mailbox command "
13225 "failed status x%x add_status x%x "
13226 "mbx status x%x\n",
13227 shdr_status, shdr_add_status, rc);
13228 return -ENXIO;
13229 }
13230
13231 /* Post only what is requested. */
13232 if (xri_cnt >= cnt)
13233 break;
13234 }
13235 return rc;
13236}
13237
4f774513
JS
13238/**
13239 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13240 * @phba: pointer to lpfc_hba struct that the frame was received on
13241 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13242 *
13243 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13244 * valid type of frame that the LPFC driver will handle. This function will
13245 * return a zero if the frame is a valid frame or a non zero value when the
13246 * frame does not pass the check.
13247 **/
13248static int
13249lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13250{
474ffb74
TH
13251 /* make rctl_names static to save stack space */
13252 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
4f774513
JS
13253 char *type_names[] = FC_TYPE_NAMES_INIT;
13254 struct fc_vft_header *fc_vft_hdr;
546fc854 13255 uint32_t *header = (uint32_t *) fc_hdr;
4f774513
JS
13256
13257 switch (fc_hdr->fh_r_ctl) {
13258 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13259 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13260 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13261 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13262 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13263 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13264 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13265 case FC_RCTL_DD_CMD_STATUS: /* command status */
13266 case FC_RCTL_ELS_REQ: /* extended link services request */
13267 case FC_RCTL_ELS_REP: /* extended link services reply */
13268 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13269 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13270 case FC_RCTL_BA_NOP: /* basic link service NOP */
13271 case FC_RCTL_BA_ABTS: /* basic link service abort */
13272 case FC_RCTL_BA_RMC: /* remove connection */
13273 case FC_RCTL_BA_ACC: /* basic accept */
13274 case FC_RCTL_BA_RJT: /* basic reject */
13275 case FC_RCTL_BA_PRMT:
13276 case FC_RCTL_ACK_1: /* acknowledge_1 */
13277 case FC_RCTL_ACK_0: /* acknowledge_0 */
13278 case FC_RCTL_P_RJT: /* port reject */
13279 case FC_RCTL_F_RJT: /* fabric reject */
13280 case FC_RCTL_P_BSY: /* port busy */
13281 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13282 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13283 case FC_RCTL_LCR: /* link credit reset */
13284 case FC_RCTL_END: /* end */
13285 break;
13286 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13287 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13288 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13289 return lpfc_fc_frame_check(phba, fc_hdr);
13290 default:
13291 goto drop;
13292 }
13293 switch (fc_hdr->fh_type) {
13294 case FC_TYPE_BLS:
13295 case FC_TYPE_ELS:
13296 case FC_TYPE_FCP:
13297 case FC_TYPE_CT:
13298 break;
13299 case FC_TYPE_IP:
13300 case FC_TYPE_ILS:
13301 default:
13302 goto drop;
13303 }
546fc854 13304
4f774513 13305 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
546fc854
JS
13306 "2538 Received frame rctl:%s type:%s "
13307 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
4f774513 13308 rctl_names[fc_hdr->fh_r_ctl],
546fc854
JS
13309 type_names[fc_hdr->fh_type],
13310 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13311 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13312 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
4f774513
JS
13313 return 0;
13314drop:
13315 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13316 "2539 Dropped frame rctl:%s type:%s\n",
13317 rctl_names[fc_hdr->fh_r_ctl],
13318 type_names[fc_hdr->fh_type]);
13319 return 1;
13320}
13321
13322/**
13323 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13324 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13325 *
13326 * This function processes the FC header to retrieve the VFI from the VF
13327 * header, if one exists. This function will return the VFI if one exists
13328 * or 0 if no VSAN Header exists.
13329 **/
13330static uint32_t
13331lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13332{
13333 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13334
13335 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13336 return 0;
13337 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13338}
13339
13340/**
13341 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13342 * @phba: Pointer to the HBA structure to search for the vport on
13343 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13344 * @fcfi: The FC Fabric ID that the frame came from
13345 *
13346 * This function searches the @phba for a vport that matches the content of the
13347 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13348 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13349 * returns the matching vport pointer or NULL if unable to match frame to a
13350 * vport.
13351 **/
13352static struct lpfc_vport *
13353lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13354 uint16_t fcfi)
13355{
13356 struct lpfc_vport **vports;
13357 struct lpfc_vport *vport = NULL;
13358 int i;
13359 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13360 fc_hdr->fh_d_id[1] << 8 |
13361 fc_hdr->fh_d_id[2]);
bf08611b
JS
13362 if (did == Fabric_DID)
13363 return phba->pport;
4f774513
JS
13364 vports = lpfc_create_vport_work_array(phba);
13365 if (vports != NULL)
13366 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13367 if (phba->fcf.fcfi == fcfi &&
13368 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13369 vports[i]->fc_myDID == did) {
13370 vport = vports[i];
13371 break;
13372 }
13373 }
13374 lpfc_destroy_vport_work_array(phba, vports);
13375 return vport;
13376}
13377
45ed1190
JS
13378/**
13379 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13380 * @vport: The vport to work on.
13381 *
13382 * This function updates the receive sequence time stamp for this vport. The
13383 * receive sequence time stamp indicates the time that the last frame of the
13384 * the sequence that has been idle for the longest amount of time was received.
13385 * the driver uses this time stamp to indicate if any received sequences have
13386 * timed out.
13387 **/
13388void
13389lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13390{
13391 struct lpfc_dmabuf *h_buf;
13392 struct hbq_dmabuf *dmabuf = NULL;
13393
13394 /* get the oldest sequence on the rcv list */
13395 h_buf = list_get_first(&vport->rcv_buffer_list,
13396 struct lpfc_dmabuf, list);
13397 if (!h_buf)
13398 return;
13399 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13400 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13401}
13402
13403/**
13404 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13405 * @vport: The vport that the received sequences were sent to.
13406 *
13407 * This function cleans up all outstanding received sequences. This is called
13408 * by the driver when a link event or user action invalidates all the received
13409 * sequences.
13410 **/
13411void
13412lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13413{
13414 struct lpfc_dmabuf *h_buf, *hnext;
13415 struct lpfc_dmabuf *d_buf, *dnext;
13416 struct hbq_dmabuf *dmabuf = NULL;
13417
13418 /* start with the oldest sequence on the rcv list */
13419 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13420 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13421 list_del_init(&dmabuf->hbuf.list);
13422 list_for_each_entry_safe(d_buf, dnext,
13423 &dmabuf->dbuf.list, list) {
13424 list_del_init(&d_buf->list);
13425 lpfc_in_buf_free(vport->phba, d_buf);
13426 }
13427 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13428 }
13429}
13430
13431/**
13432 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13433 * @vport: The vport that the received sequences were sent to.
13434 *
13435 * This function determines whether any received sequences have timed out by
13436 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13437 * indicates that there is at least one timed out sequence this routine will
13438 * go through the received sequences one at a time from most inactive to most
13439 * active to determine which ones need to be cleaned up. Once it has determined
13440 * that a sequence needs to be cleaned up it will simply free up the resources
13441 * without sending an abort.
13442 **/
13443void
13444lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13445{
13446 struct lpfc_dmabuf *h_buf, *hnext;
13447 struct lpfc_dmabuf *d_buf, *dnext;
13448 struct hbq_dmabuf *dmabuf = NULL;
13449 unsigned long timeout;
13450 int abort_count = 0;
13451
13452 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13453 vport->rcv_buffer_time_stamp);
13454 if (list_empty(&vport->rcv_buffer_list) ||
13455 time_before(jiffies, timeout))
13456 return;
13457 /* start with the oldest sequence on the rcv list */
13458 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13459 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13460 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13461 dmabuf->time_stamp);
13462 if (time_before(jiffies, timeout))
13463 break;
13464 abort_count++;
13465 list_del_init(&dmabuf->hbuf.list);
13466 list_for_each_entry_safe(d_buf, dnext,
13467 &dmabuf->dbuf.list, list) {
13468 list_del_init(&d_buf->list);
13469 lpfc_in_buf_free(vport->phba, d_buf);
13470 }
13471 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13472 }
13473 if (abort_count)
13474 lpfc_update_rcv_time_stamp(vport);
13475}
13476
4f774513
JS
13477/**
13478 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13479 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13480 *
13481 * This function searches through the existing incomplete sequences that have
13482 * been sent to this @vport. If the frame matches one of the incomplete
13483 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13484 * make up that sequence. If no sequence is found that matches this frame then
13485 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13486 * This function returns a pointer to the first dmabuf in the sequence list that
13487 * the frame was linked to.
13488 **/
13489static struct hbq_dmabuf *
13490lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13491{
13492 struct fc_frame_header *new_hdr;
13493 struct fc_frame_header *temp_hdr;
13494 struct lpfc_dmabuf *d_buf;
13495 struct lpfc_dmabuf *h_buf;
13496 struct hbq_dmabuf *seq_dmabuf = NULL;
13497 struct hbq_dmabuf *temp_dmabuf = NULL;
13498
4d9ab994 13499 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 13500 dmabuf->time_stamp = jiffies;
4f774513
JS
13501 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13502 /* Use the hdr_buf to find the sequence that this frame belongs to */
13503 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13504 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13505 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13506 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13507 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13508 continue;
13509 /* found a pending sequence that matches this frame */
13510 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13511 break;
13512 }
13513 if (!seq_dmabuf) {
13514 /*
13515 * This indicates first frame received for this sequence.
13516 * Queue the buffer on the vport's rcv_buffer_list.
13517 */
13518 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 13519 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
13520 return dmabuf;
13521 }
13522 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
13523 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13524 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
13525 list_del_init(&seq_dmabuf->hbuf.list);
13526 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13527 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 13528 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
13529 return dmabuf;
13530 }
45ed1190
JS
13531 /* move this sequence to the tail to indicate a young sequence */
13532 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13533 seq_dmabuf->time_stamp = jiffies;
13534 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
13535 if (list_empty(&seq_dmabuf->dbuf.list)) {
13536 temp_hdr = dmabuf->hbuf.virt;
13537 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13538 return seq_dmabuf;
13539 }
4f774513
JS
13540 /* find the correct place in the sequence to insert this frame */
13541 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13542 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13543 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13544 /*
13545 * If the frame's sequence count is greater than the frame on
13546 * the list then insert the frame right after this frame
13547 */
eeead811
JS
13548 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13549 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
13550 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13551 return seq_dmabuf;
13552 }
13553 }
13554 return NULL;
13555}
13556
6669f9bb
JS
13557/**
13558 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13559 * @vport: pointer to a vitural port
13560 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13561 *
13562 * This function tries to abort from the partially assembed sequence, described
13563 * by the information from basic abbort @dmabuf. It checks to see whether such
13564 * partially assembled sequence held by the driver. If so, it shall free up all
13565 * the frames from the partially assembled sequence.
13566 *
13567 * Return
13568 * true -- if there is matching partially assembled sequence present and all
13569 * the frames freed with the sequence;
13570 * false -- if there is no matching partially assembled sequence present so
13571 * nothing got aborted in the lower layer driver
13572 **/
13573static bool
13574lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13575 struct hbq_dmabuf *dmabuf)
13576{
13577 struct fc_frame_header *new_hdr;
13578 struct fc_frame_header *temp_hdr;
13579 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13580 struct hbq_dmabuf *seq_dmabuf = NULL;
13581
13582 /* Use the hdr_buf to find the sequence that matches this frame */
13583 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13584 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13585 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13586 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13587 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13588 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13589 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13590 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13591 continue;
13592 /* found a pending sequence that matches this frame */
13593 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13594 break;
13595 }
13596
13597 /* Free up all the frames from the partially assembled sequence */
13598 if (seq_dmabuf) {
13599 list_for_each_entry_safe(d_buf, n_buf,
13600 &seq_dmabuf->dbuf.list, list) {
13601 list_del_init(&d_buf->list);
13602 lpfc_in_buf_free(vport->phba, d_buf);
13603 }
13604 return true;
13605 }
13606 return false;
13607}
13608
13609/**
546fc854 13610 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
13611 * @phba: Pointer to HBA context object.
13612 * @cmd_iocbq: pointer to the command iocbq structure.
13613 * @rsp_iocbq: pointer to the response iocbq structure.
13614 *
546fc854 13615 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
13616 * event. It properly releases the memory allocated to the sequence abort
13617 * accept iocb.
13618 **/
13619static void
546fc854 13620lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
13621 struct lpfc_iocbq *cmd_iocbq,
13622 struct lpfc_iocbq *rsp_iocbq)
13623{
13624 if (cmd_iocbq)
13625 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13626}
13627
6d368e53
JS
13628/**
13629 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13630 * @phba: Pointer to HBA context object.
13631 * @xri: xri id in transaction.
13632 *
13633 * This function validates the xri maps to the known range of XRIs allocated an
13634 * used by the driver.
13635 **/
7851fe2c 13636uint16_t
6d368e53
JS
13637lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13638 uint16_t xri)
13639{
13640 int i;
13641
13642 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13643 if (xri == phba->sli4_hba.xri_ids[i])
13644 return i;
13645 }
13646 return NO_XRI;
13647}
13648
13649
6669f9bb 13650/**
546fc854 13651 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
13652 * @phba: Pointer to HBA context object.
13653 * @fc_hdr: pointer to a FC frame header.
13654 *
546fc854 13655 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
13656 * event after aborting the sequence handling.
13657 **/
13658static void
546fc854 13659lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
6669f9bb
JS
13660 struct fc_frame_header *fc_hdr)
13661{
13662 struct lpfc_iocbq *ctiocb = NULL;
13663 struct lpfc_nodelist *ndlp;
5ffc266e
JS
13664 uint16_t oxid, rxid;
13665 uint32_t sid, fctl;
6669f9bb 13666 IOCB_t *icmd;
546fc854 13667 int rc;
6669f9bb
JS
13668
13669 if (!lpfc_is_link_up(phba))
13670 return;
13671
13672 sid = sli4_sid_from_fc_hdr(fc_hdr);
13673 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 13674 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb
JS
13675
13676 ndlp = lpfc_findnode_did(phba->pport, sid);
13677 if (!ndlp) {
13678 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13679 "1268 Find ndlp returned NULL for oxid:x%x "
13680 "SID:x%x\n", oxid, sid);
13681 return;
13682 }
6d368e53 13683 if (lpfc_sli4_xri_inrange(phba, rxid))
19ca7609 13684 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
6669f9bb 13685
546fc854 13686 /* Allocate buffer for rsp iocb */
6669f9bb
JS
13687 ctiocb = lpfc_sli_get_iocbq(phba);
13688 if (!ctiocb)
13689 return;
13690
5ffc266e
JS
13691 /* Extract the F_CTL field from FC_HDR */
13692 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13693
6669f9bb 13694 icmd = &ctiocb->iocb;
6669f9bb 13695 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 13696 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
13697 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13698 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13699 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13700
13701 /* Fill in the rest of iocb fields */
13702 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13703 icmd->ulpBdeCount = 0;
13704 icmd->ulpLe = 1;
13705 icmd->ulpClass = CLASS3;
6d368e53 13706 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
be858b65 13707 ctiocb->context1 = ndlp;
6669f9bb 13708
6669f9bb
JS
13709 ctiocb->iocb_cmpl = NULL;
13710 ctiocb->vport = phba->pport;
546fc854 13711 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 13712 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
13713 ctiocb->sli4_xritag = NO_XRI;
13714
13715 /* If the oxid maps to the FCP XRI range or if it is out of range,
13716 * send a BLS_RJT. The driver no longer has that exchange.
13717 * Override the IOCB for a BA_RJT.
13718 */
13719 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13720 phba->sli4_hba.max_cfg_param.xri_base) ||
13721 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13722 phba->sli4_hba.max_cfg_param.xri_base)) {
13723 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13724 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13725 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13726 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13727 }
6669f9bb 13728
5ffc266e
JS
13729 if (fctl & FC_FC_EX_CTX) {
13730 /* ABTS sent by responder to CT exchange, construction
13731 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13732 * field and RX_ID from ABTS for RX_ID field.
13733 */
546fc854
JS
13734 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13735 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
5ffc266e
JS
13736 } else {
13737 /* ABTS sent by initiator to CT exchange, construction
13738 * of BA_ACC will need to allocate a new XRI as for the
13739 * XRI_TAG and RX_ID fields.
13740 */
546fc854
JS
13741 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13742 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
5ffc266e 13743 }
546fc854 13744 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 13745
546fc854 13746 /* Xmit CT abts response on exchange <xid> */
6669f9bb 13747 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
546fc854
JS
13748 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13749 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13750
13751 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13752 if (rc == IOCB_ERROR) {
13753 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13754 "2925 Failed to issue CT ABTS RSP x%x on "
13755 "xri x%x, Data x%x\n",
13756 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13757 phba->link_state);
13758 lpfc_sli_release_iocbq(phba, ctiocb);
13759 }
6669f9bb
JS
13760}
13761
13762/**
13763 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13764 * @vport: Pointer to the vport on which this sequence was received
13765 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13766 *
13767 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13768 * receive sequence is only partially assembed by the driver, it shall abort
13769 * the partially assembled frames for the sequence. Otherwise, if the
13770 * unsolicited receive sequence has been completely assembled and passed to
13771 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13772 * unsolicited sequence has been aborted. After that, it will issue a basic
13773 * accept to accept the abort.
13774 **/
13775void
13776lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13777 struct hbq_dmabuf *dmabuf)
13778{
13779 struct lpfc_hba *phba = vport->phba;
13780 struct fc_frame_header fc_hdr;
5ffc266e 13781 uint32_t fctl;
6669f9bb
JS
13782 bool abts_par;
13783
6669f9bb
JS
13784 /* Make a copy of fc_hdr before the dmabuf being released */
13785 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 13786 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 13787
5ffc266e
JS
13788 if (fctl & FC_FC_EX_CTX) {
13789 /*
13790 * ABTS sent by responder to exchange, just free the buffer
13791 */
6669f9bb 13792 lpfc_in_buf_free(phba, &dmabuf->dbuf);
5ffc266e
JS
13793 } else {
13794 /*
13795 * ABTS sent by initiator to exchange, need to do cleanup
13796 */
13797 /* Try to abort partially assembled seq */
13798 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
13799
13800 /* Send abort to ULP if partially seq abort failed */
13801 if (abts_par == false)
13802 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
13803 else
13804 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13805 }
6669f9bb 13806 /* Send basic accept (BA_ACC) to the abort requester */
546fc854 13807 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
6669f9bb
JS
13808}
13809
4f774513
JS
13810/**
13811 * lpfc_seq_complete - Indicates if a sequence is complete
13812 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13813 *
13814 * This function checks the sequence, starting with the frame described by
13815 * @dmabuf, to see if all the frames associated with this sequence are present.
13816 * the frames associated with this sequence are linked to the @dmabuf using the
13817 * dbuf list. This function looks for two major things. 1) That the first frame
13818 * has a sequence count of zero. 2) There is a frame with last frame of sequence
13819 * set. 3) That there are no holes in the sequence count. The function will
13820 * return 1 when the sequence is complete, otherwise it will return 0.
13821 **/
13822static int
13823lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
13824{
13825 struct fc_frame_header *hdr;
13826 struct lpfc_dmabuf *d_buf;
13827 struct hbq_dmabuf *seq_dmabuf;
13828 uint32_t fctl;
13829 int seq_count = 0;
13830
13831 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13832 /* make sure first fame of sequence has a sequence count of zero */
13833 if (hdr->fh_seq_cnt != seq_count)
13834 return 0;
13835 fctl = (hdr->fh_f_ctl[0] << 16 |
13836 hdr->fh_f_ctl[1] << 8 |
13837 hdr->fh_f_ctl[2]);
13838 /* If last frame of sequence we can return success. */
13839 if (fctl & FC_FC_END_SEQ)
13840 return 1;
13841 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
13842 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13843 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13844 /* If there is a hole in the sequence count then fail. */
eeead811 13845 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
13846 return 0;
13847 fctl = (hdr->fh_f_ctl[0] << 16 |
13848 hdr->fh_f_ctl[1] << 8 |
13849 hdr->fh_f_ctl[2]);
13850 /* If last frame of sequence we can return success. */
13851 if (fctl & FC_FC_END_SEQ)
13852 return 1;
13853 }
13854 return 0;
13855}
13856
13857/**
13858 * lpfc_prep_seq - Prep sequence for ULP processing
13859 * @vport: Pointer to the vport on which this sequence was received
13860 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13861 *
13862 * This function takes a sequence, described by a list of frames, and creates
13863 * a list of iocbq structures to describe the sequence. This iocbq list will be
13864 * used to issue to the generic unsolicited sequence handler. This routine
13865 * returns a pointer to the first iocbq in the list. If the function is unable
13866 * to allocate an iocbq then it throw out the received frames that were not
13867 * able to be described and return a pointer to the first iocbq. If unable to
13868 * allocate any iocbqs (including the first) this function will return NULL.
13869 **/
13870static struct lpfc_iocbq *
13871lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
13872{
7851fe2c 13873 struct hbq_dmabuf *hbq_buf;
4f774513
JS
13874 struct lpfc_dmabuf *d_buf, *n_buf;
13875 struct lpfc_iocbq *first_iocbq, *iocbq;
13876 struct fc_frame_header *fc_hdr;
13877 uint32_t sid;
7851fe2c 13878 uint32_t len, tot_len;
eeead811 13879 struct ulp_bde64 *pbde;
4f774513
JS
13880
13881 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13882 /* remove from receive buffer list */
13883 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 13884 lpfc_update_rcv_time_stamp(vport);
4f774513 13885 /* get the Remote Port's SID */
6669f9bb 13886 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 13887 tot_len = 0;
4f774513
JS
13888 /* Get an iocbq struct to fill in. */
13889 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
13890 if (first_iocbq) {
13891 /* Initialize the first IOCB. */
8fa38513 13892 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
13893 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
13894 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
13895 first_iocbq->iocb.ulpContext = NO_XRI;
13896 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
13897 be16_to_cpu(fc_hdr->fh_ox_id);
13898 /* iocbq is prepped for internal consumption. Physical vpi. */
13899 first_iocbq->iocb.unsli3.rcvsli3.vpi =
13900 vport->phba->vpi_ids[vport->vpi];
4f774513
JS
13901 /* put the first buffer into the first IOCBq */
13902 first_iocbq->context2 = &seq_dmabuf->dbuf;
13903 first_iocbq->context3 = NULL;
13904 first_iocbq->iocb.ulpBdeCount = 1;
13905 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13906 LPFC_DATA_BUF_SIZE;
13907 first_iocbq->iocb.un.rcvels.remoteID = sid;
7851fe2c 13908 tot_len = bf_get(lpfc_rcqe_length,
4d9ab994 13909 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
7851fe2c 13910 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
13911 }
13912 iocbq = first_iocbq;
13913 /*
13914 * Each IOCBq can have two Buffers assigned, so go through the list
13915 * of buffers for this sequence and save two buffers in each IOCBq
13916 */
13917 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
13918 if (!iocbq) {
13919 lpfc_in_buf_free(vport->phba, d_buf);
13920 continue;
13921 }
13922 if (!iocbq->context3) {
13923 iocbq->context3 = d_buf;
13924 iocbq->iocb.ulpBdeCount++;
eeead811
JS
13925 pbde = (struct ulp_bde64 *)
13926 &iocbq->iocb.unsli3.sli3Words[4];
13927 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
7851fe2c
JS
13928
13929 /* We need to get the size out of the right CQE */
13930 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13931 len = bf_get(lpfc_rcqe_length,
13932 &hbq_buf->cq_event.cqe.rcqe_cmpl);
13933 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
13934 tot_len += len;
4f774513
JS
13935 } else {
13936 iocbq = lpfc_sli_get_iocbq(vport->phba);
13937 if (!iocbq) {
13938 if (first_iocbq) {
13939 first_iocbq->iocb.ulpStatus =
13940 IOSTAT_FCP_RSP_ERROR;
13941 first_iocbq->iocb.un.ulpWord[4] =
13942 IOERR_NO_RESOURCES;
13943 }
13944 lpfc_in_buf_free(vport->phba, d_buf);
13945 continue;
13946 }
13947 iocbq->context2 = d_buf;
13948 iocbq->context3 = NULL;
13949 iocbq->iocb.ulpBdeCount = 1;
13950 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13951 LPFC_DATA_BUF_SIZE;
7851fe2c
JS
13952
13953 /* We need to get the size out of the right CQE */
13954 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13955 len = bf_get(lpfc_rcqe_length,
13956 &hbq_buf->cq_event.cqe.rcqe_cmpl);
13957 tot_len += len;
13958 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
13959
4f774513
JS
13960 iocbq->iocb.un.rcvels.remoteID = sid;
13961 list_add_tail(&iocbq->list, &first_iocbq->list);
13962 }
13963 }
13964 return first_iocbq;
13965}
13966
6669f9bb
JS
13967static void
13968lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
13969 struct hbq_dmabuf *seq_dmabuf)
13970{
13971 struct fc_frame_header *fc_hdr;
13972 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
13973 struct lpfc_hba *phba = vport->phba;
13974
13975 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13976 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
13977 if (!iocbq) {
13978 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13979 "2707 Ring %d handler: Failed to allocate "
13980 "iocb Rctl x%x Type x%x received\n",
13981 LPFC_ELS_RING,
13982 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13983 return;
13984 }
13985 if (!lpfc_complete_unsol_iocb(phba,
13986 &phba->sli.ring[LPFC_ELS_RING],
13987 iocbq, fc_hdr->fh_r_ctl,
13988 fc_hdr->fh_type))
6d368e53 13989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
13990 "2540 Ring %d handler: unexpected Rctl "
13991 "x%x Type x%x received\n",
13992 LPFC_ELS_RING,
13993 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13994
13995 /* Free iocb created in lpfc_prep_seq */
13996 list_for_each_entry_safe(curr_iocb, next_iocb,
13997 &iocbq->list, list) {
13998 list_del_init(&curr_iocb->list);
13999 lpfc_sli_release_iocbq(phba, curr_iocb);
14000 }
14001 lpfc_sli_release_iocbq(phba, iocbq);
14002}
14003
4f774513
JS
14004/**
14005 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14006 * @phba: Pointer to HBA context object.
14007 *
14008 * This function is called with no lock held. This function processes all
14009 * the received buffers and gives it to upper layers when a received buffer
14010 * indicates that it is the final frame in the sequence. The interrupt
14011 * service routine processes received buffers at interrupt contexts and adds
14012 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14013 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14014 * appropriate receive function when the final frame in a sequence is received.
14015 **/
4d9ab994
JS
14016void
14017lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14018 struct hbq_dmabuf *dmabuf)
4f774513 14019{
4d9ab994 14020 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
14021 struct fc_frame_header *fc_hdr;
14022 struct lpfc_vport *vport;
14023 uint32_t fcfi;
4f774513 14024
4f774513 14025 /* Process each received buffer */
4d9ab994
JS
14026 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14027 /* check to see if this a valid type of frame */
14028 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14029 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14030 return;
14031 }
7851fe2c
JS
14032 if ((bf_get(lpfc_cqe_code,
14033 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14034 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14035 &dmabuf->cq_event.cqe.rcqe_cmpl);
14036 else
14037 fcfi = bf_get(lpfc_rcqe_fcf_id,
14038 &dmabuf->cq_event.cqe.rcqe_cmpl);
4d9ab994 14039 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
c868595d 14040 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
4d9ab994
JS
14041 /* throw out the frame */
14042 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14043 return;
14044 }
6669f9bb
JS
14045 /* Handle the basic abort sequence (BA_ABTS) event */
14046 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14047 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14048 return;
14049 }
14050
4d9ab994
JS
14051 /* Link this frame */
14052 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14053 if (!seq_dmabuf) {
14054 /* unable to add frame to vport - throw it out */
14055 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14056 return;
14057 }
14058 /* If not last frame in sequence continue processing frames. */
def9c7a9 14059 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 14060 return;
def9c7a9 14061
6669f9bb
JS
14062 /* Send the complete sequence to the upper layer protocol */
14063 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 14064}
6fb120a7
JS
14065
14066/**
14067 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14068 * @phba: pointer to lpfc hba data structure.
14069 *
14070 * This routine is invoked to post rpi header templates to the
14071 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
14072 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14073 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
14074 *
14075 * This routine does not require any locks. It's usage is expected
14076 * to be driver load or reset recovery when the driver is
14077 * sequential.
14078 *
14079 * Return codes
af901ca1 14080 * 0 - successful
d439d286 14081 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
14082 * When this error occurs, the driver is not guaranteed
14083 * to have any rpi regions posted to the device and
14084 * must either attempt to repost the regions or take a
14085 * fatal error.
14086 **/
14087int
14088lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14089{
14090 struct lpfc_rpi_hdr *rpi_page;
14091 uint32_t rc = 0;
6d368e53
JS
14092 uint16_t lrpi = 0;
14093
14094 /* SLI4 ports that support extents do not require RPI headers. */
14095 if (!phba->sli4_hba.rpi_hdrs_in_use)
14096 goto exit;
14097 if (phba->sli4_hba.extents_in_use)
14098 return -EIO;
6fb120a7 14099
6fb120a7 14100 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
14101 /*
14102 * Assign the rpi headers a physical rpi only if the driver
14103 * has not initialized those resources. A port reset only
14104 * needs the headers posted.
14105 */
14106 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14107 LPFC_RPI_RSRC_RDY)
14108 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14109
6fb120a7
JS
14110 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14111 if (rc != MBX_SUCCESS) {
14112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14113 "2008 Error %d posting all rpi "
14114 "headers\n", rc);
14115 rc = -EIO;
14116 break;
14117 }
14118 }
14119
6d368e53
JS
14120 exit:
14121 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14122 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
14123 return rc;
14124}
14125
14126/**
14127 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14128 * @phba: pointer to lpfc hba data structure.
14129 * @rpi_page: pointer to the rpi memory region.
14130 *
14131 * This routine is invoked to post a single rpi header to the
14132 * HBA consistent with the SLI-4 interface spec. This memory region
14133 * maps up to 64 rpi context regions.
14134 *
14135 * Return codes
af901ca1 14136 * 0 - successful
d439d286
JS
14137 * -ENOMEM - No available memory
14138 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
14139 **/
14140int
14141lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14142{
14143 LPFC_MBOXQ_t *mboxq;
14144 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14145 uint32_t rc = 0;
6fb120a7
JS
14146 uint32_t shdr_status, shdr_add_status;
14147 union lpfc_sli4_cfg_shdr *shdr;
14148
6d368e53
JS
14149 /* SLI4 ports that support extents do not require RPI headers. */
14150 if (!phba->sli4_hba.rpi_hdrs_in_use)
14151 return rc;
14152 if (phba->sli4_hba.extents_in_use)
14153 return -EIO;
14154
6fb120a7
JS
14155 /* The port is notified of the header region via a mailbox command. */
14156 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14157 if (!mboxq) {
14158 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14159 "2001 Unable to allocate memory for issuing "
14160 "SLI_CONFIG_SPECIAL mailbox command\n");
14161 return -ENOMEM;
14162 }
14163
14164 /* Post all rpi memory regions to the port. */
14165 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
14166 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14167 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14168 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
14169 sizeof(struct lpfc_sli4_cfg_mhdr),
14170 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
14171
14172
14173 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
14174 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14175 rpi_page->start_rpi);
6d368e53
JS
14176 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14177 hdr_tmpl, rpi_page->page_count);
14178
6fb120a7
JS
14179 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14180 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 14181 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
14182 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14183 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14184 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14185 if (rc != MBX_TIMEOUT)
14186 mempool_free(mboxq, phba->mbox_mem_pool);
14187 if (shdr_status || shdr_add_status || rc) {
14188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14189 "2514 POST_RPI_HDR mailbox failed with "
14190 "status x%x add_status x%x, mbx status x%x\n",
14191 shdr_status, shdr_add_status, rc);
14192 rc = -ENXIO;
14193 }
14194 return rc;
14195}
14196
14197/**
14198 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14199 * @phba: pointer to lpfc hba data structure.
14200 *
14201 * This routine is invoked to post rpi header templates to the
14202 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
14203 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14204 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
14205 *
14206 * Returns
af901ca1 14207 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
14208 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14209 **/
14210int
14211lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14212{
6d368e53
JS
14213 unsigned long rpi;
14214 uint16_t max_rpi, rpi_limit;
14215 uint16_t rpi_remaining, lrpi = 0;
6fb120a7
JS
14216 struct lpfc_rpi_hdr *rpi_hdr;
14217
14218 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6fb120a7
JS
14219 rpi_limit = phba->sli4_hba.next_rpi;
14220
14221 /*
6d368e53
JS
14222 * Fetch the next logical rpi. Because this index is logical,
14223 * the driver starts at 0 each time.
6fb120a7
JS
14224 */
14225 spin_lock_irq(&phba->hbalock);
6d368e53
JS
14226 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14227 if (rpi >= rpi_limit)
6fb120a7
JS
14228 rpi = LPFC_RPI_ALLOC_ERROR;
14229 else {
14230 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14231 phba->sli4_hba.max_cfg_param.rpi_used++;
14232 phba->sli4_hba.rpi_count++;
14233 }
14234
14235 /*
14236 * Don't try to allocate more rpi header regions if the device limit
6d368e53 14237 * has been exhausted.
6fb120a7
JS
14238 */
14239 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14240 (phba->sli4_hba.rpi_count >= max_rpi)) {
14241 spin_unlock_irq(&phba->hbalock);
14242 return rpi;
14243 }
14244
6d368e53
JS
14245 /*
14246 * RPI header postings are not required for SLI4 ports capable of
14247 * extents.
14248 */
14249 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14250 spin_unlock_irq(&phba->hbalock);
14251 return rpi;
14252 }
14253
6fb120a7
JS
14254 /*
14255 * If the driver is running low on rpi resources, allocate another
14256 * page now. Note that the next_rpi value is used because
14257 * it represents how many are actually in use whereas max_rpi notes
14258 * how many are supported max by the device.
14259 */
6d368e53 14260 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
6fb120a7
JS
14261 spin_unlock_irq(&phba->hbalock);
14262 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14263 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14264 if (!rpi_hdr) {
14265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14266 "2002 Error Could not grow rpi "
14267 "count\n");
14268 } else {
6d368e53
JS
14269 lrpi = rpi_hdr->start_rpi;
14270 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
14271 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14272 }
14273 }
14274
14275 return rpi;
14276}
14277
d7c47992
JS
14278/**
14279 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14280 * @phba: pointer to lpfc hba data structure.
14281 *
14282 * This routine is invoked to release an rpi to the pool of
14283 * available rpis maintained by the driver.
14284 **/
14285void
14286__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14287{
14288 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14289 phba->sli4_hba.rpi_count--;
14290 phba->sli4_hba.max_cfg_param.rpi_used--;
14291 }
14292}
14293
6fb120a7
JS
14294/**
14295 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14296 * @phba: pointer to lpfc hba data structure.
14297 *
14298 * This routine is invoked to release an rpi to the pool of
14299 * available rpis maintained by the driver.
14300 **/
14301void
14302lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14303{
14304 spin_lock_irq(&phba->hbalock);
d7c47992 14305 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
14306 spin_unlock_irq(&phba->hbalock);
14307}
14308
14309/**
14310 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14311 * @phba: pointer to lpfc hba data structure.
14312 *
14313 * This routine is invoked to remove the memory region that
14314 * provided rpi via a bitmask.
14315 **/
14316void
14317lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14318{
14319 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
14320 kfree(phba->sli4_hba.rpi_ids);
14321 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
14322}
14323
14324/**
14325 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14326 * @phba: pointer to lpfc hba data structure.
14327 *
14328 * This routine is invoked to remove the memory region that
14329 * provided rpi via a bitmask.
14330 **/
14331int
14332lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14333{
14334 LPFC_MBOXQ_t *mboxq;
14335 struct lpfc_hba *phba = ndlp->phba;
14336 int rc;
14337
14338 /* The port is notified of the header region via a mailbox command. */
14339 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14340 if (!mboxq)
14341 return -ENOMEM;
14342
14343 /* Post all rpi memory regions to the port. */
14344 lpfc_resume_rpi(mboxq, ndlp);
14345 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14346 if (rc == MBX_NOT_FINISHED) {
14347 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14348 "2010 Resume RPI Mailbox failed "
14349 "status %d, mbxStatus x%x\n", rc,
14350 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14351 mempool_free(mboxq, phba->mbox_mem_pool);
14352 return -EIO;
14353 }
14354 return 0;
14355}
14356
14357/**
14358 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 14359 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 14360 *
76a95d75 14361 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
14362 *
14363 * Returns:
14364 * 0 success
14365 * -Evalue otherwise
14366 **/
14367int
76a95d75 14368lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
14369{
14370 LPFC_MBOXQ_t *mboxq;
14371 int rc = 0;
6a9c52cf 14372 int retval = MBX_SUCCESS;
6fb120a7 14373 uint32_t mbox_tmo;
76a95d75 14374 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
14375 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14376 if (!mboxq)
14377 return -ENOMEM;
76a95d75 14378 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 14379 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 14380 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 14381 if (rc != MBX_SUCCESS) {
76a95d75 14382 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
14383 "2022 INIT VPI Mailbox failed "
14384 "status %d, mbxStatus x%x\n", rc,
14385 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 14386 retval = -EIO;
6fb120a7 14387 }
6a9c52cf 14388 if (rc != MBX_TIMEOUT)
76a95d75 14389 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
14390
14391 return retval;
6fb120a7
JS
14392}
14393
14394/**
14395 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14396 * @phba: pointer to lpfc hba data structure.
14397 * @mboxq: Pointer to mailbox object.
14398 *
14399 * This routine is invoked to manually add a single FCF record. The caller
14400 * must pass a completely initialized FCF_Record. This routine takes
14401 * care of the nonembedded mailbox operations.
14402 **/
14403static void
14404lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14405{
14406 void *virt_addr;
14407 union lpfc_sli4_cfg_shdr *shdr;
14408 uint32_t shdr_status, shdr_add_status;
14409
14410 virt_addr = mboxq->sge_array->addr[0];
14411 /* The IOCTL status is embedded in the mailbox subheader. */
14412 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14413 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14414 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14415
14416 if ((shdr_status || shdr_add_status) &&
14417 (shdr_status != STATUS_FCF_IN_USE))
14418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14419 "2558 ADD_FCF_RECORD mailbox failed with "
14420 "status x%x add_status x%x\n",
14421 shdr_status, shdr_add_status);
14422
14423 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14424}
14425
14426/**
14427 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14428 * @phba: pointer to lpfc hba data structure.
14429 * @fcf_record: pointer to the initialized fcf record to add.
14430 *
14431 * This routine is invoked to manually add a single FCF record. The caller
14432 * must pass a completely initialized FCF_Record. This routine takes
14433 * care of the nonembedded mailbox operations.
14434 **/
14435int
14436lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14437{
14438 int rc = 0;
14439 LPFC_MBOXQ_t *mboxq;
14440 uint8_t *bytep;
14441 void *virt_addr;
14442 dma_addr_t phys_addr;
14443 struct lpfc_mbx_sge sge;
14444 uint32_t alloc_len, req_len;
14445 uint32_t fcfindex;
14446
14447 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14448 if (!mboxq) {
14449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14450 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14451 return -ENOMEM;
14452 }
14453
14454 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14455 sizeof(uint32_t);
14456
14457 /* Allocate DMA memory and set up the non-embedded mailbox command */
14458 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14459 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14460 req_len, LPFC_SLI4_MBX_NEMBED);
14461 if (alloc_len < req_len) {
14462 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14463 "2523 Allocated DMA memory size (x%x) is "
14464 "less than the requested DMA memory "
14465 "size (x%x)\n", alloc_len, req_len);
14466 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14467 return -ENOMEM;
14468 }
14469
14470 /*
14471 * Get the first SGE entry from the non-embedded DMA memory. This
14472 * routine only uses a single SGE.
14473 */
14474 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14475 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
14476 virt_addr = mboxq->sge_array->addr[0];
14477 /*
14478 * Configure the FCF record for FCFI 0. This is the driver's
14479 * hardcoded default and gets used in nonFIP mode.
14480 */
14481 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14482 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14483 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14484
14485 /*
14486 * Copy the fcf_index and the FCF Record Data. The data starts after
14487 * the FCoE header plus word10. The data copy needs to be endian
14488 * correct.
14489 */
14490 bytep += sizeof(uint32_t);
14491 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14492 mboxq->vport = phba->pport;
14493 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14495 if (rc == MBX_NOT_FINISHED) {
14496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14497 "2515 ADD_FCF_RECORD mailbox failed with "
14498 "status 0x%x\n", rc);
14499 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14500 rc = -EIO;
14501 } else
14502 rc = 0;
14503
14504 return rc;
14505}
14506
14507/**
14508 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14509 * @phba: pointer to lpfc hba data structure.
14510 * @fcf_record: pointer to the fcf record to write the default data.
14511 * @fcf_index: FCF table entry index.
14512 *
14513 * This routine is invoked to build the driver's default FCF record. The
14514 * values used are hardcoded. This routine handles memory initialization.
14515 *
14516 **/
14517void
14518lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14519 struct fcf_record *fcf_record,
14520 uint16_t fcf_index)
14521{
14522 memset(fcf_record, 0, sizeof(struct fcf_record));
14523 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14524 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14525 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14526 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14527 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14528 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14529 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14530 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14531 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14532 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14533 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14534 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14535 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 14536 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
14537 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14538 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14539 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14540 /* Set the VLAN bit map */
14541 if (phba->valid_vlan) {
14542 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14543 = 1 << (phba->vlan_id % 8);
14544 }
14545}
14546
14547/**
0c9ab6f5 14548 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
14549 * @phba: pointer to lpfc hba data structure.
14550 * @fcf_index: FCF table entry offset.
14551 *
0c9ab6f5
JS
14552 * This routine is invoked to scan the entire FCF table by reading FCF
14553 * record and processing it one at a time starting from the @fcf_index
14554 * for initial FCF discovery or fast FCF failover rediscovery.
14555 *
25985edc 14556 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 14557 * otherwise.
6fb120a7
JS
14558 **/
14559int
0c9ab6f5 14560lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
14561{
14562 int rc = 0, error;
14563 LPFC_MBOXQ_t *mboxq;
6fb120a7 14564
32b9793f 14565 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
14566 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14567 if (!mboxq) {
14568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14569 "2000 Failed to allocate mbox for "
14570 "READ_FCF cmd\n");
4d9ab994 14571 error = -ENOMEM;
0c9ab6f5 14572 goto fail_fcf_scan;
6fb120a7 14573 }
ecfd03c6 14574 /* Construct the read FCF record mailbox command */
0c9ab6f5 14575 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
14576 if (rc) {
14577 error = -EINVAL;
0c9ab6f5 14578 goto fail_fcf_scan;
6fb120a7 14579 }
ecfd03c6 14580 /* Issue the mailbox command asynchronously */
6fb120a7 14581 mboxq->vport = phba->pport;
0c9ab6f5 14582 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
14583
14584 spin_lock_irq(&phba->hbalock);
14585 phba->hba_flag |= FCF_TS_INPROG;
14586 spin_unlock_irq(&phba->hbalock);
14587
6fb120a7 14588 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 14589 if (rc == MBX_NOT_FINISHED)
6fb120a7 14590 error = -EIO;
ecfd03c6 14591 else {
38b92ef8
JS
14592 /* Reset eligible FCF count for new scan */
14593 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 14594 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 14595 error = 0;
32b9793f 14596 }
0c9ab6f5 14597fail_fcf_scan:
4d9ab994
JS
14598 if (error) {
14599 if (mboxq)
14600 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 14601 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 14602 spin_lock_irq(&phba->hbalock);
a93ff37a 14603 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
14604 spin_unlock_irq(&phba->hbalock);
14605 }
6fb120a7
JS
14606 return error;
14607}
a0c87cbd 14608
0c9ab6f5 14609/**
a93ff37a 14610 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
14611 * @phba: pointer to lpfc hba data structure.
14612 * @fcf_index: FCF table entry offset.
14613 *
14614 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 14615 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 14616 *
25985edc 14617 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
14618 * otherwise.
14619 **/
14620int
14621lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14622{
14623 int rc = 0, error;
14624 LPFC_MBOXQ_t *mboxq;
14625
14626 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14627 if (!mboxq) {
14628 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14629 "2763 Failed to allocate mbox for "
14630 "READ_FCF cmd\n");
14631 error = -ENOMEM;
14632 goto fail_fcf_read;
14633 }
14634 /* Construct the read FCF record mailbox command */
14635 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14636 if (rc) {
14637 error = -EINVAL;
14638 goto fail_fcf_read;
14639 }
14640 /* Issue the mailbox command asynchronously */
14641 mboxq->vport = phba->pport;
14642 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14643 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14644 if (rc == MBX_NOT_FINISHED)
14645 error = -EIO;
14646 else
14647 error = 0;
14648
14649fail_fcf_read:
14650 if (error && mboxq)
14651 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14652 return error;
14653}
14654
14655/**
14656 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14657 * @phba: pointer to lpfc hba data structure.
14658 * @fcf_index: FCF table entry offset.
14659 *
14660 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 14661 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 14662 *
25985edc 14663 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
14664 * otherwise.
14665 **/
14666int
14667lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14668{
14669 int rc = 0, error;
14670 LPFC_MBOXQ_t *mboxq;
14671
14672 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14673 if (!mboxq) {
14674 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14675 "2758 Failed to allocate mbox for "
14676 "READ_FCF cmd\n");
14677 error = -ENOMEM;
14678 goto fail_fcf_read;
14679 }
14680 /* Construct the read FCF record mailbox command */
14681 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14682 if (rc) {
14683 error = -EINVAL;
14684 goto fail_fcf_read;
14685 }
14686 /* Issue the mailbox command asynchronously */
14687 mboxq->vport = phba->pport;
14688 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14689 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14690 if (rc == MBX_NOT_FINISHED)
14691 error = -EIO;
14692 else
14693 error = 0;
14694
14695fail_fcf_read:
14696 if (error && mboxq)
14697 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14698 return error;
14699}
14700
7d791df7
JS
14701/**
14702 * lpfc_check_next_fcf_pri
14703 * phba pointer to the lpfc_hba struct for this port.
14704 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
14705 * routine when the rr_bmask is empty. The FCF indecies are put into the
14706 * rr_bmask based on their priority level. Starting from the highest priority
14707 * to the lowest. The most likely FCF candidate will be in the highest
14708 * priority group. When this routine is called it searches the fcf_pri list for
14709 * next lowest priority group and repopulates the rr_bmask with only those
14710 * fcf_indexes.
14711 * returns:
14712 * 1=success 0=failure
14713 **/
14714int
14715lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
14716{
14717 uint16_t next_fcf_pri;
14718 uint16_t last_index;
14719 struct lpfc_fcf_pri *fcf_pri;
14720 int rc;
14721 int ret = 0;
14722
14723 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
14724 LPFC_SLI4_FCF_TBL_INDX_MAX);
14725 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14726 "3060 Last IDX %d\n", last_index);
14727 if (list_empty(&phba->fcf.fcf_pri_list)) {
14728 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14729 "3061 Last IDX %d\n", last_index);
14730 return 0; /* Empty rr list */
14731 }
14732 next_fcf_pri = 0;
14733 /*
14734 * Clear the rr_bmask and set all of the bits that are at this
14735 * priority.
14736 */
14737 memset(phba->fcf.fcf_rr_bmask, 0,
14738 sizeof(*phba->fcf.fcf_rr_bmask));
14739 spin_lock_irq(&phba->hbalock);
14740 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14741 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
14742 continue;
14743 /*
14744 * the 1st priority that has not FLOGI failed
14745 * will be the highest.
14746 */
14747 if (!next_fcf_pri)
14748 next_fcf_pri = fcf_pri->fcf_rec.priority;
14749 spin_unlock_irq(&phba->hbalock);
14750 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14751 rc = lpfc_sli4_fcf_rr_index_set(phba,
14752 fcf_pri->fcf_rec.fcf_index);
14753 if (rc)
14754 return 0;
14755 }
14756 spin_lock_irq(&phba->hbalock);
14757 }
14758 /*
14759 * if next_fcf_pri was not set above and the list is not empty then
14760 * we have failed flogis on all of them. So reset flogi failed
14761 * and start at the begining.
14762 */
14763 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
14764 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14765 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
14766 /*
14767 * the 1st priority that has not FLOGI failed
14768 * will be the highest.
14769 */
14770 if (!next_fcf_pri)
14771 next_fcf_pri = fcf_pri->fcf_rec.priority;
14772 spin_unlock_irq(&phba->hbalock);
14773 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14774 rc = lpfc_sli4_fcf_rr_index_set(phba,
14775 fcf_pri->fcf_rec.fcf_index);
14776 if (rc)
14777 return 0;
14778 }
14779 spin_lock_irq(&phba->hbalock);
14780 }
14781 } else
14782 ret = 1;
14783 spin_unlock_irq(&phba->hbalock);
14784
14785 return ret;
14786}
0c9ab6f5
JS
14787/**
14788 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
14789 * @phba: pointer to lpfc hba data structure.
14790 *
14791 * This routine is to get the next eligible FCF record index in a round
14792 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 14793 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
14794 * shall be returned, otherwise, the next eligible FCF record's index
14795 * shall be returned.
14796 **/
14797uint16_t
14798lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
14799{
14800 uint16_t next_fcf_index;
14801
3804dc84 14802 /* Search start from next bit of currently registered FCF index */
7d791df7 14803next_priority:
3804dc84
JS
14804 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
14805 LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
14806 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14807 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
14808 next_fcf_index);
14809
0c9ab6f5 14810 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
14811 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14812 /*
14813 * If we have wrapped then we need to clear the bits that
14814 * have been tested so that we can detect when we should
14815 * change the priority level.
14816 */
0c9ab6f5
JS
14817 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14818 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
14819 }
14820
3804dc84
JS
14821
14822 /* Check roundrobin failover list empty condition */
7d791df7
JS
14823 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
14824 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
14825 /*
14826 * If next fcf index is not found check if there are lower
14827 * Priority level fcf's in the fcf_priority list.
14828 * Set up the rr_bmask with all of the avaiable fcf bits
14829 * at that level and continue the selection process.
14830 */
14831 if (lpfc_check_next_fcf_pri_level(phba))
14832 goto next_priority;
3804dc84
JS
14833 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14834 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
14835 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
14836 return LPFC_FCOE_FCF_NEXT_NONE;
14837 else {
14838 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14839 "3063 Only FCF available idx %d, flag %x\n",
14840 next_fcf_index,
14841 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
14842 return next_fcf_index;
14843 }
3804dc84
JS
14844 }
14845
7d791df7
JS
14846 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
14847 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
14848 LPFC_FCF_FLOGI_FAILED)
14849 goto next_priority;
14850
3804dc84 14851 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
14852 "2845 Get next roundrobin failover FCF (x%x)\n",
14853 next_fcf_index);
14854
0c9ab6f5
JS
14855 return next_fcf_index;
14856}
14857
14858/**
14859 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
14860 * @phba: pointer to lpfc hba data structure.
14861 *
14862 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 14863 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
14864 * does not go beyond the range of the driver allocated bmask dimension
14865 * before setting the bit.
14866 *
14867 * Returns 0 if the index bit successfully set, otherwise, it returns
14868 * -EINVAL.
14869 **/
14870int
14871lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
14872{
14873 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14874 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
14875 "2610 FCF (x%x) reached driver's book "
14876 "keeping dimension:x%x\n",
0c9ab6f5
JS
14877 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14878 return -EINVAL;
14879 }
14880 /* Set the eligible FCF record index bmask */
14881 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
14882
3804dc84 14883 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 14884 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
14885 "bmask\n", fcf_index);
14886
0c9ab6f5
JS
14887 return 0;
14888}
14889
14890/**
3804dc84 14891 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
14892 * @phba: pointer to lpfc hba data structure.
14893 *
14894 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 14895 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
14896 * does not go beyond the range of the driver allocated bmask dimension
14897 * before clearing the bit.
14898 **/
14899void
14900lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
14901{
7d791df7 14902 struct lpfc_fcf_pri *fcf_pri;
0c9ab6f5
JS
14903 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14904 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
14905 "2762 FCF (x%x) reached driver's book "
14906 "keeping dimension:x%x\n",
0c9ab6f5
JS
14907 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14908 return;
14909 }
14910 /* Clear the eligible FCF record index bmask */
7d791df7
JS
14911 spin_lock_irq(&phba->hbalock);
14912 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14913 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
14914 list_del_init(&fcf_pri->list);
14915 break;
14916 }
14917 }
14918 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 14919 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
14920
14921 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 14922 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 14923 "bmask\n", fcf_index);
0c9ab6f5
JS
14924}
14925
ecfd03c6
JS
14926/**
14927 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
14928 * @phba: pointer to lpfc hba data structure.
14929 *
14930 * This routine is the completion routine for the rediscover FCF table mailbox
14931 * command. If the mailbox command returned failure, it will try to stop the
14932 * FCF rediscover wait timer.
14933 **/
14934void
14935lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
14936{
14937 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14938 uint32_t shdr_status, shdr_add_status;
14939
14940 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14941
14942 shdr_status = bf_get(lpfc_mbox_hdr_status,
14943 &redisc_fcf->header.cfg_shdr.response);
14944 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
14945 &redisc_fcf->header.cfg_shdr.response);
14946 if (shdr_status || shdr_add_status) {
0c9ab6f5 14947 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
14948 "2746 Requesting for FCF rediscovery failed "
14949 "status x%x add_status x%x\n",
14950 shdr_status, shdr_add_status);
0c9ab6f5 14951 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 14952 spin_lock_irq(&phba->hbalock);
0c9ab6f5 14953 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
14954 spin_unlock_irq(&phba->hbalock);
14955 /*
14956 * CVL event triggered FCF rediscover request failed,
14957 * last resort to re-try current registered FCF entry.
14958 */
14959 lpfc_retry_pport_discovery(phba);
14960 } else {
14961 spin_lock_irq(&phba->hbalock);
0c9ab6f5 14962 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
14963 spin_unlock_irq(&phba->hbalock);
14964 /*
14965 * DEAD FCF event triggered FCF rediscover request
14966 * failed, last resort to fail over as a link down
14967 * to FCF registration.
14968 */
14969 lpfc_sli4_fcf_dead_failthrough(phba);
14970 }
0c9ab6f5
JS
14971 } else {
14972 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 14973 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
14974 /*
14975 * Start FCF rediscovery wait timer for pending FCF
14976 * before rescan FCF record table.
14977 */
14978 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 14979 }
ecfd03c6
JS
14980
14981 mempool_free(mbox, phba->mbox_mem_pool);
14982}
14983
14984/**
3804dc84 14985 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
14986 * @phba: pointer to lpfc hba data structure.
14987 *
14988 * This routine is invoked to request for rediscovery of the entire FCF table
14989 * by the port.
14990 **/
14991int
14992lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
14993{
14994 LPFC_MBOXQ_t *mbox;
14995 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14996 int rc, length;
14997
0c9ab6f5
JS
14998 /* Cancel retry delay timers to all vports before FCF rediscover */
14999 lpfc_cancel_all_vport_retry_delay_timer(phba);
15000
ecfd03c6
JS
15001 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15002 if (!mbox) {
15003 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15004 "2745 Failed to allocate mbox for "
15005 "requesting FCF rediscover.\n");
15006 return -ENOMEM;
15007 }
15008
15009 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15010 sizeof(struct lpfc_sli4_cfg_mhdr));
15011 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15012 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15013 length, LPFC_SLI4_MBX_EMBED);
15014
15015 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15016 /* Set count to 0 for invalidating the entire FCF database */
15017 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15018
15019 /* Issue the mailbox command asynchronously */
15020 mbox->vport = phba->pport;
15021 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15022 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15023
15024 if (rc == MBX_NOT_FINISHED) {
15025 mempool_free(mbox, phba->mbox_mem_pool);
15026 return -EIO;
15027 }
15028 return 0;
15029}
15030
fc2b989b
JS
15031/**
15032 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15033 * @phba: pointer to lpfc hba data structure.
15034 *
15035 * This function is the failover routine as a last resort to the FCF DEAD
15036 * event when driver failed to perform fast FCF failover.
15037 **/
15038void
15039lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15040{
15041 uint32_t link_state;
15042
15043 /*
15044 * Last resort as FCF DEAD event failover will treat this as
15045 * a link down, but save the link state because we don't want
15046 * it to be changed to Link Down unless it is already down.
15047 */
15048 link_state = phba->link_state;
15049 lpfc_linkdown(phba);
15050 phba->link_state = link_state;
15051
15052 /* Unregister FCF if no devices connected to it */
15053 lpfc_unregister_unused_fcf(phba);
15054}
15055
a0c87cbd
JS
15056/**
15057 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15058 * @phba: pointer to lpfc hba data structure.
15059 *
15060 * This function read region 23 and parse TLV for port status to
15061 * decide if the user disaled the port. If the TLV indicates the
15062 * port is disabled, the hba_flag is set accordingly.
15063 **/
15064void
15065lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15066{
15067 LPFC_MBOXQ_t *pmb = NULL;
15068 MAILBOX_t *mb;
15069 uint8_t *rgn23_data = NULL;
15070 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
15071 int rc;
15072
15073 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15074 if (!pmb) {
15075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15076 "2600 lpfc_sli_read_serdes_param failed to"
15077 " allocate mailbox memory\n");
15078 goto out;
15079 }
15080 mb = &pmb->u.mb;
15081
15082 /* Get adapter Region 23 data */
15083 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15084 if (!rgn23_data)
15085 goto out;
15086
15087 do {
15088 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15089 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15090
15091 if (rc != MBX_SUCCESS) {
15092 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15093 "2601 lpfc_sli_read_link_ste failed to"
15094 " read config region 23 rc 0x%x Status 0x%x\n",
15095 rc, mb->mbxStatus);
15096 mb->un.varDmp.word_cnt = 0;
15097 }
15098 /*
15099 * dump mem may return a zero when finished or we got a
15100 * mailbox error, either way we are done.
15101 */
15102 if (mb->un.varDmp.word_cnt == 0)
15103 break;
15104 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15105 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15106
15107 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15108 rgn23_data + offset,
15109 mb->un.varDmp.word_cnt);
15110 offset += mb->un.varDmp.word_cnt;
15111 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15112
15113 data_size = offset;
15114 offset = 0;
15115
15116 if (!data_size)
15117 goto out;
15118
15119 /* Check the region signature first */
15120 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15122 "2619 Config region 23 has bad signature\n");
15123 goto out;
15124 }
15125 offset += 4;
15126
15127 /* Check the data structure version */
15128 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15130 "2620 Config region 23 has bad version\n");
15131 goto out;
15132 }
15133 offset += 4;
15134
15135 /* Parse TLV entries in the region */
15136 while (offset < data_size) {
15137 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15138 break;
15139 /*
15140 * If the TLV is not driver specific TLV or driver id is
15141 * not linux driver id, skip the record.
15142 */
15143 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15144 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15145 (rgn23_data[offset + 3] != 0)) {
15146 offset += rgn23_data[offset + 1] * 4 + 4;
15147 continue;
15148 }
15149
15150 /* Driver found a driver specific TLV in the config region */
15151 sub_tlv_len = rgn23_data[offset + 1] * 4;
15152 offset += 4;
15153 tlv_offset = 0;
15154
15155 /*
15156 * Search for configured port state sub-TLV.
15157 */
15158 while ((offset < data_size) &&
15159 (tlv_offset < sub_tlv_len)) {
15160 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15161 offset += 4;
15162 tlv_offset += 4;
15163 break;
15164 }
15165 if (rgn23_data[offset] != PORT_STE_TYPE) {
15166 offset += rgn23_data[offset + 1] * 4 + 4;
15167 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15168 continue;
15169 }
15170
15171 /* This HBA contains PORT_STE configured */
15172 if (!rgn23_data[offset + 2])
15173 phba->hba_flag |= LINK_DISABLED;
15174
15175 goto out;
15176 }
15177 }
15178out:
15179 if (pmb)
15180 mempool_free(pmb, phba->mbox_mem_pool);
15181 kfree(rgn23_data);
15182 return;
15183}
695a814e 15184
52d52440
JS
15185/**
15186 * lpfc_wr_object - write an object to the firmware
15187 * @phba: HBA structure that indicates port to create a queue on.
15188 * @dmabuf_list: list of dmabufs to write to the port.
15189 * @size: the total byte value of the objects to write to the port.
15190 * @offset: the current offset to be used to start the transfer.
15191 *
15192 * This routine will create a wr_object mailbox command to send to the port.
15193 * the mailbox command will be constructed using the dma buffers described in
15194 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15195 * BDEs that the imbedded mailbox can support. The @offset variable will be
15196 * used to indicate the starting offset of the transfer and will also return
15197 * the offset after the write object mailbox has completed. @size is used to
15198 * determine the end of the object and whether the eof bit should be set.
15199 *
15200 * Return 0 is successful and offset will contain the the new offset to use
15201 * for the next write.
15202 * Return negative value for error cases.
15203 **/
15204int
15205lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15206 uint32_t size, uint32_t *offset)
15207{
15208 struct lpfc_mbx_wr_object *wr_object;
15209 LPFC_MBOXQ_t *mbox;
15210 int rc = 0, i = 0;
15211 uint32_t shdr_status, shdr_add_status;
15212 uint32_t mbox_tmo;
15213 union lpfc_sli4_cfg_shdr *shdr;
15214 struct lpfc_dmabuf *dmabuf;
15215 uint32_t written = 0;
15216
15217 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15218 if (!mbox)
15219 return -ENOMEM;
15220
15221 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15222 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15223 sizeof(struct lpfc_mbx_wr_object) -
15224 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15225
15226 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15227 wr_object->u.request.write_offset = *offset;
15228 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15229 wr_object->u.request.object_name[0] =
15230 cpu_to_le32(wr_object->u.request.object_name[0]);
15231 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15232 list_for_each_entry(dmabuf, dmabuf_list, list) {
15233 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15234 break;
15235 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15236 wr_object->u.request.bde[i].addrHigh =
15237 putPaddrHigh(dmabuf->phys);
15238 if (written + SLI4_PAGE_SIZE >= size) {
15239 wr_object->u.request.bde[i].tus.f.bdeSize =
15240 (size - written);
15241 written += (size - written);
15242 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15243 } else {
15244 wr_object->u.request.bde[i].tus.f.bdeSize =
15245 SLI4_PAGE_SIZE;
15246 written += SLI4_PAGE_SIZE;
15247 }
15248 i++;
15249 }
15250 wr_object->u.request.bde_count = i;
15251 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15252 if (!phba->sli4_hba.intr_enable)
15253 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15254 else {
a183a15f 15255 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
15256 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15257 }
15258 /* The IOCTL status is embedded in the mailbox subheader. */
15259 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15260 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15261 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15262 if (rc != MBX_TIMEOUT)
15263 mempool_free(mbox, phba->mbox_mem_pool);
15264 if (shdr_status || shdr_add_status || rc) {
15265 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15266 "3025 Write Object mailbox failed with "
15267 "status x%x add_status x%x, mbx status x%x\n",
15268 shdr_status, shdr_add_status, rc);
15269 rc = -ENXIO;
15270 } else
15271 *offset += wr_object->u.response.actual_write_length;
15272 return rc;
15273}
15274
695a814e
JS
15275/**
15276 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15277 * @vport: pointer to vport data structure.
15278 *
15279 * This function iterate through the mailboxq and clean up all REG_LOGIN
15280 * and REG_VPI mailbox commands associated with the vport. This function
15281 * is called when driver want to restart discovery of the vport due to
15282 * a Clear Virtual Link event.
15283 **/
15284void
15285lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15286{
15287 struct lpfc_hba *phba = vport->phba;
15288 LPFC_MBOXQ_t *mb, *nextmb;
15289 struct lpfc_dmabuf *mp;
78730cfe 15290 struct lpfc_nodelist *ndlp;
d439d286 15291 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 15292 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 15293 LIST_HEAD(mbox_cmd_list);
63e801ce 15294 uint8_t restart_loop;
695a814e 15295
d439d286 15296 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
15297 spin_lock_irq(&phba->hbalock);
15298 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15299 if (mb->vport != vport)
15300 continue;
15301
15302 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15303 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15304 continue;
15305
d439d286
JS
15306 list_del(&mb->list);
15307 list_add_tail(&mb->list, &mbox_cmd_list);
15308 }
15309 /* Clean up active mailbox command with the vport */
15310 mb = phba->sli.mbox_active;
15311 if (mb && (mb->vport == vport)) {
15312 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15313 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15314 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15315 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15316 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15317 /* Put reference count for delayed processing */
15318 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15319 /* Unregister the RPI when mailbox complete */
15320 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15321 }
15322 }
63e801ce
JS
15323 /* Cleanup any mailbox completions which are not yet processed */
15324 do {
15325 restart_loop = 0;
15326 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15327 /*
15328 * If this mailox is already processed or it is
15329 * for another vport ignore it.
15330 */
15331 if ((mb->vport != vport) ||
15332 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15333 continue;
15334
15335 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15336 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15337 continue;
15338
15339 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15340 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15341 ndlp = (struct lpfc_nodelist *)mb->context2;
15342 /* Unregister the RPI when mailbox complete */
15343 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15344 restart_loop = 1;
15345 spin_unlock_irq(&phba->hbalock);
15346 spin_lock(shost->host_lock);
15347 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15348 spin_unlock(shost->host_lock);
15349 spin_lock_irq(&phba->hbalock);
15350 break;
15351 }
15352 }
15353 } while (restart_loop);
15354
d439d286
JS
15355 spin_unlock_irq(&phba->hbalock);
15356
15357 /* Release the cleaned-up mailbox commands */
15358 while (!list_empty(&mbox_cmd_list)) {
15359 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
15360 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15361 mp = (struct lpfc_dmabuf *) (mb->context1);
15362 if (mp) {
15363 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15364 kfree(mp);
15365 }
78730cfe 15366 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 15367 mb->context2 = NULL;
78730cfe 15368 if (ndlp) {
ec21b3b0 15369 spin_lock(shost->host_lock);
589a52d6 15370 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 15371 spin_unlock(shost->host_lock);
78730cfe 15372 lpfc_nlp_put(ndlp);
78730cfe 15373 }
695a814e 15374 }
695a814e
JS
15375 mempool_free(mb, phba->mbox_mem_pool);
15376 }
d439d286
JS
15377
15378 /* Release the ndlp with the cleaned-up active mailbox command */
15379 if (act_mbx_ndlp) {
15380 spin_lock(shost->host_lock);
15381 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15382 spin_unlock(shost->host_lock);
15383 lpfc_nlp_put(act_mbx_ndlp);
695a814e 15384 }
695a814e
JS
15385}
15386
2a9bf3d0
JS
15387/**
15388 * lpfc_drain_txq - Drain the txq
15389 * @phba: Pointer to HBA context object.
15390 *
15391 * This function attempt to submit IOCBs on the txq
15392 * to the adapter. For SLI4 adapters, the txq contains
15393 * ELS IOCBs that have been deferred because the there
15394 * are no SGLs. This congestion can occur with large
15395 * vport counts during node discovery.
15396 **/
15397
15398uint32_t
15399lpfc_drain_txq(struct lpfc_hba *phba)
15400{
15401 LIST_HEAD(completions);
15402 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15403 struct lpfc_iocbq *piocbq = 0;
15404 unsigned long iflags = 0;
15405 char *fail_msg = NULL;
15406 struct lpfc_sglq *sglq;
15407 union lpfc_wqe wqe;
15408
15409 spin_lock_irqsave(&phba->hbalock, iflags);
15410 if (pring->txq_cnt > pring->txq_max)
15411 pring->txq_max = pring->txq_cnt;
15412
15413 spin_unlock_irqrestore(&phba->hbalock, iflags);
15414
15415 while (pring->txq_cnt) {
15416 spin_lock_irqsave(&phba->hbalock, iflags);
15417
19ca7609
JS
15418 piocbq = lpfc_sli_ringtx_get(phba, pring);
15419 sglq = __lpfc_sli_get_sglq(phba, piocbq);
2a9bf3d0 15420 if (!sglq) {
19ca7609 15421 __lpfc_sli_ringtx_put(phba, pring, piocbq);
2a9bf3d0
JS
15422 spin_unlock_irqrestore(&phba->hbalock, iflags);
15423 break;
15424 } else {
2a9bf3d0
JS
15425 if (!piocbq) {
15426 /* The txq_cnt out of sync. This should
15427 * never happen
15428 */
15429 sglq = __lpfc_clear_active_sglq(phba,
6d368e53 15430 sglq->sli4_lxritag);
2a9bf3d0
JS
15431 spin_unlock_irqrestore(&phba->hbalock, iflags);
15432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15433 "2823 txq empty and txq_cnt is %d\n ",
15434 pring->txq_cnt);
15435 break;
15436 }
15437 }
15438
15439 /* The xri and iocb resources secured,
15440 * attempt to issue request
15441 */
6d368e53 15442 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
15443 piocbq->sli4_xritag = sglq->sli4_xritag;
15444 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15445 fail_msg = "to convert bpl to sgl";
15446 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15447 fail_msg = "to convert iocb to wqe";
15448 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15449 fail_msg = " - Wq is full";
15450 else
15451 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15452
15453 if (fail_msg) {
15454 /* Failed means we can't issue and need to cancel */
15455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15456 "2822 IOCB failed %s iotag 0x%x "
15457 "xri 0x%x\n",
15458 fail_msg,
15459 piocbq->iotag, piocbq->sli4_xritag);
15460 list_add_tail(&piocbq->list, &completions);
15461 }
15462 spin_unlock_irqrestore(&phba->hbalock, iflags);
15463 }
15464
2a9bf3d0
JS
15465 /* Cancel all the IOCBs that cannot be issued */
15466 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15467 IOERR_SLI_ABORTED);
15468
15469 return pring->txq_cnt;
15470}